DTLS: Difference between revisions
Jump to navigation
Jump to search
(Created page with "Datagram Transport Layer Security https://en.wikipedia.org/wiki/Datagram_Transport_Layer_Security provides communications security for datagram protocols to prevent eavesdrop...") |
|||
| Line 17: | Line 17: | ||
cd example/ |
cd example/ |
||
</pre> |
</pre> |
||
Modifier client.js |
|||
<pre> |
|||
"use strict"; |
|||
var dtls = require( '../' ); |
|||
var fs = require( 'fs' ); |
|||
var cpt=0; |
|||
dtls.setLogLevel( dtls.logLevel.FINE ); |
|||
var pem = fs.readFileSync( 'cert.pem' ); |
|||
var client = dtls.connect( 4433, 'localhost', 'udp4', function() { |
|||
console.log( 'Sending application data' ); |
|||
client.send( new Buffer( 'foo\n', 'utf8') ); |
|||
}); |
|||
client.on( 'message', function( msg ) { |
|||
console.log( 'Received application data' ); |
|||
console.log( msg.toString('utf8') ); |
|||
console.log( 'Sending application data' ); |
|||
client.send( new Buffer( process.argv[2] +': cpt=' + (cpt++) +'\n' ) ); |
|||
}); |
|||
</pre> |
|||
Modifier server.js |
|||
<pre> |
|||
"use strict"; |
|||
var dtls = require( '../' ); |
|||
var fs = require( 'fs' ); |
|||
dtls.setLogLevel( dtls.logLevel.INFO ); |
|||
var cert = fs.readFileSync( 'cert.pem' ); |
|||
var key = fs.readFileSync( 'key.pem' ); |
|||
var server = dtls.createServer({ |
|||
type: 'udp4', |
|||
key: key, |
|||
cert: cert |
|||
}); |
|||
server.bind( 4433 ); |
|||
server.on( 'secureConnection', function( socket ) { |
|||
console.log( 'New connection from ' + |
|||
[ socket.rinfo.address, socket.rinfo.port ].join(':') ); |
|||
socket.on( 'message', function( message ) { |
|||
// Get the ascii encoded text content and trim whitespace at the end. |
|||
var inText = message.toString( 'ascii' ).replace( /\s*$/, '' ); |
|||
var outText = '[ECHO]' + inText + '[/ECHO]'; |
|||
console.log( 'in: ' + inText ); |
|||
console.log( 'out: ' + outText ); |
|||
socket.send( new Buffer( outText + '\n', 'ascii' ) ); |
|||
}); |
|||
}); |
|||
</pre> |
|||
Générer un certificat |
Générer un certificat |
||
| Line 23: | Line 87: | ||
</pre> |
</pre> |
||
Exécuter |
Exécuter dans un terminal 1 |
||
<pre> |
|||
| ⚫ | |||
</pre> |
|||
Exécuter dans un terminal 2 |
|||
<pre> |
|||
node client.js CLIENT1 |
|||
</pre> |
|||
Exécuter dans un terminal 3 |
|||
<pre> |
<pre> |
||
node client.js CLIENT2 |
|||
cp cert.pem server.pem |
|||
| ⚫ | |||
^C |
|||
</pre> |
</pre> |
||
Revision as of 14:18, 18 February 2016
Datagram Transport Layer Security https://en.wikipedia.org/wiki/Datagram_Transport_Layer_Security
provides communications security for datagram protocols to prevent eavesdropping, tampering, or message forgery.
API
Java
http://git.bouncycastle.org/repositories/bc-java
Node.js
Installer
git clone https://github.com/Rantanen/node-dtls.git cd node-dtls/ npm install cd example/
Modifier client.js
"use strict";
var dtls = require( '../' );
var fs = require( 'fs' );
var cpt=0;
dtls.setLogLevel( dtls.logLevel.FINE );
var pem = fs.readFileSync( 'cert.pem' );
var client = dtls.connect( 4433, 'localhost', 'udp4', function() {
console.log( 'Sending application data' );
client.send( new Buffer( 'foo\n', 'utf8') );
});
client.on( 'message', function( msg ) {
console.log( 'Received application data' );
console.log( msg.toString('utf8') );
console.log( 'Sending application data' );
client.send( new Buffer( process.argv[2] +': cpt=' + (cpt++) +'\n' ) );
});
Modifier server.js
"use strict";
var dtls = require( '../' );
var fs = require( 'fs' );
dtls.setLogLevel( dtls.logLevel.INFO );
var cert = fs.readFileSync( 'cert.pem' );
var key = fs.readFileSync( 'key.pem' );
var server = dtls.createServer({
type: 'udp4',
key: key,
cert: cert
});
server.bind( 4433 );
server.on( 'secureConnection', function( socket ) {
console.log( 'New connection from ' +
[ socket.rinfo.address, socket.rinfo.port ].join(':') );
socket.on( 'message', function( message ) {
// Get the ascii encoded text content and trim whitespace at the end.
var inText = message.toString( 'ascii' ).replace( /\s*$/, '' );
var outText = '[ECHO]' + inText + '[/ECHO]';
console.log( 'in: ' + inText );
console.log( 'out: ' + outText );
socket.send( new Buffer( outText + '\n', 'ascii' ) );
});
});
Générer un certificat
openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem
Exécuter dans un terminal 1
node server.js
Exécuter dans un terminal 2
node client.js CLIENT1
Exécuter dans un terminal 3
node client.js CLIENT2