/
oniondesc.go
214 lines (191 loc) · 5.89 KB
/
oniondesc.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// oniondesc.go - deal with onion service descriptors
//
// To the extent possible under law, Ivan Markin waived all copyright
// and related or neighboring rights to this module of onionutil, using the creative
// commons "cc0" public domain dedication. See LICENSE or
// <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
package onionutil
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"encoding/binary"
"encoding/pem"
"errors"
"fmt"
"log"
"strconv"
"strings"
"time"
"github.com/nogoegst/onionutil/pkcs1"
"github.com/nogoegst/onionutil/torparse"
)
type OnionDescriptor struct {
DescID []byte
Version int
PermanentKey *rsa.PublicKey
SecretIDPart []byte
PublicationTime time.Time
ProtocolVersions []int
IntropointsBlock []byte
Signature []byte
Replica int
}
var (
MinReplica = 0
MaxReplica = 1
DescVersion = 2
ProtocolVersions = []int{2, 3}
)
// Initialize defaults
func (desc *OnionDescriptor) InitDefaults() {
desc.Version = DescVersion
desc.ProtocolVersions = ProtocolVersions
}
// Finalize descriptor to sign.
func (desc *OnionDescriptor) Finalize(now time.Time) error {
nowunix := now.Unix()
desc.PublicationTime = time.Unix(nowunix-nowunix%(60*60), 0)
permID, err := CalcPermanentID(desc.PermanentKey)
if err != nil {
return err
}
desc.SecretIDPart = CalcSecretID(permID, now, byte(desc.Replica))
desc.DescID = CalcDescriptorID(permID, desc.SecretIDPart)
return nil
}
// TODO return a pointer to descs not descs themselves?
func ParseOnionDescriptors(descsData []byte) (descs []OnionDescriptor, rest []byte) {
docs, rest := torparse.ParseTorDocument(descsData)
for _, doc := range docs {
var desc OnionDescriptor
if _, ok := doc["rendezvous-service-descriptor"]; !ok {
log.Printf("Got a document that is not an onion service")
continue
} else {
desc.DescID = doc["rendezvous-service-descriptor"].FJoined()
}
version, err := strconv.ParseInt(string(doc["version"].FJoined()), 10, 0)
if err != nil {
log.Printf("Error parsing descriptor version: %v", err)
continue
}
desc.Version = int(version)
permanentKey, _, err := pkcs1.DecodePublicKeyDER(doc["permanent-key"].FJoined())
if err != nil {
log.Printf("Decoding DER sequence of PulicKey has failed: %v.", err)
continue
}
desc.PermanentKey = permanentKey
desc.IntropointsBlock = doc["introduction-points"].FJoined()
if len(doc["signature"][0]) < 1 {
log.Printf("Empty signature")
continue
}
desc.Signature = doc["signature"].FJoined()
descs = append(descs, desc)
}
return descs, rest
}
func (desc *OnionDescriptor) Bytes() []byte {
w := new(bytes.Buffer)
permPubKeyDER, err := pkcs1.EncodePublicKeyDER(desc.PermanentKey)
if err != nil {
log.Fatalf("Cannot encode public key into DER sequence.")
}
fmt.Fprintf(w, "rendezvous-service-descriptor %s\n", Base32Encode(desc.DescID))
fmt.Fprintf(w, "version %d\n", desc.Version)
fmt.Fprintf(w, "permanent-key\n%s",
pem.EncodeToMemory(&pem.Block{Type: "RSA PUBLIC KEY",
Bytes: permPubKeyDER}))
fmt.Fprintf(w, "secret-id-part %s\n",
Base32Encode(desc.SecretIDPart))
fmt.Fprintf(w, "publication-time %v\n",
desc.PublicationTime.Format("2006-01-02 15:04:05"))
var protoversions []string
for _, v := range desc.ProtocolVersions {
protoversions = append(protoversions, fmt.Sprintf("%d", v))
}
fmt.Fprintf(w, "protocol-versions %v\n",
strings.Join(protoversions, ","))
if len(desc.IntropointsBlock) > 0 {
pemIntroBlock := &pem.Block{Type: "MESSAGE", Bytes: []byte(desc.IntropointsBlock)}
fmt.Fprintf(w, "introduction-points\n%s", pem.EncodeToMemory(pemIntroBlock))
}
fmt.Fprintf(w, "signature\n")
if len(desc.Signature) > 0 {
pemSignature := pem.EncodeToMemory(&pem.Block{Type: "SIGNATURE", Bytes: desc.Signature})
fmt.Fprintf(w, "%s", pemSignature)
}
return w.Bytes()
}
func (desc *OnionDescriptor) OnionID() (string, error) {
permID, err := CalcPermanentID(desc.PermanentKey)
if err != nil {
return "", fmt.Errorf("Error in calculating permanent id: %v", err)
}
onionID := Base32Encode(permID)
return onionID, nil
}
func (desc *OnionDescriptor) Sign(signer crypto.Signer) error {
descDigest := Hash(desc.Bytes())
signature, err := signer.Sign(rand.Reader, descDigest, crypto.Hash(0))
if err != nil {
return err
}
desc.Signature = signature
return nil
}
func (desc *OnionDescriptor) VerifySignature() error {
signature := desc.Signature
desc.Signature = []byte{}
descDigest := Hash(desc.Bytes())
desc.Signature = signature
return rsa.VerifyPKCS1v15(desc.PermanentKey, 0, descDigest, signature)
}
/* TODO: there is no `descriptor-cookie` now (because we need IP list encryption etc) */
func CalcSecretID(permID []byte, now time.Time, replica byte) (secretID []byte) {
permIDByte := uint32(permID[0])
timePeriodInt := (uint32(now.Unix()) + permIDByte*86400/256) / 86400
var timePeriod = new(bytes.Buffer)
binary.Write(timePeriod, binary.BigEndian, timePeriodInt)
h := sha1.New()
h.Write(timePeriod.Bytes())
h.Write([]byte{replica})
secretID = h.Sum(nil)
return secretID
}
func CalcDescriptorID(permID, secretID []byte) (descID []byte) {
h := sha1.New()
h.Write(permID)
h.Write(secretID)
descID = h.Sum(nil)
return descID
}
func CalcDescIDByOnion(onion string, t time.Time, replica int) (string, error) {
permID, err := Base32Decode(onion)
if err != nil {
return "", err
}
secretID := CalcSecretID(permID, t, byte(replica))
descID := CalcDescriptorID(permID, secretID)
return Base32Encode(descID), nil
}
func (desc *OnionDescriptor) FullSign(signer crypto.Signer) error {
var ok bool
desc.PermanentKey, ok = signer.Public().(*rsa.PublicKey)
if !ok {
return errors.New("signer is not RSA")
}
err := desc.Finalize(time.Now())
if err != nil {
return fmt.Errorf("unable to update descriptor: %v", err)
}
err = desc.Sign(signer)
if err != nil {
return fmt.Errorf("unable to sign descriptor: %v", err)
}
return nil
}