44 "bytes"
55 "context"
66 "crypto/sha256"
7+ _ "embed"
78 "encoding/base64"
89 "encoding/json"
910 "flag"
@@ -27,6 +28,9 @@ const (
2728 defaultKeyserverVRFKey = "vKHX1vKXl7yF0qBiDxCUXWgOHlapMvqFeIBXt7c29iQ="
2829)
2930
31+ //go:embed witness_policy.txt
32+ var defaultWitnessPolicy []byte
33+
3034func main () {
3135 allFlag := flag .Bool ("all" , false , "list all public keys in the transparency log" )
3236 flag .Parse ()
@@ -46,6 +50,7 @@ func main() {
4650 fmt .Fprintf (os .Stderr , " AGE_KEYSERVER_URL Default keyserver URL\n " )
4751 fmt .Fprintf (os .Stderr , " AGE_KEYSERVER_PUBKEY Default keyserver transparency log vkey\n " )
4852 fmt .Fprintf (os .Stderr , " AGE_KEYSERVER_VRFKEY Default keyserver transparency log VRF public key\n " )
53+ fmt .Fprintf (os .Stderr , " AGE_KEYSERVER_POLICY Default keyserver transparency log witness policy\n " )
4954 os .Exit (2 )
5055 }
5156
@@ -67,6 +72,21 @@ func main() {
6772 os .Exit (1 )
6873 }
6974
75+ witnessPolicy := defaultWitnessPolicy
76+ if policyPath := os .Getenv ("AGE_KEYSERVER_POLICY" ); policyPath != "" {
77+ witnessPolicy , err = os .ReadFile (policyPath )
78+ if err != nil {
79+ fmt .Fprintf (os .Stderr , "Error: failed to read witness policy file: %v\n " , err )
80+ os .Exit (1 )
81+ }
82+ }
83+ policy , err := torchwood .ParsePolicy (witnessPolicy )
84+ if err != nil {
85+ fmt .Fprintf (os .Stderr , "Error: invalid witness policy: %v\n " , err )
86+ os .Exit (1 )
87+ }
88+ policy = torchwood .ThresholdPolicy (2 , torchwood .SingleVerifierPolicy (v ), policy )
89+
7090 vrfKeyB64 := os .Getenv ("AGE_KEYSERVER_VRFKEY" )
7191 if vrfKeyB64 == "" {
7292 vrfKeyB64 = defaultKeyserverVRFKey
@@ -86,7 +106,7 @@ func main() {
86106 email = strings .TrimSpace (strings .ToLower (email ))
87107
88108 if * allFlag {
89- pubkeys , err := monitorLog (server , v , vrfKey , email )
109+ pubkeys , err := monitorLog (server , v . Name (), policy , vrfKey , email )
90110 if err != nil {
91111 fmt .Fprintf (os .Stderr , "Error: %v\n " , err )
92112 os .Exit (1 )
@@ -97,7 +117,7 @@ func main() {
97117 return
98118 }
99119
100- pubkey , err := lookupKey (server , v , vrfKey , email )
120+ pubkey , err := lookupKey (server , v . Name (), policy , vrfKey , email )
101121 if err != nil {
102122 fmt .Fprintf (os .Stderr , "Error: %v\n " , err )
103123 os .Exit (1 )
@@ -106,7 +126,7 @@ func main() {
106126 fmt .Println (pubkey )
107127}
108128
109- func lookupKey (serverURL string , v note. Verifier , vrfKey * vrf.PublicKey , email string ) (string , error ) {
129+ func lookupKey (serverURL string , logOrigin string , policy torchwood. Policy , vrfKey * vrf.PublicKey , email string ) (string , error ) {
110130 // Build the lookup URL
111131 lookupURL := serverURL + "/api/lookup?email=" + url .QueryEscape (email )
112132
@@ -168,15 +188,14 @@ func lookupKey(serverURL string, v note.Verifier, vrfKey *vrf.PublicKey, email s
168188 h := sha256 .New ()
169189 h .Write ([]byte (result .Pubkey ))
170190 entry := h .Sum (vrfHash ) // vrf-r255(email) || SHA-256(pubkey)
171- if err := torchwood .VerifyProof (v .Name (), torchwood .SingleVerifierPolicy (v ),
172- tlog .RecordHash (entry ), []byte (result .Proof )); err != nil {
191+ if err := torchwood .VerifyProof (logOrigin , policy , tlog .RecordHash (entry ), []byte (result .Proof )); err != nil {
173192 return "" , fmt .Errorf ("failed to verify key proof: %w" , err )
174193 }
175194
176195 return result .Pubkey , nil
177196}
178197
179- func monitorLog (serverURL string , v note. Verifier , vrfKey * vrf.PublicKey , email string ) ([]string , error ) {
198+ func monitorLog (serverURL string , logOrigin string , policy torchwood. Policy , vrfKey * vrf.PublicKey , email string ) ([]string , error ) {
180199 // Request the VRF proof and history from the monitor endpoint
181200 monitorURL := serverURL + "/api/monitor?email=" + url .QueryEscape (email )
182201 client := & http.Client {
@@ -237,7 +256,7 @@ func monitorLog(serverURL string, v note.Verifier, vrfKey *vrf.PublicKey, email
237256 if err != nil {
238257 return nil , fmt .Errorf ("failed to read checkpoint: %w" , err )
239258 }
240- checkpoint , _ , err := torchwood .VerifyCheckpoint (v . Name (), torchwood . SingleVerifierPolicy ( v ) , signedCheckpoint )
259+ checkpoint , _ , err := torchwood .VerifyCheckpoint (logOrigin , policy , signedCheckpoint )
241260 if err != nil {
242261 return nil , fmt .Errorf ("failed to parse checkpoint: %w" , err )
243262 }
0 commit comments