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