2 * Copyright (C) 2013 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <sys/types.h>
23 /* HACK: we need the RSAPublicKey struct
24 * but RSA_verify conflits with openssl */
25 #define RSA_verify RSA_verify_mincrypt
26 #include "mincrypt/rsa.h"
29 #include <openssl/evp.h>
30 #include <openssl/objects.h>
31 #include <openssl/pem.h>
32 #include <openssl/rsa.h>
33 #include <openssl/sha.h>
35 // Convert OpenSSL RSA private key to android pre-computed RSAPublicKey format.
36 // Lifted from secure adb's mincrypt key generation.
37 static int convert_to_mincrypt_format(RSA *rsa, RSAPublicKey *pkey)
42 if (RSA_size(rsa) != RSANUMBYTES)
45 BN_CTX* ctx = BN_CTX_new();
46 BIGNUM* r32 = BN_new();
47 BIGNUM* rr = BN_new();
49 BIGNUM* rem = BN_new();
51 BIGNUM* n0inv = BN_new();
55 BN_set_bit(r, RSANUMWORDS * 32);
56 BN_mod_sqr(rr, r, n, ctx);
57 BN_div(NULL, rem, n, r32, ctx);
58 BN_mod_inverse(n0inv, rem, r32, ctx);
60 pkey->len = RSANUMWORDS;
61 pkey->n0inv = 0 - BN_get_word(n0inv);
62 for (i = 0; i < RSANUMWORDS; i++) {
63 BN_div(rr, rem, rr, r32, ctx);
64 pkey->rr[i] = BN_get_word(rem);
65 BN_div(n, rem, n, r32, ctx);
66 pkey->n[i] = BN_get_word(rem);
68 pkey->exponent = BN_get_word(rsa->e);
84 static int write_public_keyfile(RSA *private_key, const char *private_key_path)
91 if (asprintf(&path, "%s.pub", private_key_path) < 0)
94 if (convert_to_mincrypt_format(private_key, &pkey) < 0)
97 bfile = BIO_new_file(path, "w");
101 BIO_write(bfile, &pkey, sizeof(pkey));
111 static int convert_x509(const char *pem_file, const char *key_file)
115 EVP_PKEY *pkey = NULL;
119 if (!pem_file || !key_file) {
123 f = fopen(pem_file, "r");
125 printf("Failed to open '%s'\n", pem_file);
129 cert = PEM_read_X509(f, &cert, NULL, NULL);
131 printf("Failed to read PEM certificate from file '%s'\n", pem_file);
135 pkey = X509_get_pubkey(cert);
137 printf("Failed to extract public key from certificate '%s'\n", pem_file);
141 rsa = EVP_PKEY_get1_RSA(pkey);
143 printf("Failed to get the RSA public key from '%s'\n", pem_file);
147 if (write_public_keyfile(rsa, key_file) < 0) {
148 printf("Failed to write public key\n");
171 static int generate_key(const char *file)
175 RSA* rsa = RSA_new();
176 BIGNUM* exponent = BN_new();
177 EVP_PKEY* pkey = EVP_PKEY_new();
179 if (!pkey || !exponent || !rsa) {
180 printf("Failed to allocate key\n");
184 BN_set_word(exponent, RSA_F4);
185 RSA_generate_key_ex(rsa, 2048, exponent, NULL);
186 EVP_PKEY_set1_RSA(pkey, rsa);
188 f = fopen(file, "w");
190 printf("Failed to open '%s'\n", file);
194 if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) {
195 printf("Failed to write key\n");
199 if (write_public_keyfile(rsa, file) < 0) {
200 printf("Failed to write public key\n");
216 printf("Usage: generate_verity_key <path-to-key> | -convert <path-to-x509-pem> <path-to-key>\n");
219 int main(int argc, char *argv[]) {
221 return generate_key(argv[1]);
222 } else if (argc == 4 && !strcmp(argv[1], "-convert")) {
223 return convert_x509(argv[2], argv[3]);