Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
Fido2Applet
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Josh Ji
Fido2Applet
Commits
ba3ad98f
Commit
ba3ad98f
authored
Apr 13, 2022
by
Josh Ji
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Client Pin not finished
parent
d270c116
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
425 additions
and
138 deletions
+425
-138
.idea/codeStyles/codeStyleConfig.xml
.idea/codeStyles/codeStyleConfig.xml
+5
-0
src/main/java/com/josh/vku2f/COSEKey.java
src/main/java/com/josh/vku2f/COSEKey.java
+18
-0
src/main/java/com/josh/vku2f/CTAP2.java
src/main/java/com/josh/vku2f/CTAP2.java
+117
-138
src/main/java/com/josh/vku2f/ClientPIN.java
src/main/java/com/josh/vku2f/ClientPIN.java
+28
-0
src/main/java/com/josh/vku2f/ClientPINCommand.java
src/main/java/com/josh/vku2f/ClientPINCommand.java
+100
-0
src/main/java/com/josh/vku2f/ClientPINResponse.java
src/main/java/com/josh/vku2f/ClientPINResponse.java
+9
-0
src/main/java/com/josh/vku2f/ClientPINSubCommand.java
src/main/java/com/josh/vku2f/ClientPINSubCommand.java
+13
-0
src/main/java/com/josh/vku2f/PinUvAuthProtocol.java
src/main/java/com/josh/vku2f/PinUvAuthProtocol.java
+14
-0
src/main/java/com/josh/vku2f/PinUvAuthProtocolOne.java
src/main/java/com/josh/vku2f/PinUvAuthProtocolOne.java
+86
-0
src/main/java/com/josh/vku2f/PinUvAuthToken.java
src/main/java/com/josh/vku2f/PinUvAuthToken.java
+35
-0
No files found.
.idea/codeStyles/codeStyleConfig.xml
0 → 100644
View file @
ba3ad98f
<component
name=
"ProjectCodeStyleConfiguration"
>
<state>
<option
name=
"PREFERRED_PROJECT_CODE_STYLE"
value=
"Default"
/>
</state>
</component>
\ No newline at end of file
src/main/java/com/josh/vku2f/COSEKey.java
0 → 100644
View file @
ba3ad98f
package
com.josh.vku2f
;
public
class
COSEKey
{
private
byte
[]
w
;
COSEKey
(){
w
=
new
byte
[
64
];
}
public
void
setW
(
byte
[]
w
){
this
.
w
=
w
;
}
public
byte
[]
getW
(){
return
w
;
}
public
void
encode
(
CBOREncoder
cborEncoder
){
}
}
src/main/java/com/josh/vku2f/CTAP2.java
View file @
ba3ad98f
...
...
@@ -24,22 +24,17 @@ import javacard.framework.ISOException;
import
javacard.framework.JCSystem
;
import
javacard.framework.UserException
;
import
javacard.framework.Util
;
import
javacard.security.ECKey
;
import
javacard.security.ECPrivateKey
;
import
javacard.security.ECPublicKey
;
import
javacard.security.KeyBuilder
;
import
javacard.security.KeyPair
;
import
javacard.security.MessageDigest
;
import
javacard.security.Signature
;
import
javacard.security.*
;
import
javacardx.apdu.ExtendedLength
;
import
static
com
.
josh
.
vku2f
.
CTAP2ErrorCode
.*;
import
static
com
.
josh
.
vku2f
.
ClientPINSubCommand
.*;
public
class
CTAP2
extends
Applet
implements
ExtendedLength
{
private
final
CBORDecoder
cborDecoder
;
private
final
CBOREncoder
cborEncoder
;
private
byte
[]
input
Buffer
;
private
byte
[]
data
Buffer
;
private
byte
[]
scratch
;
private
byte
[]
info
;
...
...
@@ -52,11 +47,17 @@ public class CTAP2 extends Applet implements ExtendedLength {
private
final
AttestationKeyPair
attestationKeyPair
;
private
CredentialArray
credentialArray
;
private
AuthenticatorGetAssertion
authenticatorGetAssertion
;
private
AuthenticatorMakeCredential
authenticatorMakeCredential
;
private
final
KeyPair
ecDhKeyPair
;
private
final
boolean
[]
ecDhSet
;
private
AuthenticatorGetAssertion
authenticatorGetAssertion
;
private
final
ClientPINCommand
clientPINCommand
;
private
byte
MAX_PIN_RETRIES
=
(
byte
)
0x08
;
private
byte
MAX_UV_RETRIES
=
(
byte
)
0x08
;
private
byte
pinRetries
;
private
byte
uvRetries
;
// private final KeyPair ecDhKeyPair;
// private final boolean[] ecDhSet;
private
PinUvAuthProtocolOne
pinUvAuthProtocolOne
;
private
final
boolean
[]
isChaining
;
private
final
boolean
[]
isOutChaining
;
private
boolean
personalizeComplete
;
...
...
@@ -96,9 +97,9 @@ public class CTAP2 extends Applet implements ExtendedLength {
// 1210 bytes of a transient buffer for read-in and out
// We advertise 1200 bytes supported, but 10 bytes for protocol nonsense
try
{
input
Buffer
=
JCSystem
.
makeTransientByteArray
((
short
)
1210
,
JCSystem
.
CLEAR_ON_DESELECT
);
data
Buffer
=
JCSystem
.
makeTransientByteArray
((
short
)
1210
,
JCSystem
.
CLEAR_ON_DESELECT
);
}
catch
(
Exception
e
)
{
input
Buffer
=
new
byte
[
1210
];
data
Buffer
=
new
byte
[
1210
];
}
try
{
scratch
=
JCSystem
.
makeTransientByteArray
((
short
)
512
,
JCSystem
.
CLEAR_ON_DESELECT
);
...
...
@@ -122,8 +123,9 @@ public class CTAP2 extends Applet implements ExtendedLength {
JCSystem
.
MEMORY_TYPE_TRANSIENT_RESET
,
KeyBuilder
.
LENGTH_EC_FP_256
,
false
);
ECPrivateKey
ecDhPriv
=
(
ECPrivateKey
)
KeyBuilder
.
buildKey
(
KeyBuilder
.
ALG_TYPE_EC_FP_PRIVATE
,
JCSystem
.
MEMORY_TYPE_TRANSIENT_RESET
,
KeyBuilder
.
LENGTH_EC_FP_256
,
false
);
ecDhKeyPair
=
new
KeyPair
(
ecDhPub
,
ecDhPriv
);
ecDhSet
=
JCSystem
.
makeTransientBooleanArray
((
short
)
1
,
JCSystem
.
CLEAR_ON_RESET
);
clientPINCommand
=
new
ClientPINCommand
();
// ecDhKeyPair = new KeyPair(ecDhPub, ecDhPriv);
// ecDhSet = JCSystem.makeTransientBooleanArray((short) 1, JCSystem.CLEAR_ON_RESET);
}
...
...
@@ -139,7 +141,7 @@ public class CTAP2 extends Applet implements ExtendedLength {
return
;
}
// Need to grab the CTAP command byte
switch
(
input
Buffer
[
0
])
{
switch
(
data
Buffer
[
0
])
{
case
FIDO2_AUTHENTICATOR_MAKE_CREDENTIAL:
authMakeCredential
(
apdu
,
tempVars
[
3
]);
break
;
...
...
@@ -202,11 +204,11 @@ public class CTAP2 extends Applet implements ExtendedLength {
returnError
(
apdu
,
CTAP1_ERR_INVALID_COMMAND
);
return
;
}
input
Buffer
[
0
]
=
0x00
;
tempVars
[
0
]
=
(
short
)
(
attestationKeyPair
.
getPubkey
(
input
Buffer
,
(
short
)
1
)
+
1
);
data
Buffer
[
0
]
=
0x00
;
tempVars
[
0
]
=
(
short
)
(
attestationKeyPair
.
getPubkey
(
data
Buffer
,
(
short
)
1
)
+
1
);
apdu
.
setOutgoing
();
apdu
.
setOutgoingLength
(
tempVars
[
0
]);
apdu
.
sendBytesLong
(
input
Buffer
,
(
short
)
0
,
tempVars
[
0
]);
apdu
.
sendBytesLong
(
data
Buffer
,
(
short
)
0
,
tempVars
[
0
]);
}
/** get counter's value */
...
...
@@ -214,8 +216,8 @@ public class CTAP2 extends Applet implements ExtendedLength {
short
count
=
credentialArray
.
getCount
();
apdu
.
setOutgoing
();
apdu
.
setOutgoingLength
((
short
)
2
);
Util
.
setShort
(
input
Buffer
,(
short
)
0
,
count
);
apdu
.
sendBytesLong
(
input
Buffer
,(
short
)
0
,(
short
)
2
);
Util
.
setShort
(
data
Buffer
,(
short
)
0
,
count
);
apdu
.
sendBytesLong
(
data
Buffer
,(
short
)
0
,(
short
)
2
);
}
/**
...
...
@@ -229,12 +231,12 @@ public class CTAP2 extends Applet implements ExtendedLength {
returnError
(
apdu
,
CTAP1_ERR_INVALID_COMMAND
);
return
;
}
Util
.
arrayCopy
(
input
Buffer
,
(
short
)
1
,
scratch
,
(
short
)
0
,
(
short
)
(
bufLen
-
1
));
input
Buffer
[
0
]
=
0x00
;
tempVars
[
2
]
=
attestationKeyPair
.
sign
(
scratch
,
(
short
)
0
,
tempVars
[
1
],
input
Buffer
,
(
short
)
1
);
Util
.
arrayCopy
(
data
Buffer
,
(
short
)
1
,
scratch
,
(
short
)
0
,
(
short
)
(
bufLen
-
1
));
data
Buffer
[
0
]
=
0x00
;
tempVars
[
2
]
=
attestationKeyPair
.
sign
(
scratch
,
(
short
)
0
,
tempVars
[
1
],
data
Buffer
,
(
short
)
1
);
apdu
.
setOutgoing
();
apdu
.
setOutgoingLength
((
short
)
(
tempVars
[
2
]
+
1
));
apdu
.
sendBytesLong
(
input
Buffer
,
(
short
)
0
,
(
short
)
(
tempVars
[
2
]
+
1
));
apdu
.
sendBytesLong
(
data
Buffer
,
(
short
)
0
,
(
short
)
(
tempVars
[
2
]
+
1
));
}
public
void
attestSetCert
(
APDU
apdu
,
short
bufLen
)
{
...
...
@@ -243,19 +245,19 @@ public class CTAP2 extends Applet implements ExtendedLength {
return
;
}
// We don't actually use any CBOR here, simplify copying
attestationKeyPair
.
setCert
(
input
Buffer
,
(
short
)
1
,
(
short
)
(
bufLen
-
1
));
attestationKeyPair
.
setCert
(
data
Buffer
,
(
short
)
1
,
(
short
)
(
bufLen
-
1
));
MessageDigest
dig
=
MessageDigest
.
getInstance
(
MessageDigest
.
ALG_SHA_256
,
false
);
short
len
=
(
short
)
(
dig
.
doFinal
(
attestationKeyPair
.
x509cert
,
(
short
)
0
,
attestationKeyPair
.
x509len
,
input
Buffer
,
(
short
)
3
)
+
3
);
input
Buffer
[
0
]
=
0x00
;
Util
.
setShort
(
input
Buffer
,
(
short
)
1
,
attestationKeyPair
.
x509len
);
short
len
=
(
short
)
(
dig
.
doFinal
(
attestationKeyPair
.
x509cert
,
(
short
)
0
,
attestationKeyPair
.
x509len
,
data
Buffer
,
(
short
)
3
)
+
3
);
data
Buffer
[
0
]
=
0x00
;
Util
.
setShort
(
data
Buffer
,
(
short
)
1
,
attestationKeyPair
.
x509len
);
apdu
.
setOutgoing
();
apdu
.
setOutgoingLength
(
len
);
apdu
.
sendBytesLong
(
input
Buffer
,
(
short
)
0
,
len
);
apdu
.
sendBytesLong
(
data
Buffer
,
(
short
)
0
,
len
);
}
public
void
authMakeCredential
(
APDU
apdu
,
short
bufLen
)
{
// Init the decoder
cborDecoder
.
init
(
input
Buffer
,
(
short
)
1
,
bufLen
);
cborDecoder
.
init
(
data
Buffer
,
(
short
)
1
,
bufLen
);
// create a credential object
try
{
authenticatorMakeCredential
=
new
AuthenticatorMakeCredential
(
cborDecoder
);
...
...
@@ -293,10 +295,10 @@ public class CTAP2 extends Applet implements ExtendedLength {
// Initialise the output buffer, for CBOR writing.
// output buffer needs 0x00 as first byte as status code
input
Buffer
[
0
]
=
0x00
;
cborEncoder
.
init
(
input
Buffer
,
(
short
)
1
,
(
short
)
1199
);
data
Buffer
[
0
]
=
0x00
;
cborEncoder
.
init
(
data
Buffer
,
(
short
)
1
,
(
short
)
1199
);
// Create a map in the buffer
tempVars
[
0
]
=
cborEncoder
.
startMap
((
short
)
3
);
tempVars
[
0
]
=
cborEncoder
.
startMap
((
short
)
3
);
// current offset
// Attestation stuff
cborEncoder
.
writeRawByte
((
byte
)
0x01
);
...
...
@@ -310,14 +312,14 @@ public class CTAP2 extends Applet implements ExtendedLength {
tempVars
[
7
]
=
tempVars
[
0
];
// Create the SHA256 hash of the RP ID
tempCredential
.
rpEntity
.
getRp
(
scratch
,
(
short
)
0
);
tempVars
[
0
]
+=
sha256MessageDigest
.
doFinal
(
scratch
,
(
short
)
0
,
tempCredential
.
rpEntity
.
getRpLen
(),
input
Buffer
,
tempVars
[
0
]);
tempVars
[
0
]
+=
sha256MessageDigest
.
doFinal
(
scratch
,
(
short
)
0
,
tempCredential
.
rpEntity
.
getRpLen
(),
data
Buffer
,
tempVars
[
0
]);
// Set flags - User presence, user verified, attestation present
input
Buffer
[
tempVars
[
0
]++]
=
(
byte
)
0x45
;
data
Buffer
[
tempVars
[
0
]++]
=
(
byte
)
0x45
;
// Set the signature counter
tempVars
[
0
]
+=
tempCredential
.
readCounter
(
input
Buffer
,
tempVars
[
0
]);
tempVars
[
0
]
+=
tempCredential
.
readCounter
(
data
Buffer
,
tempVars
[
0
]);
// Read the credential details in
// Just note down where this starts for future ref
tempVars
[
0
]
+=
tempCredential
.
getAttestedData
(
input
Buffer
,
tempVars
[
0
]);
tempVars
[
0
]
+=
tempCredential
.
getAttestedData
(
data
Buffer
,
tempVars
[
0
]);
// Generate and then attach the attestation
cborEncoder
.
writeRawByte
((
byte
)
0x03
);
...
...
@@ -337,7 +339,7 @@ public class CTAP2 extends Applet implements ExtendedLength {
// We sign over the client data hash and the attested data.
// AuthenticatorData is first. We noted down where it begins and know how long
// it is.
attestationKeyPair
.
update
(
input
Buffer
,
tempVars
[
7
],
(
short
)
(
tempCredential
.
getAttestedLen
()
+
37
));
attestationKeyPair
.
update
(
data
Buffer
,
tempVars
[
7
],
(
short
)
(
tempCredential
.
getAttestedLen
()
+
37
));
// The client data hash is next, which we use to finish off the signature.
tempVars
[
4
]
=
attestationKeyPair
.
sign
(
authenticatorMakeCredential
.
dataHash
,
(
short
)
0
,
(
short
)
authenticatorMakeCredential
.
dataHash
.
length
,
scratch
,
(
short
)
0
);
// Create the byte string for the signature
...
...
@@ -361,7 +363,7 @@ public class CTAP2 extends Applet implements ExtendedLength {
public
void
authGetAssertion
(
APDU
apdu
,
short
bufLen
)
{
nextAssertion
[
0
]
=
(
short
)
0
;
// Decode the CBOR array for the assertion
cborDecoder
.
init
(
input
Buffer
,
(
short
)
1
,
bufLen
);
cborDecoder
.
init
(
data
Buffer
,
(
short
)
1
,
bufLen
);
try
{
authenticatorGetAssertion
=
new
AuthenticatorGetAssertion
(
cborDecoder
);
}
catch
(
UserException
e
)
{
...
...
@@ -391,9 +393,9 @@ public class CTAP2 extends Applet implements ExtendedLength {
// Create the output
// Status flags first
input
Buffer
[
0
]
=
0x00
;
data
Buffer
[
0
]
=
0x00
;
// Create the encoder
cborEncoder
.
init
(
input
Buffer
,
(
short
)
1
,
(
short
)
1199
);
cborEncoder
.
init
(
data
Buffer
,
(
short
)
1
,
(
short
)
1199
);
// Determine if we need 4 or 5 in the array
if
(
assertionCredentials
.
length
>
1
)
{
doAssertionCommon
(
cborEncoder
,
(
short
)
5
);
...
...
@@ -425,11 +427,11 @@ public class CTAP2 extends Applet implements ExtendedLength {
// Copy the hash in
authenticatorGetAssertion
.
getHash
(
scratch
,
(
short
)
37
);
// Create the output
String
a
=
"adfadsfa"
;
// Status flags first
input
Buffer
[
0
]
=
0x00
;
data
Buffer
[
0
]
=
0x00
;
// Create the encoder
cborEncoder
.
init
(
input
Buffer
,
(
short
)
1
,
(
short
)
1199
);
cborEncoder
.
init
(
data
Buffer
,
(
short
)
1
,
(
short
)
1199
);
doAssertionCommon
(
cborEncoder
,
(
short
)
4
);
nextAssertion
[
0
]++;
...
...
@@ -440,35 +442,64 @@ public class CTAP2 extends Applet implements ExtendedLength {
// Process the AuthenticatorClientPin feature
// Note: we only implement the keyAgreement bit
public
void
clientPin
(
APDU
apdu
,
short
buf
Len
)
{
public
void
clientPin
(
APDU
apdu
,
short
buf
ferLength
)
{
try
{
cborDecoder
.
init
(
inputBuffer
,
(
short
)
1
,
bufLen
);
cborDecoder
.
init
(
dataBuffer
,
(
short
)
1
,
bufferLength
);
// Start reading
cborDecoder
.
readMajorType
(
CBORBase
.
TYPE_MAP
);
// Read pinUvAuthProtocol
if
(
cborDecoder
.
readInt8
()
!=
(
byte
)
0x01
)
{
UserException
.
throwIt
(
CTAP2_ERR_INVALID_CBOR
);
return
;
}
// Read subCommand
if
(
cborDecoder
.
readInt8
()
!=
(
byte
)
0x01
)
{
UserException
.
throwIt
(
CTAP2_ERR_INVALID_CBOR
);
return
;
}
// Subcommand now
if
(
cborDecoder
.
readInt8
()
!=
(
byte
)
0x02
)
{
UserException
.
throwIt
(
CTAP2_ERR_INVALID_CBOR
);
return
;
}
// Actual subcommand
switch
(
cborDecoder
.
readInt8
())
{
case
0x02
:
// Seems to be a Diffie-Hellman thing
generateDH
(
apdu
);
clientPINCommand
.
decodeCommand
(
cborDecoder
);
switch
(
clientPINCommand
.
getSubCommandCode
()){
case
SUBCOMMAND_GET_PIN_RETRIES:
dataBuffer
[
0
]
=
0x00
;
cborEncoder
.
init
(
dataBuffer
,
(
short
)
1
,
(
short
)(
1199
));
cborEncoder
.
startMap
(
ClientPINResponse
.
PIN_RETRIES
);
cborEncoder
.
encodeUInt8
(
pinRetries
);
break
;
case
SUBCOMMAND_GET_KEY_AGREEMENT:
dataBuffer
[
0
]
=
0x00
;
cborEncoder
.
init
(
dataBuffer
,
(
short
)
1
,
(
short
)
1199
);
// Start a map
cborEncoder
.
startMap
((
short
)
1
);
// Encode the COSE key identifier
cborEncoder
.
encodeUInt8
((
byte
)
0x01
);
// Start the COSE map
cborEncoder
.
startMap
((
short
)
5
);
// Kty tag
cborEncoder
.
encodeUInt8
((
byte
)
0x01
);
// Kty value - EC2
cborEncoder
.
encodeUInt8
((
byte
)
0x02
);
// Alg tag
cborEncoder
.
encodeUInt8
((
byte
)
0x03
);
// Alg value - ES256 (-7, 6 in negative format)
cborEncoder
.
encodeNegativeUInt8
((
byte
)
0x06
);
// Crv tag - negative
cborEncoder
.
encodeNegativeUInt8
((
byte
)
0x00
);
// Crv value - P-256
cborEncoder
.
encodeUInt8
((
byte
)
0x01
);
// X-coord tag
cborEncoder
.
encodeNegativeUInt8
((
byte
)
0x01
);
// X-coord value
cborEncoder
.
encodeByteString
(
pinUvAuthProtocolOne
.
getPublicKey
(),
(
short
)
1
,
(
short
)
32
);
// Y-coord tag
cborEncoder
.
encodeNegativeUInt8
((
byte
)
0x02
);
// Y-coord value
cborEncoder
.
encodeByteString
(
pinUvAuthProtocolOne
.
getPublicKey
(),
(
short
)
33
,
(
short
)
32
);
break
;
case
SUBCOMMAND_SET_PIN:
break
;
case
SUBCOMMAND_CHANGE_PIN:
break
;
case
SUBCOMMAND_GET_PIN_TOKEN:
break
;
case
SUBCOMMAND_GET_PIN_UV_AUTH_TOKEN_UV:
break
;
case
SUBCOMMAND_GET_UV_RETRIES:
break
;
case
SUBCOMMAND_GET_PIN_UV_AUTH_TOKEN_PIN:
break
;
default
:
UserException
.
throwIt
(
CTAP2_ERR_UNSUPPORTED_OPTION
);
}
// That's it
sendLongChaining
(
apdu
,
cborEncoder
.
getCurrentOffset
());
}
catch
(
UserException
e
)
{
returnError
(
apdu
,
e
.
getReason
());
}
...
...
@@ -483,58 +514,6 @@ public class CTAP2 extends Applet implements ExtendedLength {
}
}
// Generate a session-specific ECDH P-256 key for Diffie-Hellman with the
// platform (Used for PIN ,but we only ever do it for hmac-secret)
private
void
generateDH
(
APDU
apdu
)
{
byte
[]
w
;
try
{
w
=
JCSystem
.
makeTransientByteArray
((
short
)
65
,
JCSystem
.
CLEAR_ON_RESET
);
}
catch
(
Exception
e
)
{
w
=
new
byte
[
65
];
}
if
(!
ecDhSet
[
0
])
{
// Grab the public key and set its parameters
KeyParams
.
sec256r1params
((
ECKey
)
ecDhKeyPair
.
getPublic
());
// Generate a new key-pair
ecDhKeyPair
.
genKeyPair
();
}
((
ECPublicKey
)
ecDhKeyPair
.
getPublic
()).
getW
(
w
,
(
short
)
0
);
// Return the data requested
inputBuffer
[
0
]
=
0x00
;
cborEncoder
.
init
(
inputBuffer
,
(
short
)
1
,
(
short
)
1199
);
// Start a map
cborEncoder
.
startMap
((
short
)
1
);
// Encode the COSE key identifier
cborEncoder
.
encodeUInt8
((
byte
)
0x01
);
// Start the COSE map
cborEncoder
.
startMap
((
short
)
5
);
// Kty tag
cborEncoder
.
encodeUInt8
((
byte
)
0x01
);
// Kty value - EC2
cborEncoder
.
encodeUInt8
((
byte
)
0x02
);
// Alg tag
cborEncoder
.
encodeUInt8
((
byte
)
0x03
);
// Alg value - ES256 (-7, 6 in negative format)
cborEncoder
.
encodeNegativeUInt8
((
byte
)
0x06
);
// Crv tag - negative
cborEncoder
.
encodeNegativeUInt8
((
byte
)
0x00
);
// Crv value - P-256
cborEncoder
.
encodeUInt8
((
byte
)
0x01
);
// X-coord tag
cborEncoder
.
encodeNegativeUInt8
((
byte
)
0x01
);
// X-coord value
cborEncoder
.
encodeByteString
(
w
,
(
short
)
1
,
(
short
)
32
);
// Y-coord tag
cborEncoder
.
encodeNegativeUInt8
((
byte
)
0x02
);
// Y-coord value
cborEncoder
.
encodeByteString
(
w
,
(
short
)
33
,
(
short
)
32
);
// That's it
sendLongChaining
(
apdu
,
cborEncoder
.
getCurrentOffset
());
}
/**
* Finds all credentials scoped to the RpId, and optionally the allowList, in
...
...
@@ -669,8 +648,8 @@ public class CTAP2 extends Applet implements ExtendedLength {
// Create the authenticator info if not present.
if
(
info
==
null
)
{
// Create the authGetInfo - 0x00 is success
input
Buffer
[
0
]
=
0x00
;
cborEncoder
.
init
(
input
Buffer
,
(
short
)
1
,
(
short
)
1199
);
data
Buffer
[
0
]
=
0x00
;
cborEncoder
.
init
(
data
Buffer
,
(
short
)
1
,
(
short
)
1199
);
cborEncoder
.
startMap
((
short
)
4
);
// 0x01, versions
cborEncoder
.
encodeUInt8
((
byte
)
0x01
);
...
...
@@ -700,11 +679,11 @@ public class CTAP2 extends Applet implements ExtendedLength {
// Done
JCSystem
.
beginTransaction
();
info
=
new
byte
[
cborEncoder
.
getCurrentOffset
()];
Util
.
arrayCopy
(
input
Buffer
,
(
short
)
0
,
info
,
(
short
)
0
,
cborEncoder
.
getCurrentOffset
());
Util
.
arrayCopy
(
data
Buffer
,
(
short
)
0
,
info
,
(
short
)
0
,
cborEncoder
.
getCurrentOffset
());
JCSystem
.
commitTransaction
();
}
// Send it
Util
.
arrayCopyNonAtomic
(
info
,
(
short
)
0
,
input
Buffer
,
(
short
)
0
,
(
short
)
info
.
length
);
Util
.
arrayCopyNonAtomic
(
info
,
(
short
)
0
,
data
Buffer
,
(
short
)
0
,
(
short
)
info
.
length
);
sendLongChaining
(
apdu
,
(
short
)
info
.
length
);
}
...
...
@@ -845,22 +824,22 @@ public class CTAP2 extends Applet implements ExtendedLength {
// Copy buffer
chainRam
[
1
]
=
tempVars
[
4
];
// chainRam[0] is the current point in the buffer we start from
chainRam
[
0
]
=
Util
.
arrayCopyNonAtomic
(
buffer
,
apdu
.
getOffsetCdata
(),
input
Buffer
,
chainRam
[
0
],
chainRam
[
1
]);
chainRam
[
0
]
=
Util
.
arrayCopyNonAtomic
(
buffer
,
apdu
.
getOffsetCdata
(),
data
Buffer
,
chainRam
[
0
],
chainRam
[
1
]);
return
0x00
;
}
else
if
(
isChaining
[
0
])
{
// Must be the last of the chaining - make the copy and return the length.
chainRam
[
1
]
=
tempVars
[
4
];
chainRam
[
0
]
=
Util
.
arrayCopyNonAtomic
(
buffer
,
apdu
.
getOffsetCdata
(),
input
Buffer
,
chainRam
[
0
],
chainRam
[
1
]);
chainRam
[
0
]
=
Util
.
arrayCopyNonAtomic
(
buffer
,
apdu
.
getOffsetCdata
(),
data
Buffer
,
chainRam
[
0
],
chainRam
[
1
]);
isChaining
[
0
]
=
false
;
isChaining
[
1
]
=
true
;
return
chainRam
[
0
];
}
else
if
(
tempVars
[
3
]
==
0x01
)
{
input
Buffer
[
0
]
=
buffer
[
apdu
.
getOffsetCdata
()];
data
Buffer
[
0
]
=
buffer
[
apdu
.
getOffsetCdata
()];
return
0x01
;
}
else
if
(
apdu
.
getCurrentState
()
==
APDU
.
STATE_FULL_INCOMING
)
{
// We need to do no more
// Read the entirety of the buffer into the inBuf
Util
.
arrayCopyNonAtomic
(
buffer
,
apdu
.
getOffsetCdata
(),
input
Buffer
,
(
short
)
0
,
tempVars
[
3
]);
Util
.
arrayCopyNonAtomic
(
buffer
,
apdu
.
getOffsetCdata
(),
data
Buffer
,
(
short
)
0
,
tempVars
[
3
]);
return
tempVars
[
4
];
}
else
{
// The APDU needs a multi-stage copy
...
...
@@ -871,7 +850,7 @@ public class CTAP2 extends Applet implements ExtendedLength {
tempVars
[
4
]
=
0
;
while
(
tempVars
[
3
]
>
0
)
{
// Copy data
tempVars
[
4
]
=
Util
.
arrayCopyNonAtomic
(
buffer
,
apdu
.
getOffsetCdata
(),
input
Buffer
,
tempVars
[
4
],
tempVars
[
5
]);
tempVars
[
4
]
=
Util
.
arrayCopyNonAtomic
(
buffer
,
apdu
.
getOffsetCdata
(),
data
Buffer
,
tempVars
[
4
],
tempVars
[
5
]);
// Decrement vars[3] by the bytes copied
tempVars
[
3
]
-=
tempVars
[
5
];
// Pull more bytes
...
...
@@ -895,7 +874,7 @@ public class CTAP2 extends Applet implements ExtendedLength {
// More to go after this
outChainRam
[
0
]
-=
256
;
byte
[]
buf
=
apdu
.
getBuffer
();
Util
.
arrayCopyNonAtomic
(
input
Buffer
,
outChainRam
[
1
],
buf
,
(
short
)
0
,
(
short
)
256
);
Util
.
arrayCopyNonAtomic
(
data
Buffer
,
outChainRam
[
1
],
buf
,
(
short
)
0
,
(
short
)
256
);
apdu
.
setOutgoingAndSend
((
short
)
0
,
(
short
)
256
);
outChainRam
[
1
]
+=
256
;
if
(
outChainRam
[
0
]
>
255
)
{
...
...
@@ -908,7 +887,7 @@ public class CTAP2 extends Applet implements ExtendedLength {
}
else
{
// This is the last message
byte
[]
buf
=
apdu
.
getBuffer
();
Util
.
arrayCopyNonAtomic
(
input
Buffer
,
outChainRam
[
1
],
buf
,
(
short
)
0
,
outChainRam
[
0
]);
Util
.
arrayCopyNonAtomic
(
data
Buffer
,
outChainRam
[
1
],
buf
,
(
short
)
0
,
outChainRam
[
0
]);
apdu
.
setOutgoingAndSend
((
short
)
0
,
outChainRam
[
0
]);
isOutChaining
[
0
]
=
false
;
outChainRam
[
0
]
=
0
;
...
...
@@ -932,7 +911,7 @@ public class CTAP2 extends Applet implements ExtendedLength {
outChainRam
[
0
]
=
(
short
)
(
dataLen
-
256
);
// Send the first 256 bytes out
byte
[]
buf
=
apdu
.
getBuffer
();
Util
.
arrayCopyNonAtomic
(
input
Buffer
,
(
short
)
0
,
buf
,
(
short
)
0
,
(
short
)
256
);
Util
.
arrayCopyNonAtomic
(
data
Buffer
,
(
short
)
0
,
buf
,
(
short
)
0
,
(
short
)
256
);
apdu
.
setOutgoingAndSend
((
short
)
0
,
(
short
)
256
);
outChainRam
[
1
]
=
256
;
// Throw the 61 xx
...
...
@@ -948,7 +927,7 @@ public class CTAP2 extends Applet implements ExtendedLength {
isOutChaining
[
0
]
=
false
;
apdu
.
setOutgoing
();
apdu
.
setOutgoingLength
(
dataLen
);
apdu
.
sendBytesLong
(
input
Buffer
,
(
short
)
0
,
dataLen
);
apdu
.
sendBytesLong
(
data
Buffer
,
(
short
)
0
,
dataLen
);
ISOException
.
throwIt
(
ISO7816
.
SW_NO_ERROR
);
}
}
...
...
@@ -963,8 +942,8 @@ public class CTAP2 extends Applet implements ExtendedLength {
}
private
void
getCert
(
APDU
apdu
)
{
input
Buffer
[
0
]
=
0x00
;
tempVars
[
0
]
=
(
short
)
(
attestationKeyPair
.
getCert
(
input
Buffer
,
(
short
)
1
)
+
1
);
data
Buffer
[
0
]
=
0x00
;
tempVars
[
0
]
=
(
short
)
(
attestationKeyPair
.
getCert
(
data
Buffer
,
(
short
)
1
)
+
1
);
sendLongChaining
(
apdu
,
tempVars
[
0
]);
}
...
...
src/main/java/com/josh/vku2f/ClientPIN.java
0 → 100644
View file @
ba3ad98f
package
com.josh.vku2f
;
import
javacard.framework.JCSystem
;
public
class
ClientPIN
{
private
short
i
;
// counter
private
byte
[]
PIN
;
private
boolean
[]
checked
;
ClientPIN
(){
checked
=
JCSystem
.
makeTransientBooleanArray
((
short
)
1
,
JCSystem
.
CLEAR_ON_DESELECT
);
}
public
boolean
checkPIN
(
byte
[]
pin
){
if
(
PIN
.
length
!=
pin
.
length
){
return
false
;
}
for
(
i
=
0
;
i
<
PIN
.
length
;
i
++){
if
(
PIN
[
i
]
!=
pin
[
i
])
{
return
false
;
}
}
checked
[
0
]
=
true
;
return
true
;
}
public
void
setPIN
(){
}
}
src/main/java/com/josh/vku2f/ClientPINCommand.java
0 → 100644
View file @
ba3ad98f
package
com.josh.vku2f
;
import
javacard.framework.UserException
;
public
class
ClientPINCommand
{
public
static
final
byte
PARAMETER_PROTOCOL
=
0x01
;
public
static
final
byte
PARAMETER_SUBCOMMAND
=
0x02
;
public
static
final
byte
PARAMETER_KEY_AGREEMENT
=
0x03
;
public
static
final
byte
PARAMETER_PIN_UV_AUTH_PARAM
=
0x04
;
public
static
final
byte
PARAMETER_NEW_PIN_ENC
=
0x05
;
public
static
final
byte
PARAMETER_PIN_HASH_ENC
=
0x06
;
public
static
final
byte
PARAMETER_PERMISSIONS
=
0x09
;
public
static
final
byte
PARAMETER_RP_ID
=
0x0A
;
private
byte
protocol
;
// unsigned int
private
byte
subCommandCode
;
// unsigned int
private
byte
[]
keyAgreement
;
// COSE object
private
byte
[]
pinUvAuthParam
;
// byte string
private
byte
[]
newPinEnc
;
// byte string
private
byte
[]
pinHashEnc
;
// byte string
private
byte
permissions
;
// unsigned int
private
byte
[]
rpId
;
// text string
public
void
decodeCommand
(
CBORDecoder
cborDecoder
)
throws
UserException
{
short
commandLength
=
cborDecoder
.
readMajorType
(
CBORBase
.
TYPE_MAP
);
do
{
byte
commandKey
=
cborDecoder
.
readInt8
();
short
valueLength
;
switch
(
commandKey
)
{
case
PARAMETER_PROTOCOL:
protocol
=
cborDecoder
.
readInt8
();
break
;
case
PARAMETER_SUBCOMMAND:
subCommandCode
=
cborDecoder
.
readInt8
();
break
;
case
PARAMETER_KEY_AGREEMENT:
valueLength
=
cborDecoder
.
readLength
();
keyAgreement
=
new
byte
[
valueLength
];
cborDecoder
.
readRawByteArray
(
keyAgreement
,
(
short
)
0
,
valueLength
);
break
;
case
PARAMETER_PIN_UV_AUTH_PARAM:
valueLength
=
cborDecoder
.
readLength
();
pinUvAuthParam
=
new
byte
[
valueLength
];
cborDecoder
.
readRawByteArray
(
pinUvAuthParam
,
(
short
)
0
,
valueLength
);
break
;
case
PARAMETER_NEW_PIN_ENC:
valueLength
=
cborDecoder
.
readLength
();
newPinEnc
=
new
byte
[
valueLength
];
cborDecoder
.
readRawByteArray
(
newPinEnc
,
(
short
)
0
,
valueLength
);
break
;
case
PARAMETER_PIN_HASH_ENC:
valueLength
=
cborDecoder
.
readLength
();
pinHashEnc
=
new
byte
[
valueLength
];
cborDecoder
.
readRawByteArray
(
pinHashEnc
,
(
short
)
0
,
valueLength
);
break
;
case
PARAMETER_PERMISSIONS:
permissions
=
cborDecoder
.
readInt8
();
break
;
case
PARAMETER_RP_ID:
valueLength
=
cborDecoder
.
readLength
();
rpId
=
new
byte
[
valueLength
];
cborDecoder
.
readRawByteArray
(
rpId
,
(
short
)
0
,
valueLength
);
break
;
}
commandLength
--;
}
while
(
commandLength
>=
1
);
}
public
byte
getProtocol
()
{
return
protocol
;
}
public
byte
getSubCommandCode
()
{
return
subCommandCode
;
}
public
byte
[]
getKeyAgreement
()
{
return
keyAgreement
;
}
public
byte
[]
getPinUvAuthParam
()
{
return
pinUvAuthParam
;
}
public
byte
[]
getNewPinEnc
()
{
return
newPinEnc
;
}
public
byte
[]
getPinHashEnc
()
{
return
pinHashEnc
;
}
public
byte
getPermissions
()
{
return
permissions
;
}
public
byte
[]
getRpId
()
{
return
rpId
;
}
}
src/main/java/com/josh/vku2f/ClientPINResponse.java
0 → 100644
View file @
ba3ad98f
package
com.josh.vku2f
;
public
class
ClientPINResponse
{
public
static
byte
KEY_AGREEMENT
=
(
byte
)
0x01
;
public
static
byte
PIN_UV_AUTH_TOKEN
=
(
byte
)
0x02
;
public
static
byte
PIN_RETRIES
=
(
byte
)
0x03
;
public
static
byte
POWER_CYCLE_STATE
=
(
byte
)
0x04
;
public
static
byte
UV_RETRIES
=
(
byte
)
0x05
;
}
src/main/java/com/josh/vku2f/ClientPINSubCommand.java
0 → 100644
View file @
ba3ad98f
package
com.josh.vku2f
;
public
class
ClientPINSubCommand
{
public
static
final
byte
SUBCOMMAND_GET_PIN_RETRIES
=
(
byte
)
0x01
;
public
static
final
byte
SUBCOMMAND_GET_KEY_AGREEMENT
=
(
byte
)
0x02
;
public
static
final
byte
SUBCOMMAND_SET_PIN
=
(
byte
)
0x03
;
public
static
final
byte
SUBCOMMAND_CHANGE_PIN
=
(
byte
)
0x04
;
public
static
final
byte
SUBCOMMAND_GET_PIN_TOKEN
=
(
byte
)
0x05
;
public
static
final
byte
SUBCOMMAND_GET_PIN_UV_AUTH_TOKEN_UV
=
(
byte
)
0x06
;
public
static
final
byte
SUBCOMMAND_GET_UV_RETRIES
=
(
byte
)
0x07
;
// no 0x08
public
static
final
byte
SUBCOMMAND_GET_PIN_UV_AUTH_TOKEN_PIN
=
(
byte
)
0x09
;
}
src/main/java/com/josh/vku2f/PinUvAuthProtocol.java
0 → 100644
View file @
ba3ad98f
package
com.josh.vku2f
;
public
abstract
class
PinUvAuthProtocol
{
private
PinUvAuthToken
pinUvAuthToken
;
public
abstract
void
initialize
();
public
abstract
void
regenerate
();
public
abstract
void
resetPinUvAuthToken
();
public
abstract
byte
[]
getPublicKey
();
public
abstract
byte
[]
decapsulate
(
COSEKey
peerCoseKey
);
public
abstract
void
decrypt
(
byte
[]
sharedSecret
,
byte
[]
cipherText
);
public
abstract
void
verify
(
byte
[]
key
,
byte
[]
message
,
byte
[]
signature
);
}
src/main/java/com/josh/vku2f/PinUvAuthProtocolOne.java
0 → 100644
View file @
ba3ad98f
package
com.josh.vku2f
;
import
javacard.framework.JCSystem
;
import
javacard.security.*
;
public
class
PinUvAuthProtocolOne
extends
PinUvAuthProtocol
{
private
KeyPair
ecDhKeyPair
;
private
boolean
[]
ecDhSet
;
@Override
public
void
initialize
()
{
ECPublicKey
ecDhPub
=
(
ECPublicKey
)
KeyBuilder
.
buildKey
(
KeyBuilder
.
ALG_TYPE_EC_FP_PUBLIC
,
JCSystem
.
MEMORY_TYPE_TRANSIENT_RESET
,
KeyBuilder
.
LENGTH_EC_FP_256
,
false
);
ECPrivateKey
ecDhPriv
=
(
ECPrivateKey
)
KeyBuilder
.
buildKey
(
KeyBuilder
.
ALG_TYPE_EC_FP_PRIVATE
,
JCSystem
.
MEMORY_TYPE_TRANSIENT_RESET
,
KeyBuilder
.
LENGTH_EC_FP_256
,
false
);
ecDhKeyPair
=
new
KeyPair
(
ecDhPub
,
ecDhPriv
);
ecDhSet
=
JCSystem
.
makeTransientBooleanArray
((
short
)
1
,
JCSystem
.
CLEAR_ON_RESET
);
}
@Override
public
void
regenerate
()
{
}
@Override
public
void
resetPinUvAuthToken
()
{
}
@Override
public
byte
[]
getPublicKey
()
{
byte
[]
w
;
try
{
w
=
JCSystem
.
makeTransientByteArray
((
short
)
65
,
JCSystem
.
CLEAR_ON_RESET
);
}
catch
(
Exception
e
)
{
w
=
new
byte
[
65
];
}
if
(!
ecDhSet
[
0
])
{
// Grab the public key and set it's parameters
KeyParams
.
sec256r1params
((
ECKey
)
ecDhKeyPair
.
getPublic
());
// Generate a new key-pair
ecDhKeyPair
.
genKeyPair
();
ecDhSet
[
0
]
=
true
;
}
((
ECPublicKey
)
ecDhKeyPair
.
getPublic
()).
getW
(
w
,
(
short
)
0
);
// Return the data requested
return
w
;
}
public
byte
[]
encapsulate
(
COSEKey
peerCOSEKey
){
return
null
;
}
@Override
public
byte
[]
decapsulate
(
COSEKey
peerCOSEKey
)
{
return
null
;
}
public
byte
[]
encrypt
(
byte
[]
key
,
byte
[]
plaintext
){
return
null
;
}
@Override
public
void
decrypt
(
byte
[]
sharedSecret
,
byte
[]
cipherText
)
{
}
public
byte
[]
authenticate
(
byte
[]
key
,
byte
[]
message
){
return
null
;
}
@Override
public
void
verify
(
byte
[]
key
,
byte
[]
message
,
byte
[]
signature
)
{
}
private
byte
[]
ecdh
(
COSEKey
peerCoseKey
){
return
null
;
}
private
byte
[]
kdf
(
byte
[]
Z
){
return
null
;
}
}
src/main/java/com/josh/vku2f/PinUvAuthToken.java
0 → 100644
View file @
ba3ad98f
package
com.josh.vku2f
;
public
class
PinUvAuthToken
{
private
byte
[]
token
;
private
byte
protocol
;
private
byte
permissionsRPID
;
private
byte
permissionsSet
;
private
byte
usageTimer
;
private
boolean
inUseFlag
;
private
byte
initialUsageTimeLimit
;
private
byte
userPresentTimeLimit
;
private
byte
maxUsageTimePeriod
;
private
boolean
userVerifiedFlag
;
private
boolean
userPresentFlag
;
PinUvAuthToken
(){
resetTokenState
();
}
public
void
generateNewToken
(){
}
public
void
resetTokenState
(){
permissionsRPID
=
0x00
;
permissionsSet
=
0x00
;
usageTimer
=
0x00
;
inUseFlag
=
false
;
initialUsageTimeLimit
=
0x00
;
userPresentTimeLimit
=
0x00
;
maxUsageTimePeriod
=
0x00
;
userVerifiedFlag
=
false
;
userPresentFlag
=
false
;
}
public
boolean
isInUse
(){
return
inUseFlag
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment