19
19
import org .bouncycastle .asn1 .x509 .Extension ;
20
20
import org .bouncycastle .asn1 .x509 .GeneralNames ;
21
21
import org .bouncycastle .asn1 .x509 .TBSCertificate ;
22
+ import org .bouncycastle .cert .X509v3CertificateBuilder ;
23
+ import org .bouncycastle .cert .jcajce .JcaX509CertificateConverter ;
24
+ import org .bouncycastle .cert .jcajce .JcaX509v3CertificateBuilder ;
22
25
import org .bouncycastle .jce .provider .BouncyCastleProvider ;
26
+ import org .bouncycastle .operator .ContentSigner ;
27
+ import org .bouncycastle .operator .jcajce .JcaContentSignerBuilder ;
23
28
import org .junit .jupiter .api .BeforeAll ;
24
29
import org .junit .jupiter .api .Disabled ;
25
30
import org .junit .jupiter .api .Nested ;
33
38
import javax .crypto .spec .OAEPParameterSpec ;
34
39
import javax .crypto .spec .PSource ;
35
40
import javax .crypto .spec .SecretKeySpec ;
36
- import javax .security .auth .x500 .X500Principal ;
37
41
import java .io .IOException ;
38
42
import java .math .BigInteger ;
39
43
import java .net .URISyntaxException ;
53
57
import java .security .interfaces .RSAPublicKey ;
54
58
import java .security .spec .MGF1ParameterSpec ;
55
59
import java .util .Calendar ;
60
+ import java .util .Date ;
56
61
import java .util .LinkedList ;
57
62
import java .util .List ;
58
63
import java .util .Objects ;
61
66
import static org .junit .jupiter .api .Assertions .assertEquals ;
62
67
import static org .junit .jupiter .api .Assertions .assertNotNull ;
63
68
import static org .junit .jupiter .api .Assertions .assertThrows ;
69
+ import static org .junit .jupiter .api .Assertions .fail ;
64
70
import static org .mockito .Mockito .mock ;
65
71
import static org .mockito .Mockito .verify ;
66
72
import static org .mockito .Mockito .verifyNoMoreInteractions ;
@@ -376,6 +382,37 @@ public void testGenerateAttestation() throws Exception {
376
382
verifyNoMoreInteractions (certificate , symmetricKey );
377
383
}
378
384
385
+ /**
386
+ * Creates a self-signed X.509 public-key certificate.
387
+ *
388
+ * @param pair KeyPair to create the cert for
389
+ * @return self-signed X509Certificate
390
+ */
391
+ private static X509Certificate createSelfSignedCertificate (final KeyPair pair ) {
392
+ Security .addProvider (new BouncyCastleProvider ());
393
+ final int timeRange = 10000 ;
394
+ X509Certificate cert = null ;
395
+ try {
396
+
397
+ X500Name issuerName = new X500Name ("CN=TEST2, OU=TEST2, O=TEST2, C=TEST2" );
398
+ X500Name subjectName = new X500Name ("CN=TEST, OU=TEST, O=TEST, C=TEST" );
399
+ BigInteger serialNumber = BigInteger .ONE ;
400
+ Date notBefore = new Date (System .currentTimeMillis () - timeRange );
401
+ Date notAfter = new Date (System .currentTimeMillis () + timeRange );
402
+ X509v3CertificateBuilder builder =
403
+ new JcaX509v3CertificateBuilder (issuerName , serialNumber , notBefore , notAfter ,
404
+ subjectName , pair .getPublic ());
405
+ ContentSigner signer =
406
+ new JcaContentSignerBuilder ("SHA256WithRSA" ).setProvider ("BC" ).build (
407
+ pair .getPrivate ());
408
+ return new JcaX509CertificateConverter ().setProvider ("BC" ).getCertificate (
409
+ builder .build (signer ));
410
+ } catch (Exception e ) {
411
+ fail ("Exception occurred while creating a cert" , e );
412
+ }
413
+ return cert ;
414
+ }
415
+
379
416
/**
380
417
* Tests {@link AttestationCertificateAuthority#
381
418
* AttestationCertificateAuthority(SupplyChainValidationService, PrivateKey,
@@ -390,14 +427,13 @@ public void testGenerateCredential() throws Exception {
390
427
final String identityProofLabelString = "label" ;
391
428
byte [] identityProofLabel = identityProofLabelString .getBytes (StandardCharsets .UTF_8 );
392
429
byte [] modulus = ((RSAPublicKey ) keyPair .getPublic ()).getModulus ().toByteArray ();
393
- X500Principal principal = new X500Principal ("CN=TEST, OU=TEST, O=TEST, C=TEST" );
394
430
int validDays = 1 ;
395
431
396
432
// create mocks for testing
397
433
IdentityProof identityProof = mock (IdentityProof .class );
398
434
AsymmetricPublicKey asymmetricPublicKey = mock (AsymmetricPublicKey .class );
399
435
StorePubKey storePubKey = mock (StorePubKey .class );
400
- X509Certificate acaCertificate = mock ( X509Certificate . class );
436
+ X509Certificate acaCertificate = createSelfSignedCertificate ( keyPair );
401
437
402
438
// assign ACA fields
403
439
ReflectionTestUtils .setField (aca , "validDays" , validDays );
@@ -406,10 +442,6 @@ public void testGenerateCredential() throws Exception {
406
442
// prepare identity proof interactions
407
443
when (identityProof .getLabel ()).thenReturn (identityProofLabel );
408
444
409
- // prepare other mocks
410
- when (acaCertificate .getSubjectX500Principal ()).thenReturn (principal );
411
- when (acaCertificate .getIssuerX500Principal ()).thenReturn (principal );
412
-
413
445
// perform the test
414
446
X509Certificate certificate = abstractProcessor .accessGenerateCredential (keyPair .getPublic (),
415
447
null ,
@@ -453,7 +485,6 @@ public void testGenerateCredential() throws Exception {
453
485
assertEquals (tomorrow .get (Calendar .DATE ), afterDate .get (Calendar .DATE ));
454
486
455
487
// validate mock interactions
456
- verify (acaCertificate ).getSubjectX500Principal ();
457
488
verifyNoMoreInteractions (identityProof , asymmetricPublicKey , storePubKey );
458
489
}
459
490
0 commit comments