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: import ;
51:
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60:
61:
65: public class RSAKeyPairX509Codec
66: implements IKeyPairCodec
67: {
68: private static final Logger log = Configuration.DEBUG ?
69: Logger.getLogger(RSAKeyPairX509Codec.class.getName()) : null;
70:
71: private static final OID RSA_ALG_OID = new OID(Registry.RSA_OID_STRING);
72:
73:
74:
75: public int getFormatID()
76: {
77: return X509_FORMAT;
78: }
79:
80:
118: public byte[] encodePublicKey(PublicKey key)
119: {
120: if (Configuration.DEBUG)
121: log.entering(this.getClass().getName(), "encodePublicKey()", key);
122: if (! (key instanceof GnuRSAPublicKey))
123: throw new InvalidParameterException("key");
124:
125: DERValue derOID = new DERValue(DER.OBJECT_IDENTIFIER, RSA_ALG_OID);
126:
127: GnuRSAPublicKey rsaKey = (GnuRSAPublicKey) key;
128: BigInteger n = rsaKey.getN();
129: BigInteger e = rsaKey.getE();
130:
131: DERValue derN = new DERValue(DER.INTEGER, n);
132: DERValue derE = new DERValue(DER.INTEGER, e);
133:
134: ArrayList algorithmID = new ArrayList(2);
135: algorithmID.add(derOID);
136: algorithmID.add(new DERValue(DER.NULL, null));
137: DERValue derAlgorithmID = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,
138: algorithmID);
139:
140: ArrayList publicKey = new ArrayList(2);
141: publicKey.add(derN);
142: publicKey.add(derE);
143: DERValue derPublicKey = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,
144: publicKey);
145: byte[] spkBytes = derPublicKey.getEncoded();
146: DERValue derSPK = new DERValue(DER.BIT_STRING, new BitString(spkBytes));
147:
148: ArrayList spki = new ArrayList(2);
149: spki.add(derAlgorithmID);
150: spki.add(derSPK);
151: DERValue derSPKI = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, spki);
152:
153: byte[] result;
154: ByteArrayOutputStream baos = new ByteArrayOutputStream();
155: try
156: {
157: DERWriter.write(baos, derSPKI);
158: result = baos.toByteArray();
159: }
160: catch (IOException x)
161: {
162: InvalidParameterException y = new InvalidParameterException(x.getMessage());
163: y.initCause(x);
164: throw y;
165: }
166: if (Configuration.DEBUG)
167: log.exiting(this.getClass().getName(), "encodePublicKey()", result);
168: return result;
169: }
170:
171:
174: public byte[] encodePrivateKey(PrivateKey key)
175: {
176: throw new InvalidParameterException("Wrong format for private keys");
177: }
178:
179:
187: public PublicKey decodePublicKey(byte[] input)
188: {
189: if (Configuration.DEBUG)
190: log.entering(this.getClass().getName(), "decodePublicKey()", input);
191: if (input == null)
192: throw new InvalidParameterException("Input bytes MUST NOT be null");
193:
194: BigInteger n, e;
195: DERReader der = new DERReader(input);
196: try
197: {
198: DERValue derSPKI = der.read();
199: DerUtil.checkIsConstructed(derSPKI, "Wrong SubjectPublicKeyInfo field");
200:
201: DERValue derAlgorithmID = der.read();
202: DerUtil.checkIsConstructed(derAlgorithmID, "Wrong AlgorithmIdentifier field");
203:
204: DERValue derOID = der.read();
205: if (! (derOID.getValue() instanceof OID))
206: throw new InvalidParameterException("Wrong Algorithm field");
207:
208: OID algOID = (OID) derOID.getValue();
209: if (! algOID.equals(RSA_ALG_OID))
210: throw new InvalidParameterException("Unexpected OID: " + algOID);
211:
212:
213: DERValue val = der.read();
214: if (val.getTag() == DER.NULL)
215: val = der.read();
216:
217: if (! (val.getValue() instanceof BitString))
218: throw new InvalidParameterException("Wrong SubjectPublicKey field");
219:
220: byte[] spkBytes = ((BitString) val.getValue()).toByteArray();
221:
222: der = new DERReader(spkBytes);
223: val = der.read();
224: DerUtil.checkIsConstructed(derAlgorithmID, "Wrong subjectPublicKey field");
225:
226: val = der.read();
227: DerUtil.checkIsBigInteger(val, "Wrong modulus field");
228: n = (BigInteger) val.getValue();
229: val = der.read();
230: DerUtil.checkIsBigInteger(val, "Wrong publicExponent field");
231: e = (BigInteger) val.getValue();
232: }
233: catch (IOException x)
234: {
235: InvalidParameterException y = new InvalidParameterException(x.getMessage());
236: y.initCause(x);
237: throw y;
238: }
239: PublicKey result = new GnuRSAPublicKey(Registry.X509_ENCODING_ID, n, e);
240: if (Configuration.DEBUG)
241: log.exiting(this.getClass().getName(), "decodePublicKey()", result);
242: return result;
243: }
244:
245:
248: public PrivateKey decodePrivateKey(byte[] input)
249: {
250: throw new InvalidParameterException("Wrong format for private keys");
251: }
252: }