-
Notifications
You must be signed in to change notification settings - Fork 15
/
DtlsSocket.js
138 lines (105 loc) · 4.38 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
132
133
134
135
136
137
138
"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.isServer = isServer;
this.parameters = new SecurityParameterContainer();
this.recordLayer = new DtlsRecordLayer( dgram, rinfo, this.parameters );
this.handshakeHandler = isServer
? new ServerHandshakeHandler( this.parameters, this.keyContext, rinfo )
: new ClientHandshakeHandler( this.parameters );
this.handshakeHandler.onSend = function( packets, callback ) {
this.recordLayer.send( packets, callback );
}.bind( this );
this.handshakeHandler.onHandshake = function() {
log.info( 'Handshake done' );
this.emit( 'secureConnect', 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( 'secureConnect', 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 packet = {
type: dtls.MessageType.applicationData,
buffer: buffer
};
this.recordLayer.send( packet, callback );
};
DtlsSocket.prototype.close = function() {
if( this.isServer )
throw new Error(
'Attempting to close a server socket. Close the server instead' );
this.dgram.close();
};
DtlsSocket.prototype.handle = function( buffer ) {
var self = this;
this.recordLayer.getPackets( buffer, function( packet ) {
var handler = DtlsSocket.handlers[ packet.type ];
if( !handler ) {
var msgType = dtls.MessageTypeName[ packet.type ];
return log.error( 'Handler not found for', msgType, 'message' );
}
handler.call( self, packet );
});
};
DtlsSocket.handlers = [];
DtlsSocket.handlers[ dtls.MessageType.handshake ] = function( message ) {
this.handshakeHandler.process( message );
};
DtlsSocket.handlers[ dtls.MessageType.changeCipherSpec ] = function( message ) {
// Record layer does the work here.
log.info( 'Changed Cipher Spec' );
};
DtlsSocket.handlers[ dtls.MessageType.applicationData ] = function( message ) {
this.emit( 'message', message.fragment );
};
module.exports = DtlsSocket;