blob: ad4556e4302fc89876b20df8fca7305ace46a4a1 [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>
29
30
31PJ_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
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +000048
Nanang Izzuddin006cc012009-10-16 03:06:13 +000049 /**
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +000050 * This opaque structure describes the secure socket.
51 */
52typedef struct pj_ssl_sock_t pj_ssl_sock_t;
53
Nanang Izzuddin006cc012009-10-16 03:06:13 +000054
55/**
56 * Opaque declaration of endpoint certificate or credentials. This may contains
57 * certificate, private key, and trusted Certificate Authorities list.
58 */
59typedef struct pj_ssl_cert_t pj_ssl_cert_t;
60
61
62/**
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +000063 * Describe structure of certificate info.
64 */
65typedef struct pj_ssl_cert_info {
66 pj_str_t subject; /**< Subject. */
67 pj_str_t issuer; /**< Issuer. */
68 unsigned version; /**< Certificate version. */
69 pj_time_val validity_start; /**< Validity start. */
70 pj_time_val validity_end; /**< Validity end. */
71 pj_bool_t validity_use_gmt; /**< Flag if validity date/time
72 use GMT. */
73} pj_ssl_cert_info;
74
75
76/**
Nanang Izzuddin006cc012009-10-16 03:06:13 +000077 * Create credential from files.
78 *
79 * @param CA_file The file of trusted CA list.
80 * @param cert_file The file of certificate.
81 * @param privkey_file The file of private key.
82 * @param privkey_pass The password of private key, if any.
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +000083 * @param p_cert Pointer to credential instance to be created.
Nanang Izzuddin006cc012009-10-16 03:06:13 +000084 *
85 * @return PJ_SUCCESS when successful.
86 */
87PJ_DECL(pj_status_t) pj_ssl_cert_load_from_files(pj_pool_t *pool,
88 const pj_str_t *CA_file,
89 const pj_str_t *cert_file,
90 const pj_str_t *privkey_file,
91 const pj_str_t *privkey_pass,
92 pj_ssl_cert_t **p_cert);
93
94
95/**
96 * Cipher suites enumeration.
97 */
98typedef enum pj_ssl_cipher {
99
100 /* NULL */
101 TLS_NULL_WITH_NULL_NULL = 0x00000000,
102
103 /* TLS/SSLv3 */
104 TLS_RSA_WITH_NULL_MD5 = 0x00000001,
105 TLS_RSA_WITH_NULL_SHA = 0x00000002,
106 TLS_RSA_WITH_NULL_SHA256 = 0x0000003B,
107 TLS_RSA_WITH_RC4_128_MD5 = 0x00000004,
108 TLS_RSA_WITH_RC4_128_SHA = 0x00000005,
109 TLS_RSA_WITH_3DES_EDE_CBC_SHA = 0x0000000A,
110 TLS_RSA_WITH_AES_128_CBC_SHA = 0x0000002F,
111 TLS_RSA_WITH_AES_256_CBC_SHA = 0x00000035,
112 TLS_RSA_WITH_AES_128_CBC_SHA256 = 0x0000003C,
113 TLS_RSA_WITH_AES_256_CBC_SHA256 = 0x0000003D,
114 TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA = 0x0000000D,
115 TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA = 0x00000010,
116 TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA = 0x00000013,
117 TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA = 0x00000016,
118 TLS_DH_DSS_WITH_AES_128_CBC_SHA = 0x00000030,
119 TLS_DH_RSA_WITH_AES_128_CBC_SHA = 0x00000031,
120 TLS_DHE_DSS_WITH_AES_128_CBC_SHA = 0x00000032,
121 TLS_DHE_RSA_WITH_AES_128_CBC_SHA = 0x00000033,
122 TLS_DH_DSS_WITH_AES_256_CBC_SHA = 0x00000036,
123 TLS_DH_RSA_WITH_AES_256_CBC_SHA = 0x00000037,
124 TLS_DHE_DSS_WITH_AES_256_CBC_SHA = 0x00000038,
125 TLS_DHE_RSA_WITH_AES_256_CBC_SHA = 0x00000039,
126 TLS_DH_DSS_WITH_AES_128_CBC_SHA256 = 0x0000003E,
127 TLS_DH_RSA_WITH_AES_128_CBC_SHA256 = 0x0000003F,
128 TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 = 0x00000040,
129 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 = 0x00000067,
130 TLS_DH_DSS_WITH_AES_256_CBC_SHA256 = 0x00000068,
131 TLS_DH_RSA_WITH_AES_256_CBC_SHA256 = 0x00000069,
132 TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 = 0x0000006A,
133 TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 = 0x0000006B,
134 TLS_DH_anon_WITH_RC4_128_MD5 = 0x00000018,
135 TLS_DH_anon_WITH_3DES_EDE_CBC_SHA = 0x0000001B,
136 TLS_DH_anon_WITH_AES_128_CBC_SHA = 0x00000034,
137 TLS_DH_anon_WITH_AES_256_CBC_SHA = 0x0000003A,
138 TLS_DH_anon_WITH_AES_128_CBC_SHA256 = 0x0000006C,
139 TLS_DH_anon_WITH_AES_256_CBC_SHA256 = 0x0000006D,
140
141 /* TLS (deprecated) */
142 TLS_RSA_EXPORT_WITH_RC4_40_MD5 = 0x00000003,
143 TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5 = 0x00000006,
144 TLS_RSA_WITH_IDEA_CBC_SHA = 0x00000007,
145 TLS_RSA_EXPORT_WITH_DES40_CBC_SHA = 0x00000008,
146 TLS_RSA_WITH_DES_CBC_SHA = 0x00000009,
147 TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA = 0x0000000B,
148 TLS_DH_DSS_WITH_DES_CBC_SHA = 0x0000000C,
149 TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA = 0x0000000E,
150 TLS_DH_RSA_WITH_DES_CBC_SHA = 0x0000000F,
151 TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA = 0x00000011,
152 TLS_DHE_DSS_WITH_DES_CBC_SHA = 0x00000012,
153 TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA = 0x00000014,
154 TLS_DHE_RSA_WITH_DES_CBC_SHA = 0x00000015,
155 TLS_DH_anon_EXPORT_WITH_RC4_40_MD5 = 0x00000017,
156 TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA = 0x00000019,
157 TLS_DH_anon_WITH_DES_CBC_SHA = 0x0000001A,
158
159 /* SSLv3 */
160 SSL_FORTEZZA_KEA_WITH_NULL_SHA = 0x0000001C,
161 SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA= 0x0000001D,
162 SSL_FORTEZZA_KEA_WITH_RC4_128_SHA = 0x0000001E,
163
164 /* SSLv2 */
165 SSL_CK_RC4_128_WITH_MD5 = 0x00010080,
166 SSL_CK_RC4_128_EXPORT40_WITH_MD5 = 0x00020080,
167 SSL_CK_RC2_128_CBC_WITH_MD5 = 0x00030080,
168 SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5 = 0x00040080,
169 SSL_CK_IDEA_128_CBC_WITH_MD5 = 0x00050080,
170 SSL_CK_DES_64_CBC_WITH_MD5 = 0x00060040,
171 SSL_CK_DES_192_EDE3_CBC_WITH_MD5 = 0x000700C0
172
173} pj_ssl_cipher;
174
175
176/**
177 * Get cipher list supported by SSL/TLS backend.
178 *
179 * @param ciphers The ciphers buffer to receive cipher list.
180 * @param cipher_num Maximum number of ciphers to be received.
181 *
182 * @return PJ_SUCCESS when successful.
183 */
184PJ_DECL(pj_status_t) pj_ssl_cipher_get_availables(pj_ssl_cipher ciphers[],
185 unsigned *cipher_num);
186
187
188/**
189 * Get cipher name string.
190 *
191 * @param cipher The cipher.
192 *
193 * @return The cipher name or NULL if cipher is not recognized.
194 */
195PJ_DECL(const char*) pj_ssl_cipher_name(pj_ssl_cipher cipher);
196
197
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000198/**
199 * This structure contains the callbacks to be called by the secure socket.
200 */
201typedef struct pj_ssl_sock_cb
202{
203 /**
204 * This callback is called when a data arrives as the result of
205 * pj_ssl_sock_start_read().
206 *
207 * @param ssock The secure socket.
208 * @param data The buffer containing the new data, if any. If
209 * the status argument is non-PJ_SUCCESS, this
210 * argument may be NULL.
211 * @param size The length of data in the buffer.
212 * @param status The status of the read operation. This may contain
213 * non-PJ_SUCCESS for example when the TCP connection
214 * has been closed. In this case, the buffer may
215 * contain left over data from previous callback which
216 * the application may want to process.
217 * @param remainder If application wishes to leave some data in the
218 * buffer (common for TCP applications), it should
219 * move the remainder data to the front part of the
220 * buffer and set the remainder length here. The value
221 * of this parameter will be ignored for datagram
222 * sockets.
223 *
224 * @return PJ_TRUE if further read is desired, and PJ_FALSE
225 * when application no longer wants to receive data.
226 * Application may destroy the secure socket in the
227 * callback and return PJ_FALSE here.
228 */
229 pj_bool_t (*on_data_read)(pj_ssl_sock_t *ssock,
230 void *data,
231 pj_size_t size,
232 pj_status_t status,
233 pj_size_t *remainder);
234 /**
235 * This callback is called when a packet arrives as the result of
236 * pj_ssl_sock_start_recvfrom().
237 *
238 * @param ssock The secure socket.
239 * @param data The buffer containing the packet, if any. If
240 * the status argument is non-PJ_SUCCESS, this
241 * argument will be set to NULL.
242 * @param size The length of packet in the buffer. If
243 * the status argument is non-PJ_SUCCESS, this
244 * argument will be set to zero.
245 * @param src_addr Source address of the packet.
246 * @param addr_len Length of the source address.
247 * @param status This contains
248 *
249 * @return PJ_TRUE if further read is desired, and PJ_FALSE
250 * when application no longer wants to receive data.
251 * Application may destroy the secure socket in the
252 * callback and return PJ_FALSE here.
253 */
254 pj_bool_t (*on_data_recvfrom)(pj_ssl_sock_t *ssock,
255 void *data,
256 pj_size_t size,
257 const pj_sockaddr_t *src_addr,
258 int addr_len,
259 pj_status_t status);
260
261 /**
262 * This callback is called when data has been sent.
263 *
264 * @param ssock The secure socket.
265 * @param send_key Key associated with the send operation.
266 * @param sent If value is positive non-zero it indicates the
267 * number of data sent. When the value is negative,
268 * it contains the error code which can be retrieved
269 * by negating the value (i.e. status=-sent).
270 *
271 * @return Application may destroy the secure socket in the
272 * callback and return PJ_FALSE here.
273 */
274 pj_bool_t (*on_data_sent)(pj_ssl_sock_t *ssock,
275 pj_ioqueue_op_key_t *send_key,
276 pj_ssize_t sent);
277
278 /**
279 * This callback is called when new connection arrives as the result
280 * of pj_ssl_sock_start_accept().
281 *
282 * @param ssock The secure socket.
283 * @param newsock The new incoming secure socket.
284 * @param src_addr The source address of the connection.
285 * @param addr_len Length of the source address.
286 *
287 * @return PJ_TRUE if further accept() is desired, and PJ_FALSE
288 * when application no longer wants to accept incoming
289 * connection. Application may destroy the secure socket
290 * in the callback and return PJ_FALSE here.
291 */
292 pj_bool_t (*on_accept_complete)(pj_ssl_sock_t *ssock,
293 pj_ssl_sock_t *newsock,
294 const pj_sockaddr_t *src_addr,
295 int src_addr_len);
296
297 /**
298 * This callback is called when pending connect operation has been
299 * completed.
300 *
301 * @param ssock The secure socket.
302 * @param status The connection result. If connection has been
303 * successfully established, the status will contain
304 * PJ_SUCCESS.
305 *
306 * @return Application may destroy the secure socket in the
307 * callback and return PJ_FALSE here.
308 */
309 pj_bool_t (*on_connect_complete)(pj_ssl_sock_t *ssock,
310 pj_status_t status);
311
312} pj_ssl_sock_cb;
313
314
315/**
316 * Enumeration of secure socket protocol types.
317 */
318typedef enum pj_ssl_sock_proto
319{
320 PJ_SSL_SOCK_PROTO_DEFAULT, /**< Default protocol of backend. */
321 PJ_SSL_SOCK_PROTO_TLS1, /**< TLSv1.0 protocol. */
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000322 PJ_SSL_SOCK_PROTO_SSL3, /**< SSLv3.0 protocol. */
323 PJ_SSL_SOCK_PROTO_SSL23, /**< SSLv3.0 but can roll back to
324 SSLv2.0. */
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000325 PJ_SSL_SOCK_PROTO_SSL2, /**< SSLv2.0 protocol. */
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000326 PJ_SSL_SOCK_PROTO_DTLS1 /**< DTLSv1.0 protocol. */
327} pj_ssl_sock_proto;
328
329
330/**
331 * Definition of secure socket info structure.
332 */
333typedef struct pj_ssl_sock_info
334{
335 /**
336 * Describes whether secure socket connection is established, i.e: TLS/SSL
337 * handshaking has been done successfully.
338 */
339 pj_bool_t established;
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000340
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000341 /**
342 * Describes secure socket protocol being used.
343 */
344 pj_ssl_sock_proto proto;
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000345
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000346 /**
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000347 * Describes cipher suite being used, this will only be set when connection
348 * is established.
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000349 */
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000350 pj_ssl_cipher cipher;
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000351
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000352 /**
353 * Describes local address.
354 */
355 pj_sockaddr local_addr;
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000356
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000357 /**
358 * Describes remote address.
359 */
360 pj_sockaddr remote_addr;
361
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000362 /**
363 * Describes active local certificate info.
364 */
365 pj_ssl_cert_info local_cert_info;
366
367 /**
368 * Describes active remote certificate info.
369 */
370 pj_ssl_cert_info remote_cert_info;
371
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000372} pj_ssl_sock_info;
373
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000374
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000375/**
376 * Definition of secure socket creation parameters.
377 */
378typedef struct pj_ssl_sock_param
379{
380 /**
381 * Specifies socket address family, either pj_AF_INET() and pj_AF_INET6().
382 *
383 * Default is pj_AF_INET().
384 */
385 int sock_af;
386
387 /**
388 * Specify socket type, either pj_SOCK_DGRAM() or pj_SOCK_STREAM().
389 *
390 * Default is pj_SOCK_STREAM().
391 */
392 int sock_type;
393
394 /**
395 * Specify the ioqueue to use. Secure socket uses the ioqueue to perform
396 * active socket operations, see \ref PJ_ACTIVESOCK for more detail.
397 */
398 pj_ioqueue_t *ioqueue;
399
400 /**
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000401 * Specify the timer heap to use. Secure socket uses the timer to provide
402 * auto cancelation on asynchronous operation when it takes longer time
403 * than specified timeout period, e.g: security negotiation timeout.
404 */
405 pj_timer_heap_t *timer_heap;
406
407 /**
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000408 * Specify secure socket callbacks, see #pj_ssl_sock_cb.
409 */
410 pj_ssl_sock_cb cb;
411
412 /**
413 * Specify secure socket user data.
414 */
415 void *user_data;
416
417 /**
418 * Specify security protocol to use, see #pj_ssl_sock_proto.
419 *
420 * Default is PJ_SSL_SOCK_PROTO_DEFAULT.
421 */
422 pj_ssl_sock_proto proto;
423
424 /**
425 * Number of concurrent asynchronous operations that is to be supported
426 * by the secure socket. This value only affects socket receive and
427 * accept operations -- the secure socket will issue one or more
428 * asynchronous read and accept operations based on the value of this
429 * field. Setting this field to more than one will allow more than one
430 * incoming data or incoming connections to be processed simultaneously
431 * on multiprocessor systems, when the ioqueue is polled by more than
432 * one threads.
433 *
434 * The default value is 1.
435 */
436 unsigned async_cnt;
437
438 /**
439 * The ioqueue concurrency to be forced on the socket when it is
440 * registered to the ioqueue. See #pj_ioqueue_set_concurrency() for more
441 * info about ioqueue concurrency.
442 *
443 * When this value is -1, the concurrency setting will not be forced for
444 * this socket, and the socket will inherit the concurrency setting of
445 * the ioqueue. When this value is zero, the secure socket will disable
446 * concurrency for the socket. When this value is +1, the secure socket
447 * will enable concurrency for the socket.
448 *
449 * The default value is -1.
450 */
451 int concurrency;
452
453 /**
454 * If this option is specified, the secure socket will make sure that
455 * asynchronous send operation with stream oriented socket will only
456 * call the callback after all data has been sent. This means that the
457 * secure socket will automatically resend the remaining data until
458 * all data has been sent.
459 *
460 * Please note that when this option is specified, it is possible that
461 * error is reported after partial data has been sent. Also setting
462 * this will disable the ioqueue concurrency for the socket.
463 *
464 * Default value is 1.
465 */
466 pj_bool_t whole_data;
467
468 /**
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000469 * Specify buffer size for sending operation. Buffering sending data
470 * is used for allowing application to perform multiple outstanding
471 * send operations. Whenever application specifies this setting too
472 * small, sending operation may return PJ_ENOMEM.
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000473 *
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000474 * Default value is 8192 bytes.
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000475 */
476 pj_size_t send_buffer_size;
477
478 /**
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000479 * Specify buffer size for receiving encrypted (and perhaps compressed)
480 * data on underlying socket. This setting is unused on Symbian, since
481 * SSL/TLS Symbian backend, CSecureSocket, can use application buffer
482 * directly.
483 *
484 * Default value is 1500.
485 */
486 pj_size_t read_buffer_size;
487
488 /**
489 * Number of ciphers contained in the specified cipher preference.
490 * If this is set to zero, then default cipher list of the backend
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000491 * will be used.
492 */
Nanang Izzuddin006cc012009-10-16 03:06:13 +0000493 unsigned ciphers_num;
494
495 /**
496 * Ciphers and order preference. If empty, then default cipher list and
497 * its default order of the backend will be used.
498 */
499 pj_ssl_cipher *ciphers;
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000500
501 /**
502 * Security negotiation timeout. If this is set to zero (both sec and
503 * msec), the negotiation doesn't have a timeout.
504 *
505 * Default value is zero.
506 */
507 pj_time_val timeout;
508
509 /**
510 * Specify whether endpoint should verify peer certificate.
511 *
512 * Default value is PJ_FALSE.
513 */
514 pj_bool_t verify_peer;
515
516 /**
517 * When secure socket is acting as server (handles incoming connection),
518 * it will require the client to provide certificate.
519 *
520 * Default value is PJ_FALSE.
521 */
522 pj_bool_t require_client_cert;
523
524 /**
525 * When secure socket is acting as client (perform outgoing connection)
526 * and it needs to verify server name (e.g: host or domain name) by
527 * matching it to the name specified in the server certificate. This
528 * setting is useful when the server is hosting multiple domains for
529 * the same listening socket.
530 *
531 * Default value is zero/not-set.
532 */
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000533 pj_str_t server_name;
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000534
535} pj_ssl_sock_param;
536
537
538/**
539 * Initialize the secure socket parameters for its creation with
540 * the default values.
541 *
542 * @param param The parameter to be initialized.
543 */
544PJ_DECL(void) pj_ssl_sock_param_default(pj_ssl_sock_param *param);
545
546
547/**
548 * Create secure socket instance.
549 *
550 * @param pool The pool for allocating secure socket instance.
551 * @param param The secure socket parameter, see #pj_ssl_sock_param.
552 * @param p_ssock Pointer to secure socket instance to be created.
553 *
554 * @return PJ_SUCCESS when successful.
555 */
556PJ_DECL(pj_status_t) pj_ssl_sock_create(pj_pool_t *pool,
557 const pj_ssl_sock_param *param,
558 pj_ssl_sock_t **p_ssock);
559
560
561/**
562 * Set secure socket certificate or credentials. Credentials may include
563 * certificate, private key and trusted Certification Authorities list.
564 * Normally, server socket must provide certificate (and private key).
565 * Socket client may also need to provide certificate in case requested
566 * by the server.
567 *
568 * @param ssock The secure socket instance.
569 * @param pool The pool.
570 * @param cert The endpoint certificate/credentials, see
571 * #pj_ssl_cert_t.
572 *
573 * @return PJ_SUCCESS if the operation has been successful,
574 * or the appropriate error code on failure.
575 */
576PJ_DECL(pj_status_t) pj_ssl_sock_set_certificate(
577 pj_ssl_sock_t *ssock,
578 pj_pool_t *pool,
579 const pj_ssl_cert_t *cert);
580
581
582/**
583 * Close and destroy the secure socket.
584 *
585 * @param ssock The secure socket.
586 *
587 * @return PJ_SUCCESS if the operation has been successful,
588 * or the appropriate error code on failure.
589 */
590PJ_DECL(pj_status_t) pj_ssl_sock_close(pj_ssl_sock_t *ssock);
591
592
593/**
594 * Associate arbitrary data with the secure socket. Application may
595 * inspect this data in the callbacks and associate it with higher
596 * level processing.
597 *
598 * @param ssock The secure socket.
599 * @param user_data The user data to be associated with the secure
600 * socket.
601 *
602 * @return PJ_SUCCESS if the operation has been successful,
603 * or the appropriate error code on failure.
604 */
605PJ_DECL(pj_status_t) pj_ssl_sock_set_user_data(pj_ssl_sock_t *ssock,
606 void *user_data);
607
608/**
609 * Retrieve the user data previously associated with this secure
610 * socket.
611 *
612 * @param ssock The secure socket.
613 *
614 * @return The user data.
615 */
616PJ_DECL(void*) pj_ssl_sock_get_user_data(pj_ssl_sock_t *ssock);
617
618
619/**
620 * Retrieve the local address and port used by specified secure socket.
621 *
622 * @param ssock The secure socket.
623 * @param info The info buffer to be set, see #pj_ssl_sock_info.
624 *
625 * @return PJ_SUCCESS on successful.
626 */
627PJ_DECL(pj_status_t) pj_ssl_sock_get_info(pj_ssl_sock_t *ssock,
628 pj_ssl_sock_info *info);
629
630
631/**
632 * Starts read operation on this secure socket. This function will create
633 * \a async_cnt number of buffers (the \a async_cnt parameter was given
634 * in \a pj_ssl_sock_create() function) where each buffer is \a buff_size
635 * long. The buffers are allocated from the specified \a pool. Once the
636 * buffers are created, it then issues \a async_cnt number of asynchronous
637 * \a recv() operations to the socket and returns back to caller. Incoming
638 * data on the socket will be reported back to application via the
639 * \a on_data_read() callback.
640 *
641 * Application only needs to call this function once to initiate read
642 * operations. Further read operations will be done automatically by the
643 * secure socket when \a on_data_read() callback returns non-zero.
644 *
645 * @param ssock The secure socket.
646 * @param pool Pool used to allocate buffers for incoming data.
647 * @param buff_size The size of each buffer, in bytes.
648 * @param flags Flags to be given to pj_ioqueue_recv().
649 *
650 * @return PJ_SUCCESS if the operation has been successful,
651 * or the appropriate error code on failure.
652 */
653PJ_DECL(pj_status_t) pj_ssl_sock_start_read(pj_ssl_sock_t *ssock,
654 pj_pool_t *pool,
655 unsigned buff_size,
656 pj_uint32_t flags);
657
658/**
659 * Same as #pj_ssl_sock_start_read(), except that the application
660 * supplies the buffers for the read operation so that the acive socket
661 * does not have to allocate the buffers.
662 *
663 * @param ssock The secure socket.
664 * @param pool Pool used to allocate buffers for incoming data.
665 * @param buff_size The size of each buffer, in bytes.
666 * @param readbuf Array of packet buffers, each has buff_size size.
667 * @param flags Flags to be given to pj_ioqueue_recv().
668 *
669 * @return PJ_SUCCESS if the operation has been successful,
670 * or the appropriate error code on failure.
671 */
672PJ_DECL(pj_status_t) pj_ssl_sock_start_read2(pj_ssl_sock_t *ssock,
673 pj_pool_t *pool,
674 unsigned buff_size,
675 void *readbuf[],
676 pj_uint32_t flags);
677
678/**
679 * Same as pj_ssl_sock_start_read(), except that this function is used
680 * only for datagram sockets, and it will trigger \a on_data_recvfrom()
681 * callback instead.
682 *
683 * @param ssock The secure socket.
684 * @param pool Pool used to allocate buffers for incoming data.
685 * @param buff_size The size of each buffer, in bytes.
686 * @param flags Flags to be given to pj_ioqueue_recvfrom().
687 *
688 * @return PJ_SUCCESS if the operation has been successful,
689 * or the appropriate error code on failure.
690 */
691PJ_DECL(pj_status_t) pj_ssl_sock_start_recvfrom(pj_ssl_sock_t *ssock,
692 pj_pool_t *pool,
693 unsigned buff_size,
694 pj_uint32_t flags);
695
696/**
697 * Same as #pj_ssl_sock_start_recvfrom() except that the recvfrom()
698 * operation takes the buffer from the argument rather than creating
699 * new ones.
700 *
701 * @param ssock The secure socket.
702 * @param pool Pool used to allocate buffers for incoming data.
703 * @param buff_size The size of each buffer, in bytes.
704 * @param readbuf Array of packet buffers, each has buff_size size.
705 * @param flags Flags to be given to pj_ioqueue_recvfrom().
706 *
707 * @return PJ_SUCCESS if the operation has been successful,
708 * or the appropriate error code on failure.
709 */
710PJ_DECL(pj_status_t) pj_ssl_sock_start_recvfrom2(pj_ssl_sock_t *ssock,
711 pj_pool_t *pool,
712 unsigned buff_size,
713 void *readbuf[],
714 pj_uint32_t flags);
715
716/**
717 * Send data using the socket.
718 *
719 * @param ssock The secure socket.
720 * @param send_key The operation key to send the data, which is useful
721 * if application wants to submit multiple pending
722 * send operations and want to track which exact data
723 * has been sent in the \a on_data_sent() callback.
724 * @param data The data to be sent. This data must remain valid
725 * until the data has been sent.
726 * @param size The size of the data.
727 * @param flags Flags to be given to pj_ioqueue_send().
728 *
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000729 * @return PJ_SUCCESS if data has been sent immediately, or
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000730 * PJ_EPENDING if data cannot be sent immediately or
731 * PJ_ENOMEM when sending buffer could not handle all
732 * queued data, see \a send_buffer_size. The callback
733 * \a on_data_sent() will be called when data is actually
734 * sent. Any other return value indicates error condition.
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000735 */
736PJ_DECL(pj_status_t) pj_ssl_sock_send(pj_ssl_sock_t *ssock,
737 pj_ioqueue_op_key_t *send_key,
738 const void *data,
739 pj_ssize_t *size,
740 unsigned flags);
741
742/**
743 * Send datagram using the socket.
744 *
745 * @param ssock The secure socket.
746 * @param send_key The operation key to send the data, which is useful
747 * if application wants to submit multiple pending
748 * send operations and want to track which exact data
749 * has been sent in the \a on_data_sent() callback.
750 * @param data The data to be sent. This data must remain valid
751 * until the data has been sent.
752 * @param size The size of the data.
753 * @param flags Flags to be given to pj_ioqueue_send().
754 * @param addr The destination address.
755 * @param addr_len Length of buffer containing destination address.
756 *
757 * @return PJ_SUCCESS if data has been sent immediately, or
758 * PJ_EPENDING if data cannot be sent immediately. In
759 * this case the \a on_data_sent() callback will be
760 * called when data is actually sent. Any other return
761 * value indicates error condition.
762 */
763PJ_DECL(pj_status_t) pj_ssl_sock_sendto(pj_ssl_sock_t *ssock,
764 pj_ioqueue_op_key_t *send_key,
765 const void *data,
766 pj_ssize_t *size,
767 unsigned flags,
768 const pj_sockaddr_t *addr,
769 int addr_len);
770
771
772/**
773 * Starts asynchronous socket accept() operations on this secure socket.
774 * This function will issue \a async_cnt number of asynchronous \a accept()
775 * operations to the socket and returns back to caller. Incoming
776 * connection on the socket will be reported back to application via the
777 * \a on_accept_complete() callback.
778 *
779 * Application only needs to call this function once to initiate accept()
780 * operations. Further accept() operations will be done automatically by
781 * the secure socket when \a on_accept_complete() callback returns non-zero.
782 *
783 * @param ssock The secure socket.
784 * @param pool Pool used to allocate some internal data for the
785 * operation.
786 * @param localaddr Local address to bind on.
787 * @param addr_len Length of buffer containing local address.
788 *
789 * @return PJ_SUCCESS if the operation has been successful,
790 * or the appropriate error code on failure.
791 */
792PJ_DECL(pj_status_t) pj_ssl_sock_start_accept(pj_ssl_sock_t *ssock,
793 pj_pool_t *pool,
794 const pj_sockaddr_t *local_addr,
795 int addr_len);
796
797
798/**
799 * Starts asynchronous socket connect() operation and SSL/TLS handshaking
800 * for this socket. Once the connection is done (either successfully or not),
801 * the \a on_connect_complete() callback will be called.
802 *
803 * @param ssock The secure socket.
804 * @param pool The pool to allocate some internal data for the
805 * operation.
806 * @param localaddr Local address.
807 * @param remaddr Remote address.
808 * @param addr_len Length of buffer containing above addresses.
809 *
810 * @return PJ_SUCCESS if connection can be established immediately
811 * or PJ_EPENDING if connection cannot be established
812 * immediately. In this case the \a on_connect_complete()
813 * callback will be called when connection is complete.
814 * Any other return value indicates error condition.
815 */
816PJ_DECL(pj_status_t) pj_ssl_sock_start_connect(pj_ssl_sock_t *ssock,
817 pj_pool_t *pool,
818 const pj_sockaddr_t *localaddr,
819 const pj_sockaddr_t *remaddr,
820 int addr_len);
821
822
823/**
Nanang Izzuddinea6d3c42009-10-26 15:47:52 +0000824 * Starts SSL/TLS renegotiation over an already established SSL connection
825 * for this socket. This operation is performed transparently, no callback
826 * will be called once the renegotiation completed successfully. However,
827 * when the renegotiation fails, the connection will be closed and callback
828 * \a on_data_read() will be invoked with non-PJ_SUCCESS status code.
829 *
830 * @param ssock The secure socket.
831 *
832 * @return PJ_SUCCESS if renegotiation is completed immediately,
833 * or PJ_EPENDING if renegotiation has been started and
834 * waiting for completion, or the appropriate error code
835 * on failure.
836 */
837PJ_DECL(pj_status_t) pj_ssl_sock_renegotiate(pj_ssl_sock_t *ssock);
838
839
840/**
Nanang Izzuddin6c62bf42009-08-27 19:55:13 +0000841 * @}
842 */
843
844PJ_END_DECL
845
846#endif /* __PJ_SSL_SOCK_H__ */