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:
67: public class DSSKeyPairPKCS8Codec
68: implements IKeyPairCodec
69: {
70: private static final Logger log = Configuration.DEBUG ?
71: Logger.getLogger(DSSKeyPairPKCS8Codec.class.getName()) : null;
72:
73: private static final OID DSA_ALG_OID = new OID(Registry.DSA_OID_STRING);
74:
75:
76:
77: public int getFormatID()
78: {
79: return PKCS8_FORMAT;
80: }
81:
82:
85: public byte[] encodePublicKey(PublicKey key)
86: {
87: throw new InvalidParameterException("Wrong format for public keys");
88: }
89:
90:
118: public byte[] encodePrivateKey(PrivateKey key)
119: {
120: if (! (key instanceof DSSPrivateKey))
121: throw new InvalidParameterException("Wrong key type");
122:
123: DERValue derVersion = new DERValue(DER.INTEGER, BigInteger.ZERO);
124:
125: DERValue derOID = new DERValue(DER.OBJECT_IDENTIFIER, DSA_ALG_OID);
126:
127: DSSPrivateKey pk = (DSSPrivateKey) key;
128: BigInteger p = pk.getParams().getP();
129: BigInteger q = pk.getParams().getQ();
130: BigInteger g = pk.getParams().getG();
131: BigInteger x = pk.getX();
132:
133: ArrayList params = new ArrayList(3);
134: params.add(new DERValue(DER.INTEGER, p));
135: params.add(new DERValue(DER.INTEGER, q));
136: params.add(new DERValue(DER.INTEGER, g));
137: DERValue derParams = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, params);
138:
139: ArrayList algorithmID = new ArrayList(2);
140: algorithmID.add(derOID);
141: algorithmID.add(derParams);
142: DERValue derAlgorithmID = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,
143: algorithmID);
144:
145:
146: DERValue derX = new DERValue(DER.INTEGER, x);
147: DERValue derPrivateKey = new DERValue(DER.OCTET_STRING, derX.getEncoded());
148:
149: ArrayList pki = new ArrayList(3);
150: pki.add(derVersion);
151: pki.add(derAlgorithmID);
152: pki.add(derPrivateKey);
153: DERValue derPKI = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, pki);
154:
155: byte[] result;
156: ByteArrayOutputStream baos = new ByteArrayOutputStream();
157: try
158: {
159: DERWriter.write(baos, derPKI);
160: result = baos.toByteArray();
161: }
162: catch (IOException e)
163: {
164: InvalidParameterException y = new InvalidParameterException(e.getMessage());
165: y.initCause(e);
166: throw y;
167: }
168: return result;
169: }
170:
171:
174: public PublicKey decodePublicKey(byte[] input)
175: {
176: throw new InvalidParameterException("Wrong format for public keys");
177: }
178:
179:
187: public PrivateKey decodePrivateKey(byte[] input)
188: {
189: if (Configuration.DEBUG)
190: log.entering(this.getClass().getName(), "decodePrivateKey");
191: if (input == null)
192: throw new InvalidParameterException("Input bytes MUST NOT be null");
193:
194: BigInteger version, p, q, g, x;
195: DERReader der = new DERReader(input);
196: try
197: {
198: DERValue derPKI = der.read();
199: DerUtil.checkIsConstructed(derPKI, "Wrong PrivateKeyInfo field");
200:
201: DERValue derVersion = der.read();
202: if (! (derVersion.getValue() instanceof BigInteger))
203: throw new InvalidParameterException("Wrong Version field");
204:
205: version = (BigInteger) derVersion.getValue();
206: if (version.compareTo(BigInteger.ZERO) != 0)
207: throw new InvalidParameterException("Unexpected Version: " + version);
208:
209: DERValue derAlgoritmID = der.read();
210: DerUtil.checkIsConstructed(derAlgoritmID, "Wrong AlgorithmIdentifier field");
211:
212: DERValue derOID = der.read();
213: OID algOID = (OID) derOID.getValue();
214: if (! algOID.equals(DSA_ALG_OID))
215: throw new InvalidParameterException("Unexpected OID: " + algOID);
216:
217: DERValue derParams = der.read();
218: DerUtil.checkIsConstructed(derParams, "Wrong DSS Parameters field");
219:
220: DERValue val = der.read();
221: DerUtil.checkIsBigInteger(val, "Wrong P field");
222: p = (BigInteger) val.getValue();
223: val = der.read();
224: DerUtil.checkIsBigInteger(val, "Wrong Q field");
225: q = (BigInteger) val.getValue();
226: val = der.read();
227: DerUtil.checkIsBigInteger(val, "Wrong G field");
228: g = (BigInteger) val.getValue();
229:
230: val = der.read();
231: if (Configuration.DEBUG)
232: log.fine("val = " + val);
233: byte[] xBytes = (byte[]) val.getValue();
234: if (Configuration.DEBUG)
235: log.fine(Util.dumpString(xBytes, "xBytes: "));
236: DERReader der2 = new DERReader(xBytes);
237: val = der2.read();
238: DerUtil.checkIsBigInteger(val, "Wrong X field");
239: x = (BigInteger) val.getValue();
240: }
241: catch (IOException e)
242: {
243: InvalidParameterException y = new InvalidParameterException(e.getMessage());
244: y.initCause(e);
245: throw y;
246: }
247: if (Configuration.DEBUG)
248: log.exiting(this.getClass().getName(), "decodePrivateKey");
249: return new DSSPrivateKey(Registry.PKCS8_ENCODING_ID, p, q, g, x);
250: }
251: }