1 /*
   2  * Copyright (c) 1997, 2014, 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 sun.security.util.*;
  30 import java.math.BigInteger;
  31 import java.security.AlgorithmParametersSpi;
  32 import java.security.spec.AlgorithmParameterSpec;
  33 import java.security.spec.InvalidParameterSpecException;
  34 import javax.crypto.spec.DHParameterSpec;
  35 
  36 /**
  37  * This class implements the parameter set used by the
  38  * Diffie-Hellman key agreement as defined in the PKCS #3 standard.
  39  *
  40  * @author Jan Luehe
  41  *
  42  */
  43 public final class DHParameters extends AlgorithmParametersSpi {
  44 
  45     // The prime (p).
  46     private BigInteger p = BigInteger.ZERO;
  47 
  48     // The base (g).
  49     private BigInteger g = BigInteger.ZERO;
  50 
  51     // The private-value length (l)
  52     private int l = 0;
  53 
  54     protected void engineInit(AlgorithmParameterSpec paramSpec)
  55         throws InvalidParameterSpecException {
  56             if (!(paramSpec instanceof DHParameterSpec)) {
  57                 throw new InvalidParameterSpecException
  58                     ("Inappropriate parameter specification");
  59             }
  60             this.p = ((DHParameterSpec)paramSpec).getP();
  61             this.g = ((DHParameterSpec)paramSpec).getG();
  62             this.l = ((DHParameterSpec)paramSpec).getL();
  63     }
  64 
  65     protected void engineInit(byte[] params) throws IOException {
  66         try {
  67             DerValue encodedParams = new DerValue(params);
  68 
  69             if (encodedParams.tag != DerValue.tag_Sequence) {
  70                 throw new IOException("DH params parsing error");
  71             }
  72 
  73             encodedParams.data.reset();
  74 
  75             this.p = encodedParams.data.getBigInteger();
  76             this.g = encodedParams.data.getBigInteger();
  77 
  78             // Private-value length is OPTIONAL
  79             if (encodedParams.data.available() != 0) {
  80                 this.l = encodedParams.data.getInteger();
  81             }
  82 
  83             if (encodedParams.data.available() != 0) {
  84                 throw new IOException
  85                     ("DH parameter parsing error: Extra data");
  86             }
  87         } catch (NumberFormatException e) {
  88             throw new IOException("Private-value length too big");
  89         }
  90     }
  91 
  92     protected void engineInit(byte[] params, String decodingMethod)
  93         throws IOException {
  94             engineInit(params);
  95     }
  96 
  97     protected <T extends AlgorithmParameterSpec>
  98         T engineGetParameterSpec(Class<T> paramSpec)
  99         throws InvalidParameterSpecException {
 100 
 101         if (DHParameterSpec.class.isAssignableFrom(paramSpec)) {
 102             return paramSpec.cast(new DHParameterSpec(this.p, this.g, this.l));
 103         } else {
 104             throw new InvalidParameterSpecException
 105                 ("Inappropriate parameter Specification");
 106         }
 107     }
 108 
 109     protected byte[] engineGetEncoded() throws IOException {
 110         DerOutputStream out = new DerOutputStream();
 111         DerOutputStream bytes = new DerOutputStream();
 112 
 113         bytes.putInteger(this.p);
 114         bytes.putInteger(this.g);
 115         // Private-value length is OPTIONAL
 116         if (this.l > 0) {
 117             bytes.putInteger(this.l);
 118         }
 119         out.write(DerValue.tag_Sequence, bytes);
 120         return out.toByteArray();
 121     }
 122 
 123     protected byte[] engineGetEncoded(String encodingMethod)
 124         throws IOException {
 125             return engineGetEncoded();
 126     }
 127 
 128     /*
 129      * Returns a formatted string describing the parameters.
 130      */
 131     protected String engineToString() {
 132         String LINE_SEP = System.getProperty("line.separator");
 133 
 134         StringBuilder sb
 135             = new StringBuilder("SunJCE Diffie-Hellman Parameters:"
 136                                + LINE_SEP + "p:" + LINE_SEP
 137                                + Debug.toHexString(this.p)
 138                                + LINE_SEP + "g:" + LINE_SEP
 139                                + Debug.toHexString(this.g));
 140         if (this.l != 0)
 141             sb.append(LINE_SEP + "l:" + LINE_SEP + "    " + this.l);
 142         return sb.toString();
 143     }
 144 }