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.
8296465Sdelphij *
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).
15296465Sdelphij *
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.
22296465Sdelphij *
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 :-).
37296465Sdelphij * 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)"
40296465Sdelphij *
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.
52296465Sdelphij *
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>
68109998Smarkm
69296465SdelphijEVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
70296465Sdelphij                                  void *u)
71296465Sdelphij{
72296465Sdelphij    char *nm = NULL;
73296465Sdelphij    const unsigned char *p = NULL;
74296465Sdelphij    unsigned char *data = NULL;
75296465Sdelphij    long len;
76296465Sdelphij    EVP_PKEY *ret = NULL;
77109998Smarkm
78296465Sdelphij    if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp, cb, u))
79296465Sdelphij        return NULL;
80296465Sdelphij    p = data;
81109998Smarkm
82296465Sdelphij    if (strcmp(nm, PEM_STRING_RSA) == 0)
83296465Sdelphij        ret = d2i_PrivateKey(EVP_PKEY_RSA, x, &p, len);
84296465Sdelphij    else if (strcmp(nm, PEM_STRING_DSA) == 0)
85296465Sdelphij        ret = d2i_PrivateKey(EVP_PKEY_DSA, x, &p, len);
86296465Sdelphij    else if (strcmp(nm, PEM_STRING_ECPRIVATEKEY) == 0)
87296465Sdelphij        ret = d2i_PrivateKey(EVP_PKEY_EC, x, &p, len);
88296465Sdelphij    else if (strcmp(nm, PEM_STRING_PKCS8INF) == 0) {
89296465Sdelphij        PKCS8_PRIV_KEY_INFO *p8inf;
90296465Sdelphij        p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len);
91296465Sdelphij        if (!p8inf)
92296465Sdelphij            goto p8err;
93296465Sdelphij        ret = EVP_PKCS82PKEY(p8inf);
94296465Sdelphij        if (x) {
95296465Sdelphij            if (*x)
96296465Sdelphij                EVP_PKEY_free((EVP_PKEY *)*x);
97296465Sdelphij            *x = ret;
98296465Sdelphij        }
99296465Sdelphij        PKCS8_PRIV_KEY_INFO_free(p8inf);
100296465Sdelphij    } else if (strcmp(nm, PEM_STRING_PKCS8) == 0) {
101296465Sdelphij        PKCS8_PRIV_KEY_INFO *p8inf;
102296465Sdelphij        X509_SIG *p8;
103296465Sdelphij        int klen;
104296465Sdelphij        char psbuf[PEM_BUFSIZE];
105296465Sdelphij        p8 = d2i_X509_SIG(NULL, &p, len);
106296465Sdelphij        if (!p8)
107296465Sdelphij            goto p8err;
108296465Sdelphij        if (cb)
109296465Sdelphij            klen = cb(psbuf, PEM_BUFSIZE, 0, u);
110296465Sdelphij        else
111296465Sdelphij            klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
112296465Sdelphij        if (klen <= 0) {
113296465Sdelphij            PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, PEM_R_BAD_PASSWORD_READ);
114296465Sdelphij            X509_SIG_free(p8);
115296465Sdelphij            goto err;
116296465Sdelphij        }
117296465Sdelphij        p8inf = PKCS8_decrypt(p8, psbuf, klen);
118296465Sdelphij        X509_SIG_free(p8);
119296465Sdelphij        if (!p8inf)
120296465Sdelphij            goto p8err;
121296465Sdelphij        ret = EVP_PKCS82PKEY(p8inf);
122296465Sdelphij        if (x) {
123296465Sdelphij            if (*x)
124296465Sdelphij                EVP_PKEY_free((EVP_PKEY *)*x);
125296465Sdelphij            *x = ret;
126296465Sdelphij        }
127296465Sdelphij        PKCS8_PRIV_KEY_INFO_free(p8inf);
128296465Sdelphij    }
129296465Sdelphij p8err:
130296465Sdelphij    if (ret == NULL)
131296465Sdelphij        PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, ERR_R_ASN1_LIB);
132296465Sdelphij err:
133296465Sdelphij    OPENSSL_free(nm);
134296465Sdelphij    OPENSSL_cleanse(data, len);
135296465Sdelphij    OPENSSL_free(data);
136296465Sdelphij    return (ret);
137296465Sdelphij}
138109998Smarkm
139109998Smarkm#ifndef OPENSSL_NO_FP_API
140296465SdelphijEVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
141296465Sdelphij                              void *u)
142296465Sdelphij{
143296465Sdelphij    BIO *b;
144296465Sdelphij    EVP_PKEY *ret;
145109998Smarkm
146296465Sdelphij    if ((b = BIO_new(BIO_s_file())) == NULL) {
147296465Sdelphij        PEMerr(PEM_F_PEM_READ_PRIVATEKEY, ERR_R_BUF_LIB);
148296465Sdelphij        return (0);
149296465Sdelphij    }
150296465Sdelphij    BIO_set_fp(b, fp, BIO_NOCLOSE);
151296465Sdelphij    ret = PEM_read_bio_PrivateKey(b, x, cb, u);
152296465Sdelphij    BIO_free(b);
153296465Sdelphij    return (ret);
154296465Sdelphij}
155109998Smarkm#endif
156