| 40 | } |
| 41 | |
| 42 | apr_status_t md_jws_sign(md_json_t **pmsg, apr_pool_t *p, |
| 43 | md_data_t *payload, md_json_t *prot_fields, |
| 44 | struct md_pkey_t *pkey, const char *key_id) |
| 45 | { |
| 46 | md_json_t *msg, *jprotected, *jwk; |
| 47 | const char *prot64, *pay64, *sign64, *sign, *prot; |
| 48 | md_data_t data; |
| 49 | apr_status_t rv; |
| 50 | |
| 51 | msg = md_json_create(p); |
| 52 | jprotected = md_json_clone(p, prot_fields); |
| 53 | md_json_sets("RS256", jprotected, "alg", NULL); |
| 54 | if (key_id) { |
| 55 | md_json_sets(key_id, jprotected, "kid", NULL); |
| 56 | } |
| 57 | else { |
| 58 | rv = md_jws_get_jwk(&jwk, p, pkey); |
| 59 | if (APR_SUCCESS != rv) { |
| 60 | md_log_perror(MD_LOG_MARK, MD_LOG_WARNING, rv, p, "get jwk"); |
| 61 | goto cleanup; |
| 62 | } |
| 63 | md_json_setj(jwk, jprotected, "jwk", NULL); |
| 64 | } |
| 65 | |
| 66 | prot = md_json_writep(jprotected, p, MD_JSON_FMT_COMPACT); |
| 67 | if (!prot) { |
| 68 | rv = APR_EINVAL; |
| 69 | md_log_perror(MD_LOG_MARK, MD_LOG_WARNING, rv, p, "serialize protected"); |
| 70 | goto cleanup; |
| 71 | } |
| 72 | |
| 73 | md_data_init(&data, prot, strlen(prot)); |
| 74 | prot64 = md_util_base64url_encode(&data, p); |
| 75 | md_json_sets(prot64, msg, "protected", NULL); |
| 76 | |
| 77 | pay64 = md_util_base64url_encode(payload, p); |
| 78 | md_json_sets(pay64, msg, "payload", NULL); |
| 79 | sign = apr_psprintf(p, "%s.%s", prot64, pay64); |
| 80 | |
| 81 | rv = md_crypt_sign64(&sign64, pkey, p, sign, strlen(sign)); |
| 82 | if (APR_SUCCESS != rv) { |
| 83 | md_log_perror(MD_LOG_MARK, MD_LOG_WARNING, rv, p, "jwk signed message"); |
| 84 | goto cleanup; |
| 85 | } |
| 86 | md_json_sets(sign64, msg, "signature", NULL); |
| 87 | |
| 88 | cleanup: |
| 89 | *pmsg = (APR_SUCCESS == rv)? msg : NULL; |
| 90 | return rv; |
| 91 | } |
| 92 | |
| 93 | apr_status_t md_jws_pkey_thumb(const char **pthumb, apr_pool_t *p, struct md_pkey_t *pkey) |
| 94 | { |
no test coverage detected