Save This Page
Home » openjdk-7 » com.sun.crypto » provider » [javadoc | source]
    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.InvalidKeyException;
   32   import java.security.InvalidAlgorithmParameterException;
   33   import java.security.ProviderException;
   34   import java.security.PublicKey;
   35   import javax.crypto;
   36   import javax.crypto.spec.DHParameterSpec;
   37   import sun.security.util;
   38   
   39   
   40   /**
   41    * A public key in X.509 format for the Diffie-Hellman key agreement algorithm.
   42    *
   43    * @author Jan Luehe
   44    *
   45    *
   46    * @see DHPrivateKey
   47    * @see java.security.KeyAgreement
   48    */
   49   final class DHPublicKey implements PublicKey,
   50   javax.crypto.interfaces.DHPublicKey, Serializable {
   51   
   52       static final long serialVersionUID = 7647557958927458271L;
   53   
   54       // the public key
   55       private BigInteger y;
   56   
   57       // the key bytes, without the algorithm information
   58       private byte[] key;
   59   
   60       // the encoded key
   61       private byte[] encodedKey;
   62   
   63       // the prime modulus
   64       private BigInteger p;
   65   
   66       // the base generator
   67       private BigInteger g;
   68   
   69       // the private-value length
   70       private int l;
   71   
   72       private int DH_data[] = { 1, 2, 840, 113549, 1, 3, 1 };
   73   
   74       /**
   75        * Make a DH public key out of a public value <code>y</code>, a prime
   76        * modulus <code>p</code>, and a base generator <code>g</code>.
   77        *
   78        * @param y the public value
   79        * @param p the prime modulus
   80        * @param g the base generator
   81        *
   82        * @exception InvalidKeyException if the key cannot be encoded
   83        */
   84       DHPublicKey(BigInteger y, BigInteger p, BigInteger g)
   85           throws InvalidKeyException {
   86           this(y, p, g, 0);
   87       }
   88   
   89       /**
   90        * Make a DH public key out of a public value <code>y</code>, a prime
   91        * modulus <code>p</code>, a base generator <code>g</code>, and a
   92        * private-value length <code>l</code>.
   93        *
   94        * @param y the public value
   95        * @param p the prime modulus
   96        * @param g the base generator
   97        * @param l the private-value length
   98        *
   99        * @exception ProviderException if the key cannot be encoded
  100        */
  101       DHPublicKey(BigInteger y, BigInteger p, BigInteger g, int l) {
  102           this.y = y;
  103           this.p = p;
  104           this.g = g;
  105           this.l = l;
  106           try {
  107               this.key = new DerValue(DerValue.tag_Integer,
  108                                       this.y.toByteArray()).toByteArray();
  109               this.encodedKey = getEncoded();
  110           } catch (IOException e) {
  111               throw new ProviderException("Cannot produce ASN.1 encoding", e);
  112           }
  113       }
  114   
  115       /**
  116        * Make a DH public key from its DER encoding (X.509).
  117        *
  118        * @param encodedKey the encoded key
  119        *
  120        * @exception InvalidKeyException if the encoded key does not represent
  121        * a Diffie-Hellman public key
  122        */
  123       DHPublicKey(byte[] encodedKey) throws InvalidKeyException {
  124           InputStream inStream = new ByteArrayInputStream(encodedKey);
  125           try {
  126               DerValue derKeyVal = new DerValue(inStream);
  127               if (derKeyVal.tag != DerValue.tag_Sequence) {
  128                   throw new InvalidKeyException ("Invalid key format");
  129               }
  130   
  131               /*
  132                * Parse the algorithm identifier
  133                */
  134               DerValue algid = derKeyVal.data.getDerValue();
  135               if (algid.tag != DerValue.tag_Sequence) {
  136                   throw new InvalidKeyException("AlgId is not a SEQUENCE");
  137               }
  138               DerInputStream derInStream = algid.toDerInputStream();
  139               ObjectIdentifier oid = derInStream.getOID();
  140               if (oid == null) {
  141                   throw new InvalidKeyException("Null OID");
  142               }
  143               if (derInStream.available() == 0) {
  144                   throw new InvalidKeyException("Parameters missing");
  145               }
  146   
  147               /*
  148                * Parse the parameters
  149                */
  150               DerValue params = derInStream.getDerValue();
  151               if (params.tag == DerValue.tag_Null) {
  152                   throw new InvalidKeyException("Null parameters");
  153               }
  154               if (params.tag != DerValue.tag_Sequence) {
  155                   throw new InvalidKeyException("Parameters not a SEQUENCE");
  156               }
  157               params.data.reset();
  158               this.p = params.data.getBigInteger();
  159               this.g = params.data.getBigInteger();
  160               // Private-value length is OPTIONAL
  161               if (params.data.available() != 0) {
  162                   this.l = params.data.getInteger();
  163               }
  164               if (params.data.available() != 0) {
  165                   throw new InvalidKeyException("Extra parameter data");
  166               }
  167   
  168               /*
  169                * Parse the key
  170                */
  171               this.key = derKeyVal.data.getBitString();
  172               parseKeyBits();
  173               if (derKeyVal.data.available() != 0) {
  174                   throw new InvalidKeyException("Excess key data");
  175               }
  176   
  177               this.encodedKey = (byte[])encodedKey.clone();
  178   
  179           } catch (NumberFormatException e) {
  180               throw new InvalidKeyException("Private-value length too big");
  181   
  182           } catch (IOException e) {
  183               throw new InvalidKeyException(
  184                   "Error parsing key encoding: " + e.toString());
  185           }
  186       }
  187   
  188       /**
  189        * Returns the encoding format of this key: "X.509"
  190        */
  191       public String getFormat() {
  192           return "X.509";
  193       }
  194   
  195       /**
  196        * Returns the name of the algorithm associated with this key: "DH"
  197        */
  198       public String getAlgorithm() {
  199           return "DH";
  200       }
  201   
  202       /**
  203        * Get the encoding of the key.
  204        */
  205       public synchronized byte[] getEncoded() {
  206           if (this.encodedKey == null) {
  207               try {
  208                   DerOutputStream algid = new DerOutputStream();
  209   
  210                   // store oid in algid
  211                   algid.putOID(new ObjectIdentifier(DH_data));
  212   
  213                   // encode parameters
  214                   DerOutputStream params = new DerOutputStream();
  215                   params.putInteger(this.p);
  216                   params.putInteger(this.g);
  217                   if (this.l != 0)
  218                       params.putInteger(this.l);
  219                   // wrap parameters into SEQUENCE
  220                   DerValue paramSequence = new DerValue(DerValue.tag_Sequence,
  221                                                         params.toByteArray());
  222                   // store parameter SEQUENCE in algid
  223                   algid.putDerValue(paramSequence);
  224   
  225                   // wrap algid into SEQUENCE, and store it in key encoding
  226                   DerOutputStream tmpDerKey = new DerOutputStream();
  227                   tmpDerKey.write(DerValue.tag_Sequence, algid);
  228   
  229                   // store key data
  230                   tmpDerKey.putBitString(this.key);
  231   
  232                   // wrap algid and key into SEQUENCE
  233                   DerOutputStream derKey = new DerOutputStream();
  234                   derKey.write(DerValue.tag_Sequence, tmpDerKey);
  235                   this.encodedKey = derKey.toByteArray();
  236               } catch (IOException e) {
  237                   return null;
  238               }
  239           }
  240           return (byte[])this.encodedKey.clone();
  241       }
  242   
  243       /**
  244        * Returns the public value, <code>y</code>.
  245        *
  246        * @return the public value, <code>y</code>
  247        */
  248       public BigInteger getY() {
  249           return this.y;
  250       }
  251   
  252       /**
  253        * Returns the key parameters.
  254        *
  255        * @return the key parameters
  256        */
  257       public DHParameterSpec getParams() {
  258           if (this.l != 0)
  259               return new DHParameterSpec(this.p, this.g, this.l);
  260           else
  261               return new DHParameterSpec(this.p, this.g);
  262       }
  263   
  264       public String toString() {
  265           String LINE_SEP = System.getProperty("line.separator");
  266   
  267           StringBuffer strbuf
  268               = new StringBuffer("SunJCE Diffie-Hellman Public Key:"
  269                                  + LINE_SEP + "y:" + LINE_SEP
  270                                  + Debug.toHexString(this.y)
  271                                  + LINE_SEP + "p:" + LINE_SEP
  272                                  + Debug.toHexString(this.p)
  273                                  + LINE_SEP + "g:" + LINE_SEP
  274                                  + Debug.toHexString(this.g));
  275           if (this.l != 0)
  276               strbuf.append(LINE_SEP + "l:" + LINE_SEP + "    " + this.l);
  277           return strbuf.toString();
  278       }
  279   
  280       private void parseKeyBits() throws InvalidKeyException {
  281           try {
  282               DerInputStream in = new DerInputStream(this.key);
  283               this.y = in.getBigInteger();
  284           } catch (IOException e) {
  285               throw new InvalidKeyException(
  286                   "Error parsing key encoding: " + e.toString());
  287           }
  288       }
  289   
  290       /**
  291        * Calculates a hash code value for the object.
  292        * Objects that are equal will also have the same hashcode.
  293        */
  294       public int hashCode() {
  295           int retval = 0;
  296           byte[] enc = getEncoded();
  297   
  298           for (int i = 1; i < enc.length; i++) {
  299               retval += enc[i] * i;
  300           }
  301           return(retval);
  302       }
  303   
  304       public boolean equals(Object obj) {
  305           if (this == obj)
  306               return true;
  307   
  308           if (!(obj instanceof PublicKey))
  309               return false;
  310   
  311           byte[] thisEncoded = this.getEncoded();
  312           byte[] thatEncoded = ((PublicKey)obj).getEncoded();
  313   
  314           return java.util.Arrays.equals(thisEncoded, thatEncoded);
  315       }
  316   
  317       /**
  318        * Replace the DH public key to be serialized.
  319        *
  320        * @return the standard KeyRep object to be serialized
  321        *
  322        * @throws java.io.ObjectStreamException if a new object representing
  323        * this DH public key could not be created
  324        */
  325       private Object writeReplace() throws java.io.ObjectStreamException {
  326           return new KeyRep(KeyRep.Type.PUBLIC,
  327                           getAlgorithm(),
  328                           getFormat(),
  329                           getEncoded());
  330       }
  331   }

Save This Page
Home » openjdk-7 » com.sun.crypto » provider » [javadoc | source]