Coverage Report

Created: 2025-06-13 06:56

/src/openssl/crypto/pem/pem_local.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2021 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 <openssl/core_dispatch.h>
11
#include <openssl/pem.h>
12
#include <openssl/encoder.h>
13
14
/*
15
 * Selectors, named according to the ASN.1 names used throughout libcrypto.
16
 *
17
 * Note that these are not absolutely mandatory, they are rather a wishlist
18
 * of sorts.  The provider implementations are free to make choices that
19
 * make sense for them, based on these selectors.
20
 * For example, the EC backend is likely to really just output the private
21
 * key to a PKCS#8 structure, even thought PEM_SELECTION_PrivateKey specifies
22
 * the public key as well.  This is fine, as long as the corresponding
23
 * decoding operation can return an object that contains what libcrypto
24
 * expects.
25
 */
26
0
# define PEM_SELECTION_PUBKEY           EVP_PKEY_PUBLIC_KEY
27
0
# define PEM_SELECTION_PrivateKey       EVP_PKEY_KEYPAIR
28
0
# define PEM_SELECTION_Parameters       EVP_PKEY_KEY_PARAMETERS
29
30
/*
31
 * Properties, named according to the ASN.1 names used throughout libcrypto.
32
 */
33
0
# define PEM_STRUCTURE_PUBKEY "SubjectPublicKeyInfo"
34
0
# define PEM_STRUCTURE_PrivateKey "PrivateKeyInfo"
35
0
# define PEM_STRUCTURE_Parameters "type-specific"
36
37
# define PEM_STRUCTURE_RSAPrivateKey "type-specific"
38
# define PEM_STRUCTURE_RSAPublicKey "type-specific"
39
40
/* Alternative IMPLEMENT macros for provided encoders */
41
42
# define IMPLEMENT_PEM_provided_write_body_vars(type, asn1, pq)         \
43
0
    int ret = 0;                                                        \
44
0
    OSSL_ENCODER_CTX *ctx =                                             \
45
0
        OSSL_ENCODER_CTX_new_for_##type(x, PEM_SELECTION_##asn1,        \
46
0
                                       "PEM", PEM_STRUCTURE_##asn1,     \
47
0
                                       (pq));                           \
48
0
                                                                        \
49
0
    if (OSSL_ENCODER_CTX_get_num_encoders(ctx) == 0) {                  \
50
0
        OSSL_ENCODER_CTX_free(ctx);                                     \
51
0
        goto legacy;                                                    \
52
0
    }
53
# define IMPLEMENT_PEM_provided_write_body_pass()                       \
54
0
    ret = 1;                                                            \
55
0
    if (kstr == NULL && cb == NULL) {                                   \
56
0
        if (u != NULL) {                                                \
57
0
            kstr = u;                                                   \
58
0
            klen = strlen(u);                                           \
59
0
        } else {                                                        \
60
0
            cb = PEM_def_callback;                                      \
61
0
        }                                                               \
62
0
    }                                                                   \
63
0
    if (enc != NULL) {                                                  \
64
0
        ret = 0;                                                        \
65
0
        if (OSSL_ENCODER_CTX_set_cipher(ctx, EVP_CIPHER_get0_name(enc), \
66
0
                                        NULL)) {                        \
67
0
            ret = 1;                                                    \
68
0
            if (kstr != NULL                                            \
69
0
                && !OSSL_ENCODER_CTX_set_passphrase(ctx, kstr, klen))   \
70
0
                ret = 0;                                                \
71
0
            else if (cb != NULL                                         \
72
0
                     && !OSSL_ENCODER_CTX_set_pem_password_cb(ctx,      \
73
0
                                                              cb, u))   \
74
0
                ret = 0;                                                \
75
0
        }                                                               \
76
0
    }                                                                   \
77
0
    if (!ret) {                                                         \
78
0
        OSSL_ENCODER_CTX_free(ctx);                                     \
79
0
        return 0;                                                       \
80
0
    }
81
# define IMPLEMENT_PEM_provided_write_body_main(type, outtype)          \
82
0
    ret = OSSL_ENCODER_to_##outtype(ctx, out);                          \
83
0
    OSSL_ENCODER_CTX_free(ctx);                                         \
84
0
    return ret
85
# define IMPLEMENT_PEM_provided_write_body_fallback(str, asn1,          \
86
                                                    writename)          \
87
0
    legacy:                                                             \
88
0
    return PEM_ASN1_##writename((i2d_of_void *)i2d_##asn1, str, out,    \
89
0
                                x, NULL, NULL, 0, NULL, NULL)
90
# define IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1,       \
91
                                                       writename)       \
92
    legacy:                                                             \
93
    return PEM_ASN1_##writename##((i2d_of_void *)i2d_##asn1, str, out,  \
94
                                  x, enc, kstr, klen, cb, u)
95
96
# define IMPLEMENT_PEM_provided_write_to(name, TYPE, type, str, asn1,   \
97
                                         OUTTYPE, outtype, writename)   \
98
    PEM_write_fnsig(name, TYPE, OUTTYPE, writename)                     \
99
0
    {                                                                   \
100
0
        IMPLEMENT_PEM_provided_write_body_vars(type, asn1, NULL);       \
101
0
        IMPLEMENT_PEM_provided_write_body_main(type, outtype);          \
102
0
        IMPLEMENT_PEM_provided_write_body_fallback(str, asn1,           \
103
0
                                                   writename);          \
104
0
    }                                                                   \
Unexecuted instantiation: PEM_write_bio_PUBKEY
Unexecuted instantiation: PEM_write_PUBKEY
105
    PEM_write_ex_fnsig(name, TYPE, OUTTYPE, writename)                  \
106
0
    {                                                                   \
107
0
        IMPLEMENT_PEM_provided_write_body_vars(type, asn1, propq);      \
108
0
        IMPLEMENT_PEM_provided_write_body_main(type, outtype);          \
109
0
        IMPLEMENT_PEM_provided_write_body_fallback(str, asn1,           \
110
0
                                                   writename);          \
111
0
    }
Unexecuted instantiation: PEM_write_bio_PUBKEY_ex
Unexecuted instantiation: PEM_write_PUBKEY_ex
112
113
114
# define IMPLEMENT_PEM_provided_write_cb_to(name, TYPE, type, str, asn1, \
115
                                            OUTTYPE, outtype, writename) \
116
    PEM_write_cb_fnsig(name, TYPE, OUTTYPE, writename)                  \
117
    {                                                                   \
118
        IMPLEMENT_PEM_provided_write_body_vars(type, asn1, NULL);       \
119
        IMPLEMENT_PEM_provided_write_body_pass();                       \
120
        IMPLEMENT_PEM_provided_write_body_main(type, outtype);          \
121
        IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1,        \
122
                                                      writename);       \
123
    }                                                                   \
124
    PEM_write_ex_cb_fnsig(name, TYPE, OUTTYPE, writename)               \
125
    {                                                                   \
126
        IMPLEMENT_PEM_provided_write_body_vars(type, asn1, propq);      \
127
        IMPLEMENT_PEM_provided_write_body_pass();                       \
128
        IMPLEMENT_PEM_provided_write_body_main(type, outtype);          \
129
        IMPLEMENT_PEM_provided_write_body_fallback(str, asn1,           \
130
                                                   writename);          \
131
    }
132
133
# ifdef OPENSSL_NO_STDIO
134
135
#  define IMPLEMENT_PEM_provided_write_fp(name, TYPE, type, str, asn1)
136
#  define IMPLEMENT_PEM_provided_write_cb_fp(name, TYPE, type, str, asn1)
137
138
# else
139
140
#  define IMPLEMENT_PEM_provided_write_fp(name, TYPE, type, str, asn1)    \
141
    IMPLEMENT_PEM_provided_write_to(name, TYPE, type, str, asn1, FILE, fp, write)
142
#  define IMPLEMENT_PEM_provided_write_cb_fp(name, TYPE, type, str, asn1) \
143
    IMPLEMENT_PEM_provided_write_cb_to(name, TYPE, type, str, asn1, FILE, fp, write)
144
145
# endif
146
147
# define IMPLEMENT_PEM_provided_write_bio(name, TYPE, type, str, asn1)    \
148
    IMPLEMENT_PEM_provided_write_to(name, TYPE, type, str, asn1, BIO, bio, write_bio)
149
# define IMPLEMENT_PEM_provided_write_cb_bio(name, TYPE, type, str, asn1) \
150
    IMPLEMENT_PEM_provided_write_cb_to(name, TYPE, type, str, asn1, BIO, bio, write_bio)
151
152
# define IMPLEMENT_PEM_provided_write(name, TYPE, type, str, asn1)        \
153
    IMPLEMENT_PEM_provided_write_bio(name, TYPE, type, str, asn1)         \
154
    IMPLEMENT_PEM_provided_write_fp(name, TYPE, type, str, asn1)
155
156
# define IMPLEMENT_PEM_provided_write_cb(name, TYPE, type, str, asn1)     \
157
    IMPLEMENT_PEM_provided_write_cb_bio(name, TYPE, type, str, asn1)      \
158
    IMPLEMENT_PEM_provided_write_cb_fp(name, TYPE, type, str, asn1)
159
160
# define IMPLEMENT_PEM_provided_rw(name, TYPE, type, str, asn1)           \
161
    IMPLEMENT_PEM_read(name, TYPE, str, asn1)                             \
162
    IMPLEMENT_PEM_provided_write(name, TYPE, type, str, asn1)
163
164
# define IMPLEMENT_PEM_provided_rw_cb(name, TYPE, type, str, asn1)        \
165
    IMPLEMENT_PEM_read(name, TYPE, str, asn1)                             \
166
    IMPLEMENT_PEM_provided_write_cb(name, TYPE, type, str, asn1)
167