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