class TrustStore

Represents transport-neutral trusted certificate sources.

Definitions

def self.load(path, **options)

Load a PEM-encoded certificate bundle from the given path.

Signature

parameter path String | Interface(:to_path)

The path to the certificate bundle.

parameter options Hash

Options forwarded to .parse.

returns TrustStore

The loaded trust store.

Implementation

def self.load(path, **options)
	return parse(File.read(path), **options)
end

def self.parse(certificate_bundle, system_certificates: false)

Parse a PEM-encoded certificate bundle into a trust store.

Signature

parameter certificate_bundle String

One or more trusted certificates encoded as PEM.

parameter system_certificates Boolean

Whether system-provided trusted certificates should be included.

returns TrustStore

The parsed trust store.

raises ArgumentError

If the bundle does not contain any certificates.

raises TypeError

If the bundle is not a string.

Implementation

def self.parse(certificate_bundle, system_certificates: false)
	return self.new(certificates: Certificates.parse(certificate_bundle), system_certificates: system_certificates)
end

def initialize(certificates: [], system_certificates: false)

Initialize a trust store from PEM-encoded trusted certificates.

Signature

parameter certificates Array(String)

The trusted certificates encoded as PEM.

parameter system_certificates Boolean

Whether system-provided trusted certificates should be included.

raises ArgumentError

If no source of trusted certificates is specified.

raises TypeError

If certificates are not provided as strings.

Implementation

def initialize(certificates: [], system_certificates: false)
	unless certificates.is_a?(Array) && certificates.all?{|certificate| certificate.is_a?(String)}
		raise TypeError, "Certificates must be provided as an array of strings!"
	end
	
	unless certificates.any? || system_certificates
		raise ArgumentError, "At least one source of trusted certificates must be specified!"
	end
	
	@certificates = certificates
	@system_certificates = system_certificates
end

attr :certificates

Signature

attribute Array(String)

The individual trusted certificates encoded as PEM.

def system_certificates?

Whether system-provided trusted certificates should be included.

Signature

returns Boolean

true if system-provided trusted certificates should be included.

Implementation

def system_certificates?
	@system_certificates
end

def ==(other)

Compare trust stores by their ordered certificates and system certificate policy.

Signature

parameter other Object

The object to compare.

returns Boolean

Whether both trust stores have the same class and values.

Implementation

def ==(other)
	return other.instance_of?(self.class) &&
		@certificates.eql?(other.certificates) &&
		@system_certificates.eql?(other.system_certificates?)
end

def hash

Compute a hash from the trust store values. Freeze the trust store before using it as a hash key.

Signature

returns Integer

The hash of the trust store.

Implementation

def hash
	return [self.class, @certificates, @system_certificates].hash
end

def freeze

Freeze the trust store and independent copies of its certificates, without freezing caller-owned values.

Signature

returns TrustStore

This immutable trust store. Use dup.freeze to preserve the original trust store too.

Implementation

def freeze
	return self if frozen?
	
	@certificates = @certificates.map{|certificate| certificate.dup.freeze}.freeze
	
	super
end

def inspect

Get a representation of the trust store without exposing certificate material.

Signature

returns String

A redacted representation of the trust store.

Implementation

def inspect
	attributes = {
		certificates: @certificates.size,
		system_certificates: @system_certificates,
	}
	
	return "\#<#{self.class} #{attributes.inspect}>"
end