3709192002-03-20Martin Nilsson #pike __REAL_VERSION__
e1fb092014-02-14Martin Nilsson #require constant(SSL.Cipher)
33ef431997-03-13Niels Möller 
8de1612013-10-26Henrik Grubbström (Grubba) //! Dummy HTTPS server/client
f5bb032001-09-17Martin Nilsson 
52b26e2013-12-06Henrik Grubbström (Grubba) #ifndef PORT
33ef431997-03-13Niels Möller #define PORT 25678
52b26e2013-12-06Henrik Grubbström (Grubba) #endif
33ef431997-03-13Niels Möller 
7871142013-08-01Martin Nilsson #ifdef SSL3_DEBUG #define SSL3_DEBUG_MSG(X ...) werror(X) #else /*! SSL3_DEBUG */ #define SSL3_DEBUG_MSG(X ...) #endif /* SSL3_DEBUG */
33ef431997-03-13Niels Möller import Stdio;
8de1612013-10-26Henrik Grubbström (Grubba) #ifndef HTTPS_CLIENT
d51db52004-04-20Martin Nilsson inherit SSL.sslport;
33ef431997-03-13Niels Möller 
8de1612013-10-26Henrik Grubbström (Grubba) protected void create() { SSL3_DEBUG_MSG("https->create\n"); sslport::create(); } void my_accept_callback(object f) { werror("Accept!\n"); conn(accept()); } #endif
33ef431997-03-13Niels Möller class conn { import Stdio; object sslfile;
b7a48e2013-11-27Henrik Grubbström (Grubba)  string message = "HTTP/1.0 200 Ok\r\n" "Connection: close\r\n" "Content-Length: 132\r\n" "Content-Type: text/html; charset=ISO-8859-1\r\n" "Date: Thu, 01 Jan 1970 00:00:01 GMT\r\n" "Server: Bare-Bones\r\n" "\r\n" "<html><head><title>SSL-3 server</title></head>\n" "<body><h1>This is a minimal SSL-3 http server</h1>\n" "<hr><it>/nisse</it></body></html>\n";
33ef431997-03-13Niels Möller  int index = 0;
5f22e31997-03-17Niels Möller  void write_callback()
33ef431997-03-13Niels Möller  {
ead9722003-01-20Martin Nilsson  if (index < sizeof(message))
33ef431997-03-13Niels Möller  { int written = sslfile->write(message[index..]); if (written > 0) index += written; else sslfile->close(); }
ead9722003-01-20Martin Nilsson  if (index == sizeof(message))
33ef431997-03-13Niels Möller  sslfile->close(); } void read_callback(mixed id, string data) {
7871142013-08-01Martin Nilsson  SSL3_DEBUG_MSG("Received: '" + data + "'\n");
5f22e31997-03-17Niels Möller  sslfile->set_write_callback(write_callback);
33ef431997-03-13Niels Möller  }
b55e172010-12-26Henrik Grubbström (Grubba)  protected void create(object f)
33ef431997-03-13Niels Möller  { sslfile = f;
5f22e31997-03-17Niels Möller  sslfile->set_nonblocking(read_callback, 0, 0);
33ef431997-03-13Niels Möller  } } class no_random {
34adca2004-02-03Martin Nilsson  object arcfour = Crypto.Arcfour();
33ef431997-03-13Niels Möller 
b55e172010-12-26Henrik Grubbström (Grubba)  protected void create(string|void secret)
33ef431997-03-13Niels Möller  { if (!secret)
2e68752014-04-02Martin Nilsson  secret = sprintf("%s%4c", random_string(32), time()); arcfour->set_encrypt_key(Crypto.SHA256.hash(secret)); read(1000);
33ef431997-03-13Niels Möller  } string read(int size) {
2e68752014-04-02Martin Nilsson  return arcfour->crypt( "\021"*size );
33ef431997-03-13Niels Möller  } } /* 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 */
8de1612013-10-26Henrik Grubbström (Grubba) class client
33ef431997-03-13Niels Möller {
8de1612013-10-26Henrik Grubbström (Grubba)  constant request = "HEAD / HTTP/1.0\r\n" "Host: localhost:" + PORT + "\r\n" "\r\n"; SSL.sslfile ssl; int sent; void write_cb() { int bytes = ssl->write(request[sent..]); if (bytes > 0) { sent += bytes; } else if (sent < 0) { werror("Failed to write data: %s\n", strerror(ssl->errno())); exit(17); } if (sent == sizeof(request)) { ssl->set_write_callback(UNDEFINED); } } void got_data(mixed ignored, string data) { werror("Data: %O\n", data); } void con_closed() { werror("Connection closed.\n"); exit(0); } protected void create(Stdio.File con) { SSL.context ctx = SSL.context(); ctx->random = no_random()->read;
cba3fe2014-03-25Henrik Grubbström (Grubba)  // Make sure all cipher suites are available.
74cb402014-03-25Henrik Grubbström (Grubba)  ctx->preferred_suites = ctx->get_suites(-1, 2);
8de1612013-10-26Henrik Grubbström (Grubba)  werror("Starting\n"); ssl = SSL.sslfile(con, ctx, 1); ssl->set_nonblocking(got_data, write_cb, con_closed); }
33ef431997-03-13Niels Möller } int main() {
8de1612013-10-26Henrik Grubbström (Grubba) #ifdef HTTPS_CLIENT Stdio.File con = Stdio.File(); if (!con->connect("127.0.0.1", PORT)) { werror("Failed to connect to server: %s\n", strerror(con->errno())); return 17; } client(con); return -17; #else
11410b2014-03-08Henrik Grubbström (Grubba)  Crypto.Sign key;
87af182014-04-01Martin Nilsson  string certificate;
11410b2014-03-08Henrik Grubbström (Grubba) 
2e68752014-04-02Martin Nilsson  key = Crypto.RSA()->generate_key(1024);
87af182014-04-01Martin Nilsson  certificate =
cba3fe2014-03-25Henrik Grubbström (Grubba)  Standards.X509.make_selfsigned_certificate(key, 3600*4, ([ "organizationName" : "Test", "commonName" : "*", ]));
87af182014-04-01Martin Nilsson  add_cert(key, ({ certificate }), ({ "*" }));
cba3fe2014-03-25Henrik Grubbström (Grubba) 
2e68752014-04-02Martin Nilsson  key = Crypto.DSA()->generate_key(1024, 160);
87af182014-04-01Martin Nilsson  certificate =
cba3fe2014-03-25Henrik Grubbström (Grubba)  Standards.X509.make_selfsigned_certificate(key, 3600*4, ([ "organizationName" : "Test", "commonName" : "*", ]));
87af182014-04-01Martin Nilsson  add_cert(key, ({ certificate }));
cba3fe2014-03-25Henrik Grubbström (Grubba)  #if constant(Crypto.ECC.Curve) key = Crypto.ECC.SECP_521R1.ECDSA()-> set_random(Crypto.Random.random_string)->generate_key();
87af182014-04-01Martin Nilsson  certificate =
cba3fe2014-03-25Henrik Grubbström (Grubba)  Standards.X509.make_selfsigned_certificate(key, 3600*4, ([ "organizationName" : "Test", "commonName" : "*", ]));
87af182014-04-01Martin Nilsson  add_cert(key, ({ certificate }));
cba3fe2014-03-25Henrik Grubbström (Grubba) #endif
74cb402014-03-25Henrik Grubbström (Grubba)  // Make sure all cipher suites are available. preferred_suites = get_suites(-1, 2);
d5d6e22014-04-01Martin Nilsson  SSL3_DEBUG_MSG("Cipher suites:\n%s", .Constants.fmt_cipher_suites(preferred_suites));
74cb402014-03-25Henrik Grubbström (Grubba) 
2dc3172014-03-10Henrik Grubbström (Grubba)  SSL3_DEBUG_MSG("Certs:\n%O\n", cert_pairs);
cba3fe2014-03-25Henrik Grubbström (Grubba) 
33ef431997-03-13Niels Möller  random = no_random()->read; werror("Starting\n");
cfc83a1997-03-15Niels Möller  if (!bind(PORT, my_accept_callback)) { perror(""); return 17; }
1614ba2013-11-24Henrik Grubbström (Grubba)  else { werror("Listening on port %d.\n", PORT);
cfc83a1997-03-15Niels Möller  return -17;
1614ba2013-11-24Henrik Grubbström (Grubba)  }
8de1612013-10-26Henrik Grubbström (Grubba) #endif
6ecec91997-03-15Niels Möller }