Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 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 | |
Gaurav Shah | ed9c96a | 2010-03-30 18:56:07 -0700 | [diff] [blame] | 11 | #include <stdint.h> |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 12 | #include <openssl/bn.h> |
| 13 | #include <openssl/evp.h> |
| 14 | #include <openssl/pem.h> |
| 15 | #include <openssl/x509.h> |
| 16 | #include <string.h> |
| 17 | #include <unistd.h> |
| 18 | |
| 19 | /* Command line tool to extract RSA public keys from X.509 certificates |
| 20 | * and output a pre-processed version of keys for use by RSA verification |
| 21 | * routines. |
| 22 | */ |
| 23 | |
| 24 | int check(RSA* key) { |
| 25 | int public_exponent = BN_get_word(key->e); |
| 26 | int modulus = BN_num_bits(key->n); |
| 27 | |
| 28 | if (public_exponent != 65537) { |
| 29 | fprintf(stderr, "WARNING: Public exponent should be 65537 (but is %d).\n", |
| 30 | public_exponent); |
| 31 | } |
| 32 | |
| 33 | if (modulus != 1024 && modulus != 2048 && modulus != 4096 |
| 34 | && modulus != 8192) { |
| 35 | fprintf(stderr, "ERROR: Unknown modulus length = %d.\n", modulus); |
| 36 | return 0; |
| 37 | } |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | /* Pre-processes and outputs RSA public key to standard out. |
| 42 | */ |
| 43 | void output(RSA* key) { |
| 44 | int i, nwords; |
| 45 | BIGNUM *N = key->n; |
Gaurav Shah | 48ed9b8 | 2010-03-11 16:31:35 -0800 | [diff] [blame] | 46 | BIGNUM *Big1 = NULL, *Big2 = NULL, *Big32 = NULL, *BigMinus1 = NULL; |
| 47 | BIGNUM *B = NULL; |
| 48 | BIGNUM *N0inv= NULL, *R = NULL, *RR = NULL, *RRTemp = NULL, *NnumBits = NULL; |
| 49 | BIGNUM *n = NULL, *rr = NULL; |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 50 | BN_CTX *bn_ctx = BN_CTX_new(); |
| 51 | uint32_t n0invout; |
| 52 | |
| 53 | N = key->n; |
| 54 | /* Output size of RSA key in 32-bit words */ |
| 55 | nwords = BN_num_bits(N) / 32; |
Gaurav Shah | 16ca324 | 2010-03-11 13:41:25 -0800 | [diff] [blame] | 56 | if (-1 == write(1, &nwords, sizeof(nwords))) |
| 57 | goto failure; |
| 58 | |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 59 | |
| 60 | /* Initialize BIGNUMs */ |
| 61 | Big1 = BN_new(); |
| 62 | Big2 = BN_new(); |
| 63 | Big32 = BN_new(); |
| 64 | BigMinus1 = BN_new(); |
| 65 | N0inv= BN_new(); |
| 66 | R = BN_new(); |
| 67 | RR = BN_new(); |
| 68 | RRTemp = BN_new(); |
| 69 | NnumBits = BN_new(); |
| 70 | n = BN_new(); |
| 71 | rr = BN_new(); |
| 72 | |
| 73 | |
| 74 | BN_set_word(Big1, 1L); |
| 75 | BN_set_word(Big2, 2L); |
| 76 | BN_set_word(Big32, 32L); |
| 77 | BN_sub(BigMinus1, Big1, Big2); |
| 78 | |
| 79 | B = BN_new(); |
| 80 | BN_exp(B, Big2, Big32, bn_ctx); /* B = 2^32 */ |
| 81 | |
| 82 | /* Calculate and output N0inv = -1 / N[0] mod 2^32 */ |
| 83 | BN_mod_inverse(N0inv, N, B, bn_ctx); |
| 84 | BN_sub(N0inv, B, N0inv); |
| 85 | n0invout = BN_get_word(N0inv); |
Gaurav Shah | 16ca324 | 2010-03-11 13:41:25 -0800 | [diff] [blame] | 86 | if (-1 == write(1, &n0invout, sizeof(n0invout))) |
| 87 | goto failure; |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 88 | |
| 89 | /* Calculate R = 2^(# of key bits) */ |
| 90 | BN_set_word(NnumBits, BN_num_bits(N)); |
| 91 | BN_exp(R, Big2, NnumBits, bn_ctx); |
| 92 | |
| 93 | /* Calculate RR = R^2 mod N */ |
| 94 | BN_copy(RR, R); |
| 95 | BN_mul(RRTemp, RR, R, bn_ctx); |
| 96 | BN_mod(RR, RRTemp, N, bn_ctx); |
| 97 | |
| 98 | |
| 99 | /* Write out modulus as little endian array of integers. */ |
| 100 | for (i = 0; i < nwords; ++i) { |
| 101 | uint32_t nout; |
| 102 | |
| 103 | BN_mod(n, N, B, bn_ctx); /* n = N mod B */ |
| 104 | nout = BN_get_word(n); |
Gaurav Shah | 16ca324 | 2010-03-11 13:41:25 -0800 | [diff] [blame] | 105 | if (-1 == write(1, &nout, sizeof(nout))) |
| 106 | goto failure; |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 107 | |
| 108 | BN_rshift(N, N, 32); /* N = N/B */ |
| 109 | } |
| 110 | |
| 111 | /* Write R^2 as little endian array of integers. */ |
| 112 | for (i = 0; i < nwords; ++i) { |
| 113 | uint32_t rrout; |
| 114 | |
| 115 | BN_mod(rr, RR, B, bn_ctx); /* rr = RR mod B */ |
| 116 | rrout = BN_get_word(rr); |
Gaurav Shah | 16ca324 | 2010-03-11 13:41:25 -0800 | [diff] [blame] | 117 | if (-1 == write(1, &rrout, sizeof(rrout))) |
| 118 | goto failure; |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 119 | |
| 120 | BN_rshift(RR, RR, 32); /* RR = RR/B */ |
| 121 | } |
| 122 | |
Gaurav Shah | 16ca324 | 2010-03-11 13:41:25 -0800 | [diff] [blame] | 123 | failure: |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 124 | /* Free BIGNUMs. */ |
| 125 | BN_free(Big1); |
| 126 | BN_free(Big2); |
| 127 | BN_free(Big32); |
| 128 | BN_free(BigMinus1); |
| 129 | BN_free(N0inv); |
| 130 | BN_free(R); |
| 131 | BN_free(RRTemp); |
| 132 | BN_free(NnumBits); |
| 133 | BN_free(n); |
| 134 | BN_free(rr); |
| 135 | |
| 136 | } |
| 137 | |
| 138 | int main(int argc, char* argv[]) { |
Gaurav Shah | 551037b | 2010-11-01 13:33:32 -0700 | [diff] [blame] | 139 | int cert_mode = 0; |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 140 | FILE* fp; |
| 141 | X509* cert = NULL; |
| 142 | RSA* pubkey = NULL; |
| 143 | EVP_PKEY* key; |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 144 | char *progname; |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 145 | |
Gaurav Shah | 551037b | 2010-11-01 13:33:32 -0700 | [diff] [blame] | 146 | if (argc != 3 || (strcmp(argv[1], "-cert") && strcmp(argv[1], "-pub"))) { |
Bill Richardson | feb2518 | 2013-03-07 12:54:29 -0800 | [diff] [blame] | 147 | progname = strrchr(argv[0], '/'); |
| 148 | if (progname) |
| 149 | progname++; |
| 150 | else |
| 151 | progname = argv[0]; |
| 152 | fprintf(stderr, "Usage: %s <-cert | -pub> <file>\n", progname); |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 153 | return -1; |
| 154 | } |
| 155 | |
Gaurav Shah | 551037b | 2010-11-01 13:33:32 -0700 | [diff] [blame] | 156 | if (!strcmp(argv[1], "-cert")) |
| 157 | cert_mode = 1; |
| 158 | |
| 159 | fp = fopen(argv[2], "r"); |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 160 | |
| 161 | if (!fp) { |
Gaurav Shah | 551037b | 2010-11-01 13:33:32 -0700 | [diff] [blame] | 162 | fprintf(stderr, "Couldn't open file %s!\n", argv[2]); |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 163 | return -1; |
| 164 | } |
| 165 | |
Gaurav Shah | 551037b | 2010-11-01 13:33:32 -0700 | [diff] [blame] | 166 | if (cert_mode) { |
| 167 | /* Read the certificate */ |
| 168 | if (!PEM_read_X509(fp, &cert, NULL, NULL)) { |
| 169 | fprintf(stderr, "Couldn't read certificate.\n"); |
| 170 | goto fail; |
| 171 | } |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 172 | |
Gaurav Shah | 551037b | 2010-11-01 13:33:32 -0700 | [diff] [blame] | 173 | /* Get the public key from the certificate. */ |
| 174 | key = X509_get_pubkey(cert); |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 175 | |
Gaurav Shah | 551037b | 2010-11-01 13:33:32 -0700 | [diff] [blame] | 176 | /* Convert to a RSA_style key. */ |
| 177 | if (!(pubkey = EVP_PKEY_get1_RSA(key))) { |
| 178 | fprintf(stderr, "Couldn't convert to a RSA style key.\n"); |
| 179 | goto fail; |
| 180 | } |
| 181 | } else { |
| 182 | /* Read the pubkey in .PEM format. */ |
| 183 | if (!(pubkey = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL))) { |
| 184 | fprintf(stderr, "Couldn't read public key file.\n"); |
| 185 | goto fail; |
| 186 | } |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | if (check(pubkey)) { |
Gaurav Shah | 551037b | 2010-11-01 13:33:32 -0700 | [diff] [blame] | 190 | output(pubkey); |
Gaurav Shah | 322536d | 2010-01-28 15:01:23 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | fail: |
| 194 | X509_free(cert); |
| 195 | RSA_free(pubkey); |
| 196 | fclose(fp); |
| 197 | |
| 198 | return 0; |
| 199 | } |