forked from textnow/gosoap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wsse.go
283 lines (227 loc) · 6.92 KB
/
wsse.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package soap
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/tls"
"encoding/base64"
"encoding/xml"
"fmt"
"time"
)
// Implements the WS-Security standard using X.509 certificate signatures.
// https://www.di-mgt.com.au/xmldsig2.html is a handy reference to the WS-Security signing process.
const (
wsseNS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
wsuNS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
dsigNS = "http://www.w3.org/2000/09/xmldsig#"
encTypeBinary = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"
valTypeX509Token = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"
canonicalizationExclusiveC14N = "http://www.w3.org/2001/10/xml-exc-c14n#"
rsaSha1Sig = "http://www.w3.org/2000/09/xmldsig#rsa-sha1"
sha1Sig = "http://www.w3.org/2000/09/xmldsig#sha1"
)
// WSSEAuthInfo contains the information required to use WS-Security X.509 signing.
type WSSEAuthInfo struct {
certDER tls.Certificate
key crypto.PrivateKey
}
// WSSEAuthIDs contains generated IDs used in WS-Security X.509 signing.
type WSSEAuthIDs struct {
securityTokenID string
bodyID string
}
// NewWSSEAuthInfo retrieves the supplied certificate path and key path for signing SOAP requests.
// These requests will be secured using the WS-Security X.509 security standard.
func NewWSSEAuthInfo(certPath string, keyPath string) (*WSSEAuthInfo, error) {
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return nil, err
}
return &WSSEAuthInfo{
certDER: cert,
key: cert.PrivateKey,
}, nil
}
type binarySecurityToken struct {
XMLName xml.Name `xml:"wsse:BinarySecurityToken"`
XMLNS string `xml:"xmlns:wsu,attr"`
WsuID string `xml:"wsu:Id,attr"`
EncodingType string `xml:"EncodingType,attr"`
ValueType string `xml:"ValueType,attr"`
Value string `xml:",chardata"`
}
type canonicalizationMethod struct {
XMLName xml.Name `xml:"CanonicalizationMethod"`
Algorithm string `xml:"Algorithm,attr"`
}
type signatureMethod struct {
XMLName xml.Name `xml:"SignatureMethod"`
Algorithm string `xml:"Algorithm,attr"`
}
type digestMethod struct {
XMLName xml.Name `xml:"DigestMethod"`
Algorithm string `xml:"Algorithm,attr"`
}
type digestValue struct {
XMLName xml.Name `xml:"DigestValue"`
Value string `xml:",chardata"`
}
type transform struct {
XMLName xml.Name `xml:"Transform"`
Algorithm string `xml:"Algorithm,attr"`
}
type transforms struct {
XMLName xml.Name `xml:"Transforms"`
Transform transform
}
type signatureReference struct {
XMLName xml.Name `xml:"Reference"`
URI string `xml:"URI,attr"`
Transforms transforms
DigestMethod digestMethod
DigestValue digestValue
}
type signedInfo struct {
XMLName xml.Name `xml:"SignedInfo"`
XMLNS string `xml:"xmlns,attr"`
CanonicalizationMethod canonicalizationMethod
SignatureMethod signatureMethod
Reference signatureReference
}
type strReference struct {
XMLName xml.Name `xml:"wsse:Reference"`
ValueType string `xml:"ValueType,attr"`
URI string `xml:"URI,attr"`
}
type securityTokenReference struct {
XMLName xml.Name `xml:"wsse:SecurityTokenReference"`
XMLNS string `xml:"xmlns:wsu,attr"`
Reference strReference
}
type keyInfo struct {
XMLName xml.Name `xml:"KeyInfo"`
SecurityTokenReference securityTokenReference
}
type signature struct {
XMLName xml.Name `xml:"Signature"`
XMLNS string `xml:"xmlns,attr"`
SignedInfo signedInfo
SignatureValue string `xml:"SignatureValue"`
KeyInfo keyInfo
}
type security struct {
XMLName xml.Name `xml:"wsse:Security"`
XMLNS string `xml:"xmlns:wsse,attr"`
BinarySecurityToken binarySecurityToken
Signature signature
}
func (w *WSSEAuthIDs) generateToken() ([]byte, error) {
// We use a concatentation of the time and 10 securely generated random numbers to be the tokens.
b := make([]byte, 10)
token := sha1.New()
token.Write([]byte(time.Now().Format(time.RFC3339)))
_, err := rand.Read(b)
if err != nil {
return nil, err
}
token.Write(b)
tokenHex := token.Sum(nil)
return tokenHex, nil
}
func generateWSSEAuthIDs() (*WSSEAuthIDs, error) {
w := &WSSEAuthIDs{}
securityTokenHex, err := w.generateToken()
if err != nil {
return nil, err
}
w.securityTokenID = fmt.Sprintf("SecurityToken-%x", securityTokenHex)
bodyTokenHex, err := w.generateToken()
if err != nil {
return nil, err
}
w.bodyID = fmt.Sprintf("Body-%x", bodyTokenHex)
return w, nil
}
func (w *WSSEAuthInfo) sign(body Body, ids *WSSEAuthIDs) (security, error) {
// 0. We create the body_id and security_token_id values
body.ID = ids.bodyID
// 1. We create the DigestValue of the body.
// We make some changes to canonicalize things.
// Since we have a copy, this is ok
bodyEnc, err := xml.Marshal(body)
if err != nil {
return security{}, err
}
canonBodyEnc, err := canonicalize(bodyEnc, "Body")
if err != nil {
return security{}, err
}
bodyHasher := sha1.New()
bodyHasher.Write(canonBodyEnc)
encodedBodyDigest := base64.StdEncoding.EncodeToString(bodyHasher.Sum(nil))
// 2. Set the DigestValue then sign the 'SignedInfo' struct
signedInfo := signedInfo{
XMLNS: dsigNS,
CanonicalizationMethod: canonicalizationMethod{
Algorithm: canonicalizationExclusiveC14N,
},
SignatureMethod: signatureMethod{
Algorithm: rsaSha1Sig,
},
Reference: signatureReference{
URI: "#" + ids.bodyID,
Transforms: transforms{
Transform: transform{
Algorithm: canonicalizationExclusiveC14N,
},
},
DigestMethod: digestMethod{
Algorithm: sha1Sig,
},
DigestValue: digestValue{
Value: encodedBodyDigest,
},
},
}
signedInfoEnc, err := xml.Marshal(signedInfo)
if err != nil {
return security{}, err
}
signedInfoHasher := sha1.New()
signedInfoHasher.Write(signedInfoEnc)
signedInfoDigest := signedInfoHasher.Sum(nil)
privateKey := w.key.(*rsa.PrivateKey)
signatureValue, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA1, signedInfoDigest)
if err != nil {
return security{}, err
}
encodedSignatureValue := base64.StdEncoding.EncodeToString(signatureValue)
encodedCertificateValue := base64.StdEncoding.EncodeToString(w.certDER.Certificate[0])
secHeader := security{
XMLNS: wsseNS,
BinarySecurityToken: binarySecurityToken{
XMLNS: wsuNS,
WsuID: ids.securityTokenID,
EncodingType: encTypeBinary,
ValueType: valTypeX509Token,
Value: encodedCertificateValue,
},
Signature: signature{
XMLNS: dsigNS,
SignedInfo: signedInfo,
SignatureValue: encodedSignatureValue,
KeyInfo: keyInfo{
SecurityTokenReference: securityTokenReference{
XMLNS: wsuNS,
Reference: strReference{
ValueType: valTypeX509Token,
URI: "#" + ids.securityTokenID,
},
},
},
},
}
return secHeader, nil
}