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; program request_program=Request;
3d13aa2002-11-27Martin Nilsson //! The simplest SSL server possible. Binds a port and calls //! a callback with @[Request] 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
6343a32002-11-22H. William Welliver III static void new_connection() {
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  string my_certificate = MIME.decode_base64(
6343a32002-11-22H. William Welliver III  "MIIBxDCCAW4CAQAwDQYJKoZIhvcNAQEEBQAwbTELMAkGA1UEBhMCREUxEzARBgNV\n" "BAgTClRodWVyaW5nZW4xEDAOBgNVBAcTB0lsbWVuYXUxEzARBgNVBAoTClRVIEls\n" "bWVuYXUxDDAKBgNVBAsTA1BNSTEUMBIGA1UEAxMLZGVtbyBzZXJ2ZXIwHhcNOTYw\n" "NDMwMDUzNjU4WhcNOTYwNTMwMDUzNjU5WjBtMQswCQYDVQQGEwJERTETMBEGA1UE\n" "CBMKVGh1ZXJpbmdlbjEQMA4GA1UEBxMHSWxtZW5hdTETMBEGA1UEChMKVFUgSWxt\n" "ZW5hdTEMMAoGA1UECxMDUE1JMRQwEgYDVQQDEwtkZW1vIHNlcnZlcjBcMA0GCSqG\n" "SIb3DQEBAQUAA0sAMEgCQQDBB6T7bGJhRhRSpDESxk6FKh3iKKrpn4KcDtFM0W6s\n" "16QSPz6J0Z2a00lDxudwhJfQFkarJ2w44Gdl/8b+de37AgMBAAEwDQYJKoZIhvcN\n" "AQEEBQADQQB5O9VOLqt28vjLBuSP1De92uAiLURwg41idH8qXxmylD39UE/YtHnf\n" "bC6QS0pqetnZpQj1yEsjRTeVfuRfANGw\n");
a2be872005-12-28Martin Nilsson  string my_key = MIME.decode_base64(
6343a32002-11-22H. William Welliver III  "MIIBOwIBAAJBAMEHpPtsYmFGFFKkMRLGToUqHeIoqumfgpwO0UzRbqzXpBI/PonR\n" "nZrTSUPG53CEl9AWRqsnbDjgZ2X/xv517fsCAwEAAQJBALzUbJmkQm1kL9dUVclH\n" "A2MTe15VaDTY3N0rRaZ/LmSXb3laiOgBnrFBCz+VRIi88go3wQ3PKLD8eQ5to+SB\n" "oWECIQDrmq//unoW1+/+D3JQMGC1KT4HJprhfxBsEoNrmyIhSwIhANG9c0bdpJse\n" "VJA0y6nxLeB9pyoGWNZrAB4636jTOigRAiBhLQlAqhJnT6N+H7LfnkSVFDCwVFz3\n" "eygz2yL3hCH8pwIhAKE6vEHuodmoYCMWorT5tGWM0hLpHCN/z3Btm38BGQSxAiAz\n" "jwsOclu4b+H8zopfzpAaoB8xMcbs0heN+GNNI0h/dQ==\n"); /* PKCS#1 Private key structure: RSAPrivateKey ::= SEQUENCE { version Version, modulus INTEGER, -- n publicExponent INTEGER, -- e privateExponent INTEGER, -- d prime1 INTEGER, -- p prime2 INTEGER, -- q exponent1 INTEGER, -- d mod (p-1) exponent2 INTEGER, -- d mod (q-1) coefficient INTEGER -- (inverse of q) mod p } Version ::= INTEGER */
a2be872005-12-28Martin Nilsson  //! void set_default_keycert() { set_key(my_key); set_certificate(my_certificate); }
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) { return t=='O' && sprintf("%O(%O:%d)", this_program, interface, port); }
2b3fe22004-02-29Martin Nilsson #endif