blob: f9fc69b0eeafc2f8de394810e99d436346c1b7af [file] [log] [blame]
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +00001/* $Id$ */
2/*
3 * Copyright (C) 2009 Teluu Inc. (http://www.teluu.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#ifndef __PJ_SSL_SOCK_H__
20#define __PJ_SSL_SOCK_H__
21
22/**
23 * @file ssl_sock.h
24 * @brief Secure socket
25 */
26
27#include <pj/ioqueue.h>
28#include <pj/sock.h>
Benny Prijonoa25bc9d2009-11-09 08:51:34 +000029#include <pj/sock_qos.h>
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +000030
31
32PJ_BEGIN_DECL
33
34/**
35 * @defgroup PJ_SSL_SOCK Secure socket I/O
36 * @brief Secure socket provides security on socket operation using standard
37 * security protocols such as SSL and TLS.
38 * @ingroup PJ_IO
39 * @{
40 *
41 * Secure socket wraps normal socket and applies security features, i.e:
42 * privacy and data integrity, on the socket traffic, using standard security
43 * protocols such as SSL and TLS.
44 *
45 * Secure socket employs active socket operations, which is similar to (and
46 * described more detail) in \ref PJ_ACTIVESOCK.
47 */
48
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +000049
Nanang Izzuddin006cc012009-10-16 03:06:13 +000050 /**
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +000051 * This opaque structure describes the secure socket.
52 */
53typedef struct pj_ssl_sock_t pj_ssl_sock_t;
54
Nanang Izzuddin006cc012009-10-16 03:06:13 +000055
56/**
57 * Opaque declaration of endpoint certificate or credentials. This may contains
58 * certificate, private key, and trusted Certificate Authorities list.
59 */
60typedef struct pj_ssl_cert_t pj_ssl_cert_t;
61
62
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +000063typedef enum pj_ssl_cert_verify_flag_t
64{
65 /**
66 * No error in verification.
67 */
68 PJ_SSL_CERT_ESUCCESS = 0,
69
70 /**
71 * The issuer certificate cannot be found.
72 */
73 PJ_SSL_CERT_EISSUER_NOT_FOUND = (1 << 0),
74
75 /**
76 * The certificate is untrusted.
77 */
78 PJ_SSL_CERT_EUNTRUSTED = (1 << 1),
79
80 /**
81 * The certificate has expired or not yet valid.
82 */
83 PJ_SSL_CERT_EVALIDITY_PERIOD = (1 << 2),
84
85 /**
86 * One or more fields of the certificate cannot be decoded due to
87 * invalid format.
88 */
89 PJ_SSL_CERT_EINVALID_FORMAT = (1 << 3),
90
91 /**
92 * The certificate cannot be used for the specified purpose.
93 */
94 PJ_SSL_CERT_EINVALID_PURPOSE = (1 << 4),
95
96 /**
97 * The issuer info in the certificate does not match to the (candidate)
98 * issuer certificate, e.g: issuer name not match to subject name
99 * of (candidate) issuer certificate.
100 */
101 PJ_SSL_CERT_EISSUER_MISMATCH = (1 << 5),
102
103 /**
104 * The CRL certificate cannot be found or cannot be read properly.
105 */
106 PJ_SSL_CERT_ECRL_FAILURE = (1 << 6),
107
108 /**
109 * The certificate has been revoked.
110 */
111 PJ_SSL_CERT_EREVOKED = (1 << 7),
112
113 /**
114 * The certificate chain length is too long.
115 */
116 PJ_SSL_CERT_ECHAIN_TOO_LONG = (1 << 8),
117
118 /**
119 * The server identity does not match to any identities specified in
120 * the certificate, e.g: subjectAltName extension, subject common name.
121 * This flag will only be set by application as SSL socket does not
122 * perform server identity verification.
123 */
124 PJ_SSL_CERT_EIDENTITY_NOT_MATCH = (1 << 30),
125
126 /**
127 * Unknown verification error.
128 */
129 PJ_SSL_CERT_EUNKNOWN = (1 << 31)
130
131} pj_ssl_cert_verify_flag_t;
132
133
134typedef enum pj_ssl_cert_name_type
135{
136 PJ_SSL_CERT_NAME_UNKNOWN = 0,
137 PJ_SSL_CERT_NAME_RFC822,
138 PJ_SSL_CERT_NAME_DNS,
139 PJ_SSL_CERT_NAME_URI,
140 PJ_SSL_CERT_NAME_IP
141} pj_ssl_cert_name_type;
142
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000143/**
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000144 * Describe structure of certificate info.
145 */
146typedef struct pj_ssl_cert_info {
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +0000147
148 unsigned version; /**< Certificate version */
149
150 pj_uint8_t serial_no[20]; /**< Serial number, array of
151 octets, first index is
152 MSB */
153
154 struct {
155 pj_str_t cn; /**< Common name */
156 pj_str_t info; /**< One line subject, fields
157 are separated by slash */
158 } subject; /**< Subject */
159
160 struct {
161 pj_str_t cn; /**< Common name */
162 pj_str_t info; /**< One line subject, fields
163 are separated by slash.*/
164 } issuer; /**< Issuer */
165
166 struct {
167 pj_time_val start; /**< Validity start */
168 pj_time_val end; /**< Validity end */
169 pj_bool_t gmt; /**< Flag if validity date/time
170 use GMT */
171 } validity; /**< Validity */
172
173 struct {
174 unsigned cnt; /**< # of entry */
175 struct {
176 pj_ssl_cert_name_type type;
177 /**< Name type */
178 pj_str_t name; /**< The name */
179 } *entry; /**< Subject alt name entry */
180 } subj_alt_name; /**< Subject alternative
181 name extension */
182
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000183} pj_ssl_cert_info;
184
185
186/**
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000187 * Create credential from files.
188 *
189 * @param CA_file The file of trusted CA list.
190 * @param cert_file The file of certificate.
191 * @param privkey_file The file of private key.
192 * @param privkey_pass The password of private key, if any.
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000193 * @param p_cert Pointer to credential instance to be created.
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000194 *
195 * @return PJ_SUCCESS when successful.
196 */
197PJ_DECL(pj_status_t) pj_ssl_cert_load_from_files(pj_pool_t *pool,
198 const pj_str_t *CA_file,
199 const pj_str_t *cert_file,
200 const pj_str_t *privkey_file,
201 const pj_str_t *privkey_pass,
202 pj_ssl_cert_t **p_cert);
203
204
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +0000205/**
206 * Dump SSL certificate info.
207 *
208 * @param ci The certificate info.
209 * @param prefix Prefix string for each line.
210 * @param buf The buffer where certificate info will be printed on.
211 * @param buf_size The buffer size.
212 *
213 * @return PJ_SUCCESS when successful.
214 */
215PJ_DECL(pj_status_t) pj_ssl_cert_info_dump(const pj_ssl_cert_info *ci,
216 const char *prefix,
217 char *buf,
218 pj_size_t buf_size);
219
220
221/**
222 * Get SSL certificate verification error messages from verification status.
223 *
224 * @param verify_status The SSL certificate verification status.
225 * @param error_strings Array of strings to receive the verification error
226 * messages.
227 * @param count On input it specifies maximum error messages should be
228 * retrieved. On output it specifies the number of error
229 * messages retrieved.
230 *
231 * @return PJ_SUCCESS when successful.
232 */
233PJ_DECL(pj_status_t) pj_ssl_cert_verify_error_st(pj_uint32_t verify_status,
234 const char *error_strings[],
235 unsigned *count);
236
237
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000238/**
239 * Cipher suites enumeration.
240 */
241typedef enum pj_ssl_cipher {
242
243 /* NULL */
244 TLS_NULL_WITH_NULL_NULL = 0x00000000,
245
246 /* TLS/SSLv3 */
247 TLS_RSA_WITH_NULL_MD5 = 0x00000001,
248 TLS_RSA_WITH_NULL_SHA = 0x00000002,
249 TLS_RSA_WITH_NULL_SHA256 = 0x0000003B,
250 TLS_RSA_WITH_RC4_128_MD5 = 0x00000004,
251 TLS_RSA_WITH_RC4_128_SHA = 0x00000005,
252 TLS_RSA_WITH_3DES_EDE_CBC_SHA = 0x0000000A,
253 TLS_RSA_WITH_AES_128_CBC_SHA = 0x0000002F,
254 TLS_RSA_WITH_AES_256_CBC_SHA = 0x00000035,
255 TLS_RSA_WITH_AES_128_CBC_SHA256 = 0x0000003C,
256 TLS_RSA_WITH_AES_256_CBC_SHA256 = 0x0000003D,
257 TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA = 0x0000000D,
258 TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA = 0x00000010,
259 TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA = 0x00000013,
260 TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA = 0x00000016,
261 TLS_DH_DSS_WITH_AES_128_CBC_SHA = 0x00000030,
262 TLS_DH_RSA_WITH_AES_128_CBC_SHA = 0x00000031,
263 TLS_DHE_DSS_WITH_AES_128_CBC_SHA = 0x00000032,
264 TLS_DHE_RSA_WITH_AES_128_CBC_SHA = 0x00000033,
265 TLS_DH_DSS_WITH_AES_256_CBC_SHA = 0x00000036,
266 TLS_DH_RSA_WITH_AES_256_CBC_SHA = 0x00000037,
267 TLS_DHE_DSS_WITH_AES_256_CBC_SHA = 0x00000038,
268 TLS_DHE_RSA_WITH_AES_256_CBC_SHA = 0x00000039,
269 TLS_DH_DSS_WITH_AES_128_CBC_SHA256 = 0x0000003E,
270 TLS_DH_RSA_WITH_AES_128_CBC_SHA256 = 0x0000003F,
271 TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 = 0x00000040,
272 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 = 0x00000067,
273 TLS_DH_DSS_WITH_AES_256_CBC_SHA256 = 0x00000068,
274 TLS_DH_RSA_WITH_AES_256_CBC_SHA256 = 0x00000069,
275 TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 = 0x0000006A,
276 TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 = 0x0000006B,
277 TLS_DH_anon_WITH_RC4_128_MD5 = 0x00000018,
278 TLS_DH_anon_WITH_3DES_EDE_CBC_SHA = 0x0000001B,
279 TLS_DH_anon_WITH_AES_128_CBC_SHA = 0x00000034,
280 TLS_DH_anon_WITH_AES_256_CBC_SHA = 0x0000003A,
281 TLS_DH_anon_WITH_AES_128_CBC_SHA256 = 0x0000006C,
282 TLS_DH_anon_WITH_AES_256_CBC_SHA256 = 0x0000006D,
283
284 /* TLS (deprecated) */
285 TLS_RSA_EXPORT_WITH_RC4_40_MD5 = 0x00000003,
286 TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5 = 0x00000006,
287 TLS_RSA_WITH_IDEA_CBC_SHA = 0x00000007,
288 TLS_RSA_EXPORT_WITH_DES40_CBC_SHA = 0x00000008,
289 TLS_RSA_WITH_DES_CBC_SHA = 0x00000009,
290 TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA = 0x0000000B,
291 TLS_DH_DSS_WITH_DES_CBC_SHA = 0x0000000C,
292 TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA = 0x0000000E,
293 TLS_DH_RSA_WITH_DES_CBC_SHA = 0x0000000F,
294 TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA = 0x00000011,
295 TLS_DHE_DSS_WITH_DES_CBC_SHA = 0x00000012,
296 TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA = 0x00000014,
297 TLS_DHE_RSA_WITH_DES_CBC_SHA = 0x00000015,
298 TLS_DH_anon_EXPORT_WITH_RC4_40_MD5 = 0x00000017,
299 TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA = 0x00000019,
300 TLS_DH_anon_WITH_DES_CBC_SHA = 0x0000001A,
301
302 /* SSLv3 */
303 SSL_FORTEZZA_KEA_WITH_NULL_SHA = 0x0000001C,
304 SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA= 0x0000001D,
305 SSL_FORTEZZA_KEA_WITH_RC4_128_SHA = 0x0000001E,
306
307 /* SSLv2 */
308 SSL_CK_RC4_128_WITH_MD5 = 0x00010080,
309 SSL_CK_RC4_128_EXPORT40_WITH_MD5 = 0x00020080,
310 SSL_CK_RC2_128_CBC_WITH_MD5 = 0x00030080,
311 SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5 = 0x00040080,
312 SSL_CK_IDEA_128_CBC_WITH_MD5 = 0x00050080,
313 SSL_CK_DES_64_CBC_WITH_MD5 = 0x00060040,
314 SSL_CK_DES_192_EDE3_CBC_WITH_MD5 = 0x000700C0
315
316} pj_ssl_cipher;
317
318
319/**
320 * Get cipher list supported by SSL/TLS backend.
321 *
322 * @param ciphers The ciphers buffer to receive cipher list.
323 * @param cipher_num Maximum number of ciphers to be received.
324 *
325 * @return PJ_SUCCESS when successful.
326 */
327PJ_DECL(pj_status_t) pj_ssl_cipher_get_availables(pj_ssl_cipher ciphers[],
328 unsigned *cipher_num);
329
330
331/**
332 * Get cipher name string.
333 *
334 * @param cipher The cipher.
335 *
336 * @return The cipher name or NULL if cipher is not recognized.
337 */
338PJ_DECL(const char*) pj_ssl_cipher_name(pj_ssl_cipher cipher);
339
340
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000341/**
342 * This structure contains the callbacks to be called by the secure socket.
343 */
344typedef struct pj_ssl_sock_cb
345{
346 /**
347 * This callback is called when a data arrives as the result of
348 * pj_ssl_sock_start_read().
349 *
350 * @param ssock The secure socket.
351 * @param data The buffer containing the new data, if any. If
352 * the status argument is non-PJ_SUCCESS, this
353 * argument may be NULL.
354 * @param size The length of data in the buffer.
355 * @param status The status of the read operation. This may contain
356 * non-PJ_SUCCESS for example when the TCP connection
357 * has been closed. In this case, the buffer may
358 * contain left over data from previous callback which
359 * the application may want to process.
360 * @param remainder If application wishes to leave some data in the
361 * buffer (common for TCP applications), it should
362 * move the remainder data to the front part of the
363 * buffer and set the remainder length here. The value
364 * of this parameter will be ignored for datagram
365 * sockets.
366 *
367 * @return PJ_TRUE if further read is desired, and PJ_FALSE
368 * when application no longer wants to receive data.
369 * Application may destroy the secure socket in the
370 * callback and return PJ_FALSE here.
371 */
372 pj_bool_t (*on_data_read)(pj_ssl_sock_t *ssock,
373 void *data,
374 pj_size_t size,
375 pj_status_t status,
376 pj_size_t *remainder);
377 /**
378 * This callback is called when a packet arrives as the result of
379 * pj_ssl_sock_start_recvfrom().
380 *
381 * @param ssock The secure socket.
382 * @param data The buffer containing the packet, if any. If
383 * the status argument is non-PJ_SUCCESS, this
384 * argument will be set to NULL.
385 * @param size The length of packet in the buffer. If
386 * the status argument is non-PJ_SUCCESS, this
387 * argument will be set to zero.
388 * @param src_addr Source address of the packet.
389 * @param addr_len Length of the source address.
390 * @param status This contains
391 *
392 * @return PJ_TRUE if further read is desired, and PJ_FALSE
393 * when application no longer wants to receive data.
394 * Application may destroy the secure socket in the
395 * callback and return PJ_FALSE here.
396 */
397 pj_bool_t (*on_data_recvfrom)(pj_ssl_sock_t *ssock,
398 void *data,
399 pj_size_t size,
400 const pj_sockaddr_t *src_addr,
401 int addr_len,
402 pj_status_t status);
403
404 /**
405 * This callback is called when data has been sent.
406 *
407 * @param ssock The secure socket.
408 * @param send_key Key associated with the send operation.
409 * @param sent If value is positive non-zero it indicates the
410 * number of data sent. When the value is negative,
411 * it contains the error code which can be retrieved
412 * by negating the value (i.e. status=-sent).
413 *
414 * @return Application may destroy the secure socket in the
415 * callback and return PJ_FALSE here.
416 */
417 pj_bool_t (*on_data_sent)(pj_ssl_sock_t *ssock,
418 pj_ioqueue_op_key_t *send_key,
419 pj_ssize_t sent);
420
421 /**
422 * This callback is called when new connection arrives as the result
423 * of pj_ssl_sock_start_accept().
424 *
425 * @param ssock The secure socket.
426 * @param newsock The new incoming secure socket.
427 * @param src_addr The source address of the connection.
428 * @param addr_len Length of the source address.
429 *
430 * @return PJ_TRUE if further accept() is desired, and PJ_FALSE
431 * when application no longer wants to accept incoming
432 * connection. Application may destroy the secure socket
433 * in the callback and return PJ_FALSE here.
434 */
435 pj_bool_t (*on_accept_complete)(pj_ssl_sock_t *ssock,
436 pj_ssl_sock_t *newsock,
437 const pj_sockaddr_t *src_addr,
438 int src_addr_len);
439
440 /**
441 * This callback is called when pending connect operation has been
442 * completed.
443 *
444 * @param ssock The secure socket.
445 * @param status The connection result. If connection has been
446 * successfully established, the status will contain
447 * PJ_SUCCESS.
448 *
449 * @return Application may destroy the secure socket in the
450 * callback and return PJ_FALSE here.
451 */
452 pj_bool_t (*on_connect_complete)(pj_ssl_sock_t *ssock,
453 pj_status_t status);
454
455} pj_ssl_sock_cb;
456
457
458/**
459 * Enumeration of secure socket protocol types.
460 */
461typedef enum pj_ssl_sock_proto
462{
463 PJ_SSL_SOCK_PROTO_DEFAULT, /**< Default protocol of backend. */
464 PJ_SSL_SOCK_PROTO_TLS1, /**< TLSv1.0 protocol. */
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000465 PJ_SSL_SOCK_PROTO_SSL3, /**< SSLv3.0 protocol. */
466 PJ_SSL_SOCK_PROTO_SSL23, /**< SSLv3.0 but can roll back to
467 SSLv2.0. */
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000468 PJ_SSL_SOCK_PROTO_SSL2, /**< SSLv2.0 protocol. */
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000469 PJ_SSL_SOCK_PROTO_DTLS1 /**< DTLSv1.0 protocol. */
470} pj_ssl_sock_proto;
471
472
473/**
474 * Definition of secure socket info structure.
475 */
476typedef struct pj_ssl_sock_info
477{
478 /**
479 * Describes whether secure socket connection is established, i.e: TLS/SSL
480 * handshaking has been done successfully.
481 */
482 pj_bool_t established;
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000483
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000484 /**
485 * Describes secure socket protocol being used.
486 */
487 pj_ssl_sock_proto proto;
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000488
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000489 /**
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000490 * Describes cipher suite being used, this will only be set when connection
491 * is established.
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000492 */
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000493 pj_ssl_cipher cipher;
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000494
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000495 /**
496 * Describes local address.
497 */
498 pj_sockaddr local_addr;
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000499
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000500 /**
501 * Describes remote address.
502 */
503 pj_sockaddr remote_addr;
504
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000505 /**
506 * Describes active local certificate info.
507 */
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +0000508 pj_ssl_cert_info *local_cert_info;
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000509
510 /**
511 * Describes active remote certificate info.
512 */
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +0000513 pj_ssl_cert_info *remote_cert_info;
514
515 /**
516 * Status of peer certificate verification.
517 */
518 pj_uint32_t verify_status;
519
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000520} pj_ssl_sock_info;
521
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000522
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000523/**
524 * Definition of secure socket creation parameters.
525 */
526typedef struct pj_ssl_sock_param
527{
528 /**
529 * Specifies socket address family, either pj_AF_INET() and pj_AF_INET6().
530 *
531 * Default is pj_AF_INET().
532 */
533 int sock_af;
534
535 /**
536 * Specify socket type, either pj_SOCK_DGRAM() or pj_SOCK_STREAM().
537 *
538 * Default is pj_SOCK_STREAM().
539 */
540 int sock_type;
541
542 /**
543 * Specify the ioqueue to use. Secure socket uses the ioqueue to perform
544 * active socket operations, see \ref PJ_ACTIVESOCK for more detail.
545 */
546 pj_ioqueue_t *ioqueue;
547
548 /**
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000549 * Specify the timer heap to use. Secure socket uses the timer to provide
550 * auto cancelation on asynchronous operation when it takes longer time
551 * than specified timeout period, e.g: security negotiation timeout.
552 */
553 pj_timer_heap_t *timer_heap;
554
555 /**
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000556 * Specify secure socket callbacks, see #pj_ssl_sock_cb.
557 */
558 pj_ssl_sock_cb cb;
559
560 /**
561 * Specify secure socket user data.
562 */
563 void *user_data;
564
565 /**
566 * Specify security protocol to use, see #pj_ssl_sock_proto.
567 *
568 * Default is PJ_SSL_SOCK_PROTO_DEFAULT.
569 */
570 pj_ssl_sock_proto proto;
571
572 /**
573 * Number of concurrent asynchronous operations that is to be supported
574 * by the secure socket. This value only affects socket receive and
575 * accept operations -- the secure socket will issue one or more
576 * asynchronous read and accept operations based on the value of this
577 * field. Setting this field to more than one will allow more than one
578 * incoming data or incoming connections to be processed simultaneously
579 * on multiprocessor systems, when the ioqueue is polled by more than
580 * one threads.
581 *
582 * The default value is 1.
583 */
584 unsigned async_cnt;
585
586 /**
587 * The ioqueue concurrency to be forced on the socket when it is
588 * registered to the ioqueue. See #pj_ioqueue_set_concurrency() for more
589 * info about ioqueue concurrency.
590 *
591 * When this value is -1, the concurrency setting will not be forced for
592 * this socket, and the socket will inherit the concurrency setting of
593 * the ioqueue. When this value is zero, the secure socket will disable
594 * concurrency for the socket. When this value is +1, the secure socket
595 * will enable concurrency for the socket.
596 *
597 * The default value is -1.
598 */
599 int concurrency;
600
601 /**
602 * If this option is specified, the secure socket will make sure that
603 * asynchronous send operation with stream oriented socket will only
604 * call the callback after all data has been sent. This means that the
605 * secure socket will automatically resend the remaining data until
606 * all data has been sent.
607 *
608 * Please note that when this option is specified, it is possible that
609 * error is reported after partial data has been sent. Also setting
610 * this will disable the ioqueue concurrency for the socket.
611 *
612 * Default value is 1.
613 */
614 pj_bool_t whole_data;
615
616 /**
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000617 * Specify buffer size for sending operation. Buffering sending data
618 * is used for allowing application to perform multiple outstanding
619 * send operations. Whenever application specifies this setting too
620 * small, sending operation may return PJ_ENOMEM.
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000621 *
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000622 * Default value is 8192 bytes.
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000623 */
624 pj_size_t send_buffer_size;
625
626 /**
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000627 * Specify buffer size for receiving encrypted (and perhaps compressed)
628 * data on underlying socket. This setting is unused on Symbian, since
629 * SSL/TLS Symbian backend, CSecureSocket, can use application buffer
630 * directly.
631 *
632 * Default value is 1500.
633 */
634 pj_size_t read_buffer_size;
635
636 /**
637 * Number of ciphers contained in the specified cipher preference.
638 * If this is set to zero, then default cipher list of the backend
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000639 * will be used.
640 */
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000641 unsigned ciphers_num;
642
643 /**
644 * Ciphers and order preference. If empty, then default cipher list and
645 * its default order of the backend will be used.
646 */
647 pj_ssl_cipher *ciphers;
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000648
649 /**
650 * Security negotiation timeout. If this is set to zero (both sec and
651 * msec), the negotiation doesn't have a timeout.
652 *
653 * Default value is zero.
654 */
655 pj_time_val timeout;
656
657 /**
658 * Specify whether endpoint should verify peer certificate.
659 *
660 * Default value is PJ_FALSE.
661 */
662 pj_bool_t verify_peer;
663
664 /**
665 * When secure socket is acting as server (handles incoming connection),
666 * it will require the client to provide certificate.
667 *
668 * Default value is PJ_FALSE.
669 */
670 pj_bool_t require_client_cert;
671
672 /**
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +0000673 * Server name indication. When secure socket is acting as client
674 * (perform outgoing connection) and the server may host multiple
675 * 'virtual' servers at a single underlying network address, setting
676 * this will allow client to tell the server a name of the server
677 * it is contacting.
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000678 *
679 * Default value is zero/not-set.
680 */
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000681 pj_str_t server_name;
Benny Prijonoa25bc9d2009-11-09 08:51:34 +0000682
683 /**
684 * QoS traffic type to be set on this transport. When application wants
685 * to apply QoS tagging to the transport, it's preferable to set this
686 * field rather than \a qos_param fields since this is more portable.
687 *
688 * Default value is PJ_QOS_TYPE_BEST_EFFORT.
689 */
690 pj_qos_type qos_type;
691
692 /**
693 * Set the low level QoS parameters to the transport. This is a lower
694 * level operation than setting the \a qos_type field and may not be
695 * supported on all platforms.
696 *
697 * By default all settings in this structure are disabled.
698 */
699 pj_qos_params qos_params;
700
701 /**
702 * Specify if the transport should ignore any errors when setting the QoS
703 * traffic type/parameters.
704 *
705 * Default: PJ_TRUE
706 */
707 pj_bool_t qos_ignore_error;
708
709
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000710} pj_ssl_sock_param;
711
712
713/**
714 * Initialize the secure socket parameters for its creation with
715 * the default values.
716 *
717 * @param param The parameter to be initialized.
718 */
719PJ_DECL(void) pj_ssl_sock_param_default(pj_ssl_sock_param *param);
720
721
722/**
723 * Create secure socket instance.
724 *
725 * @param pool The pool for allocating secure socket instance.
726 * @param param The secure socket parameter, see #pj_ssl_sock_param.
727 * @param p_ssock Pointer to secure socket instance to be created.
728 *
729 * @return PJ_SUCCESS when successful.
730 */
731PJ_DECL(pj_status_t) pj_ssl_sock_create(pj_pool_t *pool,
732 const pj_ssl_sock_param *param,
733 pj_ssl_sock_t **p_ssock);
734
735
736/**
737 * Set secure socket certificate or credentials. Credentials may include
738 * certificate, private key and trusted Certification Authorities list.
739 * Normally, server socket must provide certificate (and private key).
740 * Socket client may also need to provide certificate in case requested
741 * by the server.
742 *
743 * @param ssock The secure socket instance.
744 * @param pool The pool.
745 * @param cert The endpoint certificate/credentials, see
746 * #pj_ssl_cert_t.
747 *
748 * @return PJ_SUCCESS if the operation has been successful,
749 * or the appropriate error code on failure.
750 */
751PJ_DECL(pj_status_t) pj_ssl_sock_set_certificate(
752 pj_ssl_sock_t *ssock,
753 pj_pool_t *pool,
754 const pj_ssl_cert_t *cert);
755
756
757/**
758 * Close and destroy the secure socket.
759 *
760 * @param ssock The secure socket.
761 *
762 * @return PJ_SUCCESS if the operation has been successful,
763 * or the appropriate error code on failure.
764 */
765PJ_DECL(pj_status_t) pj_ssl_sock_close(pj_ssl_sock_t *ssock);
766
767
768/**
769 * Associate arbitrary data with the secure socket. Application may
770 * inspect this data in the callbacks and associate it with higher
771 * level processing.
772 *
773 * @param ssock The secure socket.
774 * @param user_data The user data to be associated with the secure
775 * socket.
776 *
777 * @return PJ_SUCCESS if the operation has been successful,
778 * or the appropriate error code on failure.
779 */
780PJ_DECL(pj_status_t) pj_ssl_sock_set_user_data(pj_ssl_sock_t *ssock,
781 void *user_data);
782
783/**
784 * Retrieve the user data previously associated with this secure
785 * socket.
786 *
787 * @param ssock The secure socket.
788 *
789 * @return The user data.
790 */
791PJ_DECL(void*) pj_ssl_sock_get_user_data(pj_ssl_sock_t *ssock);
792
793
794/**
795 * Retrieve the local address and port used by specified secure socket.
796 *
797 * @param ssock The secure socket.
798 * @param info The info buffer to be set, see #pj_ssl_sock_info.
799 *
800 * @return PJ_SUCCESS on successful.
801 */
802PJ_DECL(pj_status_t) pj_ssl_sock_get_info(pj_ssl_sock_t *ssock,
803 pj_ssl_sock_info *info);
804
805
806/**
807 * Starts read operation on this secure socket. This function will create
808 * \a async_cnt number of buffers (the \a async_cnt parameter was given
809 * in \a pj_ssl_sock_create() function) where each buffer is \a buff_size
810 * long. The buffers are allocated from the specified \a pool. Once the
811 * buffers are created, it then issues \a async_cnt number of asynchronous
812 * \a recv() operations to the socket and returns back to caller. Incoming
813 * data on the socket will be reported back to application via the
814 * \a on_data_read() callback.
815 *
816 * Application only needs to call this function once to initiate read
817 * operations. Further read operations will be done automatically by the
818 * secure socket when \a on_data_read() callback returns non-zero.
819 *
820 * @param ssock The secure socket.
821 * @param pool Pool used to allocate buffers for incoming data.
822 * @param buff_size The size of each buffer, in bytes.
823 * @param flags Flags to be given to pj_ioqueue_recv().
824 *
825 * @return PJ_SUCCESS if the operation has been successful,
826 * or the appropriate error code on failure.
827 */
828PJ_DECL(pj_status_t) pj_ssl_sock_start_read(pj_ssl_sock_t *ssock,
829 pj_pool_t *pool,
830 unsigned buff_size,
831 pj_uint32_t flags);
832
833/**
834 * Same as #pj_ssl_sock_start_read(), except that the application
835 * supplies the buffers for the read operation so that the acive socket
836 * does not have to allocate the buffers.
837 *
838 * @param ssock The secure socket.
839 * @param pool Pool used to allocate buffers for incoming data.
840 * @param buff_size The size of each buffer, in bytes.
841 * @param readbuf Array of packet buffers, each has buff_size size.
842 * @param flags Flags to be given to pj_ioqueue_recv().
843 *
844 * @return PJ_SUCCESS if the operation has been successful,
845 * or the appropriate error code on failure.
846 */
847PJ_DECL(pj_status_t) pj_ssl_sock_start_read2(pj_ssl_sock_t *ssock,
848 pj_pool_t *pool,
849 unsigned buff_size,
850 void *readbuf[],
851 pj_uint32_t flags);
852
853/**
854 * Same as pj_ssl_sock_start_read(), except that this function is used
855 * only for datagram sockets, and it will trigger \a on_data_recvfrom()
856 * callback instead.
857 *
858 * @param ssock The secure socket.
859 * @param pool Pool used to allocate buffers for incoming data.
860 * @param buff_size The size of each buffer, in bytes.
861 * @param flags Flags to be given to pj_ioqueue_recvfrom().
862 *
863 * @return PJ_SUCCESS if the operation has been successful,
864 * or the appropriate error code on failure.
865 */
866PJ_DECL(pj_status_t) pj_ssl_sock_start_recvfrom(pj_ssl_sock_t *ssock,
867 pj_pool_t *pool,
868 unsigned buff_size,
869 pj_uint32_t flags);
870
871/**
872 * Same as #pj_ssl_sock_start_recvfrom() except that the recvfrom()
873 * operation takes the buffer from the argument rather than creating
874 * new ones.
875 *
876 * @param ssock The secure socket.
877 * @param pool Pool used to allocate buffers for incoming data.
878 * @param buff_size The size of each buffer, in bytes.
879 * @param readbuf Array of packet buffers, each has buff_size size.
880 * @param flags Flags to be given to pj_ioqueue_recvfrom().
881 *
882 * @return PJ_SUCCESS if the operation has been successful,
883 * or the appropriate error code on failure.
884 */
885PJ_DECL(pj_status_t) pj_ssl_sock_start_recvfrom2(pj_ssl_sock_t *ssock,
886 pj_pool_t *pool,
887 unsigned buff_size,
888 void *readbuf[],
889 pj_uint32_t flags);
890
891/**
892 * Send data using the socket.
893 *
894 * @param ssock The secure socket.
895 * @param send_key The operation key to send the data, which is useful
896 * if application wants to submit multiple pending
897 * send operations and want to track which exact data
898 * has been sent in the \a on_data_sent() callback.
899 * @param data The data to be sent. This data must remain valid
900 * until the data has been sent.
901 * @param size The size of the data.
902 * @param flags Flags to be given to pj_ioqueue_send().
903 *
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000904 * @return PJ_SUCCESS if data has been sent immediately, or
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000905 * PJ_EPENDING if data cannot be sent immediately or
906 * PJ_ENOMEM when sending buffer could not handle all
907 * queued data, see \a send_buffer_size. The callback
908 * \a on_data_sent() will be called when data is actually
909 * sent. Any other return value indicates error condition.
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000910 */
911PJ_DECL(pj_status_t) pj_ssl_sock_send(pj_ssl_sock_t *ssock,
912 pj_ioqueue_op_key_t *send_key,
913 const void *data,
914 pj_ssize_t *size,
915 unsigned flags);
916
917/**
918 * Send datagram using the socket.
919 *
920 * @param ssock The secure socket.
921 * @param send_key The operation key to send the data, which is useful
922 * if application wants to submit multiple pending
923 * send operations and want to track which exact data
924 * has been sent in the \a on_data_sent() callback.
925 * @param data The data to be sent. This data must remain valid
926 * until the data has been sent.
927 * @param size The size of the data.
928 * @param flags Flags to be given to pj_ioqueue_send().
929 * @param addr The destination address.
930 * @param addr_len Length of buffer containing destination address.
931 *
932 * @return PJ_SUCCESS if data has been sent immediately, or
933 * PJ_EPENDING if data cannot be sent immediately. In
934 * this case the \a on_data_sent() callback will be
935 * called when data is actually sent. Any other return
936 * value indicates error condition.
937 */
938PJ_DECL(pj_status_t) pj_ssl_sock_sendto(pj_ssl_sock_t *ssock,
939 pj_ioqueue_op_key_t *send_key,
940 const void *data,
941 pj_ssize_t *size,
942 unsigned flags,
943 const pj_sockaddr_t *addr,
944 int addr_len);
945
946
947/**
948 * Starts asynchronous socket accept() operations on this secure socket.
949 * This function will issue \a async_cnt number of asynchronous \a accept()
950 * operations to the socket and returns back to caller. Incoming
951 * connection on the socket will be reported back to application via the
952 * \a on_accept_complete() callback.
953 *
954 * Application only needs to call this function once to initiate accept()
955 * operations. Further accept() operations will be done automatically by
956 * the secure socket when \a on_accept_complete() callback returns non-zero.
957 *
958 * @param ssock The secure socket.
959 * @param pool Pool used to allocate some internal data for the
960 * operation.
961 * @param localaddr Local address to bind on.
962 * @param addr_len Length of buffer containing local address.
963 *
964 * @return PJ_SUCCESS if the operation has been successful,
965 * or the appropriate error code on failure.
966 */
967PJ_DECL(pj_status_t) pj_ssl_sock_start_accept(pj_ssl_sock_t *ssock,
968 pj_pool_t *pool,
969 const pj_sockaddr_t *local_addr,
970 int addr_len);
971
972
973/**
974 * Starts asynchronous socket connect() operation and SSL/TLS handshaking
975 * for this socket. Once the connection is done (either successfully or not),
976 * the \a on_connect_complete() callback will be called.
977 *
978 * @param ssock The secure socket.
979 * @param pool The pool to allocate some internal data for the
980 * operation.
981 * @param localaddr Local address.
982 * @param remaddr Remote address.
983 * @param addr_len Length of buffer containing above addresses.
984 *
985 * @return PJ_SUCCESS if connection can be established immediately
986 * or PJ_EPENDING if connection cannot be established
987 * immediately. In this case the \a on_connect_complete()
988 * callback will be called when connection is complete.
989 * Any other return value indicates error condition.
990 */
991PJ_DECL(pj_status_t) pj_ssl_sock_start_connect(pj_ssl_sock_t *ssock,
992 pj_pool_t *pool,
993 const pj_sockaddr_t *localaddr,
994 const pj_sockaddr_t *remaddr,
995 int addr_len);
996
997
998/**
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000999 * Starts SSL/TLS renegotiation over an already established SSL connection
1000 * for this socket. This operation is performed transparently, no callback
1001 * will be called once the renegotiation completed successfully. However,
1002 * when the renegotiation fails, the connection will be closed and callback
1003 * \a on_data_read() will be invoked with non-PJ_SUCCESS status code.
1004 *
1005 * @param ssock The secure socket.
1006 *
1007 * @return PJ_SUCCESS if renegotiation is completed immediately,
1008 * or PJ_EPENDING if renegotiation has been started and
1009 * waiting for completion, or the appropriate error code
1010 * on failure.
1011 */
1012PJ_DECL(pj_status_t) pj_ssl_sock_renegotiate(pj_ssl_sock_t *ssock);
1013
1014
1015/**
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +00001016 * @}
1017 */
1018
1019PJ_END_DECL
1020
1021#endif /* __PJ_SSL_SOCK_H__ */