Skip to content

Commit 12336a4

Browse files
committed
Backport OCSP nonce encoding and length fix from main
WE2-1132 Signed-off-by: Mart Somermaa <[email protected]>
1 parent 0a0d4ac commit 12336a4

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

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

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

23-
/*
24-
* Copyright 2017 The Netty Project
25-
* Copyright (c) 2020-2025 Estonian Information System Authority
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

4225
import org.bouncycastle.asn1.DEROctetString;
@@ -48,17 +31,17 @@
4831
import org.bouncycastle.cert.ocsp.OCSPReq;
4932
import org.bouncycastle.cert.ocsp.OCSPReqBuilder;
5033

34+
import java.io.IOException;
5135
import java.security.SecureRandom;
5236
import java.util.Objects;
5337

5438
/**
55-
* This is a simplified version of Bouncy Castle's {@link OCSPReqBuilder}.
56-
*
57-
* @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.
5841
*/
5942
public final class OcspRequestBuilder {
6043

61-
private static final SecureRandom GENERATOR = new SecureRandom();
44+
private static final SecureRandom RANDOM_GENERATOR = new SecureRandom();
6245

6346
private boolean ocspNonceEnabled = true;
6447
private CertificateID certificateId;
@@ -82,19 +65,24 @@ public OCSPReq build() throws OCSPException {
8265
builder.addRequest(Objects.requireNonNull(certificateId, "certificateId"));
8366

8467
if (ocspNonceEnabled) {
85-
addNonce(builder);
68+
try {
69+
addNonce(builder);
70+
} catch (IOException e) {
71+
throw new OCSPException("Failed to generate OCSP nonce extension", e);
72+
}
8673
}
8774

8875
return builder.build();
8976
}
9077

91-
private void addNonce(OCSPReqBuilder builder) {
92-
final byte[] nonce = new byte[8];
93-
GENERATOR.nextBytes(nonce);
78+
private void addNonce(OCSPReqBuilder builder) throws IOException {
79+
final byte[] nonce = new byte[32];
80+
RANDOM_GENERATOR.nextBytes(nonce);
9481

9582
final Extension[] extensions = new Extension[]{
9683
new Extension(OCSPObjectIdentifiers.id_pkix_ocsp_nonce, false,
97-
new DEROctetString(nonce))
84+
// Follow OpenSSL OCSP nonce encoding convention and add double octet string header.
85+
new DEROctetString(new DEROctetString(nonce)))
9886
};
9987
builder.setRequestExtensions(new Extensions(extensions));
10088
}

0 commit comments

Comments
 (0)