146 lines
5.5 KiB
Go
146 lines
5.5 KiB
Go
package identity
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
)
|
|
|
|
func TestCandidateViewPseudonymKeyIsPurposeDerived(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
secret := bytes.Repeat([]byte("k"), MinKeySecretBytes)
|
|
keyring, err := NewKeyring(secret)
|
|
if err != nil {
|
|
t.Fatalf("NewKeyring() error = %v", err)
|
|
}
|
|
key, err := keyring.CandidateViewPseudonymKey()
|
|
if err != nil {
|
|
t.Fatalf("CandidateViewPseudonymKey() error = %v", err)
|
|
}
|
|
|
|
candidateID := uuid.New()
|
|
organizationID := uuid.New()
|
|
first, err := key.ForOrganization(organizationID, candidateID)
|
|
if err != nil {
|
|
t.Fatalf("ForOrganization() error = %v", err)
|
|
}
|
|
preview, err := key.ForCandidatePreview(candidateID)
|
|
if err != nil {
|
|
t.Fatalf("ForCandidatePreview() error = %v", err)
|
|
}
|
|
repeated, err := keyring.CandidateViewPseudonymKey()
|
|
if err != nil {
|
|
t.Fatalf("CandidateViewPseudonymKey() repeat error = %v", err)
|
|
}
|
|
previewAgain, err := repeated.ForCandidatePreview(candidateID)
|
|
if err != nil {
|
|
t.Fatalf("ForCandidatePreview() repeat error = %v", err)
|
|
}
|
|
|
|
if preview == first || previewAgain != preview {
|
|
t.Fatal("preview pseudonym must be stable and separate from organization pseudonyms")
|
|
}
|
|
if bytes.Contains([]byte(preview), secret) {
|
|
t.Fatal("preview pseudonym exposed the root secret")
|
|
}
|
|
}
|
|
|
|
func TestCandidateViewPseudonymKeyRequiresValidKeyring(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if _, err := (*Keyring)(nil).CandidateViewPseudonymKey(); err == nil {
|
|
t.Fatal("CandidateViewPseudonymKey() accepted a nil keyring")
|
|
}
|
|
}
|
|
|
|
func TestAssessmentAnswerFingerprintIsKeyedDeterministicAndScoped(t *testing.T) {
|
|
keyring, err := NewKeyring(bytes.Repeat([]byte{1}, MinKeySecretBytes))
|
|
if err != nil {
|
|
t.Fatalf("NewKeyring() error = %v", err)
|
|
}
|
|
other, err := NewKeyring(bytes.Repeat([]byte{2}, MinKeySecretBytes))
|
|
if err != nil {
|
|
t.Fatalf("NewKeyring(other) error = %v", err)
|
|
}
|
|
family, otherFamily := uuid.New(), uuid.New()
|
|
answer := []byte(`{"format":"number","number":42}`)
|
|
first := keyring.AssessmentAnswerFingerprint(family, 1, answer)
|
|
if first.IsZero() || first != keyring.AssessmentAnswerFingerprint(family, 1, answer) {
|
|
t.Fatal("the same family version and answer must give the same non-zero fingerprint")
|
|
}
|
|
for name, different := range map[string]domain.AssessmentDigest{
|
|
"another answer": keyring.AssessmentAnswerFingerprint(family, 1, []byte(`{"format":"number","number":43}`)),
|
|
"another family": keyring.AssessmentAnswerFingerprint(otherFamily, 1, answer),
|
|
"another family version": keyring.AssessmentAnswerFingerprint(family, 2, answer),
|
|
"another secret": other.AssessmentAnswerFingerprint(family, 1, answer),
|
|
} {
|
|
if different == first {
|
|
t.Errorf("%s gave the same fingerprint", name)
|
|
}
|
|
}
|
|
plain := sha256.Sum256(append(append(family[:], 0, 0, 0, 1), answer...))
|
|
if first == domain.AssessmentDigest(plain) {
|
|
t.Fatal("the fingerprint is a plain hash and can be checked without the server secret")
|
|
}
|
|
if keyring.AssessmentDigestKeyVersion() < 1 {
|
|
t.Fatal("the digest key version must be positive")
|
|
}
|
|
}
|
|
|
|
func TestAssessmentSessionAndNetworkDigestsAreBoundToTheAttempt(t *testing.T) {
|
|
keyring, err := NewKeyring(bytes.Repeat([]byte{1}, MinKeySecretBytes))
|
|
if err != nil {
|
|
t.Fatalf("NewKeyring() error = %v", err)
|
|
}
|
|
attempt, otherAttempt, session, otherSession := uuid.New(), uuid.New(), uuid.New(), uuid.New()
|
|
sessionDigest := keyring.AssessmentSessionDigest(attempt, session)
|
|
if sessionDigest != keyring.AssessmentSessionDigest(attempt, session) {
|
|
t.Fatal("the session digest is not deterministic")
|
|
}
|
|
if sessionDigest == keyring.AssessmentSessionDigest(otherAttempt, session) {
|
|
t.Fatal("one session has the same digest in two attempts, so attempts could be linked")
|
|
}
|
|
if sessionDigest == keyring.AssessmentSessionDigest(attempt, otherSession) {
|
|
t.Fatal("two sessions of one attempt have the same digest")
|
|
}
|
|
networkDigest := keyring.AssessmentNetworkDigest(attempt, "198.51.100.0")
|
|
if networkDigest != keyring.AssessmentNetworkDigest(attempt, "198.51.100.0") {
|
|
t.Fatal("the network digest is not deterministic")
|
|
}
|
|
if networkDigest == keyring.AssessmentNetworkDigest(otherAttempt, "198.51.100.0") {
|
|
t.Fatal("one network has the same digest in two attempts, so attempts could be linked")
|
|
}
|
|
if networkDigest == keyring.AssessmentNetworkDigest(attempt, "198.51.100.1") {
|
|
t.Fatal("two client networks have the same digest")
|
|
}
|
|
// Each purpose has its own key: the same bytes under another purpose differ.
|
|
if sessionDigest == keyring.AssessmentNetworkDigest(attempt, string(session[:])) {
|
|
t.Fatal("session and network digests share a key")
|
|
}
|
|
}
|
|
|
|
func TestResultHistoryCursorKeyIsSeparateFromIdempotencyAndBoundToTheCandidate(t *testing.T) {
|
|
keyring, err := NewKeyring([]byte(strings.Repeat("k", MinKeySecretBytes)))
|
|
if err != nil {
|
|
t.Fatalf("NewKeyring() error = %v", err)
|
|
}
|
|
signature := keyring.ResultHistoryCursorSignature("position", "candidate-a")
|
|
if signature != keyring.ResultHistoryCursorSignature("position", "candidate-a") {
|
|
t.Fatal("the cursor signature is not deterministic")
|
|
}
|
|
if signature == keyring.ResultHistoryCursorSignature("position", "candidate-b") ||
|
|
signature == keyring.ResultHistoryCursorSignature("other", "candidate-a") {
|
|
t.Fatal("the cursor signature does not depend on the position and the candidate")
|
|
}
|
|
if signature == keyring.IdempotencyScope("position", "", "candidate-a") ||
|
|
signature == keyring.IdempotencyScope("assessment.result-history.cursor.v1", "position", "candidate-a") {
|
|
t.Fatal("the cursor signature shares a key with the idempotency scope")
|
|
}
|
|
}
|