Apache HTTPD
md_jws.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <apr_lib.h>
18#include <apr_strings.h>
19#include <apr_tables.h>
20#include <apr_buckets.h>
21
22#include "md_crypt.h"
23#include "md_json.h"
24#include "md_jws.h"
25#include "md_log.h"
26#include "md_util.h"
27
29{
31
32 if (!pkey) return APR_EINVAL;
33
36 md_json_sets("RSA", jwk, "kty", NULL);
38 *pjwk = jwk;
39 return APR_SUCCESS;
40}
41
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;
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 }
64 }
65
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));
75 md_json_sets(prot64, msg, "protected", NULL);
76
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
89 *pmsg = (APR_SUCCESS == rv)? msg : NULL;
90 return rv;
91}
92
94{
95 const char *e64, *n64, *s;
97 apr_status_t rv;
98
101 if (!e64 || !n64) {
102 return APR_EINVAL;
103 }
104
105 /* whitespace and order is relevant, since we hand out a digest of this */
106 s = apr_psprintf(p, "{\"e\":\"%s\",\"kty\":\"RSA\",\"n\":\"%s\"}", e64, n64);
109 return rv;
110}
111
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;
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);
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));
133 md_json_sets(prot64, msg, "protected", NULL);
134
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
145cleanup:
146 *pmsg = (APR_SUCCESS == rv)? msg : NULL;
147 return rv;
148}
APR-UTIL Buckets/Bucket Brigades.
APR general purpose library routines.
APR Strings library.
APR Table library.
#define APR_EINVAL
Definition apr_errno.h:711
apr_datum_t * pkey
Definition apr_dbm.h:158
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
void * data
void const char apr_status_t(* cleanup)(void *))
const char * s
Definition apr_strings.h:95
const char * md_pkey_get_rsa_e64(md_pkey_t *pkey, apr_pool_t *p)
Definition md_crypt.c:979
const char * md_pkey_get_rsa_n64(md_pkey_t *pkey, apr_pool_t *p)
Definition md_crypt.c:999
apr_status_t md_crypt_sign64(const char **psign64, md_pkey_t *pkey, apr_pool_t *p, const char *d, size_t dlen)
Definition md_crypt.c:1019
apr_status_t md_crypt_sha256_digest64(const char **pdigest64, apr_pool_t *p, const md_data_t *d)
Definition md_crypt.c:1088
apr_status_t md_crypt_hmac64(const char **pmac64, const md_data_t *hmac_key, apr_pool_t *p, const char *d, size_t dlen)
Definition md_crypt.c:1116
apr_pool_t * p
Definition md_event.c:32
md_json_t * md_json_create(apr_pool_t *pool)
Definition md_json.c:92
apr_status_t md_json_sets(const char *value, md_json_t *json,...)
Definition md_json.c:430
md_json_t * md_json_clone(apr_pool_t *pool, const md_json_t *json)
Definition md_json.c:116
apr_status_t md_json_setj(const md_json_t *value, md_json_t *json,...)
Definition md_json.c:527
const char * md_json_writep(const md_json_t *json, apr_pool_t *p, md_json_fmt_t fmt)
Definition md_json.c:992
@ MD_JSON_FMT_COMPACT
Definition md_json.h:43
apr_status_t md_jws_hmac(md_json_t **pmsg, apr_pool_t *p, md_data_t *payload, md_json_t *prot_fields, const md_data_t *hmac_key)
Definition md_jws.c:112
apr_status_t md_jws_sign(md_json_t **pmsg, apr_pool_t *p, md_data_t *payload, md_json_t *prot_fields, struct md_pkey_t *pkey, const char *key_id)
Definition md_jws.c:42
apr_status_t md_jws_get_jwk(md_json_t **pjwk, apr_pool_t *p, struct md_pkey_t *pkey)
Definition md_jws.c:28
apr_status_t md_jws_pkey_thumb(const char **pthumb, apr_pool_t *p, struct md_pkey_t *pkey)
Definition md_jws.c:93
void md_log_perror(const char *file, int line, md_log_level_t level, apr_status_t rv, apr_pool_t *p, const char *fmt,...)
Definition md_log.c:68
#define MD_LOG_MARK
Definition md_log.h:39
@ MD_LOG_WARNING
Definition md_log.h:25
void md_data_init_str(md_data_t *d, const char *str)
Definition md_util.c:102
void md_data_init(md_data_t *d, const char *data, apr_size_t len)
Definition md_util.c:95
const char * md_util_base64url_encode(const md_data_t *data, apr_pool_t *pool)
Definition md_util.c:1207
return NULL
Definition mod_so.c:359