1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58:
59: public class DHKeyPairPKCS8Codec
60: implements IKeyPairCodec
61: {
62: private static final OID DH_ALG_OID = new OID(Registry.DH_OID_STRING);
63:
64:
65:
66: public int getFormatID()
67: {
68: return PKCS8_FORMAT;
69: }
70:
71:
74: public byte[] encodePublicKey(PublicKey key)
75: {
76: throw new InvalidParameterException("Wrong format for public keys");
77: }
78:
79:
114: public byte[] encodePrivateKey(PrivateKey key)
115: {
116: if (! (key instanceof GnuDHPrivateKey))
117: throw new InvalidParameterException("Wrong key type");
118:
119: DERValue derVersion = new DERValue(DER.INTEGER, BigInteger.ZERO);
120:
121: DERValue derOID = new DERValue(DER.OBJECT_IDENTIFIER, DH_ALG_OID);
122:
123: GnuDHPrivateKey pk = (GnuDHPrivateKey) key;
124: BigInteger p = pk.getParams().getP();
125: BigInteger g = pk.getParams().getG();
126: BigInteger q = pk.getQ();
127: if (q == null)
128: q = BigInteger.ZERO;
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, g));
134: params.add(new DERValue(DER.INTEGER, q));
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: DERValue derPrivateKey = new DERValue(DER.OCTET_STRING, Util.trim(x));
144:
145: ArrayList pki = new ArrayList(3);
146: pki.add(derVersion);
147: pki.add(derAlgorithmID);
148: pki.add(derPrivateKey);
149: DERValue derPKI = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, pki);
150:
151: byte[] result;
152: ByteArrayOutputStream baos = new ByteArrayOutputStream();
153: try
154: {
155: DERWriter.write(baos, derPKI);
156: result = baos.toByteArray();
157: }
158: catch (IOException e)
159: {
160: InvalidParameterException y = new InvalidParameterException();
161: y.initCause(e);
162: throw y;
163: }
164:
165: return result;
166: }
167:
168:
171: public PublicKey decodePublicKey(byte[] input)
172: {
173: throw new InvalidParameterException("Wrong format for public keys");
174: }
175:
176:
184: public PrivateKey decodePrivateKey(byte[] input)
185: {
186: if (input == null)
187: throw new InvalidParameterException("Input bytes MUST NOT be null");
188:
189: BigInteger version, p, q, g, x;
190: DERReader der = new DERReader(input);
191: try
192: {
193: DERValue derPKI = der.read();
194: DerUtil.checkIsConstructed(derPKI, "Wrong PrivateKeyInfo field");
195:
196: DERValue derVersion = der.read();
197: if (! (derVersion.getValue() instanceof BigInteger))
198: throw new InvalidParameterException("Wrong Version field");
199:
200: version = (BigInteger) derVersion.getValue();
201: if (version.compareTo(BigInteger.ZERO) != 0)
202: throw new InvalidParameterException("Unexpected Version: " + version);
203:
204: DERValue derAlgoritmID = der.read();
205: DerUtil.checkIsConstructed(derAlgoritmID, "Wrong AlgorithmIdentifier field");
206:
207: DERValue derOID = der.read();
208: OID algOID = (OID) derOID.getValue();
209: if (! algOID.equals(DH_ALG_OID))
210: throw new InvalidParameterException("Unexpected OID: " + algOID);
211:
212: DERValue derParams = der.read();
213: DerUtil.checkIsConstructed(derParams, "Wrong DSS Parameters field");
214:
215: DERValue val = der.read();
216: DerUtil.checkIsBigInteger(val, "Wrong P field");
217: p = (BigInteger) val.getValue();
218: val = der.read();
219: DerUtil.checkIsBigInteger(val, "Wrong G field");
220: g = (BigInteger) val.getValue();
221: val = der.read();
222: DerUtil.checkIsBigInteger(val, "Wrong Q field");
223: q = (BigInteger) val.getValue();
224: if (q.compareTo(BigInteger.ZERO) == 0)
225: q = null;
226:
227: val = der.read();
228: byte[] xBytes = (byte[]) val.getValue();
229: x = new BigInteger(1, xBytes);
230: }
231: catch (IOException e)
232: {
233: InvalidParameterException y = new InvalidParameterException();
234: y.initCause(e);
235: throw y;
236: }
237:
238: return new GnuDHPrivateKey(Registry.PKCS8_ENCODING_ID, q, p, g, x);
239: }
240: }