summaryrefslogtreecommitdiff
path: root/src/jws/libwebidoidc-jws.c
blob: a35b854a498e77738000a0711f6b487bb3f4b038 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
  webid-oidc, implementation of the Solid specification
  Copyright (C) 2020, 2021  Vivien Kraus

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as
  published by the Free Software Foundation, either version 3 of the
  License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include <utilities.h>

#define _(s) dgettext (PACKAGE, s)

SCM_SYMBOL (rs256, "RS256");
SCM_SYMBOL (rs512, "RS512");
SCM_SYMBOL (es256, "ES256");
SCM_SYMBOL (es384, "ES384");
SCM_SYMBOL (es512, "ES512");
SCM_SYMBOL (ps256, "PS256");
SCM_SYMBOL (ps384, "PS384");
SCM_SYMBOL (ps512, "PS512");

SCM_SYMBOL (sha256, "SHA-256");
SCM_SYMBOL (sha384, "SHA-384");
SCM_SYMBOL (sha512, "SHA-512");

SCM_SYMBOL (kty_ec, "EC");
SCM_SYMBOL (kty_rsa, "RSA");

SCM_SYMBOL (incompatible_alg, "incompatible-alg");

/* Required for utilities.h */
SCM_SYMBOL (p256, "P-256");
SCM_SYMBOL (p384, "P-384");
SCM_SYMBOL (p521, "P-521");
SCM_SYMBOL (kcrv, "crv");
SCM_SYMBOL (kx, "x");
SCM_SYMBOL (ky, "y");
SCM_SYMBOL (kd, "d");
SCM_SYMBOL (kn, "n");
SCM_SYMBOL (ke, "e");
SCM_SYMBOL (kp, "p");
SCM_SYMBOL (kq, "q");
SCM_SYMBOL (kdp, "dp");
SCM_SYMBOL (kdq, "dq");
SCM_SYMBOL (kqi, "qi");

void webid_oidc_random (size_t len, uint8_t * dst);
SCM webidoidc_hash_g (SCM alg, SCM payload);
SCM webidoidc_kty_g (SCM key);

static void
generate_random (void *unused, size_t len, uint8_t * dst)
{
  (void) unused;
  webid_oidc_random (len, dst);
}

static void
do_dsa_signature_clear (void *ptr)
{
  struct dsa_signature *sig = ptr;
  dsa_signature_clear (sig);
}

static void
dynwind_dsa_signature_clear (struct dsa_signature *sig)
{
  scm_dynwind_unwind_handler (do_dsa_signature_clear, sig,
			      SCM_F_WIND_EXPLICITLY);
}

SCM_DEFINE (webidoidc_jws_sign_g, "sign", 3, 0, 0,
	    (SCM alg, SCM key, SCM payload),
	    "Sign @var{payload} with @var{key}, using the given algorithm @var{alg}. If @var{alg} is not @code{'RS256}, @code{'RS512}, @code{'ES256}, @code{'ES384}, @code{'ES512}, @code{'PS256}, @code{'PS384}, or @code{'PS512}, or is not compatible with the key type, throw an error.")
{
  SCM digest;
  size_t c_digest_size;
  uint8_t *c_digest;
  if (scm_is_eq (alg, rs256)
      || scm_is_eq (alg, es256) || scm_is_eq (alg, ps256))
    {
      digest = webidoidc_hash_g (sha256, payload);
    }
  else if (scm_is_eq (alg, es384) || scm_is_eq (alg, ps384))
    {
      digest = webidoidc_hash_g (sha384, payload);
    }
  else if (scm_is_eq (alg, rs512)
	   || scm_is_eq (alg, es512) || scm_is_eq (alg, ps512))
    {
      digest = webidoidc_hash_g (sha512, payload);
    }
  else
    {
      scm_throw (incompatible_alg, scm_list_2 (alg, key));
    }
  c_digest = get_as_bytevector (digest, &c_digest_size, 1);
  if (scm_is_eq (alg, es256)
      || scm_is_eq (alg, es384) || scm_is_eq (alg, es512))
    {
      struct ecc_scalar c_key;
      SCM crv = scm_assq_ref (key, kcrv);
      const struct ecc_curve *c_crv = do_ecc_curve_load (crv, 1);
      struct dsa_signature sig;
      SCM r, s;
      uint8_t *c_r, *c_s, *c_ret;
      size_t c_r_size, c_s_size;
      scm_dynwind_begin (0);
      ecc_scalar_init (&c_key, c_crv);
      dynwind_ecc_scalar_clear (&c_key);
      dsa_signature_init (&sig);
      dynwind_dsa_signature_clear (&sig);
      if (!do_ecc_scalar_load (&c_key, key))
	{
	  scm_throw (incompatible_alg, scm_list_2 (alg, key));
	}
      ecdsa_sign (&c_key, NULL, &generate_random, c_digest_size, c_digest,
		  &sig);
      r = wrap_mpz_t (sig.r);
      s = wrap_mpz_t (sig.s);
      c_r = get_as_bytevector (r, &c_r_size, 1);
      c_s = get_as_bytevector (s, &c_s_size, 1);
      scm_dynwind_end ();
      c_ret = scm_gc_malloc_pointerless (c_r_size + c_s_size, "signature");
      memcpy (c_ret, c_r, c_r_size);
      memcpy (c_ret + c_r_size, c_s, c_s_size);
      return wrap_bytevector (c_r_size + c_s_size, c_ret);
    }
  else
    {
      struct rsa_public_key c_pub;
      struct rsa_private_key c_key;
      mpz_t c_sig;
      SCM ret;
      scm_dynwind_begin (0);
      rsa_public_key_init (&c_pub);
      dynwind_rsa_public_key_clear (&c_pub);
      rsa_private_key_init (&c_key);
      dynwind_rsa_private_key_clear (&c_key);
      if (!do_rsa_public_key_load (&c_pub, key)
	  || !do_rsa_private_key_load (&c_key, key))
	{
	  scm_throw (incompatible_alg, scm_list_2 (alg, key));
	}
      mpz_init (c_sig);
      dynwind_mpz_t_clear (&c_sig);
      if (scm_is_eq (alg, rs256)
	  && rsa_sha256_sign_digest_tr (&c_pub, &c_key, NULL,
					&generate_random, c_digest, c_sig))
	{
	  ret = wrap_mpz_t (c_sig);
	}
      else if (scm_is_eq (alg, rs512)
	       && rsa_sha512_sign_digest_tr (&c_pub, &c_key, NULL,
					     &generate_random, c_digest,
					     c_sig))
	{
	  ret = wrap_mpz_t (c_sig);
	}
      else if (scm_is_eq (alg, ps256))
	{
	  uint8_t padding[SHA256_DIGEST_SIZE];
	  webid_oidc_random (SHA256_DIGEST_SIZE, padding);
	  if (rsa_pss_sha256_sign_digest_tr
	      (&c_pub, &c_key, NULL, &generate_random, SHA256_DIGEST_SIZE,
	       padding, c_digest, c_sig))
	    {
	      ret = wrap_mpz_t (c_sig);
	    }
	  else
	    {
	      scm_wrong_type_arg ("sign", SCM_ARG2, key);
	    }
	}
      else if (scm_is_eq (alg, ps384))
	{
	  uint8_t padding[SHA384_DIGEST_SIZE];
	  webid_oidc_random (SHA384_DIGEST_SIZE, padding);
	  if (rsa_pss_sha384_sign_digest_tr
	      (&c_pub, &c_key, NULL, &generate_random, SHA384_DIGEST_SIZE,
	       padding, c_digest, c_sig))
	    {
	      ret = wrap_mpz_t (c_sig);
	    }
	  else
	    {
	      scm_wrong_type_arg ("sign", SCM_ARG2, key);
	    }
	}
      else if (scm_is_eq (alg, ps512))
	{
	  uint8_t padding[SHA512_DIGEST_SIZE];
	  webid_oidc_random (SHA512_DIGEST_SIZE, padding);
	  if (rsa_pss_sha512_sign_digest_tr
	      (&c_pub, &c_key, NULL, &generate_random, SHA512_DIGEST_SIZE,
	       padding, c_digest, c_sig))
	    {
	      ret = wrap_mpz_t (c_sig);
	    }
	  else
	    {
	      scm_wrong_type_arg ("sign", SCM_ARG2, key);
	    }
	}
      else
	{
	  scm_throw (incompatible_alg, scm_list_2 (alg, key));
	}
      scm_dynwind_end ();
      return ret;
    }
  return SCM_UNDEFINED;
}

SCM_DEFINE (webidoidc_jws_verify_g, "verify", 4, 0, 0,
	    (SCM alg, SCM key, SCM payload, SCM signature),
	    "Verify that @var{signature} is the signature of @var{payload} with @var{key}, using the given algorithm @var{alg}. If @var{alg} is not @code{'RS256}, @code{'RS512}, @code{'ES256}, @code{'ES384}, @code{'ES512}, @code{'PS256}, @code{'PS384}, or @code{'PS512}, or @var{alg} cannot be used with @var{key}, throw @code{incompatible-alg}.")
{
  SCM digest;
  size_t c_digest_size;
  uint8_t *c_digest;
  if (scm_is_eq (alg, rs256)
      || scm_is_eq (alg, es256) || scm_is_eq (alg, ps256))
    {
      digest = webidoidc_hash_g (sha256, payload);
    }
  else if (scm_is_eq (alg, es384) || scm_is_eq (alg, ps384))
    {
      digest = webidoidc_hash_g (sha384, payload);
    }
  else if (scm_is_eq (alg, rs512)
	   || scm_is_eq (alg, es512) || scm_is_eq (alg, ps512))
    {
      digest = webidoidc_hash_g (sha512, payload);
    }
  else
    {
      scm_throw (incompatible_alg, scm_list_2 (alg, key));
    }
  c_digest = get_as_bytevector (digest, &c_digest_size, 1);
  if (scm_is_eq (alg, es256) || scm_is_eq (alg, es384)
      || scm_is_eq (alg, es512))
    {
      struct ecc_point c_pub;
      SCM crv = scm_assq_ref (key, kcrv);
      const struct ecc_curve *c_crv = do_ecc_curve_load (crv, 1);
      struct dsa_signature sig;
      size_t c_sig_size;
      uint8_t *c_sig;
      SCM r, s;
      int ok = 0;
      c_sig = get_as_bytevector (signature, &c_sig_size, 1);
      r = wrap_bytevector (c_sig_size / 2, c_sig);
      s =
	wrap_bytevector (c_sig_size - c_sig_size / 2,
			 c_sig + (c_sig_size / 2));
      scm_dynwind_begin (0);
      ecc_point_init (&c_pub, c_crv);
      dynwind_ecc_point_clear (&c_pub);
      dsa_signature_init (&sig);
      dynwind_dsa_signature_clear (&sig);
      if (!do_ecc_point_load (&c_pub, key))
	{
	  scm_throw (incompatible_alg, scm_list_2 (alg, key));
	}
      do_mpz_t_load (sig.r, r, 1);
      do_mpz_t_load (sig.s, s, 1);
      ok = ecdsa_verify (&c_pub, c_digest_size, c_digest, &sig);
      scm_dynwind_end ();
      return scm_from_bool (ok);
    }
  else
    {
      struct rsa_public_key c_pub;
      mpz_t c_sig;
      int ok = 0;
      scm_dynwind_begin (0);
      rsa_public_key_init (&c_pub);
      dynwind_rsa_public_key_clear (&c_pub);
      if (!do_rsa_public_key_load (&c_pub, key))
	{
	  scm_throw (incompatible_alg, scm_list_2 (alg, key));
	}
      mpz_init (c_sig);
      dynwind_mpz_t_clear (&c_sig);
      do_mpz_t_load (c_sig, signature, 1);
      if (scm_is_eq (alg, rs256))
	{
	  ok = rsa_sha256_verify_digest (&c_pub, c_digest, c_sig);
	}
      else if (scm_is_eq (alg, rs512))
	{
	  ok = rsa_sha512_verify_digest (&c_pub, c_digest, c_sig);
	}
      else if (scm_is_eq (alg, ps256))
	{
	  ok =
	    rsa_pss_sha256_verify_digest (&c_pub, c_digest_size, c_digest,
					  c_sig);
	}
      else if (scm_is_eq (alg, ps384))
	{
	  ok =
	    rsa_pss_sha384_verify_digest (&c_pub, c_digest_size, c_digest,
					  c_sig);
	}
      else if (scm_is_eq (alg, ps512))
	{
	  ok =
	    rsa_pss_sha512_verify_digest (&c_pub, c_digest_size, c_digest,
					  c_sig);
	}
      else
	{
	  scm_throw (incompatible_alg, scm_list_2 (alg, key));
	}
      scm_dynwind_end ();
      return scm_from_bool (ok);
    }
}

void
init_webidoidc_jws (void)
{
#ifndef SCM_MAGIC_SNARFER
#include "libwebidoidc-jws.x"
#endif /* not SCM_MAGIC_SNARFER */
}