3709192002-03-20Martin Nilsson #pike __REAL_VERSION__
576edc2014-02-14Martin Nilsson #require constant(SSL.Cipher)
33ef431997-03-13Niels Möller 
d0efae2013-10-26Henrik Grubbström (Grubba) //! Dummy HTTPS server/client
f5bb032001-09-17Martin Nilsson 
bf3f972013-12-06Henrik Grubbström (Grubba) #ifndef PORT
33ef431997-03-13Niels Möller #define PORT 25678
bf3f972013-12-06Henrik Grubbström (Grubba) #endif
33ef431997-03-13Niels Möller 
d35d322014-04-15Henrik Grubbström (Grubba) #ifndef CIPHER_BITS
df67102014-04-15Henrik Grubbström (Grubba) #define CIPHER_BITS 112
d35d322014-04-15Henrik Grubbström (Grubba) #endif #ifndef RSA_BITS #define RSA_BITS 4096 #endif #ifndef DSA_BITS #define DSA_BITS 2048 #endif #ifndef KE_MODE #define KE_MODE 1 #endif #ifndef HOST #define HOST "127.0.0.1" #endif
b06b5d2013-08-01Martin Nilsson #ifdef SSL3_DEBUG #define SSL3_DEBUG_MSG(X ...) werror(X) #else /*! SSL3_DEBUG */ #define SSL3_DEBUG_MSG(X ...) #endif /* SSL3_DEBUG */
7948952014-04-12Henrik Grubbström (Grubba) class MyContext {
3909e62014-05-15Martin Nilsson  inherit SSL.Context;
7948952014-04-12Henrik Grubbström (Grubba) 
6840852014-05-04Martin Nilsson  SSL.Alert alert_factory(.Connection con,
7948952014-04-12Henrik Grubbström (Grubba)  int level, int description, SSL.Constants.ProtocolVersion version,
0ddaf82014-04-24Martin Nilsson  string|void message)
7948952014-04-12Henrik Grubbström (Grubba)  { if (message) { werror("ALERT [%s: %d:%d]: %s", SSL.Constants.fmt_version(version), level, description, message); }
0ddaf82014-04-24Martin Nilsson  return ::alert_factory(con, level, description, version, message);
7948952014-04-12Henrik Grubbström (Grubba)  } }
d0efae2013-10-26Henrik Grubbström (Grubba) #ifndef HTTPS_CLIENT
b807c02014-04-12Henrik Grubbström (Grubba) SSL.sslport port;
d0efae2013-10-26Henrik Grubbström (Grubba)  void my_accept_callback(object f) {
54caaf2014-04-24Martin Nilsson  Conn(port->accept());
d0efae2013-10-26Henrik Grubbström (Grubba) } #endif
54caaf2014-04-24Martin Nilsson class Conn {
33ef431997-03-13Niels Möller  object sslfile;
da4f8b2013-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) {
b06b5d2013-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  } }
54caaf2014-04-24Martin Nilsson class Client
33ef431997-03-13Niels Möller {
d0efae2013-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;
54caaf2014-04-24Martin Nilsson 
d0efae2013-10-26Henrik Grubbström (Grubba)  void write_cb() { int bytes = ssl->write(request[sent..]); if (bytes > 0) { sent += bytes; } else if (sent < 0) {
54caaf2014-04-24Martin Nilsson  exit(17, "Failed to write data: %s\n", strerror(ssl->errno()));
d0efae2013-10-26Henrik Grubbström (Grubba)  } if (sent == sizeof(request)) { ssl->set_write_callback(UNDEFINED); } }
54caaf2014-04-24Martin Nilsson 
d0efae2013-10-26Henrik Grubbström (Grubba)  void got_data(mixed ignored, string data) { werror("Data: %O\n", data); }
54caaf2014-04-24Martin Nilsson 
d0efae2013-10-26Henrik Grubbström (Grubba)  void con_closed() {
54caaf2014-04-24Martin Nilsson  exit(0, "Connection closed.\n");
d0efae2013-10-26Henrik Grubbström (Grubba)  } protected void create(Stdio.File con) {
3909e62014-05-15Martin Nilsson  SSL.Context ctx = MyContext();
9c39422014-03-25Henrik Grubbström (Grubba)  // Make sure all cipher suites are available.
915d772014-03-25Henrik Grubbström (Grubba)  ctx->preferred_suites = ctx->get_suites(-1, 2);
d0efae2013-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 }
71756d2014-05-16Martin Nilsson string common_name; void make_certificate(SSL.Context ctx, Crypto.Sign key, void|Crypto.Hash hash) { mapping attrs = ([ "organizationName" : "Test", "commonName" : common_name, ]); string cert = Standards.X509.make_selfsigned_certificate(key, 3600*24, attrs, 0, hash); ctx->add_cert(key, ({ cert }), ({ "*" })); }
33ef431997-03-13Niels Möller int main() {
d0efae2013-10-26Henrik Grubbström (Grubba) #ifdef HTTPS_CLIENT Stdio.File con = Stdio.File();
d35d322014-04-15Henrik Grubbström (Grubba)  if (!con->connect(HOST, PORT)) {
d0efae2013-10-26Henrik Grubbström (Grubba)  werror("Failed to connect to server: %s\n", strerror(con->errno())); return 17; }
54caaf2014-04-24Martin Nilsson  Client(con);
d0efae2013-10-26Henrik Grubbström (Grubba)  return -17; #else
3909e62014-05-15Martin Nilsson  SSL.Context ctx = MyContext();
b807c02014-04-12Henrik Grubbström (Grubba) 
557b462014-03-08Henrik Grubbström (Grubba)  Crypto.Sign key;
935b342014-04-01Martin Nilsson  string certificate;
557b462014-03-08Henrik Grubbström (Grubba) 
71756d2014-05-16Martin Nilsson  common_name = gethostname();
d35d322014-04-15Henrik Grubbström (Grubba)  common_name = (gethostbyname(common_name) || ({ common_name }))[0]; werror("Common name: %O\n", common_name); werror("Generating RSA certificate (%d bits)...\n", RSA_BITS); key = Crypto.RSA()->generate_key(RSA_BITS);
71756d2014-05-16Martin Nilsson  make_certificate(ctx, key);
935b342014-04-01Martin Nilsson 
d35d322014-04-15Henrik Grubbström (Grubba)  // Compat with OLD clients.
71756d2014-05-16Martin Nilsson  make_certificate(ctx, key, Crypto.SHA1);
9c39422014-03-25Henrik Grubbström (Grubba) 
d35d322014-04-15Henrik Grubbström (Grubba)  werror("Generating DSA certificate (%d bits)...\n", DSA_BITS); catch { // NB: Not all versions of Nettle support q sizes other than 160. key = Crypto.DSA()->generate_key(DSA_BITS, 256);
71756d2014-05-16Martin Nilsson  make_certificate(ctx, key);
d35d322014-04-15Henrik Grubbström (Grubba)  }; // Compat with OLD clients. // // The old FIPS standard maxed out at 1024 & 160 bits with SHA-1.
e3bc622014-04-02Martin Nilsson  key = Crypto.DSA()->generate_key(1024, 160);
71756d2014-05-16Martin Nilsson  make_certificate(ctx, key, Crypto.SHA1);
9c39422014-03-25Henrik Grubbström (Grubba)  #if constant(Crypto.ECC.Curve)
d35d322014-04-15Henrik Grubbström (Grubba)  werror("Generating ECDSA certificate (%d bits)...\n", 521);
0f12b12014-04-02Martin Nilsson  key = Crypto.ECC.SECP_521R1.ECDSA()->generate_key();
71756d2014-05-16Martin Nilsson  make_certificate(ctx, key);
d35d322014-04-15Henrik Grubbström (Grubba)  // Compat with OLD clients. // // Unlikely to be needed, but the cost is minimal.
71756d2014-05-16Martin Nilsson  make_certificate(ctx, key, Crypto.SHA1);
9c39422014-03-25Henrik Grubbström (Grubba) #endif
915d772014-03-25Henrik Grubbström (Grubba)  // Make sure all cipher suites are available.
d35d322014-04-15Henrik Grubbström (Grubba)  ctx->preferred_suites = ctx->get_suites(CIPHER_BITS, KE_MODE);
bd7c0f2014-04-01Martin Nilsson  SSL3_DEBUG_MSG("Cipher suites:\n%s",
b807c02014-04-12Henrik Grubbström (Grubba)  .Constants.fmt_cipher_suites(ctx->preferred_suites)); SSL3_DEBUG_MSG("Certs:\n%O\n", ctx->cert_pairs); port = SSL.sslport(ctx);
9c39422014-03-25Henrik Grubbström (Grubba) 
33ef431997-03-13Niels Möller  werror("Starting\n");
b807c02014-04-12Henrik Grubbström (Grubba)  if (!port->bind(PORT, my_accept_callback))
cfc83a1997-03-15Niels Möller  {
54caaf2014-04-24Martin Nilsson  Stdio.perror("");
cfc83a1997-03-15Niels Möller  return 17; }
4652a22013-11-24Henrik Grubbström (Grubba)  else { werror("Listening on port %d.\n", PORT);
cfc83a1997-03-15Niels Möller  return -17;
4652a22013-11-24Henrik Grubbström (Grubba)  }
d0efae2013-10-26Henrik Grubbström (Grubba) #endif
6ecec91997-03-15Niels Möller }