KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Enumeration JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import javax.crypto.interfaces.DHPrivateKey;
30 import javax.crypto.spec.DHParameterSpec;
31 import javax.crypto.spec.DHPrivateKeySpec;
32
33 import org.apache.geronimo.util.asn1.ASN1Sequence;
34 import org.apache.geronimo.util.asn1.DEREncodable;
35 import org.apache.geronimo.util.asn1.DERInteger;
36 import org.apache.geronimo.util.asn1.DERObjectIdentifier;
37 import org.apache.geronimo.util.asn1.DEROutputStream;
38 import org.apache.geronimo.util.asn1.pkcs.DHParameter;
39 import org.apache.geronimo.util.asn1.pkcs.PKCSObjectIdentifiers;
40 import org.apache.geronimo.util.asn1.pkcs.PrivateKeyInfo;
41 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
42 import org.apache.geronimo.util.crypto.params.DHPrivateKeyParameters;
43 import org.apache.geronimo.util.jce.interfaces.PKCS12BagAttributeCarrier;
44
45 public class JCEDHPrivateKey
46     implements DHPrivateKey, PKCS12BagAttributeCarrier
47 {
48     BigInteger JavaDoc x;
49
50     DHParameterSpec dhSpec;
51
52     private Hashtable JavaDoc pkcs12Attributes = new Hashtable JavaDoc();
53     private Vector JavaDoc pkcs12Ordering = new Vector JavaDoc();
54
55     protected JCEDHPrivateKey()
56     {
57     }
58
59     JCEDHPrivateKey(
60         DHPrivateKey key)
61     {
62         this.x = key.getX();
63         this.dhSpec = key.getParams();
64     }
65
66     JCEDHPrivateKey(
67         DHPrivateKeySpec spec)
68     {
69         this.x = spec.getX();
70         this.dhSpec = new DHParameterSpec(spec.getP(), spec.getG());
71     }
72
73     JCEDHPrivateKey(
74         PrivateKeyInfo info)
75     {
76         DHParameter params = new DHParameter((ASN1Sequence)info.getAlgorithmId().getParameters());
77         DERInteger derX = (DERInteger)info.getPrivateKey();
78
79         this.x = derX.getValue();
80         if (params.getL() != null)
81         {
82             this.dhSpec = new DHParameterSpec(params.getP(), params.getG(), params.getL().intValue());
83         }
84         else
85         {
86             this.dhSpec = new DHParameterSpec(params.getP(), params.getG());
87         }
88     }
89
90     JCEDHPrivateKey(
91         DHPrivateKeyParameters params)
92     {
93         this.x = params.getX();
94         this.dhSpec = new DHParameterSpec(params.getParameters().getP(), params.getParameters().getG());
95     }
96
97     public String JavaDoc getAlgorithm()
98     {
99         return "DH";
100     }
101
102     /**
103      * return the encoding format we produce in getEncoded().
104      *
105      * @return the string "PKCS#8"
106      */

107     public String JavaDoc getFormat()
108     {
109         return "PKCS#8";
110     }
111
112     /**
113      * Return a PKCS8 representation of the key. The sequence returned
114      * represents a full PrivateKeyInfo object.
115      *
116      * @return a PKCS8 representation of the key.
117      */

118     public byte[] getEncoded()
119     {
120         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
121         DEROutputStream dOut = new DEROutputStream(bOut);
122         PrivateKeyInfo info = new PrivateKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.dhKeyAgreement, new DHParameter(dhSpec.getP(), dhSpec.getG(), dhSpec.getL()).getDERObject()), new DERInteger(getX()));
123
124         try
125         {
126             dOut.writeObject(info);
127             dOut.close();
128         }
129         catch (IOException JavaDoc e)
130         {
131             throw new RuntimeException JavaDoc("Error encoding DH private key");
132         }
133
134         return bOut.toByteArray();
135     }
136
137     public DHParameterSpec getParams()
138     {
139         return dhSpec;
140     }
141
142     public BigInteger JavaDoc getX()
143     {
144         return x;
145     }
146
147     private void readObject(
148         ObjectInputStream JavaDoc in)
149         throws IOException JavaDoc, ClassNotFoundException JavaDoc
150     {
151         x = (BigInteger JavaDoc)in.readObject();
152
153         this.dhSpec = new DHParameterSpec((BigInteger JavaDoc)in.readObject(), (BigInteger JavaDoc)in.readObject(), in.readInt());
154     }
155
156     private void writeObject(
157         ObjectOutputStream JavaDoc out)
158         throws IOException JavaDoc
159     {
160         out.writeObject(this.getX());
161         out.writeObject(dhSpec.getP());
162         out.writeObject(dhSpec.getG());
163         out.writeInt(dhSpec.getL());
164     }
165
166     public void setBagAttribute(
167         DERObjectIdentifier oid,
168         DEREncodable attribute)
169     {
170         pkcs12Attributes.put(oid, attribute);
171         pkcs12Ordering.addElement(oid);
172     }
173
174     public DEREncodable getBagAttribute(
175         DERObjectIdentifier oid)
176     {
177         return (DEREncodable)pkcs12Attributes.get(oid);
178     }
179
180     public Enumeration JavaDoc getBagAttributeKeys()
181     {
182         return pkcs12Ordering.elements();
183     }
184 }
185
Popular Tags