Artifact
5bf94ee7f7a60571493d0815f02efd83b7c484dc:
0000: 2f 2a 0a 20 2a 20 43 6f 70 79 72 69 67 68 74 20 /*. * Copyright
0010: 28 43 29 20 31 39 39 37 2d 32 30 30 30 20 4d 61 (C) 1997-2000 Ma
0020: 74 74 20 4e 65 77 6d 61 6e 20 3c 6d 61 74 74 40 tt Newman <matt@
0030: 6e 6f 76 61 64 69 67 6d 2e 63 6f 6d 3e 0a 20 2a novadigm.com>. *
0040: 0a 20 2a 20 54 4c 53 20 28 61 6b 61 20 53 53 4c . * TLS (aka SSL
0050: 29 20 43 68 61 6e 6e 65 6c 20 2d 20 63 61 6e 20 ) Channel - can
0060: 62 65 20 6c 61 79 65 72 65 64 20 6f 6e 20 61 6e be layered on an
0070: 79 20 62 69 2d 64 69 72 65 63 74 69 6f 6e 61 6c y bi-directional
0080: 0a 20 2a 20 54 63 6c 5f 43 68 61 6e 6e 65 6c 20 . * Tcl_Channel
0090: 28 4e 6f 74 65 3a 20 52 65 71 75 69 72 65 73 20 (Note: Requires
00a0: 54 72 66 20 43 6f 72 65 20 50 61 74 63 68 29 0a Trf Core Patch).
00b0: 20 2a 0a 20 2a 20 54 68 69 73 20 77 61 73 20 62 *. * This was b
00c0: 75 69 6c 74 20 66 72 6f 6d 20 73 63 72 61 74 63 uilt from scratc
00d0: 68 20 62 61 73 65 64 20 75 70 6f 6e 20 6f 62 73 h based upon obs
00e0: 65 72 76 61 74 69 6f 6e 20 6f 66 20 4f 70 65 6e ervation of Open
00f0: 53 53 4c 20 30 2e 39 2e 32 42 0a 20 2a 0a 20 2a SSL 0.9.2B. *. *
0100: 20 41 64 64 69 74 69 6f 6e 20 63 72 65 64 69 74 Addition credit
0110: 20 69 73 20 64 75 65 20 66 6f 72 20 41 6e 64 72 is due for Andr
0120: 65 61 73 20 4b 75 70 72 69 65 73 20 28 61 2e 6b eas Kupries (a.k
0130: 75 70 72 69 65 73 40 77 65 73 74 65 6e 64 2e 63 upries@westend.c
0140: 6f 6d 29 2c 20 66 6f 72 0a 20 2a 20 70 72 6f 76 om), for. * prov
0150: 69 64 69 6e 67 20 74 68 65 20 54 63 6c 5f 52 65 iding the Tcl_Re
0160: 70 6c 61 63 65 43 68 61 6e 6e 65 6c 20 6d 65 63 placeChannel mec
0170: 68 61 6e 69 73 6d 20 61 6e 64 20 77 6f 72 6b 69 hanism and worki
0180: 6e 67 20 63 6c 6f 73 65 6c 79 20 77 69 74 68 20 ng closely with
0190: 6d 65 0a 20 2a 20 74 6f 20 65 6e 68 61 6e 63 65 me. * to enhance
01a0: 20 69 74 20 74 6f 20 73 75 70 70 6f 72 74 20 66 it to support f
01b0: 75 6c 6c 20 66 69 6c 65 65 76 65 6e 74 20 73 65 ull fileevent se
01c0: 6d 61 6e 74 69 63 73 2e 0a 20 2a 0a 20 2a 20 41 mantics.. *. * A
01d0: 6c 73 6f 20 77 6f 72 6b 20 64 6f 6e 65 20 62 79 lso work done by
01e0: 20 74 68 65 20 66 6f 6c 6c 6f 77 20 70 65 6f 70 the follow peop
01f0: 6c 65 20 70 72 6f 76 69 64 65 64 20 74 68 65 20 le provided the
0200: 69 6d 70 65 74 75 73 20 74 6f 20 64 6f 20 74 68 impetus to do th
0210: 69 73 20 22 72 69 67 68 74 22 3a 2d 0a 20 2a 09 is "right":-. *.
0220: 74 63 6c 53 53 4c 20 28 43 6f 6c 69 6e 20 4d 63 tclSSL (Colin Mc
0230: 43 6f 72 6d 61 63 6b 2c 20 53 68 61 72 65 64 20 Cormack, Shared
0240: 54 65 63 68 6e 6f 6c 6f 67 79 29 0a 20 2a 09 53 Technology). *.S
0250: 53 4c 74 63 6c 20 28 50 65 74 65 72 20 41 6e 74 SLtcl (Peter Ant
0260: 6d 61 6e 29 0a 20 2a 0a 20 2a 2f 0a 23 69 66 6e man). *. */.#ifn
0270: 64 65 66 20 5f 54 4c 53 49 4e 54 5f 48 0a 23 64 def _TLSINT_H.#d
0280: 65 66 69 6e 65 20 5f 54 4c 53 49 4e 54 5f 48 0a efine _TLSINT_H.
0290: 0a 23 69 6e 63 6c 75 64 65 20 22 74 6c 73 2e 68 .#include "tls.h
02a0: 22 0a 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e ".#include <errn
02b0: 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 o.h>.#include <s
02c0: 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 tring.h>.#includ
02d0: 65 20 3c 73 74 64 69 6e 74 2e 68 3e 0a 0a 23 69 e <stdint.h>..#i
02e0: 66 64 65 66 20 5f 5f 57 49 4e 33 32 5f 5f 0a 23 fdef __WIN32__.#
02f0: 64 65 66 69 6e 65 20 57 49 4e 33 32 5f 4c 45 41 define WIN32_LEA
0300: 4e 5f 41 4e 44 5f 4d 45 41 4e 0a 23 69 6e 63 6c N_AND_MEAN.#incl
0310: 75 64 65 20 3c 77 69 6e 64 6f 77 73 2e 68 3e 0a ude <windows.h>.
0320: 23 69 6e 63 6c 75 64 65 20 3c 77 69 6e 63 72 79 #include <wincry
0330: 70 74 2e 68 3e 20 2f 2a 20 4f 70 65 6e 53 53 4c pt.h> /* OpenSSL
0340: 20 6e 65 65 64 73 20 74 68 69 73 20 6f 6e 20 57 needs this on W
0350: 69 6e 64 6f 77 73 20 2a 2f 0a 23 65 6e 64 69 66 indows */.#endif
0360: 0a 0a 2f 2a 20 48 61 6e 64 6c 65 20 74 63 6c 38 ../* Handle tcl8
0370: 2e 33 2d 3e 74 63 6c 38 2e 34 20 43 4f 4e 53 54 .3->tcl8.4 CONST
0380: 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 69 66 6e changes */.#ifn
0390: 64 65 66 20 43 4f 4e 53 54 38 34 0a 23 64 65 66 def CONST84.#def
03a0: 69 6e 65 20 43 4f 4e 53 54 38 34 0a 23 65 6e 64 ine CONST84.#end
03b0: 69 66 0a 0a 23 69 66 64 65 66 20 4e 4f 5f 50 41 if..#ifdef NO_PA
03c0: 54 45 4e 54 53 0a 23 20 20 64 65 66 69 6e 65 20 TENTS.# define
03d0: 4e 4f 5f 49 44 45 41 0a 23 20 20 64 65 66 69 6e NO_IDEA.# defin
03e0: 65 20 4e 4f 5f 52 43 32 0a 23 20 20 64 65 66 69 e NO_RC2.# defi
03f0: 6e 65 20 4e 4f 5f 52 43 34 0a 23 20 20 64 65 66 ne NO_RC4.# def
0400: 69 6e 65 20 4e 4f 5f 52 43 35 0a 23 20 20 64 65 ine NO_RC5.# de
0410: 66 69 6e 65 20 4e 4f 5f 52 53 41 0a 23 20 20 69 fine NO_RSA.# i
0420: 66 6e 64 65 66 20 4e 4f 5f 53 53 4c 32 0a 23 20 fndef NO_SSL2.#
0430: 20 20 20 64 65 66 69 6e 65 20 4e 4f 5f 53 53 4c define NO_SSL
0440: 32 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 2.# endif.#endi
0450: 66 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 6f 70 65 f..#include <ope
0460: 6e 73 73 6c 2f 73 73 6c 2e 68 3e 0a 23 69 6e 63 nssl/ssl.h>.#inc
0470: 6c 75 64 65 20 3c 6f 70 65 6e 73 73 6c 2f 65 72 lude <openssl/er
0480: 72 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 6f r.h>.#include <o
0490: 70 65 6e 73 73 6c 2f 72 61 6e 64 2e 68 3e 0a 23 penssl/rand.h>.#
04a0: 69 6e 63 6c 75 64 65 20 3c 6f 70 65 6e 73 73 6c include <openssl
04b0: 2f 6f 70 65 6e 73 73 6c 76 2e 68 3e 0a 0a 2f 2a /opensslv.h>../*
04c0: 0a 20 2a 20 44 65 74 65 72 6d 69 6e 65 20 69 66 . * Determine if
04d0: 20 77 65 20 73 68 6f 75 6c 64 20 75 73 65 20 74 we should use t
04e0: 68 65 20 70 72 65 2d 4f 70 65 6e 53 53 4c 20 31 he pre-OpenSSL 1
04f0: 2e 31 2e 30 20 41 50 49 0a 20 2a 2f 0a 23 75 6e .1.0 API. */.#un
0500: 64 65 66 20 54 43 4c 54 4c 53 5f 4f 50 45 4e 53 def TCLTLS_OPENS
0510: 53 4c 5f 50 52 45 5f 31 5f 31 0a 23 69 66 20 28 SL_PRE_1_1.#if (
0520: 64 65 66 69 6e 65 64 28 4c 49 42 52 45 53 53 4c defined(LIBRESSL
0530: 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 29 _VERSION_NUMBER)
0540: 29 20 7c 7c 20 4f 50 45 4e 53 53 4c 5f 56 45 52 ) || OPENSSL_VER
0550: 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 3c 20 30 78 SION_NUMBER < 0x
0560: 31 30 31 30 30 30 30 30 4c 0a 23 20 20 64 65 66 10100000L.# def
0570: 69 6e 65 20 54 43 4c 54 4c 53 5f 4f 50 45 4e 53 ine TCLTLS_OPENS
0580: 53 4c 5f 50 52 45 5f 31 5f 31 5f 41 50 49 20 31 SL_PRE_1_1_API 1
0590: 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 .#endif..#ifndef
05a0: 20 45 43 4f 4e 4e 41 42 4f 52 54 45 44 0a 23 64 ECONNABORTED.#d
05b0: 65 66 69 6e 65 20 45 43 4f 4e 4e 41 42 4f 52 54 efine ECONNABORT
05c0: 45 44 09 31 33 30 09 2f 2a 20 53 6f 66 74 77 61 ED.130./* Softwa
05d0: 72 65 20 63 61 75 73 65 64 20 63 6f 6e 6e 65 63 re caused connec
05e0: 74 69 6f 6e 20 61 62 6f 72 74 20 2a 2f 0a 23 65 tion abort */.#e
05f0: 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 45 43 4f ndif.#ifndef ECO
0600: 4e 4e 52 45 53 45 54 0a 23 64 65 66 69 6e 65 20 NNRESET.#define
0610: 45 43 4f 4e 4e 52 45 53 45 54 09 31 33 31 09 2f ECONNRESET.131./
0620: 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 72 65 73 * Connection res
0630: 65 74 20 62 79 20 70 65 65 72 20 2a 2f 0a 23 65 et by peer */.#e
0640: 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 54 43 4c ndif..#ifdef TCL
0650: 45 58 54 5f 54 43 4c 54 4c 53 5f 44 45 42 55 47 EXT_TCLTLS_DEBUG
0660: 0a 23 69 6e 63 6c 75 64 65 20 3c 63 74 79 70 65 .#include <ctype
0670: 2e 68 3e 0a 23 64 65 66 69 6e 65 20 64 70 72 69 .h>.#define dpri
0680: 6e 74 66 28 2e 2e 2e 29 20 7b 20 66 70 72 69 6e ntf(...) { fprin
0690: 74 66 28 73 74 64 65 72 72 2c 20 22 25 73 3a 25 tf(stderr, "%s:%
06a0: 69 3a 25 73 28 29 3a 22 2c 20 5f 5f 46 49 4c 45 i:%s():", __FILE
06b0: 5f 5f 2c 20 5f 5f 4c 49 4e 45 5f 5f 2c 20 5f 5f __, __LINE__, __
06c0: 66 75 6e 63 5f 5f 29 3b 20 66 70 72 69 6e 74 66 func__); fprintf
06d0: 28 73 74 64 65 72 72 2c 20 5f 5f 56 41 5f 41 52 (stderr, __VA_AR
06e0: 47 53 5f 5f 29 3b 20 66 70 72 69 6e 74 66 28 73 GS__); fprintf(s
06f0: 74 64 65 72 72 2c 20 22 5c 6e 22 29 3b 20 7d 0a tderr, "\n"); }.
0700: 23 64 65 66 69 6e 65 20 64 70 72 69 6e 74 42 75 #define dprintBu
0710: 66 66 65 72 28 62 75 66 66 65 72 4e 61 6d 65 2c ffer(bufferName,
0720: 20 62 75 66 66 65 72 4c 65 6e 67 74 68 29 20 7b bufferLength) {
0730: 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
0740: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0750: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0760: 20 20 20 20 69 6e 74 20 64 70 72 69 6e 74 42 75 int dprintBu
0770: 66 66 65 72 49 64 78 3b 20 5c 0a 20 20 20 20 20 fferIdx; \.
0780: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0790: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
07a0: 20 20 20 20 20 20 20 20 20 20 20 20 75 6e 73 69 unsi
07b0: 67 6e 65 64 20 63 68 61 72 20 64 70 72 69 6e 74 gned char dprint
07c0: 42 75 66 66 65 72 43 68 61 72 3b 20 5c 0a 20 20 BufferChar; \.
07d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
07e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
07f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 f
0800: 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 printf(stderr, "
0810: 25 73 3a 25 69 3a 25 73 28 29 3a 25 73 5b 25 6c %s:%i:%s():%s[%l
0820: 6c 75 5d 3d 7b 22 2c 20 5f 5f 46 49 4c 45 5f 5f lu]={", __FILE__
0830: 2c 20 5f 5f 4c 49 4e 45 5f 5f 2c 20 5f 5f 66 75 , __LINE__, __fu
0840: 6e 63 5f 5f 2c 20 23 62 75 66 66 65 72 4e 61 6d nc__, #bufferNam
0850: 65 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e e, (unsigned lon
0860: 67 20 6c 6f 6e 67 29 20 62 75 66 66 65 72 4c 65 g long) bufferLe
0870: 6e 67 74 68 29 3b 20 5c 0a 20 20 20 20 20 20 20 ngth); \.
0880: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0890: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
08a0: 20 20 20 20 20 20 20 20 20 20 66 6f 72 20 28 64 for (d
08b0: 70 72 69 6e 74 42 75 66 66 65 72 49 64 78 20 3d printBufferIdx =
08c0: 20 30 3b 20 64 70 72 69 6e 74 42 75 66 66 65 72 0; dprintBuffer
08d0: 49 64 78 20 3c 20 62 75 66 66 65 72 4c 65 6e 67 Idx < bufferLeng
08e0: 74 68 3b 20 64 70 72 69 6e 74 42 75 66 66 65 72 th; dprintBuffer
08f0: 49 64 78 2b 2b 29 20 7b 20 5c 0a 20 20 20 20 20 Idx++) { \.
0900: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0910: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0920: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0930: 20 20 20 20 64 70 72 69 6e 74 42 75 66 66 65 72 dprintBuffer
0940: 43 68 61 72 20 3d 20 62 75 66 66 65 72 4e 61 6d Char = bufferNam
0950: 65 5b 64 70 72 69 6e 74 42 75 66 66 65 72 49 64 e[dprintBufferId
0960: 78 5d 3b 20 5c 0a 20 20 20 20 20 20 20 20 20 20 x]; \.
0970: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0980: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0990: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
09a0: 66 20 28 69 73 61 6c 70 68 61 28 64 70 72 69 6e f (isalpha(dprin
09b0: 74 42 75 66 66 65 72 43 68 61 72 29 20 7c 7c 20 tBufferChar) ||
09c0: 69 73 64 69 67 69 74 28 64 70 72 69 6e 74 42 75 isdigit(dprintBu
09d0: 66 66 65 72 43 68 61 72 29 29 20 7b 20 5c 0a 20 fferChar)) { \.
09e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
09f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0a00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0a10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0a20: 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 fprintf(stderr,
0a30: 22 27 25 63 27 20 22 2c 20 64 70 72 69 6e 74 42 "'%c' ", dprintB
0a40: 75 66 66 65 72 43 68 61 72 29 3b 20 5c 0a 20 20 ufferChar); \.
0a50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0a60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0a70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0a80: 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 20 } else {
0a90: 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
0aa0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0ab0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0ac0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0ad0: 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 fprintf(stder
0ae0: 72 2c 20 22 25 30 32 78 20 22 2c 20 28 75 6e 73 r, "%02x ", (uns
0af0: 69 67 6e 65 64 20 69 6e 74 29 20 64 70 72 69 6e igned int) dprin
0b00: 74 42 75 66 66 65 72 43 68 61 72 29 3b 20 5c 0a tBufferChar); \.
0b10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0b20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0b30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0b40: 20 20 20 20 20 20 20 20 20 7d 3b 20 5c 0a 20 20 }; \.
0b50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0b60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0b70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d }
0b80: 3b 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ; \.
0b90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0ba0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0bb0: 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 fprintf(std
0bc0: 65 72 72 2c 20 22 7d 5c 6e 22 29 3b 20 5c 0a 20 err, "}\n"); \.
0bd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0be0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0bf0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a }.
0c00: 23 64 65 66 69 6e 65 20 64 70 72 69 6e 74 46 6c #define dprintFl
0c10: 61 67 73 28 73 74 61 74 65 50 74 72 29 20 7b 20 ags(statePtr) {
0c20: 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
0c30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0c40: 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 fprintf(stderr
0c50: 2c 20 22 25 73 3a 25 69 3a 25 73 28 29 3a 25 73 , "%s:%i:%s():%s
0c60: 2d 3e 66 6c 61 67 73 3d 30 22 2c 20 5f 5f 46 49 ->flags=0", __FI
0c70: 4c 45 5f 5f 2c 20 5f 5f 4c 49 4e 45 5f 5f 2c 20 LE__, __LINE__,
0c80: 5f 5f 66 75 6e 63 5f 5f 2c 20 23 73 74 61 74 65 __func__, #state
0c90: 50 74 72 29 3b 20 5c 0a 20 20 20 20 20 20 20 20 Ptr); \.
0ca0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0cb0: 20 20 20 20 20 20 20 20 69 66 20 28 28 28 73 74 if (((st
0cc0: 61 74 65 50 74 72 29 2d 3e 66 6c 61 67 73 20 26 atePtr)->flags &
0cd0: 20 54 4c 53 5f 54 43 4c 5f 41 53 59 4e 43 29 20 TLS_TCL_ASYNC)
0ce0: 3d 3d 20 54 4c 53 5f 54 43 4c 5f 41 53 59 4e 43 == TLS_TCL_ASYNC
0cf0: 29 20 7b 20 66 70 72 69 6e 74 66 28 73 74 64 65 ) { fprintf(stde
0d00: 72 72 2c 22 7c 54 4c 53 5f 54 43 4c 5f 41 53 59 rr,"|TLS_TCL_ASY
0d10: 4e 43 22 29 3b 20 7d 3b 20 5c 0a 20 20 20 20 20 NC"); }; \.
0d20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0d30: 20 20 20 20 20 20 20 20 20 20 20 69 66 20 28 28 if ((
0d40: 28 73 74 61 74 65 50 74 72 29 2d 3e 66 6c 61 67 (statePtr)->flag
0d50: 73 20 26 20 54 4c 53 5f 54 43 4c 5f 53 45 52 56 s & TLS_TCL_SERV
0d60: 45 52 29 20 3d 3d 20 54 4c 53 5f 54 43 4c 5f 53 ER) == TLS_TCL_S
0d70: 45 52 56 45 52 29 20 7b 20 66 70 72 69 6e 74 66 ERVER) { fprintf
0d80: 28 73 74 64 65 72 72 2c 22 7c 54 4c 53 5f 54 43 (stderr,"|TLS_TC
0d90: 4c 5f 53 45 52 56 45 52 22 29 3b 20 7d 3b 20 5c L_SERVER"); }; \
0da0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
0db0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0dc0: 20 69 66 20 28 28 28 73 74 61 74 65 50 74 72 29 if (((statePtr)
0dd0: 2d 3e 66 6c 61 67 73 20 26 20 54 4c 53 5f 54 43 ->flags & TLS_TC
0de0: 4c 5f 49 4e 49 54 29 20 3d 3d 20 54 4c 53 5f 54 L_INIT) == TLS_T
0df0: 43 4c 5f 49 4e 49 54 29 20 7b 20 66 70 72 69 6e CL_INIT) { fprin
0e00: 74 66 28 73 74 64 65 72 72 2c 22 7c 54 4c 53 5f tf(stderr,"|TLS_
0e10: 54 43 4c 5f 49 4e 49 54 22 29 3b 20 7d 3b 20 5c TCL_INIT"); }; \
0e20: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
0e30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0e40: 20 69 66 20 28 28 28 73 74 61 74 65 50 74 72 29 if (((statePtr)
0e50: 2d 3e 66 6c 61 67 73 20 26 20 54 4c 53 5f 54 43 ->flags & TLS_TC
0e60: 4c 5f 44 45 42 55 47 29 20 3d 3d 20 54 4c 53 5f L_DEBUG) == TLS_
0e70: 54 43 4c 5f 44 45 42 55 47 29 20 7b 20 66 70 72 TCL_DEBUG) { fpr
0e80: 69 6e 74 66 28 73 74 64 65 72 72 2c 22 7c 54 4c intf(stderr,"|TL
0e90: 53 5f 54 43 4c 5f 44 45 42 55 47 22 29 3b 20 7d S_TCL_DEBUG"); }
0ea0: 3b 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ; \.
0eb0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0ec0: 20 20 20 20 69 66 20 28 28 28 73 74 61 74 65 50 if (((stateP
0ed0: 74 72 29 2d 3e 66 6c 61 67 73 20 26 20 54 4c 53 tr)->flags & TLS
0ee0: 5f 54 43 4c 5f 43 41 4c 4c 42 41 43 4b 29 20 3d _TCL_CALLBACK) =
0ef0: 3d 20 54 4c 53 5f 54 43 4c 5f 43 41 4c 4c 42 41 = TLS_TCL_CALLBA
0f00: 43 4b 29 20 7b 20 66 70 72 69 6e 74 66 28 73 74 CK) { fprintf(st
0f10: 64 65 72 72 2c 22 7c 54 4c 53 5f 54 43 4c 5f 43 derr,"|TLS_TCL_C
0f20: 41 4c 4c 42 41 43 4b 22 29 3b 20 7d 3b 20 5c 0a ALLBACK"); }; \.
0f30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0f40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0f50: 69 66 20 28 28 28 73 74 61 74 65 50 74 72 29 2d if (((statePtr)-
0f60: 3e 66 6c 61 67 73 20 26 20 54 4c 53 5f 54 43 4c >flags & TLS_TCL
0f70: 5f 48 41 4e 44 53 48 41 4b 45 5f 46 41 49 4c 45 _HANDSHAKE_FAILE
0f80: 44 29 20 3d 3d 20 54 4c 53 5f 54 43 4c 5f 48 41 D) == TLS_TCL_HA
0f90: 4e 44 53 48 41 4b 45 5f 46 41 49 4c 45 44 29 20 NDSHAKE_FAILED)
0fa0: 7b 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 { fprintf(stderr
0fb0: 2c 22 7c 54 4c 53 5f 54 43 4c 5f 48 41 4e 44 53 ,"|TLS_TCL_HANDS
0fc0: 48 41 4b 45 5f 46 41 49 4c 45 44 22 29 3b 20 7d HAKE_FAILED"); }
0fd0: 3b 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ; \.
0fe0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0ff0: 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 fprintf(stde
1000: 72 72 2c 20 22 5c 6e 22 29 3b 20 5c 0a 20 20 20 rr, "\n"); \.
1010: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1020: 20 20 20 20 20 20 20 20 20 20 20 7d 0a 23 65 6c }.#el
1030: 73 65 0a 23 64 65 66 69 6e 65 20 64 70 72 69 6e se.#define dprin
1040: 74 66 28 2e 2e 2e 29 20 69 66 20 28 30 29 20 7b tf(...) if (0) {
1050: 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
1060: 20 5f 5f 56 41 5f 41 52 47 53 5f 5f 29 3b 20 7d __VA_ARGS__); }
1070: 0a 23 64 65 66 69 6e 65 20 64 70 72 69 6e 74 42 .#define dprintB
1080: 75 66 66 65 72 28 62 75 66 66 65 72 4e 61 6d 65 uffer(bufferName
1090: 2c 20 62 75 66 66 65 72 4c 65 6e 67 74 68 29 20 , bufferLength)
10a0: 2f 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 64 70 72 /**/.#define dpr
10b0: 69 6e 74 46 6c 61 67 73 28 73 74 61 74 65 50 74 intFlags(statePt
10c0: 72 29 20 2f 2a 2a 2f 0a 23 65 6e 64 69 66 0a 0a r) /**/.#endif..
10d0: 23 64 65 66 69 6e 65 20 54 43 4c 54 4c 53 5f 53 #define TCLTLS_S
10e0: 53 4c 5f 45 52 52 4f 52 28 73 73 6c 2c 65 72 72 SL_ERROR(ssl,err
10f0: 29 20 28 28 63 68 61 72 2a 29 45 52 52 5f 72 65 ) ((char*)ERR_re
1100: 61 73 6f 6e 5f 65 72 72 6f 72 5f 73 74 72 69 6e ason_error_strin
1110: 67 28 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 g((unsigned long
1120: 29 53 53 4c 5f 67 65 74 5f 65 72 72 6f 72 28 28 )SSL_get_error((
1130: 73 73 6c 29 2c 28 65 72 72 29 29 29 29 0a 2f 2a ssl),(err))))./*
1140: 0a 20 2a 20 4f 70 65 6e 53 53 4c 20 42 49 4f 20 . * OpenSSL BIO
1150: 52 6f 75 74 69 6e 65 73 0a 20 2a 2f 0a 23 64 65 Routines. */.#de
1160: 66 69 6e 65 20 42 49 4f 5f 54 59 50 45 5f 54 43 fine BIO_TYPE_TC
1170: 4c 09 28 31 39 7c 30 78 30 34 30 30 29 0a 0a 2f L.(19|0x0400)../
1180: 2a 0a 20 2a 20 44 65 66 69 6e 65 73 20 66 6f 72 *. * Defines for
1190: 20 53 74 61 74 65 2e 66 6c 61 67 73 0a 20 2a 2f State.flags. */
11a0: 0a 23 64 65 66 69 6e 65 20 54 4c 53 5f 54 43 4c .#define TLS_TCL
11b0: 5f 41 53 59 4e 43 09 28 31 3c 3c 30 29 09 2f 2a _ASYNC.(1<<0)./*
11c0: 20 6e 6f 6e 2d 62 6c 6f 63 6b 69 6e 67 20 6d 6f non-blocking mo
11d0: 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4c de */.#define TL
11e0: 53 5f 54 43 4c 5f 53 45 52 56 45 52 09 28 31 3c S_TCL_SERVER.(1<
11f0: 3c 31 29 09 2f 2a 20 53 65 72 76 65 72 2d 53 69 <1)./* Server-Si
1200: 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4c de */.#define TL
1210: 53 5f 54 43 4c 5f 49 4e 49 54 09 28 31 3c 3c 32 S_TCL_INIT.(1<<2
1220: 29 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 69 6e )./* Initializin
1230: 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a g connection */.
1240: 23 64 65 66 69 6e 65 20 54 4c 53 5f 54 43 4c 5f #define TLS_TCL_
1250: 44 45 42 55 47 09 28 31 3c 3c 33 29 09 2f 2a 20 DEBUG.(1<<3)./*
1260: 53 68 6f 77 20 64 65 62 75 67 20 74 72 61 63 69 Show debug traci
1270: 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4c ng */.#define TL
1280: 53 5f 54 43 4c 5f 43 41 4c 4c 42 41 43 4b 09 28 S_TCL_CALLBACK.(
1290: 31 3c 3c 34 29 09 2f 2a 20 49 6e 20 61 20 63 61 1<<4)./* In a ca
12a0: 6c 6c 62 61 63 6b 2c 20 70 72 65 76 65 6e 74 20 llback, prevent
12b0: 75 70 64 61 74 65 0a 09 09 09 09 09 20 2a 20 6c update...... * l
12c0: 6f 6f 70 69 6e 67 20 70 72 6f 62 6c 65 6d 2e 20 ooping problem.
12d0: 5b 42 75 67 20 31 36 35 32 33 38 30 5d 20 2a 2f [Bug 1652380] */
12e0: 0a 23 64 65 66 69 6e 65 20 54 4c 53 5f 54 43 4c .#define TLS_TCL
12f0: 5f 48 41 4e 44 53 48 41 4b 45 5f 46 41 49 4c 45 _HANDSHAKE_FAILE
1300: 44 20 28 31 3c 3c 35 29 20 2f 2a 20 53 65 74 20 D (1<<5) /* Set
1310: 6f 6e 20 68 61 6e 64 73 68 61 6b 65 20 66 61 69 on handshake fai
1320: 6c 75 72 65 73 20 61 6e 64 20 6f 6e 63 65 0a 20 lures and once.
1330: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1340: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1350: 20 20 20 20 20 20 20 20 2a 20 73 65 74 2c 20 61 * set, a
1360: 6c 6c 20 66 75 72 74 68 65 72 20 49 2f 4f 20 77 ll further I/O w
1370: 69 6c 6c 20 72 65 73 75 6c 74 0a 20 20 20 20 20 ill result.
1380: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1390: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13a0: 20 20 20 20 2a 20 69 6e 20 45 43 4f 4e 4e 41 42 * in ECONNAB
13b0: 4f 52 54 45 44 20 65 72 72 6f 72 73 2e 20 2a 2f ORTED errors. */
13c0: 0a 0a 23 64 65 66 69 6e 65 20 54 4c 53 5f 54 43 ..#define TLS_TC
13d0: 4c 5f 44 45 4c 41 59 20 28 35 29 0a 0a 2f 2a 0a L_DELAY (5)../*.
13e0: 20 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 * This structur
13f0: 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 e describes the
1400: 70 65 72 2d 69 6e 73 74 61 6e 63 65 20 73 74 61 per-instance sta
1410: 74 65 0a 20 2a 20 6f 66 20 61 6e 20 73 73 6c 20 te. * of an ssl
1420: 63 68 61 6e 6e 65 6c 2e 0a 20 2a 0a 20 2a 20 54 channel.. *. * T
1430: 68 65 20 53 53 4c 20 70 72 6f 63 65 73 73 69 6e he SSL processin
1440: 67 20 63 6f 6e 74 65 78 74 20 69 73 20 6d 61 69 g context is mai
1450: 6e 74 61 69 6e 65 64 20 68 65 72 65 2c 20 69 6e ntained here, in
1460: 20 74 68 65 20 43 6c 69 65 6e 74 44 61 74 61 0a the ClientData.
1470: 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 */.typedef stru
1480: 63 74 20 53 74 61 74 65 20 7b 0a 09 54 63 6c 5f ct State {..Tcl_
1490: 43 68 61 6e 6e 65 6c 20 73 65 6c 66 3b 20 20 20 Channel self;
14a0: 20 20 20 20 2f 2a 20 74 68 69 73 20 73 6f 63 6b /* this sock
14b0: 65 74 20 63 68 61 6e 6e 65 6c 20 2a 2f 0a 09 54 et channel */..T
14c0: 63 6c 5f 54 69 6d 65 72 54 6f 6b 65 6e 20 74 69 cl_TimerToken ti
14d0: 6d 65 72 3b 0a 0a 09 69 6e 74 20 66 6c 61 67 73 mer;...int flags
14e0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
14f0: 2a 20 73 65 65 20 53 74 61 74 65 2e 66 6c 61 67 * see State.flag
1500: 73 20 61 62 6f 76 65 20 20 2a 2f 0a 09 69 6e 74 s above */..int
1510: 20 77 61 74 63 68 4d 61 73 6b 3b 20 20 20 20 20 watchMask;
1520: 20 20 20 20 20 2f 2a 20 63 75 72 72 65 6e 74 20 /* current
1530: 57 61 74 63 68 50 72 6f 63 20 6d 61 73 6b 20 2a WatchProc mask *
1540: 2f 0a 09 69 6e 74 20 6d 6f 64 65 3b 20 20 20 20 /..int mode;
1550: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 75 /* cu
1560: 72 72 65 6e 74 20 6d 6f 64 65 20 6f 66 20 70 61 rrent mode of pa
1570: 72 65 6e 74 20 63 68 61 6e 6e 65 6c 20 2a 2f 0a rent channel */.
1580: 0a 09 54 63 6c 5f 49 6e 74 65 72 70 20 2a 69 6e ..Tcl_Interp *in
1590: 74 65 72 70 3b 20 20 20 20 20 2f 2a 20 69 6e 74 terp; /* int
15a0: 65 72 70 72 65 74 65 72 20 69 6e 20 77 68 69 63 erpreter in whic
15b0: 68 20 74 68 69 73 20 72 65 73 69 64 65 73 20 2a h this resides *
15c0: 2f 0a 09 54 63 6c 5f 4f 62 6a 20 2a 63 61 6c 6c /..Tcl_Obj *call
15d0: 62 61 63 6b 3b 20 20 20 20 20 20 2f 2a 20 73 63 back; /* sc
15e0: 72 69 70 74 20 63 61 6c 6c 65 64 20 66 6f 72 20 ript called for
15f0: 74 72 61 63 69 6e 67 2c 20 76 65 72 69 66 79 69 tracing, verifyi
1600: 6e 67 20 61 6e 64 20 65 72 72 6f 72 73 20 2a 2f ng and errors */
1610: 0a 09 54 63 6c 5f 4f 62 6a 20 2a 70 61 73 73 77 ..Tcl_Obj *passw
1620: 6f 72 64 3b 20 20 20 20 20 20 2f 2a 20 73 63 72 ord; /* scr
1630: 69 70 74 20 63 61 6c 6c 65 64 20 66 6f 72 20 63 ipt called for c
1640: 65 72 74 69 66 69 63 61 74 65 20 70 61 73 73 77 ertificate passw
1650: 6f 72 64 20 2a 2f 20 0a 0a 09 69 6e 74 20 76 66 ord */ ...int vf
1660: 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 lags;
1670: 20 20 2f 2a 20 76 65 72 69 66 79 20 66 6c 61 67 /* verify flag
1680: 73 20 2a 2f 0a 09 53 53 4c 20 2a 73 73 6c 3b 20 s */..SSL *ssl;
1690: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16a0: 20 53 74 72 75 63 74 20 66 6f 72 20 53 53 4c 20 Struct for SSL
16b0: 70 72 6f 63 65 73 73 69 6e 67 20 2a 2f 0a 09 53 processing */..S
16c0: 53 4c 5f 43 54 58 20 2a 63 74 78 3b 20 20 20 20 SL_CTX *ctx;
16d0: 20 20 20 20 20 20 20 2f 2a 20 53 53 4c 20 43 6f /* SSL Co
16e0: 6e 74 65 78 74 20 2a 2f 0a 09 42 49 4f 20 2a 62 ntext */..BIO *b
16f0: 69 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 io;
1700: 20 20 2f 2a 20 53 74 72 75 63 74 20 66 6f 72 20 /* Struct for
1710: 53 53 4c 20 70 72 6f 63 65 73 73 69 6e 67 20 2a SSL processing *
1720: 2f 0a 09 42 49 4f 20 2a 70 5f 62 69 6f 3b 20 20 /..BIO *p_bio;
1730: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
1740: 72 65 6e 74 20 42 49 4f 20 28 74 68 61 74 20 69 rent BIO (that i
1750: 73 20 6c 61 79 65 72 65 64 20 6f 6e 20 54 63 6c s layered on Tcl
1760: 5f 43 68 61 6e 6e 65 6c 29 20 2a 2f 0a 0a 09 63 _Channel) */...c
1770: 68 61 72 20 2a 65 72 72 3b 0a 7d 20 53 74 61 74 har *err;.} Stat
1780: 65 3b 0a 0a 23 69 66 64 65 66 20 55 53 45 5f 54 e;..#ifdef USE_T
1790: 43 4c 5f 53 54 55 42 53 0a 23 69 66 6e 64 65 66 CL_STUBS.#ifndef
17a0: 20 54 63 6c 5f 53 74 61 63 6b 43 68 61 6e 6e 65 Tcl_StackChanne
17b0: 6c 0a 23 65 72 72 6f 72 20 22 55 6e 61 62 6c 65 l.#error "Unable
17c0: 20 74 6f 20 63 6f 6d 70 69 6c 65 20 6f 6e 20 74 to compile on t
17d0: 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 54 his version of T
17e0: 63 6c 22 0a 23 65 6e 64 69 66 20 2f 2a 20 54 63 cl".#endif /* Tc
17f0: 6c 5f 47 65 74 53 74 61 63 6b 65 64 43 68 61 6e l_GetStackedChan
1800: 6e 65 6c 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a nel */.#endif /*
1810: 20 55 53 45 5f 54 43 4c 5f 53 54 55 42 53 20 2a USE_TCL_STUBS *
1820: 2f 0a 0a 2f 2a 0a 20 2a 20 46 6f 72 77 61 72 64 /../*. * Forward
1830: 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 0a 20 2a declarations. *
1840: 2f 0a 54 63 6c 5f 43 68 61 6e 6e 65 6c 54 79 70 /.Tcl_ChannelTyp
1850: 65 20 2a 54 6c 73 5f 43 68 61 6e 6e 65 6c 54 79 e *Tls_ChannelTy
1860: 70 65 28 76 6f 69 64 29 3b 0a 54 63 6c 5f 43 68 pe(void);.Tcl_Ch
1870: 61 6e 6e 65 6c 20 20 20 20 20 54 6c 73 5f 47 65 annel Tls_Ge
1880: 74 50 61 72 65 6e 74 28 53 74 61 74 65 20 2a 73 tParent(State *s
1890: 74 61 74 65 50 74 72 29 3b 0a 0a 54 63 6c 5f 4f tatePtr);..Tcl_O
18a0: 62 6a 20 20 20 20 20 20 20 20 20 2a 54 6c 73 5f bj *Tls_
18b0: 4e 65 77 58 35 30 39 4f 62 6a 28 54 63 6c 5f 49 NewX509Obj(Tcl_I
18c0: 6e 74 65 72 70 20 2a 69 6e 74 65 72 70 2c 20 58 nterp *interp, X
18d0: 35 30 39 20 2a 63 65 72 74 29 3b 0a 76 6f 69 64 509 *cert);.void
18e0: 20 20 20 20 20 20 20 20 20 20 20 20 54 6c 73 5f Tls_
18f0: 45 72 72 6f 72 28 53 74 61 74 65 20 2a 73 74 61 Error(State *sta
1900: 74 65 50 74 72 2c 20 63 68 61 72 20 2a 6d 73 67 tePtr, char *msg
1910: 29 3b 0a 76 6f 69 64 20 20 20 20 20 20 20 20 20 );.void
1920: 20 20 20 54 6c 73 5f 46 72 65 65 28 63 68 61 72 Tls_Free(char
1930: 20 2a 62 6c 6f 63 6b 50 74 72 29 3b 0a 76 6f 69 *blockPtr);.voi
1940: 64 20 20 20 20 20 20 20 20 20 20 20 20 54 6c 73 d Tls
1950: 5f 43 6c 65 61 6e 28 53 74 61 74 65 20 2a 73 74 _Clean(State *st
1960: 61 74 65 50 74 72 29 3b 0a 69 6e 74 20 20 20 20 atePtr);.int
1970: 20 20 20 20 20 20 20 20 20 54 6c 73 5f 57 61 69 Tls_Wai
1980: 74 46 6f 72 43 6f 6e 6e 65 63 74 28 53 74 61 74 tForConnect(Stat
1990: 65 20 2a 73 74 61 74 65 50 74 72 2c 20 69 6e 74 e *statePtr, int
19a0: 20 2a 65 72 72 6f 72 43 6f 64 65 50 74 72 29 3b *errorCodePtr);
19b0: 0a 0a 42 49 4f 20 20 20 20 20 20 20 20 20 20 20 ..BIO
19c0: 20 20 2a 42 49 4f 5f 6e 65 77 5f 74 63 6c 28 53 *BIO_new_tcl(S
19d0: 74 61 74 65 2a 20 73 74 61 74 65 50 74 72 2c 20 tate* statePtr,
19e0: 69 6e 74 20 66 6c 61 67 73 29 3b 0a 0a 23 64 65 int flags);..#de
19f0: 66 69 6e 65 20 50 54 52 32 49 4e 54 28 78 29 20 fine PTR2INT(x)
1a00: 28 28 69 6e 74 29 20 28 28 69 6e 74 70 74 72 5f ((int) ((intptr_
1a10: 74 29 20 28 78 29 29 29 0a 0a 23 65 6e 64 69 66 t) (x)))..#endif
1a20: 20 2f 2a 20 5f 54 4c 53 49 4e 54 5f 48 20 2a 2f /* _TLSINT_H */
1a30: 0a .