diff options
Diffstat (limited to 'libsylph/ssl_hostname_validation.c')
| -rw-r--r-- | libsylph/ssl_hostname_validation.c | 282 |
1 files changed, 282 insertions, 0 deletions
diff --git a/libsylph/ssl_hostname_validation.c b/libsylph/ssl_hostname_validation.c new file mode 100644 index 0000000..cb2cea9 --- /dev/null +++ b/libsylph/ssl_hostname_validation.c | |||
| @@ -0,0 +1,282 @@ | |||
| 1 | /* | ||
| 2 | * Helper functions to perform basic hostname validation using OpenSSL. | ||
| 3 | * | ||
| 4 | * Copyright (C) 2012, iSEC Partners. | ||
| 5 | * | ||
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| 7 | * this software and associated documentation files (the "Software"), to deal in | ||
| 8 | * the Software without restriction, including without limitation the rights to | ||
| 9 | + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
| 10 | * of the Software, and to permit persons to whom the Software is furnished to do | ||
| 11 | + so, subject to the following conditions: | ||
| 12 | + | ||
| 13 | + The above copyright notice and this permission notice shall be included in all | ||
| 14 | * copies or substantial portions of the Software. | ||
| 15 | * | ||
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 22 | * SOFTWARE. | ||
| 23 | * | ||
| 24 | * Author: Alban Diquet | ||
| 25 | * | ||
| 26 | * https://github.com/iSECPartners/ssl-conservatory | ||
| 27 | * | ||
| 28 | * Modified naming convention to match LibSylph. | ||
| 29 | * | ||
| 30 | */ | ||
| 31 | |||
| 32 | #ifdef HAVE_CONFIG_H | ||
| 33 | # include "config.h" | ||
| 34 | #endif | ||
| 35 | |||
| 36 | #if USE_SSL | ||
| 37 | |||
| 38 | #include <glib.h> | ||
| 39 | |||
| 40 | #include <strings.h> | ||
| 41 | #include <openssl/x509v3.h> | ||
| 42 | #include <openssl/ssl.h> | ||
| 43 | |||
| 44 | #include "utils.h" | ||
| 45 | #include "ssl_hostname_validation.h" | ||
| 46 | |||
| 47 | |||
| 48 | #define HOSTNAME_MAX_SIZE 255 | ||
| 49 | |||
| 50 | |||
| 51 | /* The following host_match() function is based on cURL/libcurl code. */ | ||
| 52 | |||
| 53 | /*************************************************************************** | ||
| 54 | * _ _ ____ _ | ||
| 55 | * Project ___| | | | _ \| | | ||
| 56 | * / __| | | | |_) | | | ||
| 57 | * | (__| |_| | _ <| |___ | ||
| 58 | * \___|\___/|_| \_\_____| | ||
| 59 | * | ||
| 60 | * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al. | ||
| 61 | * | ||
| 62 | * This software is licensed as described in the file COPYING, which | ||
| 63 | * you should have received as part of this distribution. The terms | ||
| 64 | * are also available at http://curl.haxx.se/docs/copyright.html. | ||
| 65 | * | ||
| 66 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell | ||
| 67 | * copies of the Software, and permit persons to whom the Software is | ||
| 68 | * furnished to do so, under the terms of the COPYING file. | ||
| 69 | * | ||
| 70 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
| 71 | * KIND, either express or implied. | ||
| 72 | * | ||
| 73 | ***************************************************************************/ | ||
| 74 | |||
| 75 | /*************************************************************************** | ||
| 76 | * | ||
| 77 | * COPYRIGHT AND PERMISSION NOTICE | ||
| 78 | * | ||
| 79 | * Copyright (c) 1996 - 2014, Daniel Stenberg, <daniel@haxx.se>. | ||
| 80 | * | ||
| 81 | * All rights reserved. | ||
| 82 | * | ||
| 83 | * Permission to use, copy, modify, and distribute this software for any purpose | ||
| 84 | * with or without fee is hereby granted, provided that the above copyright | ||
| 85 | * notice and this permission notice appear in all copies. | ||
| 86 | * | ||
| 87 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 88 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 89 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN | ||
| 90 | * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
| 91 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
| 92 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE | ||
| 93 | * OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 94 | * | ||
| 95 | * Except as contained in this notice, the name of a copyright holder shall not | ||
| 96 | * be used in advertising or otherwise to promote the sale, use or other dealings | ||
| 97 | * in this Software without prior written authorization of the copyright holder. | ||
| 98 | * | ||
| 99 | ***************************************************************************/ | ||
| 100 | |||
| 101 | /** | ||
| 102 | * Match a hostname against a wildcard pattern. | ||
| 103 | * E.g. | ||
| 104 | * "foo.host.com" matches "*.host.com". | ||
| 105 | * | ||
| 106 | * We use the matching rule described in RFC6125, section 6.4.3. | ||
| 107 | * http://tools.ietf.org/html/rfc6125#section-6.4.3 | ||
| 108 | */ | ||
| 109 | |||
| 110 | static int host_match(const char *hostname, const char *pattern) | ||
| 111 | { | ||
| 112 | const char *pattern_label_end, *pattern_wildcard, *hostname_label_end; | ||
| 113 | size_t prefixlen, suffixlen; | ||
| 114 | |||
| 115 | if (!pattern || !*pattern || !hostname || !*hostname) | ||
| 116 | return SSL_HOSTNAME_MATCH_NOT_FOUND; | ||
| 117 | |||
| 118 | /* trivial case */ | ||
| 119 | if (g_ascii_strcasecmp(pattern, hostname) == 0) | ||
| 120 | return SSL_HOSTNAME_MATCH_FOUND; | ||
| 121 | |||
| 122 | pattern_wildcard = strchr(pattern, '*'); | ||
| 123 | if (pattern_wildcard == NULL) { | ||
| 124 | return g_ascii_strcasecmp(pattern, hostname) == 0 ? | ||
| 125 | SSL_HOSTNAME_MATCH_FOUND : SSL_HOSTNAME_MATCH_NOT_FOUND; | ||
| 126 | } | ||
| 127 | |||
| 128 | /* We require at least 2 dots in pattern to avoid too wide wildcard | ||
| 129 | match. */ | ||
| 130 | pattern_label_end = strchr(pattern, '.'); | ||
| 131 | if (pattern_label_end == NULL || | ||
| 132 | strchr(pattern_label_end + 1, '.') == NULL || | ||
| 133 | pattern_wildcard > pattern_label_end || | ||
| 134 | g_ascii_strncasecmp(pattern, "xn--", 4) == 0) { | ||
| 135 | return g_ascii_strcasecmp(pattern, hostname) == 0 ? | ||
| 136 | SSL_HOSTNAME_MATCH_FOUND : SSL_HOSTNAME_MATCH_NOT_FOUND; | ||
| 137 | } | ||
| 138 | |||
| 139 | hostname_label_end = strchr(hostname, '.'); | ||
| 140 | if (hostname_label_end == NULL || | ||
| 141 | g_ascii_strcasecmp(pattern_label_end, hostname_label_end) != 0) | ||
| 142 | return SSL_HOSTNAME_MATCH_NOT_FOUND; | ||
| 143 | |||
| 144 | /* The wildcard must match at least one character, so the left-most | ||
| 145 | label of the hostname is at least as large as the left-most label | ||
| 146 | of the pattern. */ | ||
| 147 | if (hostname_label_end - hostname < pattern_label_end - pattern) | ||
| 148 | return SSL_HOSTNAME_MATCH_NOT_FOUND; | ||
| 149 | |||
| 150 | prefixlen = pattern_wildcard - pattern; | ||
| 151 | suffixlen = pattern_label_end - (pattern_wildcard + 1); | ||
| 152 | return g_ascii_strncasecmp(pattern, hostname, prefixlen) == 0 && | ||
| 153 | g_ascii_strncasecmp(pattern_wildcard + 1, hostname_label_end - suffixlen, suffixlen) == 0 ? | ||
| 154 | SSL_HOSTNAME_MATCH_FOUND : SSL_HOSTNAME_MATCH_NOT_FOUND; | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | /** | ||
| 159 | * Tries to find a match for hostname in the certificate's Common Name field. | ||
| 160 | * | ||
| 161 | * Returns MatchFound if a match was found. | ||
| 162 | * Returns MatchNotFound if no matches were found. | ||
| 163 | * Returns MalformedCertificate if the Common Name had a NUL character embedded in it. | ||
| 164 | * Returns Error if the Common Name could not be extracted. | ||
| 165 | */ | ||
| 166 | static SSLHostnameValidationResult matches_common_name(const char *hostname, const X509 *server_cert) { | ||
| 167 | int common_name_loc = -1; | ||
| 168 | X509_NAME_ENTRY *common_name_entry = NULL; | ||
| 169 | ASN1_STRING *common_name_asn1 = NULL; | ||
| 170 | char *common_name_str = NULL; | ||
| 171 | |||
| 172 | // Find the position of the CN field in the Subject field of the certificate | ||
| 173 | common_name_loc = X509_NAME_get_index_by_NID(X509_get_subject_name((X509 *) server_cert), NID_commonName, -1); | ||
| 174 | if (common_name_loc < 0) { | ||
| 175 | return SSL_HOSTNAME_ERROR; | ||
| 176 | } | ||
| 177 | |||
| 178 | // Extract the CN field | ||
| 179 | common_name_entry = X509_NAME_get_entry(X509_get_subject_name((X509 *) server_cert), common_name_loc); | ||
| 180 | if (common_name_entry == NULL) { | ||
| 181 | return SSL_HOSTNAME_ERROR; | ||
| 182 | } | ||
| 183 | |||
| 184 | // Convert the CN field to a C string | ||
| 185 | common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry); | ||
| 186 | if (common_name_asn1 == NULL) { | ||
| 187 | return SSL_HOSTNAME_ERROR; | ||
| 188 | } | ||
| 189 | common_name_str = (char *) ASN1_STRING_data(common_name_asn1); | ||
| 190 | |||
| 191 | debug_print("matches_common_name: %s\n", common_name_str); | ||
| 192 | |||
| 193 | // Make sure there isn't an embedded NUL character in the CN | ||
| 194 | if (ASN1_STRING_length(common_name_asn1) != strlen(common_name_str)) { | ||
| 195 | return SSL_HOSTNAME_MALFORMED_CERTIFICATE; | ||
| 196 | } | ||
| 197 | |||
| 198 | // Compare expected hostname with the CN | ||
| 199 | return host_match(hostname, common_name_str); | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | /** | ||
| 204 | * Tries to find a match for hostname in the certificate's Subject Alternative Name extension. | ||
| 205 | * | ||
| 206 | * Returns MatchFound if a match was found. | ||
| 207 | * Returns MatchNotFound if no matches were found. | ||
| 208 | * Returns MalformedCertificate if any of the hostnames had a NUL character embedded in it. | ||
| 209 | * Returns NoSANPresent if the SAN extension was not present in the certificate. | ||
| 210 | */ | ||
| 211 | static SSLHostnameValidationResult matches_subject_alternative_name(const char *hostname, const X509 *server_cert) { | ||
| 212 | SSLHostnameValidationResult result = SSL_HOSTNAME_MATCH_NOT_FOUND; | ||
| 213 | int i; | ||
| 214 | int san_names_nb = -1; | ||
| 215 | STACK_OF(GENERAL_NAME) *san_names = NULL; | ||
| 216 | |||
| 217 | // Try to extract the names within the SAN extension from the certificate | ||
| 218 | san_names = X509_get_ext_d2i((X509 *) server_cert, NID_subject_alt_name, NULL, NULL); | ||
| 219 | if (san_names == NULL) { | ||
| 220 | return SSL_HOSTNAME_NO_SAN_PRESENT; | ||
| 221 | } | ||
| 222 | san_names_nb = sk_GENERAL_NAME_num(san_names); | ||
| 223 | |||
| 224 | // Check each name within the extension | ||
| 225 | for (i=0; i<san_names_nb; i++) { | ||
| 226 | const GENERAL_NAME *current_name = sk_GENERAL_NAME_value(san_names, i); | ||
| 227 | |||
| 228 | if (current_name->type == GEN_DNS) { | ||
| 229 | // Current name is a DNS name, let's check it | ||
| 230 | char *dns_name = (char *) ASN1_STRING_data(current_name->d.dNSName); | ||
| 231 | |||
| 232 | debug_print("matches_subject_alternative_name: %s\n", dns_name); | ||
| 233 | |||
| 234 | // Make sure there isn't an embedded NUL character in the DNS name | ||
| 235 | if (ASN1_STRING_length(current_name->d.dNSName) != strlen(dns_name)) { | ||
| 236 | result = SSL_HOSTNAME_MALFORMED_CERTIFICATE; | ||
| 237 | break; | ||
| 238 | } | ||
| 239 | else { // Compare expected hostname with the DNS name | ||
| 240 | if (host_match(hostname, dns_name) == SSL_HOSTNAME_MATCH_FOUND) { | ||
| 241 | result = SSL_HOSTNAME_MATCH_FOUND; | ||
| 242 | break; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | } | ||
| 246 | } | ||
| 247 | sk_GENERAL_NAME_pop_free(san_names, GENERAL_NAME_free); | ||
| 248 | |||
| 249 | return result; | ||
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | /** | ||
| 254 | * Validates the server's identity by looking for the expected hostname in the | ||
| 255 | * server's certificate. As described in RFC 6125, it first tries to find a match | ||
| 256 | * in the Subject Alternative Name extension. If the extension is not present in | ||
| 257 | * the certificate, it checks the Common Name instead. | ||
| 258 | * | ||
| 259 | * Returns MatchFound if a match was found. | ||
| 260 | * Returns MatchNotFound if no matches were found. | ||
| 261 | * Returns MalformedCertificate if any of the hostnames had a NUL character embedded in it. | ||
| 262 | * Returns Error if there was an error. | ||
| 263 | */ | ||
| 264 | SSLHostnameValidationResult ssl_validate_hostname(const char *hostname, const X509 *server_cert) { | ||
| 265 | SSLHostnameValidationResult result; | ||
| 266 | |||
| 267 | debug_print("ssl_validate_hostname: validating hostname: %s\n", hostname); | ||
| 268 | |||
| 269 | if((hostname == NULL) || (server_cert == NULL)) | ||
| 270 | return SSL_HOSTNAME_ERROR; | ||
| 271 | |||
| 272 | // First try the Subject Alternative Names extension | ||
| 273 | result = matches_subject_alternative_name(hostname, server_cert); | ||
| 274 | if (result == SSL_HOSTNAME_NO_SAN_PRESENT) { | ||
| 275 | // Extension was not found: try the Common Name | ||
| 276 | result = matches_common_name(hostname, server_cert); | ||
| 277 | } | ||
| 278 | |||
| 279 | return result; | ||
| 280 | } | ||
| 281 | |||
| 282 | #endif /* USE_SSL */ | ||
