Skip to content

Commit 656d992

Browse files
committed
Extract tbs der by directly modifying ASN1
1 parent fd30b16 commit 656d992

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

lib/sigstore/verifier.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,29 @@ def pack_digitally_signed(sct, certificate, issuer_key_id = nil)
196196
end
197197

198198
def tbs_certificate_der(certificate)
199-
tbs_cert = certificate.dup
200199
oid = OpenSSL::X509::Extension.new("1.3.6.1.4.1.11129.2.4.2", "").oid
201-
tbs_cert.extensions = tbs_cert.extensions.reject do |ext|
200+
certificate.extensions.find do |ext|
202201
ext.oid == oid
203-
end
204-
# ensure the underlying certificate is marked as modified
205-
tbs_cert.serial = tbs_cert.serial + 1
206-
tbs_cert.serial = tbs_cert.serial - 1
202+
end || raise("No PrecertificateSignedCertificateTimestamps (#{oid.inspect}) found for the certificate")
203+
204+
# This uglyness is needed because there is no way to force modifying an X509 certificate
205+
# in a way that it will be serialized with the modifications.
206+
seq = OpenSSL::ASN1.decode(certificate.to_der).value[0]
207+
seq.value = seq.value.map do |v|
208+
next v unless v.tag == 3
209+
210+
v.value = v.value.map do |v2|
211+
v2.value = v2.value.map do |v3|
212+
next if v3.first.oid == "1.3.6.1.4.1.11129.2.4.2"
207213

208-
raise "no #{oid} extension found" unless certificate.extensions.size == tbs_cert.extensions.size + 1
214+
v3
215+
end.compact!
216+
v2
217+
end
218+
v
219+
end
209220

210-
OpenSSL::ASN1.decode(tbs_cert.to_der).value[0].to_der.b
221+
seq.to_der
211222
end
212223

213224
# https://letsencrypt.org/2018/04/04/sct-encoding.html

0 commit comments

Comments
 (0)