aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Brown <mcb30@ipxe.org>2023-06-02 13:49:27 +0100
committerMichael Brown <mcb30@ipxe.org>2023-06-02 13:54:42 +0100
commitb0093571f8bc0207673bb6a6ad5081263e7863b6 (patch)
tree9328631ee9f4a70c18d339d0ce3d20a9ff49a0d1
parent6a7f560e60837fc2ce82a7aa976035656f7d231e (diff)
downloadipxe-b0093571f8bc0207673bb6a6ad5081263e7863b6.zip
ipxe-b0093571f8bc0207673bb6a6ad5081263e7863b6.tar.gz
ipxe-b0093571f8bc0207673bb6a6ad5081263e7863b6.tar.bz2
[crypto] Add support for PKCS#8 private key formatpkcs8
Signed-off-by: Michael Brown <mcb30@ipxe.org>
-rw-r--r--src/crypto/asn1.c26
-rw-r--r--src/crypto/rsa.c19
-rw-r--r--src/include/ipxe/asn1.h2
-rw-r--r--src/tests/rsa_test.c60
4 files changed, 105 insertions, 2 deletions
diff --git a/src/crypto/asn1.c b/src/crypto/asn1.c
index 549ee4d..dc9d1c5 100644
--- a/src/crypto/asn1.c
+++ b/src/crypto/asn1.c
@@ -590,6 +590,32 @@ int asn1_signature_algorithm ( const struct asn1_cursor *cursor,
}
/**
+ * Check ASN.1 OID-identified algorithm
+ *
+ * @v cursor ASN.1 object cursor
+ * @v expected Expected algorithm
+ * @ret rc Return status code
+ */
+int asn1_check_algorithm ( const struct asn1_cursor *cursor,
+ struct asn1_algorithm *expected ) {
+ struct asn1_algorithm *actual;
+ int rc;
+
+ /* Parse algorithm */
+ if ( ( rc = asn1_algorithm ( cursor, &actual ) ) != 0 )
+ return rc;
+
+ /* Check algorithm matches */
+ if ( actual != expected ) {
+ DBGC ( cursor, "ASN1 %p algorithm %s does not match %s\n",
+ cursor, actual->name, expected->name );
+ return -ENOTTY_ALGORITHM;
+ }
+
+ return 0;
+}
+
+/**
* Parse ASN.1 GeneralizedTime
*
* @v cursor ASN.1 cursor
diff --git a/src/crypto/rsa.c b/src/crypto/rsa.c
index a389557..16c67d8 100644
--- a/src/crypto/rsa.c
+++ b/src/crypto/rsa.c
@@ -164,7 +164,7 @@ static int rsa_parse_mod_exp ( struct asn1_cursor *modulus,
int is_private;
int rc;
- /* Enter subjectPublicKeyInfo/RSAPrivateKey */
+ /* Enter subjectPublicKeyInfo/privateKeyInfo/RSAPrivateKey */
memcpy ( &cursor, raw, sizeof ( cursor ) );
asn1_enter ( &cursor, ASN1_SEQUENCE );
@@ -177,6 +177,23 @@ static int rsa_parse_mod_exp ( struct asn1_cursor *modulus,
/* Skip version */
asn1_skip_any ( &cursor );
+ /* Enter privateKey, if present */
+ if ( asn1_check_algorithm ( &cursor,
+ &rsa_encryption_algorithm ) == 0 ) {
+
+ /* Skip privateKeyAlgorithm */
+ asn1_skip_any ( &cursor );
+
+ /* Enter privateKey */
+ asn1_enter ( &cursor, ASN1_OCTET_STRING );
+
+ /* Enter RSAPrivateKey */
+ asn1_enter ( &cursor, ASN1_SEQUENCE );
+
+ /* Skip version */
+ asn1_skip ( &cursor, ASN1_INTEGER );
+ }
+
} else {
/* Public key */
diff --git a/src/include/ipxe/asn1.h b/src/include/ipxe/asn1.h
index fdf06f1..77429f3 100644
--- a/src/include/ipxe/asn1.h
+++ b/src/include/ipxe/asn1.h
@@ -424,6 +424,8 @@ extern int asn1_digest_algorithm ( const struct asn1_cursor *cursor,
struct asn1_algorithm **algorithm );
extern int asn1_signature_algorithm ( const struct asn1_cursor *cursor,
struct asn1_algorithm **algorithm );
+extern int asn1_check_algorithm ( const struct asn1_cursor *cursor,
+ struct asn1_algorithm *expected );
extern int asn1_generalized_time ( const struct asn1_cursor *cursor,
time_t *time );
extern int asn1_grow ( struct asn1_builder *builder, size_t extra );
diff --git a/src/tests/rsa_test.c b/src/tests/rsa_test.c
index c5b587c..46894f6 100644
--- a/src/tests/rsa_test.c
+++ b/src/tests/rsa_test.c
@@ -206,7 +206,7 @@ struct rsa_signature_test {
sizeof ( bad_signature ) ); \
} while ( 0 )
-/** "Hello world" encryption and decryption test */
+/** "Hello world" encryption and decryption test (traditional PKCS#1 key) */
RSA_ENCRYPT_DECRYPT_TEST ( hw_test,
PRIVATE ( 0x30, 0x82, 0x01, 0x3b, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00,
0xd2, 0xf1, 0x04, 0x67, 0xf6, 0x2c, 0x96, 0x07, 0xa6, 0xbd,
@@ -260,6 +260,63 @@ RSA_ENCRYPT_DECRYPT_TEST ( hw_test,
0x88, 0x4f, 0xec, 0x43, 0x9c, 0xed, 0xb3, 0xf2, 0x19, 0x89,
0x38, 0x43, 0xf9, 0x41 ) );
+/** "Hello world" encryption and decryption test (PKCS#8 key) */
+RSA_ENCRYPT_DECRYPT_TEST ( hw_test_pkcs8,
+ PRIVATE ( 0x30, 0x82, 0x01, 0x55, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06,
+ 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
+ 0x05, 0x00, 0x04, 0x82, 0x01, 0x3f, 0x30, 0x82, 0x01, 0x3b,
+ 0x02, 0x01, 0x00, 0x02, 0x41, 0x00, 0xd2, 0xf1, 0x04, 0x67,
+ 0xf6, 0x2c, 0x96, 0x07, 0xa6, 0xbd, 0x85, 0xac, 0xc1, 0x17,
+ 0x5d, 0xe8, 0xf0, 0x93, 0x94, 0x0c, 0x45, 0x67, 0x26, 0x67,
+ 0xde, 0x7e, 0xfb, 0xa8, 0xda, 0xbd, 0x07, 0xdf, 0xcf, 0x45,
+ 0x04, 0x6d, 0xbd, 0x69, 0x8b, 0xfb, 0xc1, 0x72, 0xc0, 0xfc,
+ 0x03, 0x04, 0xf2, 0x82, 0xc4, 0x7b, 0x6a, 0x3e, 0xec, 0x53,
+ 0x7a, 0xe3, 0x4e, 0xa8, 0xc9, 0xf9, 0x1f, 0x2a, 0x13, 0x0d,
+ 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x40, 0x49, 0xb8, 0x61,
+ 0xc9, 0xd3, 0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x6a, 0xab, 0xc0, 0x62,
+ 0x91, 0xb2, 0xbf, 0x1d, 0x73, 0x5b, 0xd5, 0x4c, 0xbd, 0x16,
+ 0x0f, 0x24, 0xc9, 0xf5, 0xa7, 0xdd, 0x94, 0xd6, 0xf8, 0xae,
+ 0xd3, 0xa0, 0x9f, 0x4d, 0xff, 0x8d, 0x81, 0x34, 0x47, 0xff,
+ 0x2a, 0x87, 0x96, 0xd3, 0x17, 0x5d, 0x93, 0x4d, 0x7b, 0x27,
+ 0x88, 0x4f, 0xec, 0x43, 0x9c, 0xed, 0xb3, 0xf2, 0x19, 0x89,
+ 0x38, 0x43, 0xf9, 0x41 ) );
+
/** Random message MD5 signature test */
RSA_SIGNATURE_TEST ( md5_test,
PRIVATE ( 0x30, 0x82, 0x01, 0x3b, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00,
@@ -486,6 +543,7 @@ RSA_SIGNATURE_TEST ( sha256_test,
static void rsa_test_exec ( void ) {
rsa_encrypt_decrypt_ok ( &hw_test );
+ rsa_encrypt_decrypt_ok ( &hw_test_pkcs8 );
rsa_signature_ok ( &md5_test );
rsa_signature_ok ( &sha1_test );
rsa_signature_ok ( &sha256_test );