Documentation
¶
Overview ¶
Package pkcs7 implements parsing and generation of some PKCS#7 structures.
Index ¶
- Constants
- Variables
- func DegenerateCertificate(cert []byte) ([]byte, error)
- func Encrypt(content []byte, recipients []*x509.Certificate) ([]byte, error)
- func EncryptUsingPSK(content []byte, key []byte) ([]byte, error)
- type Attribute
- type MessageDigestMismatchError
- type PKCS7
- func (p7 *PKCS7) Decrypt(cert *x509.Certificate, pkey crypto.PrivateKey) ([]byte, error)
- func (p7 *PKCS7) DecryptUsingPSK(key []byte) ([]byte, error)
- func (p7 *PKCS7) GetOnlySigner() *x509.Certificate
- func (p7 *PKCS7) UnmarshalSignedAttribute(attributeType asn1.ObjectIdentifier, out interface{}) error
- func (p7 *PKCS7) Verify() (err error)
- func (p7 *PKCS7) VerifyWithChain(truststore *x509.CertPool) (err error)
- type SignedData
- func (sd *SignedData) AddCertificate(cert *x509.Certificate)
- func (sd *SignedData) AddSigner(ee *x509.Certificate, pkey crypto.PrivateKey, config SignerInfoConfig) error
- func (sd *SignedData) AddSignerChain(ee *x509.Certificate, pkey crypto.PrivateKey, chain []*x509.Certificate, ...) error
- func (sd *SignedData) AddSignerNoChain(ee *x509.Certificate, pkey crypto.PrivateKey, config SignerInfoConfig) error
- func (sd *SignedData) Detach()
- func (sd *SignedData) Finish() ([]byte, error)
- func (sd *SignedData) GetSignedData() *SignedDataInfo
- func (sd *SignedData) SetDigestAlgorithm(d asn1.ObjectIdentifier)
- type SignedDataInfo
- type SignerInfoConfig
Constants ¶
const ( // EncryptionAlgorithmDESCBC is the DES CBC encryption algorithm EncryptionAlgorithmDESCBC = iota // EncryptionAlgorithmAES128GCM is the AES 128 bits with GCM encryption algorithm EncryptionAlgorithmAES128GCM // EncryptionAlgorithmAES256GCM is the AES 256 bits with GCM encryption algorithm EncryptionAlgorithmAES256GCM )
Variables ¶
var ( //OIDData Signed Data OIDs OIDData = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1} // OIDSignedData Signed Data OID OIDSignedData = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 2} // OIDEnvelopedData Enveloped Data OID OIDEnvelopedData = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 3} // OIDEncryptedData EncryptedData OID OIDEncryptedData = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 6} // OIDAttributeContentType AttributeContentType OID OIDAttributeContentType = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 3} // OIDAttributeMessageDigest AttributeMessageDigest OID OIDAttributeMessageDigest = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 4} // OIDAttributeSigningTime AttributeSigningTime OID OIDAttributeSigningTime = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 5} // OIDDigestAlgorithmSHA1 Digest Algorithms OID OIDDigestAlgorithmSHA1 = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 26} // OIDDigestAlgorithmSHA256 DigestAlgorithmSHA256 OID OIDDigestAlgorithmSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1} // OIDDigestAlgorithmSHA384 DigestAlgorithmSHA384 OID OIDDigestAlgorithmSHA384 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 2} // OIDDigestAlgorithmSHA512 DigestAlgorithmSHA512 OID OIDDigestAlgorithmSHA512 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 3} // OIDEncryptionAlgorithmRSA Signature Algorithms OID OIDEncryptionAlgorithmRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1} // OIDEncryptionAlgorithmRSASHA1 EncryptionAlgorithmRSASHA1 OID OIDEncryptionAlgorithmRSASHA1 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 5} // OIDEncryptionAlgorithmRSASHA256 EncryptionAlgorithmRSASHA256 OID OIDEncryptionAlgorithmRSASHA256 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 11} // OIDEncryptionAlgorithmRSASHA384 EncryptionAlgorithmRSASHA384 OIDEncryptionAlgorithmRSASHA384 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 12} // OIDEncryptionAlgorithmRSASHA512 EncryptionAlgorithmRSASHA512 OID OIDEncryptionAlgorithmRSASHA512 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 13} // OIDEncryptionAlgorithmDSA EncryptionAlgorithmDSA OID OIDEncryptionAlgorithmDSA = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 3} // OIDDigestAlgorithmECDSASHA1 DigestAlgorithmECDSASHA1 OID OIDDigestAlgorithmECDSASHA1 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 1} // OIDDigestAlgorithmECDSASHA256 DigestAlgorithmECDSASHA256 OID OIDDigestAlgorithmECDSASHA256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2} // OIDDigestAlgorithmECDSASHA384 DigestAlgorithmECDSASHA384 OID OIDDigestAlgorithmECDSASHA384 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 3} // OIDDigestAlgorithmECDSASHA512 DigestAlgorithmECDSASHA512 OID OIDDigestAlgorithmECDSASHA512 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 4} // OIDEncryptionAlgorithmECDSAP256 EncryptionAlgorithmECDSAP256 OID OIDEncryptionAlgorithmECDSAP256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 7} // OIDEncryptionAlgorithmECDSAP384 EncryptionAlgorithmECDSAP384 OID OIDEncryptionAlgorithmECDSAP384 = asn1.ObjectIdentifier{1, 3, 132, 0, 34} // OIDEncryptionAlgorithmECDSAP521 EncryptionAlgorithmECDSAP521 OID OIDEncryptionAlgorithmECDSAP521 = asn1.ObjectIdentifier{1, 3, 132, 0, 35} //OIDEncryptionAlgorithmDESCBC EncryptionAlgorithmDESCBC OID OIDEncryptionAlgorithmDESCBC = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 7} // OIDEncryptionAlgorithmDESEDE3CBC EncryptionAlgorithmDESEDE3CBC OID OIDEncryptionAlgorithmDESEDE3CBC = asn1.ObjectIdentifier{1, 2, 840, 113549, 3, 7} // OIDEncryptionAlgorithmAES256CBC OIDEncryptionAlgorithmAES256CBC OID OIDEncryptionAlgorithmAES256CBC = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 1, 42} // OIDEncryptionAlgorithmAES128GCM EncryptionAlgorithmAES128GCM OID OIDEncryptionAlgorithmAES128GCM = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 1, 6} // OIDEncryptionAlgorithmAES128CBC EncryptionAlgorithmAES128CBC OID OIDEncryptionAlgorithmAES128CBC = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 1, 2} // OIDEncryptionAlgorithmAES256GCM EncryptionAlgorithmAES256GCM OID OIDEncryptionAlgorithmAES256GCM = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 1, 46} )
var ContentEncryptionAlgorithm = EncryptionAlgorithmDESCBC
ContentEncryptionAlgorithm determines the algorithm used to encrypt the plaintext message. Change the value of this variable to change which algorithm is used in the Encrypt() function.
var ErrNotEncryptedContent = errors.New("pkcs7: content data is a decryptable data type")
ErrNotEncryptedContent is returned when attempting to Decrypt data that is not encrypted data
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.
var ErrUnsupportedAlgorithm = errors.New("pkcs7: cannot decrypt data: only RSA, DES, DES-EDE3, AES-256-CBC and AES-128-GCM supported")
ErrUnsupportedAlgorithm tells you when our quick dev assumptions have failed
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)
var ErrUnsupportedEncryptionAlgorithm = errors.New("pkcs7: cannot encrypt content: only DES-CBC and AES-GCM supported")
ErrUnsupportedEncryptionAlgorithm is returned when attempting to encrypt content with an unsupported algorithm.
Functions ¶
func DegenerateCertificate ¶
DegenerateCertificate creates a signed data structure containing only the provided certificate or certificate chain.
func Encrypt ¶
func Encrypt(content []byte, recipients []*x509.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 current value of the global ContentEncryptionAlgorithm package variable. By default, the value is EncryptionAlgorithmDESCBC. To use a different algorithm, change the value before calling Encrypt(). For example:
ContentEncryptionAlgorithm = EncryptionAlgorithmAES128GCM
TODO(fullsailor): Add support for encrypting content with other algorithms
Types ¶
type Attribute ¶
type Attribute struct { Type asn1.ObjectIdentifier Value interface{} }
Attribute represents a key value pair attribute. Value must be marshalable byte `encoding/asn1`
type MessageDigestMismatchError ¶
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 []*x509.Certificate CRLs []pkix.CertificateList Signers []signerInfo // contains filtered or unexported fields }
PKCS7 Represents a PKCS7 structure
func (*PKCS7) Decrypt ¶
func (p7 *PKCS7) Decrypt(cert *x509.Certificate, pkey crypto.PrivateKey) ([]byte, error)
Decrypt decrypts encrypted content info for recipient cert and private key
func (*PKCS7) DecryptUsingPSK ¶
DecryptUsingPSK decrypts encrypted data using caller provided pre-shared secret
func (*PKCS7) GetOnlySigner ¶
func (p7 *PKCS7) GetOnlySigner() *x509.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) UnmarshalSignedAttribute ¶
func (p7 *PKCS7) UnmarshalSignedAttribute(attributeType asn1.ObjectIdentifier, out interface{}) error
UnmarshalSignedAttribute decodes a single attribute from the signer info
type SignedData ¶
type SignedData struct {
// contains filtered or unexported fields
}
SignedData is an opaque data structure for creating signed data payloads
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 NewSignedDataWithContentType ¶
func NewSignedDataWithContentType(contentType asn1.ObjectIdentifier, data []byte) (*SignedData, error)
NewSignedDataWithContentType takes content type and 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 (*SignedData) AddCertificate ¶
func (sd *SignedData) AddCertificate(cert *x509.Certificate)
AddCertificate adds the certificate to the payload. Useful for parent certificates
func (*SignedData) AddSigner ¶
func (sd *SignedData) AddSigner(ee *x509.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 *x509.Certificate, pkey crypto.PrivateKey, chain []*x509.Certificate, config SignerInfoConfig) error
AddSignerChain signs attributes about the content and adds certificates and signers infos to the Signed Data. The certificate and private 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.
Following RFC 2315, 9.2 SignerInfo type, the distinguished name of the issuer of the end-entity signer is stored in the issuerAndSerialNumber section of the SignedData.SignerInfo, alongside the serial number of the end-entity.
func (*SignedData) AddSignerNoChain ¶
func (sd *SignedData) AddSignerNoChain(ee *x509.Certificate, pkey crypto.PrivateKey, config SignerInfoConfig) error
AddSignerNoChain is a wrapper around AddSignerChain() that adds a signer without any parent. Use this method, if no certificate needs to be placed in SignedData certificates
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() *SignedDataInfo
GetSignedData Get signed data
func (*SignedData) SetDigestAlgorithm ¶
func (sd *SignedData) SetDigestAlgorithm(d asn1.ObjectIdentifier)
SetDigestAlgorithm sets the digest algorithm to be used in the signing process
type SignedDataInfo ¶
type SignedDataInfo struct { Version int `asn1:"default:1"` DigestAlgorithmIdentifiers []pkix.AlgorithmIdentifier `asn1:"set"` ContentInfo contentInfo Certificates rawCertificates `asn1:"optional,tag:0"` CRLs []pkix.CertificateList `asn1:"optional,tag:1"` SignerInfos []signerInfo `asn1:"set"` }
SignedDataInfo Signed data info
type SignerInfoConfig ¶
type SignerInfoConfig struct { ExtraSignedAttributes []Attribute ExtraUnsignedAttributes []Attribute }
SignerInfoConfig are optional values to include when adding a signer