1/* $OpenBSD: jpake.c,v 1.7 2012/06/18 11:43:53 dtucker Exp $ */ 2/* 3 * Copyright (c) 2008 Damien Miller. All rights reserved. 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18/* 19 * Shared components of zero-knowledge password auth using J-PAKE protocol 20 * as described in: 21 * 22 * F. Hao, P. Ryan, "Password Authenticated Key Exchange by Juggling", 23 * 16th Workshop on Security Protocols, Cambridge, April 2008 24 * 25 * http://grouper.ieee.org/groups/1363/Research/contributions/hao-ryan-2008.pdf 26 */ 27 28#include "includes.h" 29 30#ifdef JPAKE 31 32#include <sys/types.h> 33 34#include <stdio.h> 35#include <string.h> 36#include <stdarg.h> 37 38#ifdef __APPLE_CRYPTO__ 39#include "ossl-bn.h" 40#include "ossl-evp.h" 41#else 42#include <openssl/bn.h> 43#include <openssl/evp.h> 44#endif 45 46#include "xmalloc.h" 47#include "ssh2.h" 48#include "key.h" 49#include "hostfile.h" 50#include "auth.h" 51#include "buffer.h" 52#include "packet.h" 53#include "dispatch.h" 54#include "log.h" 55#include "misc.h" 56 57#include "jpake.h" 58#include "schnorr.h" 59 60 61/* RFC3526 group 5, 1536 bits */ 62#define JPAKE_GROUP_G "2" 63#define JPAKE_GROUP_P \ 64 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74" \ 65 "020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437" \ 66 "4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ 67 "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF05" \ 68 "98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB" \ 69 "9ED529077096966D670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF" 70 71struct modp_group * 72jpake_default_group(void) 73{ 74 return modp_group_from_g_and_safe_p(JPAKE_GROUP_G, JPAKE_GROUP_P); 75} 76 77struct jpake_ctx * 78jpake_new(void) 79{ 80 struct jpake_ctx *ret; 81 82 ret = xcalloc(1, sizeof(*ret)); 83 84 ret->grp = jpake_default_group(); 85 86 ret->s = ret->k = NULL; 87 ret->x1 = ret->x2 = ret->x3 = ret->x4 = NULL; 88 ret->g_x1 = ret->g_x2 = ret->g_x3 = ret->g_x4 = NULL; 89 ret->a = ret->b = NULL; 90 91 ret->client_id = ret->server_id = NULL; 92 ret->h_k_cid_sessid = ret->h_k_sid_sessid = NULL; 93 94 debug3("%s: alloc %p", __func__, ret); 95 96 return ret; 97} 98 99void 100jpake_free(struct jpake_ctx *pctx) 101{ 102 debug3("%s: free %p", __func__, pctx); 103 104#define JPAKE_BN_CLEAR_FREE(v) \ 105 do { \ 106 if ((v) != NULL) { \ 107 BN_clear_free(v); \ 108 (v) = NULL; \ 109 } \ 110 } while (0) 111#define JPAKE_BUF_CLEAR_FREE(v, l) \ 112 do { \ 113 if ((v) != NULL) { \ 114 bzero((v), (l)); \ 115 xfree(v); \ 116 (v) = NULL; \ 117 (l) = 0; \ 118 } \ 119 } while (0) 120 121 JPAKE_BN_CLEAR_FREE(pctx->s); 122 JPAKE_BN_CLEAR_FREE(pctx->k); 123 JPAKE_BN_CLEAR_FREE(pctx->x1); 124 JPAKE_BN_CLEAR_FREE(pctx->x2); 125 JPAKE_BN_CLEAR_FREE(pctx->x3); 126 JPAKE_BN_CLEAR_FREE(pctx->x4); 127 JPAKE_BN_CLEAR_FREE(pctx->g_x1); 128 JPAKE_BN_CLEAR_FREE(pctx->g_x2); 129 JPAKE_BN_CLEAR_FREE(pctx->g_x3); 130 JPAKE_BN_CLEAR_FREE(pctx->g_x4); 131 JPAKE_BN_CLEAR_FREE(pctx->a); 132 JPAKE_BN_CLEAR_FREE(pctx->b); 133 134 JPAKE_BUF_CLEAR_FREE(pctx->client_id, pctx->client_id_len); 135 JPAKE_BUF_CLEAR_FREE(pctx->server_id, pctx->server_id_len); 136 JPAKE_BUF_CLEAR_FREE(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len); 137 JPAKE_BUF_CLEAR_FREE(pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len); 138 139#undef JPAKE_BN_CLEAR_FREE 140#undef JPAKE_BUF_CLEAR_FREE 141 142 bzero(pctx, sizeof(*pctx)); 143 xfree(pctx); 144} 145 146/* dump entire jpake_ctx. NB. includes private values! */ 147void 148jpake_dump(struct jpake_ctx *pctx, const char *fmt, ...) 149{ 150 char *out; 151 va_list args; 152 153 out = NULL; 154 va_start(args, fmt); 155 vasprintf(&out, fmt, args); 156 va_end(args); 157 if (out == NULL) 158 fatal("%s: vasprintf failed", __func__); 159 160 debug3("%s: %s (ctx at %p)", __func__, out, pctx); 161 if (pctx == NULL) { 162 free(out); 163 return; 164 } 165 166#define JPAKE_DUMP_BN(a) do { \ 167 if ((a) != NULL) \ 168 JPAKE_DEBUG_BN(((a), "%s = ", #a)); \ 169 } while (0) 170#define JPAKE_DUMP_BUF(a, b) do { \ 171 if ((a) != NULL) \ 172 JPAKE_DEBUG_BUF((a, b, "%s", #a)); \ 173 } while (0) 174 175 JPAKE_DUMP_BN(pctx->s); 176 JPAKE_DUMP_BN(pctx->k); 177 JPAKE_DUMP_BN(pctx->x1); 178 JPAKE_DUMP_BN(pctx->x2); 179 JPAKE_DUMP_BN(pctx->x3); 180 JPAKE_DUMP_BN(pctx->x4); 181 JPAKE_DUMP_BN(pctx->g_x1); 182 JPAKE_DUMP_BN(pctx->g_x2); 183 JPAKE_DUMP_BN(pctx->g_x3); 184 JPAKE_DUMP_BN(pctx->g_x4); 185 JPAKE_DUMP_BN(pctx->a); 186 JPAKE_DUMP_BN(pctx->b); 187 188 JPAKE_DUMP_BUF(pctx->client_id, pctx->client_id_len); 189 JPAKE_DUMP_BUF(pctx->server_id, pctx->server_id_len); 190 JPAKE_DUMP_BUF(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len); 191 JPAKE_DUMP_BUF(pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len); 192 193 debug3("%s: %s done", __func__, out); 194 free(out); 195} 196 197/* Shared parts of step 1 exchange calculation */ 198void 199jpake_step1(struct modp_group *grp, 200 u_char **id, u_int *id_len, 201 BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2, 202 u_char **priv1_proof, u_int *priv1_proof_len, 203 u_char **priv2_proof, u_int *priv2_proof_len) 204{ 205 BN_CTX *bn_ctx; 206 207 if ((bn_ctx = BN_CTX_new()) == NULL) 208 fatal("%s: BN_CTX_new", __func__); 209 210 /* Random nonce to prevent replay */ 211 *id = xmalloc(KZP_ID_LEN); 212 *id_len = KZP_ID_LEN; 213 arc4random_buf(*id, *id_len); 214 215 /* 216 * x1/x3 is a random element of Zq 217 * x2/x4 is a random element of Z*q 218 * We also exclude [1] from x1/x3 candidates and [0, 1] from 219 * x2/x4 candiates to avoid possible degeneracy (i.e. g^0, g^1). 220 */ 221 if ((*priv1 = bn_rand_range_gt_one(grp->q)) == NULL || 222 (*priv2 = bn_rand_range_gt_one(grp->q)) == NULL) 223 fatal("%s: bn_rand_range_gt_one", __func__); 224 225 /* 226 * client: g_x1 = g^x1 mod p / server: g_x3 = g^x3 mod p 227 * client: g_x2 = g^x2 mod p / server: g_x4 = g^x4 mod p 228 */ 229 if ((*g_priv1 = BN_new()) == NULL || 230 (*g_priv2 = BN_new()) == NULL) 231 fatal("%s: BN_new", __func__); 232 if (BN_mod_exp(*g_priv1, grp->g, *priv1, grp->p, bn_ctx) == -1) 233 fatal("%s: BN_mod_exp", __func__); 234 if (BN_mod_exp(*g_priv2, grp->g, *priv2, grp->p, bn_ctx) == -1) 235 fatal("%s: BN_mod_exp", __func__); 236 237 /* Generate proofs for holding x1/x3 and x2/x4 */ 238 if (schnorr_sign_buf(grp->p, grp->q, grp->g, 239 *priv1, *g_priv1, *id, *id_len, 240 priv1_proof, priv1_proof_len) != 0) 241 fatal("%s: schnorr_sign", __func__); 242 if (schnorr_sign_buf(grp->p, grp->q, grp->g, 243 *priv2, *g_priv2, *id, *id_len, 244 priv2_proof, priv2_proof_len) != 0) 245 fatal("%s: schnorr_sign", __func__); 246 247 BN_CTX_free(bn_ctx); 248} 249 250/* Shared parts of step 2 exchange calculation */ 251void 252jpake_step2(struct modp_group *grp, BIGNUM *s, 253 BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2, 254 const u_char *theirid, u_int theirid_len, 255 const u_char *myid, u_int myid_len, 256 const u_char *theirpub1_proof, u_int theirpub1_proof_len, 257 const u_char *theirpub2_proof, u_int theirpub2_proof_len, 258 BIGNUM **newpub, 259 u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len) 260{ 261 BN_CTX *bn_ctx; 262 BIGNUM *tmp, *exponent; 263 264 /* Validate peer's step 1 values */ 265 if (BN_cmp(theirpub1, BN_value_one()) <= 0) 266 fatal("%s: theirpub1 <= 1", __func__); 267 if (BN_cmp(theirpub1, grp->p) >= 0) 268 fatal("%s: theirpub1 >= p", __func__); 269 if (BN_cmp(theirpub2, BN_value_one()) <= 0) 270 fatal("%s: theirpub2 <= 1", __func__); 271 if (BN_cmp(theirpub2, grp->p) >= 0) 272 fatal("%s: theirpub2 >= p", __func__); 273 274 if (schnorr_verify_buf(grp->p, grp->q, grp->g, theirpub1, 275 theirid, theirid_len, theirpub1_proof, theirpub1_proof_len) != 1) 276 fatal("%s: schnorr_verify theirpub1 failed", __func__); 277 if (schnorr_verify_buf(grp->p, grp->q, grp->g, theirpub2, 278 theirid, theirid_len, theirpub2_proof, theirpub2_proof_len) != 1) 279 fatal("%s: schnorr_verify theirpub2 failed", __func__); 280 281 if ((bn_ctx = BN_CTX_new()) == NULL) 282 fatal("%s: BN_CTX_new", __func__); 283 284 if ((*newpub = BN_new()) == NULL || 285 (tmp = BN_new()) == NULL || 286 (exponent = BN_new()) == NULL) 287 fatal("%s: BN_new", __func__); 288 289 /* 290 * client: exponent = x2 * s mod p 291 * server: exponent = x4 * s mod p 292 */ 293 if (BN_mod_mul(exponent, mypriv2, s, grp->q, bn_ctx) != 1) 294 fatal("%s: BN_mod_mul (exponent = mypriv2 * s mod p)", 295 __func__); 296 297 /* 298 * client: tmp = g^(x1 + x3 + x4) mod p 299 * server: tmp = g^(x1 + x2 + x3) mod p 300 */ 301 if (BN_mod_mul(tmp, mypub1, theirpub1, grp->p, bn_ctx) != 1) 302 fatal("%s: BN_mod_mul (tmp = mypub1 * theirpub1 mod p)", 303 __func__); 304 if (BN_mod_mul(tmp, tmp, theirpub2, grp->p, bn_ctx) != 1) 305 fatal("%s: BN_mod_mul (tmp = tmp * theirpub2 mod p)", __func__); 306 307 /* 308 * client: a = tmp^exponent = g^((x1+x3+x4) * x2 * s) mod p 309 * server: b = tmp^exponent = g^((x1+x2+x3) * x4 * s) mod p 310 */ 311 if (BN_mod_exp(*newpub, tmp, exponent, grp->p, bn_ctx) != 1) 312 fatal("%s: BN_mod_mul (newpub = tmp^exponent mod p)", __func__); 313 314 JPAKE_DEBUG_BN((tmp, "%s: tmp = ", __func__)); 315 JPAKE_DEBUG_BN((exponent, "%s: exponent = ", __func__)); 316 317 /* Note the generator here is 'tmp', not g */ 318 if (schnorr_sign_buf(grp->p, grp->q, tmp, exponent, *newpub, 319 myid, myid_len, 320 newpub_exponent_proof, newpub_exponent_proof_len) != 0) 321 fatal("%s: schnorr_sign newpub", __func__); 322 323 BN_clear_free(tmp); /* XXX stash for later use? */ 324 BN_clear_free(exponent); /* XXX stash for later use? (yes, in conf) */ 325 326 BN_CTX_free(bn_ctx); 327} 328 329/* Confirmation hash calculation */ 330void 331jpake_confirm_hash(const BIGNUM *k, 332 const u_char *endpoint_id, u_int endpoint_id_len, 333 const u_char *sess_id, u_int sess_id_len, 334 u_char **confirm_hash, u_int *confirm_hash_len) 335{ 336 Buffer b; 337 338 /* 339 * Calculate confirmation proof: 340 * client: H(k || client_id || session_id) 341 * server: H(k || server_id || session_id) 342 */ 343 buffer_init(&b); 344 buffer_put_bignum2(&b, k); 345 buffer_put_string(&b, endpoint_id, endpoint_id_len); 346 buffer_put_string(&b, sess_id, sess_id_len); 347 if (hash_buffer(buffer_ptr(&b), buffer_len(&b), EVP_sha256(), 348 confirm_hash, confirm_hash_len) != 0) 349 fatal("%s: hash_buffer", __func__); 350 buffer_free(&b); 351} 352 353/* Shared parts of key derivation and confirmation calculation */ 354void 355jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val, 356 BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2, 357 BIGNUM *theirpub1, BIGNUM *theirpub2, 358 const u_char *my_id, u_int my_id_len, 359 const u_char *their_id, u_int their_id_len, 360 const u_char *sess_id, u_int sess_id_len, 361 const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len, 362 BIGNUM **k, 363 u_char **confirm_hash, u_int *confirm_hash_len) 364{ 365 BN_CTX *bn_ctx; 366 BIGNUM *tmp; 367 368 if ((bn_ctx = BN_CTX_new()) == NULL) 369 fatal("%s: BN_CTX_new", __func__); 370 if ((tmp = BN_new()) == NULL || 371 (*k = BN_new()) == NULL) 372 fatal("%s: BN_new", __func__); 373 374 /* Validate step 2 values */ 375 if (BN_cmp(step2_val, BN_value_one()) <= 0) 376 fatal("%s: step2_val <= 1", __func__); 377 if (BN_cmp(step2_val, grp->p) >= 0) 378 fatal("%s: step2_val >= p", __func__); 379 380 /* 381 * theirpriv2_s_proof is calculated with a different generator: 382 * tmp = g^(mypriv1+mypriv2+theirpub1) = g^mypub1*g^mypub2*g^theirpub1 383 * Calculate it here so we can check the signature. 384 */ 385 if (BN_mod_mul(tmp, mypub1, mypub2, grp->p, bn_ctx) != 1) 386 fatal("%s: BN_mod_mul (tmp = mypub1 * mypub2 mod p)", __func__); 387 if (BN_mod_mul(tmp, tmp, theirpub1, grp->p, bn_ctx) != 1) 388 fatal("%s: BN_mod_mul (tmp = tmp * theirpub1 mod p)", __func__); 389 390 JPAKE_DEBUG_BN((tmp, "%s: tmp = ", __func__)); 391 392 if (schnorr_verify_buf(grp->p, grp->q, tmp, step2_val, 393 their_id, their_id_len, 394 theirpriv2_s_proof, theirpriv2_s_proof_len) != 1) 395 fatal("%s: schnorr_verify theirpriv2_s_proof failed", __func__); 396 397 /* 398 * Derive shared key: 399 * client: k = (b / g^(x2*x4*s))^x2 = g^((x1+x3)*x2*x4*s) 400 * server: k = (a / g^(x2*x4*s))^x4 = g^((x1+x3)*x2*x4*s) 401 * 402 * Computed as: 403 * client: k = (g_x4^(q - (x2 * s)) * b)^x2 mod p 404 * server: k = (g_x2^(q - (x4 * s)) * b)^x4 mod p 405 */ 406 if (BN_mul(tmp, mypriv2, s, bn_ctx) != 1) 407 fatal("%s: BN_mul (tmp = mypriv2 * s)", __func__); 408 if (BN_mod_sub(tmp, grp->q, tmp, grp->q, bn_ctx) != 1) 409 fatal("%s: BN_mod_sub (tmp = q - tmp mod q)", __func__); 410 if (BN_mod_exp(tmp, theirpub2, tmp, grp->p, bn_ctx) != 1) 411 fatal("%s: BN_mod_exp (tmp = theirpub2^tmp) mod p", __func__); 412 if (BN_mod_mul(tmp, tmp, step2_val, grp->p, bn_ctx) != 1) 413 fatal("%s: BN_mod_mul (tmp = tmp * step2_val) mod p", __func__); 414 if (BN_mod_exp(*k, tmp, mypriv2, grp->p, bn_ctx) != 1) 415 fatal("%s: BN_mod_exp (k = tmp^mypriv2) mod p", __func__); 416 417 BN_CTX_free(bn_ctx); 418 BN_clear_free(tmp); 419 420 jpake_confirm_hash(*k, my_id, my_id_len, sess_id, sess_id_len, 421 confirm_hash, confirm_hash_len); 422} 423 424/* 425 * Calculate and check confirmation hash from peer. Returns 1 on success 426 * 0 on failure/mismatch. 427 */ 428int 429jpake_check_confirm(const BIGNUM *k, 430 const u_char *peer_id, u_int peer_id_len, 431 const u_char *sess_id, u_int sess_id_len, 432 const u_char *peer_confirm_hash, u_int peer_confirm_hash_len) 433{ 434 u_char *expected_confirm_hash; 435 u_int expected_confirm_hash_len; 436 int success = 0; 437 438 /* Calculate and verify expected confirmation hash */ 439 jpake_confirm_hash(k, peer_id, peer_id_len, sess_id, sess_id_len, 440 &expected_confirm_hash, &expected_confirm_hash_len); 441 442 JPAKE_DEBUG_BUF((expected_confirm_hash, expected_confirm_hash_len, 443 "%s: expected confirm hash", __func__)); 444 JPAKE_DEBUG_BUF((peer_confirm_hash, peer_confirm_hash_len, 445 "%s: received confirm hash", __func__)); 446 447 if (peer_confirm_hash_len != expected_confirm_hash_len) 448 error("%s: confirmation length mismatch (my %u them %u)", 449 __func__, expected_confirm_hash_len, peer_confirm_hash_len); 450 else if (timingsafe_bcmp(peer_confirm_hash, expected_confirm_hash, 451 expected_confirm_hash_len) == 0) 452 success = 1; 453 bzero(expected_confirm_hash, expected_confirm_hash_len); 454 xfree(expected_confirm_hash); 455 debug3("%s: success = %d", __func__, success); 456 return success; 457} 458 459/* XXX main() function with tests */ 460 461#endif /* JPAKE */ 462 463