Skip to content

Commit b009357

Browse files
committedJun 2, 2023·
[crypto] Add support for PKCS#8 private key format
Signed-off-by: Michael Brown <mcb30@ipxe.org>
1 parent 6a7f560 commit b009357

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed
 

‎src/crypto/asn1.c

+26
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,32 @@ int asn1_signature_algorithm ( const struct asn1_cursor *cursor,
589589
return 0;
590590
}
591591

592+
/**
593+
* Check ASN.1 OID-identified algorithm
594+
*
595+
* @v cursor ASN.1 object cursor
596+
* @v expected Expected algorithm
597+
* @ret rc Return status code
598+
*/
599+
int asn1_check_algorithm ( const struct asn1_cursor *cursor,
600+
struct asn1_algorithm *expected ) {
601+
struct asn1_algorithm *actual;
602+
int rc;
603+
604+
/* Parse algorithm */
605+
if ( ( rc = asn1_algorithm ( cursor, &actual ) ) != 0 )
606+
return rc;
607+
608+
/* Check algorithm matches */
609+
if ( actual != expected ) {
610+
DBGC ( cursor, "ASN1 %p algorithm %s does not match %s\n",
611+
cursor, actual->name, expected->name );
612+
return -ENOTTY_ALGORITHM;
613+
}
614+
615+
return 0;
616+
}
617+
592618
/**
593619
* Parse ASN.1 GeneralizedTime
594620
*

‎src/crypto/rsa.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static int rsa_parse_mod_exp ( struct asn1_cursor *modulus,
164164
int is_private;
165165
int rc;
166166

167-
/* Enter subjectPublicKeyInfo/RSAPrivateKey */
167+
/* Enter subjectPublicKeyInfo/privateKeyInfo/RSAPrivateKey */
168168
memcpy ( &cursor, raw, sizeof ( cursor ) );
169169
asn1_enter ( &cursor, ASN1_SEQUENCE );
170170

@@ -177,6 +177,23 @@ static int rsa_parse_mod_exp ( struct asn1_cursor *modulus,
177177
/* Skip version */
178178
asn1_skip_any ( &cursor );
179179

180+
/* Enter privateKey, if present */
181+
if ( asn1_check_algorithm ( &cursor,
182+
&rsa_encryption_algorithm ) == 0 ) {
183+
184+
/* Skip privateKeyAlgorithm */
185+
asn1_skip_any ( &cursor );
186+
187+
/* Enter privateKey */
188+
asn1_enter ( &cursor, ASN1_OCTET_STRING );
189+
190+
/* Enter RSAPrivateKey */
191+
asn1_enter ( &cursor, ASN1_SEQUENCE );
192+
193+
/* Skip version */
194+
asn1_skip ( &cursor, ASN1_INTEGER );
195+
}
196+
180197
} else {
181198

182199
/* Public key */

‎src/include/ipxe/asn1.h

+2
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ extern int asn1_digest_algorithm ( const struct asn1_cursor *cursor,
424424
struct asn1_algorithm **algorithm );
425425
extern int asn1_signature_algorithm ( const struct asn1_cursor *cursor,
426426
struct asn1_algorithm **algorithm );
427+
extern int asn1_check_algorithm ( const struct asn1_cursor *cursor,
428+
struct asn1_algorithm *expected );
427429
extern int asn1_generalized_time ( const struct asn1_cursor *cursor,
428430
time_t *time );
429431
extern int asn1_grow ( struct asn1_builder *builder, size_t extra );

‎src/tests/rsa_test.c

+59-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ struct rsa_signature_test {
206206
sizeof ( bad_signature ) ); \
207207
} while ( 0 )
208208

209-
/** "Hello world" encryption and decryption test */
209+
/** "Hello world" encryption and decryption test (traditional PKCS#1 key) */
210210
RSA_ENCRYPT_DECRYPT_TEST ( hw_test,
211211
PRIVATE ( 0x30, 0x82, 0x01, 0x3b, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00,
212212
0xd2, 0xf1, 0x04, 0x67, 0xf6, 0x2c, 0x96, 0x07, 0xa6, 0xbd,
@@ -260,6 +260,63 @@ RSA_ENCRYPT_DECRYPT_TEST ( hw_test,
260260
0x88, 0x4f, 0xec, 0x43, 0x9c, 0xed, 0xb3, 0xf2, 0x19, 0x89,
261261
0x38, 0x43, 0xf9, 0x41 ) );
262262

263+
/** "Hello world" encryption and decryption test (PKCS#8 key) */
264+
RSA_ENCRYPT_DECRYPT_TEST ( hw_test_pkcs8,
265+
PRIVATE ( 0x30, 0x82, 0x01, 0x55, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06,
266+
0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
267+
0x05, 0x00, 0x04, 0x82, 0x01, 0x3f, 0x30, 0x82, 0x01, 0x3b,
268+
0x02, 0x01, 0x00, 0x02, 0x41, 0x00, 0xd2, 0xf1, 0x04, 0x67,
269+
0xf6, 0x2c, 0x96, 0x07, 0xa6, 0xbd, 0x85, 0xac, 0xc1, 0x17,
270+
0x5d, 0xe8, 0xf0, 0x93, 0x94, 0x0c, 0x45, 0x67, 0x26, 0x67,
271+
0xde, 0x7e, 0xfb, 0xa8, 0xda, 0xbd, 0x07, 0xdf, 0xcf, 0x45,
272+
0x04, 0x6d, 0xbd, 0x69, 0x8b, 0xfb, 0xc1, 0x72, 0xc0, 0xfc,
273+
0x03, 0x04, 0xf2, 0x82, 0xc4, 0x7b, 0x6a, 0x3e, 0xec, 0x53,
274+
0x7a, 0xe3, 0x4e, 0xa8, 0xc9, 0xf9, 0x1f, 0x2a, 0x13, 0x0d,
275+
0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x40, 0x49, 0xb8, 0x61,
276+
0xc9, 0xd3, 0x87, 0x11, 0x87, 0xeb, 0x06, 0x21, 0x49, 0x96,
277+
0xd2, 0x0b, 0xc7, 0xf5, 0x0c, 0x1e, 0x99, 0x8b, 0x47, 0xd9,
278+
0x6c, 0x43, 0x9e, 0x2d, 0x65, 0x7d, 0xcc, 0xc2, 0x8b, 0x1a,
279+
0x6f, 0x2b, 0x55, 0xbe, 0xb3, 0x9f, 0xd1, 0xe2, 0x9a, 0xde,
280+
0x1d, 0xac, 0xec, 0x67, 0xec, 0xa5, 0xbf, 0x9c, 0x30, 0xd6,
281+
0xf9, 0x0a, 0x1a, 0x48, 0xf3, 0xc2, 0x93, 0x3a, 0x17, 0x27,
282+
0x21, 0x02, 0x21, 0x00, 0xfc, 0x8d, 0xfb, 0xee, 0x8a, 0xaa,
283+
0x45, 0x19, 0x4b, 0xf0, 0x68, 0xb0, 0x02, 0x38, 0x3e, 0x03,
284+
0x6b, 0x24, 0x77, 0x20, 0xbd, 0x5e, 0x6c, 0x76, 0xdb, 0xc9,
285+
0xe1, 0x43, 0xa3, 0x40, 0x62, 0x6f, 0x02, 0x21, 0x00, 0xd5,
286+
0xd1, 0xb4, 0x4d, 0x03, 0x40, 0x69, 0x3f, 0x9a, 0xa7, 0x44,
287+
0x15, 0x28, 0x1e, 0xa5, 0x5f, 0xcf, 0x97, 0x21, 0x12, 0xb3,
288+
0xe6, 0x1c, 0x9a, 0x8d, 0xb7, 0xb4, 0x80, 0x3a, 0x9c, 0xb0,
289+
0x43, 0x02, 0x20, 0x71, 0xf0, 0xa0, 0xab, 0x82, 0xf5, 0xc4,
290+
0x8c, 0xe0, 0x1c, 0xcb, 0x2e, 0x35, 0x22, 0x28, 0xa0, 0x24,
291+
0x33, 0x64, 0x67, 0x69, 0xe7, 0xf2, 0xa9, 0x41, 0x09, 0x78,
292+
0x4e, 0xaa, 0x95, 0x3e, 0x93, 0x02, 0x21, 0x00, 0x85, 0xcc,
293+
0x4d, 0xd9, 0x0b, 0x39, 0xd9, 0x22, 0x75, 0xf2, 0x49, 0x46,
294+
0x3b, 0xee, 0xc1, 0x69, 0x6d, 0x0b, 0x93, 0x24, 0x92, 0xf2,
295+
0x61, 0xdf, 0xcc, 0xe2, 0xb1, 0xce, 0xb3, 0xde, 0xac, 0xe5,
296+
0x02, 0x21, 0x00, 0x9c, 0x23, 0x6a, 0x95, 0xa6, 0xfe, 0x1e,
297+
0xd8, 0x0c, 0x3f, 0x6e, 0xe6, 0x0a, 0xeb, 0x97, 0xd6, 0x36,
298+
0x1c, 0x80, 0xc1, 0x02, 0x87, 0x0d, 0x4d, 0xfe, 0x28, 0x02,
299+
0x1e, 0xde, 0xe1, 0xcc, 0x72 ),
300+
PUBLIC ( 0x30, 0x5c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
301+
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x4b, 0x00,
302+
0x30, 0x48, 0x02, 0x41, 0x00, 0xd2, 0xf1, 0x04, 0x67, 0xf6,
303+
0x2c, 0x96, 0x07, 0xa6, 0xbd, 0x85, 0xac, 0xc1, 0x17, 0x5d,
304+
0xe8, 0xf0, 0x93, 0x94, 0x0c, 0x45, 0x67, 0x26, 0x67, 0xde,
305+
0x7e, 0xfb, 0xa8, 0xda, 0xbd, 0x07, 0xdf, 0xcf, 0x45, 0x04,
306+
0x6d, 0xbd, 0x69, 0x8b, 0xfb, 0xc1, 0x72, 0xc0, 0xfc, 0x03,
307+
0x04, 0xf2, 0x82, 0xc4, 0x7b, 0x6a, 0x3e, 0xec, 0x53, 0x7a,
308+
0xe3, 0x4e, 0xa8, 0xc9, 0xf9, 0x1f, 0x2a, 0x13, 0x0d, 0x02,
309+
0x03, 0x01, 0x00, 0x01 ),
310+
PLAINTEXT ( 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c,
311+
0x64, 0x0a ),
312+
CIPHERTEXT ( 0x39, 0xff, 0x5c, 0x54, 0x65, 0x3e, 0x6a, 0xab, 0xc0, 0x62,
313+
0x91, 0xb2, 0xbf, 0x1d, 0x73, 0x5b, 0xd5, 0x4c, 0xbd, 0x16,
314+
0x0f, 0x24, 0xc9, 0xf5, 0xa7, 0xdd, 0x94, 0xd6, 0xf8, 0xae,
315+
0xd3, 0xa0, 0x9f, 0x4d, 0xff, 0x8d, 0x81, 0x34, 0x47, 0xff,
316+
0x2a, 0x87, 0x96, 0xd3, 0x17, 0x5d, 0x93, 0x4d, 0x7b, 0x27,
317+
0x88, 0x4f, 0xec, 0x43, 0x9c, 0xed, 0xb3, 0xf2, 0x19, 0x89,
318+
0x38, 0x43, 0xf9, 0x41 ) );
319+
263320
/** Random message MD5 signature test */
264321
RSA_SIGNATURE_TEST ( md5_test,
265322
PRIVATE ( 0x30, 0x82, 0x01, 0x3b, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00,
@@ -486,6 +543,7 @@ RSA_SIGNATURE_TEST ( sha256_test,
486543
static void rsa_test_exec ( void ) {
487544

488545
rsa_encrypt_decrypt_ok ( &hw_test );
546+
rsa_encrypt_decrypt_ok ( &hw_test_pkcs8 );
489547
rsa_signature_ok ( &md5_test );
490548
rsa_signature_ok ( &sha1_test );
491549
rsa_signature_ok ( &sha256_test );

0 commit comments

Comments
 (0)
Please sign in to comment.