summaryrefslogtreecommitdiff
path: root/school/node_modules/node-forge/tests/nodejs-ws.js
blob: 164962d5b4c0bd9ec251a911130397a5a9f1bf22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var sys = require('sys');
var ws = require('./ws');
var forge = require('../js/forge');

// function to create certificate
var createCert = function(cn, data)
{
   sys.puts(
      'Generating 512-bit key-pair and certificate for \"' + cn + '\".');
   var keys = forge.pki.rsa.generateKeyPair(512);
   sys.puts('key-pair created.');

   var cert = forge.pki.createCertificate();
   cert.serialNumber = '01';
   cert.validity.notBefore = new Date();
   cert.validity.notAfter = new Date();
   cert.validity.notAfter.setFullYear(
      cert.validity.notBefore.getFullYear() + 1);
   var attrs = [{
      name: 'commonName',
      value: cn
   }, {
      name: 'countryName',
      value: 'US'
   }, {
      shortName: 'ST',
      value: 'Virginia'
   }, {
      name: 'localityName',
      value: 'Blacksburg'
   }, {
      name: 'organizationName',
      value: 'Test'
   }, {
      shortName: 'OU',
      value: 'Test'
   }];
   cert.setSubject(attrs);
   cert.setIssuer(attrs);
   cert.setExtensions([{
      name: 'basicConstraints',
      cA: true
   }, {
      name: 'keyUsage',
      keyCertSign: true,
      digitalSignature: true,
      nonRepudiation: true,
      keyEncipherment: true,
      dataEncipherment: true
   }, {
      name: 'subjectAltName',
      altNames: [{
         type: 6, // URI
         value: 'http://myuri.com/webid#me'
      }]
   }]);
   // FIXME: add subjectKeyIdentifier extension
   // FIXME: add authorityKeyIdentifier extension
   cert.publicKey = keys.publicKey;
   
   // self-sign certificate
   cert.sign(keys.privateKey);
   
   // save data
   data[cn] = {
      cert: forge.pki.certificateToPem(cert),
      privateKey: forge.pki.privateKeyToPem(keys.privateKey)
   };
   
   sys.puts('certificate created for \"' + cn + '\": \n' + data[cn].cert);
};

var data = {};

// create certificate for server
createCert('server', data);

// function to create TLS server connection
var createTls = function(websocket)
{
   return forge.tls.createConnection(
   {
      server: true,
      caStore: [],
      sessionCache: {},
      // supported cipher suites in order of preference
      cipherSuites: [
         forge.tls.CipherSuites.TLS_RSA_WITH_AES_128_CBC_SHA,
         forge.tls.CipherSuites.TLS_RSA_WITH_AES_256_CBC_SHA],
      connected: function(c)
      {
         sys.puts('Server connected');
      },
      verifyClient: true,
      verify: function(c, verified, depth, certs)
      {
         sys.puts(
            'Server verifying certificate w/CN: \"' +
            certs[0].subject.getField('CN').value +
            '\", verified: ' + verified + '...');
         
         // accept any certificate (could actually do WebID authorization from
         // here within the protocol)
         return true;
      },
      getCertificate: function(c, hint)
      {
         sys.puts('Server getting certificate for \"' + hint[0] + '\"...');
         return data.server.cert;
      },
      getPrivateKey: function(c, cert)
      {
         return data.server.privateKey;
      },
      tlsDataReady: function(c)
      {
         // send base64-encoded TLS data over websocket
         websocket.write(forge.util.encode64(c.tlsData.getBytes()));
      },
      dataReady: function(c)
      {
         sys.puts('Server received \"' + c.data.getBytes() + '\"');
         
         // send response
         c.prepare('Hello Client');
      },
      closed: function(c)
      {
         sys.puts('Server disconnected.');
         websocket.end();
      },
      error: function(c, error)
      {
         sys.puts('Server error: ' + error.message);
      }
   });
};

// create websocket server
var port = 8080;
ws.createServer(function(websocket)
{
   // create TLS server connection
   var tls = createTls(websocket);
   
   websocket.addListener('connect', function(resource)
   {
      sys.puts('connected: ' + resource);
      
      // close connection after 10 seconds
      setTimeout(websocket.end, 10 * 1000);
   });
   
   websocket.addListener('data', function(data)
   {
      // base64-decode data and process it
      tls.process(forge.util.decode64(data));
   });
   
   websocket.addListener('close', function()
   {
      sys.puts('closed');
   });
}).listen(port);

sys.puts('server running on port ' + port);