10Sstevel@tonic-gate /* crypto/pem/pem_pkey.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/buffer.h>
620Sstevel@tonic-gate #include <openssl/objects.h>
630Sstevel@tonic-gate #include <openssl/evp.h>
640Sstevel@tonic-gate #include <openssl/rand.h>
650Sstevel@tonic-gate #include <openssl/x509.h>
660Sstevel@tonic-gate #include <openssl/pkcs12.h>
670Sstevel@tonic-gate #include <openssl/pem.h>
680Sstevel@tonic-gate
690Sstevel@tonic-gate static int do_pk8pkey(BIO *bp, EVP_PKEY *x, int isder,
700Sstevel@tonic-gate int nid, const EVP_CIPHER *enc,
710Sstevel@tonic-gate char *kstr, int klen,
720Sstevel@tonic-gate pem_password_cb *cb, void *u);
730Sstevel@tonic-gate static int do_pk8pkey_fp(FILE *bp, EVP_PKEY *x, int isder,
740Sstevel@tonic-gate int nid, const EVP_CIPHER *enc,
750Sstevel@tonic-gate char *kstr, int klen,
760Sstevel@tonic-gate pem_password_cb *cb, void *u);
770Sstevel@tonic-gate
780Sstevel@tonic-gate /* These functions write a private key in PKCS#8 format: it is a "drop in"
790Sstevel@tonic-gate * replacement for PEM_write_bio_PrivateKey() and friends. As usual if 'enc'
800Sstevel@tonic-gate * is NULL then it uses the unencrypted private key form. The 'nid' versions
810Sstevel@tonic-gate * uses PKCS#5 v1.5 PBE algorithms whereas the others use PKCS#5 v2.0.
820Sstevel@tonic-gate */
830Sstevel@tonic-gate
PEM_write_bio_PKCS8PrivateKey_nid(BIO * bp,EVP_PKEY * x,int nid,char * kstr,int klen,pem_password_cb * cb,void * u)840Sstevel@tonic-gate int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, EVP_PKEY *x, int nid,
850Sstevel@tonic-gate char *kstr, int klen,
860Sstevel@tonic-gate pem_password_cb *cb, void *u)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate return do_pk8pkey(bp, x, 0, nid, NULL, kstr, klen, cb, u);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
PEM_write_bio_PKCS8PrivateKey(BIO * bp,EVP_PKEY * x,const EVP_CIPHER * enc,char * kstr,int klen,pem_password_cb * cb,void * u)910Sstevel@tonic-gate int PEM_write_bio_PKCS8PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
920Sstevel@tonic-gate char *kstr, int klen,
930Sstevel@tonic-gate pem_password_cb *cb, void *u)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate return do_pk8pkey(bp, x, 0, -1, enc, kstr, klen, cb, u);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
i2d_PKCS8PrivateKey_bio(BIO * bp,EVP_PKEY * x,const EVP_CIPHER * enc,char * kstr,int klen,pem_password_cb * cb,void * u)980Sstevel@tonic-gate int i2d_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
990Sstevel@tonic-gate char *kstr, int klen,
1000Sstevel@tonic-gate pem_password_cb *cb, void *u)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate return do_pk8pkey(bp, x, 1, -1, enc, kstr, klen, cb, u);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate
i2d_PKCS8PrivateKey_nid_bio(BIO * bp,EVP_PKEY * x,int nid,char * kstr,int klen,pem_password_cb * cb,void * u)1050Sstevel@tonic-gate int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, EVP_PKEY *x, int nid,
1060Sstevel@tonic-gate char *kstr, int klen,
1070Sstevel@tonic-gate pem_password_cb *cb, void *u)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate return do_pk8pkey(bp, x, 1, nid, NULL, kstr, klen, cb, u);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
do_pk8pkey(BIO * bp,EVP_PKEY * x,int isder,int nid,const EVP_CIPHER * enc,char * kstr,int klen,pem_password_cb * cb,void * u)1120Sstevel@tonic-gate static int do_pk8pkey(BIO *bp, EVP_PKEY *x, int isder, int nid, const EVP_CIPHER *enc,
1130Sstevel@tonic-gate char *kstr, int klen,
1140Sstevel@tonic-gate pem_password_cb *cb, void *u)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate X509_SIG *p8;
1170Sstevel@tonic-gate PKCS8_PRIV_KEY_INFO *p8inf;
1180Sstevel@tonic-gate char buf[PEM_BUFSIZE];
1190Sstevel@tonic-gate int ret;
1200Sstevel@tonic-gate if(!(p8inf = EVP_PKEY2PKCS8(x))) {
121*2139Sjp161948 PEMerr(PEM_F_DO_PK8PKEY,
1220Sstevel@tonic-gate PEM_R_ERROR_CONVERTING_PRIVATE_KEY);
1230Sstevel@tonic-gate return 0;
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate if(enc || (nid != -1)) {
1260Sstevel@tonic-gate if(!kstr) {
1270Sstevel@tonic-gate if(!cb) klen = PEM_def_callback(buf, PEM_BUFSIZE, 1, u);
1280Sstevel@tonic-gate else klen = cb(buf, PEM_BUFSIZE, 1, u);
1290Sstevel@tonic-gate if(klen <= 0) {
130*2139Sjp161948 PEMerr(PEM_F_DO_PK8PKEY,PEM_R_READ_KEY);
1310Sstevel@tonic-gate PKCS8_PRIV_KEY_INFO_free(p8inf);
1320Sstevel@tonic-gate return 0;
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate kstr = buf;
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate p8 = PKCS8_encrypt(nid, enc, kstr, klen, NULL, 0, 0, p8inf);
1380Sstevel@tonic-gate if(kstr == buf) OPENSSL_cleanse(buf, klen);
1390Sstevel@tonic-gate PKCS8_PRIV_KEY_INFO_free(p8inf);
1400Sstevel@tonic-gate if(isder) ret = i2d_PKCS8_bio(bp, p8);
1410Sstevel@tonic-gate else ret = PEM_write_bio_PKCS8(bp, p8);
1420Sstevel@tonic-gate X509_SIG_free(p8);
1430Sstevel@tonic-gate return ret;
1440Sstevel@tonic-gate } else {
1450Sstevel@tonic-gate if(isder) ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
1460Sstevel@tonic-gate else ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(bp, p8inf);
1470Sstevel@tonic-gate PKCS8_PRIV_KEY_INFO_free(p8inf);
1480Sstevel@tonic-gate return ret;
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate
d2i_PKCS8PrivateKey_bio(BIO * bp,EVP_PKEY ** x,pem_password_cb * cb,void * u)1520Sstevel@tonic-gate EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate PKCS8_PRIV_KEY_INFO *p8inf = NULL;
1550Sstevel@tonic-gate X509_SIG *p8 = NULL;
1560Sstevel@tonic-gate int klen;
1570Sstevel@tonic-gate EVP_PKEY *ret;
1580Sstevel@tonic-gate char psbuf[PEM_BUFSIZE];
1590Sstevel@tonic-gate p8 = d2i_PKCS8_bio(bp, NULL);
1600Sstevel@tonic-gate if(!p8) return NULL;
1610Sstevel@tonic-gate if (cb) klen=cb(psbuf,PEM_BUFSIZE,0,u);
1620Sstevel@tonic-gate else klen=PEM_def_callback(psbuf,PEM_BUFSIZE,0,u);
1630Sstevel@tonic-gate if (klen <= 0) {
1640Sstevel@tonic-gate PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_BIO, PEM_R_BAD_PASSWORD_READ);
1650Sstevel@tonic-gate X509_SIG_free(p8);
1660Sstevel@tonic-gate return NULL;
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate p8inf = PKCS8_decrypt(p8, psbuf, klen);
1690Sstevel@tonic-gate X509_SIG_free(p8);
1700Sstevel@tonic-gate if(!p8inf) return NULL;
1710Sstevel@tonic-gate ret = EVP_PKCS82PKEY(p8inf);
1720Sstevel@tonic-gate PKCS8_PRIV_KEY_INFO_free(p8inf);
1730Sstevel@tonic-gate if(!ret) return NULL;
1740Sstevel@tonic-gate if(x) {
1750Sstevel@tonic-gate if(*x) EVP_PKEY_free(*x);
1760Sstevel@tonic-gate *x = ret;
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate return ret;
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
1820Sstevel@tonic-gate
i2d_PKCS8PrivateKey_fp(FILE * fp,EVP_PKEY * x,const EVP_CIPHER * enc,char * kstr,int klen,pem_password_cb * cb,void * u)1830Sstevel@tonic-gate int i2d_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
1840Sstevel@tonic-gate char *kstr, int klen,
1850Sstevel@tonic-gate pem_password_cb *cb, void *u)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate return do_pk8pkey_fp(fp, x, 1, -1, enc, kstr, klen, cb, u);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
i2d_PKCS8PrivateKey_nid_fp(FILE * fp,EVP_PKEY * x,int nid,char * kstr,int klen,pem_password_cb * cb,void * u)1900Sstevel@tonic-gate int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, EVP_PKEY *x, int nid,
1910Sstevel@tonic-gate char *kstr, int klen,
1920Sstevel@tonic-gate pem_password_cb *cb, void *u)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate return do_pk8pkey_fp(fp, x, 1, nid, NULL, kstr, klen, cb, u);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
PEM_write_PKCS8PrivateKey_nid(FILE * fp,EVP_PKEY * x,int nid,char * kstr,int klen,pem_password_cb * cb,void * u)1970Sstevel@tonic-gate int PEM_write_PKCS8PrivateKey_nid(FILE *fp, EVP_PKEY *x, int nid,
1980Sstevel@tonic-gate char *kstr, int klen,
1990Sstevel@tonic-gate pem_password_cb *cb, void *u)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate return do_pk8pkey_fp(fp, x, 0, nid, NULL, kstr, klen, cb, u);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate
PEM_write_PKCS8PrivateKey(FILE * fp,EVP_PKEY * x,const EVP_CIPHER * enc,char * kstr,int klen,pem_password_cb * cb,void * u)2040Sstevel@tonic-gate int PEM_write_PKCS8PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
2050Sstevel@tonic-gate char *kstr, int klen, pem_password_cb *cb, void *u)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate return do_pk8pkey_fp(fp, x, 0, -1, enc, kstr, klen, cb, u);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
do_pk8pkey_fp(FILE * fp,EVP_PKEY * x,int isder,int nid,const EVP_CIPHER * enc,char * kstr,int klen,pem_password_cb * cb,void * u)2100Sstevel@tonic-gate static int do_pk8pkey_fp(FILE *fp, EVP_PKEY *x, int isder, int nid, const EVP_CIPHER *enc,
2110Sstevel@tonic-gate char *kstr, int klen,
2120Sstevel@tonic-gate pem_password_cb *cb, void *u)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate BIO *bp;
2150Sstevel@tonic-gate int ret;
2160Sstevel@tonic-gate if(!(bp = BIO_new_fp(fp, BIO_NOCLOSE))) {
217*2139Sjp161948 PEMerr(PEM_F_DO_PK8PKEY_FP,ERR_R_BUF_LIB);
2180Sstevel@tonic-gate return(0);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate ret = do_pk8pkey(bp, x, isder, nid, enc, kstr, klen, cb, u);
2210Sstevel@tonic-gate BIO_free(bp);
2220Sstevel@tonic-gate return ret;
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
d2i_PKCS8PrivateKey_fp(FILE * fp,EVP_PKEY ** x,pem_password_cb * cb,void * u)2250Sstevel@tonic-gate EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate BIO *bp;
2280Sstevel@tonic-gate EVP_PKEY *ret;
2290Sstevel@tonic-gate if(!(bp = BIO_new_fp(fp, BIO_NOCLOSE))) {
2300Sstevel@tonic-gate PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_FP,ERR_R_BUF_LIB);
2310Sstevel@tonic-gate return NULL;
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate ret = d2i_PKCS8PrivateKey_bio(bp, x, cb, u);
2340Sstevel@tonic-gate BIO_free(bp);
2350Sstevel@tonic-gate return ret;
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate #endif
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate IMPLEMENT_PEM_rw(PKCS8, X509_SIG, PEM_STRING_PKCS8, X509_SIG)
2410Sstevel@tonic-gate IMPLEMENT_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO, PEM_STRING_PKCS8INF,
2420Sstevel@tonic-gate PKCS8_PRIV_KEY_INFO)
243