OLD | NEW |
1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 /* C port of DumpPublicKey.java from the Android Open source project with | 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 | 7 * support for additional RSA key sizes. (platform/system/core,git/libmincrypt |
8 * /tools/DumpPublicKey.java). Uses the OpenSSL X509 and BIGNUM library. | 8 * /tools/DumpPublicKey.java). Uses the OpenSSL X509 and BIGNUM library. |
9 */ | 9 */ |
10 | 10 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 BN_free(N0inv); | 129 BN_free(N0inv); |
130 BN_free(R); | 130 BN_free(R); |
131 BN_free(RRTemp); | 131 BN_free(RRTemp); |
132 BN_free(NnumBits); | 132 BN_free(NnumBits); |
133 BN_free(n); | 133 BN_free(n); |
134 BN_free(rr); | 134 BN_free(rr); |
135 | 135 |
136 } | 136 } |
137 | 137 |
138 int main(int argc, char* argv[]) { | 138 int main(int argc, char* argv[]) { |
| 139 int cert_mode = 0; |
139 FILE* fp; | 140 FILE* fp; |
140 X509* cert = NULL; | 141 X509* cert = NULL; |
141 RSA* pubkey = NULL; | 142 RSA* pubkey = NULL; |
142 EVP_PKEY* key; | 143 EVP_PKEY* key; |
143 | 144 |
144 if (argc != 2) { | 145 if (argc != 3 || (strcmp(argv[1], "-cert") && strcmp(argv[1], "-pub"))) { |
145 fprintf(stderr, "Usage: %s <certfile>\n", argv[0]); | 146 fprintf(stderr, "Usage: %s <-cert | -pub> <file>\n", argv[0]); |
146 return -1; | 147 return -1; |
147 } | 148 } |
148 | 149 |
149 fp = fopen(argv[1], "r"); | 150 if (!strcmp(argv[1], "-cert")) |
| 151 cert_mode = 1; |
| 152 |
| 153 fp = fopen(argv[2], "r"); |
150 | 154 |
151 if (!fp) { | 155 if (!fp) { |
152 fprintf(stderr, "Couldn't open certificate file!\n"); | 156 fprintf(stderr, "Couldn't open file %s!\n", argv[2]); |
153 return -1; | 157 return -1; |
154 } | 158 } |
155 | 159 |
156 /* Read the certificate */ | 160 if (cert_mode) { |
157 if (!PEM_read_X509(fp, &cert, NULL, NULL)) { | 161 /* Read the certificate */ |
158 fprintf(stderr, "Couldn't read certificate.\n"); | 162 if (!PEM_read_X509(fp, &cert, NULL, NULL)) { |
159 goto fail; | 163 fprintf(stderr, "Couldn't read certificate.\n"); |
160 } | 164 goto fail; |
| 165 } |
161 | 166 |
162 /* Get the public key from the certificate. */ | 167 /* Get the public key from the certificate. */ |
163 key = X509_get_pubkey(cert); | 168 key = X509_get_pubkey(cert); |
164 | 169 |
165 /* Convert to a RSA_style key. */ | 170 /* Convert to a RSA_style key. */ |
166 if (!(pubkey = EVP_PKEY_get1_RSA(key))) { | 171 if (!(pubkey = EVP_PKEY_get1_RSA(key))) { |
167 fprintf(stderr, "Couldn't convert to a RSA style key.\n"); | 172 fprintf(stderr, "Couldn't convert to a RSA style key.\n"); |
168 goto fail; | 173 goto fail; |
| 174 } |
| 175 } else { |
| 176 /* Read the pubkey in .PEM format. */ |
| 177 if (!(pubkey = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL))) { |
| 178 fprintf(stderr, "Couldn't read public key file.\n"); |
| 179 goto fail; |
| 180 } |
169 } | 181 } |
170 | 182 |
171 if (check(pubkey)) { | 183 if (check(pubkey)) { |
172 output (pubkey); | 184 output(pubkey); |
173 } | 185 } |
174 | 186 |
175 fail: | 187 fail: |
176 X509_free(cert); | 188 X509_free(cert); |
177 RSA_free(pubkey); | 189 RSA_free(pubkey); |
178 fclose(fp); | 190 fclose(fp); |
179 | 191 |
180 return 0; | 192 return 0; |
181 } | 193 } |
OLD | NEW |