From patchwork Thu Apr 11 07:48:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?7KCV7KeA7JqwIHwgRXVnZW5l?= X-Patchwork-Id: 48008 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c90a:b0:1a7:a0dc:8de5 with SMTP id gx10csp135049pzb; Thu, 11 Apr 2024 00:48:32 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXMPrXq8ePX8VUz1LVbcfpZ77X6houfMO3d/FAj3ricWGP0XVSAgyLr3WCcU02QBPZ8az2LZmWYgOkWGpqbM/2jTp5xv3FuSoEcnA== X-Google-Smtp-Source: AGHT+IHkFMjn59s0BfR1NIduq6FPan2KqfjXmOCT5yGsSj6TSrkVxUHLjRta/EQRAzhjbCKWnafZ X-Received: by 2002:a50:cccc:0:b0:56e:30cc:ebad with SMTP id b12-20020a50cccc000000b0056e30ccebadmr3172518edj.32.1712821712334; Thu, 11 Apr 2024 00:48:32 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id p25-20020a50cd99000000b0056e688f3bc0si494005edi.533.2024.04.11.00.48.31; Thu, 11 Apr 2024 00:48:32 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@bitsensing.onmicrosoft.com header.s=selector1-bitsensing-onmicrosoft-com header.b=EyJJt9km; arc=fail (body hash mismatch); spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B097268D198; Thu, 11 Apr 2024 10:48:28 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from SEVP216CU002.outbound.protection.outlook.com (mail-koreacentralazon11022019.outbound.protection.outlook.com [52.101.154.19]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 77F0968B779 for ; Thu, 11 Apr 2024 10:48:21 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ecKFUNnbTFfSXGBi/cKuuMAbj60HBAVZZATg1z3dRq+GTA+OFRhxazOy1M3v0aI75TdQGOKowteWZ3/1Rjmcw/zqN+UzwvQdZ5zBHBw48xsPJaDym6lCVSNJJzdh9fH+GtX/ZXhHu1q8Q91hUB2t4T7bygqFuQtKop08ujMxF8GkbBPT1myWAKpaN4OvpKKNos/y8RPqINdZo/sbXQRCqVwsqwefvBHWQP0+zLitM/7/Uu1qY62LUY88WBw7rszalaKz3s9w89u1dKLxIQa7DGMdQDpGPq41Zy+Xp57ZMrOE8aQXN24H4GJu7MtKjmD8/FcNYEz3BpYSj6YE+55rFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=MF6fklz/iSvnKnPRuvffaENTvd3GmyRkUwTL+h1ka1U=; b=eio1Gw0MLEtcOOWCoAZpMePiiAHSf4itv49+0tQHaQwskqNYJ5JsJhxGjCoI7gihSvY1BFRw6d2HdFIg8THPihbZ/mnj7UoOWBnrG79rOkJph9xm4ccjW57GpBDh8OX369tuTIqL0skkbtWPfccbgrqpDFtInSD9pI2F8tZd7DB5uLmj77tejvOpaOfqV57yqdloZjU2D/xU/VcNf1WPAFhxyeyETjuq64nkgr+kMnHrjeWl055e+692yFfMEmfLqaBPFiCcGEcbkygZcQ1FVDeh3j5tuVW44WPKxwVTrubj8DP0u46BoobSNVMbrTIwdgjKIQfH26oYAyc2M8UK3A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=bitsensing.com; dmarc=pass action=none header.from=bitsensing.com; dkim=pass header.d=bitsensing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bitsensing.onmicrosoft.com; s=selector1-bitsensing-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=MF6fklz/iSvnKnPRuvffaENTvd3GmyRkUwTL+h1ka1U=; b=EyJJt9kmxLM9iZvfYWiRau8q3cozQY5C4bmf4n1lZMp3lff8In2qMtC6/a37uS7XXR1bdiGYCvq6EL/OBmGpesPCpcCTr5xB2tGVxxMJ0vZBnkeGgAwUazmRZRSAj7P/xIqV+gv1YmV4X9g/ivVD4OEIi2gh+chmI1hx9Ur7Ptw= Received: from SL2P216MB1481.KORP216.PROD.OUTLOOK.COM (2603:1096:101:1f::7) by SE2P216MB1442.KORP216.PROD.OUTLOOK.COM (2603:1096:101:2c::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.55; Thu, 11 Apr 2024 07:48:16 +0000 Received: from SL2P216MB1481.KORP216.PROD.OUTLOOK.COM ([fe80::98b3:5aed:a402:9e22]) by SL2P216MB1481.KORP216.PROD.OUTLOOK.COM ([fe80::98b3:5aed:a402:9e22%4]) with mapi id 15.20.7409.053; Thu, 11 Apr 2024 07:48:14 +0000 From: =?ks_c_5601-1987?b?waTB9r/sIHwgRXVnZW5l?= To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH] avformat/httpauth.c [support both RFC 2617 and RFC 7617] Thread-Index: AdqL5JSWsafkZARdTsaPUb6dbGKGwQ== Date: Thu, 11 Apr 2024 07:48:14 +0000 Message-ID: Accept-Language: ko-KR, en-US Content-Language: ko-KR X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=bitsensing.com; x-ms-exchange-messagesentrepresentingtype: 1 x-ms-publictraffictype: Email x-ms-traffictypediagnostic: SL2P216MB1481:EE_|SE2P216MB1442:EE_ x-ms-office365-filtering-correlation-id: 67db1f6b-1ab2-4a3e-0b29-08dc59fbbe48 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 0u+OWZPHa2MFnhL3s7LxAcbpjWk8UUprOK+l1LNdCzeAaDX525b7sdOAnvI2fGfvfIKzdCvhlDIeAcqZVpJY7VMeSMad1+AGQ1ggHRftVirCmgsfpCwCmgkIJYIrGQDq4FbuT/xVTifhaLxqvnB/yqDs7wh3R1L8zCvsGg+NmKJwMCLHFIHD/h5VKRjDdD/qxyFbrQV7D4WC9sg+ditYtccWrknV+OSXtc0lGbtYcnBDnxOQmBIJ1yNdEh1HteZox4QlSBBiSbWXCnVYme4gDeKOFoLO43KI2d5Ly7BetLlpVUtjXf7ZZEYVjezylQd0TJGdOMGRdMSBns3iqRQQEMVwZXhfXAMI4aVg425EADthzWvV63eHVVZmlUKQX7+vxGddIUa2TL09WDksFZ72BmxDLqeHTaadbXTzP68ZgcP7i8hVb7LB9pRisJ3DdwWyFvmIAPxKP7wPf+ogTBWnFvIOK+k891clCT5NrUUoPf6AWHDxzR77r2VcoQx2TWAJEPjoxVHuclX5OuiieeEWDz+soCzrlSUXl0plA2GJ3QuH1MXs+p5syVlS0INhgT8pf85yvTC+OYmFAsR9LCcajX8kvyUd4ZFu4+5z53Q2P2lasIC1+UN3kkEeKHcTYPFjgLcAXFx3dvnKsRyKJYwbliUCJg/7/iNU70UjuGIUfCgIphRI20sg6iqSRWu3bTGiKQhp19XzRac1+AOolb6ct//SN1lcud4IfisaLOD5lvI= x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:ko; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SL2P216MB1481.KORP216.PROD.OUTLOOK.COM; PTR:; CAT:NONE; SFS:(13230031)(366007)(1800799015)(376005)(38070700009); DIR:OUT; SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?ks_c_5601-1987?q?xD4Jvx91uUbRLbBIF/z?= =?ks_c_5601-1987?q?/Tm5ltm3aJOy7ig2TtidNaEDMAwvFohId1FPGZaIbTG+wV168LUGFts+?= =?ks_c_5601-1987?q?yEPvCsxjW4KmepgWhOaQCTMQUZCFsMpA3wGHETuzOEKP9rACHIN5aaQm?= =?ks_c_5601-1987?q?n5A7x5Aza1CHD+vPqHnaYRuq6S+D6iW4BxY5D5mTQqz+hM0CF231+dCU?= =?ks_c_5601-1987?q?Qjz0pXbt7wmKRfLyoYlF7SBeacrSpTz8vzGYS4z47AOXwu0Xw2WG5i8y?= =?ks_c_5601-1987?q?nwnRdUzVm2lctNhqWyuqvEQvmTxeQmBjscyG0ux/y1kjlL50m1li+5rE?= =?ks_c_5601-1987?q?Vz89YPxXs8Ih6EZqdaVXkQkl+2T6NH97aiYcg1dWgQjBKaSo2xF7bGLT?= =?ks_c_5601-1987?q?OuLTia4AapPoO64e5Ey6vA4xSo6t31evzEoRFplRnL36oUp/tELG4hdM?= =?ks_c_5601-1987?q?1AHyeKq10Bo0ht4hrRGLRK+KCK8Yhth9RF7c1oM8IEKLMCRKv29OKj4r?= =?ks_c_5601-1987?q?JqiNxBoSDNnqVLpI08ubi58WJrlUpSmRnXHQeuKBfjTIpLjd1KRTtLyD?= =?ks_c_5601-1987?q?7VG+ihBc52+LxC7ewqFry7I3BiBgKMZJU8HXFz0UjgUXgjb5YSwT66oV?= =?ks_c_5601-1987?q?lMXHE+jqXPw+nqguyVBJZ6o7WAHk+FCbx+Co3VVU98uzeLOKiCKUnyew?= =?ks_c_5601-1987?q?bWnq+ec8LAwyCLcWSBk6qAxcFC5PrqyemkpTBEIxntp5UiOO1wU/kjFH?= =?ks_c_5601-1987?q?giSdo5pn5+4WWNKSXQ+Ky8iz8nZVc6vle87GzPthpWtfjLU6KqveQr8H?= =?ks_c_5601-1987?q?Zj5to2kDChdXFg9XEl8FQ7iuz6wAWucWuJipZOX6aWV8K5QwU3Pq7DbE?= =?ks_c_5601-1987?q?5dtpWWxvNvnhb0hzejHylDPJw5MvrOSjYVJ9Vu/3rP3HtVDmvk/g/dEZ?= =?ks_c_5601-1987?q?ForyBy2nyqAofPPdp+ls72DKphREUDPxzK09185lxDKZitidFFErn/Q/?= =?ks_c_5601-1987?q?rUwmSEh53xiwjVtBPplsnNuUcfEsO9qrSrzpiXBtn2+7gVXfzY/Py82E?= =?ks_c_5601-1987?q?C1O/CknStEMl1XJ06s3cIT/nW4mOdPrm7y78OSLzfP9rWO6MFZRik2Po?= =?ks_c_5601-1987?q?iJV7+14BfoR6SGErcg2ORC4sJLqr5gglbptoyQGUlMWzXfsBaUxGwLlR?= =?ks_c_5601-1987?q?zzjdMB4zc7ruNiK16p6fDa1JT82C5UIAQGTuu4Iw4UC9APHTvw9Wx9dG?= =?ks_c_5601-1987?q?abbcO6KerPZ9hCcGS0CNTpWU0/+yxTbpC9nZjT5MaNnZsTOR0pje39f5?= =?ks_c_5601-1987?q?Uf1tXAHMCOxFMQp66JkOpyTvkbXswFLKtJH6klV4m0xio482dwDDZgWB?= =?ks_c_5601-1987?q?Kq4XdGrmjCYMBb2x1g0frsWfUtQDxRbHjVSZQDflxQOrdGrRTlG8LBQ5?= =?ks_c_5601-1987?q?4YMOPqYR50O4B4Tz2iKJ7/SZqDTFnXkU63YSmtE/DxeybGnIQb8Cl/O4?= =?ks_c_5601-1987?q?ogfUAomW0atc9fBVsYCS+dWXCfnI5wsuKHUBaLJnb5CFZKhrFnZR8XR3?= =?ks_c_5601-1987?q?e4haWBTJj5K9UmdK6beYojLSils3LUJUARLxjltx66NriTDSUhyv5PYB?= =?ks_c_5601-1987?q?CMBaa1nQkj37pW8ZH79JlTtOcm4c52U8TYvjBoLHxdNmzhHe8gkYvp+u?= =?ks_c_5601-1987?q?IWgLRb8pryWNDmcHgJX9K2OOeAnAF1m0Mz6F4mCia8y8jIQv+kgS/7qp?= =?ks_c_5601-1987?q?oZCPuuLum3OTn?= MIME-Version: 1.0 X-OriginatorOrg: bitsensing.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SL2P216MB1481.KORP216.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-Network-Message-Id: 67db1f6b-1ab2-4a3e-0b29-08dc59fbbe48 X-MS-Exchange-CrossTenant-originalarrivaltime: 11 Apr 2024 07:48:14.3369 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 5039430d-7fb7-4b01-9192-aeefd0db9500 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: QXF4dBir0iq2HsKfcCjtC1NcUeWcmzaz2Eh7ydyrxe26+ZDXIH+vz8EEeLPJm/9rrOY7IUuMd3os75vXe3i2kg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SE2P216MB1442 Subject: [FFmpeg-devel] [PATCH] avformat/httpauth.c [support both RFC 2617 and RFC 7617] X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: dmlEgNgh4Wg4 - Updated the make_digest_auth() function to support both RFC 2617 and RFC 7617 digest authentication. - Supports sha256 , sha512-256 along with the existing md5. MD5 and sha256 were tested, but sha512-256 was not tested due to lack of testable server. - AVHashContext instead of AVMD5 structure. - update_md5_strings() -> update_hash_strings(). - There are some lynt issues in the old code of make_digest_auth, but this is a feature update patch, so I didn't fix it. - modified the implementation of RFC7616 based on community feedback. Signed-off-by: Eugene-bitsensing --- libavformat/httpauth.c | 105 ++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 48 deletions(-) diff --git a/libavformat/httpauth.c b/libavformat/httpauth.c index 9780928357..ba2ebea3a4 100644 --- a/libavformat/httpauth.c +++ b/libavformat/httpauth.c @@ -24,7 +24,7 @@ #include "libavutil/avstring.h" #include "internal.h" #include "libavutil/random_seed.h" -#include "libavutil/md5.h" +#include "libavutil/hash.h" #include "urldecode.h" static void handle_basic_params(HTTPAuthState *state, const char *key, @@ -117,35 +117,35 @@ void ff_http_auth_handle_header(HTTPAuthState *state, const char *key, } } - -static void update_md5_strings(struct AVMD5 *md5ctx, ...) +/* Generate hash string, updated to use AVHashContext to support other algorithms */ +static void update_hash_strings(struct AVHashContext* hash_ctx, ...) { va_list vl; - va_start(vl, md5ctx); + va_start(vl, hash_ctx); while (1) { const char* str = va_arg(vl, const char*); if (!str) break; - av_md5_update(md5ctx, str, strlen(str)); + av_hash_update(hash_ctx, (const uint8_t*)str, strlen(str)); } va_end(vl); } -/* Generate a digest reply, according to RFC 2617. */ +/* Generate a digest reply, according to RFC 2617. Update to support RFC 7617*/ static char *make_digest_auth(HTTPAuthState *state, const char *username, const char *password, const char *uri, const char *method) { DigestParams *digest = &state->digest_params; - int len; + size_t len; // change int -> size_t uint32_t cnonce_buf[2]; char cnonce[17]; char nc[9]; int i; - char A1hash[33], A2hash[33], response[33]; - struct AVMD5 *md5ctx; - uint8_t hash[16]; + char a1_hash[65], a2_hash[65], response[65]; // increase hash size for SHA-2 + struct AVHashContext* hash_ctx = NULL; // use AVHashContext for other algorithm support + size_t len_hash = 33; // Modifiable hash length, MD5:32, SHA-2:64 char *authstr; digest->nc++; @@ -156,52 +156,61 @@ static char *make_digest_auth(HTTPAuthState *state, const char *username, cnonce_buf[i] = av_get_random_seed(); ff_data_to_hex(cnonce, (const uint8_t*) cnonce_buf, sizeof(cnonce_buf), 1); - md5ctx = av_md5_alloc(); - if (!md5ctx) - return NULL; - - av_md5_init(md5ctx); - update_md5_strings(md5ctx, username, ":", state->realm, ":", password, NULL); - av_md5_final(md5ctx, hash); - ff_data_to_hex(A1hash, hash, 16, 1); - - if (!strcmp(digest->algorithm, "") || !strcmp(digest->algorithm, "MD5")) { - } else if (!strcmp(digest->algorithm, "MD5-sess")) { - av_md5_init(md5ctx); - update_md5_strings(md5ctx, A1hash, ":", digest->nonce, ":", cnonce, NULL); - av_md5_final(md5ctx, hash); - ff_data_to_hex(A1hash, hash, 16, 1); - } else { - /* Unsupported algorithm */ - av_free(md5ctx); + /* Generate hash context by algorithm. */ + const char* algorithm = digest->algorithm; + const char* hashing_algorithm; + if (!*algorithm) { + algorithm = "MD5"; // if empty, use MD5 as Default + hashing_algorithm = "MD5"; + } + else if (av_stristr(algorithm, "md5") || av_stristr(algorithm, "MD5")) { + hashing_algorithm = "MD5"; + } + else if (av_stristr(algorithm, "sha256") || av_stristr(algorithm, "sha-256")) { + hashing_algorithm = "SHA256"; + len_hash = 65; // SHA-2: 64 characters. + } + else if (av_stristr(algorithm, "sha512-256") || av_stristr(algorithm, "sha-512-256")) { + hashing_algorithm = "SHA512_256"; + len_hash = 65; // SHA-2: 64 characters. + } + else { // Unsupported algorithm return NULL; } - av_md5_init(md5ctx); - update_md5_strings(md5ctx, method, ":", uri, NULL); - av_md5_final(md5ctx, hash); - ff_data_to_hex(A2hash, hash, 16, 1); + int ret = av_hash_alloc(&hash_ctx, hashing_algorithm); + if (ret < 0) + return NULL; - av_md5_init(md5ctx); - update_md5_strings(md5ctx, A1hash, ":", digest->nonce, NULL); - if (!strcmp(digest->qop, "auth") || !strcmp(digest->qop, "auth-int")) { - update_md5_strings(md5ctx, ":", nc, ":", cnonce, ":", digest->qop, NULL); + /* a1 hash calculation */ + av_hash_init(hash_ctx); + update_hash_strings(hash_ctx, username, ":", state->realm, ":", password, NULL); + if (av_stristr(algorithm, "-sess")) { + update_hash_strings(hash_ctx, ":", digest->nonce, ":", cnonce, NULL); } - update_md5_strings(md5ctx, ":", A2hash, NULL); - av_md5_final(md5ctx, hash); - ff_data_to_hex(response, hash, 16, 1); - - av_free(md5ctx); - - if (!strcmp(digest->qop, "") || !strcmp(digest->qop, "auth")) { - } else if (!strcmp(digest->qop, "auth-int")) { - /* qop=auth-int not supported */ - return NULL; - } else { - /* Unsupported qop value. */ + av_hash_final_hex(hash_ctx, a1_hash, len_hash); + + /* a2 hash calculation */ + av_hash_init(hash_ctx); + update_hash_strings(hash_ctx, method, ":", uri, NULL); + av_hash_final_hex(hash_ctx, a2_hash, len_hash); + + /* response hash calculation */ + av_hash_init(hash_ctx); + update_hash_strings(hash_ctx, a1_hash, ":", digest->nonce, NULL); + if (!strcmp(digest->qop, "auth")) { + update_hash_strings(hash_ctx, ":", nc, ":", cnonce, ":", digest->qop, NULL); + } + else if (!strcmp(digest->qop, "auth-int")) { // unsupported + av_hash_freep(&hash_ctx); return NULL; } + update_hash_strings(hash_ctx,":", a2_hash, NULL); + update_hash_strings(hash_ctx, NULL); + av_hash_final_hex(hash_ctx, response, len_hash); + av_hash_freep(&hash_ctx); + /* Authorization header generation */ len = strlen(username) + strlen(state->realm) + strlen(digest->nonce) + strlen(uri) + strlen(response) + strlen(digest->algorithm) + strlen(digest->opaque) + strlen(digest->qop) + strlen(cnonce) +