blob: 90f872e98abf023d1e958e2e832dff8a44e42337 [file] [log] [blame]
Chung-yih Wangeec11822009-07-02 00:22:04 +08001/*
2**
3** Copyright 2009, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "CertTool"
19
20#include <stdio.h>
Chung-yih Wangeec11822009-07-02 00:22:04 +080021#include <openssl/pem.h>
22#include <openssl/pkcs12.h>
23#include <openssl/rsa.h>
24#include <openssl/x509v3.h>
25#include <cutils/log.h>
26
27#include "cert.h"
28
29static PKEY_STORE pkey_store[KEYGEN_STORE_SIZE];
30static int store_index = 0;
31
32static char emsg[][30] = {
33 "",
34 STR(ERR_INVALID_KEY_LENGTH),
35 STR(ERR_CONSTRUCT_NEW_DATA),
36 STR(ERR_RSA_KEYGEN),
37 STR(ERR_X509_PROCESS),
Chung-yih Wang719eba52009-07-24 11:33:45 +080038 STR(ERR_SPKAC_TOO_LONG),
39 STR(ERR_INVALID_ARGS),
Chung-yih Wangeec11822009-07-02 00:22:04 +080040};
41
Chung-yih Wang719eba52009-07-24 11:33:45 +080042static void save_in_store(EVP_PKEY *pkey)
Chung-yih Wangeec11822009-07-02 00:22:04 +080043{
44 EVP_PKEY *newpkey = EVP_PKEY_new();
45 RSA *rsa = EVP_PKEY_get1_RSA(pkey);
46 EVP_PKEY_set1_RSA(newpkey, rsa);
47 PKEY_STORE_free(pkey_store[store_index]);
Chung-yih Wangfd3db872009-07-28 18:37:13 +080048 pkey_store[store_index].key_len = i2d_RSA_PUBKEY(rsa, &pkey_store[store_index].public_key);
Chung-yih Wangeec11822009-07-02 00:22:04 +080049 pkey_store[store_index++].pkey = newpkey;
50 store_index %= KEYGEN_STORE_SIZE;
51 RSA_free(rsa);
52}
53
54static EVP_PKEY *get_pkey_from_store(X509 *cert)
55{
56 int i, key_len;
57 unsigned char *buf = NULL;
58 if ((key_len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), &buf)) == 0) {
59 return NULL;
60 }
61 for (i = 0 ; i < KEYGEN_STORE_SIZE ; ++i) {
62 if ((key_len == pkey_store[i].key_len) &&
63 memcmp(buf, pkey_store[i].public_key, key_len) == 0) {
64 break;
65 }
66 }
67 free(buf);
68 return (i == KEYGEN_STORE_SIZE) ? NULL : pkey_store[i].pkey;
69}
70
Chung-yih Wang719eba52009-07-24 11:33:45 +080071int gen_csr(int bits, const char *challenge, char reply[REPLY_MAX])
Chung-yih Wangeec11822009-07-02 00:22:04 +080072{
73 int len, ret_code = 0;
74 BIGNUM *bn = NULL;
Chung-yih Wang719eba52009-07-24 11:33:45 +080075 char *spkstr = NULL;
Chung-yih Wangeec11822009-07-02 00:22:04 +080076 EVP_PKEY *pkey = NULL;
77 RSA *rsa = NULL;
Chung-yih Wang719eba52009-07-24 11:33:45 +080078 NETSCAPE_SPKI *req = NULL;
Chung-yih Wangeec11822009-07-02 00:22:04 +080079
Chung-yih Wang719eba52009-07-24 11:33:45 +080080 if (challenge == NULL) {
81 ret_code = ERR_INVALID_ARGS;
82 goto err;
83 }
Chung-yih Wangeec11822009-07-02 00:22:04 +080084
85 if ((bits != KEYLENGTH_MEDIUM) && (bits != KEYLENGTH_MAXIMUM)) {
86 ret_code = ERR_INVALID_KEY_LENGTH;
87 goto err;
88 }
89
90 if (((pkey = EVP_PKEY_new()) == NULL) ||
Chung-yih Wang719eba52009-07-24 11:33:45 +080091 ((req = NETSCAPE_SPKI_new()) == NULL) ||
Chung-yih Wangeec11822009-07-02 00:22:04 +080092 ((rsa = RSA_new()) == NULL) || ((bn = BN_new()) == NULL)) {
93 ret_code = ERR_CONSTRUCT_NEW_DATA;
94 goto err;
95 }
96
97 if (!BN_set_word(bn, RSA_F4) ||
98 !RSA_generate_key_ex(rsa, bits, bn, NULL) ||
99 !EVP_PKEY_assign_RSA(pkey, rsa)) {
100 ret_code = ERR_RSA_KEYGEN;
101 goto err;
102 }
103
Chung-yih Wangeec11822009-07-02 00:22:04 +0800104 rsa = NULL;
Chung-yih Wang719eba52009-07-24 11:33:45 +0800105 ASN1_STRING_set(req->spkac->challenge, challenge, (int)strlen(challenge));
106 NETSCAPE_SPKI_set_pubkey(req, pkey);
107 NETSCAPE_SPKI_sign(req, pkey, EVP_md5());
108 spkstr = NETSCAPE_SPKI_b64_encode(req);
Chung-yih Wangeec11822009-07-02 00:22:04 +0800109
Chung-yih Wang719eba52009-07-24 11:33:45 +0800110 if ((strlcpy(reply, spkstr, REPLY_MAX)) < REPLY_MAX) {
111 save_in_store(pkey);
Chung-yih Wangeec11822009-07-02 00:22:04 +0800112 } else {
Chung-yih Wang719eba52009-07-24 11:33:45 +0800113 ret_code = ERR_SPKAC_TOO_LONG;
Chung-yih Wangeec11822009-07-02 00:22:04 +0800114 }
115
116err:
117 if (rsa) RSA_free(rsa);
118 if (bn) BN_free(bn);
Chung-yih Wang719eba52009-07-24 11:33:45 +0800119 if (req) NETSCAPE_SPKI_free(req);
Chung-yih Wangeec11822009-07-02 00:22:04 +0800120 if (pkey) EVP_PKEY_free(pkey);
Chung-yih Wang719eba52009-07-24 11:33:45 +0800121 if (spkstr) OPENSSL_free(spkstr);
Chung-yih Wangeec11822009-07-02 00:22:04 +0800122 if ((ret_code > 0) && (ret_code < ERR_MAXIMUM)) LOGE(emsg[ret_code]);
Chung-yih Wang719eba52009-07-24 11:33:45 +0800123 return -ret_code;
Chung-yih Wangeec11822009-07-02 00:22:04 +0800124}
125
Chung-yih Wangc9c119e2009-07-16 19:54:33 +0800126PKCS12 *get_p12_handle(const char *buf, int bufLen)
Chung-yih Wangeec11822009-07-02 00:22:04 +0800127{
Chung-yih Wangeec11822009-07-02 00:22:04 +0800128 BIO *bp = NULL;
129 PKCS12 *p12 = NULL;
130
Chung-yih Wangc9c119e2009-07-16 19:54:33 +0800131 if (!buf || (bufLen < 1) || (buf[0] != 48)) goto err;
Chung-yih Wangeec11822009-07-02 00:22:04 +0800132
Chung-yih Wangbf20b992009-07-02 23:42:12 +0800133 bp = BIO_new(BIO_s_mem());
134 if (!bp) goto err;
135
Chung-yih Wangeec11822009-07-02 00:22:04 +0800136 if (!BIO_write(bp, buf, bufLen)) goto err;
137
Chung-yih Wangc9c119e2009-07-16 19:54:33 +0800138 p12 = d2i_PKCS12_bio(bp, NULL);
139
Chung-yih Wangeec11822009-07-02 00:22:04 +0800140err:
141 if (bp) BIO_free(bp);
Chung-yih Wangc9c119e2009-07-16 19:54:33 +0800142 return p12;
143}
144
145PKCS12_KEYSTORE *get_pkcs12_keystore_handle(const char *buf, int bufLen,
146 const char *passwd)
147{
148 PKCS12_KEYSTORE *p12store = NULL;
149 EVP_PKEY *pkey = NULL;
150 X509 *cert = NULL;
151 STACK_OF(X509) *certs = NULL;
152 PKCS12 *p12 = get_p12_handle(buf, bufLen);
153
154 if (p12 == NULL) return NULL;
155 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
156 LOGE("Can not parse PKCS12 content");
157 PKCS12_free(p12);
158 return NULL;
159 }
160 if ((p12store = malloc(sizeof(PKCS12_KEYSTORE))) == NULL) {
161 if (cert) X509_free(cert);
162 if (pkey) EVP_PKEY_free(pkey);
163 if (certs) sk_X509_free(certs);
164 }
165 p12store->p12 = p12;
166 p12store->pkey = pkey;
167 p12store->cert = cert;
168 p12store->certs = certs;
169 return p12store;
170}
171
172void free_pkcs12_keystore(PKCS12_KEYSTORE *p12store)
173{
174 if (p12store != NULL) {
175 if (p12store->cert) X509_free(p12store->cert);
176 if (p12store->pkey) EVP_PKEY_free(p12store->pkey);
177 if (p12store->certs) sk_X509_free(p12store->certs);
178 free(p12store);
179 }
180}
181
182int is_pkcs12(const char *buf, int bufLen)
183{
184 int ret = 0;
185 PKCS12 *p12 = get_p12_handle(buf, bufLen);
186 if (p12 != NULL) ret = 1;
187 PKCS12_free(p12);
Chung-yih Wangeec11822009-07-02 00:22:04 +0800188 return ret;
189}
190
Chung-yih Wangc9c119e2009-07-16 19:54:33 +0800191static int convert_to_pem(void *data, int is_cert, char *buf, int size)
192{
193 int len = 0;
194 BIO *bio = NULL;
195
196 if (data == NULL) return -1;
197
198 if ((bio = BIO_new(BIO_s_mem())) == NULL) goto err;
199 if (is_cert) {
200 if ((len = PEM_write_bio_X509(bio, (X509*)data)) == 0) {
201 goto err;
202 }
203 } else {
204 if ((len = PEM_write_bio_PrivateKey(bio, (EVP_PKEY *)data, NULL,
205 NULL, 0, NULL, NULL)) == 0) {
206 goto err;
207 }
208 }
209 if (len < size && (len = BIO_read(bio, buf, size - 1)) > 0) {
210 buf[len] = 0;
211 }
212err:
213 if (bio) BIO_free(bio);
Chung-yih Wang09960232009-09-01 16:45:13 +0800214 return len;
Chung-yih Wangc9c119e2009-07-16 19:54:33 +0800215}
216
217int get_pkcs12_certificate(PKCS12_KEYSTORE *p12store, char *buf, int size)
218{
219 if ((p12store != NULL) && (p12store->cert != NULL)) {
Chung-yih Wang09960232009-09-01 16:45:13 +0800220 int len = convert_to_pem((void*)p12store->cert, 1, buf, size);
221 return (len == 0) ? -1 : 0;
Chung-yih Wangc9c119e2009-07-16 19:54:33 +0800222 }
223 return -1;
224}
225
226int get_pkcs12_private_key(PKCS12_KEYSTORE *p12store, char *buf, int size)
227{
228 if ((p12store != NULL) && (p12store->pkey != NULL)) {
Chung-yih Wang09960232009-09-01 16:45:13 +0800229 int len = convert_to_pem((void*)p12store->pkey, 0, buf, size);
230 return (len == 0) ? -1 : 0;
Chung-yih Wangc9c119e2009-07-16 19:54:33 +0800231 }
232 return -1;
233}
234
235int pop_pkcs12_certs_stack(PKCS12_KEYSTORE *p12store, char *buf, int size)
236{
237 X509 *cert = NULL;
Chung-yih Wang09960232009-09-01 16:45:13 +0800238 int len = 0;
Chung-yih Wangc9c119e2009-07-16 19:54:33 +0800239
Chung-yih Wang09960232009-09-01 16:45:13 +0800240 if ((p12store != NULL) && (p12store->certs != NULL)) {
241 while (((cert = sk_X509_pop(p12store->certs)) != NULL) && (len < size)) {
242 int s = convert_to_pem((void*)cert, 1, buf + len, size - len);
Chung-yih Wang60c821c2009-09-02 11:54:24 +0800243 if (s == 0) {
244 LOGE("buffer size is too small. len=%d size=%d\n", len, size);
245 return -1;
246 }
Chung-yih Wang09960232009-09-01 16:45:13 +0800247 len += s;
248 X509_free(cert);
249 }
250 return (len == 0) ? -1 : 0;
Chung-yih Wangc9c119e2009-07-16 19:54:33 +0800251 }
252 return -1;
253}
254
Chung-yih Wangeec11822009-07-02 00:22:04 +0800255X509* parse_cert(const char *buf, int bufLen)
256{
257 X509 *cert = NULL;
258 BIO *bp = NULL;
259
260 if(!buf || bufLen < 1)
261 return NULL;
262
263 bp = BIO_new(BIO_s_mem());
264 if (!bp) goto err;
265
266 if (!BIO_write(bp, buf, bufLen)) goto err;
267
268 cert = PEM_read_bio_X509(bp, NULL, NULL, NULL);
269 if (!cert) {
270 BIO_free(bp);
271 if((bp = BIO_new(BIO_s_mem())) == NULL) goto err;
272
273 if(!BIO_write(bp, (char *) buf, bufLen)) goto err;
274 cert = d2i_X509_bio(bp, NULL);
275 }
276
277err:
278 if (bp) BIO_free(bp);
279 return cert;
280}
281
282static int get_distinct_name(X509_NAME *dname, char *buf, int size)
283{
284 int i, len;
285 char *p, *name;
286
287 if (X509_NAME_oneline(dname, buf, size) == NULL) {
288 return -1;
289 }
290 name = strstr(buf, "/CN=");
291 p = name = name ? (name + 4) : buf;
292 while (*p != 0) {
293 if (*p == ' ') *p = '_';
294 if (*p == '/') {
295 *p = 0;
296 break;
297 }
298 ++p;
299 }
300 return 0;
301}
302
303int get_cert_name(X509 *cert, char *buf, int size)
304{
305 if (!cert) return -1;
306 return get_distinct_name(X509_get_subject_name(cert), buf, size);
307}
308
309int get_issuer_name(X509 *cert, char *buf, int size)
310{
311 if (!cert) return -1;
312 return get_distinct_name(X509_get_issuer_name(cert), buf, size);
313}
314
315int is_ca_cert(X509 *cert)
316{
317 int ret = 0;
318 BASIC_CONSTRAINTS *bs = (BASIC_CONSTRAINTS *)
319 X509_get_ext_d2i(cert, NID_basic_constraints, NULL, NULL);
320 if (bs != NULL) ret = bs->ca;
321 if (bs) BASIC_CONSTRAINTS_free(bs);
322 return ret;
323}
324
325int get_private_key_pem(X509 *cert, char *buf, int size)
326{
327 int len = 0;
328 BIO *bio = NULL;
329 EVP_PKEY *pkey = get_pkey_from_store(cert);
330
331 if (pkey == NULL) return -1;
332
333 bio = BIO_new(BIO_s_mem());
334 if ((bio = BIO_new(BIO_s_mem())) == NULL) goto err;
335 if (!PEM_write_bio_PrivateKey(bio, pkey, NULL,NULL,0,NULL, NULL)) {
336 goto err;
337 }
338 if ((len = BIO_read(bio, buf, size - 1)) > 0) {
339 buf[len] = 0;
340 }
341err:
342 if (bio) BIO_free(bio);
343 return (len == 0) ? -1 : 0;
344}