-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigned_info.go
158 lines (136 loc) · 3.84 KB
/
signed_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package xmldsig
import (
"context"
"crypto/x509"
"encoding/base64"
"errors"
"github.com/beevik/etree"
rhtree "github.com/russellhaering/goxmldsig/etreeutils"
)
type SignedInfo struct {
Id string
CanonicalizationMethod *CanonicalizationMethod
SignatureMethod *SignatureMethod
References []*Reference
signature *Signature
cachedXml *etree.Element
}
func newSignedInfo(signature *Signature) *SignedInfo {
return &SignedInfo{
signature: signature,
}
}
func (xml *SignedInfo) root() *SignedXml {
return xml.signature.root()
}
func (xml *SignedInfo) validateDigests(ctx context.Context) ([]*etree.Element, error) {
validated := make([]*etree.Element, 0)
for _, reference := range xml.References {
err := reference.validateDigest(ctx)
if err != nil {
return nil, err
}
validated = append(validated, reference.cachedXml)
}
return validated, nil
}
func (xml *SignedInfo) validateSignature(ctx context.Context, cert *x509.Certificate) error {
elementNsContext, err := rhtree.NSBuildParentContext(xml.cachedXml)
if err != nil {
return err
}
detachtedElement, err := rhtree.NSDetatch(elementNsContext, xml.cachedXml)
if err != nil {
return err
}
canonicalizedData, err := xml.CanonicalizationMethod.canonicalizer.Canonicalize(ctx, detachtedElement)
if err != nil {
return err
}
signatureMethod, err := GetSignatureMethod(xml.SignatureMethod.Algorithm)
if err != nil {
return err
}
signatureAlgorithm, err := signatureMethod.GetSignatureAlgorithm()
if err != nil {
return err
}
signatureValue, err := base64.StdEncoding.DecodeString(xml.signature.SignatureValue.Value)
if err != nil {
return err
}
err = cert.CheckSignature(signatureAlgorithm, canonicalizedData, signatureValue)
if err != nil {
return err
}
return nil
}
func (xml *SignedInfo) loadXml(el *etree.Element) error {
err := validateElement(el, "SignedInfo", XmlDSigNamespaceUri)
if err != nil {
return err
}
// Get the canonicalization method
canonicalizationMethodElement, err := getSingleChildElement(el, "CanonicalizationMethod", XmlDSigNamespaceUri)
if err != nil {
return err
}
xml.CanonicalizationMethod = newCanonicalizationMethod(xml)
err = xml.CanonicalizationMethod.loadXml(canonicalizationMethodElement)
if err != nil {
return err
}
// Get the signature method
signatureMethodElement, err := getSingleChildElement(el, "SignatureMethod", XmlDSigNamespaceUri)
if err != nil {
return err
}
xml.SignatureMethod = newSignatureMethod(xml)
err = xml.SignatureMethod.loadXml(signatureMethodElement)
if err != nil {
return err
}
// Get the references
referenceElements := el.SelectElements("Reference")
for _, referenceElement := range referenceElements {
reference := newReference(xml)
err := reference.loadXml(referenceElement)
if err != nil {
return err
}
xml.References = append(xml.References, reference)
}
xml.cachedXml = el
return nil
}
func (xml *SignedInfo) getXml() (*etree.Element, error) {
el := etree.NewElement("SignedInfo")
el.Space = xml.root().getElementSpace(XmlDSigNamespaceUri)
if xml.Id != "" {
el.CreateAttr("Id", xml.Id)
}
if xml.CanonicalizationMethod == nil {
return nil, errors.New("signed info does not contain a CanonicalizationMethod element")
}
canonicalizationMethodElement, err := xml.CanonicalizationMethod.getXml()
if err != nil {
return nil, err
}
el.AddChild(canonicalizationMethodElement)
if xml.SignatureMethod == nil {
return nil, errors.New("signed info does not contain a SignatureMethod element")
}
signatureMethodElement, err := xml.SignatureMethod.getXml()
if err != nil {
return nil, err
}
el.AddChild(signatureMethodElement)
for _, reference := range xml.References {
referenceElement, err := reference.getXml()
if err != nil {
return nil, err
}
el.AddChild(referenceElement)
}
return el, nil
}