Qpid Proton C API 0.41.1
Loading...
Searching...
No Matches
tls.h
Go to the documentation of this file.
1#ifndef PROTON_TLS_H
2#define PROTON_TLS_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>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
40
82
86typedef struct pn_tls_t pn_tls_t;
87
95
99
100#define PN_TLS_OK (0)
101#define PN_TLS_INIT_ERR (-1)
102#define PN_TLS_PROTOCOL_ERR (-2)
103#define PN_TLS_AUTHENTICATION_ERR (-3)
104#define PN_TLS_STATE_ERR (-4)
105
106
119
126PN_TLS_EXTERN void pn_tls_config_free(pn_tls_config_t *domain);
127
153 const char *credential_1,
154 const char *credential_2,
155 const char *password);
156
183 const char *certificate_db);
184
207
233 const pn_tls_verify_mode_t mode,
234 const char *trusted_CAs);
235
246PN_TLS_EXTERN int pn_tls_config_set_impl_ciphers(pn_tls_config_t *domain, const char *ciphers);
247
254PN_TLS_EXTERN pn_tls_t *pn_tls(pn_tls_config_t *domain);
255
266PN_TLS_EXTERN int pn_tls_start(pn_tls_t *tls);
267
268
269PN_TLS_EXTERN void pn_tls_free(pn_tls_t *tls);
270
284PN_TLS_EXTERN bool pn_tls_get_cipher(pn_tls_t *tls, const char **cipher, size_t *size);
285
292PN_TLS_EXTERN int pn_tls_get_ssf(pn_tls_t *tls);
293
306PN_TLS_EXTERN bool pn_tls_get_protocol_version(pn_tls_t *tls, const char **version, size_t *size);
307
340PN_TLS_EXTERN int pn_tls_set_peer_hostname(pn_tls_t *tls, const char *hostname);
341
355PN_TLS_EXTERN int pn_tls_get_peer_hostname(pn_tls_t *tls, char *hostname, size_t *bufsize);
356
364PN_TLS_EXTERN const char* pn_tls_get_remote_subject(pn_tls_t *tls);
365
369typedef enum {
370 PN_TLS_CERT_SUBJECT_COUNTRY_NAME,
371 PN_TLS_CERT_SUBJECT_STATE_OR_PROVINCE,
372 PN_TLS_CERT_SUBJECT_CITY_OR_LOCALITY,
373 PN_TLS_CERT_SUBJECT_ORGANIZATION_NAME,
374 PN_TLS_CERT_SUBJECT_ORGANIZATION_UNIT,
375 PN_TLS_CERT_SUBJECT_COMMON_NAME
377
381typedef enum {
382 PN_TLS_SHA1, /* Produces hash that is 20 bytes long */
383 PN_TLS_SHA256, /* Produces hash that is 32 bytes long */
384 PN_TLS_SHA512, /* Produces hash that is 64 bytes long */
385 PN_TLS_MD5 /* Produces hash that is 16 bytes long */
387
401PN_TLS_EXTERN int pn_tls_get_cert_fingerprint(pn_tls_t *tls0,
402 char *fingerprint,
403 size_t fingerprint_length,
404 pn_tls_hash_alg hash_alg);
405
419
420PN_TLS_EXTERN bool pn_tls_is_encrypt_output_pending(pn_tls_t *tls);
421PN_TLS_EXTERN bool pn_tls_is_decrypt_output_pending(pn_tls_t *tls);
422
423
424// True if peers have negotiated a TLS session. False indicates handshake in progress or protocol error.
425PN_TLS_EXTERN bool pn_tls_is_secure(pn_tls_t *tls);
426
427
428// ----------------------------------------------------------------------------------------------------------------------
429
430// A common pool of buffers to put both encrypted and decrypted bytes in:
431// - this could easily just be split into 2 result buffer sets if preferred.
432
433// Give buffers to store encryption/decryption results
434// returns the number of buffers taken - it's possible that we don't have space
435// to record all of them
436PN_TLS_EXTERN size_t pn_tls_give_encrypt_output_buffers(pn_tls_t*, pn_raw_buffer_t const*, size_t count);
437PN_TLS_EXTERN size_t pn_tls_give_decrypt_output_buffers(pn_tls_t*, pn_raw_buffer_t const*, size_t count);
438
439
440// Take result buffers back into app ownership, return the actual number of buffers returned
441// keep calling these until the number returned is 0 to make sure you get all buffers currently available
442// Gives only buffers with encrypted/decrypted content before pn_tls_stop() and all buffers afterwards.
443PN_TLS_EXTERN size_t pn_tls_take_decrypt_output_buffers(pn_tls_t*, pn_raw_buffer_t*, size_t count);
444PN_TLS_EXTERN size_t pn_tls_take_encrypt_output_buffers(pn_tls_t*, pn_raw_buffer_t*, size_t count);
445
446// Stage data to be encrypted by the engine at a future pn_tls_process() step.
447// returned value is number of buffers taken (ownership transfer)
448// i.e. held by the tls code - governed by capacity.
449PN_TLS_EXTERN size_t pn_tls_give_encrypt_input_buffers(pn_tls_t*, pn_raw_buffer_t const* bufs, size_t count_bufs);
450
451// returned value is number of buffers taken
452PN_TLS_EXTERN size_t pn_tls_give_decrypt_input_buffers(pn_tls_t*, pn_raw_buffer_t const* bufs, size_t count_bufs);
453
454// Take input buffers back into app ownership, return the actual number of buffers returned
455// Returns buffers whose data is fully processed (or pn_tls_stop() called).
456// keep calling these until the number returned is 0 to make sure you get all buffers currently available
457PN_TLS_EXTERN size_t pn_tls_take_encrypt_input_buffers(pn_tls_t*, pn_raw_buffer_t*, size_t count);
458PN_TLS_EXTERN size_t pn_tls_take_decrypt_input_buffers(pn_tls_t*, pn_raw_buffer_t*, size_t count);
459
460// Return the max number of additional input buffers we can hold
461PN_TLS_EXTERN size_t pn_tls_get_encrypt_input_buffer_capacity(pn_tls_t*);
462PN_TLS_EXTERN size_t pn_tls_get_decrypt_input_buffer_capacity(pn_tls_t*);
463
464// True if there is no remaining space in the XXcrypt result buffers owned by the
465// engine and there is XXcryped data available to put in a result buffer since
466// the last pn_tls_process()
467PN_TLS_EXTERN bool pn_tls_need_encrypt_output_buffers(pn_tls_t*);
468PN_TLS_EXTERN bool pn_tls_need_decrypt_output_buffers(pn_tls_t*);
469
470PN_TLS_EXTERN size_t pn_tls_get_encrypt_output_buffer_capacity(pn_tls_t*);
471PN_TLS_EXTERN size_t pn_tls_get_decrypt_output_buffer_capacity(pn_tls_t*);
472
473// Number of buffers ready to be returned by take operation since last pn_tls_process() or pn_tls_stop()
474PN_TLS_EXTERN size_t pn_tls_get_decrypt_output_buffer_count(pn_tls_t*);
475PN_TLS_EXTERN size_t pn_tls_get_encrypt_output_buffer_count(pn_tls_t*);
476
477PN_TLS_EXTERN uint32_t pn_tls_get_last_decrypt_output_buffer_size(pn_tls_t*);
478PN_TLS_EXTERN uint32_t pn_tls_get_last_encrypt_output_buffer_size(pn_tls_t*);
479
480
481// Configurable. Zero implies "use default". No effect after pn_tls_start().
482PN_TLS_EXTERN void pn_tls_set_encrypt_input_buffer_max_capacity(pn_tls_t*, size_t s);
483PN_TLS_EXTERN void pn_tls_set_decrypt_input_buffer_max_capacity(pn_tls_t*, size_t s);
484PN_TLS_EXTERN void pn_tls_set_encrypt_output_buffer_max_capacity(pn_tls_t*, size_t s);
485PN_TLS_EXTERN void pn_tls_set_decrypt_output_buffer_max_capacity(pn_tls_t*, size_t s);
486
487// Process as much unencrypted data to encrypted result data as possible.
488// Also process as much undecrypted data to decryptedcrypted result data as possible.
489// Check for TLS engine errors here.
490// Always returns an error if called before pn_tls_start() or after pn_tls_stop().
491PN_TLS_EXTERN int pn_tls_process(pn_tls_t* tls);
492
493// Future pn_tls_process() or pn_tls_give_xxx() are no-ops.
494// Unused encrypt/decrypt result buffers become zero length encrypted/decrypted result
495// buffers and can be reclaimed.
496// Future: If no closure from peer or self closure not written to result buffer, session resume cancelled.
497PN_TLS_EXTERN int pn_tls_stop(pn_tls_t* tls);
498
499// Confirms receipt of the peer's TLS closure record. This confirms clean shutdown and
500// the absence of a truncation attack.
501PN_TLS_EXTERN bool pn_tls_is_input_closed(pn_tls_t* tls);
502
503// Closes the encrypt side and appends the TLS closure record to the pending encypted
504// output. pn_tls_give_encrypt_input_buffers() will no longer take any supplied buffers.
505PN_TLS_EXTERN void pn_tls_close_output(pn_tls_t* tls);
506
507// If non-zero the TLS session was unable to start or was aborted.
508// The application should stop all read activity, and take all
509// remaining encrypted content and write it onto the connection
510// (i.e. until pn_tls_is_encrypt_output_pending() is false), then
511// close the associated connection. This will allow the error to be
512// propagated to the peer if expected by the TLS protocol. Specific return values TBD (INIT_FAILED,
513// BAD_AUTH, TLS_PROTOCOL_ERROR, ...).
514PN_TLS_EXTERN int pn_tls_get_session_error(pn_tls_t* tls);
515
516// Error string associated with the fatal TLS session error. zero if no error, buf is null or buf_len is 0.
517PN_TLS_EXTERN size_t pn_tls_get_session_error_string(pn_tls_t* tls, char *buf, size_t buf_len);
518
540PN_TLS_EXTERN int pn_tls_config_set_alpn_protocols(pn_tls_config_t *domain, const char **protocols, size_t protocol_count);
541
554PN_TLS_EXTERN bool pn_tls_get_alpn_protocol(pn_tls_t *tls, const char **protocol_name, size_t *size);
555
556// TODO: Tracing. Session tickets.
557
561
562#ifdef __cplusplus
563}
564#endif
565
566#endif /* tls.h */
A descriptor used to represent a single raw buffer in memory.
Definition raw_connection.h:54
PN_TLS_EXTERN bool pn_tls_get_alpn_protocol(pn_tls_t *tls, const char **protocol_name, size_t *size)
Get the name of the negotiated application protocol.
PN_TLS_EXTERN bool pn_tls_get_cipher(pn_tls_t *tls, const char **cipher, size_t *size)
Get the name of the Cipher that is currently in use.
PN_TLS_EXTERN int pn_tls_get_peer_hostname(pn_tls_t *tls, char *hostname, size_t *bufsize)
Access the configured peer identity.
PN_TLS_EXTERN int pn_tls_start(pn_tls_t *tls)
Start a TLS session.
PN_TLS_EXTERN void pn_tls_config_free(pn_tls_config_t *domain)
Release an TLS configuration domain.
PN_TLS_EXTERN int pn_tls_get_cert_fingerprint(pn_tls_t *tls0, char *fingerprint, size_t fingerprint_length, pn_tls_hash_alg hash_alg)
Get the fingerprint of the certificate.
pn_tls_hash_alg
Enumeration identifying hashing algorithm.
Definition tls.h:381
PN_TLS_EXTERN int pn_tls_set_peer_hostname(pn_tls_t *tls, const char *hostname)
Set the expected identity of the remote peer.
PN_TLS_EXTERN pn_tls_config_t * pn_tls_config(pn_tls_mode_t mode)
Create an TLS configuration domain.
PN_TLS_EXTERN int pn_tls_config_set_trusted_certs(pn_tls_config_t *domain, const char *certificate_db)
Configure the set of trusted CA certificates used by this domain to verify peers.
struct pn_tls_t pn_tls_t
Definition tls.h:86
PN_TLS_EXTERN const char * pn_tls_get_remote_subject_subfield(pn_tls_t *tls, pn_tls_cert_subject_subfield field)
Returns a char pointer that contains the value of the sub field of the subject field in the tls certi...
pn_tls_mode_t
Determines the type of TLS endpoint.
Definition tls.h:91
pn_tls_verify_mode_t
Determines the level of peer validation.
Definition tls.h:201
PN_TLS_EXTERN int pn_tls_config_set_peer_authentication(pn_tls_config_t *domain, const pn_tls_verify_mode_t mode, const char *trusted_CAs)
Configure the level of verification used on the peer certificate.
PN_TLS_EXTERN bool pn_tls_get_protocol_version(pn_tls_t *tls, const char **version, size_t *size)
Get the name of the TLS protocol that is currently in use.
PN_TLS_EXTERN int pn_tls_config_set_credentials(pn_tls_config_t *domain, const char *credential_1, const char *credential_2, const char *password)
Set the certificate that identifies the local node to the remote.
PN_TLS_EXTERN int pn_tls_config_set_impl_ciphers(pn_tls_config_t *domain, const char *ciphers)
Configure the list of permitted ciphers.
PN_TLS_EXTERN const char * pn_tls_get_remote_subject(pn_tls_t *tls)
Get the subject from the peers certificate.
pn_tls_cert_subject_subfield
Enumeration identifying the sub fields of the subject field in the tls certificate.
Definition tls.h:369
struct pn_tls_config_t pn_tls_config_t
API for using TLS separate from AMQP connections.
Definition tls.h:81
PN_TLS_EXTERN pn_tls_t * pn_tls(pn_tls_config_t *domain)
Create a new TLS session object derived from a domain.
PN_TLS_EXTERN int pn_tls_get_ssf(pn_tls_t *tls)
Get the SSF (security strength factor) of the Cipher that is currently in use.
PN_TLS_EXTERN int pn_tls_config_set_alpn_protocols(pn_tls_config_t *domain, const char **protocols, size_t protocol_count)
Provide an ordered list of application protols for RFC 7301 negotiation.
@ PN_TLS_MODE_SERVER
Local connection endpoint is an TLS server.
Definition tls.h:93
@ PN_TLS_MODE_CLIENT
Local connection endpoint is an TLS client.
Definition tls.h:92
@ PN_TLS_VERIFY_NULL
internal use only
Definition tls.h:202
@ PN_TLS_VERIFY_PEER_NAME
require valid certificate and matching name
Definition tls.h:205
@ PN_TLS_VERIFY_PEER
require peer to provide a valid identifying certificate
Definition tls.h:203
@ PN_TLS_ANONYMOUS_PEER
do not require a certificate nor cipher authorization
Definition tls.h:204