| 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 | /** |
| 17 | * @file cert_authorities.h |
| 18 | * |
| 19 | * This contains unstable APIs for selecting a client-sent certificate in |
| 20 | * response to the CertificateRequest message, and the necessary server-side |
| 21 | * work to support that selection (currently, just certificate_authorities). |
| 22 | * |
| 23 | * See also https://github.com/aws/s2n-tls/issues/5330. Please comment there if |
| 24 | * you're using the feature and report back on your experience. |
| 25 | */ |
| 26 | |
| 27 | #pragma once |
| 28 | |
| 29 | #include <s2n.h> |
| 30 | #include <stdbool.h> |
| 31 | #include <stdint.h> |
| 32 | #include <stdio.h> |
| 33 | #include <sys/types.h> |
| 34 | #ifndef _WIN32 |
| 35 | #include <sys/uio.h> |
| 36 | #endif |
| 37 | |
| 38 | #ifdef __cplusplus |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
| 42 | struct s2n_certificate_request; |
| 43 | struct s2n_certificate_authority_list; |
| 44 | |
| 45 | /** |
| 46 | * Get the certificate_authorities list from the certificate request. |
| 47 | * |
| 48 | * @returns The list, with a lifetime equivalent to the s2n_certificate_request. |
| 49 | * This points into the certificate request (it is not a copy of the list). |
| 50 | * Can be null if the list is not available. |
| 51 | */ |
| 52 | S2N_API struct s2n_certificate_authority_list *s2n_certificate_request_get_ca_list(struct s2n_certificate_request *request); |
| 53 | |
| 54 | /** |
| 55 | * Sets the certificate chain to return in response to the certificate request. |
| 56 | * |
| 57 | * @param chain must outlive the connection being served. |
| 58 | * |
| 59 | * @warning It is not recommended to free or modify the `chain`. It must |
| 60 | * outlive this connection. |
| 61 | * |
| 62 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 63 | */ |
| 64 | S2N_API int s2n_certificate_request_set_certificate(struct s2n_certificate_request *request, struct s2n_cert_chain_and_key *chain); |
| 65 | |
| 66 | /** |
| 67 | * Checks whether the offered list has a next entry. |
| 68 | * |
| 69 | * @param list A pointer to the certificate authority list being read. |
| 70 | * @returns bool If true, there is a next entry. |
| 71 | */ |
| 72 | S2N_API bool s2n_certificate_authority_list_has_next(struct s2n_certificate_authority_list *list); |
| 73 | |
| 74 | /** |
| 75 | * Obtains the next entry in the certificate authority list. |
| 76 | * |
| 77 | * The lifetime of the name is bound to the lifetime of the list (in other |
| 78 | * words, it must not be used past the certificate request callback). |
| 79 | * |
| 80 | * The name contains the bytes sent by the server. This should be a DER-encoded |
| 81 | * DistinguishedName, but s2n-tls does not currently strictly enforce the |
| 82 | * contents of the parsed name. |
| 83 | * |
| 84 | * See https://tools.ietf.org/rfc/rfc8446#section-4.2.4. |
| 85 | * |
| 86 | * @param list A pointer to the list being read. |
| 87 | * @param name An out-pointer to the next name in the list. |
| 88 | * @param length An out-pointer which is initialized to the length of the next name. |
| 89 | * |
| 90 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 91 | */ |
| 92 | S2N_API int s2n_certificate_authority_list_next(struct s2n_certificate_authority_list *list, uint8_t **name, uint16_t *length); |
| 93 | |
| 94 | /** |
| 95 | * Returns the certificate authority list to its original state. |
| 96 | * |
| 97 | * When `s2n_certificate_authority_list_reread` is called, the list is reset to the beginning. |
| 98 | * |
| 99 | * @param list The list to re-read. |
| 100 | */ |
| 101 | S2N_API int s2n_certificate_authority_list_reread(struct s2n_certificate_authority_list *list); |
| 102 | |
| 103 | /* CertificateRequest callback. |
| 104 | * |
| 105 | * @param conn The connection object for the in-progress connection. |
| 106 | * @param ctx A pointer to a user defined context provided in s2n_config_set_cert_request_callback. |
| 107 | * @param request CertificateRequest, containing metadata about the incoming |
| 108 | * request and a setter for the certificate to use. |
| 109 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure |
| 110 | */ |
| 111 | typedef int (*s2n_cert_request_callback)(struct s2n_connection *conn, void *ctx, struct s2n_certificate_request *request); |
| 112 | |
| 113 | /* Allows the caller to set a callback function that will be called after |
| 114 | * CertificateRequest message is parsed. |
| 115 | * |
| 116 | * This is currently only called on the client side. |
| 117 | * |
| 118 | * @param config A pointer to the s2n_config object. |
| 119 | * @param cert_req_callback The callback to be invoked. |
| 120 | * @param ctx A pointer to a user defined context to be sent to `cert_req_callback`. |
| 121 | * @returns S2N_SUCCESS on success. S2N_FAILURE on failure. |
| 122 | */ |
| 123 | S2N_API int s2n_config_set_cert_request_callback(struct s2n_config *config, s2n_cert_request_callback cert_req_callback, void *ctx); |
| 124 | |
| 125 | #ifdef __cplusplus |
| 126 | } |
| 127 | #endif |