gen.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * \file gen.c
  3. *
  4. * Generate a fake RSA certificate file, given as input e, d, p, q.
  5. */
  6. #define _GNU_SOURCE
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <openssl/bn.h>
  12. #include <openssl/ssl.h>
  13. #include <openssl/rsa.h>
  14. static void
  15. usage(int ret)
  16. {
  17. static const char* help_message = "%s usage: \n"
  18. "%s pub [-n MODULUS | -p PRIME -q PRIME] -e PUBLIC_EXPONENT\n"
  19. "%s priv -p PRIME -q PRIME -e PUBLIC_EXPONENT -d PRIVATE_EXPONENT\n";
  20. fprintf(stderr, help_message, program_invocation_short_name,
  21. program_invocation_name, program_invocation_name);
  22. exit(ret);
  23. }
  24. static int
  25. pubkey_generation(RSA* rsa)
  26. {
  27. BN_CTX *ctx = BN_CTX_new();
  28. /* we need <N, e> to get a valid public key. */
  29. if (!(rsa->e &&
  30. (rsa->n ||(rsa->p && rsa->q)))) {
  31. fprintf(stderr, "Not enough parameter for the public key generation!\n");
  32. exit(EXIT_FAILURE);
  33. }
  34. if (!rsa->n) {
  35. rsa->n = BN_new();
  36. BN_mul(rsa->n, rsa->p, rsa->q, ctx);
  37. }
  38. PEM_write_RSAPublicKey(stdout, rsa);
  39. BN_CTX_free(ctx);
  40. RSA_free(rsa);
  41. return EXIT_SUCCESS;
  42. }
  43. static int
  44. privkey_generation(RSA *rsa)
  45. {
  46. BIGNUM *p1, *q1;
  47. BN_CTX *ctx = BN_CTX_new();
  48. if (!(rsa->p && rsa->q && rsa->e && rsa->d)) {
  49. fprintf(stderr, "Not enough parameter for the private key generation!\n");
  50. return EXIT_FAILURE;
  51. }
  52. p1 = BN_new();
  53. q1 = BN_new();
  54. rsa->n = BN_new();
  55. rsa->iqmp = BN_new();
  56. rsa->dmp1 = BN_new();
  57. rsa->dmq1 = BN_new();
  58. /* generating RSA key */
  59. BN_mul(rsa->n, rsa->p, rsa->q, ctx);
  60. BN_sub(p1, rsa->p, BN_value_one());
  61. BN_sub(q1, rsa->q, BN_value_one());
  62. BN_mod(rsa->dmq1, rsa->d, q1, ctx);
  63. BN_mod(rsa->dmp1, rsa->d, p1, ctx);
  64. BN_mod_inverse(rsa->iqmp, rsa->q, rsa->p, ctx);
  65. PEM_write_RSAPrivateKey(stdout, rsa, NULL, NULL, 0, NULL, NULL);
  66. BN_CTX_free(ctx);
  67. return EXIT_SUCCESS;
  68. }
  69. int main(int argc, char **argv)
  70. {
  71. int opt;
  72. RSA *rsa = RSA_new();
  73. rsa->n = rsa->e = rsa->p = rsa->q = NULL;
  74. if (argc < 3) usage(EXIT_FAILURE);
  75. while ((opt = getopt(argc-1, argv+1, "d:e:N:n:p:q:")) != -1) {
  76. switch (opt) {
  77. case 'N':
  78. case 'n':
  79. if (!BN_dec2bn(&rsa->n, optarg)) usage(EXIT_FAILURE);
  80. break;
  81. case 'd':
  82. if (!BN_dec2bn(&rsa->d, optarg)) usage(EXIT_FAILURE);
  83. break;
  84. case 'e':
  85. if (!BN_dec2bn(&rsa->e, optarg)) usage(EXIT_FAILURE);
  86. break;
  87. case 'p':
  88. if (!BN_dec2bn(&rsa->p, optarg)) usage(EXIT_FAILURE);
  89. break;
  90. case 'q':
  91. if (!BN_dec2bn(&rsa->q, optarg)) usage(EXIT_FAILURE);
  92. break;
  93. default:
  94. usage(EXIT_FAILURE);
  95. }
  96. }
  97. if (!strcmp(argv[1], "pub"))
  98. return pubkey_generation(rsa);
  99. else if (!strcmp(argv[1], "priv"))
  100. return privkey_generation(rsa);
  101. else
  102. usage(EXIT_FAILURE);
  103. /* creating public key */
  104. /*
  105. EVP_PKEY *pk;
  106. pk = EVP_PKEY_new();
  107. EVP_PKEY_set1_RSA(pk, rsa);
  108. */
  109. /* creating dummy certificate */
  110. /*
  111. X509* crt;
  112. crt = X509_new();
  113. if (!X509_set_pubkey(crt, pk)) exit(EXIT_FAILURE);
  114. */
  115. /* PEM_write_X509(stdout, crt); */
  116. /*
  117. X509_free(crt);
  118. EVP_PKEY_free(pk);
  119. BN_free(q1);
  120. BN_free(p1);
  121. RSA_free(rsa);
  122. */
  123. return EXIT_SUCCESS;
  124. }