@@ -22,6 +22,7 @@ import (
22
22
"encoding/pem"
23
23
"net/http/httptest"
24
24
"path"
25
+ "path/filepath"
25
26
"testing"
26
27
"time"
27
28
@@ -49,23 +50,8 @@ func TestTSAMTLS(t *testing.T) {
49
50
50
51
// Set up TSA server with TLS
51
52
timestampCACert , timestampServerCert , timestampServerKey , timestampClientCert , timestampClientKey := generateMTLSKeys (t , td )
52
- viper .Set ("timestamp-signer" , "memory" )
53
- viper .Set ("timestamp-signer-hash" , "sha256" )
54
- viper .Set ("disable-ntp-monitoring" , true )
55
- viper .Set ("tls-host" , "0.0.0.0" )
56
- viper .Set ("tls-port" , 3000 )
57
- viper .Set ("tls-ca" , timestampCACert )
58
- viper .Set ("tls-key" , timestampServerKey )
59
- viper .Set ("tls-certificate" , timestampServerCert )
60
- tsaAPIServer := tsaserver .NewRestAPIServer ("localhost" , 3000 , []string {"https" }, false , 10 * time .Second , 10 * time .Second )
61
- tsaServer := httptest .NewServer (tsaAPIServer .GetHandler ())
62
- t .Cleanup (tsaServer .Close )
63
- tsaClient , err := tsaclient .GetTimestampClient (tsaServer .URL )
64
- must (err , t )
65
- tsaChain , err := tsaClient .Timestamp .GetTimestampCertChain (nil )
66
- must (err , t )
67
- timestampServerURL := tsaServer .URL + "/api/v1/timestamp"
68
- timestampChainFile := mkfile (tsaChain .Payload , td , t )
53
+ timestampServerURL , timestampChainFile , tsaCleanup := setUpTSAServerWithTLS (t , td , timestampCACert , timestampServerKey , timestampServerCert )
54
+ t .Cleanup (tsaCleanup )
69
55
70
56
ko := options.KeyOpts {
71
57
KeyRef : pemKeyRef ,
@@ -98,6 +84,53 @@ func TestTSAMTLS(t *testing.T) {
98
84
must (verifyCmd .Exec (context .Background (), []string {imgName }), t )
99
85
}
100
86
87
+ func TestSignBlobTSAMTLS (t * testing.T ) {
88
+ td := t .TempDir ()
89
+ blob := time .Now ().Format ("Mon Jan 2 15:04:05 MST 2006" )
90
+ blobPath := mkfile (blob , td , t )
91
+ timestampPath := filepath .Join (td , "timestamp.txt" )
92
+ bundlePath := filepath .Join (td , "cosign.bundle" )
93
+
94
+ _ , privKey , pubKey := keypair (t , td )
95
+
96
+ // Set up TSA server with TLS
97
+ timestampCACert , timestampServerCert , timestampServerKey , timestampClientCert , timestampClientKey := generateMTLSKeys (t , td )
98
+ timestampServerURL , timestampChainFile , tsaCleanup := setUpTSAServerWithTLS (t , td , timestampCACert , timestampServerKey , timestampServerCert )
99
+ t .Cleanup (tsaCleanup )
100
+
101
+ signingKO := options.KeyOpts {
102
+ KeyRef : privKey ,
103
+ PassFunc : passFunc ,
104
+ TSAServerURL : timestampServerURL ,
105
+ TSAClientCACert : timestampCACert ,
106
+ TSAClientCert : timestampClientCert ,
107
+ TSAClientKey : timestampClientKey ,
108
+ TSAServerName : "server.example.com" ,
109
+ RFC3161TimestampPath : timestampPath ,
110
+ BundlePath : bundlePath ,
111
+ }
112
+ sig , err := sign .SignBlobCmd (ro , signingKO , blobPath , true , "" , "" , false )
113
+ must (err , t )
114
+
115
+ verifyKO := options.KeyOpts {
116
+ KeyRef : pubKey ,
117
+ TSACertChainPath : timestampChainFile ,
118
+ RFC3161TimestampPath : timestampPath ,
119
+ BundlePath : bundlePath ,
120
+ }
121
+
122
+ verifyCmd := cliverify.VerifyBlobCmd {
123
+ KeyOpts : verifyKO ,
124
+ SigRef : string (sig ),
125
+ CertVerifyOptions : options.CertVerifyOptions {
126
+ CertIdentityRegexp : ".*" ,
127
+ CertOidcIssuerRegexp : ".*" ,
128
+ },
129
+ IgnoreTlog : true ,
130
+ }
131
+ must (verifyCmd .Exec (context .Background (), blobPath ), t )
132
+ }
133
+
101
134
func generateSigningKeys (t * testing.T , td string ) (string , string , string ) {
102
135
rootCert , rootKey , _ := GenerateRootCa ()
103
136
pemRoot := pem .EncodeToMemory (& pem.Block {Type : "CERTIFICATE" , Bytes : rootCert .Raw })
@@ -141,3 +174,23 @@ func generateMTLSKeys(t *testing.T, td string) (string, string, string, string,
141
174
clientPemKeyRef := mkfile (string (clientKeyPem ), td , t )
142
175
return pemRootRef , serverPemLeafRef , serverPemKeyRef , clientPemLeafRef , clientPemKeyRef
143
176
}
177
+
178
+ func setUpTSAServerWithTLS (t * testing.T , td , timestampCACert , timestampServerKey , timestampServerCert string ) (string , string , func ()) {
179
+ viper .Set ("timestamp-signer" , "memory" )
180
+ viper .Set ("timestamp-signer-hash" , "sha256" )
181
+ viper .Set ("disable-ntp-monitoring" , true )
182
+ viper .Set ("tls-host" , "0.0.0.0" )
183
+ viper .Set ("tls-port" , 3000 )
184
+ viper .Set ("tls-ca" , timestampCACert )
185
+ viper .Set ("tls-key" , timestampServerKey )
186
+ viper .Set ("tls-certificate" , timestampServerCert )
187
+ tsaAPIServer := tsaserver .NewRestAPIServer ("localhost" , 3000 , []string {"https" }, false , 10 * time .Second , 10 * time .Second )
188
+ tsaServer := httptest .NewServer (tsaAPIServer .GetHandler ())
189
+ tsaClient , err := tsaclient .GetTimestampClient (tsaServer .URL )
190
+ must (err , t )
191
+ tsaChain , err := tsaClient .Timestamp .GetTimestampCertChain (nil )
192
+ must (err , t )
193
+ timestampServerURL := tsaServer .URL + "/api/v1/timestamp"
194
+ timestampChainFile := mkfile (tsaChain .Payload , td , t )
195
+ return timestampServerURL , timestampChainFile , tsaServer .Close
196
+ }
0 commit comments