1109998Smarkm/* crypto/pem/pem_pkey.c */ 2109998Smarkm/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3109998Smarkm * All rights reserved. 4109998Smarkm * 5109998Smarkm * This package is an SSL implementation written 6109998Smarkm * by Eric Young (eay@cryptsoft.com). 7109998Smarkm * The implementation was written so as to conform with Netscapes SSL. 8280297Sjkim * 9109998Smarkm * This library is free for commercial and non-commercial use as long as 10109998Smarkm * the following conditions are aheared to. The following conditions 11109998Smarkm * apply to all code found in this distribution, be it the RC4, RSA, 12109998Smarkm * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13109998Smarkm * included with this distribution is covered by the same copyright terms 14109998Smarkm * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15280297Sjkim * 16109998Smarkm * Copyright remains Eric Young's, and as such any Copyright notices in 17109998Smarkm * the code are not to be removed. 18109998Smarkm * If this package is used in a product, Eric Young should be given attribution 19109998Smarkm * as the author of the parts of the library used. 20109998Smarkm * This can be in the form of a textual message at program startup or 21109998Smarkm * in documentation (online or textual) provided with the package. 22280297Sjkim * 23109998Smarkm * Redistribution and use in source and binary forms, with or without 24109998Smarkm * modification, are permitted provided that the following conditions 25109998Smarkm * are met: 26109998Smarkm * 1. Redistributions of source code must retain the copyright 27109998Smarkm * notice, this list of conditions and the following disclaimer. 28109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright 29109998Smarkm * notice, this list of conditions and the following disclaimer in the 30109998Smarkm * documentation and/or other materials provided with the distribution. 31109998Smarkm * 3. All advertising materials mentioning features or use of this software 32109998Smarkm * must display the following acknowledgement: 33109998Smarkm * "This product includes cryptographic software written by 34109998Smarkm * Eric Young (eay@cryptsoft.com)" 35109998Smarkm * The word 'cryptographic' can be left out if the rouines from the library 36109998Smarkm * being used are not cryptographic related :-). 37280297Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from 38109998Smarkm * the apps directory (application code) you must include an acknowledgement: 39109998Smarkm * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40280297Sjkim * 41109998Smarkm * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42109998Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44109998Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45109998Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46109998Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47109998Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49109998Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50109998Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51109998Smarkm * SUCH DAMAGE. 52280297Sjkim * 53109998Smarkm * The licence and distribution terms for any publically available version or 54109998Smarkm * derivative of this code cannot be changed. i.e. this code cannot simply be 55109998Smarkm * copied and put under another distribution licence 56109998Smarkm * [including the GNU Public Licence.] 57109998Smarkm */ 58109998Smarkm 59109998Smarkm#include <stdio.h> 60109998Smarkm#include "cryptlib.h" 61109998Smarkm#include <openssl/buffer.h> 62109998Smarkm#include <openssl/objects.h> 63109998Smarkm#include <openssl/evp.h> 64109998Smarkm#include <openssl/rand.h> 65109998Smarkm#include <openssl/x509.h> 66109998Smarkm#include <openssl/pkcs12.h> 67109998Smarkm#include <openssl/pem.h> 68238405Sjkim#ifndef OPENSSL_NO_ENGINE 69280297Sjkim# include <openssl/engine.h> 70238405Sjkim#endif 71290207Sjkim#ifndef OPENSSL_NO_DH 72290207Sjkim# include <openssl/dh.h> 73290207Sjkim#endif 74238405Sjkim#include "asn1_locl.h" 75109998Smarkm 76238405Sjkimint pem_check_suffix(const char *pem_str, const char *suffix); 77109998Smarkm 78280297SjkimEVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, 79280297Sjkim void *u) 80280297Sjkim{ 81280297Sjkim char *nm = NULL; 82280297Sjkim const unsigned char *p = NULL; 83280297Sjkim unsigned char *data = NULL; 84280297Sjkim long len; 85280297Sjkim int slen; 86280297Sjkim EVP_PKEY *ret = NULL; 87109998Smarkm 88280297Sjkim if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp, cb, u)) 89280297Sjkim return NULL; 90280297Sjkim p = data; 91109998Smarkm 92280297Sjkim if (strcmp(nm, PEM_STRING_PKCS8INF) == 0) { 93280297Sjkim PKCS8_PRIV_KEY_INFO *p8inf; 94280297Sjkim p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len); 95280297Sjkim if (!p8inf) 96280297Sjkim goto p8err; 97280297Sjkim ret = EVP_PKCS82PKEY(p8inf); 98280297Sjkim if (x) { 99280297Sjkim if (*x) 100280297Sjkim EVP_PKEY_free((EVP_PKEY *)*x); 101280297Sjkim *x = ret; 102280297Sjkim } 103280297Sjkim PKCS8_PRIV_KEY_INFO_free(p8inf); 104280297Sjkim } else if (strcmp(nm, PEM_STRING_PKCS8) == 0) { 105280297Sjkim PKCS8_PRIV_KEY_INFO *p8inf; 106280297Sjkim X509_SIG *p8; 107280297Sjkim int klen; 108280297Sjkim char psbuf[PEM_BUFSIZE]; 109280297Sjkim p8 = d2i_X509_SIG(NULL, &p, len); 110280297Sjkim if (!p8) 111280297Sjkim goto p8err; 112280297Sjkim if (cb) 113280297Sjkim klen = cb(psbuf, PEM_BUFSIZE, 0, u); 114280297Sjkim else 115280297Sjkim klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u); 116337982Sjkim if (klen < 0) { 117280297Sjkim PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, PEM_R_BAD_PASSWORD_READ); 118280297Sjkim X509_SIG_free(p8); 119280297Sjkim goto err; 120280297Sjkim } 121280297Sjkim p8inf = PKCS8_decrypt(p8, psbuf, klen); 122280297Sjkim X509_SIG_free(p8); 123325337Sjkim OPENSSL_cleanse(psbuf, klen); 124280297Sjkim if (!p8inf) 125280297Sjkim goto p8err; 126280297Sjkim ret = EVP_PKCS82PKEY(p8inf); 127280297Sjkim if (x) { 128280297Sjkim if (*x) 129280297Sjkim EVP_PKEY_free((EVP_PKEY *)*x); 130280297Sjkim *x = ret; 131280297Sjkim } 132280297Sjkim PKCS8_PRIV_KEY_INFO_free(p8inf); 133280297Sjkim } else if ((slen = pem_check_suffix(nm, "PRIVATE KEY")) > 0) { 134280297Sjkim const EVP_PKEY_ASN1_METHOD *ameth; 135280297Sjkim ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen); 136280297Sjkim if (!ameth || !ameth->old_priv_decode) 137280297Sjkim goto p8err; 138280297Sjkim ret = d2i_PrivateKey(ameth->pkey_id, x, &p, len); 139280297Sjkim } 140280297Sjkim p8err: 141280297Sjkim if (ret == NULL) 142280297Sjkim PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, ERR_R_ASN1_LIB); 143280297Sjkim err: 144280297Sjkim OPENSSL_free(nm); 145280297Sjkim OPENSSL_cleanse(data, len); 146280297Sjkim OPENSSL_free(data); 147280297Sjkim return (ret); 148280297Sjkim} 149109998Smarkm 150238405Sjkimint PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, 151280297Sjkim unsigned char *kstr, int klen, 152280297Sjkim pem_password_cb *cb, void *u) 153280297Sjkim{ 154280297Sjkim char pem_str[80]; 155280297Sjkim if (!x->ameth || x->ameth->priv_encode) 156280297Sjkim return PEM_write_bio_PKCS8PrivateKey(bp, x, enc, 157280297Sjkim (char *)kstr, klen, cb, u); 158238405Sjkim 159280297Sjkim BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str); 160280297Sjkim return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey, 161280297Sjkim pem_str, bp, x, enc, kstr, klen, cb, u); 162280297Sjkim} 163238405Sjkim 164238405SjkimEVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x) 165280297Sjkim{ 166280297Sjkim char *nm = NULL; 167280297Sjkim const unsigned char *p = NULL; 168280297Sjkim unsigned char *data = NULL; 169280297Sjkim long len; 170280297Sjkim int slen; 171280297Sjkim EVP_PKEY *ret = NULL; 172238405Sjkim 173280297Sjkim if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_PARAMETERS, 174280297Sjkim bp, 0, NULL)) 175280297Sjkim return NULL; 176280297Sjkim p = data; 177238405Sjkim 178280297Sjkim if ((slen = pem_check_suffix(nm, "PARAMETERS")) > 0) { 179280297Sjkim ret = EVP_PKEY_new(); 180280297Sjkim if (!ret) 181280297Sjkim goto err; 182280297Sjkim if (!EVP_PKEY_set_type_str(ret, nm, slen) 183280297Sjkim || !ret->ameth->param_decode 184280297Sjkim || !ret->ameth->param_decode(ret, &p, len)) { 185280297Sjkim EVP_PKEY_free(ret); 186280297Sjkim ret = NULL; 187280297Sjkim goto err; 188280297Sjkim } 189280297Sjkim if (x) { 190280297Sjkim if (*x) 191280297Sjkim EVP_PKEY_free((EVP_PKEY *)*x); 192280297Sjkim *x = ret; 193280297Sjkim } 194280297Sjkim } 195280297Sjkim err: 196280297Sjkim if (ret == NULL) 197280297Sjkim PEMerr(PEM_F_PEM_READ_BIO_PARAMETERS, ERR_R_ASN1_LIB); 198280297Sjkim OPENSSL_free(nm); 199280297Sjkim OPENSSL_free(data); 200280297Sjkim return (ret); 201280297Sjkim} 202238405Sjkim 203238405Sjkimint PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x) 204280297Sjkim{ 205280297Sjkim char pem_str[80]; 206280297Sjkim if (!x->ameth || !x->ameth->param_encode) 207280297Sjkim return 0; 208238405Sjkim 209280297Sjkim BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str); 210280297Sjkim return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode, 211280297Sjkim pem_str, bp, x, NULL, NULL, 0, 0, NULL); 212280297Sjkim} 213238405Sjkim 214109998Smarkm#ifndef OPENSSL_NO_FP_API 215280297SjkimEVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, 216280297Sjkim void *u) 217280297Sjkim{ 218280297Sjkim BIO *b; 219280297Sjkim EVP_PKEY *ret; 220109998Smarkm 221280297Sjkim if ((b = BIO_new(BIO_s_file())) == NULL) { 222280297Sjkim PEMerr(PEM_F_PEM_READ_PRIVATEKEY, ERR_R_BUF_LIB); 223280297Sjkim return (0); 224280297Sjkim } 225280297Sjkim BIO_set_fp(b, fp, BIO_NOCLOSE); 226280297Sjkim ret = PEM_read_bio_PrivateKey(b, x, cb, u); 227280297Sjkim BIO_free(b); 228280297Sjkim return (ret); 229280297Sjkim} 230238405Sjkim 231238405Sjkimint PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc, 232280297Sjkim unsigned char *kstr, int klen, 233280297Sjkim pem_password_cb *cb, void *u) 234280297Sjkim{ 235280297Sjkim BIO *b; 236280297Sjkim int ret; 237238405Sjkim 238280297Sjkim if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) { 239280297Sjkim PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY, ERR_R_BUF_LIB); 240280297Sjkim return 0; 241280297Sjkim } 242280297Sjkim ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u); 243280297Sjkim BIO_free(b); 244280297Sjkim return ret; 245280297Sjkim} 246238405Sjkim 247109998Smarkm#endif 248290207Sjkim 249290207Sjkim#ifndef OPENSSL_NO_DH 250290207Sjkim 251290207Sjkim/* Transparently read in PKCS#3 or X9.42 DH parameters */ 252290207Sjkim 253290207SjkimDH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u) 254290207Sjkim{ 255290207Sjkim char *nm = NULL; 256290207Sjkim const unsigned char *p = NULL; 257290207Sjkim unsigned char *data = NULL; 258290207Sjkim long len; 259290207Sjkim DH *ret = NULL; 260290207Sjkim 261290207Sjkim if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_DHPARAMS, bp, cb, u)) 262290207Sjkim return NULL; 263290207Sjkim p = data; 264290207Sjkim 265290207Sjkim if (!strcmp(nm, PEM_STRING_DHXPARAMS)) 266290207Sjkim ret = d2i_DHxparams(x, &p, len); 267290207Sjkim else 268290207Sjkim ret = d2i_DHparams(x, &p, len); 269290207Sjkim 270290207Sjkim if (ret == NULL) 271290207Sjkim PEMerr(PEM_F_PEM_READ_BIO_DHPARAMS, ERR_R_ASN1_LIB); 272290207Sjkim OPENSSL_free(nm); 273290207Sjkim OPENSSL_free(data); 274290207Sjkim return ret; 275290207Sjkim} 276290207Sjkim 277290207Sjkim# ifndef OPENSSL_NO_FP_API 278290207SjkimDH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u) 279290207Sjkim{ 280290207Sjkim BIO *b; 281290207Sjkim DH *ret; 282290207Sjkim 283290207Sjkim if ((b = BIO_new(BIO_s_file())) == NULL) { 284290207Sjkim PEMerr(PEM_F_PEM_READ_DHPARAMS, ERR_R_BUF_LIB); 285290207Sjkim return (0); 286290207Sjkim } 287290207Sjkim BIO_set_fp(b, fp, BIO_NOCLOSE); 288290207Sjkim ret = PEM_read_bio_DHparams(b, x, cb, u); 289290207Sjkim BIO_free(b); 290290207Sjkim return (ret); 291290207Sjkim} 292290207Sjkim# endif 293290207Sjkim 294290207Sjkim#endif 295