| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <errno.h> |
| 20 |
#include <fcntl.h> |
| 21 |
#include <stddef.h> |
| 22 |
#include <stdint.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <unistd.h> |
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
#include <openssl/crypto.h> |
| 29 |
#include <openssl/err.h> |
| 30 |
#include <openssl/x509v3.h> |
| 31 |
#include <openssl/x509.h> |
| 32 |
#include <openssl/pem.h> |
| 33 |
|
| 34 |
#include "api/s2n.h" |
| 35 |
#include "stuffer/s2n_stuffer.h" |
| 36 |
#include "tls/s2n_cipher_suites.h" |
| 37 |
#include "tls/s2n_config.h" |
| 38 |
#include "tls/s2n_connection.h" |
| 39 |
#include "tls/s2n_crypto.h" |
| 40 |
#include "tls/s2n_tls.h" |
| 41 |
#include "tls/s2n_tls_parameters.h" |
| 42 |
#include "utils/s2n_random.h" |
| 43 |
#include "utils/s2n_safety.h" |
| 44 |
#include "s2n_test.h" |
| 45 |
|
| 46 |
#define MAX_TOKENS 1024 |
| 47 |
#define MAX_CERTIFICATES 256 |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
size_t find_strings(const uint8_t *buf, size_t len, const char **output_strings, size_t max_strings) |
| 54 |
{ |
| 55 |
size_t num_strings = 0; |
| 56 |
int cursor = 0; |
| 57 |
while(1) { |
| 58 |
if (cursor >= len || num_strings == max_strings) { |
| 59 |
return num_strings; |
| 60 |
} |
| 61 |
const char *cur_str = (const char *) (buf + cursor); |
| 62 |
const char *next_null = (const char *) memchr((const void *) cur_str, '\0', (len - cursor)); |
| 63 |
if (next_null == NULL) { |
| 64 |
return num_strings; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
cursor = (((const uint8_t *) next_null - buf) + 1); |
| 69 |
if (cursor >= len) { |
| 70 |
return num_strings; |
| 71 |
} |
| 72 |
output_strings[num_strings] = cur_str; |
| 73 |
num_strings++; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
GENERAL_NAME *string_to_general_name(const char *str) |
| 78 |
{ |
| 79 |
ASN1_IA5STRING *asn1_name_str = ASN1_IA5STRING_new(); |
| 80 |
if (!asn1_name_str) { |
| 81 |
return NULL; |
| 82 |
} |
| 83 |
|
| 84 |
if (!ASN1_STRING_set(asn1_name_str, str, strlen(str))) { |
| 85 |
ASN1_IA5STRING_free(asn1_name_str); |
| 86 |
return NULL; |
| 87 |
} |
| 88 |
|
| 89 |
GENERAL_NAME *san_name = GENERAL_NAME_new(); |
| 90 |
if (!san_name) { |
| 91 |
ASN1_IA5STRING_free(asn1_name_str); |
| 92 |
return NULL; |
| 93 |
} |
| 94 |
|
| 95 |
GENERAL_NAME_set0_value(san_name, GEN_DNS, asn1_name_str); |
| 96 |
return san_name; |
| 97 |
} |
| 98 |
|
| 99 |
static int set_x509_sans(X509* x509_cert, const char **names, size_t num_sans) |
| 100 |
{ |
| 101 |
GENERAL_NAMES* san_names = sk_GENERAL_NAME_new_null(); |
| 102 |
if (!san_names) { |
| 103 |
return -1; |
| 104 |
} |
| 105 |
|
| 106 |
for (int i = 0; i < num_sans; i++) { |
| 107 |
GENERAL_NAME *san_name = string_to_general_name(names[i]); |
| 108 |
if (!san_name) { |
| 109 |
continue; |
| 110 |
} |
| 111 |
sk_GENERAL_NAME_push(san_names, san_name); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
if (X509_add1_ext_i2d(x509_cert, NID_subject_alt_name, san_names, 0, X509V3_ADD_REPLACE) <= 0) { |
| 116 |
GENERAL_NAMES_free(san_names); |
| 117 |
return -1; |
| 118 |
} |
| 119 |
|
| 120 |
GENERAL_NAMES_free(san_names); |
| 121 |
return S2N_SUCCESS; |
| 122 |
} |
| 123 |
|
| 124 |
static int set_x509_cns(X509 *x509_cert, const char **cns, size_t num_cns) |
| 125 |
{ |
| 126 |
X509_NAME *x509_name = X509_NAME_new(); |
| 127 |
if (!x509_name) { |
| 128 |
return -1; |
| 129 |
} |
| 130 |
|
| 131 |
for (int i = 0; i < num_cns; i++) { |
| 132 |
X509_NAME_add_entry_by_NID(x509_name, NID_commonName, MBSTRING_ASC, (unsigned char *)(uintptr_t) cns[i], -1, -1, 1); |
| 133 |
} |
| 134 |
|
| 135 |
X509_set_subject_name(x509_cert, x509_name); |
| 136 |
X509_NAME_free(x509_name); |
| 137 |
return S2N_SUCCESS; |
| 138 |
} |
| 139 |
|
| 140 |
static struct s2n_cert_chain_and_key *create_cert(const char **names, int num_names) |
| 141 |
{ |
| 142 |
struct s2n_cert_chain_and_key *cert = s2n_cert_chain_and_key_new(); |
| 143 |
X509 *x509_cert = X509_new(); |
| 144 |
|
| 145 |
if (!x509_cert || !cert) { |
| 146 |
goto cert_cleanup; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
const uint8_t is_san = names[0][0] & 0x1; |
| 153 |
if (is_san) { |
| 154 |
if (set_x509_sans(x509_cert, names, num_names) < 0) { |
| 155 |
goto cert_cleanup; |
| 156 |
} |
| 157 |
if (s2n_cert_chain_and_key_load_sans(cert, x509_cert) < 0) { |
| 158 |
goto cert_cleanup; |
| 159 |
} |
| 160 |
} else { |
| 161 |
if (set_x509_cns(x509_cert, names, num_names) < 0) { |
| 162 |
goto cert_cleanup; |
| 163 |
} |
| 164 |
if (s2n_cert_chain_and_key_load_cns(cert, x509_cert) < 0) { |
| 165 |
goto cert_cleanup; |
| 166 |
} |
| 167 |
} |
| 168 |
X509_free(x509_cert); |
| 169 |
x509_cert = NULL; |
| 170 |
|
| 171 |
|
| 172 |
s2n_pkey_type pkey_type = (names[0][0] & 0x2) ? S2N_PKEY_TYPE_RSA: S2N_PKEY_TYPE_ECDSA; |
| 173 |
struct s2n_cert *head = calloc(1, sizeof(struct s2n_cert)); |
| 174 |
if (!head) { |
| 175 |
goto cert_cleanup; |
| 176 |
} |
| 177 |
|
| 178 |
head->pkey_type = pkey_type; |
| 179 |
cert->cert_chain->head = head; |
| 180 |
return cert; |
| 181 |
|
| 182 |
cert_cleanup: |
| 183 |
if (cert) { s2n_cert_chain_and_key_free(cert); } |
| 184 |
if (x509_cert) { X509_free(x509_cert); } |
| 185 |
return NULL; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
static size_t create_certs(const char **strings, unsigned int num_strings, struct s2n_cert_chain_and_key **out_certs, unsigned int num_certs) |
| 193 |
{ |
| 194 |
if (num_certs == 0) { |
| 195 |
return S2N_SUCCESS; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
if (num_strings <= num_certs) { |
| 200 |
return S2N_SUCCESS; |
| 201 |
} |
| 202 |
|
| 203 |
const int num_names_per_cert = num_strings / num_certs; |
| 204 |
size_t num_certs_added = 0; |
| 205 |
for (int i = 0; i < num_certs; i++) { |
| 206 |
struct s2n_cert_chain_and_key *cert = create_cert((&strings[i*num_names_per_cert]), num_names_per_cert); |
| 207 |
if (!cert) { |
| 208 |
continue; |
| 209 |
} |
| 210 |
out_certs[num_certs_added] = cert; |
| 211 |
num_certs_added++; |
| 212 |
} |
| 213 |
|
| 214 |
return num_certs_added; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
int s2n_fuzz_test(const uint8_t *buf, size_t len) |
| 225 |
{ |
| 226 |
struct s2n_config *config = s2n_config_new(); |
| 227 |
struct s2n_connection *conn = s2n_connection_new(S2N_SERVER); |
| 228 |
struct s2n_cert_chain_and_key *certs[MAX_CERTIFICATES] = { NULL }; |
| 229 |
|
| 230 |
if (!config || !conn || len == 0) { |
| 231 |
goto cleanup; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
const char *strings[MAX_TOKENS] = { NULL }; |
| 238 |
size_t num_strings = find_strings(buf, len, strings, MAX_TOKENS); |
| 239 |
if (num_strings == 0) { |
| 240 |
goto cleanup; |
| 241 |
} |
| 242 |
|
| 243 |
if (s2n_connection_set_config(conn, config) < 0) { |
| 244 |
goto cleanup; |
| 245 |
} |
| 246 |
|
| 247 |
const int max_certs_to_create = buf[0] % MAX_CERTIFICATES; |
| 248 |
const int num_certs = create_certs(strings, num_strings, certs, max_certs_to_create); |
| 249 |
if (num_certs == 0) { |
| 250 |
goto cleanup; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
for (int i = 0; i < num_certs; i++) { |
| 255 |
if (s2n_config_add_cert_chain_and_key_to_store(config, certs[i]) < 0) { |
| 256 |
goto cleanup; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
for (int i = 0; i < num_strings; i++) { |
| 261 |
strncpy(conn->server_name, strings[i], S2N_MAX_SERVER_NAME); |
| 262 |
|
| 263 |
s2n_conn_find_name_matching_certs(conn); |
| 264 |
} |
| 265 |
cleanup: |
| 266 |
for (int i = 0; i < MAX_CERTIFICATES; i++) { |
| 267 |
if (certs[i]) { |
| 268 |
free(certs[i]->cert_chain->head); |
| 269 |
certs[i]->cert_chain->head = NULL; |
| 270 |
s2n_cert_chain_and_key_free(certs[i]); |
| 271 |
} |
| 272 |
} |
| 273 |
if (conn != NULL) { s2n_connection_free(conn); } |
| 274 |
if (config != NULL) { s2n_config_free(config); } |
| 275 |
|
| 276 |
return S2N_SUCCESS; |
| 277 |
} |
| 278 |
|
| 279 |
S2N_FUZZ_TARGET(NULL, s2n_fuzz_test, NULL) |