aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorSchanzenbach, Martin <mschanzenbach@posteo.de>2020-02-06 22:34:11 +0100
committerSchanzenbach, Martin <mschanzenbach@posteo.de>2020-02-09 20:38:11 +0100
commitd06446f143610790d9a0530d524d8e9db2a03b8c (patch)
treed42cdc3c342922b56a41487d5ed49fc1c981066a /src/util
parenta80090ffcc10a2a2c188313e997d16802f2777f1 (diff)
downloadgnunet-d06446f143610790d9a0530d524d8e9db2a03b8c.tar.gz
gnunet-d06446f143610790d9a0530d524d8e9db2a03b8c.zip
add base64url encoding to util (RFC7515)
Diffstat (limited to 'src/util')
-rw-r--r--src/util/strings.c85
1 files changed, 84 insertions, 1 deletions
diff --git a/src/util/strings.c b/src/util/strings.c
index 4e7451283..a291dc4e1 100644
--- a/src/util/strings.c
+++ b/src/util/strings.c
@@ -1897,6 +1897,41 @@ GNUNET_STRINGS_base64_encode (const void *in, size_t len, char **output)
1897} 1897}
1898 1898
1899 1899
1900/**
1901 * Encode into Base64url. RFC7515
1902 *
1903 * @param in the data to encode
1904 * @param len the length of the input
1905 * @param output where to write the output (*output should be NULL,
1906 * is allocated)
1907 * @return the size of the output
1908 */
1909size_t
1910GNUNET_STRINGS_base64url_encode (const void *in, size_t len, char **output)
1911{
1912 char *enc;
1913 size_t pos;
1914
1915 GNUNET_STRINGS_base64_encode (in, len, output);
1916 enc = *output;
1917 // Replace with correct characters for base64url
1918 pos = 0;
1919 while ('\0' != enc[pos])
1920 {
1921 if ('+' == enc[pos])
1922 enc[pos] = '-';
1923 if ('/' == enc[pos])
1924 enc[pos] = '_';
1925 if ('=' == enc[pos])
1926 {
1927 enc[pos] = '\0';
1928 break;
1929 }
1930 pos++;
1931 }
1932 return strlen (enc);
1933}
1934
1900#define cvtfind(a) \ 1935#define cvtfind(a) \
1901 ((((a) >= 'A') && ((a) <= 'Z')) \ 1936 ((((a) >= 'A') && ((a) <= 'Z')) \
1902 ? (a) - 'A' \ 1937 ? (a) - 'A' \
@@ -1929,7 +1964,7 @@ GNUNET_STRINGS_base64_decode (const char *data, size_t len, void **out)
1929 "ignoring CR/LF\n"); \ 1964 "ignoring CR/LF\n"); \
1930 i++; \ 1965 i++; \
1931 if (i >= len) \ 1966 if (i >= len) \
1932 goto END; \ 1967 goto END; \
1933 } 1968 }
1934 1969
1935 output = GNUNET_malloc ((len * 3 / 4) + 8); 1970 output = GNUNET_malloc ((len * 3 / 4) + 8);
@@ -1978,4 +2013,52 @@ END:
1978} 2013}
1979 2014
1980 2015
2016/**
2017 * Decode from Base64url. RFC7515
2018 *
2019 * @param data the data to decode
2020 * @param len the length of the input
2021 * @param output where to write the output (*output should be NULL,
2022 * is allocated)
2023 * @return the size of the output
2024 */
2025size_t
2026GNUNET_STRINGS_base64url_decode (const char *data, size_t len, void **out)
2027{
2028 char *s;
2029 int padding;
2030 size_t ret;
2031
2032 /* make enough space for padding */
2033 s = GNUNET_malloc (len + 3);
2034 memcpy (s, data, len);
2035
2036 for (int i = 0; i < strlen (s); i++)
2037 {
2038 if (s[i] == '-')
2039 s[i] = '+';
2040 if (s[i] == '_')
2041 s[i] = '/';
2042 }
2043 padding = len % 4;
2044 switch (padding) // Pad with trailing '='s
2045 {
2046 case 0:
2047 break; // No pad chars in this case
2048 case 2:
2049 strncpy (&s[len], "==", 2);
2050 break; // Two pad chars
2051 case 3:
2052 s[len] = '=';
2053 break; // One pad char
2054 default:
2055 GNUNET_assert (0);
2056 break;
2057 }
2058 ret = GNUNET_STRINGS_base64_decode (s, strlen (s), out);
2059 GNUNET_free (s);
2060 return ret;
2061}
2062
2063
1981/* end of strings.c */ 2064/* end of strings.c */