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-
4023package eu .webeid .security .validator .ocsp ;
4124
4225import org .bouncycastle .asn1 .DEROctetString ;
4831import org .bouncycastle .cert .ocsp .OCSPReq ;
4932import org .bouncycastle .cert .ocsp .OCSPReqBuilder ;
5033
34+ import java .io .IOException ;
5135import java .security .SecureRandom ;
5236import 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 */
5942public 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