...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package pkcs7
18
19 import (
20 "encoding/asn1"
21 )
22
23 type contentInfo2 struct {
24 ContentType asn1.ObjectIdentifier
25 Value asn1.RawValue
26 }
27
28
29
30 func NewContentInfo(contentType asn1.ObjectIdentifier, data interface{}) (ci ContentInfo, err error) {
31 if data == nil {
32 return ContentInfo{ContentType: contentType}, nil
33 }
34
35
36
37
38 encoded, err := asn1.Marshal(data)
39 if err != nil {
40 return ContentInfo{}, err
41 }
42 ci2 := contentInfo2{
43 ContentType: contentType,
44 Value: asn1.RawValue{
45 Class: asn1.ClassContextSpecific,
46 Tag: 0,
47 IsCompound: true,
48 Bytes: encoded,
49 },
50 }
51 ciblob, err := asn1.Marshal(ci2)
52 if err != nil {
53 return ContentInfo{}, nil
54 }
55 return ContentInfo{Raw: ciblob, ContentType: contentType}, nil
56 }
57
58
59 func (ci ContentInfo) Unmarshal(dest interface{}) (err error) {
60
61 var ci2 contentInfo2
62 _, err = asn1.Unmarshal(ci.Raw, &ci2)
63 if err == nil {
64
65 _, err = asn1.Unmarshal(ci2.Value.Bytes, dest)
66 }
67 return
68 }
69
70
71 func (ci ContentInfo) Bytes() ([]byte, error) {
72 var value asn1.RawValue
73 if err := ci.Unmarshal(&value); err != nil {
74 if _, ok := err.(asn1.SyntaxError); ok {
75
76 return nil, nil
77 }
78 return nil, err
79 }
80 return value.Bytes, nil
81 }
82
View as plain text