Hex Artifact Content
Not logged in

Artifact 77d02aa7e4338b8cbee00c1a7b072d47a79bfd38:

File src/http_ssl.c part of check-in [16f6fd904a] - Add SSL support. by dmitry on 2009-11-09 15:32:32.

0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20  /*.** Copyright 
0010: 28 63 29 20 32 30 30 39 20 44 2e 20 52 69 63 68  (c) 2009 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54  ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66  his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f  ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75  u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20  te it and/or.** 
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20  modify it under 
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65  the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62   GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76  lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c  ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65  ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73  s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20  tributed in the 
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c  hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20  l be useful,.** 
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20  but WITHOUT ANY 
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75  WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69  t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a  ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54  * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52  Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55   A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20  RPOSE.  See the 
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50  GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f  ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a  r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c  ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20  d have received 
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e  a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63  U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e  .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72  g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69  ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65  te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20  ation, Inc., 59 
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53  Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73  uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31  ton, MA  02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20  307, USA..**.** 
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69  Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20  nformation:.**  
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a   drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68  *   http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a  waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
0370: 20 6d 61 6e 61 67 65 73 20 6c 6f 77 2d 6c 65 76   manages low-lev
0380: 65 6c 20 53 53 4c 20 63 6f 6d 6d 75 6e 69 63 61  el SSL communica
0390: 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  tions..**.** Thi
03a0: 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
03b0: 73 20 61 20 73 69 6e 67 6c 65 74 6f 6e 2e 20 20  s a singleton.  
03c0: 41 20 73 69 6e 67 6c 65 20 53 53 4c 20 63 6f 6e  A single SSL con
03d0: 6e 65 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 61  nection may be a
03e0: 63 74 69 76 65 0a 2a 2a 20 61 74 20 61 20 74 69  ctive.** at a ti
03f0: 6d 65 2e 20 20 53 74 61 74 65 20 69 6e 66 6f 72  me.  State infor
0400: 6d 61 74 69 6f 6e 20 69 73 20 73 74 6f 72 65 64  mation is stored
0410: 20 69 6e 20 73 74 61 74 69 63 20 76 61 72 69 61   in static varia
0420: 62 6c 65 73 2e 20 20 54 68 65 20 69 64 65 6e 74  bles.  The ident
0430: 69 74 79 0a 2a 2a 20 6f 66 20 74 68 65 20 73 65  ity.** of the se
0440: 72 76 65 72 20 69 73 20 68 65 6c 64 20 69 6e 20  rver is held in 
0450: 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73  global variables
0460: 20 74 68 61 74 20 61 72 65 20 73 65 74 20 62 79   that are set by
0470: 20 75 72 6c 5f 70 61 72 73 65 28 29 2e 0a 2a 2a   url_parse()..**
0480: 0a 2a 2a 20 53 53 4c 20 73 75 70 70 6f 72 74 20  .** SSL support 
0490: 69 73 20 61 62 73 74 72 61 63 74 65 64 20 6f 75  is abstracted ou
04a0: 74 20 69 6e 74 6f 20 74 68 69 73 20 6d 6f 64 75  t into this modu
04b0: 6c 65 20 62 65 63 61 75 73 65 20 46 6f 73 73 69  le because Fossi
04c0: 6c 20 63 61 6e 0a 2a 2a 20 62 65 20 63 6f 6d 70  l can.** be comp
04d0: 69 6c 65 64 20 77 69 74 68 6f 75 74 20 53 53 4c  iled without SSL
04e0: 20 73 75 70 70 6f 72 74 20 28 77 68 69 63 68 20   support (which 
04f0: 72 65 71 75 69 72 65 73 20 4f 70 65 6e 53 53 4c  requires OpenSSL
0500: 20 6c 69 62 72 61 72 79 29 0a 2a 2f 0a 0a 23 69   library).*/..#i
0510: 6e 63 6c 75 64 65 20 22 63 6f 6e 66 69 67 2e 68  nclude "config.h
0520: 22 0a 0a 23 69 66 64 65 66 20 46 4f 53 53 49 4c  "..#ifdef FOSSIL
0530: 5f 45 4e 41 42 4c 45 5f 53 53 4c 0a 0a 23 69 6e  _ENABLE_SSL..#in
0540: 63 6c 75 64 65 20 3c 6f 70 65 6e 73 73 6c 2f 62  clude <openssl/b
0550: 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  io.h>.#include <
0560: 6f 70 65 6e 73 73 6c 2f 73 73 6c 2e 68 3e 0a 23  openssl/ssl.h>.#
0570: 69 6e 63 6c 75 64 65 20 3c 6f 70 65 6e 73 73 6c  include <openssl
0580: 2f 65 72 72 2e 68 3e 0a 0a 23 69 6e 63 6c 75 64  /err.h>..#includ
0590: 65 20 22 68 74 74 70 5f 73 73 6c 2e 68 22 0a 23  e "http_ssl.h".#
05a0: 69 6e 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e  include <assert.
05b0: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73  h>.#include <sys
05c0: 2f 74 79 70 65 73 2e 68 3e 0a 0a 2f 2a 0a 2a 2a  /types.h>../*.**
05d0: 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20   There can only 
05e0: 62 65 20 61 20 73 69 6e 67 6c 65 20 4f 70 65 6e  be a single Open
05f0: 53 53 4c 20 49 4f 20 63 6f 6e 6e 65 63 74 69 6f  SSL IO connectio
0600: 6e 20 6f 70 65 6e 20 61 74 20 61 20 74 69 6d 65  n open at a time
0610: 2e 0a 2a 2a 20 53 74 61 74 65 20 69 6e 66 6f 72  ..** State infor
0620: 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 61  mation about tha
0630: 74 20 49 4f 20 69 73 20 73 74 6f 72 65 64 20 69  t IO is stored i
0640: 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  n the following.
0650: 2a 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  ** local variabl
0660: 65 73 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es:.*/.static in
0670: 74 20 73 73 6c 49 73 49 6e 69 74 20 3d 20 30 3b  t sslIsInit = 0;
0680: 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65      /* True afte
0690: 72 20 67 6c 6f 62 61 6c 20 69 6e 69 74 69 61 6c  r global initial
06a0: 69 7a 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69  ization */.stati
06b0: 63 20 42 49 4f 20 2a 69 42 69 6f 3b 20 20 20 20  c BIO *iBio;    
06c0: 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 53          /* OpenS
06d0: 53 4c 20 49 2f 4f 20 61 62 73 74 72 61 63 74 69  SL I/O abstracti
06e0: 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  on */.static cha
06f0: 72 20 2a 73 73 6c 45 72 72 4d 73 67 20 3d 20 30  r *sslErrMsg = 0
0700: 3b 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 6d 6f  ;  /* Text of mo
0710: 73 74 20 72 65 63 65 6e 74 20 4f 70 65 6e 53 53  st recent OpenSS
0720: 4c 20 65 72 72 6f 72 20 2a 2f 0a 73 74 61 74 69  L error */.stati
0730: 63 20 53 53 4c 5f 43 54 58 20 2a 73 73 6c 43 74  c SSL_CTX *sslCt
0740: 78 3b 20 20 20 20 20 20 2f 2a 20 53 53 4c 20 63  x;      /* SSL c
0750: 6f 6e 74 65 78 74 20 2a 2f 0a 73 74 61 74 69 63  ontext */.static
0760: 20 53 53 4c 20 2a 73 73 6c 3b 0a 0a 0a 2f 2a 0a   SSL *ssl;.../*.
0770: 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 53 53 4c  ** Clear the SSL
0780: 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a   error message.*
0790: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 73  /.static void ss
07a0: 6c 5f 63 6c 65 61 72 5f 65 72 72 6d 73 67 28 76  l_clear_errmsg(v
07b0: 6f 69 64 29 7b 0a 20 20 66 72 65 65 28 73 73 6c  oid){.  free(ssl
07c0: 45 72 72 4d 73 67 29 3b 0a 20 20 73 73 6c 45 72  ErrMsg);.  sslEr
07d0: 72 4d 73 67 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  rMsg = 0;.}../*.
07e0: 2a 2a 20 53 65 74 20 74 68 65 20 53 53 4c 20 65  ** Set the SSL e
07f0: 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f  rror message..*/
0800: 0a 76 6f 69 64 20 73 73 6c 5f 73 65 74 5f 65 72  .void ssl_set_er
0810: 72 6d 73 67 28 63 68 61 72 20 2a 7a 46 6f 72 6d  rmsg(char *zForm
0820: 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  at, ...){.  va_l
0830: 69 73 74 20 61 70 3b 0a 20 20 73 73 6c 5f 63 6c  ist ap;.  ssl_cl
0840: 65 61 72 5f 65 72 72 6d 73 67 28 29 3b 0a 20 20  ear_errmsg();.  
0850: 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f  va_start(ap, zFo
0860: 72 6d 61 74 29 3b 0a 20 20 73 73 6c 45 72 72 4d  rmat);.  sslErrM
0870: 73 67 20 3d 20 76 6d 70 72 69 6e 74 66 28 7a 46  sg = vmprintf(zF
0880: 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61  ormat, ap);.  va
0890: 5f 65 6e 64 28 61 70 29 3b 0a 7d 0a 0a 2f 2a 0a  _end(ap);.}../*.
08a0: 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 75  ** Return the cu
08b0: 72 72 65 6e 74 20 53 53 4c 20 65 72 72 6f 72 20  rrent SSL error 
08c0: 6d 65 73 73 61 67 65 0a 2a 2f 0a 63 6f 6e 73 74  message.*/.const
08d0: 20 63 68 61 72 20 2a 73 73 6c 5f 65 72 72 6d 73   char *ssl_errms
08e0: 67 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72  g(void){.  retur
08f0: 6e 20 73 73 6c 45 72 72 4d 73 67 3b 0a 7d 0a 0a  n sslErrMsg;.}..
0900: 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20  /*.** Call this 
0910: 72 6f 75 74 69 6e 65 20 6f 6e 63 65 20 62 65 66  routine once bef
0920: 6f 72 65 20 61 6e 79 20 6f 74 68 65 72 20 75 73  ore any other us
0930: 65 20 6f 66 20 74 68 65 20 53 53 4c 20 69 6e 74  e of the SSL int
0940: 65 72 66 61 63 65 2e 0a 2a 2a 20 54 68 69 73 20  erface..** This 
0950: 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 69 6e 69  routine does ini
0960: 74 69 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69  tial configurati
0970: 6f 6e 20 6f 66 20 74 68 65 20 53 53 4c 20 6d 6f  on of the SSL mo
0980: 64 75 6c 65 2e 0a 2a 2f 0a 76 6f 69 64 20 73 73  dule..*/.void ss
0990: 6c 5f 67 6c 6f 62 61 6c 5f 69 6e 69 74 28 76 6f  l_global_init(vo
09a0: 69 64 29 7b 0a 20 20 69 66 28 20 73 73 6c 49 73  id){.  if( sslIs
09b0: 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 53  Init==0 ){.    S
09c0: 53 4c 5f 6c 69 62 72 61 72 79 5f 69 6e 69 74 28  SL_library_init(
09d0: 29 3b 0a 20 20 20 20 53 53 4c 5f 6c 6f 61 64 5f  );.    SSL_load_
09e0: 65 72 72 6f 72 5f 73 74 72 69 6e 67 73 28 29 3b  error_strings();
09f0: 0a 20 20 20 20 45 52 52 5f 6c 6f 61 64 5f 42 49  .    ERR_load_BI
0a00: 4f 5f 73 74 72 69 6e 67 73 28 29 3b 0a 20 20 20  O_strings();.   
0a10: 20 4f 70 65 6e 53 53 4c 5f 61 64 64 5f 61 6c 6c   OpenSSL_add_all
0a20: 5f 61 6c 67 6f 72 69 74 68 6d 73 28 29 3b 20 20  _algorithms();  
0a30: 20 20 0a 20 20 20 20 73 73 6c 43 74 78 20 3d 20    .    sslCtx = 
0a40: 53 53 4c 5f 43 54 58 5f 6e 65 77 28 53 53 4c 76  SSL_CTX_new(SSLv
0a50: 32 33 5f 63 6c 69 65 6e 74 5f 6d 65 74 68 6f 64  23_client_method
0a60: 28 29 29 3b 0a 20 20 20 20 73 73 6c 49 73 49 6e  ());.    sslIsIn
0a70: 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f  it = 1;.  }.}../
0a80: 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72  *.** Call this r
0a90: 6f 75 74 69 6e 65 20 74 6f 20 73 68 75 74 64 6f  outine to shutdo
0aa0: 77 6e 20 74 68 65 20 53 53 4c 20 6d 6f 64 75 6c  wn the SSL modul
0ab0: 65 20 70 72 69 6f 72 20 74 6f 20 70 72 6f 67 72  e prior to progr
0ac0: 61 6d 20 65 78 69 74 2e 0a 2a 2f 0a 76 6f 69 64  am exit..*/.void
0ad0: 20 73 73 6c 5f 67 6c 6f 62 61 6c 5f 73 68 75 74   ssl_global_shut
0ae0: 64 6f 77 6e 28 76 6f 69 64 29 7b 0a 20 20 69 66  down(void){.  if
0af0: 28 20 73 73 6c 49 73 49 6e 69 74 20 29 7b 0a 20  ( sslIsInit ){. 
0b00: 20 20 20 53 53 4c 5f 43 54 58 5f 66 72 65 65 28     SSL_CTX_free(
0b10: 73 73 6c 43 74 78 29 3b 0a 20 20 20 20 73 73 6c  sslCtx);.    ssl
0b20: 5f 63 6c 65 61 72 5f 65 72 72 6d 73 67 28 29 3b  _clear_errmsg();
0b30: 0a 20 20 20 20 73 73 6c 49 73 49 6e 69 74 20 3d  .    sslIsInit =
0b40: 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
0b50: 20 43 6c 6f 73 65 20 74 68 65 20 63 75 72 72 65   Close the curre
0b60: 6e 74 6c 79 20 6f 70 65 6e 20 53 53 4c 20 63 6f  ntly open SSL co
0b70: 6e 6e 65 63 74 69 6f 6e 2e 20 20 49 66 20 6e 6f  nnection.  If no
0b80: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f   connection is o
0b90: 70 65 6e 2c 20 0a 2a 2a 20 74 68 69 73 20 72 6f  pen, .** this ro
0ba0: 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
0bb0: 2e 0a 2a 2f 0a 76 6f 69 64 20 73 73 6c 5f 63 6c  ..*/.void ssl_cl
0bc0: 6f 73 65 28 76 6f 69 64 29 7b 0a 20 20 69 66 28  ose(void){.  if(
0bd0: 20 69 42 69 6f 21 3d 4e 55 4c 4c 20 29 7b 0a 20   iBio!=NULL ){. 
0be0: 20 20 20 42 49 4f 5f 72 65 73 65 74 28 69 42 69     BIO_reset(iBi
0bf0: 6f 29 3b 0a 20 20 20 20 42 49 4f 5f 66 72 65 65  o);.    BIO_free
0c00: 5f 61 6c 6c 28 69 42 69 6f 29 3b 0a 20 20 7d 0a  _all(iBio);.  }.
0c10: 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 6e  }../*.** Open an
0c20: 20 53 53 4c 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e   SSL connection.
0c30: 20 20 54 68 65 20 69 64 65 6e 74 69 66 79 20 6f    The identify o
0c40: 66 20 74 68 65 20 73 65 72 76 65 72 20 69 73 20  f the server is 
0c50: 64 65 74 65 72 6d 69 6e 65 64 0a 2a 2a 20 62 79  determined.** by
0c60: 20 67 6c 6f 62 61 6c 20 76 61 72 69 62 6c 65 73   global varibles
0c70: 20 74 68 61 74 20 61 72 65 20 73 65 74 20 75 73   that are set us
0c80: 69 6e 67 20 75 72 6c 5f 70 61 72 73 65 28 29 3a  ing url_parse():
0c90: 0a 2a 2a 0a 2a 2a 20 20 20 20 67 2e 75 72 6c 4e  .**.**    g.urlN
0ca0: 61 6d 65 20 20 20 20 20 20 20 4e 61 6d 65 20 6f  ame       Name o
0cb0: 66 20 74 68 65 20 73 65 72 76 65 72 2e 20 20 45  f the server.  E
0cc0: 78 3a 20 77 77 77 2e 66 6f 73 73 69 6c 2d 73 63  x: www.fossil-sc
0cd0: 6d 2e 6f 72 67 0a 2a 2a 20 20 20 20 67 2e 75 72  m.org.**    g.ur
0ce0: 6c 50 6f 72 74 20 20 20 20 20 20 20 54 43 50 2f  lPort       TCP/
0cf0: 49 50 20 70 6f 72 74 20 74 6f 20 75 73 65 2e 20  IP port to use. 
0d00: 20 45 78 3a 20 38 30 0a 2a 2a 0a 2a 2a 20 52 65   Ex: 80.**.** Re
0d10: 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
0d20: 6f 66 20 65 72 72 6f 72 73 2e 0a 2a 2f 0a 69 6e  of errors..*/.in
0d30: 74 20 73 73 6c 5f 6f 70 65 6e 28 76 6f 69 64 29  t ssl_open(void)
0d40: 7b 0a 20 20 58 35 30 39 20 2a 63 65 72 74 3b 0a  {.  X509 *cert;.
0d50: 20 20 69 6e 74 20 68 61 73 53 61 76 65 64 43 65    int hasSavedCe
0d60: 72 74 69 66 69 63 61 74 65 20 3d 20 30 3b 0a 0a  rtificate = 0;..
0d70: 20 20 73 73 6c 5f 67 6c 6f 62 61 6c 5f 69 6e 69    ssl_global_ini
0d80: 74 28 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 63  t();..  /* Get c
0d90: 65 72 74 69 66 69 63 61 74 65 20 66 6f 72 20 63  ertificate for c
0da0: 75 72 72 65 6e 74 20 73 65 72 76 65 72 20 66 72  urrent server fr
0db0: 6f 6d 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67  om global config
0dc0: 20 61 6e 64 0a 20 20 20 2a 20 28 69 66 20 77 65   and.   * (if we
0dd0: 20 68 61 76 65 20 69 74 20 69 6e 20 63 6f 6e 66   have it in conf
0de0: 69 67 29 20 61 64 64 20 69 74 20 74 6f 20 63 65  ig) add it to ce
0df0: 72 74 69 66 69 63 61 74 65 20 73 74 6f 72 65 2e  rtificate store.
0e00: 0a 20 20 20 2a 2f 0a 20 20 63 65 72 74 20 3d 20  .   */.  cert = 
0e10: 73 73 6c 5f 67 65 74 5f 63 65 72 74 69 66 69 63  ssl_get_certific
0e20: 61 74 65 28 29 3b 0a 20 20 69 66 20 28 20 63 65  ate();.  if ( ce
0e30: 72 74 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20  rt != NULL ){.  
0e40: 20 20 58 35 30 39 5f 53 54 4f 52 45 5f 61 64 64    X509_STORE_add
0e50: 5f 63 65 72 74 28 53 53 4c 5f 43 54 58 5f 67 65  _cert(SSL_CTX_ge
0e60: 74 5f 63 65 72 74 5f 73 74 6f 72 65 28 73 73 6c  t_cert_store(ssl
0e70: 43 74 78 29 2c 20 63 65 72 74 29 3b 0a 20 20 20  Ctx), cert);.   
0e80: 20 58 35 30 39 5f 66 72 65 65 28 63 65 72 74 29   X509_free(cert)
0e90: 3b 0a 20 20 20 20 68 61 73 53 61 76 65 64 43 65  ;.    hasSavedCe
0ea0: 72 74 69 66 69 63 61 74 65 20 3d 20 31 3b 0a 20  rtificate = 1;. 
0eb0: 20 7d 0a 0a 20 20 69 42 69 6f 20 3d 20 42 49 4f   }..  iBio = BIO
0ec0: 5f 6e 65 77 5f 73 73 6c 5f 63 6f 6e 6e 65 63 74  _new_ssl_connect
0ed0: 28 73 73 6c 43 74 78 29 3b 0a 20 20 42 49 4f 5f  (sslCtx);.  BIO_
0ee0: 67 65 74 5f 73 73 6c 28 69 42 69 6f 2c 20 26 73  get_ssl(iBio, &s
0ef0: 73 6c 29 3b 0a 20 20 53 53 4c 5f 73 65 74 5f 6d  sl);.  SSL_set_m
0f00: 6f 64 65 28 73 73 6c 2c 20 53 53 4c 5f 4d 4f 44  ode(ssl, SSL_MOD
0f10: 45 5f 41 55 54 4f 5f 52 45 54 52 59 29 3b 0a 20  E_AUTO_RETRY);. 
0f20: 20 69 66 28 20 69 42 69 6f 3d 3d 4e 55 4c 4c 20   if( iBio==NULL 
0f30: 29 20 7b 0a 20 20 20 20 73 73 6c 5f 73 65 74 5f  ) {.    ssl_set_
0f40: 65 72 72 6d 73 67 28 22 53 53 4c 3a 20 63 61 6e  errmsg("SSL: can
0f50: 6e 6f 74 20 6f 70 65 6e 20 53 53 4c 20 28 25 73  not open SSL (%s
0f60: 29 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  )", .           
0f70: 20 20 20 20 20 20 20 20 20 45 52 52 5f 72 65 61           ERR_rea
0f80: 73 6f 6e 5f 65 72 72 6f 72 5f 73 74 72 69 6e 67  son_error_string
0f90: 28 45 52 52 5f 67 65 74 5f 65 72 72 6f 72 28 29  (ERR_get_error()
0fa0: 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  ));.    return 1
0fb0: 3b 20 20 20 20 0a 20 20 7d 0a 20 20 0a 20 20 63  ;    .  }.  .  c
0fc0: 68 61 72 20 2a 63 6f 6e 6e 53 74 72 20 3d 20 6d  har *connStr = m
0fd0: 70 72 69 6e 74 66 28 22 25 73 3a 25 64 22 2c 20  printf("%s:%d", 
0fe0: 67 2e 75 72 6c 4e 61 6d 65 2c 20 67 2e 75 72 6c  g.urlName, g.url
0ff0: 50 6f 72 74 29 3b 0a 20 20 42 49 4f 5f 73 65 74  Port);.  BIO_set
1000: 5f 63 6f 6e 6e 5f 68 6f 73 74 6e 61 6d 65 28 69  _conn_hostname(i
1010: 42 69 6f 2c 20 63 6f 6e 6e 53 74 72 29 3b 0a 20  Bio, connStr);. 
1020: 20 66 72 65 65 28 63 6f 6e 6e 53 74 72 29 3b 0a   free(connStr);.
1030: 20 20 0a 20 20 69 66 28 20 42 49 4f 5f 64 6f 5f    .  if( BIO_do_
1040: 63 6f 6e 6e 65 63 74 28 69 42 69 6f 29 3c 3d 30  connect(iBio)<=0
1050: 20 29 7b 0a 20 20 20 20 73 73 6c 5f 73 65 74 5f   ){.    ssl_set_
1060: 65 72 72 6d 73 67 28 22 53 53 4c 3a 20 63 61 6e  errmsg("SSL: can
1070: 6e 6f 74 20 63 6f 6e 6e 65 63 74 20 74 6f 20 68  not connect to h
1080: 6f 73 74 20 25 73 3a 25 64 20 28 25 73 29 22 2c  ost %s:%d (%s)",
1090: 20 0a 20 20 20 20 20 20 20 20 67 2e 75 72 6c 4e   .        g.urlN
10a0: 61 6d 65 2c 20 67 2e 75 72 6c 50 6f 72 74 2c 20  ame, g.urlPort, 
10b0: 45 52 52 5f 72 65 61 73 6f 6e 5f 65 72 72 6f 72  ERR_reason_error
10c0: 5f 73 74 72 69 6e 67 28 45 52 52 5f 67 65 74 5f  _string(ERR_get_
10d0: 65 72 72 6f 72 28 29 29 29 3b 0a 20 20 20 20 73  error()));.    s
10e0: 73 6c 5f 63 6c 6f 73 65 28 29 3b 0a 20 20 20 20  sl_close();.    
10f0: 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
1100: 0a 20 20 69 66 28 20 42 49 4f 5f 64 6f 5f 68 61  .  if( BIO_do_ha
1110: 6e 64 73 68 61 6b 65 28 69 42 69 6f 29 3c 3d 30  ndshake(iBio)<=0
1120: 20 29 20 7b 0a 20 20 20 20 73 73 6c 5f 73 65 74   ) {.    ssl_set
1130: 5f 65 72 72 6d 73 67 28 22 45 72 72 6f 72 20 65  _errmsg("Error e
1140: 73 74 61 62 6c 69 73 68 69 6e 67 20 53 53 4c 20  stablishing SSL 
1150: 63 6f 6e 6e 65 63 74 69 6f 6e 20 25 73 3a 25 64  connection %s:%d
1160: 20 28 25 73 29 22 2c 20 0a 20 20 20 20 20 20 20   (%s)", .       
1170: 20 67 2e 75 72 6c 4e 61 6d 65 2c 20 67 2e 75 72   g.urlName, g.ur
1180: 6c 50 6f 72 74 2c 20 45 52 52 5f 72 65 61 73 6f  lPort, ERR_reaso
1190: 6e 5f 65 72 72 6f 72 5f 73 74 72 69 6e 67 28 45  n_error_string(E
11a0: 52 52 5f 67 65 74 5f 65 72 72 6f 72 28 29 29 29  RR_get_error()))
11b0: 3b 0a 20 20 20 20 73 73 6c 5f 63 6c 6f 73 65 28  ;.    ssl_close(
11c0: 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
11d0: 0a 20 20 7d 0a 20 20 2f 2a 20 43 68 65 63 6b 20  .  }.  /* Check 
11e0: 69 66 20 63 65 72 74 69 66 69 63 61 74 65 20 69  if certificate i
11f0: 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 65 72  s valid */.  cer
1200: 74 20 3d 20 53 53 4c 5f 67 65 74 5f 70 65 65 72  t = SSL_get_peer
1210: 5f 63 65 72 74 69 66 69 63 61 74 65 28 73 73 6c  _certificate(ssl
1220: 29 3b 0a 0a 20 20 69 66 20 28 20 63 65 72 74 20  );..  if ( cert 
1230: 3d 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 73  == NULL ){.    s
1240: 73 6c 5f 73 65 74 5f 65 72 72 6d 73 67 28 22 4e  sl_set_errmsg("N
1250: 6f 20 53 53 4c 20 63 65 72 74 69 66 69 63 61 74  o SSL certificat
1260: 65 20 77 61 73 20 70 72 65 73 65 6e 74 65 64 20  e was presented 
1270: 62 79 20 74 68 65 20 70 65 65 72 22 29 3b 0a 20  by the peer");. 
1280: 20 20 20 73 73 6c 5f 63 6c 6f 73 65 28 29 3b 0a     ssl_close();.
1290: 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
12a0: 7d 0a 0a 20 20 69 66 28 20 53 53 4c 5f 67 65 74  }..  if( SSL_get
12b0: 5f 76 65 72 69 66 79 5f 72 65 73 75 6c 74 28 73  _verify_result(s
12c0: 73 6c 29 20 21 3d 20 58 35 30 39 5f 56 5f 4f 4b  sl) != X509_V_OK
12d0: 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 64 65   ){.    char *de
12e0: 73 63 2c 20 2a 70 72 6f 6d 70 74 3b 0a 20 20 20  sc, *prompt;.   
12f0: 20 42 49 4f 20 2a 6d 65 6d 3b 0a 20 20 20 20 0a   BIO *mem;.    .
1300: 20 20 20 20 6d 65 6d 20 3d 20 42 49 4f 5f 6e 65      mem = BIO_ne
1310: 77 28 42 49 4f 5f 73 5f 6d 65 6d 28 29 29 3b 0a  w(BIO_s_mem());.
1320: 20 20 20 20 58 35 30 39 5f 4e 41 4d 45 5f 70 72      X509_NAME_pr
1330: 69 6e 74 5f 65 78 28 6d 65 6d 2c 20 58 35 30 39  int_ex(mem, X509
1340: 5f 67 65 74 5f 73 75 62 6a 65 63 74 5f 6e 61 6d  _get_subject_nam
1350: 65 28 63 65 72 74 29 2c 20 32 2c 20 58 4e 5f 46  e(cert), 2, XN_F
1360: 4c 41 47 5f 4d 55 4c 54 49 4c 49 4e 45 29 3b 0a  LAG_MULTILINE);.
1370: 20 20 20 20 42 49 4f 5f 70 75 74 73 28 6d 65 6d      BIO_puts(mem
1380: 2c 20 22 5c 6e 5c 6e 49 73 73 75 65 64 20 42 79  , "\n\nIssued By
1390: 3a 5c 6e 5c 6e 22 29 3b 0a 20 20 20 20 58 35 30  :\n\n");.    X50
13a0: 39 5f 4e 41 4d 45 5f 70 72 69 6e 74 5f 65 78 28  9_NAME_print_ex(
13b0: 6d 65 6d 2c 20 58 35 30 39 5f 67 65 74 5f 69 73  mem, X509_get_is
13c0: 73 75 65 72 5f 6e 61 6d 65 28 63 65 72 74 29 2c  suer_name(cert),
13d0: 20 32 2c 20 58 4e 5f 46 4c 41 47 5f 4d 55 4c 54   2, XN_FLAG_MULT
13e0: 49 4c 49 4e 45 29 3b 0a 20 20 20 20 42 49 4f 5f  ILINE);.    BIO_
13f0: 77 72 69 74 65 28 6d 65 6d 2c 20 22 22 2c 20 31  write(mem, "", 1
1400: 29 3b 20 2f 2f 20 6e 75 6c 6c 2d 74 65 72 6d 69  ); // null-termi
1410: 6e 61 74 65 20 6d 65 6d 20 62 75 66 66 65 72 0a  nate mem buffer.
1420: 20 20 20 20 42 49 4f 5f 67 65 74 5f 6d 65 6d 5f      BIO_get_mem_
1430: 64 61 74 61 28 6d 65 6d 2c 20 26 64 65 73 63 29  data(mem, &desc)
1440: 3b 0a 20 20 20 20 0a 20 20 20 20 63 68 61 72 20  ;.    .    char 
1450: 2a 77 61 72 6e 69 6e 67 20 3d 20 22 22 3b 0a 20  *warning = "";. 
1460: 20 20 20 69 66 28 20 68 61 73 53 61 76 65 64 43     if( hasSavedC
1470: 65 72 74 69 66 69 63 61 74 65 20 29 7b 0a 20 20  ertificate ){.  
1480: 20 20 20 20 77 61 72 6e 69 6e 67 20 3d 20 22 57      warning = "W
1490: 41 52 4e 49 4e 47 3a 20 43 65 72 74 69 66 69 63  ARNING: Certific
14a0: 61 74 65 20 64 6f 65 73 6e 27 74 20 6d 61 74 63  ate doesn't matc
14b0: 68 20 74 68 65 20 22 0a 20 20 20 20 20 20 20 20  h the ".        
14c0: 20 20 20 20 20 20 20 20 22 73 61 76 65 64 20 63          "saved c
14d0: 65 72 74 69 66 69 63 61 74 65 20 66 6f 72 20 74  ertificate for t
14e0: 68 69 73 20 68 6f 73 74 21 22 3b 0a 20 20 20 20  his host!";.    
14f0: 7d 0a 20 20 20 20 70 72 6f 6d 70 74 20 3d 20 6d  }.    prompt = m
1500: 70 72 69 6e 74 66 28 22 5c 6e 55 6e 6b 6e 6f 77  printf("\nUnknow
1510: 6e 20 53 53 4c 20 63 65 72 74 69 66 69 63 61 74  n SSL certificat
1520: 65 3a 5c 6e 5c 6e 25 73 5c 6e 5c 6e 25 73 5c 6e  e:\n\n%s\n\n%s\n
1530: 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
1540: 20 20 20 20 20 20 20 22 41 63 63 65 70 74 20 63         "Accept c
1550: 65 72 74 69 66 69 63 61 74 65 20 5b 61 3d 61 6c  ertificate [a=al
1560: 77 61 79 73 2f 79 2f 4e 5d 3f 20 22 2c 20 64 65  ways/y/N]? ", de
1570: 73 63 2c 20 77 61 72 6e 69 6e 67 29 3b 0a 20 20  sc, warning);.  
1580: 20 20 42 49 4f 5f 66 72 65 65 28 6d 65 6d 29 3b    BIO_free(mem);
1590: 0a 0a 20 20 20 20 42 6c 6f 62 20 61 6e 73 3b 0a  ..    Blob ans;.
15a0: 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 61      blob_zero(&a
15b0: 6e 73 29 3b 0a 20 20 20 20 70 72 6f 6d 70 74 5f  ns);.    prompt_
15c0: 75 73 65 72 28 70 72 6f 6d 70 74 2c 20 26 61 6e  user(prompt, &an
15d0: 73 29 3b 0a 20 20 20 20 66 72 65 65 28 20 70 72  s);.    free( pr
15e0: 6f 6d 70 74 20 29 3b 0a 20 20 20 20 69 66 28 20  ompt );.    if( 
15f0: 62 6c 6f 62 5f 73 74 72 28 26 61 6e 73 29 5b 30  blob_str(&ans)[0
1600: 5d 21 3d 27 79 27 20 26 26 20 62 6c 6f 62 5f 73  ]!='y' && blob_s
1610: 74 72 28 26 61 6e 73 29 5b 30 5d 21 3d 27 61 27  tr(&ans)[0]!='a'
1620: 20 29 20 7b 0a 20 20 20 20 20 20 58 35 30 39 5f   ) {.      X509_
1630: 66 72 65 65 28 63 65 72 74 29 3b 0a 20 20 20 20  free(cert);.    
1640: 20 20 73 73 6c 5f 73 65 74 5f 65 72 72 6d 73 67    ssl_set_errmsg
1650: 28 22 53 53 4c 20 63 65 72 74 69 66 69 63 61 74  ("SSL certificat
1660: 65 20 64 65 63 6c 69 6e 65 64 22 29 3b 0a 20 20  e declined");.  
1670: 20 20 20 20 73 73 6c 5f 63 6c 6f 73 65 28 29 3b      ssl_close();
1680: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
1690: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62  .    }.    if( b
16a0: 6c 6f 62 5f 73 74 72 28 26 61 6e 73 29 5b 30 5d  lob_str(&ans)[0]
16b0: 3d 3d 27 61 27 20 29 20 7b 0a 20 20 20 20 20 20  =='a' ) {.      
16c0: 73 73 6c 5f 73 61 76 65 5f 63 65 72 74 69 66 69  ssl_save_certifi
16d0: 63 61 74 65 28 63 65 72 74 29 3b 0a 20 20 20 20  cate(cert);.    
16e0: 7d 0a 20 20 7d 0a 20 20 58 35 30 39 5f 66 72 65  }.  }.  X509_fre
16f0: 65 28 63 65 72 74 29 3b 0a 20 20 72 65 74 75 72  e(cert);.  retur
1700: 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61  n 0;.}../*.** Sa
1710: 76 65 20 63 65 72 74 69 66 69 63 61 74 65 20 74  ve certificate t
1720: 6f 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 2e  o global config.
1730: 0a 2a 2f 0a 76 6f 69 64 20 73 73 6c 5f 73 61 76  .*/.void ssl_sav
1740: 65 5f 63 65 72 74 69 66 69 63 61 74 65 28 58 35  e_certificate(X5
1750: 30 39 20 2a 63 65 72 74 29 0a 7b 0a 20 20 42 49  09 *cert).{.  BI
1760: 4f 20 2a 6d 65 6d 3b 0a 20 20 63 68 61 72 20 2a  O *mem;.  char *
1770: 7a 43 65 72 74 2c 20 2a 7a 48 6f 73 74 3b 0a 0a  zCert, *zHost;..
1780: 20 20 6d 65 6d 20 3d 20 42 49 4f 5f 6e 65 77 28    mem = BIO_new(
1790: 42 49 4f 5f 73 5f 6d 65 6d 28 29 29 3b 0a 20 20  BIO_s_mem());.  
17a0: 50 45 4d 5f 77 72 69 74 65 5f 62 69 6f 5f 58 35  PEM_write_bio_X5
17b0: 30 39 28 6d 65 6d 2c 20 63 65 72 74 29 3b 0a 20  09(mem, cert);. 
17c0: 20 42 49 4f 5f 77 72 69 74 65 28 6d 65 6d 2c 20   BIO_write(mem, 
17d0: 22 22 2c 20 31 29 3b 20 2f 2f 20 6e 75 6c 6c 2d  "", 1); // null-
17e0: 74 65 72 6d 69 6e 61 74 65 20 6d 65 6d 20 62 75  terminate mem bu
17f0: 66 66 65 72 0a 20 20 42 49 4f 5f 67 65 74 5f 6d  ffer.  BIO_get_m
1800: 65 6d 5f 64 61 74 61 28 6d 65 6d 2c 20 26 7a 43  em_data(mem, &zC
1810: 65 72 74 29 3b 0a 20 20 7a 48 6f 73 74 20 3d 20  ert);.  zHost = 
1820: 6d 70 72 69 6e 74 66 28 22 63 65 72 74 3a 25 73  mprintf("cert:%s
1830: 22 2c 20 67 2e 75 72 6c 4e 61 6d 65 29 3b 0a 20  ", g.urlName);. 
1840: 20 64 62 5f 73 65 74 28 7a 48 6f 73 74 2c 20 7a   db_set(zHost, z
1850: 43 65 72 74 2c 20 31 29 3b 0a 20 20 66 72 65 65  Cert, 1);.  free
1860: 28 7a 48 6f 73 74 29 3b 0a 20 20 42 49 4f 5f 66  (zHost);.  BIO_f
1870: 72 65 65 28 6d 65 6d 29 3b 20 20 0a 7d 0a 0a 2f  ree(mem);  .}../
1880: 2a 0a 2a 2a 20 47 65 74 20 63 65 72 74 69 66 69  *.** Get certifi
1890: 63 61 74 65 20 66 6f 72 20 67 2e 75 72 6c 4e 61  cate for g.urlNa
18a0: 6d 65 20 66 72 6f 6d 20 67 6c 6f 62 61 6c 20 63  me from global c
18b0: 6f 6e 66 69 67 2e 0a 2a 2a 20 52 65 74 75 72 6e  onfig..** Return
18c0: 20 4e 55 4c 4c 20 69 66 20 6e 6f 20 63 65 72 74   NULL if no cert
18d0: 69 66 69 63 61 74 65 20 66 6f 75 6e 64 2e 0a 2a  ificate found..*
18e0: 2f 0a 58 35 30 39 20 2a 73 73 6c 5f 67 65 74 5f  /.X509 *ssl_get_
18f0: 63 65 72 74 69 66 69 63 61 74 65 28 29 0a 7b 0a  certificate().{.
1900: 20 20 63 68 61 72 20 2a 7a 48 6f 73 74 2c 20 2a    char *zHost, *
1910: 7a 43 65 72 74 3b 0a 20 20 42 49 4f 20 2a 6d 65  zCert;.  BIO *me
1920: 6d 3b 0a 20 20 58 35 30 39 20 2a 63 65 72 74 3b  m;.  X509 *cert;
1930: 0a 0a 20 20 7a 48 6f 73 74 20 3d 20 6d 70 72 69  ..  zHost = mpri
1940: 6e 74 66 28 22 63 65 72 74 3a 25 73 22 2c 20 67  ntf("cert:%s", g
1950: 2e 75 72 6c 4e 61 6d 65 29 3b 0a 20 20 7a 43 65  .urlName);.  zCe
1960: 72 74 20 3d 20 64 62 5f 67 65 74 28 7a 48 6f 73  rt = db_get(zHos
1970: 74 2c 20 4e 55 4c 4c 29 3b 0a 20 20 66 72 65 65  t, NULL);.  free
1980: 28 7a 48 6f 73 74 29 3b 0a 20 20 69 66 20 28 20  (zHost);.  if ( 
1990: 7a 43 65 72 74 3d 3d 4e 55 4c 4c 20 29 0a 20 20  zCert==NULL ).  
19a0: 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a 20    return NULL;. 
19b0: 20 6d 65 6d 20 3d 20 42 49 4f 5f 6e 65 77 28 42   mem = BIO_new(B
19c0: 49 4f 5f 73 5f 6d 65 6d 28 29 29 3b 0a 20 20 42  IO_s_mem());.  B
19d0: 49 4f 5f 70 75 74 73 28 6d 65 6d 2c 20 7a 43 65  IO_puts(mem, zCe
19e0: 72 74 29 3b 0a 20 20 63 65 72 74 20 3d 20 50 45  rt);.  cert = PE
19f0: 4d 5f 72 65 61 64 5f 62 69 6f 5f 58 35 30 39 28  M_read_bio_X509(
1a00: 6d 65 6d 2c 20 4e 55 4c 4c 2c 20 30 2c 20 4e 55  mem, NULL, 0, NU
1a10: 4c 4c 29 3b 0a 20 20 66 72 65 65 28 7a 43 65 72  LL);.  free(zCer
1a20: 74 29 3b 0a 20 20 42 49 4f 5f 66 72 65 65 28 6d  t);.  BIO_free(m
1a30: 65 6d 29 3b 20 20 0a 20 20 72 65 74 75 72 6e 20  em);  .  return 
1a40: 63 65 72 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  cert;.}../*.** S
1a50: 65 6e 64 20 63 6f 6e 74 65 6e 74 20 6f 75 74 20  end content out 
1a60: 6f 76 65 72 20 74 68 65 20 53 53 4c 20 63 6f 6e  over the SSL con
1a70: 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 69 7a 65  nection..*/.size
1a80: 5f 74 20 73 73 6c 5f 73 65 6e 64 28 76 6f 69 64  _t ssl_send(void
1a90: 20 2a 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20   *NotUsed, void 
1aa0: 2a 70 43 6f 6e 74 65 6e 74 2c 20 73 69 7a 65 5f  *pContent, size_
1ab0: 74 20 4e 29 7b 0a 20 20 73 69 7a 65 5f 74 20 73  t N){.  size_t s
1ac0: 65 6e 74 3b 0a 20 20 73 69 7a 65 5f 74 20 74 6f  ent;.  size_t to
1ad0: 74 61 6c 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65  tal = 0;.  while
1ae0: 28 20 4e 3e 30 20 29 7b 0a 20 20 20 20 73 65 6e  ( N>0 ){.    sen
1af0: 74 20 3d 20 42 49 4f 5f 77 72 69 74 65 28 69 42  t = BIO_write(iB
1b00: 69 6f 2c 20 70 43 6f 6e 74 65 6e 74 2c 20 4e 29  io, pContent, N)
1b10: 3b 0a 20 20 20 20 69 66 28 20 73 65 6e 74 3c 3d  ;.    if( sent<=
1b20: 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 74  0 ) break;.    t
1b30: 6f 74 61 6c 20 2b 3d 20 73 65 6e 74 3b 0a 20 20  otal += sent;.  
1b40: 20 20 4e 20 2d 3d 20 73 65 6e 74 3b 0a 20 20 20    N -= sent;.   
1b50: 20 70 43 6f 6e 74 65 6e 74 20 3d 20 28 76 6f 69   pContent = (voi
1b60: 64 2a 29 26 28 28 63 68 61 72 2a 29 70 43 6f 6e  d*)&((char*)pCon
1b70: 74 65 6e 74 29 5b 73 65 6e 74 5d 3b 0a 20 20 7d  tent)[sent];.  }
1b80: 0a 20 20 72 65 74 75 72 6e 20 74 6f 74 61 6c 3b  .  return total;
1b90: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 65 69 76  .}../*.** Receiv
1ba0: 65 20 63 6f 6e 74 65 6e 74 20 62 61 63 6b 20 66  e content back f
1bb0: 72 6f 6d 20 74 68 65 20 53 53 4c 20 63 6f 6e 6e  rom the SSL conn
1bc0: 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 69 7a 65 5f  ection..*/.size_
1bd0: 74 20 73 73 6c 5f 72 65 63 65 69 76 65 28 76 6f  t ssl_receive(vo
1be0: 69 64 20 2a 4e 6f 74 55 73 65 64 2c 20 76 6f 69  id *NotUsed, voi
1bf0: 64 20 2a 70 43 6f 6e 74 65 6e 74 2c 20 73 69 7a  d *pContent, siz
1c00: 65 5f 74 20 4e 29 7b 0a 20 20 73 69 7a 65 5f 74  e_t N){.  size_t
1c10: 20 67 6f 74 3b 0a 20 20 73 69 7a 65 5f 74 20 74   got;.  size_t t
1c20: 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 77 68 69 6c  otal = 0;.  whil
1c30: 65 28 20 4e 3e 30 20 29 7b 0a 20 20 20 20 67 6f  e( N>0 ){.    go
1c40: 74 20 3d 20 42 49 4f 5f 72 65 61 64 28 69 42 69  t = BIO_read(iBi
1c50: 6f 2c 20 70 43 6f 6e 74 65 6e 74 2c 20 4e 29 3b  o, pContent, N);
1c60: 0a 20 20 20 20 69 66 28 20 67 6f 74 3c 3d 30 20  .    if( got<=0 
1c70: 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 74 6f 74  ) break;.    tot
1c80: 61 6c 20 2b 3d 20 67 6f 74 3b 0a 20 20 20 20 4e  al += got;.    N
1c90: 20 2d 3d 20 67 6f 74 3b 0a 20 20 20 20 70 43 6f   -= got;.    pCo
1ca0: 6e 74 65 6e 74 20 3d 20 28 76 6f 69 64 2a 29 26  ntent = (void*)&
1cb0: 28 28 63 68 61 72 2a 29 70 43 6f 6e 74 65 6e 74  ((char*)pContent
1cc0: 29 5b 67 6f 74 5d 3b 0a 20 20 7d 0a 20 20 72 65  )[got];.  }.  re
1cd0: 74 75 72 6e 20 74 6f 74 61 6c 3b 0a 7d 0a 0a 23  turn total;.}..#
1ce0: 65 6e 64 69 66 20 2f 2a 20 46 4f 53 53 49 4c 5f  endif /* FOSSIL_
1cf0: 45 4e 41 42 4c 45 5f 53 53 4c 20 2a 2f 0a        ENABLE_SSL */.