OpenSSLDSAPublicKey.java revision 0731d6d00c5e30c05e035d3ae96327029d07a606
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 17package org.apache.harmony.xnet.provider.jsse; 18 19import java.io.IOException; 20import java.io.NotSerializableException; 21import java.io.ObjectInputStream; 22import java.io.ObjectOutputStream; 23import java.math.BigInteger; 24import java.security.InvalidKeyException; 25import java.security.interfaces.DSAParams; 26import java.security.interfaces.DSAPublicKey; 27import java.security.spec.DSAPublicKeySpec; 28import java.security.spec.InvalidKeySpecException; 29 30public class OpenSSLDSAPublicKey implements DSAPublicKey, OpenSSLKeyHolder { 31 private static final long serialVersionUID = 5238609500353792232L; 32 33 private transient OpenSSLKey key; 34 35 private transient OpenSSLDSAParams params; 36 37 OpenSSLDSAPublicKey(OpenSSLKey key) { 38 this.key = key; 39 } 40 41 @Override 42 public OpenSSLKey getOpenSSLKey() { 43 return key; 44 } 45 46 OpenSSLDSAPublicKey(DSAPublicKeySpec dsaKeySpec) throws InvalidKeySpecException { 47 try { 48 key = new OpenSSLKey(NativeCrypto.EVP_PKEY_new_DSA( 49 dsaKeySpec.getP().toByteArray(), 50 dsaKeySpec.getQ().toByteArray(), 51 dsaKeySpec.getG().toByteArray(), 52 dsaKeySpec.getY().toByteArray(), 53 null)); 54 } catch (Exception e) { 55 throw new InvalidKeySpecException(e); 56 } 57 } 58 59 private void ensureReadParams() { 60 if (params == null) { 61 params = new OpenSSLDSAParams(key); 62 } 63 } 64 65 static OpenSSLKey getInstance(DSAPublicKey dsaPublicKey) throws InvalidKeyException { 66 try { 67 final DSAParams dsaParams = dsaPublicKey.getParams(); 68 return new OpenSSLKey(NativeCrypto.EVP_PKEY_new_DSA( 69 dsaParams.getP().toByteArray(), 70 dsaParams.getQ().toByteArray(), 71 dsaParams.getG().toByteArray(), 72 dsaPublicKey.getY().toByteArray(), 73 null)); 74 } catch (Exception e) { 75 throw new InvalidKeyException(e); 76 } 77 } 78 79 @Override 80 public DSAParams getParams() { 81 ensureReadParams(); 82 return params; 83 } 84 85 @Override 86 public String getAlgorithm() { 87 return "DSA"; 88 } 89 90 @Override 91 public String getFormat() { 92 return "X.509"; 93 } 94 95 @Override 96 public byte[] getEncoded() { 97 return NativeCrypto.i2d_PUBKEY(key.getPkeyContext()); 98 } 99 100 @Override 101 public BigInteger getY() { 102 ensureReadParams(); 103 return params.getY(); 104 } 105 106 @Override 107 public boolean equals(Object o) { 108 if (o == this) { 109 return true; 110 } 111 112 if (o instanceof OpenSSLDSAPublicKey) { 113 OpenSSLDSAPublicKey other = (OpenSSLDSAPublicKey) o; 114 115 /* 116 * We can shortcut the true case, but it still may be equivalent but 117 * different copies. 118 */ 119 if (key.equals(other.getOpenSSLKey())) { 120 return true; 121 } 122 } 123 124 if (!(o instanceof DSAPublicKey)) { 125 return false; 126 } 127 128 ensureReadParams(); 129 130 DSAPublicKey other = (DSAPublicKey) o; 131 return params.getY().equals(other.getY()) && params.equals(other.getParams()); 132 } 133 134 @Override 135 public int hashCode() { 136 ensureReadParams(); 137 138 return params.getY().hashCode() ^ params.hashCode(); 139 } 140 141 @Override 142 public String toString() { 143 ensureReadParams(); 144 145 final StringBuilder sb = new StringBuilder("OpenSSLDSAPublicKey{"); 146 sb.append("Y="); 147 sb.append(params.getY().toString(16)); 148 sb.append(','); 149 sb.append("params="); 150 sb.append(params.toString()); 151 sb.append('}'); 152 153 return sb.toString(); 154 } 155 156 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { 157 stream.defaultReadObject(); 158 159 final BigInteger g = (BigInteger) stream.readObject(); 160 final BigInteger p = (BigInteger) stream.readObject(); 161 final BigInteger q = (BigInteger) stream.readObject(); 162 final BigInteger y = (BigInteger) stream.readObject(); 163 164 key = new OpenSSLKey(NativeCrypto.EVP_PKEY_new_DSA( 165 p.toByteArray(), 166 q.toByteArray(), 167 g.toByteArray(), 168 y.toByteArray(), 169 null)); 170 } 171 172 private void writeObject(ObjectOutputStream stream) throws IOException { 173 if (getOpenSSLKey().isEngineBased()) { 174 throw new NotSerializableException("engine-based keys can not be serialized"); 175 } 176 stream.defaultWriteObject(); 177 178 ensureReadParams(); 179 stream.writeObject(params.getG()); 180 stream.writeObject(params.getP()); 181 stream.writeObject(params.getQ()); 182 stream.writeObject(params.getY()); 183 } 184} 185