-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigner.go
139 lines (114 loc) · 3.6 KB
/
signer.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 main
import (
"crypto"
"crypto/rsa"
"crypto/x509"
"fmt"
"io/ioutil"
"math/rand"
"github.com/beevik/etree"
xades "github.com/digitalautonomy/goxades_sri"
dsig "github.com/russellhaering/goxmldsig"
"golang.org/x/crypto/pkcs12"
)
func (s *signer) readCertificateAndKey(certPath string) *xades.MemoryX509KeyStore {
p12, _ := ioutil.ReadFile(certPath)
blocks, _ := pkcs12.ToPEM(p12, "")
var endUserKey *rsa.PrivateKey
var endUserCert *x509.Certificate
var endUserCertBytes []byte
var certChain []*x509.Certificate
for _, b := range blocks {
if b.Type == "CERTIFICATE" {
cert, err := x509.ParseCertificate(b.Bytes)
err = err
if cert.IsCA {
certChain = append(certChain, cert)
} else {
endUserCert = cert
endUserCertBytes = b.Bytes
}
} else if b.Type == "PRIVATE KEY" {
key, err := x509.ParsePKCS1PrivateKey(b.Bytes)
err = err
endUserKey = key
}
}
certChain = certChain
return &xades.MemoryX509KeyStore{
PrivateKey: endUserKey,
Cert: endUserCert,
CertBinary: endUserCertBytes,
}
}
func (s *signer) canonicalSerialize(el *etree.Element) ([]byte, error) {
doc := etree.NewDocument()
doc.SetRoot(el.Copy())
doc.WriteSettings = etree.WriteSettings{
CanonicalAttrVal: true,
CanonicalEndTags: true,
CanonicalText: true,
}
return doc.WriteToBytes()
}
const randomIdentifierSize = 1000000
func (s *signer) addRandomIdentifiersFrom(intn func(int) int) func(*xades.SigningContext) {
r := func() int {
return intn(randomIdentifierSize)
}
return func(ctx *xades.SigningContext) {
ctx.SignatureId = fmt.Sprintf("Signature%06d", r())
ctx.SignedInfoId = fmt.Sprintf("Signature-SignedInfo%06d", r())
ctx.SignatureValueId = fmt.Sprintf("SignatureValue%06d", r())
ctx.ObjectId = fmt.Sprintf("%s-Object%06d", ctx.SignatureId, r())
ctx.KeyInfoId = fmt.Sprintf("Certificate%06d", r())
ctx.SignedPropertiesId = fmt.Sprintf("%s-SignedProperties%06d", ctx.SignatureId, r())
ctx.ReferenceMainDocumentId = fmt.Sprintf("Reference-ID-%06d", r())
ctx.ReferencePropertiesId = fmt.Sprintf("SignedPropertiesID%06d", r())
}
}
type signer struct {
addIdentifiersFunc func(*xades.SigningContext)
}
func (s *signer) addIdentifiers(ctx *xades.SigningContext) {
if s.addIdentifiersFunc != nil {
s.addIdentifiersFunc(ctx)
} else {
s.addRandomIdentifiersFrom(rand.Intn)(ctx)
}
}
func (s *signer) signInvoiceWith(certPath, xmlPath string) string {
doc := etree.NewDocument()
doc.ReadFromFile(xmlPath)
keyStore := s.readCertificateAndKey(certPath)
canonicalizer := dsig.MakeC14N10RecCanonicalizer()
signContext := xades.SigningContext{
DataContext: xades.SignedDataContext{
Canonicalizer: canonicalizer,
Hash: crypto.SHA1,
ReferenceURI: "#comprobante",
IsEnveloped: true,
},
PropertiesContext: xades.SignedPropertiesContext{
Canonicalizer: canonicalizer,
Hash: crypto.SHA1,
},
Canonicalizer: canonicalizer,
Hash: crypto.SHA1,
KeyStore: *keyStore,
DsigNamespacePrefix: "ds",
EtsiNamespacePrefix: "etsi",
EtsiNamespaceAtTopLevel: true,
IncludeKeyValue: true,
IncludeSignedDataObjectProperties: true,
SignedDataObjectDescription: "contenido comprobante",
ReferenceDataLast: true,
ReferenceCertificate: true,
ReferenceAvoidTransformElements: true,
}
s.addIdentifiers(&signContext)
signature, _ := xades.CreateSignature(doc.Root(), &signContext)
doc.Root().AddChild(signature)
b, _ := s.canonicalSerialize(doc.Root())
return string(b)
}