Debian Codegraph @

ietf-cms/signed_data.go

raw symbols SCIP
1 package cms
2
3 import (
4 "crypto/x509"
5 "encoding/asn1"
6
7 "github.com/github/smimesign/ietf-cms/protocol"
8 )
9
10 // SignedData represents a signed message or detached signature.
11 type SignedData struct {
12 psd *protocol.SignedData
13 }
14
15 // NewSignedData creates a new SignedData from the given data.
16 func NewSignedData(data []byte) (*SignedData, error) {
17 eci, err := protocol.NewDataEncapsulatedContentInfo(data)
18 if err != nil {
19 return nil, err
20 }
21
22 psd, err := protocol.NewSignedData(eci)
23 if err != nil {
24 return nil, err
25 }
26
27 return &SignedData{psd}, nil
28 }
29
30 // ParseSignedData parses a SignedData from BER encoded data.
31 func ParseSignedData(ber []byte) (*SignedData, error) {
32 ci, err := protocol.ParseContentInfo(ber)
33 if err != nil {
34 return nil, err
35 }
36
37 psd, err := ci.SignedDataContent()
38 if err != nil {
39 return nil, err
40 }
41
42 return &SignedData{psd}, nil
43 }
44
45 // GetData gets the encapsulated data from the SignedData. Nil will be returned
46 // if this is a detached signature. A protocol.ErrWrongType will be returned if
47 // the SignedData encapsulates something other than data (1.2.840.113549.1.7.1).
48 func (sd *SignedData) GetData() ([]byte, error) {
49 return sd.psd.EncapContentInfo.DataEContent()
50 }
51
52 // GetCertificates gets all the certificates stored in the SignedData.
53 func (sd *SignedData) GetCertificates() ([]*x509.Certificate, error) {
54 return sd.psd.X509Certificates()
55 }
56
57 // SetCertificates replaces the certificates stored in the SignedData with new
58 // ones.
59 func (sd *SignedData) SetCertificates(certs []*x509.Certificate) error {
60 sd.psd.ClearCertificates()
61 for _, cert := range certs {
62 if err := sd.psd.AddCertificate(cert); err != nil {
63 return err
64 }
65 }
66 return nil
67 }
68
69 // Detached removes the data content from this SignedData. No more signatures
70 // can be added after this method has been called.
71 func (sd *SignedData) Detached() {
72 sd.psd.EncapContentInfo.EContent = asn1.RawValue{}
73 }
74
75 // IsDetached checks if this SignedData has data content.
76 func (sd *SignedData) IsDetached() bool {
77 return sd.psd.EncapContentInfo.EContent.Bytes == nil
78 }
79
80 // ToDER encodes this SignedData message using DER.
81 func (sd *SignedData) ToDER() ([]byte, error) {
82 return sd.psd.ContentInfoDER()
83 }