Skip to content

Commit e60862a

Browse files
committed
Use BinaryUtil.concat instead of ByteArray.concat where appropriate
1 parent 6dfbe9a commit e60862a

File tree

6 files changed

+97
-110
lines changed

6 files changed

+97
-110
lines changed

webauthn-server-attestation/src/main/java/com/yubico/fido/metadata/AAGUID.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.annotation.JsonValue;
5+
import com.yubico.internal.util.BinaryUtil;
56
import com.yubico.internal.util.ExceptionUtil;
67
import com.yubico.webauthn.data.ByteArray;
7-
import com.yubico.webauthn.data.exception.HexException;
88
import java.util.regex.Matcher;
99
import java.util.regex.Pattern;
1010
import lombok.AccessLevel;
@@ -105,12 +105,14 @@ private static ByteArray parse(String value) {
105105
Matcher matcher = AAGUID_PATTERN.matcher(value);
106106
if (matcher.find()) {
107107
try {
108-
return ByteArray.fromHex(matcher.group(1))
109-
.concat(ByteArray.fromHex(matcher.group(2)))
110-
.concat(ByteArray.fromHex(matcher.group(3)))
111-
.concat(ByteArray.fromHex(matcher.group(4)))
112-
.concat(ByteArray.fromHex(matcher.group(5)));
113-
} catch (HexException e) {
108+
return new ByteArray(
109+
BinaryUtil.concat(
110+
BinaryUtil.fromHex(matcher.group(1)),
111+
BinaryUtil.fromHex(matcher.group(2)),
112+
BinaryUtil.fromHex(matcher.group(3)),
113+
BinaryUtil.fromHex(matcher.group(4)),
114+
BinaryUtil.fromHex(matcher.group(5))));
115+
} catch (Exception e) {
114116
throw new RuntimeException(
115117
"This exception should be impossible, please file a bug report.", e);
116118
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory
2929
import com.fasterxml.jackson.databind.node.ObjectNode
3030
import com.fasterxml.jackson.databind.node.TextNode
3131
import com.upokecenter.cbor.CBORObject
32+
import com.yubico.internal.util.BinaryUtil
3233
import com.yubico.internal.util.JacksonCodecs
3334
import com.yubico.webauthn.data.AssertionExtensionInputs
3435
import com.yubico.webauthn.data.AuthenticatorAssertionResponse
@@ -2419,13 +2420,14 @@ class RelyingPartyAssertionSpec
24192420

24202421
it("a U2F-formatted public key.") {
24212422
val testData = RealExamples.YubiKeyNeo.asRegistrationTestData
2422-
val x = ByteArray.fromHex(
2423+
val x = BinaryUtil.fromHex(
24232424
"39C94FBBDDC694A925E6F8657C66916CFE84CD0222EDFCF281B21F5CDC347923"
24242425
)
2425-
val y = ByteArray.fromHex(
2426+
val y = BinaryUtil.fromHex(
24262427
"D6B0D2021CFE1724A6FE81E3568C4FFAE339298216A30AFC18C0B975F2E2A891"
24272428
)
2428-
val u2fPubkey = ByteArray.fromHex("04").concat(x).concat(y)
2429+
val u2fPubkey =
2430+
new ByteArray(BinaryUtil.concat(BinaryUtil.fromHex("04"), x, y))
24292431

24302432
val cred1 = RegisteredCredential
24312433
.builder()

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,18 +1745,15 @@ class RelyingPartyRegistrationSpec
17451745
key,
17461746
COSEAlgorithmIdentifier.RS256,
17471747
)
1748-
new ByteArray(
1748+
BinaryUtil.concat(
17491749
java.util.Arrays.copyOfRange(
17501750
authDataBytes,
17511751
0,
17521752
32 + 1 + 4 + 16 + 2,
1753-
)
1753+
),
1754+
authData.getAttestedCredentialData.get.getCredentialId.getBytes,
1755+
reencodedKey.getBytes,
17541756
)
1755-
.concat(
1756-
authData.getAttestedCredentialData.get.getCredentialId
1757-
)
1758-
.concat(reencodedKey)
1759-
.getBytes
17601757
}
17611758

17621759
def modifyAttobjPubkeyAlg(attObjBytes: ByteArray)

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory
2929
import com.fasterxml.jackson.databind.node.ObjectNode
3030
import com.fasterxml.jackson.databind.node.TextNode
3131
import com.upokecenter.cbor.CBORObject
32+
import com.yubico.internal.util.BinaryUtil
3233
import com.yubico.internal.util.JacksonCodecs
3334
import com.yubico.webauthn.data.AssertionExtensionInputs
3435
import com.yubico.webauthn.data.AuthenticatorAssertionResponse
@@ -2511,13 +2512,14 @@ class RelyingPartyV2AssertionSpec
25112512

25122513
it("a U2F-formatted public key.") {
25132514
val testData = RealExamples.YubiKeyNeo.asRegistrationTestData
2514-
val x = ByteArray.fromHex(
2515+
val x = BinaryUtil.fromHex(
25152516
"39C94FBBDDC694A925E6F8657C66916CFE84CD0222EDFCF281B21F5CDC347923"
25162517
)
2517-
val y = ByteArray.fromHex(
2518+
val y = BinaryUtil.fromHex(
25182519
"D6B0D2021CFE1724A6FE81E3568C4FFAE339298216A30AFC18C0B975F2E2A891"
25192520
)
2520-
val u2fPubkey = ByteArray.fromHex("04").concat(x).concat(y)
2521+
val u2fPubkey =
2522+
new ByteArray(BinaryUtil.concat(BinaryUtil.fromHex("04"), x, y))
25212523

25222524
val rp = RelyingParty
25232525
.builder()

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,18 +1736,15 @@ class RelyingPartyV2RegistrationSpec
17361736
key,
17371737
COSEAlgorithmIdentifier.RS256,
17381738
)
1739-
new ByteArray(
1739+
BinaryUtil.concat(
17401740
java.util.Arrays.copyOfRange(
17411741
authDataBytes,
17421742
0,
17431743
32 + 1 + 4 + 16 + 2,
1744-
)
1744+
),
1745+
authData.getAttestedCredentialData.get.getCredentialId.getBytes,
1746+
reencodedKey.getBytes,
17451747
)
1746-
.concat(
1747-
authData.getAttestedCredentialData.get.getCredentialId
1748-
)
1749-
.concat(reencodedKey)
1750-
.getBytes
17511748
}
17521749

17531750
def modifyAttobjPubkeyAlg(attObjBytes: ByteArray)

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

Lines changed: 70 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -930,105 +930,92 @@ object TestAuthenticator {
930930
case 3 => { // RSA
931931
val cose = CBORObject.DecodeFromBytes(cosePubkey.getBytes)
932932
(
933-
new ByteArray(BinaryUtil.encodeUint16(symmetric getOrElse 0x0010))
934-
.concat(
935-
new ByteArray(
936-
BinaryUtil.encodeUint16(scheme getOrElse TpmRsaScheme.RSASSA)
937-
)
938-
)
939-
.concat(
940-
new ByteArray(BinaryUtil.encodeUint16(RsaKeySizeBits))
941-
) // key_bits
942-
.concat(
943-
new ByteArray(
944-
BinaryUtil.encodeUint32(
945-
new BigInteger(1, cose.get(-2).GetByteString()).longValue()
946-
)
947-
)
948-
) // exponent
949-
,
950-
new ByteArray(
951-
BinaryUtil.encodeUint16(cose.get(-1).GetByteString().length)
952-
).concat(new ByteArray(cose.get(-1).GetByteString())), // modulus
933+
BinaryUtil.concat(
934+
BinaryUtil.encodeUint16(symmetric getOrElse 0x0010),
935+
BinaryUtil.encodeUint16(scheme getOrElse TpmRsaScheme.RSASSA),
936+
// key_bits
937+
BinaryUtil.encodeUint16(RsaKeySizeBits),
938+
// exponent
939+
BinaryUtil.encodeUint32(
940+
new BigInteger(1, cose.get(-2).GetByteString()).longValue()
941+
),
942+
),
943+
BinaryUtil.concat(
944+
BinaryUtil.encodeUint16(cose.get(-1).GetByteString().length),
945+
// modulus
946+
cose.get(-1).GetByteString(),
947+
),
953948
)
954949
}
955950
case 2 => { // EC
956951
val pubkey = WebAuthnCodecs
957952
.importCosePublicKey(cosePubkey)
958953
.asInstanceOf[ECPublicKey]
959954
(
960-
new ByteArray(BinaryUtil.encodeUint16(symmetric getOrElse 0x0010))
961-
.concat(
962-
new ByteArray(BinaryUtil.encodeUint16(scheme getOrElse 0x0010))
963-
)
964-
.concat(
965-
new ByteArray(BinaryUtil.encodeUint16(coseKeyAlg match {
966-
case COSEAlgorithmIdentifier.ES256 => 0x0003
967-
case COSEAlgorithmIdentifier.ES384 => 0x0004
968-
case COSEAlgorithmIdentifier.ES512 => 0x0005
969-
case COSEAlgorithmIdentifier.RS1 |
970-
COSEAlgorithmIdentifier.RS256 |
971-
COSEAlgorithmIdentifier.RS384 |
972-
COSEAlgorithmIdentifier.RS512 |
973-
COSEAlgorithmIdentifier.EdDSA =>
974-
???
975-
}))
976-
)
977-
.concat(
978-
new ByteArray(BinaryUtil.encodeUint16(0x0010))
979-
) // kdf_scheme: ??? (unused?)
980-
,
981-
new ByteArray(
982-
BinaryUtil.encodeUint16(pubkey.getW.getAffineX.toByteArray.length)
983-
)
984-
.concat(new ByteArray(pubkey.getW.getAffineX.toByteArray))
985-
.concat(
986-
new ByteArray(
987-
BinaryUtil.encodeUint16(
988-
pubkey.getW.getAffineY.toByteArray.length
989-
)
990-
)
991-
)
992-
.concat(new ByteArray(pubkey.getW.getAffineY.toByteArray)),
955+
BinaryUtil.concat(
956+
BinaryUtil.encodeUint16(symmetric getOrElse 0x0010),
957+
BinaryUtil.encodeUint16(scheme getOrElse 0x0010),
958+
BinaryUtil.encodeUint16(coseKeyAlg match {
959+
case COSEAlgorithmIdentifier.ES256 => 0x0003
960+
case COSEAlgorithmIdentifier.ES384 => 0x0004
961+
case COSEAlgorithmIdentifier.ES512 => 0x0005
962+
case COSEAlgorithmIdentifier.RS1 | COSEAlgorithmIdentifier.RS256 |
963+
COSEAlgorithmIdentifier.RS384 |
964+
COSEAlgorithmIdentifier.RS512 |
965+
COSEAlgorithmIdentifier.EdDSA =>
966+
???
967+
}),
968+
// kdf_scheme: ??? (unused?)
969+
BinaryUtil.encodeUint16(0x0010),
970+
),
971+
BinaryUtil.concat(
972+
BinaryUtil.encodeUint16(pubkey.getW.getAffineX.toByteArray.length),
973+
pubkey.getW.getAffineX.toByteArray,
974+
BinaryUtil.encodeUint16(
975+
pubkey.getW.getAffineY.toByteArray.length
976+
),
977+
pubkey.getW.getAffineY.toByteArray,
978+
),
993979
)
994980
}
995981
}
996-
val pubArea = new ByteArray(BinaryUtil.encodeUint16(signAlg))
997-
.concat(new ByteArray(BinaryUtil.encodeUint16(hashId)))
998-
.concat(
999-
new ByteArray(
1000-
BinaryUtil.encodeUint32(attributes getOrElse Attributes.SIGN_ENCRYPT)
1001-
)
982+
val pubArea = new ByteArray(
983+
BinaryUtil.concat(
984+
BinaryUtil.encodeUint16(signAlg),
985+
BinaryUtil.encodeUint16(hashId),
986+
BinaryUtil.encodeUint32(attributes getOrElse Attributes.SIGN_ENCRYPT),
987+
// authPolicy is ignored by TpmAttestationStatementVerifier
988+
BinaryUtil.encodeUint16(0),
989+
parameters,
990+
unique,
1002991
)
1003-
.concat(
1004-
new ByteArray(BinaryUtil.encodeUint16(0))
1005-
) // authPolicy is ignored by TpmAttestationStatementVerifier
1006-
.concat(parameters)
1007-
.concat(unique)
1008-
1009-
val qualifiedSigner = ByteArray.fromHex("")
1010-
val clockInfo = ByteArray.fromHex("0000000000000000111111112222222233")
1011-
val firmwareVersion = ByteArray.fromHex("0000000000000000")
992+
)
993+
994+
val qualifiedSigner = BinaryUtil.fromHex("")
995+
val clockInfo = BinaryUtil.fromHex("0000000000000000111111112222222233")
996+
val firmwareVersion = BinaryUtil.fromHex("0000000000000000")
1012997
val attestedName =
1013998
modifyAttestedName(
1014999
new ByteArray(BinaryUtil.encodeUint16(hashId)).concat(hashFunc(pubArea))
10151000
)
1016-
val attestedQualifiedName = ByteArray.fromHex("")
1017-
1018-
val certInfo = magic
1019-
.concat(`type`)
1020-
.concat(new ByteArray(BinaryUtil.encodeUint16(qualifiedSigner.size)))
1021-
.concat(qualifiedSigner)
1022-
.concat(new ByteArray(BinaryUtil.encodeUint16(extraData.size)))
1023-
.concat(extraData)
1024-
.concat(clockInfo)
1025-
.concat(firmwareVersion)
1026-
.concat(new ByteArray(BinaryUtil.encodeUint16(attestedName.size)))
1027-
.concat(attestedName)
1028-
.concat(
1029-
new ByteArray(BinaryUtil.encodeUint16(attestedQualifiedName.size))
1001+
val attestedQualifiedName = BinaryUtil.fromHex("")
1002+
1003+
val certInfo = new ByteArray(
1004+
BinaryUtil.concat(
1005+
magic.getBytes,
1006+
`type`.getBytes,
1007+
BinaryUtil.encodeUint16(qualifiedSigner.length),
1008+
qualifiedSigner,
1009+
BinaryUtil.encodeUint16(extraData.size),
1010+
extraData.getBytes,
1011+
clockInfo,
1012+
firmwareVersion,
1013+
BinaryUtil.encodeUint16(attestedName.size),
1014+
attestedName.getBytes,
1015+
BinaryUtil.encodeUint16(attestedQualifiedName.length),
1016+
attestedQualifiedName,
10301017
)
1031-
.concat(attestedQualifiedName)
1018+
)
10321019

10331020
val sig = sign(certInfo, cert.key, cert.alg)
10341021

0 commit comments

Comments
 (0)