1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
package org.spongycastle.jce.provider.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Security;
import java.util.Hashtable;
import org.spongycastle.asn1.ASN1InputStream;
import org.spongycastle.asn1.ASN1Sequence;
import org.spongycastle.asn1.DEROutputStream;
import org.spongycastle.asn1.x509.X509Name;
import org.spongycastle.jce.PKCS10CertificationRequest;
import org.spongycastle.jce.X509Principal;
import org.spongycastle.jce.provider.BouncyCastleProvider;
import org.spongycastle.util.test.SimpleTestResult;
import org.spongycastle.util.test.Test;
import org.spongycastle.util.test.TestResult;
/**
**/
public class PKCS10CertRequestTest
implements Test
{
public String getName()
{
return "PKCS10CertRequest";
}
public TestResult perform()
{
try
{
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "SC");
kpg.initialize(512);
KeyPair kp = kpg.generateKeyPair();
Hashtable attrs = new Hashtable();
attrs.put(X509Principal.C, "AU");
attrs.put(X509Principal.O, "The Legion of the Bouncy Castle");
attrs.put(X509Principal.L, "Melbourne");
attrs.put(X509Principal.ST, "Victoria");
attrs.put(X509Principal.EmailAddress, "feedback-crypto@bouncycastle.org");
X509Name subject = new X509Name(attrs);
PKCS10CertificationRequest req1 = new PKCS10CertificationRequest(
"SHA1withRSA",
subject,
kp.getPublic(),
null,
kp.getPrivate());
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
DEROutputStream dOut = new DEROutputStream(bOut);
dOut.writeObject(req1);
dOut.close();
ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray());
ASN1InputStream dIn = new ASN1InputStream(bIn);
PKCS10CertificationRequest req2 = new PKCS10CertificationRequest(
(ASN1Sequence)dIn.readObject());
if (!req2.verify())
{
return new SimpleTestResult(false, getName() + ": Failed verify check.");
}
if (!req2.getPublicKey().equals(req1.getPublicKey()))
{
return new SimpleTestResult(false, getName() + ": Failed public key check.");
}
return new SimpleTestResult(true, getName() + ": Okay");
}
catch (Exception e)
{
e.printStackTrace();
return new SimpleTestResult(false, getName() + ": exception - " + e.toString());
}
}
public static void main(
String[] args)
{
Security.addProvider(new BouncyCastleProvider());
Test test = new PKCS10CertRequestTest();
TestResult result = test.perform();
System.out.println(result.toString());
}
}
|