summaryrefslogtreecommitdiff
path: root/src/jwk/generate-key.c
blob: 61e7089e2706acba3023a553b1c1859e2286b676 (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
/*
  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/>.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */

#include <stdio.h>
#include <stdlib.h>
#include <libguile.h>
#include <gettext.h>

#define _(s) gettext (s)

SCM webidoidc_generate_key_g (SCM args);
SCM webidoidc_strip_key_g (SCM key);
SCM webidoidc_jkt_g (SCM key);

extern int init_webidoidc (void);

static void
run (void *params, int argc, char *argv[])
{
  SCM data;
  char *end;
  size_t n_size = 0;
  (void) params;
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);
  if (argc != 2
      || (strcmp (argv[1], "P-256") != 0
	  && strcmp (argv[1], "P-384") != 0
	  && strcmp (argv[1], "P-521") != 0
	  && ((n_size = strtoull (argv[1], &end, 10)) == 0 || *end != '\0')))
    {
      fprintf (stderr, _("Usage: generate-key [NUMBER OF BITS | CURVE]\n"));
      exit (1);
    }
  init_webidoidc ();
  if (strcmp (argv[1], "P-256") == 0
      || strcmp (argv[1], "P-384") == 0 || strcmp (argv[1], "P-521") == 0)
    {
      data =
	webidoidc_generate_key_g (scm_list_2
				  (scm_from_utf8_keyword ("crv"),
				   scm_from_utf8_symbol (argv[1])));
    }
  else
    {
      data =
	webidoidc_generate_key_g (scm_list_2
				  (scm_from_utf8_keyword ("n-size"),
				   scm_from_size_t (n_size)));
    }
  scm_display (webidoidc_jkt_g (data), scm_current_error_port ());
  fprintf (stderr, "\n");
  scm_display (data, scm_current_output_port ());
}

int
main (int argc, char *argv[])
{
  scm_boot_guile (argc, argv, run, NULL);
  return 0;
}