aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDr. David Alan Gilbert <dave@treblig.org>2018-02-22 22:10:04 +0000
committerDr. David Alan Gilbert <dave@treblig.org>2018-02-25 02:08:49 +0000
commitab534c13c395bdcce5115945b796821cd1e6d75f (patch)
tree78b18d4852cc0e9bc2565ce775c1523331b53161
parent8e49e86f59739d6e9dae3072864736553f78b466 (diff)
downloadpurple-matrix-ab534c13c395bdcce5115945b796821cd1e6d75f.tar.gz
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 <dave@treblig.org>
-rw-r--r--matrix-json.c31
-rw-r--r--matrix-json.h8
2 files changed, 39 insertions, 0 deletions
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);