-
Notifications
You must be signed in to change notification settings - Fork 15
/
DtlsSocket.js
131 lines (99 loc) · 4.25 KB
/
DtlsSocket.js
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
"use strict";
var util = require( 'util' );
var EventEmitter = require( 'events' ).EventEmitter;
var log = require( 'logg' ).getLogger( 'dtls.DtlsSocket' );
var crypto = require( 'crypto' );
var constants = require( 'constants' );
var dgram = require( 'dgram' );
var dtls = require( './dtls' );
var SecurityParameterContainer = require( './SecurityParameterContainer' );
var DtlsRecordLayer = require( './DtlsRecordLayer' );
var ServerHandshakeHandler = require( './ServerHandshakeHandler' );
var ClientHandshakeHandler = require( './ClientHandshakeHandler' );
var HandshakeBuilder = require( './HandshakeBuilder' );
var CipherInfo = require( './CipherInfo' );
var DtlsHandshake = require( './packets/DtlsHandshake' );
var DtlsClientHello = require( './packets/DtlsClientHello' );
var DtlsHelloVerifyRequest = require( './packets/DtlsHelloVerifyRequest' );
var DtlsServerHello = require( './packets/DtlsServerHello' );
var DtlsCertificate = require( './packets/DtlsCertificate' );
var DtlsServerHelloDone = require( './packets/DtlsServerHelloDone' );
var DtlsClientKeyExchange_rsa = require( './packets/DtlsClientKeyExchange_rsa' );
var DtlsPreMasterSecret = require( './packets/DtlsPreMasterSecret' );
var DtlsChangeCipherSpec = require( './packets/DtlsChangeCipherSpec' );
var DtlsFinished = require( './packets/DtlsFinished' );
var DtlsExtension = require( './packets/DtlsExtension' );
var DtlsProtocolVersion = require( './packets/DtlsProtocolVersion' );
var DtlsRandom = require( './packets/DtlsRandom' );
var SocketState = {
uninitialized: 0,
sendHello: 1,
handshakeDone: 2,
clientFinished: 3,
};
var DtlsSocket = function( dgram, rinfo, keyContext, isServer ) {
log.info( 'New session' );
this.dgram = dgram;
this.rinfo = rinfo;
this.keyContext = keyContext;
this.parameters = new SecurityParameterContainer();
this.recordLayer = new DtlsRecordLayer( dgram, rinfo, this.parameters );
this.handshakeHandler = isServer
? new ServerHandshakeHandler( this.parameters, this.keyContext )
: new ClientHandshakeHandler( this.parameters );
this.handshakeHandler.onSend = function( packets ) {
this.recordLayer.send( packets );
}.bind( this );
this.handshakeHandler.onHandshake = function() {
this.emit( 'secureConnection', this );
}.bind( this );
};
util.inherits( DtlsSocket, EventEmitter );
DtlsSocket.connect = function( port, address, type, callback ) {
var dgramSocket = dgram.createSocket( type );
var socket = new DtlsSocket( dgramSocket, { address: address, port: port });
socket.renegotiate();
dgramSocket.on( 'message', socket.handle.bind( socket ) );
if( callback )
socket.once( 'secureConnection', callback );
return socket;
};
DtlsSocket.prototype.renegotiate = function() {
this.handshakeHandler.renegotiate();
};
DtlsSocket.prototype.send = function( buffer, offset, length, callback ) {
// Slice the buffer if we have offset specified and wrap it into a packet
// structure that holds the message type as well.
if( offset )
buffer = buffer.slice( offset, offset + length );
var lengthBuffer = new Buffer(2);
lengthBuffer.writeUInt16BE( buffer.length );
var packet = {
type: dtls.MessageType.applicationData,
buffer: Buffer.concat([ buffer ])
};
this.recordLayer.send( packet, callback );
};
DtlsSocket.prototype.handle = function( buffer ) {
var self = this;
log.fine( 'Incoming packet; length:', buffer.length );
this.recordLayer.getPackets( buffer, function( packet ) {
var msgType = dtls.MessageTypeName[ packet.type ];
var handler = self[ 'process_' + msgType ];
if( !handler )
return log.error( 'Handler not found for', msgType, 'message' );
handler.call( self, packet );
});
};
DtlsSocket.prototype.process_handshake = function( message ) {
this.handshakeHandler.process( message );
};
DtlsSocket.prototype.process_changeCipherSpec = function( message ) {
// Record layer does the work here.
log.info( 'Changed Cipher Spec' );
};
DtlsSocket.prototype.process_applicationData = function( message ) {
log.info( 'Received application data: ', message.fragment );
this.emit( 'message', message.fragment );
};
module.exports = DtlsSocket;