KickJava   Java API By Example, From Geeks To Geeks.

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


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.math.BigInteger JavaDoc;
23 import java.security.interfaces.DSAParams JavaDoc;
24 import java.security.interfaces.DSAPrivateKey JavaDoc;
25 import java.security.spec.DSAParameterSpec JavaDoc;
26 import java.security.spec.DSAPrivateKeySpec JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import org.apache.geronimo.util.asn1.ASN1Sequence;
32 import org.apache.geronimo.util.asn1.DEREncodable;
33 import org.apache.geronimo.util.asn1.DERInteger;
34 import org.apache.geronimo.util.asn1.DERObjectIdentifier;
35 import org.apache.geronimo.util.asn1.DEROutputStream;
36 import org.apache.geronimo.util.asn1.pkcs.PrivateKeyInfo;
37 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
38 import org.apache.geronimo.util.asn1.x509.DSAParameter;
39 import org.apache.geronimo.util.asn1.x9.X9ObjectIdentifiers;
40 import org.apache.geronimo.util.crypto.params.DSAPrivateKeyParameters;
41 import org.apache.geronimo.util.jce.interfaces.PKCS12BagAttributeCarrier;
42
43 public class JDKDSAPrivateKey
44     implements DSAPrivateKey JavaDoc, PKCS12BagAttributeCarrier
45 {
46     BigInteger JavaDoc x;
47     DSAParams JavaDoc dsaSpec;
48
49     private Hashtable JavaDoc pkcs12Attributes = new Hashtable JavaDoc();
50     private Vector JavaDoc pkcs12Ordering = new Vector JavaDoc();
51
52     protected JDKDSAPrivateKey()
53     {
54     }
55
56     JDKDSAPrivateKey(
57         DSAPrivateKey JavaDoc key)
58     {
59         this.x = key.getX();
60         this.dsaSpec = key.getParams();
61     }
62
63     JDKDSAPrivateKey(
64         DSAPrivateKeySpec JavaDoc spec)
65     {
66         this.x = spec.getX();
67         this.dsaSpec = new DSAParameterSpec JavaDoc(spec.getP(), spec.getQ(), spec.getG());
68     }
69
70     JDKDSAPrivateKey(
71         PrivateKeyInfo info)
72     {
73         DSAParameter params = new DSAParameter((ASN1Sequence)info.getAlgorithmId().getParameters());
74         DERInteger derX = (DERInteger)info.getPrivateKey();
75
76         this.x = derX.getValue();
77         this.dsaSpec = new DSAParameterSpec JavaDoc(params.getP(), params.getQ(), params.getG());
78     }
79
80     JDKDSAPrivateKey(
81         DSAPrivateKeyParameters params)
82     {
83         this.x = params.getX();
84         this.dsaSpec = new DSAParameterSpec JavaDoc(params.getParameters().getP(), params.getParameters().getQ(), params.getParameters().getG());
85     }
86
87     public String JavaDoc getAlgorithm()
88     {
89         return "DSA";
90     }
91
92     /**
93      * return the encoding format we produce in getEncoded().
94      *
95      * @return the string "PKCS#8"
96      */

97     public String JavaDoc getFormat()
98     {
99         return "PKCS#8";
100     }
101
102     /**
103      * Return a PKCS8 representation of the key. The sequence returned
104      * represents a full PrivateKeyInfo object.
105      *
106      * @return a PKCS8 representation of the key.
107      */

108     public byte[] getEncoded()
109     {
110         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
111         DEROutputStream dOut = new DEROutputStream(bOut);
112         PrivateKeyInfo info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, new DSAParameter(dsaSpec.getP(), dsaSpec.getQ(), dsaSpec.getG()).getDERObject()), new DERInteger(getX()));
113
114         try
115         {
116             dOut.writeObject(info);
117             dOut.close();
118         }
119         catch (IOException JavaDoc e)
120         {
121             throw new RuntimeException JavaDoc("Error encoding DSA private key");
122         }
123
124         return bOut.toByteArray();
125     }
126
127     public DSAParams JavaDoc getParams()
128     {
129         return dsaSpec;
130     }
131
132     public BigInteger JavaDoc getX()
133     {
134         return x;
135     }
136
137     public void setBagAttribute(
138         DERObjectIdentifier oid,
139         DEREncodable attribute)
140     {
141         pkcs12Attributes.put(oid, attribute);
142         pkcs12Ordering.addElement(oid);
143     }
144
145     public DEREncodable getBagAttribute(
146         DERObjectIdentifier oid)
147     {
148         return (DEREncodable)pkcs12Attributes.get(oid);
149     }
150
151     public Enumeration JavaDoc getBagAttributeKeys()
152     {
153         return pkcs12Ordering.elements();
154     }
155 }
156
Popular Tags