Coverage Report

Created: 2026-10-27 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/pem/internal.h
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#ifndef OPENSSL_HEADER_CRYPTO_PEM_INTERNAL_H
16
#define OPENSSL_HEADER_CRYPTO_PEM_INTERNAL_H
17
18
#include <openssl/base.h>
19
#include <openssl/pem.h>
20
21
#include "../mem_internal.h"
22
23
24
BSSL_NAMESPACE_BEGIN
25
26
// These macros make the PEM_read/PEM_write functions easier to maintain and
27
// write. Now they are all implemented with either:
28
// IMPLEMENT_PEM_rw(...) or IMPLEMENT_PEM_rw_cb(...)
29
30
#define IMPLEMENT_PEM_read_fp(name, type, str, asn1)                         \
31
  static void *pem_read_##name##_d2i(void **x, const unsigned char **inp,    \
32
0
                                     long len) {                             \
33
0
    return d2i_##asn1((type **)x, inp, len);                                 \
34
0
  }                                                                          \
Unexecuted instantiation: ssl_x509.cc:pem_read_SSL_SESSION_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_X509_REQ_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_X509_CRL_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_PKCS7_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_RSAPublicKey_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_RSA_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_DSA_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_DSAparams_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_EC_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_DHparams_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_xaux.cc:pem_read_X509_AUX_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_pk8.cc:pem_read_PKCS8_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_pk8.cc:pem_read_PKCS8_PRIV_KEY_INFO_d2i(void**, unsigned char const**, long)
35
  OPENSSL_EXPORT type *PEM_read_##name(FILE *fp, type **x,                   \
36
0
                                       pem_password_cb *cb, void *u) {       \
37
0
    return (type *)PEM_ASN1_read(pem_read_##name##_d2i, str, fp, (void **)x, \
38
0
                                 cb, u);                                     \
39
0
  }
Unexecuted instantiation: PEM_read_SSL_SESSION
Unexecuted instantiation: PEM_read_X509_REQ
Unexecuted instantiation: PEM_read_X509_CRL
Unexecuted instantiation: PEM_read_PKCS7
Unexecuted instantiation: PEM_read_RSAPublicKey
Unexecuted instantiation: PEM_read_RSA_PUBKEY
Unexecuted instantiation: PEM_read_DSA_PUBKEY
Unexecuted instantiation: PEM_read_DSAparams
Unexecuted instantiation: PEM_read_EC_PUBKEY
Unexecuted instantiation: PEM_read_DHparams
Unexecuted instantiation: PEM_read_PUBKEY
Unexecuted instantiation: PEM_read_X509_AUX
Unexecuted instantiation: PEM_read_PKCS8
Unexecuted instantiation: PEM_read_PKCS8_PRIV_KEY_INFO
40
41
#define IMPLEMENT_PEM_write_fp(name, type, str, asn1)                        \
42
0
  static int pem_write_##name##_i2d(const void *x, unsigned char **outp) {   \
43
0
    return i2d_##asn1((const type *)x, outp);                                \
44
0
  }                                                                          \
Unexecuted instantiation: ssl_x509.cc:pem_write_SSL_SESSION_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_X509_REQ_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_X509_REQ_NEW_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_X509_CRL_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_PKCS7_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_RSAPublicKey_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_RSA_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_DSA_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_DSAparams_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_EC_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_DHparams_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_xaux.cc:pem_write_X509_AUX_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_pk8.cc:pem_write_PKCS8_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_pk8.cc:pem_write_PKCS8_PRIV_KEY_INFO_i2d(void const*, unsigned char**)
45
0
  OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x) {             \
46
0
    return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, NULL, NULL, 0, \
47
0
                          NULL, NULL);                                       \
48
0
  }
49
50
#define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)                       \
51
0
  static int pem_write_##name##_i2d(const void *x, unsigned char **outp) {     \
52
0
    return i2d_##asn1((const type *)x, outp);                                  \
53
0
  }                                                                            \
Unexecuted instantiation: pem_all.cc:pem_write_RSAPrivateKey_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_DSAPrivateKey_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_ECPrivateKey_i2d(void const*, unsigned char**)
54
  OPENSSL_EXPORT int PEM_write_##name(                                         \
55
      FILE *fp, const type *x, const EVP_CIPHER *enc,                          \
56
0
      const unsigned char *pass, int pass_len, pem_password_cb *cb, void *u) { \
57
0
    return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, pass,       \
58
0
                          pass_len, cb, u);                                    \
59
0
  }
Unexecuted instantiation: PEM_write_RSAPrivateKey
Unexecuted instantiation: PEM_write_DSAPrivateKey
Unexecuted instantiation: PEM_write_ECPrivateKey
60
61
#define IMPLEMENT_PEM_read_bio(name, type, str, asn1)                         \
62
  static void *pem_read_bio_##name##_d2i(void **x, const unsigned char **inp, \
63
0
                                         long len) {                          \
64
0
    return d2i_##asn1((type **)x, inp, len);                                  \
65
0
  }                                                                           \
Unexecuted instantiation: ssl_x509.cc:pem_read_bio_SSL_SESSION_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_X509_REQ_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_X509_CRL_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_PKCS7_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_RSAPublicKey_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_RSA_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_DSA_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_DSAparams_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_EC_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_DHparams_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_all.cc:pem_read_bio_PUBKEY_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_xaux.cc:pem_read_bio_X509_AUX_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_pk8.cc:pem_read_bio_PKCS8_d2i(void**, unsigned char const**, long)
Unexecuted instantiation: pem_pk8.cc:pem_read_bio_PKCS8_PRIV_KEY_INFO_d2i(void**, unsigned char const**, long)
66
  OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x,                 \
67
0
                                           pem_password_cb *cb, void *u) {    \
68
0
    return (type *)PEM_ASN1_read_bio(pem_read_bio_##name##_d2i, str, bp,      \
69
0
                                     (void **)x, cb, u);                      \
70
0
  }
Unexecuted instantiation: PEM_read_bio_SSL_SESSION
Unexecuted instantiation: PEM_read_bio_X509_REQ
Unexecuted instantiation: PEM_read_bio_X509_CRL
Unexecuted instantiation: PEM_read_bio_PKCS7
Unexecuted instantiation: PEM_read_bio_RSAPublicKey
Unexecuted instantiation: PEM_read_bio_RSA_PUBKEY
Unexecuted instantiation: PEM_read_bio_DSA_PUBKEY
Unexecuted instantiation: PEM_read_bio_DSAparams
Unexecuted instantiation: PEM_read_bio_EC_PUBKEY
Unexecuted instantiation: PEM_read_bio_DHparams
Unexecuted instantiation: PEM_read_bio_PUBKEY
Unexecuted instantiation: PEM_read_bio_X509_AUX
Unexecuted instantiation: PEM_read_bio_PKCS8
Unexecuted instantiation: PEM_read_bio_PKCS8_PRIV_KEY_INFO
71
72
#define IMPLEMENT_PEM_write_bio(name, type, str, asn1)                         \
73
0
  static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
74
0
    return i2d_##asn1((const type *)x, outp);                                  \
75
0
  }                                                                            \
Unexecuted instantiation: ssl_x509.cc:pem_write_bio_SSL_SESSION_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_X509_REQ_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_X509_REQ_NEW_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_X509_CRL_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_PKCS7_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_RSAPublicKey_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_RSA_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_DSA_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_DSAparams_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_EC_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_DHparams_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_PUBKEY_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_xaux.cc:pem_write_bio_X509_AUX_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_pk8.cc:pem_write_bio_PKCS8_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_pk8.cc:pem_write_bio_PKCS8_PRIV_KEY_INFO_i2d(void const*, unsigned char**)
76
0
  OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x) {            \
77
0
    return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, NULL,    \
78
0
                              NULL, 0, NULL, NULL);                            \
79
0
  }
80
81
#define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1)                      \
82
0
  static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
83
0
    return i2d_##asn1((const type *)x, outp);                                  \
84
0
  }                                                                            \
Unexecuted instantiation: pem_all.cc:pem_write_bio_RSAPrivateKey_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_DSAPrivateKey_i2d(void const*, unsigned char**)
Unexecuted instantiation: pem_all.cc:pem_write_bio_ECPrivateKey_i2d(void const*, unsigned char**)
85
  OPENSSL_EXPORT int PEM_write_bio_##name(                                     \
86
      BIO *bp, const type *x, const EVP_CIPHER *enc,                           \
87
0
      const unsigned char *pass, int pass_len, pem_password_cb *cb, void *u) { \
88
0
    return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, enc,     \
89
0
                              pass, pass_len, cb, u);                          \
90
0
  }
Unexecuted instantiation: PEM_write_bio_RSAPrivateKey
Unexecuted instantiation: PEM_write_bio_DSAPrivateKey
Unexecuted instantiation: PEM_write_bio_ECPrivateKey
91
92
#define IMPLEMENT_PEM_write(name, type, str, asn1) \
93
  IMPLEMENT_PEM_write_bio(name, type, str, asn1)   \
94
  IMPLEMENT_PEM_write_fp(name, type, str, asn1)
95
96
#define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \
97
  IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1)   \
98
  IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)
99
100
#define IMPLEMENT_PEM_read(name, type, str, asn1) \
101
  IMPLEMENT_PEM_read_bio(name, type, str, asn1)   \
102
  IMPLEMENT_PEM_read_fp(name, type, str, asn1)
103
104
#define IMPLEMENT_PEM_rw(name, type, str, asn1) \
105
  IMPLEMENT_PEM_read(name, type, str, asn1)     \
106
  IMPLEMENT_PEM_write(name, type, str, asn1)
107
108
#define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \
109
  IMPLEMENT_PEM_read(name, type, str, asn1)        \
110
  IMPLEMENT_PEM_write_cb(name, type, str, asn1)
111
112
// PEM_get_EVP_CIPHER_INFO decodes `header` as a PEM header block and writes the
113
// specified cipher and IV to `cipher`. It returns one on success and zero on
114
// error. `header` must be a NUL-terminated string. If `header` does not
115
// specify encryption, this function will return success and set
116
// `cipher->cipher` to NULL.
117
int PEM_get_EVP_CIPHER_INFO(const char *header, EVP_CIPHER_INFO *cipher);
118
119
// PEM_do_header decrypts `*len` bytes from `data` in-place according to the
120
// information in `cipher`. On success, it returns one and sets `*len` to the
121
// length of the plaintext. Otherwise, it returns zero. If `cipher` specifies
122
// encryption, the key is derived from a password returned from `callback`.
123
int PEM_do_header(const EVP_CIPHER_INFO *cipher, uint8_t *data, size_t *len,
124
                  pem_password_cb *callback, void *u);
125
126
// PEM_read_bio_inner differs from `PEM_read_bio` on the out pointer `len`
127
// so that it guarantee non-negativeness on this output and it takes in
128
// owned types.
129
int PEM_read_bio_inner(BIO *bp, bssl::UniquePtr<char> *name,
130
                       bssl::UniquePtr<char> *header,
131
                       bssl::Array<uint8_t> *data);
132
133
BSSL_NAMESPACE_END
134
135
#endif  // OPENSSL_HEADER_CRYPTO_PEM_INTERNAL_H