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.
17 #define _GNU_SOURCE /* needed for asprintf */
23 #include <sys/types.h>
26 /* HACK: we need the RSAPublicKey struct
27 * but RSA_verify conflits with openssl */
28 #define RSA_verify RSA_verify_mincrypt
29 #include "mincrypt/rsa.h"
32 #include <openssl/evp.h>
33 #include <openssl/objects.h>
34 #include <openssl/pem.h>
35 #include <openssl/rsa.h>
36 #include <openssl/sha.h>
38 // Convert OpenSSL RSA private key to android pre-computed RSAPublicKey format.
39 // Lifted from secure adb's mincrypt key generation.
40 static int convert_to_mincrypt_format(RSA *rsa, RSAPublicKey *pkey)
45 if (RSA_size(rsa) != RSANUMBYTES)
48 BN_CTX* ctx = BN_CTX_new();
49 BIGNUM* r32 = BN_new();
50 BIGNUM* rr = BN_new();
52 BIGNUM* rem = BN_new();
54 BIGNUM* n0inv = BN_new();
58 BN_set_bit(r, RSANUMWORDS * 32);
59 BN_mod_sqr(rr, r, n, ctx);
60 BN_div(NULL, rem, n, r32, ctx);
61 BN_mod_inverse(n0inv, rem, r32, ctx);
63 pkey->len = RSANUMWORDS;
64 pkey->n0inv = 0 - BN_get_word(n0inv);
65 for (i = 0; i < RSANUMWORDS; i++) {
66 BN_div(rr, rem, rr, r32, ctx);
67 pkey->rr[i] = BN_get_word(rem);
68 BN_div(n, rem, n, r32, ctx);
69 pkey->n[i] = BN_get_word(rem);
71 pkey->exponent = BN_get_word(rsa->e);
87 static int write_public_keyfile(RSA *private_key, const char *private_key_path)
94 if (asprintf(&path, "%s.pub", private_key_path) < 0)
97 if (convert_to_mincrypt_format(private_key, &pkey) < 0)
100 bfile = BIO_new_file(path, "w");
104 BIO_write(bfile, &pkey, sizeof(pkey));
114 static int convert_x509(const char *pem_file, const char *key_file)
118 EVP_PKEY *pkey = NULL;
122 if (!pem_file || !key_file) {
126 f = fopen(pem_file, "r");
128 printf("Failed to open '%s'\n", pem_file);
132 cert = PEM_read_X509(f, &cert, NULL, NULL);
134 printf("Failed to read PEM certificate from file '%s'\n", pem_file);
138 pkey = X509_get_pubkey(cert);
140 printf("Failed to extract public key from certificate '%s'\n", pem_file);
144 rsa = EVP_PKEY_get1_RSA(pkey);
146 printf("Failed to get the RSA public key from '%s'\n", pem_file);
150 if (write_public_keyfile(rsa, key_file) < 0) {
151 printf("Failed to write public key\n");
174 static int generate_key(const char *file)
178 RSA* rsa = RSA_new();
179 BIGNUM* exponent = BN_new();
180 EVP_PKEY* pkey = EVP_PKEY_new();
182 if (!pkey || !exponent || !rsa) {
183 printf("Failed to allocate key\n");
187 BN_set_word(exponent, RSA_F4);
188 RSA_generate_key_ex(rsa, 2048, exponent, NULL);
189 EVP_PKEY_set1_RSA(pkey, rsa);
191 f = fopen(file, "w");
193 printf("Failed to open '%s'\n", file);
197 if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) {
198 printf("Failed to write key\n");
202 if (write_public_keyfile(rsa, file) < 0) {
203 printf("Failed to write public key\n");
219 printf("Usage: generate_verity_key <path-to-key> | -convert <path-to-x509-pem> <path-to-key>\n");
222 int main(int argc, char *argv[]) {
224 return generate_key(argv[1]);
225 } else if (argc == 4 && !strcmp(argv[1], "-convert")) {
226 return convert_x509(argv[2], argv[3]);