-
Notifications
You must be signed in to change notification settings - Fork 24
/
krypton.c
8183 lines (6994 loc) · 222 KB
/
krypton.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
#ifdef NS_MODULE_LINES
#line 1 "src/krypton.h"
/**/
#endif
/*
* Copyright (c) 2015 Cesanta Software Limited
* All rights reserved
*/
#ifndef _KRYPTON_H
#define _KRYPTON_H
#ifdef KR_LOCALS
#include <kr_locals.h>
#endif
typedef struct x509_store_ctx_st X509_STORE_CTX;
typedef struct ssl_st SSL;
typedef struct ssl_ctx_st SSL_CTX;
typedef struct ssl_method_st SSL_METHOD;
int SSL_library_init(void);
SSL *SSL_new(SSL_CTX *ctx);
int SSL_set_fd(SSL *ssl, int fd);
int SSL_get_fd(SSL *ssl);
int SSL_accept(SSL *ssl);
int SSL_connect(SSL *ssl);
int SSL_read(SSL *ssl, void *buf, int num);
int SSL_write(SSL *ssl, const void *buf, int num);
int SSL_shutdown(SSL *ssl);
void SSL_free(SSL *ssl);
#define SSL_ERROR_NONE 0
#define SSL_ERROR_SSL 1
#define SSL_ERROR_WANT_READ 2
#define SSL_ERROR_WANT_WRITE 3
#define SSL_ERROR_SYSCALL 5
#define SSL_ERROR_ZERO_RETURN 6
#define SSL_ERROR_WANT_CONNECT 7
#define SSL_ERROR_WANT_ACCEPT 8
int SSL_get_error(const SSL *ssl, int ret);
const SSL_METHOD *TLSv1_2_method(void);
const SSL_METHOD *TLSv1_2_server_method(void);
const SSL_METHOD *TLSv1_2_client_method(void);
const SSL_METHOD *SSLv23_method(void);
const SSL_METHOD *SSLv23_server_method(void);
const SSL_METHOD *SSLv23_client_method(void);
SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth);
#define SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER 0x00000002L
#define SSL_CTX_set_mode(ctx, op) SSL_CTX_ctrl((ctx), 33, (op), NULL)
long SSL_CTX_ctrl(SSL_CTX *, int, long, void *);
/* for the client */
#define SSL_VERIFY_NONE 0x00
#define SSL_VERIFY_PEER 0x01
#define SSL_VERIFY_FAIL_IF_NO_PEER_CERT 0x02
#define SSL_VERIFY_CLIENT_ONCE 0x04
void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
int (*verify_callback)(int, X509_STORE_CTX *));
int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
const char *CAPath);
/* Krypton-specific. */
int SSL_CTX_kr_set_verify_name(SSL_CTX *ctx, const char *name);
/* for the server */
#define SSL_FILETYPE_PEM 1
int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file);
int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type);
int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type);
void SSL_CTX_free(SSL_CTX *);
#endif /* _KRYPTON_H */
#ifdef NS_MODULE_LINES
#line 1 "src/src/ktypes.h"
/**/
#endif
/*
* Copyright (c) 2015 Cesanta Software Limited
* All rights reserved
*/
#ifndef _KTYPES_H
#define _KTYPES_H
#define _FILE_OFFSET_BITS 64
#define _GNU_SOURCE
#undef WIN32_LEAN_AND_MEAN // Let windows.h always include winsock2.h
#ifndef NS_INTERNAL
#define NS_INTERNAL static
#else
#define NS_INTERNAL
#endif
#include <sys/types.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#ifdef _MSC_VER
#include <winsock2.h>
#include <windows.h>
#define __unused
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
typedef int int32_t;
typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;
typedef unsigned long uintptr_t;
typedef long ssize_t;
#define __func__ ""
#define __packed
#ifndef alloca
#define alloca(x) _alloca(x)
#endif
#ifndef EWOULDBLOCK
#define EWOULDBLOCK WSAEWOULDBLOCK
#endif
#define SOCKET_ERRNO WSAGetLastError()
#pragma comment(lib, "ws2_32.lib") // Linking with winsock library
#else /* _MSC_VER */
#include <stdint.h>
#include <unistd.h>
#define __packed __attribute__((packed))
#define SOCKET_ERRNO errno
#endif
#ifndef BYTE_ORDER
#define LITTLE_ENDIAN 0x41424344UL
#define BIG_ENDIAN 0x44434241UL
#define PDP_ENDIAN 0x42414443UL
#define BYTE_ORDER LITTLE_ENDIAN
#endif
#ifndef htobe16
#define htobe16 htons
#endif
#ifndef htobe32
#define htobe32 htonl
#endif
#ifndef be16toh
#define be16toh ntohs
#endif
#ifndef be32toh
#define be32toh ntohl
#endif
#ifndef htobe64
#if BYTE_ORDER == LITTLE_ENDIAN
#define htobe64(x) \
(((uint64_t) htonl((x) &0xffffffff) << 32) | htonl((x) >> 32))
#define xxhtobe64(x) __builtin_bswap64(x)
#else
#define htobe64
#endif
#endif
/* #define KRYPTON_DEBUG 1 */
#if defined(KRYPTON_DEBUG)
#define dprintf(x) printf x
#else
#define dprintf(x)
#endif
/* #define KRYPTON_DEBUG_NONBLOCKING 1 */
struct ro_vec {
const uint8_t *ptr;
size_t len;
};
struct vec {
uint8_t *ptr;
size_t len;
};
typedef struct pem_st PEM;
typedef struct der_st DER;
typedef struct X509_st X509;
typedef struct _RSA_CTX RSA_CTX;
struct x509_store_ctx_st {
int dummy;
};
struct ssl_method_st {
uint8_t sv_undefined : 1;
uint8_t cl_undefined : 1;
};
struct ssl_ctx_st {
#ifndef KR_NO_LOAD_CA_STORE
X509 *ca_store;
#else
char *ca_file;
#endif
PEM *pem_cert;
RSA_CTX *rsa_privkey;
uint8_t mode;
uint8_t vrfy_mode;
struct ssl_method_st meth;
char *verify_name;
};
#define STATE_INITIAL 0
#define STATE_CL_HELLO_SENT 1
#define STATE_CL_HELLO_WAIT 2
#define STATE_CL_HELLO_RCVD 3
#define STATE_SV_HELLO_SENT 4
#define STATE_SV_HELLO_RCVD 5
#define STATE_SV_CERT_RCVD 6
#define STATE_SV_DONE_RCVD 7
#define STATE_CLIENT_FINISHED 8
#define STATE_ESTABLISHED 9
#define STATE_CLOSING 10
struct ssl_st {
struct ssl_ctx_st *ctx;
struct tls_security *cur;
struct tls_security *nxt;
/* rcv buffer: can be 16bit lens? */
#define RX_INITIAL_BUF 256
uint8_t *rx_buf;
uint32_t rx_len;
uint32_t rx_max_len;
uint8_t *tx_buf;
uint32_t tx_len;
uint32_t tx_max_len;
int fd;
int err;
/* for handling appdata recvs */
unsigned int copied;
struct vec extra_appdata;
uint8_t *appdata_eom;
uint8_t state;
uint8_t vrfy_result;
uint8_t mode_defined : 1;
uint8_t is_server : 1;
uint8_t got_appdata : 1;
uint8_t tx_enc : 1;
uint8_t rx_enc : 1;
uint8_t close_notify : 1;
uint8_t fatal : 1;
uint8_t write_pending : 1;
};
NS_INTERNAL void ssl_err(struct ssl_st *ssl, int err);
#if KRYPTON_DEBUG
void hex_dumpf(FILE *f, const void *buf, size_t len, size_t llen);
void hex_dump(const void *ptr, size_t len, size_t llen);
#endif
typedef struct _bigint bigint; /**< An alias for _bigint */
/* Amalgamated: #include "tlsproto.h" */
/* Amalgamated: #include "crypto.h" */
/* Amalgamated: #include "bigint_impl.h" */
/* Amalgamated: #include "bigint.h" */
/* Amalgamated: #include "pem.h" */
/* Amalgamated: #include "ber.h" */
/* Amalgamated: #include "../openssl/ssl.h" */
/* Amalgamated: #include "tls.h" */
/* Amalgamated: #include "ber.h" */
/* Amalgamated: #include "x509.h" */
#endif /* _KTYPES_H */
#ifdef NS_MODULE_LINES
#line 1 "src/src/tlsproto.h"
/**/
#endif
/*
* Copyright (c) 2015 Cesanta Software Limited
* All rights reserved
*/
#ifndef _TLSPROTO_H
#define _TLSPROTO_H
/* set to number of null ciphers */
#define ALLOW_NULL_CIPHERS 0
/* just count non-NULL ciphers */
#define NUM_CIPHER_SUITES 4
#define NUM_COMPRESSORS 1
#pragma pack(1)
struct tls_random {
uint32_t time;
uint8_t opaque[28];
} __packed;
struct tls_premaster_secret {
uint16_t version;
uint8_t opaque[46];
} __packed;
struct tls_hmac_hdr {
uint64_t seq;
uint8_t type;
uint16_t vers;
uint16_t len;
} __packed;
struct tls_hdr {
uint8_t type;
uint16_t vers;
uint16_t len;
} __packed;
struct tls_EXT_reneg {
uint16_t type;
uint16_t len;
uint8_t ri_len;
} __packed;
struct tls_svr_hello {
uint8_t type;
uint8_t len_hi;
uint16_t len;
uint16_t version;
struct tls_random random;
uint8_t sess_id_len;
uint16_t cipher_suite;
uint8_t compressor;
uint16_t ext_len;
struct tls_EXT_reneg ext_reneg;
} __packed;
struct tls_cl_hello {
uint8_t type;
uint8_t len_hi;
uint16_t len;
uint16_t version;
struct tls_random random;
uint8_t sess_id_len;
uint16_t cipher_suites_len;
uint16_t cipher_suite[NUM_CIPHER_SUITES + ALLOW_NULL_CIPHERS + 1];
uint8_t num_compressors;
uint8_t compressor[NUM_COMPRESSORS];
uint16_t ext_len;
struct tls_EXT_reneg ext_reneg;
} __packed;
struct tls_cert {
uint8_t type;
uint8_t len_hi;
uint16_t len;
uint8_t certs_len_hi;
uint16_t certs_len;
} __packed;
struct tls_cert_hdr {
/* for chains */
uint8_t cert_len_hi;
uint16_t cert_len;
} __packed;
struct tls_svr_hello_done {
uint8_t type;
uint8_t len_hi;
uint16_t len;
} __packed;
struct tls_change_cipher_spec {
uint8_t one;
} __packed;
struct tls_finished {
uint8_t type;
uint8_t len_hi;
uint16_t len;
uint8_t vrfy[12];
} __packed;
struct tls_alert {
uint8_t level;
uint8_t desc;
} __packed;
#pragma pack()
#define TLS_CHANGE_CIPHER_SPEC 20
#define TLS_ALERT 21
#define TLS_HANDSHAKE 22
#define TLS_APP_DATA 23
#define TLS_HEARTBEAT 24
#define HANDSHAKE_HELLO_REQ 0
#define HANDSHAKE_CLIENT_HELLO 1
#define HANDSHAKE_SERVER_HELLO 2
#define HANDSHAKE_NEW_SESSION_TICKET 4
#define HANDSHAKE_CERTIFICATE 11
#define HANDSHAKE_SERVER_KEY_EXCH 12
#define HANDSHAKE_CERTIFICATE_REQ 13
#define HANDSHAKE_SERVER_HELLO_DONE 14
#define HANDSHAKE_CERTIFICATE_VRFY 15
#define HANDSHAKE_CLIENT_KEY_EXCH 16
#define HANDSHAKE_FINISHED 20
#define EXT_SERVER_NAME 0x0000
#define EXT_SESSION_TICKET 0x0023
#define EXT_HEARTBEAT 0x000f
#define EXT_SIG_ALGOS 0x000d
#define EXT_NPN 0x3374
#define EXT_RENEG_INFO 0xff01
#define ALERT_LEVEL_WARNING 1
#define ALERT_LEVEL_FATAL 2
#define ALERT_CLOSE_NOTIFY 0
#define ALERT_UNEXPECTED_MESSAGE 10
#define ALERT_BAD_RECORD_MAC 20
#define ALERT_RECORD_OVERFLOW 22
#define ALERT_HANDSHAKE_FAILURE 40
#define ALERT_BAD_CERT 42
#define ALERT_UNSUPPORTED_CERT 43
#define ALERT_CERT_REVOKED 44
#define ALERT_CERT_EXPIRED 43
#define ALERT_CERT_UNKNOWN 46
#define ALERT_ILLEGAL_PARAMETER 47
#define ALERT_UNKNOWN_CA 48
#define ALERT_ACCESS_DENIED 49
#define ALERT_DECODE_ERROR 50
#define ALERT_DECRYPT_ERROR 51
#define ALERT_PROTOCOL_VERSION 70
#define ALERT_INSUFFICIENT_SECURITY 71
#define ALERT_INTERNAL_ERROR 80
#define ALERT_USER_CANCELLED 90
#define ALERT_NO_RENEGOTIATION 100
#define ALERT_UNSUPPORTED_EXT 110
/* http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4
*/
typedef enum {
#if ALLOW_NULL_CIPHERS
TLS_RSA_WITH_NULL_MD5 = 0x0001,
#endif
TLS_RSA_WITH_RC4_128_MD5 = 0x0004,
TLS_RSA_WITH_RC4_128_SHA = 0x0005,
TLS_RSA_WITH_AES_128_CBC_SHA = 0x002f,
TLS_RSA_WITH_AES_128_CBC_SHA256 = 0x003c,
} kr_cs_id;
#define TLS_EMPTY_RENEGOTIATION_INFO_SCSV 0x00ff
#define COMPRESSOR_NULL 0x00
#endif /* _TLSPROTO_H */
#ifdef NS_MODULE_LINES
#line 1 "src/src/kexterns.h"
/**/
#endif
#ifndef _KEXTERNS_H
#define _KEXTERNS_H
#ifdef KR_EXT_IO
extern ssize_t kr_send(int fd, const void *buf, size_t len, int flags);
extern ssize_t kr_recv(int fd, void *buf, size_t len, int flags);
#endif
#ifdef KR_EXT_RANDOM
extern int kr_get_random(uint8_t *out, size_t len);
#endif
#ifdef KR_EXT_MD5
extern void kr_hash_md5_v(size_t num_msgs, const uint8_t *msgs[],
const size_t *msg_lens, uint8_t *digest);
#endif
#ifdef KR_EXT_SHA1
extern void kr_hash_sha1_v(size_t num_msgs, const uint8_t *msgs[],
const size_t *msg_lens, uint8_t *digest);
#endif
#ifdef KR_EXT_SHA256
extern void kr_hash_sha256_v(size_t num_msgs, const uint8_t *msgs[],
const size_t *msg_lens, uint8_t *digest);
#endif
/* Some defaults. */
#if !defined(KR_EXT_IO) && (defined(_POSIX_VERSION) || defined(WIN32))
#define kr_send send
#define kr_recv recv
#if defined(_POSIX_VERSION)
#include <sys/socket.h>
#endif
#endif
#if !defined(KR_EXT_RANDOM)
#if defined(_POSIX_VERSION)
#define KR_RANDOM_SOURCE_FILE "/dev/urandom"
#else
#define KR_USE_RAND
#endif
#endif
#endif /* _KEXTERNS_H */
#ifdef NS_MODULE_LINES
#line 1 "src/src/crypto.h"
/**/
#endif
/*
* Copyright (c) 2015 Cesanta Software Limited
* All rights reserved
*/
#ifndef _CRYPTO_H
#define _CRYPTO_H
NS_INTERNAL int get_random_nonzero(uint8_t *out, size_t len);
/* axTLS crypto functions, see C files for copyright info */
typedef struct _SHA256_CTX SHA256_CTX;
NS_INTERNAL void prf(const uint8_t *sec, size_t sec_len, const uint8_t *seed,
size_t seed_len, uint8_t *out, size_t olen);
/* SHA256 */
#define SHA256_SIZE 32
#define SHA256_BLOCK_LENGTH 64
struct _SHA256_CTX {
uint32_t state[8];
uint64_t bitcount;
uint8_t buffer[SHA256_BLOCK_LENGTH];
};
NS_INTERNAL void SHA256_Init(SHA256_CTX *c);
NS_INTERNAL void SHA256_Update(SHA256_CTX *, const uint8_t *input, size_t len);
NS_INTERNAL void SHA256_Final(uint8_t digest[32], SHA256_CTX *);
#define SHA1_SIZE 20
#define MD5_SIZE 16
#define MAX_DIGEST_SIZE SHA256_SIZE
#define AES_IV_SIZE 16
#define MAX_IV_SIZE AES_IV_SIZE
#define RC4_KEY_SIZE 16
#define AES128_KEY_SIZE 16
#define AES256_KEY_SIZE 32
#define MAX_KEY_SIZE AES256_KEY_SIZE
/* RSA */
NS_INTERNAL void RSA_priv_key_new(RSA_CTX **rsa_ctx, const uint8_t *modulus,
int mod_len, const uint8_t *pub_exp,
int pub_len, const uint8_t *priv_exp,
int priv_len, const uint8_t *p, int p_len,
const uint8_t *q, int q_len,
const uint8_t *dP, int dP_len,
const uint8_t *dQ, int dQ_len,
const uint8_t *qInv, int qInv_len);
NS_INTERNAL void RSA_pub_key_new(RSA_CTX **rsa_ctx, const uint8_t *modulus,
int mod_len, const uint8_t *pub_exp,
int pub_len);
NS_INTERNAL void RSA_free(RSA_CTX *ctx);
NS_INTERNAL int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data,
uint8_t *out_data, int out_len, int is_decryption);
NS_INTERNAL bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg);
NS_INTERNAL int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data,
uint16_t in_len, uint8_t *out_data, int is_signing);
NS_INTERNAL bigint *RSA_public(const RSA_CTX *c, bigint *bi_msg);
NS_INTERNAL int RSA_block_size(RSA_CTX *ctx);
#if defined(CONFIG_SSL_CERT_VERIFICATION) || \
defined(CONFIG_SSL_GENERATE_X509_CERT)
NS_INTERNAL bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig,
int sig_len, bigint *modulus,
bigint *pub_exp);
NS_INTERNAL void RSA_print(const RSA_CTX *ctx);
#endif
/* Faster modular arithmetic, bigger code */
#define CONFIG_BIGINT_BARRETT 1
/* faster multiplies, bigger code, only worth it for bigger keys or systems
* with very slow multiplys. Not worth it on x86.
*/
/* #define CONFIG_BIGINT_KARATSUBA 1 */
#define MUL_KARATSUBA_THRESH 20
#define SQU_KARATSUBA_THRESH 40
NS_INTERNAL int kr_hmac_len(kr_cs_id cs);
/* cs = 0 -> client MAC, cs = 1 -> server MAC. */
#define KR_CLIENT_MAC 0
#define KR_SERVER_MAC 1
NS_INTERNAL void kr_ssl_hmac(SSL *ssl, int cs, size_t num_msgs,
const uint8_t *msgs[], const size_t *msg_lens,
uint8_t *digest);
typedef void (*kr_hash_func_t)(size_t, const uint8_t **, const size_t *,
uint8_t *);
NS_INTERNAL void kr_hmac_v(kr_hash_func_t hash_func, const uint8_t *key,
size_t key_len, size_t num_msgs,
const uint8_t *msgs[], const size_t *msg_lens,
uint8_t *digest, size_t digest_len);
typedef struct {
uint8_t block_len;
uint8_t key_len;
uint8_t iv_len;
} kr_cipher_info;
NS_INTERNAL const kr_cipher_info *kr_cipher_get_info(kr_cs_id cs);
NS_INTERNAL void *kr_cipher_setup(kr_cs_id cs, int decrypt, const uint8_t *key,
const uint8_t *iv);
NS_INTERNAL void kr_cipher_ctx_free(kr_cs_id cs, void *ctx);
NS_INTERNAL void kr_cipher_set_iv(kr_cs_id cs, void *ctx, const uint8_t *iv);
NS_INTERNAL void kr_cipher_encrypt(kr_cs_id cs, void *ctx, const uint8_t *msg,
int len, uint8_t *out);
NS_INTERNAL void kr_cipher_decrypt(kr_cs_id cs, void *ctx, const uint8_t *msg,
int len, uint8_t *out);
#endif /* _CRYPTO_H */
#ifdef NS_MODULE_LINES
#line 1 "src/src/bigint_impl.h"
/**/
#endif
/*
* Copyright (c) 2007, Cameron Rich
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the axTLS project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef BIGINT_IMPL_HEADER
#define BIGINT_IMPL_HEADER
/* Maintain a number of precomputed variables when doing reduction */
#define BIGINT_M_OFFSET 0 /**< Normal modulo offset. */
#define BIGINT_P_OFFSET 1 /**< p modulo offset. */
#define BIGINT_Q_OFFSET 2 /**< q module offset. */
#define BIGINT_NUM_MODS 3 /**< The number of modulus constants used. */
/* Architecture specific functions for big ints */
#if defined(CONFIG_INTEGER_8BIT)
#define COMP_RADIX 256U /**< Max component + 1 */
#define COMP_MAX 0xFFFFU /**< (Max dbl comp -1) */
#define COMP_BIT_SIZE 8 /**< Number of bits in a component. */
#define COMP_BYTE_SIZE 1 /**< Number of bytes in a component. */
#define COMP_NUM_NIBBLES 2 /**< Used For diagnostics only. */
typedef uint8_t comp; /**< A single precision component. */
typedef uint16_t long_comp; /**< A double precision component. */
typedef int16_t slong_comp; /**< A signed double precision component. */
#elif defined(CONFIG_INTEGER_16BIT)
#define COMP_RADIX 65536U /**< Max component + 1 */
#define COMP_MAX 0xFFFFFFFFU /**< (Max dbl comp -1) */
#define COMP_BIT_SIZE 16 /**< Number of bits in a component. */
#define COMP_BYTE_SIZE 2 /**< Number of bytes in a component. */
#define COMP_NUM_NIBBLES 4 /**< Used For diagnostics only. */
typedef uint16_t comp; /**< A single precision component. */
typedef uint32_t long_comp; /**< A double precision component. */
typedef int32_t slong_comp; /**< A signed double precision component. */
#else /* regular 32 bit */
#ifdef WIN32
#define COMP_RADIX 4294967296i64
#define COMP_MAX 0xFFFFFFFFFFFFFFFFui64
#else
#define COMP_RADIX 4294967296ULL /**< Max component + 1 */
#define COMP_MAX 0xFFFFFFFFFFFFFFFFULL /**< (Max dbl comp -1) */
#endif
#define COMP_BIT_SIZE 32 /**< Number of bits in a component. */
#define COMP_BYTE_SIZE 4 /**< Number of bytes in a component. */
#define COMP_NUM_NIBBLES 8 /**< Used For diagnostics only. */
typedef uint32_t comp; /**< A single precision component. */
typedef uint64_t long_comp; /**< A double precision component. */
typedef int64_t slong_comp; /**< A signed double precision component. */
#endif
/**
* @struct _bigint
* @brief A big integer basic object
*/
struct _bigint {
struct _bigint *next; /**< The next bigint in the cache. */
short size; /**< The number of components in this bigint. */
short max_comps; /**< The heapsize allocated for this bigint */
int refs; /**< An internal reference count. */
comp *comps; /**< A ptr to the actual component data */
};
/**
* Maintains the state of the cache, and a number of variables used in
* reduction.
*/
struct _BI_CTX /**< A big integer "session" context. */
{
bigint *active_list; /**< Bigints currently used. */
bigint *free_list; /**< Bigints not used. */
bigint *bi_radix; /**< The radix used. */
bigint *bi_mod[BIGINT_NUM_MODS]; /**< modulus */
#if defined(CONFIG_BIGINT_MONTGOMERY)
bigint *bi_RR_mod_m[BIGINT_NUM_MODS]; /**< R^2 mod m */
bigint *bi_R_mod_m[BIGINT_NUM_MODS]; /**< R mod m */
comp N0_dash[BIGINT_NUM_MODS];
#elif defined(CONFIG_BIGINT_BARRETT)
bigint *bi_mu[BIGINT_NUM_MODS]; /**< Storage for mu */
#endif
bigint *bi_normalised_mod[BIGINT_NUM_MODS]; /**< Normalised mod storage. */
bigint **g; /**< Used by sliding-window. */
int window; /**< The size of the sliding window */
int active_count; /**< Number of active bigints. */
int free_count; /**< Number of free bigints. */
#ifdef CONFIG_BIGINT_MONTGOMERY
uint8_t use_classical; /**< Use classical reduction. */
#endif
uint8_t mod_offset; /**< The mod offset we are using */
};
typedef struct _BI_CTX BI_CTX;
#ifndef WIN32
#define max(a, b) \
((a) > (b) ? (a) : (b)) /**< Find the maximum of 2 numbers. \
*/
#define min(a, b) \
((a) < (b) ? (a) : (b)) /**< Find the minimum of 2 numbers. \
*/
#endif
#define PERMANENT 0x7FFF55AA /**< A magic number for permanents. */
#endif
#ifdef NS_MODULE_LINES
#line 1 "src/src/bigint.h"
/**/
#endif
/*
* Copyright (c) 2007, Cameron Rich
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the axTLS project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef BIGINT_HEADER
#define BIGINT_HEADER
NS_INTERNAL BI_CTX *bi_initialize(void);
NS_INTERNAL void bi_terminate(BI_CTX *ctx);
NS_INTERNAL void bi_permanent(bigint *bi);
NS_INTERNAL void bi_depermanent(bigint *bi);
NS_INTERNAL void bi_clear_cache(BI_CTX *ctx);
NS_INTERNAL void bi_free(BI_CTX *ctx, bigint *bi);
NS_INTERNAL bigint *bi_copy(bigint *bi);
NS_INTERNAL bigint *bi_clone(BI_CTX *ctx, const bigint *bi);
NS_INTERNAL void bi_export(BI_CTX *ctx, bigint *bi, uint8_t *data, int size);
NS_INTERNAL bigint *bi_import(BI_CTX *ctx, const uint8_t *data, int len);
NS_INTERNAL bigint *int_to_bi(BI_CTX *ctx, comp i);
/* the functions that actually do something interesting */
NS_INTERNAL bigint *bi_add(BI_CTX *ctx, bigint *bia, bigint *bib);
NS_INTERNAL bigint *bi_subtract(BI_CTX *ctx, bigint *bia, bigint *bib,
int *is_negative);
NS_INTERNAL bigint *bi_divide(BI_CTX *ctx, bigint *bia, bigint *bim,
int is_mod);
NS_INTERNAL bigint *bi_multiply(BI_CTX *ctx, bigint *bia, bigint *bib);
NS_INTERNAL bigint *bi_mod_power(BI_CTX *ctx, bigint *bi, bigint *biexp);
#if 0
NS_INTERNAL bigint *bi_mod_power2(BI_CTX *ctx, bigint *bi,
bigint *bim, bigint *biexp);
#endif
NS_INTERNAL int bi_compare(bigint *bia, bigint *bib);
NS_INTERNAL void bi_set_mod(BI_CTX *ctx, bigint *bim, int mod_offset);
NS_INTERNAL void bi_free_mod(BI_CTX *ctx, int mod_offset);
#ifdef CONFIG_SSL_FULL_MODE
NS_INTERNAL void bi_print(const char *label, bigint *bi);
NS_INTERNAL bigint *bi_str_import(BI_CTX *ctx, const char *data);
#endif
/**
* @def bi_mod
* Find the residue of B. bi_set_mod() must be called before hand.
*/
#define bi_mod(A, B) bi_divide(A, B, ctx->bi_mod[ctx->mod_offset], 1)
/**
* bi_residue() is technically the same as bi_mod(), but it uses the
* appropriate reduction technique (which is bi_mod() when doing classical
* reduction).
*/
#if defined(CONFIG_BIGINT_MONTGOMERY)
#define bi_residue(A, B) bi_mont(A, B)
NS_INTERNAL bigint *bi_mont(BI_CTX *ctx, bigint *bixy);
#elif defined(CONFIG_BIGINT_BARRETT)
#define bi_residue(A, B) bi_barrett(A, B)
NS_INTERNAL bigint *bi_barrett(BI_CTX *ctx, bigint *bi);
#else /* if defined(CONFIG_BIGINT_CLASSICAL) */
#define bi_residue(A, B) bi_mod(A, B)
#endif
#ifdef CONFIG_BIGINT_SQUARE
NS_INTERNAL bigint *bi_square(BI_CTX *ctx, bigint *bi);
#else
#define bi_square(A, B) bi_multiply(A, bi_copy(B), B)
#endif
NS_INTERNAL bigint *bi_crt(BI_CTX *ctx, bigint *bi, bigint *dP, bigint *dQ,
bigint *p, bigint *q, bigint *qInv);
#endif
#ifdef NS_MODULE_LINES
#line 1 "src/src/tls.h"
/**/
#endif
/*
* Copyright (c) 2015 Cesanta Software Limited
* All rights reserved
*/
#ifndef _TLS_H
#define _TLS_H
typedef struct tls_security {
/*
* client_write_MAC_key
* server_write_MAC_key
* client_write_key
* server_write_key
*/
uint8_t keys[MAX_DIGEST_SIZE * 2 + MAX_KEY_SIZE * 2 + MAX_IV_SIZE * 2];
uint64_t client_write_seq;
uint64_t server_write_seq;
uint16_t cipher_suite;
uint16_t peer_vers;
uint8_t compressor;
uint8_t cipher_negotiated : 1;
uint8_t compressor_negotiated : 1;
uint8_t bitpad : 6;
RSA_CTX *svr_key;
uint8_t master_secret[48];
struct tls_random cl_rnd;
struct tls_random sv_rnd;
void *server_write_ctx;
void *client_write_ctx;
SHA256_CTX handshakes_hash;
} * tls_sec_t;
NS_INTERNAL tls_sec_t tls_new_security(void);
NS_INTERNAL void tls_free_security(tls_sec_t sec);
/* generic */
NS_INTERNAL int tls_handle_recv(SSL *ssl, uint8_t *out, size_t out_len);
NS_INTERNAL void tls_generate_keys(tls_sec_t sec, int is_server);
NS_INTERNAL int tls_send(SSL *ssl, uint8_t type, const void *buf, size_t len);
NS_INTERNAL int tls_tx_push(SSL *ssl, const void *data, size_t len);
NS_INTERNAL ssize_t tls_write(SSL *ssl, const uint8_t *buf, size_t sz);
NS_INTERNAL int tls_alert(SSL *ssl, uint8_t level, uint8_t desc);
NS_INTERNAL int tls_close_notify(SSL *ssl);
/* client */
NS_INTERNAL int tls_cl_finish(SSL *ssl);
NS_INTERNAL int tls_cl_hello(SSL *ssl);
NS_INTERNAL int tls_check_server_finished(tls_sec_t sec, const uint8_t *vrfy,
size_t vrfy_len);
NS_INTERNAL void tls_generate_client_finished(tls_sec_t sec, uint8_t *vrfy,
size_t vrfy_len);
/* server */
NS_INTERNAL int tls_sv_hello(SSL *ssl);
NS_INTERNAL int tls_sv_finish(SSL *ssl);
NS_INTERNAL int tls_check_client_finished(tls_sec_t sec, const uint8_t *vrfy,
size_t vrfy_len);
NS_INTERNAL void tls_generate_server_finished(tls_sec_t sec, uint8_t *vrfy,
size_t vrfy_len);
NS_INTERNAL void tls_compute_master_secret(tls_sec_t sec,
struct tls_premaster_secret *pre);
#endif /* _TLS_H */
#ifdef NS_MODULE_LINES
#line 1 "src/src/ber.h"
/**/
#endif
/*
* Copyright (c) 2010 Gianni Tedesco <gianni@scaramanga.co.uk>
* Released under the MIT license.
*/
#ifndef _GBER_H
#define _GBER_H
typedef uint16_t gber_tag_t;
struct gber_tag {
size_t ber_len;
gber_tag_t ber_tag;
uint8_t ber_id;
};
#define GBER_CLASS_UNIVERSAL 0
#define GBER_CLASS_APPLICATION 1
#define GBER_CLASS_CONTEXT 2
#define GBER_CLASS_PRIVATE 3
/* returns start of data if present or NULL if truncated */
NS_INTERNAL const uint8_t *ber_decode_tag(struct gber_tag *tag,
const uint8_t *ptr, size_t len);
NS_INTERNAL unsigned int ber_id_octet_constructed(uint8_t id);
#if 0
NS_INTERNAL const uint8_t *ber_tag_info(struct gber_tag *tag,
const uint8_t *ptr, size_t len);
NS_INTERNAL unsigned int ber_id_octet_class(uint8_t id);
#endif
#if KRYPTON_DEBUG
NS_INTERNAL const char *ber_id_octet_clsname(uint8_t id);
int ber_dump(const uint8_t *ptr, size_t len);
int ber_dumpf(FILE *f, const uint8_t *ptr, size_t len);
#endif
#endif /* _GBER_H */
#ifdef NS_MODULE_LINES
#line 1 "src/src/pem.h"
/**/
#endif
/*
* Copyright (c) 2015 Cesanta Software Limited
* All rights reserved
*/
#ifndef _PEM_H
#define _PEM_H
struct pem_st {
unsigned int tot_len;
uint16_t num_obj;