@@ -17,6 +17,7 @@ package scti
17
17
import (
18
18
"bytes"
19
19
"crypto"
20
+ "crypto/ecdsa"
20
21
"crypto/sha256"
21
22
"crypto/x509"
22
23
"encoding/hex"
@@ -401,3 +402,56 @@ func setupSigner(fakeSig []byte) (crypto.Signer, error) {
401
402
402
403
return testdata .NewSignerWithFixedSig (key , fakeSig ), nil
403
404
}
405
+
406
+ func TestBuildCp (t * testing.T ) {
407
+ // Create a test signer.
408
+ ecdsaSigner , err := loadPEMPrivateKey ("../testdata/test_ct_server_ecdsa_private_key.pem" )
409
+ if err != nil {
410
+ t .Fatalf ("Can't open key: %v" , err )
411
+ }
412
+
413
+ // Define test data.
414
+ size := uint64 (12345 )
415
+ hash := []byte ("test_hash_value_12345678901234567890" )
416
+
417
+ // Build the checkpoint which is in the RFC6962NoteSignature format.
418
+ checkpoint , err := buildCp (ecdsaSigner , size , fixedTimeMillis , hash )
419
+ if err != nil {
420
+ t .Errorf ("buildCp failed: %v" , err )
421
+ }
422
+
423
+ // Verify whether the checkpoint is empty.
424
+ if len (checkpoint ) == 0 {
425
+ t .Errorf ("buildCp returned an empty checkpoint" )
426
+ }
427
+
428
+ // Verify that the checkpoint can be parsed.
429
+ var sig rfc6962NoteSignature
430
+ _ , err = tls .Unmarshal (checkpoint , & sig )
431
+ if err != nil {
432
+ t .Errorf ("failed to unmarshal checkpoint: %v" , err )
433
+ }
434
+ // Verify the timestamp in the note signature.
435
+ if sig .Timestamp != fixedTimeMillis {
436
+ t .Errorf ("buildCp returned wrong timestamp, got %d, want %d" , sig .Timestamp , fixedTimeMillis )
437
+ }
438
+
439
+ // Verify the signature using the public key.
440
+ sth := rfc6962.SignedTreeHead {
441
+ Version : rfc6962 .V1 ,
442
+ TreeSize : size ,
443
+ Timestamp : fixedTimeMillis ,
444
+ }
445
+ copy (sth .SHA256RootHash [:], hash )
446
+
447
+ sthBytes , err := serializeSTHSignatureInput (sth )
448
+ if err != nil {
449
+ t .Fatalf ("serializeSTHSignatureInput(): %v" , err )
450
+ }
451
+
452
+ h := sha256 .Sum256 (sthBytes )
453
+ valid := ecdsa .VerifyASN1 (ecdsaSigner .Public ().(* ecdsa.PublicKey ), h [:], sig .Signature .Signature )
454
+ if ! valid {
455
+ t .Errorf ("buildCp returned an invalid signature" )
456
+ }
457
+ }
0 commit comments