-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
189 lines (157 loc) · 4.31 KB
/
client_test.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
package globalsign
import (
"context"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/pem"
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/unidoc/timestamp"
"golang.org/x/crypto/ocsp"
)
func TestSigning(t *testing.T) {
apiKey := os.Getenv("GLOBALSIGN_DSS_API_KEY")
apiSecret := os.Getenv("GLOBALSIGN_DSS_API_SECRET")
apiBaseURL := "https://emea.api.dss.globalsign.com:8443/v2"
certPath := os.Getenv("GLOBALSIGN_DSS_CERT_PATH")
keyPath := os.Getenv("GLOBALSIGN_DSS_KEY_PATH")
t.Logf("Login with API key: %s and API Secret: %s", apiKey, apiSecret)
t.Logf("Cert path: %s and key path: %s", certPath, keyPath)
httpC, err := NewHTTPClientWithCertificate(certPath, keyPath)
if err != nil {
t.Error(err)
t.FailNow()
}
c, err := New(httpC, SetBaseURL(apiBaseURL))
if err != nil {
t.Error(err)
t.FailNow()
}
resp, httpResp, err := c.LoginService.Login(context.Background(), &LoginRequest{APIKey: apiKey, APISecret: apiSecret})
if err != nil {
t.Error("Login() failed with code:", httpResp.StatusCode)
t.FailNow()
}
t.Logf("Access Token: %s", resp.AccessToken)
c.SetAuthToken(resp.AccessToken)
// get identity
identity, httpResp, err := c.DigitalSigningService.Identity(context.Background(), &IdentityRequest{
SubjectDn: map[string]interface{}{
"common_name": "Galih Rivanto",
},
})
if err != nil {
t.Error(err)
t.Error("Identity() failed with code:", httpResp.StatusCode)
t.FailNow()
}
t.Logf("ID: %s", identity.ID)
t.Logf("Signing Cert: %s", identity.SigningCert)
t.Logf("OCSP Resp: %s", identity.OCSPResponse)
// get certificate path
cert, httpResp, err := c.DigitalSigningService.CertificatePath(context.Background())
if err != nil {
t.Error(err)
t.Error("Certicate() failed with code:", httpResp.StatusCode)
t.FailNow()
}
t.Logf("CA: %s", cert.CA)
// create certificate chain
// from signing and ca cert
var certChain []*x509.Certificate
issuerCertData := []byte(identity.SigningCert)
for len(issuerCertData) != 0 {
var block *pem.Block
block, issuerCertData = pem.Decode(issuerCertData)
if block == nil {
break
}
issuer, err := x509.ParseCertificate(block.Bytes)
if err != nil {
t.Error(err)
t.FailNow()
}
certChain = append(certChain, issuer)
}
caCertData := []byte(cert.CA)
for len(caCertData) != 0 {
var block *pem.Block
block, caCertData = pem.Decode(caCertData)
if block == nil {
break
}
issuer, err := x509.ParseCertificate(block.Bytes)
if err != nil {
t.Error(err)
t.FailNow()
}
certChain = append(certChain, issuer)
}
ocspDecoded, err := base64.StdEncoding.DecodeString(identity.OCSPResponse)
if err != nil {
t.Error(err)
t.FailNow()
}
ocspResponse, err := ocsp.ParseResponse(ocspDecoded, certChain[1])
if err != nil {
t.Error(err)
t.FailNow()
}
t.Logf("OCSP: %v", ocspResponse)
// mock digest
digest := sha256.Sum256([]byte(fmt.Sprintf("%x", time.Now().Unix())))
// encode to hex
digestHex := strings.ToUpper(hex.EncodeToString(digest[:]))
t.Logf("Digest: %s", digestHex)
// get timestamp
timestampResp, httpResp, err := c.DigitalSigningService.Timestamp(context.Background(), &TimestampRequest{
Digest: digestHex,
})
if err != nil {
t.Error(err)
t.Error("Timestamp() failed with code:", httpResp.StatusCode)
t.FailNow()
}
t.Logf("Timestamp: %s", timestampResp.Token)
decodedTs, err := base64.StdEncoding.DecodeString(timestampResp.Token)
if err != nil {
t.Error(err)
t.FailNow()
}
t.Logf("Timestamp: %s", string(decodedTs))
tsResp, err := timestamp.Parse(decodedTs)
if err != nil {
t.Error(err)
t.FailNow()
}
t.Logf("Timestamp Token: %v", tsResp)
// get signature
signature, httpResp, err := c.DigitalSigningService.Sign(context.Background(), &SigningRequest{
ID: identity.ID,
Digest: digestHex,
})
if err != nil {
t.Error(err)
t.Error("Sign() failed with code:", httpResp.StatusCode)
t.FailNow()
}
t.Logf("Signature: %s", signature.Signature)
signatureHash, err := hex.DecodeString(signature.Signature)
if err != nil {
t.Error(err)
t.FailNow()
}
t.Logf("Signature: %s", string(signatureHash))
trustChain, _, err := c.DigitalSigningService.TrustChain(context.Background())
if err != nil {
t.Error(err)
t.Error("TrusChain() failed with code:", httpResp.StatusCode)
t.FailNow()
}
t.Logf("Trust Chain: %v", trustChain.Path)
}