52
52
import org .jruby .RubyObject ;
53
53
import org .jruby .RubyString ;
54
54
import org .jruby .anno .JRubyMethod ;
55
+ import org .jruby .ext .openssl .impl .ASN1Registry ;
55
56
import org .jruby .runtime .ObjectAllocator ;
56
57
import org .jruby .runtime .ThreadContext ;
57
58
import org .jruby .runtime .Visibility ;
@@ -101,39 +102,35 @@ public IRubyObject initialize(final ThreadContext context, IRubyObject subject,
101
102
originalIssuer = (X509Cert ) issuer ;
102
103
BigInteger serial = subjectCert .getSerial ();
103
104
104
- return initializeImpl (context , serial , originalIssuer , digest );
105
+ return initializeImpl (context . runtime , serial , originalIssuer , digest );
105
106
}
106
107
107
108
@ JRubyMethod (name = "initialize" , visibility = Visibility .PRIVATE )
108
109
public IRubyObject initialize (final ThreadContext context , IRubyObject subject , IRubyObject issuer ) {
109
- Ruby runtime = context .getRuntime () ;
110
+ final Ruby runtime = context .runtime ;
110
111
111
112
X509Cert subjectCert = (X509Cert ) subject ;
112
113
originalIssuer = (X509Cert ) issuer ;
113
114
BigInteger serial = subjectCert .getSerial ();
114
115
115
- Digest digestInstance = new Digest (runtime , _Digest (runtime ));
116
- IRubyObject digest = digestInstance . initialize ( context , new IRubyObject [] { RubyString .newString (runtime , "SHA1" ) } );
116
+ Digest digest = new Digest (runtime , _Digest (runtime ));
117
+ digest . initializeImpl ( runtime , RubyString .newString (runtime , "SHA1" ), runtime . getNil () );
117
118
118
- return initializeImpl (context , serial , originalIssuer , digest );
119
+ return initializeImpl (runtime , serial , originalIssuer , digest );
119
120
}
120
121
121
122
@ JRubyMethod (name = "initialize" , visibility = Visibility .PRIVATE )
122
123
public IRubyObject initialize (final ThreadContext context , IRubyObject der ) {
123
- Ruby runtime = context .getRuntime ();
124
-
125
124
RubyString derStr = StringHelper .readPossibleDERInput (context , der );
126
- try {
125
+ try {
127
126
return initializeImpl (derStr .getBytes ());
128
127
}
129
- catch (IOException e ) {
130
- throw newOCSPError (runtime , e );
128
+ catch (Exception e ) {
129
+ throw newOCSPError (context . runtime , e );
131
130
}
132
131
}
133
132
134
- private IRubyObject initializeImpl (final ThreadContext context , BigInteger serial ,
135
- IRubyObject issuerCert , IRubyObject digest ) {
136
- Ruby runtime = context .getRuntime ();
133
+ private IRubyObject initializeImpl (final Ruby runtime , BigInteger serial , X509Cert issuerCert , IRubyObject digest ) {
137
134
138
135
Digest rubyDigest = (Digest ) digest ;
139
136
ASN1ObjectIdentifier oid = ASN1 .sym2Oid (runtime , rubyDigest .getName ().toLowerCase ());
@@ -147,10 +144,8 @@ private IRubyObject initializeImpl(final ThreadContext context, BigInteger seria
147
144
throw newOCSPError (runtime , e );
148
145
}
149
146
150
- X509Cert rubyCert = (X509Cert ) issuerCert ;
151
-
152
147
try {
153
- this .bcCertId = new CertificateID (calc , new X509CertificateHolder (rubyCert .getAuxCert ().getEncoded ()), serial ).toASN1Primitive ();
148
+ this .bcCertId = new CertificateID (calc , new X509CertificateHolder (issuerCert .getAuxCert ().getEncoded ()), serial ).toASN1Primitive ();
154
149
}
155
150
catch (Exception e ) {
156
151
throw newOCSPError (runtime , e );
@@ -159,7 +154,7 @@ private IRubyObject initializeImpl(final ThreadContext context, BigInteger seria
159
154
return this ;
160
155
}
161
156
162
- private IRubyObject initializeImpl (byte [] derByteStream ) throws IOException {
157
+ private IRubyObject initializeImpl (byte [] derByteStream ) {
163
158
this .bcCertId = CertID .getInstance (derByteStream );
164
159
165
160
return this ;
@@ -171,8 +166,8 @@ public IRubyObject serial() {
171
166
}
172
167
173
168
@ JRubyMethod (name = "issuer_name_hash" )
174
- public IRubyObject issuer_name_hash () {
175
- Ruby runtime = getRuntime () ;
169
+ public IRubyObject issuer_name_hash (ThreadContext context ) {
170
+ Ruby runtime = context . runtime ;
176
171
String oidSym = ASN1 .oid2Sym (runtime , getBCCertificateID ().getHashAlgOID ());
177
172
RubyString digestName = RubyString .newString (runtime , oidSym );
178
173
@@ -183,17 +178,14 @@ public IRubyObject issuer_name_hash() {
183
178
// a hash of a hash if we don't have the original issuer around.
184
179
if (originalIssuer == null ) {
185
180
try {
186
- return Digest .hexdigest (runtime . getCurrentContext () , this , digestName ,
181
+ return Digest .hexdigest (context , this , digestName ,
187
182
RubyString .newString (runtime , bcCertId .getIssuerNameHash ().getEncoded ("DER" )));
188
183
}
189
184
catch (IOException e ) {
190
185
throw newOCSPError (runtime , e );
191
186
}
192
187
}
193
- else {
194
- return Digest .hexdigest (runtime .getCurrentContext (), this , digestName ,
195
- originalIssuer .getSubject ().to_der (runtime .getCurrentContext ()));
196
- }
188
+ return Digest .hexdigest (context , this , digestName , originalIssuer .getSubject ().to_der (context ));
197
189
}
198
190
199
191
// For whatever reason, the MRI Ruby tests appear to suggest that they compute the hexdigest hash
@@ -202,34 +194,30 @@ public IRubyObject issuer_name_hash() {
202
194
// is already computed and can't be reversed to get to the original key, so we just compute
203
195
// a hash of a hash if we don't have the original issuer around.
204
196
@ JRubyMethod (name = "issuer_key_hash" )
205
- public IRubyObject issuer_key_hash () {
206
- Ruby runtime = getRuntime () ;
197
+ public IRubyObject issuer_key_hash (ThreadContext context ) {
198
+ Ruby runtime = context . runtime ;
207
199
String oidSym = ASN1 .oid2Sym (runtime , getBCCertificateID ().getHashAlgOID ());
208
200
RubyString digestName = RubyString .newString (runtime , oidSym );
209
201
210
- if ( originalIssuer == null ) {
211
- try {
212
- return Digest .hexdigest (runtime . getCurrentContext () , this , RubyString . newString ( runtime , oidSym ) ,
202
+ try {
203
+ if ( originalIssuer == null ) {
204
+ return Digest .hexdigest (context , this , digestName ,
213
205
RubyString .newString (runtime , bcCertId .getIssuerKeyHash ().getEncoded ("DER" )));
214
206
}
215
- catch ( IOException e ) {
216
- throw newOCSPError ( runtime , e );
217
- }
207
+ PKey key = ( PKey ) originalIssuer . public_key ( context );
208
+ byte [] key_der = key . toASN1PublicInfo (). toASN1Primitive (). getEncoded ( ASN1Encoding . DER );
209
+ return Digest . hexdigest ( context , this , digestName , RubyString . newStringNoCopy ( runtime , key_der ));
218
210
}
219
- else {
220
- PKey key = (PKey )originalIssuer .public_key (runtime .getCurrentContext ());
221
- return Digest .hexdigest (runtime .getCurrentContext (), this , digestName , key .to_der ());
211
+ catch (IOException e ) {
212
+ throw newOCSPError (runtime , e );
222
213
}
223
214
}
224
215
225
216
@ JRubyMethod (name = "hash_algorithm" )
226
217
public IRubyObject hash_algorithm () {
227
218
Ruby runtime = getRuntime ();
228
219
ASN1ObjectIdentifier oid = bcCertId .getHashAlgorithm ().getAlgorithm ();
229
- Integer nid = ASN1 .oid2nid (runtime , oid );
230
- String ln = ASN1 .nid2ln (runtime , nid );
231
-
232
- return RubyString .newString (runtime , ln );
220
+ return RubyString .newString (runtime , ASN1 .o2a (runtime , oid ));
233
221
}
234
222
235
223
@ JRubyMethod (name = "cmp" )
0 commit comments