KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > jce > provider > JCEDHPublicKey


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.jce.provider;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.io.ObjectOutputStream JavaDoc;
24 import java.math.BigInteger JavaDoc;
25
26 import javax.crypto.interfaces.DHPublicKey;
27 import javax.crypto.spec.DHParameterSpec;
28 import javax.crypto.spec.DHPublicKeySpec;
29
30 import org.apache.geronimo.util.asn1.ASN1Sequence;
31 import org.apache.geronimo.util.asn1.DERInteger;
32 import org.apache.geronimo.util.asn1.DEROutputStream;
33 import org.apache.geronimo.util.asn1.pkcs.DHParameter;
34 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
35 import org.apache.geronimo.util.asn1.x509.SubjectPublicKeyInfo;
36 import org.apache.geronimo.util.asn1.x9.X9ObjectIdentifiers;
37 import org.apache.geronimo.util.crypto.params.DHPublicKeyParameters;
38
39 public class JCEDHPublicKey
40     implements DHPublicKey
41 {
42     private BigInteger JavaDoc y;
43     private DHParameterSpec dhSpec;
44
45     JCEDHPublicKey(
46         DHPublicKeySpec spec)
47     {
48         this.y = spec.getY();
49         this.dhSpec = new DHParameterSpec(spec.getP(), spec.getG());
50     }
51
52     JCEDHPublicKey(
53         DHPublicKey key)
54     {
55         this.y = key.getY();
56         this.dhSpec = key.getParams();
57     }
58
59     JCEDHPublicKey(
60         DHPublicKeyParameters params)
61     {
62         this.y = params.getY();
63         this.dhSpec = new DHParameterSpec(params.getParameters().getP(), params.getParameters().getG(), 0);
64     }
65
66     JCEDHPublicKey(
67         BigInteger JavaDoc y,
68         DHParameterSpec dhSpec)
69     {
70         this.y = y;
71         this.dhSpec = dhSpec;
72     }
73
74     JCEDHPublicKey(
75         SubjectPublicKeyInfo info)
76     {
77         DHParameter params = new DHParameter((ASN1Sequence)info.getAlgorithmId().getParameters());
78         DERInteger derY = null;
79
80         try
81         {
82             derY = (DERInteger)info.getPublicKey();
83         }
84         catch (IOException JavaDoc e)
85         {
86             throw new IllegalArgumentException JavaDoc("invalid info structure in DH public key");
87         }
88
89         this.y = derY.getValue();
90         if (params.getL() != null)
91         {
92             this.dhSpec = new DHParameterSpec(params.getP(), params.getG(), params.getL().intValue());
93         }
94         else
95         {
96             this.dhSpec = new DHParameterSpec(params.getP(), params.getG());
97         }
98     }
99
100     public String JavaDoc getAlgorithm()
101     {
102         return "DH";
103     }
104
105     public String JavaDoc getFormat()
106     {
107         return "X.509";
108     }
109
110     public byte[] getEncoded()
111     {
112         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
113         DEROutputStream dOut = new DEROutputStream(bOut);
114         SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.dhpublicnumber, new DHParameter(dhSpec.getP(), dhSpec.getG(), dhSpec.getL()).getDERObject()), new DERInteger(y));
115
116         try
117         {
118             dOut.writeObject(info);
119             dOut.close();
120         }
121         catch (IOException JavaDoc e)
122         {
123             throw new RuntimeException JavaDoc("Error encoding DH public key");
124         }
125
126         return bOut.toByteArray();
127
128     }
129
130     public DHParameterSpec getParams()
131     {
132         return dhSpec;
133     }
134
135     public BigInteger JavaDoc getY()
136     {
137         return y;
138     }
139
140     private void readObject(
141         ObjectInputStream JavaDoc in)
142         throws IOException JavaDoc, ClassNotFoundException JavaDoc
143     {
144         this.y = (BigInteger JavaDoc)in.readObject();
145         this.dhSpec = new DHParameterSpec((BigInteger JavaDoc)in.readObject(), (BigInteger JavaDoc)in.readObject(), in.readInt());
146     }
147
148     private void writeObject(
149         ObjectOutputStream JavaDoc out)
150         throws IOException JavaDoc
151     {
152         out.writeObject(this.getY());
153         out.writeObject(dhSpec.getP());
154         out.writeObject(dhSpec.getG());
155         out.writeInt(dhSpec.getL());
156     }
157 }
158
Popular Tags