| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef _GNU_SOURCE |
| 17 |
#define _GNU_SOURCE |
| 18 |
#endif |
| 19 |
|
| 20 |
#include "crypto/s2n_certificate.h" |
| 21 |
|
| 22 |
#include <openssl/pem.h> |
| 23 |
#include <openssl/x509v3.h> |
| 24 |
#include <string.h> |
| 25 |
#include <strings.h> |
| 26 |
|
| 27 |
#include "api/s2n.h" |
| 28 |
#include "crypto/s2n_openssl_x509.h" |
| 29 |
#include "tls/extensions/s2n_extension_list.h" |
| 30 |
#include "tls/s2n_connection.h" |
| 31 |
#include "utils/s2n_array.h" |
| 32 |
#include "utils/s2n_mem.h" |
| 33 |
#include "utils/s2n_safety.h" |
| 34 |
|
| 35 |
int s2n_cert_set_cert_type(struct s2n_cert *cert, s2n_pkey_type pkey_type) |
| 36 |
{ |
| 37 |
POSIX_ENSURE_REF(cert); |
| 38 |
cert->pkey_type = pkey_type; |
| 39 |
POSIX_GUARD_RESULT(s2n_pkey_setup_for_type(&cert->public_key, pkey_type)); |
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
int s2n_create_cert_chain_from_stuffer(struct s2n_cert_chain *cert_chain_out, struct s2n_stuffer *chain_in_stuffer) |
| 44 |
{ |
| 45 |
DEFER_CLEANUP(struct s2n_stuffer cert_out_stuffer = { 0 }, s2n_stuffer_free); |
| 46 |
POSIX_GUARD(s2n_stuffer_growable_alloc(&cert_out_stuffer, 2048)); |
| 47 |
|
| 48 |
struct s2n_cert **insert = &cert_chain_out->head; |
| 49 |
uint32_t chain_size = 0; |
| 50 |
while (s2n_stuffer_has_pem_encapsulated_block(chain_in_stuffer)) { |
| 51 |
int result = s2n_stuffer_certificate_from_pem(chain_in_stuffer, &cert_out_stuffer); |
| 52 |
POSIX_ENSURE(result == S2N_SUCCESS, S2N_ERR_INVALID_PEM); |
| 53 |
|
| 54 |
DEFER_CLEANUP(struct s2n_blob mem = { 0 }, s2n_free); |
| 55 |
POSIX_GUARD(s2n_alloc(&mem, sizeof(struct s2n_cert))); |
| 56 |
POSIX_GUARD(s2n_blob_zero(&mem)); |
| 57 |
|
| 58 |
struct s2n_cert *new_node = (struct s2n_cert *) (void *) mem.data; |
| 59 |
POSIX_GUARD(s2n_alloc(&new_node->raw, s2n_stuffer_data_available(&cert_out_stuffer))); |
| 60 |
POSIX_GUARD(s2n_stuffer_read(&cert_out_stuffer, &new_node->raw)); |
| 61 |
ZERO_TO_DISABLE_DEFER_CLEANUP(mem); |
| 62 |
|
| 63 |
|
| 64 |
chain_size += new_node->raw.size + 3; |
| 65 |
new_node->next = NULL; |
| 66 |
*insert = new_node; |
| 67 |
insert = &new_node->next; |
| 68 |
}; |
| 69 |
|
| 70 |
POSIX_ENSURE(chain_size > 0, S2N_ERR_NO_CERTIFICATE_IN_PEM); |
| 71 |
cert_chain_out->chain_size = chain_size; |
| 72 |
|
| 73 |
return S2N_SUCCESS; |
| 74 |
} |
| 75 |
|
| 76 |
int s2n_cert_chain_and_key_set_cert_chain_from_stuffer(struct s2n_cert_chain_and_key *cert_and_key, struct s2n_stuffer *chain_in_stuffer) |
| 77 |
{ |
| 78 |
POSIX_ENSURE_REF(cert_and_key); |
| 79 |
POSIX_ENSURE_REF(cert_and_key->cert_chain); |
| 80 |
return s2n_create_cert_chain_from_stuffer(cert_and_key->cert_chain, chain_in_stuffer); |
| 81 |
} |
| 82 |
|
| 83 |
int s2n_cert_chain_and_key_set_cert_chain_bytes(struct s2n_cert_chain_and_key *cert_and_key, uint8_t *cert_chain_pem, uint32_t cert_chain_len) |
| 84 |
{ |
| 85 |
DEFER_CLEANUP(struct s2n_stuffer chain_in_stuffer = { 0 }, s2n_stuffer_free); |
| 86 |
|
| 87 |
POSIX_GUARD(s2n_stuffer_init_ro_from_string(&chain_in_stuffer, cert_chain_pem, cert_chain_len)); |
| 88 |
POSIX_GUARD(s2n_cert_chain_and_key_set_cert_chain_from_stuffer(cert_and_key, &chain_in_stuffer)); |
| 89 |
|
| 90 |
return S2N_SUCCESS; |
| 91 |
} |
| 92 |
|
| 93 |
int s2n_cert_chain_and_key_set_cert_chain(struct s2n_cert_chain_and_key *cert_and_key, const char *cert_chain_pem) |
| 94 |
{ |
| 95 |
DEFER_CLEANUP(struct s2n_stuffer chain_in_stuffer = { 0 }, s2n_stuffer_free); |
| 96 |
|
| 97 |
|
| 98 |
POSIX_GUARD(s2n_stuffer_alloc_ro_from_string(&chain_in_stuffer, cert_chain_pem)); |
| 99 |
POSIX_GUARD(s2n_cert_chain_and_key_set_cert_chain_from_stuffer(cert_and_key, &chain_in_stuffer)); |
| 100 |
|
| 101 |
return S2N_SUCCESS; |
| 102 |
} |
| 103 |
|
| 104 |
int s2n_cert_chain_and_key_set_private_key_from_stuffer(struct s2n_cert_chain_and_key *cert_and_key, |
| 105 |
struct s2n_stuffer *key_in_stuffer, struct s2n_stuffer *key_out_stuffer) |
| 106 |
{ |
| 107 |
POSIX_ENSURE_REF(cert_and_key); |
| 108 |
POSIX_ENSURE_REF(cert_and_key->private_key); |
| 109 |
|
| 110 |
struct s2n_blob key_blob = { 0 }; |
| 111 |
|
| 112 |
POSIX_GUARD(s2n_pkey_zero_init(cert_and_key->private_key)); |
| 113 |
|
| 114 |
|
| 115 |
int type_hint = 0; |
| 116 |
POSIX_GUARD(s2n_stuffer_private_key_from_pem(key_in_stuffer, key_out_stuffer, &type_hint)); |
| 117 |
key_blob.size = s2n_stuffer_data_available(key_out_stuffer); |
| 118 |
key_blob.data = s2n_stuffer_raw_read(key_out_stuffer, key_blob.size); |
| 119 |
POSIX_ENSURE_REF(key_blob.data); |
| 120 |
|
| 121 |
POSIX_GUARD_RESULT(s2n_asn1der_to_private_key(cert_and_key->private_key, &key_blob, type_hint)); |
| 122 |
return S2N_SUCCESS; |
| 123 |
} |
| 124 |
|
| 125 |
int s2n_cert_chain_and_key_set_private_key_bytes(struct s2n_cert_chain_and_key *cert_and_key, uint8_t *private_key_pem, uint32_t private_key_len) |
| 126 |
{ |
| 127 |
DEFER_CLEANUP(struct s2n_stuffer key_in_stuffer = { 0 }, s2n_stuffer_free); |
| 128 |
DEFER_CLEANUP(struct s2n_stuffer key_out_stuffer = { 0 }, s2n_stuffer_free); |
| 129 |
|
| 130 |
|
| 131 |
POSIX_GUARD(s2n_stuffer_init_ro_from_string(&key_in_stuffer, private_key_pem, private_key_len)); |
| 132 |
POSIX_GUARD(s2n_stuffer_growable_alloc(&key_out_stuffer, private_key_len)); |
| 133 |
|
| 134 |
POSIX_GUARD(s2n_cert_chain_and_key_set_private_key_from_stuffer(cert_and_key, &key_in_stuffer, &key_out_stuffer)); |
| 135 |
|
| 136 |
return S2N_SUCCESS; |
| 137 |
} |
| 138 |
|
| 139 |
int s2n_cert_chain_and_key_set_private_key(struct s2n_cert_chain_and_key *cert_and_key, const char *private_key_pem) |
| 140 |
{ |
| 141 |
POSIX_ENSURE_REF(private_key_pem); |
| 142 |
|
| 143 |
DEFER_CLEANUP(struct s2n_stuffer key_in_stuffer = { 0 }, s2n_stuffer_free); |
| 144 |
DEFER_CLEANUP(struct s2n_stuffer key_out_stuffer = { 0 }, s2n_stuffer_free); |
| 145 |
|
| 146 |
|
| 147 |
POSIX_GUARD(s2n_stuffer_alloc_ro_from_string(&key_in_stuffer, private_key_pem)); |
| 148 |
POSIX_GUARD(s2n_stuffer_growable_alloc(&key_out_stuffer, strlen(private_key_pem))); |
| 149 |
|
| 150 |
POSIX_GUARD(s2n_cert_chain_and_key_set_private_key_from_stuffer(cert_and_key, &key_in_stuffer, &key_out_stuffer)); |
| 151 |
|
| 152 |
return S2N_SUCCESS; |
| 153 |
} |
| 154 |
|
| 155 |
int s2n_cert_chain_and_key_set_ocsp_data(struct s2n_cert_chain_and_key *chain_and_key, const uint8_t *data, uint32_t length) |
| 156 |
{ |
| 157 |
POSIX_ENSURE_REF(chain_and_key); |
| 158 |
POSIX_GUARD(s2n_free(&chain_and_key->ocsp_status)); |
| 159 |
if (data && length) { |
| 160 |
POSIX_GUARD(s2n_alloc(&chain_and_key->ocsp_status, length)); |
| 161 |
POSIX_CHECKED_MEMCPY(chain_and_key->ocsp_status.data, data, length); |
| 162 |
} |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
int s2n_cert_chain_and_key_set_sct_list(struct s2n_cert_chain_and_key *chain_and_key, const uint8_t *data, uint32_t length) |
| 167 |
{ |
| 168 |
POSIX_ENSURE_REF(chain_and_key); |
| 169 |
POSIX_GUARD(s2n_free(&chain_and_key->sct_list)); |
| 170 |
if (data && length) { |
| 171 |
POSIX_GUARD(s2n_alloc(&chain_and_key->sct_list, length)); |
| 172 |
POSIX_CHECKED_MEMCPY(chain_and_key->sct_list.data, data, length); |
| 173 |
} |
| 174 |
return 0; |
| 175 |
} |
| 176 |
|
| 177 |
struct s2n_cert_chain_and_key *s2n_cert_chain_and_key_new(void) |
| 178 |
{ |
| 179 |
DEFER_CLEANUP(struct s2n_blob chain_and_key_mem = { 0 }, s2n_free); |
| 180 |
PTR_GUARD_POSIX(s2n_alloc(&chain_and_key_mem, sizeof(struct s2n_cert_chain_and_key))); |
| 181 |
PTR_GUARD_POSIX(s2n_blob_zero(&chain_and_key_mem)); |
| 182 |
|
| 183 |
DEFER_CLEANUP(struct s2n_blob cert_chain_mem = { 0 }, s2n_free); |
| 184 |
PTR_GUARD_POSIX(s2n_alloc(&cert_chain_mem, sizeof(struct s2n_cert_chain))); |
| 185 |
PTR_GUARD_POSIX(s2n_blob_zero(&cert_chain_mem)); |
| 186 |
|
| 187 |
DEFER_CLEANUP(struct s2n_blob pkey_mem = { 0 }, s2n_free); |
| 188 |
PTR_GUARD_POSIX(s2n_alloc(&pkey_mem, sizeof(s2n_cert_private_key))); |
| 189 |
PTR_GUARD_POSIX(s2n_blob_zero(&pkey_mem)); |
| 190 |
|
| 191 |
DEFER_CLEANUP(struct s2n_array *cn_names = NULL, s2n_array_free_p); |
| 192 |
cn_names = s2n_array_new(sizeof(struct s2n_blob)); |
| 193 |
PTR_ENSURE_REF(cn_names); |
| 194 |
|
| 195 |
DEFER_CLEANUP(struct s2n_array *san_names = NULL, s2n_array_free_p); |
| 196 |
san_names = s2n_array_new(sizeof(struct s2n_blob)); |
| 197 |
PTR_ENSURE_REF(san_names); |
| 198 |
|
| 199 |
struct s2n_cert_chain_and_key *chain_and_key = (struct s2n_cert_chain_and_key *) (void *) chain_and_key_mem.data; |
| 200 |
chain_and_key->cert_chain = (struct s2n_cert_chain *) (void *) cert_chain_mem.data; |
| 201 |
chain_and_key->private_key = (s2n_cert_private_key *) (void *) pkey_mem.data; |
| 202 |
chain_and_key->cn_names = cn_names; |
| 203 |
chain_and_key->san_names = san_names; |
| 204 |
|
| 205 |
ZERO_TO_DISABLE_DEFER_CLEANUP(chain_and_key_mem); |
| 206 |
ZERO_TO_DISABLE_DEFER_CLEANUP(cert_chain_mem); |
| 207 |
ZERO_TO_DISABLE_DEFER_CLEANUP(pkey_mem); |
| 208 |
ZERO_TO_DISABLE_DEFER_CLEANUP(cn_names); |
| 209 |
ZERO_TO_DISABLE_DEFER_CLEANUP(san_names); |
| 210 |
return chain_and_key; |
| 211 |
} |
| 212 |
|
| 213 |
DEFINE_POINTER_CLEANUP_FUNC(GENERAL_NAMES *, GENERAL_NAMES_free); |
| 214 |
|
| 215 |
int s2n_cert_chain_and_key_load_sans(struct s2n_cert_chain_and_key *chain_and_key, X509 *x509_cert) |
| 216 |
{ |
| 217 |
POSIX_ENSURE_REF(chain_and_key->san_names); |
| 218 |
POSIX_ENSURE_REF(x509_cert); |
| 219 |
|
| 220 |
DEFER_CLEANUP(GENERAL_NAMES *san_names = X509_get_ext_d2i(x509_cert, NID_subject_alt_name, NULL, NULL), GENERAL_NAMES_free_pointer); |
| 221 |
if (san_names == NULL) { |
| 222 |
|
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
const int num_san_names = sk_GENERAL_NAME_num(san_names); |
| 227 |
for (int i = 0; i < num_san_names; i++) { |
| 228 |
GENERAL_NAME *san_name = sk_GENERAL_NAME_value(san_names, i); |
| 229 |
if (!san_name) { |
| 230 |
continue; |
| 231 |
} |
| 232 |
|
| 233 |
if (san_name->type == GEN_DNS) { |
| 234 |
|
| 235 |
unsigned char *san_str = san_name->d.dNSName->data; |
| 236 |
const size_t san_str_len = san_name->d.dNSName->length; |
| 237 |
struct s2n_blob *san_blob = NULL; |
| 238 |
POSIX_GUARD_RESULT(s2n_array_pushback(chain_and_key->san_names, (void **) &san_blob)); |
| 239 |
if (!san_blob) { |
| 240 |
POSIX_BAIL(S2N_ERR_NULL_SANS); |
| 241 |
} |
| 242 |
|
| 243 |
if (s2n_alloc(san_blob, san_str_len)) { |
| 244 |
S2N_ERROR_PRESERVE_ERRNO(); |
| 245 |
} |
| 246 |
|
| 247 |
POSIX_CHECKED_MEMCPY(san_blob->data, san_str, san_str_len); |
| 248 |
san_blob->size = san_str_len; |
| 249 |
|
| 250 |
POSIX_GUARD(s2n_blob_char_to_lower(san_blob)); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
DEFINE_POINTER_CLEANUP_FUNC(unsigned char *, OPENSSL_free); |
| 267 |
|
| 268 |
int s2n_cert_chain_and_key_load_cns(struct s2n_cert_chain_and_key *chain_and_key, X509 *x509_cert) |
| 269 |
{ |
| 270 |
POSIX_ENSURE_REF(chain_and_key->cn_names); |
| 271 |
POSIX_ENSURE_REF(x509_cert); |
| 272 |
|
| 273 |
X509_NAME *subject = X509_get_subject_name(x509_cert); |
| 274 |
if (!subject) { |
| 275 |
return 0; |
| 276 |
} |
| 277 |
|
| 278 |
int lastpos = -1; |
| 279 |
while ((lastpos = X509_NAME_get_index_by_NID(subject, NID_commonName, lastpos)) >= 0) { |
| 280 |
X509_NAME_ENTRY *name_entry = X509_NAME_get_entry(subject, lastpos); |
| 281 |
if (!name_entry) { |
| 282 |
continue; |
| 283 |
} |
| 284 |
|
| 285 |
ASN1_STRING *asn1_str = X509_NAME_ENTRY_get_data(name_entry); |
| 286 |
if (!asn1_str) { |
| 287 |
continue; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
DEFER_CLEANUP(unsigned char *utf8_str, OPENSSL_free_pointer); |
| 300 |
const int utf8_out_len = ASN1_STRING_to_UTF8(&utf8_str, asn1_str); |
| 301 |
if (utf8_out_len < 0) { |
| 302 |
|
| 303 |
ZERO_TO_DISABLE_DEFER_CLEANUP(utf8_str); |
| 304 |
continue; |
| 305 |
} else if (utf8_out_len == 0) { |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
} else { |
| 311 |
struct s2n_blob *cn_name = NULL; |
| 312 |
POSIX_GUARD_RESULT(s2n_array_pushback(chain_and_key->cn_names, (void **) &cn_name)); |
| 313 |
if (cn_name == NULL) { |
| 314 |
POSIX_BAIL(S2N_ERR_NULL_CN_NAME); |
| 315 |
} |
| 316 |
|
| 317 |
if (s2n_alloc(cn_name, utf8_out_len) < 0) { |
| 318 |
S2N_ERROR_PRESERVE_ERRNO(); |
| 319 |
} |
| 320 |
POSIX_CHECKED_MEMCPY(cn_name->data, utf8_str, utf8_out_len); |
| 321 |
cn_name->size = utf8_out_len; |
| 322 |
|
| 323 |
POSIX_GUARD(s2n_blob_char_to_lower(cn_name)); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
return 0; |
| 328 |
} |
| 329 |
|
| 330 |
static int s2n_cert_chain_and_key_set_names(struct s2n_cert_chain_and_key *chain_and_key, X509 *cert) |
| 331 |
{ |
| 332 |
POSIX_GUARD(s2n_cert_chain_and_key_load_sans(chain_and_key, cert)); |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
POSIX_GUARD(s2n_cert_chain_and_key_load_cns(chain_and_key, cert)); |
| 338 |
return 0; |
| 339 |
} |
| 340 |
|
| 341 |
int s2n_cert_chain_and_key_load(struct s2n_cert_chain_and_key *chain_and_key) |
| 342 |
{ |
| 343 |
POSIX_ENSURE_REF(chain_and_key); |
| 344 |
POSIX_ENSURE_REF(chain_and_key->cert_chain); |
| 345 |
POSIX_ENSURE_REF(chain_and_key->cert_chain->head); |
| 346 |
POSIX_ENSURE_REF(chain_and_key->private_key); |
| 347 |
struct s2n_cert *head = chain_and_key->cert_chain->head; |
| 348 |
|
| 349 |
DEFER_CLEANUP(X509 *leaf_cert = NULL, X509_free_pointer); |
| 350 |
POSIX_GUARD_RESULT(s2n_openssl_x509_parse(&head->raw, &leaf_cert)); |
| 351 |
POSIX_GUARD_RESULT(s2n_openssl_x509_get_cert_info(leaf_cert, &head->info)); |
| 352 |
|
| 353 |
|
| 354 |
DEFER_CLEANUP(struct s2n_pkey public_key = { 0 }, s2n_pkey_free); |
| 355 |
s2n_pkey_type pkey_type = S2N_PKEY_TYPE_UNKNOWN; |
| 356 |
POSIX_GUARD_RESULT(s2n_pkey_from_x509(leaf_cert, &public_key, &pkey_type)); |
| 357 |
|
| 358 |
POSIX_ENSURE(pkey_type != S2N_PKEY_TYPE_UNKNOWN, S2N_ERR_CERT_TYPE_UNSUPPORTED); |
| 359 |
POSIX_GUARD(s2n_cert_set_cert_type(head, pkey_type)); |
| 360 |
|
| 361 |
|
| 362 |
if (s2n_pkey_check_key_exists(chain_and_key->private_key) == S2N_SUCCESS) { |
| 363 |
POSIX_GUARD(s2n_pkey_match(&public_key, chain_and_key->private_key)); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
POSIX_GUARD(s2n_cert_chain_and_key_set_names(chain_and_key, leaf_cert)); |
| 368 |
|
| 369 |
|
| 370 |
struct s2n_cert *current = head->next; |
| 371 |
while (current != NULL) { |
| 372 |
DEFER_CLEANUP(X509 *parsed_cert = NULL, X509_free_pointer); |
| 373 |
POSIX_GUARD_RESULT(s2n_openssl_x509_parse(¤t->raw, &parsed_cert)); |
| 374 |
POSIX_GUARD_RESULT(s2n_openssl_x509_get_cert_info(parsed_cert, ¤t->info)); |
| 375 |
|
| 376 |
current = current->next; |
| 377 |
} |
| 378 |
|
| 379 |
return S2N_SUCCESS; |
| 380 |
} |
| 381 |
|
| 382 |
int s2n_cert_chain_and_key_load_pem(struct s2n_cert_chain_and_key *chain_and_key, const char *chain_pem, const char *private_key_pem) |
| 383 |
{ |
| 384 |
POSIX_ENSURE_REF(chain_and_key); |
| 385 |
|
| 386 |
POSIX_GUARD(s2n_cert_chain_and_key_set_cert_chain(chain_and_key, chain_pem)); |
| 387 |
POSIX_GUARD(s2n_cert_chain_and_key_set_private_key(chain_and_key, private_key_pem)); |
| 388 |
|
| 389 |
POSIX_GUARD(s2n_cert_chain_and_key_load(chain_and_key)); |
| 390 |
|
| 391 |
return S2N_SUCCESS; |
| 392 |
} |
| 393 |
|
| 394 |
int s2n_cert_chain_and_key_load_public_pem_bytes(struct s2n_cert_chain_and_key *chain_and_key, uint8_t *chain_pem, uint32_t chain_pem_len) |
| 395 |
{ |
| 396 |
POSIX_ENSURE_REF(chain_and_key); |
| 397 |
POSIX_GUARD(s2n_cert_chain_and_key_set_cert_chain_bytes(chain_and_key, chain_pem, chain_pem_len)); |
| 398 |
POSIX_GUARD(s2n_cert_chain_and_key_load(chain_and_key)); |
| 399 |
return S2N_SUCCESS; |
| 400 |
} |
| 401 |
|
| 402 |
int s2n_cert_chain_and_key_load_pem_bytes(struct s2n_cert_chain_and_key *chain_and_key, uint8_t *chain_pem, |
| 403 |
uint32_t chain_pem_len, uint8_t *private_key_pem, uint32_t private_key_pem_len) |
| 404 |
{ |
| 405 |
POSIX_ENSURE_REF(chain_and_key); |
| 406 |
|
| 407 |
POSIX_GUARD(s2n_cert_chain_and_key_set_cert_chain_bytes(chain_and_key, chain_pem, chain_pem_len)); |
| 408 |
POSIX_GUARD(s2n_cert_chain_and_key_set_private_key_bytes(chain_and_key, private_key_pem, private_key_pem_len)); |
| 409 |
|
| 410 |
POSIX_GUARD(s2n_cert_chain_and_key_load(chain_and_key)); |
| 411 |
|
| 412 |
return S2N_SUCCESS; |
| 413 |
} |
| 414 |
|
| 415 |
S2N_CLEANUP_RESULT s2n_cert_chain_and_key_ptr_free(struct s2n_cert_chain_and_key **cert_and_key) |
| 416 |
{ |
| 417 |
RESULT_ENSURE_REF(cert_and_key); |
| 418 |
RESULT_GUARD_POSIX(s2n_cert_chain_and_key_free(*cert_and_key)); |
| 419 |
*cert_and_key = NULL; |
| 420 |
return S2N_RESULT_OK; |
| 421 |
} |
| 422 |
|
| 423 |
int s2n_cert_chain_and_key_free(struct s2n_cert_chain_and_key *cert_and_key) |
| 424 |
{ |
| 425 |
if (cert_and_key == NULL) { |
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
if (cert_and_key->cert_chain) { |
| 431 |
struct s2n_cert *node = cert_and_key->cert_chain->head; |
| 432 |
while (node) { |
| 433 |
|
| 434 |
POSIX_GUARD(s2n_free(&node->raw)); |
| 435 |
|
| 436 |
cert_and_key->cert_chain->head = node->next; |
| 437 |
|
| 438 |
POSIX_GUARD(s2n_free_object((uint8_t **) &node, sizeof(struct s2n_cert))); |
| 439 |
node = cert_and_key->cert_chain->head; |
| 440 |
} |
| 441 |
|
| 442 |
POSIX_GUARD(s2n_free_object((uint8_t **) &cert_and_key->cert_chain, sizeof(struct s2n_cert_chain))); |
| 443 |
} |
| 444 |
|
| 445 |
if (cert_and_key->private_key) { |
| 446 |
POSIX_GUARD(s2n_pkey_free(cert_and_key->private_key)); |
| 447 |
POSIX_GUARD(s2n_free_object((uint8_t **) &cert_and_key->private_key, sizeof(s2n_cert_private_key))); |
| 448 |
} |
| 449 |
|
| 450 |
uint32_t len = 0; |
| 451 |
|
| 452 |
if (cert_and_key->san_names) { |
| 453 |
POSIX_GUARD_RESULT(s2n_array_num_elements(cert_and_key->san_names, &len)); |
| 454 |
for (uint32_t i = 0; i < len; i++) { |
| 455 |
struct s2n_blob *san_name = NULL; |
| 456 |
POSIX_GUARD_RESULT(s2n_array_get(cert_and_key->san_names, i, (void **) &san_name)); |
| 457 |
POSIX_GUARD(s2n_free(san_name)); |
| 458 |
} |
| 459 |
POSIX_GUARD_RESULT(s2n_array_free(cert_and_key->san_names)); |
| 460 |
cert_and_key->san_names = NULL; |
| 461 |
} |
| 462 |
|
| 463 |
if (cert_and_key->cn_names) { |
| 464 |
POSIX_GUARD_RESULT(s2n_array_num_elements(cert_and_key->cn_names, &len)); |
| 465 |
for (uint32_t i = 0; i < len; i++) { |
| 466 |
struct s2n_blob *cn_name = NULL; |
| 467 |
POSIX_GUARD_RESULT(s2n_array_get(cert_and_key->cn_names, i, (void **) &cn_name)); |
| 468 |
POSIX_GUARD(s2n_free(cn_name)); |
| 469 |
} |
| 470 |
POSIX_GUARD_RESULT(s2n_array_free(cert_and_key->cn_names)); |
| 471 |
cert_and_key->cn_names = NULL; |
| 472 |
} |
| 473 |
|
| 474 |
POSIX_GUARD(s2n_free(&cert_and_key->ocsp_status)); |
| 475 |
POSIX_GUARD(s2n_free(&cert_and_key->sct_list)); |
| 476 |
|
| 477 |
POSIX_GUARD(s2n_free_object((uint8_t **) &cert_and_key, sizeof(struct s2n_cert_chain_and_key))); |
| 478 |
return 0; |
| 479 |
} |
| 480 |
|
| 481 |
int s2n_cert_chain_free(struct s2n_cert_chain *cert_chain) |
| 482 |
{ |
| 483 |
|
| 484 |
if (cert_chain) { |
| 485 |
struct s2n_cert *node = cert_chain->head; |
| 486 |
while (node) { |
| 487 |
|
| 488 |
POSIX_GUARD(s2n_free(&node->raw)); |
| 489 |
|
| 490 |
cert_chain->head = node->next; |
| 491 |
|
| 492 |
POSIX_GUARD(s2n_free_object((uint8_t **) &node, sizeof(struct s2n_cert))); |
| 493 |
node = cert_chain->head; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
return S2N_SUCCESS; |
| 498 |
} |
| 499 |
|
| 500 |
int s2n_send_cert_chain(struct s2n_connection *conn, struct s2n_stuffer *out, struct s2n_cert_chain_and_key *chain_and_key) |
| 501 |
{ |
| 502 |
POSIX_ENSURE_REF(conn); |
| 503 |
POSIX_ENSURE_REF(out); |
| 504 |
POSIX_ENSURE_REF(chain_and_key); |
| 505 |
struct s2n_cert_chain *chain = chain_and_key->cert_chain; |
| 506 |
POSIX_ENSURE_REF(chain); |
| 507 |
struct s2n_cert *cur_cert = chain->head; |
| 508 |
POSIX_ENSURE_REF(cur_cert); |
| 509 |
|
| 510 |
struct s2n_stuffer_reservation cert_chain_size = { 0 }; |
| 511 |
POSIX_GUARD(s2n_stuffer_reserve_uint24(out, &cert_chain_size)); |
| 512 |
|
| 513 |
|
| 514 |
bool first_entry = true; |
| 515 |
while (cur_cert) { |
| 516 |
POSIX_ENSURE_REF(cur_cert); |
| 517 |
POSIX_GUARD(s2n_stuffer_write_uint24(out, cur_cert->raw.size)); |
| 518 |
POSIX_GUARD(s2n_stuffer_write_bytes(out, cur_cert->raw.data, cur_cert->raw.size)); |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
if (conn->actual_protocol_version >= S2N_TLS13) { |
| 526 |
if (first_entry) { |
| 527 |
POSIX_GUARD(s2n_extension_list_send(S2N_EXTENSION_LIST_CERTIFICATE, conn, out)); |
| 528 |
first_entry = false; |
| 529 |
} else { |
| 530 |
POSIX_GUARD(s2n_extension_list_send(S2N_EXTENSION_LIST_EMPTY, conn, out)); |
| 531 |
} |
| 532 |
} |
| 533 |
cur_cert = cur_cert->next; |
| 534 |
} |
| 535 |
|
| 536 |
POSIX_GUARD(s2n_stuffer_write_vector_size(&cert_chain_size)); |
| 537 |
|
| 538 |
return 0; |
| 539 |
} |
| 540 |
|
| 541 |
int s2n_send_empty_cert_chain(struct s2n_stuffer *out) |
| 542 |
{ |
| 543 |
POSIX_ENSURE_REF(out); |
| 544 |
POSIX_GUARD(s2n_stuffer_write_uint24(out, 0)); |
| 545 |
return 0; |
| 546 |
} |
| 547 |
|
| 548 |
static int s2n_does_cert_san_match_hostname(const struct s2n_cert_chain_and_key *chain_and_key, const struct s2n_blob *dns_name) |
| 549 |
{ |
| 550 |
POSIX_ENSURE_REF(chain_and_key); |
| 551 |
POSIX_ENSURE_REF(dns_name); |
| 552 |
|
| 553 |
struct s2n_array *san_names = chain_and_key->san_names; |
| 554 |
uint32_t len = 0; |
| 555 |
POSIX_GUARD_RESULT(s2n_array_num_elements(san_names, &len)); |
| 556 |
for (uint32_t i = 0; i < len; i++) { |
| 557 |
struct s2n_blob *san_name = NULL; |
| 558 |
POSIX_GUARD_RESULT(s2n_array_get(san_names, i, (void **) &san_name)); |
| 559 |
POSIX_ENSURE_REF(san_name); |
| 560 |
if ((dns_name->size == san_name->size) && (strncasecmp((const char *) dns_name->data, (const char *) san_name->data, dns_name->size) == 0)) { |
| 561 |
return 1; |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
return 0; |
| 566 |
} |
| 567 |
|
| 568 |
static int s2n_does_cert_cn_match_hostname(const struct s2n_cert_chain_and_key *chain_and_key, const struct s2n_blob *dns_name) |
| 569 |
{ |
| 570 |
POSIX_ENSURE_REF(chain_and_key); |
| 571 |
POSIX_ENSURE_REF(dns_name); |
| 572 |
|
| 573 |
struct s2n_array *cn_names = chain_and_key->cn_names; |
| 574 |
uint32_t len = 0; |
| 575 |
POSIX_GUARD_RESULT(s2n_array_num_elements(cn_names, &len)); |
| 576 |
for (uint32_t i = 0; i < len; i++) { |
| 577 |
struct s2n_blob *cn_name = NULL; |
| 578 |
POSIX_GUARD_RESULT(s2n_array_get(cn_names, i, (void **) &cn_name)); |
| 579 |
POSIX_ENSURE_REF(cn_name); |
| 580 |
if ((dns_name->size == cn_name->size) && (strncasecmp((const char *) dns_name->data, (const char *) cn_name->data, dns_name->size) == 0)) { |
| 581 |
return 1; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
return 0; |
| 586 |
} |
| 587 |
|
| 588 |
int s2n_cert_chain_and_key_matches_dns_name(const struct s2n_cert_chain_and_key *chain_and_key, const struct s2n_blob *dns_name) |
| 589 |
{ |
| 590 |
uint32_t len = 0; |
| 591 |
POSIX_GUARD_RESULT(s2n_array_num_elements(chain_and_key->san_names, &len)); |
| 592 |
if (len > 0) { |
| 593 |
if (s2n_does_cert_san_match_hostname(chain_and_key, dns_name)) { |
| 594 |
return 1; |
| 595 |
} |
| 596 |
} else { |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
if (s2n_does_cert_cn_match_hostname(chain_and_key, dns_name)) { |
| 602 |
return 1; |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
return 0; |
| 607 |
} |
| 608 |
|
| 609 |
int s2n_cert_chain_and_key_set_ctx(struct s2n_cert_chain_and_key *cert_and_key, void *ctx) |
| 610 |
{ |
| 611 |
POSIX_ENSURE_REF(cert_and_key); |
| 612 |
cert_and_key->context = ctx; |
| 613 |
return 0; |
| 614 |
} |
| 615 |
|
| 616 |
void *s2n_cert_chain_and_key_get_ctx(struct s2n_cert_chain_and_key *cert_and_key) |
| 617 |
{ |
| 618 |
PTR_ENSURE_REF(cert_and_key); |
| 619 |
return cert_and_key->context; |
| 620 |
} |
| 621 |
|
| 622 |
s2n_pkey_type s2n_cert_chain_and_key_get_pkey_type(struct s2n_cert_chain_and_key *chain_and_key) |
| 623 |
{ |
| 624 |
if (chain_and_key == NULL |
| 625 |
|| chain_and_key->cert_chain == NULL |
| 626 |
|| chain_and_key->cert_chain->head == NULL) { |
| 627 |
return S2N_PKEY_TYPE_UNKNOWN; |
| 628 |
} |
| 629 |
return chain_and_key->cert_chain->head->pkey_type; |
| 630 |
} |
| 631 |
|
| 632 |
s2n_cert_private_key *s2n_cert_chain_and_key_get_private_key(struct s2n_cert_chain_and_key *chain_and_key) |
| 633 |
{ |
| 634 |
PTR_ENSURE_REF(chain_and_key); |
| 635 |
return chain_and_key->private_key; |
| 636 |
} |
| 637 |
|
| 638 |
int s2n_cert_chain_get_length(const struct s2n_cert_chain_and_key *chain_and_key, uint32_t *cert_length) |
| 639 |
{ |
| 640 |
POSIX_ENSURE_REF(chain_and_key); |
| 641 |
POSIX_ENSURE_REF(cert_length); |
| 642 |
|
| 643 |
struct s2n_cert *head_cert = chain_and_key->cert_chain->head; |
| 644 |
POSIX_ENSURE_REF(head_cert); |
| 645 |
*cert_length = 1; |
| 646 |
struct s2n_cert *next_cert = head_cert->next; |
| 647 |
while (next_cert != NULL) { |
| 648 |
*cert_length += 1; |
| 649 |
next_cert = next_cert->next; |
| 650 |
} |
| 651 |
|
| 652 |
return S2N_SUCCESS; |
| 653 |
} |
| 654 |
|
| 655 |
int s2n_cert_chain_get_cert(const struct s2n_cert_chain_and_key *chain_and_key, struct s2n_cert **out_cert, |
| 656 |
const uint32_t cert_idx) |
| 657 |
{ |
| 658 |
POSIX_ENSURE_REF(chain_and_key); |
| 659 |
POSIX_ENSURE_REF(out_cert); |
| 660 |
|
| 661 |
struct s2n_cert *cur_cert = chain_and_key->cert_chain->head; |
| 662 |
POSIX_ENSURE_REF(cur_cert); |
| 663 |
uint32_t counter = 0; |
| 664 |
|
| 665 |
struct s2n_cert *next_cert = cur_cert->next; |
| 666 |
|
| 667 |
while ((next_cert != NULL) && (counter < cert_idx)) { |
| 668 |
cur_cert = next_cert; |
| 669 |
next_cert = next_cert->next; |
| 670 |
counter++; |
| 671 |
} |
| 672 |
|
| 673 |
POSIX_ENSURE(counter == cert_idx, S2N_ERR_NO_CERT_FOUND); |
| 674 |
POSIX_ENSURE(cur_cert != NULL, S2N_ERR_NO_CERT_FOUND); |
| 675 |
*out_cert = cur_cert; |
| 676 |
|
| 677 |
return S2N_SUCCESS; |
| 678 |
} |
| 679 |
|
| 680 |
int s2n_cert_get_der(const struct s2n_cert *cert, const uint8_t **out_cert_der, uint32_t *cert_length) |
| 681 |
{ |
| 682 |
POSIX_ENSURE_REF(cert); |
| 683 |
POSIX_ENSURE_REF(out_cert_der); |
| 684 |
POSIX_ENSURE_REF(cert_length); |
| 685 |
|
| 686 |
*cert_length = cert->raw.size; |
| 687 |
*out_cert_der = cert->raw.data; |
| 688 |
|
| 689 |
return S2N_SUCCESS; |
| 690 |
} |
| 691 |
|
| 692 |
static int s2n_asn1_obj_free(ASN1_OBJECT **data) |
| 693 |
{ |
| 694 |
if (*data != NULL) { |
| 695 |
ASN1_OBJECT_free(*data); |
| 696 |
} |
| 697 |
return S2N_SUCCESS; |
| 698 |
} |
| 699 |
|
| 700 |
static int s2n_asn1_string_free(ASN1_STRING **data) |
| 701 |
{ |
| 702 |
if (*data != NULL) { |
| 703 |
ASN1_STRING_free(*data); |
| 704 |
} |
| 705 |
return S2N_SUCCESS; |
| 706 |
} |
| 707 |
|
| 708 |
static int s2n_utf8_string_from_extension_data(const uint8_t *extension_data, uint32_t extension_len, uint8_t *out_data, uint32_t *out_len) |
| 709 |
{ |
| 710 |
DEFER_CLEANUP(ASN1_STRING *asn1_str = NULL, s2n_asn1_string_free); |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
const uint8_t *asn1_str_data = extension_data; |
| 717 |
asn1_str = d2i_ASN1_UTF8STRING(NULL, (const unsigned char **) (void *) &asn1_str_data, extension_len); |
| 718 |
POSIX_ENSURE(asn1_str != NULL, S2N_ERR_INVALID_X509_EXTENSION_TYPE); |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
int type = ASN1_STRING_type(asn1_str); |
| 723 |
POSIX_ENSURE(type == V_ASN1_UTF8STRING, S2N_ERR_INVALID_X509_EXTENSION_TYPE); |
| 724 |
|
| 725 |
int len = ASN1_STRING_length(asn1_str); |
| 726 |
POSIX_ENSURE_GTE(len, 0); |
| 727 |
if (out_data != NULL) { |
| 728 |
POSIX_ENSURE((int64_t) *out_len >= (int64_t) len, S2N_ERR_INSUFFICIENT_MEM_SIZE); |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
unsigned char *internal_data = ASN1_STRING_data(asn1_str); |
| 734 |
POSIX_ENSURE_REF(internal_data); |
| 735 |
POSIX_CHECKED_MEMCPY(out_data, internal_data, len); |
| 736 |
} |
| 737 |
*out_len = len; |
| 738 |
return S2N_SUCCESS; |
| 739 |
} |
| 740 |
|
| 741 |
int s2n_cert_get_utf8_string_from_extension_data_length(const uint8_t *extension_data, uint32_t extension_len, uint32_t *utf8_str_len) |
| 742 |
{ |
| 743 |
POSIX_ENSURE_REF(extension_data); |
| 744 |
POSIX_ENSURE_GT(extension_len, 0); |
| 745 |
POSIX_ENSURE_REF(utf8_str_len); |
| 746 |
|
| 747 |
POSIX_GUARD(s2n_utf8_string_from_extension_data(extension_data, extension_len, NULL, utf8_str_len)); |
| 748 |
|
| 749 |
return S2N_SUCCESS; |
| 750 |
} |
| 751 |
|
| 752 |
int s2n_cert_get_utf8_string_from_extension_data(const uint8_t *extension_data, uint32_t extension_len, uint8_t *out_data, uint32_t *out_len) |
| 753 |
{ |
| 754 |
POSIX_ENSURE_REF(extension_data); |
| 755 |
POSIX_ENSURE_GT(extension_len, 0); |
| 756 |
POSIX_ENSURE_REF(out_data); |
| 757 |
POSIX_ENSURE_REF(out_len); |
| 758 |
|
| 759 |
POSIX_GUARD(s2n_utf8_string_from_extension_data(extension_data, extension_len, out_data, out_len)); |
| 760 |
|
| 761 |
return S2N_SUCCESS; |
| 762 |
} |
| 763 |
|
| 764 |
static int s2n_parse_x509_extension(struct s2n_cert *cert, const uint8_t *oid, |
| 765 |
uint8_t *ext_value, uint32_t *ext_value_len, bool *critical) |
| 766 |
{ |
| 767 |
POSIX_ENSURE_REF(cert->raw.data); |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
uint8_t *der_in = cert->raw.data; |
| 775 |
DEFER_CLEANUP(X509 *x509_cert = d2i_X509(NULL, (const unsigned char **) (void *) &der_in, cert->raw.size), |
| 776 |
X509_free_pointer); |
| 777 |
POSIX_ENSURE_REF(x509_cert); |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
int ext_count_value = X509_get_ext_count(x509_cert); |
| 784 |
POSIX_ENSURE_GT(ext_count_value, 0); |
| 785 |
size_t ext_count = (size_t) ext_count_value; |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
DEFER_CLEANUP(ASN1_OBJECT *asn1_obj_in = OBJ_txt2obj((const char *) oid, 0), s2n_asn1_obj_free); |
| 793 |
POSIX_ENSURE_REF(asn1_obj_in); |
| 794 |
|
| 795 |
for (size_t loc = 0; loc < ext_count; loc++) { |
| 796 |
ASN1_OCTET_STRING *asn1_str = NULL; |
| 797 |
bool match_found = false; |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
X509_EXTENSION *x509_ext = X509_get_ext(x509_cert, loc); |
| 806 |
POSIX_ENSURE_REF(x509_ext); |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
ASN1_OBJECT *asn1_obj = X509_EXTENSION_get_object(x509_ext); |
| 814 |
POSIX_ENSURE_REF(asn1_obj); |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
match_found = (0 == OBJ_cmp(asn1_obj_in, asn1_obj)); |
| 820 |
|
| 821 |
|
| 822 |
if (match_found) { |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
asn1_str = X509_EXTENSION_get_data(x509_ext); |
| 828 |
POSIX_ENSURE_REF(asn1_str); |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
int len = ASN1_STRING_length(asn1_str); |
| 833 |
if (ext_value != NULL) { |
| 834 |
POSIX_ENSURE_GTE(len, 0); |
| 835 |
POSIX_ENSURE(*ext_value_len >= (uint32_t) len, S2N_ERR_INSUFFICIENT_MEM_SIZE); |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
unsigned char *internal_data = ASN1_STRING_data(asn1_str); |
| 841 |
POSIX_ENSURE_REF(internal_data); |
| 842 |
POSIX_CHECKED_MEMCPY(ext_value, internal_data, len); |
| 843 |
} |
| 844 |
if (critical != NULL) { |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
*critical = X509_EXTENSION_get_critical(x509_ext); |
| 851 |
} |
| 852 |
*ext_value_len = len; |
| 853 |
return S2N_SUCCESS; |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
POSIX_BAIL(S2N_ERR_X509_EXTENSION_VALUE_NOT_FOUND); |
| 858 |
} |
| 859 |
|
| 860 |
int s2n_cert_get_x509_extension_value_length(struct s2n_cert *cert, const uint8_t *oid, uint32_t *ext_value_len) |
| 861 |
{ |
| 862 |
POSIX_ENSURE_REF(cert); |
| 863 |
POSIX_ENSURE_REF(oid); |
| 864 |
POSIX_ENSURE_REF(ext_value_len); |
| 865 |
|
| 866 |
POSIX_GUARD(s2n_parse_x509_extension(cert, oid, NULL, ext_value_len, NULL)); |
| 867 |
|
| 868 |
return S2N_SUCCESS; |
| 869 |
} |
| 870 |
|
| 871 |
int s2n_cert_get_x509_extension_value(struct s2n_cert *cert, const uint8_t *oid, |
| 872 |
uint8_t *ext_value, uint32_t *ext_value_len, bool *critical) |
| 873 |
{ |
| 874 |
POSIX_ENSURE_REF(cert); |
| 875 |
POSIX_ENSURE_REF(oid); |
| 876 |
POSIX_ENSURE_REF(ext_value); |
| 877 |
POSIX_ENSURE_REF(ext_value_len); |
| 878 |
POSIX_ENSURE_REF(critical); |
| 879 |
|
| 880 |
POSIX_GUARD(s2n_parse_x509_extension(cert, oid, ext_value, ext_value_len, critical)); |
| 881 |
|
| 882 |
return S2N_SUCCESS; |
| 883 |
} |