-
Notifications
You must be signed in to change notification settings - Fork 0
/
doorbird.c
232 lines (183 loc) · 5.48 KB
/
doorbird.c
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
#include <sodium.h>
#include <string.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
typedef struct {
unsigned char id[3];
unsigned char version;
uint32_t opslimit;
uint32_t memlimit;
unsigned char salt[16];
unsigned char nonce[8];
unsigned char ciphertext[34];
}__attribute__((packed)) doorbird_pkt;
typedef struct {
char intercom_id[6];
char event [8];
unsigned timestamp;
} __attribute__((packed)) doorbird_cipher_text;
#define LOGGING(format, ...) printf(format, ##__VA_ARGS__)
#ifdef DEBUG
#define LOG_DEBUG(format, ...) printf(format, ##__VA_ARGS__)
#else
#define LOG_DEBUG(format, ...) do {} while (0)
#endif
#define CRYPTO_SALT_BYTES 16
#define CRYPTO_ARGON_OUT_SIZE 32
#define MAX(x,y) (x>y?x:y)
void hexdump(unsigned char * data, int len) {
for(int i=0; i<len;i++) {
printf("0x%02X ", data[i]);
}
}
unsigned char* stretchPasswordArgon(const char *password, unsigned char *salt, unsigned* oplimit, unsigned* memlimit) {
if (sodium_is_zero(salt, CRYPTO_SALT_BYTES) ) {
randombytes_buf(salt, CRYPTO_SALT_BYTES);
return NULL;
}
unsigned char* key = malloc(CRYPTO_ARGON_OUT_SIZE);
if (!*oplimit) {
*oplimit = crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE;
}
if (!*memlimit) {
*memlimit = crypto_pwhash_MEMLIMIT_MIN;
}
if (crypto_pwhash(key, CRYPTO_ARGON_OUT_SIZE, password, strlen(password), salt, *oplimit, *memlimit, crypto_pwhash_ALG_ARGON2I13)) {
LOGGING("Argon2 Failed\n");
*oplimit = 0;
*memlimit = 0;
free(key);
return NULL;
}
return key;
}
doorbird_cipher_text decrypt_broadcast_notification(const doorbird_pkt * notification, const unsigned char* password) {
doorbird_cipher_text decrypted = {{0},{0},0};
int res = 0;
if(res = crypto_aead_chacha20poly1305_decrypt((unsigned char*)&decrypted, NULL, NULL, notification->ciphertext, sizeof(notification->ciphertext), NULL, 0, notification->nonce, password)){
LOGGING("crypto_aead_chacha20poly1305_decrypt() failed %d\n", res);
perror(NULL);
}
return decrypted;
}
doorbird_cipher_text decode_packet(unsigned char * packet, int size, char * password) {
char pass5[6];
doorbird_pkt * pkt = (doorbird_pkt*)packet;
doorbird_cipher_text decrypted = {{0},{0},0};
if (size != 70) {
LOG_DEBUG("Packet wrong size, ignoring!\n");
return decrypted;
}
if (pkt->id[0] != 0xDE || pkt->id[1] != 0xAD || pkt->id[2] != 0xBE) {
LOG_DEBUG("Wrong ident!, ignoring\n");
return decrypted;
}
LOG_DEBUG("Version %d detected\n", packet[3]);
strncpy(pass5, password, 5);
pass5[5] = 0;
unsigned opslimit = ntohl(pkt->opslimit);
unsigned memlimit = ntohl(pkt->memlimit);
LOG_DEBUG("Opslimit %u, memlimit %u\n", opslimit, memlimit);
#ifdef DEBUG
LOG_DEBUG("salt: ");
hexdump(pkt->salt, sizeof(pkt->salt));
LOG_DEBUG("\n");
#endif
unsigned char* stretchPass = stretchPasswordArgon(pass5, pkt->salt, &opslimit, &memlimit);
if (stretchPass == NULL) {
LOGGING("Error making stretchpass!\n");
}
#ifdef DEBUG
LOG_DEBUG("pass5: %s\n", pass5);
LOG_DEBUG("Stretch Pass: ");
hexdump(stretchPass, CRYPTO_ARGON_OUT_SIZE);
LOG_DEBUG("\n");
LOG_DEBUG("nonce text: ");
hexdump(pkt->nonce, sizeof(pkt->nonce));
LOG_DEBUG("\n");
LOG_DEBUG("cipher text: ");
hexdump(pkt->ciphertext, sizeof(pkt->ciphertext));
LOG_DEBUG("\n");
#endif
decrypted = decrypt_broadcast_notification(pkt, stretchPass);
free(stretchPass);
decrypted.timestamp = ntohl(decrypted.timestamp);
return decrypted;
}
void handle_event(char * event_str) {
LOGGING("Got an event: '%s'\n", event_str);
/**
* Add your logic for doorbell events here
*/
}
int main(int argc, char *argv[]) {
struct sockaddr_in si_me, si_other;
int s1=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
int s2=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
int port1=6524;
int port2=35344;
int broadcast=1;
unsigned int timestamp;
fd_set readfds;
char *user,*password;
if(argc < 2) {
printf("Usage:\n./doorbird user password\n");
return -1;
}
user = argv[1];
password = argv[2];
setsockopt(s1, SOL_SOCKET, SO_BROADCAST,
&broadcast, sizeof broadcast);
setsockopt(s2, SOL_SOCKET, SO_BROADCAST,
&broadcast, sizeof broadcast);
memset(&si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(port1);
si_me.sin_addr.s_addr = INADDR_ANY;
bind(s1, (struct sockaddr *)&si_me, sizeof(si_me));
si_me.sin_port = htons(port2);
bind(s2, (struct sockaddr *)&si_me, sizeof(si_me));
while(1)
{
FD_ZERO(&readfds);
FD_SET(s1, &readfds);
FD_SET(s2, &readfds);
char buf[10000];
int len = 0;
unsigned slen=sizeof(struct sockaddr);
int activity = select(MAX(s1,s2)+1, &readfds, NULL, NULL, NULL);
if (activity > 0) {
if(FD_ISSET(s1, &readfds)) {
len = recvfrom(s1, buf, sizeof(buf)-1, 0, (struct sockaddr *)&si_other, &slen);
} else {
len = recvfrom(s2, buf, sizeof(buf)-1, 0, (struct sockaddr *)&si_other, &slen);
}
}
LOG_DEBUG("Got packet of size %d\n", len);
doorbird_cipher_text event = decode_packet(buf, len, password);
if(event.timestamp == 0) {
continue;
}
char intercom_id[10];
char event_str[10];
strncpy(intercom_id, event.intercom_id, 6);
intercom_id[6] = 0;
strncpy(event_str, event.event, 8);
event_str[8] = 0;
if(strncmp(user,intercom_id,6) != 0) {
LOGGING("Not the expected doorbel %s, ignoring\n", intercom_id);
continue;
}
if(timestamp == event.timestamp) {
LOGGING("Retransmitted event, ignorning\n");
continue;
}
timestamp = event.timestamp;
handle_event(event_str);
}
return 0;
}