Fossil SCM

fossil-scm / src / http_ssl.c
Source Blame History 1253 lines
68fa646… drh 1 /*
c19f34c… drh 2 ** Copyright (c) 2009 D. Richard Hipp
68fa646… drh 3 **
68fa646… drh 4 ** This program is free software; you can redistribute it and/or
c7133bd… drh 5 ** modify it under the terms of the Simplified BSD License (also
c7133bd… drh 6 ** known as the "2-Clause License" or "FreeBSD License".)
68fa646… drh 7 **
68fa646… drh 8 ** This program is distributed in the hope that it will be useful,
c7133bd… drh 9 ** but without any warranty; without even the implied warranty of
c7133bd… drh 10 ** merchantability or fitness for a particular purpose.
68fa646… drh 11 **
68fa646… drh 12 ** Author contact information:
68fa646… drh 13 ** [email protected]
68fa646… drh 14 ** http://www.hwaci.com/drh/
68fa646… drh 15 **
68fa646… drh 16 *******************************************************************************
68fa646… drh 17 **
68fa646… drh 18 ** This file manages low-level SSL communications.
68fa646… drh 19 **
68fa646… drh 20 ** This file implements a singleton. A single SSL connection may be active
f6263bb… drh 21 ** at a time. State information is stored in static variables.
f6263bb… drh 22 **
f6263bb… drh 23 ** The SSL connections can be either a client or a server. But all
f6263bb… drh 24 ** connections for a single process must be of the same type, either client
f6263bb… drh 25 ** or server.
68fa646… drh 26 **
68fa646… drh 27 ** SSL support is abstracted out into this module because Fossil can
68fa646… drh 28 ** be compiled without SSL support (which requires OpenSSL library)
68fa646… drh 29 */
68fa646… drh 30
68fa646… drh 31 #include "config.h"
b282400… drh 32 #include "http_ssl.h"
68fa646… drh 33
68fa646… drh 34 #ifdef FOSSIL_ENABLE_SSL
68fa646… drh 35
68fa646… drh 36 #include <openssl/bio.h>
68fa646… drh 37 #include <openssl/ssl.h>
68fa646… drh 38 #include <openssl/err.h>
67147dd… drh 39 #include <openssl/x509.h>
6753e74… danshearer 40 #include <openssl/x509v3.h>
68fa646… drh 41
68fa646… drh 42 #include <assert.h>
68fa646… drh 43 #include <sys/types.h>
68fa646… drh 44
68fa646… drh 45 /*
68fa646… drh 46 ** There can only be a single OpenSSL IO connection open at a time.
68fa646… drh 47 ** State information about that IO is stored in the following
68fa646… drh 48 ** local variables:
68fa646… drh 49 */
f6263bb… drh 50 static int sslIsInit = 0; /* 0: uninit 1: init as client 2: init as server */
3a00b61… drh 51 static BIO *iBio = 0; /* OpenSSL I/O abstraction */
68fa646… drh 52 static char *sslErrMsg = 0; /* Text of most recent OpenSSL error */
68fa646… drh 53 static SSL_CTX *sslCtx; /* SSL context */
68fa646… drh 54 static SSL *ssl;
3c194e2… drh 55 static struct { /* Accept this SSL cert for this session only */
3c194e2… drh 56 char *zHost; /* Subject or host name */
3b529d9… drh 57 char *zHash; /* SHA2-256 hash of the cert */
3c194e2… drh 58 } sException;
3c194e2… drh 59 static int sslNoCertVerify = 0; /* Do not verify SSL certs */
f6263bb… drh 60
f6263bb… drh 61
f6263bb… drh 62 /* This is a self-signed cert in the PEM format that can be used when
f6263bb… drh 63 ** no other certs are available.
f6263bb… drh 64 */
275da70… danield 65 static const char sslSelfCert[] =
f6263bb… drh 66 "-----BEGIN CERTIFICATE-----\n"
f6263bb… drh 67 "MIIDMTCCAhkCFGrDmuJkkzWERP/ITBvzwwI2lv0TMA0GCSqGSIb3DQEBCwUAMFQx\n"
f6263bb… drh 68 "CzAJBgNVBAYTAlVTMQswCQYDVQQIDAJOQzESMBAGA1UEBwwJQ2hhcmxvdHRlMRMw\n"
f6263bb… drh 69 "EQYDVQQKDApGb3NzaWwtU0NNMQ8wDQYDVQQDDAZGb3NzaWwwIBcNMjExMjI3MTEz\n"
f6263bb… drh 70 "MTU2WhgPMjEyMTEyMjcxMTMxNTZaMFQxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJO\n"
f6263bb… drh 71 "QzESMBAGA1UEBwwJQ2hhcmxvdHRlMRMwEQYDVQQKDApGb3NzaWwtU0NNMQ8wDQYD\n"
f6263bb… drh 72 "VQQDDAZGb3NzaWwwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCCbTU2\n"
f6263bb… drh 73 "6GRQHQqLq7vyZ0OxpAxmgfAKCxt6eIz+jBi2ZM/CB5vVXWVh2+SkSiWEA3UZiUqX\n"
f6263bb… drh 74 "xZlzmS/CglZdiwLLDJML8B4OiV72oivFH/vJ7+cbvh1dTxnYiHuww7GfQngPrLfe\n"
f6263bb… drh 75 "fiIYPDk1GTUJHBQ7Ue477F7F8vKuHdVgwktF/JDM6M60aSqlo2D/oysirrb+dlur\n"
f6263bb… drh 76 "Tlv0rjsYOfq6bLAajoL3qi/vek6DNssoywbge4PfbTgS9g7Gcgncbcet5pvaS12J\n"
f6263bb… drh 77 "avhFcd4JU4Ity49Hl9S/C2MfZ1tE53xVggRwKz4FPj65M5uymTdcxtjKXtCxIE1k\n"
f6263bb… drh 78 "KxJxXQh7rIYjm+RTAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAFkdtpqcybAzJN8G\n"
f6263bb… drh 79 "+ONuUm5sXNbWta7JGvm8l0BTSBcCUtJA3hn16iJqXA9KmLnaF2denC4EYk+KlVU1\n"
f6263bb… drh 80 "QXxskPJ4jB8A5B05jMijYv0nzCxKhviI8CR7GLEEGKzeg9pbW0+O3vaVehoZtdFX\n"
f6263bb… drh 81 "z3SsCssr9QjCLiApQxMzW1Iv3od2JXeHBwfVMFrWA1VCEUCRs8OSW/VOqDPJLVEi\n"
f6263bb… drh 82 "G6wxc4kN9dLK+5S29q3nzl24/qzXoF8P9Re5KBCbrwaHgy+OEEceq5jkmfGFxXjw\n"
f6263bb… drh 83 "pvVCNry5uAhH5NqbXZampUWqiWtM4eTaIPo7Y2mDA1uWhuWtO6F9PsnFJlQHCnwy\n"
f6263bb… drh 84 "s/TsrXk=\n"
f6263bb… drh 85 "-----END CERTIFICATE-----\n";
f6263bb… drh 86
f6263bb… drh 87 /* This is the private-key corresponding to the cert above
f6263bb… drh 88 */
275da70… danield 89 static const char sslSelfPKey[] =
f6263bb… drh 90 "-----BEGIN PRIVATE KEY-----\n"
f6263bb… drh 91 "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCCbTU26GRQHQqL\n"
f6263bb… drh 92 "q7vyZ0OxpAxmgfAKCxt6eIz+jBi2ZM/CB5vVXWVh2+SkSiWEA3UZiUqXxZlzmS/C\n"
f6263bb… drh 93 "glZdiwLLDJML8B4OiV72oivFH/vJ7+cbvh1dTxnYiHuww7GfQngPrLfefiIYPDk1\n"
f6263bb… drh 94 "GTUJHBQ7Ue477F7F8vKuHdVgwktF/JDM6M60aSqlo2D/oysirrb+dlurTlv0rjsY\n"
f6263bb… drh 95 "Ofq6bLAajoL3qi/vek6DNssoywbge4PfbTgS9g7Gcgncbcet5pvaS12JavhFcd4J\n"
f6263bb… drh 96 "U4Ity49Hl9S/C2MfZ1tE53xVggRwKz4FPj65M5uymTdcxtjKXtCxIE1kKxJxXQh7\n"
f6263bb… drh 97 "rIYjm+RTAgMBAAECggEANfTH1vc8yIe7HRzmm9lsf8jF+II4s2705y2H5qY+cvYx\n"
f6263bb… drh 98 "nKtZJGOG1X0KkYy7CGoFv5K0cSUl3lS5FVamM/yWIzoIex/Sz2C1EIL2aI5as6ez\n"
f6263bb… drh 99 "jB6SN0/J+XI8+Vt7186/rHxfdIPpxuzjHbxX3HTpScETNWcLrghbrPxakbTPPxwt\n"
f6263bb… drh 100 "+x7QlPmmkFNuMfvkzToFf9NdwL++44TeBPOpvD/Lrw+eyqdth9RJPq9cM96plh9V\n"
f6263bb… drh 101 "HuRqeD8+QNafaXBdSQs3FJK/cDK/vWGKZWIfFVSDbDhwYljkXGijreFjtXQfkkpF\n"
f6263bb… drh 102 "rl1J87/H9Ee7z8fTD2YXQHl+0/rghAVtac3u54dpQQKBgQC2XG3OEeMrOp9dNkUd\n"
f6263bb… drh 103 "F8VffUg0ecwG+9L3LCe7U71K0kPmXjV6xNnuYcNQu84kptc5vI8wD23p29LaxdNc\n"
f6263bb… drh 104 "9m0lcw06/YYBOPkNphcHkINYZTvVJF10mL3isymzMaTtwDkZUkOjL1B+MTiFT/qp\n"
f6263bb… drh 105 "ARKrTYGJ4HxY7+tUkI5pUmg4PQKBgQC3GA4d1Rz3Pb/RRpcsZgWknKsKhoN36mSn\n"
f6263bb… drh 106 "xFJ3wPBvVv2B1ltTMzh/+the0ty6clzMrvoLERzRcheDsNrc/j/TUVG8sVdBYJwX\n"
f6263bb… drh 107 "tMZyFW4NVMOErT/1ukh6jBqIMBo6NJL3EV/AKj0yniksgKOr0/AAduAccnGST8Jd\n"
f6263bb… drh 108 "SHOdjwvHzwKBgGZBq/zqgNTDuYseHGE07CMgcDWkumiMGv8ozlq3mSR0hUiPOTPP\n"
f6263bb… drh 109 "YFjQjyIdPXnF6FfiyPPtIvgIoNK2LVAqiod+XUPf152l4dnqcW13dn9BvOxGyPTR\n"
f6263bb… drh 110 "lWCikFaAFviOWjY9r9m4dU1dslDmySqthFd0TZgPvgps9ivkJ0cdw30NAoGAMC/E\n"
f6263bb… drh 111 "h1VvKiK2OP27C5ROJ+STn1GHiCfIFd81VQ8SODtMvL8NifgRBp2eFFaqgOdYRQZI\n"
f6263bb… drh 112 "CGGYlAbS6XXCJCdF5Peh62dA75PdgN+y2pOJQzjrvB9cle9Q4++7i9wdCvSLOTr5\n"
f6263bb… drh 113 "WDnFoWy+qVexu6crovOmR9ZWzYrwPFy1EOJ010ECgYBl7Q+jmjOSqsVwhFZ0U7LG\n"
f6263bb… drh 114 "diN+vXhWfn1wfOWd8u79oaqU/Oy7xyKW2p3H5z2KFrBM/vib53Lh4EwFZjcX+jVG\n"
f6263bb… drh 115 "krAmbL+M/hP7z3TD2UbESAzR/c6l7FU45xN84Lsz5npkR8H/uAHuqLgb9e430Mjx\n"
f6263bb… drh 116 "YNMwdb8rChHHChNZu6zuxw==\n"
f6263bb… drh 117 "-----END PRIVATE KEY-----\n";
f6263bb… drh 118
f6263bb… drh 119 /*
f6263bb… drh 120 ** Read a PEM certificate from memory and push it into an SSL_CTX.
f6263bb… drh 121 ** Return the number of errors.
f6263bb… drh 122 */
f6263bb… drh 123 static int sslctx_use_cert_from_mem(
f6263bb… drh 124 SSL_CTX *ctx,
f6263bb… drh 125 const char *pData,
f6263bb… drh 126 int nData
f6263bb… drh 127 ){
f6263bb… drh 128 BIO *in;
f6263bb… drh 129 int rc = 1;
f6263bb… drh 130 X509 *x = 0;
f6263bb… drh 131 X509 *cert = 0;
f6263bb… drh 132
f6263bb… drh 133 in = BIO_new_mem_buf(pData, nData);
f6263bb… drh 134 if( in==0 ) goto end_of_ucfm;
99ab5cd… danield 135 /* x = X509_new_ex(ctx->libctx, ctx->propq); */
f6263bb… drh 136 x = X509_new();
f6263bb… drh 137 if( x==0 ) goto end_of_ucfm;
f6263bb… drh 138 cert = PEM_read_bio_X509(in, &x, 0, 0);
f6263bb… drh 139 if( cert==0 ) goto end_of_ucfm;
f6263bb… drh 140 rc = SSL_CTX_use_certificate(ctx, x)<=0;
f6263bb… drh 141 end_of_ucfm:
f6263bb… drh 142 X509_free(x);
f6263bb… drh 143 BIO_free(in);
f6263bb… drh 144 return rc;
f6263bb… drh 145 }
f6263bb… drh 146
f6263bb… drh 147 /*
f6263bb… drh 148 ** Read a PEM private key from memory and add it to an SSL_CTX.
f6263bb… drh 149 ** Return the number of errors.
f6263bb… drh 150 */
f6263bb… drh 151 static int sslctx_use_pkey_from_mem(
f6263bb… drh 152 SSL_CTX *ctx,
f6263bb… drh 153 const char *pData,
f6263bb… drh 154 int nData
f6263bb… drh 155 ){
f6263bb… drh 156 int rc = 1;
f6263bb… drh 157 BIO *in;
f6263bb… drh 158 EVP_PKEY *pkey = 0;
f6263bb… drh 159
f6263bb… drh 160 in = BIO_new_mem_buf(pData, nData);
f6263bb… drh 161 if( in==0 ) goto end_of_upkfm;
f6263bb… drh 162 pkey = PEM_read_bio_PrivateKey(in, 0, 0, 0);
f6263bb… drh 163 if( pkey==0 ) goto end_of_upkfm;
f6263bb… drh 164 rc = SSL_CTX_use_PrivateKey(ctx, pkey)<=0;
f6263bb… drh 165 EVP_PKEY_free(pkey);
f6263bb… drh 166 end_of_upkfm:
f6263bb… drh 167 BIO_free(in);
f6263bb… drh 168 return rc;
f6263bb… drh 169 }
68fa646… drh 170
68fa646… drh 171 /*
68fa646… drh 172 ** Clear the SSL error message
68fa646… drh 173 */
68fa646… drh 174 static void ssl_clear_errmsg(void){
68fa646… drh 175 free(sslErrMsg);
68fa646… drh 176 sslErrMsg = 0;
68fa646… drh 177 }
68fa646… drh 178
68fa646… drh 179 /*
68fa646… drh 180 ** Set the SSL error message.
68fa646… drh 181 */
cfb8d66… jan.nijtmans 182 void ssl_set_errmsg(const char *zFormat, ...){
68fa646… drh 183 va_list ap;
68fa646… drh 184 ssl_clear_errmsg();
68fa646… drh 185 va_start(ap, zFormat);
68fa646… drh 186 sslErrMsg = vmprintf(zFormat, ap);
68fa646… drh 187 va_end(ap);
68fa646… drh 188 }
68fa646… drh 189
68fa646… drh 190 /*
68fa646… drh 191 ** Return the current SSL error message
68fa646… drh 192 */
68fa646… drh 193 const char *ssl_errmsg(void){
68fa646… drh 194 return sslErrMsg;
68fa646… drh 195 }
68fa646… drh 196
68fa646… drh 197 /*
9a0c995… drh 198 ** When a server requests a client certificate that hasn't been provided,
9a0c995… drh 199 ** display a warning message explaining what to do next.
9a0c995… drh 200 */
9a0c995… drh 201 static int ssl_client_cert_callback(SSL *ssl, X509 **x509, EVP_PKEY **pkey){
9a0c995… drh 202 fossil_warning("The remote server requested a client certificate for "
9a0c995… drh 203 "authentication. Specify the pathname to a file containing the PEM "
9a0c995… drh 204 "encoded certificate and private key with the --ssl-identity option "
9a0c995… drh 205 "or the ssl-identity setting.");
cfb8d66… jan.nijtmans 206 return 0; /* no cert available */
cfb8d66… jan.nijtmans 207 }
cfb8d66… jan.nijtmans 208
cfb8d66… jan.nijtmans 209 /*
d847300… danield 210 ** Convert an OpenSSL ASN1_TIME to an ISO8601 timestamp.
d847300… danield 211 **
275da70… danield 212 ** Per RFC 5280, ASN1 timestamps in X.509 certificates must
d847300… danield 213 ** be in UTC (Zulu timezone) with no fractional seconds.
d847300… danield 214 **
d847300… danield 215 ** If showUtc==1, add " UTC" at the end of the returned string. This is
d847300… danield 216 ** not ISO8601-compliant, but makes the displayed value more user-friendly.
d847300… danield 217 */
af8109c… danield 218 static const char *ssl_asn1time_to_iso8601(ASN1_TIME *asn1_time,
d847300… danield 219 int showUtc){
d847300… danield 220 assert( showUtc==0 || showUtc==1 );
d847300… danield 221 if( !ASN1_TIME_check(asn1_time) ){
d847300… danield 222 return mprintf("Bad time value");
d847300… danield 223 }else{
d847300… danield 224 char res[20];
d847300… danield 225 char *pr = res;
0d239b5… drh 226 #if OPENSSL_VERSION_NUMBER < 0x10100000L
0d239b5… drh 227 #define ASN1_STRING_get0_data ASN1_STRING_data
0d239b5… drh 228 #endif
0d239b5… drh 229 const char *pt = (const char *)ASN1_STRING_get0_data(asn1_time);
d847300… danield 230 /* 0123456789 1234
d847300… danield 231 ** UTCTime: YYMMDDHHMMSSZ (YY >= 50 ? 19YY : 20YY)
d847300… danield 232 ** GeneralizedTime: YYYYMMDDHHMMSSZ */
0d239b5… drh 233 if( ASN1_STRING_length(asn1_time) < 15 ){
d847300… danield 234 /* UTCTime, fill out century digits */
d847300… danield 235 *pr++ = pt[0]>='5' ? '1' : '2';
d847300… danield 236 *pr++ = pt[0]>='5' ? '9' : '0';
d847300… danield 237 }else{
d847300… danield 238 /* GeneralizedTime, copy century digits and advance source */
d847300… danield 239 *pr++ = pt[0]; *pr++ = pt[1];
d847300… danield 240 pt += 2;
d847300… danield 241 }
d847300… danield 242 *pr++ = pt[0]; *pr++ = pt[1]; *pr++ = '-';
d847300… danield 243 *pr++ = pt[2]; *pr++ = pt[3]; *pr++ = '-';
d847300… danield 244 *pr++ = pt[4]; *pr++ = pt[5]; *pr++ = ' ';
d847300… danield 245 *pr++ = pt[6]; *pr++ = pt[7]; *pr++ = ':';
d847300… danield 246 *pr++ = pt[8]; *pr++ = pt[9]; *pr++ = ':';
d847300… danield 247 *pr++ = pt[10]; *pr++ = pt[11]; *pr = '\0';
d847300… danield 248 return mprintf("%s%s", res, (showUtc ? " UTC" : ""));
d847300… danield 249 }
d847300… danield 250 }
d847300… danield 251
d847300… danield 252 /*
68fa646… drh 253 ** Call this routine once before any other use of the SSL interface.
68fa646… drh 254 ** This routine does initial configuration of the SSL module.
68fa646… drh 255 */
f6263bb… drh 256 static void ssl_global_init_client(void){
5eb8f01… drh 257 const char *identityFile;
cfb8d66… jan.nijtmans 258
68fa646… drh 259 if( sslIsInit==0 ){
e225dc9… drh 260 const char *zFile;
e225dc9… drh 261 const char *zCaFile = 0;
e225dc9… drh 262 const char *zCaDirectory = 0;
e225dc9… drh 263 int i;
e225dc9… drh 264
68fa646… drh 265 SSL_library_init();
68fa646… drh 266 SSL_load_error_strings();
cfb8d66… jan.nijtmans 267 OpenSSL_add_all_algorithms();
68fa646… drh 268 sslCtx = SSL_CTX_new(SSLv23_client_method());
7a44fdd… drh 269 /* Disable SSLv2 and SSLv3 */
7a44fdd… drh 270 SSL_CTX_set_options(sslCtx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3);
7a44fdd… drh 271
e225dc9… drh 272 /* Find the trust store */
e225dc9… drh 273 zFile = 0;
e225dc9… drh 274 for(i=0; zFile==0 && i<5; i++){
e225dc9… drh 275 switch( i ){
e2bdc10… danield 276 case 0: /* First priority is environment variables */
e225dc9… drh 277 zFile = fossil_getenv(X509_get_default_cert_file_env());
e225dc9… drh 278 break;
e225dc9… drh 279 case 1:
e225dc9… drh 280 zFile = fossil_getenv(X509_get_default_cert_dir_env());
e225dc9… drh 281 break;
e225dc9… drh 282 case 2:
0ca1fc4… drh 283 if( !g.repositoryOpen ) db_open_config(0,0);
e225dc9… drh 284 zFile = db_get("ssl-ca-location",0);
e225dc9… drh 285 break;
e225dc9… drh 286 case 3:
e225dc9… drh 287 zFile = X509_get_default_cert_file();
e225dc9… drh 288 break;
e225dc9… drh 289 case 4:
e225dc9… drh 290 zFile = X509_get_default_cert_dir();
e225dc9… drh 291 break;
e225dc9… drh 292 }
e225dc9… drh 293 if( zFile==0 ) continue;
e225dc9… drh 294 switch( file_isdir(zFile, ExtFILE) ){
9a0c995… drh 295 case 0: { /* doesn't exist */
e225dc9… drh 296 zFile = 0;
9a0c995… drh 297 break;
9a0c995… drh 298 }
9a0c995… drh 299 case 1: { /* directory */
e225dc9… drh 300 zCaFile = 0;
e225dc9… drh 301 zCaDirectory = zFile;
5eb8f01… drh 302 break;
5eb8f01… drh 303 }
5eb8f01… drh 304 case 2: { /* file */
e225dc9… drh 305 zCaFile = zFile;
e225dc9… drh 306 zCaDirectory = 0;
5eb8f01… drh 307 break;
5eb8f01… drh 308 }
5eb8f01… drh 309 }
e225dc9… drh 310 }
e225dc9… drh 311 if( zFile==0 ){
e225dc9… drh 312 /* fossil_fatal("Cannot find a trust store"); */
e225dc9… drh 313 }else if( SSL_CTX_load_verify_locations(sslCtx, zCaFile, zCaDirectory)==0 ){
e225dc9… drh 314 fossil_fatal("Cannot load CA root certificates from %s", zFile);
e225dc9… drh 315 }
0ca1fc4… drh 316
6fc64ab… florian 317 /* Enable OpenSSL to use the Windows system ROOT certificate store to search for
6fc64ab… florian 318 ** certificates missing in the file and directory trust stores already loaded by
6fc64ab… florian 319 ** `SSL_CTX_load_verify_locations()'.
6fc64ab… florian 320 ** This feature was introduced with OpenSSL 3.2.0, and may be enabled by default
6fc64ab… florian 321 ** for future versions of OpenSSL, and explicit initialization may be redundant.
6fc64ab… florian 322 ** NOTE TO HACKERS TWEAKING THEIR OPENSSL CONFIGURATION:
6fc64ab… florian 323 ** The following OpenSSL configuration options must not be used for this feature
6fc64ab… florian 324 ** to be available: `no-autoalginit', `no-winstore'. The Fossil makefiles do not
6fc64ab… florian 325 ** currently set these options when building OpenSSL for Windows. */
de6a059… florian 326 #if defined(_WIN32)
6fc64ab… florian 327 #if OPENSSL_VERSION_NUMBER >= 0x030200000
2b6ad00… drh 328 if( SSLeay()!=0x30500000 /* Don't use for 3.5.0 due to a bug */
2b6ad00… drh 329 && SSL_CTX_load_verify_store(sslCtx, "org.openssl.winstore:")==0
2b6ad00… drh 330 ){
5d993d5… florian 331 fossil_print("NOTICE: Failed to load the Windows root certificates.\n");
de6a059… florian 332 }
6fc64ab… florian 333 #endif /* OPENSSL_VERSION_NUMBER >= 0x030200000 */
6fc64ab… florian 334 #endif /* _WIN32 */
6fc64ab… florian 335
5eb8f01… drh 336 /* Load client SSL identity, preferring the filename specified on the
5eb8f01… drh 337 ** command line */
5eb8f01… drh 338 if( g.zSSLIdentity!=0 ){
5eb8f01… drh 339 identityFile = g.zSSLIdentity;
5eb8f01… drh 340 }else{
5eb8f01… drh 341 identityFile = db_get("ssl-identity", 0);
5eb8f01… drh 342 }
5eb8f01… drh 343 if( identityFile!=0 && identityFile[0]!='\0' ){
82b4294… drh 344 if( SSL_CTX_use_certificate_chain_file(sslCtx,identityFile)!=1
5eb8f01… drh 345 || SSL_CTX_use_PrivateKey_file(sslCtx,identityFile,SSL_FILETYPE_PEM)!=1
5eb8f01… drh 346 ){
31c7bdb… larrybr 347 fossil_fatal("Could not load SSL identity from %s", identityFile);
5eb8f01… drh 348 }
5eb8f01… drh 349 }
5eb8f01… drh 350 /* Register a callback to tell the user what to do when the server asks
5eb8f01… drh 351 ** for a cert */
5eb8f01… drh 352 SSL_CTX_set_client_cert_cb(sslCtx, ssl_client_cert_callback);
5eb8f01… drh 353
5eb8f01… drh 354 sslIsInit = 1;
f6263bb… drh 355 }else{
f6263bb… drh 356 assert( sslIsInit==1 );
68fa646… drh 357 }
68fa646… drh 358 }
68fa646… drh 359
68fa646… drh 360 /*
68fa646… drh 361 ** Call this routine to shutdown the SSL module prior to program exit.
68fa646… drh 362 */
68fa646… drh 363 void ssl_global_shutdown(void){
68fa646… drh 364 if( sslIsInit ){
68fa646… drh 365 SSL_CTX_free(sslCtx);
68fa646… drh 366 ssl_clear_errmsg();
68fa646… drh 367 sslIsInit = 0;
68fa646… drh 368 }
f39c525… andybradford 369 socket_global_shutdown();
68fa646… drh 370 }
68fa646… drh 371
68fa646… drh 372 /*
f6263bb… drh 373 ** Close the currently open client SSL connection. If no connection is open,
68fa646… drh 374 ** this routine is a no-op.
68fa646… drh 375 */
f6263bb… drh 376 void ssl_close_client(void){
68fa646… drh 377 if( iBio!=NULL ){
68fa646… drh 378 (void)BIO_reset(iBio);
68fa646… drh 379 BIO_free_all(iBio);
3a00b61… drh 380 iBio = NULL;
3a33435… jan.nijtmans 381 }
f39c525… andybradford 382 socket_close();
f39c525… andybradford 383 }
f39c525… andybradford 384
33118a3… danshearer 385 /*
33118a3… danshearer 386 ** Return the host name of the server that pUrlData connects to, without
33118a3… danshearer 387 ** a port number. When the connection goes through a proxy,
33118a3… danshearer 388 ** pUrlData->name holds the proxy hostname, and the server hostname is in
33118a3… danshearer 389 ** pUrlData->proxyOrigName.
33118a3… danshearer 390 */
33118a3… danshearer 391 static const char *ssl_server_name(const UrlData *pUrlData){
33118a3… danshearer 392 return pUrlData->useProxy ? pUrlData->proxyOrigName : pUrlData->name;
33118a3… danshearer 393 }
33118a3… danshearer 394
3a33435… jan.nijtmans 395 /* See RFC2817 for details */
3a33435… jan.nijtmans 396 static int establish_proxy_tunnel(UrlData *pUrlData, BIO *bio){
3a33435… jan.nijtmans 397 int rc, httpVerMin;
3a33435… jan.nijtmans 398 char *bbuf;
3a33435… jan.nijtmans 399 Blob snd, reply;
3a33435… jan.nijtmans 400 int done=0,end=0;
33118a3… danshearer 401 const char *zHost = ssl_server_name(pUrlData);
33118a3… danshearer 402 const char *zLb = strchr(zHost, ':') ? "[" : ""; /* IPv6 brackets */
33118a3… danshearer 403 const char *zRb = zLb[0] ? "]" : "";
3a33435… jan.nijtmans 404 blob_zero(&snd);
33118a3… danshearer 405 blob_appendf(&snd, "CONNECT %s%s%s:%d HTTP/1.1\r\n",
33118a3… danshearer 406 zLb, zHost, zRb, pUrlData->proxyOrigPort);
33118a3… danshearer 407 blob_appendf(&snd, "Host: %s%s%s:%d\r\n",
33118a3… danshearer 408 zLb, zHost, zRb, pUrlData->proxyOrigPort);
3a33435… jan.nijtmans 409 if( pUrlData->proxyAuth ){
3a33435… jan.nijtmans 410 blob_appendf(&snd, "Proxy-Authorization: %s\r\n", pUrlData->proxyAuth);
3a33435… jan.nijtmans 411 }
3a33435… jan.nijtmans 412 blob_append(&snd, "Proxy-Connection: keep-alive\r\n", -1);
a7a7df7… jan.nijtmans 413 blob_appendf(&snd, "User-Agent: %s\r\n", get_user_agent());
3a33435… jan.nijtmans 414 blob_append(&snd, "\r\n", 2);
3a33435… jan.nijtmans 415 BIO_write(bio, blob_buffer(&snd), blob_size(&snd));
3a33435… jan.nijtmans 416 blob_reset(&snd);
3a33435… jan.nijtmans 417
3a33435… jan.nijtmans 418 /* Wait for end of reply */
3a33435… jan.nijtmans 419 blob_zero(&reply);
3a33435… jan.nijtmans 420 do{
3a33435… jan.nijtmans 421 int len;
3a33435… jan.nijtmans 422 char buf[256];
3a33435… jan.nijtmans 423 len = BIO_read(bio, buf, sizeof(buf));
3a33435… jan.nijtmans 424 blob_append(&reply, buf, len);
3a33435… jan.nijtmans 425
3a33435… jan.nijtmans 426 bbuf = blob_buffer(&reply);
3a33435… jan.nijtmans 427 len = blob_size(&reply);
3a33435… jan.nijtmans 428 while(end < len) {
e8d328c… drh 429 if( bbuf[end]=='\n' ) {
e8d328c… drh 430 if( (end+1<len && bbuf[end+1]=='\n')
5a282cf… george 431 || (end+2<len && bbuf[end+1]=='\r' && bbuf[end+2]=='\n')
e8d328c… drh 432 ){
3a33435… jan.nijtmans 433 done = 1;
3a33435… jan.nijtmans 434 break;
3a33435… jan.nijtmans 435 }
3a33435… jan.nijtmans 436 }
3a33435… jan.nijtmans 437 end++;
3a33435… jan.nijtmans 438 }
3a33435… jan.nijtmans 439 }while(!done);
3a33435… jan.nijtmans 440 sscanf(bbuf, "HTTP/1.%d %d", &httpVerMin, &rc);
3a33435… jan.nijtmans 441 blob_reset(&reply);
3a33435… jan.nijtmans 442 return rc;
3a33435… jan.nijtmans 443 }
3a33435… jan.nijtmans 444
3a33435… jan.nijtmans 445 /*
3c194e2… drh 446 ** Invoke this routine to disable SSL cert verification. After
3c194e2… drh 447 ** this call is made, any SSL cert that the server provides will
3c194e2… drh 448 ** be accepted. Communication will still be encrypted, but the
3c194e2… drh 449 ** client has no way of knowing whether it is talking to the
3c194e2… drh 450 ** real server or a man-in-the-middle imposter.
3c194e2… drh 451 */
9f8dc18… drh 452 void ssl_disable_cert_verification(void){
275da70… danield 453 sslNoCertVerify = 1;
275da70… danield 454 }
275da70… danield 455
275da70… danield 456 /*
6753e74… danshearer 457 ** Return true if zHost is an IPv4 or IPv6 address. A certificate
6753e74… danshearer 458 ** lists an IP address separately from its DNS names, and RFC 6066 does not
6753e74… danshearer 459 ** permit an IP address as the TLS server name.
6753e74… danshearer 460 */
6753e74… danshearer 461 static int ssl_host_is_ip_address(const char *zHost){
6753e74… danshearer 462 ASN1_OCTET_STRING *pIp = a2i_IPADDRESS(zHost);
6753e74… danshearer 463 if( pIp==0 ){
6753e74… danshearer 464 ERR_clear_error();
6753e74… danshearer 465 return 0;
6753e74… danshearer 466 }
6753e74… danshearer 467 ASN1_OCTET_STRING_free(pIp);
6753e74… danshearer 468 return 1;
6753e74… danshearer 469 }
6753e74… danshearer 470
6753e74… danshearer 471 /*
6753e74… danshearer 472 ** Return true if zHost contains a byte outside ASCII. RFC 6066 permits
6753e74… danshearer 473 ** only an ASCII DNS name as the TLS server name, so an internationalized
6753e74… danshearer 474 ** domain name would have to be sent in its "xn--" form, but Fossil does not
6753e74… danshearer 475 ** use that form.
6753e74… danshearer 476 */
6753e74… danshearer 477 static int ssl_host_is_non_ascii(const char *zHost){
6753e74… danshearer 478 while( *zHost ){
6753e74… danshearer 479 if( (unsigned char)*zHost >= 0x80 ) return 1;
6753e74… danshearer 480 zHost++;
6753e74… danshearer 481 }
6753e74… danshearer 482 return 0;
6753e74… danshearer 483 }
6753e74… danshearer 484
6753e74… danshearer 485 /*
f6263bb… drh 486 ** Open an SSL connection as a client that is to connect to the server
f6263bb… drh 487 ** identified by pUrlData.
f6263bb… drh 488 **
f6263bb… drh 489 * The identify of the server is determined as follows:
68fa646… drh 490 **
09908ab… drh 491 ** pUrlData->name Name of the server. Ex: fossil-scm.org
455b2aa… drh 492 ** g.url.name Name of the proxy server, if proxying.
ca0a58f… jan.nijtmans 493 ** pUrlData->port TCP/IP port to use. Ex: 80
68fa646… drh 494 **
68fa646… drh 495 ** Return the number of errors.
68fa646… drh 496 */
f6263bb… drh 497 int ssl_open_client(UrlData *pUrlData){
074767b… drh 498 X509 *cert;
aaab2a1… drh 499 const char *zRemoteHost;
6362cc0… danshearer 500 const char *zTlsName; /* zRemoteHost needed by the TLS layer */
6362cc0… danshearer 501 char zDotless[256]; /* zRemoteHost without a trailing dot */
6753e74… danshearer 502 int isIpAddr; /* True if zRemoteHost is an IP address */
f39c525… andybradford 503 BIO *sBio;
3c194e2… drh 504
f6263bb… drh 505 ssl_global_init_client();
f39c525… andybradford 506 if( socket_open(pUrlData) ){
f39c525… andybradford 507 ssl_set_errmsg("SSL: cannot open socket (%s)", socket_errmsg());
f39c525… andybradford 508 return 1;
f39c525… andybradford 509 }
f39c525… andybradford 510 sBio = BIO_new_socket(socket_get_fd(), 0);
3a33435… jan.nijtmans 511 if( pUrlData->useProxy ){
f39c525… andybradford 512 int rc = establish_proxy_tunnel(pUrlData, sBio);
3a33435… jan.nijtmans 513 if( rc<200||rc>299 ){
3a33435… jan.nijtmans 514 ssl_set_errmsg("SSL: proxy connect failed with HTTP status code %d", rc);
f39c525… andybradford 515 ssl_close_client();
3a33435… jan.nijtmans 516 return 1;
3a33435… jan.nijtmans 517 }
3a33435… jan.nijtmans 518
3a33435… jan.nijtmans 519 pUrlData->path = pUrlData->proxyUrlPath;
f39c525… andybradford 520 }
f39c525… andybradford 521 iBio = BIO_new_ssl(sslCtx, 1);
f39c525… andybradford 522 BIO_push(iBio, sBio);
f39c525… andybradford 523 BIO_set_ssl(sBio, ssl, BIO_NOCLOSE);
f39c525… andybradford 524 BIO_set_ssl_mode(iBio, 1);
074767b… drh 525 BIO_get_ssl(iBio, &ssl);
074767b… drh 526
33118a3… danshearer 527 zRemoteHost = ssl_server_name(pUrlData);
6362cc0… danshearer 528 /* A fully qualified name may end with a dot, as in "example.com.".
6362cc0… danshearer 529 ** The RFC 6066 TLS server name and all certificate names are
6362cc0… danshearer 530 ** dotless */
6362cc0… danshearer 531 zTlsName = zRemoteHost;
6362cc0… danshearer 532 {
6362cc0… danshearer 533 size_t n = strlen(zRemoteHost);
6362cc0… danshearer 534 if( n>1 && zRemoteHost[n-1]=='.' && n<sizeof(zDotless) ){
6362cc0… danshearer 535 memcpy(zDotless, zRemoteHost, n-1);
6362cc0… danshearer 536 zDotless[n-1] = 0;
6362cc0… danshearer 537 zTlsName = zDotless;
6362cc0… danshearer 538 }
6362cc0… danshearer 539 }
6362cc0… danshearer 540 isIpAddr = ssl_host_is_ip_address(zTlsName);
f39c525… andybradford 541
cb52442… drh 542 #if (SSLEAY_VERSION_NUMBER >= 0x00908070) && !defined(OPENSSL_NO_TLSEXT)
6362cc0… danshearer 543 if( !isIpAddr && !ssl_host_is_non_ascii(zTlsName)
6362cc0… danshearer 544 && !SSL_set_tlsext_host_name(ssl, zTlsName)
6753e74… danshearer 545 ){
074767b… drh 546 fossil_warning("WARNING: failed to set server name indication (SNI), "
074767b… drh 547 "continuing without it.\n");
074767b… drh 548 }
cb52442… drh 549 #endif
074767b… drh 550
074767b… drh 551 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
aaab2a1… drh 552 #if OPENSSL_VERSION_NUMBER >= 0x010002000
aaab2a1… drh 553 if( !sslNoCertVerify ){
aaab2a1… drh 554 X509_VERIFY_PARAM *param = 0;
aaab2a1… drh 555 param = SSL_get0_param(ssl);
6753e74… danshearer 556 if( isIpAddr ){
6362cc0… danshearer 557 if( !X509_VERIFY_PARAM_set1_ip_asc(param, zTlsName) ){
6753e74… danshearer 558 fossil_fatal("failed to set IP address.");
6753e74… danshearer 559 }
6362cc0… danshearer 560 }else if( !X509_VERIFY_PARAM_set1_host(param, zTlsName,
6362cc0… danshearer 561 strlen(zTlsName)) ){
aaab2a1… drh 562 fossil_fatal("failed to set hostname.");
aaab2a1… drh 563 }
aaab2a1… drh 564 /* SSL_set_verify(ssl, SSL_VERIFY_PEER, 0); */
aaab2a1… drh 565 }
aaab2a1… drh 566 #endif
aaab2a1… drh 567
f39c525… andybradford 568 if( BIO_do_handshake(iBio)<=0 ){
cfb8d66… jan.nijtmans 569 ssl_set_errmsg("Error establishing SSL connection %s:%d (%s)",
f39c525… andybradford 570 zRemoteHost,
f39c525… andybradford 571 pUrlData->useProxy ? pUrlData->proxyOrigPort : pUrlData->port,
3a33435… jan.nijtmans 572 ERR_reason_error_string(ERR_get_error()));
f6263bb… drh 573 ssl_close_client();
68fa646… drh 574 return 1;
68fa646… drh 575 }
68fa646… drh 576 /* Check if certificate is valid */
68fa646… drh 577 cert = SSL_get_peer_certificate(ssl);
68fa646… drh 578
68fa646… drh 579 if ( cert==NULL ){
68fa646… drh 580 ssl_set_errmsg("No SSL certificate was presented by the peer");
f6263bb… drh 581 ssl_close_client();
3c194e2… drh 582 return 1;
3c194e2… drh 583 }
3c194e2… drh 584
5623188… danield 585 /* Debugging hint: On unix-like system, run something like:
5623188… danield 586 **
5623188… danield 587 ** SSL_CERT_DIR=/tmp ./fossil sync
5623188… danield 588 **
5623188… danield 589 ** to cause certificate validation to fail, and thus test the fallback
5623188… danield 590 ** logic.
5623188… danield 591 */
3c194e2… drh 592 if( !sslNoCertVerify && SSL_get_verify_result(ssl)!=X509_V_OK ){
82d177f… mistachkin 593 int x, desclen;
3c194e2… drh 594 char *desc, *prompt;
3c194e2… drh 595 Blob ans;
3c194e2… drh 596 char cReply;
3c194e2… drh 597 BIO *mem;
82d177f… mistachkin 598 unsigned char md[EVP_MAX_MD_SIZE];
82d177f… mistachkin 599 char zHash[EVP_MAX_MD_SIZE*2+1];
3c194e2… drh 600 unsigned int mdLength = (int)sizeof(md);
3c194e2… drh 601
3c194e2… drh 602 memset(md, 0, sizeof(md));
3c194e2… drh 603 zHash[0] = 0;
64d79ad… drh 604 /* MMNNFFPPS */
64d79ad… drh 605 #if OPENSSL_VERSION_NUMBER >= 0x010000000
64d79ad… drh 606 x = X509_digest(cert, EVP_sha256(), md, &mdLength);
64d79ad… drh 607 #else
64d79ad… drh 608 x = X509_digest(cert, EVP_sha1(), md, &mdLength);
64d79ad… drh 609 #endif
64d79ad… drh 610 if( x ){
53db40e… drh 611 unsigned j;
3c194e2… drh 612 for(j=0; j<mdLength && j*2+1<sizeof(zHash); ++j){
3c194e2… drh 613 zHash[j*2] = "0123456789abcdef"[md[j]>>4];
3c194e2… drh 614 zHash[j*2+1] = "0123456789abcdef"[md[j]&0xf];
3c194e2… drh 615 }
3c194e2… drh 616 zHash[j*2] = 0;
3c194e2… drh 617 }
3c194e2… drh 618
3c194e2… drh 619 if( ssl_certificate_exception_exists(pUrlData, zHash) ){
3c194e2… drh 620 /* Ignore the failure because an exception exists */
3c194e2… drh 621 ssl_one_time_exception(pUrlData, zHash);
3c194e2… drh 622 }else{
3c194e2… drh 623 /* Tell the user about the failure and ask what to do */
3c194e2… drh 624 mem = BIO_new(BIO_s_mem());
5623188… danield 625 BIO_puts(mem, " subject: ");
3c194e2… drh 626 X509_NAME_print_ex(mem, X509_get_subject_name(cert), 0, XN_FLAG_ONELINE);
5623188… danield 627 BIO_puts(mem, "\n issuer: ");
3c194e2… drh 628 X509_NAME_print_ex(mem, X509_get_issuer_name(cert), 0, XN_FLAG_ONELINE);
d847300… danield 629 BIO_printf(mem, "\n notBefore: %s",
d847300… danield 630 ssl_asn1time_to_iso8601(X509_get_notBefore(cert), 1));
d847300… danield 631 BIO_printf(mem, "\n notAfter: %s",
d847300… danield 632 ssl_asn1time_to_iso8601(X509_get_notAfter(cert), 1));
5623188… danield 633 BIO_printf(mem, "\n sha256: %s", zHash);
82d177f… mistachkin 634 desclen = BIO_get_mem_data(mem, &desc);
275da70… danield 635
82d177f… mistachkin 636 prompt = mprintf("Unable to verify SSL cert from %s\n%.*s\n"
48a860f… stephan 637 "accept this cert and continue (y/N/fingerprint)? ",
33118a3… danshearer 638 zRemoteHost, desclen, desc);
3c194e2… drh 639 BIO_free(mem);
275da70… danield 640
3c194e2… drh 641 prompt_user(prompt, &ans);
3c194e2… drh 642 free(prompt);
3c194e2… drh 643 cReply = blob_str(&ans)[0];
5623188… danield 644 if( cReply!='y' && cReply!='Y'
5623188… danield 645 && fossil_stricmp(blob_str(&ans),zHash)!=0
5623188… danield 646 ){
3c194e2… drh 647 X509_free(cert);
3c194e2… drh 648 ssl_set_errmsg("SSL cert declined");
f6263bb… drh 649 ssl_close_client();
48a860f… stephan 650 blob_reset(&ans);
3c194e2… drh 651 return 1;
3c194e2… drh 652 }
48a860f… stephan 653 blob_reset(&ans);
3c194e2… drh 654 ssl_one_time_exception(pUrlData, zHash);
3c194e2… drh 655 prompt_user("remember this exception (y/N)? ", &ans);
3c194e2… drh 656 cReply = blob_str(&ans)[0];
3c194e2… drh 657 if( cReply=='y' || cReply=='Y') {
edd280c… stephan 658 db_open_config(0,0);
3c194e2… drh 659 ssl_remember_certificate_exception(pUrlData, zHash);
3c194e2… drh 660 }
3c194e2… drh 661 blob_reset(&ans);
3c194e2… drh 662 }
8b9c933… drh 663 }
8b9c933… drh 664
68fa646… drh 665 X509_free(cert);
68fa646… drh 666 return 0;
68fa646… drh 667 }
68fa646… drh 668
68fa646… drh 669 /*
99a319b… wyoung 670 ** Remember that the cert with the given hash is acceptable for
33118a3… danshearer 671 ** use with the server that pUrlData connects to.
3c194e2… drh 672 */
3c194e2… drh 673 LOCAL void ssl_remember_certificate_exception(
3c194e2… drh 674 UrlData *pUrlData,
3c194e2… drh 675 const char *zHash
3c194e2… drh 676 ){
33118a3… danshearer 677 db_set_mprintf(zHash, 1, "cert:%s", ssl_server_name(pUrlData));
3c194e2… drh 678 }
3c194e2… drh 679
3c194e2… drh 680 /*
3c194e2… drh 681 ** Return true if the there exists a certificate exception for
33118a3… danshearer 682 ** the server that pUrlData connects to that matches the hash.
3c194e2… drh 683 */
3c194e2… drh 684 LOCAL int ssl_certificate_exception_exists(
3c194e2… drh 685 UrlData *pUrlData,
3c194e2… drh 686 const char *zHash
3c194e2… drh 687 ){
3c194e2… drh 688 char *zName, *zValue;
33118a3… danshearer 689 if( fossil_strcmp(sException.zHost,ssl_server_name(pUrlData))==0
3c194e2… drh 690 && fossil_strcmp(sException.zHash,zHash)==0
3c194e2… drh 691 ){
3c194e2… drh 692 return 1;
3c194e2… drh 693 }
33118a3… danshearer 694 zName = mprintf("cert:%s", ssl_server_name(pUrlData));
3c194e2… drh 695 zValue = db_get(zName,0);
3c194e2… drh 696 fossil_free(zName);
3c194e2… drh 697 return zValue!=0 && strcmp(zHash,zValue)==0;
3c194e2… drh 698 }
3c194e2… drh 699
3c194e2… drh 700 /*
3c194e2… drh 701 ** Remember zHash as an acceptable certificate for this session only.
3c194e2… drh 702 */
9f8dc18… drh 703 LOCAL void ssl_one_time_exception(
3c194e2… drh 704 UrlData *pUrlData,
3c194e2… drh 705 const char *zHash
3c194e2… drh 706 ){
3c194e2… drh 707 fossil_free(sException.zHost);
33118a3… danshearer 708 sException.zHost = fossil_strdup(ssl_server_name(pUrlData));
3c194e2… drh 709 fossil_free(sException.zHash);
9f8dc18… drh 710 sException.zHash = fossil_strdup(zHash);
3c194e2… drh 711 }
3c194e2… drh 712
3c194e2… drh 713 /*
f6263bb… drh 714 ** Send content out over the SSL connection from the client to
f6263bb… drh 715 ** the server.
68fa646… drh 716 */
68fa646… drh 717 size_t ssl_send(void *NotUsed, void *pContent, size_t N){
68fa646… drh 718 size_t total = 0;
68fa646… drh 719 while( N>0 ){
4abf607… mistachkin 720 int sent = BIO_write(iBio, pContent, N);
4abf607… mistachkin 721 if( sent<=0 ){
4abf607… mistachkin 722 if( BIO_should_retry(iBio) ){
4abf607… mistachkin 723 continue;
4abf607… mistachkin 724 }
4abf607… mistachkin 725 break;
4abf607… mistachkin 726 }
68fa646… drh 727 total += sent;
68fa646… drh 728 N -= sent;
68fa646… drh 729 pContent = (void*)&((char*)pContent)[sent];
68fa646… drh 730 }
68fa646… drh 731 return total;
68fa646… drh 732 }
68fa646… drh 733
68fa646… drh 734 /*
f6263bb… drh 735 ** Receive content back from the client SSL connection. In other
f6263bb… drh 736 ** words read the reply back from the server.
68fa646… drh 737 */
68fa646… drh 738 size_t ssl_receive(void *NotUsed, void *pContent, size_t N){
68fa646… drh 739 size_t total = 0;
68fa646… drh 740 while( N>0 ){
4abf607… mistachkin 741 int got = BIO_read(iBio, pContent, N);
4abf607… mistachkin 742 if( got<=0 ){
4abf607… mistachkin 743 if( BIO_should_retry(iBio) ){
4abf607… mistachkin 744 continue;
4abf607… mistachkin 745 }
4abf607… mistachkin 746 break;
4abf607… mistachkin 747 }
68fa646… drh 748 total += got;
68fa646… drh 749 N -= got;
68fa646… drh 750 pContent = (void*)&((char*)pContent)[got];
68fa646… drh 751 }
68fa646… drh 752 return total;
68fa646… drh 753 }
68fa646… drh 754
f6263bb… drh 755 /*
f6263bb… drh 756 ** Initialize the SSL library so that it is able to handle
f6263bb… drh 757 ** server-side connections. Invoke fossil_fatal() if there are
f6263bb… drh 758 ** any problems.
f6263bb… drh 759 **
f6263bb… drh 760 ** If zKeyFile and zCertFile are not NULL, then they are the names
f6263bb… drh 761 ** of disk files that hold the certificate and private-key for the
f6263bb… drh 762 ** server. If zCertFile is not NULL but zKeyFile is NULL, then
f6263bb… drh 763 ** zCertFile is assumed to be a concatenation of the certificate and
f6263bb… drh 764 ** the private-key in the PEM format.
f6263bb… drh 765 **
d7008b3… drh 766 ** If zCertFile is "unsafe-builtin", then a built-in self-signed cert
d7008b3… drh 767 ** is used. This built-in cert is insecure and should only be used for
d7008b3… drh 768 ** testing and debugging.
f6263bb… drh 769 */
f6263bb… drh 770 void ssl_init_server(const char *zCertFile, const char *zKeyFile){
d7008b3… drh 771 if( sslIsInit==0 && zCertFile ){
f6263bb… drh 772 SSL_library_init();
f6263bb… drh 773 SSL_load_error_strings();
f6263bb… drh 774 OpenSSL_add_all_algorithms();
f6263bb… drh 775 sslCtx = SSL_CTX_new(SSLv23_server_method());
f6263bb… drh 776 if( sslCtx==0 ){
f6263bb… drh 777 ERR_print_errors_fp(stderr);
f6263bb… drh 778 fossil_fatal("Error initializing the SSL server");
f6263bb… drh 779 }
d7008b3… drh 780 if( fossil_strcmp(zCertFile,"unsafe-builtin")==0 ){
d7008b3… drh 781 if( sslctx_use_cert_from_mem(sslCtx, sslSelfCert, -1)
d7008b3… drh 782 || sslctx_use_pkey_from_mem(sslCtx, sslSelfPKey, -1)
d7008b3… drh 783 ){
d7008b3… drh 784 fossil_fatal("Error loading self-signed CERT and KEY");
d7008b3… drh 785 }
d7008b3… drh 786 }else{
82c62e5… stephan 787 if( SSL_CTX_use_certificate_chain_file(sslCtx,zCertFile)!=1 ){
f6263bb… drh 788 ERR_print_errors_fp(stderr);
f6263bb… drh 789 fossil_fatal("Error loading CERT file \"%s\"", zCertFile);
f6263bb… drh 790 }
f6263bb… drh 791 if( zKeyFile==0 ) zKeyFile = zCertFile;
f6263bb… drh 792 if( SSL_CTX_use_PrivateKey_file(sslCtx, zKeyFile, SSL_FILETYPE_PEM)<=0 ){
f6263bb… drh 793 ERR_print_errors_fp(stderr);
d7008b3… drh 794 if( strcmp(zKeyFile,zCertFile)==0 ){
d7008b3… drh 795 fossil_fatal("The private key is not found in \"%s\". "
d7008b3… drh 796 "Either append the private key to the certification in that "
d7008b3… drh 797 "file or use a separate --pkey option to specify the private key.",
d7008b3… drh 798 zKeyFile);
d7008b3… drh 799 }else{
d7008b3… drh 800 fossil_fatal("Error loading the private key from file \"%s\"",
d7008b3… drh 801 zKeyFile);
d7008b3… drh 802 }
d7008b3… drh 803 }
f6263bb… drh 804 }
f6263bb… drh 805 if( !SSL_CTX_check_private_key(sslCtx) ){
f6263bb… drh 806 fossil_fatal("PRIVATE KEY \"%s\" does not match CERT \"%s\"",
f6263bb… drh 807 zKeyFile, zCertFile);
f6263bb… drh 808 }
7a3bf55… drh 809 SSL_CTX_set_mode(sslCtx, SSL_MODE_AUTO_RETRY);
f6263bb… drh 810 sslIsInit = 2;
f6263bb… drh 811 }else{
f6263bb… drh 812 assert( sslIsInit==2 );
f6263bb… drh 813 }
f6263bb… drh 814 }
f6263bb… drh 815
f6263bb… drh 816 typedef struct SslServerConn {
f6263bb… drh 817 SSL *ssl; /* The SSL codec */
7a3bf55… drh 818 int iSocket; /* The socket */
8d456a5… stephan 819 BIO *bio; /* BIO object. Needed for EOF detection. */
f6263bb… drh 820 } SslServerConn;
f6263bb… drh 821
f6263bb… drh 822 /*
2584796… stephan 823 ** Create a new server-side codec. The argument is the socket's file
2584796… stephan 824 ** descriptor from which the codec reads and writes. The returned
2584796… stephan 825 ** memory must eventually be passed to ssl_close_server().
f6263bb… drh 826 */
7a3bf55… drh 827 void *ssl_new_server(int iSocket){
f6263bb… drh 828 SslServerConn *pServer = fossil_malloc_zero(sizeof(*pServer));
7a3bf55… drh 829 BIO *b = BIO_new_socket(iSocket, 0);
f6263bb… drh 830 pServer->ssl = SSL_new(sslCtx);
7a3bf55… drh 831 pServer->iSocket = iSocket;
8d456a5… stephan 832 pServer->bio = b;
7a3bf55… drh 833 SSL_set_bio(pServer->ssl, b, b);
f6263bb… drh 834 SSL_accept(pServer->ssl);
f6263bb… drh 835 return (void*)pServer;
f6263bb… drh 836 }
f6263bb… drh 837
f6263bb… drh 838 /*
f6263bb… drh 839 ** Close a server-side code previously returned from ssl_new_server().
f6263bb… drh 840 */
f6263bb… drh 841 void ssl_close_server(void *pServerArg){
f6263bb… drh 842 SslServerConn *pServer = (SslServerConn*)pServerArg;
f6263bb… drh 843 SSL_free(pServer->ssl);
f6263bb… drh 844 fossil_free(pServer);
f6263bb… drh 845 }
f6263bb… drh 846
f6263bb… drh 847 /*
f6263bb… drh 848 ** Return TRUE if there are no more bytes available to be read from
f6263bb… drh 849 ** the client.
f6263bb… drh 850 */
f6263bb… drh 851 int ssl_eof(void *pServerArg){
f6263bb… drh 852 SslServerConn *pServer = (SslServerConn*)pServerArg;
06e300e… stephan 853 return BIO_eof(pServer->bio);
f6263bb… drh 854 }
f6263bb… drh 855
f6263bb… drh 856 /*
f6263bb… drh 857 ** Read cleartext bytes that have been received from the client and
f6263bb… drh 858 ** decrypted by the SSL server codec.
b0834be… stephan 859 **
b0834be… stephan 860 ** If the expected payload size unknown, i.e. if the HTTP
b0834be… stephan 861 ** Content-Length: header field has not been parsed, the doLoop
b0834be… stephan 862 ** argument should be 0, or SSL_read() may block and wait for more
b0834be… stephan 863 ** data than is eventually going to arrive (on Windows). On
b0834be… stephan 864 ** non-Windows builds, it has been our experience that the final
b0834be… stephan 865 ** argument must always be true, as discussed at length at:
b0834be… stephan 866 **
b0834be… stephan 867 ** https://fossil-scm.org/forum/forumpost/2f818850abb72719
f6263bb… drh 868 */
b0834be… stephan 869 size_t ssl_read_server(void *pServerArg, char *zBuf, size_t nBuf, int doLoop){
b890451… stephan 870 int n;
8d456a5… stephan 871 size_t rc = 0;
f6263bb… drh 872 SslServerConn *pServer = (SslServerConn*)pServerArg;
f6263bb… drh 873 if( nBuf>0x7fffffff ){ fossil_fatal("SSL read too big"); }
b0834be… stephan 874 while( nBuf!=rc && BIO_eof(pServer->bio)==0 ){
8d456a5… stephan 875 n = SSL_read(pServer->ssl, zBuf + rc, (int)(nBuf - rc));
b0834be… stephan 876 if( n>0 ){
b70557f… stephan 877 rc += n;
acffc8f… stephan 878 }
b0834be… stephan 879 if( doLoop==0 || n<=0 ){
b0834be… stephan 880 break;
b0834be… stephan 881 }
8d456a5… stephan 882 }
8d456a5… stephan 883 return rc;
f6263bb… drh 884 }
f6263bb… drh 885
f6263bb… drh 886 /*
b0834be… stephan 887 ** Read a single line of text from the client, up to nBuf-1 bytes. On
b0834be… stephan 888 ** success, writes nBuf-1 bytes to zBuf and NUL-terminates zBuf.
b0834be… stephan 889 ** Returns NULL on an I/O error or at EOF.
f6263bb… drh 890 */
f6263bb… drh 891 char *ssl_gets(void *pServerArg, char *zBuf, int nBuf){
f6263bb… drh 892 int n = 0;
f6263bb… drh 893 int i;
f6263bb… drh 894 SslServerConn *pServer = (SslServerConn*)pServerArg;
06e300e… stephan 895
06e300e… stephan 896 if( BIO_eof(pServer->bio) ) return 0;
f6263bb… drh 897 for(i=0; i<nBuf-1; i++){
f6263bb… drh 898 n = SSL_read(pServer->ssl, &zBuf[i], 1);
f6263bb… drh 899 if( n<=0 ){
f6263bb… drh 900 return 0;
f6263bb… drh 901 }
f6263bb… drh 902 if( zBuf[i]=='\n' ) break;
f6263bb… drh 903 }
f6263bb… drh 904 zBuf[i+1] = 0;
f6263bb… drh 905 return zBuf;
f6263bb… drh 906 }
f6263bb… drh 907
f6263bb… drh 908
f6263bb… drh 909 /*
f6263bb… drh 910 ** Write cleartext bytes into the SSL server codec so that they can
f6263bb… drh 911 ** be encrypted and sent back to the client.
f6263bb… drh 912 */
f6263bb… drh 913 size_t ssl_write_server(void *pServerArg, char *zBuf, size_t nBuf){
f6263bb… drh 914 int n;
f6263bb… drh 915 SslServerConn *pServer = (SslServerConn*)pServerArg;
7a3bf55… drh 916 if( nBuf<=0 ) return 0;
f6263bb… drh 917 if( nBuf>0x7fffffff ){ fossil_fatal("SSL write too big"); }
f6263bb… drh 918 n = SSL_write(pServer->ssl, zBuf, (int)nBuf);
7a3bf55… drh 919 if( n<=0 ){
7a3bf55… drh 920 return -SSL_get_error(pServer->ssl, n);
7a3bf55… drh 921 }else{
7a3bf55… drh 922 return n;
7a3bf55… drh 923 }
f6263bb… drh 924 }
f6263bb… drh 925
7a3bf55… drh 926 #endif /* FOSSIL_ENABLE_SSL */
7a3bf55… drh 927
2a563d6… drh 928 #ifdef FOSSIL_ENABLE_SSL
0abeed1… drh 929 /*
0abeed1… drh 930 ** zPath is a name that might be a file or directory containing a trust
0abeed1… drh 931 ** store. *pzStore is the name of the trust store to actually use.
0abeed1… drh 932 **
0abeed1… drh 933 ** If *pzStore is not NULL (meaning no trust store has been found yet)
0abeed1… drh 934 ** and if zPath exists, then set *pzStore to point to zPath.
0abeed1… drh 935 */
0abeed1… drh 936 static void trust_location_usable(const char *zPath, const char **pzStore){
0abeed1… drh 937 if( *pzStore!=0 ) return;
0abeed1… drh 938 if( file_isdir(zPath, ExtFILE)>0 ) *pzStore = zPath;
0abeed1… drh 939 }
2a563d6… drh 940 #endif /* FOSSIL_ENABLE_SSL */
0abeed1… drh 941
bc23620… drh 942 /*
3e7c7e2… drh 943 ** COMMAND: tls-config* abbrv-subcom
3e7c7e2… drh 944 ** COMMAND: ssl-config abbrv-subcom
bc23620… drh 945 **
f6263bb… drh 946 ** Usage: %fossil ssl-config [SUBCOMMAND] [OPTIONS...] [ARGS...]
bc23620… drh 947 **
bc23620… drh 948 ** This command is used to view or modify the TLS (Transport Layer
bc23620… drh 949 ** Security) configuration for Fossil. TLS (formerly SSL) is the
bc23620… drh 950 ** encryption technology used for secure HTTPS transport.
bc23620… drh 951 **
bc23620… drh 952 ** Sub-commands:
bc23620… drh 953 **
62cb8ea… drh 954 ** remove-exception DOMAINS Remove TLS cert exceptions for the domains
62cb8ea… drh 955 ** listed. Or remove them all if the --all
62cb8ea… drh 956 ** option is specified.
62cb8ea… drh 957 **
62cb8ea… drh 958 ** scrub ?--force? Remove all SSL configuration data from the
62cb8ea… drh 959 ** repository. Use --force to omit the
62cb8ea… drh 960 ** confirmation.
62cb8ea… drh 961 **
62cb8ea… drh 962 ** show ?-v? Show the TLS configuration. Add -v to see
62cb8ea… drh 963 ** additional explanation
bc23620… drh 964 */
bc23620… drh 965 void test_tlsconfig_info(void){
bc23620… drh 966 const char *zCmd;
bc23620… drh 967 size_t nCmd;
bc23620… drh 968 int nHit = 0;
0abeed1… drh 969
bc23620… drh 970 db_find_and_open_repository(OPEN_OK_NOT_FOUND|OPEN_SUBSTITUTE,0);
bc23620… drh 971 db_open_config(1,0);
f6263bb… drh 972 if( g.argc==2 || (g.argc>=3 && g.argv[2][0]=='-') ){
f6263bb… drh 973 zCmd = "show";
f6263bb… drh 974 nCmd = 4;
f6263bb… drh 975 }else{
f6263bb… drh 976 zCmd = g.argv[2];
f6263bb… drh 977 nCmd = strlen(zCmd);
f6263bb… drh 978 }
f6263bb… drh 979 if( strncmp("scrub",zCmd,nCmd)==0 && nCmd>4 ){
f6263bb… drh 980 int bForce = find_option("force","f",0)!=0;
f6263bb… drh 981 verify_all_options();
f6263bb… drh 982 if( !bForce ){
f6263bb… drh 983 Blob ans;
f6263bb… drh 984 char cReply;
f6263bb… drh 985 prompt_user(
f6263bb… drh 986 "Scrubbing the SSL configuration will permanently delete information.\n"
f6263bb… drh 987 "Changes cannot be undone. Continue (y/N)? ", &ans);
f6263bb… drh 988 cReply = blob_str(&ans)[0];
f6263bb… drh 989 if( cReply!='y' && cReply!='Y' ){
f6263bb… drh 990 fossil_exit(1);
f6263bb… drh 991 }
f6263bb… drh 992 }
f6263bb… drh 993 db_unprotect(PROTECT_ALL);
f6263bb… drh 994 db_multi_exec(
f6263bb… drh 995 "PRAGMA secure_delete=ON;"
f6263bb… drh 996 "DELETE FROM config WHERE name GLOB 'ssl-*';"
f6263bb… drh 997 );
f6263bb… drh 998 db_protect_pop();
f6263bb… drh 999 }else
bc23620… drh 1000 if( strncmp("show",zCmd,nCmd)==0 ){
c1350c8… drh 1001 #if defined(FOSSIL_ENABLE_SSL)
bc23620… drh 1002 const char *zName, *zValue;
0abeed1… drh 1003 const char *zUsed = 0; /* Trust store location actually used */
bc23620… drh 1004 size_t nName;
317c665… drh 1005 #endif
bc23620… drh 1006 Stmt q;
f6263bb… drh 1007 int verbose = find_option("verbose","v",0)!=0;
f6263bb… drh 1008 verify_all_options();
f6263bb… drh 1009
f6263bb… drh 1010 #if !defined(FOSSIL_ENABLE_SSL)
0abeed1… drh 1011 fossil_print("OpenSSL-version: (none)\n");
f6263bb… drh 1012 if( verbose ){
f6263bb… drh 1013 fossil_print("\n"
f6263bb… drh 1014 " The OpenSSL library is not used by this build of Fossil\n\n"
f6263bb… drh 1015 );
f6263bb… drh 1016 }
f6263bb… drh 1017 #else
2b6ad00… drh 1018 fossil_print("OpenSSL-version: %s (0x%09llx)\n",
2b6ad00… drh 1019 SSLeay_version(SSLEAY_VERSION), (unsigned long long)SSLeay());
f6263bb… drh 1020 if( verbose ){
f6263bb… drh 1021 fossil_print("\n"
f6263bb… drh 1022 " The version of the OpenSSL library being used\n"
f6263bb… drh 1023 " by this instance of Fossil. Version 3.0.0 or\n"
f6263bb… drh 1024 " later is recommended.\n\n"
f6263bb… drh 1025 );
f6263bb… drh 1026 }
f6263bb… drh 1027
0abeed1… drh 1028 fossil_print("Trust store location\n");
bc23620… drh 1029 zName = X509_get_default_cert_file_env();
bc23620… drh 1030 zValue = fossil_getenv(zName);
bc23620… drh 1031 if( zValue==0 ) zValue = "";
0abeed1… drh 1032 trust_location_usable(zValue, &zUsed);
bc23620… drh 1033 nName = strlen(zName);
0abeed1… drh 1034 fossil_print(" %s:%*s%s\n", zName, 19-nName, "", zValue);
bc23620… drh 1035 zName = X509_get_default_cert_dir_env();
bc23620… drh 1036 zValue = fossil_getenv(zName);
bc23620… drh 1037 if( zValue==0 ) zValue = "";
0abeed1… drh 1038 trust_location_usable(zValue, &zUsed);
0abeed1… drh 1039 nName = strlen(zName);
0abeed1… drh 1040 fossil_print(" %s:%*s%s\n", zName, 19-nName, "", zValue);
0abeed1… drh 1041 if( verbose ){
0abeed1… drh 1042 fossil_print("\n"
0abeed1… drh 1043 " Environment variables that determine alternative locations for\n"
0abeed1… drh 1044 " the root certificates used by Fossil when it is acting as a SSL\n"
e225dc9… drh 1045 " client. If specified, these alternative locations take top\n"
e225dc9… drh 1046 " priority.\n\n"
e225dc9… drh 1047 );
e225dc9… drh 1048 }
e225dc9… drh 1049
e225dc9… drh 1050 zValue = db_get("ssl-ca-location","");
e225dc9… drh 1051 trust_location_usable(zValue, &zUsed);
e225dc9… drh 1052 fossil_print(" ssl-ca-location: %s\n", zValue);
e225dc9… drh 1053 if( verbose ){
e225dc9… drh 1054 fossil_print("\n"
e225dc9… drh 1055 " This setting is the name of a file or directory that contains\n"
e225dc9… drh 1056 " the complete set of root certificates used by Fossil when it\n"
e225dc9… drh 1057 " is acting as a SSL client. If defined, this setting takes\n"
e225dc9… drh 1058 " priority over built-in paths.\n\n"
0abeed1… drh 1059 );
0abeed1… drh 1060 }
e225dc9… drh 1061
0abeed1… drh 1062
0abeed1… drh 1063 zValue = X509_get_default_cert_file();
0abeed1… drh 1064 trust_location_usable(zValue, &zUsed);
0abeed1… drh 1065 fossil_print(" OpenSSL-cert-file: %s\n", zValue);
0abeed1… drh 1066 zValue = X509_get_default_cert_dir();
0abeed1… drh 1067 trust_location_usable(zValue, &zUsed);
0abeed1… drh 1068 fossil_print(" OpenSSL-cert-dir: %s\n", X509_get_default_cert_dir());
0abeed1… drh 1069 if( verbose ){
0abeed1… drh 1070 fossil_print("\n"
0abeed1… drh 1071 " The default locations for the set of root certificates\n"
0abeed1… drh 1072 " used by the \"fossil sync\" and similar commands to verify\n"
0abeed1… drh 1073 " the identity of servers for \"https:\" URLs. These values\n"
0abeed1… drh 1074 " come into play when Fossil is used as a TLS client. These\n"
0abeed1… drh 1075 " values are built into your OpenSSL library.\n\n"
0abeed1… drh 1076 );
0abeed1… drh 1077 }
6fc64ab… florian 1078
de6a059… florian 1079 #if defined(_WIN32)
2b6ad00… drh 1080 fossil_print(" OpenSSL-winstore: %s\n",
2b6ad00… drh 1081 (SSLeay()>=0x30200000 && SSLeay()!=0x30500000) ? "Yes" : "No");
6fc64ab… florian 1082 if( verbose ){
6fc64ab… florian 1083 fossil_print("\n"
2b6ad00… drh 1084 " OpenSSL 3.2.0, or newer, but not version 3.5.0 due to a bug,\n"
a9b075a… florian 1085 " are able to use the root certificates managed by the Windows\n"
a9b075a… florian 1086 " operating system. The installed root certificates are listed\n"
a9b075a… florian 1087 " by the command:\n\n"
de6a059… florian 1088 " certutil -store \"ROOT\"\n\n"
6fc64ab… florian 1089 );
6fc64ab… florian 1090 }
6fc64ab… florian 1091 #endif /* _WIN32 */
0abeed1… drh 1092
0abeed1… drh 1093 if( zUsed==0 ) zUsed = "";
0abeed1… drh 1094 fossil_print(" Trust store used: %s\n", zUsed);
0abeed1… drh 1095 if( verbose ){
0abeed1… drh 1096 fossil_print("\n"
0abeed1… drh 1097 " The location that is actually used for the root certificates\n"
0abeed1… drh 1098 " used to verify the identity of servers for \"https:\" URLs.\n"
0abeed1… drh 1099 " This will be one of the first of the five locations listed\n"
0abeed1… drh 1100 " above that actually exists.\n\n"
0abeed1… drh 1101 );
0abeed1… drh 1102 }
0abeed1… drh 1103
0abeed1… drh 1104
0abeed1… drh 1105 #endif /* FOSSIL_ENABLE_SSL */
0abeed1… drh 1106
0abeed1… drh 1107
0abeed1… drh 1108 fossil_print("ssl-identity: %s\n", db_get("ssl-identity",""));
d7008b3… drh 1109 if( verbose ){
d7008b3… drh 1110 fossil_print("\n"
d7008b3… drh 1111 " This setting is the name of a file that contains the PEM-format\n"
d7008b3… drh 1112 " certificate and private-key used by Fossil clients to authenticate\n"
d7008b3… drh 1113 " with servers. Few servers actually require this, so this setting\n"
d7008b3… drh 1114 " is usually blank.\n\n"
f6263bb… drh 1115 );
f6263bb… drh 1116 }
f6263bb… drh 1117
bc23620… drh 1118 db_prepare(&q,
0abeed1… drh 1119 "SELECT name, '', value FROM global_config"
bc23620… drh 1120 " WHERE name GLOB 'cert:*'"
bc23620… drh 1121 "UNION ALL "
0abeed1… drh 1122 "SELECT name, date(mtime,'unixepoch'), value FROM config"
bc23620… drh 1123 " WHERE name GLOB 'cert:*'"
bc23620… drh 1124 " ORDER BY name"
bc23620… drh 1125 );
f6263bb… drh 1126 nHit = 0;
bc23620… drh 1127 while( db_step(&q)==SQLITE_ROW ){
0abeed1… drh 1128 /* 123456789 123456789 123456789 */
0abeed1… drh 1129 if( verbose ){
0abeed1… drh 1130 fossil_print("exception: %-40s %s\n"
0abeed1… drh 1131 " hash: %.57s\n",
0abeed1… drh 1132 db_column_text(&q,0)+5, db_column_text(&q,1),
0abeed1… drh 1133 db_column_text(&q,2));
0abeed1… drh 1134 }else{
0abeed1… drh 1135 fossil_print("exception: %-40s %s\n",
0abeed1… drh 1136 db_column_text(&q,0)+5, db_column_text(&q,1));
0abeed1… drh 1137 }
f6263bb… drh 1138 nHit++;
bc23620… drh 1139 }
bc23620… drh 1140 db_finalize(&q);
f6263bb… drh 1141 if( nHit && verbose ){
f6263bb… drh 1142 fossil_print("\n"
f6263bb… drh 1143 " The exceptions are server certificates that the Fossil client\n"
f6263bb… drh 1144 " is unable to verify using root certificates, but which should be\n"
f6263bb… drh 1145 " accepted anyhow.\n\n"
f6263bb… drh 1146 );
f6263bb… drh 1147 }
f6263bb… drh 1148
bc23620… drh 1149 }else
bc23620… drh 1150 if( strncmp("remove-exception",zCmd,nCmd)==0 ){
bc23620… drh 1151 int i;
bc23620… drh 1152 Blob sql;
bc23620… drh 1153 char *zSep = "(";
bc23620… drh 1154 db_begin_transaction();
bc23620… drh 1155 blob_init(&sql, 0, 0);
bc23620… drh 1156 if( g.argc==4 && find_option("all",0,0)!=0 ){
bc23620… drh 1157 blob_append_sql(&sql,
bc23620… drh 1158 "DELETE FROM global_config WHERE name GLOB 'cert:*';\n"
bc23620… drh 1159 "DELETE FROM global_config WHERE name GLOB 'trusted:*';\n"
bc23620… drh 1160 "DELETE FROM config WHERE name GLOB 'cert:*';\n"
bc23620… drh 1161 "DELETE FROM config WHERE name GLOB 'trusted:*';\n"
bc23620… drh 1162 );
bc23620… drh 1163 }else{
bc23620… drh 1164 if( g.argc<4 ){
bc23620… drh 1165 usage("remove-exception DOMAIN-NAME ...");
bc23620… drh 1166 }
bc23620… drh 1167 blob_append_sql(&sql,"DELETE FROM global_config WHERE name IN ");
bc23620… drh 1168 for(i=3; i<g.argc; i++){
bc23620… drh 1169 blob_append_sql(&sql,"%s'cert:%q','trust:%q'",
bc23620… drh 1170 zSep/*safe-for-%s*/, g.argv[i], g.argv[i]);
bc23620… drh 1171 zSep = ",";
bc23620… drh 1172 }
bc23620… drh 1173 blob_append_sql(&sql,");\n");
bc23620… drh 1174 zSep = "(";
bc23620… drh 1175 blob_append_sql(&sql,"DELETE FROM config WHERE name IN ");
bc23620… drh 1176 for(i=3; i<g.argc; i++){
bc23620… drh 1177 blob_append_sql(&sql,"%s'cert:%q','trusted:%q'",
bc23620… drh 1178 zSep/*safe-for-%s*/, g.argv[i], g.argv[i]);
bc23620… drh 1179 zSep = ",";
bc23620… drh 1180 }
bc23620… drh 1181 blob_append_sql(&sql,");");
bc23620… drh 1182 }
156c890… drh 1183 db_unprotect(PROTECT_CONFIG);
bc23620… drh 1184 db_exec_sql(blob_str(&sql));
156c890… drh 1185 db_protect_pop();
bc23620… drh 1186 db_commit_transaction();
bc23620… drh 1187 blob_reset(&sql);
bc23620… drh 1188 }else
bc23620… drh 1189 /*default*/{
bc23620… drh 1190 fossil_fatal("unknown sub-command \"%s\".\nshould be one of:"
d7008b3… drh 1191 " remove-exception scrub show",
bc23620… drh 1192 zCmd);
bc23620… drh 1193 }
f6263bb… drh 1194 }
f6263bb… drh 1195
f6263bb… drh 1196 /*
f6263bb… drh 1197 ** WEBPAGE: .well-known
f6263bb… drh 1198 **
f6263bb… drh 1199 ** If the "--acme" option was supplied to "fossil server" or "fossil http" or
f6263bb… drh 1200 ** similar, then this page returns the content of files found in the
f6263bb… drh 1201 ** ".well-known" subdirectory of the same directory that contains the
f6263bb… drh 1202 ** repository file. This facilitates Automated Certificate
f6263bb… drh 1203 ** Management using tools like "certbot".
f6263bb… drh 1204 **
f6263bb… drh 1205 ** The content is returned directly, without any interpretation, using
f6263bb… drh 1206 ** a generic mimetype.
f6263bb… drh 1207 */
f6263bb… drh 1208 void wellknown_page(void){
f6263bb… drh 1209 char *zPath = 0;
f6263bb… drh 1210 const char *zTail = P("name");
f6263bb… drh 1211 Blob content;
f6263bb… drh 1212 int i;
f6263bb… drh 1213 char c;
f6263bb… drh 1214 if( !g.fAllowACME ) goto wellknown_notfound;
f6263bb… drh 1215 if( g.zRepositoryName==0 ) goto wellknown_notfound;
f6263bb… drh 1216 if( zTail==0 ) goto wellknown_notfound;
f6263bb… drh 1217 zPath = mprintf("%z/.well-known/%s", file_dirname(g.zRepositoryName), zTail);
f6263bb… drh 1218 for(i=0; (c = zTail[i])!=0; i++){
f6263bb… drh 1219 if( fossil_isalnum(c) ) continue;
f6263bb… drh 1220 if( c=='.' ){
f6263bb… drh 1221 if( i==0 || zTail[i-1]=='/' || zTail[i-1]=='.' ) goto wellknown_notfound;
f6263bb… drh 1222 continue;
f6263bb… drh 1223 }
f6263bb… drh 1224 if( c==',' || c!='-' || c=='/' || c==':' || c=='_' || c=='~' ) continue;
f6263bb… drh 1225 goto wellknown_notfound;
f6263bb… drh 1226 }
f6263bb… drh 1227 if( strstr("/..", zPath)!=0 ) goto wellknown_notfound;
f6263bb… drh 1228 if( !file_isfile(zPath, ExtFILE) ) goto wellknown_notfound;
f6263bb… drh 1229 blob_read_from_file(&content, zPath, ExtFILE);
f6263bb… drh 1230 cgi_set_content(&content);
f6263bb… drh 1231 cgi_set_content_type(mimetype_from_name(zPath));
f6263bb… drh 1232 cgi_reply();
f6263bb… drh 1233 return;
f6263bb… drh 1234
f6263bb… drh 1235 wellknown_notfound:
f6263bb… drh 1236 fossil_free(zPath);
2afff83… drh 1237 webpage_notfound_error(0 /*works-like:""*/);
f6263bb… drh 1238 return;
bb09ff8… drh 1239 }
bb09ff8… drh 1240
bb09ff8… drh 1241 /*
bb09ff8… drh 1242 ** Return the OpenSSL version number being used. Space to hold
bb09ff8… drh 1243 ** this name is obtained from fossil_malloc() and should be
bb09ff8… drh 1244 ** freed by the caller.
bb09ff8… drh 1245 */
bb09ff8… drh 1246 char *fossil_openssl_version(void){
275da70… danield 1247 #if defined(FOSSIL_ENABLE_SSL)
bb09ff8… drh 1248 return mprintf("%s (0x%09x)\n",
2b6ad00… drh 1249 SSLeay_version(SSLEAY_VERSION), (sqlite3_uint64)SSLeay());
bb09ff8… drh 1250 #else
bb09ff8… drh 1251 return mprintf("none");
bb09ff8… drh 1252 #endif
67147dd… drh 1253 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button