Skip to content

Commit 0042697

Browse files
committed
Refactor OCSP digest calculator and request builder
Signed-off-by: Mart Somermaa <[email protected]>
1 parent 2bc89ea commit 0042697

File tree

3 files changed

+26
-50
lines changed

3 files changed

+26
-50
lines changed

src/main/java/eu/webeid/security/validator/certvalidators/SubjectCertificateNotRevokedValidator.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424

2525
import eu.webeid.security.exceptions.AuthTokenException;
2626
import eu.webeid.security.exceptions.UserCertificateOCSPCheckFailedException;
27-
import eu.webeid.security.validator.ocsp.*;
27+
import eu.webeid.security.validator.ocsp.DigestCalculatorImpl;
28+
import eu.webeid.security.validator.ocsp.OcspClient;
29+
import eu.webeid.security.validator.ocsp.OcspRequestBuilder;
30+
import eu.webeid.security.validator.ocsp.OcspResponseValidator;
31+
import eu.webeid.security.validator.ocsp.OcspServiceProvider;
2832
import eu.webeid.security.validator.ocsp.service.OcspService;
2933
import org.bouncycastle.asn1.ocsp.OCSPObjectIdentifiers;
3034
import org.bouncycastle.asn1.ocsp.OCSPResponseStatus;
@@ -41,10 +45,6 @@
4145
import org.bouncycastle.operator.OperatorCreationException;
4246
import org.slf4j.Logger;
4347
import org.slf4j.LoggerFactory;
44-
import eu.webeid.security.validator.ocsp.Digester;
45-
import eu.webeid.security.validator.ocsp.OcspClient;
46-
import eu.webeid.security.validator.ocsp.OcspRequestBuilder;
47-
import eu.webeid.security.validator.ocsp.OcspServiceProvider;
4848

4949
import java.io.IOException;
5050
import java.math.BigInteger;
@@ -58,7 +58,7 @@
5858
public final class SubjectCertificateNotRevokedValidator {
5959

6060
private static final Logger LOG = LoggerFactory.getLogger(SubjectCertificateNotRevokedValidator.class);
61-
private static final DigestCalculator DIGEST_CALCULATOR = Digester.sha1();
61+
private static final DigestCalculator DIGEST_CALCULATOR = DigestCalculatorImpl.sha1();
6262

6363
private final SubjectCertificateTrustedValidator trustValidator;
6464
private final OcspClient ocspClient;
@@ -86,10 +86,6 @@ public void validateCertificateNotRevoked(X509Certificate subjectCertificate) th
8686
try {
8787
OcspService ocspService = ocspServiceProvider.getService(subjectCertificate);
8888

89-
if (!ocspService.doesSupportNonce()) {
90-
LOG.debug("Disabling OCSP nonce extension");
91-
}
92-
9389
final CertificateID certificateId = getCertificateId(subjectCertificate,
9490
Objects.requireNonNull(trustValidator.getSubjectCertificateIssuerCertificate()));
9591

@@ -98,6 +94,10 @@ public void validateCertificateNotRevoked(X509Certificate subjectCertificate) th
9894
.enableOcspNonce(ocspService.doesSupportNonce())
9995
.build();
10096

97+
if (!ocspService.doesSupportNonce()) {
98+
LOG.debug("Disabling OCSP nonce extension");
99+
}
100+
101101
LOG.debug("Sending OCSP request");
102102
final OCSPResp response = Objects.requireNonNull(ocspClient.request(ocspService.getAccessLocation(), request));
103103
if (response.getStatus() != OCSPResponseStatus.SUCCESSFUL) {

src/main/java/eu/webeid/security/validator/ocsp/Digester.java renamed to src/main/java/eu/webeid/security/validator/ocsp/DigestCalculatorImpl.java

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,9 @@
2020
* SOFTWARE.
2121
*/
2222

23-
/*
24-
* Copyright 2017 The Netty Project
25-
* Copyright 2020 The Web eID project
26-
*
27-
* The Netty Project and The Web eID Project license this file to you under the
28-
* Apache License, version 2.0 (the "License"); you may not use this file except
29-
* in compliance with the License. You may obtain a copy of the License at:
30-
*
31-
* http://www.apache.org/licenses/LICENSE-2.0
32-
*
33-
* Unless required by applicable law or agreed to in writing, software
34-
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35-
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
36-
* License for the specific language governing permissions and limitations under
37-
* the License.
38-
*/
39-
4023
package eu.webeid.security.validator.ocsp;
4124

42-
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
25+
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
4326
import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
4427
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
4528
import org.bouncycastle.crypto.Digest;
@@ -52,33 +35,26 @@
5235

5336
/**
5437
* BouncyCastle's OCSPReqBuilder needs a DigestCalculator but BC doesn't
55-
* provide any public implementations of that interface. That's why we need to
56-
* write our own. There's a default SHA-1 implementation and one for SHA-256.
57-
* Which one to use will depend on the Certificate Authority (CA).
38+
* provide any public implementations of it, hence this implementation.
5839
*/
59-
public final class Digester implements DigestCalculator {
40+
public final class DigestCalculatorImpl implements DigestCalculator {
41+
42+
private static final AlgorithmIdentifier SHA1 = new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1);
43+
private static final AlgorithmIdentifier SHA256 = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256);
6044

6145
private final DigestOutputStream dos;
6246
private final AlgorithmIdentifier algId;
6347

64-
public static DigestCalculator sha1() {
65-
final Digest digest = new SHA1Digest();
66-
final AlgorithmIdentifier algId = new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1);
6748

68-
return new Digester(digest, algId);
49+
public static DigestCalculator sha1() {
50+
return new DigestCalculatorImpl(new SHA1Digest(), SHA1);
6951
}
7052

7153
public static DigestCalculator sha256() {
72-
Digest digest = new SHA256Digest();
73-
74-
// The OID for SHA-256: http://www.oid-info.com/get/2.16.840.1.101.3.4.2.1
75-
final ASN1ObjectIdentifier oid = new ASN1ObjectIdentifier("2.16.840.1.101.3.4.2.1").intern();
76-
final AlgorithmIdentifier algId = new AlgorithmIdentifier(oid);
77-
78-
return new Digester(digest, algId);
54+
return new DigestCalculatorImpl(new SHA256Digest(), SHA256);
7955
}
8056

81-
private Digester(Digest digest, AlgorithmIdentifier algId) {
57+
private DigestCalculatorImpl(Digest digest, AlgorithmIdentifier algId) {
8258
this.dos = new DigestOutputStream(digest);
8359
this.algId = algId;
8460
}

src/main/java/eu/webeid/security/validator/ocsp/OcspRequestBuilder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@
3636
import java.util.Objects;
3737

3838
/**
39-
* This is a simplified version of Bouncy Castle's {@link OCSPReqBuilder}.
40-
*
41-
* @see OCSPReqBuilder
39+
* This is a wrapper around Bouncy Castle's {@link OCSPReqBuilder} that
40+
* adds the OCSP nonce extension to the request if needed.
4241
*/
4342
public final class OcspRequestBuilder {
4443

45-
private static final SecureRandom GENERATOR = new SecureRandom();
44+
private static final SecureRandom RANDOM_GENERATOR = new SecureRandom();
4645

4746
private boolean ocspNonceEnabled = true;
4847
private CertificateID certificateId;
@@ -69,7 +68,7 @@ public OCSPReq build() throws OCSPException {
6968
try {
7069
addNonce(builder);
7170
} catch (IOException e) {
72-
throw new OCSPException("Failed to generate OCSP NONCE extension", e);
71+
throw new OCSPException("Failed to generate OCSP nonce extension", e);
7372
}
7473
}
7574

@@ -78,10 +77,11 @@ public OCSPReq build() throws OCSPException {
7877

7978
private void addNonce(OCSPReqBuilder builder) throws IOException {
8079
final byte[] nonce = new byte[32];
81-
GENERATOR.nextBytes(nonce);
80+
RANDOM_GENERATOR.nextBytes(nonce);
8281

8382
final Extension[] extensions = new Extension[]{
8483
new Extension(OCSPObjectIdentifiers.id_pkix_ocsp_nonce, false,
84+
// Follow OpenSSL OCSP nonce encoding convention and add double octet string header.
8585
new DEROctetString(new DEROctetString(nonce)))
8686
};
8787
builder.setRequestExtensions(new Extensions(extensions));

0 commit comments

Comments
 (0)