1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50:
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59:
60:
64: public class RSAKeyPairPKCS8Codec
65: implements IKeyPairCodec
66: {
67: private static final Logger log = Configuration.DEBUG ?
68: Logger.getLogger(RSAKeyPairPKCS8Codec.class.getName()) : null;
69:
70: private static final OID RSA_ALG_OID = new OID(Registry.RSA_OID_STRING);
71:
72:
73:
74: public int getFormatID()
75: {
76: return PKCS8_FORMAT;
77: }
78:
79:
82: public byte[] encodePublicKey(PublicKey key)
83: {
84: throw new InvalidParameterException("Wrong format for public keys");
85: }
86:
87:
127: public byte[] encodePrivateKey(PrivateKey key)
128: {
129: if (Configuration.DEBUG)
130: log.entering(this.getClass().getName(), "encodePrivateKey()", key);
131: if (! (key instanceof GnuRSAPrivateKey))
132: throw new InvalidParameterException("Wrong key type");
133:
134: GnuRSAPrivateKey pk = (GnuRSAPrivateKey) key;
135: BigInteger n = pk.getN();
136: BigInteger e = pk.getE();
137: BigInteger d = pk.getPrivateExponent();
138: BigInteger p = pk.getPrimeP();
139: BigInteger q = pk.getPrimeQ();
140: BigInteger dP = pk.getPrimeExponentP();
141: BigInteger dQ = pk.getPrimeExponentQ();
142: BigInteger qInv = pk.getCrtCoefficient();
143:
144: DERValue derVersion = new DERValue(DER.INTEGER, BigInteger.ZERO);
145:
146: DERValue derOID = new DERValue(DER.OBJECT_IDENTIFIER, RSA_ALG_OID);
147:
148: ArrayList algorithmID = new ArrayList(2);
149: algorithmID.add(derOID);
150: algorithmID.add(new DERValue(DER.NULL, null));
151: DERValue derAlgorithmID = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,
152: algorithmID);
153:
154: DERValue derRSAVersion = new DERValue(DER.INTEGER, BigInteger.ZERO);
155: DERValue derN = new DERValue(DER.INTEGER, n);
156: DERValue derE = new DERValue(DER.INTEGER, e);
157: DERValue derD = new DERValue(DER.INTEGER, d);
158: DERValue derP = new DERValue(DER.INTEGER, p);
159: DERValue derQ = new DERValue(DER.INTEGER, q);
160: DERValue derDP = new DERValue(DER.INTEGER, dP);
161: DERValue derDQ = new DERValue(DER.INTEGER, dQ);
162: DERValue derQInv = new DERValue(DER.INTEGER, qInv);
163:
164: ArrayList rsaPrivateKey = new ArrayList();
165: rsaPrivateKey.add(derRSAVersion);
166: rsaPrivateKey.add(derN);
167: rsaPrivateKey.add(derE);
168: rsaPrivateKey.add(derD);
169: rsaPrivateKey.add(derP);
170: rsaPrivateKey.add(derQ);
171: rsaPrivateKey.add(derDP);
172: rsaPrivateKey.add(derDQ);
173: rsaPrivateKey.add(derQInv);
174: DERValue derRSAPrivateKey = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,
175: rsaPrivateKey);
176: byte[] pkBytes = derRSAPrivateKey.getEncoded();
177: DERValue derPrivateKey = new DERValue(DER.OCTET_STRING, pkBytes);
178:
179: ArrayList pki = new ArrayList(3);
180: pki.add(derVersion);
181: pki.add(derAlgorithmID);
182: pki.add(derPrivateKey);
183: DERValue derPKI = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, pki);
184:
185: byte[] result;
186: ByteArrayOutputStream baos = new ByteArrayOutputStream();
187: try
188: {
189: DERWriter.write(baos, derPKI);
190: result = baos.toByteArray();
191: }
192: catch (IOException x)
193: {
194: InvalidParameterException y = new InvalidParameterException();
195: y.initCause(x);
196: throw y;
197: }
198: if (Configuration.DEBUG)
199: log.exiting(this.getClass().getName(), "encodePrivateKey()", result);
200: return result;
201: }
202:
203:
206: public PublicKey decodePublicKey(byte[] input)
207: {
208: throw new InvalidParameterException("Wrong format for public keys");
209: }
210:
211:
219: public PrivateKey decodePrivateKey(byte[] input)
220: {
221: if (Configuration.DEBUG)
222: log.entering(this.getClass().getName(), "decodePrivateKey()", input);
223: if (input == null)
224: throw new InvalidParameterException("Input bytes MUST NOT be null");
225:
226: BigInteger version, n, e, d, p, q, dP, dQ, qInv;
227: DERReader der = new DERReader(input);
228: try
229: {
230: DERValue derPKI = der.read();
231: DerUtil.checkIsConstructed(derPKI, "Wrong PrivateKeyInfo field");
232:
233: DERValue derVersion = der.read();
234: DerUtil.checkIsBigInteger(derVersion, "Wrong Version field");
235: version = (BigInteger) derVersion.getValue();
236: if (version.compareTo(BigInteger.ZERO) != 0)
237: throw new InvalidParameterException("Unexpected Version: " + version);
238:
239: DERValue derAlgoritmID = der.read();
240: DerUtil.checkIsConstructed(derAlgoritmID, "Wrong AlgorithmIdentifier field");
241:
242: DERValue derOID = der.read();
243: OID algOID = (OID) derOID.getValue();
244: if (! algOID.equals(RSA_ALG_OID))
245: throw new InvalidParameterException("Unexpected OID: " + algOID);
246:
247:
248: DERValue val = der.read();
249: if (val.getTag() == DER.NULL)
250: val = der.read();
251:
252: byte[] pkBytes = (byte[]) val.getValue();
253: der = new DERReader(pkBytes);
254: DERValue derRSAPrivateKey = der.read();
255: DerUtil.checkIsConstructed(derRSAPrivateKey, "Wrong RSAPrivateKey field");
256:
257: val = der.read();
258: DerUtil.checkIsBigInteger(val, "Wrong RSAPrivateKey Version field");
259: version = (BigInteger) val.getValue();
260: if (version.compareTo(BigInteger.ZERO) != 0)
261: throw new InvalidParameterException("Unexpected RSAPrivateKey Version: "
262: + version);
263:
264: val = der.read();
265: DerUtil.checkIsBigInteger(val, "Wrong modulus field");
266: n = (BigInteger) val.getValue();
267: val = der.read();
268: DerUtil.checkIsBigInteger(val, "Wrong publicExponent field");
269: e = (BigInteger) val.getValue();
270: val = der.read();
271: DerUtil.checkIsBigInteger(val, "Wrong privateExponent field");
272: d = (BigInteger) val.getValue();
273: val = der.read();
274: DerUtil.checkIsBigInteger(val, "Wrong prime1 field");
275: p = (BigInteger) val.getValue();
276: val = der.read();
277: DerUtil.checkIsBigInteger(val, "Wrong prime2 field");
278: q = (BigInteger) val.getValue();
279: val = der.read();
280: DerUtil.checkIsBigInteger(val, "Wrong exponent1 field");
281: dP = (BigInteger) val.getValue();
282: val = der.read();
283: DerUtil.checkIsBigInteger(val, "Wrong exponent2 field");
284: dQ = (BigInteger) val.getValue();
285: val = der.read();
286: DerUtil.checkIsBigInteger(val, "Wrong coefficient field");
287: qInv = (BigInteger) val.getValue();
288: }
289: catch (IOException x)
290: {
291: InvalidParameterException y = new InvalidParameterException();
292: y.initCause(x);
293: throw y;
294: }
295: PrivateKey result = new GnuRSAPrivateKey(Registry.PKCS8_ENCODING_ID,
296: n, e, d, p, q, dP, dQ, qInv);
297: if (Configuration.DEBUG)
298: log.exiting(this.getClass().getName(), "decodePrivateKey()", result);
299: return result;
300: }
301: }