| 481 | } |
| 482 | |
| 483 | int MakeX509CSR( |
| 484 | const String& cn, |
| 485 | const String& keyfile, |
| 486 | const String& csrfile, |
| 487 | const String& certfile, |
| 488 | long validFrom, |
| 489 | long validFor, |
| 490 | bool ca |
| 491 | ) |
| 492 | { |
| 493 | char errbuf[256]; |
| 494 | |
| 495 | InitializeOpenSSL(); |
| 496 | |
| 497 | RSA *rsa = RSA_new(); |
| 498 | BIGNUM *e = BN_new(); |
| 499 | |
| 500 | if (!rsa || !e) { |
| 501 | ERR_error_string_n(ERR_peek_error(), errbuf, sizeof errbuf); |
| 502 | Log(LogCritical, "SSL") |
| 503 | << "Error while creating RSA key: " << ERR_peek_error() << ", \"" << errbuf << "\""; |
| 504 | BOOST_THROW_EXCEPTION(openssl_error() |
| 505 | << boost::errinfo_api_function("RSA_generate_key") |
| 506 | << errinfo_openssl_error(ERR_peek_error())); |
| 507 | } |
| 508 | |
| 509 | BN_set_word(e, RSA_F4); |
| 510 | |
| 511 | if (!RSA_generate_key_ex(rsa, 4096, e, nullptr)) { |
| 512 | ERR_error_string_n(ERR_peek_error(), errbuf, sizeof errbuf); |
| 513 | Log(LogCritical, "SSL") |
| 514 | << "Error while creating RSA key: " << ERR_peek_error() << ", \"" << errbuf << "\""; |
| 515 | BOOST_THROW_EXCEPTION(openssl_error() |
| 516 | << boost::errinfo_api_function("RSA_generate_key") |
| 517 | << errinfo_openssl_error(ERR_peek_error())); |
| 518 | } |
| 519 | |
| 520 | BN_free(e); |
| 521 | |
| 522 | Log(LogInformation, "base") |
| 523 | << "Writing private key to '" << keyfile << "'."; |
| 524 | |
| 525 | BIO *bio = BIO_new_file(const_cast<char *>(keyfile.CStr()), "w"); |
| 526 | |
| 527 | if (!bio) { |
| 528 | ERR_error_string_n(ERR_peek_error(), errbuf, sizeof errbuf); |
| 529 | Log(LogCritical, "SSL") |
| 530 | << "Error while opening private RSA key file '" << keyfile << "': " << ERR_peek_error() << ", \"" << errbuf << "\""; |
| 531 | BOOST_THROW_EXCEPTION(openssl_error() |
| 532 | << boost::errinfo_api_function("BIO_new_file") |
| 533 | << errinfo_openssl_error(ERR_peek_error()) |
| 534 | << boost::errinfo_file_name(keyfile)); |
| 535 | } |
| 536 | |
| 537 | if (!PEM_write_bio_RSAPrivateKey(bio, rsa, nullptr, nullptr, 0, nullptr, nullptr)) { |
| 538 | ERR_error_string_n(ERR_peek_error(), errbuf, sizeof errbuf); |
| 539 | Log(LogCritical, "SSL") |
| 540 | << "Error while writing private RSA key to file '" << keyfile << "': " << ERR_peek_error() << ", \"" << errbuf << "\""; |
no test coverage detected