2 * PKCS #8 (Private-key information syntax)
3 * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
19 struct crypto_private_key * pkcs8_key_import(const u8 *buf, size_t len)
27 /* PKCS #8, Chapter 6 */
29 /* PrivateKeyInfo ::= SEQUENCE */
30 if (asn1_get_next(buf, len, &hdr) < 0 ||
31 hdr.class != ASN1_CLASS_UNIVERSAL ||
32 hdr.tag != ASN1_TAG_SEQUENCE) {
33 wpa_printf(MSG_DEBUG, "PKCS #8: Does not start with PKCS #8 "
34 "header (SEQUENCE); assume PKCS #8 not used");
38 end = pos + hdr.length;
40 /* version Version (Version ::= INTEGER) */
41 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
42 hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_INTEGER) {
43 wpa_printf(MSG_DEBUG, "PKCS #8: Expected INTEGER - found "
44 "class %d tag 0x%x; assume PKCS #8 not used",
53 if (bignum_set_unsigned_bin(zero, hdr.payload, hdr.length) < 0) {
54 wpa_printf(MSG_DEBUG, "PKCS #8: Failed to parse INTEGER");
58 pos = hdr.payload + hdr.length;
60 if (bignum_cmp_d(zero, 0) != 0) {
61 wpa_printf(MSG_DEBUG, "PKCS #8: Expected zero INTEGER in the "
62 "beginning of private key; not found; assume "
69 /* privateKeyAlgorithm PrivateKeyAlgorithmIdentifier
70 * (PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier) */
71 if (asn1_get_next(pos, len, &hdr) < 0 ||
72 hdr.class != ASN1_CLASS_UNIVERSAL ||
73 hdr.tag != ASN1_TAG_SEQUENCE) {
74 wpa_printf(MSG_DEBUG, "PKCS #8: Expected SEQUENCE "
75 "(AlgorithmIdentifier) - found class %d tag 0x%x; "
76 "assume PKCS #8 not used",
81 if (asn1_get_oid(hdr.payload, hdr.length, &oid, &pos)) {
82 wpa_printf(MSG_DEBUG, "PKCS #8: Failed to parse OID "
83 "(algorithm); assume PKCS #8 not used");
87 asn1_oid_to_str(&oid, obuf, sizeof(obuf));
88 wpa_printf(MSG_DEBUG, "PKCS #8: algorithm=%s", obuf);
91 oid.oid[0] != 1 /* iso */ ||
92 oid.oid[1] != 2 /* member-body */ ||
93 oid.oid[2] != 840 /* us */ ||
94 oid.oid[3] != 113549 /* rsadsi */ ||
95 oid.oid[4] != 1 /* pkcs */ ||
96 oid.oid[5] != 1 /* pkcs-1 */ ||
97 oid.oid[6] != 1 /* rsaEncryption */) {
98 wpa_printf(MSG_DEBUG, "PKCS #8: Unsupported private key "
99 "algorithm %s", obuf);
103 pos = hdr.payload + hdr.length;
105 /* privateKey PrivateKey (PrivateKey ::= OCTET STRING) */
106 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
107 hdr.class != ASN1_CLASS_UNIVERSAL ||
108 hdr.tag != ASN1_TAG_OCTETSTRING) {
109 wpa_printf(MSG_DEBUG, "PKCS #8: Expected OCTETSTRING "
110 "(privateKey) - found class %d tag 0x%x",
114 wpa_printf(MSG_DEBUG, "PKCS #8: Try to parse RSAPrivateKey");
116 return (struct crypto_private_key *)
117 crypto_rsa_import_private_key(hdr.payload, hdr.length);
121 struct crypto_private_key *
122 pkcs8_enc_key_import(const u8 *buf, size_t len, const char *passwd)
125 const u8 *pos, *end, *enc_alg;
135 * EncryptedPrivateKeyInfo ::= SEQUENCE {
136 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
137 * encryptedData EncryptedData }
138 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
139 * EncryptedData ::= OCTET STRING
142 if (asn1_get_next(buf, len, &hdr) < 0 ||
143 hdr.class != ASN1_CLASS_UNIVERSAL ||
144 hdr.tag != ASN1_TAG_SEQUENCE) {
145 wpa_printf(MSG_DEBUG, "PKCS #8: Does not start with PKCS #8 "
146 "header (SEQUENCE); assume encrypted PKCS #8 not "
151 end = pos + hdr.length;
153 /* encryptionAlgorithm EncryptionAlgorithmIdentifier */
154 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
155 hdr.class != ASN1_CLASS_UNIVERSAL ||
156 hdr.tag != ASN1_TAG_SEQUENCE) {
157 wpa_printf(MSG_DEBUG, "PKCS #8: Expected SEQUENCE "
158 "(AlgorithmIdentifier) - found class %d tag 0x%x; "
159 "assume encrypted PKCS #8 not used",
163 enc_alg = hdr.payload;
164 enc_alg_len = hdr.length;
165 pos = hdr.payload + hdr.length;
167 /* encryptedData EncryptedData */
168 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
169 hdr.class != ASN1_CLASS_UNIVERSAL ||
170 hdr.tag != ASN1_TAG_OCTETSTRING) {
171 wpa_printf(MSG_DEBUG, "PKCS #8: Expected OCTETSTRING "
172 "(encryptedData) - found class %d tag 0x%x",
177 data = pkcs5_decrypt(enc_alg, enc_alg_len, hdr.payload, hdr.length,
180 struct crypto_private_key *key;
181 key = pkcs8_key_import(data, data_len);