| 1 |
package cms |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/x509" |
| 5 |
"encoding/asn1" |
| 6 |
|
| 7 |
"github.com/github/smimesign/ietf-cms/protocol" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
type SignedData struct { |
| 12 |
psd *protocol.SignedData |
| 13 |
} |
| 14 |
|
| 15 |
|
| 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 |
|
| 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 |
|
| 46 |
|
| 47 |
|
| 48 |
func (sd *SignedData) GetData() ([]byte, error) { |
| 49 |
return sd.psd.EncapContentInfo.DataEContent() |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
func (sd *SignedData) GetCertificates() ([]*x509.Certificate, error) { |
| 54 |
return sd.psd.X509Certificates() |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 70 |
|
| 71 |
func (sd *SignedData) Detached() { |
| 72 |
sd.psd.EncapContentInfo.EContent = asn1.RawValue{} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
func (sd *SignedData) IsDetached() bool { |
| 77 |
return sd.psd.EncapContentInfo.EContent.Bytes == nil |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
func (sd *SignedData) ToDER() ([]byte, error) { |
| 82 |
return sd.psd.ContentInfoDER() |
| 83 |
} |