001: package org.bouncycastle.jce.provider.test;
002:
003: import org.bouncycastle.asn1.DERObjectIdentifier;
004: import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
005: import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
006: import org.bouncycastle.asn1.x509.X509Name;
007: import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
008: import org.bouncycastle.jce.ECGOST3410NamedCurveTable;
009: import org.bouncycastle.jce.PKCS10CertificationRequest;
010: import org.bouncycastle.jce.X509Principal;
011: import org.bouncycastle.jce.interfaces.ECPointEncoder;
012: import org.bouncycastle.jce.provider.BouncyCastleProvider;
013: import org.bouncycastle.jce.spec.ECParameterSpec;
014: import org.bouncycastle.jce.spec.ECPrivateKeySpec;
015: import org.bouncycastle.jce.spec.ECPublicKeySpec;
016: import org.bouncycastle.math.ec.ECCurve;
017: import org.bouncycastle.util.encoders.Base64;
018: import org.bouncycastle.util.encoders.Hex;
019: import org.bouncycastle.util.test.SimpleTest;
020:
021: import java.math.BigInteger;
022: import java.security.KeyFactory;
023: import java.security.KeyPair;
024: import java.security.KeyPairGenerator;
025: import java.security.PrivateKey;
026: import java.security.PublicKey;
027: import java.security.SecureRandom;
028: import java.security.Security;
029: import java.security.Signature;
030: import java.security.spec.RSAPrivateCrtKeySpec;
031: import java.security.spec.RSAPublicKeySpec;
032: import java.util.Hashtable;
033:
034: /**
035: **/
036: public class PKCS10CertRequestTest extends SimpleTest {
037: private byte[] gost3410EC_A = Base64
038: .decode("MIIBOzCB6wIBADB/MQ0wCwYDVQQDEwR0ZXN0MRUwEwYDVQQKEwxEZW1vcyBDbyBMdGQxHjAcBgNV"
039: + "BAsTFUNyeXB0b2dyYXBoeSBkaXZpc2lvbjEPMA0GA1UEBxMGTW9zY293MQswCQYDVQQGEwJydTEZ"
040: + "MBcGCSqGSIb3DQEJARYKc2RiQGRvbC5ydTBjMBwGBiqFAwICEzASBgcqhQMCAiMBBgcqhQMCAh4B"
041: + "A0MABEBYx0P2D7YuuZo5HgdIAUKAXcLBDZ+4LYFgbKjrfStVfH59lc40BQ2FZ7M703hLpXK8GiBQ"
042: + "GEYpKaAuQZnMIpByoAAwCAYGKoUDAgIDA0EAgXMcTrhdOY2Er2tHOSAgnMezqrYxocZTWhxmW5Rl"
043: + "JY6lbXH5rndCn4swFzXU+YhgAsJv1wQBaoZEWRl5WV4/nA==");
044:
045: private byte[] gost3410EC_B = Base64
046: .decode("MIIBPTCB7QIBADCBgDENMAsGA1UEAxMEdGVzdDEWMBQGA1UEChMNRGVtb3MgQ28gTHRkLjEeMBwG"
047: + "A1UECxMVQ3J5cHRvZ3JhcGh5IGRpdmlzaW9uMQ8wDQYDVQQHEwZNb3Njb3cxCzAJBgNVBAYTAnJ1"
048: + "MRkwFwYJKoZIhvcNAQkBFgpzZGJAZG9sLnJ1MGMwHAYGKoUDAgITMBIGByqFAwICIwIGByqFAwIC"
049: + "HgEDQwAEQI5SLoWT7dZVilbV9j5B/fyIDuDs6x4pjqNC2TtFYbpRHrk/Wc5g/mcHvD80tsm5o1C7"
050: + "7cizNzkvAVUM4VT4Dz6gADAIBgYqhQMCAgMDQQAoT5TwJ8o+bSrxckymyo3diwG7ZbSytX4sRiKy"
051: + "wXPWRS9LlBvPO2NqwpS2HUnxSU8rzfL9fJcybATf7Yt1OEVq");
052:
053: private byte[] gost3410EC_C = Base64
054: .decode("MIIBRDCB9AIBADCBhzEVMBMGA1UEAxMMdGVzdCByZXF1ZXN0MRUwEwYDVQQKEwxEZW1vcyBDbyBM"
055: + "dGQxHjAcBgNVBAsTFUNyeXB0b2dyYXBoeSBkaXZpc2lvbjEPMA0GA1UEBxMGTW9zY293MQswCQYD"
056: + "VQQGEwJydTEZMBcGCSqGSIb3DQEJARYKc2RiQGRvbC5ydTBjMBwGBiqFAwICEzASBgcqhQMCAiMD"
057: + "BgcqhQMCAh4BA0MABEBcmGh7OmR4iqqj+ycYo1S1fS7r5PhisSQU2Ezuz8wmmmR2zeTZkdMYCOBa"
058: + "UTMNms0msW3wuYDho7nTDNscHTB5oAAwCAYGKoUDAgIDA0EAVoOMbfyo1Un4Ss7WQrUjHJoiaYW8"
059: + "Ime5LeGGU2iW3ieAv6es/FdMrwTKkqn5dhd3aL/itFg5oQbhyfXw5yw/QQ==");
060:
061: private byte[] gost3410EC_ExA = Base64
062: .decode("MIIBOzCB6wIBADB/MQ0wCwYDVQQDEwR0ZXN0MRUwEwYDVQQKEwxEZW1vcyBDbyBMdGQxHjAcBgNV"
063: + "BAsTFUNyeXB0b2dyYXBoeSBkaXZpc2lvbjEPMA0GA1UEBxMGTW9zY293MQswCQYDVQQGEwJydTEZ"
064: + "MBcGCSqGSIb3DQEJARYKc2RiQGRvbC5ydTBjMBwGBiqFAwICEzASBgcqhQMCAiQABgcqhQMCAh4B"
065: + "A0MABEDkqNT/3f8NHj6EUiWnK4JbVZBh31bEpkwq9z3jf0u8ZndG56Vt+K1ZB6EpFxLT7hSIos0w"
066: + "weZ2YuTZ4w43OgodoAAwCAYGKoUDAgIDA0EASk/IUXWxoi6NtcUGVF23VRV1L3undB4sRZLp4Vho"
067: + "gQ7m3CMbZFfJ2cPu6QyarseXGYHmazoirH5lGjEo535c1g==");
068:
069: private byte[] gost3410EC_ExB = Base64
070: .decode("MIIBPTCB7QIBADCBgDENMAsGA1UEAxMEdGVzdDEWMBQGA1UEChMNRGVtb3MgQ28gTHRkLjEeMBwG"
071: + "A1UECxMVQ3J5cHRvZ3JhcGh5IGRpdmlzaW9uMQ8wDQYDVQQHEwZNb3Njb3cxCzAJBgNVBAYTAnJ1"
072: + "MRkwFwYJKoZIhvcNAQkBFgpzZGJAZG9sLnJ1MGMwHAYGKoUDAgITMBIGByqFAwICJAEGByqFAwIC"
073: + "HgEDQwAEQMBWYUKPy/1Kxad9ChAmgoSWSYOQxRnXo7KEGLU5RNSXA4qMUvArWzvhav+EYUfTbWLh"
074: + "09nELDyHt2XQcvgQHnSgADAIBgYqhQMCAgMDQQAdaNhgH/ElHp64mbMaEo1tPCg9Q22McxpH8rCz"
075: + "E0QBpF4H5mSSQVGI5OAXHToetnNuh7gHHSynyCupYDEHTbkZ");
076:
077: public String getName() {
078: return "PKCS10CertRequest";
079: }
080:
081: private void generationTest(int keySize, String keyName,
082: String sigName, String provider) throws Exception {
083: KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyName,
084: "BC");
085:
086: kpg.initialize(keySize);
087:
088: KeyPair kp = kpg.genKeyPair();
089:
090: Hashtable attrs = new Hashtable();
091:
092: attrs.put(X509Principal.C, "AU");
093: attrs.put(X509Principal.O, "The Legion of the Bouncy Castle");
094: attrs.put(X509Principal.L, "Melbourne");
095: attrs.put(X509Principal.ST, "Victoria");
096: attrs.put(X509Principal.EmailAddress,
097: "feedback-crypto@bouncycastle.org");
098:
099: X509Name subject = new X509Name(attrs);
100:
101: PKCS10CertificationRequest req1 = new PKCS10CertificationRequest(
102: sigName, subject, kp.getPublic(), null,
103: kp.getPrivate(), provider);
104:
105: byte[] bytes = req1.getEncoded();
106:
107: PKCS10CertificationRequest req2 = new PKCS10CertificationRequest(
108: bytes);
109:
110: if (!req2.verify(provider)) {
111: fail(sigName + ": Failed verify check.");
112: }
113:
114: if (!req2.getPublicKey(provider).equals(
115: req1.getPublicKey(provider))) {
116: fail(keyName + ": Failed public key check.");
117: }
118: }
119:
120: /*
121: * we generate a self signed certificate for the sake of testing - SHA224withECDSA
122: */
123: private void createECRequest(String algorithm,
124: DERObjectIdentifier algOid) throws Exception {
125: ECCurve.Fp curve = new ECCurve.Fp(
126: new BigInteger(
127: "6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151"), // q (or p)
128: new BigInteger(
129: "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC",
130: 16), // a
131: new BigInteger(
132: "0051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00",
133: 16)); // b
134:
135: ECParameterSpec spec = new ECParameterSpec(
136: curve,
137: curve
138: .decodePoint(Hex
139: .decode("02C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66")), // G
140: new BigInteger(
141: "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409",
142: 16)); // n
143:
144: ECPrivateKeySpec privKeySpec = new ECPrivateKeySpec(
145: new BigInteger(
146: "5769183828869504557786041598510887460263120754767955773309066354712783118202294874205844512909370791582896372147797293913785865682804434049019366394746072023"), // d
147: spec);
148:
149: ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(
150: curve
151: .decodePoint(Hex
152: .decode("026BFDD2C9278B63C92D6624F151C9D7A822CC75BD983B17D25D74C26740380022D3D8FAF304781E416175EADF4ED6E2B47142D2454A7AC7801DD803CF44A4D1F0AC")), // Q
153: spec);
154:
155: //
156: // set up the keys
157: //
158: PrivateKey privKey;
159: PublicKey pubKey;
160:
161: KeyFactory fact = KeyFactory.getInstance("ECDSA", "BC");
162:
163: privKey = fact.generatePrivate(privKeySpec);
164: pubKey = fact.generatePublic(pubKeySpec);
165:
166: PKCS10CertificationRequest req = new PKCS10CertificationRequest(
167: algorithm, new X509Name("CN=XXX"), pubKey, null,
168: privKey);
169: if (!req.verify()) {
170: fail("Failed verify check EC.");
171: }
172:
173: req = new PKCS10CertificationRequest(req.getEncoded());
174: if (!req.verify()) {
175: fail("Failed verify check EC encoded.");
176: }
177:
178: //
179: // try with point compression turned off
180: //
181: ((ECPointEncoder) pubKey).setPointFormat("UNCOMPRESSED");
182:
183: req = new PKCS10CertificationRequest(algorithm, new X509Name(
184: "CN=XXX"), pubKey, null, privKey);
185: if (!req.verify()) {
186: fail("Failed verify check EC uncompressed.");
187: }
188:
189: req = new PKCS10CertificationRequest(req.getEncoded());
190: if (!req.verify()) {
191: fail("Failed verify check EC uncompressed encoded.");
192: }
193:
194: if (!req.getSignatureAlgorithm().getObjectId().equals(algOid)) {
195: fail("ECDSA oid incorrect.");
196: }
197:
198: if (req.getSignatureAlgorithm().getParameters() != null) {
199: fail("ECDSA parameters incorrect.");
200: }
201:
202: Signature sig = Signature.getInstance(algorithm, "BC");
203:
204: sig.initVerify(pubKey);
205:
206: sig.update(req.getCertificationRequestInfo().getEncoded());
207:
208: if (!sig.verify(req.getSignature().getBytes())) {
209: fail("signature not mapped correctly.");
210: }
211: }
212:
213: private void createECGOSTRequest() throws Exception {
214: String algorithm = "GOST3411withECGOST3410";
215: KeyPairGenerator ecGostKpg = KeyPairGenerator.getInstance(
216: "ECGOST3410", "BC");
217:
218: ecGostKpg.initialize(ECGOST3410NamedCurveTable
219: .getParameterSpec("GostR3410-2001-CryptoPro-A"),
220: new SecureRandom());
221:
222: //
223: // set up the keys
224: //
225: KeyPair pair = ecGostKpg.generateKeyPair();
226: PrivateKey privKey = pair.getPrivate();
227: PublicKey pubKey = pair.getPublic();
228:
229: PKCS10CertificationRequest req = new PKCS10CertificationRequest(
230: algorithm, new X509Name("CN=XXX"), pubKey, null,
231: privKey);
232: if (!req.verify()) {
233: fail("Failed verify check EC.");
234: }
235:
236: req = new PKCS10CertificationRequest(req.getEncoded());
237: if (!req.verify()) {
238: fail("Failed verify check EC encoded.");
239: }
240:
241: if (!req
242: .getSignatureAlgorithm()
243: .getObjectId()
244: .equals(
245: CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_2001)) {
246: fail("ECGOST oid incorrect.");
247: }
248:
249: if (req.getSignatureAlgorithm().getParameters() != null) {
250: fail("ECGOST parameters incorrect.");
251: }
252:
253: Signature sig = Signature.getInstance(algorithm, "BC");
254:
255: sig.initVerify(pubKey);
256:
257: sig.update(req.getCertificationRequestInfo().getEncoded());
258:
259: if (!sig.verify(req.getSignature().getBytes())) {
260: fail("signature not mapped correctly.");
261: }
262: }
263:
264: private void createPSSTest(String algorithm) throws Exception {
265: RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(
266: new BigInteger(
267: "a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137",
268: 16), new BigInteger("010001", 16));
269:
270: RSAPrivateCrtKeySpec privKeySpec = new RSAPrivateCrtKeySpec(
271: new BigInteger(
272: "a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137",
273: 16),
274: new BigInteger("010001", 16),
275: new BigInteger(
276: "33a5042a90b27d4f5451ca9bbbd0b44771a101af884340aef9885f2a4bbe92e894a724ac3c568c8f97853ad07c0266c8c6a3ca0929f1e8f11231884429fc4d9ae55fee896a10ce707c3ed7e734e44727a39574501a532683109c2abacaba283c31b4bd2f53c3ee37e352cee34f9e503bd80c0622ad79c6dcee883547c6a3b325",
277: 16),
278: new BigInteger(
279: "e7e8942720a877517273a356053ea2a1bc0c94aa72d55c6e86296b2dfc967948c0a72cbccca7eacb35706e09a1df55a1535bd9b3cc34160b3b6dcd3eda8e6443",
280: 16),
281: new BigInteger(
282: "b69dca1cf7d4d7ec81e75b90fcca874abcde123fd2700180aa90479b6e48de8d67ed24f9f19d85ba275874f542cd20dc723e6963364a1f9425452b269a6799fd",
283: 16),
284: new BigInteger(
285: "28fa13938655be1f8a159cbaca5a72ea190c30089e19cd274a556f36c4f6e19f554b34c077790427bbdd8dd3ede2448328f385d81b30e8e43b2fffa027861979",
286: 16),
287: new BigInteger(
288: "1a8b38f398fa712049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729",
289: 16),
290: new BigInteger(
291: "27156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24a79f4d",
292: 16));
293:
294: KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
295:
296: PrivateKey privKey = fact.generatePrivate(privKeySpec);
297: PublicKey pubKey = fact.generatePublic(pubKeySpec);
298:
299: PKCS10CertificationRequest req = new PKCS10CertificationRequest(
300: algorithm, new X509Name("CN=XXX"), pubKey, null,
301: privKey);
302: if (!req.verify()) {
303: fail("Failed verify check PSS.");
304: }
305:
306: req = new PKCS10CertificationRequest(req.getEncoded());
307: if (!req.verify()) {
308: fail("Failed verify check PSS encoded.");
309: }
310:
311: if (!req.getSignatureAlgorithm().getObjectId().equals(
312: PKCSObjectIdentifiers.id_RSASSA_PSS)) {
313: fail("PSS oid incorrect.");
314: }
315:
316: if (req.getSignatureAlgorithm().getParameters() == null) {
317: fail("PSS parameters incorrect.");
318: }
319:
320: Signature sig = Signature.getInstance(algorithm, "BC");
321:
322: sig.initVerify(pubKey);
323:
324: sig.update(req.getCertificationRequestInfo().getEncoded());
325:
326: if (!sig.verify(req.getSignature().getBytes())) {
327: fail("signature not mapped correctly.");
328: }
329: }
330:
331: public void performTest() throws Exception {
332: generationTest(512, "RSA", "SHA1withRSA", "BC");
333: generationTest(512, "GOST3410", "GOST3411withGOST3410", "BC");
334:
335: if (Security.getProvider("SunRsaSign") != null) {
336: generationTest(512, "RSA", "SHA1withRSA", "SunRsaSign");
337: }
338:
339: // elliptic curve GOST A parameter set
340: PKCS10CertificationRequest req = new PKCS10CertificationRequest(
341: gost3410EC_A);
342: if (!req.verify()) {
343: fail("Failed verify check gost3410EC_A.");
344: }
345:
346: // elliptic curve GOST B parameter set
347: req = new PKCS10CertificationRequest(gost3410EC_B);
348: if (!req.verify()) {
349: fail("Failed verify check gost3410EC_B.");
350: }
351:
352: // elliptic curve GOST C parameter set
353: req = new PKCS10CertificationRequest(gost3410EC_C);
354: if (!req.verify()) {
355: fail("Failed verify check gost3410EC_C.");
356: }
357:
358: // elliptic curve GOST ExA parameter set
359: req = new PKCS10CertificationRequest(gost3410EC_ExA);
360: if (!req.verify()) {
361: fail("Failed verify check gost3410EC_ExA.");
362: }
363:
364: // elliptic curve GOST ExB parameter set
365: req = new PKCS10CertificationRequest(gost3410EC_ExB);
366: if (!req.verify()) {
367: fail("Failed verify check gost3410EC_ExA.");
368: }
369:
370: // elliptic curve openSSL
371: KeyPairGenerator g = KeyPairGenerator
372: .getInstance("ECDSA", "BC");
373:
374: ECCurve curve = new ECCurve.Fp(
375: new BigInteger(
376: "883423532389192164791648750360308885314476597252960362792450860609699839"), // q
377: new BigInteger(
378: "7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc",
379: 16), // a
380: new BigInteger(
381: "6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a",
382: 16)); // b
383:
384: ECParameterSpec ecSpec = new ECParameterSpec(
385: curve,
386: curve
387: .decodePoint(Hex
388: .decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G
389: new BigInteger(
390: "883423532389192164791648750360308884807550341691627752275345424702807307")); // n
391:
392: g.initialize(ecSpec, new SecureRandom());
393:
394: KeyPair kp = g.generateKeyPair();
395:
396: req = new PKCS10CertificationRequest("ECDSAWITHSHA1",
397: new X509Name("CN=XXX"), kp.getPublic(), null, kp
398: .getPrivate());
399: if (!req.verify()) {
400: fail("Failed verify check EC.");
401: }
402:
403: createECRequest("SHA1withECDSA",
404: X9ObjectIdentifiers.ecdsa_with_SHA1);
405: createECRequest("SHA224withECDSA",
406: X9ObjectIdentifiers.ecdsa_with_SHA224);
407: createECRequest("SHA256withECDSA",
408: X9ObjectIdentifiers.ecdsa_with_SHA256);
409: createECRequest("SHA384withECDSA",
410: X9ObjectIdentifiers.ecdsa_with_SHA384);
411: createECRequest("SHA512withECDSA",
412: X9ObjectIdentifiers.ecdsa_with_SHA512);
413:
414: createECGOSTRequest();
415:
416: createPSSTest("SHA1withRSAandMGF1");
417: createPSSTest("SHA224withRSAandMGF1");
418: createPSSTest("SHA256withRSAandMGF1");
419: createPSSTest("SHA384withRSAandMGF1");
420: }
421:
422: public static void main(String[] args) {
423: Security.addProvider(new BouncyCastleProvider());
424:
425: runTest(new PKCS10CertRequestTest());
426: }
427: }
|