Skip to content

Commit e3530c5

Browse files
committed
also align cert-id (use same public-key info)
as before (the PKey#to_der fixes) + cleanup internals
1 parent 3b853eb commit e3530c5

File tree

1 file changed

+27
-39
lines changed

1 file changed

+27
-39
lines changed

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

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.jruby.RubyObject;
5353
import org.jruby.RubyString;
5454
import org.jruby.anno.JRubyMethod;
55+
import org.jruby.ext.openssl.impl.ASN1Registry;
5556
import org.jruby.runtime.ObjectAllocator;
5657
import org.jruby.runtime.ThreadContext;
5758
import org.jruby.runtime.Visibility;
@@ -101,39 +102,35 @@ public IRubyObject initialize(final ThreadContext context, IRubyObject subject,
101102
originalIssuer = (X509Cert) issuer;
102103
BigInteger serial = subjectCert.getSerial();
103104

104-
return initializeImpl(context, serial, originalIssuer, digest);
105+
return initializeImpl(context.runtime, serial, originalIssuer, digest);
105106
}
106107

107108
@JRubyMethod(name = "initialize", visibility = Visibility.PRIVATE)
108109
public IRubyObject initialize(final ThreadContext context, IRubyObject subject, IRubyObject issuer) {
109-
Ruby runtime = context.getRuntime();
110+
final Ruby runtime = context.runtime;
110111

111112
X509Cert subjectCert = (X509Cert) subject;
112113
originalIssuer = (X509Cert) issuer;
113114
BigInteger serial = subjectCert.getSerial();
114115

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());
117118

118-
return initializeImpl(context, serial, originalIssuer, digest);
119+
return initializeImpl(runtime, serial, originalIssuer, digest);
119120
}
120121

121122
@JRubyMethod(name = "initialize", visibility = Visibility.PRIVATE)
122123
public IRubyObject initialize(final ThreadContext context, IRubyObject der) {
123-
Ruby runtime = context.getRuntime();
124-
125124
RubyString derStr = StringHelper.readPossibleDERInput(context, der);
126-
try {
125+
try {
127126
return initializeImpl(derStr.getBytes());
128127
}
129-
catch (IOException e) {
130-
throw newOCSPError(runtime, e);
128+
catch (Exception e) {
129+
throw newOCSPError(context.runtime, e);
131130
}
132131
}
133132

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) {
137134

138135
Digest rubyDigest = (Digest) digest;
139136
ASN1ObjectIdentifier oid = ASN1.sym2Oid(runtime, rubyDigest.getName().toLowerCase());
@@ -147,10 +144,8 @@ private IRubyObject initializeImpl(final ThreadContext context, BigInteger seria
147144
throw newOCSPError(runtime, e);
148145
}
149146

150-
X509Cert rubyCert = (X509Cert) issuerCert;
151-
152147
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();
154149
}
155150
catch (Exception e) {
156151
throw newOCSPError(runtime, e);
@@ -159,7 +154,7 @@ private IRubyObject initializeImpl(final ThreadContext context, BigInteger seria
159154
return this;
160155
}
161156

162-
private IRubyObject initializeImpl(byte[] derByteStream) throws IOException {
157+
private IRubyObject initializeImpl(byte[] derByteStream) {
163158
this.bcCertId = CertID.getInstance(derByteStream);
164159

165160
return this;
@@ -171,8 +166,8 @@ public IRubyObject serial() {
171166
}
172167

173168
@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;
176171
String oidSym = ASN1.oid2Sym(runtime, getBCCertificateID().getHashAlgOID());
177172
RubyString digestName = RubyString.newString(runtime, oidSym);
178173

@@ -183,17 +178,14 @@ public IRubyObject issuer_name_hash() {
183178
// a hash of a hash if we don't have the original issuer around.
184179
if (originalIssuer == null) {
185180
try {
186-
return Digest.hexdigest(runtime.getCurrentContext(), this, digestName,
181+
return Digest.hexdigest(context, this, digestName,
187182
RubyString.newString(runtime, bcCertId.getIssuerNameHash().getEncoded("DER")));
188183
}
189184
catch (IOException e) {
190185
throw newOCSPError(runtime, e);
191186
}
192187
}
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));
197189
}
198190

199191
// 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() {
202194
// is already computed and can't be reversed to get to the original key, so we just compute
203195
// a hash of a hash if we don't have the original issuer around.
204196
@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;
207199
String oidSym = ASN1.oid2Sym(runtime, getBCCertificateID().getHashAlgOID());
208200
RubyString digestName = RubyString.newString(runtime, oidSym);
209201

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,
213205
RubyString.newString(runtime, bcCertId.getIssuerKeyHash().getEncoded("DER")));
214206
}
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));
218210
}
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);
222213
}
223214
}
224215

225216
@JRubyMethod(name = "hash_algorithm")
226217
public IRubyObject hash_algorithm() {
227218
Ruby runtime = getRuntime();
228219
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));
233221
}
234222

235223
@JRubyMethod(name = "cmp")

0 commit comments

Comments
 (0)