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 <s2n.h>
19 
20 #include "crypto/s2n_hmac.h"
21 #include "stuffer/s2n_stuffer.h"
22 #include "tls/s2n_early_data.h"
23 #include "utils/s2n_array.h"
24 #include "utils/s2n_blob.h"
25 #include "utils/s2n_result.h"
26 
27 typedef enum {
28     S2N_PSK_TYPE_RESUMPTION = 0,
29     S2N_PSK_TYPE_EXTERNAL,
30 } s2n_psk_type;
31 
32 typedef enum {
33     S2N_PSK_KE_UNKNOWN = 0,
34     S2N_PSK_KE,
35     S2N_PSK_DHE_KE,
36 } s2n_psk_key_exchange_mode;
37 
38 struct s2n_psk {
39     s2n_psk_type type;
40     struct s2n_blob identity;
41     struct s2n_blob secret;
42     s2n_hmac_algorithm hmac_alg;
43     uint32_t ticket_age_add;
44     uint64_t ticket_issue_time;
45     struct s2n_blob early_secret;
46     struct s2n_early_data_config early_data_config;
47 
48     /* This field is used with session tickets to track the lifetime
49      * of the original full handshake across multiple tickets.
50      * See https://tools.ietf.org/rfc/rfc8446#section-4.6.1
51      */
52     uint64_t keying_material_expiration;
53 };
54 S2N_RESULT s2n_psk_init(struct s2n_psk *psk, s2n_psk_type type);
55 S2N_CLEANUP_RESULT s2n_psk_wipe(struct s2n_psk *psk);
56 S2N_RESULT s2n_psk_clone(struct s2n_psk *new_psk, struct s2n_psk *original_psk);
57 
58 struct s2n_psk_parameters {
59     s2n_psk_type type;
60     struct s2n_array psk_list;
61     uint16_t binder_list_size;
62     uint16_t chosen_psk_wire_index;
63     struct s2n_psk *chosen_psk;
64     s2n_psk_key_exchange_mode psk_ke_mode;
65 };
66 S2N_RESULT s2n_psk_parameters_init(struct s2n_psk_parameters *params);
67 S2N_RESULT s2n_psk_parameters_offered_psks_size(struct s2n_psk_parameters *params, uint32_t *size);
68 S2N_CLEANUP_RESULT s2n_psk_parameters_wipe(struct s2n_psk_parameters *params);
69 S2N_CLEANUP_RESULT s2n_psk_parameters_wipe_secrets(struct s2n_psk_parameters *params);
70 
71 struct s2n_offered_psk {
72     struct s2n_blob identity;
73     uint16_t wire_index;
74     uint32_t obfuscated_ticket_age;
75 };
76 
77 struct s2n_offered_psk_list {
78     struct s2n_connection *conn;
79     struct s2n_stuffer wire_data;
80     uint16_t wire_index;
81 };
82 
83 S2N_RESULT s2n_finish_psk_extension(struct s2n_connection *conn);
84 
85 int s2n_psk_calculate_binder_hash(struct s2n_connection *conn, s2n_hmac_algorithm hmac_alg,
86         const struct s2n_blob *partial_client_hello, struct s2n_blob *output_binder_hash);
87 int s2n_psk_calculate_binder(struct s2n_psk *psk, const struct s2n_blob *binder_hash,
88         struct s2n_blob *output_binder);
89 int s2n_psk_verify_binder(struct s2n_connection *conn, struct s2n_psk *psk,
90         const struct s2n_blob *partial_client_hello, struct s2n_blob *binder_to_verify);
91 
92 S2N_RESULT s2n_connection_set_psk_type(struct s2n_connection *conn, s2n_psk_type type);
93 S2N_RESULT s2n_psk_validate_keying_material(struct s2n_connection *conn);
94