Coverage Report

Created: 2026-02-16 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/pem/pem_all.cc
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
#include <stdio.h>
16
17
#include <openssl/bio.h>
18
#include <openssl/dh.h>
19
#include <openssl/dsa.h>
20
#include <openssl/evp.h>
21
#include <openssl/pem.h>
22
#include <openssl/pkcs7.h>
23
#include <openssl/rsa.h>
24
#include <openssl/x509.h>
25
26
static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa);
27
static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa);
28
static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey);
29
30
0
IMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ)
Unexecuted instantiation: PEM_write_bio_X509_REQ
Unexecuted instantiation: PEM_write_X509_REQ
31
0
32
0
IMPLEMENT_PEM_write(X509_REQ_NEW, X509_REQ, PEM_STRING_X509_REQ_OLD, X509_REQ)
Unexecuted instantiation: PEM_write_bio_X509_REQ_NEW
Unexecuted instantiation: PEM_write_X509_REQ_NEW
33
0
IMPLEMENT_PEM_rw(X509_CRL, X509_CRL, PEM_STRING_X509_CRL, X509_CRL)
Unexecuted instantiation: PEM_write_bio_X509_CRL
Unexecuted instantiation: PEM_write_X509_CRL
34
0
IMPLEMENT_PEM_rw(PKCS7, PKCS7, PEM_STRING_PKCS7, PKCS7)
Unexecuted instantiation: PEM_write_bio_PKCS7
Unexecuted instantiation: PEM_write_PKCS7
35
0
36
0
// We treat RSA or DSA private keys as a special case. For private keys we
37
0
// read in an EVP_PKEY structure with PEM_read_bio_PrivateKey() and extract
38
0
// the relevant private key: this means can handle "traditional" and PKCS#8
39
0
// formats transparently.
40
0
static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa) {
41
0
  if (!key) {
42
0
    return nullptr;
43
0
  }
44
0
  if (EVP_PKEY_id(key) != EVP_PKEY_RSA) {
45
    // Don't accept RSA-PSS keys in this function.
46
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY);
47
0
    return nullptr;
48
0
  }
49
0
  RSA *rtmp = EVP_PKEY_get1_RSA(key);
50
0
  if (!rtmp) {
51
0
    return nullptr;
52
0
  }
53
0
  if (rsa) {
54
0
    RSA_free(*rsa);
55
0
    *rsa = rtmp;
56
0
  }
57
0
  return rtmp;
58
0
}
59
60
RSA *PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **rsa, pem_password_cb *cb,
61
0
                                void *u) {
62
0
  bssl::UniquePtr<EVP_PKEY> pkey(PEM_read_bio_PrivateKey(bp, nullptr, cb, u));
63
0
  return pkey_get_rsa(pkey.get(), rsa);
64
0
}
65
66
0
RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **rsa, pem_password_cb *cb, void *u) {
67
0
  bssl::UniquePtr<EVP_PKEY> pkey(PEM_read_PrivateKey(fp, nullptr, cb, u));
68
0
  return pkey_get_rsa(pkey.get(), rsa);
69
0
}
70
71
IMPLEMENT_PEM_write_cb_const(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey)
72
73
74
0
IMPLEMENT_PEM_rw_const(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey)
Unexecuted instantiation: PEM_write_bio_RSAPublicKey
Unexecuted instantiation: PEM_write_RSAPublicKey
75
0
IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY)
Unexecuted instantiation: PEM_write_bio_RSA_PUBKEY
Unexecuted instantiation: PEM_write_RSA_PUBKEY
76
0
#ifndef OPENSSL_NO_DSA
77
0
static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa) {
78
0
  DSA *dtmp;
79
0
  if (!key) {
80
0
    return nullptr;
81
0
  }
82
0
  dtmp = EVP_PKEY_get1_DSA(key);
83
0
  EVP_PKEY_free(key);
84
0
  if (!dtmp) {
85
0
    return nullptr;
86
0
  }
87
0
  if (dsa) {
88
0
    DSA_free(*dsa);
89
0
    *dsa = dtmp;
90
0
  }
91
0
  return dtmp;
92
0
}
93
94
DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb,
95
0
                                void *u) {
96
0
  EVP_PKEY *pktmp;
97
0
  pktmp = PEM_read_bio_PrivateKey(bp, nullptr, cb, u);
98
0
  return pkey_get_dsa(pktmp, dsa);  // will free pktmp
99
0
}
100
101
IMPLEMENT_PEM_write_cb_const(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey)
102
103
0
IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY)
Unexecuted instantiation: PEM_write_bio_DSA_PUBKEY
Unexecuted instantiation: PEM_write_DSA_PUBKEY
104
0
DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u) {
105
0
  EVP_PKEY *pktmp;
106
0
  pktmp = PEM_read_PrivateKey(fp, nullptr, cb, u);
107
0
  return pkey_get_dsa(pktmp, dsa);  // will free pktmp
108
0
}
109
110
0
IMPLEMENT_PEM_rw_const(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
Unexecuted instantiation: PEM_write_bio_DSAparams
Unexecuted instantiation: PEM_write_DSAparams
111
0
#endif
112
0
static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey) {
113
0
  EC_KEY *dtmp;
114
0
  if (!key) {
115
0
    return nullptr;
116
0
  }
117
0
  dtmp = EVP_PKEY_get1_EC_KEY(key);
118
0
  EVP_PKEY_free(key);
119
0
  if (!dtmp) {
120
0
    return nullptr;
121
0
  }
122
0
  if (eckey) {
123
0
    EC_KEY_free(*eckey);
124
0
    *eckey = dtmp;
125
0
  }
126
0
  return dtmp;
127
0
}
128
129
EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb,
130
0
                                  void *u) {
131
0
  EVP_PKEY *pktmp;
132
0
  pktmp = PEM_read_bio_PrivateKey(bp, nullptr, cb, u);
133
0
  return pkey_get_eckey(pktmp, key);  // will free pktmp
134
0
}
135
136
IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY,
137
                       ECPrivateKey)
138
139
0
IMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY)
Unexecuted instantiation: PEM_write_bio_EC_PUBKEY
Unexecuted instantiation: PEM_write_EC_PUBKEY
140
0
EC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb,
141
0
                              void *u) {
142
0
  EVP_PKEY *pktmp;
143
0
  pktmp = PEM_read_PrivateKey(fp, nullptr, cb, u);
144
0
  return pkey_get_eckey(pktmp, eckey);  // will free pktmp
145
0
}
146
147
148
0
IMPLEMENT_PEM_rw_const(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
Unexecuted instantiation: PEM_write_bio_DHparams
Unexecuted instantiation: PEM_write_DHparams
149
0
150
IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)
Unexecuted instantiation: PEM_write_bio_PUBKEY
Unexecuted instantiation: PEM_write_PUBKEY