3709192002-03-20Martin Nilsson #pike __REAL_VERSION__
34adca2004-02-03Martin Nilsson /* $Id: https.pike,v 1.15 2004/02/03 13:52:40 nilsson Exp $
33ef431997-03-13Niels Möller  *
e200aa1997-04-18Niels Möller  * dummy https server
33ef431997-03-13Niels Möller  */
f5bb032001-09-17Martin Nilsson //! Dummy HTTPS server
33ef431997-03-13Niels Möller #define PORT 25678 import Stdio; inherit "sslport"; string my_certificate = MIME.decode_base64( "MIIBxDCCAW4CAQAwDQYJKoZIhvcNAQEEBQAwbTELMAkGA1UEBhMCREUxEzARBgNV\n" "BAgTClRodWVyaW5nZW4xEDAOBgNVBAcTB0lsbWVuYXUxEzARBgNVBAoTClRVIEls\n" "bWVuYXUxDDAKBgNVBAsTA1BNSTEUMBIGA1UEAxMLZGVtbyBzZXJ2ZXIwHhcNOTYw\n" "NDMwMDUzNjU4WhcNOTYwNTMwMDUzNjU5WjBtMQswCQYDVQQGEwJERTETMBEGA1UE\n" "CBMKVGh1ZXJpbmdlbjEQMA4GA1UEBxMHSWxtZW5hdTETMBEGA1UEChMKVFUgSWxt\n" "ZW5hdTEMMAoGA1UECxMDUE1JMRQwEgYDVQQDEwtkZW1vIHNlcnZlcjBcMA0GCSqG\n" "SIb3DQEBAQUAA0sAMEgCQQDBB6T7bGJhRhRSpDESxk6FKh3iKKrpn4KcDtFM0W6s\n" "16QSPz6J0Z2a00lDxudwhJfQFkarJ2w44Gdl/8b+de37AgMBAAEwDQYJKoZIhvcN\n" "AQEEBQADQQB5O9VOLqt28vjLBuSP1De92uAiLURwg41idH8qXxmylD39UE/YtHnf\n" "bC6QS0pqetnZpQj1yEsjRTeVfuRfANGw\n"); string my_key = MIME.decode_base64( "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"); class conn { import Stdio; object sslfile; string message = "<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"; 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) {
8dcd741997-03-15Niels Möller #ifdef SSL3_DEBUG
5f22e31997-03-17Niels Möller  werror("Received: '" + data + "'\n");
8dcd741997-03-15Niels Möller #endif
5f22e31997-03-17Niels Möller  sslfile->set_write_callback(write_callback);
33ef431997-03-13Niels Möller  } void create(object f) { 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  void create(string|void secret) { if (!secret) secret = sprintf("Foo!%4c", time());
34adca2004-02-03Martin Nilsson  arcfour->set_encrypt_key(Crypto.SHA->hash(secret));
33ef431997-03-13Niels Möller  } string read(int size) {
ad78742000-03-28Henrik Grubbström (Grubba)  return arcfour->crypt(replace(allocate(size), 0, "\021") * "");
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 */ void my_accept_callback(object f) { werror("Accept!\n");
5f22e31997-03-17Niels Möller  conn(accept());
33ef431997-03-13Niels Möller } int main() {
8dcd741997-03-15Niels Möller #ifdef SSL3_DEBUG
6244142003-01-27Martin Nilsson  werror("Cert: '%s'\n", Crypto.string_to_hex(my_certificate)); werror("Key: '%s'\n", Crypto.string_to_hex(my_key)); // werror("Decoded cert: %O\n", SSL.asn1.ber_decode(my_certificate)->get_asn1());
8dcd741997-03-15Niels Möller #endif
62c4d51999-03-17Henrik Grubbström (Grubba) #if 0
6ecec91997-03-15Niels Möller  array key = SSL.asn1.ber_decode(my_key)->get_asn1()[1];
8dcd741997-03-15Niels Möller #ifdef SSL3_DEBUG
6244142003-01-27Martin Nilsson  werror("Decoded key: %O\n", key);
8dcd741997-03-15Niels Möller #endif
cfc83a1997-03-15Niels Möller  object n = key[1][1]; object e = key[2][1]; object d = key[3][1]; object p = key[4][1]; object q = key[5][1];
33ef431997-03-13Niels Möller 
6244142003-01-27Martin Nilsson  werror("n = %s\np = %s\nq = %s\npq = %s\n", n->digits(), p->digits(), q->digits(), (p*q)->digits());
8dcd741997-03-15Niels Möller 
34adca2004-02-03Martin Nilsson  rsa = Crypto.RSA();
33ef431997-03-13Niels Möller  rsa->set_public_key(n, e); rsa->set_private_key(d);
62c4d51999-03-17Henrik Grubbström (Grubba) #else /* !0 */ // FIXME: Is this correct? rsa = Standards.PKCS.RSA.parse_private_key(my_key); #endif /* 0 */
33ef431997-03-13Niels Möller  certificates = ({ my_certificate }); random = no_random()->read; werror("Starting\n");
cfc83a1997-03-15Niels Möller  if (!bind(PORT, my_accept_callback)) { perror(""); return 17; } else return -17;
33ef431997-03-13Niels Möller }
6ecec91997-03-15Niels Möller  void create() {
8dcd741997-03-15Niels Möller #ifdef SSL3_DEBUG
6ecec91997-03-15Niels Möller  werror("https->create\n");
5f22e31997-03-17Niels Möller #endif
6ecec91997-03-15Niels Möller  sslport::create(); }
aa77d52001-04-18Pär Svensson