The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.dumpkey; |
| 18 | |
| 19 | import java.io.FileInputStream; |
| 20 | import java.math.BigInteger; |
| 21 | import java.security.cert.CertificateFactory; |
| 22 | import java.security.cert.Certificate; |
| 23 | import java.security.KeyStore; |
| 24 | import java.security.Key; |
| 25 | import java.security.PublicKey; |
| 26 | import java.security.interfaces.RSAPublicKey; |
| 27 | import 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 | */ |
| 34 | class DumpPublicKey { |
| 35 | /** |
| 36 | * @param key to perform sanity checks on |
Doug Zongker | 35d9ad5 | 2012-07-25 12:08:33 -0700 | [diff] [blame^] | 37 | * @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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | * @throws Exception if the key has the wrong size or public exponent |
Doug Zongker | 35d9ad5 | 2012-07-25 12:08:33 -0700 | [diff] [blame^] | 41 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | */ |
Doug Zongker | 35d9ad5 | 2012-07-25 12:08:33 -0700 | [diff] [blame^] | 43 | static int check(RSAPublicKey key) throws Exception { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | BigInteger pubexp = key.getPublicExponent(); |
| 45 | BigInteger modulus = key.getModulus(); |
Doug Zongker | 35d9ad5 | 2012-07-25 12:08:33 -0700 | [diff] [blame^] | 46 | int version; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | |
Doug Zongker | 35d9ad5 | 2012-07-25 12:08:33 -0700 | [diff] [blame^] | 48 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | |
Doug Zongker | 35d9ad5 | 2012-07-25 12:08:33 -0700 | [diff] [blame^] | 57 | if (modulus.bitLength() != 2048) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | throw new Exception("Modulus should be 2048 bits long but is " + |
| 59 | modulus.bitLength() + " bits."); |
Doug Zongker | 35d9ad5 | 2012-07-25 12:08:33 -0700 | [diff] [blame^] | 60 | } |
| 61 | |
| 62 | return version; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param key to output |
Doug Zongker | 35d9ad5 | 2012-07-25 12:08:33 -0700 | [diff] [blame^] | 67 | * @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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 70 | */ |
| 71 | static String print(RSAPublicKey key) throws Exception { |
Doug Zongker | 35d9ad5 | 2012-07-25 12:08:33 -0700 | [diff] [blame^] | 72 | int version = check(key); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 73 | |
| 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 Zongker | 35d9ad5 | 2012-07-25 12:08:33 -0700 | [diff] [blame^] | 80 | if (version > 1) { |
| 81 | result.append("v"); |
| 82 | result.append(Integer.toString(version)); |
| 83 | result.append(" "); |
| 84 | } |
| 85 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 86 | 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 Zongker | 5e12d73 | 2010-01-29 10:47:38 -0800 | [diff] [blame] | 101 | long n = N.mod(B).longValue(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 102 | 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 Zongker | 5e12d73 | 2010-01-29 10:47:38 -0800 | [diff] [blame] | 115 | long rr = RR.mod(B).longValue(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | 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 | } |