forked from aws/s2n-tls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s2n.h
3262 lines (2967 loc) · 142 KB
/
s2n.h
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
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
/**
* @file s2n.h
* s2n-tls is a C99 implementation of the TLS/SSL protocols that is designed to
* be simple, small, fast, and with security as a priority. <br> It is released and
* licensed under the Apache License 2.0.
*/
#pragma once
#if ((__GNUC__ >= 4) || defined(__clang__)) && defined(S2N_EXPORTS)
/**
* Marks a function as belonging to the public s2n API.
*/
#define S2N_API __attribute__((visibility("default")))
#else
/**
* Marks a function as belonging to the public s2n API.
*/
#define S2N_API
#endif /* __GNUC__ >= 4 || defined(__clang__) */
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/uio.h>
/**
* Function return code
*/
#define S2N_SUCCESS 0
/**
* Function return code
*/
#define S2N_FAILURE -1
/**
* Callback return code
*/
#define S2N_CALLBACK_BLOCKED -2
/**
* s2n minimum supported TLS record major version
*/
#define S2N_MINIMUM_SUPPORTED_TLS_RECORD_MAJOR_VERSION 2
/**
* s2n maximum supported TLS record major version
*/
#define S2N_MAXIMUM_SUPPORTED_TLS_RECORD_MAJOR_VERSION 3
/**
* s2n SSL 2.0 Version Constant
*/
#define S2N_SSLv2 20
/**
* s2n SSL 3.0 Version Constant
*/
#define S2N_SSLv3 30
/**
* s2n TLS 1.0 Version Constant
*/
#define S2N_TLS10 31
/**
* s2n TLS 1.1 Version Constant
*/
#define S2N_TLS11 32
/**
* s2n TLS 1.2 Version Constant
*/
#define S2N_TLS12 33
/**
* s2n TLS 1.3 Version Constant
*/
#define S2N_TLS13 34
/**
* s2n Unknown TLS Version
*/
#define S2N_UNKNOWN_PROTOCOL_VERSION 0
/**
* s2n-tls functions that return 'int' return 0 to indicate success and -1 to indicate
* failure.
*
* s2n-tls functions that return pointer types return NULL in the case of
* failure.
*
* When an s2n-tls function returns a failure, s2n_errno will be set to a value
* corresponding to the error. This error value can be translated into a string
* explaining the error in English by calling s2n_strerror(s2n_errno, "EN").
* A string containing human readable error name; can be generated with `s2n_strerror_name`.
* A string containing internal debug information, including filename and line number, can be generated with `s2n_strerror_debug`.
* This string is useful to include when reporting issues to the s2n-tls development team.
*
* @warning To avoid possible confusion, s2n_errno should be cleared after processing an error: `s2n_errno = S2N_ERR_T_OK`
*/
S2N_API extern __thread int s2n_errno;
/**
* This function can be used instead of trying to resolve `s2n_errno` directly
* in runtimes where thread-local variables may not be easily accessible.
*
* @returns The address of the thread-local `s2n_errno` variable
*/
S2N_API extern int *s2n_errno_location(void);
/**
* Used to help applications determine why an s2n-tls function failed.
*
* This enum is optimized for use in C switch statements. Each value in the enum represents
* an error "category".
*
* s2n-tls organizes errors into different "types" to allow applications to handle error
* values without catching all possibilities. Applications using non-blocking I/O should check
* the error type to determine if the I/O operation failed because it would block or for some other
* error. To retrieve the type for a given error use `s2n_error_get_type()`. Applications should
* perform any error handling logic using these high level types.
*
* See the [Error Handling](https://github.com/aws/s2n-tls/blob/main/docs/USAGE-GUIDE.md#error-handling) section for how the errors should be interpreted.
*/
typedef enum {
/** No error */
S2N_ERR_T_OK = 0,
/** Underlying I/O operation failed, check system errno */
S2N_ERR_T_IO,
/** EOF */
S2N_ERR_T_CLOSED,
/** Underlying I/O operation would block */
S2N_ERR_T_BLOCKED,
/** Incoming Alert */
S2N_ERR_T_ALERT,
/** Failure in some part of the TLS protocol. Ex: CBC verification failure */
S2N_ERR_T_PROTO,
/** Error internal to s2n-tls. A precondition could have failed. */
S2N_ERR_T_INTERNAL,
/** User input error. Ex: Providing an invalid cipher preference version */
S2N_ERR_T_USAGE
} s2n_error_type;
/**
* Gets the category of error from an error.
*
* s2n-tls organizes errors into different "types" to allow applications to do logic on error values without catching all possibilities.
* Applications using non-blocking I/O should check error type to determine if the I/O operation failed because
* it would block or for some other error.
*
* @param error The error from s2n. Usually this is `s2n_errno`.
* @returns An s2n_error_type
*/
S2N_API extern int s2n_error_get_type(int error);
/**
* An opaque configuration object, used by clients and servers for holding cryptographic certificates, keys and preferences.
*/
struct s2n_config;
/**
* An opaque connection. Used to track each s2n connection.
*/
struct s2n_connection;
/**
* Prevents S2N from calling `OPENSSL_init_crypto`/`OPENSSL_cleanup`/`EVP_cleanup` on OpenSSL versions
* prior to 1.1.x. This allows applications or languages that also init OpenSSL to interoperate
* with S2N.
*
* @warning This function must be called BEFORE s2n_init() to have any effect. It will return an error
* if s2n is already initialized.
*
* @note If you disable this and are using a version of OpenSSL/libcrypto < 1.1.x, you will
* be responsible for library init and cleanup (specifically `OPENSSL_add_all_algorithms()`
* or `OPENSSL_init_crypto()`, and EVP_* APIs will not be usable unless the library is initialized.
*
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_crypto_disable_init(void);
/**
* Prevents S2N from installing an atexit handler, which allows safe shutdown of S2N from within a
* re-entrant shared library
*
* @warning This function must be called BEFORE s2n_init() to have any effect. It will return an error
* if s2n is already initialized.
*
* @note This will cause `s2n_cleanup` to do complete cleanup of s2n-tls when called from the main
* thread (the thread `s2n_init` was called from).
*
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_disable_atexit(void);
/**
* Fetches the OpenSSL version s2n-tls was compiled with. This can be used by applications to validate at runtime
* that the versions of s2n-tls and Openssl that they have loaded are correct.
*
* @returns the version number of OpenSSL that s2n-tls was compiled with
*/
S2N_API extern unsigned long s2n_get_openssl_version(void);
/**
* Initializes the s2n-tls library and should be called once in your application, before any other s2n-tls
* functions are called. Failure to call s2n_init() will result in errors from other s2n-tls functions.
*
* @warning This function is not thread safe and should only be called once.
*
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_init(void);
/**
* Cleans up any internal resources used by s2n-tls. This function should be called from each thread or process
* that is created subsequent to calling `s2n_init` when that thread or process is done calling other s2n-tls functions.
*
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_cleanup(void);
/**
* Create a new s2n_config object. This object can (and should) be associated with many connection objects.
*
* @returns returns a new configuration object suitable for associating certs and keys.
*/
S2N_API extern struct s2n_config *s2n_config_new(void);
/**
* Frees the memory associated with an `s2n_config` object.
*
* @param config The configuration object being freed
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_free(struct s2n_config *config);
/**
* Frees the DH params associated with an `s2n_config` object.
*
* @param config The configuration object with DH params being freed
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_free_dhparams(struct s2n_config *config);
/**
* Frees the certificate chain and key associated with an `s2n_config` object.
*
* @param config The configuration object with DH params being freed
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_free_cert_chain_and_key(struct s2n_config *config);
/**
* Callback function type used to get the system time.
*
* @param void* A pointer to arbitrary data for use within the callback
* @param uint64_t* A pointer that the callback will set to the time in nanoseconds
* The function should return 0 on success and -1 on failure.
*/
typedef int (*s2n_clock_time_nanoseconds)(void *, uint64_t *);
/**
* Cache callback function that allows the caller to retrieve SSL session data
* from a cache.
*
* The callback function takes six arguments:
* a pointer to the s2n_connection object,
* a pointer to arbitrary data for use within the callback,
* a pointer to a key which can be used to retrieve the cached entry,
* a 64 bit unsigned integer specifying the size of this key,
* a pointer to a memory location where the value should be stored,
* and a pointer to a 64 bit unsigned integer specifying the size of this value.
*
* Initially *value_size will be set to the amount of space allocated for the value,
* the callback should set *value_size to the actual size of the data returned.
* If there is insufficient space, -1 should be returned.
* If the cache is not ready to provide data for the request,
* S2N_CALLBACK_BLOCKED should be returned.
*
* This will cause s2n_negotiate() to return S2N_BLOCKED_ON_APPLICATION_INPUT.
*/
typedef int (*s2n_cache_retrieve_callback)(struct s2n_connection *conn, void *, const void *key, uint64_t key_size, void *value, uint64_t *value_size);
/**
* Cache callback function that allows the caller to store SSL session data in a
* cache.
*
* The callback function takes seven arguments:
* a pointer to the s2n_connection object,
* a pointer to arbitrary data for use within the callback,
* a 64-bit unsigned integer specifying the number of seconds the session data may be stored for,
* a pointer to a key which can be used to retrieve the cached entry,
* a 64 bit unsigned integer specifying the size of this key,
* a pointer to a value which should be stored,
* and a 64 bit unsigned integer specified the size of this value.
*/
typedef int (*s2n_cache_store_callback)(struct s2n_connection *conn, void *, uint64_t ttl_in_seconds, const void *key, uint64_t key_size, const void *value, uint64_t value_size);
/**
* Cache callback function that allows the caller to set a callback function
* that will be used to delete SSL session data from a cache.
*
* The callback function takes four arguments:
* a pointer to s2n_connection object,
* a pointer to arbitrary data for use within the callback,
* a pointer to a key which can be used to delete the cached entry,
* and a 64 bit unsigned integer specifying the size of this key.
*/
typedef int (*s2n_cache_delete_callback)(struct s2n_connection *conn, void *, const void *key, uint64_t key_size);
/**
* Allows the caller to set a callback function that will be used to get the
* system time. The time returned should be the number of nanoseconds since the
* Unix epoch (Midnight, January 1st, 1970).
*
* s2n-tls uses this clock for timestamps.
*
* @param config The configuration object being updated
* @param clock_fn The wall clock time callback function
* @param ctx An opaque pointer that the callback will be invoked with
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_set_wall_clock(struct s2n_config *config, s2n_clock_time_nanoseconds clock_fn, void *ctx);
/**
* Allows the caller to set a callback function that will be used to get
* monotonic time. The monotonic time is the time since an arbitrary, unspecified
* point. Unlike wall clock time, it MUST never move backwards.
*
* s2n-tls uses this clock for timers.
*
* @param config The configuration object being updated
* @param clock_fn The monotonic time callback function
* @param ctx An opaque pointer that the callback will be invoked with
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_set_monotonic_clock(struct s2n_config *config, s2n_clock_time_nanoseconds clock_fn, void *ctx);
/**
* Translates an s2n_error code to a human readable string explaining the error.
*
* @param error The error code to explain. Usually this is s2n_errno
* @param lang The language to explain the error code. Pass "EN" or NULL for English.
* @returns The error string
*/
S2N_API extern const char *s2n_strerror(int error, const char *lang);
/**
* Translates an s2n_error code to a human readable string containing internal debug
* information, including file name and line number. This function is useful when
* reporting issues to the s2n-tls development team.
*
* @param error The error code to explain. Usually this is s2n_errno
* @param lang The language to explain the error code. Pass "EN" or NULL for English.
* @returns The error string
*/
S2N_API extern const char *s2n_strerror_debug(int error, const char *lang);
/**
* Translates an s2n_error code to a human readable string.
*
* @param error The error code to explain. Usually this is s2n_errno
* @returns The error string
*/
S2N_API extern const char *s2n_strerror_name(int error);
/**
* Opaque stack trace structure.
*/
struct s2n_stacktrace;
/**
* Checks if s2n stack trace captures are enabled.
*
* @returns True if stack traces are enabled. False if they are disabled.
*/
S2N_API extern bool s2n_stack_traces_enabled(void);
/**
* Configures the s2n stack trace captures option.
*
* @param newval Boolean to determine if stack traces should be enabled. True to enable them. False to disable them.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_stack_traces_enabled_set(bool newval);
/**
* Calculates the s2n stack trace.
*
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_calculate_stacktrace(void);
/**
* Prints the s2n stack trace to a file. The file descriptor is expected to be
* open and ready for writing.
*
* @param fptr A pointer to the file s2n-tls should write the stack trace to.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_print_stacktrace(FILE *fptr);
/**
* Clean up the memory used to contain the stack trace.
*
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_free_stacktrace(void);
/**
* Export the s2n_stacktrace.
*
* @param trace A pointer to the s2n_stacktrace to fill.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_get_stacktrace(struct s2n_stacktrace *trace);
/**
* Allows the caller to set a callback function that will be used to store SSL
* session data in a cache.
*
* @param config The configuration object being updated
* @param cache_store_callback The cache store callback function.
* @param data An opaque context pointer that the callback will be invoked with.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_set_cache_store_callback(struct s2n_config *config, s2n_cache_store_callback cache_store_callback, void *data);
/**
* Allows the caller to set a callback function that will be used to retrieve SSL
* session data from a cache.
*
* @param config The configuration object being updated
* @param cache_retrieve_callback The cache retrieve callback function.
* @param data An opaque context pointer that the callback will be invoked with.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_set_cache_retrieve_callback(struct s2n_config *config, s2n_cache_retrieve_callback cache_retrieve_callback, void *data);
/**
* Allows the caller to set a callback function that will be used to delete SSL
* session data from a cache.
*
* @param config The configuration object being updated
* @param cache_delete_callback The cache delete callback function.
* @param data An opaque context pointer that the callback will be invoked with.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_set_cache_delete_callback(struct s2n_config *config, s2n_cache_delete_callback cache_delete_callback, void *data);
/**
* Called when `s2n_init` is executed.
*/
typedef int (*s2n_mem_init_callback)(void);
/**
* Will be called when `s2n_cleanup` is executed.
*/
typedef int (*s2n_mem_cleanup_callback)(void);
/**
* A function that can allocate at least `requested` bytes of memory.
*
* It stores the location of that memory in **\*ptr** and the size of the allocated
* data in **\*allocated**. The function may choose to allocate more memory
* than was requested. s2n-tls will consider all allocated memory available for
* use, and will attempt to free all allocated memory when able.
*/
typedef int (*s2n_mem_malloc_callback)(void **ptr, uint32_t requested, uint32_t *allocated);
/**
* Frees memory allocated by s2n_mem_malloc_callback.
*/
typedef int (*s2n_mem_free_callback)(void *ptr, uint32_t size);
/**
* Allows the caller to override s2n-tls's internal memory handling functions.
*
* @warning This function must be called before s2n_init().
*
* @param mem_init_callback The s2n_mem_init_callback
* @param mem_cleanup_callback The s2n_mem_cleanup_callback
* @param mem_malloc_callback The s2n_mem_malloc_callback
* @param mem_free_callback The s2n_mem_free_callback
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_mem_set_callbacks(s2n_mem_init_callback mem_init_callback, s2n_mem_cleanup_callback mem_cleanup_callback,
s2n_mem_malloc_callback mem_malloc_callback, s2n_mem_free_callback mem_free_callback);
/**
* A callback function that will be called when s2n-tls is initialized.
*/
typedef int (*s2n_rand_init_callback)(void);
/**
* A callback function that will be called when `s2n_cleanup` is executed.
*/
typedef int (*s2n_rand_cleanup_callback)(void);
/**
* A callback function that will be used to provide entropy to the s2n-tls
* random number generators.
*/
typedef int (*s2n_rand_seed_callback)(void *data, uint32_t size);
/**
* A callback function that will be used to mix in entropy every time the RNG
* is invoked.
*/
typedef int (*s2n_rand_mix_callback)(void *data, uint32_t size);
/**
* Allows the caller to override s2n-tls's entropy functions.
*
* @warning This function must be called before s2n_init().
*
* @param rand_init_callback The s2n_rand_init_callback
* @param rand_cleanup_callback The s2n_rand_cleanup_callback
* @param rand_seed_callback The s2n_rand_seed_callback
* @param rand_mix_callback The s2n_rand_mix_callback
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_rand_set_callbacks(s2n_rand_init_callback rand_init_callback, s2n_rand_cleanup_callback rand_cleanup_callback,
s2n_rand_seed_callback rand_seed_callback, s2n_rand_mix_callback rand_mix_callback);
/**
* TLS extensions supported by s2n-tls
*/
typedef enum {
S2N_EXTENSION_SERVER_NAME = 0,
S2N_EXTENSION_MAX_FRAG_LEN = 1,
S2N_EXTENSION_OCSP_STAPLING = 5,
S2N_EXTENSION_SUPPORTED_GROUPS = 10,
S2N_EXTENSION_EC_POINT_FORMATS = 11,
S2N_EXTENSION_SIGNATURE_ALGORITHMS = 13,
S2N_EXTENSION_ALPN = 16,
S2N_EXTENSION_CERTIFICATE_TRANSPARENCY = 18,
S2N_EXTENSION_RENEGOTIATION_INFO = 65281,
} s2n_tls_extension_type;
/**
* MFL configurations from https://datatracker.ietf.org/doc/html/rfc6066#section-4.
*/
typedef enum {
S2N_TLS_MAX_FRAG_LEN_512 = 1,
S2N_TLS_MAX_FRAG_LEN_1024 = 2,
S2N_TLS_MAX_FRAG_LEN_2048 = 3,
S2N_TLS_MAX_FRAG_LEN_4096 = 4,
} s2n_max_frag_len;
/**
* Opaque certificate type.
*/
struct s2n_cert;
/**
* Opaque certificate chain and key type.
*/
struct s2n_cert_chain_and_key;
/**
* Opaque key type.
*/
struct s2n_pkey;
/**
* Opaque public key type.
*/
typedef struct s2n_pkey s2n_cert_public_key;
/**
* Opaque private key type.
*/
typedef struct s2n_pkey s2n_cert_private_key;
/**
* Creates a new s2n_cert_chain_and_key object. This object can be associated
* with many config objects. It is used to represent a certificate and key pair.
*
* @returns A new object used to represent a certificate-chain/key pair
*/
S2N_API extern struct s2n_cert_chain_and_key *s2n_cert_chain_and_key_new(void);
/**
* Associates a certificate chain and private key with an `s2n_cert_chain_and_key` object.
*
* `cert_chain_pem` should be a PEM encoded certificate chain, with the first
* certificate in the chain being your leaf certificate. `private_key_pem`
* should be a PEM encoded private key corresponding to the leaf certificate.
*
* @note Prefer using s2n_cert_chain_and_key_load_pem_bytes.
*
* @param chain_and_key The certificate chain and private key handle
* @param chain_pem A byte array of a PEM encoded certificate chain.
* @param private_key_pem A byte array of a PEM encoded key.
*
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_cert_chain_and_key_load_pem(struct s2n_cert_chain_and_key *chain_and_key, const char *chain_pem, const char *private_key_pem);
/**
* Associates a certificate chain and private key with an `s2n_cert_chain_and_key` object.
*
* `cert_chain_pem` should be a PEM encoded certificate chain, with the first
* certificate in the chain being your leaf certificate. `private_key_pem`
* should be a PEM encoded private key corresponding to the leaf certificate.
*
* @param chain_and_key The certificate chain and private key handle
* @param chain_pem A byte array of a PEM encoded certificate chain.
* @param chain_pem_len Size of `chain_pem`
* @param private_key_pem A byte array of a PEM encoded key.
* @param private_key_pem_len Size of `private_key_pem`
*
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_cert_chain_and_key_load_pem_bytes(struct s2n_cert_chain_and_key *chain_and_key, uint8_t *chain_pem, uint32_t chain_pem_len, uint8_t *private_key_pem, uint32_t private_key_pem_len);
/**
* Associates a public certificate chain with a `s2n_cert_chain_and_key` object. It does
* NOT set a private key, so the connection will need to be configured to
* [offload private key operations](https://github.com/aws/s2n-tls/blob/main/docs/USAGE-GUIDE.md#offloading-asynchronous-private-key-operations).
*
* @param chain_and_key The certificate chain and private key handle
* @param chain_pem A byte array of a PEM encoded certificate chain.
* @param chain_pem_len Size of `chain_pem`
*
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_cert_chain_and_key_load_public_pem_bytes(struct s2n_cert_chain_and_key *chain_and_key, uint8_t *chain_pem, uint32_t chain_pem_len);
/**
* Frees the memory associated with an `s2n_cert_chain_and_key` object.
*
* @param cert_and_key The certificate chain and private key handle
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_cert_chain_and_key_free(struct s2n_cert_chain_and_key *cert_and_key);
/**
* Adds a context to the `s2n_cert_chain_and_key` object.
*
* @param cert_and_key The certificate chain and private key handle
* @param ctx An opaque pointer to user supplied data.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_cert_chain_and_key_set_ctx(struct s2n_cert_chain_and_key *cert_and_key, void *ctx);
/**
* Get the user supplied context from the `s2n_cert_chain_and_key` object.
*
* @param cert_and_key The certificate chain and private key handle
* @returns The user supplied pointer from s2n_cert_chain_and_key_set_ctx()
*/
S2N_API extern void *s2n_cert_chain_and_key_get_ctx(struct s2n_cert_chain_and_key *cert_and_key);
/**
* Get the private key from the `s2n_cert_chain_and_key` object.
*
* @param cert_and_key The certificate chain and private key handle
* @returns A pointer to the `s2n_cert_private_key`
*/
S2N_API extern s2n_cert_private_key *s2n_cert_chain_and_key_get_private_key(struct s2n_cert_chain_and_key *cert_and_key);
/**
* Set the raw OCSP stapling data for a certificate chain.
*
* @param chain_and_key The certificate chain handle
* @param data A pointer to the raw OCSP stapling data bytes. The data will be copied.
* @param length The length of the data bytes.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_cert_chain_and_key_set_ocsp_data(struct s2n_cert_chain_and_key *chain_and_key, const uint8_t *data, uint32_t length);
/**
* Set the signed certificate timestamp (SCT) for a certificate chain.
* This is used for Certificate Transparency.
*
* @param chain_and_key The certificate chain handle
* @param data A pointer to the SCT data. The data will be copied.
* @param length The length of the data bytes.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_cert_chain_and_key_set_sct_list(struct s2n_cert_chain_and_key *chain_and_key, const uint8_t *data, uint32_t length);
/**
* A callback function that is invoked if s2n-tls cannot resolve a conflict between
* two certificates with the same domain name. This function is invoked while certificates
* are added to an `s2n_config`.
*
* Currently, the only builtin resolution for domain name conflicts is certificate type(RSA,
* ECDSA, etc). The callback should return a pointer to the `s2n_cert_chain_and_key` that
* should be used for dns name `name`.
*
* If NULL is returned, the first certificate will be used. Typically an application
* will use properties like trust and expiry to implement tiebreaking.
*/
typedef struct s2n_cert_chain_and_key *(*s2n_cert_tiebreak_callback)(struct s2n_cert_chain_and_key *cert1, struct s2n_cert_chain_and_key *cert2, uint8_t *name, uint32_t name_len);
/**
* Sets the `s2n_cert_tiebreak_callback` for resolving domain name conflicts.
* If no callback is set, the first certificate added for a domain name will always be preferred.
*
* @param config The configuration object being updated
* @param cert_tiebreak_cb The pointer to the certificate tiebreak function
*
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_set_cert_tiebreak_callback(struct s2n_config *config, s2n_cert_tiebreak_callback cert_tiebreak_cb);
/**
* Associates a certificate chain and private key with an `s2n_config` object.
* Using this API, only one cert chain of each type (like ECDSA or RSA) may be associated with a config.
* `cert_chain_pem` should be a PEM encoded certificate chain, with the first certificate
* in the chain being your server's certificate. `private_key_pem` should be a
* PEM encoded private key corresponding to the server certificate.
*
* @deprecated Use s2n_config_add_cert_chain_and_key_to_store instead.
*
* @param config The configuration object being updated
* @param cert_chain_pem A byte array of a PEM encoded certificate chain.
* @param private_key_pem A byte array of a PEM encoded key.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure.
*/
S2N_API extern int s2n_config_add_cert_chain_and_key(struct s2n_config *config, const char *cert_chain_pem, const char *private_key_pem);
/**
* The preferred method of associating a certificate chain and private key pair with an `s2n_config` object.
* This method may be called multiple times to support multiple key types(RSA, ECDSA) and multiple domains.
* On the server side, the certificate selected will be based on the incoming SNI value and the
* client's capabilities(supported ciphers).
*
* In the case of no certificate matching the client's SNI extension or if no SNI extension was sent by
* the client, the certificate from the `first` call to `s2n_config_add_cert_chain_and_key_to_store`
* will be selected.
*
* @warning It is not recommended to free or modify the `cert_key_pair` as any subsequent changes will be
* reflected in the config.
*
* @param config The configuration object being updated
* @param cert_key_pair The certificate chain and private key handle
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_add_cert_chain_and_key_to_store(struct s2n_config *config, struct s2n_cert_chain_and_key *cert_key_pair);
/**
* Explicitly sets certificate chain and private key pairs to be used as defaults for each auth
* method (key type). A "default" certificate is used when there is not an SNI match with any other
* configured certificate.
*
* Only one certificate can be set as the default per auth method (one RSA default, one ECDSA default,
* etc.). All previous default certificates will be cleared and re-set when this API is called.
*
* This API is called for a specific `s2n_config` object. s2n-tls will attempt to automatically choose
* default certificates for each auth method (key type) based on the order that `s2n_cert_chain_and_key`
* are added to the `s2n_config` using one of the APIs listed above.
* `s2n_config_set_cert_chain_and_key_defaults` can be called at any time; s2n-tls will clear defaults
* and no longer attempt to automatically choose any default certificates.
*
* @param config The configuration object being updated
* @param cert_key_pairs An array of certificate chain and private key handles
* @param num_cert_key_pairs The amount of handles in cert_key_pairs
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_set_cert_chain_and_key_defaults(struct s2n_config *config,
struct s2n_cert_chain_and_key **cert_key_pairs, uint32_t num_cert_key_pairs);
/**
* Adds to the trust store from a CA file or directory containing trusted certificates.
* To completely override those locations, call s2n_config_wipe_trust_store() before calling
* this function.
*
* @note The trust store will be initialized with the common locations for the host
* operating system by default.
* @param config The configuration object being updated
* @param ca_pem_filename A string for the file path of the CA PEM file.
* @param ca_dir A string for the directory of the CA PEM files.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_set_verification_ca_location(struct s2n_config *config, const char *ca_pem_filename, const char *ca_dir);
/**
* Adds a PEM to the trust store. This will allocate memory, and load PEM into the
* Trust Store. Note that the trust store will be initialized with the common locations
* for the host operating system by default. To completely override those locations,
* call s2n_config_wipe_trust_store before calling this function.
*
* @param config The configuration object being updated
* @param pem The string value of the PEM certificate.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_add_pem_to_trust_store(struct s2n_config *config, const char *pem);
/**
* Clear the trust store.
*
* Note that the trust store will be initialized with the common locations for
* the host operating system by default. To completely override those locations,
* call this before functions like `s2n_config_set_verification_ca_location()`
* or `s2n_config_add_pem_to_trust_store()`
*
* @param config The configuration object being updated
*
* @returns 0 on success and -1 on error
*/
S2N_API extern int s2n_config_wipe_trust_store(struct s2n_config *config);
typedef enum {
S2N_VERIFY_AFTER_SIGN_DISABLED,
S2N_VERIFY_AFTER_SIGN_ENABLED
} s2n_verify_after_sign;
/**
* Toggle whether generated signatures are verified before being sent.
*
* Although signatures produced by the underlying libcrypto should always be valid,
* hardware faults, bugs in the signing implementation, or other uncommon factors
* can cause unexpected mistakes in the final signatures. Because these mistakes
* can leak information about the private key, applications with low trust in their
* hardware or libcrypto may want to verify signatures before sending them.
*
* However, this feature will significantly impact handshake latency.
* Additionally, most libcrypto implementations already check for common errors in signatures.
*/
S2N_API extern int s2n_config_set_verify_after_sign(struct s2n_config *config, s2n_verify_after_sign mode);
/**
* Set a custom send buffer size.
*
* This buffer is used to stage records for sending. By default,
* enough memory is allocated to hold a single record of the maximum
* size configured for the connection. With the default fragment size,
* that is about 8K bytes.
*
* Less memory can be allocated for the send buffer, but this will result in
* smaller, more fragmented records and increased overhead. While the absolute
* minimum size required is 1034 bytes, at least 2K bytes is recommended for
* reasonable record sizes.
*
* More memory can be allocated for the send buffer. This will result in s2n-tls
* buffering multiple records before sending them, reducing system write calls.
* At least 17K bytes is recommended for this use case, or at least 35K bytes
* if larger fragment sizes are used via `s2n_connection_prefer_throughput()`.
*
* @param config The configuration object being updated
* @param size The desired custom buffer size.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_set_send_buffer_size(struct s2n_config *config, uint32_t size);
/**
* Enable or disable recieving of multiple TLS records in a single s2n_recv call
*
* Legacy behavior is to return after reading a single TLS record which may not be the most
* efficient way to invoke this function, especially if larger receive buffers are used.
*
* @param config The configuration object being updated
* @param enabled Set to `true` if multiple record recieve is to be enabled; `false` to disable.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_set_recv_multi_record(struct s2n_config *config, bool enabled);
/**
* A callback function invoked (usually multiple times) during X.509 validation for each
* name encountered in the leaf certificate.
*
* Return 1 to trust that hostname or 0 to not trust the hostname.
*
* If this function returns 1, then the certificate is considered trusted and that portion
* of the X.509 validation will succeed.
*
* If no hostname results in a 1 being returned, the certificate will be untrusted and the
* validation will terminate immediately.
*
* Data is a opaque user context set in s2n_config_set_verify_host_callback() or s2n_connection_set_verify_host_callback().
*/
typedef uint8_t (*s2n_verify_host_fn)(const char *host_name, size_t host_name_len, void *data);
/**
* Sets the callback to use for verifying that a hostname from an X.509 certificate is trusted.
*
* The default behavior is to require that the hostname match the server name set with s2n_set_server_name().
* This will likely lead to all client certificates being rejected, so the callback will need to be overriden when using
* client authentication.
*
* This change will be inherited by s2n_connections using this config. If a separate callback for different connections
* using the same config is desired, see s2n_connection_set_verify_host_callback().
*
* @param config The configuration object being updated
* @param data A user supplied opaque context to pass back to the callback
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_set_verify_host_callback(struct s2n_config *config, s2n_verify_host_fn, void *data);
/**
* Toggles whether or not to validate stapled OCSP responses.
*
* 1 means OCSP responses will be validated when they are encountered, while 0 means this step will
* be skipped.
*
* The default value is 1 if the underlying libCrypto implementation supports OCSP.
*
* @param config The configuration object being updated
* @param check_ocsp The desired OCSP response check configuration
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_set_check_stapled_ocsp_response(struct s2n_config *config, uint8_t check_ocsp);
/**
* Turns off all X.509 validation during the negotiation phase of the connection. This should only
* be used for testing or debugging purposes.
*
* @param config The configuration object being updated
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_disable_x509_verification(struct s2n_config *config);
/**
* Sets the maximum allowed depth of a cert chain used for X509 validation. The default value is
* 7. If this limit is exceeded, validation will fail if s2n_config_disable_x509_verification()
* has not been called. 0 is an illegal value and will return an error.
* 1 means only a root certificate will be used.
*
* @param config The configuration object being updated
* @param max_depth The number of allowed certificates in the certificate chain
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_set_max_cert_chain_depth(struct s2n_config *config, uint16_t max_depth);
/**
* Associates a set of Diffie-Hellman parameters with an `s2n_config` object.
* @note `dhparams_pem` should be PEM encoded DH parameters.
*
* @param config The configuration object being updated
* @param dhparams_pem A string containing the PEM encoded DH parameters.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_add_dhparams(struct s2n_config *config, const char *dhparams_pem);
/**
* Sets the security policy that includes the cipher/kem/signature/ecc preferences and
* protocol version.
*
* See the [USAGE-GUIDE.md](https://github.com/aws/s2n-tls/blob/main/docs/USAGE-GUIDE.md) for how to use security policies.
*/
S2N_API extern int s2n_config_set_cipher_preferences(struct s2n_config *config, const char *version);
/**
* Appends the provided application protocol to the preference list
*
* The data provided in `protocol` parameter will be copied into an internal buffer
*
* @param config The configuration object being updated
* @param protocol A pointer to a byte array value
* @param protocol_len The length of bytes that should be read from `protocol`. Note: this value cannot be 0, otherwise an error will be returned.
*/
S2N_API extern int s2n_config_append_protocol_preference(struct s2n_config *config, const uint8_t *protocol, uint8_t protocol_len);
/**
* Sets the application protocol preferences on an `s2n_config` object.
* `protocols` is a list in order of preference, with most preferred protocol first, and of
* length `protocol_count`.
*
* When acting as an `S2N_CLIENT` the protocol list is included in the Client Hello message
* as the ALPN extension.
*
* As an `S2N_SERVER`, the list is used to negotiate a mutual application protocol with the
* client. After the negotiation for the connection has completed, the agreed upon protocol
* can be retrieved with s2n_get_application_protocol()
*
* @param config The configuration object being updated
* @param protocols The list of preferred protocols, in order of preference
* @param protocol_count The size of the protocols list
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_config_set_protocol_preferences(struct s2n_config *config, const char *const *protocols, int protocol_count);
/**
* Enum used to define the type, if any, of certificate status request
* a connection should make during the handshake. The only supported status request type is