Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

70 wiersze
2.2 KiB

  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/bio.h>
  15. #include <openssl/bn.h>
  16. #include <openssl/err.h>
  17. #include <openssl/pem.h>
  18. #include <openssl/rsa.h>
  19. #include "../crypto/test/scoped_types.h"
  20. #include "internal.h"
  21. static const struct argument kArguments[] = {
  22. {
  23. "-nprimes", kOptionalArgument,
  24. "The number of primes to generate (default: 2)",
  25. },
  26. {
  27. "-bits", kOptionalArgument,
  28. "The number of bits in the modulus (default: 2048)",
  29. },
  30. {
  31. "", kOptionalArgument, "",
  32. },
  33. };
  34. bool GenerateRSAKey(const std::vector<std::string> &args) {
  35. std::map<std::string, std::string> args_map;
  36. if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
  37. PrintUsage(kArguments);
  38. return false;
  39. }
  40. unsigned bits, nprimes = 0;
  41. if (!GetUnsigned(&bits, "-bits", 2048, args_map) ||
  42. !GetUnsigned(&nprimes, "-nprimes", 2, args_map)) {
  43. PrintUsage(kArguments);
  44. return false;
  45. }
  46. ScopedRSA rsa(RSA_new());
  47. ScopedBIGNUM e(BN_new());
  48. ScopedBIO bio(BIO_new_fp(stdout, BIO_NOCLOSE));
  49. if (!BN_set_word(e.get(), RSA_F4) ||
  50. !RSA_generate_multi_prime_key(rsa.get(), bits, nprimes, e.get(), NULL) ||
  51. !PEM_write_bio_RSAPrivateKey(bio.get(), rsa.get(), NULL /* cipher */,
  52. NULL /* key */, 0 /* key len */,
  53. NULL /* password callback */,
  54. NULL /* callback arg */)) {
  55. ERR_print_errors_fp(stderr);
  56. return false;
  57. }
  58. return true;
  59. }