2
0

Socket.hx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package sys.ssl;
  2. import cpp.NativeSocket;
  3. import cpp.NativeSsl;
  4. private typedef SocketHandle = Dynamic;
  5. private typedef CONF = Dynamic;
  6. private typedef SSL = Dynamic;
  7. private class SocketInput extends haxe.io.Input {
  8. @:allow(sys.ssl.Socket) private var __s : Socket;
  9. public function new( s : Socket ) {
  10. this.__s = s;
  11. }
  12. public override function readByte() {
  13. return try {
  14. __s.handshake();
  15. NativeSsl.ssl_recv_char( @:privateAccess __s.ssl );
  16. } catch( e : Dynamic ) {
  17. if( e == "Blocking" )
  18. throw haxe.io.Error.Blocked;
  19. else if( __s == null )
  20. throw haxe.io.Error.Custom(e);
  21. else
  22. throw new haxe.io.Eof();
  23. }
  24. }
  25. public override function readBytes( buf : haxe.io.Bytes, pos : Int, len : Int ) : Int {
  26. var r : Int;
  27. if( __s == null )
  28. throw "Invalid handle";
  29. try {
  30. __s.handshake();
  31. r = NativeSsl.ssl_recv( @:privateAccess __s.ssl, buf.getData(), pos, len );
  32. } catch( e : Dynamic ) {
  33. if( e == "Blocking" )
  34. throw haxe.io.Error.Blocked;
  35. else
  36. throw haxe.io.Error.Custom(e);
  37. }
  38. if( r == 0 )
  39. throw new haxe.io.Eof();
  40. return r;
  41. }
  42. public override function close() {
  43. super.close();
  44. if( __s != null ) __s.close();
  45. }
  46. }
  47. private class SocketOutput extends haxe.io.Output {
  48. @:allow(sys.ssl.Socket) private var __s : Socket;
  49. public function new( s : Socket ) {
  50. this.__s = s;
  51. }
  52. public override function writeByte( c : Int ) {
  53. if( __s == null )
  54. throw "Invalid handle";
  55. try {
  56. __s.handshake();
  57. NativeSsl.ssl_send_char( @:privateAccess __s.ssl, c );
  58. } catch( e : Dynamic ) {
  59. if( e == "Blocking" )
  60. throw haxe.io.Error.Blocked;
  61. else
  62. throw haxe.io.Error.Custom(e);
  63. }
  64. }
  65. public override function writeBytes( buf : haxe.io.Bytes, pos : Int, len : Int) : Int {
  66. return try {
  67. __s.handshake();
  68. NativeSsl.ssl_send( @:privateAccess __s.ssl, buf.getData(), pos, len );
  69. } catch( e : Dynamic ) {
  70. if( e == "Blocking" )
  71. throw haxe.io.Error.Blocked;
  72. else
  73. throw haxe.io.Error.Custom(e);
  74. }
  75. }
  76. public override function close() {
  77. super.close();
  78. if( __s != null ) __s.close();
  79. }
  80. }
  81. @:coreApi
  82. class Socket extends sys.net.Socket {
  83. public static var DEFAULT_VERIFY_CERT : Null<Bool> = true;
  84. public static var DEFAULT_CA : Null<Certificate>;
  85. private var conf : CONF;
  86. private var ssl : SSL;
  87. public var verifyCert : Null<Bool>;
  88. private var caCert : Null<Certificate>;
  89. private var hostname : String;
  90. private var ownCert : Null<Certificate>;
  91. private var ownKey : Null<Key>;
  92. private var altSNIContexts : Null<Array<{match: String->Bool, key: Key, cert: Certificate}>>;
  93. private var sniCallback : Dynamic;
  94. private var handshakeDone : Bool;
  95. private override function init() : Void {
  96. __s = NativeSocket.socket_new( false );
  97. input = new SocketInput( this );
  98. output = new SocketOutput( this );
  99. if( DEFAULT_VERIFY_CERT && DEFAULT_CA == null ){
  100. try {
  101. DEFAULT_CA = Certificate.loadDefaults();
  102. }catch( e : Dynamic ){}
  103. }
  104. caCert = DEFAULT_CA;
  105. verifyCert = DEFAULT_VERIFY_CERT;
  106. }
  107. public override function connect(host : sys.net.Host, port : Int) : Void {
  108. try {
  109. conf = buildSSLConfig( false );
  110. ssl = NativeSsl.ssl_new( conf );
  111. handshakeDone = false;
  112. NativeSsl.ssl_set_socket( ssl, __s );
  113. if( hostname == null )
  114. hostname = host.host;
  115. if( hostname != null )
  116. NativeSsl.ssl_set_hostname( ssl, hostname );
  117. NativeSocket.socket_connect( __s, host.ip, port );
  118. handshake();
  119. } catch( s : String ) {
  120. if( s == "Invalid socket handle" )
  121. throw "Failed to connect on "+host.host+":"+port;
  122. else
  123. cpp.Lib.rethrow(s);
  124. } catch( e : Dynamic ) {
  125. cpp.Lib.rethrow(e);
  126. }
  127. }
  128. public function handshake() : Void {
  129. if( !handshakeDone ){
  130. try {
  131. NativeSsl.ssl_handshake( ssl );
  132. handshakeDone = true;
  133. } catch( e : Dynamic ) {
  134. if( e == "Blocking" )
  135. throw haxe.io.Error.Blocked;
  136. else
  137. cpp.Lib.rethrow( e );
  138. }
  139. }
  140. }
  141. public function setCA( cert : Certificate ) : Void {
  142. caCert = cert;
  143. }
  144. public function setHostname( name : String ) : Void {
  145. hostname = name;
  146. }
  147. public function setCertificate( cert : Certificate, key : Key ) : Void {
  148. ownCert = cert;
  149. ownKey = key;
  150. }
  151. public override function read() : String {
  152. handshake();
  153. var b = NativeSsl.ssl_read( ssl );
  154. if( b == null )
  155. return "";
  156. return haxe.io.Bytes.ofData(b).toString();
  157. }
  158. public override function write( content : String ) : Void {
  159. handshake();
  160. NativeSsl.ssl_write( ssl, haxe.io.Bytes.ofString(content).getData() );
  161. }
  162. public override function close() : Void {
  163. if( ssl != null ) NativeSsl.ssl_close( ssl );
  164. if( conf != null ) NativeSsl.conf_close( conf );
  165. if( altSNIContexts != null )
  166. sniCallback = null;
  167. NativeSocket.socket_close( __s );
  168. var input : SocketInput = cast input;
  169. var output : SocketOutput = cast output;
  170. @:privateAccess input.__s = output.__s = null;
  171. input.close();
  172. output.close();
  173. }
  174. public function addSNICertificate( cbServernameMatch : String->Bool, cert : Certificate, key : Key ) : Void {
  175. if( altSNIContexts == null )
  176. altSNIContexts = [];
  177. altSNIContexts.push( {match: cbServernameMatch, cert: cert, key: key} );
  178. }
  179. public override function bind( host : sys.net.Host, port : Int ) : Void {
  180. conf = buildSSLConfig( true );
  181. NativeSocket.socket_bind( __s, host.ip, port );
  182. }
  183. public override function accept() : Socket {
  184. var c = NativeSocket.socket_accept( __s );
  185. var ssl = NativeSsl.ssl_new( conf );
  186. NativeSsl.ssl_set_socket( ssl, c );
  187. var s = Type.createEmptyInstance( sys.ssl.Socket );
  188. s.__s = c;
  189. s.ssl = ssl;
  190. s.input = new SocketInput(s);
  191. s.output = new SocketOutput(s);
  192. s.handshakeDone = false;
  193. return s;
  194. }
  195. public function peerCertificate() : sys.ssl.Certificate {
  196. var x = NativeSsl.ssl_get_peer_certificate( ssl );
  197. return x==null ? null : new sys.ssl.Certificate( x );
  198. }
  199. private function buildSSLConfig( server : Bool ) : CONF {
  200. var conf : CONF = NativeSsl.conf_new( server );
  201. if( ownCert != null && ownKey != null )
  202. NativeSsl.conf_set_cert( conf, @:privateAccess ownCert.__x, @:privateAccess ownKey.__k );
  203. if ( altSNIContexts != null ) {
  204. sniCallback = function(servername) {
  205. var servername = new String(cast servername);
  206. for( c in altSNIContexts ){
  207. if( c.match(servername) )
  208. return @:privateAccess {key: c.key.__k, cert: c.cert.__x};
  209. }
  210. if( ownKey != null && ownCert != null )
  211. return @:privateAccess { key: ownKey.__k, cert: ownCert.__x };
  212. return null;
  213. }
  214. NativeSsl.conf_set_servername_callback( conf, sniCallback );
  215. }
  216. if ( caCert != null )
  217. NativeSsl.conf_set_ca( conf, caCert == null ? null : @:privateAccess caCert.__x );
  218. if( verifyCert == null )
  219. NativeSsl.conf_set_verify( conf, 2 );
  220. else
  221. NativeSsl.conf_set_verify( conf, verifyCert ? 1 : 0 );
  222. return conf;
  223. }
  224. static function __init__() : Void {
  225. NativeSsl.init();
  226. }
  227. }