ctfe

package
Version: v0.0.0-...-d52c0f3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2018 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Overview

Package ctfe contains a usage example by providing an implementation of an RFC6962 compatible CT log server using a Trillian log server as backend storage via its GRPC API.

IMPORTANT: Only code rooted within this part of the tree should refer to the CT Github repository. Other parts of the system must not assume that the data they're processing is X.509 or CT related.

The CT repository can be found at: https://github.com/google/certificate-transparency

Index

Constants

View Source
const (
	AddChainName          = EntrypointName("AddChain")
	AddPreChainName       = EntrypointName("AddPreChain")
	GetSTHName            = EntrypointName("GetSTH")
	GetSTHConsistencyName = EntrypointName("GetSTHConsistency")
	GetProofByHashName    = EntrypointName("GetProofByHash")
	GetEntriesName        = EntrypointName("GetEntries")
	GetRootsName          = EntrypointName("GetRoots")
	GetEntryAndProofName  = EntrypointName("GetEntryAndProof")
)

Constants for entrypoint names, as exposed in statistics/logging.

View Source
const CertificateQuotaUserPrefix = "@intermediate"

CertificateQuotaUserPrefix is prepended to all User quota ids association with intermediate certificates.

Variables

Entrypoints is a list of entrypoint names as exposed in statistics/logging.

View Source
var (
	// MaxGetEntriesAllowed is the number of entries we allow in a get-entries request
	MaxGetEntriesAllowed int64 = 1000
)

Functions

func GetCTLogID

func GetCTLogID(pk crypto.PublicKey) ([sha256.Size]byte, error)

GetCTLogID takes the key manager for a log and returns the LogID. (see RFC 6962 S3.2) In CT V1 the log id is a hash of the public key.

func IsPrecertificate

func IsPrecertificate(cert *x509.Certificate) (bool, error)

IsPrecertificate tests if a certificate is a pre-certificate as defined in CT. An error is returned if the CT extension is present but is not ASN.1 NULL as defined by the spec.

func LogConfigFromFile

func LogConfigFromFile(filename string) ([]*configpb.LogConfig, error)

LogConfigFromFile creates a slice of LogConfig options from the given filename, which should contain text-protobuf encoded configuration data.

func MultiLogConfigFromFile

func MultiLogConfigFromFile(filename string) (*configpb.LogMultiConfig, error)

MultiLogConfigFromFile creates a LogMultiConfig proto from the given filename, which should contain text-protobuf encoded configuration data. Does not do full validation of the config but checks that it is non empty.

func PingTreeHead

func PingTreeHead(ctx context.Context, client trillian.TrillianLogClient, logID int64, prefix string) error

PingTreeHead retrieves a tree head for the given log, and updates the STH timestamp metrics correspondingly. TODO(pavelkalinnikov): Should we cache the resulting STH?

func QuotaUserForCert

func QuotaUserForCert(c *x509.Certificate) string

QuotaUserForCert returns a User quota id string for the passed in certificate. This is intended to be used for quota limiting by intermediate certificates, but the function does not enforce anything about the passed in cert.

Format returned is:

"CertificateQuotaUserPrefix Subject hex(SHA256(SubjectPublicKeyInfo)[0:5])"

See tests for examples.

func ToMultiLogConfig

func ToMultiLogConfig(cfg []*configpb.LogConfig, beSpec string) *configpb.LogMultiConfig

ToMultiLogConfig creates a multi backend config proto from the data loaded from a single-backend configuration file. All the log configs reference a default backend spec as provided.

func ValidateChain

func ValidateChain(rawChain [][]byte, validationOpts CertValidationOpts) ([]*x509.Certificate, error)

ValidateChain takes the certificate chain as it was parsed from a JSON request. Ensures all elements in the chain decode as X.509 certificates. Ensures that there is a valid path from the end entity certificate in the chain to a trusted root cert, possibly using the intermediates supplied in the chain. Then applies the RFC requirement that the path must involve all the submitted chain in the order of submission.

func ValidateLogMultiConfig

func ValidateLogMultiConfig(cfg *configpb.LogMultiConfig) (map[string]*configpb.LogBackend, error)

ValidateLogMultiConfig checks that a config is valid for use with multiple backend log servers. The rules applied are:

1. The backend set must define a set of log backends with distinct (non empty) names and non empty backend specs. 2. The backend specs must all be distinct. 3. The log configs must all specify a log backend and each must be one of those defined in the backend set. 4. If NotBeforeStart or NotBeforeLimit are set for a log then these fields must be valid timestamp protos. If both are set then NotBeforeLimit must not be before NotBeforeStart. 5. The prefixes of configured logs must all be distinct and must not be empty. 6. The set of tree ids for each configured backend must be distinct.

Types

type AppHandler

type AppHandler struct {
	Info    *logInfo
	Handler func(context.Context, *logInfo, http.ResponseWriter, *http.Request) (int, error)
	Name    EntrypointName
	Method  string // http.MethodGet or http.MethodPost
}

AppHandler holds a logInfo and a handler function that uses it, and is an implementation of the http.Handler interface.

func (AppHandler) ServeHTTP

func (a AppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP for an AppHandler invokes the underlying handler function but does additional common error and stats processing.

type CertValidationOpts

type CertValidationOpts struct {
	// contains filtered or unexported fields
}

CertValidationOpts contains various parameters for certificate chain validation

type DefaultMirrorSTHFactory

type DefaultMirrorSTHFactory struct{}

DefaultMirrorSTHFactory creates DefaultMirrorSTHStorage instances.

func (DefaultMirrorSTHFactory) NewStorage

NewStorage creates a dummy STH storage.

type DefaultMirrorSTHStorage

type DefaultMirrorSTHStorage struct{}

DefaultMirrorSTHStorage is a dummy STH storage that always returns an error.

func (DefaultMirrorSTHStorage) GetMirrorSTH

func (st DefaultMirrorSTHStorage) GetMirrorSTH(ctx context.Context, maxTreeSize int64) (*ct.SignedTreeHead, error)

GetMirrorSTH returns an error.

type DefaultRequestLog

type DefaultRequestLog struct {
}

DefaultRequestLog is an implementation of RequestLog that does nothing except log the calls at a high level of verbosity.

func (*DefaultRequestLog) AddCertToChain

func (dlr *DefaultRequestLog) AddCertToChain(_ context.Context, cert *x509.Certificate)

AddCertToChain logs some issuer / subject / timing fields from a certificate that is part of a submitted chain.

func (*DefaultRequestLog) AddDERToChain

func (dlr *DefaultRequestLog) AddDERToChain(_ context.Context, d []byte)

AddDERToChain logs the raw bytes of a submitted certificate.

func (*DefaultRequestLog) FirstAndSecond

func (dlr *DefaultRequestLog) FirstAndSecond(_ context.Context, f, s int64)

FirstAndSecond logs request parameters.

func (*DefaultRequestLog) IssueSCT

func (dlr *DefaultRequestLog) IssueSCT(_ context.Context, sct []byte)

IssueSCT logs an SCT that will be issued to a client.

func (*DefaultRequestLog) LeafHash

func (dlr *DefaultRequestLog) LeafHash(_ context.Context, lh []byte)

LeafHash logs request parameters.

func (*DefaultRequestLog) LeafIndex

func (dlr *DefaultRequestLog) LeafIndex(_ context.Context, li int64)

LeafIndex logs request parameters.

func (*DefaultRequestLog) LogPrefix

func (dlr *DefaultRequestLog) LogPrefix(_ context.Context, p string)

LogPrefix logs the prefix of the CT log that this request is for.

func (*DefaultRequestLog) Start

Start logs the start of request processing.

func (*DefaultRequestLog) StartAndEnd

func (dlr *DefaultRequestLog) StartAndEnd(_ context.Context, s, e int64)

StartAndEnd logs request parameters.

func (*DefaultRequestLog) Status

func (dlr *DefaultRequestLog) Status(_ context.Context, s int)

Status logs the response HTTP status code after processing completes.

func (*DefaultRequestLog) TreeSize

func (dlr *DefaultRequestLog) TreeSize(_ context.Context, ts int64)

TreeSize logs request parameters.

type EntrypointName

type EntrypointName string

EntrypointName identifies a CT entrypoint as defined in section 4 of RFC 6962.

type InstanceOptions

type InstanceOptions struct {
	// Config holds the original configuration options for the log.
	Config *configpb.LogConfig
	// Client is a corresponding Trillian log client.
	Client trillian.TrillianLogClient
	// Deadline is a timeout for Trillian RPC requests.
	Deadline time.Duration
	// MetricFactory allows creating metrics.
	MetricFactory monitoring.MetricFactory
	// ErrorMapper converts an error from an RPC request to an HTTP status, plus
	// a boolean to indicate whether the conversion succeeded.
	ErrorMapper func(error) (int, bool)
	// RequestLog provides structured logging of CTFE requests.
	RequestLog RequestLog
	// RemoteUser returns a string representing the originating host for the
	// given request. This string will be used as a User quota key.
	// If unset, no quota will be requested for remote users.
	RemoteQuotaUser func(*http.Request) string
	// CertificateQuotaUser returns a string represeing the passed in
	// intermediate certificate. This string will be user as a User quota key for
	// the cert. Quota will be requested for each intermediate in an
	// add-[pre]-chain request so as to allow individual issers to be rate
	// limited. If unset, no quota will be requested for intermediate
	// certificates.
	CertificateQuotaUser func(*x509.Certificate) string
}

InstanceOptions describes the options for a log instance.

type LogSTHGetter

type LogSTHGetter struct {
	// contains filtered or unexported fields
}

LogSTHGetter is an STHGetter implementation for regular (non-mirror) logs, i.e. logs that have their own key and actively sign STHs.

func (*LogSTHGetter) GetSTH

func (sg *LogSTHGetter) GetSTH(ctx context.Context) (*ct.SignedTreeHead, error)

GetSTH retrieves and builds a tree head structure for the given log.

type MirrorSTHGetter

type MirrorSTHGetter struct {
	// contains filtered or unexported fields
}

MirrorSTHGetter is an STHGetter implementation for mirror logs. It assumes no knowledge of the key, and returns STHs obtained from an external source represented by the MirrorSTHStorage interface.

func (*MirrorSTHGetter) GetSTH

func (sg *MirrorSTHGetter) GetSTH(ctx context.Context) (*ct.SignedTreeHead, error)

GetSTH returns a known source log's STH with as large TreeSize and/or timestamp as possible, but such that TreeSize <= Trillian log size. This is to ensure that the mirror doesn't expose a "future" state of the log before it is properly stored in Trillian.

type MirrorSTHStorage

type MirrorSTHStorage interface {
	// GetMirrorSTH returns an STH of TreeSize <= maxTreeSize. It does best
	// effort to maximize the returned STH's TreeSize and/or Timestamp.
	GetMirrorSTH(ctx context.Context, maxTreeSize int64) (*ct.SignedTreeHead, error)
}

MirrorSTHStorage provides STHs of a source log to be served from a mirror.

type PEMCertPool

type PEMCertPool struct {
	// contains filtered or unexported fields
}

PEMCertPool is a wrapper / extension to x509.CertPool. It allows us to access the raw certs, which we need to serve get-roots request and has stricter handling on loading certs into the pool. CertPool ignores errors if at least one cert loads correctly but PEMCertPool requires all certs to load.

func NewPEMCertPool

func NewPEMCertPool() *PEMCertPool

NewPEMCertPool creates a new, empty, instance of PEMCertPool.

func (*PEMCertPool) AddCert

func (p *PEMCertPool) AddCert(cert *x509.Certificate)

AddCert adds a certificate to a pool. Uses fingerprint to weed out duplicates. cert must not be nil.

func (*PEMCertPool) AppendCertsFromPEM

func (p *PEMCertPool) AppendCertsFromPEM(pemCerts []byte) (ok bool)

AppendCertsFromPEM adds certs to the pool from a byte slice assumed to contain PEM encoded data. Skips over non certificate blocks in the data. Returns true if all certificates in the data were parsed and added to the pool successfully and at least one certificate was found.

func (*PEMCertPool) AppendCertsFromPEMFile

func (p *PEMCertPool) AppendCertsFromPEMFile(pemFile string) error

AppendCertsFromPEMFile adds certs from a file that contains concatenated PEM data.

func (*PEMCertPool) CertPool

func (p *PEMCertPool) CertPool() *x509.CertPool

CertPool returns the underlying CertPool.

func (*PEMCertPool) RawCertificates

func (p *PEMCertPool) RawCertificates() []*x509.Certificate

RawCertificates returns a list of the raw bytes of certificates that are in this pool

func (*PEMCertPool) Subjects

func (p *PEMCertPool) Subjects() (res [][]byte)

Subjects returns a list of the DER-encoded subjects of all of the certificates in the pool.

type PathHandlers

type PathHandlers map[string]AppHandler

PathHandlers maps from a path to the relevant AppHandler instance.

func SetUpInstance

func SetUpInstance(ctx context.Context, opts InstanceOptions) (*PathHandlers, error)

SetUpInstance sets up a log instance using the provided configuration, and returns a set of handlers for this log.

func SetUpMirrorInstance

func SetUpMirrorInstance(ctx context.Context, opts InstanceOptions, stor MirrorSTHStorage) (*PathHandlers, error)

SetUpMirrorInstance sets up a log mirror instance using the provided configuration, and returns a set of handlers for it.

type RequestLog

type RequestLog interface {
	// Start will be called once at the beginning of handling each request.
	// The supplied context will be the one used for request processing and
	// can be used by the logger to set values on the returned context.
	// The returned context should be used in all the following calls to
	// this API. This is normally arranged by the request handler code.
	Start(context.Context) context.Context
	// LogPrefix will be called once per request to set the log prefix.
	LogPrefix(context.Context, string)
	// AddDERToChain will be called once for each certificate in a submitted
	// chain. It's called early in request processing so the supplied bytes
	// have not been checked for validity. Calls will be in order of the
	// certificates as presented in the request with the root last.
	AddDERToChain(context.Context, []byte)
	// AddCertToChain will be called once for each certificate in the chain
	// after it has been parsed and verified. Calls will be in order of the
	// certificates as presented in the request with the root last.
	AddCertToChain(context.Context, *x509.Certificate)
	// FirstAndSecond will be called once for a consistency proof request with
	// the first and second tree sizes involved (if they parse correctly).
	FirstAndSecond(context.Context, int64, int64)
	// StartAndEnd will be called once for a get entries request with the
	// endpoints of the range requested (if they parse correctly).
	StartAndEnd(context.Context, int64, int64)
	// LeafIndex will be called once with the index of a leaf being requested
	// by get entry and proof (if the request params parse correctly).
	LeafIndex(context.Context, int64)
	// TreeSize will be called once with the requested tree size for get entry
	// and proof requests (if the request params parse correctly).
	TreeSize(context.Context, int64)
	// LeafHash will be called once for get proof by hash requests with the
	// requested hash value (if the parameters parse correctly).
	LeafHash(context.Context, []byte)
	// IssueSCT will be called once when the server is about to issue an SCT to a
	// client. This should not be called if the submission process fails before an
	// SCT could be presented to a client, even if this is unrelated to
	// the validity of the submitted chain. The SCT bytes will be in TLS
	// serialized format.
	IssueSCT(context.Context, []byte)
	// Status will be called once to set the HTTP status code that was the
	// the result after the request has been handled.
	Status(context.Context, int)
}

RequestLog allows implementations to do structured logging of CTFE request parameters, submitted chains and other internal details that are useful for log operators when debugging issues. CTFE handlers will call the appropriate methods during request processing. The implementation is responsible for collating and storing the resulting logging information.

type STHGetter

type STHGetter interface {
	// GetSTH returns the latest STH for the log, as required by the RFC-6962
	// get-sth endpoint: https://tools.ietf.org/html/rfc6962#section-4.3.
	GetSTH(ctx context.Context) (*ct.SignedTreeHead, error)
}

STHGetter provides latest STHs for a log.

type SignatureCache

type SignatureCache struct {
	// contains filtered or unexported fields
}

SignatureCache is a one-entry cache that stores the last generated signature for a given bytes input. It helps to reduce the number of signing operations, and the number of distinct signatures produced for the same input (some signing methods are non-deterministic).

func (*SignatureCache) GetSignature

func (sc *SignatureCache) GetSignature(input []byte) (ct.DigitallySigned, bool)

GetSignature returns the latest signature for the given bytes input. If the input is not in the cache, it returns (_, false).

func (*SignatureCache) SetSignature

func (sc *SignatureCache) SetSignature(input []byte, sig ct.DigitallySigned)

SetSignature associates the signature with the given bytes input.

Directories

Path Synopsis
Package configpb holds a config protobuf for the CT personality.
Package configpb holds a config protobuf for the CT personality.
The ct_server binary runs the CT personality.
The ct_server binary runs the CT personality.
Package testonly contains code and data that should only be used by tests.
Package testonly contains code and data that should only be used by tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL