Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/pem/pem_pkey.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
/*
11
 * We need to use some deprecated APIs
12
 */
13
#include "internal/deprecated.h"
14
15
#include <stdio.h>
16
#include <openssl/buffer.h>
17
#include <openssl/objects.h>
18
#include <openssl/evp.h>
19
#include <openssl/x509.h>
20
#include <openssl/pkcs12.h>
21
#include <openssl/pem.h>
22
#include <openssl/engine.h>
23
#include <openssl/dh.h>
24
#include <openssl/decoder.h>
25
#include <openssl/ui.h>
26
#include "internal/cryptlib.h"
27
#include "internal/passphrase.h"
28
#include "crypto/asn1.h"
29
#include "crypto/x509.h"
30
#include "crypto/evp.h"
31
#include "pem_local.h"
32
33
int ossl_pem_check_suffix(const char *pem_str, const char *suffix);
34
35
static EVP_PKEY *pem_read_bio_key_decoder(BIO *bp, EVP_PKEY **x,
36
    pem_password_cb *cb, void *u,
37
    OSSL_LIB_CTX *libctx,
38
    const char *propq,
39
    int selection)
40
124k
{
41
124k
    EVP_PKEY *pkey = NULL;
42
124k
    OSSL_DECODER_CTX *dctx = NULL;
43
124k
    int pos, newpos;
44
45
124k
    if ((pos = BIO_tell(bp)) < 0)
46
        /* We can depend on BIO_tell() thanks to the BIO_f_readbuffer() */
47
0
        return NULL;
48
49
124k
    dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "PEM", NULL, NULL,
50
124k
        selection, libctx, propq);
51
52
124k
    if (dctx == NULL)
53
0
        return NULL;
54
55
124k
    if (cb == NULL)
56
0
        cb = PEM_def_callback;
57
58
124k
    if (!OSSL_DECODER_CTX_set_pem_password_cb(dctx, cb, u))
59
0
        goto err;
60
61
124k
    ERR_set_mark();
62
124k
    while (!OSSL_DECODER_from_bio(dctx, bp) || pkey == NULL)
63
0
        if (BIO_eof(bp) != 0 || (newpos = BIO_tell(bp)) < 0 || newpos <= pos) {
64
0
            ERR_clear_last_mark();
65
0
            goto err;
66
0
        } else {
67
0
            if (ERR_GET_REASON(ERR_peek_error()) == ERR_R_UNSUPPORTED) {
68
                /* unsupported PEM data, try again */
69
0
                ERR_pop_to_mark();
70
0
                ERR_set_mark();
71
0
            } else {
72
                /* other error, bail out */
73
0
                ERR_clear_last_mark();
74
0
                goto err;
75
0
            }
76
0
            pos = newpos;
77
0
        }
78
124k
    ERR_pop_to_mark();
79
80
    /* if we were asked for private key, the public key is optional */
81
124k
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
82
124k
        selection = selection & ~OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
83
84
124k
    if (!evp_keymgmt_util_has(pkey, selection)) {
85
0
        EVP_PKEY_free(pkey);
86
0
        pkey = NULL;
87
0
        ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
88
0
        goto err;
89
0
    }
90
91
124k
    if (x != NULL) {
92
0
        EVP_PKEY_free(*x);
93
0
        *x = pkey;
94
0
    }
95
96
124k
err:
97
124k
    OSSL_DECODER_CTX_free(dctx);
98
124k
    return pkey;
99
124k
}
100
101
static EVP_PKEY *pem_read_bio_key_legacy(BIO *bp, EVP_PKEY **x,
102
    pem_password_cb *cb, void *u,
103
    OSSL_LIB_CTX *libctx,
104
    const char *propq,
105
    int selection)
106
0
{
107
0
    char *nm = NULL;
108
0
    const unsigned char *p = NULL;
109
0
    unsigned char *data = NULL;
110
0
    long len;
111
0
    int slen;
112
0
    EVP_PKEY *ret = NULL;
113
114
0
    ERR_set_mark(); /* not interested in PEM read errors */
115
0
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
116
0
        if (!PEM_bytes_read_bio_secmem(&data, &len, &nm,
117
0
                PEM_STRING_EVP_PKEY,
118
0
                bp, cb, u)) {
119
0
            ERR_pop_to_mark();
120
0
            return NULL;
121
0
        }
122
0
    } else {
123
0
        const char *pem_string = PEM_STRING_PARAMETERS;
124
125
0
        if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
126
0
            pem_string = PEM_STRING_PUBLIC;
127
0
        if (!PEM_bytes_read_bio(&data, &len, &nm,
128
0
                pem_string,
129
0
                bp, cb, u)) {
130
0
            ERR_pop_to_mark();
131
0
            return NULL;
132
0
        }
133
0
    }
134
0
    ERR_clear_last_mark();
135
0
    p = data;
136
137
0
    if (strcmp(nm, PEM_STRING_PKCS8INF) == 0) {
138
0
        PKCS8_PRIV_KEY_INFO *p8inf;
139
140
0
        if ((p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len)) == NULL)
141
0
            goto p8err;
142
0
        ret = evp_pkcs82pkey_legacy(p8inf, libctx, propq);
143
0
        if (x != NULL) {
144
0
            EVP_PKEY_free(*x);
145
0
            *x = ret;
146
0
        }
147
0
        PKCS8_PRIV_KEY_INFO_free(p8inf);
148
0
    } else if (strcmp(nm, PEM_STRING_PKCS8) == 0) {
149
0
        PKCS8_PRIV_KEY_INFO *p8inf;
150
0
        X509_SIG *p8;
151
0
        int klen;
152
0
        char psbuf[PEM_BUFSIZE];
153
154
0
        if ((p8 = d2i_X509_SIG(NULL, &p, len)) == NULL)
155
0
            goto p8err;
156
0
        if (cb != NULL)
157
0
            klen = cb(psbuf, PEM_BUFSIZE, 0, u);
158
0
        else
159
0
            klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
160
0
        if (klen < 0) {
161
0
            ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
162
0
            X509_SIG_free(p8);
163
0
            goto err;
164
0
        }
165
0
        p8inf = PKCS8_decrypt(p8, psbuf, klen);
166
0
        X509_SIG_free(p8);
167
0
        OPENSSL_cleanse(psbuf, klen);
168
0
        if (p8inf == NULL)
169
0
            goto p8err;
170
0
        ret = evp_pkcs82pkey_legacy(p8inf, libctx, propq);
171
0
        if (x != NULL) {
172
0
            EVP_PKEY_free(*x);
173
0
            *x = ret;
174
0
        }
175
0
        PKCS8_PRIV_KEY_INFO_free(p8inf);
176
0
    } else if ((slen = ossl_pem_check_suffix(nm, "PRIVATE KEY")) > 0) {
177
0
        const EVP_PKEY_ASN1_METHOD *ameth;
178
0
        ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
179
0
        if (ameth == NULL || ameth->old_priv_decode == NULL)
180
0
            goto p8err;
181
0
        ret = ossl_d2i_PrivateKey_legacy(ameth->pkey_id, x, &p, len, libctx,
182
0
            propq);
183
0
    } else if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0
184
0
        && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
185
        /* Trying legacy PUBKEY decoding only if we do not want private key. */
186
0
        ret = ossl_d2i_PUBKEY_legacy(x, &p, len);
187
0
    } else if ((selection & EVP_PKEY_KEYPAIR) == 0
188
0
        && (slen = ossl_pem_check_suffix(nm, "PARAMETERS")) > 0) {
189
        /* Trying legacy params decoding only if we do not want a key. */
190
0
        ret = EVP_PKEY_new();
191
0
        if (ret == NULL)
192
0
            goto err;
193
0
        if (!EVP_PKEY_set_type_str(ret, nm, slen)
194
0
            || !ret->ameth->param_decode
195
0
            || !ret->ameth->param_decode(ret, &p, len)) {
196
0
            EVP_PKEY_free(ret);
197
0
            ret = NULL;
198
0
            goto err;
199
0
        }
200
0
        if (x) {
201
0
            EVP_PKEY_free(*x);
202
0
            *x = ret;
203
0
        }
204
0
    }
205
206
0
p8err:
207
0
    if (ret == NULL && ERR_peek_last_error() == 0)
208
        /* ensure some error is reported but do not hide the real one */
209
0
        ERR_raise(ERR_LIB_PEM, ERR_R_ASN1_LIB);
210
0
err:
211
0
    OPENSSL_secure_free(nm);
212
0
    OPENSSL_secure_clear_free(data, len);
213
0
    return ret;
214
0
}
215
216
static EVP_PKEY *pem_read_bio_key(BIO *bp, EVP_PKEY **x,
217
    pem_password_cb *cb, void *u,
218
    OSSL_LIB_CTX *libctx,
219
    const char *propq,
220
    int selection)
221
124k
{
222
124k
    EVP_PKEY *ret = NULL;
223
124k
    BIO *new_bio = NULL;
224
124k
    int pos;
225
124k
    struct ossl_passphrase_data_st pwdata = { 0 };
226
227
124k
    if ((pos = BIO_tell(bp)) < 0) {
228
0
        new_bio = BIO_new(BIO_f_readbuffer());
229
0
        if (new_bio == NULL)
230
0
            return NULL;
231
0
        bp = BIO_push(new_bio, bp);
232
0
        pos = BIO_tell(bp);
233
0
    }
234
235
124k
    if (cb == NULL)
236
124k
        cb = PEM_def_callback;
237
238
124k
    if (!ossl_pw_set_pem_password_cb(&pwdata, cb, u)
239
124k
        || !ossl_pw_enable_passphrase_caching(&pwdata))
240
0
        goto err;
241
242
124k
    ERR_set_mark();
243
124k
    ret = pem_read_bio_key_decoder(bp, x, ossl_pw_pem_password, &pwdata,
244
124k
        libctx, propq, selection);
245
124k
    if (ret == NULL
246
0
        && (BIO_seek(bp, pos) < 0
247
0
            || (ret = pem_read_bio_key_legacy(bp, x,
248
0
                    ossl_pw_pem_password, &pwdata,
249
0
                    libctx, propq,
250
0
                    selection))
251
0
                == NULL))
252
0
        ERR_clear_last_mark();
253
124k
    else
254
124k
        ERR_pop_to_mark();
255
256
124k
err:
257
124k
    ossl_pw_clear_passphrase_data(&pwdata);
258
124k
    if (new_bio != NULL) {
259
0
        BIO_pop(new_bio);
260
0
        BIO_free(new_bio);
261
0
    }
262
124k
    return ret;
263
124k
}
264
265
EVP_PKEY *PEM_read_bio_PUBKEY_ex(BIO *bp, EVP_PKEY **x,
266
    pem_password_cb *cb, void *u,
267
    OSSL_LIB_CTX *libctx, const char *propq)
268
0
{
269
0
    return pem_read_bio_key(bp, x, cb, u, libctx, propq,
270
0
        EVP_PKEY_PUBLIC_KEY);
271
0
}
272
273
EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
274
    void *u)
275
0
{
276
0
    return PEM_read_bio_PUBKEY_ex(bp, x, cb, u, NULL, NULL);
277
0
}
278
279
#ifndef OPENSSL_NO_STDIO
280
EVP_PKEY *PEM_read_PUBKEY_ex(FILE *fp, EVP_PKEY **x,
281
    pem_password_cb *cb, void *u,
282
    OSSL_LIB_CTX *libctx, const char *propq)
283
0
{
284
0
    BIO *b;
285
0
    EVP_PKEY *ret;
286
287
0
    if ((b = BIO_new(BIO_s_file())) == NULL) {
288
0
        ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
289
0
        return 0;
290
0
    }
291
0
    BIO_set_fp(b, fp, BIO_NOCLOSE);
292
0
    ret = PEM_read_bio_PUBKEY_ex(b, x, cb, u, libctx, propq);
293
0
    BIO_free(b);
294
0
    return ret;
295
0
}
296
297
EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
298
0
{
299
0
    return PEM_read_PUBKEY_ex(fp, x, cb, u, NULL, NULL);
300
0
}
301
#endif
302
303
EVP_PKEY *PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x,
304
    pem_password_cb *cb, void *u,
305
    OSSL_LIB_CTX *libctx, const char *propq)
306
124k
{
307
124k
    return pem_read_bio_key(bp, x, cb, u, libctx, propq,
308
        /* we also want the public key, if available */
309
124k
        EVP_PKEY_KEYPAIR);
310
124k
}
311
312
EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
313
    void *u)
314
124k
{
315
124k
    return PEM_read_bio_PrivateKey_ex(bp, x, cb, u, NULL, NULL);
316
124k
}
317
318
PEM_write_cb_ex_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
319
0
{
320
0
    IMPLEMENT_PEM_provided_write_body_vars(pkey, PrivateKey, propq);
321
322
0
    IMPLEMENT_PEM_provided_write_body_pass();
323
0
    IMPLEMENT_PEM_provided_write_body_main(pkey, bio);
324
325
0
legacy:
326
0
    if (x != NULL && (x->ameth == NULL || x->ameth->priv_encode != NULL))
327
0
        return PEM_write_bio_PKCS8PrivateKey(out, x, enc,
328
0
            (const char *)kstr, klen, cb, u);
329
0
    return PEM_write_bio_PrivateKey_traditional(out, x, enc, kstr, klen, cb, u);
330
0
}
331
332
PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
333
0
{
334
0
    return PEM_write_bio_PrivateKey_ex(out, x, enc, kstr, klen, cb, u,
335
0
        NULL, NULL);
336
0
}
337
338
/*
339
 * Note: there is no way to tell a provided pkey encoder to use "traditional"
340
 * encoding.  Therefore, if the pkey is provided, we try to take a copy
341
 */
342
int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x,
343
    const EVP_CIPHER *enc,
344
    const unsigned char *kstr, int klen,
345
    pem_password_cb *cb, void *u)
346
0
{
347
0
    char pem_str[80];
348
0
    EVP_PKEY *copy = NULL;
349
0
    int ret;
350
351
0
    if (x == NULL)
352
0
        return 0;
353
354
0
    if (evp_pkey_is_assigned(x)
355
0
        && evp_pkey_is_provided(x)
356
0
        && evp_pkey_copy_downgraded(&copy, x))
357
0
        x = copy;
358
359
0
    if (x->ameth == NULL || x->ameth->old_priv_encode == NULL) {
360
0
        ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
361
0
        EVP_PKEY_free(copy);
362
0
        return 0;
363
0
    }
364
0
    BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
365
0
    ret = PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
366
0
        pem_str, bp, x, enc, kstr, klen, cb, u);
367
368
0
    EVP_PKEY_free(copy);
369
0
    return ret;
370
0
}
371
372
static int no_password_cb(char *buf, int num, int rwflag, void *userdata)
373
0
{
374
0
    return -1;
375
0
}
376
377
EVP_PKEY *PEM_read_bio_Parameters_ex(BIO *bp, EVP_PKEY **x,
378
    OSSL_LIB_CTX *libctx, const char *propq)
379
0
{
380
    /*
381
     * PEM_read_bio_Parameters(_ex) should never ask for a password. Any attempt
382
     * to get a password just fails.
383
     */
384
0
    return pem_read_bio_key(bp, x, no_password_cb, NULL, libctx, propq,
385
0
        EVP_PKEY_KEY_PARAMETERS);
386
0
}
387
388
EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
389
0
{
390
0
    return PEM_read_bio_Parameters_ex(bp, x, NULL, NULL);
391
0
}
392
393
PEM_write_fnsig(Parameters, EVP_PKEY, BIO, write_bio)
394
0
{
395
0
    char pem_str[80];
396
0
    IMPLEMENT_PEM_provided_write_body_vars(pkey, Parameters, NULL);
397
398
0
    IMPLEMENT_PEM_provided_write_body_main(pkey, bio);
399
400
0
legacy:
401
0
    if (!x->ameth || !x->ameth->param_encode)
402
0
        return 0;
403
404
0
    BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
405
0
    return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
406
0
        pem_str, out, x, NULL, NULL, 0, 0, NULL);
407
0
}
408
409
#ifndef OPENSSL_NO_STDIO
410
EVP_PKEY *PEM_read_PrivateKey_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
411
    void *u, OSSL_LIB_CTX *libctx,
412
    const char *propq)
413
0
{
414
0
    BIO *b;
415
0
    EVP_PKEY *ret;
416
417
0
    if ((b = BIO_new(BIO_s_file())) == NULL) {
418
0
        ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
419
0
        return 0;
420
0
    }
421
0
    BIO_set_fp(b, fp, BIO_NOCLOSE);
422
0
    ret = PEM_read_bio_PrivateKey_ex(b, x, cb, u, libctx, propq);
423
0
    BIO_free(b);
424
0
    return ret;
425
0
}
426
427
EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
428
    void *u)
429
0
{
430
0
    return PEM_read_PrivateKey_ex(fp, x, cb, u, NULL, NULL);
431
0
}
432
433
PEM_write_cb_ex_fnsig(PrivateKey, EVP_PKEY, FILE, write)
434
0
{
435
0
    BIO *b;
436
0
    int ret;
437
438
0
    if ((b = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
439
0
        ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
440
0
        return 0;
441
0
    }
442
0
    ret = PEM_write_bio_PrivateKey_ex(b, x, enc, kstr, klen, cb, u,
443
0
        libctx, propq);
444
0
    BIO_free(b);
445
0
    return ret;
446
0
}
447
448
PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, FILE, write)
449
0
{
450
0
    return PEM_write_PrivateKey_ex(out, x, enc, kstr, klen, cb, u, NULL, NULL);
451
0
}
452
#endif