Debian Codegraph @

crt/s2n/tls/s2n_client_cert_verify.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 "stuffer/s2n_stuffer.h"
19 #include "tls/s2n_async_pkey.h"
20 #include "tls/s2n_config.h"
21 #include "tls/s2n_connection.h"
22 #include "tls/s2n_signature_algorithms.h"
23 #include "tls/s2n_tls.h"
24 #include "utils/s2n_safety.h"
25
26 static int s2n_client_cert_verify_send_complete(struct s2n_connection *conn, struct s2n_blob *signature);
27
28 int s2n_client_cert_verify_recv(struct s2n_connection *conn)
29 {
30 POSIX_ENSURE_REF(conn);
31 struct s2n_handshake_hashes *hashes = conn->handshake.hashes;
32 POSIX_ENSURE_REF(hashes);
33
34 struct s2n_stuffer *in = &conn->handshake.io;
35
36 S2N_ASYNC_OFFLOAD_POSIX_GUARD(conn, {
37 POSIX_GUARD_RESULT(s2n_signature_algorithm_recv(conn, in));
38 const struct s2n_signature_scheme *chosen_sig_scheme = conn->handshake_params.client_cert_sig_scheme;
39 POSIX_ENSURE_REF(chosen_sig_scheme);
40
41 struct s2n_pkey *pkey = &conn->handshake_params.client_public_key;
42
43 POSIX_ENSURE(s2n_result_is_ok(s2n_signature_scheme_params_match(conn, pkey, chosen_sig_scheme)), S2N_ERR_INVALID_SIGNATURE_ALGORITHM);
44
45 uint16_t signature_size = 0;
46 struct s2n_blob signature = { 0 };
47 POSIX_GUARD(s2n_stuffer_read_uint16(in, &signature_size));
48 signature.size = signature_size;
49 signature.data = s2n_stuffer_raw_read(in, signature.size);
50 POSIX_ENSURE_REF(signature.data);
51
52 /* Use a copy of the hash state since the verify digest computation may modify the running hash state we need later. */
53 struct s2n_hash_state *hash_state = &hashes->hash_workspace;
54 POSIX_GUARD_RESULT(s2n_handshake_copy_hash_state(conn, chosen_sig_scheme->hash_alg, hash_state));
55
56 /* Verify the signature */
57 POSIX_GUARD(s2n_async_pkey_verify(conn, chosen_sig_scheme->sig_alg, hash_state, &signature));
58 });
59
60 /* Client certificate has been verified. Minimize required handshake hash algs */
61 POSIX_GUARD(s2n_conn_update_required_handshake_hashes(conn));
62
63 return S2N_SUCCESS;
64 }
65
66 int s2n_client_cert_verify_send(struct s2n_connection *conn)
67 {
68 POSIX_ENSURE_REF(conn);
69 struct s2n_handshake_hashes *hashes = conn->handshake.hashes;
70 POSIX_ENSURE_REF(hashes);
71
72 S2N_ASYNC_PKEY_GUARD(conn);
73 struct s2n_stuffer *out = &conn->handshake.io;
74
75 if (conn->actual_protocol_version >= S2N_TLS12) {
76 POSIX_GUARD(s2n_stuffer_write_uint16(out, conn->handshake_params.client_cert_sig_scheme->iana_value));
77 }
78 const struct s2n_signature_scheme *chosen_sig_scheme = conn->handshake_params.client_cert_sig_scheme;
79 POSIX_ENSURE_REF(chosen_sig_scheme);
80
81 /* Use a copy of the hash state since the verify digest computation may modify the running hash state we need later. */
82 struct s2n_hash_state *hash_state = &hashes->hash_workspace;
83 POSIX_GUARD_RESULT(s2n_handshake_copy_hash_state(conn, chosen_sig_scheme->hash_alg, hash_state));
84
85 S2N_ASYNC_PKEY_SIGN(conn, chosen_sig_scheme->sig_alg, hash_state, s2n_client_cert_verify_send_complete);
86 }
87
88 static int s2n_client_cert_verify_send_complete(struct s2n_connection *conn, struct s2n_blob *signature)
89 {
90 struct s2n_stuffer *out = &conn->handshake.io;
91
92 POSIX_GUARD(s2n_stuffer_write_uint16(out, signature->size));
93 POSIX_GUARD(s2n_stuffer_write(out, signature));
94
95 /* Client certificate has been verified. Minimize required handshake hash algs */
96 POSIX_GUARD(s2n_conn_update_required_handshake_hashes(conn));
97
98 return S2N_SUCCESS;
99 }