Alexandre Lision | 8af73cb | 2013-12-10 14:11:20 -0500 | [diff] [blame] | 1 | /* $Id: ssl_sock_common.c 3999 2012-03-30 07:10:13Z bennylp $ */ |
Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2009-2011 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 | #include <pj/ssl_sock.h> |
| 20 | #include <pj/assert.h> |
| 21 | #include <pj/errno.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 | param->send_buffer_size = 8192; |
| 38 | #if !defined(PJ_SYMBIAN) || PJ_SYMBIAN==0 |
| 39 | param->read_buffer_size = 1500; |
| 40 | #endif |
| 41 | param->qos_type = PJ_QOS_TYPE_BEST_EFFORT; |
| 42 | param->qos_ignore_error = PJ_TRUE; |
| 43 | |
| 44 | /* Security config */ |
| 45 | param->proto = PJ_SSL_SOCK_PROTO_DEFAULT; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | PJ_DEF(pj_status_t) pj_ssl_cert_get_verify_status_strings( |
| 50 | pj_uint32_t verify_status, |
| 51 | const char *error_strings[], |
| 52 | unsigned *count) |
| 53 | { |
| 54 | unsigned i = 0, shift_idx = 0; |
| 55 | unsigned unknown = 0; |
| 56 | pj_uint32_t errs; |
| 57 | |
| 58 | PJ_ASSERT_RETURN(error_strings && count, PJ_EINVAL); |
| 59 | |
| 60 | if (verify_status == PJ_SSL_CERT_ESUCCESS && *count) { |
| 61 | error_strings[0] = "OK"; |
| 62 | *count = 1; |
| 63 | return PJ_SUCCESS; |
| 64 | } |
| 65 | |
| 66 | errs = verify_status; |
| 67 | |
| 68 | while (errs && i < *count) { |
| 69 | pj_uint32_t err; |
| 70 | const char *p = NULL; |
| 71 | |
| 72 | if ((errs & 1) == 0) { |
| 73 | shift_idx++; |
| 74 | errs >>= 1; |
| 75 | continue; |
| 76 | } |
| 77 | |
| 78 | err = (1 << shift_idx); |
| 79 | |
| 80 | switch (err) { |
| 81 | case PJ_SSL_CERT_EISSUER_NOT_FOUND: |
| 82 | p = "The issuer certificate cannot be found"; |
| 83 | break; |
| 84 | case PJ_SSL_CERT_EUNTRUSTED: |
| 85 | p = "The certificate is untrusted"; |
| 86 | break; |
| 87 | case PJ_SSL_CERT_EVALIDITY_PERIOD: |
| 88 | p = "The certificate has expired or not yet valid"; |
| 89 | break; |
| 90 | case PJ_SSL_CERT_EINVALID_FORMAT: |
| 91 | p = "One or more fields of the certificate cannot be decoded " |
| 92 | "due to invalid format"; |
| 93 | break; |
| 94 | case PJ_SSL_CERT_EISSUER_MISMATCH: |
| 95 | p = "The issuer info in the certificate does not match to the " |
| 96 | "(candidate) issuer certificate"; |
| 97 | break; |
| 98 | case PJ_SSL_CERT_ECRL_FAILURE: |
| 99 | p = "The CRL certificate cannot be found or cannot be read " |
| 100 | "properly"; |
| 101 | break; |
| 102 | case PJ_SSL_CERT_EREVOKED: |
| 103 | p = "The certificate has been revoked"; |
| 104 | break; |
| 105 | case PJ_SSL_CERT_EINVALID_PURPOSE: |
| 106 | p = "The certificate or CA certificate cannot be used for the " |
| 107 | "specified purpose"; |
| 108 | break; |
| 109 | case PJ_SSL_CERT_ECHAIN_TOO_LONG: |
| 110 | p = "The certificate chain length is too long"; |
| 111 | break; |
| 112 | case PJ_SSL_CERT_EIDENTITY_NOT_MATCH: |
| 113 | p = "The server identity does not match to any identities " |
| 114 | "specified in the certificate"; |
| 115 | break; |
| 116 | case PJ_SSL_CERT_EUNKNOWN: |
| 117 | default: |
| 118 | unknown++; |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | /* Set error string */ |
| 123 | if (p) |
| 124 | error_strings[i++] = p; |
| 125 | |
| 126 | /* Next */ |
| 127 | shift_idx++; |
| 128 | errs >>= 1; |
| 129 | } |
| 130 | |
| 131 | /* Unknown error */ |
| 132 | if (unknown && i < *count) |
| 133 | error_strings[i++] = "Unknown verification error"; |
| 134 | |
| 135 | *count = i; |
| 136 | |
| 137 | return PJ_SUCCESS; |
| 138 | } |