| 895 | |
| 896 | #ifdef USE_SHA256 |
| 897 | int createAuthHeaderSHA256( |
| 898 | const char* user, const char* password, int password_len, |
| 899 | const char* method, const char* uri, const char* msgbody, |
| 900 | const char* auth, const char* algo, unsigned int nonce_count, |
| 901 | char* result, size_t result_len) |
| 902 | { |
| 903 | |
| 904 | unsigned char resp_hex[SHA256_HASH_HEX_SIZE+1]; |
| 905 | char realm[MAX_HEADER_LEN], |
| 906 | sipuri[MAX_HEADER_LEN], |
| 907 | nonce[MAX_HEADER_LEN], |
| 908 | authtype[16], |
| 909 | cnonce[32], |
| 910 | nc[32], |
| 911 | opaque[64]; |
| 912 | int has_opaque = 0; |
| 913 | int written = 0; |
| 914 | |
| 915 | // Extract the Auth Type - If not present, using 'none' |
| 916 | cnonce[0] = '\0'; |
| 917 | if (getAuthParameter("qop", auth, authtype, sizeof(authtype))) { |
| 918 | // Sloppy auth type recognition (may be "auth,auth-int") |
| 919 | if (stristr(authtype, "auth-int")) { |
| 920 | strncpy(authtype, "auth-int", sizeof(authtype) - 1); |
| 921 | } else if (stristr(authtype, "auth")) { |
| 922 | strncpy(authtype, "auth", sizeof(authtype) - 1); |
| 923 | } |
| 924 | sprintf(cnonce, "%x", rand()); |
| 925 | sprintf(nc, "%08x", nonce_count); |
| 926 | } |
| 927 | |
| 928 | // Extract the Opaque value - if present |
| 929 | if (getAuthParameter("opaque", auth, opaque, sizeof(opaque))) { |
| 930 | has_opaque = 1; |
| 931 | } |
| 932 | |
| 933 | // Extract the Realm |
| 934 | if (!getAuthParameter("realm", auth, realm, sizeof(realm))) { |
| 935 | snprintf(result, result_len, "createAuthHeaderSHA256: couldn't parse realm in '%s'", auth); |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | written += snprintf( |
| 940 | result + written, result_len - written, |
| 941 | "Digest username=\"%s\",realm=\"%s\"", user, realm); |
| 942 | |
| 943 | // Construct the URI |
| 944 | if (auth_uri == nullptr) { |
| 945 | snprintf(sipuri, sizeof(sipuri), "sip:%s", uri); |
| 946 | } else { |
| 947 | snprintf(sipuri, sizeof(sipuri), "sip:%s", auth_uri); |
| 948 | } |
| 949 | |
| 950 | if (cnonce[0] != '\0') { |
| 951 | // No double quotes around nc and qop (RFC3261): |
| 952 | // |
| 953 | // dig-resp = username / realm / nonce / digest-uri / dresponse |
| 954 | // / algorithm / cnonce / opaque / message-qop |
no test coverage detected