Debian Codegraph @

crt/s2n/tls/s2n_server_cert.c

raw symbols SCIP
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 #include "api/s2n.h"
17 #include "error/s2n_errno.h"
18 #include "tls/s2n_auth_selection.h"
19 #include "tls/s2n_cipher_suites.h"
20 #include "tls/s2n_tls.h"
21 #include "utils/s2n_safety.h"
22
23 int s2n_server_cert_recv(struct s2n_connection *conn)
24 {
25 /* s2n_server_cert_recv() may be re-entered due to handling an async callback.
26 * We operate on a copy of `handshake.io` to ensure the stuffer is initilized properly on the re-entry case.
27 */
28 struct s2n_stuffer in = conn->handshake.io;
29
30 if (conn->actual_protocol_version == S2N_TLS13) {
31 uint8_t certificate_request_context_len = 0;
32 POSIX_GUARD(s2n_stuffer_read_uint8(&in, &certificate_request_context_len));
33 S2N_ERROR_IF(certificate_request_context_len != 0, S2N_ERR_BAD_MESSAGE);
34 }
35
36 uint32_t size_of_all_certificates = 0;
37 POSIX_GUARD(s2n_stuffer_read_uint24(&in, &size_of_all_certificates));
38
39 S2N_ERROR_IF(size_of_all_certificates > s2n_stuffer_data_available(&in) || size_of_all_certificates < 3,
40 S2N_ERR_BAD_MESSAGE);
41
42 DEFER_CLEANUP(s2n_cert_public_key public_key = { 0 }, s2n_pkey_free);
43 POSIX_GUARD(s2n_pkey_zero_init(&public_key));
44
45 s2n_pkey_type actual_cert_pkey_type;
46 struct s2n_blob cert_chain = { 0 };
47 cert_chain.size = size_of_all_certificates;
48 cert_chain.data = s2n_stuffer_raw_read(&in, size_of_all_certificates);
49 POSIX_ENSURE_REF(cert_chain.data);
50
51 POSIX_GUARD_RESULT(s2n_x509_validator_validate_cert_chain(&conn->x509_validator, conn, cert_chain.data,
52 cert_chain.size, &actual_cert_pkey_type, &public_key));
53
54 POSIX_GUARD(s2n_is_cert_type_valid_for_auth(conn, actual_cert_pkey_type));
55 POSIX_GUARD_RESULT(s2n_pkey_setup_for_type(&public_key, actual_cert_pkey_type));
56 conn->handshake_params.server_public_key = public_key;
57 ZERO_TO_DISABLE_DEFER_CLEANUP(public_key);
58
59 /* Update handshake.io to reflect the true stuffer state after all async callbacks are handled. */
60 conn->handshake.io = in;
61
62 return 0;
63 }
64
65 int s2n_server_cert_send(struct s2n_connection *conn)
66 {
67 S2N_ERROR_IF(conn->handshake_params.our_chain_and_key == NULL, S2N_ERR_CERT_TYPE_UNSUPPORTED);
68 if (conn->actual_protocol_version == S2N_TLS13) {
69 /* server's certificate request context should always be of zero length */
70 /* https://tools.ietf.org/html/rfc8446#section-4.4.2 */
71 uint8_t certificate_request_context_len = 0;
72 POSIX_GUARD(s2n_stuffer_write_uint8(&conn->handshake.io, certificate_request_context_len));
73 }
74
75 POSIX_GUARD(s2n_send_cert_chain(conn, &conn->handshake.io, conn->handshake_params.our_chain_and_key));
76
77 return 0;
78 }