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.io.IOException;
     20 import java.io.NotSerializableException;
     21 import java.io.ObjectInputStream;
     22 import java.io.ObjectOutputStream;
     23 import java.math.BigInteger;
     24 import java.security.InvalidAlgorithmParameterException;
     25 import java.security.InvalidKeyException;
     26 import java.security.interfaces.ECPrivateKey;
     27 import java.security.spec.ECParameterSpec;
     28 import java.security.spec.ECPrivateKeySpec;
     29 import java.security.spec.InvalidKeySpecException;
     30 import java.util.Arrays;
     31 
     32 public final class OpenSSLECPrivateKey implements ECPrivateKey, OpenSSLKeyHolder {
     33     private static final long serialVersionUID = -4036633595001083922L;
     34 
     35     private static final String ALGORITHM = "EC";
     36 
     37     protected transient OpenSSLKey key;
     38 
     39     protected transient OpenSSLECGroupContext group;
     40 
     41     public OpenSSLECPrivateKey(OpenSSLECGroupContext group, OpenSSLKey key) {
     42         this.group = group;
     43         this.key = key;
     44     }
     45 
     46     public OpenSSLECPrivateKey(OpenSSLKey key) {
     47         final long origGroup = NativeCrypto.EC_KEY_get0_group(key.getPkeyContext());
     48         this.group = new OpenSSLECGroupContext(NativeCrypto.EC_GROUP_dup(origGroup));
     49         this.key = key;
     50     }
     51 
     52     public OpenSSLECPrivateKey(ECPrivateKeySpec ecKeySpec) throws InvalidKeySpecException {
     53         try {
     54             group = OpenSSLECGroupContext.getInstance(ecKeySpec.getParams());
     55             final BigInteger privKey = ecKeySpec.getS();
     56             key = new OpenSSLKey(NativeCrypto.EVP_PKEY_new_EC_KEY(group.getContext(), 0,
     57                     privKey.toByteArray()));
     58         } catch (Exception e) {
     59             throw new InvalidKeySpecException(e);
     60         }
     61     }
     62 
     63     public static OpenSSLKey getInstance(ECPrivateKey ecPrivateKey) throws InvalidKeyException {
     64         try {
     65             OpenSSLECGroupContext group = OpenSSLECGroupContext.getInstance(ecPrivateKey
     66                     .getParams());
     67             final BigInteger privKey = ecPrivateKey.getS();
     68             return new OpenSSLKey(NativeCrypto.EVP_PKEY_new_EC_KEY(group.getContext(), 0,
     69                     privKey.toByteArray()));
     70         } catch (Exception e) {
     71             throw new InvalidKeyException(e);
     72         }
     73     }
     74 
     75     @Override
     76     public String getAlgorithm() {
     77         return ALGORITHM;
     78     }
     79 
     80     @Override
     81     public String getFormat() {
     82         return "PKCS#8";
     83     }
     84 
     85     @Override
     86     public byte[] getEncoded() {
     87         return NativeCrypto.i2d_PKCS8_PRIV_KEY_INFO(key.getPkeyContext());
     88     }
     89 
     90     @Override
     91     public ECParameterSpec getParams() {
     92         return group.getECParameterSpec();
     93     }
     94 
     95     @Override
     96     public BigInteger getS() {
     97         if (key.isEngineBased()) {
     98             throw new UnsupportedOperationException("private key value S cannot be extracted");
     99         }
    100 
    101         return getPrivateKey();
    102     }
    103 
    104     private BigInteger getPrivateKey() {
    105         return new BigInteger(NativeCrypto.EC_KEY_get_private_key(key.getPkeyContext()));
    106     }
    107 
    108     @Override
    109     public OpenSSLKey getOpenSSLKey() {
    110         return key;
    111     }
    112 
    113     @Override
    114     public boolean equals(Object o) {
    115         if (o == this) {
    116             return true;
    117         }
    118 
    119         if (o instanceof OpenSSLECPrivateKey) {
    120             OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o;
    121             return key.equals(other.key);
    122         }
    123 
    124         if (!(o instanceof ECPrivateKey)) {
    125             return false;
    126         }
    127 
    128         final ECPrivateKey other = (ECPrivateKey) o;
    129         if (!getPrivateKey().equals(other.getS())) {
    130             return false;
    131         }
    132 
    133         final ECParameterSpec spec = getParams();
    134         final ECParameterSpec otherSpec = other.getParams();
    135 
    136         return spec.getCurve().equals(otherSpec.getCurve())
    137                 && spec.getGenerator().equals(otherSpec.getGenerator())
    138                 && spec.getOrder().equals(otherSpec.getOrder())
    139                 && spec.getCofactor() == otherSpec.getCofactor();
    140     }
    141 
    142     @Override
    143     public int hashCode() {
    144         return Arrays.hashCode(NativeCrypto.i2d_PKCS8_PRIV_KEY_INFO(key.getPkeyContext()));
    145     }
    146 
    147     @Override
    148     public String toString() {
    149         return NativeCrypto.EVP_PKEY_print_private(key.getPkeyContext());
    150     }
    151 
    152     private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
    153         stream.defaultReadObject();
    154 
    155         byte[] encoded = (byte[]) stream.readObject();
    156 
    157         key = new OpenSSLKey(NativeCrypto.d2i_PKCS8_PRIV_KEY_INFO(encoded));
    158 
    159         final long origGroup = NativeCrypto.EC_KEY_get0_group(key.getPkeyContext());
    160         group = new OpenSSLECGroupContext(NativeCrypto.EC_GROUP_dup(origGroup));
    161     }
    162 
    163     private void writeObject(ObjectOutputStream stream) throws IOException {
    164         if (key.isEngineBased()) {
    165             throw new NotSerializableException("engine-based keys can not be serialized");
    166         }
    167 
    168         stream.defaultWriteObject();
    169         stream.writeObject(getEncoded());
    170     }
    171 }
    172