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 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 rt!=NULL ){.
0e40: 58 35 30 39 5f 53 54 4f 52 45 5f 61 64 64 5f 63 X509_STORE_add_c
0e50: 65 72 74 28 53 53 4c 5f 43 54 58 5f 67 65 74 5f ert(SSL_CTX_get_
0e60: 63 65 72 74 5f 73 74 6f 72 65 28 73 73 6c 43 74 cert_store(sslCt
0e70: 78 29 2c 20 63 65 72 74 29 3b 0a 20 20 20 20 58 x), cert);. X
0e80: 35 30 39 5f 66 72 65 65 28 63 65 72 74 29 3b 0a 509_free(cert);.
0e90: 20 20 20 20 68 61 73 53 61 76 65 64 43 65 72 74 hasSavedCert
0ea0: 69 66 69 63 61 74 65 20 3d 20 31 3b 0a 20 20 7d ificate = 1;. }
0eb0: 0a 0a 20 20 69 42 69 6f 20 3d 20 42 49 4f 5f 6e .. iBio = BIO_n
0ec0: 65 77 5f 73 73 6c 5f 63 6f 6e 6e 65 63 74 28 73 ew_ssl_connect(s
0ed0: 73 6c 43 74 78 29 3b 0a 20 20 42 49 4f 5f 67 65 slCtx);. BIO_ge
0ee0: 74 5f 73 73 6c 28 69 42 69 6f 2c 20 26 73 73 6c t_ssl(iBio, &ssl
0ef0: 29 3b 0a 20 20 53 53 4c 5f 73 65 74 5f 6d 6f 64 );. SSL_set_mod
0f00: 65 28 73 73 6c 2c 20 53 53 4c 5f 4d 4f 44 45 5f e(ssl, SSL_MODE_
0f10: 41 55 54 4f 5f 52 45 54 52 59 29 3b 0a 20 20 69 AUTO_RETRY);. i
0f20: 66 28 20 69 42 69 6f 3d 3d 4e 55 4c 4c 20 29 20 f( iBio==NULL )
0f30: 7b 0a 20 20 20 20 73 73 6c 5f 73 65 74 5f 65 72 {. ssl_set_er
0f40: 72 6d 73 67 28 22 53 53 4c 3a 20 63 61 6e 6e 6f rmsg("SSL: canno
0f50: 74 20 6f 70 65 6e 20 53 53 4c 20 28 25 73 29 22 t open SSL (%s)"
0f60: 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
0f70: 20 20 20 20 20 20 20 45 52 52 5f 72 65 61 73 6f ERR_reaso
0f80: 6e 5f 65 72 72 6f 72 5f 73 74 72 69 6e 67 28 45 n_error_string(E
0f90: 52 52 5f 67 65 74 5f 65 72 72 6f 72 28 29 29 29 RR_get_error()))
0fa0: 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 20 ;. return 1;
0fb0: 20 20 20 0a 20 20 7d 0a 20 20 0a 20 20 63 68 61 . }. . cha
0fc0: 72 20 2a 63 6f 6e 6e 53 74 72 20 3d 20 6d 70 72 r *connStr = mpr
0fd0: 69 6e 74 66 28 22 25 73 3a 25 64 22 2c 20 67 2e intf("%s:%d", g.
0fe0: 75 72 6c 4e 61 6d 65 2c 20 67 2e 75 72 6c 50 6f urlName, g.urlPo
0ff0: 72 74 29 3b 0a 20 20 42 49 4f 5f 73 65 74 5f 63 rt);. BIO_set_c
1000: 6f 6e 6e 5f 68 6f 73 74 6e 61 6d 65 28 69 42 69 onn_hostname(iBi
1010: 6f 2c 20 63 6f 6e 6e 53 74 72 29 3b 0a 20 20 66 o, connStr);. f
1020: 72 65 65 28 63 6f 6e 6e 53 74 72 29 3b 0a 20 20 ree(connStr);.
1030: 0a 20 20 69 66 28 20 42 49 4f 5f 64 6f 5f 63 6f . if( BIO_do_co
1040: 6e 6e 65 63 74 28 69 42 69 6f 29 3c 3d 30 20 29 nnect(iBio)<=0 )
1050: 7b 0a 20 20 20 20 73 73 6c 5f 73 65 74 5f 65 72 {. ssl_set_er
1060: 72 6d 73 67 28 22 53 53 4c 3a 20 63 61 6e 6e 6f rmsg("SSL: canno
1070: 74 20 63 6f 6e 6e 65 63 74 20 74 6f 20 68 6f 73 t connect to hos
1080: 74 20 25 73 3a 25 64 20 28 25 73 29 22 2c 20 0a t %s:%d (%s)", .
1090: 20 20 20 20 20 20 20 20 67 2e 75 72 6c 4e 61 6d g.urlNam
10a0: 65 2c 20 67 2e 75 72 6c 50 6f 72 74 2c 20 45 52 e, g.urlPort, ER
10b0: 52 5f 72 65 61 73 6f 6e 5f 65 72 72 6f 72 5f 73 R_reason_error_s
10c0: 74 72 69 6e 67 28 45 52 52 5f 67 65 74 5f 65 72 tring(ERR_get_er
10d0: 72 6f 72 28 29 29 29 3b 0a 20 20 20 20 73 73 6c ror()));. ssl
10e0: 5f 63 6c 6f 73 65 28 29 3b 0a 20 20 20 20 72 65 _close();. re
10f0: 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 turn 1;. }. .
1100: 20 69 66 28 20 42 49 4f 5f 64 6f 5f 68 61 6e 64 if( BIO_do_hand
1110: 73 68 61 6b 65 28 69 42 69 6f 29 3c 3d 30 20 29 shake(iBio)<=0 )
1120: 20 7b 0a 20 20 20 20 73 73 6c 5f 73 65 74 5f 65 {. ssl_set_e
1130: 72 72 6d 73 67 28 22 45 72 72 6f 72 20 65 73 74 rrmsg("Error est
1140: 61 62 6c 69 73 68 69 6e 67 20 53 53 4c 20 63 6f ablishing SSL co
1150: 6e 6e 65 63 74 69 6f 6e 20 25 73 3a 25 64 20 28 nnection %s:%d (
1160: 25 73 29 22 2c 20 0a 20 20 20 20 20 20 20 20 67 %s)", . g
1170: 2e 75 72 6c 4e 61 6d 65 2c 20 67 2e 75 72 6c 50 .urlName, g.urlP
1180: 6f 72 74 2c 20 45 52 52 5f 72 65 61 73 6f 6e 5f ort, ERR_reason_
1190: 65 72 72 6f 72 5f 73 74 72 69 6e 67 28 45 52 52 error_string(ERR
11a0: 5f 67 65 74 5f 65 72 72 6f 72 28 29 29 29 3b 0a _get_error()));.
11b0: 20 20 20 20 73 73 6c 5f 63 6c 6f 73 65 28 29 3b ssl_close();
11c0: 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
11d0: 20 7d 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 }. /* Check if
11e0: 20 63 65 72 74 69 66 69 63 61 74 65 20 69 73 20 certificate is
11f0: 76 61 6c 69 64 20 2a 2f 0a 20 20 63 65 72 74 20 valid */. cert
1200: 3d 20 53 53 4c 5f 67 65 74 5f 70 65 65 72 5f 63 = SSL_get_peer_c
1210: 65 72 74 69 66 69 63 61 74 65 28 73 73 6c 29 3b ertificate(ssl);
1220: 0a 0a 20 20 69 66 20 28 20 63 65 72 74 3d 3d 4e .. if ( cert==N
1230: 55 4c 4c 20 29 7b 0a 20 20 20 20 73 73 6c 5f 73 ULL ){. ssl_s
1240: 65 74 5f 65 72 72 6d 73 67 28 22 4e 6f 20 53 53 et_errmsg("No SS
1250: 4c 20 63 65 72 74 69 66 69 63 61 74 65 20 77 61 L certificate wa
1260: 73 20 70 72 65 73 65 6e 74 65 64 20 62 79 20 74 s presented by t
1270: 68 65 20 70 65 65 72 22 29 3b 0a 20 20 20 20 73 he peer");. s
1280: 73 6c 5f 63 6c 6f 73 65 28 29 3b 0a 20 20 20 20 sl_close();.
1290: 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 return 1;. }..
12a0: 20 69 66 28 20 53 53 4c 5f 67 65 74 5f 76 65 72 if( SSL_get_ver
12b0: 69 66 79 5f 72 65 73 75 6c 74 28 73 73 6c 29 20 ify_result(ssl)
12c0: 21 3d 20 58 35 30 39 5f 56 5f 4f 4b 20 29 7b 0a != X509_V_OK ){.
12d0: 20 20 20 20 63 68 61 72 20 2a 64 65 73 63 2c 20 char *desc,
12e0: 2a 70 72 6f 6d 70 74 3b 0a 20 20 20 20 42 49 4f *prompt;. BIO
12f0: 20 2a 6d 65 6d 3b 0a 20 20 20 20 0a 20 20 20 20 *mem;. .
1300: 6d 65 6d 20 3d 20 42 49 4f 5f 6e 65 77 28 42 49 mem = BIO_new(BI
1310: 4f 5f 73 5f 6d 65 6d 28 29 29 3b 0a 20 20 20 20 O_s_mem());.
1320: 58 35 30 39 5f 4e 41 4d 45 5f 70 72 69 6e 74 5f X509_NAME_print_
1330: 65 78 28 6d 65 6d 2c 20 58 35 30 39 5f 67 65 74 ex(mem, X509_get
1340: 5f 73 75 62 6a 65 63 74 5f 6e 61 6d 65 28 63 65 _subject_name(ce
1350: 72 74 29 2c 20 32 2c 20 58 4e 5f 46 4c 41 47 5f rt), 2, XN_FLAG_
1360: 4d 55 4c 54 49 4c 49 4e 45 29 3b 0a 20 20 20 20 MULTILINE);.
1370: 42 49 4f 5f 70 75 74 73 28 6d 65 6d 2c 20 22 5c BIO_puts(mem, "\
1380: 6e 5c 6e 49 73 73 75 65 64 20 42 79 3a 5c 6e 5c n\nIssued By:\n\
1390: 6e 22 29 3b 0a 20 20 20 20 58 35 30 39 5f 4e 41 n");. X509_NA
13a0: 4d 45 5f 70 72 69 6e 74 5f 65 78 28 6d 65 6d 2c ME_print_ex(mem,
13b0: 20 58 35 30 39 5f 67 65 74 5f 69 73 73 75 65 72 X509_get_issuer
13c0: 5f 6e 61 6d 65 28 63 65 72 74 29 2c 20 32 2c 20 _name(cert), 2,
13d0: 58 4e 5f 46 4c 41 47 5f 4d 55 4c 54 49 4c 49 4e XN_FLAG_MULTILIN
13e0: 45 29 3b 0a 20 20 20 20 42 49 4f 5f 77 72 69 74 E);. BIO_writ
13f0: 65 28 6d 65 6d 2c 20 22 22 2c 20 31 29 3b 20 2f e(mem, "", 1); /
1400: 2f 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 / null-terminate
1410: 20 6d 65 6d 20 62 75 66 66 65 72 0a 20 20 20 20 mem buffer.
1420: 42 49 4f 5f 67 65 74 5f 6d 65 6d 5f 64 61 74 61 BIO_get_mem_data
1430: 28 6d 65 6d 2c 20 26 64 65 73 63 29 3b 0a 20 20 (mem, &desc);.
1440: 20 20 0a 20 20 20 20 63 68 61 72 20 2a 77 61 72 . char *war
1450: 6e 69 6e 67 20 3d 20 22 22 3b 0a 20 20 20 20 69 ning = "";. i
1460: 66 28 20 68 61 73 53 61 76 65 64 43 65 72 74 69 f( hasSavedCerti
1470: 66 69 63 61 74 65 20 29 7b 0a 20 20 20 20 20 20 ficate ){.
1480: 77 61 72 6e 69 6e 67 20 3d 20 22 57 41 52 4e 49 warning = "WARNI
1490: 4e 47 3a 20 43 65 72 74 69 66 69 63 61 74 65 20 NG: Certificate
14a0: 64 6f 65 73 6e 27 74 20 6d 61 74 63 68 20 74 68 doesn't match th
14b0: 65 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 e ".
14c0: 20 20 20 20 22 73 61 76 65 64 20 63 65 72 74 69 "saved certi
14d0: 66 69 63 61 74 65 20 66 6f 72 20 74 68 69 73 20 ficate for this
14e0: 68 6f 73 74 21 22 3b 0a 20 20 20 20 7d 0a 20 20 host!";. }.
14f0: 20 20 70 72 6f 6d 70 74 20 3d 20 6d 70 72 69 6e prompt = mprin
1500: 74 66 28 22 5c 6e 55 6e 6b 6e 6f 77 6e 20 53 53 tf("\nUnknown SS
1510: 4c 20 63 65 72 74 69 66 69 63 61 74 65 3a 5c 6e L certificate:\n
1520: 5c 6e 25 73 5c 6e 5c 6e 25 73 5c 6e 22 0a 20 20 \n%s\n\n%s\n".
1530: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1540: 20 20 20 22 41 63 63 65 70 74 20 63 65 72 74 69 "Accept certi
1550: 66 69 63 61 74 65 20 5b 61 3d 61 6c 77 61 79 73 ficate [a=always
1560: 2f 79 2f 4e 5d 3f 20 22 2c 20 64 65 73 63 2c 20 /y/N]? ", desc,
1570: 77 61 72 6e 69 6e 67 29 3b 0a 20 20 20 20 42 49 warning);. BI
1580: 4f 5f 66 72 65 65 28 6d 65 6d 29 3b 0a 0a 20 20 O_free(mem);..
1590: 20 20 42 6c 6f 62 20 61 6e 73 3b 0a 20 20 20 20 Blob ans;.
15a0: 62 6c 6f 62 5f 7a 65 72 6f 28 26 61 6e 73 29 3b blob_zero(&ans);
15b0: 0a 20 20 20 20 70 72 6f 6d 70 74 5f 75 73 65 72 . prompt_user
15c0: 28 70 72 6f 6d 70 74 2c 20 26 61 6e 73 29 3b 0a (prompt, &ans);.
15d0: 20 20 20 20 66 72 65 65 28 70 72 6f 6d 70 74 29 free(prompt)
15e0: 3b 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 73 ;. if( blob_s
15f0: 74 72 28 26 61 6e 73 29 5b 30 5d 21 3d 27 79 27 tr(&ans)[0]!='y'
1600: 20 26 26 20 62 6c 6f 62 5f 73 74 72 28 26 61 6e && blob_str(&an
1610: 73 29 5b 30 5d 21 3d 27 61 27 20 29 20 7b 0a 20 s)[0]!='a' ) {.
1620: 20 20 20 20 20 58 35 30 39 5f 66 72 65 65 28 63 X509_free(c
1630: 65 72 74 29 3b 0a 20 20 20 20 20 20 73 73 6c 5f ert);. ssl_
1640: 73 65 74 5f 65 72 72 6d 73 67 28 22 53 53 4c 20 set_errmsg("SSL
1650: 63 65 72 74 69 66 69 63 61 74 65 20 64 65 63 6c certificate decl
1660: 69 6e 65 64 22 29 3b 0a 20 20 20 20 20 20 73 73 ined");. ss
1670: 6c 5f 63 6c 6f 73 65 28 29 3b 0a 20 20 20 20 20 l_close();.
1680: 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
1690: 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 73 74 . if( blob_st
16a0: 72 28 26 61 6e 73 29 5b 30 5d 3d 3d 27 61 27 20 r(&ans)[0]=='a'
16b0: 29 20 7b 0a 20 20 20 20 20 20 73 73 6c 5f 73 61 ) {. ssl_sa
16c0: 76 65 5f 63 65 72 74 69 66 69 63 61 74 65 28 63 ve_certificate(c
16d0: 65 72 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ert);. }. }.
16e0: 20 20 58 35 30 39 5f 66 72 65 65 28 63 65 72 74 X509_free(cert
16f0: 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d );. return 0;.}
1700: 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 63 65 72 ../*.** Save cer
1710: 74 69 66 69 63 61 74 65 20 74 6f 20 67 6c 6f 62 tificate to glob
1720: 61 6c 20 63 6f 6e 66 69 67 2e 0a 2a 2f 0a 76 6f al config..*/.vo
1730: 69 64 20 73 73 6c 5f 73 61 76 65 5f 63 65 72 74 id ssl_save_cert
1740: 69 66 69 63 61 74 65 28 58 35 30 39 20 2a 63 65 ificate(X509 *ce
1750: 72 74 29 7b 0a 20 20 42 49 4f 20 2a 6d 65 6d 3b rt){. BIO *mem;
1760: 0a 20 20 63 68 61 72 20 2a 7a 43 65 72 74 2c 20 . char *zCert,
1770: 2a 7a 48 6f 73 74 3b 0a 0a 20 20 6d 65 6d 20 3d *zHost;.. mem =
1780: 20 42 49 4f 5f 6e 65 77 28 42 49 4f 5f 73 5f 6d BIO_new(BIO_s_m
1790: 65 6d 28 29 29 3b 0a 20 20 50 45 4d 5f 77 72 69 em());. PEM_wri
17a0: 74 65 5f 62 69 6f 5f 58 35 30 39 28 6d 65 6d 2c te_bio_X509(mem,
17b0: 20 63 65 72 74 29 3b 0a 20 20 42 49 4f 5f 77 72 cert);. BIO_wr
17c0: 69 74 65 28 6d 65 6d 2c 20 22 22 2c 20 31 29 3b ite(mem, "", 1);
17d0: 20 2f 2f 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 // null-termina
17e0: 74 65 20 6d 65 6d 20 62 75 66 66 65 72 0a 20 20 te mem buffer.
17f0: 42 49 4f 5f 67 65 74 5f 6d 65 6d 5f 64 61 74 61 BIO_get_mem_data
1800: 28 6d 65 6d 2c 20 26 7a 43 65 72 74 29 3b 0a 20 (mem, &zCert);.
1810: 20 7a 48 6f 73 74 20 3d 20 6d 70 72 69 6e 74 66 zHost = mprintf
1820: 28 22 63 65 72 74 3a 25 73 22 2c 20 67 2e 75 72 ("cert:%s", g.ur
1830: 6c 4e 61 6d 65 29 3b 0a 20 20 64 62 5f 73 65 74 lName);. db_set
1840: 28 7a 48 6f 73 74 2c 20 7a 43 65 72 74 2c 20 31 (zHost, zCert, 1
1850: 29 3b 0a 20 20 66 72 65 65 28 7a 48 6f 73 74 29 );. free(zHost)
1860: 3b 0a 20 20 42 49 4f 5f 66 72 65 65 28 6d 65 6d ;. BIO_free(mem
1870: 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 ); .}../*.** Ge
1880: 74 20 63 65 72 74 69 66 69 63 61 74 65 20 66 6f t certificate fo
1890: 72 20 67 2e 75 72 6c 4e 61 6d 65 20 66 72 6f 6d r g.urlName from
18a0: 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 2e 0a global config..
18b0: 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 ** Return NULL i
18c0: 66 20 6e 6f 20 63 65 72 74 69 66 69 63 61 74 65 f no certificate
18d0: 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 58 35 30 39 20 found..*/.X509
18e0: 2a 73 73 6c 5f 67 65 74 5f 63 65 72 74 69 66 69 *ssl_get_certifi
18f0: 63 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 63 68 cate(void){. ch
1900: 61 72 20 2a 7a 48 6f 73 74 2c 20 2a 7a 43 65 72 ar *zHost, *zCer
1910: 74 3b 0a 20 20 42 49 4f 20 2a 6d 65 6d 3b 0a 20 t;. BIO *mem;.
1920: 20 58 35 30 39 20 2a 63 65 72 74 3b 0a 0a 20 20 X509 *cert;..
1930: 7a 48 6f 73 74 20 3d 20 6d 70 72 69 6e 74 66 28 zHost = mprintf(
1940: 22 63 65 72 74 3a 25 73 22 2c 20 67 2e 75 72 6c "cert:%s", g.url
1950: 4e 61 6d 65 29 3b 0a 20 20 7a 43 65 72 74 20 3d Name);. zCert =
1960: 20 64 62 5f 67 65 74 28 7a 48 6f 73 74 2c 20 4e db_get(zHost, N
1970: 55 4c 4c 29 3b 0a 20 20 66 72 65 65 28 7a 48 6f ULL);. free(zHo
1980: 73 74 29 3b 0a 20 20 69 66 20 28 20 7a 43 65 72 st);. if ( zCer
1990: 74 3d 3d 4e 55 4c 4c 20 29 0a 20 20 20 20 72 65 t==NULL ). re
19a0: 74 75 72 6e 20 4e 55 4c 4c 3b 0a 20 20 6d 65 6d turn NULL;. mem
19b0: 20 3d 20 42 49 4f 5f 6e 65 77 28 42 49 4f 5f 73 = BIO_new(BIO_s
19c0: 5f 6d 65 6d 28 29 29 3b 0a 20 20 42 49 4f 5f 70 _mem());. BIO_p
19d0: 75 74 73 28 6d 65 6d 2c 20 7a 43 65 72 74 29 3b uts(mem, zCert);
19e0: 0a 20 20 63 65 72 74 20 3d 20 50 45 4d 5f 72 65 . cert = PEM_re
19f0: 61 64 5f 62 69 6f 5f 58 35 30 39 28 6d 65 6d 2c ad_bio_X509(mem,
1a00: 20 4e 55 4c 4c 2c 20 30 2c 20 4e 55 4c 4c 29 3b NULL, 0, NULL);
1a10: 0a 20 20 66 72 65 65 28 7a 43 65 72 74 29 3b 0a . free(zCert);.
1a20: 20 20 42 49 4f 5f 66 72 65 65 28 6d 65 6d 29 3b BIO_free(mem);
1a30: 20 20 0a 20 20 72 65 74 75 72 6e 20 63 65 72 74 . return cert
1a40: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 6e 64 20 ;.}../*.** Send
1a50: 63 6f 6e 74 65 6e 74 20 6f 75 74 20 6f 76 65 72 content out over
1a60: 20 74 68 65 20 53 53 4c 20 63 6f 6e 6e 65 63 74 the SSL connect
1a70: 69 6f 6e 2e 0a 2a 2f 0a 73 69 7a 65 5f 74 20 73 ion..*/.size_t s
1a80: 73 6c 5f 73 65 6e 64 28 76 6f 69 64 20 2a 4e 6f sl_send(void *No
1a90: 74 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 43 6f tUsed, void *pCo
1aa0: 6e 74 65 6e 74 2c 20 73 69 7a 65 5f 74 20 4e 29 ntent, size_t N)
1ab0: 7b 0a 20 20 73 69 7a 65 5f 74 20 73 65 6e 74 3b {. size_t sent;
1ac0: 0a 20 20 73 69 7a 65 5f 74 20 74 6f 74 61 6c 20 . size_t total
1ad0: 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 4e 3e = 0;. while( N>
1ae0: 30 20 29 7b 0a 20 20 20 20 73 65 6e 74 20 3d 20 0 ){. sent =
1af0: 42 49 4f 5f 77 72 69 74 65 28 69 42 69 6f 2c 20 BIO_write(iBio,
1b00: 70 43 6f 6e 74 65 6e 74 2c 20 4e 29 3b 0a 20 20 pContent, N);.
1b10: 20 20 69 66 28 20 73 65 6e 74 3c 3d 30 20 29 20 if( sent<=0 )
1b20: 62 72 65 61 6b 3b 0a 20 20 20 20 74 6f 74 61 6c break;. total
1b30: 20 2b 3d 20 73 65 6e 74 3b 0a 20 20 20 20 4e 20 += sent;. N
1b40: 2d 3d 20 73 65 6e 74 3b 0a 20 20 20 20 70 43 6f -= sent;. pCo
1b50: 6e 74 65 6e 74 20 3d 20 28 76 6f 69 64 2a 29 26 ntent = (void*)&
1b60: 28 28 63 68 61 72 2a 29 70 43 6f 6e 74 65 6e 74 ((char*)pContent
1b70: 29 5b 73 65 6e 74 5d 3b 0a 20 20 7d 0a 20 20 72 )[sent];. }. r
1b80: 65 74 75 72 6e 20 74 6f 74 61 6c 3b 0a 7d 0a 0a eturn total;.}..
1b90: 2f 2a 0a 2a 2a 20 52 65 63 65 69 76 65 20 63 6f /*.** Receive co
1ba0: 6e 74 65 6e 74 20 62 61 63 6b 20 66 72 6f 6d 20 ntent back from
1bb0: 74 68 65 20 53 53 4c 20 63 6f 6e 6e 65 63 74 69 the SSL connecti
1bc0: 6f 6e 2e 0a 2a 2f 0a 73 69 7a 65 5f 74 20 73 73 on..*/.size_t ss
1bd0: 6c 5f 72 65 63 65 69 76 65 28 76 6f 69 64 20 2a l_receive(void *
1be0: 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 NotUsed, void *p
1bf0: 43 6f 6e 74 65 6e 74 2c 20 73 69 7a 65 5f 74 20 Content, size_t
1c00: 4e 29 7b 0a 20 20 73 69 7a 65 5f 74 20 67 6f 74 N){. size_t got
1c10: 3b 0a 20 20 73 69 7a 65 5f 74 20 74 6f 74 61 6c ;. size_t total
1c20: 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 4e = 0;. while( N
1c30: 3e 30 20 29 7b 0a 20 20 20 20 67 6f 74 20 3d 20 >0 ){. got =
1c40: 42 49 4f 5f 72 65 61 64 28 69 42 69 6f 2c 20 70 BIO_read(iBio, p
1c50: 43 6f 6e 74 65 6e 74 2c 20 4e 29 3b 0a 20 20 20 Content, N);.
1c60: 20 69 66 28 20 67 6f 74 3c 3d 30 20 29 20 62 72 if( got<=0 ) br
1c70: 65 61 6b 3b 0a 20 20 20 20 74 6f 74 61 6c 20 2b eak;. total +
1c80: 3d 20 67 6f 74 3b 0a 20 20 20 20 4e 20 2d 3d 20 = got;. N -=
1c90: 67 6f 74 3b 0a 20 20 20 20 70 43 6f 6e 74 65 6e got;. pConten
1ca0: 74 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 68 t = (void*)&((ch
1cb0: 61 72 2a 29 70 43 6f 6e 74 65 6e 74 29 5b 67 6f ar*)pContent)[go
1cc0: 74 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e t];. }. return
1cd0: 20 74 6f 74 61 6c 3b 0a 7d 0a 0a 23 65 6e 64 69 total;.}..#endi
1ce0: 66 20 2f 2a 20 46 4f 53 53 49 4c 5f 45 4e 41 42 f /* FOSSIL_ENAB
1cf0: 4c 45 5f 53 53 4c 20 2a 2f 0a LE_SSL */.