Debian Codegraph @

Xpjproject/pjlib/include/pj/ssl_sock.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2009-2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #ifndef __PJ_SSL_SOCK_H__
19 #define __PJ_SSL_SOCK_H__
20
21 /**
22 * @file ssl_sock.h
23 * @brief Secure socket
24 */
25
26 #include <pj/ioqueue.h>
27 #include <pj/sock.h>
28 #include <pj/sock_qos.h>
29
30
31 PJ_BEGIN_DECL
32
33 /**
34 * @defgroup PJ_SSL_SOCK Secure socket I/O
35 * @brief Secure socket provides security on socket operation using standard
36 * security protocols such as SSL and TLS.
37 * @ingroup PJ_IO
38 * @{
39 *
40 * Secure socket wraps normal socket and applies security features, i.e:
41 * privacy and data integrity, on the socket traffic, using standard security
42 * protocols such as SSL and TLS.
43 *
44 * Secure socket employs active socket operations, which is similar to (and
45 * described more detail) in \ref PJ_ACTIVESOCK.
46 */
47
48
49 /**
50 * This opaque structure describes the secure socket.
51 */
52 typedef struct pj_ssl_sock_t pj_ssl_sock_t;
53
54
55 /**
56 * Opaque declaration of endpoint certificate or credentials. This may contains
57 * certificate, private key, and trusted Certificate Authorities list.
58 */
59 typedef struct pj_ssl_cert_t pj_ssl_cert_t;
60
61
62 /**
63 * Bitwise flag for SSL certificate verification.
64 */
65 typedef enum pj_ssl_cert_verify_flag_t
66 {
67 /**
68 * No error in verification.
69 */
70 PJ_SSL_CERT_ESUCCESS = 0,
71
72 /**
73 * The issuer certificate cannot be found.
74 */
75 PJ_SSL_CERT_EISSUER_NOT_FOUND = (1 << 0),
76
77 /**
78 * The certificate is untrusted.
79 */
80 PJ_SSL_CERT_EUNTRUSTED = (1 << 1),
81
82 /**
83 * The certificate has expired or not yet valid.
84 */
85 PJ_SSL_CERT_EVALIDITY_PERIOD = (1 << 2),
86
87 /**
88 * One or more fields of the certificate cannot be decoded due to
89 * invalid format.
90 */
91 PJ_SSL_CERT_EINVALID_FORMAT = (1 << 3),
92
93 /**
94 * The certificate cannot be used for the specified purpose.
95 */
96 PJ_SSL_CERT_EINVALID_PURPOSE = (1 << 4),
97
98 /**
99 * The issuer info in the certificate does not match to the (candidate)
100 * issuer certificate, e.g: issuer name not match to subject name
101 * of (candidate) issuer certificate.
102 */
103 PJ_SSL_CERT_EISSUER_MISMATCH = (1 << 5),
104
105 /**
106 * The CRL certificate cannot be found or cannot be read properly.
107 */
108 PJ_SSL_CERT_ECRL_FAILURE = (1 << 6),
109
110 /**
111 * The certificate has been revoked.
112 */
113 PJ_SSL_CERT_EREVOKED = (1 << 7),
114
115 /**
116 * The certificate chain length is too long.
117 */
118 PJ_SSL_CERT_ECHAIN_TOO_LONG = (1 << 8),
119
120 /**
121 * The certificate signature is created using a weak hashing algorithm.
122 */
123 PJ_SSL_CERT_EWEAK_SIGNATURE = (1 << 9),
124
125 /**
126 * The server identity does not match to any identities specified in
127 * the certificate, e.g: subjectAltName extension, subject common name.
128 * This flag will only be set by application as SSL socket does not
129 * perform server identity verification.
130 */
131 PJ_SSL_CERT_EIDENTITY_NOT_MATCH = (1 << 30),
132
133 /**
134 * Unknown verification error.
135 */
136 PJ_SSL_CERT_EUNKNOWN = (1 << 31)
137
138 } pj_ssl_cert_verify_flag_t;
139
140
141 /**
142 * Type of SSL certificate name.
143 */
144 typedef enum pj_ssl_cert_name_type
145 {
146 PJ_SSL_CERT_NAME_UNKNOWN = 0,
147 PJ_SSL_CERT_NAME_RFC822,
148 PJ_SSL_CERT_NAME_DNS,
149 PJ_SSL_CERT_NAME_URI,
150 PJ_SSL_CERT_NAME_IP
151 } pj_ssl_cert_name_type;
152
153
154 /**
155 * Field type for looking up SSL certificate in the certificate stores.
156 */
157 typedef enum pj_ssl_cert_lookup_type
158 {
159 /**
160 * No certificate to be looked up.
161 */
162 PJ_SSL_CERT_LOOKUP_NONE,
163
164 /**
165 * Lookup by subject, this will lookup any first certificate whose
166 * subject containing the specified keyword. Note that subject may not
167 * be unique in the store, so the lookup may end up selecting a wrong
168 * certificate.
169 */
170 PJ_SSL_CERT_LOOKUP_SUBJECT,
171
172 /**
173 * Lookup by fingerprint/thumbprint (SHA1 hash), this will lookup
174 * any first certificate whose fingerprint matching the specified
175 * keyword. The keyword is an array of hash octets.
176 */
177 PJ_SSL_CERT_LOOKUP_FINGERPRINT,
178
179 /**
180 * Lookup by friendly name, this will lookup any first certificate
181 * whose friendly name containing the specified keyword. Note that
182 * friendly name may not be unique in the store, so the lookup may end up
183 * selecting a wrong certificate.
184 */
185 PJ_SSL_CERT_LOOKUP_FRIENDLY_NAME
186
187 } pj_ssl_cert_lookup_type;
188
189
190 /**
191 * Describe structure of certificate lookup criteria.
192 */
193 typedef struct pj_ssl_cert_lookup_criteria
194 {
195 /**
196 * Certificate field type to look.
197 */
198 pj_ssl_cert_lookup_type type;
199
200 /*
201 * Keyword to match on the field specified in \a type.
202 */
203 pj_str_t keyword;
204
205 } pj_ssl_cert_lookup_criteria;
206
207
208 /**
209 * The SSL certificate buffer.
210 */
211 typedef pj_str_t pj_ssl_cert_buffer;
212
213
214 /**
215 * Data type of the direct SSL certificate.
216 */
217 typedef enum pj_ssl_cert_direct_type
218 {
219 /**
220 * No data.
221 */
222 PJ_SSL_CERT_DIRECT_NONE = 0,
223
224 /**
225 * Data is OpenSSL EVP_PKEY.
226 */
227 PJ_SSL_CERT_DIRECT_OPENSSL_EVP_PKEY = 1,
228
229 /**
230 * Data is OpenSSL X509 certificate.
231 */
232 PJ_SSL_CERT_DIRECT_OPENSSL_X509_CERT = 2,
233
234 } pj_ssl_cert_direct_type;
235
236
237 /**
238 * The direct SSL certificate. Instead of loading certificate from file,
239 * buffer, or OS certificate store, application can provide direct access
240 * to the backend specific certificate data using this structure.
241 */
242 typedef struct pj_ssl_cert_direct
243 {
244 /**
245 * Bit flag of the type of direct certificate data, see
246 * \ref pj_ssl_cert_direct_type.
247 */
248 unsigned type;
249
250 /**
251 * Pointer to backend specific private key object, e.g: OpenSSL EVP_PKEY.
252 */
253 void *privkey;
254
255 /**
256 * Pointer to backend specific certificate object, e.g: OpenSSL X509.
257 */
258 void *cert;
259
260 } pj_ssl_cert_direct;
261
262
263 /**
264 * Describe structure of certificate info.
265 */
266 typedef struct pj_ssl_cert_info {
267
268 unsigned version; /**< Certificate version */
269
270 pj_uint8_t serial_no[20]; /**< Serial number, array of
271 octets, first index is
272 MSB */
273
274 struct {
275 pj_str_t cn; /**< Common name */
276 pj_str_t info; /**< One line subject, fields
277 are separated by slash, e.g:
278 "CN=sample.org/OU=HRD" */
279 } subject; /**< Subject */
280
281 struct {
282 pj_str_t cn; /**< Common name */
283 pj_str_t info; /**< One line subject, fields
284 are separated by slash.*/
285 } issuer; /**< Issuer */
286
287 struct {
288 pj_time_val start; /**< Validity start */
289 pj_time_val end; /**< Validity end */
290 pj_bool_t gmt; /**< Flag if validity date/time
291 use GMT */
292 } validity; /**< Validity */
293
294 struct {
295 unsigned cnt; /**< # of entry */
296 struct {
297 pj_ssl_cert_name_type type;
298 /**< Name type */
299 pj_str_t name; /**< The name */
300 } *entry; /**< Subject alt name entry */
301 } subj_alt_name; /**< Subject alternative
302 name extension */
303
304 pj_str_t raw; /**< Raw certificate in PEM format, only
305 available for remote certificate. */
306
307 struct {
308 unsigned cnt; /**< # of entry */
309 pj_str_t *cert_raw;
310 } raw_chain;
311
312 } pj_ssl_cert_info;
313
314
315 /**
316 * Create credential from files. TLS server application can provide multiple
317 * certificates (RSA, ECC, and DSA) by supplying certificate name with "_rsa"
318 * suffix, e.g: "pjsip_rsa.pem", the library will automatically check for
319 * other certificates with "_ecc" and "_dsa" suffix.
320 *
321 * @param pool The pool.
322 * @param CA_file The file of trusted CA list.
323 * @param cert_file The file of certificate.
324 * @param privkey_file The file of private key.
325 * @param privkey_pass The password of private key, if any.
326 * @param p_cert Pointer to credential instance.
327 * If the credential instance is NULL, a new credential
328 * instance will be created, otherwise the specified
329 * credential instance will be used.
330 *
331 * @return PJ_SUCCESS when successful.
332 */
333 PJ_DECL(pj_status_t) pj_ssl_cert_load_from_files(pj_pool_t *pool,
334 const pj_str_t *CA_file,
335 const pj_str_t *cert_file,
336 const pj_str_t *privkey_file,
337 const pj_str_t *privkey_pass,
338 pj_ssl_cert_t **p_cert);
339
340 /**
341 * Create credential from files. TLS server application can provide multiple
342 * certificates (RSA, ECC, and DSA) by supplying certificate name with "_rsa"
343 * suffix, e.g: "pjsip_rsa.pem", the library will automatically check for
344 * other certificates with "_ecc" and "_dsa" suffix.
345 *
346 * This is the same as pj_ssl_cert_load_from_files() but also
347 * accepts an additional param CA_path to load CA certificates from
348 * a directory.
349 *
350 * @param pool The pool.
351 * @param CA_file The file of trusted CA list.
352 * @param CA_path The path to a directory of trusted CA list.
353 * @param cert_file The file of certificate.
354 * @param privkey_file The file of private key.
355 * @param privkey_pass The password of private key, if any.
356 * @param p_cert Pointer to credential instance.
357 * If the credential instance is NULL, a new credential
358 * instance will be created, otherwise the specified
359 * credential instance will be used.
360 *
361 * @return PJ_SUCCESS when successful.
362 */
363 PJ_DECL(pj_status_t) pj_ssl_cert_load_from_files2(
364 pj_pool_t *pool,
365 const pj_str_t *CA_file,
366 const pj_str_t *CA_path,
367 const pj_str_t *cert_file,
368 const pj_str_t *privkey_file,
369 const pj_str_t *privkey_pass,
370 pj_ssl_cert_t **p_cert);
371
372
373 /**
374 * Create credential from data buffer. The certificate expected is in
375 * PEM format.
376 *
377 * @param pool The pool.
378 * @param CA_buf The buffer of trusted CA list.
379 * @param cert_buf The buffer of certificate.
380 * @param privkey_buf The buffer of private key.
381 * @param privkey_pass The password of private key, if any.
382 * @param p_cert Pointer to credential instance.
383 * If the credential instance is NULL, a new credential
384 * instance will be created, otherwise the specified
385 * credential instance will be used.
386 *
387 * @return PJ_SUCCESS when successful.
388 */
389 PJ_DECL(pj_status_t) pj_ssl_cert_load_from_buffer(pj_pool_t *pool,
390 const pj_ssl_cert_buffer *CA_buf,
391 const pj_ssl_cert_buffer *cert_buf,
392 const pj_ssl_cert_buffer *privkey_buf,
393 const pj_str_t *privkey_pass,
394 pj_ssl_cert_t **p_cert);
395
396
397 /**
398 * Create credential from OS certificate store, this function will lookup
399 * certificate using the specified criterias.
400 *
401 * Currently this is used by Windows Schannel backend only, it will lookup
402 * in the Current User store first, if no certificate with the specified
403 * criteria is not found, it will lookup in the Local Machine store.
404 *
405 * Note that for manual verification (e.g: when pj_ssl_sock_param.verify_peer
406 * is disabled), the backend will provide pre-verification result against
407 * trusted CA certificates in Current User store only (will not check CA
408 * certificates in the Local Machine store).
409 *
410 * @param pool The pool.
411 * @param criteria The lookup criteria.
412 * @param p_cert Pointer to credential instance.
413 * If the credential instance is NULL, a new credential
414 * instance will be created, otherwise the specified
415 * credential instance will be used.
416 *
417 * @return PJ_SUCCESS when successful.
418 */
419 PJ_DECL(pj_status_t) pj_ssl_cert_load_from_store(
420 pj_pool_t *pool,
421 const pj_ssl_cert_lookup_criteria *criteria,
422 pj_ssl_cert_t **p_cert);
423
424
425 /**
426 * Create credential from the backend specific objects.
427 *
428 * For example, application can use OpenSSL ENGINE to load a private key from
429 * a hardware device, and then provide the EVP_PKEY instance to be used by
430 * OpenSSL backend via this function.
431 *
432 * Application should maintain the objects lifetime until the credential
433 * instance is no longer used (e.g: SSL socket is destroyed).
434 * In OpenSSL version 3, the SSL socket utilizes the reference counting
435 * feature to manage object lifetimes. Specifically,
436 * pj_ssl_sock_set_certificate() increments the reference count and
437 * pj_ssl_sock_close() decrements it.
438 *
439 * @param pool The pool.
440 * @param cert_direct The backend specific objects.
441 * @param p_cert Pointer to credential instance.
442 * If the credential instance is NULL, a new credential
443 * instance will be created, otherwise the specified
444 * credential instance will be used.
445 *
446 * @return PJ_SUCCESS when successful.
447 */
448 PJ_DECL(pj_status_t) pj_ssl_cert_load_direct(
449 pj_pool_t *pool,
450 pj_ssl_cert_direct *cert_direct,
451 pj_ssl_cert_t **p_cert);
452
453
454 /**
455 * Dump SSL certificate info.
456 *
457 * @param ci The certificate info.
458 * @param indent String for left indentation.
459 * @param buf The buffer where certificate info will be printed on.
460 * @param buf_size The buffer size.
461 *
462 * @return The length of the dump result, or -1 when buffer size
463 * is not sufficient.
464 */
465 PJ_DECL(pj_ssize_t) pj_ssl_cert_info_dump(const pj_ssl_cert_info *ci,
466 const char *indent,
467 char *buf,
468 pj_size_t buf_size);
469
470
471 /**
472 * Get SSL certificate verification error messages from verification status.
473 *
474 * @param verify_status The SSL certificate verification status.
475 * @param error_strings Array of strings to receive the verification error
476 * messages.
477 * @param count On input it specifies maximum error messages should be
478 * retrieved. On output it specifies the number of error
479 * messages retrieved.
480 *
481 * @return PJ_SUCCESS when successful.
482 */
483 PJ_DECL(pj_status_t) pj_ssl_cert_get_verify_status_strings(
484 pj_uint32_t verify_status,
485 const char *error_strings[],
486 unsigned *count);
487
488 /**
489 * Wipe out the keys in the SSL certificate.
490 *
491 * @param cert The SSL certificate.
492 *
493 */
494 PJ_DECL(void) pj_ssl_cert_wipe_keys(pj_ssl_cert_t *cert);
495
496
497 /**
498 * Cipher suites enumeration.
499 */
500 typedef enum pj_ssl_cipher {
501
502 /* Unsupported cipher */
503 PJ_TLS_UNKNOWN_CIPHER = -1,
504
505 /* NULL */
506 PJ_TLS_NULL_WITH_NULL_NULL = 0x00000000,
507
508 /* TLS/SSLv3 */
509 PJ_TLS_RSA_WITH_NULL_MD5 = 0x00000001,
510 PJ_TLS_RSA_WITH_NULL_SHA = 0x00000002,
511 PJ_TLS_RSA_WITH_NULL_SHA256 = 0x0000003B,
512 PJ_TLS_RSA_WITH_RC4_128_MD5 = 0x00000004,
513 PJ_TLS_RSA_WITH_RC4_128_SHA = 0x00000005,
514 PJ_TLS_RSA_WITH_3DES_EDE_CBC_SHA = 0x0000000A,
515 PJ_TLS_RSA_WITH_AES_128_CBC_SHA = 0x0000002F,
516 PJ_TLS_RSA_WITH_AES_256_CBC_SHA = 0x00000035,
517 PJ_TLS_RSA_WITH_AES_128_CBC_SHA256 = 0x0000003C,
518 PJ_TLS_RSA_WITH_AES_256_CBC_SHA256 = 0x0000003D,
519 PJ_TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA = 0x0000000D,
520 PJ_TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA = 0x00000010,
521 PJ_TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA = 0x00000013,
522 PJ_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA = 0x00000016,
523 PJ_TLS_DH_DSS_WITH_AES_128_CBC_SHA = 0x00000030,
524 PJ_TLS_DH_RSA_WITH_AES_128_CBC_SHA = 0x00000031,
525 PJ_TLS_DHE_DSS_WITH_AES_128_CBC_SHA = 0x00000032,
526 PJ_TLS_DHE_RSA_WITH_AES_128_CBC_SHA = 0x00000033,
527 PJ_TLS_DH_DSS_WITH_AES_256_CBC_SHA = 0x00000036,
528 PJ_TLS_DH_RSA_WITH_AES_256_CBC_SHA = 0x00000037,
529 PJ_TLS_DHE_DSS_WITH_AES_256_CBC_SHA = 0x00000038,
530 PJ_TLS_DHE_RSA_WITH_AES_256_CBC_SHA = 0x00000039,
531 PJ_TLS_DH_DSS_WITH_AES_128_CBC_SHA256 = 0x0000003E,
532 PJ_TLS_DH_RSA_WITH_AES_128_CBC_SHA256 = 0x0000003F,
533 PJ_TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 = 0x00000040,
534 PJ_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 = 0x00000067,
535 PJ_TLS_DH_DSS_WITH_AES_256_CBC_SHA256 = 0x00000068,
536 PJ_TLS_DH_RSA_WITH_AES_256_CBC_SHA256 = 0x00000069,
537 PJ_TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 = 0x0000006A,
538 PJ_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 = 0x0000006B,
539 PJ_TLS_DH_anon_WITH_RC4_128_MD5 = 0x00000018,
540 PJ_TLS_DH_anon_WITH_3DES_EDE_CBC_SHA = 0x0000001B,
541 PJ_TLS_DH_anon_WITH_AES_128_CBC_SHA = 0x00000034,
542 PJ_TLS_DH_anon_WITH_AES_256_CBC_SHA = 0x0000003A,
543 PJ_TLS_DH_anon_WITH_AES_128_CBC_SHA256 = 0x0000006C,
544 PJ_TLS_DH_anon_WITH_AES_256_CBC_SHA256 = 0x0000006D,
545
546 PJ_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 = 0x0000c02c,
547 PJ_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 = 0x0000c030,
548 PJ_TLS_DHE_DSS_WITH_AES_256_GCM_SHA384 = 0x000000a3,
549 PJ_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 = 0x0000009f,
550 PJ_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 = 0x0000c02b,
551 PJ_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 = 0x0000c02f,
552
553 /* TLS 1.3 cipher suites */
554 PJ_TLS_AES_128_GCM_SHA256 = 0x00001301,
555 PJ_TLS_AES_256_GCM_SHA384 = 0x00001302,
556 PJ_TLS_CHACHA20_POLY1305_SHA256 = 0x00001303,
557
558 /* TLS (deprecated) */
559 PJ_TLS_RSA_EXPORT_WITH_RC4_40_MD5 = 0x00000003,
560 PJ_TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5 = 0x00000006,
561 PJ_TLS_RSA_WITH_IDEA_CBC_SHA = 0x00000007,
562 PJ_TLS_RSA_EXPORT_WITH_DES40_CBC_SHA = 0x00000008,
563 PJ_TLS_RSA_WITH_DES_CBC_SHA = 0x00000009,
564 PJ_TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA = 0x0000000B,
565 PJ_TLS_DH_DSS_WITH_DES_CBC_SHA = 0x0000000C,
566 PJ_TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA = 0x0000000E,
567 PJ_TLS_DH_RSA_WITH_DES_CBC_SHA = 0x0000000F,
568 PJ_TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA = 0x00000011,
569 PJ_TLS_DHE_DSS_WITH_DES_CBC_SHA = 0x00000012,
570 PJ_TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA = 0x00000014,
571 PJ_TLS_DHE_RSA_WITH_DES_CBC_SHA = 0x00000015,
572 PJ_TLS_DH_anon_EXPORT_WITH_RC4_40_MD5 = 0x00000017,
573 PJ_TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA = 0x00000019,
574 PJ_TLS_DH_anon_WITH_DES_CBC_SHA = 0x0000001A,
575
576 /* SSLv3 */
577 PJ_SSL_FORTEZZA_KEA_WITH_NULL_SHA = 0x0000001C,
578 PJ_SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA = 0x0000001D,
579 PJ_SSL_FORTEZZA_KEA_WITH_RC4_128_SHA = 0x0000001E,
580
581 /* SSLv2 */
582 PJ_SSL_CK_RC4_128_WITH_MD5 = 0x00010080,
583 PJ_SSL_CK_RC4_128_EXPORT40_WITH_MD5 = 0x00020080,
584 PJ_SSL_CK_RC2_128_CBC_WITH_MD5 = 0x00030080,
585 PJ_SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5 = 0x00040080,
586 PJ_SSL_CK_IDEA_128_CBC_WITH_MD5 = 0x00050080,
587 PJ_SSL_CK_DES_64_CBC_WITH_MD5 = 0x00060040,
588 PJ_SSL_CK_DES_192_EDE3_CBC_WITH_MD5 = 0x000700C0
589
590 } pj_ssl_cipher;
591
592
593 /**
594 * Get cipher list supported by SSL/TLS backend.
595 *
596 * @param ciphers The ciphers buffer to receive cipher list.
597 * @param cipher_num Maximum number of ciphers to be received.
598 *
599 * @return PJ_SUCCESS when successful.
600 */
601 PJ_DECL(pj_status_t) pj_ssl_cipher_get_availables(pj_ssl_cipher ciphers[],
602 unsigned *cipher_num);
603
604
605 /**
606 * Check if the specified cipher is supported by SSL/TLS backend.
607 *
608 * @param cipher The cipher.
609 *
610 * @return PJ_TRUE when supported.
611 */
612 PJ_DECL(pj_bool_t) pj_ssl_cipher_is_supported(pj_ssl_cipher cipher);
613
614
615 /**
616 * Get cipher name string.
617 *
618 * @param cipher The cipher.
619 *
620 * @return The cipher name or NULL if cipher is not recognized/
621 * supported.
622 */
623 PJ_DECL(const char*) pj_ssl_cipher_name(pj_ssl_cipher cipher);
624
625
626 /**
627 * Get cipher ID from cipher name string. Note that on different backends
628 * (e.g. OpenSSL or Symbian implementation), cipher names may not be
629 * equivalent for the same cipher ID.
630 *
631 * @param cipher_name The cipher name string.
632 *
633 * @return The cipher ID or PJ_TLS_UNKNOWN_CIPHER if the cipher
634 * name string is not recognized/supported.
635 */
636 PJ_DECL(pj_ssl_cipher) pj_ssl_cipher_id(const char *cipher_name);
637
638 /**
639 * Elliptic curves enumeration.
640 */
641 typedef enum pj_ssl_curve
642 {
643 PJ_TLS_UNKNOWN_CURVE = 0,
644 PJ_TLS_CURVE_SECT163K1 = 1,
645 PJ_TLS_CURVE_SECT163R1 = 2,
646 PJ_TLS_CURVE_SECT163R2 = 3,
647 PJ_TLS_CURVE_SECT193R1 = 4,
648 PJ_TLS_CURVE_SECT193R2 = 5,
649 PJ_TLS_CURVE_SECT233K1 = 6,
650 PJ_TLS_CURVE_SECT233R1 = 7,
651 PJ_TLS_CURVE_SECT239K1 = 8,
652 PJ_TLS_CURVE_SECT283K1 = 9,
653 PJ_TLS_CURVE_SECT283R1 = 10,
654 PJ_TLS_CURVE_SECT409K1 = 11,
655 PJ_TLS_CURVE_SECT409R1 = 12,
656 PJ_TLS_CURVE_SECT571K1 = 13,
657 PJ_TLS_CURVE_SECT571R1 = 14,
658 PJ_TLS_CURVE_SECP160K1 = 15,
659 PJ_TLS_CURVE_SECP160R1 = 16,
660 PJ_TLS_CURVE_SECP160R2 = 17,
661 PJ_TLS_CURVE_SECP192K1 = 18,
662 PJ_TLS_CURVE_SECP192R1 = 19,
663 PJ_TLS_CURVE_SECP224K1 = 20,
664 PJ_TLS_CURVE_SECP224R1 = 21,
665 PJ_TLS_CURVE_SECP256K1 = 22,
666 PJ_TLS_CURVE_SECP256R1 = 23,
667 PJ_TLS_CURVE_SECP384R1 = 24,
668 PJ_TLS_CURVE_SECP521R1 = 25,
669 PJ_TLS_CURVE_BRAINPOOLP256R1 = 26,
670 PJ_TLS_CURVE_BRAINPOOLP384R1 = 27,
671 PJ_TLS_CURVE_BRAINPOOLP512R1 = 28,
672 PJ_TLS_CURVE_ARBITRARY_EXPLICIT_PRIME_CURVES = 0XFF01,
673 PJ_TLS_CURVE_ARBITRARY_EXPLICIT_CHAR2_CURVES = 0XFF02
674 } pj_ssl_curve;
675
676 /**
677 * Get curve list supported by SSL/TLS backend.
678 *
679 * @param curves The curves buffer to receive curve list.
680 * @param curve_num Maximum number of curves to be received.
681 *
682 * @return PJ_SUCCESS when successful.
683 */
684 PJ_DECL(pj_status_t) pj_ssl_curve_get_availables(pj_ssl_curve curves[],
685 unsigned *curve_num);
686
687 /**
688 * Check if the specified curve is supported by SSL/TLS backend.
689 *
690 * @param curve The curve.
691 *
692 * @return PJ_TRUE when supported.
693 */
694 PJ_DECL(pj_bool_t) pj_ssl_curve_is_supported(pj_ssl_curve curve);
695
696
697 /**
698 * Get curve name string.
699 *
700 * @param curve The curve.
701 *
702 * @return The curve name or NULL if curve is not recognized/
703 * supported.
704 */
705 PJ_DECL(const char*) pj_ssl_curve_name(pj_ssl_curve curve);
706
707 /**
708 * Get curve ID from curve name string. Note that on different backends
709 * (e.g. OpenSSL or Symbian implementation), curve names may not be
710 * equivalent for the same curve ID.
711 *
712 * @param curve_name The curve name string.
713 *
714 * @return The curve ID or PJ_TLS_UNKNOWN_CURVE if the curve
715 * name string is not recognized/supported.
716 */
717 PJ_DECL(pj_ssl_curve) pj_ssl_curve_id(const char *curve_name);
718
719 /**
720 * Entropy enumeration
721 */
722 typedef enum pj_ssl_entropy
723 {
724 PJ_SSL_ENTROPY_NONE = 0, /**< None */
725 PJ_SSL_ENTROPY_EGD = 1, /**< EGD */
726 PJ_SSL_ENTROPY_RANDOM = 2, /**< Random */
727 PJ_SSL_ENTROPY_URANDOM = 3, /**< Urandom */
728 PJ_SSL_ENTROPY_FILE = 4, /**< File */
729 PJ_SSL_ENTROPY_UNKNOWN = 0x0F /**< Unknown */
730 } pj_ssl_entropy_t;
731
732 /**
733 * This structure contains the callbacks to be called by the secure socket.
734 */
735 typedef struct pj_ssl_sock_cb
736 {
737 /**
738 * This callback is called when a data arrives as the result of
739 * pj_ssl_sock_start_read().
740 *
741 * @param ssock The secure socket.
742 * @param data The buffer containing the new data, if any. If
743 * the status argument is non-PJ_SUCCESS, this
744 * argument may be NULL.
745 * @param size The length of data in the buffer.
746 * @param status The status of the read operation. This may contain
747 * non-PJ_SUCCESS for example when the TCP connection
748 * has been closed. In this case, the buffer may
749 * contain left over data from previous callback which
750 * the application may want to process.
751 * @param remainder If application wishes to leave some data in the
752 * buffer (common for TCP applications), it should
753 * move the remainder data to the front part of the
754 * buffer and set the remainder length here. The value
755 * of this parameter will be ignored for datagram
756 * sockets.
757 *
758 * @return PJ_TRUE if further read is desired, and PJ_FALSE
759 * when application no longer wants to receive data.
760 * Application may destroy the secure socket in the
761 * callback and return PJ_FALSE here.
762 */
763 pj_bool_t (*on_data_read)(pj_ssl_sock_t *ssock,
764 void *data,
765 pj_size_t size,
766 pj_status_t status,
767 pj_size_t *remainder);
768 /**
769 * This callback is called when a packet arrives as the result of
770 * pj_ssl_sock_start_recvfrom().
771 *
772 * @param ssock The secure socket.
773 * @param data The buffer containing the packet, if any. If
774 * the status argument is non-PJ_SUCCESS, this
775 * argument will be set to NULL.
776 * @param size The length of packet in the buffer. If
777 * the status argument is non-PJ_SUCCESS, this
778 * argument will be set to zero.
779 * @param src_addr Source address of the packet.
780 * @param addr_len Length of the source address.
781 * @param status This contains
782 *
783 * @return PJ_TRUE if further read is desired, and PJ_FALSE
784 * when application no longer wants to receive data.
785 * Application may destroy the secure socket in the
786 * callback and return PJ_FALSE here.
787 */
788 pj_bool_t (*on_data_recvfrom)(pj_ssl_sock_t *ssock,
789 void *data,
790 pj_size_t size,
791 const pj_sockaddr_t *src_addr,
792 int addr_len,
793 pj_status_t status);
794
795 /**
796 * This callback is called when data has been sent.
797 *
798 * @param ssock The secure socket.
799 * @param send_key Key associated with the send operation.
800 * @param sent If value is positive non-zero it indicates the
801 * number of data sent. When the value is negative,
802 * it contains the error code which can be retrieved
803 * by negating the value (i.e. status=-sent).
804 *
805 * @return Application may destroy the secure socket in the
806 * callback and return PJ_FALSE here.
807 */
808 pj_bool_t (*on_data_sent)(pj_ssl_sock_t *ssock,
809 pj_ioqueue_op_key_t *send_key,
810 pj_ssize_t sent);
811
812 /**
813 * This callback is called when new connection arrives as the result
814 * of pj_ssl_sock_start_accept(). If the status of accept operation is
815 * needed use on_accept_complete2 instead of this callback.
816 *
817 * @param ssock The secure socket.
818 * @param newsock The new incoming secure socket.
819 * @param src_addr The source address of the connection.
820 * @param addr_len Length of the source address.
821 *
822 * @return PJ_TRUE if further accept() is desired, and PJ_FALSE
823 * when application no longer wants to accept incoming
824 * connection. Application may destroy the secure socket
825 * in the callback and return PJ_FALSE here.
826 */
827 pj_bool_t (*on_accept_complete)(pj_ssl_sock_t *ssock,
828 pj_ssl_sock_t *newsock,
829 const pj_sockaddr_t *src_addr,
830 int src_addr_len);
831 /**
832 * This callback is called when new connection arrives as the result
833 * of pj_ssl_sock_start_accept().
834 *
835 * @param asock The active socket.
836 * @param newsock The new incoming socket.
837 * @param src_addr The source address of the connection.
838 * @param addr_len Length of the source address.
839 * @param status The status of the accept operation. This may contain
840 * non-PJ_SUCCESS for example when the TCP listener is in
841 * bad state for example on iOS platform after the
842 * application waking up from background.
843 *
844 * @return PJ_TRUE if further accept() is desired, and PJ_FALSE
845 * when application no longer wants to accept incoming
846 * connection. Application may destroy the active socket
847 * in the callback and return PJ_FALSE here.
848 */
849 pj_bool_t (*on_accept_complete2)(pj_ssl_sock_t *ssock,
850 pj_ssl_sock_t *newsock,
851 const pj_sockaddr_t *src_addr,
852 int src_addr_len,
853 pj_status_t status);
854
855 /**
856 * This callback is called when pending connect operation has been
857 * completed.
858 *
859 * @param ssock The secure socket.
860 * @param status The connection result. If connection has been
861 * successfully established, the status will contain
862 * PJ_SUCCESS.
863 *
864 * @return Application may destroy the secure socket in the
865 * callback and return PJ_FALSE here.
866 */
867 pj_bool_t (*on_connect_complete)(pj_ssl_sock_t *ssock,
868 pj_status_t status);
869
870 /**
871 * This callback is called when certificate verification is being done.
872 * Certification info can be obtained from #pj_ssl_sock_info. Currently
873 * it's only implemented for OpenSSL backend.
874 *
875 * If this is set, the callback will always be invoked, even when peer
876 * verification is disabled (pj_ssl_sock_param.verify_peer set to
877 * PJ_FALSE).
878 *
879 * @param ssock The secure socket.
880 * @param is_server PJ_TRUE to indicate an incoming connection.
881 *
882 * @return Return PJ_TRUE if verification is successful.
883 * If verification failed, then the connection will be
884 * dropped immediately.
885 *
886 */
887 pj_bool_t (*on_verify_cb)(pj_ssl_sock_t *ssock, pj_bool_t is_server);
888
889 } pj_ssl_sock_cb;
890
891
892 /**
893 * Enumeration of secure socket protocol types.
894 * This can be combined using bitwise OR operation.
895 */
896 typedef enum pj_ssl_sock_proto
897 {
898 /**
899 * Default protocol of backend.
900 * Typically this will be set to all supported non-deprecated protocols,
901 * which, currently is TLSv1.2 and TLSv1.3.
902 */
903 PJ_SSL_SOCK_PROTO_DEFAULT = 0,
904
905 /**
906 * SSLv2.0 protocol.
907 * This protocol has been deprecated.
908 */
909 PJ_SSL_SOCK_PROTO_SSL2 = (1 << 0),
910
911 /**
912 * SSLv3.0 protocol.
913 * This protocol has been deprecated.
914 */
915 PJ_SSL_SOCK_PROTO_SSL3 = (1 << 1),
916
917 /**
918 * TLSv1.0 protocol.
919 * This protocol has been deprecated.
920 */
921 PJ_SSL_SOCK_PROTO_TLS1 = (1 << 2),
922
923 /**
924 * TLSv1.1 protocol.
925 * This protocol has been deprecated.
926 */
927 PJ_SSL_SOCK_PROTO_TLS1_1 = (1 << 3),
928
929 /**
930 * TLSv1.2 protocol.
931 */
932 PJ_SSL_SOCK_PROTO_TLS1_2 = (1 << 4),
933
934 /**
935 * TLSv1.3 protocol.
936 */
937 PJ_SSL_SOCK_PROTO_TLS1_3 = (1 << 5),
938
939 /**
940 * This protocol has been deprecated.
941 */
942 PJ_SSL_SOCK_PROTO_SSL23 = (1 << 16) - 1,
943
944 /**
945 * This will enable all the backend's supported protocols.
946 */
947 PJ_SSL_SOCK_PROTO_ALL = (1 << 16) - 1,
948
949 /**
950 * DTLSv1.0 protocol.
951 */
952 PJ_SSL_SOCK_PROTO_DTLS1 = (1 << 16),
953
954 } pj_ssl_sock_proto;
955
956
957 /**
958 * Definition of secure socket info structure.
959 */
960 typedef struct pj_ssl_sock_info
961 {
962 /**
963 * Describes whether secure socket connection is established, i.e: TLS/SSL
964 * handshaking has been done successfully.
965 */
966 pj_bool_t established;
967
968 /**
969 * Describes secure socket protocol being used, see #pj_ssl_sock_proto.
970 * Use bitwise OR operation to combine the protocol type.
971 */
972 pj_uint32_t proto;
973
974 /**
975 * Describes cipher suite being used, this will only be set when connection
976 * is established.
977 */
978 pj_ssl_cipher cipher;
979
980 /**
981 * Describes local address.
982 */
983 pj_sockaddr local_addr;
984
985 /**
986 * Describes remote address.
987 */
988 pj_sockaddr remote_addr;
989
990 /**
991 * Describes active local certificate info.
992 */
993 pj_ssl_cert_info *local_cert_info;
994
995 /**
996 * Describes active remote certificate info.
997 */
998 pj_ssl_cert_info *remote_cert_info;
999
1000 /**
1001 * Status of peer certificate verification.
1002 */
1003 pj_uint32_t verify_status;
1004
1005 /**
1006 * Last native error returned by the backend.
1007 */
1008 unsigned long last_native_err;
1009
1010 /**
1011 * Group lock assigned to the ioqueue key.
1012 */
1013 pj_grp_lock_t *grp_lock;
1014
1015 /**
1016 * Native TLS/SSL instance of the backend. Currently only available for
1017 * OpenSSL backend (this will contain the OpenSSL "SSL" instance).
1018 */
1019 void *native_ssl;
1020
1021 } pj_ssl_sock_info;
1022
1023
1024 /**
1025 * Definition of secure socket creation parameters.
1026 */
1027 typedef struct pj_ssl_sock_param
1028 {
1029 /**
1030 * Optional group lock to be assigned to the ioqueue key.
1031 *
1032 * Note that when a secure socket listener is configured with a group
1033 * lock, any new secure socket of an accepted incoming connection
1034 * will have its own group lock created automatically by the library,
1035 * this group lock can be queried via pj_ssl_sock_get_info() in the info
1036 * field pj_ssl_sock_info::grp_lock.
1037 */
1038 pj_grp_lock_t *grp_lock;
1039
1040 /**
1041 * Specifies socket address family, either pj_AF_INET() and pj_AF_INET6().
1042 *
1043 * Default is pj_AF_INET().
1044 */
1045 int sock_af;
1046
1047 /**
1048 * Specify socket type, either pj_SOCK_DGRAM() or pj_SOCK_STREAM().
1049 *
1050 * Default is pj_SOCK_STREAM().
1051 */
1052 int sock_type;
1053
1054 /**
1055 * Specify the ioqueue to use. Secure socket uses the ioqueue to perform
1056 * active socket operations, see \ref PJ_ACTIVESOCK for more detail.
1057 */
1058 pj_ioqueue_t *ioqueue;
1059
1060 /**
1061 * Specify the timer heap to use. Secure socket uses the timer to provide
1062 * auto cancelation on asynchronous operation when it takes longer time
1063 * than specified timeout period, e.g: security negotiation timeout.
1064 */
1065 pj_timer_heap_t *timer_heap;
1066
1067 /**
1068 * Specify secure socket callbacks, see #pj_ssl_sock_cb.
1069 */
1070 pj_ssl_sock_cb cb;
1071
1072 /**
1073 * Specify secure socket user data.
1074 */
1075 void *user_data;
1076
1077 /**
1078 * Specify security protocol to use, see #pj_ssl_sock_proto. Use bitwise OR
1079 * operation to combine the protocol type.
1080 *
1081 * Default is PJ_SSL_SOCK_PROTO_DEFAULT.
1082 */
1083 pj_uint32_t proto;
1084
1085 /**
1086 * Number of concurrent asynchronous operations that is to be supported
1087 * by the secure socket. This value only affects socket receive and
1088 * accept operations -- the secure socket will issue one or more
1089 * asynchronous read and accept operations based on the value of this
1090 * field. Setting this field to more than one will allow more than one
1091 * incoming data or incoming connections to be processed simultaneously
1092 * on multiprocessor systems, when the ioqueue is polled by more than
1093 * one threads.
1094 *
1095 * The default value is 1.
1096 */
1097 unsigned async_cnt;
1098
1099 /**
1100 * The ioqueue concurrency to be forced on the socket when it is
1101 * registered to the ioqueue. See #pj_ioqueue_set_concurrency() for more
1102 * info about ioqueue concurrency.
1103 *
1104 * When this value is -1, the concurrency setting will not be forced for
1105 * this socket, and the socket will inherit the concurrency setting of
1106 * the ioqueue. When this value is zero, the secure socket will disable
1107 * concurrency for the socket. When this value is +1, the secure socket
1108 * will enable concurrency for the socket.
1109 *
1110 * The default value is -1.
1111 */
1112 int concurrency;
1113
1114 /**
1115 * If this option is specified, the secure socket will make sure that
1116 * asynchronous send operation with stream oriented socket will only
1117 * call the callback after all data has been sent. This means that the
1118 * secure socket will automatically resend the remaining data until
1119 * all data has been sent.
1120 *
1121 * Please note that when this option is specified, it is possible that
1122 * error is reported after partial data has been sent. Also setting
1123 * this will disable the ioqueue concurrency for the socket.
1124 *
1125 * Default value is 1.
1126 */
1127 pj_bool_t whole_data;
1128
1129 /**
1130 * Specify buffer size for sending operation. Buffering sending data
1131 * is used for allowing application to perform multiple outstanding
1132 * send operations. Whenever application specifies this setting too
1133 * small, sending operation may return PJ_ENOMEM.
1134 *
1135 * Default value is 8192 bytes.
1136 */
1137 pj_size_t send_buffer_size;
1138
1139 /**
1140 * Specify buffer size for receiving encrypted (and perhaps compressed)
1141 * data on underlying socket. This setting is unused on Symbian, since
1142 * SSL/TLS Symbian backend, CSecureSocket, can use application buffer
1143 * directly.
1144 *
1145 * Default value is 1500.
1146 */
1147 pj_size_t read_buffer_size;
1148
1149 /**
1150 * Number of ciphers contained in the specified cipher preference.
1151 * If this is set to zero, then the cipher list used will be determined
1152 * by the backend default (for OpenSSL backend, setting
1153 * PJ_SSL_SOCK_OSSL_CIPHERS will be used).
1154 */
1155 unsigned ciphers_num;
1156
1157 /**
1158 * Ciphers and order preference. If empty, then default cipher list and
1159 * its default order of the backend will be used.
1160 */
1161 pj_ssl_cipher *ciphers;
1162
1163 /**
1164 * Number of curves contained in the specified curve preference.
1165 * If this is set to zero, then default curve list of the backend
1166 * will be used.
1167 *
1168 * Default: 0 (zero).
1169 */
1170 unsigned curves_num;
1171
1172 /**
1173 * Curves and order preference. The #pj_ssl_curve_get_availables()
1174 * can be used to check the available curves supported by backend.
1175 */
1176 pj_ssl_curve *curves;
1177
1178 /**
1179 * The supported signature algorithms. Set the sigalgs string
1180 * using this form:
1181 * "<DIGEST>+<ALGORITHM>:<DIGEST>+<ALGORITHM>"
1182 * Digests are: "RSA", "DSA" or "ECDSA"
1183 * Algorithms are: "MD5", "SHA1", "SHA224", "SHA256", "SHA384", "SHA512"
1184 * Example: "ECDSA+SHA256:RSA+SHA256"
1185 */
1186 pj_str_t sigalgs;
1187
1188 /**
1189 * Reseed random number generator.
1190 * For type #PJ_SSL_ENTROPY_FILE, parameter \a entropy_path
1191 * must be set to a file.
1192 * For type #PJ_SSL_ENTROPY_EGD, parameter \a entropy_path
1193 * must be set to a socket.
1194 *
1195 * Default value is PJ_SSL_ENTROPY_NONE.
1196 */
1197 pj_ssl_entropy_t entropy_type;
1198
1199 /**
1200 * When using a file/socket for entropy #PJ_SSL_ENTROPY_EGD or
1201 * #PJ_SSL_ENTROPY_FILE, \a entropy_path must contain the path
1202 * to entropy socket/file.
1203 *
1204 * Default value is an empty string.
1205 */
1206 pj_str_t entropy_path;
1207
1208 /**
1209 * Security negotiation timeout. If this is set to zero (both sec and
1210 * msec), the negotiation doesn't have a timeout.
1211 *
1212 * Default value is zero.
1213 */
1214 pj_time_val timeout;
1215
1216 /**
1217 * Specify whether endpoint should verify peer certificate.
1218 *
1219 * Default value is PJ_FALSE.
1220 */
1221 pj_bool_t verify_peer;
1222
1223 /**
1224 * When secure socket is acting as server (handles incoming connection),
1225 * it will require the client to provide certificate.
1226 *
1227 * Default value is PJ_FALSE.
1228 */
1229 pj_bool_t require_client_cert;
1230
1231 /**
1232 * Server name indication. When secure socket is acting as client
1233 * (perform outgoing connection) and the server may host multiple
1234 * 'virtual' servers at a single underlying network address, setting
1235 * this will allow client to tell the server a name of the server
1236 * it is contacting. This must be set to hostname and literal IP addresses
1237 * are not allowed.
1238 *
1239 * Default value is zero/not-set.
1240 */
1241 pj_str_t server_name;
1242
1243 /**
1244 * Specify if SO_REUSEADDR should be used for listening socket. This
1245 * option will only be used with accept() operation.
1246 *
1247 * Default is PJ_FALSE.
1248 */
1249 pj_bool_t reuse_addr;
1250
1251 /**
1252 * QoS traffic type to be set on this transport. When application wants
1253 * to apply QoS tagging to the transport, it's preferable to set this
1254 * field rather than \a qos_param fields since this is more portable.
1255 *
1256 * Default value is PJ_QOS_TYPE_BEST_EFFORT.
1257 */
1258 pj_qos_type qos_type;
1259
1260 /**
1261 * Set the low level QoS parameters to the transport. This is a lower
1262 * level operation than setting the \a qos_type field and may not be
1263 * supported on all platforms.
1264 *
1265 * By default all settings in this structure are disabled.
1266 */
1267 pj_qos_params qos_params;
1268
1269 /**
1270 * Specify if the transport should ignore any errors when setting the QoS
1271 * traffic type/parameters.
1272 *
1273 * Default: PJ_TRUE
1274 */
1275 pj_bool_t qos_ignore_error;
1276
1277 /**
1278 * Specify options to be set on the transport.
1279 *
1280 * For GnuTLS backend, by default TCP_NODELAY will be set. For other
1281 * SSL backends, the default is no options.
1282 *
1283 */
1284 pj_sockopt_params sockopt_params;
1285
1286 /**
1287 * Specify if the transport should ignore any errors when setting the
1288 * sockopt parameters.
1289 *
1290 * Default: PJ_TRUE
1291 *
1292 */
1293 pj_bool_t sockopt_ignore_error;
1294
1295 /**
1296 * Specify if should set close-on-exec flag for socket.
1297 *
1298 * Default: PJ_TRUE
1299 */
1300 pj_bool_t sock_cloexec;
1301
1302 /**
1303 * Specify if renegotiation is enabled for TLSv1.2 or earlier.
1304 *
1305 * Default: PJ_TRUE
1306 */
1307 pj_bool_t enable_renegotiation;
1308
1309 } pj_ssl_sock_param;
1310
1311
1312 /**
1313 * The parameter for pj_ssl_sock_start_connect2().
1314 */
1315 typedef struct pj_ssl_start_connect_param {
1316 /**
1317 * The pool to allocate some internal data for the operation.
1318 */
1319 pj_pool_t *pool;
1320
1321 /**
1322 * Local address.
1323 */
1324 const pj_sockaddr_t *localaddr;
1325
1326 /**
1327 * Port range for socket binding, relative to the start port number
1328 * specified in \a localaddr. This is only applicable when the start port
1329 * number is non zero.
1330 */
1331 pj_uint16_t local_port_range;
1332
1333 /**
1334 * Remote address.
1335 */
1336 const pj_sockaddr_t *remaddr;
1337
1338 /**
1339 * Length of buffer containing above addresses.
1340 */
1341 int addr_len;
1342
1343 } pj_ssl_start_connect_param;
1344
1345
1346 /**
1347 * Initialize the secure socket parameters for its creation with
1348 * the default values.
1349 *
1350 * @param param The parameter to be initialized.
1351 */
1352 PJ_DECL(void) pj_ssl_sock_param_default(pj_ssl_sock_param *param);
1353
1354
1355 /**
1356 * Duplicate pj_ssl_sock_param.
1357 *
1358 * @param pool Pool to allocate memory.
1359 * @param dst Destination parameter.
1360 * @param src Source parameter.
1361 */
1362 PJ_DECL(void) pj_ssl_sock_param_copy(pj_pool_t *pool,
1363 pj_ssl_sock_param *dst,
1364 const pj_ssl_sock_param *src);
1365
1366
1367 /**
1368 * Create secure socket instance.
1369 *
1370 * @param pool The pool for allocating secure socket instance.
1371 * @param param The secure socket parameter, see #pj_ssl_sock_param.
1372 * @param p_ssock Pointer to secure socket instance to be created.
1373 *
1374 * @return PJ_SUCCESS when successful.
1375 */
1376 PJ_DECL(pj_status_t) pj_ssl_sock_create(pj_pool_t *pool,
1377 const pj_ssl_sock_param *param,
1378 pj_ssl_sock_t **p_ssock);
1379
1380
1381 /**
1382 * Set secure socket certificate or credentials. Credentials may include
1383 * certificate, private key and trusted Certification Authorities list.
1384 * Normally, server socket must provide certificate (and private key).
1385 * Socket client may also need to provide certificate in case requested
1386 * by the server.
1387 *
1388 * @param ssock The secure socket instance.
1389 * @param pool The pool.
1390 * @param cert The endpoint certificate/credentials, see
1391 * #pj_ssl_cert_t.
1392 *
1393 * @return PJ_SUCCESS if the operation has been successful,
1394 * or the appropriate error code on failure.
1395 */
1396 PJ_DECL(pj_status_t) pj_ssl_sock_set_certificate(
1397 pj_ssl_sock_t *ssock,
1398 pj_pool_t *pool,
1399 const pj_ssl_cert_t *cert);
1400
1401
1402 /**
1403 * Close and destroy the secure socket.
1404 *
1405 * @param ssock The secure socket.
1406 *
1407 * @return PJ_SUCCESS if the operation has been successful,
1408 * or the appropriate error code on failure.
1409 */
1410 PJ_DECL(pj_status_t) pj_ssl_sock_close(pj_ssl_sock_t *ssock);
1411
1412
1413 /**
1414 * Associate arbitrary data with the secure socket. Application may
1415 * inspect this data in the callbacks and associate it with higher
1416 * level processing.
1417 *
1418 * @param ssock The secure socket.
1419 * @param user_data The user data to be associated with the secure
1420 * socket.
1421 *
1422 * @return PJ_SUCCESS if the operation has been successful,
1423 * or the appropriate error code on failure.
1424 */
1425 PJ_DECL(pj_status_t) pj_ssl_sock_set_user_data(pj_ssl_sock_t *ssock,
1426 void *user_data);
1427
1428 /**
1429 * Retrieve the user data previously associated with this secure
1430 * socket.
1431 *
1432 * @param ssock The secure socket.
1433 *
1434 * @return The user data.
1435 */
1436 PJ_DECL(void*) pj_ssl_sock_get_user_data(pj_ssl_sock_t *ssock);
1437
1438
1439 /**
1440 * Retrieve the local address and port used by specified secure socket.
1441 *
1442 * @param ssock The secure socket.
1443 * @param info The info buffer to be set, see #pj_ssl_sock_info.
1444 *
1445 * @return PJ_SUCCESS on successful.
1446 */
1447 PJ_DECL(pj_status_t) pj_ssl_sock_get_info(pj_ssl_sock_t *ssock,
1448 pj_ssl_sock_info *info);
1449
1450
1451 /**
1452 * Starts read operation on this secure socket. This function will create
1453 * \a async_cnt number of buffers (the \a async_cnt parameter was given
1454 * in \a pj_ssl_sock_create() function) where each buffer is \a buff_size
1455 * long. The buffers are allocated from the specified \a pool. Once the
1456 * buffers are created, it then issues \a async_cnt number of asynchronous
1457 * \a recv() operations to the socket and returns back to caller. Incoming
1458 * data on the socket will be reported back to application via the
1459 * \a on_data_read() callback.
1460 *
1461 * Application only needs to call this function once to initiate read
1462 * operations. Further read operations will be done automatically by the
1463 * secure socket when \a on_data_read() callback returns non-zero.
1464 *
1465 * @param ssock The secure socket.
1466 * @param pool Pool used to allocate buffers for incoming data.
1467 * @param buff_size The size of each buffer, in bytes.
1468 * @param flags Flags to be given to pj_ioqueue_recv().
1469 *
1470 * @return PJ_SUCCESS if the operation has been successful,
1471 * or the appropriate error code on failure.
1472 */
1473 PJ_DECL(pj_status_t) pj_ssl_sock_start_read(pj_ssl_sock_t *ssock,
1474 pj_pool_t *pool,
1475 unsigned buff_size,
1476 pj_uint32_t flags);
1477
1478 /**
1479 * Same as #pj_ssl_sock_start_read(), except that the application
1480 * supplies the buffers for the read operation so that the acive socket
1481 * does not have to allocate the buffers.
1482 *
1483 * @param ssock The secure socket.
1484 * @param pool Pool used to allocate buffers for incoming data.
1485 * @param buff_size The size of each buffer, in bytes.
1486 * @param readbuf Array of packet buffers, each has buff_size size.
1487 * @param flags Flags to be given to pj_ioqueue_recv().
1488 *
1489 * @return PJ_SUCCESS if the operation has been successful,
1490 * or the appropriate error code on failure.
1491 */
1492 PJ_DECL(pj_status_t) pj_ssl_sock_start_read2(pj_ssl_sock_t *ssock,
1493 pj_pool_t *pool,
1494 unsigned buff_size,
1495 void *readbuf[],
1496 pj_uint32_t flags);
1497
1498 /**
1499 * Same as pj_ssl_sock_start_read(), except that this function is used
1500 * only for datagram sockets, and it will trigger \a on_data_recvfrom()
1501 * callback instead.
1502 *
1503 * @param ssock The secure socket.
1504 * @param pool Pool used to allocate buffers for incoming data.
1505 * @param buff_size The size of each buffer, in bytes.
1506 * @param flags Flags to be given to pj_ioqueue_recvfrom().
1507 *
1508 * @return PJ_SUCCESS if the operation has been successful,
1509 * or the appropriate error code on failure.
1510 */
1511 PJ_DECL(pj_status_t) pj_ssl_sock_start_recvfrom(pj_ssl_sock_t *ssock,
1512 pj_pool_t *pool,
1513 unsigned buff_size,
1514 pj_uint32_t flags);
1515
1516 /**
1517 * Same as #pj_ssl_sock_start_recvfrom() except that the recvfrom()
1518 * operation takes the buffer from the argument rather than creating
1519 * new ones.
1520 *
1521 * @param ssock The secure socket.
1522 * @param pool Pool used to allocate buffers for incoming data.
1523 * @param buff_size The size of each buffer, in bytes.
1524 * @param readbuf Array of packet buffers, each has buff_size size.
1525 * @param flags Flags to be given to pj_ioqueue_recvfrom().
1526 *
1527 * @return PJ_SUCCESS if the operation has been successful,
1528 * or the appropriate error code on failure.
1529 */
1530 PJ_DECL(pj_status_t) pj_ssl_sock_start_recvfrom2(pj_ssl_sock_t *ssock,
1531 pj_pool_t *pool,
1532 unsigned buff_size,
1533 void *readbuf[],
1534 pj_uint32_t flags);
1535
1536 /**
1537 * Send data using the socket.
1538 *
1539 * @param ssock The secure socket.
1540 * @param send_key The operation key to send the data, which is useful
1541 * if application wants to submit multiple pending
1542 * send operations and want to track which exact data
1543 * has been sent in the \a on_data_sent() callback.
1544 * @param data The data to be sent. This data must remain valid
1545 * until the data has been sent.
1546 * @param size The size of the data.
1547 * @param flags Flags to be given to pj_ioqueue_send().
1548 *
1549 * @return PJ_SUCCESS if data has been sent immediately, or
1550 * PJ_EPENDING if data cannot be sent immediately or
1551 * PJ_ENOMEM when sending buffer could not handle all
1552 * queued data, see \a send_buffer_size. The callback
1553 * \a on_data_sent() will be called when data is actually
1554 * sent. Any other return value indicates error condition.
1555 */
1556 PJ_DECL(pj_status_t) pj_ssl_sock_send(pj_ssl_sock_t *ssock,
1557 pj_ioqueue_op_key_t *send_key,
1558 const void *data,
1559 pj_ssize_t *size,
1560 unsigned flags);
1561
1562 /**
1563 * Send datagram using the socket.
1564 *
1565 * @param ssock The secure socket.
1566 * @param send_key The operation key to send the data, which is useful
1567 * if application wants to submit multiple pending
1568 * send operations and want to track which exact data
1569 * has been sent in the \a on_data_sent() callback.
1570 * @param data The data to be sent. This data must remain valid
1571 * until the data has been sent.
1572 * @param size The size of the data.
1573 * @param flags Flags to be given to pj_ioqueue_send().
1574 * @param addr The destination address.
1575 * @param addr_len Length of buffer containing destination address.
1576 *
1577 * @return PJ_SUCCESS if data has been sent immediately, or
1578 * PJ_EPENDING if data cannot be sent immediately. In
1579 * this case the \a on_data_sent() callback will be
1580 * called when data is actually sent. Any other return
1581 * value indicates error condition.
1582 */
1583 PJ_DECL(pj_status_t) pj_ssl_sock_sendto(pj_ssl_sock_t *ssock,
1584 pj_ioqueue_op_key_t *send_key,
1585 const void *data,
1586 pj_ssize_t *size,
1587 unsigned flags,
1588 const pj_sockaddr_t *addr,
1589 int addr_len);
1590
1591
1592 /**
1593 * Starts asynchronous socket accept() operations on this secure socket.
1594 * This function will issue \a async_cnt number of asynchronous \a accept()
1595 * operations to the socket and returns back to caller. Incoming
1596 * connection on the socket will be reported back to application via the
1597 * \a on_accept_complete() callback.
1598 *
1599 * Application only needs to call this function once to initiate accept()
1600 * operations. Further accept() operations will be done automatically by
1601 * the secure socket when \a on_accept_complete() callback returns non-zero.
1602 *
1603 * @param ssock The secure socket.
1604 * @param pool Pool used to allocate some internal data for the
1605 * operation.
1606 * @param local_addr Local address to bind on.
1607 * @param addr_len Length of buffer containing local address.
1608 *
1609 * @return PJ_SUCCESS if the operation has been successful,
1610 * or the appropriate error code on failure.
1611 */
1612 PJ_DECL(pj_status_t) pj_ssl_sock_start_accept(pj_ssl_sock_t *ssock,
1613 pj_pool_t *pool,
1614 const pj_sockaddr_t *local_addr,
1615 int addr_len);
1616
1617
1618 /**
1619 * Same as #pj_ssl_sock_start_accept(), but application can provide
1620 * a secure socket parameter, which will be used to create a new secure
1621 * socket reported in \a on_accept_complete() callback when there is
1622 * an incoming connection.
1623 *
1624 * @param ssock The secure socket.
1625 * @param pool Pool used to allocate some internal data for the
1626 * operation.
1627 * @param local_addr Local address to bind on.
1628 * @param addr_len Length of buffer containing local address.
1629 * @param newsock_param Secure socket parameter for new accepted sockets.
1630 *
1631 * @return PJ_SUCCESS if the operation has been successful,
1632 * or the appropriate error code on failure.
1633 */
1634 PJ_DECL(pj_status_t)
1635 pj_ssl_sock_start_accept2(pj_ssl_sock_t *ssock,
1636 pj_pool_t *pool,
1637 const pj_sockaddr_t *local_addr,
1638 int addr_len,
1639 const pj_ssl_sock_param *newsock_param);
1640
1641
1642 /**
1643 * Starts asynchronous socket connect() operation and SSL/TLS handshaking
1644 * for this socket. Once the connection is done (either successfully or not),
1645 * the \a on_connect_complete() callback will be called.
1646 *
1647 * @param ssock The secure socket.
1648 * @param pool The pool to allocate some internal data for the
1649 * operation.
1650 * @param localaddr Local address.
1651 * @param remaddr Remote address.
1652 * @param addr_len Length of buffer containing above addresses.
1653 *
1654 * @return PJ_SUCCESS if connection can be established immediately
1655 * or PJ_EPENDING if connection cannot be established
1656 * immediately. In this case the \a on_connect_complete()
1657 * callback will be called when connection is complete.
1658 * Any other return value indicates error condition.
1659 */
1660 PJ_DECL(pj_status_t) pj_ssl_sock_start_connect(pj_ssl_sock_t *ssock,
1661 pj_pool_t *pool,
1662 const pj_sockaddr_t *localaddr,
1663 const pj_sockaddr_t *remaddr,
1664 int addr_len);
1665
1666 /**
1667 * Same as #pj_ssl_sock_start_connect(), but application can provide a
1668 * \a port_range parameter, which will be used to bind the socket to
1669 * random port.
1670 *
1671 * @param ssock The secure socket.
1672 *
1673 * @param connect_param The parameter, refer to \a pj_ssl_start_connect_param.
1674 *
1675 * @return PJ_SUCCESS if connection can be established immediately
1676 * or PJ_EPENDING if connection cannot be established
1677 * immediately. In this case the \a on_connect_complete()
1678 * callback will be called when connection is complete.
1679 * Any other return value indicates error condition.
1680 */
1681 PJ_DECL(pj_status_t) pj_ssl_sock_start_connect2(
1682 pj_ssl_sock_t *ssock,
1683 pj_ssl_start_connect_param *connect_param);
1684
1685 /**
1686 * Starts SSL/TLS renegotiation over an already established SSL connection
1687 * for this socket. This operation is performed transparently, no callback
1688 * will be called once the renegotiation completed successfully. However,
1689 * when the renegotiation fails, the connection will be closed and callback
1690 * \a on_data_read() will be invoked with non-PJ_SUCCESS status code.
1691 *
1692 * @param ssock The secure socket.
1693 *
1694 * @return PJ_SUCCESS if renegotiation is completed immediately,
1695 * or PJ_EPENDING if renegotiation has been started and
1696 * waiting for completion, or the appropriate error code
1697 * on failure.
1698 */
1699 PJ_DECL(pj_status_t) pj_ssl_sock_renegotiate(pj_ssl_sock_t *ssock);
1700
1701 /**
1702 * @}
1703 */
1704
1705 PJ_END_DECL
1706
1707 #endif /* __PJ_SSL_SOCK_H__ */