Skip to content

Commit 1cc10a0

Browse files
committed
Further fixes to issued cert generation
1 parent 8951faa commit 1cc10a0

File tree

7 files changed

+64
-32
lines changed

7 files changed

+64
-32
lines changed

HIRS_AttestationCA/src/main/java/hirs/attestationca/persist/entity/manager/CertificateRepository.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ public interface CertificateRepository extends JpaRepository<Certificate, UUID>
125125

126126
/**
127127
* Query that retrieves a list of issued attestation certificates using the provided device id,
128-
* isLDevID value and sort value.
128+
* ldevID value and sort value.
129129
*
130130
* @param deviceId device id
131-
* @param isLDevID is it a LDevId
131+
* @param ldevID is it a LDevId
132132
* @param sort sort
133133
* @return a list of issued attestation certificates
134134
*/
135-
List<IssuedAttestationCertificate> findByDeviceIdAndIsLDevID(UUID deviceId, boolean isLDevID, Sort sort);
135+
List<IssuedAttestationCertificate> findByDeviceIdAndLdevID(UUID deviceId, boolean ldevID, Sort sort);
136136

137137
/**
138138
* Query that retrieves a certificates using the provided certificate hash.
@@ -142,3 +142,4 @@ public interface CertificateRepository extends JpaRepository<Certificate, UUID>
142142
*/
143143
Certificate findByCertificateHash(int certificateHash);
144144
}
145+

HIRS_AttestationCA/src/main/java/hirs/attestationca/persist/entity/userdefined/certificate/IssuedAttestationCertificate.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class IssuedAttestationCertificate extends DeviceAssociatedCertificate {
2929
public static final String AIC_TYPE_LABEL = "TCPA Trusted Platform Identity";
3030

3131
@Column
32-
private boolean isLDevID;
32+
private boolean ldevID;
3333

3434
@ManyToOne(fetch = FetchType.EAGER)
3535
@JoinColumn(name = "ek_id")
@@ -45,18 +45,18 @@ public class IssuedAttestationCertificate extends DeviceAssociatedCertificate {
4545
* @param certificateBytes the issued certificate bytes
4646
* @param endorsementCredential the endorsement credential
4747
* @param platformCredentials the platform credentials
48-
* @param isLDevID is LDevId
48+
* @param ldevID is LDevID
4949
* @throws IOException if there is a problem extracting information from the certificate
5050
*/
5151
public IssuedAttestationCertificate(final byte[] certificateBytes,
5252
final EndorsementCredential endorsementCredential,
5353
final List<PlatformCredential> platformCredentials,
54-
final boolean isLDevID)
54+
final boolean ldevID)
5555
throws IOException {
5656
super(certificateBytes);
5757
this.endorsementCredential = endorsementCredential;
5858
this.platformCredentials = new ArrayList<>(platformCredentials);
59-
this.isLDevID = isLDevID;
59+
this.ldevID = ldevID;
6060
}
6161

6262
/**
@@ -65,14 +65,14 @@ public IssuedAttestationCertificate(final byte[] certificateBytes,
6565
* @param certificatePath path to certificate
6666
* @param endorsementCredential the endorsement credential
6767
* @param platformCredentials the platform credentials
68-
* @param isLDevID is it an LDev ID
68+
* @param ldevID is it an LDevID
6969
* @throws IOException if there is a problem extracting information from the certificate
7070
*/
7171
public IssuedAttestationCertificate(final Path certificatePath,
7272
final EndorsementCredential endorsementCredential,
7373
final List<PlatformCredential> platformCredentials,
74-
final boolean isLDevID)
74+
final boolean ldevID)
7575
throws IOException {
76-
this(readBytes(certificatePath), endorsementCredential, platformCredentials, isLDevID);
76+
this(readBytes(certificatePath), endorsementCredential, platformCredentials, ldevID);
7777
}
7878
}

HIRS_AttestationCA/src/main/java/hirs/attestationca/persist/provision/AbstractProcessor.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.bouncycastle.asn1.x509.Extension;
2424
import org.bouncycastle.asn1.x509.KeyUsage;
2525
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
26-
import org.bouncycastle.asn1.x509.X509Extension;
2726
import org.bouncycastle.cert.X509CertificateHolder;
2827
import org.bouncycastle.cert.X509v3CertificateBuilder;
2928
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
@@ -123,14 +122,14 @@ protected X509Certificate generateCredential(final PublicKey publicKey,
123122

124123
// Add signing extension
125124
builder.addExtension(
126-
X509Extension.keyUsage,
125+
Extension.keyUsage,
127126
true,
128127
new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyEncipherment)
129128
);
130129

131130
// Basic constraints
132131
builder.addExtension(
133-
X509Extension.basicConstraints,
132+
Extension.basicConstraints,
134133
true,
135134
new BasicConstraints(false)
136135
);
@@ -255,7 +254,7 @@ private EndorsementCredential getEndorsementCredential(
255254
* @param endorsementCredential the endorsement credential used to generate the AC
256255
* @param platformCredentials the platform credentials used to generate the AC
257256
* @param device the device to which the attestation certificate is tied
258-
* @param isLDevID whether the certificate is a ldevid
257+
* @param ldevID whether the certificate is a ldevid
259258
* @return whether the certificate was saved successfully
260259
* @throws {@link CertificateProcessingException} if error occurs in persisting the Attestation
261260
* Certificate
@@ -265,7 +264,7 @@ public boolean saveAttestationCertificate(final CertificateRepository certificat
265264
final EndorsementCredential endorsementCredential,
266265
final List<PlatformCredential> platformCredentials,
267266
final Device device,
268-
final boolean isLDevID) {
267+
final boolean ldevID) {
269268
List<IssuedAttestationCertificate> issuedAc;
270269
boolean generateCertificate = true;
271270
PolicyRepository scp = getPolicyRepository();
@@ -275,27 +274,27 @@ public boolean saveAttestationCertificate(final CertificateRepository certificat
275274
try {
276275
// save issued certificate
277276
IssuedAttestationCertificate attCert = new IssuedAttestationCertificate(
278-
derEncodedAttestationCertificate, endorsementCredential, platformCredentials, isLDevID);
277+
derEncodedAttestationCertificate, endorsementCredential, platformCredentials, ldevID);
279278

280279
if (scp != null) {
281280
policySettings = scp.findByName("Default");
282281

283282
Sort sortCriteria = Sort.by(Sort.Direction.DESC, "endValidity");
284-
issuedAc = certificateRepository.findByDeviceIdAndIsLDevID(device.getId(), isLDevID,
283+
issuedAc = certificateRepository.findByDeviceIdAndLdevID(device.getId(), ldevID,
285284
sortCriteria);
286285

287-
generateCertificate = isLDevID ? policySettings.isIssueDevIdCertificate()
286+
generateCertificate = ldevID ? policySettings.isIssueDevIdCertificate()
288287
: policySettings.isIssueAttestationCertificate();
289288

290289
if (issuedAc != null && issuedAc.size() > 0
291-
&& (isLDevID ? policySettings.isDevIdExpirationFlag()
290+
&& (ldevID ? policySettings.isDevIdExpirationFlag()
292291
: policySettings.isGenerateOnExpiration())) {
293292
if (issuedAc.get(0).getEndValidity().after(currentDate)) {
294293
// so the issued AC is not expired
295294
// however are we within the threshold
296295
days = ProvisionUtils.daysBetween(currentDate, issuedAc.get(0).getEndValidity());
297296
generateCertificate =
298-
days < Integer.parseInt(isLDevID ? policySettings.getDevIdReissueThreshold()
297+
days < Integer.parseInt(ldevID ? policySettings.getDevIdReissueThreshold()
299298
: policySettings.getReissueThreshold());
300299
}
301300
}

HIRS_AttestationCA/src/test/java/hirs/attestationca/persist/AttestationCertificateAuthorityTest.java

+39-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
import org.bouncycastle.asn1.x509.Extension;
2020
import org.bouncycastle.asn1.x509.GeneralNames;
2121
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;
2225
import org.bouncycastle.jce.provider.BouncyCastleProvider;
26+
import org.bouncycastle.operator.ContentSigner;
27+
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
2328
import org.junit.jupiter.api.BeforeAll;
2429
import org.junit.jupiter.api.Disabled;
2530
import org.junit.jupiter.api.Nested;
@@ -33,7 +38,6 @@
3338
import javax.crypto.spec.OAEPParameterSpec;
3439
import javax.crypto.spec.PSource;
3540
import javax.crypto.spec.SecretKeySpec;
36-
import javax.security.auth.x500.X500Principal;
3741
import java.io.IOException;
3842
import java.math.BigInteger;
3943
import java.net.URISyntaxException;
@@ -53,6 +57,7 @@
5357
import java.security.interfaces.RSAPublicKey;
5458
import java.security.spec.MGF1ParameterSpec;
5559
import java.util.Calendar;
60+
import java.util.Date;
5661
import java.util.LinkedList;
5762
import java.util.List;
5863
import java.util.Objects;
@@ -61,6 +66,7 @@
6166
import static org.junit.jupiter.api.Assertions.assertEquals;
6267
import static org.junit.jupiter.api.Assertions.assertNotNull;
6368
import static org.junit.jupiter.api.Assertions.assertThrows;
69+
import static org.junit.jupiter.api.Assertions.fail;
6470
import static org.mockito.Mockito.mock;
6571
import static org.mockito.Mockito.verify;
6672
import static org.mockito.Mockito.verifyNoMoreInteractions;
@@ -376,6 +382,37 @@ public void testGenerateAttestation() throws Exception {
376382
verifyNoMoreInteractions(certificate, symmetricKey);
377383
}
378384

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+
379416
/**
380417
* Tests {@link AttestationCertificateAuthority#
381418
* AttestationCertificateAuthority(SupplyChainValidationService, PrivateKey,
@@ -390,14 +427,13 @@ public void testGenerateCredential() throws Exception {
390427
final String identityProofLabelString = "label";
391428
byte[] identityProofLabel = identityProofLabelString.getBytes(StandardCharsets.UTF_8);
392429
byte[] modulus = ((RSAPublicKey) keyPair.getPublic()).getModulus().toByteArray();
393-
X500Principal principal = new X500Principal("CN=TEST, OU=TEST, O=TEST, C=TEST");
394430
int validDays = 1;
395431

396432
// create mocks for testing
397433
IdentityProof identityProof = mock(IdentityProof.class);
398434
AsymmetricPublicKey asymmetricPublicKey = mock(AsymmetricPublicKey.class);
399435
StorePubKey storePubKey = mock(StorePubKey.class);
400-
X509Certificate acaCertificate = mock(X509Certificate.class);
436+
X509Certificate acaCertificate = createSelfSignedCertificate(keyPair);
401437

402438
// assign ACA fields
403439
ReflectionTestUtils.setField(aca, "validDays", validDays);
@@ -406,10 +442,6 @@ public void testGenerateCredential() throws Exception {
406442
// prepare identity proof interactions
407443
when(identityProof.getLabel()).thenReturn(identityProofLabel);
408444

409-
// prepare other mocks
410-
when(acaCertificate.getSubjectX500Principal()).thenReturn(principal);
411-
when(acaCertificate.getIssuerX500Principal()).thenReturn(principal);
412-
413445
// perform the test
414446
X509Certificate certificate = abstractProcessor.accessGenerateCredential(keyPair.getPublic(),
415447
null,
@@ -453,7 +485,6 @@ public void testGenerateCredential() throws Exception {
453485
assertEquals(tomorrow.get(Calendar.DATE), afterDate.get(Calendar.DATE));
454486

455487
// validate mock interactions
456-
verify(acaCertificate).getSubjectX500Principal();
457488
verifyNoMoreInteractions(identityProof, asymmetricPublicKey, storePubKey);
458489
}
459490

HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/PersistenceJPAConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class PersistenceJPAConfig implements WebMvcConfigurer {
7474
@Value("${server.ssl.key-store-password:''}")
7575
private String keyStorePassword;
7676

77-
@Value("${server.ssl.key-alias}")
77+
@Value("${aca.certificates.signing-key-alias}")
7878
private String keyAlias;
7979

8080
@Autowired

HIRS_AttestationCAPortal/src/main/resources/application.properties

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ server.ssl.trust-store=/etc/hirs/certificates/HIRS/TrustStore.jks
2929
server.ssl.trust-alias=hirs_aca_tls_rsa_3k_sha384
3030
server.ssl.key-store-type=JKS
3131
server.ssl.key-store=/etc/hirs/certificates/HIRS/KeyStore.jks
32-
server.ssl.key-alias=HIRS_leaf_ca3_rsa_3k_sha384.pem
32+
server.ssl.key-alias=hirs_aca_tls_rsa_3k_sha384
3333
server.ssl.enabled-protocols=TLSv1.2, TLSv1.3
3434
server.ssl.ciphers=TLS_AES_256_GCM_SHA384, ECDHE-ECDSA-AES256-GCM-SHA384, ECDHE-RSA-AES256-GCM-SHA384, DHE-RSA-AES256-GCM-SHA384, AES256-GCM-SHA384
3535
# ACA specific default properties
36+
aca.certificates.signing-key-alias=HIRS_leaf_ca3_rsa_3k_sha384
3637
aca.certificates.validity=3652
3738
# Compression settings
3839
server.compression.enabled=true

HIRS_AttestationCAPortal/src/main/webapp/WEB-INF/jsp/issued-certificates.jsp

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
}
5454
},
5555
{
56-
data: 'isLDevID',
56+
data: 'ldevID',
5757
searchable:false,
5858
render: function (data, type, full, meta) {
5959
if (data === true) {
@@ -134,4 +134,4 @@
134134
</script>
135135
</jsp:body>
136136

137-
</my:page>
137+
</my:page>

0 commit comments

Comments
 (0)