1999-03-28 17:46:10 +00:00
|
|
|
/* evp_pkey.c */
|
|
|
|
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
|
|
|
|
* project 1999.
|
|
|
|
*/
|
|
|
|
/* ====================================================================
|
2002-02-13 18:21:51 +00:00
|
|
|
* Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved.
|
1999-03-28 17:46:10 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. All advertising materials mentioning features or use of this
|
|
|
|
* software must display the following acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
|
|
|
*
|
|
|
|
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission. For written permission, please contact
|
|
|
|
* licensing@OpenSSL.org.
|
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "OpenSSL"
|
|
|
|
* nor may "OpenSSL" appear in their names without prior written
|
|
|
|
* permission of the OpenSSL Project.
|
|
|
|
*
|
|
|
|
* 6. Redistributions of any form whatsoever must retain the following
|
|
|
|
* acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
|
|
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*
|
|
|
|
* This product includes cryptographic software written by Eric Young
|
|
|
|
* (eay@cryptsoft.com). This product includes software written by Tim
|
|
|
|
* Hudson (tjh@cryptsoft.com).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/x509.h>
|
|
|
|
#include <openssl/rand.h>
|
2004-04-19 18:09:28 +00:00
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/dsa.h>
|
2004-05-17 19:14:22 +00:00
|
|
|
#include <openssl/bn.h>
|
1999-03-28 17:46:10 +00:00
|
|
|
|
2002-01-02 12:45:51 +00:00
|
|
|
#ifndef OPENSSL_NO_DSA
|
2000-02-05 21:07:56 +00:00
|
|
|
static int dsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8inf, EVP_PKEY *pkey);
|
2002-01-02 12:45:51 +00:00
|
|
|
#endif
|
2002-08-07 10:49:54 +00:00
|
|
|
#ifndef OPENSSL_NO_EC
|
|
|
|
static int eckey_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8inf, EVP_PKEY *pkey);
|
2002-02-13 18:21:51 +00:00
|
|
|
#endif
|
2000-02-05 21:07:56 +00:00
|
|
|
|
1999-03-28 17:46:10 +00:00
|
|
|
/* Extract a private key from a PKCS8 structure */
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
EVP_PKEY *EVP_PKCS82PKEY (PKCS8_PRIV_KEY_INFO *p8)
|
1999-03-28 17:46:10 +00:00
|
|
|
{
|
2000-02-05 21:07:56 +00:00
|
|
|
EVP_PKEY *pkey = NULL;
|
2001-02-19 16:06:34 +00:00
|
|
|
#ifndef OPENSSL_NO_RSA
|
2000-02-05 21:07:56 +00:00
|
|
|
RSA *rsa = NULL;
|
1999-04-27 01:14:46 +00:00
|
|
|
#endif
|
2001-02-19 16:06:34 +00:00
|
|
|
#ifndef OPENSSL_NO_DSA
|
2000-02-05 21:07:56 +00:00
|
|
|
DSA *dsa = NULL;
|
2002-10-28 14:13:38 +00:00
|
|
|
ASN1_TYPE *t1, *t2;
|
2003-06-25 21:35:05 +00:00
|
|
|
ASN1_INTEGER *privkey;
|
2002-10-28 14:13:38 +00:00
|
|
|
STACK_OF(ASN1_TYPE) *ndsa = NULL;
|
2002-02-13 18:21:51 +00:00
|
|
|
#endif
|
2002-08-07 10:49:54 +00:00
|
|
|
#ifndef OPENSSL_NO_EC
|
|
|
|
EC_KEY *eckey = NULL;
|
2003-06-25 21:35:05 +00:00
|
|
|
const unsigned char *p_tmp;
|
2002-02-13 18:21:51 +00:00
|
|
|
#endif
|
2002-08-12 08:47:41 +00:00
|
|
|
#if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
|
2002-10-28 14:13:38 +00:00
|
|
|
ASN1_TYPE *param = NULL;
|
2000-02-05 21:07:56 +00:00
|
|
|
BN_CTX *ctx = NULL;
|
1999-04-27 01:14:46 +00:00
|
|
|
int plen;
|
|
|
|
#endif
|
|
|
|
X509_ALGOR *a;
|
2004-03-15 23:15:26 +00:00
|
|
|
const unsigned char *p;
|
2000-11-06 22:49:05 +00:00
|
|
|
const unsigned char *cp;
|
1999-04-27 01:14:46 +00:00
|
|
|
int pkeylen;
|
2002-02-13 18:21:51 +00:00
|
|
|
int nid;
|
1999-03-28 17:46:10 +00:00
|
|
|
char obj_tmp[80];
|
|
|
|
|
2000-02-05 21:07:56 +00:00
|
|
|
if(p8->pkey->type == V_ASN1_OCTET_STRING) {
|
|
|
|
p8->broken = PKCS8_OK;
|
1999-03-28 17:46:10 +00:00
|
|
|
p = p8->pkey->value.octet_string->data;
|
|
|
|
pkeylen = p8->pkey->value.octet_string->length;
|
2000-02-05 21:07:56 +00:00
|
|
|
} else {
|
|
|
|
p8->broken = PKCS8_NO_OCTET;
|
1999-03-28 17:46:10 +00:00
|
|
|
p = p8->pkey->value.sequence->data;
|
|
|
|
pkeylen = p8->pkey->value.sequence->length;
|
|
|
|
}
|
|
|
|
if (!(pkey = EVP_PKEY_new())) {
|
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
a = p8->pkeyalg;
|
2002-02-13 18:21:51 +00:00
|
|
|
nid = OBJ_obj2nid(a->algorithm);
|
|
|
|
switch(nid)
|
1999-03-28 17:46:10 +00:00
|
|
|
{
|
2001-02-19 16:06:34 +00:00
|
|
|
#ifndef OPENSSL_NO_RSA
|
1999-03-28 17:46:10 +00:00
|
|
|
case NID_rsaEncryption:
|
2000-11-06 22:49:05 +00:00
|
|
|
cp = p;
|
|
|
|
if (!(rsa = d2i_RSAPrivateKey (NULL,&cp, pkeylen))) {
|
1999-03-28 17:46:10 +00:00
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EVP_PKEY_assign_RSA (pkey, rsa);
|
|
|
|
break;
|
1999-04-27 01:14:46 +00:00
|
|
|
#endif
|
2002-10-28 14:13:38 +00:00
|
|
|
#ifndef OPENSSL_NO_DSA
|
1999-03-28 17:46:10 +00:00
|
|
|
case NID_dsa:
|
2002-10-28 14:13:38 +00:00
|
|
|
/* PKCS#8 DSA is weird: you just get a private key integer
|
1999-03-28 17:46:10 +00:00
|
|
|
* and parameters in the AlgorithmIdentifier the pubkey must
|
|
|
|
* be recalculated.
|
|
|
|
*/
|
|
|
|
|
2002-10-28 14:13:38 +00:00
|
|
|
/* Check for broken DSA PKCS#8, UGH! */
|
|
|
|
if(*p == (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED)) {
|
|
|
|
if(!(ndsa = ASN1_seq_unpack_ASN1_TYPE(p, pkeylen,
|
2000-05-16 23:01:19 +00:00
|
|
|
d2i_ASN1_TYPE,
|
2002-10-28 14:13:38 +00:00
|
|
|
ASN1_TYPE_free))) {
|
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
|
|
|
|
goto dsaerr;
|
|
|
|
}
|
|
|
|
if(sk_ASN1_TYPE_num(ndsa) != 2 ) {
|
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
|
|
|
|
goto dsaerr;
|
|
|
|
}
|
2000-02-05 21:07:56 +00:00
|
|
|
/* Handle Two broken types:
|
|
|
|
* SEQUENCE {parameters, priv_key}
|
|
|
|
* SEQUENCE {pub_key, priv_key}
|
|
|
|
*/
|
2000-03-07 01:03:33 +00:00
|
|
|
|
2002-10-28 14:13:38 +00:00
|
|
|
t1 = sk_ASN1_TYPE_value(ndsa, 0);
|
|
|
|
t2 = sk_ASN1_TYPE_value(ndsa, 1);
|
|
|
|
if(t1->type == V_ASN1_SEQUENCE) {
|
2000-02-05 21:07:56 +00:00
|
|
|
p8->broken = PKCS8_EMBEDDED_PARAM;
|
|
|
|
param = t1;
|
2002-10-28 14:13:38 +00:00
|
|
|
} else if(a->parameter->type == V_ASN1_SEQUENCE) {
|
2000-02-05 21:07:56 +00:00
|
|
|
p8->broken = PKCS8_NS_DB;
|
|
|
|
param = a->parameter;
|
2002-10-28 14:13:38 +00:00
|
|
|
} else {
|
1999-03-28 17:46:10 +00:00
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
|
2002-10-28 14:13:38 +00:00
|
|
|
goto dsaerr;
|
2000-02-05 21:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(t2->type != V_ASN1_INTEGER) {
|
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
|
2002-10-28 14:13:38 +00:00
|
|
|
goto dsaerr;
|
2000-02-05 21:07:56 +00:00
|
|
|
}
|
|
|
|
privkey = t2->value.integer;
|
2002-10-28 14:13:38 +00:00
|
|
|
} else {
|
|
|
|
if (!(privkey=d2i_ASN1_INTEGER (NULL, &p, pkeylen))) {
|
2000-03-07 01:03:33 +00:00
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
|
2002-10-28 14:13:38 +00:00
|
|
|
goto dsaerr;
|
2000-03-07 01:03:33 +00:00
|
|
|
}
|
|
|
|
param = p8->pkeyalg->parameter;
|
|
|
|
}
|
2002-10-28 14:13:38 +00:00
|
|
|
if (!param || (param->type != V_ASN1_SEQUENCE)) {
|
2000-02-05 21:07:56 +00:00
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
|
2002-10-28 14:13:38 +00:00
|
|
|
goto dsaerr;
|
1999-03-28 17:46:10 +00:00
|
|
|
}
|
2000-11-07 13:54:39 +00:00
|
|
|
cp = p = param->value.sequence->data;
|
2000-02-05 21:07:56 +00:00
|
|
|
plen = param->value.sequence->length;
|
2002-10-28 14:13:38 +00:00
|
|
|
if (!(dsa = d2i_DSAparams (NULL, &cp, plen))) {
|
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
|
|
|
|
goto dsaerr;
|
|
|
|
}
|
|
|
|
/* We have parameters now set private key */
|
|
|
|
if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) {
|
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_DECODE_ERROR);
|
|
|
|
goto dsaerr;
|
|
|
|
}
|
|
|
|
/* Calculate public key (ouch!) */
|
|
|
|
if (!(dsa->pub_key = BN_new())) {
|
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
|
|
|
|
goto dsaerr;
|
|
|
|
}
|
|
|
|
if (!(ctx = BN_CTX_new())) {
|
1999-03-28 17:46:10 +00:00
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
|
2002-10-28 14:13:38 +00:00
|
|
|
goto dsaerr;
|
1999-03-28 17:46:10 +00:00
|
|
|
}
|
2002-10-28 14:13:38 +00:00
|
|
|
|
|
|
|
if (!BN_mod_exp(dsa->pub_key, dsa->g,
|
|
|
|
dsa->priv_key, dsa->p, ctx)) {
|
|
|
|
|
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_PUBKEY_ERROR);
|
|
|
|
goto dsaerr;
|
|
|
|
}
|
|
|
|
|
|
|
|
EVP_PKEY_assign_DSA(pkey, dsa);
|
|
|
|
BN_CTX_free (ctx);
|
|
|
|
if(ndsa) sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
|
|
|
|
else ASN1_INTEGER_free(privkey);
|
|
|
|
break;
|
|
|
|
dsaerr:
|
|
|
|
BN_CTX_free (ctx);
|
|
|
|
sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
|
|
|
|
DSA_free(dsa);
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifndef OPENSSL_NO_EC
|
|
|
|
case NID_X9_62_id_ecPublicKey:
|
2003-06-25 21:35:05 +00:00
|
|
|
p_tmp = p;
|
|
|
|
/* extract the ec parameters */
|
2002-10-28 14:13:38 +00:00
|
|
|
param = p8->pkeyalg->parameter;
|
2002-02-13 18:21:51 +00:00
|
|
|
|
2002-10-28 14:13:38 +00:00
|
|
|
if (!param || ((param->type != V_ASN1_SEQUENCE) &&
|
|
|
|
(param->type != V_ASN1_OBJECT)))
|
2002-02-13 18:21:51 +00:00
|
|
|
{
|
2002-10-28 14:13:38 +00:00
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
|
|
|
|
goto ecerr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param->type == V_ASN1_SEQUENCE)
|
|
|
|
{
|
|
|
|
cp = p = param->value.sequence->data;
|
|
|
|
plen = param->value.sequence->length;
|
|
|
|
|
|
|
|
if (!(eckey = d2i_ECParameters(NULL, &cp, plen)))
|
2002-02-13 18:21:51 +00:00
|
|
|
{
|
2002-10-28 14:13:38 +00:00
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY,
|
|
|
|
EVP_R_DECODE_ERROR);
|
|
|
|
goto ecerr;
|
2002-02-13 18:21:51 +00:00
|
|
|
}
|
2002-10-28 14:13:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cp = p = param->value.object->data;
|
|
|
|
plen = param->value.object->length;
|
|
|
|
|
|
|
|
/* type == V_ASN1_OBJECT => the parameters are given
|
|
|
|
* by an asn1 OID
|
|
|
|
*/
|
|
|
|
if ((eckey = EC_KEY_new()) == NULL)
|
2002-02-13 18:21:51 +00:00
|
|
|
{
|
2002-10-28 14:13:38 +00:00
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY,
|
|
|
|
ERR_R_MALLOC_FAILURE);
|
|
|
|
goto ecerr;
|
2002-02-13 18:21:51 +00:00
|
|
|
}
|
2002-10-28 14:13:38 +00:00
|
|
|
if ((eckey->group = EC_GROUP_new_by_nid(
|
|
|
|
OBJ_obj2nid(a->parameter->value.object))) == NULL)
|
|
|
|
goto ecerr;
|
|
|
|
EC_GROUP_set_asn1_flag(eckey->group,
|
|
|
|
OPENSSL_EC_NAMED_CURVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have parameters now set private key */
|
2003-06-25 21:35:05 +00:00
|
|
|
if (!d2i_ECPrivateKey(&eckey, &p_tmp, pkeylen))
|
2002-10-28 14:13:38 +00:00
|
|
|
{
|
2003-06-25 21:35:05 +00:00
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
|
2002-10-28 14:13:38 +00:00
|
|
|
goto ecerr;
|
|
|
|
}
|
2003-06-25 21:35:05 +00:00
|
|
|
|
|
|
|
/* calculate public key (if necessary) */
|
|
|
|
if (!eckey->pub_key)
|
2002-10-28 14:13:38 +00:00
|
|
|
{
|
2003-06-25 21:35:05 +00:00
|
|
|
/* the public key was not included in the SEC1 private
|
|
|
|
* key => calculate the public key */
|
|
|
|
eckey->pub_key = EC_POINT_new(eckey->group);
|
|
|
|
if (!eckey->pub_key)
|
|
|
|
{
|
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
|
|
|
|
goto ecerr;
|
|
|
|
}
|
|
|
|
if (!EC_POINT_copy(eckey->pub_key,
|
|
|
|
EC_GROUP_get0_generator(eckey->group)))
|
|
|
|
{
|
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
|
|
|
|
goto ecerr;
|
|
|
|
}
|
|
|
|
if (!EC_POINT_mul(eckey->group, eckey->pub_key,
|
|
|
|
eckey->priv_key, NULL, NULL, ctx))
|
|
|
|
{
|
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
|
|
|
|
goto ecerr;
|
|
|
|
}
|
2002-10-28 14:13:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EVP_PKEY_assign_EC_KEY(pkey, eckey);
|
|
|
|
if (ctx)
|
|
|
|
BN_CTX_free(ctx);
|
2000-02-05 21:07:56 +00:00
|
|
|
break;
|
2002-10-28 14:13:38 +00:00
|
|
|
ecerr:
|
|
|
|
if (ctx)
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
if (eckey)
|
2002-08-07 10:49:54 +00:00
|
|
|
EC_KEY_free(eckey);
|
2002-10-28 14:13:38 +00:00
|
|
|
if (pkey)
|
|
|
|
EVP_PKEY_free(pkey);
|
2000-02-05 21:07:56 +00:00
|
|
|
return NULL;
|
1999-04-27 01:14:46 +00:00
|
|
|
#endif
|
1999-03-28 17:46:10 +00:00
|
|
|
default:
|
|
|
|
EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
|
2003-12-27 14:40:17 +00:00
|
|
|
if (!a->algorithm) BUF_strlcpy (obj_tmp, "NULL", sizeof obj_tmp);
|
1999-03-28 17:46:10 +00:00
|
|
|
else i2t_ASN1_OBJECT(obj_tmp, 80, a->algorithm);
|
|
|
|
ERR_add_error_data(2, "TYPE=", obj_tmp);
|
|
|
|
EVP_PKEY_free (pkey);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return pkey;
|
|
|
|
}
|
|
|
|
|
2000-02-05 21:07:56 +00:00
|
|
|
PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey)
|
|
|
|
{
|
|
|
|
return EVP_PKEY2PKCS8_broken(pkey, PKCS8_OK);
|
|
|
|
}
|
|
|
|
|
1999-03-28 17:46:10 +00:00
|
|
|
/* Turn a private key into a PKCS8 structure */
|
|
|
|
|
2000-02-05 21:07:56 +00:00
|
|
|
PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8_broken(EVP_PKEY *pkey, int broken)
|
1999-03-28 17:46:10 +00:00
|
|
|
{
|
|
|
|
PKCS8_PRIV_KEY_INFO *p8;
|
2000-02-05 21:07:56 +00:00
|
|
|
|
1999-03-28 17:46:10 +00:00
|
|
|
if (!(p8 = PKCS8_PRIV_KEY_INFO_new())) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-02-05 21:07:56 +00:00
|
|
|
p8->broken = broken;
|
2004-12-05 01:03:15 +00:00
|
|
|
if (!ASN1_INTEGER_set(p8->version, 0)) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
PKCS8_PRIV_KEY_INFO_free (p8);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-03-28 17:46:10 +00:00
|
|
|
if (!(p8->pkeyalg->parameter = ASN1_TYPE_new ())) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
PKCS8_PRIV_KEY_INFO_free (p8);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-02-05 21:07:56 +00:00
|
|
|
p8->pkey->type = V_ASN1_OCTET_STRING;
|
1999-03-28 17:46:10 +00:00
|
|
|
switch (EVP_PKEY_type(pkey->type)) {
|
2001-02-19 16:06:34 +00:00
|
|
|
#ifndef OPENSSL_NO_RSA
|
1999-03-28 17:46:10 +00:00
|
|
|
case EVP_PKEY_RSA:
|
|
|
|
|
2000-02-05 21:07:56 +00:00
|
|
|
if(p8->broken == PKCS8_NO_OCTET) p8->pkey->type = V_ASN1_SEQUENCE;
|
|
|
|
|
1999-03-28 17:46:10 +00:00
|
|
|
p8->pkeyalg->algorithm = OBJ_nid2obj(NID_rsaEncryption);
|
|
|
|
p8->pkeyalg->parameter->type = V_ASN1_NULL;
|
|
|
|
if (!ASN1_pack_string ((char *)pkey, i2d_PrivateKey,
|
|
|
|
&p8->pkey->value.octet_string)) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
PKCS8_PRIV_KEY_INFO_free (p8);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
1999-04-27 01:14:46 +00:00
|
|
|
#endif
|
2001-02-19 16:06:34 +00:00
|
|
|
#ifndef OPENSSL_NO_DSA
|
1999-03-28 17:46:10 +00:00
|
|
|
case EVP_PKEY_DSA:
|
2000-02-05 21:07:56 +00:00
|
|
|
if(!dsa_pkey2pkcs8(p8, pkey)) {
|
1999-03-28 17:46:10 +00:00
|
|
|
PKCS8_PRIV_KEY_INFO_free (p8);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-02-05 21:07:56 +00:00
|
|
|
|
1999-03-28 17:46:10 +00:00
|
|
|
break;
|
1999-04-27 01:14:46 +00:00
|
|
|
#endif
|
2002-08-12 08:47:41 +00:00
|
|
|
#ifndef OPENSSL_NO_EC
|
|
|
|
case EVP_PKEY_EC:
|
2002-08-07 10:49:54 +00:00
|
|
|
if (!eckey_pkey2pkcs8(p8, pkey))
|
2002-02-13 18:21:51 +00:00
|
|
|
{
|
|
|
|
PKCS8_PRIV_KEY_INFO_free(p8);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
1999-03-28 17:46:10 +00:00
|
|
|
default:
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
|
|
|
|
PKCS8_PRIV_KEY_INFO_free (p8);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-01-13 20:59:17 +00:00
|
|
|
RAND_add(p8->pkey->value.octet_string->data,
|
2004-03-15 23:15:26 +00:00
|
|
|
p8->pkey->value.octet_string->length, 0.0);
|
1999-03-28 17:46:10 +00:00
|
|
|
return p8;
|
|
|
|
}
|
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
PKCS8_PRIV_KEY_INFO *PKCS8_set_broken(PKCS8_PRIV_KEY_INFO *p8, int broken)
|
1999-03-28 17:46:10 +00:00
|
|
|
{
|
|
|
|
switch (broken) {
|
|
|
|
|
|
|
|
case PKCS8_OK:
|
|
|
|
p8->broken = PKCS8_OK;
|
|
|
|
return p8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PKCS8_NO_OCTET:
|
|
|
|
p8->broken = PKCS8_NO_OCTET;
|
|
|
|
p8->pkey->type = V_ASN1_SEQUENCE;
|
|
|
|
return p8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
EVPerr(EVP_F_EVP_PKCS8_SET_BROKEN,EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE);
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-19 16:06:34 +00:00
|
|
|
#ifndef OPENSSL_NO_DSA
|
2000-02-05 21:07:56 +00:00
|
|
|
static int dsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pkey)
|
|
|
|
{
|
2004-12-05 01:03:15 +00:00
|
|
|
ASN1_STRING *params = NULL;
|
|
|
|
ASN1_INTEGER *prkey = NULL;
|
|
|
|
ASN1_TYPE *ttmp = NULL;
|
|
|
|
STACK_OF(ASN1_TYPE) *ndsa = NULL;
|
|
|
|
unsigned char *p = NULL, *q;
|
2000-02-05 21:07:56 +00:00
|
|
|
int len;
|
2000-05-16 23:01:19 +00:00
|
|
|
|
2000-02-05 21:07:56 +00:00
|
|
|
p8->pkeyalg->algorithm = OBJ_nid2obj(NID_dsa);
|
|
|
|
len = i2d_DSAparams (pkey->pkey.dsa, NULL);
|
2000-06-01 22:19:21 +00:00
|
|
|
if (!(p = OPENSSL_malloc(len))) {
|
2000-02-05 21:07:56 +00:00
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
2004-12-05 01:03:15 +00:00
|
|
|
goto err;
|
2000-02-05 21:07:56 +00:00
|
|
|
}
|
|
|
|
q = p;
|
|
|
|
i2d_DSAparams (pkey->pkey.dsa, &q);
|
2004-12-05 01:03:15 +00:00
|
|
|
if (!(params = ASN1_STRING_new())) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!ASN1_STRING_set(params, p, len)) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2000-06-01 22:19:21 +00:00
|
|
|
OPENSSL_free(p);
|
2004-12-05 01:03:15 +00:00
|
|
|
p = NULL;
|
2000-02-05 21:07:56 +00:00
|
|
|
/* Get private key into integer */
|
|
|
|
if (!(prkey = BN_to_ASN1_INTEGER (pkey->pkey.dsa->priv_key, NULL))) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
|
2004-12-05 01:03:15 +00:00
|
|
|
goto err;
|
2000-02-05 21:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch(p8->broken) {
|
1999-03-28 17:46:10 +00:00
|
|
|
|
2000-02-05 21:07:56 +00:00
|
|
|
case PKCS8_OK:
|
|
|
|
case PKCS8_NO_OCTET:
|
|
|
|
|
|
|
|
if (!ASN1_pack_string((char *)prkey, i2d_ASN1_INTEGER,
|
|
|
|
&p8->pkey->value.octet_string)) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
2004-12-05 01:03:15 +00:00
|
|
|
goto err;
|
2000-02-05 21:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
M_ASN1_INTEGER_free (prkey);
|
2004-12-05 01:03:15 +00:00
|
|
|
prkey = NULL;
|
2000-02-05 21:07:56 +00:00
|
|
|
p8->pkeyalg->parameter->value.sequence = params;
|
2004-12-05 01:03:15 +00:00
|
|
|
params = NULL;
|
2000-02-05 21:07:56 +00:00
|
|
|
p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PKCS8_NS_DB:
|
|
|
|
|
|
|
|
p8->pkeyalg->parameter->value.sequence = params;
|
2004-12-05 01:03:15 +00:00
|
|
|
params = NULL;
|
2000-02-05 21:07:56 +00:00
|
|
|
p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
|
2004-12-05 01:03:15 +00:00
|
|
|
if (!(ndsa = sk_ASN1_TYPE_new_null())) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!(ttmp = ASN1_TYPE_new())) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!(ttmp->value.integer =
|
|
|
|
BN_to_ASN1_INTEGER(pkey->pkey.dsa->pub_key, NULL))) {
|
2000-02-05 21:07:56 +00:00
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
|
2004-12-05 01:03:15 +00:00
|
|
|
goto err;
|
2000-02-05 21:07:56 +00:00
|
|
|
}
|
|
|
|
ttmp->type = V_ASN1_INTEGER;
|
2004-12-05 01:03:15 +00:00
|
|
|
if (!sk_ASN1_TYPE_push(ndsa, ttmp)) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2000-02-05 21:07:56 +00:00
|
|
|
|
2004-12-05 01:03:15 +00:00
|
|
|
if (!(ttmp = ASN1_TYPE_new())) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2000-02-05 21:07:56 +00:00
|
|
|
ttmp->value.integer = prkey;
|
2004-12-05 01:03:15 +00:00
|
|
|
prkey = NULL;
|
2000-02-05 21:07:56 +00:00
|
|
|
ttmp->type = V_ASN1_INTEGER;
|
2004-12-05 01:03:15 +00:00
|
|
|
if (!sk_ASN1_TYPE_push(ndsa, ttmp)) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ttmp = NULL;
|
2000-02-05 21:07:56 +00:00
|
|
|
|
2004-12-05 01:03:15 +00:00
|
|
|
if (!(p8->pkey->value.octet_string = ASN1_OCTET_STRING_new())) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2000-02-05 21:07:56 +00:00
|
|
|
|
2000-05-16 23:01:19 +00:00
|
|
|
if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
|
2000-02-05 21:07:56 +00:00
|
|
|
&p8->pkey->value.octet_string->data,
|
|
|
|
&p8->pkey->value.octet_string->length)) {
|
|
|
|
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
2004-12-05 01:03:15 +00:00
|
|
|
goto err;
|
2000-02-05 21:07:56 +00:00
|
|
|
}
|
2000-05-16 23:01:19 +00:00
|
|
|
sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
|
2000-02-05 21:07:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PKCS8_EMBEDDED_PARAM:
|
|
|
|
|
|
|
|
p8->pkeyalg->parameter->type = V_ASN1_NULL;
|
2004-12-05 01:03:15 +00:00
|
|
|
if (!(ndsa = sk_ASN1_TYPE_new_null())) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!(ttmp = ASN1_TYPE_new())) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2000-02-05 21:07:56 +00:00
|
|
|
ttmp->value.sequence = params;
|
2004-12-05 01:03:15 +00:00
|
|
|
params = NULL;
|
2000-02-05 21:07:56 +00:00
|
|
|
ttmp->type = V_ASN1_SEQUENCE;
|
2004-12-05 01:03:15 +00:00
|
|
|
if (!sk_ASN1_TYPE_push(ndsa, ttmp)) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2000-02-05 21:07:56 +00:00
|
|
|
|
2004-12-05 01:03:15 +00:00
|
|
|
if (!(ttmp = ASN1_TYPE_new())) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2000-02-05 21:07:56 +00:00
|
|
|
ttmp->value.integer = prkey;
|
2004-12-05 01:03:15 +00:00
|
|
|
prkey = NULL;
|
2000-02-05 21:07:56 +00:00
|
|
|
ttmp->type = V_ASN1_INTEGER;
|
2004-12-05 01:03:15 +00:00
|
|
|
if (!sk_ASN1_TYPE_push(ndsa, ttmp)) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ttmp = NULL;
|
2000-02-05 21:07:56 +00:00
|
|
|
|
2004-12-05 01:03:15 +00:00
|
|
|
if (!(p8->pkey->value.octet_string = ASN1_OCTET_STRING_new())) {
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2000-02-05 21:07:56 +00:00
|
|
|
|
2000-05-16 23:01:19 +00:00
|
|
|
if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
|
2000-02-05 21:07:56 +00:00
|
|
|
&p8->pkey->value.octet_string->data,
|
|
|
|
&p8->pkey->value.octet_string->length)) {
|
|
|
|
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
|
2004-12-05 01:03:15 +00:00
|
|
|
goto err;
|
2000-02-05 21:07:56 +00:00
|
|
|
}
|
2000-05-16 23:01:19 +00:00
|
|
|
sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
|
2000-02-05 21:07:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
2004-12-05 01:03:15 +00:00
|
|
|
err:
|
|
|
|
if (p != NULL) OPENSSL_free(p);
|
|
|
|
if (params != NULL) ASN1_STRING_free(params);
|
|
|
|
if (prkey != NULL) M_ASN1_INTEGER_free(prkey);
|
|
|
|
if (ttmp != NULL) ASN1_TYPE_free(ttmp);
|
|
|
|
if (ndsa != NULL) sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
|
|
|
|
return 0;
|
2000-02-05 21:07:56 +00:00
|
|
|
}
|
|
|
|
#endif
|
2002-02-13 18:21:51 +00:00
|
|
|
|
2002-08-07 10:49:54 +00:00
|
|
|
#ifndef OPENSSL_NO_EC
|
|
|
|
static int eckey_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pkey)
|
2002-02-13 18:21:51 +00:00
|
|
|
{
|
2002-10-28 14:13:38 +00:00
|
|
|
EC_KEY *eckey;
|
|
|
|
unsigned char *p, *pp;
|
2003-06-25 21:35:05 +00:00
|
|
|
int nid, i, ret = 0;
|
|
|
|
unsigned int tmp_flags;
|
2002-02-13 18:21:51 +00:00
|
|
|
|
2002-08-07 10:49:54 +00:00
|
|
|
if (pkey->pkey.eckey == NULL || pkey->pkey.eckey->group == NULL)
|
2002-02-13 18:21:51 +00:00
|
|
|
{
|
2002-08-07 10:49:54 +00:00
|
|
|
EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, EVP_R_MISSING_PARAMETERS);
|
2002-02-13 18:21:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-10-28 14:13:38 +00:00
|
|
|
eckey = pkey->pkey.eckey;
|
|
|
|
|
|
|
|
/* set the ec parameters OID */
|
|
|
|
if (p8->pkeyalg->algorithm)
|
|
|
|
ASN1_OBJECT_free(p8->pkeyalg->algorithm);
|
|
|
|
|
|
|
|
p8->pkeyalg->algorithm = OBJ_nid2obj(NID_X9_62_id_ecPublicKey);
|
|
|
|
|
|
|
|
/* set the ec parameters */
|
|
|
|
|
|
|
|
if (p8->pkeyalg->parameter)
|
2002-02-13 18:21:51 +00:00
|
|
|
{
|
2002-10-28 14:13:38 +00:00
|
|
|
ASN1_TYPE_free(p8->pkeyalg->parameter);
|
|
|
|
p8->pkeyalg->parameter = NULL;
|
2002-02-13 18:21:51 +00:00
|
|
|
}
|
2002-10-28 14:13:38 +00:00
|
|
|
|
|
|
|
if ((p8->pkeyalg->parameter = ASN1_TYPE_new()) == NULL)
|
2002-02-13 18:21:51 +00:00
|
|
|
{
|
2002-08-07 10:49:54 +00:00
|
|
|
EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
|
2002-02-13 18:21:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-10-28 14:13:38 +00:00
|
|
|
|
|
|
|
if (EC_GROUP_get_asn1_flag(eckey->group)
|
|
|
|
&& (nid = EC_GROUP_get_nid(eckey->group)))
|
2002-02-13 18:21:51 +00:00
|
|
|
{
|
2002-10-28 14:13:38 +00:00
|
|
|
/* we have a 'named curve' => just set the OID */
|
|
|
|
p8->pkeyalg->parameter->type = V_ASN1_OBJECT;
|
|
|
|
p8->pkeyalg->parameter->value.object = OBJ_nid2obj(nid);
|
2002-02-13 18:21:51 +00:00
|
|
|
}
|
2002-10-28 14:13:38 +00:00
|
|
|
else /* explicit parameters */
|
2002-02-13 18:21:51 +00:00
|
|
|
{
|
2002-10-28 14:13:38 +00:00
|
|
|
if ((i = i2d_ECParameters(eckey, NULL)) == 0)
|
2002-02-13 18:21:51 +00:00
|
|
|
{
|
2002-10-28 14:13:38 +00:00
|
|
|
EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_EC_LIB);
|
2002-02-13 18:21:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-10-28 14:13:38 +00:00
|
|
|
if ((p = (unsigned char *) OPENSSL_malloc(i)) == NULL)
|
2002-02-13 18:21:51 +00:00
|
|
|
{
|
2002-08-07 10:49:54 +00:00
|
|
|
EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
|
2002-02-13 18:21:51 +00:00
|
|
|
return 0;
|
2002-10-28 14:13:38 +00:00
|
|
|
}
|
|
|
|
pp = p;
|
|
|
|
if (!i2d_ECParameters(eckey, &pp))
|
2002-02-13 18:21:51 +00:00
|
|
|
{
|
2002-08-07 10:49:54 +00:00
|
|
|
EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_EC_LIB);
|
2002-02-13 18:21:51 +00:00
|
|
|
OPENSSL_free(p);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-10-28 14:13:38 +00:00
|
|
|
p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
|
|
|
|
if ((p8->pkeyalg->parameter->value.sequence
|
|
|
|
= ASN1_STRING_new()) == NULL)
|
2002-02-13 18:21:51 +00:00
|
|
|
{
|
2002-10-28 14:13:38 +00:00
|
|
|
EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_ASN1_LIB);
|
|
|
|
OPENSSL_free(p);
|
2002-02-13 18:21:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-10-28 14:13:38 +00:00
|
|
|
ASN1_STRING_set(p8->pkeyalg->parameter->value.sequence, p, i);
|
2002-02-13 18:21:51 +00:00
|
|
|
OPENSSL_free(p);
|
2002-10-28 14:13:38 +00:00
|
|
|
}
|
2002-02-13 18:21:51 +00:00
|
|
|
|
2002-10-28 14:13:38 +00:00
|
|
|
/* set the private key */
|
2003-06-25 21:35:05 +00:00
|
|
|
|
|
|
|
/* do not include the parameters in the SEC1 private key
|
|
|
|
* see PKCS#11 12.11 */
|
|
|
|
tmp_flags = pkey->pkey.eckey->enc_flag;
|
|
|
|
pkey->pkey.eckey->enc_flag |= EC_PKEY_NO_PARAMETERS;
|
|
|
|
i = i2d_ECPrivateKey(pkey->pkey.eckey, NULL);
|
|
|
|
if (!i)
|
|
|
|
{
|
|
|
|
pkey->pkey.eckey->enc_flag = tmp_flags;
|
|
|
|
EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_EC_LIB);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
p = (unsigned char *) OPENSSL_malloc(i);
|
|
|
|
if (!p)
|
2002-10-28 14:13:38 +00:00
|
|
|
{
|
2003-06-25 21:35:05 +00:00
|
|
|
pkey->pkey.eckey->enc_flag = tmp_flags;
|
|
|
|
EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
|
2002-10-28 14:13:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2003-06-25 21:35:05 +00:00
|
|
|
pp = p;
|
|
|
|
if (!i2d_ECPrivateKey(pkey->pkey.eckey, &pp))
|
|
|
|
{
|
|
|
|
pkey->pkey.eckey->enc_flag = tmp_flags;
|
|
|
|
EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_EC_LIB);
|
|
|
|
OPENSSL_free(p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* restore old encoding flags */
|
|
|
|
pkey->pkey.eckey->enc_flag = tmp_flags;
|
2002-02-13 18:21:51 +00:00
|
|
|
|
2002-10-28 14:13:38 +00:00
|
|
|
switch(p8->broken) {
|
2002-02-13 18:21:51 +00:00
|
|
|
|
2002-10-28 14:13:38 +00:00
|
|
|
case PKCS8_OK:
|
2003-06-25 21:35:05 +00:00
|
|
|
p8->pkey->value.octet_string = ASN1_OCTET_STRING_new();
|
|
|
|
if (!p8->pkey->value.octet_string ||
|
|
|
|
!M_ASN1_OCTET_STRING_set(p8->pkey->value.octet_string,
|
|
|
|
(const void *)p, i))
|
|
|
|
|
2002-02-13 18:21:51 +00:00
|
|
|
{
|
2002-08-07 10:49:54 +00:00
|
|
|
EVPerr(EVP_F_EC_KEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
|
2002-02-13 18:21:51 +00:00
|
|
|
}
|
2003-06-25 21:35:05 +00:00
|
|
|
else
|
|
|
|
ret = 1;
|
2002-10-28 14:13:38 +00:00
|
|
|
break;
|
|
|
|
case PKCS8_NO_OCTET: /* RSA specific */
|
|
|
|
case PKCS8_NS_DB: /* DSA specific */
|
|
|
|
case PKCS8_EMBEDDED_PARAM: /* DSA specific */
|
|
|
|
default:
|
|
|
|
EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
|
2002-02-13 18:21:51 +00:00
|
|
|
}
|
2003-06-25 21:35:05 +00:00
|
|
|
OPENSSL_cleanse(p, (size_t)i);
|
|
|
|
OPENSSL_free(p);
|
|
|
|
return ret;
|
2002-02-13 18:21:51 +00:00
|
|
|
}
|
|
|
|
#endif
|