forked from cjdelisle/cjdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryptoAuth.c
1115 lines (992 loc) · 42.5 KB
/
CryptoAuth.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "crypto/CryptoAuth_pvt.h"
#include "crypto/AddressCalc.h"
#include "crypto/ReplayProtector.h"
#include "crypto/random/Random.h"
#include "benc/Dict.h"
#include "benc/List.h"
#include "benc/String.h"
#include "util/log/Log.h"
#include "memory/Allocator.h"
#include "util/Assert.h"
#include "util/AddrTools.h"
#include "util/Bits.h"
#include "util/Defined.h"
#include "util/Endian.h"
#include "util/Hex.h"
#include "util/events/Time.h"
#include "wire/Error.h"
#include "wire/Headers.h"
#include "wire/Message.h"
#include "crypto_box_curve25519xsalsa20poly1305.h"
#include "crypto_hash_sha256.h"
#include "crypto_scalarmult_curve25519.h"
#include <stdint.h>
#include <stdbool.h>
enum Nonce {
Nonce_HELLO = 0,
Nonce_REPEAT_HELLO = 1,
Nonce_KEY = 2,
Nonce_REPEAT_KEY = 3,
Nonce_FIRST_TRAFFIC_PACKET = 4
};
static inline void printHexKey(uint8_t output[65], uint8_t key[32])
{
if (key) {
Hex_encode(output, 65, key, 32);
} else {
Bits_memcpy(output, "NULL", 5);
}
}
static inline void printHexPubKey(uint8_t output[65], uint8_t privateKey[32])
{
if (privateKey) {
uint8_t publicKey[32];
crypto_scalarmult_curve25519_base(publicKey, privateKey);
printHexKey(output, publicKey);
} else {
printHexKey(output, NULL);
}
}
/**
* Get a shared secret.
*
* @param outputSecret an array to place the shared secret in.
* @param myPrivateKey
* @param herPublicKey
* @param logger
* @param passwordHash a 32 byte value known to both ends, this must be provably pseudorandom
* the first 32 bytes of a sha256 output from hashing a password is ok,
* whatever she happens to send me in the Auth field is NOT ok.
* If this field is null, the secret will be generated without the password.
*/
static inline void getSharedSecret(uint8_t outputSecret[32],
uint8_t myPrivateKey[32],
uint8_t herPublicKey[32],
uint8_t passwordHash[32],
struct Log* logger)
{
if (passwordHash == NULL) {
crypto_box_curve25519xsalsa20poly1305_beforenm(outputSecret, herPublicKey, myPrivateKey);
} else {
union {
struct {
uint8_t key[32];
uint8_t passwd[32];
} components;
uint8_t bytes[64];
} buff;
crypto_scalarmult_curve25519(buff.components.key, myPrivateKey, herPublicKey);
Bits_memcpy(buff.components.passwd, passwordHash, 32);
crypto_hash_sha256(outputSecret, buff.bytes, 64);
}
if (Defined(Log_KEYS)) {
uint8_t myPublicKeyHex[65];
printHexPubKey(myPublicKeyHex, myPrivateKey);
uint8_t herPublicKeyHex[65];
printHexKey(herPublicKeyHex, herPublicKey);
uint8_t passwordHashHex[65];
printHexKey(passwordHashHex, passwordHash);
uint8_t outputSecretHex[65] = "NULL";
printHexKey(outputSecretHex, outputSecret);
Log_keys(logger,
"Generated a shared secret:\n"
" myPublicKey=%s\n"
" herPublicKey=%s\n"
" passwordHash=%s\n"
" outputSecret=%s\n",
myPublicKeyHex, herPublicKeyHex, passwordHashHex, outputSecretHex);
}
}
static inline void hashPassword(uint8_t secretOut[32],
struct CryptoHeader_Challenge* challengeOut,
const String* login,
const String* password,
const uint8_t authType)
{
crypto_hash_sha256(secretOut, (uint8_t*) password->bytes, password->len);
uint8_t tempBuff[32];
if (authType == 1) {
crypto_hash_sha256(tempBuff, secretOut, 32);
} else if (authType == 2) {
crypto_hash_sha256(tempBuff, (uint8_t*) login->bytes, login->len);
} else {
Assert_failure("Unsupported auth type [%u]", authType);
}
Bits_memcpy(challengeOut, tempBuff, CryptoHeader_Challenge_SIZE);
CryptoHeader_setAuthChallengeDerivations(challengeOut, 0);
challengeOut->type = authType;
challengeOut->additional = 0;
}
/**
* Search the authorized passwords for one matching this auth header.
*
* @param auth the auth header.
* @param context the CryptoAuth engine to search in.
* @return an Auth struct with a if one is found, otherwise NULL.
*/
static inline struct CryptoAuth_User* getAuth(struct CryptoHeader_Challenge* auth,
struct CryptoAuth_pvt* ca)
{
if (auth->type == 0) {
return NULL;
}
int count = 0;
for (struct CryptoAuth_User* u = ca->users; u; u = u->next) {
count++;
if (auth->type == 1 &&
!Bits_memcmp(auth, u->passwordHash, CryptoHeader_Challenge_KEYSIZE))
{
return u;
} else if (auth->type == 2 &&
!Bits_memcmp(auth, u->userNameHash, CryptoHeader_Challenge_KEYSIZE))
{
return u;
}
}
Log_debug(ca->logger, "Got unrecognized auth, password count = [%d]", count);
return NULL;
}
/**
* Decrypt and authenticate.
*
* @param nonce a 24 byte number, may be random, cannot repeat.
* @param msg a message to encipher and authenticate.
* @param secret a shared secret.
* @return 0 if decryption is succeddful, otherwise -1.
*/
static inline Gcc_USE_RET int decryptRndNonce(uint8_t nonce[24],
struct Message* msg,
uint8_t secret[32])
{
if (msg->length < 16) {
return -1;
}
Assert_true(msg->padding >= 16);
uint8_t* startAt = msg->bytes - 16;
uint8_t paddingSpace[16];
Bits_memcpy(paddingSpace, startAt, 16);
Bits_memset(startAt, 0, 16);
if (!Defined(NSA_APPROVED)) {
if (crypto_box_curve25519xsalsa20poly1305_open_afternm(
startAt, startAt, msg->length + 16, nonce, secret) != 0)
{
return -1;
}
}
Bits_memcpy(startAt, paddingSpace, 16);
Message_shift(msg, -16, NULL);
return 0;
}
/**
* Encrypt and authenticate.
* Shifts the message by 16 bytes.
*
* @param nonce a 24 byte number, may be random, cannot repeat.
* @param msg a message to encipher and authenticate.
* @param secret a shared secret.
*/
static inline void encryptRndNonce(uint8_t nonce[24],
struct Message* msg,
uint8_t secret[32])
{
Assert_true(msg->padding >= 32);
uint8_t* startAt = msg->bytes - 32;
// This function trashes 16 bytes of the padding so we will put it back
uint8_t paddingSpace[16];
Bits_memcpy(paddingSpace, startAt, 16);
Bits_memset(startAt, 0, 32);
if (!Defined(NSA_APPROVED)) {
crypto_box_curve25519xsalsa20poly1305_afternm(
startAt, startAt, msg->length + 32, nonce, secret);
}
Bits_memcpy(startAt, paddingSpace, 16);
Message_shift(msg, 16, NULL);
}
/**
* Decrypt a packet.
*
* @param nonce a counter.
* @param msg the message to decrypt, decrypted in place.
* @param secret the shared secret.
* @param isInitiator true if we started the connection.
*/
static inline Gcc_USE_RET int decrypt(uint32_t nonce,
struct Message* msg,
uint8_t secret[32],
bool isInitiator)
{
union {
uint32_t ints[2];
uint8_t bytes[24];
} nonceAs = { .ints = {0, 0} };
nonceAs.ints[!isInitiator] = Endian_hostToLittleEndian32(nonce);
return decryptRndNonce(nonceAs.bytes, msg, secret);
}
/**
* Encrypt a packet.
*
* @param nonce a counter.
* @param msg the message to decrypt, decrypted in place.
* @param secret the shared secret.
* @param isInitiator true if we started the connection.
*/
static inline void encrypt(uint32_t nonce,
struct Message* msg,
uint8_t secret[32],
bool isInitiator)
{
union {
uint32_t ints[2];
uint8_t bytes[24];
} nonceAs = { .ints = {0, 0} };
nonceAs.ints[isInitiator] = Endian_hostToLittleEndian32(nonce);
encryptRndNonce(nonceAs.bytes, msg, secret);
}
static inline bool knowHerKey(struct CryptoAuth_Session_pvt* session)
{
return !Bits_isZero(session->pub.herPublicKey, 32);
}
static void getIp6(struct CryptoAuth_Session_pvt* session, uint8_t* addr)
{
Assert_true(knowHerKey(session));
uint8_t ip6[16];
AddressCalc_addressForPublicKey(ip6, session->pub.herPublicKey);
AddrTools_printIp(addr, ip6);
}
#define cryptoAuthDebug(wrapper, format, ...) \
do { \
if (!Defined(Log_DEBUG)) { break; } \
uint8_t addr[40] = "unknown"; \
getIp6((session), addr); \
String* dn = (session)->pub.displayName; \
Log_debug((session)->context->logger, "%p %s [%s] state[%d]: " format, (void*)(session), \
dn ? dn->bytes : "", addr, (session)->nextNonce, __VA_ARGS__); \
} while (0)
// CHECKFILES_IGNORE missing ;
#define cryptoAuthDebug0(wrapper, format) \
cryptoAuthDebug(session, format "%s", "")
static void reset(struct CryptoAuth_Session_pvt* session)
{
session->nextNonce = CryptoAuth_State_INIT;
session->isInitiator = false;
Bits_memset(session->ourTempPrivKey, 0, 32);
Bits_memset(session->ourTempPubKey, 0, 32);
Bits_memset(session->herTempPubKey, 0, 32);
Bits_memset(session->sharedSecret, 0, 32);
session->established = false;
Bits_memset(&session->pub.replayProtector, 0, sizeof(struct ReplayProtector));
}
static void resetIfTimeout(struct CryptoAuth_Session_pvt* session)
{
if (session->nextNonce == CryptoAuth_State_SENT_HELLO) {
// Lets not reset the session, we just sent one or more hello packets and
// have not received a response, if they respond after we reset then we'll
// be in a tough state.
return;
}
uint64_t nowSecs = Time_currentTimeSeconds(session->context->eventBase);
if (nowSecs - session->timeOfLastPacket > session->pub.resetAfterInactivitySeconds) {
cryptoAuthDebug(session, "No traffic in [%d] seconds, resetting connection.",
(int) (nowSecs - session->timeOfLastPacket));
session->timeOfLastPacket = nowSecs;
reset(session);
}
}
static void encryptHandshake(struct Message* message,
struct CryptoAuth_Session_pvt* session,
int setupMessage)
{
Message_shift(message, CryptoHeader_SIZE, NULL);
struct CryptoHeader* header = (struct CryptoHeader*) message->bytes;
// garbage the auth challenge and set the nonce which follows it
Random_bytes(session->context->rand, (uint8_t*) &header->auth,
CryptoHeader_Challenge_SIZE + 24);
// set the permanent key
Bits_memcpy(header->publicKey, session->context->pub.publicKey, 32);
Assert_true(knowHerKey(session));
// Password auth
uint8_t* passwordHash = NULL;
uint8_t passwordHashStore[32];
if (session->password != NULL) {
hashPassword(passwordHashStore,
&header->auth,
session->login,
session->password,
session->authType);
passwordHash = passwordHashStore;
} else {
header->auth.type = session->authType;
header->auth.additional = 0;
}
// Set the session state
header->nonce = Endian_hostToBigEndian32(session->nextNonce);
if (session->nextNonce == CryptoAuth_State_INIT ||
session->nextNonce == CryptoAuth_State_RECEIVED_HELLO)
{
// If we're sending a hello or a key
// Here we make up a temp keypair
Random_bytes(session->context->rand, session->ourTempPrivKey, 32);
crypto_scalarmult_curve25519_base(session->ourTempPubKey, session->ourTempPrivKey);
if (Defined(Log_KEYS)) {
uint8_t tempPrivateKeyHex[65];
Hex_encode(tempPrivateKeyHex, 65, session->ourTempPrivKey, 32);
uint8_t tempPubKeyHex[65];
Hex_encode(tempPubKeyHex, 65, session->ourTempPubKey, 32);
Log_keys(session->context->logger, "Generating temporary keypair\n"
" myTempPrivateKey=%s\n"
" myTempPublicKey=%s\n",
tempPrivateKeyHex, tempPubKeyHex);
}
}
Bits_memcpy(header->encryptedTempKey, session->ourTempPubKey, 32);
if (Defined(Log_KEYS)) {
uint8_t tempKeyHex[65];
Hex_encode(tempKeyHex, 65, header->encryptedTempKey, 32);
Log_keys(session->context->logger,
"Wrapping temp public key:\n"
" %s\n",
tempKeyHex);
}
cryptoAuthDebug(session, "Sending %s%s packet",
((session->nextNonce & 1) ? "repeat " : ""),
((session->nextNonce < CryptoAuth_State_RECEIVED_HELLO) ? "hello" : "key"));
uint8_t sharedSecret[32];
if (session->nextNonce < CryptoAuth_State_RECEIVED_HELLO) {
getSharedSecret(sharedSecret,
session->context->privateKey,
session->pub.herPublicKey,
passwordHash,
session->context->logger);
session->isInitiator = true;
Assert_true(session->nextNonce <= CryptoAuth_State_SENT_HELLO);
session->nextNonce = CryptoAuth_State_SENT_HELLO;
} else {
// Handshake2
// herTempPubKey was set by decryptHandshake()
Assert_ifParanoid(!Bits_isZero(session->herTempPubKey, 32));
getSharedSecret(sharedSecret,
session->context->privateKey,
session->herTempPubKey,
passwordHash,
session->context->logger);
Assert_true(session->nextNonce <= CryptoAuth_State_SENT_KEY);
session->nextNonce = CryptoAuth_State_SENT_KEY;
if (Defined(Log_KEYS)) {
uint8_t tempKeyHex[65];
Hex_encode(tempKeyHex, 65, session->herTempPubKey, 32);
Log_keys(session->context->logger,
"Using their temp public key:\n"
" %s\n",
tempKeyHex);
}
}
Assert_true((session->nextNonce < CryptoAuth_State_RECEIVED_HELLO) ==
Bits_isZero(session->herTempPubKey, 32));
// Shift message over the encryptedTempKey field.
Message_shift(message, 32 - CryptoHeader_SIZE, NULL);
encryptRndNonce(header->handshakeNonce, message, sharedSecret);
if (Defined(Log_KEYS)) {
uint8_t sharedSecretHex[65];
printHexKey(sharedSecretHex, sharedSecret);
uint8_t nonceHex[49];
Hex_encode(nonceHex, 49, header->handshakeNonce, 24);
uint8_t cipherHex[65];
printHexKey(cipherHex, message->bytes);
Log_keys(session->context->logger,
"Encrypting message with:\n"
" nonce: %s\n"
" secret: %s\n"
" cipher: %s\n",
nonceHex, sharedSecretHex, cipherHex);
}
// Shift it back -- encryptRndNonce adds 16 bytes of authenticator.
Message_shift(message, CryptoHeader_SIZE - 32 - 16, NULL);
}
/** @return 0 on success, -1 otherwise. */
int CryptoAuth_encrypt(struct CryptoAuth_Session* sessionPub, struct Message* msg)
{
struct CryptoAuth_Session_pvt* session =
Identity_check((struct CryptoAuth_Session_pvt*) sessionPub);
// If there has been no incoming traffic for a while, reset the connection to state 0.
// This will prevent "connection in bad state" situations from lasting forever.
// this will reset the session if it has timed out.
resetIfTimeout(session);
// If the nonce wraps, start over.
if (session->nextNonce >= 0xfffffff0) {
reset(session);
}
Assert_true(!((uintptr_t)msg->bytes % 4) || !"alignment fault");
// nextNonce 0: sending hello, we are initiating connection.
// nextNonce 1: sending another hello, nothing received yet.
// nextNonce 2: sending key, hello received.
// nextNonce 3: sending key again, no data packet recieved yet.
// nextNonce >3: handshake complete
//
// if it's a blind handshake, every message will be empty and nextNonce will remain
// zero until the first message is received back.
if (session->nextNonce <= CryptoAuth_State_RECEIVED_KEY) {
if (session->nextNonce < CryptoAuth_State_RECEIVED_KEY) {
encryptHandshake(msg, session, 0);
return 0;
} else {
cryptoAuthDebug0(session, "Doing final step to send message. nonce=4");
Assert_ifParanoid(!Bits_isZero(session->ourTempPrivKey, 32));
Assert_ifParanoid(!Bits_isZero(session->herTempPubKey, 32));
getSharedSecret(session->sharedSecret,
session->ourTempPrivKey,
session->herTempPubKey,
NULL,
session->context->logger);
}
}
Assert_true(msg->length > 0 && "Empty packet during handshake");
Assert_true(msg->padding >= 36 || !"not enough padding");
encrypt(session->nextNonce, msg, session->sharedSecret, session->isInitiator);
Message_push32(msg, session->nextNonce, NULL);
session->nextNonce++;
return 0;
}
/** Call the external interface and tell it that a message has been received. */
static inline void updateTime(struct CryptoAuth_Session_pvt* session, struct Message* message)
{
session->timeOfLastPacket = Time_currentTimeSeconds(session->context->eventBase);
}
static inline enum CryptoAuth_DecryptErr decryptMessage(struct CryptoAuth_Session_pvt* session,
uint32_t nonce,
struct Message* content,
uint8_t secret[32])
{
// Decrypt with authentication and replay prevention.
if (decrypt(nonce, content, secret, session->isInitiator)) {
cryptoAuthDebug0(session, "DROP authenticated decryption failed");
return CryptoAuth_DecryptErr_DECRYPT;
}
if (!ReplayProtector_checkNonce(nonce, &session->pub.replayProtector)) {
cryptoAuthDebug(session, "DROP nonce checking failed nonce=[%u]", nonce);
return CryptoAuth_DecryptErr_REPLAY;
}
return 0;
}
static bool ip6MatchesKey(uint8_t ip6[16], uint8_t key[32])
{
uint8_t calculatedIp6[16];
AddressCalc_addressForPublicKey(calculatedIp6, key);
return !Bits_memcmp(ip6, calculatedIp6, 16);
}
static enum CryptoAuth_DecryptErr decryptHandshake(struct CryptoAuth_Session_pvt* session,
const uint32_t nonce,
struct Message* message,
struct CryptoHeader* header)
{
if (message->length < CryptoHeader_SIZE) {
cryptoAuthDebug0(session, "DROP runt");
return CryptoAuth_DecryptErr_RUNT;
}
// handshake
// nextNonce 0: recieving hello.
// nextNonce 1: recieving key, we sent hello.
// nextNonce 2: recieving first data packet or duplicate hello.
// nextNonce 3: recieving first data packet.
// nextNonce >3: handshake complete
Assert_true(knowHerKey(session));
if (Bits_memcmp(session->pub.herPublicKey, header->publicKey, 32)) {
cryptoAuthDebug0(session, "DROP a packet with different public key than this session");
return CryptoAuth_DecryptErr_WRONG_PERM_PUBKEY;
}
Assert_true((session->nextNonce < CryptoAuth_State_RECEIVED_HELLO) ==
Bits_isZero(session->herTempPubKey, 32));
struct CryptoAuth_User* userObj = getAuth(&header->auth, session->context);
uint8_t* restrictedToip6 = NULL;
uint8_t* passwordHash = NULL;
if (userObj) {
passwordHash = userObj->secret;
if (userObj->restrictedToip6[0]) {
restrictedToip6 = userObj->restrictedToip6;
if (!ip6MatchesKey(restrictedToip6, session->pub.herPublicKey)) {
cryptoAuthDebug0(session, "DROP packet with key not matching restrictedToip6");
return CryptoAuth_DecryptErr_IP_RESTRICTED;
}
}
}
if (session->requireAuth && !userObj) {
cryptoAuthDebug0(session, "DROP message because auth was not given");
return CryptoAuth_DecryptErr_AUTH_REQUIRED;
}
if (!userObj && header->auth.type != 0) {
cryptoAuthDebug0(session, "DROP message with unrecognized authenticator");
return CryptoAuth_DecryptErr_UNRECOGNIZED_AUTH;
}
// What the nextNonce will become if this packet is valid.
uint32_t nextNonce;
// The secret for decrypting this message.
uint8_t sharedSecret[32];
if (nonce < Nonce_KEY) { // HELLO or REPEAT_HELLO
if (nonce == Nonce_HELLO) {
cryptoAuthDebug(session, "Received a hello packet, using auth: %d",
(userObj != NULL));
} else {
Assert_true(nonce == Nonce_REPEAT_HELLO);
cryptoAuthDebug0(session, "Received a repeat hello packet");
}
getSharedSecret(sharedSecret,
session->context->privateKey,
session->pub.herPublicKey,
passwordHash,
session->context->logger);
nextNonce = CryptoAuth_State_RECEIVED_HELLO;
} else {
if (nonce == Nonce_KEY) {
cryptoAuthDebug0(session, "Received a key packet");
} else {
Assert_true(nonce == Nonce_REPEAT_KEY);
cryptoAuthDebug0(session, "Received a repeat key packet");
}
if (!session->isInitiator) {
cryptoAuthDebug0(session, "DROP a stray key packet");
return CryptoAuth_DecryptErr_STRAY_KEY;
}
// We sent the hello, this is a key
getSharedSecret(sharedSecret,
session->ourTempPrivKey,
session->pub.herPublicKey,
passwordHash,
session->context->logger);
nextNonce = CryptoAuth_State_RECEIVED_KEY;
}
// Shift it on top of the authenticator before the encrypted public key
Message_shift(message, 48 - CryptoHeader_SIZE, NULL);
if (Defined(Log_KEYS)) {
uint8_t sharedSecretHex[65];
printHexKey(sharedSecretHex, sharedSecret);
uint8_t nonceHex[49];
Hex_encode(nonceHex, 49, header->handshakeNonce, 24);
uint8_t cipherHex[65];
printHexKey(cipherHex, message->bytes);
Log_keys(session->context->logger,
"Decrypting message with:\n"
" nonce: %s\n"
" secret: %s\n"
" cipher: %s\n",
nonceHex, sharedSecretHex, cipherHex);
}
// Decrypt her temp public key and the message.
if (decryptRndNonce(header->handshakeNonce, message, sharedSecret)) {
// just in case
Bits_memset(header, 0, CryptoHeader_SIZE);
cryptoAuthDebug(session, "DROP message with nonce [%d], decryption failed", nonce);
return CryptoAuth_DecryptErr_HANDSHAKE_DECRYPT_FAILED;
}
if (Bits_isZero(header->encryptedTempKey, 32)) {
// we need to reject 0 public keys outright because they will be confused with "unknown"
cryptoAuthDebug0(session, "DROP message with zero as temp public key");
return CryptoAuth_DecryptErr_WISEGUY;
}
if (Defined(Log_KEYS)) {
uint8_t tempKeyHex[65];
Hex_encode(tempKeyHex, 65, header->encryptedTempKey, 32);
Log_keys(session->context->logger,
"Unwrapping temp public key:\n"
" %s\n",
tempKeyHex);
}
Message_shift(message, -32, NULL);
// Post-decryption checking
if (nonce == Nonce_HELLO) {
// A new hello packet
if (!Bits_memcmp(session->herTempPubKey, header->encryptedTempKey, 32)) {
// possible replay attack or duped packet
cryptoAuthDebug0(session, "DROP dupe hello packet with same temp key");
return CryptoAuth_DecryptErr_INVALID_PACKET;
}
} else if (nonce == Nonce_KEY && session->nextNonce >= CryptoAuth_State_RECEIVED_KEY) {
// we accept a new key packet and let it change the session since the other end might have
// killed off the session while it was in the midst of setting up.
// This is NOT a repeat key packet because it's nonce is 2, not 3
if (!Bits_memcmp(session->herTempPubKey, header->encryptedTempKey, 32)) {
Assert_true(!Bits_isZero(session->herTempPubKey, 32));
cryptoAuthDebug0(session, "DROP dupe key packet with same temp key");
return CryptoAuth_DecryptErr_INVALID_PACKET;
}
} else if (nonce == Nonce_REPEAT_KEY && session->nextNonce >= CryptoAuth_State_RECEIVED_KEY) {
// Got a repeat key packet, make sure the temp key is the same as the one we know.
if (Bits_memcmp(session->herTempPubKey, header->encryptedTempKey, 32)) {
Assert_true(!Bits_isZero(session->herTempPubKey, 32));
cryptoAuthDebug0(session, "DROP repeat key packet with different temp key");
return CryptoAuth_DecryptErr_INVALID_PACKET;
}
}
// If Alice sent a hello packet then Bob sent a hello packet and they crossed on the wire,
// somebody has to yield and the other has to stand firm otherwise they will either deadlock
// each believing their hello packet is superior or they will livelock, each switching to the
// other's session and never synchronizing.
// In this event whoever has the lower permanent public key wins.
// If we receive a (possibly repeat) key packet
if (nextNonce == CryptoAuth_State_RECEIVED_KEY) {
Assert_true(nonce == Nonce_KEY || nonce == Nonce_REPEAT_KEY);
switch (session->nextNonce) {
case CryptoAuth_State_INIT:
case CryptoAuth_State_RECEIVED_HELLO:
case CryptoAuth_State_SENT_KEY: {
cryptoAuthDebug0(session, "DROP stray key packet");
return CryptoAuth_DecryptErr_STRAY_KEY;
}
case CryptoAuth_State_SENT_HELLO: {
Bits_memcpy(session->herTempPubKey, header->encryptedTempKey, 32);
break;
}
case CryptoAuth_State_RECEIVED_KEY: {
if (nonce == Nonce_KEY) {
Bits_memcpy(session->herTempPubKey, header->encryptedTempKey, 32);
} else {
Assert_true(!Bits_memcmp(session->herTempPubKey, header->encryptedTempKey, 32));
}
break;
}
default: {
Assert_true(!session->established);
if (nonce == Nonce_KEY) {
Bits_memcpy(session->herTempPubKey, header->encryptedTempKey, 32);
cryptoAuthDebug0(session, "New key packet, recalculating shared secret");
Assert_ifParanoid(!Bits_isZero(session->ourTempPrivKey, 32));
Assert_ifParanoid(!Bits_isZero(session->herTempPubKey, 32));
getSharedSecret(session->sharedSecret,
session->ourTempPrivKey,
session->herTempPubKey,
NULL,
session->context->logger);
} else {
Assert_true(!Bits_memcmp(session->herTempPubKey, header->encryptedTempKey, 32));
}
nextNonce = session->nextNonce + 1;
cryptoAuthDebug0(session, "New key packet but we are already sending data");
}
}
} else if (nextNonce == CryptoAuth_State_RECEIVED_HELLO) {
Assert_true(nonce == Nonce_HELLO || nonce == Nonce_REPEAT_HELLO);
if (Bits_memcmp(session->herTempPubKey, header->encryptedTempKey, 32)) {
// fresh new hello packet, we should reset the session.
switch (session->nextNonce) {
case CryptoAuth_State_SENT_HELLO: {
if (Bits_memcmp(session->pub.herPublicKey,
session->context->pub.publicKey, 32) < 0)
{
// It's a hello and we are the initiator but their permant public key is
// numerically lower than ours, this is so that in the event of two hello
// packets crossing on the wire, the nodes will agree on who is the
// initiator.
cryptoAuthDebug0(session,
"Incoming hello from node with lower key, resetting");
reset(session);
Bits_memcpy(session->herTempPubKey, header->encryptedTempKey, 32);
break;
} else {
// We are the initiator and thus we are sending HELLO packets, however they
// have sent a hello to us and we already sent a HELLO
// We accept the packet (return 0) but we do not alter the state because
// we have our own state and we will respond with our (key) packet.
cryptoAuthDebug0(session,
"Incoming hello from node with higher key, not resetting");
return 0;
}
}
case CryptoAuth_State_INIT: {
Bits_memcpy(session->herTempPubKey, header->encryptedTempKey, 32);
break;
}
default: {
cryptoAuthDebug0(session, "Incoming hello packet resetting session");
reset(session);
Bits_memcpy(session->herTempPubKey, header->encryptedTempKey, 32);
break;
}
}
} else {
// received a hello packet with the same key as the session we already know...
switch (session->nextNonce) {
case CryptoAuth_State_RECEIVED_HELLO:
case CryptoAuth_State_SENT_KEY: {
nextNonce = session->nextNonce;
break;
}
default: {
cryptoAuthDebug0(session, "DROP Incoming repeat hello");
// We already know the key which is being used for this hello packet and
// our state has advanced past RECEIVED_HELLO or SENT_KEY or perhaps we
// are the initiator of this session and they're sending us what should
// be a key packet but is marked as hello, it's all invalid.
return CryptoAuth_DecryptErr_INVALID_PACKET;
}
}
}
} else {
Assert_failure("should never happen");
}
// Nonces can never go backward and can only "not advance" if they're 0,1,2,3,4 session state.
Assert_true(session->nextNonce < nextNonce ||
(session->nextNonce <= CryptoAuth_State_RECEIVED_KEY && nextNonce == session->nextNonce)
);
session->nextNonce = nextNonce;
Bits_memset(&session->pub.replayProtector, 0, sizeof(struct ReplayProtector));
return 0;
}
/** @return 0 on success, -1 otherwise. */
enum CryptoAuth_DecryptErr CryptoAuth_decrypt(struct CryptoAuth_Session* sessionPub,
struct Message* msg)
{
struct CryptoAuth_Session_pvt* session =
Identity_check((struct CryptoAuth_Session_pvt*) sessionPub);
struct CryptoHeader* header = (struct CryptoHeader*) msg->bytes;
if (msg->length < 20) {
cryptoAuthDebug0(session, "DROP runt");
return CryptoAuth_DecryptErr_RUNT;
}
Assert_true(msg->padding >= 12 || "need at least 12 bytes of padding in incoming message");
Assert_true(!((uintptr_t)msg->bytes % 4) || !"alignment fault");
Assert_true(!(msg->capacity % 4) || !"length fault");
Message_shift(msg, -4, NULL);
uint32_t nonce = Endian_bigEndianToHost32(header->nonce);
if (!session->established) {
if (nonce >= Nonce_FIRST_TRAFFIC_PACKET) {
if (session->nextNonce < CryptoAuth_State_SENT_KEY) {
// This is impossible because we have not exchanged hello and key messages.
cryptoAuthDebug0(session, "DROP Received a run message to an un-setup session");
return CryptoAuth_DecryptErr_NO_SESSION;
}
cryptoAuthDebug(session, "Trying final handshake step, nonce=%u\n", nonce);
uint8_t secret[32];
Assert_ifParanoid(!Bits_isZero(session->ourTempPrivKey, 32));
Assert_ifParanoid(!Bits_isZero(session->herTempPubKey, 32));
getSharedSecret(secret,
session->ourTempPrivKey,
session->herTempPubKey,
NULL,
session->context->logger);
enum CryptoAuth_DecryptErr ret = decryptMessage(session, nonce, msg, secret);
if (!ret) {
cryptoAuthDebug0(session, "Final handshake step succeeded");
Bits_memcpy(session->sharedSecret, secret, 32);
// Now we're in run mode, no more handshake packets will be accepted
session->established = true;
session->nextNonce += 3;
updateTime(session, msg);
return 0;
}
cryptoAuthDebug0(session, "DROP Final handshake step failed");
return ret;
}
Message_shift(msg, 4, NULL);
return decryptHandshake(session, nonce, msg, header);
} else if (nonce >= Nonce_FIRST_TRAFFIC_PACKET) {
Assert_ifParanoid(!Bits_isZero(session->sharedSecret, 32));
enum CryptoAuth_DecryptErr ret = decryptMessage(session, nonce, msg, session->sharedSecret);
if (!ret) {
updateTime(session, msg);
return 0;
} else {
cryptoAuthDebug(session, "DROP Failed to [%s] message",
((ret == CryptoAuth_DecryptErr_REPLAY) ? "replay check" : "decrypt"));
return ret;
}
} else if (nonce <= Nonce_REPEAT_HELLO) {
cryptoAuthDebug(session, "hello packet during established session nonce=[%d]", nonce);
Message_shift(msg, 4, NULL);
return decryptHandshake(session, nonce, msg, header);
} else {
cryptoAuthDebug(session, "DROP key packet during established session nonce=[%d]", nonce);
return CryptoAuth_DecryptErr_KEY_PKT_ESTABLISHED_SESSION;
}
Assert_failure("unreachable");
}
/////////////////////////////////////////////////////////////////////////////////////////////////
struct CryptoAuth* CryptoAuth_new(struct Allocator* allocator,
const uint8_t* privateKey,
struct EventBase* eventBase,
struct Log* logger,
struct Random* rand)
{
struct CryptoAuth_pvt* ca = Allocator_calloc(allocator, sizeof(struct CryptoAuth_pvt), 1);
Identity_set(ca);
ca->allocator = allocator;
ca->eventBase = eventBase;
ca->logger = logger;
ca->rand = rand;
if (privateKey != NULL) {
Bits_memcpy(ca->privateKey, privateKey, 32);
} else {
Random_bytes(rand, ca->privateKey, 32);
}
crypto_scalarmult_curve25519_base(ca->pub.publicKey, ca->privateKey);
if (Defined(Log_KEYS)) {
uint8_t publicKeyHex[65];
printHexKey(publicKeyHex, ca->pub.publicKey);
uint8_t privateKeyHex[65];
printHexKey(privateKeyHex, ca->privateKey);
Log_keys(logger,
"Initialized CryptoAuth:\n myPrivateKey=%s\n myPublicKey=%s\n",
privateKeyHex,
publicKeyHex);
}
return &ca->pub;
}
int CryptoAuth_addUser_ipv6(String* password,
String* login,
uint8_t ipv6[16],
struct CryptoAuth* cryptoAuth)
{
struct CryptoAuth_pvt* ca = Identity_check((struct CryptoAuth_pvt*) cryptoAuth);
struct Allocator* alloc = Allocator_child(ca->allocator);
struct CryptoAuth_User* user = Allocator_calloc(alloc, sizeof(struct CryptoAuth_User), 1);
user->alloc = alloc;
Identity_set(user);
if (!login) {
int i = 0;
for (struct CryptoAuth_User* u = ca->users; u; u = u->next) { i++; }
user->login = login = String_printf(alloc, "Anon #%d", i);
} else {
user->login = String_clone(login, alloc);
}
struct CryptoHeader_Challenge ac;
// Users specified with a login field might want to use authType 1 still.
hashPassword(user->secret, &ac, login, password, 2);
Bits_memcpy(user->userNameHash, &ac, CryptoHeader_Challenge_KEYSIZE);
hashPassword(user->secret, &ac, NULL, password, 1);
Bits_memcpy(user->passwordHash, &ac, CryptoHeader_Challenge_KEYSIZE);
for (struct CryptoAuth_User* u = ca->users; u; u = u->next) {
if (Bits_memcmp(user->secret, u->secret, 32)) {
} else if (!login) {
} else if (String_equals(login, u->login)) {
Allocator_free(alloc);
return CryptoAuth_addUser_DUPLICATE;
}
}
if (ipv6) {
Bits_memcpy(user->restrictedToip6, ipv6, 16);
}
// Add the user to the *end* of the list
for (struct CryptoAuth_User** up = &ca->users; ; up = &(*up)->next) {
if (!*up) {
*up = user;
break;
}
}
return 0;
}
int CryptoAuth_removeUsers(struct CryptoAuth* context, String* login)
{
struct CryptoAuth_pvt* ca = Identity_check((struct CryptoAuth_pvt*) context);
int count = 0;
struct CryptoAuth_User** up = &ca->users;
struct CryptoAuth_User* u = *up;