APPS: Make it possible to load_cert() from stdin again
[oweals/openssl.git] / crypto / pem / pem_pk8.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/buffer.h>
13 #include <openssl/objects.h>
14 #include <openssl/evp.h>
15 #include <openssl/x509.h>
16 #include <openssl/pkcs12.h>
17 #include <openssl/pem.h>
18 #include <openssl/serializer.h>
19
20 static int do_pk8pkey(BIO *bp, const EVP_PKEY *x, int isder,
21                       int nid, const EVP_CIPHER *enc,
22                       const char *kstr, int klen,
23                       pem_password_cb *cb, void *u);
24
25 #ifndef OPENSSL_NO_STDIO
26 static int do_pk8pkey_fp(FILE *bp, const EVP_PKEY *x, int isder,
27                          int nid, const EVP_CIPHER *enc,
28                          const char *kstr, int klen,
29                          pem_password_cb *cb, void *u);
30 #endif
31 /*
32  * These functions write a private key in PKCS#8 format: it is a "drop in"
33  * replacement for PEM_write_bio_PrivateKey() and friends. As usual if 'enc'
34  * is NULL then it uses the unencrypted private key form. The 'nid' versions
35  * uses PKCS#5 v1.5 PBE algorithms whereas the others use PKCS#5 v2.0.
36  */
37
38 int PEM_write_bio_PKCS8PrivateKey_nid(BIO *bp, const EVP_PKEY *x, int nid,
39                                       const char *kstr, int klen,
40                                       pem_password_cb *cb, void *u)
41 {
42     return do_pk8pkey(bp, x, 0, nid, NULL, kstr, klen, cb, u);
43 }
44
45 int PEM_write_bio_PKCS8PrivateKey(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
46                                   const char *kstr, int klen,
47                                   pem_password_cb *cb, void *u)
48 {
49     return do_pk8pkey(bp, x, 0, -1, enc, kstr, klen, cb, u);
50 }
51
52 int i2d_PKCS8PrivateKey_bio(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
53                             const char *kstr, int klen,
54                             pem_password_cb *cb, void *u)
55 {
56     return do_pk8pkey(bp, x, 1, -1, enc, kstr, klen, cb, u);
57 }
58
59 int i2d_PKCS8PrivateKey_nid_bio(BIO *bp, const EVP_PKEY *x, int nid,
60                                 const char *kstr, int klen,
61                                 pem_password_cb *cb, void *u)
62 {
63     return do_pk8pkey(bp, x, 1, nid, NULL, kstr, klen, cb, u);
64 }
65
66 static int do_pk8pkey(BIO *bp, const EVP_PKEY *x, int isder, int nid,
67                       const EVP_CIPHER *enc, const char *kstr, int klen,
68                       pem_password_cb *cb, void *u)
69 {
70     int ret = 0;
71     const char *pq = isder
72         ? OSSL_SERIALIZER_PrivateKey_TO_DER_PQ
73         : OSSL_SERIALIZER_PrivateKey_TO_PEM_PQ;
74     OSSL_SERIALIZER_CTX *ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(x, pq);
75
76     if (ctx == NULL)
77         return 0;
78
79     /*
80      * If no keystring or callback is set, OpenSSL traditionally uses the
81      * user's cb argument as a password string, or if that's NULL, it falls
82      * back on PEM_def_callback().
83      */
84     if (kstr == NULL && cb == NULL) {
85         if (u != NULL) {
86             kstr = u;
87             klen = strlen(u);
88         } else {
89             cb = PEM_def_callback;
90         }
91     }
92
93     if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL) {
94         ret = 1;
95         if (enc != NULL) {
96             ret = 0;
97             if (OSSL_SERIALIZER_CTX_set_cipher(ctx, EVP_CIPHER_name(enc),
98                                                NULL)) {
99                 const unsigned char *ukstr = (const unsigned char *)kstr;
100
101                 /*
102                  * Try to pass the passphrase if one was given, or the
103                  * passphrase callback if one was given.  If none of them
104                  * are given and that's wrong, we rely on the _to_bio()
105                  * call to generate errors.
106                  */
107                 ret = 1;
108                 if (kstr != NULL
109                     && !OSSL_SERIALIZER_CTX_set_passphrase(ctx, ukstr, klen))
110                     ret = 0;
111                 else if (cb != NULL
112                          && !OSSL_SERIALIZER_CTX_set_passphrase_cb(ctx, 1,
113                                                                    cb, u))
114                     ret = 0;
115             }
116         }
117         ret = ret && OSSL_SERIALIZER_to_bio(ctx, bp);
118     } else {
119         X509_SIG *p8;
120         PKCS8_PRIV_KEY_INFO *p8inf;
121         char buf[PEM_BUFSIZE];
122
123         ret = 0;
124         if ((p8inf = EVP_PKEY2PKCS8(x)) == NULL) {
125             PEMerr(PEM_F_DO_PK8PKEY, PEM_R_ERROR_CONVERTING_PRIVATE_KEY);
126             goto legacy_end;
127         }
128         if (enc || (nid != -1)) {
129             if (kstr == NULL) {
130                 klen = cb(buf, PEM_BUFSIZE, 1, u);
131                 if (klen <= 0) {
132                     PEMerr(PEM_F_DO_PK8PKEY, PEM_R_READ_KEY);
133                     goto legacy_end;
134                 }
135
136                 kstr = buf;
137             }
138             p8 = PKCS8_encrypt(nid, enc, kstr, klen, NULL, 0, 0, p8inf);
139             if (kstr == buf)
140                 OPENSSL_cleanse(buf, klen);
141             if (p8 == NULL)
142                 goto legacy_end;
143             if (isder)
144                 ret = i2d_PKCS8_bio(bp, p8);
145             else
146                 ret = PEM_write_bio_PKCS8(bp, p8);
147             X509_SIG_free(p8);
148         } else {
149             if (isder)
150                 ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
151             else
152                 ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(bp, p8inf);
153         }
154      legacy_end:
155         PKCS8_PRIV_KEY_INFO_free(p8inf);
156     }
157     OSSL_SERIALIZER_CTX_free(ctx);
158     return ret;
159 }
160
161 EVP_PKEY *d2i_PKCS8PrivateKey_bio(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
162                                   void *u)
163 {
164     PKCS8_PRIV_KEY_INFO *p8inf = NULL;
165     X509_SIG *p8 = NULL;
166     int klen;
167     EVP_PKEY *ret;
168     char psbuf[PEM_BUFSIZE];
169
170     p8 = d2i_PKCS8_bio(bp, NULL);
171     if (p8 == NULL)
172         return NULL;
173     if (cb != NULL)
174         klen = cb(psbuf, PEM_BUFSIZE, 0, u);
175     else
176         klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
177     if (klen < 0) {
178         PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_BIO, PEM_R_BAD_PASSWORD_READ);
179         X509_SIG_free(p8);
180         return NULL;
181     }
182     p8inf = PKCS8_decrypt(p8, psbuf, klen);
183     X509_SIG_free(p8);
184     OPENSSL_cleanse(psbuf, klen);
185     if (p8inf == NULL)
186         return NULL;
187     ret = EVP_PKCS82PKEY(p8inf);
188     PKCS8_PRIV_KEY_INFO_free(p8inf);
189     if (!ret)
190         return NULL;
191     if (x != NULL) {
192         EVP_PKEY_free(*x);
193         *x = ret;
194     }
195     return ret;
196 }
197
198 #ifndef OPENSSL_NO_STDIO
199
200 int i2d_PKCS8PrivateKey_fp(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
201                            const char *kstr, int klen,
202                            pem_password_cb *cb, void *u)
203 {
204     return do_pk8pkey_fp(fp, x, 1, -1, enc, kstr, klen, cb, u);
205 }
206
207 int i2d_PKCS8PrivateKey_nid_fp(FILE *fp, const EVP_PKEY *x, int nid,
208                                const char *kstr, int klen,
209                                pem_password_cb *cb, void *u)
210 {
211     return do_pk8pkey_fp(fp, x, 1, nid, NULL, kstr, klen, cb, u);
212 }
213
214 int PEM_write_PKCS8PrivateKey_nid(FILE *fp, const EVP_PKEY *x, int nid,
215                                   const char *kstr, int klen,
216                                   pem_password_cb *cb, void *u)
217 {
218     return do_pk8pkey_fp(fp, x, 0, nid, NULL, kstr, klen, cb, u);
219 }
220
221 int PEM_write_PKCS8PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
222                               const char *kstr, int klen,
223                               pem_password_cb *cb, void *u)
224 {
225     return do_pk8pkey_fp(fp, x, 0, -1, enc, kstr, klen, cb, u);
226 }
227
228 static int do_pk8pkey_fp(FILE *fp, const EVP_PKEY *x, int isder, int nid,
229                          const EVP_CIPHER *enc, const char *kstr, int klen,
230                          pem_password_cb *cb, void *u)
231 {
232     BIO *bp;
233     int ret;
234
235     if ((bp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
236         PEMerr(PEM_F_DO_PK8PKEY_FP, ERR_R_BUF_LIB);
237         return 0;
238     }
239     ret = do_pk8pkey(bp, x, isder, nid, enc, kstr, klen, cb, u);
240     BIO_free(bp);
241     return ret;
242 }
243
244 EVP_PKEY *d2i_PKCS8PrivateKey_fp(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
245                                  void *u)
246 {
247     BIO *bp;
248     EVP_PKEY *ret;
249
250     if ((bp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
251         PEMerr(PEM_F_D2I_PKCS8PRIVATEKEY_FP, ERR_R_BUF_LIB);
252         return NULL;
253     }
254     ret = d2i_PKCS8PrivateKey_bio(bp, x, cb, u);
255     BIO_free(bp);
256     return ret;
257 }
258
259 #endif
260
261 IMPLEMENT_PEM_rw(PKCS8, X509_SIG, PEM_STRING_PKCS8, X509_SIG)
262
263
264 IMPLEMENT_PEM_rw(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO, PEM_STRING_PKCS8INF,
265              PKCS8_PRIV_KEY_INFO)