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