Skip to content

Commit 57c9770

Browse files
committed
Add function CredentialRecord.cosePublicKeyFromEs256Raw(ByteArray)
1 parent cff0ba2 commit 57c9770

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

webauthn-server-core/src/main/java/com/yubico/webauthn/CredentialRecord.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,29 @@ default PublicKeyCredentialDescriptor toPublicKeyCredentialDescriptor() {
197197
.transports(getTransports())
198198
.build();
199199
}
200+
201+
/**
202+
* Convert a credential public key from U2F format to COSE_Key format.
203+
*
204+
* <p>The U2F JavaScript API encoded credential public keys in <code>ALG_KEY_ECC_X962_RAW</code>
205+
* format as specified in <a
206+
* href="https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-registry-v2.0-id-20180227.html#public-key-representation-formats">FIDO
207+
* Registry §3.6.2 Public Key Representation Formats</a>. If your database has credential public
208+
* keys stored in this format, those public keys need to be converted to COSE_Key format before
209+
* they can be used by a {@link CredentialRecord} instance. This function performs the conversion.
210+
*
211+
* <p>If your application has only used the <code>navigator.credentials.create()</code> API to
212+
* register credentials, you likely do not need this function.
213+
*
214+
* @param es256RawKey a credential public key in <code>ALG_KEY_ECC_X962_RAW</code> format as
215+
* specified in <a
216+
* href="https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-registry-v2.0-id-20180227.html#public-key-representation-formats">FIDO
217+
* Registry §3.6.2 Public Key Representation Formats</a>.
218+
* @return a credential public key in COSE_Key format, suitable to be returned by {@link
219+
* CredentialRecord#getPublicKeyCose()}.
220+
* @see RegisteredCredential.RegisteredCredentialBuilder#publicKeyEs256Raw(ByteArray)
221+
*/
222+
static ByteArray cosePublicKeyFromEs256Raw(final ByteArray es256RawKey) {
223+
return WebAuthnCodecs.rawEcKeyToCose(es256RawKey);
224+
}
200225
}

webauthn-server-core/src/test/scala/com/yubico/webauthn/RelyingPartyV2AssertionSpec.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2525,7 +2525,8 @@ class RelyingPartyV2AssertionSpec
25252525
Helpers.CredentialRepositoryV2.withUser(
25262526
testData.userId,
25272527
credentialId = testData.assertion.get.response.getId,
2528-
publicKeyCose = WebAuthnCodecs.rawEcKeyToCose(u2fPubkey),
2528+
publicKeyCose =
2529+
CredentialRecord.cosePublicKeyFromEs256Raw(u2fPubkey),
25292530
)
25302531
)
25312532
.usernameRepository(

0 commit comments

Comments
 (0)