rsa_import_pkcs8
This commit is contained in:
		
							parent
							
								
									2816da42af
								
							
						
					
					
						commit
						d96f4bdcff
					
				| @ -108,6 +108,8 @@ int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key | ||||
| int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key); | ||||
| 
 | ||||
| int rsa_import_x509(const unsigned char *in, unsigned long inlen, rsa_key *key); | ||||
| int rsa_import_pkcs8(const unsigned char *in, unsigned long inlen, | ||||
|                      const unsigned char *passwd, unsigned long passwdlen, rsa_key *key); | ||||
| int rsa_import_radix(int radix, char *N, char *e, char *d, char *p, char *q, char *dP, char *dQ, char *qP, rsa_key *key); | ||||
| #endif | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										149
									
								
								src/pk/rsa/rsa_import_pkcs8.c
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										149
									
								
								src/pk/rsa/rsa_import_pkcs8.c
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,149 @@ | ||||
| /* LibTomCrypt, modular cryptographic library -- Tom St Denis
 | ||||
|  * | ||||
|  * LibTomCrypt is a library that provides various cryptographic | ||||
|  * algorithms in a highly modular and flexible manner. | ||||
|  * | ||||
|  * The library is free for all purposes without any express | ||||
|  * guarantee it works. | ||||
|  * | ||||
|  * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
 | ||||
|  */ | ||||
| #include "tomcrypt.h" | ||||
| 
 | ||||
| /**
 | ||||
|   @file rsa_import_pkcs8.c | ||||
|   Import a PKCS RSA key | ||||
| */ | ||||
| 
 | ||||
| #ifdef LTC_MRSA | ||||
| 
 | ||||
| /* Public-Key Cryptography Standards (PKCS) #8:
 | ||||
|  * Private-Key Information Syntax Specification Version 1.2 | ||||
|  * https://tools.ietf.org/html/rfc5208
 | ||||
|  * | ||||
|  * PrivateKeyInfo ::= SEQUENCE { | ||||
|  *      version                   Version, | ||||
|  *      privateKeyAlgorithm       PrivateKeyAlgorithmIdentifier, | ||||
|  *      privateKey                PrivateKey, | ||||
|  *      attributes           [0]  IMPLICIT Attributes OPTIONAL } | ||||
|  * where: | ||||
|  * - Version ::= INTEGER | ||||
|  * - PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier | ||||
|  * - PrivateKey ::= OCTET STRING | ||||
|  * - Attributes ::= SET OF Attribute | ||||
|  * | ||||
|  * EncryptedPrivateKeyInfo ::= SEQUENCE { | ||||
|  *        encryptionAlgorithm  EncryptionAlgorithmIdentifier, | ||||
|  *        encryptedData        EncryptedData } | ||||
|  * where: | ||||
|  * - EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier | ||||
|  * - EncryptedData ::= OCTET STRING | ||||
|  */ | ||||
| 
 | ||||
| /**
 | ||||
|   Import an RSAPublicKey or RSAPrivateKey in PKCS#8 format | ||||
|   @param in      The packet to import from | ||||
|   @param inlen   It's length (octets) | ||||
|   @param key     [out] Destination for newly imported key | ||||
|   @return CRYPT_OK if successful, upon error allocated memory is freed | ||||
| */ | ||||
| int rsa_import_pkcs8(const unsigned char *in, unsigned long inlen, | ||||
|                      const unsigned char *passwd, unsigned long passwdlen, | ||||
|                      rsa_key *key) | ||||
| { | ||||
|    int           err; | ||||
|    void          *zero, *iter; | ||||
|    unsigned char *buf1 = NULL, *buf2 = NULL; | ||||
|    unsigned long buf1len, buf2len; | ||||
|    unsigned long oid[16]; | ||||
|    oid_st        rsaoid; | ||||
|    ltc_asn1_list alg_seq[2], top_seq[3]; | ||||
|    ltc_asn1_list alg_seq_e[2], key_seq_e[2], top_seq_e[2]; | ||||
|    unsigned char *decrypted = NULL; | ||||
|    unsigned long decryptedlen; | ||||
| 
 | ||||
|    LTC_ARGCHK(in          != NULL); | ||||
|    LTC_ARGCHK(key         != NULL); | ||||
|    LTC_ARGCHK(ltc_mp.name != NULL); | ||||
| 
 | ||||
|    /* get RSA alg oid */ | ||||
|    err = pk_get_oid(PKA_RSA, &rsaoid); | ||||
|    if (err != CRYPT_OK) { goto LBL_NOFREE; } | ||||
| 
 | ||||
|    /* alloc buffers */ | ||||
|    buf1len = inlen; /* approx. */ | ||||
|    buf1 = XMALLOC(buf1len); | ||||
|    if (buf1 == NULL) { err = CRYPT_MEM; goto LBL_NOCLEAR; } | ||||
|    buf2len = inlen; /* approx. */ | ||||
|    buf2 = XMALLOC(buf2len); | ||||
|    if (buf2 == NULL) { err = CRYPT_MEM; goto LBL_FREE; } | ||||
| 
 | ||||
|    /* init key */ | ||||
|    err = mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, &zero, &iter, NULL); | ||||
|    if (err != CRYPT_OK) { goto LBL_NOCLEAR; } | ||||
| 
 | ||||
|    /* try to decode encrypted priv key */ | ||||
|    LTC_SET_ASN1(key_seq_e, 0, LTC_ASN1_OCTET_STRING, buf1, buf1len); | ||||
|    LTC_SET_ASN1(key_seq_e, 1, LTC_ASN1_INTEGER, iter, 1UL); | ||||
|    LTC_SET_ASN1(alg_seq_e, 0, LTC_ASN1_OBJECT_IDENTIFIER, oid, 16UL); | ||||
|    LTC_SET_ASN1(alg_seq_e, 1, LTC_ASN1_SEQUENCE, key_seq_e, 2UL); | ||||
|    LTC_SET_ASN1(top_seq_e, 0, LTC_ASN1_SEQUENCE, alg_seq_e, 2UL); | ||||
|    LTC_SET_ASN1(top_seq_e, 1, LTC_ASN1_OCTET_STRING, buf2, buf2len); | ||||
|    err=der_decode_sequence(in, inlen, top_seq_e, 2UL); | ||||
|    if (err == CRYPT_OK) { | ||||
|       LTC_UNUSED_PARAM(passwd); | ||||
|       LTC_UNUSED_PARAM(passwdlen); | ||||
|       /* XXX: TODO encrypted pkcs8 not implemented yet */ | ||||
|       /* fprintf(stderr, "decrypt: iter=%ld salt.len=%ld encdata.len=%ld\n", mp_get_int(iter), key_seq_e[0].size, top_seq_e[1].size); */ | ||||
|       err = CRYPT_PK_INVALID_TYPE; | ||||
|       goto LBL_ERR; | ||||
|    } | ||||
|    else { | ||||
|       decrypted    = (unsigned char *)in; | ||||
|       decryptedlen = inlen; | ||||
|    } | ||||
| 
 | ||||
|    /* try to decode unencrypted priv key */ | ||||
|    LTC_SET_ASN1(alg_seq, 0, LTC_ASN1_OBJECT_IDENTIFIER, oid, 16UL); | ||||
|    LTC_SET_ASN1(alg_seq, 1, LTC_ASN1_NULL, NULL, 0UL); | ||||
|    LTC_SET_ASN1(top_seq, 0, LTC_ASN1_INTEGER, zero, 1UL); | ||||
|    LTC_SET_ASN1(top_seq, 1, LTC_ASN1_SEQUENCE, alg_seq, 2UL); | ||||
|    LTC_SET_ASN1(top_seq, 2, LTC_ASN1_OCTET_STRING, buf1, buf1len); | ||||
|    err=der_decode_sequence(decrypted, decryptedlen, top_seq, 3UL); | ||||
|    if (err != CRYPT_OK) { goto LBL_ERR; } | ||||
| 
 | ||||
|    /* check alg oid */ | ||||
|    if ((alg_seq[0].size != rsaoid.OIDlen) || | ||||
|       XMEMCMP(rsaoid.OID, alg_seq[0].data, rsaoid.OIDlen * sizeof(rsaoid.OID[0]))) { | ||||
|       err = CRYPT_PK_INVALID_TYPE; | ||||
|       goto LBL_ERR; | ||||
|    } | ||||
| 
 | ||||
|    err = der_decode_sequence_multi(buf1, top_seq[2].size, | ||||
|                                    LTC_ASN1_INTEGER, 1UL, zero, | ||||
|                                    LTC_ASN1_INTEGER, 1UL, key->N, | ||||
|                                    LTC_ASN1_INTEGER, 1UL, key->e, | ||||
|                                    LTC_ASN1_INTEGER, 1UL, key->d, | ||||
|                                    LTC_ASN1_INTEGER, 1UL, key->p, | ||||
|                                    LTC_ASN1_INTEGER, 1UL, key->q, | ||||
|                                    LTC_ASN1_INTEGER, 1UL, key->dP, | ||||
|                                    LTC_ASN1_INTEGER, 1UL, key->dQ, | ||||
|                                    LTC_ASN1_INTEGER, 1UL, key->qP, | ||||
|                                    LTC_ASN1_EOL,     0UL, NULL); | ||||
|    if (err != CRYPT_OK) { goto LBL_ERR; } | ||||
|    mp_clear_multi(zero, iter, NULL); | ||||
|    key->type = PK_PRIVATE; | ||||
|    err = CRYPT_OK; | ||||
|    goto LBL_FREE; | ||||
| 
 | ||||
| LBL_ERR: | ||||
|    mp_clear_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, zero, iter, NULL); | ||||
| LBL_NOCLEAR: | ||||
|    XFREE(buf2); | ||||
| LBL_FREE: | ||||
|    XFREE(buf1); | ||||
| LBL_NOFREE: | ||||
|    return err; | ||||
| } | ||||
| 
 | ||||
| #endif /* LTC_MRSA */ | ||||
| @ -62,6 +62,48 @@ static const unsigned char x509_public_rsa[] = | ||||
|      bv5Aw3hiKsIG3jigKHwmMScgkl3yn+8hLkx6thNbqQoa6Yyo20RqaEFBwlZ5G8lF\ | ||||
|      rZsdeO84SeCH"; | ||||
| 
 | ||||
| static const unsigned char pkcs8_private_rsa[] = { | ||||
|    0x30, 0x82, 0x02, 0x78, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, | ||||
|    0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x02, 0x62, 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01, | ||||
|    0x00, 0x02, 0x81, 0x81, 0x00, 0xcf, 0x9a, 0xde, 0x64, 0x8a, 0xda, 0xc8, 0x33, 0x20, 0xa9, 0xd7, | ||||
|    0x83, 0x31, 0x19, 0x54, 0xb2, 0x9a, 0x85, 0xa7, 0xa1, 0xb7, 0x75, 0x33, 0xb6, 0xa9, 0xac, 0x84, | ||||
|    0x24, 0xb3, 0xde, 0xdb, 0x7d, 0x85, 0x2d, 0x96, 0x65, 0xe5, 0x3f, 0x72, 0x95, 0x24, 0x9f, 0x28, | ||||
|    0x68, 0xca, 0x4f, 0xdb, 0x44, 0x1c, 0x3e, 0x60, 0x12, 0x8a, 0xdd, 0x26, 0xa5, 0xeb, 0xff, 0x0b, | ||||
|    0x5e, 0xd4, 0x88, 0x38, 0x49, 0x2a, 0x6e, 0x5b, 0xbf, 0x12, 0x37, 0x47, 0xbd, 0x05, 0x6b, 0xbc, | ||||
|    0xdb, 0xf3, 0xee, 0xe4, 0x11, 0x8e, 0x41, 0x68, 0x7c, 0x61, 0x13, 0xd7, 0x42, 0xc8, 0x80, 0xbe, | ||||
|    0x36, 0x8f, 0xdc, 0x08, 0x8b, 0x4f, 0xac, 0xa4, 0xe2, 0x76, 0x0c, 0xc9, 0x63, 0x6c, 0x49, 0x58, | ||||
|    0x93, 0xed, 0xcc, 0xaa, 0xdc, 0x25, 0x3b, 0x0a, 0x60, 0x3f, 0x8b, 0x54, 0x3a, 0xc3, 0x4d, 0x31, | ||||
|    0xe7, 0x94, 0xa4, 0x44, 0xfd, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x81, 0x81, 0x00, 0xc8, 0x62, | ||||
|    0xb9, 0xea, 0xde, 0x44, 0x53, 0x1d, 0x56, 0x97, 0xd9, 0x97, 0x9e, 0x1a, 0xcf, 0x30, 0x1e, 0x0a, | ||||
|    0x88, 0x45, 0x86, 0x29, 0x30, 0xa3, 0x4d, 0x9f, 0x61, 0x65, 0x73, 0xe0, 0xd6, 0x87, 0x8f, 0xb6, | ||||
|    0xf3, 0x06, 0xa3, 0x82, 0xdc, 0x7c, 0xac, 0xfe, 0x9b, 0x28, 0x9a, 0xae, 0xfd, 0xfb, 0xfe, 0x2f, | ||||
|    0x0e, 0xd8, 0x97, 0x04, 0xe3, 0xbb, 0x1f, 0xd1, 0xec, 0x0d, 0xba, 0xa3, 0x49, 0x7f, 0x47, 0xac, | ||||
|    0x8a, 0x44, 0x04, 0x7e, 0x86, 0xb7, 0x39, 0x42, 0x3f, 0xad, 0x1e, 0xb7, 0x0e, 0xa5, 0x51, 0xf4, | ||||
|    0x40, 0x63, 0x1e, 0xfd, 0xbd, 0xea, 0x9f, 0x41, 0x9f, 0xa8, 0x90, 0x1d, 0x6f, 0x0a, 0x5a, 0x95, | ||||
|    0x13, 0x11, 0x0d, 0x80, 0xaf, 0x5f, 0x64, 0x98, 0x8a, 0x2c, 0x78, 0x68, 0x65, 0xb0, 0x2b, 0x8b, | ||||
|    0xa2, 0x53, 0x87, 0xca, 0xf1, 0x64, 0x04, 0xab, 0xf2, 0x7b, 0xdb, 0x83, 0xc8, 0x81, 0x02, 0x41, | ||||
|    0x00, 0xf7, 0xbe, 0x5e, 0x23, 0xc3, 0x32, 0x3f, 0xbf, 0x8b, 0x8e, 0x3a, 0xee, 0xfc, 0xfc, 0xcb, | ||||
|    0xe5, 0xf7, 0xf1, 0x0b, 0xbc, 0x42, 0x82, 0xae, 0xd5, 0x7a, 0x3e, 0xca, 0xf7, 0xd5, 0x69, 0x3f, | ||||
|    0x64, 0x25, 0xa2, 0x1f, 0xb7, 0x75, 0x75, 0x05, 0x92, 0x42, 0xeb, 0xb8, 0xf1, 0xf3, 0x0a, 0x05, | ||||
|    0xe3, 0x94, 0xd1, 0x55, 0x78, 0x35, 0xa0, 0x36, 0xa0, 0x9b, 0x7c, 0x92, 0x84, 0x6c, 0xdd, 0xdc, | ||||
|    0x4d, 0x02, 0x41, 0x00, 0xd6, 0x86, 0x0e, 0x85, 0x42, 0x0b, 0x04, 0x08, 0x84, 0x21, 0x60, 0xf0, | ||||
|    0x0e, 0x0d, 0x88, 0xfd, 0x1e, 0x36, 0x10, 0x65, 0x4f, 0x1e, 0x53, 0xb4, 0x08, 0x72, 0x80, 0x5c, | ||||
|    0x3f, 0x59, 0x66, 0x17, 0xe6, 0x98, 0xf2, 0xe9, 0x6c, 0x7a, 0x06, 0x4c, 0xac, 0x76, 0x3d, 0xed, | ||||
|    0x8c, 0xa1, 0xce, 0xad, 0x1b, 0xbd, 0xb4, 0x7d, 0x28, 0xbc, 0xe3, 0x0e, 0x38, 0x8d, 0x99, 0xd8, | ||||
|    0x05, 0xb5, 0xa3, 0x71, 0x02, 0x40, 0x6d, 0xeb, 0xc3, 0x2d, 0x2e, 0xf0, 0x5e, 0xa4, 0x88, 0x31, | ||||
|    0x05, 0x29, 0x00, 0x8a, 0xd1, 0x95, 0x29, 0x9b, 0x83, 0xcf, 0x75, 0xdb, 0x31, 0xe3, 0x7a, 0x27, | ||||
|    0xde, 0x3a, 0x74, 0x30, 0x0c, 0x76, 0x4c, 0xd4, 0x50, 0x2a, 0x40, 0x2d, 0x39, 0xd9, 0x99, 0x63, | ||||
|    0xa9, 0x5d, 0x80, 0xae, 0x53, 0xca, 0x94, 0x3f, 0x05, 0x23, 0x1e, 0xf8, 0x05, 0x04, 0xe1, 0xb8, | ||||
|    0x35, 0xf2, 0x17, 0xb3, 0xa0, 0x89, 0x02, 0x41, 0x00, 0xab, 0x90, 0x88, 0xfa, 0x60, 0x08, 0x29, | ||||
|    0x50, 0x9a, 0x43, 0x8b, 0xa0, 0x50, 0xcc, 0xd8, 0x5a, 0xfe, 0x97, 0x64, 0x63, 0x71, 0x74, 0x22, | ||||
|    0xa3, 0x20, 0x02, 0x5a, 0xcf, 0xeb, 0xc6, 0x16, 0x95, 0x54, 0xd1, 0xcb, 0xab, 0x8d, 0x1a, 0xc6, | ||||
|    0x00, 0xfa, 0x08, 0x92, 0x9c, 0x71, 0xd5, 0x52, 0x52, 0x35, 0x96, 0x71, 0x4b, 0x8b, 0x92, 0x0c, | ||||
|    0xd0, 0xe9, 0xbf, 0xad, 0x63, 0x0b, 0xa5, 0xe9, 0xb1, 0x02, 0x41, 0x00, 0xdc, 0xcc, 0x27, 0xc8, | ||||
|    0xe4, 0xdc, 0x62, 0x48, 0xd5, 0x9b, 0xaf, 0xf5, 0xab, 0x60, 0xf6, 0x21, 0xfd, 0x53, 0xe2, 0xb7, | ||||
|    0x5d, 0x09, 0xc9, 0x1a, 0xa1, 0x04, 0xa9, 0xfc, 0x61, 0x2c, 0x5d, 0x04, 0x58, 0x3a, 0x5a, 0x39, | ||||
|    0xf1, 0x4a, 0x21, 0x56, 0x67, 0xfd, 0xcc, 0x20, 0xa3, 0x8f, 0x78, 0x18, 0x5a, 0x79, 0x3d, 0x2e, | ||||
|    0x8e, 0x7e, 0x86, 0x0a, 0xe6, 0xa8, 0x33, 0xc1, 0x04, 0x17, 0x4a, 0x9f }; | ||||
| 
 | ||||
| /* private keay - hexadecimal */ | ||||
| static char *hex_d = "C862B9EADE44531D5697D9979E1ACF301E0A8845862930A34D9F616573E0D6878FB6F306A382DC7CACFE9B289AAEFDFBFE2F0ED89704E3BB1FD1EC0DBAA3497F47AC8A44047E86B739423FAD1EB70EA551F440631EFDBDEA9F419FA8901D6F0A5A9513110D80AF5F64988A2C786865B02B8BA25387CAF16404ABF27BDB83C881"; | ||||
| static char *hex_dP = "6DEBC32D2EF05EA488310529008AD195299B83CF75DB31E37A27DE3A74300C764CD4502A402D39D99963A95D80AE53CA943F05231EF80504E1B835F217B3A089"; | ||||
| @ -188,6 +230,16 @@ static int rsa_compat_test(void) | ||||
|    } | ||||
|    rsa_free(&key); | ||||
| 
 | ||||
|    /* try import private key in pkcs8 format */ | ||||
|    DO(rsa_import_pkcs8(pkcs8_private_rsa, sizeof(pkcs8_private_rsa), NULL, 0, &key)); | ||||
|    len = sizeof(buf); | ||||
|    DO(rsa_export(buf, &len, PK_PRIVATE, &key)); | ||||
|    if (len != sizeof(openssl_private_rsa) || memcmp(buf, openssl_private_rsa, len)) { | ||||
|       fprintf(stderr, "RSA private export failed to match rsa_import_pkcs8\n"); | ||||
|       return 1; | ||||
|    } | ||||
|    rsa_free(&key); | ||||
| 
 | ||||
|    /* try import private key from raw hexadecimal numbers */ | ||||
|    DO(rsa_import_radix(16, hex_N, hex_e, hex_d, hex_p, hex_q, hex_dP, hex_dQ, hex_qP, &key)); | ||||
|    len = sizeof(buf); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user