government / src / ratify / ratify.go

@ efc0e8ad4d88be48a5cdedd9b62e88420b8b6e5e | history


package ratify

import (
	"crypto/x509"
	"time"
)

type Options struct {
	// At is the time to check validity at.
	At time.Time
	// Pin, if set, is the only key the program's anchor may have. Without
	// one, the anchor inside the file is trusted, so ratification checks
	// that the program is internally consistent, not who wrote it.
	Pin Pin
	// CRLs are repeal lists to apply, as each file's bytes.
	CRLs [][]byte
}

// Ratify loads a .gov file and runs every ratification check that comes
// before policy processing. It returns the certs in depth order.
func Ratify(data []byte, opts Options) ([]*x509.Certificate, error) {
	certs, err := Load(data)
	if err != nil {
		return nil, err
	}
	certs, err = Order(certs)
	if err != nil {
		return nil, err
	}
	if opts.Pin != nil {
		if err := CheckPin(certs[0], opts.Pin); err != nil {
			return nil, err
		}
	}
	if err := CheckChain(certs); err != nil {
		return nil, err
	}
	if err := CheckValidity(certs, opts.At); err != nil {
		return nil, err
	}
	var crls []*x509.RevocationList
	for _, data := range opts.CRLs {
		more, err := LoadCRLs(data)
		if err != nil {
			return nil, err
		}
		crls = append(crls, more...)
	}
	if err := CheckRepeals(certs, crls, opts.At); err != nil {
		return nil, err
	}
	return certs, nil
}