diff options
-rw-r--r-- | include/apr_jose.h | 44 | ||||
-rw-r--r-- | jose/apr_jose.c | 298 | ||||
-rw-r--r-- | jose/apr_jose_decode.c | 115 | ||||
-rw-r--r-- | test/testjose.c | 81 |
4 files changed, 271 insertions, 267 deletions
diff --git a/include/apr_jose.h b/include/apr_jose.h index 7389d74ae..5fd8931aa 100644 --- a/include/apr_jose.h +++ b/include/apr_jose.h @@ -923,8 +923,9 @@ APR_DECLARE(apu_err_t *) apr_jose_error(apr_jose_t *jose); * be reused. * @param type the type of structure to create. * @param pool pool used to allocate the result from. + * @return The apr_jose_t is returned. */ -APR_DECLARE(apr_status_t) apr_jose_make(apr_jose_t **jose, apr_jose_type_e type, +APR_DECLARE(apr_jose_t *) apr_jose_make(apr_jose_t *jose, apr_jose_type_e type, apr_pool_t *pool); /** @@ -934,8 +935,9 @@ APR_DECLARE(apr_status_t) apr_jose_make(apr_jose_t **jose, apr_jose_type_e type, * be reused. * @param key the json representing the key. May be NULL. * @param pool pool used to allocate the result from. + * @return The apr_jose_t is returned. */ -APR_DECLARE(apr_status_t) apr_jose_jwk_make(apr_jose_t **jose, +APR_DECLARE(apr_jose_t *) apr_jose_jwk_make(apr_jose_t *jose, apr_json_value_t *key, apr_pool_t *pool); /** @@ -945,8 +947,9 @@ APR_DECLARE(apr_status_t) apr_jose_jwk_make(apr_jose_t **jose, * be reused. * @param keys the array of keys in JSON format. May be NULL. * @param pool pool used to allocate the result from. + * @return The apr_jose_t is returned. */ -APR_DECLARE(apr_status_t) apr_jose_jwks_make(apr_jose_t **jose, +APR_DECLARE(apr_jose_t *) apr_jose_jwks_make(apr_jose_t *jose, apr_json_value_t *keys, apr_pool_t *pool); /** @@ -956,9 +959,10 @@ APR_DECLARE(apr_status_t) apr_jose_jwks_make(apr_jose_t **jose, * @param header the unprotected header. * @param protected the protected header. * @param pool the pool to use. + * @return The apr_jose_signature_t is returned. */ -APR_DECLARE(apr_status_t) apr_jose_signature_make( - apr_jose_signature_t **signature, apr_json_value_t *header, +APR_DECLARE(apr_jose_signature_t *) apr_jose_signature_make( + apr_jose_signature_t *signature, apr_json_value_t *header, apr_json_value_t *protected, apr_pool_t *pool); /** @@ -967,8 +971,9 @@ APR_DECLARE(apr_status_t) apr_jose_signature_make( * @param recipient the result. * @param unprotected the unprotected header. * @param pool the pool to use. + * @return The apr_jose_recipient_t is returned. */ -APR_DECLARE(apr_status_t) apr_jose_recipient_make(apr_jose_recipient_t **recipient, +APR_DECLARE(apr_jose_recipient_t *) apr_jose_recipient_make(apr_jose_recipient_t *recipient, apr_json_value_t *unprotected, apr_pool_t *pool); /** @@ -978,8 +983,9 @@ APR_DECLARE(apr_status_t) apr_jose_recipient_make(apr_jose_recipient_t **recipie * @param unprotected the unprotected shared header. * @param protected the protected header. * @param pool the pool to use. + * @return The apr_jose_encryption_t is returned. */ -APR_DECLARE(apr_status_t) apr_jose_encryption_make(apr_jose_encryption_t **encryption, +APR_DECLARE(apr_jose_encryption_t *) apr_jose_encryption_make(apr_jose_encryption_t *encryption, apr_json_value_t *unprotected, apr_json_value_t *protected, apr_pool_t *pool); @@ -993,8 +999,9 @@ APR_DECLARE(apr_status_t) apr_jose_encryption_make(apr_jose_encryption_t **encry * @param encryption the encryption structure. * @param payload the JOSE payload to encrypt. * @param pool pool used to allocate the result from. + * @return The apr_jose_t is returned. */ -APR_DECLARE(apr_status_t) apr_jose_jwe_make(apr_jose_t **jose, +APR_DECLARE(apr_jose_t *) apr_jose_jwe_make(apr_jose_t *jose, apr_jose_recipient_t *recipient, apr_array_header_t *recipients, apr_jose_encryption_t *encryption, apr_jose_t *payload, apr_pool_t *pool); @@ -1009,8 +1016,9 @@ APR_DECLARE(apr_status_t) apr_jose_jwe_make(apr_jose_t **jose, * @param encryption the encryption structure. * @param payload the JOSE payload to encrypt. * @param pool pool used to allocate the result from. + * @return The apr_jose_t is returned. */ -APR_DECLARE(apr_status_t) apr_jose_jwe_json_make(apr_jose_t **jose, +APR_DECLARE(apr_jose_t *) apr_jose_jwe_json_make(apr_jose_t *jose, apr_jose_recipient_t *recipient, apr_array_header_t *recipients, apr_jose_encryption_t *encryption, apr_jose_t *payload, apr_pool_t *pool); @@ -1024,8 +1032,9 @@ APR_DECLARE(apr_status_t) apr_jose_jwe_json_make(apr_jose_t **jose, * @param signatures array of header / protected header / signature used with general JSON syntax. * @param payload the payload to be wrapped by this JWS. * @param pool pool used to allocate the result from. + * @return The apr_jose_t is returned. */ -APR_DECLARE(apr_status_t) apr_jose_jws_make(apr_jose_t **jose, +APR_DECLARE(apr_jose_t *) apr_jose_jws_make(apr_jose_t *jose, apr_jose_signature_t *signature, apr_array_header_t *signatures, apr_jose_t *payload, apr_pool_t *pool); @@ -1038,8 +1047,9 @@ APR_DECLARE(apr_status_t) apr_jose_jws_make(apr_jose_t **jose, * @param signatures array of header / protected header / signature used with general JSON syntax. * @param payload the payload to be wrapped by this JWS. * @param pool pool used to allocate the result from. + * @return The apr_jose_t is returned. */ -APR_DECLARE(apr_status_t) apr_jose_jws_json_make(apr_jose_t **jose, +APR_DECLARE(apr_jose_t *) apr_jose_jws_json_make(apr_jose_t *jose, apr_jose_signature_t *signature, apr_array_header_t *signatures, apr_jose_t *payload, apr_pool_t *pool); @@ -1053,8 +1063,9 @@ APR_DECLARE(apr_status_t) apr_jose_jws_json_make(apr_jose_t **jose, * be reused. * @param claims the claims to sign. * @param pool pool used to allocate the result from. + * @return The apr_jose_t is returned. */ -APR_DECLARE(apr_status_t) apr_jose_jwt_make(apr_jose_t **jose, +APR_DECLARE(apr_jose_t *) apr_jose_jwt_make(apr_jose_t *jose, apr_json_value_t *claims, apr_pool_t *pool); /** @@ -1066,8 +1077,9 @@ APR_DECLARE(apr_status_t) apr_jose_jwt_make(apr_jose_t **jose, * @param in the plaintext to sign. * @param inlen length of the plaintext. * @param pool pool used to allocate the result from. + * @return The apr_jose_t is returned. */ -APR_DECLARE(apr_status_t) apr_jose_data_make(apr_jose_t **jose, const char *typ, +APR_DECLARE(apr_jose_t *) apr_jose_data_make(apr_jose_t *jose, const char *typ, const unsigned char *in, apr_size_t inlen, apr_pool_t *pool); /** @@ -1080,8 +1092,9 @@ APR_DECLARE(apr_status_t) apr_jose_data_make(apr_jose_t **jose, const char *typ, * @param in the UTF-8 encoded text string. * @param inlen length of the UTF-8 encoded text string. * @param pool pool used to allocate the result from. + * @return The apr_jose_t is returned. */ -APR_DECLARE(apr_status_t) apr_jose_text_make(apr_jose_t **jose, const char *cty, +APR_DECLARE(apr_jose_t *) apr_jose_text_make(apr_jose_t *jose, const char *cty, const char *in, apr_size_t inlen, apr_pool_t *pool); /** @@ -1092,8 +1105,9 @@ APR_DECLARE(apr_status_t) apr_jose_text_make(apr_jose_t **jose, const char *cty, * @param cty the content type. * @param json the json object to add. * @param pool pool used to allocate the result from. + * @return The apr_jose_t is returned. */ -APR_DECLARE(apr_status_t) apr_jose_json_make(apr_jose_t **jose, const char *cty, +APR_DECLARE(apr_jose_t *) apr_jose_json_make(apr_jose_t *jose, const char *cty, apr_json_value_t *json, apr_pool_t *pool); /** diff --git a/jose/apr_jose.c b/jose/apr_jose.c index 26f6b0ffa..f48554b54 100644 --- a/jose/apr_jose.c +++ b/jose/apr_jose.c @@ -21,141 +21,138 @@ APR_DECLARE(apu_err_t *) apr_jose_error(apr_jose_t *jose) return &jose->result; } -APR_DECLARE(apr_status_t) apr_jose_make(apr_jose_t **jose, apr_jose_type_e type, +APR_DECLARE(apr_jose_t *) apr_jose_make(apr_jose_t *jose, apr_jose_type_e type, apr_pool_t *pool) { - apr_jose_t *j; - - if (*jose) { - j = *jose; - } else { - *jose = j = apr_pcalloc(pool, sizeof(apr_jose_t)); - if (!j) { - return APR_ENOMEM; + + if (!jose) { + jose = apr_pcalloc(pool, sizeof(apr_jose_t)); + if (!jose) { + return NULL; } } - j->pool = pool; - j->type = type; + jose->pool = pool; + jose->type = type; - return APR_SUCCESS; + return jose; } -APR_DECLARE(apr_status_t) apr_jose_data_make(apr_jose_t **jose, const char *typ, +APR_DECLARE(apr_jose_t *) apr_jose_data_make(apr_jose_t *jose, const char *typ, const unsigned char *in, apr_size_t inlen, apr_pool_t *pool) { - apr_jose_t *j; - apr_status_t status; - status = apr_jose_make(jose, APR_JOSE_TYPE_DATA, pool); - if (APR_SUCCESS != status) { - return status; + if (!jose) { + jose = apr_jose_make(jose, APR_JOSE_TYPE_DATA, pool); + if (!jose) { + return NULL; + } } - j = *jose; - j->typ = typ; - j->jose.data = apr_palloc(pool, sizeof(apr_jose_data_t)); - if (!j->jose.data) { - return APR_ENOMEM; + jose->typ = typ; + jose->jose.data = apr_palloc(pool, sizeof(apr_jose_data_t)); + if (!jose->jose.data) { + return NULL; } - j->jose.data->data = in; - j->jose.data->len = inlen; + jose->jose.data->data = in; + jose->jose.data->len = inlen; - return APR_SUCCESS; + return jose; } -APR_DECLARE(apr_status_t) apr_jose_json_make(apr_jose_t **jose, const char *cty, +APR_DECLARE(apr_jose_t *) apr_jose_json_make(apr_jose_t *jose, const char *cty, apr_json_value_t *json, apr_pool_t *pool) { - apr_jose_t *j; - apr_status_t status; - status = apr_jose_make(jose, APR_JOSE_TYPE_JSON, pool); - if (APR_SUCCESS != status) { - return status; + if (!jose) { + jose = apr_jose_make(jose, APR_JOSE_TYPE_JSON, pool); + if (!jose) { + return NULL; + } } - j = *jose; - j->cty = cty; - j->jose.json = apr_palloc(pool, sizeof(apr_jose_json_t)); - if (!j->jose.json) { - return APR_ENOMEM; + jose->cty = cty; + jose->jose.json = apr_palloc(pool, sizeof(apr_jose_json_t)); + if (!jose->jose.json) { + return NULL; } - j->jose.json->json = json; + jose->jose.json->json = json; - return APR_SUCCESS; + return jose; } -APR_DECLARE(apr_status_t) apr_jose_signature_make( - apr_jose_signature_t **signature, apr_json_value_t *header, +APR_DECLARE(apr_jose_signature_t *) apr_jose_signature_make( + apr_jose_signature_t *signature, apr_json_value_t *header, apr_json_value_t *protected, apr_pool_t *pool) { - apr_jose_signature_t *s; - *signature = s = apr_pcalloc(pool, sizeof(apr_jose_signature_t)); - if (!s) { - return APR_ENOMEM; + if (!signature) { + signature = apr_pcalloc(pool, sizeof(apr_jose_signature_t)); + if (!signature) { + return NULL; + } } - s->header = header; - s->protected_header = protected; + signature->header = header; + signature->protected_header = protected; - return APR_SUCCESS; + return signature; } -APR_DECLARE(apr_status_t) apr_jose_recipient_make( - apr_jose_recipient_t **recipient, apr_json_value_t *header, +APR_DECLARE(apr_jose_recipient_t *) apr_jose_recipient_make( + apr_jose_recipient_t *recipient, apr_json_value_t *header, apr_pool_t *pool) { - apr_jose_recipient_t *r; - *recipient = r = apr_pcalloc(pool, sizeof(apr_jose_recipient_t)); - if (!r) { - return APR_ENOMEM; + if (!recipient) { + recipient = apr_pcalloc(pool, sizeof(apr_jose_recipient_t)); + if (!recipient) { + return NULL; + } } - r->header = header; + recipient->header = header; - return APR_SUCCESS; + return recipient; } -APR_DECLARE(apr_status_t) apr_jose_encryption_make( - apr_jose_encryption_t **encryption, apr_json_value_t *header, +APR_DECLARE(apr_jose_encryption_t *) apr_jose_encryption_make( + apr_jose_encryption_t *encryption, apr_json_value_t *header, apr_json_value_t *protected_header, apr_pool_t *pool) { - apr_jose_encryption_t *e; - *encryption = e = apr_pcalloc(pool, sizeof(apr_jose_encryption_t)); - if (!e) { - return APR_ENOMEM; + if (!encryption) { + encryption = apr_pcalloc(pool, sizeof(apr_jose_encryption_t)); + if (!encryption) { + return NULL; + } } - e->unprotected = header; - e->protected = protected_header; + encryption->unprotected = header; + encryption->protected = protected_header; - return APR_SUCCESS; + return encryption; } -APR_DECLARE(apr_status_t) apr_jose_jwe_make(apr_jose_t **jose, +APR_DECLARE(apr_jose_t *) apr_jose_jwe_make(apr_jose_t *jose, apr_jose_recipient_t *recipient, apr_array_header_t *recipients, apr_jose_encryption_t *encryption, apr_jose_t *payload, apr_pool_t *pool) { - apr_jose_t *j; apr_jose_jwe_t *jwe; - apr_status_t status; - status = apr_jose_make(jose, APR_JOSE_TYPE_JWE, pool); - if (APR_SUCCESS != status) { - return status; + if (!jose) { + jose = apr_jose_make(jose, APR_JOSE_TYPE_JWE, pool); + if (!jose) { + return NULL; + } } - j = *jose; - j->cty = payload->cty; + jose->cty = payload->cty; - jwe = j->jose.jwe = apr_palloc(pool, sizeof(apr_jose_jwe_t)); + jwe = jose->jose.jwe = apr_palloc(pool, sizeof(apr_jose_jwe_t)); if (!jwe) { - return APR_ENOMEM; + return NULL; } jwe->recipient = recipient; @@ -163,31 +160,30 @@ APR_DECLARE(apr_status_t) apr_jose_jwe_make(apr_jose_t **jose, jwe->encryption = encryption; jwe->payload = payload; - return APR_SUCCESS; + return jose; } -APR_DECLARE(apr_status_t) apr_jose_jwe_json_make(apr_jose_t **jose, +APR_DECLARE(apr_jose_t *) apr_jose_jwe_json_make(apr_jose_t *jose, apr_jose_recipient_t *recipient, apr_array_header_t *recipients, apr_jose_encryption_t *encryption, apr_jose_t *payload, apr_pool_t *pool) { - apr_jose_t *j; apr_jose_jwe_t *jwe; - apr_status_t status; - status = apr_jose_make(jose, APR_JOSE_TYPE_JWE_JSON, pool); - if (APR_SUCCESS != status) { - return status; + if (!jose) { + jose = apr_jose_make(jose, APR_JOSE_TYPE_JWE_JSON, pool); + if (!jose) { + return NULL; + } } - j = *jose; if (payload) { - j->cty = payload->cty; + jose->cty = payload->cty; } - jwe = j->jose.jwe = apr_palloc(pool, sizeof(apr_jose_jwe_t)); + jwe = jose->jose.jwe = apr_palloc(pool, sizeof(apr_jose_jwe_t)); if (!jwe) { - return APR_ENOMEM; + return NULL; } jwe->recipient = recipient; @@ -195,159 +191,153 @@ APR_DECLARE(apr_status_t) apr_jose_jwe_json_make(apr_jose_t **jose, jwe->encryption = encryption; jwe->payload = payload; - return APR_SUCCESS; + return jose; } -APR_DECLARE(apr_status_t) apr_jose_jwk_make(apr_jose_t **jose, +APR_DECLARE(apr_jose_t *) apr_jose_jwk_make(apr_jose_t *jose, apr_json_value_t *key, apr_pool_t *pool) { - apr_jose_t *j; apr_jose_jwk_t *jwk; - apr_status_t status; - status = apr_jose_make(jose, APR_JOSE_TYPE_JWK, pool); - if (APR_SUCCESS != status) { - return status; + if (!jose) { + jose = apr_jose_make(jose, APR_JOSE_TYPE_JWK, pool); + if (!jose) { + return NULL; + } } - j = *jose; - jwk = j->jose.jwk = apr_palloc(pool, sizeof(apr_jose_jwk_t)); + jwk = jose->jose.jwk = apr_palloc(pool, sizeof(apr_jose_jwk_t)); if (!jwk) { - return APR_ENOMEM; + return NULL; } jwk->key = key; - return APR_SUCCESS; + return jose; } -APR_DECLARE(apr_status_t) apr_jose_jwks_make(apr_jose_t **jose, +APR_DECLARE(apr_jose_t *) apr_jose_jwks_make(apr_jose_t *jose, apr_json_value_t *keys, apr_pool_t *pool) { - apr_jose_t *j; apr_jose_jwks_t *jwks; - apr_status_t status; - status = apr_jose_make(jose, APR_JOSE_TYPE_JWKS, pool); - if (APR_SUCCESS != status) { - return status; + if (!jose) { + jose = apr_jose_make(jose, APR_JOSE_TYPE_JWKS, pool); + if (!jose) { + return NULL; + } } - j = *jose; - jwks = j->jose.jwks = apr_palloc(pool, sizeof(apr_jose_jwks_t)); + jwks = jose->jose.jwks = apr_palloc(pool, sizeof(apr_jose_jwks_t)); if (!jwks) { - return APR_ENOMEM; + return NULL; } jwks->keys = keys; - return APR_SUCCESS; + return jose; } -APR_DECLARE(apr_status_t) apr_jose_jws_make(apr_jose_t **jose, +APR_DECLARE(apr_jose_t *) apr_jose_jws_make(apr_jose_t *jose, apr_jose_signature_t *signature, apr_array_header_t *signatures, apr_jose_t *payload, apr_pool_t *pool) { - apr_jose_t *j; apr_jose_jws_t *jws; - apr_status_t status; - status = apr_jose_make(jose, APR_JOSE_TYPE_JWS, pool); - if (APR_SUCCESS != status) { - return status; + if (!jose) { + jose = apr_jose_make(jose, APR_JOSE_TYPE_JWS, pool); + if (!jose) { + return NULL; + } } - j = *jose; if (payload) { - j->cty = payload->cty; + jose->cty = payload->cty; } - jws = j->jose.jws = apr_pcalloc(pool, sizeof(apr_jose_jws_t)); + jws = jose->jose.jws = apr_pcalloc(pool, sizeof(apr_jose_jws_t)); if (!jws) { - return APR_ENOMEM; + return NULL; } jws->signature = signature; jws->signatures = signatures; jws->payload = payload; - return APR_SUCCESS; + return jose; } -APR_DECLARE(apr_status_t) apr_jose_jws_json_make(apr_jose_t **jose, +APR_DECLARE(apr_jose_t *) apr_jose_jws_json_make(apr_jose_t *jose, apr_jose_signature_t *signature, apr_array_header_t *signatures, apr_jose_t *payload, apr_pool_t *pool) { - apr_jose_t *j; apr_jose_jws_t *jws; - apr_status_t status; - status = apr_jose_make(jose, APR_JOSE_TYPE_JWS_JSON, pool); - if (APR_SUCCESS != status) { - return status; + if (!jose) { + jose = apr_jose_make(jose, APR_JOSE_TYPE_JWS_JSON, pool); + if (!jose) { + return NULL; + } } - j = *jose; if (payload) { - j->cty = payload->cty; + jose->cty = payload->cty; } - jws = j->jose.jws = apr_pcalloc(pool, sizeof(apr_jose_jws_t)); + jws = jose->jose.jws = apr_pcalloc(pool, sizeof(apr_jose_jws_t)); if (!jws) { - return APR_ENOMEM; + return NULL; } jws->signature = signature; jws->signatures = signatures; jws->payload = payload; - return APR_SUCCESS; + return jose; } -APR_DECLARE(apr_status_t) apr_jose_jwt_make(apr_jose_t **jose, apr_json_value_t *claims, +APR_DECLARE(apr_jose_t *) apr_jose_jwt_make(apr_jose_t *jose, apr_json_value_t *claims, apr_pool_t *pool) { - apr_jose_t *j; apr_jose_jwt_t *jwt; - apr_status_t status; - status = apr_jose_make(jose, APR_JOSE_TYPE_JWT, pool); - if (APR_SUCCESS != status) { - return status; + if (!jose) { + jose = apr_jose_make(jose, APR_JOSE_TYPE_JWT, pool); + if (!jose) { + return NULL; + } } - j = *jose; - j->cty = "JWT"; + jose->cty = "JWT"; - jwt = j->jose.jwt = apr_palloc(pool, sizeof(apr_jose_jwt_t)); + jwt = jose->jose.jwt = apr_palloc(pool, sizeof(apr_jose_jwt_t)); if (!jwt) { - return APR_ENOMEM; + return NULL; } jwt->claims = claims; - return APR_SUCCESS; + return jose; } -APR_DECLARE(apr_status_t) apr_jose_text_make(apr_jose_t **jose, const char *cty, +APR_DECLARE(apr_jose_t *) apr_jose_text_make(apr_jose_t *jose, const char *cty, const char *in, apr_size_t inlen, apr_pool_t *pool) { - apr_jose_t *j; - apr_status_t status; - status = apr_jose_make(jose, APR_JOSE_TYPE_TEXT, pool); - if (APR_SUCCESS != status) { - return status; + if (!jose) { + jose = apr_jose_make(jose, APR_JOSE_TYPE_TEXT, pool); + if (!jose) { + return NULL; + } } - j = *jose; - j->cty = cty; - j->jose.text = apr_palloc(pool, sizeof(apr_jose_text_t)); - if (!j->jose.text) { - return APR_ENOMEM; + jose->cty = cty; + jose->jose.text = apr_palloc(pool, sizeof(apr_jose_text_t)); + if (!jose->jose.text) { + return NULL; } - j->jose.text->text = in; - j->jose.text->len = inlen; + jose->jose.text->text = in; + jose->jose.text->len = inlen; - return APR_SUCCESS; + return jose; } diff --git a/jose/apr_jose_decode.c b/jose/apr_jose_decode.c index 7d0b01d49..defe06a34 100644 --- a/jose/apr_jose_decode.c +++ b/jose/apr_jose_decode.c @@ -41,23 +41,24 @@ apr_status_t apr_jose_decode_jwk(apr_jose_t **jose, const char *typ, apr_bucket_brigade *bb, apr_jose_cb_t *cb, int level, int flags, apr_pool_t *pool) { + apr_json_value_t *key; apr_jose_text_t in; apr_off_t offset; apr_status_t status; - status = apr_jose_jwk_make(jose, NULL, pool); - if (APR_SUCCESS != status) { - return status; - } - status = apr_jose_flatten(bb, &in, pool); if (APR_SUCCESS != status) { return status; } - status = apr_json_decode(&(*jose)->jose.jwk->key, in.text, in.len, &offset, + status = apr_json_decode(&key, in.text, in.len, &offset, APR_JSON_FLAGS_WHITESPACE, level, pool); + *jose = apr_jose_jwk_make(NULL, key, pool); + if (!*jose) { + return APR_ENOMEM; + } + if (APR_SUCCESS != status) { char buf[1024]; apr_strerror(status, buf, sizeof(buf)); @@ -75,23 +76,24 @@ apr_status_t apr_jose_decode_jwks(apr_jose_t **jose, const char *typ, apr_bucket_brigade *bb, apr_jose_cb_t *cb, int level, int flags, apr_pool_t *pool) { + apr_json_value_t *keys; apr_jose_text_t in; apr_off_t offset; apr_status_t status; - status = apr_jose_jwks_make(jose, NULL, pool); - if (APR_SUCCESS != status) { - return status; - } - status = apr_jose_flatten(bb, &in, pool); if (APR_SUCCESS != status) { return status; } - status = apr_json_decode(&(*jose)->jose.jwks->keys, in.text, in.len, + status = apr_json_decode(&keys, in.text, in.len, &offset, APR_JSON_FLAGS_WHITESPACE, level, pool); + *jose = apr_jose_jwks_make(NULL, keys, pool); + if (!*jose) { + return APR_ENOMEM; + } + if (APR_SUCCESS != status) { char buf[1024]; apr_strerror(status, buf, sizeof(buf)); @@ -102,7 +104,7 @@ apr_status_t apr_jose_decode_jwks(apr_jose_t **jose, return status; } - if ((*jose)->jose.jwks->keys->type != APR_JSON_ARRAY) { + if (keys->type != APR_JSON_ARRAY) { apr_errprintf(&(*jose)->result, pool, NULL, 0, "Syntax error: JWKS 'keys' is not an array"); return APR_EINVAL; @@ -115,23 +117,24 @@ apr_status_t apr_jose_decode_jwt(apr_jose_t **jose, const char *typ, apr_bucket_brigade *bb, apr_jose_cb_t *cb, int level, int flags, apr_pool_t *pool) { + apr_json_value_t *claims; apr_jose_text_t in; apr_off_t offset; apr_status_t status; - status = apr_jose_jwt_make(jose, NULL, pool); - if (APR_SUCCESS != status) { - return status; - } - status = apr_jose_flatten(bb, &in, pool); if (APR_SUCCESS != status) { return status; } - status = apr_json_decode(&(*jose)->jose.jwt->claims, in.text, in.len, &offset, + status = apr_json_decode(&claims, in.text, in.len, &offset, APR_JSON_FLAGS_WHITESPACE, level, pool); + *jose = apr_jose_jwt_make(NULL, claims, pool); + if (!*jose) { + return APR_ENOMEM; + } + if (APR_SUCCESS != status) { char buf[1024]; apr_strerror(status, buf, sizeof(buf)); @@ -157,10 +160,10 @@ apr_status_t apr_jose_decode_data(apr_jose_t **jose, const char *typ, return status; } - status = apr_jose_data_make(jose, typ, (const unsigned char *) in.text, in.len, - pool); - if (APR_SUCCESS != status) { - return status; + *jose = apr_jose_data_make(NULL, typ, (const unsigned char *) in.text, + in.len, pool); + if (!*jose) { + return APR_ENOMEM; } return status; @@ -404,9 +407,9 @@ apr_status_t apr_jose_decode_compact_jws(apr_jose_t **jose, return APR_EINIT; } - status = apr_jose_jws_make(jose, NULL, NULL, NULL, pool); - if (APR_SUCCESS != status) { - return status; + *jose = apr_jose_jws_make(*jose, NULL, NULL, NULL, pool); + if (!*jose) { + return APR_ENOMEM; } jws = (*jose)->jose.jws; @@ -415,9 +418,9 @@ apr_status_t apr_jose_decode_compact_jws(apr_jose_t **jose, * the JWS Protected Header. */ - status = apr_jose_signature_make(&jws->signature, NULL, ph, pool); - if (APR_SUCCESS != status) { - return status; + jws->signature = apr_jose_signature_make(NULL, NULL, ph, pool); + if (!jws->signature) { + return APR_ENOMEM; } dot = memchr(left, '.', right - left); @@ -501,21 +504,21 @@ apr_status_t apr_jose_decode_compact_jwe(apr_jose_t **jose, const char *left, return APR_EINIT; } - status = apr_jose_jwe_make(jose, NULL, NULL, NULL, NULL, pool); - if (APR_SUCCESS != status) { - return status; + *jose = apr_jose_jwe_make(*jose, NULL, NULL, NULL, NULL, pool); + if (!*jose) { + return APR_ENOMEM; } jwe = (*jose)->jose.jwe; - status = apr_jose_encryption_make(&jwe->encryption, NULL, + jwe->encryption = apr_jose_encryption_make(NULL, NULL, NULL, pool); - if (APR_SUCCESS != status) { - return status; + if (!jwe->encryption) { + return APR_ENOMEM; } - status = apr_jose_recipient_make(&jwe->recipient, NULL, pool); - if (APR_SUCCESS != status) { - return status; + jwe->recipient = apr_jose_recipient_make(NULL, NULL, pool); + if (!jwe->recipient) { + return APR_ENOMEM; } /* @@ -655,9 +658,9 @@ apr_status_t apr_jose_decode_compact(apr_jose_t **jose, const char *typ, left = in.text; right = in.text + in.len; - status = apr_jose_make(jose, APR_JOSE_TYPE_NONE, pool); - if (APR_SUCCESS != status) { - return status; + *jose = apr_jose_make(NULL, APR_JOSE_TYPE_NONE, pool); + if (!*jose) { + return APR_ENOMEM; } bb = apr_brigade_create(pool, brigade->bucket_alloc); @@ -852,9 +855,9 @@ apr_status_t apr_jose_decode_json_jws(apr_jose_t **jose, apr_json_value_t *val, return APR_BADCH; } - status = apr_jose_jws_json_make(jose, NULL, NULL, NULL, pool); - if (APR_SUCCESS != status) { - return status; + *jose = apr_jose_jws_json_make(*jose, NULL, NULL, NULL, pool); + if (!*jose) { + return APR_ENOMEM; } jws = (*jose)->jose.jws; @@ -1037,10 +1040,10 @@ apr_status_t apr_jose_decode_json_jws(apr_jose_t **jose, apr_json_value_t *val, return APR_SUCCESS; } - status = apr_jose_signature_make(&jws->signature, NULL, NULL, + jws->signature = apr_jose_signature_make(NULL, NULL, NULL, pool); - if (APR_SUCCESS != status) { - return status; + if (!jws->signature) { + return APR_ENOMEM; } kv = apr_json_object_get(val, APR_JOSE_JWSE_PROTECTED, @@ -1173,16 +1176,16 @@ apr_status_t apr_jose_decode_json_jwe(apr_jose_t **jose, apr_json_value_t *val, return APR_EINVAL; } - status = apr_jose_jwe_json_make(jose, NULL, NULL, NULL, NULL, pool); - if (APR_SUCCESS != status) { - return status; + *jose = apr_jose_jwe_json_make(*jose, NULL, NULL, NULL, NULL, pool); + if (!*jose) { + return APR_ENOMEM; } jwe = (*jose)->jose.jwe; - status = apr_jose_encryption_make(&jwe->encryption, NULL, + jwe->encryption = apr_jose_encryption_make(NULL, NULL, NULL, pool); - if (APR_SUCCESS != status) { - return status; + if (!jwe->encryption) { + return APR_ENOMEM; } /* @@ -1547,9 +1550,9 @@ apr_status_t apr_jose_decode_json(apr_jose_t **jose, const char *typ, apr_off_t offset; apr_status_t status; - status = apr_jose_make(jose, APR_JOSE_TYPE_NONE, pool); - if (APR_SUCCESS != status) { - return status; + *jose = apr_jose_make(NULL, APR_JOSE_TYPE_NONE, pool); + if (!*jose) { + return APR_ENOMEM; } status = apr_jose_flatten(brigade, &in, pool); diff --git a/test/testjose.c b/test/testjose.c index 411b7c5d0..a804bbbaf 100644 --- a/test/testjose.c +++ b/test/testjose.c @@ -599,8 +599,8 @@ static void test_jose_encode_jws_compact_unsecured(abts_case *tc, void *data) { apr_bucket_alloc_t *ba; apr_bucket_brigade *bb; - apr_jose_t *jose = NULL; - apr_jose_t *jdata = NULL; + apr_jose_t *jose; + apr_jose_t *jdata; apr_jose_signature_t signature; char buf[1024]; apr_size_t len = sizeof(buf); @@ -631,8 +631,8 @@ static void test_jose_encode_jws_compact_unsecured(abts_case *tc, void *data) ba = apr_bucket_alloc_create(p); bb = apr_brigade_create(p, ba); - apr_jose_data_make(&jdata, "JWT", pl, sizeof(pl), p); - apr_jose_jws_make(&jose, &signature, NULL, jdata, p); + jdata = apr_jose_data_make(NULL, "JWT", pl, sizeof(pl), p); + jose = apr_jose_jws_make(NULL, &signature, NULL, jdata, p); status = apr_jose_encode(bb, NULL, NULL, jose, &cb, p); @@ -649,8 +649,8 @@ static void test_jose_encode_jws_compact_hs256(abts_case *tc, void *data) { apr_bucket_alloc_t *ba; apr_bucket_brigade *bb; - apr_jose_t *jose = NULL; - apr_jose_t *jdata = NULL; + apr_jose_t *jose; + apr_jose_t *jdata; apr_jose_signature_t signature; char buf[1024]; apr_size_t len = sizeof(buf); @@ -685,8 +685,8 @@ static void test_jose_encode_jws_compact_hs256(abts_case *tc, void *data) ba = apr_bucket_alloc_create(p); bb = apr_brigade_create(p, ba); - apr_jose_data_make(&jdata, "JWT", pl, sizeof(pl), p); - apr_jose_jws_make(&jose, &signature, NULL, jdata, p); + jdata = apr_jose_data_make(NULL, "JWT", pl, sizeof(pl), p); + jose = apr_jose_jws_make(NULL, &signature, NULL, jdata, p); status = apr_jose_encode(bb, NULL, NULL, jose, &cb, p); @@ -703,8 +703,8 @@ static void test_jose_encode_jws_json_general(abts_case *tc, void *data) { apr_bucket_alloc_t *ba; apr_bucket_brigade *bb; - apr_jose_t *jose = NULL; - apr_jose_t *jdata = NULL; + apr_jose_t *jose; + apr_jose_t *jdata; apr_jose_signature_t **signature; apr_jose_signature_t signature1; apr_jose_signature_t signature2; @@ -771,8 +771,8 @@ static void test_jose_encode_jws_json_general(abts_case *tc, void *data) ba = apr_bucket_alloc_create(p); bb = apr_brigade_create(p, ba); - apr_jose_data_make(&jdata, "JWT", pl, sizeof(pl), p); - apr_jose_jws_json_make(&jose, NULL, signatures, jdata, p); + jdata = apr_jose_data_make(NULL, "JWT", pl, sizeof(pl), p); + jose = apr_jose_jws_json_make(NULL, NULL, signatures, jdata, p); status = apr_jose_encode(bb, NULL, NULL, jose, &cb, p); @@ -790,8 +790,8 @@ static void test_jose_encode_jws_json_flattened(abts_case *tc, void *data) { apr_bucket_alloc_t *ba; apr_bucket_brigade *bb; - apr_jose_t *jose = NULL; - apr_jose_t *jdata = NULL; + apr_jose_t *jose; + apr_jose_t *jdata; apr_jose_signature_t signature2; char buf[1024]; apr_size_t len = sizeof(buf); @@ -832,8 +832,8 @@ static void test_jose_encode_jws_json_flattened(abts_case *tc, void *data) ba = apr_bucket_alloc_create(p); bb = apr_brigade_create(p, ba); - apr_jose_data_make(&jdata, "JWT", pl, sizeof(pl), p); - apr_jose_jws_json_make(&jose, &signature2, NULL, jdata, p); + jdata = apr_jose_data_make(NULL, "JWT", pl, sizeof(pl), p); + jose = apr_jose_jws_json_make(NULL, &signature2, NULL, jdata, p); status = apr_jose_encode(bb, NULL, NULL, jose, &cb, p); @@ -851,8 +851,8 @@ static void test_jose_encode_jwe_compact_rsaes_oaep_aes_gcm(abts_case *tc, void { apr_bucket_alloc_t *ba; apr_bucket_brigade *bb; - apr_jose_t *jose = NULL; - apr_jose_t *jdata = NULL; + apr_jose_t *jose; + apr_jose_t *jdata; apr_jose_encryption_t *encryption; apr_jose_recipient_t *recipient; apr_json_value_t *header = NULL; @@ -893,10 +893,10 @@ static void test_jose_encode_jwe_compact_rsaes_oaep_aes_gcm(abts_case *tc, void ba = apr_bucket_alloc_create(p); bb = apr_brigade_create(p, ba); - apr_jose_data_make(&jdata, "JWT", pl, sizeof(pl), p); - apr_jose_recipient_make(&recipient, header, p); - apr_jose_encryption_make(&encryption, NULL, protected_header, p); - apr_jose_jwe_make(&jose, recipient, NULL, encryption, jdata, p); + jdata = apr_jose_data_make(NULL, "JWT", pl, sizeof(pl), p); + recipient = apr_jose_recipient_make(NULL, header, p); + encryption = apr_jose_encryption_make(NULL, NULL, protected_header, p); + jose = apr_jose_jwe_make(NULL, recipient, NULL, encryption, jdata, p); status = apr_jose_encode(bb, NULL, NULL, jose, &cb, p); @@ -913,8 +913,8 @@ static void test_jose_encode_jwe_json_general(abts_case *tc, void *data) { apr_bucket_alloc_t *ba; apr_bucket_brigade *bb; - apr_jose_t *jose = NULL; - apr_jose_t *jdata = NULL; + apr_jose_t *jose; + apr_jose_t *jdata; apr_json_value_t *header = NULL; apr_json_value_t *protected_header = NULL; apr_jose_recipient_t **recipient; @@ -979,9 +979,6 @@ static void test_jose_encode_jwe_json_general(abts_case *tc, void *data) recipient = apr_array_push(recipients); *recipient = &recipient2; - - - apr_json_decode(&header, h, APR_JSON_VALUE_STRING, &offset, APR_JSON_FLAGS_WHITESPACE, 10, p); apr_json_decode(&protected_header, ph, APR_JSON_VALUE_STRING, &offset, @@ -990,9 +987,9 @@ static void test_jose_encode_jwe_json_general(abts_case *tc, void *data) ba = apr_bucket_alloc_create(p); bb = apr_brigade_create(p, ba); - apr_jose_data_make(&jdata, "plain", pl, sizeof(pl), p); - apr_jose_encryption_make(&encryption, header, protected_header, p); - apr_jose_jwe_json_make(&jose, NULL, recipients, encryption, jdata, p); + jdata = apr_jose_data_make(NULL, "plain", pl, sizeof(pl), p); + encryption = apr_jose_encryption_make(NULL, header, protected_header, p); + jose = apr_jose_jwe_json_make(NULL, NULL, recipients, encryption, jdata, p); status = apr_jose_encode(bb, NULL, NULL, jose, &cb, p); @@ -1009,8 +1006,8 @@ static void test_jose_encode_jwe_json_flattened(abts_case *tc, void *data) { apr_bucket_alloc_t *ba; apr_bucket_brigade *bb; - apr_jose_t *jose = NULL; - apr_jose_t *jdata = NULL; + apr_jose_t *jose; + apr_jose_t *jdata; apr_json_value_t *header = NULL; apr_json_value_t *protected_header = NULL; apr_jose_recipient_t recipient; @@ -1061,9 +1058,9 @@ static void test_jose_encode_jwe_json_flattened(abts_case *tc, void *data) ba = apr_bucket_alloc_create(p); bb = apr_brigade_create(p, ba); - apr_jose_data_make(&jdata, "plain", pl, sizeof(pl), p); - apr_jose_encryption_make(&encryption, header, protected_header, p); - apr_jose_jwe_json_make(&jose, &recipient, NULL, encryption, jdata, p); + jdata = apr_jose_data_make(NULL, "plain", pl, sizeof(pl), p); + encryption = apr_jose_encryption_make(NULL, header, protected_header, p); + jose = apr_jose_jwe_json_make(NULL, &recipient, NULL, encryption, jdata, p); status = apr_jose_encode(bb, NULL, NULL, jose, &cb, p); @@ -1080,7 +1077,7 @@ static void test_jose_decode_jws_compact_unsecured(abts_case *tc, void *data) { apr_bucket_alloc_t *ba; apr_bucket_brigade *bb; - apr_jose_t *jose = NULL; + apr_jose_t *jose; apr_json_kv_t *kv; apr_status_t status; @@ -1121,7 +1118,7 @@ static void test_jose_decode_jws_compact_hs256(abts_case *tc, void *data) { apr_bucket_alloc_t *ba; apr_bucket_brigade *bb; - apr_jose_t *jose = NULL; + apr_jose_t *jose; apr_json_kv_t *kv; apr_status_t status; @@ -1186,7 +1183,7 @@ static void test_jose_decode_jws_json_general(abts_case *tc, void *data) apr_bucket_alloc_t *ba; apr_bucket_brigade *bb; - apr_jose_t *jose = NULL; + apr_jose_t *jose; apr_status_t status; apr_jose_cb_t cb; @@ -1220,7 +1217,7 @@ static void test_jose_decode_jws_json_flattened(abts_case *tc, void *data) { apr_bucket_alloc_t *ba; apr_bucket_brigade *bb; - apr_jose_t *jose = NULL; + apr_jose_t *jose; apr_status_t status; const char *source = "{" @@ -1261,7 +1258,7 @@ static void test_jose_decode_jwe_compact_rsaes_oaep_aes_gcm(abts_case *tc, void { apr_bucket_alloc_t *ba; apr_bucket_brigade *bb; - apr_jose_t *jose = NULL; + apr_jose_t *jose; apr_status_t status; const char *source = "eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkEyNTZHQ00ifQ." @@ -1302,7 +1299,7 @@ static void test_jose_decode_jwe_json_general(abts_case *tc, void *data) { apr_bucket_alloc_t *ba; apr_bucket_brigade *bb; - apr_jose_t *jose = NULL; + apr_jose_t *jose; apr_status_t status; const char *source = "{" @@ -1359,7 +1356,7 @@ static void test_jose_decode_jwe_json_flattened(abts_case *tc, void *data) { apr_bucket_alloc_t *ba; apr_bucket_brigade *bb; - apr_jose_t *jose = NULL; + apr_jose_t *jose; apr_status_t status; const char *source = "{" |