4646import org .jruby .ext .openssl .log .Logger ;
4747
4848import org .bouncycastle .asn1 .ASN1Encodable ;
49+ import org .bouncycastle .asn1 .ASN1EncodableVector ;
4950import org .bouncycastle .asn1 .ASN1Encoding ;
5051import org .bouncycastle .asn1 .ASN1ObjectIdentifier ;
52+ import org .bouncycastle .asn1 .ASN1OctetString ;
53+ import org .bouncycastle .asn1 .ASN1Primitive ;
5154import org .bouncycastle .asn1 .ASN1Sequence ;
55+ import org .bouncycastle .asn1 .DEROctetString ;
5256import org .bouncycastle .asn1 .DERSequence ;
5357import org .bouncycastle .asn1 .ocsp .OCSPObjectIdentifiers ;
58+ import org .bouncycastle .asn1 .ocsp .Request ;
5459import org .bouncycastle .asn1 .ocsp .Signature ;
5560import org .bouncycastle .asn1 .ocsp .TBSRequest ;
5661import org .bouncycastle .asn1 .x500 .X500Name ;
8287import org .jruby .runtime .Visibility ;
8388import org .jruby .runtime .builtin .IRubyObject ;
8489
90+ import static org .jruby .ext .openssl .OCSP .generateNonce ;
8591import static org .jruby .ext .openssl .OpenSSL .throwOperationErrorOrOriginal ;
8692import static org .jruby .ext .openssl .OCSP ._OCSP ;
8793import 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