1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/ssl/OpenSSLCertUtils.h>
#include <folly/FileUtil.h>
#include <folly/ScopeGuard.h>
#include <folly/String.h>
#include <folly/ssl/OpenSSLPtrTypes.h>
namespace folly {
namespace ssl {
namespace {
std::string getOpenSSLErrorString(unsigned long err) {
std::array<char, 256> errBuff;
ERR_error_string_n(err, errBuff.data(), errBuff.size());
return std::string(errBuff.data());
}
} // namespace
Optional<std::string> OpenSSLCertUtils::getCommonName(X509& x509) {
auto subject = X509_get_subject_name(&x509);
if (!subject) {
return none;
}
auto cnLoc = X509_NAME_get_index_by_NID(subject, NID_commonName, -1);
if (cnLoc < 0) {
return none;
}
auto cnEntry = X509_NAME_get_entry(subject, cnLoc);
if (!cnEntry) {
return none;
}
auto cnAsn = X509_NAME_ENTRY_get_data(cnEntry);
if (!cnAsn) {
return none;
}
auto cnData = reinterpret_cast<const char*>(ASN1_STRING_get0_data(cnAsn));
auto cnLen = ASN1_STRING_length(cnAsn);
if (!cnData || cnLen <= 0) {
return none;
}
return Optional<std::string>(std::string(cnData, cnLen));
}
std::vector<std::string> OpenSSLCertUtils::getSubjectAltNames(X509& x509) {
auto names = reinterpret_cast<STACK_OF(GENERAL_NAME)*>(
X509_get_ext_d2i(&x509, NID_subject_alt_name, nullptr, nullptr));
if (!names) {
return {};
}
SCOPE_EXIT {
sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
};
std::vector<std::string> ret;
auto count = sk_GENERAL_NAME_num(names);
for (int i = 0; i < count; i++) {
auto genName = sk_GENERAL_NAME_value(names, i);
if (!genName || genName->type != GEN_DNS) {
continue;
}
auto nameData = reinterpret_cast<const char*>(
ASN1_STRING_get0_data(genName->d.dNSName));
auto nameLen = ASN1_STRING_length(genName->d.dNSName);
if (!nameData || nameLen <= 0) {
continue;
}
ret.emplace_back(nameData, nameLen);
}
return ret;
}
Optional<std::string> OpenSSLCertUtils::getSubject(X509& x509) {
auto subject = X509_get_subject_name(&x509);
if (!subject) {
return none;
}
auto bio = BioUniquePtr(BIO_new(BIO_s_mem()));
if (bio == nullptr) {
throw std::runtime_error("Cannot allocate bio");
}
if (X509_NAME_print_ex(bio.get(), subject, 0, XN_FLAG_ONELINE) <= 0) {
return none;
}
char* bioData = nullptr;
size_t bioLen = BIO_get_mem_data(bio.get(), &bioData);
return std::string(bioData, bioLen);
}
Optional<std::string> OpenSSLCertUtils::getIssuer(X509& x509) {
auto issuer = X509_get_issuer_name(&x509);
if (!issuer) {
return none;
}
auto bio = BioUniquePtr(BIO_new(BIO_s_mem()));
if (bio == nullptr) {
throw std::runtime_error("Cannot allocate bio");
}
if (X509_NAME_print_ex(bio.get(), issuer, 0, XN_FLAG_ONELINE) <= 0) {
return none;
}
char* bioData = nullptr;
size_t bioLen = BIO_get_mem_data(bio.get(), &bioData);
return std::string(bioData, bioLen);
}
folly::Optional<std::string> OpenSSLCertUtils::toString(X509& x509) {
auto in = BioUniquePtr(BIO_new(BIO_s_mem()));
if (in == nullptr) {
throw std::runtime_error("Cannot allocate bio");
}
int flags = 0;
flags |= X509_FLAG_NO_HEADER | /* A few bytes of cert and data */
X509_FLAG_NO_PUBKEY | /* Public key */
X509_FLAG_NO_AUX | /* Auxiliary info? */
X509_FLAG_NO_SIGDUMP | /* Prints the signature */
X509_FLAG_NO_SIGNAME; /* Signature algorithms */
#ifdef X509_FLAG_NO_IDS
flags |= X509_FLAG_NO_IDS; /* Issuer/subject IDs */
#endif
if (X509_print_ex(in.get(), &x509, XN_FLAG_ONELINE, flags) > 0) {
char* bioData = nullptr;
size_t bioLen = BIO_get_mem_data(in.get(), &bioData);
return std::string(bioData, bioLen);
} else {
return none;
}
}
std::string OpenSSLCertUtils::getNotAfterTime(X509& x509) {
return getDateTimeStr(X509_get0_notAfter(&x509));
}
std::string OpenSSLCertUtils::getNotBeforeTime(X509& x509) {
return getDateTimeStr(X509_get0_notBefore(&x509));
}
std::string OpenSSLCertUtils::getDateTimeStr(const ASN1_TIME* time) {
if (!time) {
return "";
}
auto bio = BioUniquePtr(BIO_new(BIO_s_mem()));
if (bio == nullptr) {
throw std::runtime_error("Cannot allocate bio");
}
if (ASN1_TIME_print(bio.get(), time) <= 0) {
throw std::runtime_error("Cannot print ASN1_TIME");
}
char* bioData = nullptr;
size_t bioLen = BIO_get_mem_data(bio.get(), &bioData);
return std::string(bioData, bioLen);
}
X509UniquePtr OpenSSLCertUtils::derDecode(ByteRange range) {
auto begin = range.data();
X509UniquePtr cert(d2i_X509(nullptr, &begin, range.size()));
if (!cert) {
throw std::runtime_error("could not read cert");
}
return cert;
}
std::unique_ptr<IOBuf> OpenSSLCertUtils::derEncode(X509& x509) {
auto len = i2d_X509(&x509, nullptr);
if (len < 0) {
throw std::runtime_error("Error computing length");
}
auto buf = IOBuf::create(len);
auto dataPtr = buf->writableData();
len = i2d_X509(&x509, &dataPtr);
if (len < 0) {
throw std::runtime_error("Error converting cert to DER");
}
buf->append(len);
return buf;
}
std::vector<X509UniquePtr> OpenSSLCertUtils::readCertsFromBuffer(
ByteRange range) {
BioUniquePtr b(
BIO_new_mem_buf(const_cast<unsigned char*>(range.data()), range.size()));
if (!b) {
throw std::runtime_error("failed to create BIO");
}
std::vector<X509UniquePtr> certs;
ERR_clear_error();
while (true) {
X509UniquePtr x509(PEM_read_bio_X509(b.get(), nullptr, nullptr, nullptr));
if (x509) {
certs.push_back(std::move(x509));
continue;
}
auto err = ERR_get_error();
ERR_clear_error();
if (BIO_eof(b.get()) && ERR_GET_LIB(err) == ERR_LIB_PEM &&
ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
// Reach end of buffer.
break;
}
throw std::runtime_error(folly::to<std::string>(
"Unable to parse cert ",
certs.size(),
": ",
getOpenSSLErrorString(err)));
}
return certs;
}
std::array<uint8_t, SHA_DIGEST_LENGTH> OpenSSLCertUtils::getDigestSha1(
X509& x509) {
unsigned int len;
std::array<uint8_t, SHA_DIGEST_LENGTH> md;
int rc = X509_digest(&x509, EVP_sha1(), md.data(), &len);
if (rc <= 0) {
throw std::runtime_error("Could not calculate SHA1 digest for cert");
}
return md;
}
std::array<uint8_t, SHA256_DIGEST_LENGTH> OpenSSLCertUtils::getDigestSha256(
X509& x509) {
unsigned int len;
std::array<uint8_t, SHA256_DIGEST_LENGTH> md;
int rc = X509_digest(&x509, EVP_sha256(), md.data(), &len);
if (rc <= 0) {
throw std::runtime_error("Could not calculate SHA256 digest for cert");
}
return md;
}
X509StoreUniquePtr OpenSSLCertUtils::readStoreFromFile(std::string caFile) {
std::string certData;
if (!folly::readFile(caFile.c_str(), certData)) {
throw std::runtime_error(
folly::to<std::string>("Could not read store file: ", caFile));
}
return readStoreFromBuffer(folly::StringPiece(certData));
}
X509StoreUniquePtr OpenSSLCertUtils::readStoreFromBuffer(ByteRange certRange) {
auto certs = readCertsFromBuffer(certRange);
ERR_clear_error();
folly::ssl::X509StoreUniquePtr store(X509_STORE_new());
for (auto& caCert : certs) {
if (X509_STORE_add_cert(store.get(), caCert.get()) != 1) {
auto err = ERR_get_error();
if (ERR_GET_LIB(err) != ERR_LIB_X509 ||
ERR_GET_REASON(err) != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
throw std::runtime_error(folly::to<std::string>(
"Could not insert CA certificate into store: ",
getOpenSSLErrorString(err)));
}
}
}
return store;
}
} // namespace ssl
} // namespace folly