Coverage Report

Created: 2025-12-08 06:22

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