Debian Codegraph @

c/include/proton/ssl.h

raw
1 #ifndef PROTON_SSL_H
2 #define PROTON_SSL_H 1
3
4 /*
5 *
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements. See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership. The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License. You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied. See the License for the
20 * specific language governing permissions and limitations
21 * under the License.
22 *
23 */
24
25 #include <proton/import_export.h>
26 #include <proton/type_compat.h>
27 #include <proton/types.h>
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 /**
34 * @file
35 *
36 * @copybrief ssl
37 *
38 * @addtogroup ssl
39 * @{
40 */
41
42 /**
43 * API for using SSL with the Transport Layer.
44 *
45 * A Transport may be configured to use SSL for encryption and/or authentication. A
46 * Transport can be configured as either an "SSL client" or an "SSL server". An SSL
47 * client is the party that proactively establishes a connection to an SSL server. An SSL
48 * server is the party that accepts a connection request from a remote SSL client.
49 *
50 * This SSL implementation defines the following objects:
51
52 * @li A top-level object that stores the configuration used by one or more SSL
53 * sessions (pn_ssl_domain_t).
54 * @li A per-connection SSL session object that performs the encryption/authentication
55 * associated with the transport (pn_ssl_t).
56 *
57 * The pn_ssl_domain_t is used to construct an SSL session (pn_ssl_t). The
58 * session "adopts" its configuration from the pn_ssl_domain_t that was used to create it.
59 * For example, pn_ssl_domain_t can be configured as either a "client" or a "server". SSL
60 * sessions constructed from this domain will perform the corresponding role (either
61 * client or server).
62 *
63 * If an SSL session is created without a pn_ssl_domain_t object then a default will be used
64 * (see ::pn_ssl_init()).
65 *
66 * If either an SSL server or client needs to identify itself with the remote node, it
67 * must have its SSL certificate configured (see ::pn_ssl_domain_set_credentials()).
68 *
69 * If either an SSL server or client needs to verify the identity of the remote node, it
70 * must have its database of trusted CAs configured. By default this will be set up to use
71 * the default system database of trusted CA. But this can be changed
72 * (see ::pn_ssl_domain_set_trusted_ca_db()).
73 *
74 * The level of verification required of the remote may be configured (see
75 * ::pn_ssl_domain_set_peer_authentication)
76 *
77 * Support for SSL Client Session resume is provided (see ::pn_ssl_init,
78 * ::pn_ssl_resume_status).
79 */
80 typedef struct pn_ssl_domain_t pn_ssl_domain_t;
81
82 /**
83 * @see pn_ssl
84 */
85 typedef struct pn_ssl_t pn_ssl_t;
86
87 /**
88 * Determines the type of SSL endpoint.
89 */
90 typedef enum {
91 PN_SSL_MODE_CLIENT = 1, /**< Local connection endpoint is an SSL client */
92 PN_SSL_MODE_SERVER /**< Local connection endpoint is an SSL server */
93 } pn_ssl_mode_t;
94
95 /**
96 * Indicates whether an SSL session has been resumed.
97 */
98 typedef enum {
99 PN_SSL_RESUME_UNKNOWN, /**< Session resume state unknown/not supported */
100 PN_SSL_RESUME_NEW, /**< Session renegotiated - not resumed */
101 PN_SSL_RESUME_REUSED /**< Session resumed from previous session. */
102 } pn_ssl_resume_status_t;
103
104 /**
105 * Tests for SSL implementation present
106 *
107 * @return true if we support SSL, false if not
108 */
109 PN_EXTERN bool pn_ssl_present( void );
110
111 /**
112 * Create an SSL configuration domain
113 *
114 * This method allocates an SSL domain object. This object is used to hold the SSL
115 * configuration for one or more SSL sessions. The SSL session object (pn_ssl_t) is
116 * allocated from this object.
117 *
118 * @param[in] mode the role, client or server, assumed by all SSL sessions created
119 * with this domain.
120 * @return a pointer to the SSL domain, if SSL support is present.
121 */
122 PN_EXTERN pn_ssl_domain_t *pn_ssl_domain(pn_ssl_mode_t mode);
123
124 /**
125 * Release an SSL configuration domain
126 *
127 * This method frees an SSL domain object allocated by ::pn_ssl_domain.
128 * @param[in] domain the domain to destroy.
129 */
130 PN_EXTERN void pn_ssl_domain_free(pn_ssl_domain_t *domain);
131
132 /**
133 * Set the certificate that identifies the local node to the remote.
134 *
135 * This certificate establishes the identity for the local node for all SSL sessions
136 * created from this domain. It will be sent to the remote if the remote needs to verify
137 * the identity of this node. This may be used for both SSL servers and SSL clients (if
138 * client authentication is required by the server).
139 *
140 * @note This setting effects only those pn_ssl_t objects created after this call
141 * returns. pn_ssl_t objects created before invoking this method will use the domain's
142 * previous setting.
143 *
144 * @param[in] domain the ssl domain that will use this certificate.
145 * @param[in] credential_1 specifier for the file/database containing the identifying
146 * certificate. For Openssl users, this is a PEM file. For Windows SChannel users, this is
147 * the PKCS#12 file or system store.
148 * @param[in] credential_2 an optional key to access the identifying certificate. For
149 * Openssl users, this is an optional PEM file containing the private key used to sign the
150 * certificate. For Windows SChannel users, this is the friendly name of the
151 * self-identifying certificate if there are multiple certificates in the store.
152 * @param[in] password the password used to sign the key, else NULL if key is not
153 * protected.
154 * @return 0 on success
155 */
156 PN_EXTERN int pn_ssl_domain_set_credentials(pn_ssl_domain_t *domain,
157 const char *credential_1,
158 const char *credential_2,
159 const char *password);
160
161 /**
162 * Configure the set of trusted CA certificates used by this domain to verify peers.
163 *
164 * If the local SSL client/server needs to verify the identity of the remote, it must
165 * validate the signature of the remote's certificate. This function sets the database of
166 * trusted CAs that will be used to verify the signature of the remote's certificate.
167 *
168 * @note This setting effects only those pn_ssl_t objects created after this call
169 * returns. pn_ssl_t objects created before invoking this method will use the domain's
170 * previous setting.
171 *
172 * @note By default the list of trusted CA certificates will be set to the system default.
173 * What this is is depends on the OS and the SSL implementation used: For OpenSSL the default
174 * will depend on how the OS is set up. When using the Windows SChannel implementation the default
175 * will be the users default trusted certificate store.
176 *
177 * @param[in] domain the ssl domain that will use the database.
178 * @param[in] certificate_db database of trusted CAs, used to authenticate the peer.
179 * @return 0 on success
180 */
181 PN_EXTERN int pn_ssl_domain_set_trusted_ca_db(pn_ssl_domain_t *domain,
182 const char *certificate_db);
183
184 /**
185 * Determines the level of peer validation.
186 *
187 * ANONYMOUS_PEER does not require a valid certificate, and permits
188 * use of ciphers that do not provide authentication.
189 *
190 * VERIFY_PEER will only connect to those peers that provide a valid
191 * identifying certificate signed by a trusted CA and are using an
192 * authenticated cipher.
193 *
194 * VERIFY_PEER_NAME is like VERIFY_PEER, but also requires the peer's
195 * identity as contained in the certificate to be valid (see
196 * ::pn_ssl_set_peer_hostname).
197 *
198 * VERIFY_PEER_NAME is configured by default.
199 */
200 typedef enum {
201 PN_SSL_VERIFY_NULL = 0, /**< internal use only */
202 PN_SSL_VERIFY_PEER, /**< require peer to provide a valid identifying certificate */
203 PN_SSL_ANONYMOUS_PEER, /**< do not require a certificate nor cipher authorization */
204 PN_SSL_VERIFY_PEER_NAME /**< require valid certificate and matching name */
205 } pn_ssl_verify_mode_t;
206
207 /**
208 * Configure the level of verification used on the peer certificate.
209 *
210 * This method controls how the peer's certificate is validated, if at all. By default,
211 * servers do not attempt to verify their peers (PN_SSL_ANONYMOUS_PEER) but
212 * clients attempt to verify both the certificate and peer name (PN_SSL_VERIFY_PEER_NAME).
213 * Once certificates and trusted CAs are configured, peer verification can be enabled.
214 *
215 * @note In order to verify a peer, a trusted CA must be configured. See
216 * ::pn_ssl_domain_set_trusted_ca_db().
217 *
218 * @note Servers must provide their own certificate when verifying a peer. See
219 * ::pn_ssl_domain_set_credentials().
220 *
221 * @note This setting effects only those pn_ssl_t objects created after this call
222 * returns. pn_ssl_t objects created before invoking this method will use the domain's
223 * previous setting.
224 *
225 * @param[in] domain the ssl domain to configure.
226 * @param[in] mode the level of validation to apply to the peer
227 * @param[in] trusted_CAs path to a database of trusted CAs that the server will advertise
228 * to the peer client if the server has been configured to verify its peer.
229 * @return 0 on success
230 */
231 PN_EXTERN int pn_ssl_domain_set_peer_authentication(pn_ssl_domain_t *domain,
232 const pn_ssl_verify_mode_t mode,
233 const char *trusted_CAs);
234
235 /**
236 * Configure the list of permitted TLS protocols
237 *
238 * @param[in] domain the ssl domain to configure.
239 * @param[in] protocols string representing the protocol list.
240 * This list is a space separated string of the allowed TLS protocols,
241 * The current possibilities are TLSv1 TLSv1.1 TLSv1.2 TLSv1.3. None of the earlier SSL
242 * protocols are allowed for security reason.
243 *
244 * @note If this API not called then all the TLS protocols are allowed. The API only acts to
245 * restrict the allowed protocols to the specified set.
246 * @return 0 on success
247 */
248 PN_EXTERN int pn_ssl_domain_set_protocols(pn_ssl_domain_t *domain, const char *protocols);
249
250 /**
251 * Configure the list of permitted ciphers
252 *
253 * @note The syntax of the permitted list is undefined and will depend on the
254 * underlying SSL implementation.
255 *
256 * @param[in] domain the ssl domain to configure.
257 * @param[in] ciphers string representing the cipher list
258 * @return 0 on success
259 */
260 PN_EXTERN int pn_ssl_domain_set_ciphers(pn_ssl_domain_t *domain, const char *ciphers);
261
262 /**
263 * **Deprecated** - Use ::pn_transport_require_encryption()
264 *
265 * Permit a server to accept connection requests from non-SSL clients.
266 *
267 * This configures the server to "sniff" the incoming client data stream, and dynamically
268 * determine whether SSL/TLS is being used. This option is disabled by default: only
269 * clients using SSL/TLS are accepted.
270 *
271 * @param[in] domain the domain (server) that will accept the client connections.
272 * @return 0 on success
273 */
274 PN_EXTERN int pn_ssl_domain_allow_unsecured_client(pn_ssl_domain_t *domain);
275
276 /**
277 * Create a new SSL session object associated with a transport.
278 *
279 * A transport must have an SSL object in order to "speak" SSL over its connection. This
280 * method allocates an SSL object associates it with the transport.
281 *
282 * @param[in] transport the transport that will own the new SSL session.
283 * @return a pointer to the SSL object configured for this transport. Returns NULL if
284 * no SSL session is associated with the transport.
285 */
286 PN_EXTERN pn_ssl_t *pn_ssl(pn_transport_t *transport);
287
288 /**
289 * Initialize an SSL session.
290 *
291 * This method configures an SSL object with defaults or the configuration provided by the given
292 * domain.
293 *
294 * @note The default client domain is a secure default generally usable. This uses the system default
295 * certificates and requires peer name verification.
296 *
297 * @note However the default server domain is only really useful for test servers. This is because for
298 * a useful server you *need* to set up certificates to identify it so that clients can connect without
299 * being subject to "man-in-the-middle" attacks - see ::pn_ssl_domain_set_credentials() for details.
300 *
301 * @param[in] ssl the ssl session to configured.
302 * @param[in] domain the ssl domain used to configure the SSL session. If this value is NULL
303 * then use a default domain. This will be a client domain if the ssl object is part of a client transport
304 * and a server domain if the ssl object is part of a server transport.
305 * the transport the pn_ssl_t object
306 * @param[in] session_id if supplied, attempt to resume a previous SSL
307 * session that used the same session_id. If no previous SSL session
308 * is available, a new session will be created using the session_id
309 * and stored for future session restore (see ::::pn_ssl_resume_status).
310 * @return 0 on success, else an error code.
311 */
312 PN_EXTERN int pn_ssl_init(pn_ssl_t *ssl,
313 pn_ssl_domain_t *domain,
314 const char *session_id);
315
316 /**
317 * Get the name of the Cipher that is currently in use.
318 *
319 * Gets a text description of the cipher that is currently active, or
320 * returns FALSE if SSL is not active (no cipher). Note that the
321 * cipher in use may change over time due to renegotiation or other
322 * changes to the SSL state.
323 *
324 * @param[in] ssl the ssl client/server to query.
325 * @param[in,out] buffer buffer of size bytes to hold cipher name
326 * @param[in] size maximum number of bytes in buffer.
327 * @return True if cipher name written to buffer, False if no cipher in use.
328 */
329 PN_EXTERN bool pn_ssl_get_cipher_name(pn_ssl_t *ssl, char *buffer, size_t size);
330
331 /**
332 * Get the SSF (security strength factor) of the Cipher that is currently in use.
333 *
334 * @param[in] ssl the ssl client/server to query.
335 * @return the ssf, note that 0 means no security.
336 */
337 PN_EXTERN int pn_ssl_get_ssf(pn_ssl_t *ssl);
338
339 /**
340 * Get the name of the SSL protocol that is currently in use.
341 *
342 * Gets a text description of the SSL protocol that is currently active, or returns FALSE if SSL
343 * is not active. Note that the protocol may change over time due to renegotiation.
344 *
345 * @param[in] ssl the ssl client/server to query.
346 * @param[in,out] buffer buffer of size bytes to hold the version identifier
347 * @param[in] size maximum number of bytes in buffer.
348 * @return True if the version information was written to buffer, False if SSL connection
349 * not ready.
350 */
351 PN_EXTERN bool pn_ssl_get_protocol_name(pn_ssl_t *ssl, char *buffer, size_t size);
352
353 /**
354 * Check whether the state has been resumed.
355 *
356 * Used for client session resume. When called on an active session, indicates whether
357 * the state has been resumed from a previous session.
358 *
359 * @note This is a best-effort service - there is no guarantee that the remote server will
360 * accept the resumed parameters. The remote server may choose to ignore these
361 * parameters, and request a re-negotiation instead.
362 *
363 * @param[in] ssl the ssl session to check
364 * @return status code indicating whether or not the session has been resumed.
365 */
366 PN_EXTERN pn_ssl_resume_status_t pn_ssl_resume_status(pn_ssl_t *ssl);
367
368 /**
369 * Set the expected identity of the remote peer.
370 *
371 * By default, SSL will use the hostname associated with the connection that
372 * the transport is bound to (see ::pn_connection_set_hostname). This method
373 * allows the caller to override that default.
374 *
375 * The hostname is used for two purposes: 1) when set on an SSL client, it is sent to the
376 * server during the handshake (if Server Name Indication is supported), and 2) it is used
377 * to check against the identifying name provided in the peer's certificate. If the
378 * supplied name does not exactly match a SubjectAltName (type DNS name), or the
379 * CommonName entry in the peer's certificate, the peer is considered unauthenticated
380 * (potential imposter), and the SSL connection is aborted.
381 *
382 * @note Verification of the hostname is only done if PN_SSL_VERIFY_PEER_NAME is enabled.
383 * See ::pn_ssl_domain_set_peer_authentication.
384 *
385 * @param[in] ssl the ssl session.
386 * @param[in] hostname the expected identity of the remote. Must conform to the syntax as
387 * given in RFC1034, Section 3.5.
388 * @return 0 on success.
389 */
390 PN_EXTERN int pn_ssl_set_peer_hostname(pn_ssl_t *ssl, const char *hostname);
391
392 /**
393 * Access the configured peer identity.
394 *
395 * Return the expected identity of the remote peer, as set by ::pn_ssl_set_peer_hostname.
396 *
397 * @param[in] ssl the ssl session.
398 * @param[out] hostname buffer to hold the null-terminated name string. If null, no string
399 * is written.
400 * @param[in,out] bufsize on input set to the number of octets in hostname. On output, set
401 * to the number of octets needed to hold the value of hostname plus a null byte. Zero if
402 * no hostname set.
403 * @return 0 on success.
404 */
405 PN_EXTERN int pn_ssl_get_peer_hostname(pn_ssl_t *ssl, char *hostname, size_t *bufsize);
406
407 /**
408 * Get the subject from the peers certificate.
409 *
410 * @param[in] ssl the ssl client/server to query.
411 * @return A null terminated string representing the full subject,
412 * which is valid until the ssl object is destroyed.
413 */
414 PN_EXTERN const char* pn_ssl_get_remote_subject(pn_ssl_t *ssl);
415
416 /**
417 * Enumeration identifying the sub fields of the subject field in the ssl certificate.
418 */
419 typedef enum {
420 PN_SSL_CERT_SUBJECT_COUNTRY_NAME,
421 PN_SSL_CERT_SUBJECT_STATE_OR_PROVINCE,
422 PN_SSL_CERT_SUBJECT_CITY_OR_LOCALITY,
423 PN_SSL_CERT_SUBJECT_ORGANIZATION_NAME,
424 PN_SSL_CERT_SUBJECT_ORGANIZATION_UNIT,
425 PN_SSL_CERT_SUBJECT_COMMON_NAME
426 } pn_ssl_cert_subject_subfield;
427
428 /**
429 * Enumeration identifying hashing algorithm.
430 */
431 typedef enum {
432 PN_SSL_SHA1, /* Produces hash that is 20 bytes long */
433 PN_SSL_SHA256, /* Produces hash that is 32 bytes long */
434 PN_SSL_SHA512, /* Produces hash that is 64 bytes long */
435 PN_SSL_MD5 /* Produces hash that is 16 bytes long */
436 } pn_ssl_hash_alg;
437
438 /**
439 * Get the fingerprint of the certificate. The certificate fingerprint (as displayed in the Fingerprints section when
440 * looking at a certificate with say the Firefox browser) is the hexadecimal hash of the entire certificate.
441 * The fingerprint is not part of the certificate, rather it is computed from the certificate and can be used to uniquely identify a certificate.
442 * @param[in] ssl0 the ssl client/server to query
443 * @param[in] fingerprint char pointer. The certificate fingerprint (in hex format) will be populated in this array.
444 * If sha1 is the digest name, the fingerprint is 41 characters long (40 + 1 '\0' character), 65 characters long for
445 * sha256 and 129 characters long for sha512 and 33 characters for md5.
446 * @param[in] fingerprint_length - Must be at >= 33 for md5, >= 41 for sha1, >= 65 for sha256 and >=129 for sha512.
447 * @param[in] hash_alg the hash algorithm to use. Must be of type pn_ssl_hash_alg (currently supports sha1, sha256, sha512 and md5)
448 * @return error code - Returns 0 on success. Return a value less than zero if there were any errors. Upon execution of this function,
449 * char *fingerprint will contain the appropriate null terminated hex fingerprint
450 */
451 PN_EXTERN int pn_ssl_get_cert_fingerprint(pn_ssl_t *ssl0,
452 char *fingerprint,
453 size_t fingerprint_length,
454 pn_ssl_hash_alg hash_alg);
455
456 /**
457 * Returns a char pointer that contains the value of the sub field of the subject field in the ssl certificate. The subject field usually contains the following sub fields -
458 * C = ISO3166 two character country code
459 * ST = state or province
460 * L = Locality; generally means city
461 * O = Organization - Company Name
462 * OU = Organization Unit - division or unit
463 * CN = CommonName
464 * @param[in] ssl0 the ssl client/server to query
465 * @param[in] field The enumeration pn_ssl_cert_subject_subfield representing the required sub field.
466 * @return A null terminated string which contains the requested sub field value which is valid until the ssl object is destroyed.
467 */
468 PN_EXTERN const char* pn_ssl_get_remote_subject_subfield(pn_ssl_t *ssl0, pn_ssl_cert_subject_subfield field);
469
470 /**
471 * @}
472 */
473
474 #ifdef __cplusplus
475 }
476 #endif
477
478 #endif /* ssl.h */