|
1 | 1 | package org.bouncycastle.jce.provider.test; |
2 | 2 |
|
3 | 3 | import java.io.ByteArrayInputStream; |
4 | | -import java.io.IOException; |
5 | 4 | import java.math.BigInteger; |
6 | 5 | import java.security.KeyPair; |
7 | | -import java.security.KeyPairGenerator; |
8 | | -import java.security.MessageDigest; |
9 | | -import java.security.NoSuchAlgorithmException; |
10 | 6 | import java.security.PrivateKey; |
11 | 7 | import java.security.PublicKey; |
12 | | -import java.security.SecureRandom; |
13 | 8 | import java.security.Signature; |
14 | 9 | import java.security.cert.CertificateFactory; |
15 | 10 | import java.security.cert.X509CRL; |
|
29 | 24 | import org.bouncycastle.asn1.x500.X500Name; |
30 | 25 | import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
31 | 26 | import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier; |
32 | | -import org.bouncycastle.asn1.x509.BasicConstraints; |
33 | 27 | import org.bouncycastle.asn1.x509.CRLNumber; |
34 | 28 | import org.bouncycastle.asn1.x509.CRLReason; |
35 | | -import org.bouncycastle.asn1.x509.Certificate; |
36 | 29 | import org.bouncycastle.asn1.x509.Extension; |
37 | 30 | import org.bouncycastle.asn1.x509.Extensions; |
38 | 31 | import org.bouncycastle.asn1.x509.ExtensionsGenerator; |
39 | 32 | import org.bouncycastle.asn1.x509.GeneralName; |
40 | 33 | import org.bouncycastle.asn1.x509.GeneralNames; |
41 | | -import org.bouncycastle.asn1.x509.KeyUsage; |
42 | | -import org.bouncycastle.asn1.x509.SubjectKeyIdentifier; |
43 | 34 | import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; |
44 | 35 | import org.bouncycastle.asn1.x509.TBSCertList; |
45 | 36 | import org.bouncycastle.asn1.x509.TBSCertificate; |
@@ -190,83 +181,6 @@ public static X509Certificate createCertWithIDs(X500Name signerName, String sigN |
190 | 181 | return (X509Certificate)CertificateFactory.getInstance("X.509", "BC").generateCertificate(new ByteArrayInputStream(new DERSequence(v).getEncoded(ASN1Encoding.DER))); |
191 | 182 | } |
192 | 183 |
|
193 | | - /** |
194 | | - * Create a random 1024 bit RSA key pair |
195 | | - */ |
196 | | - public static KeyPair generateRSAKeyPair() |
197 | | - throws Exception |
198 | | - { |
199 | | - KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC"); |
200 | | - |
201 | | - kpGen.initialize(1024, new SecureRandom()); |
202 | | - |
203 | | - return kpGen.generateKeyPair(); |
204 | | - } |
205 | | - |
206 | | - public static X509Certificate generateRootCert(KeyPair pair) |
207 | | - throws Exception |
208 | | - { |
209 | | - return createSelfSignedCert("CN=Test CA Certificate", "SHA256withRSA", pair); |
210 | | - } |
211 | | - |
212 | | - public static X509Certificate generateRootCert(KeyPair pair, X500Name dn) |
213 | | - throws Exception |
214 | | - { |
215 | | - return createSelfSignedCert(dn, "SHA256withRSA", pair); |
216 | | - } |
217 | | - |
218 | | - public static X509Certificate generateIntermediateCert(PublicKey intKey, PrivateKey caKey, X509Certificate caCert) |
219 | | - throws Exception |
220 | | - { |
221 | | - return generateIntermediateCert( |
222 | | - intKey, new X500Name("CN=Test Intermediate Certificate"), caKey, caCert); |
223 | | - } |
224 | | - |
225 | | - public static X509Certificate generateIntermediateCert(PublicKey intKey, X500Name subject, PrivateKey caKey, X509Certificate caCert) |
226 | | - throws Exception |
227 | | - { |
228 | | - Certificate caCertLw = Certificate.getInstance(caCert.getEncoded()); |
229 | | - |
230 | | - ExtensionsGenerator extGen = new ExtensionsGenerator(); |
231 | | - |
232 | | - extGen.addExtension(Extension.authorityKeyIdentifier, false, new AuthorityKeyIdentifier(getDigest(caCertLw.getSubjectPublicKeyInfo()), |
233 | | - new GeneralNames(new GeneralName(caCertLw.getIssuer())), |
234 | | - caCertLw.getSerialNumber().getValue())); |
235 | | - extGen.addExtension(Extension.subjectKeyIdentifier, false, new SubjectKeyIdentifier(getDigest(SubjectPublicKeyInfo.getInstance(intKey.getEncoded())))); |
236 | | - extGen.addExtension(Extension.basicConstraints, true, new BasicConstraints(0)); |
237 | | - extGen.addExtension(Extension.keyUsage, true, new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyCertSign | KeyUsage.cRLSign)); |
238 | | - |
239 | | - return createCert( |
240 | | - caCertLw.getSubject(), |
241 | | - caKey, subject, "SHA256withRSA", extGen.generate(), intKey); |
242 | | - } |
243 | | - |
244 | | - public static X509Certificate generateEndEntityCert(PublicKey intKey, PrivateKey caKey, X509Certificate caCert) |
245 | | - throws Exception |
246 | | - { |
247 | | - return generateEndEntityCert( |
248 | | - intKey, new X500Name("CN=Test End Certificate"), caKey, caCert); |
249 | | - } |
250 | | - |
251 | | - public static X509Certificate generateEndEntityCert(PublicKey entityKey, X500Name subject, PrivateKey caKey, X509Certificate caCert) |
252 | | - throws Exception |
253 | | - { |
254 | | - Certificate caCertLw = Certificate.getInstance(caCert.getEncoded()); |
255 | | - |
256 | | - ExtensionsGenerator extGen = new ExtensionsGenerator(); |
257 | | - |
258 | | - extGen.addExtension(Extension.authorityKeyIdentifier, false, new AuthorityKeyIdentifier(getDigest(caCertLw.getSubjectPublicKeyInfo()), |
259 | | - new GeneralNames(new GeneralName(caCertLw.getIssuer())), |
260 | | - caCertLw.getSerialNumber().getValue())); |
261 | | - extGen.addExtension(Extension.subjectKeyIdentifier, false, new SubjectKeyIdentifier(getDigest(entityKey.getEncoded()))); |
262 | | - extGen.addExtension(Extension.basicConstraints, true, new BasicConstraints(0)); |
263 | | - extGen.addExtension(Extension.keyUsage, true, new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyCertSign | KeyUsage.cRLSign)); |
264 | | - |
265 | | - return createCert( |
266 | | - caCertLw.getSubject(), |
267 | | - caKey, subject, "SHA256withRSA", extGen.generate(), entityKey); |
268 | | - } |
269 | | - |
270 | 184 | public static X509CRL createCRL( |
271 | 185 | X509Certificate caCert, |
272 | 186 | PrivateKey caKey, |
@@ -309,23 +223,23 @@ public static X509CRL createCRL( |
309 | 223 | return (X509CRL)CertificateFactory.getInstance("X.509", "BC").generateCRL(new ByteArrayInputStream(new DERSequence(v).getEncoded(ASN1Encoding.DER))); |
310 | 224 | } |
311 | 225 |
|
312 | | - private static byte[] getDigest(SubjectPublicKeyInfo spki) |
313 | | - throws IOException |
314 | | - { |
315 | | - return getDigest(spki.getPublicKeyData().getBytes()); |
316 | | - } |
317 | | - |
318 | | - private static byte[] getDigest(byte[] bytes) |
319 | | - { |
320 | | - try |
321 | | - { |
322 | | - return MessageDigest.getInstance("SHA1").digest(bytes); |
323 | | - } |
324 | | - catch (NoSuchAlgorithmException e) |
325 | | - { |
326 | | - return null; |
327 | | - } |
328 | | - } |
| 226 | +// private static byte[] getDigest(SubjectPublicKeyInfo spki) |
| 227 | +// throws IOException |
| 228 | +// { |
| 229 | +// return getDigest(spki.getPublicKeyData().getBytes()); |
| 230 | +// } |
| 231 | + |
| 232 | +// private static byte[] getDigest(byte[] bytes) |
| 233 | +// { |
| 234 | +// try |
| 235 | +// { |
| 236 | +// return MessageDigest.getInstance("SHA1").digest(bytes); |
| 237 | +// } |
| 238 | +// catch (NoSuchAlgorithmException e) |
| 239 | +// { |
| 240 | +// return null; |
| 241 | +// } |
| 242 | +// } |
329 | 243 |
|
330 | 244 | private static DERBitString booleanToBitString(boolean[] id) |
331 | 245 | { |
|
0 commit comments