dump_rsa.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. */
  5. /*
  6. * C port of DumpPublicKey.java from the Android Open source project with
  7. * support for additional RSA key sizes. (platform/system/core,git/libmincrypt
  8. * /tools/DumpPublicKey.java). Uses the OpenSSL X509 and BIGNUM library.
  9. */
  10. #include <openssl/pem.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. /*
  15. * Command line tool to extract RSA public keys from X.509 certificates and
  16. * output a pre-processed version of keys for use by RSA verification routines.
  17. */
  18. int check(RSA *key)
  19. {
  20. int public_exponent = BN_get_word(key->e);
  21. int modulus = BN_num_bits(key->n);
  22. if (public_exponent != 65537 && public_exponent != 3) {
  23. fprintf(stderr, "WARNING: Non-standard public exponent %d.\n",
  24. public_exponent);
  25. }
  26. if (modulus != 1024 && modulus != 2048 && modulus != 3072 &&
  27. modulus != 4096 && modulus != 8192) {
  28. fprintf(stderr, "WARNING: Non-standard modulus length = %d.\n",
  29. modulus);
  30. }
  31. return 1;
  32. }
  33. /**
  34. * Pre-processes and outputs RSA public key to standard output.
  35. */
  36. void output(RSA *key)
  37. {
  38. BIGNUM *N;
  39. BIGNUM *Big1 = NULL, *Big2 = NULL, *Big32 = NULL, *BigMinus1 = NULL;
  40. BIGNUM *B = NULL;
  41. BIGNUM *N0inv= NULL, *R = NULL, *RR = NULL, *RRTemp = NULL;
  42. BIGNUM *NnumBits = NULL;
  43. BIGNUM *n = NULL, *rr = NULL;
  44. BN_CTX *bn_ctx = BN_CTX_new();
  45. uint32_t n0invout;
  46. int nwords, i;
  47. N = key->n;
  48. /* Output size of RSA key in 32-bit words */
  49. nwords = BN_num_bits(N) / 32;
  50. if (-1 == write(1, &nwords, sizeof(nwords)))
  51. goto failure;
  52. /* Initialize BIGNUMs */
  53. Big1 = BN_new();
  54. Big2 = BN_new();
  55. Big32 = BN_new();
  56. BigMinus1 = BN_new();
  57. N0inv= BN_new();
  58. R = BN_new();
  59. RR = BN_new();
  60. RRTemp = BN_new();
  61. NnumBits = BN_new();
  62. n = BN_new();
  63. rr = BN_new();
  64. BN_set_word(Big1, 1L);
  65. BN_set_word(Big2, 2L);
  66. BN_set_word(Big32, 32L);
  67. BN_sub(BigMinus1, Big1, Big2);
  68. B = BN_new();
  69. BN_exp(B, Big2, Big32, bn_ctx); /* B = 2^32 */
  70. /* Calculate and output N0inv = -1 / N[0] mod 2^32 */
  71. BN_mod_inverse(N0inv, N, B, bn_ctx);
  72. BN_sub(N0inv, B, N0inv);
  73. n0invout = BN_get_word(N0inv);
  74. if (-1 == write(1, &n0invout, sizeof(n0invout)))
  75. goto failure;
  76. /* Calculate R = 2^(# of key bits) */
  77. BN_set_word(NnumBits, BN_num_bits(N));
  78. BN_exp(R, Big2, NnumBits, bn_ctx);
  79. /* Calculate RR = R^2 mod N */
  80. BN_copy(RR, R);
  81. BN_mul(RRTemp, RR, R, bn_ctx);
  82. BN_mod(RR, RRTemp, N, bn_ctx);
  83. /* Write out modulus as little endian array of integers. */
  84. for (i = 0; i < nwords; ++i) {
  85. uint32_t nout;
  86. BN_mod(n, N, B, bn_ctx); /* n = N mod B */
  87. nout = BN_get_word(n);
  88. if (-1 == write(1, &nout, sizeof(nout)))
  89. goto failure;
  90. BN_rshift(N, N, 32); /* N = N/B */
  91. }
  92. /* Write R^2 as little endian array of integers. */
  93. for (i = 0; i < nwords; ++i) {
  94. uint32_t rrout;
  95. BN_mod(rr, RR, B, bn_ctx); /* rr = RR mod B */
  96. rrout = BN_get_word(rr);
  97. if (-1 == write(1, &rrout, sizeof(rrout)))
  98. goto failure;
  99. BN_rshift(RR, RR, 32); /* RR = RR/B */
  100. }
  101. failure:
  102. /* Free BIGNUMs. */
  103. BN_free(Big1);
  104. BN_free(Big2);
  105. BN_free(Big32);
  106. BN_free(BigMinus1);
  107. BN_free(N0inv);
  108. BN_free(R);
  109. BN_free(RRTemp);
  110. BN_free(NnumBits);
  111. BN_free(n);
  112. BN_free(rr);
  113. }
  114. int main(int argc, char* argv[]) {
  115. int cert_mode = 0;
  116. FILE* fp;
  117. X509* cert = NULL;
  118. RSA* pubkey = NULL;
  119. EVP_PKEY* key;
  120. char *progname;
  121. if (argc != 3 ||
  122. (strcmp(argv[1], "-cert") && strcmp(argv[1], "-pub"))) {
  123. progname = strrchr(argv[0], '/');
  124. if (progname)
  125. progname++;
  126. else
  127. progname = argv[0];
  128. fprintf(stderr, "Usage: %s <-cert | -pub> <file>\n", progname);
  129. return -1;
  130. }
  131. if (!strcmp(argv[1], "-cert"))
  132. cert_mode = 1;
  133. fp = fopen(argv[2], "r");
  134. if (!fp) {
  135. fprintf(stderr, "Couldn't open file %s!\n", argv[2]);
  136. return -1;
  137. }
  138. if (cert_mode) {
  139. /* Read the certificate */
  140. if (!PEM_read_X509(fp, &cert, NULL, NULL)) {
  141. fprintf(stderr, "Couldn't read certificate.\n");
  142. goto fail;
  143. }
  144. /* Get the public key from the certificate. */
  145. key = X509_get_pubkey(cert);
  146. /* Convert to a RSA_style key. */
  147. if (!(pubkey = EVP_PKEY_get1_RSA(key))) {
  148. fprintf(stderr, "Couldn't convert to RSA style key.\n");
  149. goto fail;
  150. }
  151. } else {
  152. /* Read the pubkey in .PEM format. */
  153. if (!(pubkey = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL))) {
  154. fprintf(stderr, "Couldn't read public key file.\n");
  155. goto fail;
  156. }
  157. }
  158. if (check(pubkey)) {
  159. output(pubkey);
  160. }
  161. fail:
  162. X509_free(cert);
  163. RSA_free(pubkey);
  164. fclose(fp);
  165. return 0;
  166. }