Debian Codegraph @

Xpjproject/pjlib/src/pj/ssl_sock_common.c

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 #include <pj/ssl_sock.h>
19 #include <pj/assert.h>
20 #include <pj/errno.h>
21 #include <pj/pool.h>
22 #include <pj/string.h>
23
24 /*
25 * Initialize the SSL socket configuration with the default values.
26 */
27 PJ_DEF(void) pj_ssl_sock_param_default(pj_ssl_sock_param *param)
28 {
29 pj_bzero(param, sizeof(*param));
30
31 /* Socket config */
32 param->sock_af = PJ_AF_INET;
33 param->sock_type = pj_SOCK_STREAM();
34 param->async_cnt = 1;
35 param->concurrency = -1;
36 param->whole_data = PJ_TRUE;
37
38 #if (PJ_SSL_SOCK_IMP == PJ_SSL_SOCK_IMP_GNUTLS)
39 /* GnuTLS is allowed to send bigger chunks.*/
40 param->send_buffer_size = 65536;
41
42 {
43 /* For GnuTLS, TCP_NODELAY is needed to avoid polling delay. */
44 static pj_int32_t val = 1;
45 param->sockopt_params.cnt = 1;
46 param->sockopt_params.options[0].level = pj_SOL_TCP();
47 param->sockopt_params.options[0].optname = pj_TCP_NODELAY();
48 param->sockopt_params.options[0].optval = &val;
49 param->sockopt_params.options[0].optlen = sizeof(pj_int32_t);
50 }
51
52 #else
53 param->send_buffer_size = 8192;
54 #endif
55 #if !defined(PJ_SYMBIAN) || PJ_SYMBIAN==0
56 param->read_buffer_size = 1500;
57 #endif
58 param->qos_type = PJ_QOS_TYPE_BEST_EFFORT;
59 param->qos_ignore_error = PJ_TRUE;
60
61 param->sockopt_ignore_error = PJ_TRUE;
62 param->sock_cloexec = PJ_TRUE;
63 param->enable_renegotiation = PJ_TRUE;
64
65 /* Security config */
66 param->proto = PJ_SSL_SOCK_PROTO_DEFAULT;
67 }
68
69
70 /*
71 * Duplicate SSL socket parameter.
72 */
73 PJ_DEF(void) pj_ssl_sock_param_copy( pj_pool_t *pool,
74 pj_ssl_sock_param *dst,
75 const pj_ssl_sock_param *src)
76 {
77 /* Init secure socket param */
78 pj_memcpy(dst, src, sizeof(*dst));
79 if (src->ciphers_num > 0) {
80 unsigned i;
81 dst->ciphers = (pj_ssl_cipher*)
82 pj_pool_calloc(pool, src->ciphers_num,
83 sizeof(pj_ssl_cipher));
84 for (i = 0; i < src->ciphers_num; ++i)
85 dst->ciphers[i] = src->ciphers[i];
86 }
87
88 if (src->curves_num > 0) {
89 unsigned i;
90 dst->curves = (pj_ssl_curve *)pj_pool_calloc(pool, src->curves_num,
91 sizeof(pj_ssl_curve));
92 for (i = 0; i < src->curves_num; ++i)
93 dst->curves[i] = src->curves[i];
94 }
95
96 if (src->server_name.slen) {
97 /* Server name must be null-terminated */
98 pj_strdup_with_null(pool, &dst->server_name, &src->server_name);
99 }
100
101 if (src->sigalgs.slen) {
102 /* Sigalgs name must be null-terminated */
103 pj_strdup_with_null(pool, &dst->sigalgs, &src->sigalgs);
104 }
105
106 if (src->entropy_path.slen) {
107 /* Path name must be null-terminated */
108 pj_strdup_with_null(pool, &dst->entropy_path, &src->entropy_path);
109 }
110
111 pj_sockopt_params_clone(pool, &dst->sockopt_params, &src->sockopt_params);
112 }
113
114
115 PJ_DEF(pj_status_t) pj_ssl_cert_get_verify_status_strings(
116 pj_uint32_t verify_status,
117 const char *error_strings[],
118 unsigned *count)
119 {
120 unsigned i = 0, shift_idx = 0;
121 unsigned unknown = 0;
122 pj_uint32_t errs;
123
124 PJ_ASSERT_RETURN(error_strings && count, PJ_EINVAL);
125
126 if (verify_status == PJ_SSL_CERT_ESUCCESS && *count) {
127 error_strings[0] = "OK";
128 *count = 1;
129 return PJ_SUCCESS;
130 }
131
132 errs = verify_status;
133
134 while (errs && i < *count) {
135 pj_uint32_t err;
136 const char *p = NULL;
137
138 if ((errs & 1) == 0) {
139 shift_idx++;
140 errs >>= 1;
141 continue;
142 }
143
144 err = (1 << shift_idx);
145
146 switch (err) {
147 case PJ_SSL_CERT_EISSUER_NOT_FOUND:
148 p = "The issuer certificate cannot be found";
149 break;
150 case PJ_SSL_CERT_EUNTRUSTED:
151 p = "The certificate is untrusted";
152 break;
153 case PJ_SSL_CERT_EVALIDITY_PERIOD:
154 p = "The certificate has expired or not yet valid";
155 break;
156 case PJ_SSL_CERT_EINVALID_FORMAT:
157 p = "One or more fields of the certificate cannot be decoded "
158 "due to invalid format";
159 break;
160 case PJ_SSL_CERT_EISSUER_MISMATCH:
161 p = "The issuer info in the certificate does not match to the "
162 "(candidate) issuer certificate";
163 break;
164 case PJ_SSL_CERT_ECRL_FAILURE:
165 p = "The CRL certificate cannot be found or cannot be read "
166 "properly";
167 break;
168 case PJ_SSL_CERT_EREVOKED:
169 p = "The certificate has been revoked";
170 break;
171 case PJ_SSL_CERT_EINVALID_PURPOSE:
172 p = "The certificate or CA certificate cannot be used for the "
173 "specified purpose";
174 break;
175 case PJ_SSL_CERT_ECHAIN_TOO_LONG:
176 p = "The certificate chain length is too long";
177 break;
178 case PJ_SSL_CERT_EWEAK_SIGNATURE:
179 p = "The certificate signature is created using a weak hashing "
180 "algorithm";
181 break;
182 case PJ_SSL_CERT_EIDENTITY_NOT_MATCH:
183 p = "The server identity does not match to any identities "
184 "specified in the certificate";
185 break;
186 case PJ_SSL_CERT_EUNKNOWN:
187 default:
188 unknown++;
189 break;
190 }
191
192 /* Set error string */
193 if (p)
194 error_strings[i++] = p;
195
196 /* Next */
197 shift_idx++;
198 errs >>= 1;
199 }
200
201 /* Unknown error */
202 if (unknown && i < *count)
203 error_strings[i++] = "Unknown verification error";
204
205 *count = i;
206
207 return PJ_SUCCESS;
208 }