-
Notifications
You must be signed in to change notification settings - Fork 1
/
PeerID.cpp
277 lines (222 loc) · 6.17 KB
/
PeerID.cpp
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "PeerID.h"
#include "CredentialManager.h"
#include "ConnectionPool.h"
#include "BValue.h"
#include <wx/socket.h>
using namespace RAIN;
/** a PeerID encapsulates everything we'll need to know about a peer -
* - their address
* - their x509 certificate
* - their public key sha1 hash (which is used as a guid across the network)
*/
PeerID::PeerID(bool self)
{
if (self)
{
// we make a peerid representing ourselves but we
// can't know our own address for obvious reasons
this->certificate = NULL;
if (!wxFileExists(CredentialManager::GetCredentialFilename(CL_CERTIFICATE)))
return;
BIO *b = BIO_new_file(CredentialManager::GetCredentialFilename(CL_CERTIFICATE).c_str(), "r");
if (!b)
{
wxLogVerbose("Warning: PeerID init: cannot open client certificate");
return;
}
if (this->certificate = PEM_read_bio_X509(b, NULL, NULL, NULL))
{
this->HashPublicKey();
} else {
wxLogVerbose("Warning: PeerID init: cannot read client certificate");
return;
}
} else {
this->certificate = NULL;
}
}
PeerID::PeerID(BEnc::Value *v)
{
wxLogVerbose("PeerID::PeerID(BEnc::Value)");
this->certificate = NULL;
if (v && v->type == BENC_VAL_DICT)
{
BENC_SAFE_CAST(v, dv, Dictionary);
BENC_SAFE_CAST(dv->getDictValue("hash"), hash, String);
BENC_SAFE_CAST(dv->getDictValue("addr"), addr, String);
BENC_SAFE_CAST(dv->getDictValue("cert"), cert, String);
if (hash && addr && cert)
{
/* full house */
this->hash = hash->getString();
this->address = addr->getString();
wxString certstr = cert->getString();
unsigned char *x509_b = (unsigned char *) certstr.c_str();
this->certificate = d2i_X509(NULL, &x509_b, certstr.Length());
} else if (addr && cert) {
this->address = addr->getString();
wxString certstr = cert->getString();
unsigned char *x509_b = (unsigned char *) certstr.c_str();
this->certificate = d2i_X509(NULL, &x509_b, certstr.Length());
this->HashPublicKey();
} else if (hash) {
this->hash = hash->getString();
}
} else if (v && v->type == BENC_VAL_STRING) {
/* this will be the hash */
BENC_SAFE_CAST(v, hash, String);
this->hash = hash->getString();
}
}
PeerID::PeerID(const wxString& address, X509 *certificate)
{
if (certificate)
{
this->SetCertificate(certificate);
} else {
this->certificate = NULL;
this->hash = wxEmptyString;
}
}
PeerID::PeerID(const wxString& hash, const wxString& address, X509 *certificate)
{
if (certificate)
this->certificate = X509_dup(certificate);
else
this->certificate = NULL;
this->hash = hash;
this->address = address;
}
PeerID::~PeerID()
{
//if (this->certificate)
// X509_free(this->certificate);
}
void PeerID::Merge(PeerID *that)
{
PEERID_WANTS wants = this->GetWants();
if (wants == PEERID_WANTS_ALL)
{
this->SetCertificate(that->certificate);
this->address = that->address;
} else if (wants == PEERID_WANTS_CERT) {
this->SetCertificate(that->certificate);
} else if (wants == PEERID_WANTS_ADDR) {
this->address = that->address;
}
}
void PeerID::HashPublicKey()
{
if (this->certificate == NULL)
return;
// hash our own public key
X509_PUBKEY *pkey = X509_get_X509_PUBKEY(this->certificate);
EVP_PKEY *evp_pkey = X509_PUBKEY_get(pkey);
int len = i2d_PUBKEY(evp_pkey, NULL);
wxString pubkey;
pubkey.Alloc(len);
char *buff = pubkey.GetWriteBuf(len);
i2d_PUBKEY(evp_pkey, (unsigned char**)&buff);
pubkey.UngetWriteBuf(len);
// hash public key
this->hash = CryptoHelper::SHA1Hash(pubkey);
}
void PeerID::SetCertificate(X509 *c)
{
if (!c)
return;
this->certificate = X509_dup(c);
// hash public key
X509_PUBKEY *pkey = X509_get_X509_PUBKEY(this->certificate);
EVP_PKEY *evp_pkey = X509_PUBKEY_get(pkey);
int len = i2d_PUBKEY(evp_pkey, NULL);
wxString pubkey;
pubkey.Alloc(len);
char *buff = pubkey.GetWriteBuf(len);
i2d_PUBKEY(evp_pkey, (unsigned char**)&buff);
pubkey.UngetWriteBuf(len);
this->hash = CryptoHelper::SHA1Hash(pubkey);
}
bool PeerID::HashMatches(const wxString& p)
{
return RAIN::SafeStringsEq(p, this->hash);
}
bool PeerID::AddressMatches(const wxString& a)
{
unsigned long porta = DEFAULT_PORT, portb = DEFAULT_PORT;
wxIPV4address addra, addrb;
if (a.Contains(":"))
{
addra.Hostname(a.BeforeLast(':'));
a.AfterLast(':').ToULong(&porta);
addra.Service(porta);
} else {
addra.Hostname(a);
addra.Service(porta);
}
if (this->address.Contains(":"))
{
addrb.Hostname(this->address.BeforeLast(':'));
this->address.AfterLast(':').ToULong(&portb);
addrb.Service(portb);
} else {
addrb.Hostname(this->address);
addrb.Service(portb);
}
return (addra.IPAddress() == addrb.IPAddress() && addra.Service() == addrb.Service());
}
wxString PeerID::CertificateAsString()
{
BIO *b = BIO_new(BIO_s_mem());
i2d_X509_bio(b, this->certificate);
BUF_MEM *bptr;
BIO_get_mem_ptr(b, &bptr);
BIO_set_close(b, BIO_NOCLOSE);
BIO_free(b);
wxString ret(bptr->data, bptr->length);
BUF_MEM_free(bptr);
return ret;
}
bool PeerID::HasAddress()
{
return !(this->address.Length() == 0);
}
wxString PeerID::GetCertificateField(int nid)
{
if (!this->certificate)
return "";
X509_NAME *name = X509_get_subject_name(this->certificate);
if (!name)
return "";
int pos = X509_NAME_get_index_by_NID(name, nid, -1);
if (pos == -1)
return "";
X509_NAME_ENTRY *nval = X509_NAME_get_entry(name, pos);
return wxString(nval->value->data);
}
PEERID_WANTS PeerID::GetWants()
{
if ((this->address.Length() == 0) && !this->certificate)
{
return PEERID_WANTS_ALL;
}
if (!this->certificate)
{
return PEERID_WANTS_CERT;
}
if (this->address.Length() == 0)
{
return PEERID_WANTS_ADDR;
}
return PEERID_WANTS_NONE;
}
BEnc::Value * PeerID::AsBValue()
{
BEnc::Dictionary *v = new BEnc::Dictionary();
v->hash["hash"] = new BEnc::String(this->hash);
if (this->address.Length() > 0)
v->hash["addr"] = new BEnc::String(this->address);
if (this->certificate)
v->hash["cert"] = new BEnc::String(this->CertificateAsString());
return v;
}