From ab534c13c395bdcce5115945b796821cd1e6d75f Mon Sep 17 00:00:00 2001 From: "Dr. David Alan Gilbert" Date: Thu, 22 Feb 2018 22:10:04 +0000 Subject: e2e: Add jws decode JSON web signatures almost use base64 but with a slightly odd encoding; decoding a JWS to base64. Signed-off-by: Dr. David Alan Gilbert --- matrix-json.c | 31 +++++++++++++++++++++++++++++++ matrix-json.h | 8 ++++++++ 2 files changed, 39 insertions(+) diff --git a/matrix-json.c b/matrix-json.c index e49176e..dcbb0bd 100644 --- a/matrix-json.c +++ b/matrix-json.c @@ -232,6 +232,37 @@ GString *matrix_canonical_json(JsonObject *object) return canonical_json_object(object, NULL); } +/* Decode a json web signature (JWS) which is almost base64, + * its needs _ -> / and - -> + and some = padding. + * as https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-41#appendix-C + * The output buffer should be upto 3 bytes longer than the input + * depending on the amount of = padding needed. + */ +void matrix_json_jws_tobase64(gchar *out, const gchar *in) +{ + unsigned int i; + for (i=0;in[i];i++) { + out[i] = in[i]; + switch (in[i]) { + case '-': + out[i] = '+'; + break; + + case '_': + out[i] = '/'; + break; + + default: + break; + } + } + while (i & 3) { + out[i] = '='; + i++; + } + out[i] = '\0'; +} + /* Just dump the Json with the string prefix for debugging */ void matrix_debug_jsonobject(const char *reason, JsonObject *object) { diff --git a/matrix-json.h b/matrix-json.h index 43b94aa..8aed2d1 100644 --- a/matrix-json.h +++ b/matrix-json.h @@ -65,6 +65,14 @@ const gchar *matrix_json_array_get_string_element(JsonArray *array, */ GString *matrix_canonical_json(JsonObject *object); +/* Decode a json web signature (JWS) which is almost base64, + * its needs _ -> / and - -> + and some = padding. + * as https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-41#appendix-C + * The output buffer should be upto 3 bytes longer than the input + * depending on the amount of = padding needed. + */ +void matrix_json_jws_tobase64(gchar *out, const gchar *in); + /* Just dump the Json with the string prefix for debugging */ void matrix_debug_jsonobject(const char *reason, JsonObject *object); -- cgit