6343a32002-11-22H. William Welliver III #pike __REAL_VERSION__
2b3fe22004-02-29Martin Nilsson #if constant(SSL.Cipher.CipherAlgorithm)
6343a32002-11-22H. William Welliver III import ".";
3d13aa2002-11-27Martin Nilsson MySSLPort port;
6343a32002-11-22H. William Welliver III int portno; string|int(0..0) interface; function(Request:void) callback;
e2c7ee2013-09-06Arne Goedeke //! object|function|program request_program=Request;
6343a32002-11-22H. William Welliver III 
3d13aa2002-11-27Martin Nilsson //! The simplest SSL server possible. Binds a port and calls
e2c7ee2013-09-06Arne Goedeke //! a callback with @[request_program] objects.
6343a32002-11-22H. William Welliver III 
f5553d2002-11-26H. William Welliver III //! Create a HTTPS (HTTP over SSL) server. //! //! @param _callback
3d13aa2002-11-27Martin Nilsson //! The function run when a request is received. //! takes one argument of type @[Request].
f5553d2002-11-26H. William Welliver III //! @param _portno
3d13aa2002-11-27Martin Nilsson //! The port number to bind to, defaults to 443.
f5553d2002-11-26H. William Welliver III //! @param _interface
3d13aa2002-11-27Martin Nilsson //! The interface address to bind to.
f5553d2002-11-26H. William Welliver III //! @param key
3d13aa2002-11-27Martin Nilsson //! An optional SSL secret key, provided in binary format, such //! as that created by @[Standards.PKCS.RSA.private_key()].
f5553d2002-11-26H. William Welliver III //! @param certificate
3f76cd2004-01-15H. William Welliver III //! An optional SSL certificate or chain of certificates with the host //! certificate first, provided in binary format.
6343a32002-11-22H. William Welliver III void create(function(Request:void) _callback, void|int _portno,
3f76cd2004-01-15H. William Welliver III  void|string _interface, void|string key, void|string|array certificate)
6343a32002-11-22H. William Welliver III { portno=_portno;
f5553d2002-11-26H. William Welliver III  if (!portno) portno=443; // default HTTPS port
6343a32002-11-22H. William Welliver III  callback=_callback; interface=_interface;
3d13aa2002-11-27Martin Nilsson  port=MySSLPort();
6343a32002-11-22H. William Welliver III  port->set_default_keycert(); if(key) port->set_key(key); if(certificate) port->set_certificate(certificate);
df60612005-12-28Martin Nilsson  if (!port->bind(portno,new_connection,[string]interface))
6343a32002-11-22H. William Welliver III  error("HTTP.Server.SSLPort: failed to bind port %s%d: %s\n", interface?interface+":":"", portno,strerror(port->errno())); }
3d13aa2002-11-27Martin Nilsson //! Closes the HTTP port.
6343a32002-11-22H. William Welliver III void close() { destruct(port); port=0; } void destroy() { close(); }
3d13aa2002-11-27Martin Nilsson //! The port accept callback
9eaf1d2008-06-28Martin Nilsson protected void new_connection()
6343a32002-11-22H. William Welliver III {
ef55472004-01-16H. William Welliver III  SSL.sslfile fd=port->accept();
6343a32002-11-22H. William Welliver III  Request r=request_program();
563bd72004-01-11Martin Nilsson  r->attach_fd(fd,this,callback);
6343a32002-11-22H. William Welliver III }
3d13aa2002-11-27Martin Nilsson //! class MySSLPort
6343a32002-11-22H. William Welliver III {
a2be872005-12-28Martin Nilsson  inherit SSL.sslport;
6343a32002-11-22H. William Welliver III 
a2be872005-12-28Martin Nilsson  //! void set_default_keycert() {
330b602013-10-15Martin Nilsson  rsa = Crypto.RSA(); rsa->generate_key( 4096, random_string ); array attrs = ({ ([ "organizationName" : Standards.ASN1.Types.PrintableString("Pike TLS server") ]), ([ "commonName" : Standards.ASN1.Types.PrintableString("*") ]), }); certificates = ({ Standards.X509.make_selfsigned_rsa_certificate(rsa, 3600*24*365, attrs) });
a2be872005-12-28Martin Nilsson  }
6343a32002-11-22H. William Welliver III 
a2be872005-12-28Martin Nilsson  //! void set_key(string skey) {
f5553d2002-11-26H. William Welliver III  rsa = Standards.PKCS.RSA.parse_private_key(skey);
6343a32002-11-22H. William Welliver III  }
a2be872005-12-28Martin Nilsson  //!
df60612005-12-28Martin Nilsson  void set_certificate(string|array(string) certificate)
a2be872005-12-28Martin Nilsson  { if(arrayp(certificate))
df60612005-12-28Martin Nilsson  certificates = [array(string)]certificate;
a2be872005-12-28Martin Nilsson  else
df60612005-12-28Martin Nilsson  certificates = ({ [string]certificate });
a2be872005-12-28Martin Nilsson  }
6343a32002-11-22H. William Welliver III  void create() { sslport::create();
f90e3c2004-02-03Martin Nilsson  random = Crypto.Random.random_string;
6343a32002-11-22H. William Welliver III  } }
2b3fe22004-02-29Martin Nilsson 
45210b2007-07-29Martin Nilsson string _sprintf(int t) {
08ca302010-02-13David Emanuel da Costa Santiago  return t=='O' && sprintf("%O(%O:%d)", this_program, interface, portno);
45210b2007-07-29Martin Nilsson }
2b3fe22004-02-29Martin Nilsson #endif