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 @Internal
36 public final class OpenSSLRSAKeyPairGenerator extends KeyPairGeneratorSpi {
37     /**
38      * Default modulus size is 0x10001 (65537)
39      */
40     private byte[] publicExponent = new byte[] {
41             0x01, 0x00, 0x01
42     };
43 
44     /**
45      * Default RSA key size 2048 bits.
46      */
47     private int modulusBits = 2048;
48 
OpenSSLRSAKeyPairGenerator()49     public OpenSSLRSAKeyPairGenerator() {}
50 
51     @Override
generateKeyPair()52     public KeyPair generateKeyPair() {
53         final OpenSSLKey key = new OpenSSLKey(NativeCrypto.RSA_generate_key_ex(modulusBits,
54                 publicExponent));
55 
56         PrivateKey privKey = OpenSSLRSAPrivateKey.getInstance(key);
57         PublicKey pubKey = new OpenSSLRSAPublicKey(key);
58 
59         return new KeyPair(pubKey, privKey);
60     }
61 
62     @Override
initialize(int keysize, SecureRandom random)63     public void initialize(int keysize, SecureRandom random) {
64         this.modulusBits = keysize;
65     }
66 
67     @Override
initialize(AlgorithmParameterSpec params, SecureRandom random)68     public void initialize(AlgorithmParameterSpec params, SecureRandom random)
69             throws InvalidAlgorithmParameterException {
70         if (!(params instanceof RSAKeyGenParameterSpec)) {
71             throw new InvalidAlgorithmParameterException("Only RSAKeyGenParameterSpec supported");
72         }
73 
74         RSAKeyGenParameterSpec spec = (RSAKeyGenParameterSpec) params;
75 
76         final BigInteger publicExponent = spec.getPublicExponent();
77         if (publicExponent != null) {
78             this.publicExponent = publicExponent.toByteArray();
79         }
80 
81         this.modulusBits = spec.getKeysize();
82     }
83 }
84