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: */package org.apache.geronimo.crypto.jce.provider;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.IOException;
020: import java.math.BigInteger;
021: import java.security.interfaces.DSAParams;
022: import java.security.interfaces.DSAPrivateKey;
023: import java.security.spec.DSAParameterSpec;
024: import java.security.spec.DSAPrivateKeySpec;
025: import java.util.Enumeration;
026: import java.util.Hashtable;
027: import java.util.Vector;
028:
029: import org.apache.geronimo.crypto.asn1.ASN1Sequence;
030: import org.apache.geronimo.crypto.asn1.DEREncodable;
031: import org.apache.geronimo.crypto.asn1.DERInteger;
032: import org.apache.geronimo.crypto.asn1.DERObjectIdentifier;
033: import org.apache.geronimo.crypto.asn1.DEROutputStream;
034: import org.apache.geronimo.crypto.asn1.pkcs.PrivateKeyInfo;
035: import org.apache.geronimo.crypto.asn1.x509.AlgorithmIdentifier;
036: import org.apache.geronimo.crypto.asn1.x509.DSAParameter;
037: import org.apache.geronimo.crypto.asn1.x9.X9ObjectIdentifiers;
038: import org.apache.geronimo.crypto.crypto.params.DSAPrivateKeyParameters;
039: import org.apache.geronimo.crypto.jce.interfaces.PKCS12BagAttributeCarrier;
040:
041: public class JDKDSAPrivateKey implements DSAPrivateKey,
042: PKCS12BagAttributeCarrier {
043: BigInteger x;
044: DSAParams dsaSpec;
045:
046: private Hashtable pkcs12Attributes = new Hashtable();
047: private Vector pkcs12Ordering = new Vector();
048:
049: protected JDKDSAPrivateKey() {
050: }
051:
052: JDKDSAPrivateKey(DSAPrivateKey key) {
053: this .x = key.getX();
054: this .dsaSpec = key.getParams();
055: }
056:
057: JDKDSAPrivateKey(DSAPrivateKeySpec spec) {
058: this .x = spec.getX();
059: this .dsaSpec = new DSAParameterSpec(spec.getP(), spec.getQ(),
060: spec.getG());
061: }
062:
063: JDKDSAPrivateKey(PrivateKeyInfo info) {
064: DSAParameter params = new DSAParameter((ASN1Sequence) info
065: .getAlgorithmId().getParameters());
066: DERInteger derX = (DERInteger) info.getPrivateKey();
067:
068: this .x = derX.getValue();
069: this .dsaSpec = new DSAParameterSpec(params.getP(), params
070: .getQ(), params.getG());
071: }
072:
073: JDKDSAPrivateKey(DSAPrivateKeyParameters params) {
074: this .x = params.getX();
075: this .dsaSpec = new DSAParameterSpec(params.getParameters()
076: .getP(), params.getParameters().getQ(), params
077: .getParameters().getG());
078: }
079:
080: public String getAlgorithm() {
081: return "DSA";
082: }
083:
084: /**
085: * return the encoding format we produce in getEncoded().
086: *
087: * @return the string "PKCS#8"
088: */
089: public String getFormat() {
090: return "PKCS#8";
091: }
092:
093: /**
094: * Return a PKCS8 representation of the key. The sequence returned
095: * represents a full PrivateKeyInfo object.
096: *
097: * @return a PKCS8 representation of the key.
098: */
099: public byte[] getEncoded() {
100: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
101: DEROutputStream dOut = new DEROutputStream(bOut);
102: PrivateKeyInfo info = new PrivateKeyInfo(
103: new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa,
104: new DSAParameter(dsaSpec.getP(),
105: dsaSpec.getQ(), dsaSpec.getG())
106: .getDERObject()),
107: new DERInteger(getX()));
108:
109: try {
110: dOut.writeObject(info);
111: dOut.close();
112: } catch (IOException e) {
113: throw new RuntimeException(
114: "Error encoding DSA private key", e);
115: }
116:
117: return bOut.toByteArray();
118: }
119:
120: public DSAParams getParams() {
121: return dsaSpec;
122: }
123:
124: public BigInteger getX() {
125: return x;
126: }
127:
128: public void setBagAttribute(DERObjectIdentifier oid,
129: DEREncodable attribute) {
130: pkcs12Attributes.put(oid, attribute);
131: pkcs12Ordering.addElement(oid);
132: }
133:
134: public DEREncodable getBagAttribute(DERObjectIdentifier oid) {
135: return (DEREncodable) pkcs12Attributes.get(oid);
136: }
137:
138: public Enumeration getBagAttributeKeys() {
139: return pkcs12Ordering.elements();
140: }
141: }
|