This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsse.go
240 lines (213 loc) · 7.65 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
package eet
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/xml"
"strings"
"github.com/gofrs/uuid"
"github.com/pkg/errors"
)
type SOAPEnvelopeRequest struct {
XMLName xml.Name `xml:"soap:Envelope"`
XmlnsSoap string `xml:"xmlns:soap,attr"`
Header SOAPHeader `xml:"SOAP-ENV:Header"`
Body SOAPBody `xml:"soap:Body"`
}
func NewSOAPEnvelopeRequest(content interface{}, signer *Signer) (SOAPEnvelopeRequest, error) {
bodyId := "id-" + strings.ToUpper(hex.EncodeToString(uuid.Must(uuid.NewV4()).Bytes()))
certId := "X509-" + strings.ToUpper(hex.EncodeToString(uuid.Must(uuid.NewV4()).Bytes()))
sigId := "SIG-" + strings.ToUpper(hex.EncodeToString(uuid.Must(uuid.NewV4()).Bytes()))
keyId := "KI-" + strings.ToUpper(hex.EncodeToString(uuid.Must(uuid.NewV4()).Bytes()))
secTokenId := "STR-" + strings.ToUpper(hex.EncodeToString(uuid.Must(uuid.NewV4()).Bytes()))
envelope := SOAPEnvelopeRequest{
XmlnsSoap: NsSoapUrl,
Header: SOAPHeader{
XmlnsSoapEnv: NsSoapEnvUrl,
Security: WsseSecurity{
XmlnsWsse: NsWsseUrl,
XmlnsWsu: NsWsuUrl,
MustUnderstand: "1",
BinarySecurityToken: WsseBinarySecurityToken{
EncodingType: EncodingBase64Url,
ValueType: ValueX509Url,
WsuId: certId,
},
Signature: DsSignature{
XmlnsDs: NsDsUrl,
Id: sigId,
SignedInfo: DsSignedInfo{
XmlnsDs: NsDsUrl,
XmlnsSoap: NsSoapUrl,
CanonicalizationMethod: DsCanonicalizationMethod{
Algorithm: NsEcUrl,
InclusiveNamespaces: EcInclusiveNamespaces{
XmlnsEc: NsEcUrl,
PrefixList: NsSoap,
},
},
SignatureMethod: DsSignatureMethod{
Algorithm: AlgorithmSHA256,
},
Reference: DsReference{
URI: "#" + bodyId,
Transforms: DsTransforms{
Transform: DsTransform{
Algorithm: NsEcUrl,
InclusiveNamespaces: EcInclusiveNamespaces{
XmlnsEc: NsEcUrl,
},
},
},
DigestMethod: DsDigestMethod{
Algorithm: AlgorithmDigestSHA256,
},
DigestValue: DsDigestValue{},
},
},
SignatureValue: DsSignatureValue{},
KeyInfo: DsKeyInfo{
Id: keyId,
SecurityTokenReference: WsseSecurityTokenReference{
XmlnsWsse: NsWsseUrl,
XmlnsWsu: NsWsuUrl,
WsuId: secTokenId,
Reference: WsseReference{
URI: "#" + certId,
ValueType: ValueX509Url,
},
},
},
},
},
},
Body: SOAPBody{
XmlnsSoap: NsSoapUrl,
XmlnsWsu: NsWsuUrl,
WsuId: bodyId,
Content: content,
},
}
// BinarySecurityToken
envelope.Header.Security.BinarySecurityToken.Value = signer.Base64Cert()
// DigestValue
body, err := xml.Marshal(envelope.Body)
if err != nil {
return SOAPEnvelopeRequest{}, errors.Wrap(err, "Failed to xml.Marshal Body")
}
bodySum := sha256.Sum256(body)
envelope.Header.Security.Signature.SignedInfo.Reference.DigestValue.Value = base64.StdEncoding.EncodeToString(bodySum[:])
// SignatureValue
signedInfo, err := xml.Marshal(envelope.Header.Security.Signature.SignedInfo)
if err != nil {
return SOAPEnvelopeRequest{}, errors.Wrap(err, "Failed to xml.Marshal Header.Security.Signature.SignedInfo")
}
signedSignedInfo, err := signer.Sign(signedInfo)
if err != nil {
return SOAPEnvelopeRequest{}, errors.Wrap(err, "Failed to Sign xml.Marshaled Header.Security.Signature.SignedInfo")
}
envelope.Header.Security.Signature.SignatureValue.Value = base64.StdEncoding.EncodeToString(signedSignedInfo)
return envelope, nil
}
type SOAPHeader struct {
XMLName xml.Name `xml:"SOAP-ENV:Header"`
XmlnsSoapEnv string `xml:"xmlns:SOAP-ENV,attr"`
Security WsseSecurity `xml:"wsse:Security"`
}
type WsseSecurity struct {
XMLName xml.Name `xml:"wsse:Security"`
XmlnsWsse string `xml:"xmlns:wsse,attr"`
XmlnsWsu string `xml:"xmlns:wsu,attr"`
MustUnderstand string `xml:"soap:mustUnderstand,attr"`
BinarySecurityToken WsseBinarySecurityToken `xml:"wsse:BinarySecurityToken"`
Signature DsSignature `xml:"ds:Signature"`
}
type DsSignature struct {
XMLName xml.Name `xml:"ds:Signature"`
XmlnsDs string `xml:"xmlns:ds,attr"`
Id string `xml:"Id,attr"`
SignedInfo DsSignedInfo `xml:"ds:SignedInfo"`
SignatureValue DsSignatureValue `xml:"ds:SignatureValue"`
KeyInfo DsKeyInfo `xml:"ds:KeyInfo"`
}
type DsSignedInfo struct {
XMLName xml.Name `xml:"ds:SignedInfo"`
XmlnsDs string `xml:"xmlns:ds,attr"`
XmlnsSoap string `xml:"xmlns:soap,attr"`
CanonicalizationMethod DsCanonicalizationMethod `xml:"ds:CanonicalizationMethod"`
SignatureMethod DsSignatureMethod `xml:"ds:SignatureMethod"`
Reference DsReference `xml:"ds:Reference"`
}
type DsCanonicalizationMethod struct {
XMLName xml.Name `xml:"ds:CanonicalizationMethod"`
Algorithm string `xml:"Algorithm,attr"`
InclusiveNamespaces EcInclusiveNamespaces `xml:"ec:InclusiveNamespaces"`
}
type EcInclusiveNamespaces struct {
XMLName xml.Name `xml:"ec:InclusiveNamespaces"`
XmlnsEc string `xml:"xmlns:ec,attr"`
PrefixList string `xml:"PrefixList,attr"`
}
type DsSignatureMethod struct {
XMLName xml.Name `xml:"ds:SignatureMethod"`
Algorithm string `xml:"Algorithm,attr"`
}
type DsReference struct {
XMLName xml.Name `xml:"ds:Reference"`
URI string `xml:"URI,attr"`
Transforms DsTransforms `xml:"ds:Transforms"`
DigestMethod DsDigestMethod `xml:"ds:DigestMethod"`
DigestValue DsDigestValue `xml:"ds:DigestValue"`
}
type DsTransforms struct {
XMLName xml.Name `xml:"ds:Transforms"`
Transform DsTransform `xml:"ds:Transform"`
}
type DsTransform struct {
XMLName xml.Name `xml:"ds:Transform"`
Algorithm string `xml:"Algorithm,attr"`
InclusiveNamespaces EcInclusiveNamespaces `xml:"ec:InclusiveNamespaces"`
}
type DsDigestMethod struct {
XMLName xml.Name `xml:"ds:DigestMethod"`
Algorithm string `xml:"Algorithm,attr"`
}
type DsDigestValue struct {
XMLName xml.Name `xml:"ds:DigestValue"`
Value string `xml:",chardata"`
}
type DsSignatureValue struct {
XMLName xml.Name `xml:"ds:SignatureValue"`
Value string `xml:",chardata"`
}
type DsKeyInfo struct {
XMLName xml.Name `xml:"ds:KeyInfo"`
Id string `xml:"Id,attr"`
SecurityTokenReference WsseSecurityTokenReference `xml:"wsse:SecurityTokenReference"`
}
type WsseSecurityTokenReference struct {
XMLName xml.Name `xml:"wsse:SecurityTokenReference"`
XmlnsWsse string `xml:"xmlns:wsse,attr"`
XmlnsWsu string `xml:"xmlns:wsu,attr"`
WsuId string `xml:"wsu:Id,attr"`
Reference WsseReference `xml:"wsse:Reference"`
}
type WsseReference struct {
XMLName xml.Name `xml:"wsse:Reference"`
URI string `xml:"URI,attr"`
ValueType string `xml:"ValueType,attr"`
}
type WsseBinarySecurityToken struct {
XMLName xml.Name `xml:"wsse:BinarySecurityToken"`
EncodingType string `xml:"EncodingType,attr"`
ValueType string `xml:"ValueType,attr"`
WsuId string `xml:"wsu:Id,attr"`
Value string `xml:",chardata"`
}
type SOAPBody struct {
XMLName xml.Name `xml:"soap:Body"`
XmlnsSoap string `xml:"xmlns:soap,attr"`
XmlnsWsu string `xml:"xmlns:wsu,attr"`
WsuId string `xml:"wsu:Id,attr"`
Content interface{} `xml:",omitempty"`
}