Hex Artifact Content

Artifact 0649a8978421a3d1d3e8231ae531d5d0eb055d72:


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 50 72 6f 76 69 64 65 73 20 42 49 4f  . * Provides BIO
0050: 20 6c 61 79 65 72 20 74 6f 20 69 6e 74 65 72 66   layer to interf
0060: 61 63 65 20 6f 70 65 6e 73 73 6c 20 74 6f 20 54  ace openssl to T
0070: 63 6c 2e 0a 20 2a 2f 0a 0a 23 69 6e 63 6c 75 64  cl.. */..#includ
0080: 65 20 22 74 6c 73 49 6e 74 2e 68 22 0a 0a 23 69  e "tlsInt.h"..#i
0090: 66 20 4f 50 45 4e 53 53 4c 5f 56 45 52 53 49 4f  f OPENSSL_VERSIO
00a0: 4e 5f 4e 55 4d 42 45 52 20 3c 20 30 78 31 30 31  N_NUMBER < 0x101
00b0: 30 30 30 30 30 4c 0a 23 64 65 66 69 6e 65 20 42  00000L.#define B
00c0: 49 4f 5f 67 65 74 5f 64 61 74 61 28 62 69 6f 29  IO_get_data(bio)
00d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
00e0: 28 28 62 69 6f 29 2d 3e 70 74 72 29 0a 23 64 65  ((bio)->ptr).#de
00f0: 66 69 6e 65 20 42 49 4f 5f 67 65 74 5f 69 6e 69  fine BIO_get_ini
0100: 74 28 62 69 6f 29 20 20 20 20 20 20 20 20 20 20  t(bio)          
0110: 20 20 20 20 20 20 28 28 62 69 6f 29 2d 3e 69 6e        ((bio)->in
0120: 69 74 29 0a 23 64 65 66 69 6e 65 20 42 49 4f 5f  it).#define BIO_
0130: 67 65 74 5f 73 68 75 74 64 6f 77 6e 28 62 69 6f  get_shutdown(bio
0140: 29 20 20 20 20 20 20 20 20 20 20 20 20 28 28 62  )            ((b
0150: 69 6f 29 2d 3e 73 68 75 74 64 6f 77 6e 29 0a 23  io)->shutdown).#
0160: 64 65 66 69 6e 65 20 42 49 4f 5f 73 65 74 5f 64  define BIO_set_d
0170: 61 74 61 28 62 69 6f 2c 20 76 61 6c 29 20 20 20  ata(bio, val)   
0180: 20 20 20 20 20 20 20 20 28 62 69 6f 29 2d 3e 70          (bio)->p
0190: 74 72 20 3d 20 28 76 61 6c 29 0a 23 64 65 66 69  tr = (val).#defi
01a0: 6e 65 20 42 49 4f 5f 73 65 74 5f 69 6e 69 74 28  ne BIO_set_init(
01b0: 62 69 6f 2c 20 76 61 6c 29 20 20 20 20 20 20 20  bio, val)       
01c0: 20 20 20 20 28 62 69 6f 29 2d 3e 69 6e 69 74 20      (bio)->init 
01d0: 3d 20 28 76 61 6c 29 0a 23 64 65 66 69 6e 65 20  = (val).#define 
01e0: 42 49 4f 5f 73 65 74 5f 73 68 75 74 64 6f 77 6e  BIO_set_shutdown
01f0: 28 62 69 6f 2c 20 76 61 6c 29 20 20 20 20 20 20  (bio, val)      
0200: 20 28 62 69 6f 29 2d 3e 73 68 75 74 64 6f 77 6e   (bio)->shutdown
0210: 20 3d 20 28 76 61 6c 29 0a 0a 2f 2a 20 58 58 58   = (val)../* XXX
0220: 3a 20 54 68 69 73 20 61 73 73 75 6d 65 73 20 74  : This assumes t
0230: 68 65 20 76 61 72 69 61 62 6c 65 20 62 65 69 6e  he variable bein
0240: 67 20 61 73 73 69 67 6e 65 64 20 74 6f 20 69 73  g assigned to is
0250: 20 42 69 6f 4d 65 74 68 6f 64 73 20 2a 2f 0a 23   BioMethods */.#
0260: 64 65 66 69 6e 65 20 42 49 4f 5f 6d 65 74 68 5f  define BIO_meth_
0270: 6e 65 77 28 74 79 70 65 5f 2c 20 6e 61 6d 65 5f  new(type_, name_
0280: 29 20 20 20 20 20 20 20 28 42 49 4f 5f 4d 45 54  )       (BIO_MET
0290: 48 4f 44 20 2a 29 54 63 6c 5f 41 6c 6c 6f 63 28  HOD *)Tcl_Alloc(
02a0: 73 69 7a 65 6f 66 28 42 49 4f 5f 4d 45 54 48 4f  sizeof(BIO_METHO
02b0: 44 29 29 3b 20 5c 0a 20 20 20 20 20 20 20 20 20  D)); \.         
02c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
02d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
02e0: 6d 65 6d 73 65 74 28 42 69 6f 4d 65 74 68 6f 64  memset(BioMethod
02f0: 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28 42 49 4f  s, 0, sizeof(BIO
0300: 5f 4d 45 54 48 4f 44 29 29 3b 20 5c 0a 20 20 20  _METHOD)); \.   
0310: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0320: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0330: 20 20 20 20 20 20 42 69 6f 4d 65 74 68 6f 64 73        BioMethods
0340: 2d 3e 74 79 70 65 20 3d 20 74 79 70 65 5f 3b 20  ->type = type_; 
0350: 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
0360: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0370: 20 20 20 20 20 20 20 20 20 20 20 42 69 6f 4d 65             BioMe
0380: 74 68 6f 64 73 2d 3e 6e 61 6d 65 20 3d 20 6e 61  thods->name = na
0390: 6d 65 5f 3b 0a 23 64 65 66 69 6e 65 20 42 49 4f  me_;.#define BIO
03a0: 5f 6d 65 74 68 5f 73 65 74 5f 77 72 69 74 65 28  _meth_set_write(
03b0: 62 69 6f 2c 20 76 61 6c 29 20 20 20 20 20 28 62  bio, val)     (b
03c0: 69 6f 29 2d 3e 62 77 72 69 74 65 20 3d 20 76 61  io)->bwrite = va
03d0: 6c 3b 0a 23 64 65 66 69 6e 65 20 42 49 4f 5f 6d  l;.#define BIO_m
03e0: 65 74 68 5f 73 65 74 5f 72 65 61 64 28 62 69 6f  eth_set_read(bio
03f0: 2c 20 76 61 6c 29 20 20 20 20 20 20 28 62 69 6f  , val)      (bio
0400: 29 2d 3e 62 72 65 61 64 20 3d 20 76 61 6c 3b 0a  )->bread = val;.
0410: 23 64 65 66 69 6e 65 20 42 49 4f 5f 6d 65 74 68  #define BIO_meth
0420: 5f 73 65 74 5f 70 75 74 73 28 62 69 6f 2c 20 76  _set_puts(bio, v
0430: 61 6c 29 20 20 20 20 20 20 28 62 69 6f 29 2d 3e  al)      (bio)->
0440: 62 70 75 74 73 20 3d 20 76 61 6c 3b 0a 23 64 65  bputs = val;.#de
0450: 66 69 6e 65 20 42 49 4f 5f 6d 65 74 68 5f 73 65  fine BIO_meth_se
0460: 74 5f 63 74 72 6c 28 62 69 6f 2c 20 76 61 6c 29  t_ctrl(bio, val)
0470: 20 20 20 20 20 20 28 62 69 6f 29 2d 3e 63 74 72        (bio)->ctr
0480: 6c 20 3d 20 76 61 6c 3b 0a 23 64 65 66 69 6e 65  l = val;.#define
0490: 20 42 49 4f 5f 6d 65 74 68 5f 73 65 74 5f 63 72   BIO_meth_set_cr
04a0: 65 61 74 65 28 62 69 6f 2c 20 76 61 6c 29 20 20  eate(bio, val)  
04b0: 20 20 28 62 69 6f 29 2d 3e 63 72 65 61 74 65 20    (bio)->create 
04c0: 3d 20 76 61 6c 3b 0a 23 64 65 66 69 6e 65 20 42  = val;.#define B
04d0: 49 4f 5f 6d 65 74 68 5f 73 65 74 5f 64 65 73 74  IO_meth_set_dest
04e0: 72 6f 79 28 62 69 6f 2c 20 76 61 6c 29 20 20 20  roy(bio, val)   
04f0: 28 62 69 6f 29 2d 3e 64 65 73 74 72 6f 79 20 3d  (bio)->destroy =
0500: 20 76 61 6c 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a   val;.#endif../*
0510: 0a 20 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  . * Forward decl
0520: 61 72 61 74 69 6f 6e 73 0a 20 2a 2f 0a 0a 73 74  arations. */..st
0530: 61 74 69 63 20 69 6e 74 20 42 69 6f 57 72 69 74  atic int BioWrit
0540: 65 09 5f 41 4e 53 49 5f 41 52 47 53 5f 20 28 28  e._ANSI_ARGS_ ((
0550: 42 49 4f 20 2a 68 2c 20 43 4f 4e 53 54 20 63 68  BIO *h, CONST ch
0560: 61 72 20 2a 62 75 66 2c 20 69 6e 74 20 6e 75 6d  ar *buf, int num
0570: 29 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 42  ));.static int B
0580: 69 6f 52 65 61 64 09 5f 41 4e 53 49 5f 41 52 47  ioRead._ANSI_ARG
0590: 53 5f 20 28 28 42 49 4f 20 2a 68 2c 20 63 68 61  S_ ((BIO *h, cha
05a0: 72 20 2a 62 75 66 2c 20 69 6e 74 20 6e 75 6d 29  r *buf, int num)
05b0: 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 42 69  );.static int Bi
05c0: 6f 50 75 74 73 09 5f 41 4e 53 49 5f 41 52 47 53  oPuts._ANSI_ARGS
05d0: 5f 20 28 28 42 49 4f 20 2a 68 2c 20 43 4f 4e 53  _ ((BIO *h, CONS
05e0: 54 20 63 68 61 72 20 2a 73 74 72 29 29 3b 0a 73  T char *str));.s
05f0: 74 61 74 69 63 20 6c 6f 6e 67 20 42 69 6f 43 74  tatic long BioCt
0600: 72 6c 09 5f 41 4e 53 49 5f 41 52 47 53 5f 20 28  rl._ANSI_ARGS_ (
0610: 28 42 49 4f 20 2a 68 2c 20 69 6e 74 20 63 6d 64  (BIO *h, int cmd
0620: 2c 20 6c 6f 6e 67 20 61 72 67 31 2c 20 76 6f 69  , long arg1, voi
0630: 64 20 2a 70 74 72 29 29 3b 0a 73 74 61 74 69 63  d *ptr));.static
0640: 20 69 6e 74 20 42 69 6f 4e 65 77 09 5f 41 4e 53   int BioNew._ANS
0650: 49 5f 41 52 47 53 5f 20 28 28 42 49 4f 20 2a 68  I_ARGS_ ((BIO *h
0660: 29 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 42  ));.static int B
0670: 69 6f 46 72 65 65 09 5f 41 4e 53 49 5f 41 52 47  ioFree._ANSI_ARG
0680: 53 5f 20 28 28 42 49 4f 20 2a 68 29 29 3b 0a 0a  S_ ((BIO *h));..
0690: 42 49 4f 20 2a 0a 42 49 4f 5f 6e 65 77 5f 74 63  BIO *.BIO_new_tc
06a0: 6c 28 73 74 61 74 65 50 74 72 2c 20 66 6c 61 67  l(statePtr, flag
06b0: 73 29 0a 20 20 20 20 53 74 61 74 65 20 2a 73 74  s).    State *st
06c0: 61 74 65 50 74 72 3b 0a 20 20 20 20 69 6e 74 20  atePtr;.    int 
06d0: 66 6c 61 67 73 3b 0a 7b 0a 20 20 20 20 42 49 4f  flags;.{.    BIO
06e0: 20 2a 62 69 6f 3b 0a 20 20 20 20 73 74 61 74 69   *bio;.    stati
06f0: 63 20 42 49 4f 5f 4d 45 54 48 4f 44 20 2a 42 69  c BIO_METHOD *Bi
0700: 6f 4d 65 74 68 6f 64 73 20 3d 20 4e 55 4c 4c 3b  oMethods = NULL;
0710: 0a 0a 20 20 20 20 69 66 20 28 42 69 6f 4d 65 74  ..    if (BioMet
0720: 68 6f 64 73 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a  hods == NULL) {.
0730: 20 20 20 20 20 20 20 20 42 69 6f 4d 65 74 68 6f          BioMetho
0740: 64 73 20 3d 20 42 49 4f 5f 6d 65 74 68 5f 6e 65  ds = BIO_meth_ne
0750: 77 28 42 49 4f 5f 54 59 50 45 5f 54 43 4c 2c 20  w(BIO_TYPE_TCL, 
0760: 22 74 63 6c 22 29 3b 0a 20 20 20 20 20 20 20 20  "tcl");.        
0770: 42 49 4f 5f 6d 65 74 68 5f 73 65 74 5f 77 72 69  BIO_meth_set_wri
0780: 74 65 28 42 69 6f 4d 65 74 68 6f 64 73 2c 20 42  te(BioMethods, B
0790: 69 6f 57 72 69 74 65 29 3b 0a 20 20 20 20 20 20  ioWrite);.      
07a0: 20 20 42 49 4f 5f 6d 65 74 68 5f 73 65 74 5f 72    BIO_meth_set_r
07b0: 65 61 64 28 42 69 6f 4d 65 74 68 6f 64 73 2c 20  ead(BioMethods, 
07c0: 42 69 6f 52 65 61 64 29 3b 0a 20 20 20 20 20 20  BioRead);.      
07d0: 20 20 42 49 4f 5f 6d 65 74 68 5f 73 65 74 5f 70    BIO_meth_set_p
07e0: 75 74 73 28 42 69 6f 4d 65 74 68 6f 64 73 2c 20  uts(BioMethods, 
07f0: 42 69 6f 50 75 74 73 29 3b 0a 20 20 20 20 20 20  BioPuts);.      
0800: 20 20 42 49 4f 5f 6d 65 74 68 5f 73 65 74 5f 63    BIO_meth_set_c
0810: 74 72 6c 28 42 69 6f 4d 65 74 68 6f 64 73 2c 20  trl(BioMethods, 
0820: 42 69 6f 43 74 72 6c 29 3b 0a 20 20 20 20 20 20  BioCtrl);.      
0830: 20 20 42 49 4f 5f 6d 65 74 68 5f 73 65 74 5f 63    BIO_meth_set_c
0840: 72 65 61 74 65 28 42 69 6f 4d 65 74 68 6f 64 73  reate(BioMethods
0850: 2c 20 42 69 6f 4e 65 77 29 3b 0a 20 20 20 20 20  , BioNew);.     
0860: 20 20 20 42 49 4f 5f 6d 65 74 68 5f 73 65 74 5f     BIO_meth_set_
0870: 64 65 73 74 72 6f 79 28 42 69 6f 4d 65 74 68 6f  destroy(BioMetho
0880: 64 73 2c 20 42 69 6f 46 72 65 65 29 3b 0a 20 20  ds, BioFree);.  
0890: 20 20 7d 0a 0a 20 20 20 20 62 69 6f 20 3d 20 42    }..    bio = B
08a0: 49 4f 5f 6e 65 77 28 42 69 6f 4d 65 74 68 6f 64  IO_new(BioMethod
08b0: 73 29 3b 0a 0a 20 20 20 20 42 49 4f 5f 73 65 74  s);..    BIO_set
08c0: 5f 64 61 74 61 28 62 69 6f 2c 20 73 74 61 74 65  _data(bio, state
08d0: 50 74 72 29 3b 0a 20 20 20 20 42 49 4f 5f 73 65  Ptr);.    BIO_se
08e0: 74 5f 69 6e 69 74 28 62 69 6f 2c 20 31 29 3b 0a  t_init(bio, 1);.
08f0: 20 20 20 20 42 49 4f 5f 73 65 74 5f 73 68 75 74      BIO_set_shut
0900: 64 6f 77 6e 28 62 69 6f 2c 20 66 6c 61 67 73 29  down(bio, flags)
0910: 3b 0a 0a 20 20 20 20 72 65 74 75 72 6e 20 62 69  ;..    return bi
0920: 6f 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  o;.}..static int
0930: 0a 42 69 6f 57 72 69 74 65 20 28 62 69 6f 2c 20  .BioWrite (bio, 
0940: 62 75 66 2c 20 62 75 66 4c 65 6e 29 0a 20 20 20  buf, bufLen).   
0950: 20 42 49 4f 20 2a 62 69 6f 3b 0a 20 20 20 20 43   BIO *bio;.    C
0960: 4f 4e 53 54 20 63 68 61 72 20 2a 62 75 66 3b 0a  ONST char *buf;.
0970: 20 20 20 20 69 6e 74 20 62 75 66 4c 65 6e 3b 0a      int bufLen;.
0980: 7b 0a 20 20 20 20 54 63 6c 5f 43 68 61 6e 6e 65  {.    Tcl_Channe
0990: 6c 20 63 68 61 6e 20 3d 20 54 6c 73 5f 47 65 74  l chan = Tls_Get
09a0: 50 61 72 65 6e 74 28 28 53 74 61 74 65 2a 29 42  Parent((State*)B
09b0: 49 4f 5f 67 65 74 5f 64 61 74 61 28 62 69 6f 29  IO_get_data(bio)
09c0: 29 3b 0a 20 20 20 20 69 6e 74 20 72 65 74 3b 0a  );.    int ret;.
09d0: 0a 20 20 20 20 64 70 72 69 6e 74 66 28 22 42 69  .    dprintf("Bi
09e0: 6f 57 72 69 74 65 28 25 70 2c 20 3c 62 75 66 3e  oWrite(%p, <buf>
09f0: 2c 20 25 64 29 20 5b 25 70 5d 22 2c 0a 09 20 20  , %d) [%p]",..  
0a00: 20 20 28 76 6f 69 64 20 2a 29 20 62 69 6f 2c 20    (void *) bio, 
0a10: 62 75 66 4c 65 6e 2c 20 28 76 6f 69 64 20 2a 29  bufLen, (void *)
0a20: 20 63 68 61 6e 29 3b 0a 0a 20 20 20 20 69 66 20   chan);..    if 
0a30: 28 63 68 61 6e 6e 65 6c 54 79 70 65 56 65 72 73  (channelTypeVers
0a40: 69 6f 6e 20 3d 3d 20 54 4c 53 5f 43 48 41 4e 4e  ion == TLS_CHANN
0a50: 45 4c 5f 56 45 52 53 49 4f 4e 5f 32 29 20 7b 0a  EL_VERSION_2) {.
0a60: 09 72 65 74 20 3d 20 54 63 6c 5f 57 72 69 74 65  .ret = Tcl_Write
0a70: 52 61 77 28 63 68 61 6e 2c 20 62 75 66 2c 20 62  Raw(chan, buf, b
0a80: 75 66 4c 65 6e 29 3b 0a 20 20 20 20 7d 20 65 6c  ufLen);.    } el
0a90: 73 65 20 7b 0a 09 72 65 74 20 3d 20 54 63 6c 5f  se {..ret = Tcl_
0aa0: 57 72 69 74 65 28 63 68 61 6e 2c 20 62 75 66 2c  Write(chan, buf,
0ab0: 20 62 75 66 4c 65 6e 29 3b 0a 20 20 20 20 7d 0a   bufLen);.    }.
0ac0: 0a 20 20 20 20 64 70 72 69 6e 74 66 28 22 5b 25  .    dprintf("[%
0ad0: 70 5d 20 42 69 6f 57 72 69 74 65 28 25 64 29 20  p] BioWrite(%d) 
0ae0: 2d 3e 20 25 64 20 5b 25 64 2e 25 64 5d 22 2c 0a  -> %d [%d.%d]",.
0af0: 09 20 20 20 20 28 76 6f 69 64 20 2a 29 20 63 68  .    (void *) ch
0b00: 61 6e 2c 20 62 75 66 4c 65 6e 2c 20 72 65 74 2c  an, bufLen, ret,
0b10: 20 54 63 6c 5f 45 6f 66 28 63 68 61 6e 29 2c 20   Tcl_Eof(chan), 
0b20: 54 63 6c 5f 47 65 74 45 72 72 6e 6f 28 29 29 3b  Tcl_GetErrno());
0b30: 0a 0a 20 20 20 20 42 49 4f 5f 63 6c 65 61 72 5f  ..    BIO_clear_
0b40: 66 6c 61 67 73 28 62 69 6f 2c 20 42 49 4f 5f 46  flags(bio, BIO_F
0b50: 4c 41 47 53 5f 57 52 49 54 45 7c 42 49 4f 5f 46  LAGS_WRITE|BIO_F
0b60: 4c 41 47 53 5f 53 48 4f 55 4c 44 5f 52 45 54 52  LAGS_SHOULD_RETR
0b70: 59 29 3b 0a 0a 20 20 20 20 69 66 20 28 72 65 74  Y);..    if (ret
0b80: 20 3d 3d 20 30 29 20 7b 0a 09 69 66 20 28 21 54   == 0) {..if (!T
0b90: 63 6c 5f 45 6f 66 28 63 68 61 6e 29 29 20 7b 0a  cl_Eof(chan)) {.
0ba0: 09 20 20 20 20 42 49 4f 5f 73 65 74 5f 72 65 74  .    BIO_set_ret
0bb0: 72 79 5f 77 72 69 74 65 28 62 69 6f 29 3b 0a 09  ry_write(bio);..
0bc0: 20 20 20 20 72 65 74 20 3d 20 2d 31 3b 0a 09 7d      ret = -1;..}
0bd0: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 42  .    }.    if (B
0be0: 49 4f 5f 73 68 6f 75 6c 64 5f 72 65 61 64 28 62  IO_should_read(b
0bf0: 69 6f 29 29 20 7b 0a 09 42 49 4f 5f 73 65 74 5f  io)) {..BIO_set_
0c00: 72 65 74 72 79 5f 72 65 61 64 28 62 69 6f 29 3b  retry_read(bio);
0c10: 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
0c20: 6e 20 72 65 74 3b 0a 7d 0a 0a 73 74 61 74 69 63  n ret;.}..static
0c30: 20 69 6e 74 0a 42 69 6f 52 65 61 64 20 28 62 69   int.BioRead (bi
0c40: 6f 2c 20 62 75 66 2c 20 62 75 66 4c 65 6e 29 0a  o, buf, bufLen).
0c50: 20 20 20 20 42 49 4f 20 2a 62 69 6f 3b 0a 20 20      BIO *bio;.  
0c60: 20 20 63 68 61 72 20 2a 62 75 66 3b 0a 20 20 20    char *buf;.   
0c70: 20 69 6e 74 20 62 75 66 4c 65 6e 3b 0a 7b 0a 20   int bufLen;.{. 
0c80: 20 20 20 54 63 6c 5f 43 68 61 6e 6e 65 6c 20 63     Tcl_Channel c
0c90: 68 61 6e 20 3d 20 54 6c 73 5f 47 65 74 50 61 72  han = Tls_GetPar
0ca0: 65 6e 74 28 28 53 74 61 74 65 2a 29 42 49 4f 5f  ent((State*)BIO_
0cb0: 67 65 74 5f 64 61 74 61 28 62 69 6f 29 29 3b 0a  get_data(bio));.
0cc0: 20 20 20 20 69 6e 74 20 72 65 74 20 3d 20 30 3b      int ret = 0;
0cd0: 0a 20 20 20 20 69 6e 74 20 74 63 6c 45 6f 66 43  .    int tclEofC
0ce0: 68 61 6e 3b 0a 0a 20 20 20 20 64 70 72 69 6e 74  han;..    dprint
0cf0: 66 28 22 42 69 6f 52 65 61 64 28 25 70 2c 20 3c  f("BioRead(%p, <
0d00: 62 75 66 3e 2c 20 25 64 29 20 5b 25 70 5d 22 2c  buf>, %d) [%p]",
0d10: 20 28 76 6f 69 64 20 2a 29 20 62 69 6f 2c 20 62   (void *) bio, b
0d20: 75 66 4c 65 6e 2c 20 28 76 6f 69 64 20 2a 29 20  ufLen, (void *) 
0d30: 63 68 61 6e 29 3b 0a 0a 20 20 20 20 69 66 20 28  chan);..    if (
0d40: 62 75 66 20 3d 3d 20 4e 55 4c 4c 29 20 72 65 74  buf == NULL) ret
0d50: 75 72 6e 20 30 3b 0a 0a 20 20 20 20 69 66 20 28  urn 0;..    if (
0d60: 63 68 61 6e 6e 65 6c 54 79 70 65 56 65 72 73 69  channelTypeVersi
0d70: 6f 6e 20 3d 3d 20 54 4c 53 5f 43 48 41 4e 4e 45  on == TLS_CHANNE
0d80: 4c 5f 56 45 52 53 49 4f 4e 5f 32 29 20 7b 0a 09  L_VERSION_2) {..
0d90: 72 65 74 20 3d 20 54 63 6c 5f 52 65 61 64 52 61  ret = Tcl_ReadRa
0da0: 77 28 63 68 61 6e 2c 20 62 75 66 2c 20 62 75 66  w(chan, buf, buf
0db0: 4c 65 6e 29 3b 0a 20 20 20 20 7d 20 65 6c 73 65  Len);.    } else
0dc0: 20 7b 0a 09 72 65 74 20 3d 20 54 63 6c 5f 52 65   {..ret = Tcl_Re
0dd0: 61 64 28 63 68 61 6e 2c 20 62 75 66 2c 20 62 75  ad(chan, buf, bu
0de0: 66 4c 65 6e 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  fLen);.    }..  
0df0: 20 20 74 63 6c 45 6f 66 43 68 61 6e 20 3d 20 54    tclEofChan = T
0e00: 63 6c 5f 45 6f 66 28 63 68 61 6e 29 3b 0a 0a 20  cl_Eof(chan);.. 
0e10: 20 20 20 64 70 72 69 6e 74 66 28 22 5b 25 70 5d     dprintf("[%p]
0e20: 20 42 69 6f 52 65 61 64 28 25 64 29 20 2d 3e 20   BioRead(%d) -> 
0e30: 25 64 20 5b 74 63 6c 45 6f 66 3d 25 64 3b 20 74  %d [tclEof=%d; t
0e40: 63 6c 45 72 72 6e 6f 3d 25 64 5d 22 2c 0a 09 20  clErrno=%d]",.. 
0e50: 20 20 20 28 76 6f 69 64 20 2a 29 20 63 68 61 6e     (void *) chan
0e60: 2c 20 62 75 66 4c 65 6e 2c 20 72 65 74 2c 20 74  , bufLen, ret, t
0e70: 63 6c 45 6f 66 43 68 61 6e 2c 20 54 63 6c 5f 47  clEofChan, Tcl_G
0e80: 65 74 45 72 72 6e 6f 28 29 29 3b 0a 0a 20 20 20  etErrno());..   
0e90: 20 42 49 4f 5f 63 6c 65 61 72 5f 66 6c 61 67 73   BIO_clear_flags
0ea0: 28 62 69 6f 2c 20 42 49 4f 5f 46 4c 41 47 53 5f  (bio, BIO_FLAGS_
0eb0: 52 45 41 44 7c 42 49 4f 5f 46 4c 41 47 53 5f 53  READ|BIO_FLAGS_S
0ec0: 48 4f 55 4c 44 5f 52 45 54 52 59 29 3b 0a 0a 20  HOULD_RETRY);.. 
0ed0: 20 20 20 69 66 20 28 72 65 74 20 3d 3d 20 30 29     if (ret == 0)
0ee0: 20 7b 0a 09 69 66 20 28 21 74 63 6c 45 6f 66 43   {..if (!tclEofC
0ef0: 68 61 6e 29 20 7b 0a 20 20 20 20 20 20 20 20 20  han) {.         
0f00: 20 20 20 64 70 72 69 6e 74 66 28 22 47 6f 74 20     dprintf("Got 
0f10: 30 20 66 72 6f 6d 20 54 63 6c 5f 52 65 61 64 20  0 from Tcl_Read 
0f20: 6f 72 20 54 63 6c 5f 52 65 61 64 52 61 77 2c 20  or Tcl_ReadRaw, 
0f30: 61 6e 64 20 45 4f 46 20 69 73 20 6e 6f 74 20 73  and EOF is not s
0f40: 65 74 20 2d 2d 20 72 65 74 20 3d 3d 20 2d 31 20  et -- ret == -1 
0f50: 6e 6f 77 22 29 3b 0a 09 20 20 20 20 42 49 4f 5f  now");..    BIO_
0f60: 73 65 74 5f 72 65 74 72 79 5f 72 65 61 64 28 62  set_retry_read(b
0f70: 69 6f 29 3b 0a 09 20 20 20 20 72 65 74 20 3d 20  io);..    ret = 
0f80: 2d 31 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 20 20  -1;..} else {.  
0f90: 20 20 20 20 20 20 20 20 20 20 64 70 72 69 6e 74            dprint
0fa0: 66 28 22 47 6f 74 20 30 20 66 72 6f 6d 20 54 63  f("Got 0 from Tc
0fb0: 6c 5f 52 65 61 64 20 6f 72 20 54 63 6c 5f 52 65  l_Read or Tcl_Re
0fc0: 61 64 52 61 77 2c 20 61 6e 64 20 45 4f 46 20 69  adRaw, and EOF i
0fd0: 73 20 73 65 74 22 29 3b 0a 20 20 20 20 20 20 20  s set");.       
0fe0: 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a   }.    } else {.
0ff0: 20 20 20 20 20 20 20 20 64 70 72 69 6e 74 66 28          dprintf(
1000: 22 47 6f 74 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72  "Got non-zero fr
1010: 6f 6d 20 54 63 6c 5f 52 65 61 64 20 6f 72 20 54  om Tcl_Read or T
1020: 63 6c 5f 52 65 61 64 52 61 77 20 3d 3d 20 72 65  cl_ReadRaw == re
1030: 74 20 3d 3d 20 25 69 22 2c 20 72 65 74 29 3b 0a  t == %i", ret);.
1040: 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 42 49      }.    if (BI
1050: 4f 5f 73 68 6f 75 6c 64 5f 77 72 69 74 65 28 62  O_should_write(b
1060: 69 6f 29 29 20 7b 0a 09 42 49 4f 5f 73 65 74 5f  io)) {..BIO_set_
1070: 72 65 74 72 79 5f 77 72 69 74 65 28 62 69 6f 29  retry_write(bio)
1080: 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 64 70 72  ;.    }..    dpr
1090: 69 6e 74 66 28 22 42 69 6f 52 65 61 64 28 25 70  intf("BioRead(%p
10a0: 2c 20 3c 62 75 66 3e 2c 20 25 64 29 20 5b 25 70  , <buf>, %d) [%p
10b0: 5d 20 72 65 74 75 72 6e 69 6e 67 20 25 69 22 2c  ] returning %i",
10c0: 20 28 76 6f 69 64 20 2a 29 20 62 69 6f 2c 20 62   (void *) bio, b
10d0: 75 66 4c 65 6e 2c 20 28 76 6f 69 64 20 2a 29 20  ufLen, (void *) 
10e0: 63 68 61 6e 2c 20 72 65 74 29 3b 0a 0a 20 20 20  chan, ret);..   
10f0: 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a   return ret;.}..
1100: 73 74 61 74 69 63 20 69 6e 74 0a 42 69 6f 50 75  static int.BioPu
1110: 74 73 09 28 62 69 6f 2c 20 73 74 72 29 0a 20 20  ts.(bio, str).  
1120: 20 20 42 49 4f 20 2a 62 69 6f 3b 0a 20 20 20 20    BIO *bio;.    
1130: 43 4f 4e 53 54 20 63 68 61 72 20 2a 73 74 72 3b  CONST char *str;
1140: 0a 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 42 69  .{.    return Bi
1150: 6f 57 72 69 74 65 28 62 69 6f 2c 20 73 74 72 2c  oWrite(bio, str,
1160: 20 28 69 6e 74 29 20 73 74 72 6c 65 6e 28 73 74   (int) strlen(st
1170: 72 29 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 6c  r));.}..static l
1180: 6f 6e 67 0a 42 69 6f 43 74 72 6c 09 28 62 69 6f  ong.BioCtrl.(bio
1190: 2c 20 63 6d 64 2c 20 6e 75 6d 2c 20 70 74 72 29  , cmd, num, ptr)
11a0: 0a 20 20 20 20 42 49 4f 20 2a 62 69 6f 3b 0a 20  .    BIO *bio;. 
11b0: 20 20 20 69 6e 74 20 63 6d 64 3b 0a 20 20 20 20     int cmd;.    
11c0: 6c 6f 6e 67 20 6e 75 6d 3b 0a 20 20 20 20 76 6f  long num;.    vo
11d0: 69 64 20 2a 70 74 72 3b 0a 7b 0a 20 20 20 20 54  id *ptr;.{.    T
11e0: 63 6c 5f 43 68 61 6e 6e 65 6c 20 63 68 61 6e 20  cl_Channel chan 
11f0: 3d 20 54 6c 73 5f 47 65 74 50 61 72 65 6e 74 28  = Tls_GetParent(
1200: 28 53 74 61 74 65 2a 29 42 49 4f 5f 67 65 74 5f  (State*)BIO_get_
1210: 64 61 74 61 28 62 69 6f 29 29 3b 0a 20 20 20 20  data(bio));.    
1220: 6c 6f 6e 67 20 72 65 74 20 3d 20 31 3b 0a 0a 20  long ret = 1;.. 
1230: 20 20 20 64 70 72 69 6e 74 66 28 22 42 69 6f 43     dprintf("BioC
1240: 74 72 6c 28 25 70 2c 20 30 78 25 78 2c 20 30 78  trl(%p, 0x%x, 0x
1250: 25 78 2c 20 25 70 29 22 2c 0a 09 20 20 20 20 28  %x, %p)",..    (
1260: 76 6f 69 64 20 2a 29 20 62 69 6f 2c 20 28 75 6e  void *) bio, (un
1270: 73 69 67 6e 65 64 20 69 6e 74 29 20 63 6d 64 2c  signed int) cmd,
1280: 20 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 20   (unsigned int) 
1290: 6e 75 6d 2c 0a 09 20 20 20 20 28 76 6f 69 64 20  num,..    (void 
12a0: 2a 29 20 70 74 72 29 3b 0a 0a 20 20 20 20 73 77  *) ptr);..    sw
12b0: 69 74 63 68 20 28 63 6d 64 29 20 7b 0a 20 20 20  itch (cmd) {.   
12c0: 20 63 61 73 65 20 42 49 4f 5f 43 54 52 4c 5f 52   case BIO_CTRL_R
12d0: 45 53 45 54 3a 0a 09 6e 75 6d 20 3d 20 30 3b 0a  ESET:..num = 0;.
12e0: 20 20 20 20 63 61 73 65 20 42 49 4f 5f 43 5f 46      case BIO_C_F
12f0: 49 4c 45 5f 53 45 45 4b 3a 0a 20 20 20 20 63 61  ILE_SEEK:.    ca
1300: 73 65 20 42 49 4f 5f 43 5f 46 49 4c 45 5f 54 45  se BIO_C_FILE_TE
1310: 4c 4c 3a 0a 09 72 65 74 20 3d 20 30 3b 0a 09 62  LL:..ret = 0;..b
1320: 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 42  reak;.    case B
1330: 49 4f 5f 43 54 52 4c 5f 49 4e 46 4f 3a 0a 09 72  IO_CTRL_INFO:..r
1340: 65 74 20 3d 20 31 3b 0a 09 62 72 65 61 6b 3b 0a  et = 1;..break;.
1350: 20 20 20 20 63 61 73 65 20 42 49 4f 5f 43 5f 53      case BIO_C_S
1360: 45 54 5f 46 44 3a 0a 20 20 20 20 20 20 20 20 64  ET_FD:.        d
1370: 70 72 69 6e 74 66 28 22 55 6e 73 75 70 70 6f 72  printf("Unsuppor
1380: 74 65 64 20 63 61 6c 6c 3a 20 42 49 4f 5f 43 5f  ted call: BIO_C_
1390: 53 45 54 5f 46 44 22 29 3b 0a 20 20 20 20 20 20  SET_FD");.      
13a0: 20 20 72 65 74 20 3d 20 2d 31 3b 0a 20 20 20 20    ret = -1;.    
13b0: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
13c0: 61 73 65 20 42 49 4f 5f 43 5f 47 45 54 5f 46 44  ase BIO_C_GET_FD
13d0: 3a 0a 20 20 20 20 20 20 20 20 64 70 72 69 6e 74  :.        dprint
13e0: 66 28 22 55 6e 73 75 70 70 6f 72 74 65 64 20 63  f("Unsupported c
13f0: 61 6c 6c 3a 20 42 49 4f 5f 43 5f 47 45 54 5f 46  all: BIO_C_GET_F
1400: 44 22 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  D");.        ret
1410: 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 62   = -1;.        b
1420: 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 42  reak;.    case B
1430: 49 4f 5f 43 54 52 4c 5f 47 45 54 5f 43 4c 4f 53  IO_CTRL_GET_CLOS
1440: 45 3a 0a 09 72 65 74 20 3d 20 42 49 4f 5f 67 65  E:..ret = BIO_ge
1450: 74 5f 73 68 75 74 64 6f 77 6e 28 62 69 6f 29 3b  t_shutdown(bio);
1460: 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73  ..break;.    cas
1470: 65 20 42 49 4f 5f 43 54 52 4c 5f 53 45 54 5f 43  e BIO_CTRL_SET_C
1480: 4c 4f 53 45 3a 0a 09 42 49 4f 5f 73 65 74 5f 73  LOSE:..BIO_set_s
1490: 68 75 74 64 6f 77 6e 28 62 69 6f 2c 20 6e 75 6d  hutdown(bio, num
14a0: 29 3b 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 63  );..break;.    c
14b0: 61 73 65 20 42 49 4f 5f 43 54 52 4c 5f 45 4f 46  ase BIO_CTRL_EOF
14c0: 3a 0a 09 64 70 72 69 6e 74 66 28 22 42 49 4f 5f  :..dprintf("BIO_
14d0: 43 54 52 4c 5f 45 4f 46 22 29 3b 0a 09 72 65 74  CTRL_EOF");..ret
14e0: 20 3d 20 54 63 6c 5f 45 6f 66 28 63 68 61 6e 29   = Tcl_Eof(chan)
14f0: 3b 0a 09 62 72 65 61 6b 3b 0a 20 20 20 20 63 61  ;..break;.    ca
1500: 73 65 20 42 49 4f 5f 43 54 52 4c 5f 50 45 4e 44  se BIO_CTRL_PEND
1510: 49 4e 47 3a 0a 09 72 65 74 20 3d 20 28 54 63 6c  ING:..ret = (Tcl
1520: 5f 49 6e 70 75 74 42 75 66 66 65 72 65 64 28 63  _InputBuffered(c
1530: 68 61 6e 29 20 3f 20 31 20 3a 20 30 29 3b 0a 09  han) ? 1 : 0);..
1540: 64 70 72 69 6e 74 66 28 22 42 49 4f 5f 43 54 52  dprintf("BIO_CTR
1550: 4c 5f 50 45 4e 44 49 4e 47 28 25 64 29 22 2c 20  L_PENDING(%d)", 
1560: 28 69 6e 74 29 20 72 65 74 29 3b 0a 09 62 72 65  (int) ret);..bre
1570: 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 42 49 4f  ak;.    case BIO
1580: 5f 43 54 52 4c 5f 57 50 45 4e 44 49 4e 47 3a 0a  _CTRL_WPENDING:.
1590: 09 72 65 74 20 3d 20 30 3b 0a 09 62 72 65 61 6b  .ret = 0;..break
15a0: 3b 0a 20 20 20 20 63 61 73 65 20 42 49 4f 5f 43  ;.    case BIO_C
15b0: 54 52 4c 5f 44 55 50 3a 0a 09 62 72 65 61 6b 3b  TRL_DUP:..break;
15c0: 0a 20 20 20 20 63 61 73 65 20 42 49 4f 5f 43 54  .    case BIO_CT
15d0: 52 4c 5f 46 4c 55 53 48 3a 0a 09 64 70 72 69 6e  RL_FLUSH:..dprin
15e0: 74 66 28 22 42 49 4f 5f 43 54 52 4c 5f 46 4c 55  tf("BIO_CTRL_FLU
15f0: 53 48 22 29 3b 0a 09 69 66 20 28 63 68 61 6e 6e  SH");..if (chann
1600: 65 6c 54 79 70 65 56 65 72 73 69 6f 6e 20 3d 3d  elTypeVersion ==
1610: 20 54 4c 53 5f 43 48 41 4e 4e 45 4c 5f 56 45 52   TLS_CHANNEL_VER
1620: 53 49 4f 4e 5f 32 29 20 7b 0a 09 20 20 20 20 72  SION_2) {..    r
1630: 65 74 20 3d 20 28 28 54 63 6c 5f 57 72 69 74 65  et = ((Tcl_Write
1640: 52 61 77 28 63 68 61 6e 2c 20 22 22 2c 20 30 29  Raw(chan, "", 0)
1650: 20 3e 3d 20 30 29 20 3f 20 31 20 3a 20 2d 31 29   >= 0) ? 1 : -1)
1660: 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 20 20 20  ;..} else {..   
1670: 20 72 65 74 20 3d 20 28 28 54 63 6c 5f 46 6c 75   ret = ((Tcl_Flu
1680: 73 68 28 63 68 61 6e 29 20 3d 3d 20 54 43 4c 5f  sh(chan) == TCL_
1690: 4f 4b 29 20 3f 20 31 20 3a 20 2d 31 29 3b 0a 09  OK) ? 1 : -1);..
16a0: 7d 0a 20 20 20 20 20 20 20 20 64 70 72 69 6e 74  }.        dprint
16b0: 66 28 22 42 49 4f 5f 43 54 52 4c 5f 46 4c 55 53  f("BIO_CTRL_FLUS
16c0: 48 20 72 65 74 75 72 6e 69 6e 67 20 76 61 6c 75  H returning valu
16d0: 65 20 25 6c 69 22 2c 20 72 65 74 29 3b 0a 09 62  e %li", ret);..b
16e0: 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c  reak;.    defaul
16f0: 74 3a 0a 09 72 65 74 20 3d 20 30 3b 0a 09 62 72  t:..ret = 0;..br
1700: 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  eak;.    }.    r
1710: 65 74 75 72 6e 28 72 65 74 29 3b 0a 7d 0a 0a 73  eturn(ret);.}..s
1720: 74 61 74 69 63 20 69 6e 74 0a 42 69 6f 4e 65 77  tatic int.BioNew
1730: 09 28 62 69 6f 29 0a 20 20 20 20 42 49 4f 20 2a  .(bio).    BIO *
1740: 62 69 6f 3b 0a 7b 0a 20 20 20 20 42 49 4f 5f 73  bio;.{.    BIO_s
1750: 65 74 5f 69 6e 69 74 28 62 69 6f 2c 20 30 29 3b  et_init(bio, 0);
1760: 0a 20 20 20 20 42 49 4f 5f 73 65 74 5f 64 61 74  .    BIO_set_dat
1770: 61 28 62 69 6f 2c 20 4e 55 4c 4c 29 3b 0a 20 20  a(bio, NULL);.  
1780: 20 20 42 49 4f 5f 63 6c 65 61 72 5f 66 6c 61 67    BIO_clear_flag
1790: 73 28 62 69 6f 2c 20 2d 31 29 3b 0a 0a 20 20 20  s(bio, -1);..   
17a0: 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 73 74   return 1;.}..st
17b0: 61 74 69 63 20 69 6e 74 0a 42 69 6f 46 72 65 65  atic int.BioFree
17c0: 09 28 62 69 6f 29 0a 20 20 20 20 42 49 4f 20 2a  .(bio).    BIO *
17d0: 62 69 6f 3b 0a 7b 0a 20 20 20 20 69 66 20 28 62  bio;.{.    if (b
17e0: 69 6f 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a 09 72  io == NULL) {..r
17f0: 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20  eturn 0;.    }. 
1800: 20 20 20 69 66 20 28 42 49 4f 5f 67 65 74 5f 73     if (BIO_get_s
1810: 68 75 74 64 6f 77 6e 28 62 69 6f 29 29 20 7b 0a  hutdown(bio)) {.
1820: 09 69 66 20 28 42 49 4f 5f 67 65 74 5f 69 6e 69  .if (BIO_get_ini
1830: 74 28 62 69 6f 29 29 20 7b 0a 09 20 20 20 20 2f  t(bio)) {..    /
1840: 2a 73 68 75 74 64 6f 77 6e 28 62 69 6f 2d 3e 6e  *shutdown(bio->n
1850: 75 6d 2c 20 32 29 20 2a 2f 0a 09 20 20 20 20 2f  um, 2) */..    /
1860: 2a 63 6c 6f 73 65 73 6f 63 6b 65 74 28 62 69 6f  *closesocket(bio
1870: 2d 3e 6e 75 6d 29 20 2a 2f 0a 09 7d 0a 20 20 20  ->num) */..}.   
1880: 20 20 20 20 20 42 49 4f 5f 73 65 74 5f 69 6e 69       BIO_set_ini
1890: 74 28 62 69 6f 2c 20 30 29 3b 0a 20 20 20 20 20  t(bio, 0);.     
18a0: 20 20 20 42 49 4f 5f 63 6c 65 61 72 5f 66 6c 61     BIO_clear_fla
18b0: 67 73 28 62 69 6f 2c 20 2d 31 29 3b 0a 20 20 20  gs(bio, -1);.   
18c0: 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b   }.    return 1;
18d0: 0a 7d 0a                                         .}.