| 127 | | void wi_crypto_initialize(void) { |
|---|
| 128 | | } |
|---|
| 129 | | |
|---|
| 130 | | |
|---|
| 131 | | |
|---|
| 132 | | #pragma mark - |
|---|
| 133 | | |
|---|
| 134 | | wi_runtime_id_t wi_rsa_runtime_id(void) { |
|---|
| 135 | | return _wi_rsa_runtime_id; |
|---|
| 136 | | } |
|---|
| 137 | | |
|---|
| 138 | | |
|---|
| 139 | | |
|---|
| 140 | | #pragma mark - |
|---|
| 141 | | |
|---|
| 142 | | wi_rsa_t * wi_rsa_alloc(void) { |
|---|
| 143 | | return wi_runtime_create_instance(_wi_rsa_runtime_id, sizeof(wi_rsa_t)); |
|---|
| 144 | | } |
|---|
| 145 | | |
|---|
| 146 | | |
|---|
| 147 | | |
|---|
| 148 | | wi_rsa_t * wi_rsa_init_with_bits(wi_rsa_t *rsa, wi_uinteger_t size) { |
|---|
| 149 | | rsa->rsa = RSA_generate_key(size, RSA_F4, NULL, NULL); |
|---|
| 150 | | |
|---|
| 151 | | if(!rsa->rsa) { |
|---|
| 152 | | wi_release(rsa); |
|---|
| 153 | | |
|---|
| 154 | | return NULL; |
|---|
| 155 | | } |
|---|
| 156 | | |
|---|
| 157 | | return rsa; |
|---|
| 158 | | } |
|---|
| 159 | | |
|---|
| 160 | | |
|---|
| 161 | | |
|---|
| 162 | | wi_rsa_t * wi_rsa_init_with_rsa(wi_rsa_t *rsa, void *_rsa) { |
|---|
| 163 | | rsa->rsa = _rsa; |
|---|
| 164 | | |
|---|
| 165 | | return rsa; |
|---|
| 166 | | } |
|---|
| 167 | | |
|---|
| 168 | | |
|---|
| 169 | | |
|---|
| 170 | | wi_rsa_t * wi_rsa_init_with_pem_file(wi_rsa_t *rsa, wi_string_t *path) { |
|---|
| 171 | | FILE *fp; |
|---|
| 172 | | |
|---|
| 173 | | fp = fopen(wi_string_cstring(path), "r"); |
|---|
| 174 | | |
|---|
| 175 | | if(!fp) { |
|---|
| 176 | | wi_error_set_errno(errno); |
|---|
| 177 | | |
|---|
| 178 | | wi_release(rsa); |
|---|
| 179 | | |
|---|
| 180 | | return NULL; |
|---|
| 181 | | } |
|---|
| 182 | | |
|---|
| 183 | | rsa->rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL); |
|---|
| 184 | | |
|---|
| 185 | | fclose(fp); |
|---|
| 186 | | |
|---|
| 187 | | if(!rsa->rsa) { |
|---|
| 188 | | wi_error_set_openssl_error(); |
|---|
| 189 | | |
|---|
| 190 | | wi_release(rsa); |
|---|
| 191 | | |
|---|
| 192 | | return NULL; |
|---|
| 193 | | } |
|---|
| 194 | | |
|---|
| 195 | | return rsa; |
|---|
| 196 | | } |
|---|
| 197 | | |
|---|
| 198 | | |
|---|
| 199 | | |
|---|
| 200 | | wi_rsa_t * wi_rsa_init_with_private_key(wi_rsa_t *rsa, wi_data_t *data) { |
|---|
| 201 | | const unsigned char *buffer; |
|---|
| 202 | | long length; |
|---|
| 203 | | |
|---|
| 204 | | buffer = wi_data_bytes(data); |
|---|
| 205 | | length = wi_data_length(data); |
|---|
| 206 | | |
|---|
| 207 | | rsa->rsa = d2i_RSAPrivateKey(NULL, &buffer, length); |
|---|
| 208 | | |
|---|
| 209 | | if(!rsa->rsa) { |
|---|
| 210 | | wi_error_set_openssl_error(); |
|---|
| 211 | | |
|---|
| 212 | | wi_release(rsa); |
|---|
| 213 | | |
|---|
| 214 | | return NULL; |
|---|
| 215 | | } |
|---|
| 216 | | |
|---|
| 217 | | rsa->private_key = wi_retain(data); |
|---|
| 218 | | |
|---|
| 219 | | return rsa; |
|---|
| 220 | | } |
|---|
| 221 | | |
|---|
| 222 | | |
|---|
| 223 | | |
|---|
| 224 | | wi_rsa_t * wi_rsa_init_with_public_key(wi_rsa_t *rsa, wi_data_t *data) { |
|---|
| 225 | | const unsigned char *buffer; |
|---|
| 226 | | long length; |
|---|
| 227 | | |
|---|
| 228 | | buffer = wi_data_bytes(data); |
|---|
| 229 | | length = wi_data_length(data); |
|---|
| 230 | | |
|---|
| 231 | | rsa->rsa = d2i_RSAPublicKey(NULL, (const unsigned char **) &buffer, length); |
|---|
| 232 | | |
|---|
| 233 | | if(!rsa->rsa) { |
|---|
| 234 | | wi_error_set_openssl_error(); |
|---|
| 235 | | |
|---|
| 236 | | wi_release(rsa); |
|---|
| 237 | | |
|---|
| 238 | | return NULL; |
|---|
| 239 | | } |
|---|
| 240 | | |
|---|
| 241 | | rsa->public_key = wi_retain(data); |
|---|
| 242 | | |
|---|
| 243 | | return rsa; |
|---|
| 244 | | } |
|---|
| 245 | | |
|---|
| 246 | | |
|---|
| 247 | | |
|---|
| 248 | | static void _wi_rsa_dealloc(wi_runtime_instance_t *instance) { |
|---|
| 249 | | wi_rsa_t *rsa = instance; |
|---|
| 250 | | |
|---|
| 251 | | RSA_free(rsa->rsa); |
|---|
| 252 | | |
|---|
| 253 | | wi_release(rsa->public_key); |
|---|
| 254 | | wi_release(rsa->private_key); |
|---|
| 255 | | } |
|---|
| 256 | | |
|---|
| 257 | | |
|---|
| 258 | | |
|---|
| 259 | | static wi_string_t * _wi_rsa_description(wi_runtime_instance_t *instance) { |
|---|
| 260 | | wi_rsa_t *rsa = instance; |
|---|
| 261 | | |
|---|
| 262 | | return wi_string_with_format(WI_STR("<%@ %p>{key = %p, bits = %lu}"), |
|---|
| 263 | | wi_runtime_class_name(rsa), |
|---|
| 264 | | rsa, |
|---|
| 265 | | rsa->rsa, |
|---|
| 266 | | wi_rsa_bits(rsa)); |
|---|
| 267 | | } |
|---|
| 268 | | |
|---|
| 269 | | |
|---|
| 270 | | |
|---|
| 271 | | #pragma mark - |
|---|
| 272 | | |
|---|
| 273 | | void * wi_rsa_rsa(wi_rsa_t *rsa) { |
|---|
| 274 | | return rsa->rsa; |
|---|
| 275 | | } |
|---|
| 276 | | |
|---|
| 277 | | |
|---|
| 278 | | |
|---|
| 279 | | wi_data_t * wi_rsa_public_key(wi_rsa_t *rsa) { |
|---|
| 280 | | unsigned char *buffer; |
|---|
| 281 | | int length; |
|---|
| 282 | | |
|---|
| 283 | | if(!rsa->public_key) { |
|---|
| 284 | | buffer = NULL; |
|---|
| 285 | | length = i2d_RSAPublicKey(rsa->rsa, &buffer); |
|---|
| 286 | | |
|---|
| 287 | | if(length <= 0) { |
|---|
| 288 | | wi_error_set_openssl_error(); |
|---|
| 289 | | |
|---|
| 290 | | return NULL; |
|---|
| 291 | | } |
|---|
| 292 | | |
|---|
| 293 | | rsa->public_key = wi_data_init_with_bytes(wi_data_alloc(), buffer, length); |
|---|
| 294 | | |
|---|
| 295 | | OPENSSL_free(buffer); |
|---|
| 296 | | } |
|---|
| 297 | | |
|---|
| 298 | | return rsa->public_key; |
|---|
| 299 | | } |
|---|
| 300 | | |
|---|
| 301 | | |
|---|
| 302 | | |
|---|
| 303 | | wi_data_t * wi_rsa_private_key(wi_rsa_t *rsa) { |
|---|
| 304 | | unsigned char *buffer; |
|---|
| 305 | | int length; |
|---|
| 306 | | |
|---|
| 307 | | if(!rsa->private_key) { |
|---|
| 308 | | buffer = NULL; |
|---|
| 309 | | length = i2d_RSAPrivateKey(rsa->rsa, &buffer); |
|---|
| 310 | | |
|---|
| 311 | | if(length <= 0) { |
|---|
| 312 | | wi_error_set_openssl_error(); |
|---|
| 313 | | |
|---|
| 314 | | return NULL; |
|---|
| 315 | | } |
|---|
| 316 | | |
|---|
| 317 | | rsa->private_key = wi_data_init_with_bytes(wi_data_alloc(), buffer, length); |
|---|
| 318 | | |
|---|
| 319 | | OPENSSL_free(buffer); |
|---|
| 320 | | } |
|---|
| 321 | | |
|---|
| 322 | | return rsa->private_key; |
|---|
| 323 | | } |
|---|
| 324 | | |
|---|
| 325 | | |
|---|
| 326 | | |
|---|
| 327 | | wi_uinteger_t wi_rsa_bits(wi_rsa_t *rsa) { |
|---|
| 328 | | return RSA_size(rsa->rsa) * 8; |
|---|
| 329 | | } |
|---|
| 330 | | |
|---|
| 331 | | |
|---|
| 332 | | |
|---|
| 333 | | #pragma mark - |
|---|
| 334 | | |
|---|
| 335 | | wi_data_t * wi_rsa_encrypt(wi_rsa_t *rsa, wi_data_t *decrypted_data) { |
|---|
| 336 | | const void *decrypted_buffer; |
|---|
| 337 | | void *encrypted_buffer; |
|---|
| 338 | | wi_uinteger_t decrypted_length, encrypted_length; |
|---|
| 339 | | |
|---|
| 340 | | decrypted_buffer = wi_data_bytes(decrypted_data); |
|---|
| 341 | | decrypted_length = wi_data_length(decrypted_data); |
|---|
| 342 | | |
|---|
| 343 | | if(!wi_rsa_encrypt_bytes(rsa, decrypted_buffer, decrypted_length, &encrypted_buffer, &encrypted_length)) |
|---|
| 344 | | return NULL; |
|---|
| 345 | | |
|---|
| 346 | | return wi_data_with_bytes_no_copy(encrypted_buffer, encrypted_length, true); |
|---|
| 347 | | } |
|---|
| 348 | | |
|---|
| 349 | | |
|---|
| 350 | | |
|---|
| 351 | | wi_boolean_t wi_rsa_encrypt_bytes(wi_rsa_t *rsa, const void *decrypted_buffer, wi_uinteger_t decrypted_length, void **out_buffer, wi_uinteger_t *out_length) { |
|---|
| 352 | | void *encrypted_buffer; |
|---|
| 353 | | int32_t encrypted_length; |
|---|
| 354 | | |
|---|
| 355 | | encrypted_buffer = wi_malloc(RSA_size(rsa->rsa)); |
|---|
| 356 | | encrypted_length = RSA_public_encrypt(decrypted_length, decrypted_buffer, encrypted_buffer, rsa->rsa, RSA_PKCS1_PADDING); |
|---|
| 357 | | |
|---|
| 358 | | if(encrypted_length == -1) { |
|---|
| 359 | | wi_error_set_openssl_error(); |
|---|
| 360 | | |
|---|
| 361 | | wi_free(encrypted_buffer); |
|---|
| 362 | | |
|---|
| 363 | | return false; |
|---|
| 364 | | } |
|---|
| 365 | | |
|---|
| 366 | | *out_buffer = encrypted_buffer; |
|---|
| 367 | | *out_length = encrypted_length; |
|---|
| 368 | | |
|---|
| 369 | | return true; |
|---|
| 370 | | } |
|---|
| 371 | | |
|---|
| 372 | | |
|---|
| 373 | | |
|---|
| 374 | | wi_data_t * wi_rsa_decrypt(wi_rsa_t *rsa, wi_data_t *encrypted_data) { |
|---|
| 375 | | const void *encrypted_buffer; |
|---|
| 376 | | void *decrypted_buffer; |
|---|
| 377 | | wi_uinteger_t encrypted_length, decrypted_length; |
|---|
| 378 | | |
|---|
| 379 | | encrypted_buffer = wi_data_bytes(encrypted_data); |
|---|
| 380 | | encrypted_length = wi_data_length(encrypted_data); |
|---|
| 381 | | |
|---|
| 382 | | if(!wi_rsa_decrypt_bytes(rsa, encrypted_buffer, encrypted_length, &decrypted_buffer, &decrypted_length)) |
|---|
| 383 | | return NULL; |
|---|
| 384 | | |
|---|
| 385 | | return wi_data_with_bytes_no_copy(decrypted_buffer, decrypted_length, true); |
|---|
| 386 | | } |
|---|
| 387 | | |
|---|
| 388 | | |
|---|
| 389 | | |
|---|
| 390 | | wi_boolean_t wi_rsa_decrypt_bytes(wi_rsa_t *rsa, const void *encrypted_buffer, wi_uinteger_t encrypted_length, void **out_buffer, wi_uinteger_t *out_length) { |
|---|
| 391 | | void *decrypted_buffer; |
|---|
| 392 | | int32_t decrypted_length; |
|---|
| 393 | | |
|---|
| 394 | | decrypted_buffer = wi_malloc(RSA_size(rsa->rsa)); |
|---|
| 395 | | decrypted_length = RSA_private_decrypt(encrypted_length, encrypted_buffer, decrypted_buffer, rsa->rsa, RSA_PKCS1_PADDING); |
|---|
| 396 | | |
|---|
| 397 | | if(decrypted_length == -1) { |
|---|
| 398 | | wi_error_set_openssl_error(); |
|---|
| 399 | | |
|---|
| 400 | | wi_free(decrypted_buffer); |
|---|
| 401 | | |
|---|
| 402 | | return false; |
|---|
| 403 | | } |
|---|
| 404 | | |
|---|
| 405 | | *out_buffer = decrypted_buffer; |
|---|
| 406 | | *out_length = decrypted_length; |
|---|
| 407 | | |
|---|
| 408 | | return true; |
|---|
| 409 | | } |
|---|
| 410 | | |
|---|
| 411 | | |
|---|
| 412 | | |
|---|
| 413 | | #pragma mark - |
|---|
| 414 | | |
|---|
| 415 | | wi_runtime_id_t wi_x509_runtime_id(void) { |
|---|
| 416 | | return _wi_x509_runtime_id; |
|---|
| 417 | | } |
|---|
| 418 | | |
|---|
| 419 | | |
|---|
| 420 | | |
|---|
| 421 | | #pragma mark - |
|---|
| 422 | | |
|---|
| 423 | | wi_x509_t * wi_x509_alloc(void) { |
|---|
| 424 | | return wi_runtime_create_instance(_wi_x509_runtime_id, sizeof(wi_x509_t)); |
|---|
| 425 | | } |
|---|
| 426 | | |
|---|
| 427 | | |
|---|
| 428 | | |
|---|
| 429 | | |
|---|
| 430 | | wi_x509_t * wi_x509_init_with_common_name(wi_x509_t *x509, wi_rsa_t *rsa, wi_string_t *common_name) { |
|---|
| 431 | | X509_REQ *req; |
|---|
| 432 | | EVP_PKEY *pkey = NULL; |
|---|
| 433 | | X509_NAME *name = NULL; |
|---|
| 434 | | BIGNUM *bn = NULL; |
|---|
| 435 | | |
|---|
| 436 | | x509->common_name = wi_retain(common_name); |
|---|
| 437 | | |
|---|
| 438 | | req = X509_REQ_new(); |
|---|
| 439 | | |
|---|
| 440 | | if(!req) |
|---|
| 441 | | goto err; |
|---|
| 442 | | |
|---|
| 443 | | if(X509_REQ_set_version(req, 0) != 1) |
|---|
| 444 | | goto err; |
|---|
| 445 | | |
|---|
| 446 | | name = X509_NAME_new(); |
|---|
| 447 | | |
|---|
| 448 | | if(X509_NAME_add_entry_by_NID(name, |
|---|
| 449 | | NID_commonName, |
|---|
| 450 | | MBSTRING_ASC, |
|---|
| 451 | | (unsigned char *) wi_string_cstring(common_name), |
|---|
| 452 | | -1, |
|---|
| 453 | | -1, |
|---|
| 454 | | 0) != 1) |
|---|
| 455 | | goto err; |
|---|
| 456 | | |
|---|
| 457 | | if(X509_REQ_set_subject_name(req, name) != 1) |
|---|
| 458 | | goto err; |
|---|
| 459 | | |
|---|
| 460 | | pkey = EVP_PKEY_new(); |
|---|
| 461 | | EVP_PKEY_set1_RSA(pkey, rsa->rsa); |
|---|
| 462 | | |
|---|
| 463 | | if(X509_REQ_set_pubkey(req, pkey) != 1) |
|---|
| 464 | | goto err; |
|---|
| 465 | | |
|---|
| 466 | | x509->x509 = X509_new(); |
|---|
| 467 | | |
|---|
| 468 | | if(!x509->x509) |
|---|
| 469 | | goto err; |
|---|
| 470 | | |
|---|
| 471 | | bn = BN_new(); |
|---|
| 472 | | |
|---|
| 473 | | if(!bn) |
|---|
| 474 | | goto err; |
|---|
| 475 | | |
|---|
| 476 | | if(BN_pseudo_rand(bn, 64, 0, 0) != 1) |
|---|
| 477 | | goto err; |
|---|
| 478 | | |
|---|
| 479 | | if(!BN_to_ASN1_INTEGER(bn, X509_get_serialNumber(x509->x509))) |
|---|
| 480 | | goto err; |
|---|
| 481 | | |
|---|
| 482 | | if(X509_set_issuer_name(x509->x509, X509_REQ_get_subject_name(req)) != 1) |
|---|
| 483 | | goto err; |
|---|
| 484 | | |
|---|
| 485 | | if(!X509_gmtime_adj(X509_get_notBefore(x509->x509), 0)) |
|---|
| 486 | | goto err; |
|---|
| 487 | | |
|---|
| 488 | | if(!X509_gmtime_adj(X509_get_notAfter(x509->x509), 3600 * 24 * 365)) |
|---|
| 489 | | goto err; |
|---|
| 490 | | |
|---|
| 491 | | if(X509_set_subject_name(x509->x509, X509_REQ_get_subject_name(req)) != 1) |
|---|
| 492 | | goto end; |
|---|
| 493 | | |
|---|
| 494 | | if(X509_set_pubkey(x509->x509, pkey) != 1) |
|---|
| 495 | | goto err; |
|---|
| 496 | | |
|---|
| 497 | | if(X509_sign(x509->x509, pkey, EVP_sha1()) == 0) |
|---|
| 498 | | goto err; |
|---|
| 499 | | |
|---|
| 500 | | goto end; |
|---|
| 501 | | |
|---|
| 502 | | err: |
|---|
| 503 | | wi_error_set_openssl_error(); |
|---|
| 504 | | |
|---|
| 505 | | wi_release(x509); |
|---|
| 506 | | |
|---|
| 507 | | x509 = NULL; |
|---|
| 508 | | |
|---|
| 509 | | end: |
|---|
| 510 | | if(req) |
|---|
| 511 | | X509_REQ_free(req); |
|---|
| 512 | | |
|---|
| 513 | | if(pkey) |
|---|
| 514 | | EVP_PKEY_free(pkey); |
|---|
| 515 | | |
|---|
| 516 | | if(name) |
|---|
| 517 | | X509_NAME_free(name); |
|---|
| 518 | | |
|---|
| 519 | | if(bn) |
|---|
| 520 | | BN_free(bn); |
|---|
| 521 | | |
|---|
| 522 | | return x509; |
|---|
| 523 | | } |
|---|
| 524 | | |
|---|
| 525 | | |
|---|
| 526 | | |
|---|
| 527 | | wi_x509_t * wi_x509_init_with_pem_file(wi_x509_t *x509, wi_string_t *path) { |
|---|
| 528 | | FILE *fp; |
|---|
| 529 | | |
|---|
| 530 | | fp = fopen(wi_string_cstring(path), "r"); |
|---|
| 531 | | |
|---|
| 532 | | if(!fp) { |
|---|
| 533 | | wi_error_set_errno(errno); |
|---|
| 534 | | |
|---|
| 535 | | wi_release(x509); |
|---|
| 536 | | |
|---|
| 537 | | return NULL; |
|---|
| 538 | | } |
|---|
| 539 | | |
|---|
| 540 | | x509->x509 = PEM_read_X509(fp, NULL, NULL, NULL); |
|---|
| 541 | | |
|---|
| 542 | | fclose(fp); |
|---|
| 543 | | |
|---|
| 544 | | if(!x509->x509) { |
|---|
| 545 | | wi_error_set_openssl_error(); |
|---|
| 546 | | |
|---|
| 547 | | wi_release(x509); |
|---|
| 548 | | |
|---|
| 549 | | return NULL; |
|---|
| 550 | | } |
|---|
| 551 | | |
|---|
| 552 | | return x509; |
|---|
| 553 | | } |
|---|
| 554 | | |
|---|
| 555 | | |
|---|
| 556 | | |
|---|
| 557 | | static void _wi_x509_dealloc(wi_runtime_instance_t *instance) { |
|---|
| 558 | | wi_x509_t *x509 = instance; |
|---|
| 559 | | |
|---|
| 560 | | X509_free(x509->x509); |
|---|
| 561 | | |
|---|
| 562 | | wi_release(x509->common_name); |
|---|
| 563 | | } |
|---|
| 564 | | |
|---|
| 565 | | |
|---|
| 566 | | |
|---|
| 567 | | static wi_string_t * _wi_x509_description(wi_runtime_instance_t *instance) { |
|---|
| 568 | | wi_x509_t *x509 = instance; |
|---|
| 569 | | |
|---|
| 570 | | return wi_string_with_format(WI_STR("<%@ %p>{x509 = %p, name = %@}"), |
|---|
| 571 | | wi_runtime_class_name(x509), |
|---|
| 572 | | x509, |
|---|
| 573 | | x509->x509, |
|---|
| 574 | | x509->common_name); |
|---|
| 575 | | } |
|---|
| 576 | | |
|---|
| 577 | | |
|---|
| 578 | | |
|---|
| 579 | | #pragma mark - |
|---|
| 580 | | |
|---|
| 581 | | void * wi_x509_x509(wi_x509_t *x509) { |
|---|
| 582 | | return x509->x509; |
|---|
| | 77 | void wi_cipher_initialize(void) { |
|---|