forked from russellhaering/goxmldsig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign_test.go
139 lines (108 loc) · 4.04 KB
/
sign_test.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
package dsig
import (
"crypto"
"encoding/base64"
"testing"
"github.com/beevik/etree"
"github.com/stretchr/testify/require"
)
func TestSign(t *testing.T) {
randomKeyStore := RandomKeyStoreForTest()
ctx := NewDefaultSigningContext(randomKeyStore)
authnRequest := &etree.Element{
Space: "samlp",
Tag: "AuthnRequest",
}
id := "_97e34c50-65ec-4132-8b39-02933960a96a"
authnRequest.CreateAttr("ID", id)
hash := crypto.SHA256.New()
canonicalized, err := ctx.Canonicalizer.Canonicalize(authnRequest)
require.NoError(t, err)
_, err = hash.Write(canonicalized)
require.NoError(t, err)
digest := hash.Sum(nil)
signed, err := ctx.SignEnveloped(authnRequest)
require.NoError(t, err)
require.NotEmpty(t, signed)
sig := signed.FindElement("//" + SignatureTag)
require.NotEmpty(t, sig)
signedInfo := sig.FindElement("//" + SignedInfoTag)
require.NotEmpty(t, signedInfo)
canonicalizationMethodElement := signedInfo.FindElement("//" + CanonicalizationMethodTag)
require.NotEmpty(t, canonicalizationMethodElement)
canonicalizationMethodAttr := canonicalizationMethodElement.SelectAttr(AlgorithmAttr)
require.NotEmpty(t, canonicalizationMethodAttr)
require.Equal(t, CanonicalXML11AlgorithmId.String(), canonicalizationMethodAttr.Value)
signatureMethodElement := signedInfo.FindElement("//" + SignatureMethodTag)
require.NotEmpty(t, signatureMethodElement)
signatureMethodAttr := signatureMethodElement.SelectAttr(AlgorithmAttr)
require.NotEmpty(t, signatureMethodAttr)
require.Equal(t, "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", signatureMethodAttr.Value)
referenceElement := signedInfo.FindElement("//" + ReferenceTag)
require.NotEmpty(t, referenceElement)
idAttr := referenceElement.SelectAttr(URIAttr)
require.NotEmpty(t, idAttr)
require.Equal(t, "#"+id, idAttr.Value)
transformsElement := referenceElement.FindElement("//" + TransformsTag)
require.NotEmpty(t, transformsElement)
transformElement := transformsElement.FindElement("//" + TransformTag)
require.NotEmpty(t, transformElement)
algorithmAttr := transformElement.SelectAttr(AlgorithmAttr)
require.NotEmpty(t, algorithmAttr)
require.Equal(t, EnvelopedSignatureAltorithmId.String(), algorithmAttr.Value)
digestMethodElement := referenceElement.FindElement("//" + DigestMethodTag)
require.NotEmpty(t, digestMethodElement)
digestMethodAttr := digestMethodElement.SelectAttr(AlgorithmAttr)
require.NotEmpty(t, digestMethodElement)
require.Equal(t, "http://www.w3.org/2001/04/xmlenc#sha256", digestMethodAttr.Value)
digestValueElement := referenceElement.FindElement("//" + DigestValueTag)
require.NotEmpty(t, digestValueElement)
require.Equal(t, base64.StdEncoding.EncodeToString(digest), digestValueElement.Text())
}
func TestSignErrors(t *testing.T) {
randomKeyStore := RandomKeyStoreForTest()
ctx := &SigningContext{
Hash: crypto.SHA512_256,
KeyStore: randomKeyStore,
IdAttribute: DefaultIdAttr,
Prefix: DefaultPrefix,
}
authnRequest := &etree.Element{
Space: "samlp",
Tag: "AuthnRequest",
}
_, err := ctx.SignEnveloped(authnRequest)
require.Error(t, err)
randomKeyStore = RandomKeyStoreForTest()
ctx = NewDefaultSigningContext(randomKeyStore)
authnRequest = &etree.Element{
Space: "samlp",
Tag: "AuthnRequest",
}
_, err = ctx.SignEnveloped(authnRequest)
require.Error(t, err)
}
func TestSignNonDefaultID(t *testing.T) {
// Sign a document by referencing a non-default ID attribute ("OtherID"),
// and confirm that the signature correctly references it.
ks := RandomKeyStoreForTest()
ctx := &SigningContext{
Hash: crypto.SHA256,
KeyStore: ks,
IdAttribute: "OtherID",
Prefix: DefaultPrefix,
Canonicalizer: MakeC14N11Canonicalizer(),
}
signable := &etree.Element{
Space: "foo",
Tag: "Bar",
}
id := "_97e34c50-65ec-4132-8b39-02933960a96b"
signable.CreateAttr("OtherID", id)
signed, err := ctx.SignEnveloped(signable)
require.NoError(t, err)
ref := signed.FindElement("./Signature/SignedInfo/Reference")
require.NotNil(t, ref)
refURI := ref.SelectAttrValue("URI", "")
require.Equal(t, refURI, "#"+id)
}