pkcs7

package
v0.29.8 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2025 License: MIT, MIT Imports: 23 Imported by: 2

README

pkcs7 implements parsing and creating signed and enveloped messages.

package main

import (
	"bytes"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"os"

    "github.com/emmansun/gmsm/pkcs7"
)

func SignAndDetach(content []byte, cert *x509.Certificate, privkey *rsa.PrivateKey) (signed []byte, err error) {
	toBeSigned, err := NewSignedData(content)
	if err != nil {
		err = fmt.Errorf("Cannot initialize signed data: %s", err)
		return
	}
	if err = toBeSigned.AddSigner(cert, privkey, SignerInfoConfig{}); err != nil {
		err = fmt.Errorf("Cannot add signer: %s", err)
		return
	}

	// Detach signature, omit if you want an embedded signature
	toBeSigned.Detach()

	signed, err = toBeSigned.Finish()
	if err != nil {
		err = fmt.Errorf("Cannot finish signing data: %s", err)
		return
	}

	// Verify the signature
	pem.Encode(os.Stdout, &pem.Block{Type: "PKCS7", Bytes: signed})
	p7, err := pkcs7.Parse(signed)
	if err != nil {
		err = fmt.Errorf("Cannot parse our signed data: %s", err)
		return
	}

	// since the signature was detached, reattach the content here
	p7.Content = content

	if bytes.Compare(content, p7.Content) != 0 {
		err = fmt.Errorf("Our content was not in the parsed data:\n\tExpected: %s\n\tActual: %s", content, p7.Content)
		return
	}
	if err = p7.Verify(); err != nil {
		err = fmt.Errorf("Cannot verify our signed data: %s", err)
		return
	}

	return signed, nil
}

Credits

This is a fork of mozilla-services/pkcs7

Documentation

Overview

Package pkcs7 implements parsing and generation of some PKCS#7 structures.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Signed Data OIDs
	OIDData                   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1}
	OIDSignedData             = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 2}
	OIDEnvelopedData          = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 3}
	OIDSignedEnvelopedData    = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 4}
	OIDDigestData             = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 5}
	OIDEncryptedData          = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 6}
	OIDAttributeContentType   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 3}
	OIDAttributeMessageDigest = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 4}
	OIDAttributeSigningTime   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 5}

	// Digest Algorithms
	OIDDigestAlgorithmSHA1   = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 26}
	OIDDigestAlgorithmSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1}
	OIDDigestAlgorithmSHA384 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 2}
	OIDDigestAlgorithmSHA512 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 3}

	OIDDigestAlgorithmDSA     = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 1}
	OIDDigestAlgorithmDSASHA1 = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 3}

	OIDDigestAlgorithmECDSASHA1   = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 1}
	OIDDigestAlgorithmECDSASHA256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2}
	OIDDigestAlgorithmECDSASHA384 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 3}
	OIDDigestAlgorithmECDSASHA512 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 4}

	// Signature Algorithms
	OIDEncryptionAlgorithmRSA       = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1}
	OIDEncryptionAlgorithmRSASHA1   = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 5}
	OIDEncryptionAlgorithmRSASHA256 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 11}
	OIDEncryptionAlgorithmRSASHA384 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 12}
	OIDEncryptionAlgorithmRSASHA512 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 13}

	OIDEncryptionAlgorithmECDSAP256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 7}
	OIDEncryptionAlgorithmECDSAP384 = asn1.ObjectIdentifier{1, 3, 132, 0, 34}
	OIDEncryptionAlgorithmECDSAP521 = asn1.ObjectIdentifier{1, 3, 132, 0, 35}
)
View Source
var (
	// SM2 Signed Data OIDs
	// 《GB/T 35275-2017 信息安全技术 SM2密码算法加密签名消息语法规范》
	SM2OIDData                = asn1.ObjectIdentifier{1, 2, 156, 10197, 6, 1, 4, 2, 1}
	SM2OIDSignedData          = asn1.ObjectIdentifier{1, 2, 156, 10197, 6, 1, 4, 2, 2}
	SM2OIDEnvelopedData       = asn1.ObjectIdentifier{1, 2, 156, 10197, 6, 1, 4, 2, 3}
	SM2OIDSignedEnvelopedData = asn1.ObjectIdentifier{1, 2, 156, 10197, 6, 1, 4, 2, 4}
	SM2OIDEncryptedData       = asn1.ObjectIdentifier{1, 2, 156, 10197, 6, 1, 4, 2, 5}

	// Digest Algorithms
	OIDDigestAlgorithmSM3 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 401}
	// SM2Sign-with-SM3
	OIDDigestAlgorithmSM2SM3 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 501}
	// Signature Algorithms SM2-1
	OIDDigestEncryptionAlgorithmSM2 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 301, 1}

	// Encryption Algorithms SM2-3
	OIDKeyEncryptionAlgorithmSM2 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 301, 3}

	//SM9 Signed Data OIDs
	SM9OIDData                = asn1.ObjectIdentifier{1, 2, 156, 10197, 6, 1, 4, 4, 1}
	SM9OIDSignedData          = asn1.ObjectIdentifier{1, 2, 156, 10197, 6, 1, 4, 4, 2}
	SM9OIDEnvelopedData       = asn1.ObjectIdentifier{1, 2, 156, 10197, 6, 1, 4, 4, 3}
	SM9OIDSignedEnvelopedData = asn1.ObjectIdentifier{1, 2, 156, 10197, 6, 1, 4, 4, 4}
	SM9OIDEncryptedData       = asn1.ObjectIdentifier{1, 2, 156, 10197, 6, 1, 4, 4, 5}

	// SM9Sign-with-SM3
	OIDDigestAlgorithmSM9SM3 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 502}

	// Signature Algorithms SM9-1
	OIDDigestEncryptionAlgorithmSM9 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 302, 1}

	// Encryption Algorithms SM9-3
	OIDKeyEncryptionAlgorithmSM9 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 302, 3}
)
View Source
var ErrNotEncryptedContent = errors.New("pkcs7: content data is NOT a decryptable data type")

ErrNotEncryptedContent is returned when attempting to Decrypt data that is not encrypted data

View Source
var ErrNotEnvelopedData = errors.New("pkcs7: content data is NOT an enveloped data type")

ErrNotEnvelopedData is returned when attempting to Decrypt data that is not enveloped data

View Source
var ErrPSKNotProvided = errors.New("pkcs7: cannot encrypt content: PSK not provided")

ErrPSKNotProvided is returned when attempting to encrypt using a PSK without actually providing the PSK.

View Source
var ErrUnsupportedAlgorithm = errors.New("pkcs7: cannot decrypt data: only RSA, SM2, DES, DES-EDE3, AES and SM4 supported")

ErrUnsupportedAlgorithm tells you when our quick dev assumptions have failed

View Source
var ErrUnsupportedContentType = errors.New("pkcs7: cannot parse data: unimplemented content type")

ErrUnsupportedContentType is returned when a PKCS7 content is not supported. Currently only Data (1.2.840.113549.1.7.1), Signed Data (1.2.840.113549.1.7.2), and Enveloped Data are supported (1.2.840.113549.1.7.3)

View Source
var ErrUnsupportedEncryptionAlgorithm = errors.New("pkcs7: cannot encrypt content: only DES-CBC, AES-CBC, AES-GCM, SM4-CBC and SM4-GCM supported")

ErrUnsupportedEncryptionAlgorithm is returned when attempting to encrypt content with an unsupported algorithm.

Functions

func DegenerateCertificate

func DegenerateCertificate(cert []byte) ([]byte, error)

DegenerateCertificate creates a signed data structure containing only the provided certificate or certificate chain.

func Encrypt

func Encrypt(cipher pkcs.Cipher, content []byte, recipients []*smx509.Certificate) ([]byte, error)

Encrypt creates and returns an envelope data PKCS7 structure with encrypted recipient keys for each recipient public key.

The algorithm used to perform encryption is determined by the argument cipher

TODO(fullsailor): Add support for encrypting content with other algorithms

func EncryptCFCA added in v0.27.3

func EncryptCFCA(cipher pkcs.Cipher, content []byte, recipients []*smx509.Certificate) ([]byte, error)

EncryptCFCA creates and returns an envelope data PKCS7 structure with encrypted recipient keys for each recipient public key. The OIDs use GM/T 0010 - 2012 set and the encrypted key use C1C2C3 format and without 0x4 prefix.

The algorithm used to perform encryption is determined by the argument cipher

func EncryptSM

func EncryptSM(cipher pkcs.Cipher, content []byte, recipients []*smx509.Certificate) ([]byte, error)

EncryptSM creates and returns an envelope data PKCS7 structure with encrypted recipient keys for each recipient public key. The OIDs use GM/T 0010 - 2012 set and the encrypted key use ASN.1 format.

The algorithm used to perform encryption is determined by the argument cipher

func EncryptSMUsingPSK

func EncryptSMUsingPSK(cipher pkcs.Cipher, content []byte, key []byte) ([]byte, error)

EncryptSMUsingPSK creates and returns an encrypted data PKCS7 structure, encrypted using caller provided pre-shared secret. This method uses China Standard OID

func EncryptUsingPSK

func EncryptUsingPSK(cipher pkcs.Cipher, content []byte, key []byte) ([]byte, error)

EncryptUsingPSK creates and returns an encrypted data PKCS7 structure, encrypted using caller provided pre-shared secret.

func EnvelopeMessageCFCA added in v0.29.3

func EnvelopeMessageCFCA(cipher pkcs.Cipher, content []byte, recipients []*smx509.Certificate) ([]byte, error)

EnvelopeMessageCFCA creates and returns an envelope data PKCS7 structure with encrypted recipient keys for each recipient public key. The OIDs use GM/T 0010 - 2012 set and the encrypted key uses ASN.1 format. This function uses recipient's SubjectKeyIdentifier to identify the recipient. This function is used for CFCA compatibility.

Types

type Attribute

type Attribute struct {
	Type  asn1.ObjectIdentifier
	Value any
}

Attribute represents a key value pair attribute. Value must be marshalable byte `encoding/asn1`

type DefaultSession added in v0.29.3

type DefaultSession struct{}

DefaultSession is the default implementation of Session without any special handling (stateless). Custom implementations can be provided to handle key reuse, cache, etc.

func (DefaultSession) DecryptDataKey added in v0.29.3

func (DefaultSession) DecryptDataKey(key []byte, priv crypto.PrivateKey, cert *smx509.Certificate, opts any) ([]byte, error)

func (DefaultSession) EncryptdDataKey added in v0.29.3

func (DefaultSession) EncryptdDataKey(key []byte, cert *smx509.Certificate, opts any) ([]byte, error)

func (DefaultSession) GenerateDataKey added in v0.29.3

func (DefaultSession) GenerateDataKey(size int) ([]byte, error)

type EnvelopedData added in v0.27.4

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

func NewEnvelopedData added in v0.27.4

func NewEnvelopedData(cipher pkcs.Cipher, content []byte) (*EnvelopedData, error)

NewEnvelopedData creates a new EnvelopedData structure with the provided cipher and content.

func NewEnvelopedDataWithSession added in v0.29.3

func NewEnvelopedDataWithSession(cipher pkcs.Cipher, content []byte, session Session) (*EnvelopedData, error)

NewEnvelopedDataWithSession creates a new EnvelopedData structure with the provided cipher, content and sessionKey.

func NewSM2EnvelopedData added in v0.27.4

func NewSM2EnvelopedData(cipher pkcs.Cipher, content []byte) (*EnvelopedData, error)

NewSM2EnvelopedData creates a new EnvelopedData structure with the provided cipher and content. The OIDs use GM/T 0010 - 2012 set.

func NewSM2EnvelopedDataWithSession added in v0.29.3

func NewSM2EnvelopedDataWithSession(cipher pkcs.Cipher, content []byte, session Session) (*EnvelopedData, error)

NewSM2EnvelopedDataWithSession creates a new EnvelopedData structure with the provided cipher, content and sessionKey. The OIDs use GM/T 0010 - 2012 set.

func (*EnvelopedData) AddRecipient added in v0.27.4

func (ed *EnvelopedData) AddRecipient(cert *smx509.Certificate, version int, encryptKeyFunc func(cert *smx509.Certificate, key []byte) ([]byte, error)) error

AddRecipient adds a recipient to the EnvelopedData structure. version 0: IssuerAndSerialNumber version 1: GB/T 35275-2017 version 2: SubjectKeyIdentifier

func (*EnvelopedData) Finish added in v0.27.4

func (ed *EnvelopedData) Finish() ([]byte, error)

Finish creates the final PKCS7 structure.

type InvalidSigningTimeError added in v0.29.7

type InvalidSigningTimeError struct {
	SigningTime time.Time
	NotBefore   time.Time // NotBefore of signer
	NotAfter    time.Time // NotAfter of signer
}

InvalidSigningTimeError is returned when the signing time attribute falls outside of the signer certificate validity.

func (*InvalidSigningTimeError) Error added in v0.29.7

func (e *InvalidSigningTimeError) Error() string

type IssuerAndSerial added in v0.29.0

type IssuerAndSerial struct {
	RawIssuer    []byte
	SerialNumber *big.Int
}

IssuerAndSerial is a structure that holds the issuer name and serial number

type MessageDigestMismatchError

type MessageDigestMismatchError struct {
	ExpectedDigest []byte
	ActualDigest   []byte
}

MessageDigestMismatchError is returned when the signer data digest does not match the computed digest for the contained content

func (*MessageDigestMismatchError) Error

func (err *MessageDigestMismatchError) Error() string

type PKCS7

type PKCS7 struct {
	Content      []byte
	Certificates []*smx509.Certificate
	CRLs         []pkix.CertificateList
	Signers      []signerInfo
	// contains filtered or unexported fields
}

PKCS7 Represents a PKCS7 structure

func Parse

func Parse(data []byte) (p7 *PKCS7, err error)

Parse decodes a DER encoded PKCS7 package and assign the default session to the PKCS7 object

func ParseWithSession added in v0.29.3

func ParseWithSession(session Session, data []byte) (p7 *PKCS7, err error)

ParseWithSession decodes a DER encoded PKCS7 package and assign the session to the PKCS7 object

func (*PKCS7) Decrypt

func (p7 *PKCS7) Decrypt(cert *smx509.Certificate, pkey crypto.PrivateKey) ([]byte, error)

Decrypt decrypts encrypted content info for recipient cert and private key

func (*PKCS7) DecryptAndVerify

func (p7 *PKCS7) DecryptAndVerify(cert *smx509.Certificate, pkey crypto.PrivateKey, verifyFunc VerifyFunc) ([]byte, error)

DecryptAndVerify decrypts encrypted content info for recipient cert and private key and verifies the signature.

func (*PKCS7) DecryptAndVerifyOnlyOne

func (p7 *PKCS7) DecryptAndVerifyOnlyOne(pkey crypto.PrivateKey, verifyFunc VerifyFunc) ([]byte, error)

DecryptAndVerifyOnlyOne decrypts encrypted content info for the only recipient private key and verifies the signature.

func (*PKCS7) DecryptCFCA added in v0.27.3

func (p7 *PKCS7) DecryptCFCA(cert *smx509.Certificate, pkey crypto.PrivateKey) ([]byte, error)

DecryptCFCA decrypts encrypted content info for recipient cert and private key whose SM2 encrypted key is C1C2C3 format and without 0x4 prefix.

func (*PKCS7) DecryptUsingPSK

func (p7 *PKCS7) DecryptUsingPSK(key []byte) ([]byte, error)

DecryptUsingPSK decrypts encrypted data using caller provided pre-shared secret

func (*PKCS7) GetOnlySigner

func (p7 *PKCS7) GetOnlySigner() *smx509.Certificate

GetOnlySigner returns an x509.Certificate for the first signer of the signed data payload. If there are more or less than one signer, nil is returned

func (*PKCS7) GetRecipients added in v0.29.0

func (p7 *PKCS7) GetRecipients() ([]RecipientInfo, error)

GetRecipients returns the list of recipients for the enveloped data

func (*PKCS7) UnmarshalSignedAttribute

func (p7 *PKCS7) UnmarshalSignedAttribute(attributeType asn1.ObjectIdentifier, out any) error

UnmarshalSignedAttribute decodes a single attribute from the signer info

func (*PKCS7) Verify

func (p7 *PKCS7) Verify() (err error)

Verify is a wrapper around VerifyWithChain() that initializes an empty trust store, effectively disabling certificate verification when validating a signature.

func (*PKCS7) VerifyAsDigest added in v0.29.7

func (p7 *PKCS7) VerifyAsDigest() (err error)

VerifyAsDigest verifies the PKCS7 signature, treats the content as a digest. It returns an error if the verification fails.

func (*PKCS7) VerifyAsDigestWithChain added in v0.29.7

func (p7 *PKCS7) VerifyAsDigestWithChain(truststore *smx509.CertPool) (err error)

VerifyAsDigestWithChain verifies the PKCS7 signature using the provided truststore and treats the content as a precomputed digest. It returns an error if the verification fails.

func (*PKCS7) VerifyWithChain

func (p7 *PKCS7) VerifyWithChain(truststore *smx509.CertPool) (err error)

VerifyWithChain checks the signatures of a PKCS7 object.

If truststore is not nil, it also verifies the chain of trust of the end-entity signer cert to one of the roots in the truststore. When the PKCS7 object includes the signing time authenticated attr verifies the chain at that time and UTC now otherwise.

func (*PKCS7) VerifyWithChainAtTime

func (p7 *PKCS7) VerifyWithChainAtTime(truststore *smx509.CertPool, currentTime *time.Time) (err error)

VerifyWithChainAtTime checks the signatures of a PKCS7 object.

If truststore is not nil, it also verifies the chain of trust of the end-entity signer cert to a root in the truststore at currentTime. It does not use the signing time authenticated attribute.

type RecipientInfo added in v0.29.3

type RecipientInfo struct {
	IssuerAndSerial
	SubjectKeyIdentifier []byte
}

RecipientInfo is a structure that holds the recipient information supports IssuerAndSerial and SubjectKeyIdentifier.

type Session added in v0.29.3

type Session interface {
	// GenerateDataKey returns the data key to be used for encryption
	GenerateDataKey(size int) ([]byte, error)

	// EncryptdDataKey encrypts the key with the provided certificate public key
	EncryptdDataKey(key []byte, cert *smx509.Certificate, opts any) ([]byte, error)

	// DecryptDataKey decrypts the key with the provided certificate private key
	DecryptDataKey(key []byte, priv crypto.PrivateKey, cert *smx509.Certificate, opts any) ([]byte, error)
}

Session is an interface that provides methods to generate and encrypt/decrypt data keys

type SignedAndEnvelopedData

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

SignedAndEnvelopedData is an opaque data structure for creating signed and enveloped data payloads

func NewSMSignedAndEnvelopedData

func NewSMSignedAndEnvelopedData(data []byte, cipher pkcs.Cipher) (*SignedAndEnvelopedData, error)

NewSMSignedAndEnvelopedData takes data and cipher and initializes a new PKCS7(SM) SignedAndEnvelopedData structure that is ready to be signed via AddSigner and encrypted via AddRecipient. The digest algorithm is set to SM3 by default.

func NewSignedAndEnvelopedData

func NewSignedAndEnvelopedData(data []byte, cipher pkcs.Cipher) (*SignedAndEnvelopedData, error)

NewSignedAndEnvelopedData takes data and cipher and initializes a new PKCS7 SignedAndEnvelopedData structure that is ready to be signed via AddSigner and encrypted via AddRecipient. The digest algorithm is set to SHA1 by default and can be changed by calling SetDigestAlgorithm.

func (*SignedAndEnvelopedData) AddCertificate

func (saed *SignedAndEnvelopedData) AddCertificate(cert *smx509.Certificate)

AddCertificate adds the certificate to the payload. Useful for parent certificates

func (*SignedAndEnvelopedData) AddRecipient

func (saed *SignedAndEnvelopedData) AddRecipient(recipient *smx509.Certificate) error

AddRecipient adds a recipient to the payload

func (*SignedAndEnvelopedData) AddSigner

func (saed *SignedAndEnvelopedData) AddSigner(ee *smx509.Certificate, pkey crypto.PrivateKey) error

AddSigner is a wrapper around AddSignerChain() that adds a signer without any parent.

func (*SignedAndEnvelopedData) AddSignerChain

func (saed *SignedAndEnvelopedData) AddSignerChain(ee *smx509.Certificate, pkey crypto.PrivateKey, parents []*smx509.Certificate) error

func (*SignedAndEnvelopedData) Finish

func (saed *SignedAndEnvelopedData) Finish() ([]byte, error)

Finish marshals the content and its signers

func (*SignedAndEnvelopedData) SetDigestAlgorithm

func (saed *SignedAndEnvelopedData) SetDigestAlgorithm(d asn1.ObjectIdentifier)

SetDigestAlgorithm sets the digest algorithm to be used in the signing process.

This should be called before adding signers

type SignedData

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

SignedData is an opaque data structure for creating signed data payloads

Example
// generate a signing cert or load a key pair
cert, err := createTestCertificate(x509.SHA256WithRSA, false)
if err != nil {
	fmt.Printf("Cannot create test certificates: %s", err)
}

// Initialize a SignedData struct with content to be signed
signedData, err := NewSignedData([]byte("Example data to be signed"))
if err != nil {
	fmt.Printf("Cannot initialize signed data: %s", err)
}

// Add the signing cert and private key
if err := signedData.AddSigner(cert.Certificate, cert.PrivateKey, SignerInfoConfig{}); err != nil {
	fmt.Printf("Cannot add signer: %s", err)
}

// Call Detach() is you want to remove content from the signature
// and generate an S/MIME detached signature
signedData.Detach()

// Finish() to obtain the signature bytes
detachedSignature, err := signedData.Finish()
if err != nil {
	fmt.Printf("Cannot finish signing data: %s", err)
}
pem.Encode(os.Stdout, &pem.Block{Type: "PKCS7", Bytes: detachedSignature})
Output:

func NewSMSignedData

func NewSMSignedData(data []byte) (*SignedData, error)

NewSMSignedData creates a new SignedData object using the provided data and sets the appropriate OIDs for SM2 and SM3 algorithms. It returns a pointer to the SignedData object and an error if any occurs during the creation of the SignedData object.

func NewSMSignedDataWithDigest added in v0.29.7

func NewSMSignedDataWithDigest(digest []byte) (*SignedData, error)

NewSMSignedDataWithDigest creates a new SignedData structure with the provided digest. The digest is expected to be a precomputed hash of the content to be signed. This function initializes the SignedData with the necessary OIDs for SM3 and SM2 algorithms.

func NewSignedData

func NewSignedData(data []byte) (*SignedData, error)

NewSignedData takes data and initializes a PKCS7 SignedData struct that is ready to be signed via AddSigner. The digest algorithm is set to SHA1 by default and can be changed by calling SetDigestAlgorithm.

func NewSignedDataWithDigest added in v0.29.7

func NewSignedDataWithDigest(digest []byte) (*SignedData, error)

NewSignedDataWithDigest creates a new SignedData structure with the provided digest. The digest is used to initialize the SignedData object, and the content is set to an empty ASN.1 RawValue. The function returns a pointer to the SignedData object and an error if any occurs.

func (*SignedData) AddCertificate

func (sd *SignedData) AddCertificate(cert *smx509.Certificate)

AddCertificate adds the certificate to the payload. Useful for parent certificates

func (*SignedData) AddSigner

func (sd *SignedData) AddSigner(ee *smx509.Certificate, pkey crypto.PrivateKey, config SignerInfoConfig) error

AddSigner is a wrapper around AddSignerChain() that adds a signer without any parent.

func (*SignedData) AddSignerChain

func (sd *SignedData) AddSignerChain(ee *smx509.Certificate, pkey crypto.PrivateKey, parents []*smx509.Certificate, config SignerInfoConfig) error

AddSignerChain signs attributes about the content and adds certificates and signers infos to the Signed Data. The certificate and private key of the end-entity signer are used to issue the signature, and any parent of that end-entity that need to be added to the list of certifications can be specified in the parents slice.

The signature algorithm used to hash the data is the one of the end-entity certificate.

func (*SignedData) Detach

func (sd *SignedData) Detach()

Detach removes content from the signed data struct to make it a detached signature. This must be called right before Finish()

func (*SignedData) Finish

func (sd *SignedData) Finish() ([]byte, error)

Finish marshals the content and its signers

func (*SignedData) GetSignedData

func (sd *SignedData) GetSignedData() *signedData

GetSignedData returns the private Signed Data

func (*SignedData) RemoveAuthenticatedAttributes

func (sd *SignedData) RemoveAuthenticatedAttributes()

RemoveAuthenticatedAttributes removes authenticated attributes from signedData similar to OpenSSL's PKCS7_NOATTR or -noattr flags

func (*SignedData) RemoveUnauthenticatedAttributes

func (sd *SignedData) RemoveUnauthenticatedAttributes()

RemoveUnauthenticatedAttributes removes unauthenticated attributes from signedData

func (*SignedData) SetDigestAlgorithm

func (sd *SignedData) SetDigestAlgorithm(d asn1.ObjectIdentifier)

SetDigestAlgorithm sets the digest algorithm to be used in the signing process.

This should be called before adding signers

func (*SignedData) SetEncryptionAlgorithm

func (sd *SignedData) SetEncryptionAlgorithm(d asn1.ObjectIdentifier)

SetEncryptionAlgorithm sets the encryption algorithm to be used in the signing process.

This should be called before adding signers

func (*SignedData) SignWithoutAttr added in v0.17.0

func (sd *SignedData) SignWithoutAttr(ee *smx509.Certificate, pkey crypto.PrivateKey, config SignerInfoConfig) error

SignWithoutAttr issues a signature on the content of the pkcs7 SignedData. Unlike AddSigner/AddSignerChain, it calculates the digest on the data alone and does not include any signed attributes like timestamp and so on.

This function is needed to sign old Android APKs, something you probably shouldn't do unless you're maintaining backward compatibility for old applications.

type SignerInfoConfig

type SignerInfoConfig struct {
	ExtraSignedAttributes   []Attribute // Additional attributes to be included in the signed attributes.
	ExtraUnsignedAttributes []Attribute // Additional attributes to be included in the unsigned attributes.
	SkipCertificates        bool        // Skip adding certificates to the payload
}

SignerInfoConfig contains configuration options for the signer information. It allows specifying additional signed and unsigned attributes, as well as an option to skip adding certificates to the payload.

type VerifyFunc

type VerifyFunc func() error

Jump to

Keyboard shortcuts

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