2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/x509.h>
63 #include <openssl/rand.h>
65 static int dsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8inf, EVP_PKEY *pkey);
67 /* Extract a private key from a PKCS8 structure */
69 EVP_PKEY *EVP_PKCS82PKEY (PKCS8_PRIV_KEY_INFO *p8)
71 EVP_PKEY *pkey = NULL;
77 ASN1_INTEGER *privkey;
78 ASN1_TYPE *t1, *t2, *param = NULL;
79 STACK_OF(ASN1_TYPE) *ndsa = NULL;
88 if(p8->pkey->type == V_ASN1_OCTET_STRING) {
89 p8->broken = PKCS8_OK;
90 p = p8->pkey->value.octet_string->data;
91 pkeylen = p8->pkey->value.octet_string->length;
93 p8->broken = PKCS8_NO_OCTET;
94 p = p8->pkey->value.sequence->data;
95 pkeylen = p8->pkey->value.sequence->length;
97 if (!(pkey = EVP_PKEY_new())) {
98 EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
102 switch (OBJ_obj2nid(a->algorithm))
105 case NID_rsaEncryption:
106 if (!(rsa = d2i_RSAPrivateKey (NULL, &p, pkeylen))) {
107 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
110 EVP_PKEY_assign_RSA (pkey, rsa);
115 /* PKCS#8 DSA is weird: you just get a private key integer
116 * and parameters in the AlgorithmIdentifier the pubkey must
120 /* Check for broken DSA PKCS#8, UGH! */
121 if(*p == (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED)) {
122 if(!(ndsa = ASN1_seq_unpack_ASN1_TYPE(p, pkeylen,
125 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
128 if(sk_ASN1_TYPE_num(ndsa) != 2 ) {
129 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
132 /* Handle Two broken types:
133 * SEQUENCE {parameters, priv_key}
134 * SEQUENCE {pub_key, priv_key}
137 t1 = sk_ASN1_TYPE_value(ndsa, 0);
138 t2 = sk_ASN1_TYPE_value(ndsa, 1);
139 if(t1->type == V_ASN1_SEQUENCE) {
140 p8->broken = PKCS8_EMBEDDED_PARAM;
142 } else if(a->parameter->type == V_ASN1_SEQUENCE) {
143 p8->broken = PKCS8_NS_DB;
144 param = a->parameter;
146 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
150 if(t2->type != V_ASN1_INTEGER) {
151 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
154 privkey = t2->value.integer;
156 if (!(privkey=d2i_ASN1_INTEGER (NULL, &p, pkeylen))) {
157 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
160 param = p8->pkeyalg->parameter;
162 if (!param || (param->type != V_ASN1_SEQUENCE)) {
163 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
166 p = param->value.sequence->data;
167 plen = param->value.sequence->length;
168 if (!(dsa = d2i_DSAparams (NULL, &p, plen))) {
169 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
172 /* We have parameters now set private key */
173 if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) {
174 EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_DECODE_ERROR);
177 /* Calculate public key (ouch!) */
178 if (!(dsa->pub_key = BN_new())) {
179 EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
182 if (!(ctx = BN_CTX_new())) {
183 EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
187 if (!BN_mod_exp(dsa->pub_key, dsa->g,
188 dsa->priv_key, dsa->p, ctx)) {
190 EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_PUBKEY_ERROR);
194 EVP_PKEY_assign_DSA(pkey, dsa);
196 if(ndsa) sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
197 else ASN1_INTEGER_free(privkey);
201 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
208 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
209 if (!a->algorithm) strcpy (obj_tmp, "NULL");
210 else i2t_ASN1_OBJECT(obj_tmp, 80, a->algorithm);
211 ERR_add_error_data(2, "TYPE=", obj_tmp);
212 EVP_PKEY_free (pkey);
218 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey)
220 return EVP_PKEY2PKCS8_broken(pkey, PKCS8_OK);
223 /* Turn a private key into a PKCS8 structure */
225 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8_broken(EVP_PKEY *pkey, int broken)
227 PKCS8_PRIV_KEY_INFO *p8;
229 if (!(p8 = PKCS8_PRIV_KEY_INFO_new())) {
230 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
234 ASN1_INTEGER_set (p8->version, 0);
235 if (!(p8->pkeyalg->parameter = ASN1_TYPE_new ())) {
236 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
237 PKCS8_PRIV_KEY_INFO_free (p8);
240 p8->pkey->type = V_ASN1_OCTET_STRING;
241 switch (EVP_PKEY_type(pkey->type)) {
245 if(p8->broken == PKCS8_NO_OCTET) p8->pkey->type = V_ASN1_SEQUENCE;
247 p8->pkeyalg->algorithm = OBJ_nid2obj(NID_rsaEncryption);
248 p8->pkeyalg->parameter->type = V_ASN1_NULL;
249 if (!ASN1_pack_string ((char *)pkey, i2d_PrivateKey,
250 &p8->pkey->value.octet_string)) {
251 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
252 PKCS8_PRIV_KEY_INFO_free (p8);
259 if(!dsa_pkey2pkcs8(p8, pkey)) {
260 PKCS8_PRIV_KEY_INFO_free (p8);
267 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
268 PKCS8_PRIV_KEY_INFO_free (p8);
271 RAND_add(p8->pkey->value.octet_string->data,
272 p8->pkey->value.octet_string->length, 0);
276 PKCS8_PRIV_KEY_INFO *PKCS8_set_broken(PKCS8_PRIV_KEY_INFO *p8, int broken)
281 p8->broken = PKCS8_OK;
286 p8->broken = PKCS8_NO_OCTET;
287 p8->pkey->type = V_ASN1_SEQUENCE;
292 EVPerr(EVP_F_EVP_PKCS8_SET_BROKEN,EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE);
300 static int dsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pkey)
305 STACK_OF(ASN1_TYPE) *ndsa;
306 unsigned char *p, *q;
309 p8->pkeyalg->algorithm = OBJ_nid2obj(NID_dsa);
310 len = i2d_DSAparams (pkey->pkey.dsa, NULL);
311 if (!(p = OPENSSL_malloc(len))) {
312 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
313 PKCS8_PRIV_KEY_INFO_free (p8);
317 i2d_DSAparams (pkey->pkey.dsa, &q);
318 params = ASN1_STRING_new();
319 ASN1_STRING_set(params, p, len);
321 /* Get private key into integer */
322 if (!(prkey = BN_to_ASN1_INTEGER (pkey->pkey.dsa->priv_key, NULL))) {
323 EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
332 if (!ASN1_pack_string((char *)prkey, i2d_ASN1_INTEGER,
333 &p8->pkey->value.octet_string)) {
334 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
335 M_ASN1_INTEGER_free (prkey);
339 M_ASN1_INTEGER_free (prkey);
340 p8->pkeyalg->parameter->value.sequence = params;
341 p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
347 p8->pkeyalg->parameter->value.sequence = params;
348 p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
349 ndsa = sk_ASN1_TYPE_new_null();
350 ttmp = ASN1_TYPE_new();
351 if (!(ttmp->value.integer = BN_to_ASN1_INTEGER (pkey->pkey.dsa->pub_key, NULL))) {
352 EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
353 PKCS8_PRIV_KEY_INFO_free(p8);
356 ttmp->type = V_ASN1_INTEGER;
357 sk_ASN1_TYPE_push(ndsa, ttmp);
359 ttmp = ASN1_TYPE_new();
360 ttmp->value.integer = prkey;
361 ttmp->type = V_ASN1_INTEGER;
362 sk_ASN1_TYPE_push(ndsa, ttmp);
364 p8->pkey->value.octet_string = ASN1_OCTET_STRING_new();
366 if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
367 &p8->pkey->value.octet_string->data,
368 &p8->pkey->value.octet_string->length)) {
370 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
371 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
372 M_ASN1_INTEGER_free(prkey);
375 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
378 case PKCS8_EMBEDDED_PARAM:
380 p8->pkeyalg->parameter->type = V_ASN1_NULL;
381 ndsa = sk_ASN1_TYPE_new_null();
382 ttmp = ASN1_TYPE_new();
383 ttmp->value.sequence = params;
384 ttmp->type = V_ASN1_SEQUENCE;
385 sk_ASN1_TYPE_push(ndsa, ttmp);
387 ttmp = ASN1_TYPE_new();
388 ttmp->value.integer = prkey;
389 ttmp->type = V_ASN1_INTEGER;
390 sk_ASN1_TYPE_push(ndsa, ttmp);
392 p8->pkey->value.octet_string = ASN1_OCTET_STRING_new();
394 if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
395 &p8->pkey->value.octet_string->data,
396 &p8->pkey->value.octet_string->length)) {
398 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
399 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
400 M_ASN1_INTEGER_free (prkey);
403 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);