Class: Sigstore::Internal::X509::Extension

Inherits:
Object
  • Object
show all
Defined in:
lib/sigstore/internal/x509.rb

Defined Under Namespace

Classes: BasicConstraints, ExtendedKeyUsage, FulcioIssuer, KeyUsage, PrecertificateSignedCertificateTimestamps, SubjectAlternativeName, SubjectKeyIdentifier

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extension) ⇒ Extension

Returns a new instance of Extension.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/sigstore/internal/x509.rb', line 229

def initialize(extension)
  @extension = extension
  value = shift_value([OpenSSL::ASN1.decode(extension.to_der)], OpenSSL::ASN1::Sequence)
  @oid = value.shift

  unless @extension.is_a?(OpenSSL::X509::Extension) && @oid.oid == self.class.oid.oid
    raise ArgumentError,
          "Invalid extension: #{@extension.inspect} is not a #{@oid.inspect} " \
          "(#{self.class} / #{self.class.oid.inspect})"
  end

  @critical = false
  @critical = value.shift.value if value.first.is_a?(OpenSSL::ASN1::Boolean)
  raise ArgumentError, "Mis-parsed the critical bit" unless @critical == @extension.critical?

  contents = shift_value(value, OpenSSL::ASN1::OctetString)
  raise ArgumentError, "Invalid extension: extra fields left in #{self}: #{value}" unless value.empty?

  parse_value(OpenSSL::ASN1.decode(contents))
rescue OpenSSL::ASN1::ASN1Error => e
  raise ArgumentError, "Invalid extension: #{e.message} for #{self.class.oid}\n#{extension.inspect}"
end

Class Attribute Details

.oidObject

Returns the value of attribute oid.



226
227
228
# File 'lib/sigstore/internal/x509.rb', line 226

def oid
  @oid
end

.schemaObject

Returns the value of attribute schema.



226
227
228
# File 'lib/sigstore/internal/x509.rb', line 226

def schema
  @schema
end

Instance Method Details

#critical?Boolean

Returns:

  • (Boolean)


252
253
254
# File 'lib/sigstore/internal/x509.rb', line 252

def critical?
  @extension.critical?
end

#shift_bitstring(value) ⇒ Object

Raises:

  • (ArgumentError)


263
264
265
266
267
268
269
270
# File 'lib/sigstore/internal/x509.rb', line 263

def shift_bitstring(value)
  raise ArgumentError, "Invalid bit string: #{value.inspect}" unless value.is_a?(OpenSSL::ASN1::BitString)

  value.value.each_byte.flat_map do |byte|
    [byte & 0b1000_0000 != 0, byte & 0b0100_0000 != 0, byte & 0b0010_0000 != 0, byte & 0b0001_0000 != 0,
     byte & 0b0000_1000 != 0, byte & 0b0000_0100 != 0, byte & 0b0000_0010 != 0, byte & 0b0000_0001 != 0]
  end[..-value.unused_bits.succ]
end

#shift_value(value, klass) ⇒ Object

Raises:

  • (ArgumentError)


256
257
258
259
260
261
# File 'lib/sigstore/internal/x509.rb', line 256

def shift_value(value, klass)
  v = value.shift
  raise ArgumentError, "Invalid extension: #{v} is not a #{klass}" unless v.is_a?(klass)

  v.value
end