Skip to content

Commit 810afc4

Browse files
committed
Merge pull request #394 from jpevarnek/cloud-provider-verification
Add logic to verify the host key of a cloud server
2 parents 2c78542 + 38e6c36 commit 810afc4

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

src/cloud/social/provider.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// <reference path='../../../../third_party/typings/browser.d.ts' />
2+
/// <reference path='../../../../third_party/sha1/sha1.d.ts' />
23

34
require('../social/monkey/process');
45

@@ -8,6 +9,8 @@ import logging = require('../../logging/logging');
89
import promises = require('../../promises/promises');
910
import queue = require('../../handler/queue');
1011

12+
import sha1 = require('crypto/sha1');
13+
1114
// https://github.com/borisyankov/DefinitelyTyped/blob/master/ssh2/ssh2-tests.ts
1215
import * as ssh2 from 'ssh2';
1316
var Client = require('ssh2').Client;
@@ -44,6 +47,9 @@ interface Invite {
4447
key: string;
4548
// True iff uProxy has root access on the server, i.e. uProxy deployed it.
4649
isAdmin?: boolean;
50+
// Host key that should be used to verify the server, base-64 encoded
51+
// (from known_hosts file or public key)
52+
hostKey?: string;
4753
}
4854

4955
// Type of the object placed, in serialised form, in storage
@@ -474,6 +480,15 @@ class Connection {
474480
connectConfig['privateKey'] = new Buffer(this.invite_.key, 'base64');
475481
}
476482

483+
if (this.invite_.hostKey) {
484+
connectConfig.hostHash = 'sha1';
485+
let keyBuffer = new Buffer(this.invite_.hostKey, 'base64');
486+
let expectedHash = sha1.hex_sha1(keyBuffer.toString('binary'));
487+
connectConfig.hostVerifier = (keyHash :string) => {
488+
return keyHash === expectedHash;
489+
};
490+
}
491+
477492
return new Promise<void>((F, R) => {
478493
this.connection_.on('ready', () => {
479494
// TODO: set a timeout here, too

third_party/sha1/sha1.d.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
// TypeScript definitions for crypto's sha1 module.
22

33
declare module 'crypto/sha1' {
4+
/**
5+
* All function arguments are interpreted as "binary strings" (e.g. as
6+
* returned by Buffer.toString("binary")) so to supply binary data or key you
7+
* can construct a string with the help of String.fromCharCode(), e.g. [0x44,
8+
* 0x5d, 0x75] -> 'D]u'.
9+
*/
10+
11+
/**
12+
* Computes the SHA1 hash of some data with the specified key.
13+
* Returns a binary string.
14+
*/
15+
function str_sha1(data:string) : string
16+
17+
/** As above but returns a hex-formatted string */
18+
function hex_sha1(data:string) : string
19+
20+
/** As above but returns a base-64-formatted string */
21+
function b64_sha1(data:string) : string
22+
423
/**
524
* Computes the HMAC-SHA1 of some data, with the specified key.
6-
* Both key and data are interpreted as "binary strings" so to supply binary
7-
* data or key you can construct a string with the help of
8-
* String.fromCharCode(), e.g. [0x44, 0x5d, 0x75] -> 'D]u'.
9-
* Ditto for return type.
25+
* Returns a binary string.
1026
*/
1127
function str_hmac_sha1(key:string, data:string) : string
1228

1329
/** As str_hmac_sha1 but returns a hex-formatted string. */
1430
function hex_hmac_sha1(key:string, data:string) : string
31+
32+
/** As above but returns a base-64-formatted string */
33+
function b64_hmac_sha1(key:string, data:string) : string
1534
}

0 commit comments

Comments
 (0)