This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
api_kaetzchen.go
140 lines (118 loc) · 3.77 KB
/
api_kaetzchen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// api_kaetzchen.go - Katzenpost mailproxy Kaetzchen API.
// Copyright (C) 2018 Yawning Angel.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package mailproxy
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"strings"
"github.com/katzenpost/core/crypto/ecdh"
"github.com/katzenpost/core/thwack"
"github.com/ugorji/go/codec"
)
const (
keyserverService = "keyserver"
keyserverVersion = 0
keyserverStatusOk = 0
keyserverStatusSyntaxError = 1
keyserverStatusNoIdentity = 2
)
var (
// ErrKeyserverSyntaxError is the error returned when the keyserver
// rejects a query due to a malformed request.
ErrKeyserverSyntaxError = errors.New("keyserver: syntax error")
// ErrNoIdentity is the error returned when the keyserver fails to
// find the requested user, or the user's public key.
ErrNoIdentity = errors.New("keyserver: user or public key not found")
jsonHandle codec.JsonHandle
)
type keyserverRequest struct {
Version int
User string
}
type keyserverResponse struct {
Version int
StatusCode int
User string
PublicKey string
}
// QueryKeyFromProvider enqueues a keyserver lookup from the sender for the
// specified recipient and returns the message identifier tag.
func (p *Proxy) QueryKeyFromProvider(senderID, recipientID string) ([]byte, error) {
acc, _, err := p.getAccount(senderID)
if err != nil {
return nil, err
}
defer acc.Deref()
_, user, provider, err := p.recipients.Normalize(recipientID)
if err != nil {
return nil, err
}
var req = keyserverRequest{
Version: keyserverVersion,
User: user,
}
var out []byte
enc := codec.NewEncoderBytes(&out, &jsonHandle)
enc.Encode(req)
return p.SendKaetzchenRequest(senderID, keyserverService, provider, out, true)
}
// ParseKeyQueryResponse parses a response obtained from a key server query.
func (p *Proxy) ParseKeyQueryResponse(payload []byte) (string, *ecdh.PublicKey, error) {
var resp keyserverResponse
dec := codec.NewDecoderBytes(bytes.TrimRight(payload, "\x00"), &jsonHandle)
if err := dec.Decode(&resp); err != nil {
return "", nil, err
}
if err := keyserverStatusCodeToErr(resp.StatusCode); err != nil {
return "", nil, err
}
pubKey := new(ecdh.PublicKey)
if err := pubKey.FromString(resp.PublicKey); err != nil {
return "", nil, err
}
return resp.User, pubKey, nil
}
func (p *Proxy) onQueryRecipient(c *thwack.Conn, l string) error {
sp := strings.Split(l, " ")
if len(sp) != 3 {
c.Log().Debugf("QUERY_RECIPIENT invalid syntax: '%v'", l)
return c.WriteReply(thwack.StatusSyntaxError)
}
tag, err := p.QueryKeyFromProvider(sp[1], sp[2])
if err != nil {
c.Log().Debugf("QUERY_RECIPIENT failed to enqueue: %v", err)
return c.WriteReply(thwack.StatusTransactionFailed)
}
return c.Writer().PrintfLine("%v %v", thwack.StatusOk, hex.EncodeToString(tag))
}
func keyserverStatusCodeToErr(statusCode int) error {
switch statusCode {
case keyserverStatusOk:
return nil
case keyserverStatusSyntaxError:
return ErrKeyserverSyntaxError
case keyserverStatusNoIdentity:
return ErrNoIdentity
default:
return fmt.Errorf("keyserver: unknown status code: %v", statusCode)
}
}
func init() {
jsonHandle.Canonical = true
jsonHandle.ErrorIfNoField = true
}