|
| 1 | +.. Licensed to the Apache Software Foundation (ASF) under one |
| 2 | + or more contributor license agreements. See the NOTICE file |
| 3 | + distributed with this work for additional information |
| 4 | + regarding copyright ownership. The ASF licenses this file |
| 5 | + to you under the Apache License, Version 2.0 (the |
| 6 | + "License"); you may not use this file except in compliance |
| 7 | + with the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | + Unless required by applicable law or agreed to in writing, |
| 12 | + software distributed under the License is distributed on an |
| 13 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + KIND, either express or implied. See the License for the |
| 15 | + specific language governing permissions and limitations |
| 16 | + under the License. |
| 17 | +
|
| 18 | +.. include:: ../../common.defs |
| 19 | + |
| 20 | +.. highlight:: cpp |
| 21 | +.. default-domain:: cpp |
| 22 | + |
| 23 | +.. _cripts-certs: |
| 24 | + |
| 25 | +Certificates |
| 26 | +************ |
| 27 | + |
| 28 | +Cripts provides a set of convenient classes for introspection into the various |
| 29 | +TLS certificates that are used. These include both the server certificates used |
| 30 | +to establish a TLS connections, as well as any client certificates used for |
| 31 | +mutual TLS. |
| 32 | + |
| 33 | +In the current implementation, these objects only work on X509 certificates as |
| 34 | +associated with the ``client`` and ``server`` connections. Let's start off with |
| 35 | +a simple example of how to use these objects: |
| 36 | + |
| 37 | +.. code-block:: cpp |
| 38 | +
|
| 39 | + do_send_response() |
| 40 | + { |
| 41 | + if (client.connection.IsTLS()) { |
| 42 | + const auto tls = cripts::Certs::Server(client.connection); |
| 43 | +
|
| 44 | + client.response["X-Subject"] = tls.subject; |
| 45 | + client.response["X-NotBefore"] = tls.notBefore; |
| 46 | + client.response["X-NotAfter"] = tls.notAfter; |
| 47 | + } |
| 48 | + } |
| 49 | +
|
| 50 | +.. _cripts-certs-objects: |
| 51 | + |
| 52 | +Objects |
| 53 | +======= |
| 54 | + |
| 55 | +There are two types of objects for the certificates: |
| 56 | + |
| 57 | +================================= =============================================================== |
| 58 | +Object Description |
| 59 | +================================= =============================================================== |
| 60 | +``cripts::Certs::Server`` The certificate used on the connection for TLS handshakes. |
| 61 | +``cripts::Certs::Client`` The mutual TLS (mTLS) certificate used on the connection. |
| 62 | +================================= =============================================================== |
| 63 | + |
| 64 | +This combined with the two kinds of connections, ``cripts::Client::Connection`` and |
| 65 | +``cripts::Server::Connection`` yields a total of four possible certificate objects. For example, to |
| 66 | +access the client mTLS provided certificate on a client connection, you would use: |
| 67 | + |
| 68 | +.. code-block:: cpp |
| 69 | +
|
| 70 | + const auto tls = cripts::Certs::Client(cripts::Client::Connection::Get()); |
| 71 | +
|
| 72 | +Or if you are using the convenience wrappers: |
| 73 | + |
| 74 | +.. code-block:: cpp |
| 75 | +
|
| 76 | + const auto tls = cripts::Certs::Client(client.connection); |
| 77 | +
|
| 78 | +.. _cripts-certs-x509: |
| 79 | + |
| 80 | +X509 Values |
| 81 | +=========== |
| 82 | + |
| 83 | +As part of the certificate objects, there are a number of values that can be |
| 84 | +accessed. These values are all based on the X509 standard and can be used to |
| 85 | +introspect the certificate. The following values are available: |
| 86 | + |
| 87 | +================================= =============================================================== |
| 88 | +Value Description |
| 89 | +================================= =============================================================== |
| 90 | +``certificate`` The raw X509 certificate in PEM format. |
| 91 | +``signature`` The raw signature of the certificate. |
| 92 | +``subject`` The subject of the certificate. |
| 93 | +``issuer`` The issuer of the certificate. |
| 94 | +``serialNumber`` The serial number of the certificate. |
| 95 | +``notBefore`` The date and time when the certificate is valid from. |
| 96 | +``notAfter`` The date and time when the certificate is valid until. |
| 97 | +``version`` The version of the certificate. |
| 98 | +================================= =============================================================== |
| 99 | + |
| 100 | +.. _cripts-certs-san: |
| 101 | + |
| 102 | +SAN Values |
| 103 | +========== |
| 104 | + |
| 105 | +We've made special provisions to access the Subject Alternative Name (SAN) values |
| 106 | +of the certificate. These values are often used to identify the hostnames or IP |
| 107 | +addresses that the certificate is valid for. Once you have the certificate object, |
| 108 | +you can access the SAN values as follows: |
| 109 | + |
| 110 | +==================== =============== =============================================================== |
| 111 | +Field X509 field Description |
| 112 | +==================== =============== =============================================================== |
| 113 | +``.san`` na An array of tuples with type and ``string_view`` of all SANs. |
| 114 | +``.san.email`` ``GEN_EMAIL`` An array of ``string_view`` of email addresses. |
| 115 | +``.san.dns`` ``GEN_DNS`` An array of ``string_view`` of DNS names. |
| 116 | +``.san.uri`` ``GEN_URI`` An array of ``string_view`` of URIs. |
| 117 | +``.san.ipadd`` ``GEN_IPADD`` An array of ``string_view`` of IP addresses. |
| 118 | +==================== =============== =============================================================== |
| 119 | + |
| 120 | +.. note:: |
| 121 | + |
| 122 | + These arrays are empty if no SAN values are present in the certificate. We also populate these |
| 123 | + arrays lazily, but they are kept for the lifetime of the certificate object. This means that |
| 124 | + you can access these values multiple times without incurring additional overhead. Remember |
| 125 | + that you can use the ``cripts::Net::IP`` class to convert the IP addresses into proper |
| 126 | + IP address objects if needed. |
| 127 | + |
| 128 | + |
| 129 | +Odds are that you will want to use one of the specific array values, such as ``.san.uri``, which is |
| 130 | +easily done in a simple loop: |
| 131 | + |
| 132 | +.. code-block:: cpp |
| 133 | +
|
| 134 | + do_remap() |
| 135 | + { |
| 136 | + if (client.connection.IsTLS()) { |
| 137 | + const auto tls = cripts::Certs::Server(client.connection); |
| 138 | +
|
| 139 | + for (auto uri : tls.san.uri) { |
| 140 | + // Check the URI string_view |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +
|
| 145 | +
|
| 146 | +You can of course loop over all SAN values, which is where the type of the value would come in handy, |
| 147 | +and why this is an array of tuples. In this scenario, you would iterate over the tuples like this: |
| 148 | + |
| 149 | +.. code-block:: cpp |
| 150 | +
|
| 151 | + do_remap() |
| 152 | + { |
| 153 | + if (client.connection.IsTLS()) { |
| 154 | + const auto tls = cripts::Certs::Server(client.connection); |
| 155 | +
|
| 156 | + for (const [type, san] : tls.san) { |
| 157 | + if (type == cripts::Certs::SAN::URI) { |
| 158 | + // Check the URI string here |
| 159 | + } else if (type == cripts::Certs::SAN::DNS) { |
| 160 | + // Check the DNS string here |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | +
|
| 166 | +In addition to traditional C++ iterators, you can also access SAN values by index. Make sure |
| 167 | +you check the size of the array first, as accessing an out-of-bounds index will give you an |
| 168 | +empty tuple. Prefer the iterator above, unless you know you want to access a specific element. |
| 169 | + |
| 170 | +Example of an alternative way to loop over all SAN values: |
| 171 | + |
| 172 | +.. code-block:: cpp |
| 173 | +
|
| 174 | + do_remap() |
| 175 | + { |
| 176 | + if (client.connection.IsTLS()) { |
| 177 | + const auto tls = cripts::Certs::Server(client.connection); |
| 178 | +
|
| 179 | + size_t san_count = tls.san.size(); |
| 180 | +
|
| 181 | + for (size_t i = 0; i < san_count; ++i) { |
| 182 | + const auto [type, san] = tls.san[i]; |
| 183 | + // Process the type and san as needed |
| 184 | + } |
| 185 | + } |
| 186 | + } |
0 commit comments