forked from alex-arce/gosoap
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwsse.go
304 lines (257 loc) · 9.01 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package soap
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/tls"
"encoding/base64"
"errors"
"reflect"
"strings"
"time"
"github.com/m29h/xml"
"github.com/google/uuid"
)
// Implements the WS-Security standard using X.509 certificate signatures.
// use SHA256 as default HMAC for signing purposes
// https://www.di-mgt.com.au/xmldsig2.html is a handy reference to the WS-Security signing process.
const (
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#"
//https://www.w3.org/TR/xmldsig-core1/#sec-MessageDigests
rsaSha1Sig = "http://www.w3.org/2000/09/xmldsig#rsa-sha1"
rsaSha256Sig = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
sha1Sig = "http://www.w3.org/2000/09/xmldsig#sha1"
sha256Sig = "http://www.w3.org/2001/04/xmlenc#sha256"
)
var (
// ErrUnableToSignEmptyEnvelope is returned if the envelope to be signed is empty. This is not valid.
ErrUnableToSignEmptyEnvelope = errors.New("unable to sign, envelope is empty")
)
// WSSEAuthInfo contains the information required to use WS-Security X.509 signing.
type WSSEAuthInfo struct {
certDER tls.Certificate
key crypto.PrivateKey
sigRef []signatureReference
}
// 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,
sigRef: make([]signatureReference, 0),
}, nil
}
type binarySecurityToken struct {
XMLName xml.Name `xml:"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd BinarySecurityToken"`
WsuID string `xml:"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd Id,attr"`
EncodingType string `xml:"EncodingType,attr"`
ValueType string `xml:"ValueType,attr"`
Value string `xml:",chardata"`
}
type inclusiveNamespaces struct {
XMLName xml.Name `xml:"http://www.w3.org/2001/10/xml-exc-c14n# InclusiveNamespaces"`
PrefixList string `xml:"PrefixList,attr"`
}
type canonicalizationMethod struct {
XMLName xml.Name `xml:"CanonicalizationMethod"`
Algorithm string `xml:"Algorithm,attr"`
InclusiveNamespaces inclusiveNamespaces
}
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:"http://www.w3.org/2000/09/xmldsig# Reference"`
URI string `xml:"URI,attr"`
Transforms transforms
DigestMethod digestMethod
DigestValue digestValue
}
type signedInfo struct {
XMLName xml.Name `xml:"http://www.w3.org/2000/09/xmldsig# SignedInfo"`
CanonicalizationMethod canonicalizationMethod
SignatureMethod signatureMethod
Reference []signatureReference
}
// =============================================================================
// Web Services Security Utility (WSU)
// =============================================================================
// timestamp allows Timestamps to be applied anywhere element wildcards are
// present, including as a SOAP header.
type timestamp struct {
XMLName xml.Name `xml:"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd Timestamp,omitempty"`
WsuID string `xml:"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd Id,attr"`
Created string `xml:"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd Created"`
Expires string `xml:"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd Expires"`
}
type strReference struct {
XMLName xml.Name `xml:"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd Reference"`
ValueType string `xml:"ValueType,attr"`
URI string `xml:"URI,attr"`
}
type securityTokenReference struct {
XMLName xml.Name `xml:"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd SecurityTokenReference"`
Reference strReference
}
type keyInfo struct {
XMLName xml.Name `xml:"http://www.w3.org/2000/09/xmldsig# KeyInfo"`
SecurityTokenReference securityTokenReference
}
type signature struct {
XMLName xml.Name `xml:"http://www.w3.org/2000/09/xmldsig# Signature"`
SignedInfo signedInfo
SignatureValue string `xml:"SignatureValue"`
KeyInfo keyInfo
}
type security struct {
XMLName xml.Name `xml:"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd Security"`
MustUnderstand int `xml:"http://schemas.xmlsoap.org/soap/envelope/ mustUnderstand,attr"`
BinarySecurityToken binarySecurityToken
Signature signature
Timestamp timestamp
}
func getWsuID() string {
return "WSSE" + uuid.New().String()
}
func (w *WSSEAuthInfo) addSignature(element any) error {
// 0. We create the id value and assign it to the incoming body.WsuID via reflect
id := getWsuID()
val := reflect.ValueOf(element)
if val.Kind().String() != "ptr" {
return errors.New("addSignature: body must be pointer")
}
if val.Elem().Kind().String() != "struct" {
return errors.New("addSignature: body must point to struct")
}
found := false
for i := 0; i < val.Elem().NumField(); i++ {
if strings.ToLower(val.Elem().Type().Field(i).Name) == "wsuid" {
val.Elem().Field(i).SetString(id)
found = true
}
}
if !found {
return errors.New("addSignature: body did not contain a WsuID struct field")
}
// 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(element)
if err != nil {
return err
}
//bodyHasher := sha1.New()
bodyHasher := sha256.New()
bodyHasher.Write(bodyEnc)
encodedBodyDigest := base64.StdEncoding.EncodeToString(bodyHasher.Sum(nil))
w.sigRef = append(w.sigRef, signatureReference{
URI: "#" + id,
Transforms: transforms{
Transform: transform{
Algorithm: canonicalizationExclusiveC14N,
},
},
DigestMethod: digestMethod{
Algorithm: sha256Sig, //sha1Sig,
},
DigestValue: digestValue{
Value: encodedBodyDigest,
},
})
return nil
}
func (w *WSSEAuthInfo) Header() HeaderBuilder {
return func(body any) (any, error) {
return w.securityHeader(body)
}
}
func (w *WSSEAuthInfo) securityHeader(body any) (security, error) {
if body == nil {
return security{}, ErrUnableToSignEmptyEnvelope
}
if err := w.addSignature(body); err != nil {
return security{}, err
}
timestamp := timestamp{
WsuID: "",
Created: time.Now().UTC().Format("2006-01-02T15:04:05.999Z07:00"),
Expires: time.Now().UTC().Add(10 * time.Second).Format("2006-01-02T15:04:05.999Z07:00"),
}
if err := w.addSignature(×tamp); err != nil {
return security{}, err
}
// 2. Set the DigestValue then sign the 'SignedInfo' struct
signedInfo := signedInfo{
CanonicalizationMethod: canonicalizationMethod{
Algorithm: canonicalizationExclusiveC14N,
},
SignatureMethod: signatureMethod{
Algorithm: rsaSha256Sig,
},
Reference: w.sigRef,
}
signedInfoEnc, err := xml.Marshal(signedInfo)
if err != nil {
return security{}, err
}
signedInfoHasher := sha256.New()
signedInfoHasher.Write(signedInfoEnc)
signedInfoDigest := signedInfoHasher.Sum(nil)
privateKey := w.key.(*rsa.PrivateKey)
signatureValue, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, signedInfoDigest)
if err != nil {
return security{}, err
}
encodedSignatureValue := base64.StdEncoding.EncodeToString(signatureValue)
encodedCertificateValue := base64.StdEncoding.EncodeToString(w.certDER.Certificate[0])
securityTokenID := getWsuID()
secHeader := security{
MustUnderstand: 1,
BinarySecurityToken: binarySecurityToken{
WsuID: securityTokenID,
EncodingType: encTypeBinary,
ValueType: valTypeX509Token,
Value: encodedCertificateValue,
},
Signature: signature{
SignedInfo: signedInfo,
SignatureValue: encodedSignatureValue,
KeyInfo: keyInfo{
SecurityTokenReference: securityTokenReference{
Reference: strReference{
ValueType: valTypeX509Token,
URI: "#" + securityTokenID,
},
},
},
},
Timestamp: timestamp,
}
w.sigRef = make([]signatureReference, 0)
return secHeader, nil
}