Home | History | Annotate | Download | only in jsse
      1 /*
      2  * Copyright (C) 2012 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 org.apache.harmony.xnet.provider.jsse;
     18 
     19 import java.math.BigInteger;
     20 import java.security.InvalidAlgorithmParameterException;
     21 import java.security.KeyPair;
     22 import java.security.KeyPairGeneratorSpi;
     23 import java.security.SecureRandom;
     24 import java.security.spec.AlgorithmParameterSpec;
     25 import java.security.spec.DSAParameterSpec;
     26 
     27 public class OpenSSLDSAKeyPairGenerator extends KeyPairGeneratorSpi {
     28 
     29     private int primeBits = 1024;
     30 
     31     private SecureRandom random = null;
     32 
     33     private byte[] g;
     34 
     35     private byte[] p;
     36 
     37     private byte[] q;
     38 
     39     @Override
     40     public KeyPair generateKeyPair() {
     41         final byte[] seed;
     42         if (random == null) {
     43             seed = null;
     44         } else {
     45             seed = new byte[20];
     46             random.nextBytes(seed);
     47         }
     48 
     49         final OpenSSLKey key = new OpenSSLKey(NativeCrypto.DSA_generate_key(primeBits, seed, g, p,
     50                 q));
     51 
     52         final OpenSSLDSAPrivateKey privKey = new OpenSSLDSAPrivateKey(key);
     53         final OpenSSLDSAPublicKey pubKey = new OpenSSLDSAPublicKey(key);
     54 
     55         return new KeyPair(pubKey, privKey);
     56     }
     57 
     58     @Override
     59     public void initialize(int keysize, SecureRandom random) {
     60         primeBits = keysize;
     61         this.random = random;
     62     }
     63 
     64     @Override
     65     public void initialize(AlgorithmParameterSpec params, SecureRandom random)
     66             throws InvalidAlgorithmParameterException {
     67         this.random = random;
     68 
     69         if (params instanceof DSAParameterSpec) {
     70             DSAParameterSpec dsaParams = (DSAParameterSpec) params;
     71 
     72             BigInteger gInt = dsaParams.getG();
     73             if (gInt != null) {
     74                 g = gInt.toByteArray();
     75             }
     76 
     77             BigInteger pInt = dsaParams.getP();
     78             if (pInt != null) {
     79                 p = pInt.toByteArray();
     80             }
     81 
     82             BigInteger qInt = dsaParams.getQ();
     83             if (qInt != null) {
     84                 q = qInt.toByteArray();
     85             }
     86         } else if (params != null) {
     87             throw new InvalidAlgorithmParameterException("Params must be DSAParameterSpec");
     88         }
     89     }
     90 }
     91