Skip to content

Commit 95e64c7

Browse files
committed
[fix] OCSP::Request nonce and certid handling (#396)
- avoid NPE in `OCSP::Request#add_nonce` when request has no extensions - retain existing request list and name when adding nonces or cert IDs - populate cert IDs and lazily extract nonces on requests from DER - Include extensions when signing `OCSP::Request` - fix NPE in `OCSP::BasicResponse#copy_nonce` when request lacks nonce
1 parent 9b09a7f commit 95e64c7

4 files changed

Lines changed: 237 additions & 73 deletions

File tree

‎src/main/java/org/jruby/ext/openssl/OCSPBasicResponse.java‎

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ public static void createBasicResponse(final Ruby runtime, final RubyModule OCSP
122122
BasicResponse.defineAnnotatedMethods(OCSPBasicResponse.class);
123123
}
124124

125-
private byte[] nonce;
126125
private List<OCSPSingleResponse> singleResponses = new ArrayList<>();
127126
private BasicOCSPResponse asn1BCBasicOCSPResp;
128127
private List<Extension> extensions = new ArrayList<>();
@@ -155,29 +154,27 @@ public IRubyObject initialize_copy(IRubyObject obj) {
155154
if ( this == obj ) return this;
156155

157156
checkFrozen();
158-
this.asn1BCBasicOCSPResp = ((OCSPBasicResponse)obj).getASN1BCOCSPResp();
157+
OCSPBasicResponse other = (OCSPBasicResponse) obj;
158+
this.asn1BCBasicOCSPResp = other.getASN1BCOCSPResp();
159+
this.extensions = new ArrayList<>(other.extensions);
160+
this.singleResponses = new ArrayList<>(other.singleResponses);
159161
return this;
160162
}
161163

162164
@JRubyMethod(name = "add_nonce", rest = true)
163165
public OCSPBasicResponse add_nonce(ThreadContext context, IRubyObject... args) {
164-
byte[] tmpNonce;
165-
if ( Arity.checkArgumentCount(context.runtime, args, 0, 1) == 0 ) {
166-
try {
167-
tmpNonce = generateNonce(context);
168-
}
169-
catch (NoSuchAlgorithmException e) {
170-
throw newOCSPError(context.runtime, e);
171-
}
166+
final boolean random = Arity.checkArgumentCount(context.runtime, args, 0, 1) == 0 || args[0].isNil();
167+
final Extension nonceExt;
168+
try {
169+
byte[] nonce = random ? generateNonce(context) : args[0].convertToString().getBytes();
170+
nonceExt = OCSPRequest.createNonceExtension(nonce);
172171
}
173-
else {
174-
RubyString input = (RubyString) args[0];
175-
tmpNonce = input.getBytes();
172+
catch (NoSuchAlgorithmException|IOException e) {
173+
throw newOCSPError(context.runtime, e);
176174
}
177175

178-
extensions.add(new Extension(OCSPObjectIdentifiers.id_pkix_ocsp_nonce, false, tmpNonce));
179-
nonce = tmpNonce;
180-
176+
extensions.removeIf(ext -> OCSPObjectIdentifiers.id_pkix_ocsp_nonce.equals(ext.getExtnId()));
177+
extensions.add(nonceExt);
181178
return this;
182179
}
183180

@@ -232,7 +229,14 @@ public OCSPBasicResponse add_status(final ThreadContext context, IRubyObject[] a
232229

233230
@JRubyMethod(name = "copy_nonce")
234231
public IRubyObject copy_nonce(final ThreadContext context, IRubyObject request) {
235-
add_nonce(context, RubyString.newString(context.runtime, ((OCSPRequest) request).getNonce()));
232+
if (!(request instanceof OCSPRequest)) {
233+
throw context.runtime.newTypeError(request, (RubyClass) _OCSP(context.runtime).getConstantAt("Request"));
234+
}
235+
byte[] reqNonce = ((OCSPRequest) request).getNonce();
236+
if (reqNonce == null) {
237+
return RubyFixnum.two(context.runtime);
238+
}
239+
add_nonce(context, RubyString.newString(context.runtime, reqNonce));
236240
return RubyFixnum.one(context.runtime);
237241
}
238242

@@ -593,7 +597,11 @@ public BasicOCSPResponse getASN1BCOCSPResp() {
593597
}
594598

595599
public byte[] getNonce() {
596-
return this.nonce;
600+
for (Extension ext : extensions) {
601+
if (OCSPObjectIdentifiers.id_pkix_ocsp_nonce.equals(ext.getExtnId())) return OCSPRequest.extractNonce(ext);
602+
}
603+
if (asn1BCBasicOCSPResp == null) return null;
604+
return OCSPRequest.extractNonce(asn1BCBasicOCSPResp.getTbsResponseData().getResponseExtensions());
597605
}
598606

599607
private ASN1GeneralizedTime rubyIntOrTimeToGenTime(IRubyObject intOrTime) {

‎src/main/java/org/jruby/ext/openssl/OCSPCertificateId.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public OCSPCertificateId(Ruby runtime, RubyClass metaClass) {
8787
public OCSPCertificateId(Ruby runtime) {
8888
this(runtime, (RubyClass) _OCSP(runtime).getConstantAt("CertificateId"));
8989
}
90+
91+
OCSPCertificateId(Ruby runtime, CertID bcCertId) {
92+
this(runtime);
93+
this.bcCertId = bcCertId;
94+
}
9095

9196
@JRubyMethod(name = "initialize", visibility = Visibility.PRIVATE)
9297
public IRubyObject initialize(final ThreadContext context, IRubyObject subject, IRubyObject issuer, IRubyObject digest) {

‎src/main/java/org/jruby/ext/openssl/OCSPRequest.java‎

Lines changed: 102 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@
4646
import org.jruby.ext.openssl.log.Logger;
4747

4848
import org.bouncycastle.asn1.ASN1Encodable;
49+
import org.bouncycastle.asn1.ASN1EncodableVector;
4950
import org.bouncycastle.asn1.ASN1Encoding;
5051
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
52+
import org.bouncycastle.asn1.ASN1OctetString;
53+
import org.bouncycastle.asn1.ASN1Primitive;
5154
import org.bouncycastle.asn1.ASN1Sequence;
55+
import org.bouncycastle.asn1.DEROctetString;
5256
import org.bouncycastle.asn1.DERSequence;
5357
import org.bouncycastle.asn1.ocsp.OCSPObjectIdentifiers;
58+
import org.bouncycastle.asn1.ocsp.Request;
5459
import org.bouncycastle.asn1.ocsp.Signature;
5560
import org.bouncycastle.asn1.ocsp.TBSRequest;
5661
import org.bouncycastle.asn1.x500.X500Name;
@@ -82,6 +87,7 @@
8287
import org.jruby.runtime.Visibility;
8388
import org.jruby.runtime.builtin.IRubyObject;
8489

90+
import static org.jruby.ext.openssl.OCSP.generateNonce;
8591
import static org.jruby.ext.openssl.OpenSSL.throwOperationErrorOrOriginal;
8692
import static org.jruby.ext.openssl.OCSP._OCSP;
8793
import static org.jruby.ext.openssl.OCSP.newOCSPError;
@@ -114,8 +120,7 @@ public static void createRequest(final Ruby runtime, final RubyModule _OCSP) {
114120
private final static String OCSP_TRUSTOTHER = "TRUSTOTHER";
115121
private final static String OCSP_NOCHAIN = "NOCHAIN";
116122
private org.bouncycastle.asn1.ocsp.OCSPRequest asn1bcReq;
117-
private List<OCSPCertificateId> certificateIds = new ArrayList<OCSPCertificateId>();
118-
private byte[] nonce;
123+
private List<OCSPCertificateId> certificateIds = new ArrayList<>();
119124

120125
@JRubyMethod(name = "initialize", rest = true, visibility = Visibility.PRIVATE)
121126
public IRubyObject initialize(final ThreadContext context, IRubyObject[] args) {
@@ -125,77 +130,95 @@ public IRubyObject initialize(final ThreadContext context, IRubyObject[] args) {
125130

126131
RubyString derString = StringHelper.readPossibleDERInput(context, args[0]);
127132
asn1bcReq = org.bouncycastle.asn1.ocsp.OCSPRequest.getInstance(derString.getBytes());
128-
133+
134+
// populate certificate IDs:
135+
certificateIds.clear();
136+
ASN1Sequence reqList = asn1bcReq.getTbsRequest().getRequestList();
137+
for (int i = 0; i < reqList.size(); i++) {
138+
Request req = Request.getInstance(reqList.getObjectAt(i));
139+
certificateIds.add(new OCSPCertificateId(runtime, req.getReqCert()));
140+
}
141+
129142
return this;
130143
}
131144

132145
@JRubyMethod(name = "add_certid")
133146
public IRubyObject add_certid(IRubyObject certId) {
134-
Ruby runtime = getRuntime();
135147
OCSPCertificateId rubyCertId = (OCSPCertificateId) certId;
136148
certificateIds.add(rubyCertId);
137-
138-
OCSPReqBuilder builder = new OCSPReqBuilder();
139-
for (OCSPCertificateId certificateId : certificateIds) {
140-
builder.addRequest(new CertificateID(certificateId.getCertID()));
141-
}
142-
143-
try {
144-
asn1bcReq = org.bouncycastle.asn1.ocsp.OCSPRequest.getInstance(builder.build().getEncoded());
145-
}
146-
catch (Exception e) {
147-
throw newOCSPError(runtime, e);
148-
}
149-
150-
if (nonce != null) {
151-
addNonceImpl();
149+
150+
GeneralName requestorName = null;
151+
Extensions requestExts = null; // optional
152+
Signature signature = null; // optional
153+
ASN1EncodableVector requests = new ASN1EncodableVector();
154+
155+
if (asn1bcReq != null) {
156+
TBSRequest currentTbsReq = asn1bcReq.getTbsRequest();
157+
requestorName = currentTbsReq.getRequestorName();
158+
requestExts = currentTbsReq.getRequestExtensions();
159+
ASN1Sequence reqList = currentTbsReq.getRequestList();
160+
for (int i = 0; i < reqList.size(); i++) {
161+
requests.add(reqList.getObjectAt(i));
162+
}
163+
signature = asn1bcReq.getOptionalSignature();
152164
}
165+
166+
requests.add(new Request(rubyCertId.getCertID(), null));
167+
TBSRequest newTbsReq = new TBSRequest(requestorName, new DERSequence(requests), requestExts);
168+
asn1bcReq = new org.bouncycastle.asn1.ocsp.OCSPRequest(newTbsReq, signature);
169+
153170
return this;
154171
}
155172

156173
@JRubyMethod(name = "add_nonce", rest = true)
157174
public IRubyObject add_nonce(ThreadContext context, IRubyObject[] args) {
158-
if ( Arity.checkArgumentCount(context.runtime, args, 0, 1) == 0 ) {
159-
try {
160-
nonce = OCSP.generateNonce(context);
161-
}
162-
catch (NoSuchAlgorithmException e) {
163-
throw newOCSPError(context.runtime, e);
164-
}
175+
final boolean random = Arity.checkArgumentCount(context.runtime, args, 0, 1) == 0 || args[0].isNil();
176+
final Extension nonceExt;
177+
try {
178+
byte[] nonce = random ? OCSP.generateNonce(context) : args[0].convertToString().getBytes();
179+
nonceExt = createNonceExtension(nonce);
165180
}
166-
else {
167-
RubyString input = (RubyString) args[0];
168-
nonce = input.getBytes();
181+
catch (NoSuchAlgorithmException|IOException e) {
182+
throw newOCSPError(context.runtime, e);
169183
}
170-
171-
addNonceImpl();
184+
addNonceImpl(nonceExt);
172185
return this;
173186
}
174-
175-
// BC doesn't have support for nonces... gotta do things manually
176-
private void addNonceImpl() {
187+
188+
private void addNonceImpl(final Extension nonceExt) {
177189
GeneralName requestorName = null;
178190
ASN1Sequence requestList = new DERSequence();
179-
Extensions extensions;
180-
Signature sig = null;
181-
List<Extension> tmpExtensions = new ArrayList<Extension>();
191+
Signature signature = null;
192+
List<Extension> tmpExtensions = new ArrayList<>();
182193

183194
if (asn1bcReq != null) {
184195
TBSRequest currentTbsReq = asn1bcReq.getTbsRequest();
185-
extensions = currentTbsReq.getRequestExtensions();
186-
sig = asn1bcReq.getOptionalSignature();
187-
Enumeration<ASN1ObjectIdentifier> oids = extensions.oids();
188-
while (oids.hasMoreElements()) {
189-
tmpExtensions.add(extensions.getExtension(oids.nextElement()));
196+
requestorName = currentTbsReq.getRequestorName();
197+
requestList = currentTbsReq.getRequestList();
198+
Extensions extensions = currentTbsReq.getRequestExtensions(); // optional
199+
if (extensions != null) {
200+
Enumeration<ASN1ObjectIdentifier> oids = extensions.oids();
201+
while (oids.hasMoreElements()) {
202+
ASN1ObjectIdentifier oid = oids.nextElement();
203+
if (!OCSPObjectIdentifiers.id_pkix_ocsp_nonce.equals(oid)) {
204+
tmpExtensions.add(extensions.getExtension(oid));
205+
}
206+
}
190207
}
208+
signature = asn1bcReq.getOptionalSignature();
191209
}
192-
193-
tmpExtensions.add(new Extension(OCSPObjectIdentifiers.id_pkix_ocsp_nonce, false, nonce));
194-
Extension[] exts = new Extension[tmpExtensions.size()];
195-
Extensions newExtensions = new Extensions(tmpExtensions.toArray(exts));
210+
211+
tmpExtensions.add(nonceExt);
212+
213+
Extensions newExtensions = new Extensions(tmpExtensions.toArray(new Extension[tmpExtensions.size()]));
196214
TBSRequest newTbsReq = new TBSRequest(requestorName, requestList, newExtensions);
197215

198-
asn1bcReq = new org.bouncycastle.asn1.ocsp.OCSPRequest(newTbsReq, sig);
216+
asn1bcReq = new org.bouncycastle.asn1.ocsp.OCSPRequest(newTbsReq, signature);
217+
}
218+
219+
static Extension createNonceExtension(final byte[] nonce) throws IOException {
220+
final byte[] nonceOctet = new DEROctetString(nonce).getEncoded();
221+
return new Extension(OCSPObjectIdentifiers.id_pkix_ocsp_nonce, false, nonceOctet);
199222
}
200223

201224
@JRubyMethod(name = "certid")
@@ -212,16 +235,17 @@ public RubyBoolean signed_p() {
212235
@JRubyMethod(name = "check_nonce")
213236
public IRubyObject check_nonce(ThreadContext context, IRubyObject response) {
214237
final Ruby runtime = context.runtime;
238+
final byte[] reqNonce = getNonce();
215239
if (response instanceof OCSPBasicResponse) {
216240
OCSPBasicResponse rubyBasicRes = (OCSPBasicResponse) response;
217-
return checkNonceImpl(runtime, this.nonce, rubyBasicRes.getNonce());
241+
return checkNonceImpl(runtime, reqNonce, rubyBasicRes.getNonce());
218242
}
219243
else if (response instanceof OCSPResponse) {
220244
OCSPResponse rubyResp = (OCSPResponse) response;
221-
return checkNonceImpl(runtime, this.nonce, ((OCSPBasicResponse)rubyResp.basic(context)).getNonce());
245+
return checkNonceImpl(runtime, reqNonce, ((OCSPBasicResponse)rubyResp.basic(context)).getNonce());
222246
}
223247
else {
224-
return checkNonceImpl(runtime, this.nonce, null);
248+
return checkNonceImpl(runtime, reqNonce, null);
225249
}
226250
}
227251

@@ -274,6 +298,12 @@ public IRubyObject sign(final ThreadContext context, IRubyObject[] args) {
274298
for (OCSPCertificateId certId : certificateIds) {
275299
builder.addRequest(new CertificateID(certId.getCertID()));
276300
}
301+
if (asn1bcReq != null) {
302+
Extensions exts = asn1bcReq.getTbsRequest().getRequestExtensions();
303+
if (exts != null) {
304+
builder.setRequestExtensions(exts);
305+
}
306+
}
277307

278308
List<X509CertificateHolder> certChain = new ArrayList<>();
279309
if (flag != RubyFixnum.fix2int(nocerts)) {
@@ -304,8 +334,6 @@ public IRubyObject sign(final ThreadContext context, IRubyObject[] args) {
304334
// handlePotentialOperationError(ex, e -> newSecurityError(runtime, e));
305335
//}
306336

307-
if (nonce != null) addNonceImpl();
308-
309337
return this;
310338
}
311339
catch (Throwable ex) {
@@ -410,7 +438,9 @@ public IRubyObject initialize_copy(IRubyObject obj) {
410438
if ( this == obj ) return this;
411439

412440
checkFrozen();
413-
this.asn1bcReq = ((OCSPRequest)obj).asn1bcReq;
441+
OCSPRequest other = (OCSPRequest) obj;
442+
this.asn1bcReq = other.asn1bcReq;
443+
this.certificateIds = new ArrayList<>(other.certificateIds);
414444
return this;
415445
}
416446

@@ -437,7 +467,25 @@ private java.security.cert.Certificate findCertByName(ASN1Encodable genX500Name,
437467
}
438468

439469
public byte[] getNonce() {
440-
return this.nonce;
470+
if (asn1bcReq == null) return null;
471+
return extractNonce(asn1bcReq.getTbsRequest().getRequestExtensions());
472+
}
473+
474+
static byte[] extractNonce(final Extensions exts) {
475+
return exts == null ? null : extractNonce(exts.getExtension(OCSPObjectIdentifiers.id_pkix_ocsp_nonce));
476+
}
477+
478+
static byte[] extractNonce(final Extension ext) {
479+
if (ext == null) return null;
480+
481+
final byte[] value = ext.getExtnValue().getOctets();
482+
try {
483+
ASN1Primitive primitive = ASN1Primitive.fromByteArray(value);
484+
if (primitive instanceof ASN1OctetString) {
485+
return ((ASN1OctetString) primitive).getOctets();
486+
}
487+
} catch (IOException ignored) {}
488+
return value;
441489
}
442490

443491
private IRubyObject checkNonceImpl(Ruby runtime, byte[] reqNonce, byte[] respNonce) {

0 commit comments

Comments
 (0)