| 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 |
#include <stdlib.h> |
| 42 |
#include <string.h> |
| 43 |
#include <stdio.h> |
| 44 |
#include "os_port.h" |
| 45 |
#include "ssl.h" |
| 46 |
|
| 47 |
static int do_obj(SSL_CTX *ssl_ctx, int obj_type, |
| 48 |
SSLObjLoader *ssl_obj, const char *password); |
| 49 |
#ifdef CONFIG_SSL_HAS_PEM |
| 50 |
static int ssl_obj_PEM_load(SSL_CTX *ssl_ctx, int obj_type, |
| 51 |
SSLObjLoader *ssl_obj, const char *password); |
| 52 |
#endif |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
EXP_FUNC int STDCALL ssl_obj_load(SSL_CTX *ssl_ctx, int obj_type, |
| 58 |
const char *filename, const char *password) |
| 59 |
{ |
| 60 |
#ifndef CONFIG_SSL_SKELETON_MODE |
| 61 |
static const char * const begin = "-----BEGIN"; |
| 62 |
int ret = SSL_OK; |
| 63 |
SSLObjLoader *ssl_obj = NULL; |
| 64 |
|
| 65 |
if (filename == NULL) |
| 66 |
{ |
| 67 |
ret = SSL_ERROR_INVALID_KEY; |
| 68 |
goto error; |
| 69 |
} |
| 70 |
|
| 71 |
ssl_obj = (SSLObjLoader *)calloc(1, sizeof(SSLObjLoader)); |
| 72 |
ssl_obj->len = get_file(filename, &ssl_obj->buf); |
| 73 |
if (ssl_obj->len <= 0) |
| 74 |
{ |
| 75 |
ret = SSL_ERROR_INVALID_KEY; |
| 76 |
goto error; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
if (strstr((char *)ssl_obj->buf, begin) != NULL) |
| 81 |
{ |
| 82 |
#ifdef CONFIG_SSL_HAS_PEM |
| 83 |
ret = ssl_obj_PEM_load(ssl_ctx, obj_type, ssl_obj, password); |
| 84 |
#else |
| 85 |
#ifdef CONFIG_DEBUG |
| 86 |
printf("%s", unsupported_str); |
| 87 |
#endif |
| 88 |
ret = SSL_ERROR_NOT_SUPPORTED; |
| 89 |
#endif |
| 90 |
} |
| 91 |
else |
| 92 |
ret = do_obj(ssl_ctx, obj_type, ssl_obj, password); |
| 93 |
|
| 94 |
error: |
| 95 |
ssl_obj_free(ssl_obj); |
| 96 |
return ret; |
| 97 |
#else |
| 98 |
#ifdef CONFIG_DEBUG |
| 99 |
printf("%s", unsupported_str); |
| 100 |
#endif |
| 101 |
return SSL_ERROR_NOT_SUPPORTED; |
| 102 |
#endif |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
EXP_FUNC int STDCALL ssl_obj_memory_load(SSL_CTX *ssl_ctx, int mem_type, |
| 109 |
const uint8_t *data, int len, const char *password) |
| 110 |
{ |
| 111 |
int ret; |
| 112 |
SSLObjLoader *ssl_obj; |
| 113 |
|
| 114 |
ssl_obj = (SSLObjLoader *)calloc(1, sizeof(SSLObjLoader)); |
| 115 |
ssl_obj->buf = (uint8_t *)malloc(len); |
| 116 |
memcpy(ssl_obj->buf, data, len); |
| 117 |
ssl_obj->len = len; |
| 118 |
ret = do_obj(ssl_ctx, mem_type, ssl_obj, password); |
| 119 |
ssl_obj_free(ssl_obj); |
| 120 |
return ret; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
static int do_obj(SSL_CTX *ssl_ctx, int obj_type, |
| 127 |
SSLObjLoader *ssl_obj, const char *password) |
| 128 |
{ |
| 129 |
int ret = SSL_OK; |
| 130 |
|
| 131 |
switch (obj_type) |
| 132 |
{ |
| 133 |
case SSL_OBJ_RSA_KEY: |
| 134 |
ret = add_private_key(ssl_ctx, ssl_obj); |
| 135 |
break; |
| 136 |
|
| 137 |
case SSL_OBJ_X509_CERT: |
| 138 |
ret = add_cert(ssl_ctx, ssl_obj->buf, ssl_obj->len); |
| 139 |
break; |
| 140 |
|
| 141 |
#ifdef CONFIG_SSL_CERT_VERIFICATION |
| 142 |
case SSL_OBJ_X509_CACERT: |
| 143 |
add_cert_auth(ssl_ctx, ssl_obj->buf, ssl_obj->len); |
| 144 |
break; |
| 145 |
#endif |
| 146 |
|
| 147 |
#ifdef CONFIG_SSL_USE_PKCS12 |
| 148 |
case SSL_OBJ_PKCS8: |
| 149 |
ret = pkcs8_decode(ssl_ctx, ssl_obj, password); |
| 150 |
break; |
| 151 |
|
| 152 |
case SSL_OBJ_PKCS12: |
| 153 |
ret = pkcs12_decode(ssl_ctx, ssl_obj, password); |
| 154 |
break; |
| 155 |
#endif |
| 156 |
default: |
| 157 |
#ifdef CONFIG_DEBUG |
| 158 |
printf("%s", unsupported_str); |
| 159 |
#endif |
| 160 |
ret = SSL_ERROR_NOT_SUPPORTED; |
| 161 |
break; |
| 162 |
} |
| 163 |
|
| 164 |
return ret; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
void ssl_obj_free(SSLObjLoader *ssl_obj) |
| 171 |
{ |
| 172 |
if (ssl_obj) |
| 173 |
{ |
| 174 |
free(ssl_obj->buf); |
| 175 |
free(ssl_obj); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
#ifdef CONFIG_SSL_HAS_PEM |
| 183 |
|
| 184 |
#define NUM_PEM_TYPES 4 |
| 185 |
#define IV_SIZE 16 |
| 186 |
#define IS_RSA_PRIVATE_KEY 0 |
| 187 |
#define IS_ENCRYPTED_PRIVATE_KEY 1 |
| 188 |
#define IS_PRIVATE_KEY 2 |
| 189 |
#define IS_CERTIFICATE 3 |
| 190 |
|
| 191 |
static const char * const begins[NUM_PEM_TYPES] = |
| 192 |
{ |
| 193 |
"-----BEGIN RSA PRIVATE KEY-----", |
| 194 |
"-----BEGIN ENCRYPTED PRIVATE KEY-----", |
| 195 |
"-----BEGIN PRIVATE KEY-----", |
| 196 |
"-----BEGIN CERTIFICATE-----", |
| 197 |
}; |
| 198 |
|
| 199 |
static const char * const ends[NUM_PEM_TYPES] = |
| 200 |
{ |
| 201 |
"-----END RSA PRIVATE KEY-----", |
| 202 |
"-----END ENCRYPTED PRIVATE KEY-----", |
| 203 |
"-----END PRIVATE KEY-----", |
| 204 |
"-----END CERTIFICATE-----", |
| 205 |
}; |
| 206 |
|
| 207 |
static const char * const aes_str[2] = |
| 208 |
{ |
| 209 |
"DEK-Info: AES-128-CBC,", |
| 210 |
"DEK-Info: AES-256-CBC," |
| 211 |
}; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
static int pem_decrypt(const char *where, const char *end, |
| 218 |
const char *password, SSLObjLoader *ssl_obj) |
| 219 |
{ |
| 220 |
int ret = -1; |
| 221 |
int is_aes_256 = 0; |
| 222 |
char *start = NULL; |
| 223 |
uint8_t iv[IV_SIZE]; |
| 224 |
int i, pem_size; |
| 225 |
MD5_CTX md5_ctx; |
| 226 |
AES_CTX aes_ctx; |
| 227 |
uint8_t key[32]; |
| 228 |
|
| 229 |
if (password == NULL || strlen(password) == 0) |
| 230 |
{ |
| 231 |
#ifdef CONFIG_DEBUG |
| 232 |
printf("Error: Need a password for this PEM file\n"); |
| 233 |
#endif |
| 234 |
goto error; |
| 235 |
} |
| 236 |
|
| 237 |
if ((start = strstr((const char *)where, aes_str[0]))) |
| 238 |
{ |
| 239 |
start += strlen(aes_str[0]); |
| 240 |
} |
| 241 |
else if ((start = strstr((const char *)where, aes_str[1]))) |
| 242 |
{ |
| 243 |
is_aes_256 = 1; |
| 244 |
start += strlen(aes_str[1]); |
| 245 |
} |
| 246 |
else |
| 247 |
{ |
| 248 |
#ifdef CONFIG_DEBUG |
| 249 |
printf("Error: Unsupported password cipher\n"); |
| 250 |
#endif |
| 251 |
goto error; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
for (i = 0; i < IV_SIZE; i++) |
| 256 |
{ |
| 257 |
char c = *start++ - '0'; |
| 258 |
iv[i] = (c > 9 ? c + '0' - 'A' + 10 : c) << 4; |
| 259 |
c = *start++ - '0'; |
| 260 |
iv[i] += (c > 9 ? c + '0' - 'A' + 10 : c); |
| 261 |
} |
| 262 |
|
| 263 |
while (*start == '\r' || *start == '\n') |
| 264 |
start++; |
| 265 |
|
| 266 |
|
| 267 |
pem_size = (int)(end-start); |
| 268 |
if (base64_decode(start, pem_size, ssl_obj->buf, &ssl_obj->len) != 0) |
| 269 |
goto error; |
| 270 |
|
| 271 |
|
| 272 |
MD5_Init(&md5_ctx); |
| 273 |
MD5_Update(&md5_ctx, (const uint8_t *)password, strlen(password)); |
| 274 |
MD5_Update(&md5_ctx, iv, SALT_SIZE); |
| 275 |
MD5_Final(key, &md5_ctx); |
| 276 |
|
| 277 |
if (is_aes_256) |
| 278 |
{ |
| 279 |
MD5_Init(&md5_ctx); |
| 280 |
MD5_Update(&md5_ctx, key, MD5_SIZE); |
| 281 |
MD5_Update(&md5_ctx, (const uint8_t *)password, strlen(password)); |
| 282 |
MD5_Update(&md5_ctx, iv, SALT_SIZE); |
| 283 |
MD5_Final(&key[MD5_SIZE], &md5_ctx); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
AES_set_key(&aes_ctx, key, iv, is_aes_256 ? AES_MODE_256 : AES_MODE_128); |
| 288 |
AES_convert_key(&aes_ctx); |
| 289 |
AES_cbc_decrypt(&aes_ctx, ssl_obj->buf, ssl_obj->buf, ssl_obj->len); |
| 290 |
ret = 0; |
| 291 |
|
| 292 |
error: |
| 293 |
return ret; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
static int new_pem_obj(SSL_CTX *ssl_ctx, int is_cacert, char *where, |
| 300 |
int remain, const char *password) |
| 301 |
{ |
| 302 |
int ret = SSL_ERROR_BAD_CERTIFICATE; |
| 303 |
SSLObjLoader *ssl_obj = NULL; |
| 304 |
|
| 305 |
while (remain > 0) |
| 306 |
{ |
| 307 |
int i, pem_size, obj_type; |
| 308 |
char *start = NULL, *end = NULL; |
| 309 |
|
| 310 |
for (i = 0; i < NUM_PEM_TYPES; i++) |
| 311 |
{ |
| 312 |
if ((start = strstr(where, begins[i])) && |
| 313 |
(end = strstr(where, ends[i]))) |
| 314 |
{ |
| 315 |
remain -= (int)(end-where); |
| 316 |
start += strlen(begins[i]); |
| 317 |
pem_size = (int)(end-start); |
| 318 |
|
| 319 |
ssl_obj = (SSLObjLoader *)calloc(1, sizeof(SSLObjLoader)); |
| 320 |
|
| 321 |
|
| 322 |
ssl_obj->buf = (uint8_t *)calloc(1, pem_size); |
| 323 |
ssl_obj->len = pem_size; |
| 324 |
|
| 325 |
if (i == IS_RSA_PRIVATE_KEY && |
| 326 |
strstr(start, "Proc-Type:") && |
| 327 |
strstr(start, "4,ENCRYPTED")) |
| 328 |
{ |
| 329 |
|
| 330 |
if (pem_decrypt(start, end, password, ssl_obj) < 0) |
| 331 |
{ |
| 332 |
ret = SSL_ERROR_BAD_CERTIFICATE; |
| 333 |
goto error; |
| 334 |
} |
| 335 |
} |
| 336 |
else |
| 337 |
{ |
| 338 |
ssl_obj->len = pem_size; |
| 339 |
if (base64_decode(start, pem_size, |
| 340 |
ssl_obj->buf, &ssl_obj->len) != 0) |
| 341 |
{ |
| 342 |
ret = SSL_ERROR_BAD_CERTIFICATE; |
| 343 |
goto error; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
switch (i) |
| 348 |
{ |
| 349 |
case IS_RSA_PRIVATE_KEY: |
| 350 |
obj_type = SSL_OBJ_RSA_KEY; |
| 351 |
break; |
| 352 |
|
| 353 |
case IS_ENCRYPTED_PRIVATE_KEY: |
| 354 |
case IS_PRIVATE_KEY: |
| 355 |
obj_type = SSL_OBJ_PKCS8; |
| 356 |
break; |
| 357 |
|
| 358 |
case IS_CERTIFICATE: |
| 359 |
obj_type = is_cacert ? |
| 360 |
SSL_OBJ_X509_CACERT : SSL_OBJ_X509_CERT; |
| 361 |
break; |
| 362 |
|
| 363 |
default: |
| 364 |
ret = SSL_ERROR_BAD_CERTIFICATE; |
| 365 |
goto error; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
if ((ret = do_obj(ssl_ctx, obj_type, ssl_obj, password))) |
| 370 |
goto error; |
| 371 |
|
| 372 |
end += strlen(ends[i]); |
| 373 |
remain -= strlen(ends[i]); |
| 374 |
while (remain > 0 && (*end == '\r' || *end == '\n')) |
| 375 |
{ |
| 376 |
end++; |
| 377 |
remain--; |
| 378 |
} |
| 379 |
|
| 380 |
where = end; |
| 381 |
break; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
ssl_obj_free(ssl_obj); |
| 386 |
ssl_obj = NULL; |
| 387 |
if (start == NULL) |
| 388 |
break; |
| 389 |
} |
| 390 |
error: |
| 391 |
ssl_obj_free(ssl_obj); |
| 392 |
return ret; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
static int ssl_obj_PEM_load(SSL_CTX *ssl_ctx, int obj_type, |
| 399 |
SSLObjLoader *ssl_obj, const char *password) |
| 400 |
{ |
| 401 |
char *start; |
| 402 |
|
| 403 |
|
| 404 |
ssl_obj->len++; |
| 405 |
ssl_obj->buf = (uint8_t *)realloc(ssl_obj->buf, ssl_obj->len); |
| 406 |
ssl_obj->buf[ssl_obj->len-1] = 0; |
| 407 |
start = (char *)ssl_obj->buf; |
| 408 |
return new_pem_obj(ssl_ctx, obj_type == SSL_OBJ_X509_CACERT, |
| 409 |
start, ssl_obj->len, password); |
| 410 |
} |
| 411 |
#endif |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
int load_key_certs(SSL_CTX *ssl_ctx) |
| 418 |
{ |
| 419 |
int ret = SSL_OK; |
| 420 |
uint32_t options = ssl_ctx->options; |
| 421 |
#ifdef CONFIG_SSL_GENERATE_X509_CERT |
| 422 |
uint8_t *cert_data = NULL; |
| 423 |
int cert_size; |
| 424 |
static const char *dn[] = |
| 425 |
{ |
| 426 |
CONFIG_SSL_X509_COMMON_NAME, |
| 427 |
CONFIG_SSL_X509_ORGANIZATION_NAME, |
| 428 |
CONFIG_SSL_X509_ORGANIZATION_UNIT_NAME |
| 429 |
}; |
| 430 |
#endif |
| 431 |
|
| 432 |
|
| 433 |
if (strlen(CONFIG_SSL_PRIVATE_KEY_LOCATION) > 0) |
| 434 |
{ |
| 435 |
if ((ret = ssl_obj_load(ssl_ctx, SSL_OBJ_RSA_KEY, |
| 436 |
CONFIG_SSL_PRIVATE_KEY_LOCATION, |
| 437 |
CONFIG_SSL_PRIVATE_KEY_PASSWORD)) < 0) |
| 438 |
goto error; |
| 439 |
} |
| 440 |
else if (!(options & SSL_NO_DEFAULT_KEY)) |
| 441 |
{ |
| 442 |
#if defined(CONFIG_SSL_USE_DEFAULT_KEY) || defined(CONFIG_SSL_SKELETON_MODE) |
| 443 |
static const |
| 444 |
#include "private_key.h" |
| 445 |
|
| 446 |
ssl_obj_memory_load(ssl_ctx, SSL_OBJ_RSA_KEY, default_private_key, |
| 447 |
default_private_key_len, NULL); |
| 448 |
#endif |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
#ifdef CONFIG_SSL_GENERATE_X509_CERT |
| 453 |
if ((cert_size = ssl_x509_create(ssl_ctx, 0, dn, &cert_data)) < 0) |
| 454 |
{ |
| 455 |
ret = cert_size; |
| 456 |
goto error; |
| 457 |
} |
| 458 |
|
| 459 |
ssl_obj_memory_load(ssl_ctx, SSL_OBJ_X509_CERT, cert_data, cert_size, NULL); |
| 460 |
free(cert_data); |
| 461 |
#else |
| 462 |
if (strlen(CONFIG_SSL_X509_CERT_LOCATION)) |
| 463 |
{ |
| 464 |
if ((ret = ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT, |
| 465 |
CONFIG_SSL_X509_CERT_LOCATION, NULL)) < 0) |
| 466 |
goto error; |
| 467 |
} |
| 468 |
else if (!(options & SSL_NO_DEFAULT_KEY)) |
| 469 |
{ |
| 470 |
#if defined(CONFIG_SSL_USE_DEFAULT_KEY) || defined(CONFIG_SSL_SKELETON_MODE) |
| 471 |
#include "cert.h" |
| 472 |
ssl_obj_memory_load(ssl_ctx, SSL_OBJ_X509_CERT, |
| 473 |
default_certificate, default_certificate_len, NULL); |
| 474 |
#endif |
| 475 |
} |
| 476 |
#endif |
| 477 |
|
| 478 |
error: |
| 479 |
#ifdef CONFIG_DEBUG |
| 480 |
if (ret) |
| 481 |
{ |
| 482 |
printf("Error: Certificate or key not loaded\n"); |
| 483 |
} |
| 484 |
#endif |
| 485 |
|
| 486 |
return ret; |
| 487 |
|
| 488 |
} |