Showing the changes to java/pkg/com/sun/crypto/provider/DHPrivateKey.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,352 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 5 | + * This code is free software; you can redistribute it and/or modify it | |
| 6 | + * under the terms of the GNU General Public License version 2 only, as | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 10 | + * | |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that | |
| 15 | + * accompanied this code). | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU General Public License version | |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, | |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 20 | + * | |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +package com.sun.crypto.provider; | |
| 27 | + | |
| 28 | +import java.io.*; | |
| 29 | +import java.math.BigInteger; | |
| 30 | +import java.security.KeyRep; | |
| 31 | +import java.security.PrivateKey; | |
| 32 | +import java.security.InvalidKeyException; | |
| 33 | +import java.security.ProviderException; | |
| 34 | +import javax.crypto.*; | |
| 35 | +import javax.crypto.spec.DHParameterSpec; | |
| 36 | +import sun.security.util.*; | |
| 37 | + | |
| 38 | +/** | |
| 39 | + * A private key in PKCS#8 format for the Diffie-Hellman key agreement | |
| 40 | + * algorithm. | |
| 41 | + * | |
| 42 | + * @author Jan Luehe | |
| 43 | + * | |
| 44 | + * | |
| 45 | + * @see DHPublicKey | |
| 46 | + * @see java.security.KeyAgreement | |
| 47 | + */ | |
| 48 | +final class DHPrivateKey implements PrivateKey, | |
| 49 | +javax.crypto.interfaces.DHPrivateKey, Serializable { | |
| 50 | + | |
| 51 | + static final long serialVersionUID = 7565477590005668886L; | |
| 52 | + | |
| 53 | + // only supported version of PKCS#8 PrivateKeyInfo | |
| 54 | + private static final BigInteger PKCS8_VERSION = BigInteger.ZERO; | |
| 55 | + | |
| 56 | + // the private key | |
| 57 | + private BigInteger x; | |
| 58 | + | |
| 59 | + // the key bytes, without the algorithm information | |
| 60 | + private byte[] key; | |
| 61 | + | |
| 62 | + // the encoded key | |
| 63 | + private byte[] encodedKey; | |
| 64 | + | |
| 65 | + // the prime modulus | |
| 66 | + private BigInteger p; | |
| 67 | + | |
| 68 | + // the base generator | |
| 69 | + private BigInteger g; | |
| 70 | + | |
| 71 | + // the private-value length | |
| 72 | + private int l; | |
| 73 | + | |
| 74 | + private int DH_data[] = { 1, 2, 840, 113549, 1, 3, 1 }; | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * Make a DH private key out of a private value <code>x</code>, a prime | |
| 78 | + * modulus <code>p</code>, and a base generator <code>g</code>. | |
| 79 | + * | |
| 80 | + * @param x the private value | |
| 81 | + * @param p the prime modulus | |
| 82 | + * @param g the base generator | |
| 83 | + * | |
| 84 | + * @exception ProviderException if the key cannot be encoded | |
| 85 | + */ | |
| 86 | + DHPrivateKey(BigInteger x, BigInteger p, BigInteger g) | |
| 87 | + throws InvalidKeyException { | |
| 88 | + this(x, p, g, 0); | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 92 | + * Make a DH private key out of a private value <code>x</code>, a prime | |
| 93 | + * modulus <code>p</code>, a base generator <code>g</code>, and a | |
| 94 | + * private-value length <code>l</code>. | |
| 95 | + * | |
| 96 | + * @param x the private value | |
| 97 | + * @param p the prime modulus | |
| 98 | + * @param g the base generator | |
| 99 | + * @param l the private-value length | |
| 100 | + * | |
| 101 | + * @exception InvalidKeyException if the key cannot be encoded | |
| 102 | + */ | |
| 103 | + DHPrivateKey(BigInteger x, BigInteger p, BigInteger g, int l) { | |
| 104 | + this.x = x; | |
| 105 | + this.p = p; | |
| 106 | + this.g = g; | |
| 107 | + this.l = l; | |
| 108 | + try { | |
| 109 | + this.key = new DerValue(DerValue.tag_Integer, | |
| 110 | + this.x.toByteArray()).toByteArray(); | |
| 111 | + this.encodedKey = getEncoded(); | |
| 112 | + } catch (IOException e) { | |
| 113 | + throw new ProviderException("Cannot produce ASN.1 encoding", e); | |
| 114 | + } | |
| 115 | + } | |
| 116 | + | |
| 117 | + /** | |
| 118 | + * Make a DH private key from its DER encoding (PKCS #8). | |
| 119 | + * | |
| 120 | + * @param encodedKey the encoded key | |
| 121 | + * | |
| 122 | + * @exception InvalidKeyException if the encoded key does not represent | |
| 123 | + * a Diffie-Hellman private key | |
| 124 | + */ | |
| 125 | + DHPrivateKey(byte[] encodedKey) throws InvalidKeyException { | |
| 126 | + InputStream inStream = new ByteArrayInputStream(encodedKey); | |
| 127 | + try { | |
| 128 | + DerValue val = new DerValue(inStream); | |
| 129 | + if (val.tag != DerValue.tag_Sequence) { | |
| 130 | + throw new InvalidKeyException ("Key not a SEQUENCE"); | |
| 131 | + } | |
| 132 | + | |
| 133 | + // | |
| 134 | + // version | |
| 135 | + // | |
| 136 | + BigInteger parsedVersion = val.data.getBigInteger(); | |
| 137 | + if (!parsedVersion.equals(PKCS8_VERSION)) { | |
| 138 | + throw new IOException("version mismatch: (supported: " + | |
| 139 | + PKCS8_VERSION + ", parsed: " + | |
| 140 | + parsedVersion); | |
| 141 | + } | |
| 142 | + | |
| 143 | + // | |
| 144 | + // privateKeyAlgorithm | |
| 145 | + // | |
| 146 | + DerValue algid = val.data.getDerValue(); | |
| 147 | + if (algid.tag != DerValue.tag_Sequence) { | |
| 148 | + throw new InvalidKeyException("AlgId is not a SEQUENCE"); | |
| 149 | + } | |
| 150 | + DerInputStream derInStream = algid.toDerInputStream(); | |
| 151 | + ObjectIdentifier oid = derInStream.getOID(); | |
| 152 | + if (oid == null) { | |
| 153 | + throw new InvalidKeyException("Null OID"); | |
| 154 | + } | |
| 155 | + if (derInStream.available() == 0) { | |
| 156 | + throw new InvalidKeyException("Parameters missing"); | |
| 157 | + } | |
| 158 | + // parse the parameters | |
| 159 | + DerValue params = derInStream.getDerValue(); | |
| 160 | + if (params.tag == DerValue.tag_Null) { | |
| 161 | + throw new InvalidKeyException("Null parameters"); | |
| 162 | + } | |
| 163 | + if (params.tag != DerValue.tag_Sequence) { | |
| 164 | + throw new InvalidKeyException("Parameters not a SEQUENCE"); | |
| 165 | + } | |
| 166 | + params.data.reset(); | |
| 167 | + this.p = params.data.getBigInteger(); | |
| 168 | + this.g = params.data.getBigInteger(); | |
| 169 | + // Private-value length is OPTIONAL | |
| 170 | + if (params.data.available() != 0) { | |
| 171 | + this.l = params.data.getInteger(); | |
| 172 | + } | |
| 173 | + if (params.data.available() != 0) { | |
| 174 | + throw new InvalidKeyException("Extra parameter data"); | |
| 175 | + } | |
| 176 | + | |
| 177 | + // | |
| 178 | + // privateKey | |
| 179 | + // | |
| 180 | + this.key = val.data.getOctetString(); | |
| 181 | + parseKeyBits(); | |
| 182 | + | |
| 183 | + // ignore OPTIONAL attributes | |
| 184 | + | |
| 185 | + this.encodedKey = (byte[])encodedKey.clone(); | |
| 186 | + | |
| 187 | + } catch (NumberFormatException e) { | |
| 188 | + InvalidKeyException ike = new InvalidKeyException( | |
| 189 | + "Private-value length too big"); | |
| 190 | + ike.initCause(e); | |
| 191 | + throw ike; | |
| 192 | + } catch (IOException e) { | |
| 193 | + InvalidKeyException ike = new InvalidKeyException( | |
| 194 | + "Error parsing key encoding: " + e.getMessage()); | |
| 195 | + ike.initCause(e); | |
| 196 | + throw ike; | |
| 197 | + } | |
| 198 | + } | |
| 199 | + | |
| 200 | + /** | |
| 201 | + * Returns the encoding format of this key: "PKCS#8" | |
| 202 | + */ | |
| 203 | + public String getFormat() { | |
| 204 | + return "PKCS#8"; | |
| 205 | + } | |
| 206 | + | |
| 207 | + /** | |
| 208 | + * Returns the name of the algorithm associated with this key: "DH" | |
| 209 | + */ | |
| 210 | + public String getAlgorithm() { | |
| 211 | + return "DH"; | |
| 212 | + } | |
| 213 | + | |
| 214 | + /** | |
| 215 | + * Get the encoding of the key. | |
| 216 | + */ | |
| 217 | + public synchronized byte[] getEncoded() { | |
| 218 | + if (this.encodedKey == null) { | |
| 219 | + try { | |
| 220 | + DerOutputStream tmp = new DerOutputStream(); | |
| 221 | + | |
| 222 | + // | |
| 223 | + // version | |
| 224 | + // | |
| 225 | + tmp.putInteger(PKCS8_VERSION); | |
| 226 | + | |
| 227 | + // | |
| 228 | + // privateKeyAlgorithm | |
| 229 | + // | |
| 230 | + DerOutputStream algid = new DerOutputStream(); | |
| 231 | + | |
| 232 | + // store OID | |
| 233 | + algid.putOID(new ObjectIdentifier(DH_data)); | |
| 234 | + // encode parameters | |
| 235 | + DerOutputStream params = new DerOutputStream(); | |
| 236 | + params.putInteger(this.p); | |
| 237 | + params.putInteger(this.g); | |
| 238 | + if (this.l != 0) | |
| 239 | + params.putInteger(this.l); | |
| 240 | + // wrap parameters into SEQUENCE | |
| 241 | + DerValue paramSequence = new DerValue(DerValue.tag_Sequence, | |
| 242 | + params.toByteArray()); | |
| 243 | + // store parameter SEQUENCE in algid | |
| 244 | + algid.putDerValue(paramSequence); | |
| 245 | + // wrap algid into SEQUENCE | |
| 246 | + tmp.write(DerValue.tag_Sequence, algid); | |
| 247 | + | |
| 248 | + // privateKey | |
| 249 | + tmp.putOctetString(this.key); | |
| 250 | + | |
| 251 | + // make it a SEQUENCE | |
| 252 | + DerOutputStream derKey = new DerOutputStream(); | |
| 253 | + derKey.write(DerValue.tag_Sequence, tmp); | |
| 254 | + this.encodedKey = derKey.toByteArray(); | |
| 255 | + } catch (IOException e) { | |
| 256 | + return null; | |
| 257 | + } | |
| 258 | + } | |
| 259 | + return (byte[])this.encodedKey.clone(); | |
| 260 | + } | |
| 261 | + | |
| 262 | + /** | |
| 263 | + * Returns the private value, <code>x</code>. | |
| 264 | + * | |
| 265 | + * @return the private value, <code>x</code> | |
| 266 | + */ | |
| 267 | + public BigInteger getX() { | |
| 268 | + return this.x; | |
| 269 | + } | |
| 270 | + | |
| 271 | + /** | |
| 272 | + * Returns the key parameters. | |
| 273 | + * | |
| 274 | + * @return the key parameters | |
| 275 | + */ | |
| 276 | + public DHParameterSpec getParams() { | |
| 277 | + if (this.l != 0) | |
| 278 | + return new DHParameterSpec(this.p, this.g, this.l); | |
| 279 | + else | |
| 280 | + return new DHParameterSpec(this.p, this.g); | |
| 281 | + } | |
| 282 | + | |
| 283 | + public String toString() { | |
| 284 | + String LINE_SEP = System.getProperty("line.separator"); | |
| 285 | + | |
| 286 | + StringBuffer strbuf | |
| 287 | + = new StringBuffer("SunJCE Diffie-Hellman Private Key:" | |
| 288 | + + LINE_SEP + "x:" + LINE_SEP | |
| 289 | + + Debug.toHexString(this.x) | |
| 290 | + + LINE_SEP + "p:" + LINE_SEP | |
| 291 | + + Debug.toHexString(this.p) | |
| 292 | + + LINE_SEP + "g:" + LINE_SEP | |
| 293 | + + Debug.toHexString(this.g)); | |
| 294 | + if (this.l != 0) | |
| 295 | + strbuf.append(LINE_SEP + "l:" + LINE_SEP + " " + this.l); | |
| 296 | + return strbuf.toString(); | |
| 297 | + } | |
| 298 | + | |
| 299 | + private void parseKeyBits() throws InvalidKeyException { | |
| 300 | + try { | |
| 301 | + DerInputStream in = new DerInputStream(this.key); | |
| 302 | + this.x = in.getBigInteger(); | |
| 303 | + } catch (IOException e) { | |
| 304 | + InvalidKeyException ike = new InvalidKeyException( | |
| 305 | + "Error parsing key encoding: " + e.getMessage()); | |
| 306 | + ike.initCause(e); | |
| 307 | + throw ike; | |
| 308 | + } | |
| 309 | + } | |
| 310 | + | |
| 311 | + /** | |
| 312 | + * Calculates a hash code value for the object. | |
| 313 | + * Objects that are equal will also have the same hashcode. | |
| 314 | + */ | |
| 315 | + public int hashCode() { | |
| 316 | + int retval = 0; | |
| 317 | + byte[] enc = getEncoded(); | |
| 318 | + | |
| 319 | + for (int i = 1; i < enc.length; i++) { | |
| 320 | + retval += enc[i] * i; | |
| 321 | + } | |
| 322 | + return(retval); | |
| 323 | + } | |
| 324 | + | |
| 325 | + public boolean equals(Object obj) { | |
| 326 | + if (this == obj) | |
| 327 | + return true; | |
| 328 | + | |
| 329 | + if (!(obj instanceof PrivateKey)) | |
| 330 | + return false; | |
| 331 | + | |
| 332 | + byte[] thisEncoded = this.getEncoded(); | |
| 333 | + byte[] thatEncoded = ((PrivateKey)obj).getEncoded(); | |
| 334 | + | |
| 335 | + return java.util.Arrays.equals(thisEncoded, thatEncoded); | |
| 336 | + } | |
| 337 | + | |
| 338 | + /** | |
| 339 | + * Replace the DH private key to be serialized. | |
| 340 | + * | |
| 341 | + * @return the standard KeyRep object to be serialized | |
| 342 | + * | |
| 343 | + * @throws java.io.ObjectStreamException if a new object representing | |
| 344 | + * this DH private key could not be created | |
| 345 | + */ | |
| 346 | + private Object writeReplace() throws java.io.ObjectStreamException { | |
| 347 | + return new KeyRep(KeyRep.Type.PRIVATE, | |
| 348 | + getAlgorithm(), | |
| 349 | + getFormat(), | |
| 350 | + getEncoded()); | |
| 351 | + } | |
| 352 | +} | |