aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/evp
diff options
context:
space:
mode:
authorDr. Stephen Henson <steve@openssl.org>1999-03-28 17:46:10 +0000
committerDr. Stephen Henson <steve@openssl.org>1999-03-28 17:46:10 +0000
commitcfcefcbe2aff81c1735c788ca6ae26b7c16cc453 (patch)
tree0b6eab23b59f090ff3cc87a4b692e8bc459b79e6 /crypto/evp
parent4b518c260151f851d205c065e86c4deb96036f1d (diff)
downloadopenssl-cfcefcbe2aff81c1735c788ca6ae26b7c16cc453.tar.gz
Further PKCS#12 integration, PBE, PKCS#8 additions.
Diffstat (limited to 'crypto/evp')
-rw-r--r--crypto/evp/Makefile.ssl4
-rw-r--r--crypto/evp/evp.err20
-rw-r--r--crypto/evp/evp.h27
-rw-r--r--crypto/evp/evp_err.c20
-rw-r--r--crypto/evp/evp_pbe.c176
-rw-r--r--crypto/evp/evp_pkey.c291
6 files changed, 530 insertions, 8 deletions
diff --git a/crypto/evp/Makefile.ssl b/crypto/evp/Makefile.ssl
index 89b7df56d3..4ed427b7a9 100644
--- a/crypto/evp/Makefile.ssl
+++ b/crypto/evp/Makefile.ssl
@@ -35,7 +35,7 @@ LIBSRC= encode.c digest.c evp_enc.c evp_key.c \
m_ripemd.c \
p_open.c p_seal.c p_sign.c p_verify.c p_lib.c p_enc.c p_dec.c \
bio_md.c bio_b64.c bio_enc.c $(ERRC).c e_null.c \
- c_all.c evp_lib.c bio_ok.c
+ c_all.c evp_lib.c bio_ok.c evp_pkey.c evp_pbe.c
LIBOBJ= encode.o digest.o evp_enc.o evp_key.o \
e_ecb_d.o e_cbc_d.o e_cfb_d.o e_ofb_d.o \
@@ -50,7 +50,7 @@ LIBOBJ= encode.o digest.o evp_enc.o evp_key.o \
m_ripemd.o \
p_open.o p_seal.o p_sign.o p_verify.o p_lib.o p_enc.o p_dec.o \
bio_md.o bio_b64.o bio_enc.o $(ERRC).o e_null.o \
- c_all.o evp_lib.o bio_ok.o
+ c_all.o evp_lib.o bio_ok.o evp_pkey.o evp_pbe.o
SRC= $(LIBSRC)
diff --git a/crypto/evp/evp.err b/crypto/evp/evp.err
index c2470f678c..367584e8a7 100644
--- a/crypto/evp/evp.err
+++ b/crypto/evp/evp.err
@@ -3,7 +3,14 @@
/* Function codes. */
#define EVP_F_D2I_PKEY 100
#define EVP_F_EVP_DECRYPTFINAL 101
+#define EVP_F_EVP_MD_CTX_COPY 110
#define EVP_F_EVP_OPENINIT 102
+#define EVP_F_EVP_PBE_ALGOR_CIPHERINIT 114
+#define EVP_F_EVP_PBE_ALG_ADD 115
+#define EVP_F_EVP_PBE_CIPHERINIT 116
+#define EVP_F_EVP_PKCS82PKEY 111
+#define EVP_F_EVP_PKCS8_SET_BROKEN 112
+#define EVP_F_EVP_PKEY2PKCS8 113
#define EVP_F_EVP_PKEY_COPY_PARAMETERS 103
#define EVP_F_EVP_PKEY_DECRYPT 104
#define EVP_F_EVP_PKEY_ENCRYPT 105
@@ -11,18 +18,27 @@
#define EVP_F_EVP_SIGNFINAL 107
#define EVP_F_EVP_VERIFYFINAL 108
#define EVP_F_RC2_MAGIC_TO_METH 109
-#define EVP_F_EVP_MD_CTX_COPY 110
/* Reason codes. */
#define EVP_R_BAD_DECRYPT 100
+#define EVP_R_BN_DECODE_ERROR 112
+#define EVP_R_BN_PUBKEY_ERROR 113
+#define EVP_R_DECODE_ERROR 114
#define EVP_R_DIFFERENT_KEY_TYPES 101
+#define EVP_R_ENCODE_ERROR 115
+#define EVP_R_EVP_PBE_CIPHERINIT_ERROR 119
+#define EVP_R_INPUT_NOT_INITALISED 111
#define EVP_R_IV_TOO_LARGE 102
+#define EVP_R_KEYGEN_FAILURE 120
#define EVP_R_MISSING_PARMATERS 103
+#define EVP_R_NO_DSA_PARAMETERS 116
#define EVP_R_NO_SIGN_FUNCTION_CONFIGURED 104
#define EVP_R_NO_VERIFY_FUNCTION_CONFIGURED 105
+#define EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE 117
#define EVP_R_PUBLIC_KEY_NOT_RSA 106
+#define EVP_R_UNKNOWN_PBE_ALGORITHM 121
#define EVP_R_UNSUPPORTED_CIPHER 107
#define EVP_R_UNSUPPORTED_KEY_SIZE 108
+#define EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM 118
#define EVP_R_WRONG_FINAL_BLOCK_LENGTH 109
#define EVP_R_WRONG_PUBLIC_KEY_TYPE 110
-#define EVP_R_INPUT_NOT_INITALISED 111
diff --git a/crypto/evp/evp.h b/crypto/evp/evp.h
index 3aad7d1d5f..31ed4791a9 100644
--- a/crypto/evp/evp.h
+++ b/crypto/evp/evp.h
@@ -261,6 +261,8 @@ typedef struct env_md_st
int ctx_size; /* how big does the ctx need to be */
} EVP_MD;
+
+
#define EVP_PKEY_NULL_method NULL,NULL,{0,0,0,0}
#ifndef NO_DSA
@@ -396,6 +398,11 @@ typedef struct evp_Encode_Ctx_st
int expect_nl;
} EVP_ENCODE_CTX;
+/* Password based encryption function */
+typedef int (EVP_PBE_KEYGEN)(unsigned char *pass, int passlen,
+ unsigned char *salt, int saltlen, int iter, EVP_CIPHER *cipher,
+ EVP_MD *md, unsigned char *key, unsigned char *iv);
+
#define EVP_PKEY_assign_RSA(pkey,rsa) EVP_PKEY_assign((pkey),EVP_PKEY_RSA,\
(char *)(rsa))
#define EVP_PKEY_assign_DSA(pkey,dsa) EVP_PKEY_assign((pkey),EVP_PKEY_DSA,\
@@ -779,7 +786,14 @@ int EVP_CIPHER_get_asn1_iv();
/* Function codes. */
#define EVP_F_D2I_PKEY 100
#define EVP_F_EVP_DECRYPTFINAL 101
+#define EVP_F_EVP_MD_CTX_COPY 110
#define EVP_F_EVP_OPENINIT 102
+#define EVP_F_EVP_PBE_ALGOR_CIPHERINIT 114
+#define EVP_F_EVP_PBE_ALG_ADD 115
+#define EVP_F_EVP_PBE_CIPHERINIT 116
+#define EVP_F_EVP_PKCS82PKEY 111
+#define EVP_F_EVP_PKCS8_SET_BROKEN 112
+#define EVP_F_EVP_PKEY2PKCS8 113
#define EVP_F_EVP_PKEY_COPY_PARAMETERS 103
#define EVP_F_EVP_PKEY_DECRYPT 104
#define EVP_F_EVP_PKEY_ENCRYPT 105
@@ -787,21 +801,30 @@ int EVP_CIPHER_get_asn1_iv();
#define EVP_F_EVP_SIGNFINAL 107
#define EVP_F_EVP_VERIFYFINAL 108
#define EVP_F_RC2_MAGIC_TO_METH 109
-#define EVP_F_EVP_MD_CTX_COPY 110
/* Reason codes. */
#define EVP_R_BAD_DECRYPT 100
+#define EVP_R_BN_DECODE_ERROR 112
+#define EVP_R_BN_PUBKEY_ERROR 113
+#define EVP_R_DECODE_ERROR 114
#define EVP_R_DIFFERENT_KEY_TYPES 101
+#define EVP_R_ENCODE_ERROR 115
+#define EVP_R_EVP_PBE_CIPHERINIT_ERROR 119
+#define EVP_R_INPUT_NOT_INITALISED 111
#define EVP_R_IV_TOO_LARGE 102
+#define EVP_R_KEYGEN_FAILURE 120
#define EVP_R_MISSING_PARMATERS 103
+#define EVP_R_NO_DSA_PARAMETERS 116
#define EVP_R_NO_SIGN_FUNCTION_CONFIGURED 104
#define EVP_R_NO_VERIFY_FUNCTION_CONFIGURED 105
+#define EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE 117
#define EVP_R_PUBLIC_KEY_NOT_RSA 106
+#define EVP_R_UNKNOWN_PBE_ALGORITHM 121
#define EVP_R_UNSUPPORTED_CIPHER 107
#define EVP_R_UNSUPPORTED_KEY_SIZE 108
+#define EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM 118
#define EVP_R_WRONG_FINAL_BLOCK_LENGTH 109
#define EVP_R_WRONG_PUBLIC_KEY_TYPE 110
-#define EVP_R_INPUT_NOT_INITALISED 111
#ifdef __cplusplus
}
diff --git a/crypto/evp/evp_err.c b/crypto/evp/evp_err.c
index 19b3a1896e..7f8fe8d5fa 100644
--- a/crypto/evp/evp_err.c
+++ b/crypto/evp/evp_err.c
@@ -65,7 +65,14 @@ static ERR_STRING_DATA EVP_str_functs[]=
{
{ERR_PACK(0,EVP_F_D2I_PKEY,0), "D2I_PKEY"},
{ERR_PACK(0,EVP_F_EVP_DECRYPTFINAL,0), "EVP_DecryptFinal"},
+{ERR_PACK(0,EVP_F_EVP_MD_CTX_COPY,0), "EVP_MD_CTX_copy"},
{ERR_PACK(0,EVP_F_EVP_OPENINIT,0), "EVP_OpenInit"},
+{ERR_PACK(0,EVP_F_EVP_PBE_ALGOR_CIPHERINIT,0), "EVP_PBE_ALGOR_CIPHERINIT"},
+{ERR_PACK(0,EVP_F_EVP_PBE_ALG_ADD,0), "EVP_PBE_ALG_ADD"},
+{ERR_PACK(0,EVP_F_EVP_PBE_CIPHERINIT,0), "EVP_PBE_CIPHERINIT"},
+{ERR_PACK(0,EVP_F_EVP_PKCS82PKEY,0), "EVP_PKCS82PKEY"},
+{ERR_PACK(0,EVP_F_EVP_PKCS8_SET_BROKEN,0), "EVP_PKCS8_SET_BROKEN"},
+{ERR_PACK(0,EVP_F_EVP_PKEY2PKCS8,0), "EVP_PKEY2PKCS8"},
{ERR_PACK(0,EVP_F_EVP_PKEY_COPY_PARAMETERS,0), "EVP_PKEY_copy_parameters"},
{ERR_PACK(0,EVP_F_EVP_PKEY_DECRYPT,0), "EVP_PKEY_decrypt"},
{ERR_PACK(0,EVP_F_EVP_PKEY_ENCRYPT,0), "EVP_PKEY_encrypt"},
@@ -73,24 +80,33 @@ static ERR_STRING_DATA EVP_str_functs[]=
{ERR_PACK(0,EVP_F_EVP_SIGNFINAL,0), "EVP_SignFinal"},
{ERR_PACK(0,EVP_F_EVP_VERIFYFINAL,0), "EVP_VerifyFinal"},
{ERR_PACK(0,EVP_F_RC2_MAGIC_TO_METH,0), "RC2_MAGIC_TO_METH"},
-{ERR_PACK(0,EVP_F_EVP_MD_CTX_COPY,0), "EVP_MD_CTX_copy"},
{0,NULL},
};
static ERR_STRING_DATA EVP_str_reasons[]=
{
{EVP_R_BAD_DECRYPT ,"bad decrypt"},
+{EVP_R_BN_DECODE_ERROR ,"bn decode error"},
+{EVP_R_BN_PUBKEY_ERROR ,"bn pubkey error"},
+{EVP_R_DECODE_ERROR ,"decode error"},
{EVP_R_DIFFERENT_KEY_TYPES ,"different key types"},
+{EVP_R_ENCODE_ERROR ,"encode error"},
+{EVP_R_EVP_PBE_CIPHERINIT_ERROR ,"evp pbe cipherinit error"},
+{EVP_R_INPUT_NOT_INITALISED ,"input not initalised"},
{EVP_R_IV_TOO_LARGE ,"iv too large"},
+{EVP_R_KEYGEN_FAILURE ,"keygen failure"},
{EVP_R_MISSING_PARMATERS ,"missing parmaters"},
+{EVP_R_NO_DSA_PARAMETERS ,"no dsa parameters"},
{EVP_R_NO_SIGN_FUNCTION_CONFIGURED ,"no sign function configured"},
{EVP_R_NO_VERIFY_FUNCTION_CONFIGURED ,"no verify function configured"},
+{EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE ,"pkcs8 unknown broken type"},
{EVP_R_PUBLIC_KEY_NOT_RSA ,"public key not rsa"},
+{EVP_R_UNKNOWN_PBE_ALGORITHM ,"unknown pbe algorithm"},
{EVP_R_UNSUPPORTED_CIPHER ,"unsupported cipher"},
{EVP_R_UNSUPPORTED_KEY_SIZE ,"unsupported key size"},
+{EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM ,"unsupported private key algorithm"},
{EVP_R_WRONG_FINAL_BLOCK_LENGTH ,"wrong final block length"},
{EVP_R_WRONG_PUBLIC_KEY_TYPE ,"wrong public key type"},
-{EVP_R_INPUT_NOT_INITALISED ,"input not initalised"},
{0,NULL},
};
diff --git a/crypto/evp/evp_pbe.c b/crypto/evp/evp_pbe.c
new file mode 100644
index 0000000000..e1ebb66357
--- /dev/null
+++ b/crypto/evp/evp_pbe.c
@@ -0,0 +1,176 @@
+/* p12_pbop.c */
+/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
+ * project 1999.
+ */
+/* ====================================================================
+ * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
+ *
+ * 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 "evp.h"
+#include "x509.h"
+#include "cryptlib.h"
+
+/* Password based encryption (PBE) functions */
+
+static STACK *pbe_algs;
+
+/* Setup a cipher context from a PBE algorithm */
+
+typedef struct {
+int pbe_nid;
+EVP_CIPHER *cipher;
+EVP_MD *md;
+EVP_PBE_KEYGEN *keygen;
+} EVP_PBE_CTL;
+
+int EVP_PBE_CipherInit (pbe_obj, pass, passlen, salt, saltlen, iter, ctx, en_de)
+ASN1_OBJECT *pbe_obj;
+int passlen, saltlen, iter;
+unsigned char *pass, *salt;
+EVP_CIPHER_CTX *ctx;
+int en_de;
+{
+
+ EVP_PBE_CTL *pbetmp, pbelu;
+ unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
+ int i;
+ pbelu.pbe_nid = OBJ_obj2nid(pbe_obj);
+ if ((pbelu.pbe_nid != NID_undef) && pbe_algs)
+ i = sk_find (pbe_algs, (char *)&pbelu);
+ else i = -1;
+
+ if (i == -1) {
+ char obj_tmp[80];
+ EVPerr(EVP_F_EVP_PBE_CIPHERINIT,EVP_R_UNKNOWN_PBE_ALGORITHM);
+ if (!pbe_obj) strcpy (obj_tmp, "NULL");
+ else i2t_ASN1_OBJECT(obj_tmp, 80, pbe_obj);
+ ERR_add_error_data(2, "TYPE=", obj_tmp);
+ return 0;
+ }
+ if (passlen == -1) passlen = strlen(pass);
+ pbetmp = (EVP_PBE_CTL *)sk_value (pbe_algs, i);
+ i = (*pbetmp->keygen)(pass, passlen, salt, saltlen, iter,
+ pbetmp->cipher, pbetmp->md, key, iv);
+ if (!i) {
+ EVPerr(EVP_F_EVP_PBE_CIPHERINIT,EVP_R_KEYGEN_FAILURE);
+ return 0;
+ }
+ EVP_CipherInit (ctx, pbetmp->cipher, key, iv, en_de);
+ return 1;
+}
+
+/* Setup a PBE algorithm but take most parameters from AlgorithmIdentifier */
+
+int EVP_PBE_ALGOR_CipherInit (algor, pass, passlen, ctx, en_de)
+X509_ALGOR *algor;
+unsigned char *pass;
+int passlen;
+EVP_CIPHER_CTX *ctx;
+int en_de;
+{
+ PBEPARAM *pbe;
+ int saltlen, iter;
+ unsigned char *salt, *pbuf;
+
+ /* Extract useful info from algor */
+ pbuf = (char *) algor->parameter->value.sequence->data;
+ if (!(pbe = d2i_PBEPARAM (NULL, &pbuf,
+ algor->parameter->value.sequence->length))) {
+ EVPerr(EVP_F_EVP_PBE_ALGOR_CIPHERINIT,EVP_R_DECODE_ERROR);
+ return 0;
+ }
+
+ if (!pbe->iter) iter = 1;
+ else iter = ASN1_INTEGER_get (pbe->iter);
+ salt = pbe->salt->data;
+ saltlen = pbe->salt->length;
+
+ if (!(EVP_PBE_CipherInit (algor->algorithm, pass, passlen, salt,
+ saltlen, iter, ctx, en_de))) {
+ EVPerr(EVP_F_EVP_PBE_ALGOR_CIPHERINIT,EVP_R_EVP_PBE_CIPHERINIT_ERROR);
+ PBEPARAM_free(pbe);
+ return 0;
+ }
+ PBEPARAM_free(pbe);
+ return 1;
+}
+
+
+static int pbe_cmp (pbe1, pbe2)
+EVP_PBE_CTL **pbe1, **pbe2;
+{
+ return ((*pbe1)->pbe_nid - (*pbe2)->pbe_nid);
+}
+
+/* Add a PBE algorithm */
+
+int EVP_PBE_alg_add (nid, cipher, md, keygen)
+int nid;
+EVP_CIPHER *cipher;
+EVP_MD *md;
+EVP_PBE_KEYGEN *keygen;
+{
+ EVP_PBE_CTL *pbe_tmp;
+ if (!pbe_algs) pbe_algs = sk_new (pbe_cmp);
+ if (!(pbe_tmp = (EVP_PBE_CTL*) Malloc (sizeof(EVP_PBE_CTL)))) {
+ EVPerr(EVP_F_EVP_PBE_ALG_ADD,ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+ pbe_tmp->pbe_nid = nid;
+ pbe_tmp->cipher = cipher;
+ pbe_tmp->md = md;
+ pbe_tmp->keygen = keygen;
+ sk_push (pbe_algs, (char *)pbe_tmp);
+ return 1;
+}
diff --git a/crypto/evp/evp_pkey.c b/crypto/evp/evp_pkey.c
new file mode 100644
index 0000000000..0046d8e84b
--- /dev/null
+++ b/crypto/evp/evp_pkey.c
@@ -0,0 +1,291 @@
+/* evp_pkey.c */
+/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
+ * project 1999.
+ */
+/* ====================================================================
+ * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
+ *
+ * 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"
+#include "x509.h"
+#include "rand.h"
+
+/* Extract a private key from a PKCS8 structure */
+
+EVP_PKEY *EVP_PKCS82PKEY (p8)
+PKCS8_PRIV_KEY_INFO *p8;
+{
+ EVP_PKEY *pkey;
+ RSA *rsa;
+ DSA *dsa;
+ ASN1_INTEGER *dsapriv;
+ X509_ALGOR *a;
+ STACK *ndsa;
+ BN_CTX *ctx;
+ unsigned char *p;
+ int plen, pkeylen;
+ char obj_tmp[80];
+
+ switch (p8->broken) {
+ case PKCS8_OK:
+ p = p8->pkey->value.octet_string->data;
+ pkeylen = p8->pkey->value.octet_string->length;
+ break;
+
+ case PKCS8_NO_OCTET:
+ p = p8->pkey->value.sequence->data;
+ pkeylen = p8->pkey->value.sequence->length;
+ break;
+
+ default:
+ EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE);
+ return NULL;
+ break;
+ }
+ if (!(pkey = EVP_PKEY_new())) {
+ EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
+ return NULL;
+ }
+ a = p8->pkeyalg;
+ switch (OBJ_obj2nid(a->algorithm))
+ {
+ case NID_rsaEncryption:
+ if (!(rsa = d2i_RSAPrivateKey (NULL, &p, pkeylen))) {
+ EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
+ return NULL;
+ }
+ EVP_PKEY_assign_RSA (pkey, rsa);
+ break;
+
+ case NID_dsa:
+ /* PKCS#8 DSA is weird: you just get a private key integer
+ * and parameters in the AlgorithmIdentifier the pubkey must
+ * be recalculated.
+ */
+
+ /* Check for broken Netscape Database DSA PKCS#8, UGH! */
+ if(*p == (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED)) {
+ if(!(ndsa = ASN1_seq_unpack(p, pkeylen,
+ (char *(*)())d2i_ASN1_INTEGER,
+ ASN1_STRING_free))) {
+ EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
+ return NULL;
+ }
+ if(sk_num(ndsa) != 2 ) {
+ EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
+ sk_pop_free(ndsa, ASN1_STRING_free);
+ return NULL;
+ }
+ dsapriv = (ASN1_INTEGER *) sk_pop(ndsa);
+ sk_pop_free(ndsa, ASN1_STRING_free);
+ } else if (!(dsapriv=d2i_ASN1_INTEGER (NULL, &p, pkeylen))) {
+ EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
+ return NULL;
+ }
+ /* Retrieve parameters */
+ if (a->parameter->type != V_ASN1_SEQUENCE) {
+ EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_NO_DSA_PARAMETERS);
+ return NULL;
+ }
+ p = a->parameter->value.sequence->data;
+ plen = a->parameter->value.sequence->length;
+ if (!(dsa = d2i_DSAparams (NULL, &p, plen))) {
+ EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
+ return NULL;
+ }
+ /* We have parameters now set private key */
+ if (!(dsa->priv_key = ASN1_INTEGER_to_BN(dsapriv, NULL))) {
+ EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_DECODE_ERROR);
+ DSA_free (dsa);
+ return NULL;
+ }
+ /* Calculate public key (ouch!) */
+ if (!(dsa->pub_key = BN_new())) {
+ EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
+ DSA_free (dsa);
+ return NULL;
+ }
+ if (!(ctx = BN_CTX_new())) {
+ EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
+ DSA_free (dsa);
+ return NULL;
+ }
+
+ 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);
+ BN_CTX_free (ctx);
+ DSA_free (dsa);
+ return NULL;
+ }
+
+ EVP_PKEY_assign_DSA (pkey, dsa);
+ BN_CTX_free (ctx);
+ break;
+
+ default:
+ EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
+ if (!a->algorithm) strcpy (obj_tmp, "NULL");
+ 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;
+}
+
+/* Turn a private key into a PKCS8 structure */
+
+PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(pkey)
+EVP_PKEY *pkey;
+{
+ PKCS8_PRIV_KEY_INFO *p8;
+ ASN1_INTEGER *dpkey;
+ unsigned char *p, *q;
+ int len;
+ if (!(p8 = PKCS8_PRIV_KEY_INFO_new())) {
+ EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
+ return NULL;
+ }
+ ASN1_INTEGER_set (p8->version, 0);
+ if (!(p8->pkeyalg->parameter = ASN1_TYPE_new ())) {
+ EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
+ PKCS8_PRIV_KEY_INFO_free (p8);
+ return NULL;
+ }
+ switch (EVP_PKEY_type(pkey->type)) {
+ case EVP_PKEY_RSA:
+
+ 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;
+
+ case EVP_PKEY_DSA:
+ p8->pkeyalg->algorithm = OBJ_nid2obj(NID_dsa);
+
+ /* get paramaters and place in AlgorithmIdentifier */
+ len = i2d_DSAparams (pkey->pkey.dsa, NULL);
+ if (!(p = Malloc(len))) {
+ EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
+ PKCS8_PRIV_KEY_INFO_free (p8);
+ return NULL;
+ }
+ q = p;
+ i2d_DSAparams (pkey->pkey.dsa, &q);
+ p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
+ p8->pkeyalg->parameter->value.sequence = ASN1_STRING_new();
+ ASN1_STRING_set(p8->pkeyalg->parameter->value.sequence, p, len);
+ Free(p);
+ /* Get private key into an integer and pack */
+ if (!(dpkey = BN_to_ASN1_INTEGER (pkey->pkey.dsa->priv_key, NULL))) {
+ EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
+ PKCS8_PRIV_KEY_INFO_free (p8);
+ return NULL;
+ }
+
+ if (!ASN1_pack_string((char *)dpkey, i2d_ASN1_INTEGER,
+ &p8->pkey->value.octet_string)) {
+ EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
+ ASN1_INTEGER_free (dpkey);
+ PKCS8_PRIV_KEY_INFO_free (p8);
+ return NULL;
+ }
+ ASN1_INTEGER_free (dpkey);
+ break;
+
+ default:
+ EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
+ PKCS8_PRIV_KEY_INFO_free (p8);
+ return NULL;
+ }
+ p8->pkey->type = V_ASN1_OCTET_STRING;
+ RAND_seed (p8->pkey->value.octet_string->data,
+ p8->pkey->value.octet_string->length);
+ return p8;
+}
+
+PKCS8_PRIV_KEY_INFO *PKCS8_set_broken(p8, broken)
+PKCS8_PRIV_KEY_INFO *p8;
+int broken;
+{
+ 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;
+
+ }
+}
+
+