sl@0: /* crypto/asn1/n_pkey.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: #include sl@0: #include "cryptlib.h" sl@0: #ifndef OPENSSL_NO_RSA sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: sl@0: #ifndef OPENSSL_NO_RC4 sl@0: sl@0: typedef struct netscape_pkey_st sl@0: { sl@0: long version; sl@0: X509_ALGOR *algor; sl@0: ASN1_OCTET_STRING *private_key; sl@0: } NETSCAPE_PKEY; sl@0: sl@0: typedef struct netscape_encrypted_pkey_st sl@0: { sl@0: ASN1_OCTET_STRING *os; sl@0: /* This is the same structure as DigestInfo so use it: sl@0: * although this isn't really anything to do with sl@0: * digests. sl@0: */ sl@0: X509_SIG *enckey; sl@0: } NETSCAPE_ENCRYPTED_PKEY; sl@0: sl@0: sl@0: ASN1_BROKEN_SEQUENCE(NETSCAPE_ENCRYPTED_PKEY) = { sl@0: ASN1_SIMPLE(NETSCAPE_ENCRYPTED_PKEY, os, ASN1_OCTET_STRING), sl@0: ASN1_SIMPLE(NETSCAPE_ENCRYPTED_PKEY, enckey, X509_SIG) sl@0: } ASN1_BROKEN_SEQUENCE_END(NETSCAPE_ENCRYPTED_PKEY) sl@0: sl@0: DECLARE_ASN1_FUNCTIONS_const(NETSCAPE_ENCRYPTED_PKEY) sl@0: DECLARE_ASN1_ENCODE_FUNCTIONS_const(NETSCAPE_ENCRYPTED_PKEY,NETSCAPE_ENCRYPTED_PKEY) sl@0: IMPLEMENT_ASN1_FUNCTIONS_const(NETSCAPE_ENCRYPTED_PKEY) sl@0: sl@0: ASN1_SEQUENCE(NETSCAPE_PKEY) = { sl@0: ASN1_SIMPLE(NETSCAPE_PKEY, version, LONG), sl@0: ASN1_SIMPLE(NETSCAPE_PKEY, algor, X509_ALGOR), sl@0: ASN1_SIMPLE(NETSCAPE_PKEY, private_key, ASN1_OCTET_STRING) sl@0: } ASN1_SEQUENCE_END(NETSCAPE_PKEY) sl@0: sl@0: DECLARE_ASN1_FUNCTIONS_const(NETSCAPE_PKEY) sl@0: DECLARE_ASN1_ENCODE_FUNCTIONS_const(NETSCAPE_PKEY,NETSCAPE_PKEY) sl@0: IMPLEMENT_ASN1_FUNCTIONS_const(NETSCAPE_PKEY) sl@0: sl@0: static RSA *d2i_RSA_NET_2(RSA **a, ASN1_OCTET_STRING *os, sl@0: int (*cb)(char *buf, int len, const char *prompt, sl@0: int verify), sl@0: int sgckey); sl@0: sl@0: EXPORT_C int i2d_Netscape_RSA(const RSA *a, unsigned char **pp, sl@0: int (*cb)(char *buf, int len, const char *prompt, sl@0: int verify)) sl@0: { sl@0: return i2d_RSA_NET(a, pp, cb, 0); sl@0: } sl@0: sl@0: EXPORT_C int i2d_RSA_NET(const RSA *a, unsigned char **pp, sl@0: int (*cb)(char *buf, int len, const char *prompt, int verify), sl@0: int sgckey) sl@0: { sl@0: int i, j, ret = 0; sl@0: int rsalen, pkeylen, olen; sl@0: NETSCAPE_PKEY *pkey = NULL; sl@0: NETSCAPE_ENCRYPTED_PKEY *enckey = NULL; sl@0: unsigned char buf[256],*zz; sl@0: unsigned char key[EVP_MAX_KEY_LENGTH]; sl@0: EVP_CIPHER_CTX ctx; sl@0: sl@0: if (a == NULL) return(0); sl@0: sl@0: if ((pkey=NETSCAPE_PKEY_new()) == NULL) goto err; sl@0: if ((enckey=NETSCAPE_ENCRYPTED_PKEY_new()) == NULL) goto err; sl@0: pkey->version = 0; sl@0: sl@0: pkey->algor->algorithm=OBJ_nid2obj(NID_rsaEncryption); sl@0: if ((pkey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err; sl@0: pkey->algor->parameter->type=V_ASN1_NULL; sl@0: sl@0: rsalen = i2d_RSAPrivateKey(a, NULL); sl@0: sl@0: /* Fake some octet strings just for the initial length sl@0: * calculation. sl@0: */ sl@0: sl@0: pkey->private_key->length=rsalen; sl@0: sl@0: pkeylen=i2d_NETSCAPE_PKEY(pkey,NULL); sl@0: sl@0: enckey->enckey->digest->length = pkeylen; sl@0: sl@0: enckey->os->length = 11; /* "private-key" */ sl@0: sl@0: enckey->enckey->algor->algorithm=OBJ_nid2obj(NID_rc4); sl@0: if ((enckey->enckey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err; sl@0: enckey->enckey->algor->parameter->type=V_ASN1_NULL; sl@0: sl@0: if (pp == NULL) sl@0: { sl@0: olen = i2d_NETSCAPE_ENCRYPTED_PKEY(enckey, NULL); sl@0: NETSCAPE_PKEY_free(pkey); sl@0: NETSCAPE_ENCRYPTED_PKEY_free(enckey); sl@0: return olen; sl@0: } sl@0: sl@0: sl@0: /* Since its RC4 encrypted length is actual length */ sl@0: if ((zz=(unsigned char *)OPENSSL_malloc(rsalen)) == NULL) sl@0: { sl@0: ASN1err(ASN1_F_I2D_RSA_NET,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: pkey->private_key->data = zz; sl@0: /* Write out private key encoding */ sl@0: i2d_RSAPrivateKey(a,&zz); sl@0: sl@0: if ((zz=OPENSSL_malloc(pkeylen)) == NULL) sl@0: { sl@0: ASN1err(ASN1_F_I2D_RSA_NET,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: if (!ASN1_STRING_set(enckey->os, "private-key", -1)) sl@0: { sl@0: ASN1err(ASN1_F_I2D_RSA_NET,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: enckey->enckey->digest->data = zz; sl@0: i2d_NETSCAPE_PKEY(pkey,&zz); sl@0: sl@0: /* Wipe the private key encoding */ sl@0: OPENSSL_cleanse(pkey->private_key->data, rsalen); sl@0: sl@0: if (cb == NULL) sl@0: cb=EVP_read_pw_string; sl@0: i=cb((char *)buf,256,"Enter Private Key password:",1); sl@0: if (i != 0) sl@0: { sl@0: ASN1err(ASN1_F_I2D_RSA_NET,ASN1_R_BAD_PASSWORD_READ); sl@0: goto err; sl@0: } sl@0: i = strlen((char *)buf); sl@0: /* If the key is used for SGC the algorithm is modified a little. */ sl@0: if(sgckey) { sl@0: EVP_Digest(buf, i, buf, NULL, EVP_md5(), NULL); sl@0: memcpy(buf + 16, "SGCKEYSALT", 10); sl@0: i = 26; sl@0: } sl@0: sl@0: EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL); sl@0: OPENSSL_cleanse(buf,256); sl@0: sl@0: /* Encrypt private key in place */ sl@0: zz = enckey->enckey->digest->data; sl@0: EVP_CIPHER_CTX_init(&ctx); sl@0: EVP_EncryptInit_ex(&ctx,EVP_rc4(),NULL,key,NULL); sl@0: EVP_EncryptUpdate(&ctx,zz,&i,zz,pkeylen); sl@0: EVP_EncryptFinal_ex(&ctx,zz + i,&j); sl@0: EVP_CIPHER_CTX_cleanup(&ctx); sl@0: sl@0: ret = i2d_NETSCAPE_ENCRYPTED_PKEY(enckey, pp); sl@0: err: sl@0: NETSCAPE_ENCRYPTED_PKEY_free(enckey); sl@0: NETSCAPE_PKEY_free(pkey); sl@0: return(ret); sl@0: } sl@0: sl@0: sl@0: EXPORT_C RSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length, sl@0: int (*cb)(char *buf, int len, const char *prompt, sl@0: int verify)) sl@0: { sl@0: return d2i_RSA_NET(a, pp, length, cb, 0); sl@0: } sl@0: sl@0: EXPORT_C RSA *d2i_RSA_NET(RSA **a, const unsigned char **pp, long length, sl@0: int (*cb)(char *buf, int len, const char *prompt, int verify), sl@0: int sgckey) sl@0: { sl@0: RSA *ret=NULL; sl@0: const unsigned char *p, *kp; sl@0: NETSCAPE_ENCRYPTED_PKEY *enckey = NULL; sl@0: sl@0: p = *pp; sl@0: sl@0: enckey = d2i_NETSCAPE_ENCRYPTED_PKEY(NULL, &p, length); sl@0: if(!enckey) { sl@0: ASN1err(ASN1_F_D2I_RSA_NET,ASN1_R_DECODING_ERROR); sl@0: return NULL; sl@0: } sl@0: sl@0: if ((enckey->os->length != 11) || (strncmp("private-key", sl@0: (char *)enckey->os->data,11) != 0)) sl@0: { sl@0: ASN1err(ASN1_F_D2I_RSA_NET,ASN1_R_PRIVATE_KEY_HEADER_MISSING); sl@0: NETSCAPE_ENCRYPTED_PKEY_free(enckey); sl@0: return NULL; sl@0: } sl@0: if (OBJ_obj2nid(enckey->enckey->algor->algorithm) != NID_rc4) sl@0: { sl@0: ASN1err(ASN1_F_D2I_RSA_NET,ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM); sl@0: goto err; sl@0: } sl@0: kp = enckey->enckey->digest->data; sl@0: if (cb == NULL) sl@0: cb=EVP_read_pw_string; sl@0: if ((ret=d2i_RSA_NET_2(a, enckey->enckey->digest,cb, sgckey)) == NULL) goto err; sl@0: sl@0: *pp = p; sl@0: sl@0: err: sl@0: NETSCAPE_ENCRYPTED_PKEY_free(enckey); sl@0: return ret; sl@0: sl@0: } sl@0: sl@0: static RSA *d2i_RSA_NET_2(RSA **a, ASN1_OCTET_STRING *os, sl@0: int (*cb)(char *buf, int len, const char *prompt, sl@0: int verify), int sgckey) sl@0: { sl@0: NETSCAPE_PKEY *pkey=NULL; sl@0: RSA *ret=NULL; sl@0: int i,j; sl@0: unsigned char buf[256]; sl@0: const unsigned char *zz; sl@0: unsigned char key[EVP_MAX_KEY_LENGTH]; sl@0: EVP_CIPHER_CTX ctx; sl@0: sl@0: i=cb((char *)buf,256,"Enter Private Key password:",0); sl@0: if (i != 0) sl@0: { sl@0: ASN1err(ASN1_F_D2I_RSA_NET_2,ASN1_R_BAD_PASSWORD_READ); sl@0: goto err; sl@0: } sl@0: sl@0: i = strlen((char *)buf); sl@0: if(sgckey){ sl@0: EVP_Digest(buf, i, buf, NULL, EVP_md5(), NULL); sl@0: memcpy(buf + 16, "SGCKEYSALT", 10); sl@0: i = 26; sl@0: } sl@0: sl@0: EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL); sl@0: OPENSSL_cleanse(buf,256); sl@0: sl@0: EVP_CIPHER_CTX_init(&ctx); sl@0: EVP_DecryptInit_ex(&ctx,EVP_rc4(),NULL, key,NULL); sl@0: EVP_DecryptUpdate(&ctx,os->data,&i,os->data,os->length); sl@0: EVP_DecryptFinal_ex(&ctx,&(os->data[i]),&j); sl@0: EVP_CIPHER_CTX_cleanup(&ctx); sl@0: os->length=i+j; sl@0: sl@0: zz=os->data; sl@0: sl@0: if ((pkey=d2i_NETSCAPE_PKEY(NULL,&zz,os->length)) == NULL) sl@0: { sl@0: ASN1err(ASN1_F_D2I_RSA_NET_2,ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY); sl@0: goto err; sl@0: } sl@0: sl@0: zz=pkey->private_key->data; sl@0: if ((ret=d2i_RSAPrivateKey(a,&zz,pkey->private_key->length)) == NULL) sl@0: { sl@0: ASN1err(ASN1_F_D2I_RSA_NET_2,ASN1_R_UNABLE_TO_DECODE_RSA_KEY); sl@0: goto err; sl@0: } sl@0: sl@0: err: sl@0: NETSCAPE_PKEY_free(pkey); sl@0: return(ret); sl@0: } sl@0: sl@0: #endif /* OPENSSL_NO_RC4 */ sl@0: sl@0: #else /* !OPENSSL_NO_RSA */ sl@0: sl@0: # if PEDANTIC sl@0: static void *dummy=&dummy; sl@0: # endif sl@0: sl@0: #endif