@@ -46,7 +46,8 @@ func TestCryptoJWKCreateRSACommand(t *testing.T) {
4646 return os .WriteFile (filepath .Join (e .Cd , "password.txt" ), []byte ("password" ), 0600 )
4747 },
4848 Cmds : map [string ]func (ts * testscript.TestScript , neg bool , args []string ){
49- "check_jwk" : checkKeyPair ,
49+ "check_jwk" : checkKeyPair ,
50+ "check_jwk_without_password" : checkKeyPairWithoutPassword ,
5051 },
5152 })
5253}
@@ -58,7 +59,8 @@ func TestCryptoJWKCreateECCommand(t *testing.T) {
5859 return os .WriteFile (filepath .Join (e .Cd , "password.txt" ), []byte ("password" ), 0600 )
5960 },
6061 Cmds : map [string ]func (ts * testscript.TestScript , neg bool , args []string ){
61- "check_jwk" : checkKeyPair ,
62+ "check_jwk" : checkKeyPair ,
63+ "check_jwk_without_password" : checkKeyPairWithoutPassword ,
6264 },
6365 })
6466}
@@ -70,7 +72,8 @@ func TestCryptoJWKCreateOKPCommand(t *testing.T) {
7072 return os .WriteFile (filepath .Join (e .Cd , "password.txt" ), []byte ("password" ), 0600 )
7173 },
7274 Cmds : map [string ]func (ts * testscript.TestScript , neg bool , args []string ){
73- "check_jwk" : checkKeyPair ,
75+ "check_jwk" : checkKeyPair ,
76+ "check_jwk_without_password" : checkKeyPairWithoutPassword ,
7477 },
7578 })
7679}
@@ -82,7 +85,8 @@ func TestCryptoJWKCreateOctCommand(t *testing.T) {
8285 return os .WriteFile (filepath .Join (e .Cd , "password.txt" ), []byte ("password" ), 0600 )
8386 },
8487 Cmds : map [string ]func (ts * testscript.TestScript , neg bool , args []string ){
85- "check_jwk" : checkKeyPair ,
88+ "check_jwk" : checkKeyPair ,
89+ "check_jwk_without_password" : checkKeyPairWithoutPassword ,
8690 },
8791 })
8892}
@@ -311,16 +315,10 @@ func TestCryptoHelp(t *testing.T) {
311315 })
312316}
313317
314- // checkKeyPair checks that the public/private key pair is valid. It performs
315- // the following checks:
316- //
317- // - Read and parse the JWK public key, validating it's a valid public key
318- // - Read and parse the JWK private key, validating it's a valid private key
319- // - Compare the public and private key SHA-1 thumbprints to verify they match
320- // - The type of the key that was created
321- // - For RSA keys, the key size is the expected size
322- // - For EC keys, the key curve is the expected curve
323- func checkKeyPair (ts * testscript.TestScript , neg bool , args []string ) {
318+ // checkKeyPair checks that the public/private key pair provided as filenames in
319+ // the first and second argument is valid. It always uses the password "password".
320+ // Other validations are delegated to the checkKeyDetails function.
321+ func checkKeyPair (ts * testscript.TestScript , _ bool , args []string ) {
324322 if len (args ) < 4 {
325323 ts .Fatalf ("expected at least 4 arguments, got %d" , len (args ))
326324 }
@@ -330,6 +328,35 @@ func checkKeyPair(ts *testscript.TestScript, neg bool, args []string) {
330328 priv , err := jose .ParseKey ([]byte (ts .ReadFile (args [1 ])), jose .WithPassword ([]byte ("password" )))
331329 ts .Check (err )
332330
331+ checkKeyDetails (ts , pub , priv , args )
332+ }
333+
334+ // checkKeyPair checks that the public/private key pair provided as filenames in
335+ // the first and second argument is valid. It assumes no password is set on the file.
336+ // Other validations are delegated to the checkKeyDetails function.
337+ func checkKeyPairWithoutPassword (ts * testscript.TestScript , _ bool , args []string ) {
338+ if len (args ) < 4 {
339+ ts .Fatalf ("expected at least 4 arguments, got %d" , len (args ))
340+ }
341+
342+ pub , err := jose .ParseKey ([]byte (ts .ReadFile (args [0 ])))
343+ ts .Check (err )
344+ priv , err := jose .ParseKey ([]byte (ts .ReadFile (args [1 ])))
345+ ts .Check (err )
346+
347+ checkKeyDetails (ts , pub , priv , args )
348+ }
349+
350+ // checkKeyDetails checks that the public/private key pair is valid. It performs
351+ // the following checks:
352+ //
353+ // - Compare the public and private key SHA-1 thumbprints to verify they match
354+ // - The type of the key that was created
355+ // - For RSA keys, the key size is the expected size, and using the expected algorithm
356+ // - For EC keys, the key curve is the expected curve, and using the expected algorithm
357+ // - For OKP keys, the key curve is the expected curve, and using the expected algorithm
358+ // - For oct keys, the key parts are of the expected type, and using the expected algorithm
359+ func checkKeyDetails (ts * testscript.TestScript , pub , priv * jose.JSONWebKey , args []string ) {
333360 keyType := strings .ToUpper (args [2 ])
334361 if keyType == "OCT" {
335362 if _ , ok := pub .Key .([]byte ); ! ok {
0 commit comments