Branch data Line data Source code
1 : : /* 2 : : * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 : : * 4 : : * Licensed under the Apache License, Version 2.0 (the "License"). 5 : : * You may not use this file except in compliance with the License. 6 : : * A copy of the License is located at 7 : : * 8 : : * http://aws.amazon.com/apache2.0 9 : : * 10 : : * or in the "license" file accompanying this file. This file is distributed 11 : : * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 : : * express or implied. See the License for the specific language governing 13 : : * permissions and limitations under the License. 14 : : */ 15 : : 16 : : #pragma once 17 : : 18 : : #include <stdint.h> 19 : : 20 : : #include "crypto/s2n_hmac.h" 21 : : #include "stuffer/s2n_stuffer.h" 22 : : 23 : 11778669 : #define S2N_TLS_CONTENT_TYPE_LENGTH 1 24 : : 25 : 363997 : #define S2N_TLS_SSLV2_HEADER_FLAG (0x80) 26 : 449 : #define S2N_TLS_SSLV2_HEADER_FLAG_UINT16 (S2N_TLS_SSLV2_HEADER_FLAG << 8) 27 : : 28 : : /* All versions of TLS define the record header the same: 29 : : * ContentType + ProtocolVersion + length 30 : : */ 31 : 11328434 : #define S2N_TLS_RECORD_HEADER_LENGTH (S2N_TLS_CONTENT_TYPE_LENGTH + S2N_TLS_PROTOCOL_VERSION_LEN + 2) 32 : : 33 : : /* 34 : : * All versions of TLS limit the data fragment to 2^14 bytes. 35 : : * 36 : : *= https://www.rfc-editor.org/rfc/rfc5246#section-6.2.1 37 : : *# The record layer fragments information blocks into TLSPlaintext 38 : : *# records carrying data in chunks of 2^14 bytes or less. 39 : : * 40 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-5.1 41 : : *# The record layer fragments information blocks into TLSPlaintext 42 : : *# records carrying data in chunks of 2^14 bytes or less. 43 : : */ 44 : 561 : #define S2N_TLS_MAXIMUM_FRAGMENT_LENGTH (1 << 14) 45 : : 46 : : /* The TLS1.2 record length allows for 1024 bytes of compression expansion and 47 : : * 1024 bytes of encryption expansion and padding. 48 : : * Since S2N does not support compression, we can ignore the compression overhead. 49 : : */ 50 : 6800324 : #define S2N_TLS12_ENCRYPTION_OVERHEAD_SIZE 1024 51 : : #define S2N_TLS12_MAX_RECORD_LEN_FOR(frag) \ 52 : 10408866 : ((frag) + S2N_TLS12_ENCRYPTION_OVERHEAD_SIZE + S2N_TLS_RECORD_HEADER_LENGTH) 53 : : #define S2N_TLS12_MAXIMUM_RECORD_LENGTH S2N_TLS12_MAX_RECORD_LEN_FOR(S2N_TLS_MAXIMUM_FRAGMENT_LENGTH) 54 : : 55 : : /* 56 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-5.2 57 : : *# An AEAD algorithm used in TLS 1.3 MUST NOT produce an expansion 58 : : *# greater than 255 octets. 59 : : */ 60 : 116087 : #define S2N_TLS13_ENCRYPTION_OVERHEAD_SIZE 255 61 : : 62 : : /** 63 : : * Return the record length (record header + encrypted stuff) for some plaintext 64 : : * data of `frag` length 65 : : */ 66 : 116087 : #define S2N_TLS13_MAX_RECORD_LEN_FOR(frag) ((frag) + S2N_TLS_CONTENT_TYPE_LENGTH \ 67 : 116087 : + S2N_TLS13_ENCRYPTION_OVERHEAD_SIZE \ 68 : 116087 : + S2N_TLS_RECORD_HEADER_LENGTH) 69 : 111360 : #define S2N_TLS13_MAXIMUM_RECORD_LENGTH S2N_TLS13_MAX_RECORD_LEN_FOR(S2N_TLS_MAXIMUM_FRAGMENT_LENGTH) 70 : : 71 : : /** 72 : : *= https://www.rfc-editor.org/rfc/rfc8446#section-5.2 73 : : *# length: The length (in bytes) of the following 74 : : *# TLSCiphertext.encrypted_record, which is the sum of the lengths of 75 : : *# the content and the padding, plus one for the inner content type, 76 : : *# plus any expansion added by the AEAD algorithm. The length 77 : : *# MUST NOT exceed 2^14 + 256 bytes. An endpoint that receives a 78 : : *# record that exceeds this length MUST terminate the connection with 79 : : *# a "record_overflow" alert. 80 : : */ 81 : : #define S2N_TLS13_MAX_RECORD_PAYLOAD_LENGTH ((1 << 14) + 256) 82 : : 83 : : /* Currently, TLS1.2 records may be larger than TLS1.3 records. 84 : : * If the protocol is unknown, assume TLS1.2. 85 : : */ 86 : 6797930 : #define S2N_TLS_MAX_RECORD_LEN_FOR(frag) S2N_TLS12_MAX_RECORD_LEN_FOR(frag) 87 : 3608542 : #define S2N_TLS_MAXIMUM_RECORD_LENGTH S2N_TLS_MAX_RECORD_LEN_FOR(S2N_TLS_MAXIMUM_FRAGMENT_LENGTH) 88 : : 89 : : struct s2n_record_header { 90 : : uint8_t content_type; 91 : : uint16_t version; 92 : : uint16_t length; 93 : : }; 94 : : 95 : : S2N_RESULT s2n_record_max_write_size(struct s2n_connection *conn, uint16_t max_fragment_size, uint16_t *max_record_size); 96 : : S2N_RESULT s2n_record_max_write_payload_size(struct s2n_connection *conn, uint16_t *max_fragment_size); 97 : : S2N_RESULT s2n_record_min_write_payload_size(struct s2n_connection *conn, uint16_t *payload_size); 98 : : S2N_RESULT s2n_record_write(struct s2n_connection *conn, uint8_t content_type, struct s2n_blob *in); 99 : : int s2n_record_writev(struct s2n_connection *conn, uint8_t content_type, const struct iovec *in, int in_count, size_t offs, size_t to_write); 100 : : int s2n_record_parse(struct s2n_connection *conn); 101 : : int s2n_record_header_parse(struct s2n_connection *conn, struct s2n_record_header *header); 102 : : int s2n_tls13_parse_record_type(struct s2n_stuffer *stuffer, uint8_t *record_type); 103 : : int s2n_sslv2_record_header_parse(struct s2n_connection *conn, uint8_t *record_type, uint8_t *client_protocol_version, uint16_t *fragment_length); 104 : : int s2n_verify_cbc(struct s2n_connection *conn, struct s2n_hmac_state *hmac, struct s2n_blob *decrypted); 105 : : S2N_RESULT s2n_aead_aad_init(const struct s2n_connection *conn, uint8_t *sequence_number, uint8_t content_type, uint16_t record_length, struct s2n_blob *ad); 106 : : S2N_RESULT s2n_tls13_aead_aad_init(struct s2n_record_header *header, struct s2n_blob *ad); 107 : : S2N_RESULT s2n_record_wipe(struct s2n_connection *conn);