| 407 | #endif // USE_SHA256 |
| 408 | |
| 409 | int createAuthHeaderMD5( |
| 410 | const char* user, const char* password, int password_len, |
| 411 | const char* method, const char* uri, const char* msgbody, |
| 412 | const char* auth, const char* algo, unsigned int nonce_count, |
| 413 | char* result, size_t result_len) |
| 414 | { |
| 415 | |
| 416 | unsigned char resp_hex[HASH_HEX_SIZE+1]; |
| 417 | char realm[MAX_HEADER_LEN], |
| 418 | sipuri[MAX_HEADER_LEN], |
| 419 | nonce[MAX_HEADER_LEN], |
| 420 | authtype[16], |
| 421 | cnonce[32], |
| 422 | nc[32], |
| 423 | opaque[64]; |
| 424 | int has_opaque = 0; |
| 425 | int written = 0; |
| 426 | |
| 427 | // Extract the Auth Type - If not present, using 'none' |
| 428 | cnonce[0] = '\0'; |
| 429 | if (getAuthParameter("qop", auth, authtype, sizeof(authtype))) { |
| 430 | // Sloppy auth type recognition (may be "auth,auth-int") |
| 431 | if (stristr(authtype, "auth-int")) { |
| 432 | strncpy(authtype, "auth-int", sizeof(authtype) - 1); |
| 433 | } else if (stristr(authtype, "auth")) { |
| 434 | strncpy(authtype, "auth", sizeof(authtype) - 1); |
| 435 | } |
| 436 | sprintf(cnonce, "%x", rand()); |
| 437 | sprintf(nc, "%08x", nonce_count); |
| 438 | } |
| 439 | |
| 440 | // Extract the Opaque value - if present |
| 441 | if (getAuthParameter("opaque", auth, opaque, sizeof(opaque))) { |
| 442 | has_opaque = 1; |
| 443 | } |
| 444 | |
| 445 | // Extract the Realm |
| 446 | if (!getAuthParameter("realm", auth, realm, sizeof(realm))) { |
| 447 | snprintf(result, result_len, "createAuthHeaderMD5: couldn't parse realm in '%s'", auth); |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | written += snprintf( |
| 452 | result + written, result_len - written, |
| 453 | "Digest username=\"%s\",realm=\"%s\"", user, realm); |
| 454 | |
| 455 | // Construct the URI |
| 456 | if (auth_uri == nullptr) { |
| 457 | snprintf(sipuri, sizeof(sipuri), "sip:%s", uri); |
| 458 | } else { |
| 459 | snprintf(sipuri, sizeof(sipuri), "sip:%s", auth_uri); |
| 460 | } |
| 461 | |
| 462 | if (cnonce[0] != '\0') { |
| 463 | // No double quotes around nc and qop (RFC3261): |
| 464 | // |
| 465 | // dig-resp = username / realm / nonce / digest-uri / dresponse |
| 466 | // / algorithm / cnonce / opaque / message-qop |
no test coverage detected