| 110 | } |
| 111 | |
| 112 | apr_status_t md_jws_hmac(md_json_t **pmsg, apr_pool_t *p, |
| 113 | md_data_t *payload, md_json_t *prot_fields, |
| 114 | const md_data_t *hmac_key) |
| 115 | { |
| 116 | md_json_t *msg, *jprotected; |
| 117 | const char *prot64, *pay64, *mac64, *sign, *prot; |
| 118 | md_data_t data; |
| 119 | apr_status_t rv; |
| 120 | |
| 121 | msg = md_json_create(p); |
| 122 | jprotected = md_json_clone(p, prot_fields); |
| 123 | md_json_sets("HS256", jprotected, "alg", NULL); |
| 124 | prot = md_json_writep(jprotected, p, MD_JSON_FMT_COMPACT); |
| 125 | if (!prot) { |
| 126 | rv = APR_EINVAL; |
| 127 | md_log_perror(MD_LOG_MARK, MD_LOG_WARNING, rv, p, "serialize protected"); |
| 128 | goto cleanup; |
| 129 | } |
| 130 | |
| 131 | md_data_init(&data, prot, strlen(prot)); |
| 132 | prot64 = md_util_base64url_encode(&data, p); |
| 133 | md_json_sets(prot64, msg, "protected", NULL); |
| 134 | |
| 135 | pay64 = md_util_base64url_encode(payload, p); |
| 136 | md_json_sets(pay64, msg, "payload", NULL); |
| 137 | sign = apr_psprintf(p, "%s.%s", prot64, pay64); |
| 138 | |
| 139 | rv = md_crypt_hmac64(&mac64, hmac_key, p, sign, strlen(sign)); |
| 140 | if (APR_SUCCESS != rv) { |
| 141 | goto cleanup; |
| 142 | } |
| 143 | md_json_sets(mac64, msg, "signature", NULL); |
| 144 | |
| 145 | cleanup: |
| 146 | *pmsg = (APR_SUCCESS == rv)? msg : NULL; |
| 147 | return rv; |
| 148 | } |
no test coverage detected