2

我正在尝试为使用 Coturn 的项目设置 TURN 服务器,但我发现文档充其量是粗略的......

我意识到有一个turnadmin工具可以为你做这件事,但我更喜欢直接在我的数据库上运行查询。这是一个可能有许多用户的应用程序,他们的共享密钥 ( hmackeyin turnusers_lt) 可能会更改(为了不与应用程序共享密码,该应用程序使用“假”密码,该密码是某些易失性用户参数的哈希值,这些参数不是如此秘密)。

我可以从很少的文档中收集到hmackey是使用领域、用户名和密码计算的:

$ turnadmin -k -u myusername -r my.realm.org -p my-password
> e.g. 0x7a69b0e2b747a4560045f79d171b78c0

鉴于我的代码将知道这三个参数,我如何构建 hmac 哈希?例如在 PHP 中我有

string hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = false ] )

$algo这里应该是SHA1,但是什么值会进入$data(例如用户/通行证的连接)和$key(例如领域)?

还有一个turn_secret表列出了领域的“值”,我猜这应该用作$key上面示例中的值,但是当我调用 turnadmin 时,添加和修改键仍然给出相同的结果。

本质上,我想做的是(伪代码):

// user registers
// pseudo-code, this is of course computed using php's password_hash function
$hashed_pw = hash($pw);
$db->query('insert into usertable (name, pass) values ($name, $hashed_pw)');

// this is implemented somewhere...
$coturn_pw = get_secret_hash($name);

// this needs implementing...
$HAMC = calc_hmac($name, $coturn_pw, 'my.realm.com');

$turndb->query('insert into turnusers_lt values (...)');

// on update, delete also update turnusers_lt

...然后在客户端,我现在应该能够使用$name$coturn_pw作为my.realm.com.

还是我想太多了,我应该为我的应用程序使用通用用户,硬编码密码并让 Coturn 弄清楚谁在和谁说话?

4

2 回答 2

2

RFC 5389中描述了如何构建 HMAC 密钥:

key = MD5(用户名“:”领域“:”SASLprep(密码))

其中 MD5 在RFC 1321中定义,SASLprep() 在RFC 4013中定义

您需要更新的唯一表是turnusers_lt. 该turn_secret表和 SHA1 算法用于生成限时凭证

INSERT INTO turnusers_lt (realm, name, hmackey) VALUES (:realm, :username, :key);

当然,使用准备好的语句而不是手动构建 SQL 字符串。

于 2017-06-02T10:49:58.310 回答
2

OrangeDog 的答案是正确的。

使用 node.js:

const crypto= require("crypto");

const username= "foo";
const realm= "here";
const password= "secret";

const hmac = crypto
        .createHash("md5")
        .update(`${username}:${realm}:${password}`)
        .digest("hex")
;
于 2018-10-11T16:22:02.353 回答