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