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.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.math.BigInteger;
024: import java.security.interfaces.RSAPrivateKey;
025: import java.security.spec.RSAPrivateKeySpec;
026: import java.util.Enumeration;
027: import java.util.Hashtable;
028: import java.util.Vector;
029:
030: import org.apache.geronimo.crypto.asn1.ASN1InputStream;
031: import org.apache.geronimo.crypto.asn1.ASN1OutputStream;
032: import org.apache.geronimo.crypto.asn1.DEREncodable;
033: import org.apache.geronimo.crypto.asn1.DERObjectIdentifier;
034: import org.apache.geronimo.crypto.crypto.params.RSAKeyParameters;
035: import org.apache.geronimo.crypto.jce.interfaces.PKCS12BagAttributeCarrier;
036:
037: public class JCERSAPrivateKey implements RSAPrivateKey,
038: PKCS12BagAttributeCarrier {
039: protected BigInteger modulus;
040: protected BigInteger privateExponent;
041:
042: private Hashtable pkcs12Attributes = new Hashtable();
043: private Vector pkcs12Ordering = new Vector();
044:
045: protected JCERSAPrivateKey() {
046: }
047:
048: JCERSAPrivateKey(RSAKeyParameters key) {
049: this .modulus = key.getModulus();
050: this .privateExponent = key.getExponent();
051: }
052:
053: JCERSAPrivateKey(RSAPrivateKeySpec spec) {
054: this .modulus = spec.getModulus();
055: this .privateExponent = spec.getPrivateExponent();
056: }
057:
058: JCERSAPrivateKey(RSAPrivateKey key) {
059: this .modulus = key.getModulus();
060: this .privateExponent = key.getPrivateExponent();
061: }
062:
063: public BigInteger getModulus() {
064: return modulus;
065: }
066:
067: public BigInteger getPrivateExponent() {
068: return privateExponent;
069: }
070:
071: public String getAlgorithm() {
072: return "RSA";
073: }
074:
075: public String getFormat() {
076: return "NULL";
077: }
078:
079: public byte[] getEncoded() {
080: return null;
081: }
082:
083: public boolean equals(Object o) {
084: if (!(o instanceof RSAPrivateKey)) {
085: return false;
086: }
087:
088: if (o == this ) {
089: return true;
090: }
091:
092: RSAPrivateKey key = (RSAPrivateKey) o;
093:
094: return getModulus().equals(key.getModulus())
095: && getPrivateExponent()
096: .equals(key.getPrivateExponent());
097: }
098:
099: public void setBagAttribute(DERObjectIdentifier oid,
100: DEREncodable attribute) {
101: pkcs12Attributes.put(oid, attribute);
102: pkcs12Ordering.addElement(oid);
103: }
104:
105: public DEREncodable getBagAttribute(DERObjectIdentifier oid) {
106: return (DEREncodable) pkcs12Attributes.get(oid);
107: }
108:
109: public Enumeration getBagAttributeKeys() {
110: return pkcs12Ordering.elements();
111: }
112:
113: private void readObject(ObjectInputStream in) throws IOException,
114: ClassNotFoundException {
115: this .modulus = (BigInteger) in.readObject();
116:
117: Object obj = in.readObject();
118:
119: if (obj instanceof Hashtable) {
120: this .pkcs12Attributes = (Hashtable) obj;
121: this .pkcs12Ordering = (Vector) in.readObject();
122: } else {
123: this .pkcs12Attributes = new Hashtable();
124: this .pkcs12Ordering = new Vector();
125:
126: ByteArrayInputStream bIn = new ByteArrayInputStream(
127: (byte[]) obj);
128: ASN1InputStream aIn = new ASN1InputStream(bIn);
129:
130: DERObjectIdentifier oid;
131:
132: while ((oid = (DERObjectIdentifier) aIn.readObject()) != null) {
133: this .setBagAttribute(oid, aIn.readObject());
134: }
135: }
136:
137: this .privateExponent = (BigInteger) in.readObject();
138: }
139:
140: private void writeObject(ObjectOutputStream out) throws IOException {
141: out.writeObject(modulus);
142:
143: if (pkcs12Ordering.size() == 0) {
144: out.writeObject(pkcs12Attributes);
145: out.writeObject(pkcs12Ordering);
146: } else {
147: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
148: ASN1OutputStream aOut = new ASN1OutputStream(bOut);
149:
150: Enumeration e = this .getBagAttributeKeys();
151:
152: while (e.hasMoreElements()) {
153: DEREncodable oid = (DEREncodable) e.nextElement();
154:
155: aOut.writeObject(oid);
156: aOut.writeObject(pkcs12Attributes.get(oid));
157: }
158:
159: out.writeObject(bOut.toByteArray());
160: }
161:
162: out.writeObject(privateExponent);
163: }
164: }
|