-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign.go
86 lines (76 loc) · 2.38 KB
/
sign.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
package xmlDigisign
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"encoding/xml"
"fmt"
)
const (
Xmlns = "http://www.w3.org/2000/09/xmldsig#"
CanonicalizationAlgorithm = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
SignatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1"
DigestMethodAlgorithm = "http://www.w3.org/2001/04/xmlenc#sha256"
)
var (
TransformAlgorithms = []string{
"http://www.w3.org/2000/09/xmldsig#enveloped-signature",
"http://www.w3.org/2006/12/xml-c14n11",
}
)
// XML data : Actual xml message in string which needs to be signed
// msgModel : Struct in which your message is going to marshal or un marshal
// Will return signature tag bytes
func (v *xmlSignature) SignXML(xmlData string, msgModel interface{}) (string, error) {
// Format the XML data
xmlData, err := formatXML(xmlData, msgModel)
if err != nil {
return "", err
}
// Canonicalize the XML data
canonicalXML := CanonicalizeXML(xmlData)
// Compute the digest of the canonical XML data
digest := sha256.Sum256([]byte(canonicalXML))
// Sign the digest using the private key
signature, err := rsa.SignPKCS1v15(rand.Reader, v.privateKey, crypto.SHA256, digest[:])
if err != nil {
return "", err
}
// Encode the signature as base64
signatureValue := base64.StdEncoding.EncodeToString(signature)
// Create the Signature struct
signatureData := Signature{
Xmlns: Xmlns,
SignedInfo: SignedInfo{
CanonicalizationMethod: CanonicalizationMethod{Algorithm: CanonicalizationAlgorithm},
SignatureMethod: SignatureMethod{Algorithm: SignatureAlgorithm},
Reference: Reference{
Transforms: Transforms{
Transform: []Transform{
{Algorithm: TransformAlgorithms[0]},
{Algorithm: TransformAlgorithms[1]},
},
},
DigestMethod: DigestMethod{Algorithm: DigestMethodAlgorithm},
DigestValue: base64.StdEncoding.EncodeToString(digest[:]),
},
},
SignatureValue: signatureValue,
KeyInfo: KeyInfo{
X509Data: X509Data{
X509SubjectName: v.certificate.Subject.String(),
X509IssuerSerial: X509IssuerSerial{
X509IssuerName: v.certificate.Issuer.String(),
X509SerialNumber: fmt.Sprintf("%v", v.certificate.SerialNumber),
},
},
},
}
signatureBytes, err := xml.Marshal(signatureData)
if err != nil {
return "", err
}
return CanonicalizeXML(string(signatureBytes)), nil
}