sign

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 22 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sign provides signing primitives used by PDF signature workflows.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoCertificates    = errors.New("sign: no certificates provided")
	ErrNilSignFunction   = errors.New("sign: signFn must not be nil")
	ErrUnsupportedKey    = errors.New("sign: unsupported key type")
	ErrPKCS12Unsupported = errors.New(
		"sign: PKCS12 loading requires golang.org/x/crypto/pkcs12; use NewLocalSigner with pre-parsed key and certificate",
	)
)

Errors reported when a signer cannot be built from the given key material.

Functions

func AddDSS

func AddDSS(pdfBytes []byte, dss *DSS) ([]byte, error)

AddDSS appends a DSS dictionary to pdfBytes using an incremental update.

func AddDocumentTimestamp

func AddDocumentTimestamp(pdfBytes []byte, tsaClient *TSAClient, hashFunc crypto.Hash) ([]byte, error)

AddDocumentTimestamp appends a PAdES document timestamp signature.

func BuildDetachedCMS

func BuildDetachedCMS(digest []byte, signer Signer, signingTime time.Time, tsaToken []byte) ([]byte, error)

BuildDetachedCMS constructs a DER-encoded CMS SignedData detached signature.

func CollectValidationData

func CollectValidationData(chain []*x509.Certificate, ocspClient *OCSPClient) ([][]byte, error)

CollectValidationData fetches OCSP responses for chain when ocspClient is set.

func OIDSignedData

func OIDSignedData() asn1.ObjectIdentifier

OIDSignedData returns the CMS signedData object identifier.

func PDF

func PDF(pdfBytes []byte, opts Options) ([]byte, error)

PDF applies a detached CMS/PAdES signature to an existing PDF using an incremental update.

Types

type Algorithm

type Algorithm int

Algorithm identifies a hash and signature algorithm pair.

const (
	SHA256WithRSA Algorithm = iota
	SHA384WithRSA
	SHA512WithRSA
	SHA256WithECDSA
	SHA384WithECDSA
	SHA512WithECDSA
)

The hash and signature algorithm pairs available for signing.

func (Algorithm) DigestOID

func (a Algorithm) DigestOID() asn1.ObjectIdentifier

DigestOID returns the digest algorithm OID.

func (Algorithm) HashFunc

func (a Algorithm) HashFunc() crypto.Hash

HashFunc returns the hash used by the algorithm.

func (Algorithm) SignatureOID

func (a Algorithm) SignatureOID() asn1.ObjectIdentifier

SignatureOID returns the signature algorithm OID.

type DSS

type DSS struct {
	Certs [][]byte
	OCSPs [][]byte
	CRLs  [][]byte
	VRI   map[string]*VRIEntry
}

DSS holds Document Security Store validation data for PAdES B-LT/B-LTA.

func NewDSS

func NewDSS() *DSS

NewDSS creates an empty Document Security Store.

func (*DSS) AddSignatureValidation

func (d *DSS) AddSignatureValidation(sigContents []byte, chain []*x509.Certificate, ocspResponses, crls [][]byte)

AddSignatureValidation adds certificate and revocation data for sigContents.

type ExternalSigner

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

ExternalSigner delegates signing to a caller-supplied function.

func NewExternalSigner

func NewExternalSigner(
	signFn func(digest []byte) ([]byte, error),
	certs []*x509.Certificate,
	algo Algorithm,
) (*ExternalSigner, error)

NewExternalSigner creates a signer backed by an external signing function.

func (*ExternalSigner) Algorithm

func (s *ExternalSigner) Algorithm() Algorithm

Algorithm returns the signature algorithm.

func (*ExternalSigner) CertificateChain

func (s *ExternalSigner) CertificateChain() []*x509.Certificate

CertificateChain returns a copy of the certificate chain.

func (*ExternalSigner) Sign

func (s *ExternalSigner) Sign(digest []byte) ([]byte, error)

Sign delegates digest signing to the configured function.

type LocalSigner

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

LocalSigner signs with a local private key.

func NewLocalSigner

func NewLocalSigner(key crypto.Signer, certs []*x509.Certificate) (*LocalSigner, error)

NewLocalSigner creates a signer from a local RSA or ECDSA private key.

func ParsePKCS12

func ParsePKCS12(_ []byte, _ string) (*LocalSigner, error)

ParsePKCS12 is reserved for future PKCS#12 support.

func (*LocalSigner) Algorithm

func (s *LocalSigner) Algorithm() Algorithm

Algorithm returns the signature algorithm.

func (*LocalSigner) CertificateChain

func (s *LocalSigner) CertificateChain() []*x509.Certificate

CertificateChain returns a copy of the certificate chain.

func (*LocalSigner) SetAlgorithm

func (s *LocalSigner) SetAlgorithm(algo Algorithm)

SetAlgorithm overrides the auto-detected algorithm.

func (*LocalSigner) Sign

func (s *LocalSigner) Sign(digest []byte) ([]byte, error)

Sign signs digest with the configured local key.

type OCSPClient

type OCSPClient struct {
	HTTPClient *http.Client

	// Timeout bounds one responder round trip. Zero uses a 30s default: the
	// responder URL comes from the certificate being validated, so it is not
	// necessarily a cooperative endpoint.
	Timeout time.Duration
}

OCSPClient fetches OCSP responses for certificate revocation checking.

func NewOCSPClient

func NewOCSPClient() *OCSPClient

NewOCSPClient creates an OCSP client.

func (*OCSPClient) FetchChainResponses

func (c *OCSPClient) FetchChainResponses(chain []*x509.Certificate) ([][]byte, error)

FetchChainResponses fetches OCSP responses for non-root certificates in chain.

func (*OCSPClient) FetchResponse

func (c *OCSPClient) FetchResponse(cert, issuer *x509.Certificate) ([]byte, error)

FetchResponse fetches a DER-encoded OCSP response for cert from its first responder URL. issuer is required to build the request.

type Options

type Options struct {
	Signer      Signer
	Level       PAdESLevel
	Name        string
	Reason      string
	Location    string
	ContactInfo string
	SigningTime time.Time
	TSAClient   *TSAClient
	OCSPClient  *OCSPClient
	CRLs        [][]byte
	ExtraCerts  [][]byte
}

Options configures PDF.

type PAdESLevel

type PAdESLevel int

PAdESLevel specifies the requested PAdES conformance level.

const (
	// LevelBB is PAdES B-B: a basic CMS detached signature.
	LevelBB PAdESLevel = iota
	// LevelBT is PAdES B-T: B-B plus a timestamp token from an RFC 3161 TSA.
	LevelBT
	// LevelBLT is reserved for PAdES B-LT validation data.
	LevelBLT
	// LevelBLTA is reserved for PAdES B-LTA archival timestamps.
	LevelBLTA
)

type Signer

type Signer interface {
	Sign(digest []byte) ([]byte, error)
	Algorithm() Algorithm
	CertificateChain() []*x509.Certificate
}

Signer performs a cryptographic signing operation.

type TSAClient

type TSAClient struct {
	URL        string
	HTTPClient *http.Client

	// Timeout bounds one timestamp round trip. Zero uses a 30s default so a
	// stalled authority cannot hang signing forever.
	Timeout time.Duration
}

TSAClient is an RFC 3161 Time-Stamp Authority client.

func NewTSAClient

func NewTSAClient(url string) *TSAClient

NewTSAClient creates a TSA client for url.

func (*TSAClient) Timestamp

func (c *TSAClient) Timestamp(digest []byte, hashFunc crypto.Hash) ([]byte, error)

Timestamp sends an RFC 3161 timestamp request and returns the timestamp token.

type VRIEntry

type VRIEntry struct {
	Certs [][]byte
	OCSPs [][]byte
	CRLs  [][]byte
}

VRIEntry holds validation data associated with one signature.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL