001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package org.apache.geronimo.util.jce.provider;
020
021 import java.io.ByteArrayOutputStream;
022 import java.io.IOException;
023 import java.math.BigInteger;
024 import java.security.interfaces.DSAParams;
025 import java.security.interfaces.DSAPrivateKey;
026 import java.security.spec.DSAParameterSpec;
027 import java.security.spec.DSAPrivateKeySpec;
028 import java.util.Enumeration;
029 import java.util.Hashtable;
030 import java.util.Vector;
031
032 import org.apache.geronimo.util.asn1.ASN1Sequence;
033 import org.apache.geronimo.util.asn1.DEREncodable;
034 import org.apache.geronimo.util.asn1.DERInteger;
035 import org.apache.geronimo.util.asn1.DERObjectIdentifier;
036 import org.apache.geronimo.util.asn1.DEROutputStream;
037 import org.apache.geronimo.util.asn1.pkcs.PrivateKeyInfo;
038 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
039 import org.apache.geronimo.util.asn1.x509.DSAParameter;
040 import org.apache.geronimo.util.asn1.x9.X9ObjectIdentifiers;
041 import org.apache.geronimo.util.crypto.params.DSAPrivateKeyParameters;
042 import org.apache.geronimo.util.jce.interfaces.PKCS12BagAttributeCarrier;
043
044 public class JDKDSAPrivateKey
045 implements DSAPrivateKey, PKCS12BagAttributeCarrier
046 {
047 BigInteger x;
048 DSAParams dsaSpec;
049
050 private Hashtable pkcs12Attributes = new Hashtable();
051 private Vector pkcs12Ordering = new Vector();
052
053 protected JDKDSAPrivateKey()
054 {
055 }
056
057 JDKDSAPrivateKey(
058 DSAPrivateKey key)
059 {
060 this.x = key.getX();
061 this.dsaSpec = key.getParams();
062 }
063
064 JDKDSAPrivateKey(
065 DSAPrivateKeySpec spec)
066 {
067 this.x = spec.getX();
068 this.dsaSpec = new DSAParameterSpec(spec.getP(), spec.getQ(), spec.getG());
069 }
070
071 JDKDSAPrivateKey(
072 PrivateKeyInfo info)
073 {
074 DSAParameter params = new DSAParameter((ASN1Sequence)info.getAlgorithmId().getParameters());
075 DERInteger derX = (DERInteger)info.getPrivateKey();
076
077 this.x = derX.getValue();
078 this.dsaSpec = new DSAParameterSpec(params.getP(), params.getQ(), params.getG());
079 }
080
081 JDKDSAPrivateKey(
082 DSAPrivateKeyParameters params)
083 {
084 this.x = params.getX();
085 this.dsaSpec = new DSAParameterSpec(params.getParameters().getP(), params.getParameters().getQ(), params.getParameters().getG());
086 }
087
088 public String getAlgorithm()
089 {
090 return "DSA";
091 }
092
093 /**
094 * return the encoding format we produce in getEncoded().
095 *
096 * @return the string "PKCS#8"
097 */
098 public String getFormat()
099 {
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 }