1aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root/* 2aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root * Copyright (C) 2012 The Android Open Source Project 3aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root * 4aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root * Licensed under the Apache License, Version 2.0 (the "License"); 5aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root * you may not use this file except in compliance with the License. 6aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root * You may obtain a copy of the License at 7aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root * 8aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root * http://www.apache.org/licenses/LICENSE-2.0 9aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root * 10aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root * Unless required by applicable law or agreed to in writing, software 11aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root * distributed under the License is distributed on an "AS IS" BASIS, 12aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root * See the License for the specific language governing permissions and 14aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root * limitations under the License. 15aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root */ 16aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root 17860d2707ce126ef8f66e3eac7ceeab6d24218cd8Kenny Rootpackage org.conscrypt; 18aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root 19aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Rootimport java.math.BigInteger; 20aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Rootimport java.security.InvalidAlgorithmParameterException; 21aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Rootimport java.security.KeyPair; 22aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Rootimport java.security.KeyPairGeneratorSpi; 23aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Rootimport java.security.PrivateKey; 24aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Rootimport java.security.PublicKey; 25aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Rootimport java.security.SecureRandom; 26aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Rootimport java.security.spec.AlgorithmParameterSpec; 27aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Rootimport java.security.spec.RSAKeyGenParameterSpec; 28aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root 29aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Rootpublic class OpenSSLRSAKeyPairGenerator extends KeyPairGeneratorSpi { 30aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root /** 31aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root * Default modulus size is 0x10001 (65537) 32aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root */ 33aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root private byte[] publicExponent = new byte[] { 34aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root 0x01, 0x00, 0x01 35aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root }; 36aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root 37aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root /** 38aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root * Default RSA key size 2048 bits. 39aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root */ 40aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root private int modulusBits = 2048; 41aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root 42aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root @Override 43aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root public KeyPair generateKeyPair() { 44aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root final OpenSSLKey key = new OpenSSLKey(NativeCrypto.RSA_generate_key_ex(modulusBits, 45aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root publicExponent)); 46aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root 47ff33acef44ed77b7907c7e6dd48f66fe32c0323fBrian Carlstrom PrivateKey privKey = OpenSSLRSAPrivateKey.getInstance(key); 48aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root PublicKey pubKey = new OpenSSLRSAPublicKey(key); 49aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root 50aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root return new KeyPair(pubKey, privKey); 51aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root } 52aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root 53aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root @Override 54aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root public void initialize(int keysize, SecureRandom random) { 55aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root this.modulusBits = keysize; 56aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root } 57aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root 58aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root @Override 59aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root public void initialize(AlgorithmParameterSpec params, SecureRandom random) 60aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root throws InvalidAlgorithmParameterException { 61aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root if (!(params instanceof RSAKeyGenParameterSpec)) { 62aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root throw new InvalidAlgorithmParameterException("Only RSAKeyGenParameterSpec supported"); 63aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root } 64aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root 65aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root RSAKeyGenParameterSpec spec = (RSAKeyGenParameterSpec) params; 66aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root 67aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root final BigInteger publicExponent = spec.getPublicExponent(); 68aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root if (publicExponent != null) { 69aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root this.publicExponent = publicExponent.toByteArray(); 70aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root } 71aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root 72aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root this.modulusBits = spec.getKeysize(); 73aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root } 74aa48fdb4d42bc5de668f11055f88fb430fdf4d61Kenny Root} 75