-
Notifications
You must be signed in to change notification settings - Fork 30
/
rustls.h
2600 lines (2417 loc) · 114 KB
/
rustls.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
#ifndef RUSTLS_H
#define RUSTLS_H
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
/**
* Describes which sort of handshake happened.
*/
typedef enum rustls_handshake_kind {
/**
* The type of handshake could not be determined.
*
* This variant should not be used.
*/
RUSTLS_HANDSHAKE_KIND_UNKNOWN = 0,
/**
* A full TLS handshake.
*
* This is the typical TLS connection initiation process when resumption is
* not yet unavailable, and the initial client hello was accepted by the server.
*/
RUSTLS_HANDSHAKE_KIND_FULL = 1,
/**
* A full TLS handshake, with an extra round-trip for a hello retry request.
*
* The server can respond with a hello retry request (HRR) if the initial client
* hello is unacceptable for several reasons, the most likely if no supported key
* shares were offered by the client.
*/
RUSTLS_HANDSHAKE_KIND_FULL_WITH_HELLO_RETRY_REQUEST = 2,
/**
* A resumed TLS handshake.
*
* Resumed handshakes involve fewer round trips and less cryptography than
* full ones, but can only happen when the peers have previously done a full
* handshake together, and then remember data about it.
*/
RUSTLS_HANDSHAKE_KIND_RESUMED = 3,
} rustls_handshake_kind;
enum rustls_result {
RUSTLS_RESULT_OK = 7000,
RUSTLS_RESULT_IO = 7001,
RUSTLS_RESULT_NULL_PARAMETER = 7002,
RUSTLS_RESULT_INVALID_DNS_NAME_ERROR = 7003,
RUSTLS_RESULT_PANIC = 7004,
RUSTLS_RESULT_CERTIFICATE_PARSE_ERROR = 7005,
RUSTLS_RESULT_PRIVATE_KEY_PARSE_ERROR = 7006,
RUSTLS_RESULT_INSUFFICIENT_SIZE = 7007,
RUSTLS_RESULT_NOT_FOUND = 7008,
RUSTLS_RESULT_INVALID_PARAMETER = 7009,
RUSTLS_RESULT_UNEXPECTED_EOF = 7010,
RUSTLS_RESULT_PLAINTEXT_EMPTY = 7011,
RUSTLS_RESULT_ACCEPTOR_NOT_READY = 7012,
RUSTLS_RESULT_ALREADY_USED = 7013,
RUSTLS_RESULT_CERTIFICATE_REVOCATION_LIST_PARSE_ERROR = 7014,
RUSTLS_RESULT_NO_SERVER_CERT_VERIFIER = 7015,
RUSTLS_RESULT_NO_DEFAULT_CRYPTO_PROVIDER = 7016,
RUSTLS_RESULT_GET_RANDOM_FAILED = 7017,
RUSTLS_RESULT_NO_CERT_RESOLVER = 7018,
RUSTLS_RESULT_NO_CERTIFICATES_PRESENTED = 7101,
RUSTLS_RESULT_DECRYPT_ERROR = 7102,
RUSTLS_RESULT_FAILED_TO_GET_CURRENT_TIME = 7103,
RUSTLS_RESULT_FAILED_TO_GET_RANDOM_BYTES = 7113,
RUSTLS_RESULT_HANDSHAKE_NOT_COMPLETE = 7104,
RUSTLS_RESULT_PEER_SENT_OVERSIZED_RECORD = 7105,
RUSTLS_RESULT_NO_APPLICATION_PROTOCOL = 7106,
RUSTLS_RESULT_BAD_MAX_FRAGMENT_SIZE = 7114,
RUSTLS_RESULT_UNSUPPORTED_NAME_TYPE = 7115,
RUSTLS_RESULT_ENCRYPT_ERROR = 7116,
RUSTLS_RESULT_CERT_ENCODING_BAD = 7121,
RUSTLS_RESULT_CERT_EXPIRED = 7122,
RUSTLS_RESULT_CERT_NOT_YET_VALID = 7123,
RUSTLS_RESULT_CERT_REVOKED = 7124,
RUSTLS_RESULT_CERT_UNHANDLED_CRITICAL_EXTENSION = 7125,
RUSTLS_RESULT_CERT_UNKNOWN_ISSUER = 7126,
RUSTLS_RESULT_CERT_BAD_SIGNATURE = 7127,
RUSTLS_RESULT_CERT_NOT_VALID_FOR_NAME = 7128,
RUSTLS_RESULT_CERT_INVALID_PURPOSE = 7129,
RUSTLS_RESULT_CERT_APPLICATION_VERIFICATION_FAILURE = 7130,
RUSTLS_RESULT_CERT_OTHER_ERROR = 7131,
RUSTLS_RESULT_CERT_UNKNOWN_REVOCATION_STATUS = 7154,
RUSTLS_RESULT_CERT_EXPIRED_REVOCATION_LIST = 7156,
RUSTLS_RESULT_MESSAGE_HANDSHAKE_PAYLOAD_TOO_LARGE = 7133,
RUSTLS_RESULT_MESSAGE_INVALID_CCS = 7134,
RUSTLS_RESULT_MESSAGE_INVALID_CONTENT_TYPE = 7135,
RUSTLS_RESULT_MESSAGE_INVALID_CERT_STATUS_TYPE = 7136,
RUSTLS_RESULT_MESSAGE_INVALID_CERT_REQUEST = 7137,
RUSTLS_RESULT_MESSAGE_INVALID_DH_PARAMS = 7138,
RUSTLS_RESULT_MESSAGE_INVALID_EMPTY_PAYLOAD = 7139,
RUSTLS_RESULT_MESSAGE_INVALID_KEY_UPDATE = 7140,
RUSTLS_RESULT_MESSAGE_INVALID_SERVER_NAME = 7141,
RUSTLS_RESULT_MESSAGE_TOO_LARGE = 7142,
RUSTLS_RESULT_MESSAGE_TOO_SHORT = 7143,
RUSTLS_RESULT_MESSAGE_MISSING_DATA = 7144,
RUSTLS_RESULT_MESSAGE_MISSING_KEY_EXCHANGE = 7145,
RUSTLS_RESULT_MESSAGE_NO_SIGNATURE_SCHEMES = 7146,
RUSTLS_RESULT_MESSAGE_TRAILING_DATA = 7147,
RUSTLS_RESULT_MESSAGE_UNEXPECTED_MESSAGE = 7148,
RUSTLS_RESULT_MESSAGE_UNKNOWN_PROTOCOL_VERSION = 7149,
RUSTLS_RESULT_MESSAGE_UNSUPPORTED_COMPRESSION = 7150,
RUSTLS_RESULT_MESSAGE_UNSUPPORTED_CURVE_TYPE = 7151,
RUSTLS_RESULT_MESSAGE_UNSUPPORTED_KEY_EXCHANGE_ALGORITHM = 7152,
RUSTLS_RESULT_MESSAGE_INVALID_OTHER = 7153,
RUSTLS_RESULT_MESSAGE_CERTIFICATE_PAYLOAD_TOO_LARGE = 7155,
RUSTLS_RESULT_PEER_INCOMPATIBLE_ERROR = 7107,
RUSTLS_RESULT_PEER_MISBEHAVED_ERROR = 7108,
RUSTLS_RESULT_INAPPROPRIATE_MESSAGE = 7109,
RUSTLS_RESULT_INAPPROPRIATE_HANDSHAKE_MESSAGE = 7110,
RUSTLS_RESULT_GENERAL = 7112,
RUSTLS_RESULT_ALERT_CLOSE_NOTIFY = 7200,
RUSTLS_RESULT_ALERT_UNEXPECTED_MESSAGE = 7201,
RUSTLS_RESULT_ALERT_BAD_RECORD_MAC = 7202,
RUSTLS_RESULT_ALERT_DECRYPTION_FAILED = 7203,
RUSTLS_RESULT_ALERT_RECORD_OVERFLOW = 7204,
RUSTLS_RESULT_ALERT_DECOMPRESSION_FAILURE = 7205,
RUSTLS_RESULT_ALERT_HANDSHAKE_FAILURE = 7206,
RUSTLS_RESULT_ALERT_NO_CERTIFICATE = 7207,
RUSTLS_RESULT_ALERT_BAD_CERTIFICATE = 7208,
RUSTLS_RESULT_ALERT_UNSUPPORTED_CERTIFICATE = 7209,
RUSTLS_RESULT_ALERT_CERTIFICATE_REVOKED = 7210,
RUSTLS_RESULT_ALERT_CERTIFICATE_EXPIRED = 7211,
RUSTLS_RESULT_ALERT_CERTIFICATE_UNKNOWN = 7212,
RUSTLS_RESULT_ALERT_ILLEGAL_PARAMETER = 7213,
RUSTLS_RESULT_ALERT_UNKNOWN_CA = 7214,
RUSTLS_RESULT_ALERT_ACCESS_DENIED = 7215,
RUSTLS_RESULT_ALERT_DECODE_ERROR = 7216,
RUSTLS_RESULT_ALERT_DECRYPT_ERROR = 7217,
RUSTLS_RESULT_ALERT_EXPORT_RESTRICTION = 7218,
RUSTLS_RESULT_ALERT_PROTOCOL_VERSION = 7219,
RUSTLS_RESULT_ALERT_INSUFFICIENT_SECURITY = 7220,
RUSTLS_RESULT_ALERT_INTERNAL_ERROR = 7221,
RUSTLS_RESULT_ALERT_INAPPROPRIATE_FALLBACK = 7222,
RUSTLS_RESULT_ALERT_USER_CANCELED = 7223,
RUSTLS_RESULT_ALERT_NO_RENEGOTIATION = 7224,
RUSTLS_RESULT_ALERT_MISSING_EXTENSION = 7225,
RUSTLS_RESULT_ALERT_UNSUPPORTED_EXTENSION = 7226,
RUSTLS_RESULT_ALERT_CERTIFICATE_UNOBTAINABLE = 7227,
RUSTLS_RESULT_ALERT_UNRECOGNISED_NAME = 7228,
RUSTLS_RESULT_ALERT_BAD_CERTIFICATE_STATUS_RESPONSE = 7229,
RUSTLS_RESULT_ALERT_BAD_CERTIFICATE_HASH_VALUE = 7230,
RUSTLS_RESULT_ALERT_UNKNOWN_PSK_IDENTITY = 7231,
RUSTLS_RESULT_ALERT_CERTIFICATE_REQUIRED = 7232,
RUSTLS_RESULT_ALERT_NO_APPLICATION_PROTOCOL = 7233,
RUSTLS_RESULT_ALERT_UNKNOWN = 7234,
RUSTLS_RESULT_CERT_REVOCATION_LIST_BAD_SIGNATURE = 7400,
RUSTLS_RESULT_CERT_REVOCATION_LIST_INVALID_CRL_NUMBER = 7401,
RUSTLS_RESULT_CERT_REVOCATION_LIST_INVALID_REVOKED_CERT_SERIAL_NUMBER = 7402,
RUSTLS_RESULT_CERT_REVOCATION_LIST_ISSUER_INVALID_FOR_CRL = 7403,
RUSTLS_RESULT_CERT_REVOCATION_LIST_OTHER_ERROR = 7404,
RUSTLS_RESULT_CERT_REVOCATION_LIST_PARSE_ERROR = 7405,
RUSTLS_RESULT_CERT_REVOCATION_LIST_UNSUPPORTED_CRL_VERSION = 7406,
RUSTLS_RESULT_CERT_REVOCATION_LIST_UNSUPPORTED_CRITICAL_EXTENSION = 7407,
RUSTLS_RESULT_CERT_REVOCATION_LIST_UNSUPPORTED_DELTA_CRL = 7408,
RUSTLS_RESULT_CERT_REVOCATION_LIST_UNSUPPORTED_INDIRECT_CRL = 7409,
RUSTLS_RESULT_CERT_REVOCATION_LIST_UNSUPPORTED_REVOCATION_REASON = 7410,
RUSTLS_RESULT_CLIENT_CERT_VERIFIER_BUILDER_NO_ROOT_ANCHORS = 7500,
RUSTLS_RESULT_INCONSISTENT_KEYS_KEYS_MISMATCH = 7600,
RUSTLS_RESULT_INCONSISTENT_KEYS_UNKNOWN = 7601,
RUSTLS_RESULT_INVALID_ENCRYPTED_CLIENT_HELLO_INVALID_CONFIG_LIST = 7700,
RUSTLS_RESULT_INVALID_ENCRYPTED_CLIENT_HELLO_NO_COMPATIBLE_CONFIG = 7701,
RUSTLS_RESULT_INVALID_ENCRYPTED_CLIENT_HELLO_SNI_REQUIRED = 7702,
};
typedef uint32_t rustls_result;
/**
* Definitions of known TLS protocol versions.
*/
typedef enum rustls_tls_version {
RUSTLS_TLS_VERSION_UNKNOWN = 0,
RUSTLS_TLS_VERSION_SSLV2 = 512,
RUSTLS_TLS_VERSION_SSLV3 = 768,
RUSTLS_TLS_VERSION_TLSV1_0 = 769,
RUSTLS_TLS_VERSION_TLSV1_1 = 770,
RUSTLS_TLS_VERSION_TLSV1_2 = 771,
RUSTLS_TLS_VERSION_TLSV1_3 = 772,
} rustls_tls_version;
/**
* A parsed ClientHello produced by a rustls_acceptor.
*
* It is used to check server name indication (SNI), ALPN protocols,
* signature schemes, and cipher suites. It can be combined with a
* `rustls_server_config` to build a `rustls_connection`.
*/
typedef struct rustls_accepted rustls_accepted;
/**
* Represents a TLS alert resulting from accepting a client.
*/
typedef struct rustls_accepted_alert rustls_accepted_alert;
/**
* A buffer and parser for ClientHello bytes.
*
* This allows reading ClientHello before choosing a rustls_server_config.
*
* It's useful when the server config will be based on parameters in the
* ClientHello: server name indication (SNI), ALPN protocols, signature
* schemes, and cipher suites.
*
* In particular, if a server wants to do some potentially expensive work
* to load a certificate for a given hostname, rustls_acceptor allows doing
* that asynchronously, as opposed to rustls_server_config_builder_set_hello_callback(),
* which doesn't work well for asynchronous I/O.
*
* The general flow is:
* - rustls_acceptor_new()
* - Loop:
* - Read bytes from the network it with rustls_acceptor_read_tls().
* - If successful, parse those bytes with rustls_acceptor_accept().
* - If that returns RUSTLS_RESULT_ACCEPTOR_NOT_READY, continue.
* - Otherwise, break.
* - If rustls_acceptor_accept() returned RUSTLS_RESULT_OK:
* - Examine the resulting rustls_accepted.
* - Create or select a rustls_server_config.
* - Call rustls_accepted_into_connection().
* - Otherwise, there was a problem with the ClientHello data and the
* connection should be rejected.
*/
typedef struct rustls_acceptor rustls_acceptor;
/**
* An X.509 certificate, as used in rustls.
* Corresponds to `CertificateDer` in the Rust pki-types API.
* <https://docs.rs/rustls-pki-types/latest/rustls_pki_types/struct.CertificateDer.html>
*/
typedef struct rustls_certificate rustls_certificate;
/**
* The complete chain of certificates to send during a TLS handshake,
* plus a private key that matches the end-entity (leaf) certificate.
*
* Corresponds to `CertifiedKey` in the Rust API.
* <https://docs.rs/rustls/latest/rustls/sign/struct.CertifiedKey.html>
*/
typedef struct rustls_certified_key rustls_certified_key;
/**
* A built client certificate verifier that can be provided to a `rustls_server_config_builder`
* with `rustls_server_config_builder_set_client_verifier`.
*/
typedef struct rustls_client_cert_verifier rustls_client_cert_verifier;
/**
* A client config that is done being constructed and is now read-only.
*
* Under the hood, this object corresponds to an `Arc<ClientConfig>`.
* <https://docs.rs/rustls/latest/rustls/struct.ClientConfig.html>
*/
typedef struct rustls_client_config rustls_client_config;
/**
* A client config being constructed.
*
* A builder can be modified by, e.g. `rustls_client_config_builder_load_roots_from_file`.
* Once you're done configuring settings, call `rustls_client_config_builder_build`
* to turn it into a *rustls_client_config.
*
* Alternatively, if an error occurs or, you don't wish to build a config,
* call `rustls_client_config_builder_free` to free the builder directly.
*
* This object is not safe for concurrent mutation. Under the hood,
* it corresponds to a `Box<ClientConfig>`.
* <https://docs.rs/rustls/latest/rustls/struct.ConfigBuilder.html>
*/
typedef struct rustls_client_config_builder rustls_client_config_builder;
typedef struct rustls_connection rustls_connection;
/**
* A C representation of a Rustls [`CryptoProvider`].
*/
typedef struct rustls_crypto_provider rustls_crypto_provider;
/**
* A `rustls_crypto_provider` builder.
*/
typedef struct rustls_crypto_provider_builder rustls_crypto_provider_builder;
/**
* An alias for `struct iovec` from uio.h (on Unix) or `WSABUF` on Windows.
*
* You should cast `const struct rustls_iovec *` to `const struct iovec *` on
* Unix, or `const *LPWSABUF` on Windows. See [`std::io::IoSlice`] for details
* on interoperability with platform specific vectored IO.
*/
typedef struct rustls_iovec rustls_iovec;
/**
* A root certificate store.
* <https://docs.rs/rustls/latest/rustls/struct.RootCertStore.html>
*/
typedef struct rustls_root_cert_store rustls_root_cert_store;
/**
* A `rustls_root_cert_store` being constructed.
*
* A builder can be modified by adding trust anchor root certificates with
* `rustls_root_cert_store_builder_add_pem`. Once you're done adding root certificates,
* call `rustls_root_cert_store_builder_build` to turn it into a `rustls_root_cert_store`.
* This object is not safe for concurrent mutation.
*/
typedef struct rustls_root_cert_store_builder rustls_root_cert_store_builder;
/**
* A built server certificate verifier that can be provided to a `rustls_client_config_builder`
* with `rustls_client_config_builder_set_server_verifier`.
*/
typedef struct rustls_server_cert_verifier rustls_server_cert_verifier;
/**
* A server config that is done being constructed and is now read-only.
*
* Under the hood, this object corresponds to an `Arc<ServerConfig>`.
* <https://docs.rs/rustls/latest/rustls/struct.ServerConfig.html>
*/
typedef struct rustls_server_config rustls_server_config;
/**
* A server config being constructed.
*
* A builder can be modified by,
* e.g. rustls_server_config_builder_load_native_roots. Once you're
* done configuring settings, call rustls_server_config_builder_build
* to turn it into a *const rustls_server_config.
*
* Alternatively, if an error occurs or, you don't wish to build a config,
* call `rustls_server_config_builder_free` to free the builder directly.
*
* This object is not safe for concurrent mutation.
* <https://docs.rs/rustls/latest/rustls/struct.ConfigBuilder.html>
*/
typedef struct rustls_server_config_builder rustls_server_config_builder;
/**
* A signing key that can be used to construct a certified key.
*/
typedef struct rustls_signing_key rustls_signing_key;
/**
* A read-only view of a slice of Rust byte slices.
*
* This is used to pass data from rustls-ffi to callback functions provided
* by the user of the API. Because Vec and slice are not `#[repr(C)]`, we
* provide access via a pointer to an opaque struct and an accessor method
* that acts on that struct to get entries of type `rustls_slice_bytes`.
* Internally, the pointee is a `&[&[u8]]`.
*
* The memory exposed is available as specified by the function
* using this in its signature. For instance, when this is a parameter to a
* callback, the lifetime will usually be the duration of the callback.
* Functions that receive one of these must not call its methods beyond the
* allowed lifetime.
*/
typedef struct rustls_slice_slice_bytes rustls_slice_slice_bytes;
/**
* A read-only view of a slice of multiple Rust `&str`'s (that is, multiple
* strings).
*
* Like `rustls_str`, this guarantees that each string contains
* UTF-8 and no NUL bytes. Strings are not NUL-terminated.
*
* This is used to pass data from rustls-ffi to callback functions provided
* by the user of the API. Because Vec and slice are not `#[repr(C)]`, we
* can't provide a straightforward `data` and `len` structure. Instead, we
* provide access via a pointer to an opaque struct and accessor methods.
* Internally, the pointee is a `&[&str]`.
*
* The memory exposed is available as specified by the function
* using this in its signature. For instance, when this is a parameter to a
* callback, the lifetime will usually be the duration of the callback.
* Functions that receive one of these must not call its methods beyond the
* allowed lifetime.
*/
typedef struct rustls_slice_str rustls_slice_str;
/**
* A cipher suite supported by rustls.
*/
typedef struct rustls_supported_ciphersuite rustls_supported_ciphersuite;
/**
* A client certificate verifier being constructed.
*
* A builder can be modified by, e.g. `rustls_web_pki_client_cert_verifier_builder_add_crl`.
*
* Once you're done configuring settings, call `rustls_web_pki_client_cert_verifier_builder_build`
* to turn it into a `rustls_client_cert_verifier`.
*
* This object is not safe for concurrent mutation.
*
* See <https://docs.rs/rustls/latest/rustls/server/struct.ClientCertVerifierBuilder.html>
* for more information.
*/
typedef struct rustls_web_pki_client_cert_verifier_builder rustls_web_pki_client_cert_verifier_builder;
/**
* A server certificate verifier being constructed.
*
* A builder can be modified by, e.g. `rustls_web_pki_server_cert_verifier_builder_add_crl`.
*
* Once you're done configuring settings, call `rustls_web_pki_server_cert_verifier_builder_build`
* to turn it into a `rustls_server_cert_verifier`. This object is not safe for concurrent mutation.
*
* See <https://docs.rs/rustls/latest/rustls/client/struct.ServerCertVerifierBuilder.html>
* for more information.
*/
typedef struct rustls_web_pki_server_cert_verifier_builder rustls_web_pki_server_cert_verifier_builder;
/**
* A read-only view on a Rust `&str`.
*
* The contents are guaranteed to be valid UTF-8.
*
* As an additional guarantee on top of Rust's normal UTF-8 guarantee,
* a `rustls_str` is guaranteed not to contain internal NUL bytes, so it is
* safe to interpolate into a C string or compare using strncmp. Keep in mind
* that it is not NUL-terminated.
*
* The memory exposed is available as specified by the function
* using this in its signature. For instance, when this is a parameter to a
* callback, the lifetime will usually be the duration of the callback.
* Functions that receive one of these must not dereference the data pointer
* beyond the allowed lifetime.
*/
typedef struct rustls_str {
const char *data;
size_t len;
} rustls_str;
/**
* A return value for a function that may return either success (0) or a
* non-zero value representing an error.
*
* The values should match socket error numbers for your operating system --
* for example, the integers for `ETIMEDOUT`, `EAGAIN`, or similar.
*/
typedef int rustls_io_result;
/**
* A callback for `rustls_connection_read_tls`.
*
* An implementation of this callback should attempt to read up to n bytes from the
* network, storing them in `buf`. If any bytes were stored, the implementation should
* set out_n to the number of bytes stored and return 0.
*
* If there was an error, the implementation should return a nonzero rustls_io_result,
* which will be passed through to the caller.
*
* On POSIX systems, returning `errno` is convenient.
*
* On other systems, any appropriate error code works.
*
* It's best to make one read attempt to the network per call. Additional reads will
* be triggered by subsequent calls to one of the `_read_tls` methods.
*
* `userdata` is set to the value provided to `rustls_connection_set_userdata`.
* In most cases that should be a struct that contains, at a minimum, a file descriptor.
*
* The buf and out_n pointers are borrowed and should not be retained across calls.
*/
typedef rustls_io_result (*rustls_read_callback)(void *userdata,
uint8_t *buf,
size_t n,
size_t *out_n);
/**
* A read-only view on a Rust byte slice.
*
* This is used to pass data from rustls-ffi to callback functions provided
* by the user of the API.
* `len` indicates the number of bytes than can be safely read.
*
* The memory exposed is available as specified by the function
* using this in its signature. For instance, when this is a parameter to a
* callback, the lifetime will usually be the duration of the callback.
* Functions that receive one of these must not dereference the data pointer
* beyond the allowed lifetime.
*/
typedef struct rustls_slice_bytes {
const uint8_t *data;
size_t len;
} rustls_slice_bytes;
/**
* A callback for `rustls_connection_write_tls` or `rustls_accepted_alert_write_tls`.
*
* An implementation of this callback should attempt to write the `n` bytes in buf
* to the network.
*
* If any bytes were written, the implementation should set `out_n` to the number of
* bytes stored and return 0.
*
* If there was an error, the implementation should return a nonzero `rustls_io_result`,
* which will be passed through to the caller.
*
* On POSIX systems, returning `errno` is convenient.
*
* On other systems, any appropriate error code works.
*
* It's best to make one write attempt to the network per call. Additional writes will
* be triggered by subsequent calls to rustls_connection_write_tls.
*
* `userdata` is set to the value provided to `rustls_connection_set_userdata`. In most
* cases that should be a struct that contains, at a minimum, a file descriptor.
*
* The buf and out_n pointers are borrowed and should not be retained across calls.
*/
typedef rustls_io_result (*rustls_write_callback)(void *userdata,
const uint8_t *buf,
size_t n,
size_t *out_n);
/**
* User-provided input to a custom certificate verifier callback.
*
* See `rustls_client_config_builder_dangerous_set_certificate_verifier()`.
*/
typedef void *rustls_verify_server_cert_user_data;
/**
* Input to a custom certificate verifier callback.
*
* See `rustls_client_config_builder_dangerous_set_certificate_verifier()`.
*
* server_name can contain a hostname, an IPv4 address in textual form, or an
* IPv6 address in textual form.
*/
typedef struct rustls_verify_server_cert_params {
struct rustls_slice_bytes end_entity_cert_der;
const struct rustls_slice_slice_bytes *intermediate_certs_der;
struct rustls_str server_name;
struct rustls_slice_bytes ocsp_response;
} rustls_verify_server_cert_params;
typedef uint32_t (*rustls_verify_server_cert_callback)(rustls_verify_server_cert_user_data userdata,
const struct rustls_verify_server_cert_params *params);
/**
* An optional callback for logging key material.
*
* See the documentation on `rustls_client_config_builder_set_key_log` and
* `rustls_server_config_builder_set_key_log` for more information about the
* lifetimes of the parameters.
*/
typedef void (*rustls_keylog_log_callback)(struct rustls_str label,
const uint8_t *client_random,
size_t client_random_len,
const uint8_t *secret,
size_t secret_len);
/**
* An optional callback for deciding if key material will be logged.
*
* See the documentation on `rustls_client_config_builder_set_key_log` and
* `rustls_server_config_builder_set_key_log` for more information about the
* lifetimes of the parameters.
*/
typedef int (*rustls_keylog_will_log_callback)(struct rustls_str label);
typedef size_t rustls_log_level;
typedef struct rustls_log_params {
rustls_log_level level;
struct rustls_str message;
} rustls_log_params;
typedef void (*rustls_log_callback)(void *userdata, const struct rustls_log_params *params);
/**
* A callback for `rustls_connection_write_tls_vectored`.
*
* An implementation of this callback should attempt to write the bytes in
* the given `count` iovecs to the network.
*
* If any bytes were written, the implementation should set out_n to the number of
* bytes written and return 0.
*
* If there was an error, the implementation should return a nonzero rustls_io_result,
* which will be passed through to the caller.
*
* On POSIX systems, returning `errno` is convenient.
*
* On other systems, any appropriate error code works.
*
* It's best to make one write attempt to the network per call. Additional write will
* be triggered by subsequent calls to one of the `_write_tls` methods.
*
* `userdata` is set to the value provided to `rustls_*_session_set_userdata`. In most
* cases that should be a struct that contains, at a minimum, a file descriptor.
*
* The iov and out_n pointers are borrowed and should not be retained across calls.
*/
typedef rustls_io_result (*rustls_write_vectored_callback)(void *userdata,
const struct rustls_iovec *iov,
size_t count,
size_t *out_n);
/**
* Any context information the callback will receive when invoked.
*/
typedef void *rustls_client_hello_userdata;
/**
* A read-only view on a Rust slice of 16-bit integers in platform endianness.
*
* This is used to pass data from rustls-ffi to callback functions provided
* by the user of the API.
* `len` indicates the number of bytes than can be safely read.
*
* The memory exposed is available as specified by the function
* using this in its signature. For instance, when this is a parameter to a
* callback, the lifetime will usually be the duration of the callback.
* Functions that receive one of these must not dereference the data pointer
* beyond the allowed lifetime.
*/
typedef struct rustls_slice_u16 {
const uint16_t *data;
size_t len;
} rustls_slice_u16;
/**
* The TLS Client Hello information provided to a ClientHelloCallback function.
*
* `server_name` is the value of the ServerNameIndication extension provided
* by the client. If the client did not send an SNI, the length of this
* `rustls_string` will be 0.
*
* `signature_schemes` carries the values supplied by the client or, if the
* client did not send this TLS extension, the default schemes in the rustls library. See:
* <https://docs.rs/rustls/latest/rustls/internal/msgs/enums/enum.SignatureScheme.html>.
*
* `alpn` carries the list of ALPN protocol names that the client proposed to
* the server. Again, the length of this list will be 0 if none were supplied.
*
* All this data, when passed to a callback function, is only accessible during
* the call and may not be modified. Users of this API must copy any values that
* they want to access when the callback returned.
*
* EXPERIMENTAL: this feature of rustls-ffi is likely to change in the future, as
* the rustls library is re-evaluating their current approach to client hello handling.
*/
typedef struct rustls_client_hello {
struct rustls_str server_name;
struct rustls_slice_u16 signature_schemes;
const struct rustls_slice_slice_bytes *alpn;
} rustls_client_hello;
/**
* Prototype of a callback that can be installed by the application at the
* `rustls_server_config`.
*
* This callback will be invoked by a `rustls_connection` once the TLS client
* hello message has been received.
*
* `userdata` will be set based on rustls_connection_set_userdata.
*
* `hello` gives the value of the available client announcements, as interpreted
* by rustls. See the definition of `rustls_client_hello` for details.
*
* NOTE:
* - the passed in `hello` and all its values are only available during the
* callback invocations.
* - the passed callback function must be safe to call multiple times concurrently
* with the same userdata, unless there is only a single config and connection
* where it is installed.
*
* EXPERIMENTAL: this feature of rustls-ffi is likely to change in the future, as
* the rustls library is re-evaluating their current approach to client hello handling.
*/
typedef const struct rustls_certified_key *(*rustls_client_hello_callback)(rustls_client_hello_userdata userdata,
const struct rustls_client_hello *hello);
/**
* Any context information the callback will receive when invoked.
*/
typedef void *rustls_session_store_userdata;
/**
* Prototype of a callback that can be installed by the application at the
* `rustls_server_config` or `rustls_client_config`.
*
* This callback will be invoked by a TLS session when looking up the data
* for a TLS session id.
*
* `userdata` will be supplied based on rustls_{client,server}_session_set_userdata.
*
* The `buf` points to `count` consecutive bytes where the
* callback is expected to copy the result to. The number of copied bytes
* needs to be written to `out_n`. The callback should not read any
* data from `buf`.
*
* If the value to copy is larger than `count`, the callback should never
* do a partial copy but instead remove the value from its store and
* act as if it was never found.
*
* The callback should return RUSTLS_RESULT_OK to indicate that a value was
* retrieved and written in its entirety into `buf`, or RUSTLS_RESULT_NOT_FOUND
* if no session was retrieved.
*
* When `remove_after` is != 0, the returned data needs to be removed
* from the store.
*
* NOTE: the passed in `key` and `buf` are only available during the
* callback invocation.
* NOTE: callbacks used in several sessions via a common config
* must be implemented thread-safe.
*/
typedef uint32_t (*rustls_session_store_get_callback)(rustls_session_store_userdata userdata,
const struct rustls_slice_bytes *key,
int remove_after,
uint8_t *buf,
size_t count,
size_t *out_n);
/**
* Prototype of a callback that can be installed by the application at the
* `rustls_server_config` or `rustls_client_config`.
*
* This callback will be invoked by a TLS session when a TLS session
* been created and an id for later use is handed to the client/has
* been received from the server.
*
* `userdata` will be supplied based on rustls_{client,server}_session_set_userdata.
*
* The callback should return RUSTLS_RESULT_OK to indicate that a value was
* successfully stored, or RUSTLS_RESULT_IO on failure.
*
* NOTE: the passed in `key` and `val` are only available during the
* callback invocation.
* NOTE: callbacks used in several sessions via a common config
* must be implemented thread-safe.
*/
typedef uint32_t (*rustls_session_store_put_callback)(rustls_session_store_userdata userdata,
const struct rustls_slice_bytes *key,
const struct rustls_slice_bytes *val);
/**
* Rustls' list of supported protocol versions. The length of the array is
* given by `RUSTLS_ALL_VERSIONS_LEN`.
*/
extern const uint16_t RUSTLS_ALL_VERSIONS[2];
/**
* The length of the array `RUSTLS_ALL_VERSIONS`.
*/
extern const size_t RUSTLS_ALL_VERSIONS_LEN;
/**
* Rustls' default list of protocol versions. The length of the array is
* given by `RUSTLS_DEFAULT_VERSIONS_LEN`.
*/
extern const uint16_t RUSTLS_DEFAULT_VERSIONS[2];
/**
* The length of the array `RUSTLS_DEFAULT_VERSIONS`.
*/
extern const size_t RUSTLS_DEFAULT_VERSIONS_LEN;
/**
* Returns a static string containing the rustls-ffi version as well as the
* rustls version. The string is alive for the lifetime of the program and does
* not need to be freed.
*/
struct rustls_str rustls_version(void);
/**
* Create and return a new rustls_acceptor.
*
* Caller owns the pointed-to memory and must eventually free it with
* `rustls_acceptor_free()`.
*/
struct rustls_acceptor *rustls_acceptor_new(void);
/**
* Free a rustls_acceptor.
*
* Parameters:
*
* acceptor: The rustls_acceptor to free.
*
* Calling with NULL is fine. Must not be called twice with the same value.
*/
void rustls_acceptor_free(struct rustls_acceptor *acceptor);
/**
* Read some TLS bytes from the network into internal buffers.
*
* The actual network I/O is performed by `callback`, which you provide.
* Rustls will invoke your callback with a suitable buffer to store the
* read bytes into. You don't have to fill it up, just fill with as many
* bytes as you get in one syscall.
*
* Parameters:
*
* acceptor: The rustls_acceptor to read bytes into.
* callback: A function that will perform the actual network I/O.
* Must be valid to call with the given userdata parameter until
* this function call returns.
* userdata: An opaque parameter to be passed directly to `callback`.
* Note: this is distinct from the `userdata` parameter set with
* `rustls_connection_set_userdata`.
* out_n: An output parameter. This will be passed through to `callback`,
* which should use it to store the number of bytes written.
*
* Returns:
*
* - 0: Success. You should call `rustls_acceptor_accept()` next.
* - Any non-zero value: error.
*
* This function passes through return values from `callback`. Typically
* `callback` should return an errno value. See `rustls_read_callback()` for
* more details.
*/
rustls_io_result rustls_acceptor_read_tls(struct rustls_acceptor *acceptor,
rustls_read_callback callback,
void *userdata,
size_t *out_n);
/**
* Parse all TLS bytes read so far.
*
* If those bytes make up a ClientHello, create a rustls_accepted from them.
*
* Parameters:
*
* acceptor: The rustls_acceptor to access.
* out_accepted: An output parameter. The pointed-to pointer will be set
* to a new rustls_accepted only when the function returns
* RUSTLS_RESULT_OK. The memory is owned by the caller and must eventually
* be freed
* out_alert: An output parameter. The pointed-to pointer will be set
* to a new rustls_accepted_alert only when the function returns
* a non-OK result. The memory is owned by the caller and must eventually
* be freed with rustls_accepted_alert_free. The caller should call
* rustls_accepted_alert_write_tls to write the alert bytes to the TLS
* connection before freeing the rustls_accepted_alert.
*
* At most one of out_accepted or out_alert will be set.
*
* Returns:
*
* - RUSTLS_RESULT_OK: a ClientHello has successfully been parsed.
* A pointer to a newly allocated rustls_accepted has been written to
* *out_accepted.
* - RUSTLS_RESULT_ACCEPTOR_NOT_READY: a full ClientHello has not yet been read.
* Read more TLS bytes to continue.
* - Any other rustls_result: the TLS bytes read so far cannot be parsed
* as a ClientHello, and reading additional bytes won't help.
*
* Memory and lifetimes:
*
* After this method returns RUSTLS_RESULT_OK, `acceptor` is
* still allocated and valid. It needs to be freed regardless of success
* or failure of this function.
*
* Calling `rustls_acceptor_accept()` multiple times on the same
* `rustls_acceptor` is acceptable from a memory perspective but pointless
* from a protocol perspective.
*/
rustls_result rustls_acceptor_accept(struct rustls_acceptor *acceptor,
struct rustls_accepted **out_accepted,
struct rustls_accepted_alert **out_alert);
/**
* Get the server name indication (SNI) from the ClientHello.
*
* Parameters:
*
* accepted: The rustls_accepted to access.
*
* Returns:
*
* A rustls_str containing the SNI field.
*
* The returned value is valid until rustls_accepted_into_connection or
* rustls_accepted_free is called on the same `accepted`. It is not owned
* by the caller and does not need to be freed.
*
* This will be a zero-length rustls_str in these error cases:
*
* - The SNI contains a NUL byte.
* - The `accepted` parameter was NULL.
* - The `accepted` parameter was already transformed into a connection
* with rustls_accepted_into_connection.
*/
struct rustls_str rustls_accepted_server_name(const struct rustls_accepted *accepted);
/**
* Get the i'th in the list of signature schemes offered in the ClientHello.
*
* This is useful in selecting a server certificate when there are multiple
* available for the same server name, for instance when selecting
* between an RSA and an ECDSA certificate.
*
* Parameters:
*
* accepted: The rustls_accepted to access.
* i: Fetch the signature scheme at this offset.
*
* Returns:
*
* A TLS Signature Scheme from <https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-signaturescheme>
*
* This will be 0 in these cases:
* - i is greater than the number of available cipher suites.
* - accepted is NULL.
* - rustls_accepted_into_connection has already been called with `accepted`.
*/
uint16_t rustls_accepted_signature_scheme(const struct rustls_accepted *accepted,
size_t i);
/**
* Get the i'th in the list of cipher suites offered in the ClientHello.
*
* Parameters:
*
* accepted: The rustls_accepted to access.
* i: Fetch the cipher suite at this offset.
*
* Returns:
*
* A cipher suite value from <https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4.>
*
* This will be 0 in these cases:
* - i is greater than the number of available cipher suites.
* - accepted is NULL.
* - rustls_accepted_into_connection has already been called with `accepted`.
*
* Note that 0 is technically a valid cipher suite "TLS_NULL_WITH_NULL_NULL",
* but this library will never support null ciphers.
*/
uint16_t rustls_accepted_cipher_suite(const struct rustls_accepted *accepted,
size_t i);
/**
* Get the i'th in the list of ALPN protocols requested in the ClientHello.
*
* accepted: The rustls_accepted to access.
* i: Fetch the ALPN value at this offset.
*
* Returns:
*
* A rustls_slice_bytes containing the i'th ALPN protocol. This may
* contain internal NUL bytes and is not guaranteed to contain valid
* UTF-8.
*
* This will be a zero-length rustls_slice bytes in these cases:
* - i is greater than the number of offered ALPN protocols.
* - The client did not offer the ALPN extension.
* - The `accepted` parameter was already transformed into a connection
* with rustls_accepted_into_connection.
*
* The returned value is valid until rustls_accepted_into_connection or
* rustls_accepted_free is called on the same `accepted`. It is not owned
* by the caller and does not need to be freed.
*
* If you are calling this from Rust, note that the `'static` lifetime
* in the return signature is fake and must not be relied upon.
*/
struct rustls_slice_bytes rustls_accepted_alpn(const struct rustls_accepted *accepted, size_t i);
/**
* Turn a rustls_accepted into a rustls_connection, given the provided
* rustls_server_config.
*
* Parameters:
*
* accepted: The rustls_accepted to transform.
* config: The configuration with which to create this connection.
* out_conn: An output parameter. The pointed-to pointer will be set
* to a new rustls_connection only when the function returns
* RUSTLS_RESULT_OK.
* out_alert: An output parameter. The pointed-to pointer will be set
* to a new rustls_accepted_alert when, and only when, the function returns
* a non-OK result. The memory is owned by the caller and must eventually
* be freed with rustls_accepted_alert_free. The caller should call
* rustls_accepted_alert_write_tls to write the alert bytes to
* the TLS connection before freeing the rustls_accepted_alert.
*
* At most one of out_conn or out_alert will be set.
*
* Returns:
*
* - RUSTLS_RESULT_OK: The `accepted` parameter was successfully
* transformed into a rustls_connection, and *out_conn was written to.
* - RUSTLS_RESULT_ALREADY_USED: This function was called twice on the
* same rustls_connection.
* - RUSTLS_RESULT_NULL_PARAMETER: One of the input parameters was NULL.
*
* Memory and lifetimes:
*
* In both success and failure cases, this consumes the contents of
* `accepted` but does not free its allocated memory. In either case,