CCF
Loading...
Searching...
No Matches
http_digest.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
6#include "ccf/ds/nonstd.h"
7
8#include <charconv>
9#include <string>
10#include <utility>
11
12namespace ccf::http
13{
14 // Helper to parse the Want-Repr-Digest request header (RFC 9530) and
15 // return the best supported algorithm name and MDType. Only sha-256,
16 // sha-384 and sha-512 are supported. Parsing is best-effort: malformed
17 // entries are ignored. If no supported algorithm can be matched,
18 // defaults to sha-256 (permitted by RFC 9530 Appendix C.2).
19 static std::pair<std::string, ccf::crypto::MDType> parse_want_repr_digest(
20 const std::string& want_repr_digest)
21 {
22 std::string best_algo;
24 int best_pref = 0;
25
26 for (const auto& entry : ccf::nonstd::split(want_repr_digest, ","))
27 {
28 auto [algo, pref_sv] =
29 ccf::nonstd::split_1(ccf::nonstd::trim(entry), "=");
30 auto algo_name = ccf::nonstd::trim(algo);
31
32 int pref = 0;
33 auto pref_trimmed = ccf::nonstd::trim(pref_sv);
34 if (!pref_trimmed.empty())
35 {
36 const auto [p, ec] = std::from_chars(
37 pref_trimmed.data(), pref_trimmed.data() + pref_trimmed.size(), pref);
38 if (ec != std::errc() || pref < 1)
39 {
40 continue;
41 }
42 }
43 else
44 {
45 pref = 1;
46 }
47
49 if (algo_name == "sha-256")
50 {
52 }
53 else if (algo_name == "sha-384")
54 {
56 }
57 else if (algo_name == "sha-512")
58 {
60 }
61
62 if (md != ccf::crypto::MDType::NONE && pref > best_pref)
63 {
64 best_algo = std::string(algo_name);
65 best_md = md;
66 best_pref = pref;
67 }
68 }
69
70 if (best_md == ccf::crypto::MDType::NONE)
71 {
72 return std::make_pair("sha-256", ccf::crypto::MDType::SHA256);
73 }
74
75 return std::make_pair(best_algo, best_md);
76 }
77}
MDType
Definition md_type.h:10
Definition http_accept.h:13
Definition app_interface.h:13