| 334 | |
| 335 | #ifdef USE_SHA256 |
| 336 | static int createAuthResponseSHA256( |
| 337 | const char* user, const char* password, int password_len, |
| 338 | const char* method, const char* uri, const char* authtype, |
| 339 | const char* msgbody, const char* realm, const char* nonce, |
| 340 | const char* cnonce, const char* nc, |
| 341 | unsigned char* result) |
| 342 | { |
| 343 | unsigned char ha1[SHA256_HASH_SIZE], ha2[SHA256_HASH_SIZE]; |
| 344 | unsigned char resp[SHA256_HASH_SIZE], body[SHA256_HASH_SIZE]; |
| 345 | unsigned char body_hex[SHA256_HASH_HEX_SIZE+1]; |
| 346 | unsigned char ha1_hex[SHA256_HASH_HEX_SIZE+1], ha2_hex[SHA256_HASH_HEX_SIZE+1]; |
| 347 | char tmp[MAX_HEADER_LEN]; |
| 348 | unsigned int digest_len = 0; |
| 349 | EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); |
| 350 | |
| 351 | // Load in A1 |
| 352 | // ha1 = SHA256(username ":" realm ":" password) |
| 353 | EVP_DigestInit_ex(mdctx, EVP_sha256(), nullptr); |
| 354 | EVP_DigestUpdate(mdctx, (unsigned char *) user, strlen(user)); |
| 355 | EVP_DigestUpdate(mdctx, ":", 1); |
| 356 | EVP_DigestUpdate(mdctx, (unsigned char *) realm, strlen(realm)); |
| 357 | EVP_DigestUpdate(mdctx, ":", 1); |
| 358 | EVP_DigestUpdate(mdctx, (unsigned char *) password, password_len); |
| 359 | EVP_DigestFinal_ex(mdctx, ha1, &digest_len); |
| 360 | hashToHex(&ha1[0], &ha1_hex[0], SHA256_HASH_SIZE); |
| 361 | |
| 362 | if (auth_uri) { |
| 363 | snprintf(tmp, sizeof(tmp), "sip:%s", auth_uri); |
| 364 | } else { |
| 365 | strncpy(tmp, uri, sizeof(tmp) - 1); |
| 366 | } |
| 367 | // If using Auth-Int make a hash of the body - which is NULL for REG |
| 368 | if (stristr(authtype, "auth-int") != nullptr) { |
| 369 | EVP_DigestInit_ex(mdctx, EVP_sha256(), nullptr); |
| 370 | EVP_DigestUpdate(mdctx, (unsigned char *) msgbody, strlen(msgbody)); |
| 371 | EVP_DigestFinal_ex(mdctx, body, &digest_len); |
| 372 | hashToHex(&body[0], &body_hex[0], SHA256_HASH_SIZE); |
| 373 | } |
| 374 | |
| 375 | // Load in A2 |
| 376 | EVP_DigestInit_ex(mdctx, EVP_sha256(), nullptr); |
| 377 | EVP_DigestUpdate(mdctx, (unsigned char *) method, strlen(method)); |
| 378 | EVP_DigestUpdate(mdctx, (unsigned char *) ":", 1); |
| 379 | EVP_DigestUpdate(mdctx, (unsigned char *) tmp, strlen(tmp)); |
| 380 | if (stristr(authtype, "auth-int") != nullptr) { |
| 381 | EVP_DigestUpdate(mdctx, (unsigned char *) ":", 1); |
| 382 | EVP_DigestUpdate(mdctx, (unsigned char *) &body_hex, SHA256_HASH_HEX_SIZE); |
| 383 | } |
| 384 | EVP_DigestFinal_ex(mdctx, ha2, &digest_len); |
| 385 | hashToHex(&ha2[0], &ha2_hex[0], SHA256_HASH_SIZE); |
| 386 | |
| 387 | EVP_DigestInit_ex(mdctx, EVP_sha256(), nullptr); |
| 388 | EVP_DigestUpdate(mdctx, (unsigned char *) &ha1_hex, SHA256_HASH_HEX_SIZE); |
| 389 | EVP_DigestUpdate(mdctx, (unsigned char *) ":", 1); |
| 390 | EVP_DigestUpdate(mdctx, (unsigned char *) nonce, strlen(nonce)); |
| 391 | if (cnonce[0] != '\0') { |
| 392 | EVP_DigestUpdate(mdctx, (unsigned char *) ":", 1); |
| 393 | EVP_DigestUpdate(mdctx, (unsigned char *) nc, strlen(nc)); |
no test coverage detected