History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/crypto/provider/DHParameters.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/crypto/provider/DHParameters.java+144−0
@@ -0,0 +1,144 @@
1+/*
2+ * Copyright (c) 1997, 2007, 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.util.*;
29+import java.io.*;
30+import sun.security.util.*;
31+import java.math.BigInteger;
32+import java.security.AlgorithmParametersSpi;
33+import java.security.spec.AlgorithmParameterSpec;
34+import java.security.spec.InvalidParameterSpecException;
35+import javax.crypto.spec.DHParameterSpec;
36+
37+/**
38+ * This class implements the parameter set used by the
39+ * Diffie-Hellman key agreement as defined in the PKCS #3 standard.
40+ *
41+ * @author Jan Luehe
42+ *
43+ */
44+public final class DHParameters extends AlgorithmParametersSpi {
45+
46+ // The prime (p).
47+ private BigInteger p = BigInteger.ZERO;
48+
49+ // The base (g).
50+ private BigInteger g = BigInteger.ZERO;
51+
52+ // The private-value length (l)
53+ private int l = 0;
54+
55+ protected void engineInit(AlgorithmParameterSpec paramSpec)
56+ throws InvalidParameterSpecException {
57+ if (!(paramSpec instanceof DHParameterSpec)) {
58+ throw new InvalidParameterSpecException
59+ ("Inappropriate parameter specification");
60+ }
61+ this.p = ((DHParameterSpec)paramSpec).getP();
62+ this.g = ((DHParameterSpec)paramSpec).getG();
63+ this.l = ((DHParameterSpec)paramSpec).getL();
64+ }
65+
66+ protected void engineInit(byte[] params) throws IOException {
67+ try {
68+ DerValue encodedParams = new DerValue(params);
69+
70+ if (encodedParams.tag != DerValue.tag_Sequence) {
71+ throw new IOException("DH params parsing error");
72+ }
73+
74+ encodedParams.data.reset();
75+
76+ this.p = encodedParams.data.getBigInteger();
77+ this.g = encodedParams.data.getBigInteger();
78+
79+ // Private-value length is OPTIONAL
80+ if (encodedParams.data.available() != 0) {
81+ this.l = encodedParams.data.getInteger();
82+ }
83+
84+ if (encodedParams.data.available() != 0) {
85+ throw new IOException
86+ ("DH parameter parsing error: Extra data");
87+ }
88+ } catch (NumberFormatException e) {
89+ throw new IOException("Private-value length too big");
90+ }
91+ }
92+
93+ protected void engineInit(byte[] params, String decodingMethod)
94+ throws IOException {
95+ engineInit(params);
96+ }
97+
98+ protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
99+ throws InvalidParameterSpecException {
100+
101+ if (DHParameterSpec.class.isAssignableFrom(paramSpec)) {
102+ return 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+ StringBuffer strbuf
135+ = new StringBuffer("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+ strbuf.append(LINE_SEP + "l:" + LINE_SEP + " " + this.l);
142+ return strbuf.toString();
143+ }
144+}