timestamp.go 3.86 KB
Newer Older
1 2 3 4 5
package irma

import (
	"crypto/sha256"
	"encoding/asn1"
6
	gobig "math/big"
7 8

	"github.com/bwesterb/go-atum"
9
	"github.com/go-errors/errors"
10 11
	"github.com/privacybydesign/gabi"
	"github.com/privacybydesign/gabi/big"
12 13 14 15 16
)

// GetTimestamp GETs a signed timestamp (a signature over the current time and the parameters)
// over the message to be signed, the randomized signatures over the attributes, and the disclosed
// attributes, for in attribute-based signature sessions.
17 18
func GetTimestamp(message string, sigs []*big.Int, disclosed [][]*big.Int, conf *Configuration) (*atum.Timestamp, error) {
	nonce, err := TimestampRequest(message, sigs, disclosed, true, conf)
19 20 21 22 23 24 25 26 27 28 29 30 31
	if err != nil {
		return nil, err
	}
	alg := atum.Ed25519
	return atum.SendRequest(TimestampServerURL, atum.Request{
		Nonce:           nonce,
		PreferredSigAlg: &alg,
	})
}

// TimestampRequest computes the nonce to be signed by a timestamp server, given a message to be signed
// in an attribute-based signature session along with the randomized signatures over the attributes
// and the disclosed attributes.
32
func TimestampRequest(message string, sigs []*big.Int, disclosed [][]*big.Int, new bool, conf *Configuration) ([]byte, error) {
33 34
	msgHash := sha256.Sum256([]byte(message))

35 36 37 38 39
	// Convert the sigs and disclosed (double) slices to (double) slices of gobig.Int's for asn1
	sigsint := make([]*gobig.Int, len(sigs))
	for i, k := range sigs {
		sigsint[i] = k.Value()
	}
40 41 42 43

	disclosedint := make([][]*gobig.Int, len(disclosed))
	dlreps := make([]*gobig.Int, len(disclosed))
	var d interface{} = disclosedint
44
	for i, _ := range disclosed {
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
		if !new {
			disclosedint[i] = make([]*gobig.Int, len(disclosed[i]))
			for j, k := range disclosed[i] {
				disclosedint[i][j] = k.Value()
			}
		} else {
			if len(disclosed[i]) < 2 || disclosed[i][1].Cmp(bigZero) == 0 {
				return nil, errors.Errorf("metadata attribute of credential %d not disclosed", i)
			}
			meta := MetadataFromInt(disclosed[i][1], conf)
			pk, err := conf.PublicKey(meta.CredentialType().IssuerIdentifier(), meta.KeyCounter())
			if err != nil {
				return nil, err
			}
			dlreps[i] = gabi.RepresentToPublicKey(pk, disclosed[i]).Value()
60 61
		}
	}
62 63 64 65
	if new {
		d = dlreps
	}

66
	bts, err := asn1.Marshal(struct {
67
		Sigs      []*gobig.Int
68
		MsgHash   []byte
69
		Disclosed interface{}
70
	}{
71
		sigsint, msgHash[:], d,
72 73 74 75 76 77 78 79 80 81 82
	})
	if err != nil {
		return nil, err
	}

	hashed := sha256.Sum256(bts)
	return hashed[:], nil
}

const TimestampServerURL = "https://metrics.privacybydesign.foundation/atum"

83
// Given an SignedMessage, verify the timestamp over the signed message, disclosed attributes,
84
// and rerandomized CL-signatures.
85 86
func (sm *SignedMessage) VerifyTimestamp(message string, conf *Configuration) error {
	if sm.Timestamp.ServerUrl != TimestampServerURL {
87 88 89 90 91 92
		return errors.New("Untrusted timestamp server")
	}

	// Extract the disclosed attributes and randomized CL-signatures from the proofs in order to
	// construct the nonce that should be signed by the timestamp server.
	zero := big.NewInt(0)
93
	size := len(sm.Signature)
94 95
	sigs := make([]*big.Int, size)
	disclosed := make([][]*big.Int, size)
96
	for i, proof := range sm.Signature {
97 98
		proofd := proof.(*gabi.ProofD)
		sigs[i] = proofd.A
99 100 101 102
		ct := MetadataFromInt(proofd.ADisclosed[1], conf).CredentialType()
		if ct == nil {
			return errors.New("Cannot verify timestamp: signature contains attributes from unknown credential type")
		}
103
		attrcount := len(ct.AttributeTypes) + 2 // plus secret key and metadata
104 105 106 107 108 109 110 111 112 113 114
		disclosed[i] = make([]*big.Int, attrcount)
		for j := 0; j < attrcount; j++ {
			val, ok := proofd.ADisclosed[j]
			if !ok {
				disclosed[i][j] = zero
			} else {
				disclosed[i][j] = val
			}
		}
	}

115
	bts, err := TimestampRequest(message, sigs, disclosed, sm.Version() >= 2, conf)
116 117 118
	if err != nil {
		return err
	}
119
	valid, err := sm.Timestamp.Verify(bts)
120 121 122 123 124 125 126 127
	if err != nil {
		return err
	}
	if !valid {
		return errors.New("Timestamp signature invalid")
	}
	return nil
}