blob: b83a7575754fe97d30cdbe0808f20b23e4044df7 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package com.android.dumpkey;
18
19import java.io.FileInputStream;
20import java.math.BigInteger;
21import java.security.cert.CertificateFactory;
22import java.security.cert.Certificate;
23import java.security.KeyStore;
24import java.security.Key;
25import java.security.PublicKey;
26import java.security.interfaces.RSAPublicKey;
27import sun.misc.BASE64Encoder;
28
29/**
30 * Command line tool to extract RSA public keys from X.509 certificates
31 * and output source code with data initializers for the keys.
32 * @hide
33 */
34class DumpPublicKey {
35 /**
36 * @param key to perform sanity checks on
Doug Zongker35d9ad52012-07-25 12:08:33 -070037 * @return version number of key. Supported versions are:
38 * 1: 2048-bit key with e=3
39 * 2: 2048-bit key with e=65537
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040 * @throws Exception if the key has the wrong size or public exponent
Doug Zongker35d9ad52012-07-25 12:08:33 -070041
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042 */
Doug Zongker35d9ad52012-07-25 12:08:33 -070043 static int check(RSAPublicKey key) throws Exception {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044 BigInteger pubexp = key.getPublicExponent();
45 BigInteger modulus = key.getModulus();
Doug Zongker35d9ad52012-07-25 12:08:33 -070046 int version;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
Doug Zongker35d9ad52012-07-25 12:08:33 -070048 if (pubexp.equals(BigInteger.valueOf(3))) {
49 version = 1;
50 } else if (pubexp.equals(BigInteger.valueOf(65537))) {
51 version = 2;
52 } else {
53 throw new Exception("Public exponent should be 3 or 65537 but is " +
54 pubexp.toString(10) + ".");
55 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
Doug Zongker35d9ad52012-07-25 12:08:33 -070057 if (modulus.bitLength() != 2048) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058 throw new Exception("Modulus should be 2048 bits long but is " +
59 modulus.bitLength() + " bits.");
Doug Zongker35d9ad52012-07-25 12:08:33 -070060 }
61
62 return version;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063 }
64
65 /**
66 * @param key to output
Doug Zongker35d9ad52012-07-25 12:08:33 -070067 * @return a String representing this public key. If the key is a
68 * version 1 key, the string will be a C initializer; this is
69 * not true for newer key versions.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070 */
71 static String print(RSAPublicKey key) throws Exception {
Doug Zongker35d9ad52012-07-25 12:08:33 -070072 int version = check(key);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073
74 BigInteger N = key.getModulus();
75
76 StringBuilder result = new StringBuilder();
77
78 int nwords = N.bitLength() / 32; // # of 32 bit integers in modulus
79
Doug Zongker35d9ad52012-07-25 12:08:33 -070080 if (version > 1) {
81 result.append("v");
82 result.append(Integer.toString(version));
83 result.append(" ");
84 }
85
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086 result.append("{");
87 result.append(nwords);
88
89 BigInteger B = BigInteger.valueOf(0x100000000L); // 2^32
90 BigInteger N0inv = B.subtract(N.modInverse(B)); // -1 / N[0] mod 2^32
91
92 result.append(",0x");
93 result.append(N0inv.toString(16));
94
95 BigInteger R = BigInteger.valueOf(2).pow(N.bitLength());
96 BigInteger RR = R.multiply(R).mod(N); // 2^4096 mod N
97
98 // Write out modulus as little endian array of integers.
99 result.append(",{");
100 for (int i = 0; i < nwords; ++i) {
Doug Zongker5e12d732010-01-29 10:47:38 -0800101 long n = N.mod(B).longValue();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 result.append(n);
103
104 if (i != nwords - 1) {
105 result.append(",");
106 }
107
108 N = N.divide(B);
109 }
110 result.append("}");
111
112 // Write R^2 as little endian array of integers.
113 result.append(",{");
114 for (int i = 0; i < nwords; ++i) {
Doug Zongker5e12d732010-01-29 10:47:38 -0800115 long rr = RR.mod(B).longValue();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 result.append(rr);
117
118 if (i != nwords - 1) {
119 result.append(",");
120 }
121
122 RR = RR.divide(B);
123 }
124 result.append("}");
125
126 result.append("}");
127 return result.toString();
128 }
129
130 public static void main(String[] args) {
131 if (args.length < 1) {
132 System.err.println("Usage: DumpPublicKey certfile ... > source.c");
133 System.exit(1);
134 }
135 try {
136 for (int i = 0; i < args.length; i++) {
137 FileInputStream input = new FileInputStream(args[i]);
138 CertificateFactory cf = CertificateFactory.getInstance("X.509");
139 Certificate cert = cf.generateCertificate(input);
140 RSAPublicKey key = (RSAPublicKey) (cert.getPublicKey());
141 check(key);
142 System.out.print(print(key));
143 System.out.println(i < args.length - 1 ? "," : "");
144 }
145 } catch (Exception e) {
146 e.printStackTrace();
147 System.exit(1);
148 }
149 System.exit(0);
150 }
151}