Socket.hx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package sys.ssl;
  2. import sys.ssl.Lib;
  3. import sys.ssl.Key.KeyPtr;
  4. import sys.ssl.Certificate.CertificatePtr;
  5. import sys.net.Socket.SocketHandle;
  6. private class SocketInput extends haxe.io.Input {
  7. @:allow(sys.ssl.Socket) private var __s : Socket;
  8. public function new( s : Socket ) {
  9. this.__s = s;
  10. }
  11. public override function readByte() {
  12. __s.handshake();
  13. var r = @:privateAccess __s.ssl.recvChar();
  14. if( r == -1 )
  15. throw haxe.io.Error.Blocked;
  16. else if( r < 0 )
  17. throw new haxe.io.Eof();
  18. return r;
  19. }
  20. public override function readBytes( buf : haxe.io.Bytes, pos : Int, len : Int ) : Int {
  21. if( pos < 0 || len < 0 || ((pos+len):UInt) > (buf.length : UInt) )
  22. throw haxe.io.Error.OutsideBounds;
  23. __s.handshake();
  24. var r = @:privateAccess __s.ssl.recv(buf, pos, len);
  25. if( r == -1 )
  26. throw haxe.io.Error.Blocked;
  27. else if( r <= 0 )
  28. throw new haxe.io.Eof();
  29. return r;
  30. }
  31. public override function close() {
  32. super.close();
  33. if( __s != null ) __s.close();
  34. }
  35. }
  36. private class SocketOutput extends haxe.io.Output {
  37. @:allow(sys.ssl.Socket) private var __s : Socket;
  38. public function new( s : Socket ) {
  39. this.__s = s;
  40. }
  41. public override function writeByte( c : Int ) {
  42. __s.handshake();
  43. var r = @:privateAccess __s.ssl.sendChar(c);
  44. if( r == -1 )
  45. throw haxe.io.Error.Blocked;
  46. else if( r < 0 )
  47. throw new haxe.io.Eof();
  48. }
  49. public override function writeBytes( buf : haxe.io.Bytes, pos : Int, len : Int) : Int {
  50. if( pos < 0 || len < 0 || ((pos+len):UInt) > (buf.length : UInt) )
  51. throw haxe.io.Error.OutsideBounds;
  52. __s.handshake();
  53. var r = @:privateAccess __s.ssl.send(buf, pos, len);
  54. if( r == -1 )
  55. throw haxe.io.Error.Blocked;
  56. else if( r < 0 )
  57. throw new haxe.io.Eof();
  58. return r;
  59. }
  60. public override function close() {
  61. super.close();
  62. if( __s != null ) __s.close();
  63. }
  64. }
  65. @:coreApi @:access(sys.net.Socket)
  66. class Socket extends sys.net.Socket {
  67. public static var DEFAULT_VERIFY_CERT : Null<Bool> = true;
  68. public static var DEFAULT_CA : Null<Certificate>;
  69. private var conf : Context.Config;
  70. private var ssl : Context;
  71. public var verifyCert : Null<Bool>;
  72. private var caCert : Null<Certificate>;
  73. private var hostname : String;
  74. private var ownCert : Null<Certificate>;
  75. private var ownKey : Null<Key>;
  76. private var altSNIContexts : Null<Array<{match: String->Bool, key: Key, cert: Certificate}>>;
  77. private var sniCallback : hl.Bytes -> Context.SNICbResult;
  78. private var handshakeDone : Bool;
  79. private var isBlocking : Bool = true;
  80. private override function init() : Void {
  81. __s = sys.net.Socket.socket_new( false );
  82. input = new SocketInput( this );
  83. output = new SocketOutput( this );
  84. if( DEFAULT_VERIFY_CERT && DEFAULT_CA == null ){
  85. try {
  86. DEFAULT_CA = Certificate.loadDefaults();
  87. }catch( e : Dynamic ){}
  88. }
  89. verifyCert = DEFAULT_VERIFY_CERT;
  90. caCert = DEFAULT_CA;
  91. }
  92. public override function connect(host : sys.net.Host, port : Int) : Void {
  93. conf = buildConfig( false );
  94. ssl = new Context( conf );
  95. ssl.setSocket(__s);
  96. handshakeDone = false;
  97. if( hostname == null )
  98. hostname = host.host;
  99. if( hostname != null )
  100. ssl.setHostname( @:privateAccess hostname.toUtf8() );
  101. if( !sys.net.Socket.socket_connect( __s, host.ip, port ) )
  102. throw new Sys.SysError("Failed to connect on "+host.toString()+":"+port);
  103. if( isBlocking )
  104. handshake();
  105. }
  106. public function handshake() : Void {
  107. if( !handshakeDone ){
  108. var r = ssl.handshake();
  109. if( r == 0 )
  110. handshakeDone = true;
  111. else if( r == -1 )
  112. throw haxe.io.Error.Blocked;
  113. else
  114. throw new haxe.io.Eof();
  115. }
  116. }
  117. override function setBlocking( b : Bool ) : Void {
  118. super.setBlocking(b);
  119. isBlocking = b;
  120. }
  121. public function setCA( cert : Certificate ) : Void {
  122. caCert = cert;
  123. }
  124. public function setHostname( name : String ) : Void {
  125. hostname = name;
  126. }
  127. public function setCertificate( cert : Certificate, key : Key ) : Void {
  128. ownCert = cert;
  129. ownKey = key;
  130. }
  131. public override function close() : Void {
  132. if( ssl != null ) ssl.close();
  133. if( conf != null ) conf.close();
  134. if( altSNIContexts != null )
  135. sniCallback = null;
  136. sys.net.Socket.socket_close( __s );
  137. var input : SocketInput = cast input;
  138. var output : SocketOutput = cast output;
  139. @:privateAccess input.__s = output.__s = null;
  140. input.close();
  141. output.close();
  142. }
  143. public function addSNICertificate( cbServernameMatch : String->Bool, cert : Certificate, key : Key ) : Void {
  144. if( altSNIContexts == null )
  145. altSNIContexts = [];
  146. altSNIContexts.push( {match: cbServernameMatch, cert: cert, key: key} );
  147. }
  148. public override function bind( host : sys.net.Host, port : Int ) : Void {
  149. conf = buildConfig( true );
  150. sys.net.Socket.socket_bind( __s, host.ip, port );
  151. }
  152. public override function accept() : Socket {
  153. var c = sys.net.Socket.socket_accept( __s );
  154. var cssl = new Context( conf );
  155. cssl.setSocket(c);
  156. var s = Type.createEmptyInstance( sys.ssl.Socket );
  157. s.__s = c;
  158. s.ssl = cssl;
  159. s.input = new SocketInput(s);
  160. s.output = new SocketOutput(s);
  161. s.handshakeDone = false;
  162. return s;
  163. }
  164. public function peerCertificate() : sys.ssl.Certificate {
  165. var x = ssl.getPeerCertificate();
  166. return x==null ? null : new sys.ssl.Certificate( x );
  167. }
  168. private function buildConfig( server : Bool ) : Context.Config {
  169. var conf = new Context.Config( server );
  170. if( ownCert != null && ownKey != null )
  171. conf.setCert( @:privateAccess ownCert.__x, @:privateAccess ownKey.__k );
  172. if ( altSNIContexts != null ) {
  173. sniCallback = function(servername:hl.Bytes) : Context.SNICbResult {
  174. var servername = @:privateAccess String.fromUTF8(servername);
  175. for( c in altSNIContexts ){
  176. if( c.match(servername) )
  177. return new Context.SNICbResult(c.cert, c.key);
  178. }
  179. if( ownKey != null && ownCert != null )
  180. return new Context.SNICbResult(ownCert, ownKey);
  181. return null;
  182. }
  183. conf.setServernameCallback(sniCallback);
  184. }
  185. if ( caCert != null )
  186. conf.setCa( caCert == null ? null : @:privateAccess caCert.__x );
  187. conf.setVerify( if( verifyCert ) 1 else if( verifyCert==null ) 2 else 0 );
  188. return conf;
  189. }
  190. }