/
authority.go
163 lines (142 loc) · 4.51 KB
/
authority.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
package authority
import (
"io/ioutil"
"net/http"
"crypto/x509"
"encoding/pem"
"encoding/xml"
"github.com/beevik/etree"
"errors"
"crypto/dsa"
"crypto/ecdsa"
"crypto/rsa"
"crypto/elliptic"
"crypto"
"github.com/bzanni/2DDoc/pkg/spec"
)
const certFirst = "-----BEGIN CERTIFICATE-----"
const certLast = "-----END CERTIFICATE-----"
var cert map[string]map[string]*x509.Certificate = make(map[string]map[string]*x509.Certificate)
var privateKey map[string]map[string]crypto.PrivateKey = make(map[string]map[string]crypto.PrivateKey)
func init() {
setCertificat("FR00", "0001", spec.GetTestCertificat())
setPrivateKey("FR00", "0001", spec.GetTestPrivateKey())
loadANTSAuthorities()
}
func GetPublicKey(authority string, id string) (key interface{}, err error) {
cert, err := getCertificat(authority, id)
if(err != nil) {
return nil, errors.New(
"Unknown certificat for certificationAuthorityId: "+authority + " and certificatId: "+id)
}
return cert.PublicKey, nil
}
func GetPrivateKey(authority string, id string) (key *ecdsa.PrivateKey, err error) {
if auth, ok := privateKey[authority]; ok {
if key, ok := auth[id]; ok {
return key.(*ecdsa.PrivateKey), nil
}
}
return nil, errors.New(
"Unknown private key for certificationAuthorityId: "+authority + " and certificatId: "+id)
}
func GetCertificateProperty(authority string, id string) (hash string, length int, err error) {
cert, err := getCertificat(authority, id)
if(err != nil) {
return "", 0, err
}
// fmt.Println("pubkey alg", cert.PublicKeyAlgorithm, cert.SignatureAlgorithm)
var signatureLength int
var signatureHash string
switch pub := cert.PublicKey.(type) {
case *rsa.PublicKey:
// fmt.Println("pub is of type RSA:", pub)
signatureLength = 103
signatureHash = "SHA-256"
case *dsa.PublicKey:
// fmt.Println("pub is of type DSA:", pub)
signatureLength = 103
signatureHash = "SHA-256"
case *ecdsa.PublicKey:
if pub.Curve == elliptic.P256() {
signatureLength = 103
signatureHash = "SHA-256"
} else if pub.Curve == elliptic.P224() {
signatureLength = 154
signatureHash = "SHA-384"
} else if pub.Curve == elliptic.P384() {
signatureLength = 154
signatureHash = "SHA-384"
} else if pub.Curve == elliptic.P521() {
signatureLength = 212
signatureHash = "SHA-512"
}
default:
return "", 0, errors.New(
"Unknown PublicKey type: "+authority + " and certificatId: "+id)
}
// fmt.Println(signatureHash, signatureLength)
return signatureHash, signatureLength, nil
}
func loadANTSAuthorities() {
resp, err := http.Get("https://ants.gouv.fr/content/download/517/5670/version/18/file/ANTS_2D-Doc_TSL_v12_sign.xml")
if err != nil {
// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
doc := etree.NewDocument()
err = doc.ReadFromString(string(body))
if err != nil {
panic(err)
}
var trustServiceStatusList TrustServiceStatusList
xml.Unmarshal(body, &trustServiceStatusList)
for _, provider := range trustServiceStatusList.TrustServiceProviderList.TrustServiceProvider {
for _, service := range provider.TSPServices.TSPService {
cert := certFirst;
for i, c := range service.ServiceInformation.ServiceDigitalIdentity.DigitalId.X509Certificate {
if i%76 == 0 {
cert += "\n"
}
cert += string(c)
}
cert += "\n"
cert += certLast
block, _ := pem.Decode([]byte(cert))
x509Cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
panic(err)
}
runes := []rune(x509Cert.Subject.OrganizationalUnit[len(x509Cert.Subject.OrganizationalUnit)-1])
setCertificat(x509Cert.Subject.CommonName, string(runes[0:4]), x509Cert)
}
}
}
func getCertificat(authority string, id string) (certificate *x509.Certificate, err error) {
if auth, ok := cert[authority]; ok {
if certificat, ok := auth[id]; ok {
return certificat, nil
}
}
return nil, errors.New(
"Unknown certificat for certificationAuthorityId: "+authority + " and certificatId: "+id)
}
func setCertificat(authority string, id string, certificat *x509.Certificate) {
if auth, ok := cert[authority]; !ok {
auth = make(map[string]*x509.Certificate)
auth[id] = certificat
cert[authority] = auth
} else {
cert[authority][id] = certificat
}
}
func setPrivateKey(authority string, id string, key crypto.PrivateKey) {
if auth, ok := privateKey[authority]; !ok {
auth = make(map[string]crypto.PrivateKey)
auth[id] = key
privateKey[authority] = auth
} else {
privateKey[authority][id] = key
}
}