Coverage Report

Created: 2025-12-10 06:24

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