Skip to content

Commit 81137a7

Browse files
committed
Reuse some generated keys in RelyingPartyRegistrationSpec
1 parent 641b496 commit 81137a7

File tree

2 files changed

+39
-40
lines changed

2 files changed

+39
-40
lines changed

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

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,6 +2125,8 @@ class RelyingPartyRegistrationSpec
21252125

21262126
def makeCred(
21272127
authDataAndKeypair: Option[(ByteArray, KeyPair)] = None,
2128+
credKeyAlgorithm: COSEAlgorithmIdentifier =
2129+
TestAuthenticator.Defaults.keyAlgorithm,
21282130
clientDataJson: Option[String] = None,
21292131
subject: X500Name = emptySubject,
21302132
rdn: Array[AttributeTypeAndValue] =
@@ -2152,8 +2154,13 @@ class RelyingPartyRegistrationSpec
21522154
) = {
21532155
val (authData, credentialKeypair) =
21542156
authDataAndKeypair.getOrElse(
2155-
TestAuthenticator.createAuthenticatorData(keyAlgorithm =
2156-
COSEAlgorithmIdentifier.ES256
2157+
TestAuthenticator.createAuthenticatorData(
2158+
credentialKeypair = Some(
2159+
TestAuthenticator.Defaults.defaultKeypair(
2160+
credKeyAlgorithm
2161+
)
2162+
),
2163+
keyAlgorithm = credKeyAlgorithm,
21572164
)
21582165
)
21592166

@@ -2719,7 +2726,11 @@ class RelyingPartyRegistrationSpec
27192726
val (authData, keypair) =
27202727
TestAuthenticator.createAuthenticatorData(
27212728
aaguid = aaguid,
2722-
keyAlgorithm = COSEAlgorithmIdentifier.ES256,
2729+
credentialKeypair = Some(
2730+
TestAuthenticator.Defaults.defaultKeypair(
2731+
COSEAlgorithmIdentifier.ES256
2732+
)
2733+
),
27232734
)
27242735
val testData = (RegistrationTestData.from _).tupled(
27252736
makeCred(
@@ -2743,7 +2754,11 @@ class RelyingPartyRegistrationSpec
27432754
val (authData, keypair) =
27442755
TestAuthenticator.createAuthenticatorData(
27452756
aaguid = aaguidInCred,
2746-
keyAlgorithm = COSEAlgorithmIdentifier.ES256,
2757+
credentialKeypair = Some(
2758+
TestAuthenticator.Defaults.defaultKeypair(
2759+
COSEAlgorithmIdentifier.ES256
2760+
)
2761+
),
27472762
)
27482763
val testData = (RegistrationTestData.from _).tupled(
27492764
makeCred(
@@ -2768,12 +2783,7 @@ class RelyingPartyRegistrationSpec
27682783
) { attributes: Long =>
27692784
val testData = (RegistrationTestData.from _).tupled(
27702785
makeCred(
2771-
authDataAndKeypair = Some(
2772-
TestAuthenticator
2773-
.createAuthenticatorData(keyAlgorithm =
2774-
COSEAlgorithmIdentifier.RS256
2775-
)
2776-
),
2786+
credKeyAlgorithm = COSEAlgorithmIdentifier.RS256,
27772787
attributes = Some(attributes & ~Attributes.SIGN_ENCRYPT),
27782788
)
27792789
)
@@ -2793,12 +2803,7 @@ class RelyingPartyRegistrationSpec
27932803
whenever(symmetric != TPM_ALG_NULL) {
27942804
val testData = (RegistrationTestData.from _).tupled(
27952805
makeCred(
2796-
authDataAndKeypair = Some(
2797-
TestAuthenticator
2798-
.createAuthenticatorData(keyAlgorithm =
2799-
COSEAlgorithmIdentifier.RS256
2800-
)
2801-
),
2806+
credKeyAlgorithm = COSEAlgorithmIdentifier.RS256,
28022807
symmetric = Some(symmetric),
28032808
)
28042809
)
@@ -2821,12 +2826,7 @@ class RelyingPartyRegistrationSpec
28212826
) {
28222827
val testData = (RegistrationTestData.from _).tupled(
28232828
makeCred(
2824-
authDataAndKeypair = Some(
2825-
TestAuthenticator
2826-
.createAuthenticatorData(keyAlgorithm =
2827-
COSEAlgorithmIdentifier.RS256
2828-
)
2829-
),
2829+
credKeyAlgorithm = COSEAlgorithmIdentifier.RS256,
28302830
scheme = Some(scheme),
28312831
)
28322832
)
@@ -2846,12 +2846,7 @@ class RelyingPartyRegistrationSpec
28462846
) { attributes: Long =>
28472847
val testData = (RegistrationTestData.from _).tupled(
28482848
makeCred(
2849-
authDataAndKeypair = Some(
2850-
TestAuthenticator
2851-
.createAuthenticatorData(keyAlgorithm =
2852-
COSEAlgorithmIdentifier.ES256
2853-
)
2854-
),
2849+
credKeyAlgorithm = COSEAlgorithmIdentifier.ES256,
28552850
attributes = Some(attributes & ~Attributes.SIGN_ENCRYPT),
28562851
)
28572852
)
@@ -2871,12 +2866,7 @@ class RelyingPartyRegistrationSpec
28712866
whenever(symmetric != TPM_ALG_NULL) {
28722867
val testData = (RegistrationTestData.from _).tupled(
28732868
makeCred(
2874-
authDataAndKeypair = Some(
2875-
TestAuthenticator
2876-
.createAuthenticatorData(keyAlgorithm =
2877-
COSEAlgorithmIdentifier.ES256
2878-
)
2879-
),
2869+
credKeyAlgorithm = COSEAlgorithmIdentifier.ES256,
28802870
symmetric = Some(symmetric),
28812871
)
28822872
)
@@ -2897,12 +2887,7 @@ class RelyingPartyRegistrationSpec
28972887
whenever(scheme != TPM_ALG_NULL) {
28982888
val testData = (RegistrationTestData.from _).tupled(
28992889
makeCred(
2900-
authDataAndKeypair = Some(
2901-
TestAuthenticator
2902-
.createAuthenticatorData(keyAlgorithm =
2903-
COSEAlgorithmIdentifier.ES256
2904-
)
2905-
),
2890+
credKeyAlgorithm = COSEAlgorithmIdentifier.ES256,
29062891
scheme = Some(scheme),
29072892
)
29082893
)

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ object TestAuthenticator {
125125
)
126126
val certValidFrom: Instant = Instant.parse("2018-09-06T17:42:00Z")
127127
val certValidTo: Instant = certValidFrom.plusSeconds(7 * 24 * 3600)
128+
129+
private var defaultKeypairs: Map[COSEAlgorithmIdentifier, KeyPair] =
130+
Map.empty
131+
def defaultKeypair(
132+
algorithm: COSEAlgorithmIdentifier = Defaults.keyAlgorithm
133+
): KeyPair = {
134+
defaultKeypairs.get(algorithm) match {
135+
case Some(keypair) => keypair
136+
case None =>
137+
val keypair = generateKeypair(algorithm)
138+
defaultKeypairs = defaultKeypairs + (algorithm -> keypair)
139+
keypair
140+
}
141+
}
128142
}
129143
val RsaKeySizeBits = 2048
130144
val Es256PrimeModulus: BigInteger = new BigInteger(

0 commit comments

Comments
 (0)