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