@@ -20,6 +20,7 @@ import (
2020 "crypto/sha256"
2121 "crypto/x509/pkix"
2222 "encoding/asn1"
23+ "encoding/base64"
2324 "encoding/json"
2425 "io"
2526 "math/big"
@@ -267,60 +268,124 @@ func TestGetTimestampResponseWithExtsAndOID(t *testing.T) {
267268 }
268269
269270 for _ , tc := range tests {
270- url := createServer (t )
271+ t .Run (tc .name + " Default Deny" , func (t * testing.T ) {
272+ url := createServer (t )
271273
272- c , err := client .GetTimestampClient (url , client .WithContentType (tc .reqMediaType ))
273- if err != nil {
274- t .Fatalf ("test '%s': unexpected error creating client: %v" , tc . name , err )
275- }
274+ c , err := client .GetTimestampClient (url , client .WithContentType (tc .reqMediaType ))
275+ if err != nil {
276+ t .Fatalf ("unexpected error creating client: %v" , err )
277+ }
276278
277- // populate additional request parameters for extensions and OID - atypical request structure
278- var req * ts.Request
279- if tc .reqMediaType == client .TimestampQueryMediaType {
280- req , err = ts .ParseRequest (tc .reqBytes )
279+ // populate additional request parameters for extensions and OID - atypical request structure
280+ var req * ts.Request
281+ if tc .reqMediaType == client .TimestampQueryMediaType {
282+ req , err = ts .ParseRequest (tc .reqBytes )
283+ if err != nil {
284+ t .Fatalf ("unexpected error parsing request: %v" , err )
285+ }
286+ } else {
287+ var jsonReq api.JSONRequest
288+ if err := json .Unmarshal (tc .reqBytes , & jsonReq ); err != nil {
289+ t .Fatalf ("unexpected error unmarshalling request: %v" , err )
290+ }
291+ decoded , _ := base64 .StdEncoding .DecodeString (jsonReq .ArtifactHash )
292+ req = & ts.Request {
293+ HashAlgorithm : crypto .SHA256 ,
294+ HashedMessage : decoded ,
295+ Certificates : jsonReq .Certificates ,
296+ Nonce : jsonReq .Nonce ,
297+ }
298+ }
299+ req .ExtraExtensions = []pkix.Extension {{Id : asn1.ObjectIdentifier {1 , 2 , 3 , 4 }, Value : []byte {1 , 2 , 3 , 4 }}}
300+ fakePolicyOID := asn1.ObjectIdentifier {1 , 2 , 3 , 4 , 5 }
301+ req .TSAPolicyOID = fakePolicyOID
302+ tsq , err := req .Marshal ()
281303 if err != nil {
282- t .Fatalf ("test '%s': unexpected error parsing request: %v" , tc . name , err )
304+ t .Fatalf ("unexpected error creating request: %v" , err )
283305 }
284- } else {
285- req , _ , err = api .ParseJSONRequest (tc .reqBytes )
306+
307+ params := timestamp .NewGetTimestampResponseParams ()
308+ params .SetTimeout (10 * time .Second )
309+ params .Request = io .NopCloser (bytes .NewReader (tsq ))
310+
311+ var respBytes bytes.Buffer
312+ clientOption := func (op * runtime.ClientOperation ) {
313+ op .ConsumesMediaTypes = []string {client .TimestampQueryMediaType }
314+ }
315+ _ , _ , err = c .Timestamp .GetTimestampResponse (params , & respBytes , clientOption )
316+ if err == nil {
317+ t .Fatalf ("expected error when requesting unaccepted policy/extensions" )
318+ }
319+ if ! strings .Contains (err .Error (), "unaccepted policy" ) {
320+ t .Fatalf ("expected unaccepted policy error, got: %v" , err )
321+ }
322+ })
323+
324+ t .Run (tc .name + " Allowed via Config" , func (t * testing.T ) {
325+ url := createServer (t , func () {
326+ viper .Set ("accepted-policy-oids" , []string {"1.3.6.1.4.1.57264.2" , "1.2.3.4.5" })
327+ viper .Set ("allow-custom-extensions" , true )
328+ })
329+
330+ c , err := client .GetTimestampClient (url , client .WithContentType (tc .reqMediaType ))
286331 if err != nil {
287- t .Fatalf ("test '%s': unexpected error parsing request : %v" , tc . name , err )
332+ t .Fatalf ("unexpected error creating client : %v" , err )
288333 }
289- }
290- req .ExtraExtensions = []pkix.Extension {{Id : asn1.ObjectIdentifier {1 , 2 , 3 , 4 }, Value : []byte {1 , 2 , 3 , 4 }}}
291- fakePolicyOID := asn1.ObjectIdentifier {1 , 2 , 3 , 4 , 5 }
292- req .TSAPolicyOID = fakePolicyOID
293- tsq , err := req .Marshal ()
294- if err != nil {
295- t .Fatalf ("test '%s': unexpected error creating request: %v" , tc .name , err )
296- }
297334
298- params := timestamp .NewGetTimestampResponseParams ()
299- params .SetTimeout (10 * time .Second )
300- params .Request = io .NopCloser (bytes .NewReader (tsq ))
335+ var req * ts.Request
336+ if tc .reqMediaType == client .TimestampQueryMediaType {
337+ req , err = ts .ParseRequest (tc .reqBytes )
338+ if err != nil {
339+ t .Fatalf ("unexpected error parsing request: %v" , err )
340+ }
341+ } else {
342+ var jsonReq api.JSONRequest
343+ if err := json .Unmarshal (tc .reqBytes , & jsonReq ); err != nil {
344+ t .Fatalf ("unexpected error unmarshalling request: %v" , err )
345+ }
346+ decoded , _ := base64 .StdEncoding .DecodeString (jsonReq .ArtifactHash )
347+ req = & ts.Request {
348+ HashAlgorithm : crypto .SHA256 ,
349+ HashedMessage : decoded ,
350+ Certificates : jsonReq .Certificates ,
351+ Nonce : jsonReq .Nonce ,
352+ }
353+ }
354+ req .ExtraExtensions = []pkix.Extension {{Id : asn1.ObjectIdentifier {1 , 2 , 3 , 4 }, Value : []byte {1 , 2 , 3 , 4 }}}
355+ fakePolicyOID := asn1.ObjectIdentifier {1 , 2 , 3 , 4 , 5 }
356+ req .TSAPolicyOID = fakePolicyOID
357+ tsq , err := req .Marshal ()
358+ if err != nil {
359+ t .Fatalf ("unexpected error creating request: %v" , err )
360+ }
301361
302- var respBytes bytes.Buffer
303- clientOption := func (op * runtime.ClientOperation ) {
304- op .ConsumesMediaTypes = []string {client .TimestampQueryMediaType }
305- }
306- _ , _ , err = c .Timestamp .GetTimestampResponse (params , & respBytes , clientOption )
307- if err != nil {
308- t .Fatalf ("test '%s': unexpected error getting timestamp response: %v" , tc .name , err )
309- }
362+ params := timestamp .NewGetTimestampResponseParams ()
363+ params .SetTimeout (10 * time .Second )
364+ params .Request = io .NopCloser (bytes .NewReader (tsq ))
310365
311- tsr , err := ts .ParseResponse (respBytes .Bytes ())
312- if err != nil {
313- t .Fatalf ("test '%s': unexpected error parsing response: %v" , tc .name , err )
314- }
366+ var respBytes bytes.Buffer
367+ clientOption := func (op * runtime.ClientOperation ) {
368+ op .ConsumesMediaTypes = []string {client .TimestampQueryMediaType }
369+ }
370+ _ , _ , err = c .Timestamp .GetTimestampResponse (params , & respBytes , clientOption )
371+ if err != nil {
372+ t .Fatalf ("unexpected error getting timestamp response: %v" , err )
373+ }
315374
316- // check policy OID
317- if ! tsr .Policy .Equal (fakePolicyOID ) {
318- t .Fatalf ("test '%s': unexpected policy ID" , tc .name )
319- }
320- // check extension is present
321- if len (tsr .Extensions ) != 1 {
322- t .Fatalf ("test '%s': expected 1 extension, got %d" , tc .name , len (tsr .Extensions ))
323- }
375+ tsr , err := ts .ParseResponse (respBytes .Bytes ())
376+ if err != nil {
377+ t .Fatalf ("unexpected error parsing response: %v" , err )
378+ }
379+
380+ // check policy OID
381+ if ! tsr .Policy .Equal (fakePolicyOID ) {
382+ t .Fatalf ("unexpected policy ID" )
383+ }
384+ // check extension is present
385+ if len (tsr .Extensions ) != 1 {
386+ t .Fatalf ("expected 1 extension, got %d" , len (tsr .Extensions ))
387+ }
388+ })
324389 }
325390}
326391
@@ -329,7 +394,7 @@ func TestGetTimestampResponseWithNoCertificateOrNonce(t *testing.T) {
329394 includeCerts := false
330395 hashFunc := crypto .SHA256
331396 hashName := "sha256"
332- oidStr := "1.2.3.4 "
397+ oidStr := "1.3.6.1.4.1.57264.2 "
333398
334399 opts := ts.RequestOptions {
335400 Certificates : includeCerts ,
0 commit comments