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.
8280304Sjkim *
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).
15280304Sjkim *
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.
22280304Sjkim *
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 :-).
37280304Sjkim * 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)"
40280304Sjkim *
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.
52280304Sjkim *
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
69280304Sjkim# include <openssl/engine.h>
70238405Sjkim#endif
71238405Sjkim#include "asn1_locl.h"
72109998Smarkm
73238405Sjkimint pem_check_suffix(const char *pem_str, const char *suffix);
74109998Smarkm
75280304SjkimEVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
76280304Sjkim                                  void *u)
77280304Sjkim{
78280304Sjkim    char *nm = NULL;
79280304Sjkim    const unsigned char *p = NULL;
80280304Sjkim    unsigned char *data = NULL;
81280304Sjkim    long len;
82280304Sjkim    int slen;
83280304Sjkim    EVP_PKEY *ret = NULL;
84109998Smarkm
85280304Sjkim    if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp, cb, u))
86280304Sjkim        return NULL;
87280304Sjkim    p = data;
88109998Smarkm
89280304Sjkim    if (strcmp(nm, PEM_STRING_PKCS8INF) == 0) {
90280304Sjkim        PKCS8_PRIV_KEY_INFO *p8inf;
91280304Sjkim        p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len);
92280304Sjkim        if (!p8inf)
93280304Sjkim            goto p8err;
94280304Sjkim        ret = EVP_PKCS82PKEY(p8inf);
95280304Sjkim        if (x) {
96280304Sjkim            if (*x)
97280304Sjkim                EVP_PKEY_free((EVP_PKEY *)*x);
98280304Sjkim            *x = ret;
99280304Sjkim        }
100280304Sjkim        PKCS8_PRIV_KEY_INFO_free(p8inf);
101280304Sjkim    } else if (strcmp(nm, PEM_STRING_PKCS8) == 0) {
102280304Sjkim        PKCS8_PRIV_KEY_INFO *p8inf;
103280304Sjkim        X509_SIG *p8;
104280304Sjkim        int klen;
105280304Sjkim        char psbuf[PEM_BUFSIZE];
106280304Sjkim        p8 = d2i_X509_SIG(NULL, &p, len);
107280304Sjkim        if (!p8)
108280304Sjkim            goto p8err;
109280304Sjkim        if (cb)
110280304Sjkim            klen = cb(psbuf, PEM_BUFSIZE, 0, u);
111280304Sjkim        else
112280304Sjkim            klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
113280304Sjkim        if (klen <= 0) {
114280304Sjkim            PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, PEM_R_BAD_PASSWORD_READ);
115280304Sjkim            X509_SIG_free(p8);
116280304Sjkim            goto err;
117280304Sjkim        }
118280304Sjkim        p8inf = PKCS8_decrypt(p8, psbuf, klen);
119280304Sjkim        X509_SIG_free(p8);
120280304Sjkim        if (!p8inf)
121280304Sjkim            goto p8err;
122280304Sjkim        ret = EVP_PKCS82PKEY(p8inf);
123280304Sjkim        if (x) {
124280304Sjkim            if (*x)
125280304Sjkim                EVP_PKEY_free((EVP_PKEY *)*x);
126280304Sjkim            *x = ret;
127280304Sjkim        }
128280304Sjkim        PKCS8_PRIV_KEY_INFO_free(p8inf);
129280304Sjkim    } else if ((slen = pem_check_suffix(nm, "PRIVATE KEY")) > 0) {
130280304Sjkim        const EVP_PKEY_ASN1_METHOD *ameth;
131280304Sjkim        ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
132280304Sjkim        if (!ameth || !ameth->old_priv_decode)
133280304Sjkim            goto p8err;
134280304Sjkim        ret = d2i_PrivateKey(ameth->pkey_id, x, &p, len);
135280304Sjkim    }
136280304Sjkim p8err:
137280304Sjkim    if (ret == NULL)
138280304Sjkim        PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, ERR_R_ASN1_LIB);
139280304Sjkim err:
140280304Sjkim    OPENSSL_free(nm);
141280304Sjkim    OPENSSL_cleanse(data, len);
142280304Sjkim    OPENSSL_free(data);
143280304Sjkim    return (ret);
144280304Sjkim}
145109998Smarkm
146238405Sjkimint PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
147280304Sjkim                             unsigned char *kstr, int klen,
148280304Sjkim                             pem_password_cb *cb, void *u)
149280304Sjkim{
150280304Sjkim    char pem_str[80];
151280304Sjkim    if (!x->ameth || x->ameth->priv_encode)
152280304Sjkim        return PEM_write_bio_PKCS8PrivateKey(bp, x, enc,
153280304Sjkim                                             (char *)kstr, klen, cb, u);
154238405Sjkim
155280304Sjkim    BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
156280304Sjkim    return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
157280304Sjkim                              pem_str, bp, x, enc, kstr, klen, cb, u);
158280304Sjkim}
159238405Sjkim
160238405SjkimEVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
161280304Sjkim{
162280304Sjkim    char *nm = NULL;
163280304Sjkim    const unsigned char *p = NULL;
164280304Sjkim    unsigned char *data = NULL;
165280304Sjkim    long len;
166280304Sjkim    int slen;
167280304Sjkim    EVP_PKEY *ret = NULL;
168238405Sjkim
169280304Sjkim    if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_PARAMETERS,
170280304Sjkim                            bp, 0, NULL))
171280304Sjkim        return NULL;
172280304Sjkim    p = data;
173238405Sjkim
174280304Sjkim    if ((slen = pem_check_suffix(nm, "PARAMETERS")) > 0) {
175280304Sjkim        ret = EVP_PKEY_new();
176280304Sjkim        if (!ret)
177280304Sjkim            goto err;
178280304Sjkim        if (!EVP_PKEY_set_type_str(ret, nm, slen)
179280304Sjkim            || !ret->ameth->param_decode
180280304Sjkim            || !ret->ameth->param_decode(ret, &p, len)) {
181280304Sjkim            EVP_PKEY_free(ret);
182280304Sjkim            ret = NULL;
183280304Sjkim            goto err;
184280304Sjkim        }
185280304Sjkim        if (x) {
186280304Sjkim            if (*x)
187280304Sjkim                EVP_PKEY_free((EVP_PKEY *)*x);
188280304Sjkim            *x = ret;
189280304Sjkim        }
190280304Sjkim    }
191280304Sjkim err:
192280304Sjkim    if (ret == NULL)
193280304Sjkim        PEMerr(PEM_F_PEM_READ_BIO_PARAMETERS, ERR_R_ASN1_LIB);
194280304Sjkim    OPENSSL_free(nm);
195280304Sjkim    OPENSSL_free(data);
196280304Sjkim    return (ret);
197280304Sjkim}
198238405Sjkim
199238405Sjkimint PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x)
200280304Sjkim{
201280304Sjkim    char pem_str[80];
202280304Sjkim    if (!x->ameth || !x->ameth->param_encode)
203280304Sjkim        return 0;
204238405Sjkim
205280304Sjkim    BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
206280304Sjkim    return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
207280304Sjkim                              pem_str, bp, x, NULL, NULL, 0, 0, NULL);
208280304Sjkim}
209238405Sjkim
210109998Smarkm#ifndef OPENSSL_NO_FP_API
211280304SjkimEVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
212280304Sjkim                              void *u)
213280304Sjkim{
214280304Sjkim    BIO *b;
215280304Sjkim    EVP_PKEY *ret;
216109998Smarkm
217280304Sjkim    if ((b = BIO_new(BIO_s_file())) == NULL) {
218280304Sjkim        PEMerr(PEM_F_PEM_READ_PRIVATEKEY, ERR_R_BUF_LIB);
219280304Sjkim        return (0);
220280304Sjkim    }
221280304Sjkim    BIO_set_fp(b, fp, BIO_NOCLOSE);
222280304Sjkim    ret = PEM_read_bio_PrivateKey(b, x, cb, u);
223280304Sjkim    BIO_free(b);
224280304Sjkim    return (ret);
225280304Sjkim}
226238405Sjkim
227238405Sjkimint PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
228280304Sjkim                         unsigned char *kstr, int klen,
229280304Sjkim                         pem_password_cb *cb, void *u)
230280304Sjkim{
231280304Sjkim    BIO *b;
232280304Sjkim    int ret;
233238405Sjkim
234280304Sjkim    if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
235280304Sjkim        PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY, ERR_R_BUF_LIB);
236280304Sjkim        return 0;
237280304Sjkim    }
238280304Sjkim    ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
239280304Sjkim    BIO_free(b);
240280304Sjkim    return ret;
241280304Sjkim}
242238405Sjkim
243109998Smarkm#endif
244