Debian Codegraph @

lib/pkcs7/attributes.go

raw symbols SCIP
1 //
2 // Copyright (c) SAS Institute Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 package pkcs7
18
19 import (
20 "encoding/asn1"
21 "errors"
22 "fmt"
23 "time"
24 )
25
26 type ErrNoAttribute struct {
27 ID asn1.ObjectIdentifier
28 }
29
30 func (e ErrNoAttribute) Error() string {
31 return fmt.Sprintf("attribute not found: %s", e.ID)
32 }
33
34 // Bytes returns a SET OF form of the attribute list for digesting, per RFC 2315 9.3, 2nd paragraph
35 func (l *AttributeList) Bytes() ([]byte, error) {
36 return marshalUnsortedSet(*l)
37 }
38
39 // Need to marshal authenticated attributes as a SET OF in order to digest them,
40 // but since go 1.15 sets get sorted which breaks the digest. Marshal as a
41 // sequence and then change the tag.
42 func marshalUnsortedSet(v interface{}) ([]byte, error) {
43 encoded, err := asn1.Marshal(v)
44 if err != nil {
45 return nil, err
46 }
47 if len(encoded) > 0 {
48 if encoded[0]&0x1f != asn1.TagSequence {
49 return nil, fmt.Errorf("expected sequence, got %d", encoded[0]&0x1f)
50 }
51 // sequence 16 -> set 17
52 encoded[0] |= 1
53 }
54 return encoded, nil
55 }
56
57 // GetOne unmarshals a single attribute, if it exists
58 func (l *AttributeList) GetOne(oid asn1.ObjectIdentifier, dest interface{}) error {
59 for _, raw := range *l {
60 if !raw.Type.Equal(oid) {
61 continue
62 }
63 rest, err := asn1.Unmarshal(raw.Values.Bytes, dest)
64 if err != nil {
65 return err
66 } else if len(rest) != 0 {
67 return fmt.Errorf("attribute %s: expected one, found multiple", oid)
68 } else {
69 return nil
70 }
71 }
72 return ErrNoAttribute{oid}
73 }
74
75 // GetAll unmarshals all values for an attribute. dest should be a pointer to a slice.
76 func (l *AttributeList) GetAll(oid asn1.ObjectIdentifier, dest interface{}) error {
77 for _, raw := range *l {
78 if raw.Type.Equal(oid) {
79 _, err := asn1.UnmarshalWithParams(raw.Values.FullBytes, dest, "set")
80 return err
81 }
82 }
83 return ErrNoAttribute{oid}
84 }
85
86 // create or append to an attribute
87 func (l *AttributeList) Add(oid asn1.ObjectIdentifier, obj interface{}) error {
88 value, err := asn1.Marshal(obj)
89 if err != nil {
90 return err
91 }
92 *l = appendAttr(*l, oid, value)
93 return nil
94 }
95
96 func appendAttr(attrList AttributeList, oid asn1.ObjectIdentifier, value []byte) AttributeList {
97 for i, attr := range attrList {
98 if attr.Type.Equal(oid) {
99 attr.Values.Bytes = append(attr.Values.Bytes, value...)
100 attrList[i] = attr
101 return attrList
102 }
103 }
104 return append(attrList, Attribute{
105 Type: oid,
106 Values: asn1.RawValue{
107 Class: asn1.ClassUniversal,
108 Tag: asn1.TagSet,
109 IsCompound: true,
110 Bytes: value,
111 }})
112 }
113
114 func (l AttributeList) Exists(oid asn1.ObjectIdentifier) bool {
115 for _, attr := range l {
116 if attr.Type.Equal(oid) {
117 return true
118 }
119 }
120 return false
121 }
122
123 func (i SignerInfo) SigningTime() (time.Time, error) {
124 var raw asn1.RawValue
125 if err := i.AuthenticatedAttributes.GetOne(OidAttributeSigningTime, &raw); err != nil {
126 return time.Time{}, err
127 }
128 return ParseTime(raw)
129 }
130
131 // AuthenticatedAttributesBytes returns a SET OF form of the attribute list for digesting, per RFC 2315 9.3, 2nd paragraph
132 func (i SignerInfo) AuthenticatedAttributesBytes() ([]byte, error) {
133 if i.RawContent == nil {
134 return i.AuthenticatedAttributes.Bytes()
135 }
136 // decode the SignerInfo as a sequence of raw values to extract how the
137 // authenticated attributes were originally encoded extract the original
138 var seq []asn1.RawValue
139 if _, err := asn1.Unmarshal(i.RawContent, &seq); err != nil {
140 return nil, err
141 }
142 if len(seq) < 4 {
143 return nil, errors.New("short sequence in SignerInfo")
144 }
145 raw := seq[3]
146 // tweak the attribute sequence to be a set
147 return marshalUnsortedSet(asn1.RawValue{
148 Tag: asn1.TagSequence,
149 IsCompound: true,
150 Bytes: raw.Bytes,
151 })
152 }