View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.jce.provider;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.math.BigInteger;
24  import java.security.interfaces.DSAParams;
25  import java.security.interfaces.DSAPrivateKey;
26  import java.security.spec.DSAParameterSpec;
27  import java.security.spec.DSAPrivateKeySpec;
28  import java.util.Enumeration;
29  import java.util.Hashtable;
30  import java.util.Vector;
31  
32  import org.apache.geronimo.util.asn1.ASN1Sequence;
33  import org.apache.geronimo.util.asn1.DEREncodable;
34  import org.apache.geronimo.util.asn1.DERInteger;
35  import org.apache.geronimo.util.asn1.DERObjectIdentifier;
36  import org.apache.geronimo.util.asn1.DEROutputStream;
37  import org.apache.geronimo.util.asn1.pkcs.PrivateKeyInfo;
38  import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
39  import org.apache.geronimo.util.asn1.x509.DSAParameter;
40  import org.apache.geronimo.util.asn1.x9.X9ObjectIdentifiers;
41  import org.apache.geronimo.util.crypto.params.DSAPrivateKeyParameters;
42  import org.apache.geronimo.util.jce.interfaces.PKCS12BagAttributeCarrier;
43  
44  public class JDKDSAPrivateKey
45      implements DSAPrivateKey, PKCS12BagAttributeCarrier
46  {
47      BigInteger          x;
48      DSAParams           dsaSpec;
49  
50      private Hashtable   pkcs12Attributes = new Hashtable();
51      private Vector      pkcs12Ordering = new Vector();
52  
53      protected JDKDSAPrivateKey()
54      {
55      }
56  
57      JDKDSAPrivateKey(
58          DSAPrivateKey    key)
59      {
60          this.x = key.getX();
61          this.dsaSpec = key.getParams();
62      }
63  
64      JDKDSAPrivateKey(
65          DSAPrivateKeySpec    spec)
66      {
67          this.x = spec.getX();
68          this.dsaSpec = new DSAParameterSpec(spec.getP(), spec.getQ(), spec.getG());
69      }
70  
71      JDKDSAPrivateKey(
72          PrivateKeyInfo  info)
73      {
74          DSAParameter    params = new DSAParameter((ASN1Sequence)info.getAlgorithmId().getParameters());
75          DERInteger      derX = (DERInteger)info.getPrivateKey();
76  
77          this.x = derX.getValue();
78          this.dsaSpec = new DSAParameterSpec(params.getP(), params.getQ(), params.getG());
79      }
80  
81      JDKDSAPrivateKey(
82          DSAPrivateKeyParameters  params)
83      {
84          this.x = params.getX();
85          this.dsaSpec = new DSAParameterSpec(params.getParameters().getP(), params.getParameters().getQ(), params.getParameters().getG());
86      }
87  
88      public String getAlgorithm()
89      {
90          return "DSA";
91      }
92  
93      /**
94       * return the encoding format we produce in getEncoded().
95       *
96       * @return the string "PKCS#8"
97       */
98      public String getFormat()
99      {
100         return "PKCS#8";
101     }
102 
103     /**
104      * Return a PKCS8 representation of the key. The sequence returned
105      * represents a full PrivateKeyInfo object.
106      *
107      * @return a PKCS8 representation of the key.
108      */
109     public byte[] getEncoded()
110     {
111         ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
112         DEROutputStream         dOut = new DEROutputStream(bOut);
113         PrivateKeyInfo          info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, new DSAParameter(dsaSpec.getP(), dsaSpec.getQ(), dsaSpec.getG()).getDERObject()), new DERInteger(getX()));
114 
115         try
116         {
117             dOut.writeObject(info);
118             dOut.close();
119         }
120         catch (IOException e)
121         {
122             throw new RuntimeException("Error encoding DSA private key");
123         }
124 
125         return bOut.toByteArray();
126     }
127 
128     public DSAParams getParams()
129     {
130         return dsaSpec;
131     }
132 
133     public BigInteger getX()
134     {
135         return x;
136     }
137 
138     public void setBagAttribute(
139         DERObjectIdentifier oid,
140         DEREncodable        attribute)
141     {
142         pkcs12Attributes.put(oid, attribute);
143         pkcs12Ordering.addElement(oid);
144     }
145 
146     public DEREncodable getBagAttribute(
147         DERObjectIdentifier oid)
148     {
149         return (DEREncodable)pkcs12Attributes.get(oid);
150     }
151 
152     public Enumeration getBagAttributeKeys()
153     {
154         return pkcs12Ordering.elements();
155     }
156 }