texinfo documentation is similar to the printed manual.
[gnutls.git] / src / certtool-cfg.c
blobd47ef764ef88237af4998884f4e278261e6f9539
1 /*
2 * Copyright (C) 2004-2012 Free Software Foundation, Inc.
4 * This file is part of GnuTLS.
6 * GnuTLS is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuTLS is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
20 * Written by Nikos Mavrogiannopoulos <nmav@gnutls.org>.
23 #include <config.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <certtool-cfg.h>
28 #include <gnutls/x509.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <inttypes.h>
32 #include <time.h>
33 #include <autoopts/options.h>
35 /* for inet_pton */
36 #include <sys/types.h>
38 #if HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
40 #elif HAVE_WS2TCPIP_H
41 # include <ws2tcpip.h>
42 #endif
43 #include <arpa/inet.h>
45 /* Gnulib portability files. */
46 #include <getpass.h>
47 #include "certtool-common.h"
49 extern int batch;
51 #define MAX_ENTRIES 16
53 typedef struct _cfg_ctx
55 char *organization;
56 char *unit;
57 char *locality;
58 char *state;
59 char *cn;
60 char *uid;
61 char *challenge_password;
62 char *pkcs9_email;
63 char *country;
64 char **dc;
65 char **dns_name;
66 char **ip_addr;
67 char **email;
68 char **dn_oid;
69 char *crl_dist_points;
70 char *password;
71 char *pkcs12_key_name;
72 int serial;
73 int expiration_days;
74 int ca;
75 int path_len;
76 int tls_www_client;
77 int tls_www_server;
78 int signing_key;
79 int encryption_key;
80 int cert_sign_key;
81 int crl_sign_key;
82 int code_sign_key;
83 int ocsp_sign_key;
84 int time_stamping_key;
85 int ipsec_ike_key;
86 char **key_purpose_oids;
87 int crl_next_update;
88 int crl_number;
89 int crq_extensions;
90 char *proxy_policy_language;
91 char **ocsp_uris;
92 char **ca_issuers_uris;
93 } cfg_ctx;
95 cfg_ctx cfg;
97 void
98 cfg_init (void)
100 memset (&cfg, 0, sizeof (cfg));
101 cfg.path_len = -1;
102 cfg.serial = -1;
105 #define READ_MULTI_LINE(name, s_name) \
106 val = optionGetValue(pov, name); \
107 if (val != NULL && val->valType == OPARG_TYPE_STRING) \
109 if (s_name == NULL) { \
110 i = 0; \
111 s_name = malloc(sizeof(char*)*MAX_ENTRIES); \
112 do { \
113 if (val && !strcmp(val->pzName, name)==0) \
114 continue; \
115 s_name[i] = strdup(val->v.strVal); \
116 i++; \
117 if (i>=MAX_ENTRIES) \
118 break; \
119 } while((val = optionNextValue(pov, val)) != NULL); \
120 s_name[i] = NULL; \
124 #define READ_MULTI_LINE_TOKENIZED(name, s_name) \
125 val = optionGetValue(pov, name); \
126 if (val != NULL && val->valType == OPARG_TYPE_STRING) \
128 char str[512]; \
129 char * p; \
130 if (s_name == NULL) { \
131 i = 0; \
132 s_name = malloc(sizeof(char*)*MAX_ENTRIES); \
133 do { \
134 if (val && !strcmp(val->pzName, name)==0) \
135 continue; \
136 strncpy(str, val->v.strVal, sizeof(str)-1); \
137 str[sizeof(str)-1] = 0; \
138 if ((p=strchr(str, ' ')) == NULL && (p=strchr(str, '\t')) == NULL) { \
139 fprintf(stderr, "Error parsing %s\n", name); \
140 exit(1); \
142 p[0] = 0; \
143 p++; \
144 s_name[i] = strdup(str); \
145 while(*p==' ' || *p == '\t') p++; \
146 if (p[0] == 0) { \
147 fprintf(stderr, "Error (2) parsing %s\n", name); \
148 exit(1); \
150 s_name[i+1] = strdup(p); \
151 i+=2; \
152 if (i>=MAX_ENTRIES) \
153 break; \
154 } while((val = optionNextValue(pov, val)) != NULL); \
155 s_name[i] = NULL; \
159 #define READ_BOOLEAN(name, s_name) \
160 val = optionGetValue(pov, name); \
161 if (val != NULL) \
163 s_name = 1; \
166 #define READ_NUMERIC(name, s_name) \
167 val = optionGetValue(pov, name); \
168 if (val != NULL) \
170 if (val->valType == OPARG_TYPE_NUMERIC) \
171 s_name = val->v.longVal; \
172 else if (val->valType == OPARG_TYPE_STRING) \
173 s_name = atoi(val->v.strVal); \
177 template_parse (const char *template)
179 /* Parsing return code */
180 int ret;
181 unsigned int i;
182 tOptionValue const * pov;
183 const tOptionValue* val;
185 pov = configFileLoad(template);
186 if (pov == NULL)
188 perror("configFileLoad");
189 fprintf(stderr, "Error loading template: %s\n", template);
190 exit(1);
193 /* Option variables */
194 val = optionGetValue(pov, "organization");
195 if (val != NULL && val->valType == OPARG_TYPE_STRING)
196 cfg.organization = strdup(val->v.strVal);
198 val = optionGetValue(pov, "unit");
199 if (val != NULL && val->valType == OPARG_TYPE_STRING)
200 cfg.unit = strdup(val->v.strVal);
202 val = optionGetValue(pov, "locality");
203 if (val != NULL && val->valType == OPARG_TYPE_STRING)
204 cfg.locality = strdup(val->v.strVal);
206 val = optionGetValue(pov, "state");
207 if (val != NULL && val->valType == OPARG_TYPE_STRING)
208 cfg.state = strdup(val->v.strVal);
210 val = optionGetValue(pov, "cn");
211 if (val != NULL && val->valType == OPARG_TYPE_STRING)
212 cfg.cn = strdup(val->v.strVal);
214 val = optionGetValue(pov, "uid");
215 if (val != NULL && val->valType == OPARG_TYPE_STRING)
216 cfg.uid = strdup(val->v.strVal);
218 val = optionGetValue(pov, "challenge_password");
219 if (val != NULL && val->valType == OPARG_TYPE_STRING)
220 cfg.challenge_password = strdup(val->v.strVal);
222 val = optionGetValue(pov, "password");
223 if (val != NULL && val->valType == OPARG_TYPE_STRING)
224 cfg.password = strdup(val->v.strVal);
226 val = optionGetValue(pov, "pkcs9_email");
227 if (val != NULL && val->valType == OPARG_TYPE_STRING)
228 cfg.pkcs9_email = strdup(val->v.strVal);
230 val = optionGetValue(pov, "country");
231 if (val != NULL && val->valType == OPARG_TYPE_STRING)
232 cfg.country = strdup(val->v.strVal);
234 READ_MULTI_LINE("dc", cfg.dc);
235 READ_MULTI_LINE("dns_name", cfg.dns_name);
236 READ_MULTI_LINE("ip_address", cfg.ip_addr);
237 READ_MULTI_LINE("email", cfg.email);
238 READ_MULTI_LINE("key_purpose_oid", cfg.key_purpose_oids);
240 READ_MULTI_LINE_TOKENIZED("dn_oid", cfg.dn_oid);
242 val = optionGetValue(pov, "crl_dist_points");
243 if (val != NULL && val->valType == OPARG_TYPE_STRING)
244 cfg.crl_dist_points = strdup(val->v.strVal);
246 val = optionGetValue(pov, "pkcs12_key_name");
247 if (val != NULL && val->valType == OPARG_TYPE_STRING)
248 cfg.pkcs12_key_name = strdup(val->v.strVal);
251 READ_NUMERIC("serial", cfg.serial);
252 READ_NUMERIC("expiration_days", cfg.expiration_days);
253 READ_NUMERIC("crl_next_update", cfg.crl_next_update);
254 READ_NUMERIC("crl_number", cfg.crl_number);
256 val = optionGetValue(pov, "proxy_policy_language");
257 if (val != NULL && val->valType == OPARG_TYPE_STRING)
258 cfg.proxy_policy_language = strdup(val->v.strVal);
260 READ_MULTI_LINE("ocsp_uri", cfg.ocsp_uris);
261 READ_MULTI_LINE("ca_issuers_uri", cfg.ca_issuers_uris);
263 READ_BOOLEAN("ca", cfg.ca);
264 READ_BOOLEAN("honor_crq_extensions", cfg.crq_extensions);
265 READ_BOOLEAN("path_len", cfg.path_len);
266 READ_BOOLEAN("tls_www_client", cfg.tls_www_client);
267 READ_BOOLEAN("tls_www_server", cfg.tls_www_server);
268 READ_BOOLEAN("signing_key", cfg.signing_key);
269 READ_BOOLEAN("encryption_key", cfg.encryption_key);
270 READ_BOOLEAN("cert_signing_key", cfg.cert_sign_key);
271 READ_BOOLEAN("crl_signing_key", cfg.crl_sign_key);
272 READ_BOOLEAN("code_signing_key", cfg.code_sign_key);
273 READ_BOOLEAN("ocsp_signing_key", cfg.ocsp_sign_key);
274 READ_BOOLEAN("time_stamping_key", cfg.time_stamping_key);
275 READ_BOOLEAN("ipsec_ike_key", cfg.ipsec_ike_key);
277 optionUnloadNested(pov);
279 return 0;
282 #define IS_NEWLINE(x) ((x[0] == '\n') || (x[0] == '\r'))
284 void
285 read_crt_set (gnutls_x509_crt_t crt, const char *input_str, const char *oid)
287 char input[128];
288 int ret;
290 fputs (input_str, stderr);
291 if (fgets (input, sizeof (input), stdin) == NULL)
292 return;
294 if (IS_NEWLINE(input))
295 return;
297 ret =
298 gnutls_x509_crt_set_dn_by_oid (crt, oid, 0, input, strlen (input) - 1);
299 if (ret < 0)
301 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
302 exit (1);
306 void
307 read_crq_set (gnutls_x509_crq_t crq, const char *input_str, const char *oid)
309 char input[128];
310 int ret;
312 fputs (input_str, stderr);
313 if (fgets (input, sizeof (input), stdin) == NULL)
314 return;
316 if (IS_NEWLINE(input))
317 return;
319 ret =
320 gnutls_x509_crq_set_dn_by_oid (crq, oid, 0, input, strlen (input) - 1);
321 if (ret < 0)
323 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
324 exit (1);
328 /* The input_str should contain %d or %u to print the default.
330 static int
331 read_int_with_default (const char *input_str, int def)
333 char *endptr;
334 long l, len;
335 static char input[128];
337 fprintf (stderr, input_str, def);
338 if (fgets (input, sizeof (input), stdin) == NULL)
339 return def;
341 if (IS_NEWLINE(input))
342 return def;
344 len = strlen (input);
346 l = strtol (input, &endptr, 0);
348 if (*endptr != '\0' && *endptr != '\r' && *endptr != '\n')
350 fprintf (stderr, "Trailing garbage ignored: `%s'\n", endptr);
351 return 0;
354 if (l <= INT_MIN || l >= INT_MAX)
356 fprintf (stderr, "Integer out of range: `%s'\n", input);
357 return 0;
360 if (input == endptr)
361 l = def;
363 return (int) l;
367 read_int (const char *input_str)
369 return read_int_with_default (input_str, 0);
372 const char *
373 read_str (const char *input_str)
375 static char input[128];
376 int len;
378 fputs (input_str, stderr);
379 if (fgets (input, sizeof (input), stdin) == NULL)
380 return NULL;
382 if (IS_NEWLINE(input))
383 return NULL;
385 len = strlen (input);
386 if ((len > 0) && (input[len - 1] == '\n'))
387 input[len - 1] = 0;
388 if (input[0] == 0)
389 return NULL;
391 return input;
394 /* Default is no
397 read_yesno (const char *input_str)
399 char input[128];
401 fputs (input_str, stderr);
402 if (fgets (input, sizeof (input), stdin) == NULL)
403 return 0;
405 if (IS_NEWLINE(input))
406 return 0;
408 if (input[0] == 'y' || input[0] == 'Y')
409 return 1;
411 return 0;
415 /* Wrapper functions for non-interactive mode.
417 const char *
418 get_pass (void)
420 if (batch)
421 return cfg.password;
422 else
423 return getpass ("Enter password: ");
426 const char *
427 get_confirmed_pass (bool empty_ok)
429 if (batch)
430 return cfg.password;
431 else
433 const char *pass = NULL;
434 char *copy = NULL;
438 if (pass)
439 printf ("Password missmatch, try again.\n");
441 free (copy);
443 pass = getpass ("Enter password: ");
444 copy = strdup (pass);
445 pass = getpass ("Confirm password: ");
447 while (strcmp (pass, copy) != 0 && !(empty_ok && *pass == '\0'));
449 free (copy);
451 return pass;
455 const char *
456 get_challenge_pass (void)
458 if (batch)
459 return cfg.challenge_password;
460 else
461 return getpass ("Enter a challenge password: ");
464 const char *
465 get_crl_dist_point_url (void)
467 if (batch)
468 return cfg.crl_dist_points;
469 else
470 return read_str ("Enter the URI of the CRL distribution point: ");
473 void
474 get_country_crt_set (gnutls_x509_crt_t crt)
476 int ret;
478 if (batch)
480 if (!cfg.country)
481 return;
482 ret =
483 gnutls_x509_crt_set_dn_by_oid (crt,
484 GNUTLS_OID_X520_COUNTRY_NAME, 0,
485 cfg.country, strlen (cfg.country));
486 if (ret < 0)
488 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
489 exit (1);
492 else
494 read_crt_set (crt, "Country name (2 chars): ",
495 GNUTLS_OID_X520_COUNTRY_NAME);
500 void
501 get_organization_crt_set (gnutls_x509_crt_t crt)
503 int ret;
505 if (batch)
507 if (!cfg.organization)
508 return;
510 ret =
511 gnutls_x509_crt_set_dn_by_oid (crt,
512 GNUTLS_OID_X520_ORGANIZATION_NAME,
513 0, cfg.organization,
514 strlen (cfg.organization));
515 if (ret < 0)
517 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
518 exit (1);
521 else
523 read_crt_set (crt, "Organization name: ",
524 GNUTLS_OID_X520_ORGANIZATION_NAME);
529 void
530 get_unit_crt_set (gnutls_x509_crt_t crt)
532 int ret;
534 if (batch)
536 if (!cfg.unit)
537 return;
539 ret =
540 gnutls_x509_crt_set_dn_by_oid (crt,
541 GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME,
542 0, cfg.unit, strlen (cfg.unit));
543 if (ret < 0)
545 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
546 exit (1);
549 else
551 read_crt_set (crt, "Organizational unit name: ",
552 GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME);
557 void
558 get_state_crt_set (gnutls_x509_crt_t crt)
560 int ret;
562 if (batch)
564 if (!cfg.state)
565 return;
566 ret =
567 gnutls_x509_crt_set_dn_by_oid (crt,
568 GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME,
569 0, cfg.state, strlen (cfg.state));
570 if (ret < 0)
572 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
573 exit (1);
576 else
578 read_crt_set (crt, "State or province name: ",
579 GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME);
584 void
585 get_locality_crt_set (gnutls_x509_crt_t crt)
587 int ret;
589 if (batch)
591 if (!cfg.locality)
592 return;
593 ret =
594 gnutls_x509_crt_set_dn_by_oid (crt,
595 GNUTLS_OID_X520_LOCALITY_NAME, 0,
596 cfg.locality, strlen (cfg.locality));
597 if (ret < 0)
599 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
600 exit (1);
603 else
605 read_crt_set (crt, "Locality name: ", GNUTLS_OID_X520_LOCALITY_NAME);
610 void
611 get_cn_crt_set (gnutls_x509_crt_t crt)
613 int ret;
615 if (batch)
617 if (!cfg.cn)
618 return;
619 ret =
620 gnutls_x509_crt_set_dn_by_oid (crt, GNUTLS_OID_X520_COMMON_NAME,
621 0, cfg.cn, strlen (cfg.cn));
622 if (ret < 0)
624 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
625 exit (1);
628 else
630 read_crt_set (crt, "Common name: ", GNUTLS_OID_X520_COMMON_NAME);
635 void
636 get_uid_crt_set (gnutls_x509_crt_t crt)
638 int ret;
640 if (batch)
642 if (!cfg.uid)
643 return;
644 ret = gnutls_x509_crt_set_dn_by_oid (crt, GNUTLS_OID_LDAP_UID, 0,
645 cfg.uid, strlen (cfg.uid));
646 if (ret < 0)
648 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
649 exit (1);
652 else
654 read_crt_set (crt, "UID: ", GNUTLS_OID_LDAP_UID);
659 void
660 get_oid_crt_set (gnutls_x509_crt_t crt)
662 int ret, i;
664 if (batch)
666 if (!cfg.dn_oid)
667 return;
668 for (i = 0; cfg.dn_oid[i] != NULL; i += 2)
670 if (cfg.dn_oid[i + 1] == NULL)
672 fprintf (stderr, "dn_oid: %s does not have an argument.\n",
673 cfg.dn_oid[i]);
674 exit (1);
676 ret = gnutls_x509_crt_set_dn_by_oid (crt, cfg.dn_oid[i], 0,
677 cfg.dn_oid[i + 1],
678 strlen (cfg.dn_oid[i + 1]));
680 if (ret < 0)
682 fprintf (stderr, "set_dn_oid: %s\n", gnutls_strerror (ret));
683 exit (1);
689 void
690 get_key_purpose_set (gnutls_x509_crt_t crt)
692 int ret, i;
694 if (batch)
696 if (!cfg.key_purpose_oids)
697 return;
698 for (i = 0; cfg.key_purpose_oids[i] != NULL; i++)
700 ret =
701 gnutls_x509_crt_set_key_purpose_oid (crt, cfg.key_purpose_oids[i],
704 if (ret < 0)
706 fprintf (stderr, "set_key_purpose_oid (%s): %s\n",
707 cfg.key_purpose_oids[i], gnutls_strerror (ret));
708 exit (1);
714 void
715 get_ocsp_issuer_set (gnutls_x509_crt_t crt)
717 int ret, i;
718 gnutls_datum_t uri;
720 if (batch)
722 if (!cfg.ocsp_uris)
723 return;
724 for (i = 0; cfg.ocsp_uris[i] != NULL; i++)
726 uri.data = cfg.ocsp_uris[i];
727 uri.size = strlen(cfg.ocsp_uris[i]);
728 ret =
729 gnutls_x509_crt_set_authority_info_access (crt, GNUTLS_IA_OCSP_URI,
730 &uri);
731 if (ret < 0)
733 fprintf (stderr, "set OCSP URI (%s): %s\n",
734 cfg.ocsp_uris[i], gnutls_strerror (ret));
735 exit (1);
741 void
742 get_ca_issuers_set (gnutls_x509_crt_t crt)
744 int ret, i;
745 gnutls_datum_t uri;
747 if (batch)
749 if (!cfg.ca_issuers_uris)
750 return;
751 for (i = 0; cfg.ca_issuers_uris[i] != NULL; i++)
753 uri.data = cfg.ca_issuers_uris[i];
754 uri.size = strlen(cfg.ca_issuers_uris[i]);
755 ret =
756 gnutls_x509_crt_set_authority_info_access (crt, GNUTLS_IA_CAISSUERS_URI,
757 &uri);
758 if (ret < 0)
760 fprintf (stderr, "set CA ISSUERS URI (%s): %s\n",
761 cfg.ca_issuers_uris[i], gnutls_strerror (ret));
762 exit (1);
769 void
770 get_pkcs9_email_crt_set (gnutls_x509_crt_t crt)
772 int ret;
774 if (batch)
776 if (!cfg.pkcs9_email)
777 return;
778 ret = gnutls_x509_crt_set_dn_by_oid (crt, GNUTLS_OID_PKCS9_EMAIL, 0,
779 cfg.pkcs9_email,
780 strlen (cfg.pkcs9_email));
781 if (ret < 0)
783 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
784 exit (1);
787 else
789 read_crt_set (crt, "E-mail: ", GNUTLS_OID_PKCS9_EMAIL);
795 get_serial (void)
797 int default_serial = time (NULL);
799 if (batch)
801 if (cfg.serial < 0)
802 return default_serial;
803 return cfg.serial;
805 else
807 return read_int_with_default
808 ("Enter the certificate's serial number in decimal (default: %u): ",
809 default_serial);
814 get_days (void)
816 int days;
818 if (batch)
820 if (cfg.expiration_days <= 0)
821 return 365;
822 else
823 return cfg.expiration_days;
825 else
829 days = read_int ("The certificate will expire in (days): ");
831 while (days == 0);
832 return days;
837 get_ca_status (void)
839 if (batch)
841 return cfg.ca;
843 else
845 return
846 read_yesno ("Does the certificate belong to an authority? (y/N): ");
851 get_crq_extensions_status (void)
853 if (batch)
855 return cfg.crq_extensions;
857 else
859 return
860 read_yesno
861 ("Do you want to honour the extensions from the request? (y/N): ");
866 get_crl_number (void)
868 if (batch)
870 return cfg.crl_number;
872 else
874 return read_int_with_default ("CRL Number: ", 1);
879 get_path_len (void)
881 if (batch)
883 return cfg.path_len;
885 else
887 return read_int_with_default
888 ("Path length constraint (decimal, %d for no constraint): ", -1);
892 const char *
893 get_pkcs12_key_name (void)
895 const char *name;
897 if (batch)
899 if (!cfg.pkcs12_key_name)
900 return "Anonymous";
901 return cfg.pkcs12_key_name;
903 else
907 name = read_str ("Enter a name for the key: ");
909 while (name == NULL);
911 return name;
915 get_tls_client_status (void)
917 if (batch)
919 return cfg.tls_www_client;
921 else
923 return read_yesno ("Is this a TLS web client certificate? (y/N): ");
928 get_tls_server_status (void)
930 if (batch)
932 return cfg.tls_www_server;
934 else
936 return
937 read_yesno ("Is this also a TLS web server certificate? (y/N): ");
941 /* convert a printable IP to binary */
942 static int
943 string_to_ip (unsigned char *ip, const char *str)
945 int len = strlen (str);
946 int ret;
948 #if HAVE_IPV6
949 if (strchr (str, ':') != NULL || len > 16)
950 { /* IPv6 */
951 ret = inet_pton (AF_INET6, str, ip);
952 if (ret <= 0)
954 fprintf (stderr, "Error in IPv6 address %s\n", str);
955 exit (1);
958 /* To be done */
959 return 16;
961 else
962 #endif
963 { /* IPv4 */
964 ret = inet_pton (AF_INET, str, ip);
965 if (ret <= 0)
967 fprintf (stderr, "Error in IPv4 address %s\n", str);
968 exit (1);
971 return 4;
976 void
977 get_ip_addr_set (int type, void *crt)
979 int ret = 0, i;
980 unsigned char ip[16];
981 int len;
983 if (batch)
985 if (!cfg.ip_addr)
986 return;
988 for (i = 0; cfg.ip_addr[i] != NULL; i++)
990 len = string_to_ip (ip, cfg.ip_addr[i]);
991 if (len <= 0)
993 fprintf (stderr, "Error parsing address: %s\n", cfg.ip_addr[i]);
994 exit (1);
997 if (type == TYPE_CRT)
998 ret =
999 gnutls_x509_crt_set_subject_alt_name (crt, GNUTLS_SAN_IPADDRESS,
1000 ip, len,
1001 GNUTLS_FSAN_APPEND);
1002 else
1003 ret =
1004 gnutls_x509_crq_set_subject_alt_name (crt, GNUTLS_SAN_IPADDRESS,
1005 ip, len,
1006 GNUTLS_FSAN_APPEND);
1008 if (ret < 0)
1009 break;
1012 else
1014 const char *p;
1017 read_str ("Enter the IP address of the subject of the certificate: ");
1018 if (!p)
1019 return;
1021 len = string_to_ip (ip, p);
1022 if (len <= 0)
1024 fprintf (stderr, "Error parsing address: %s\n", p);
1025 exit (1);
1028 if (type == TYPE_CRT)
1029 ret = gnutls_x509_crt_set_subject_alt_name (crt, GNUTLS_SAN_IPADDRESS,
1030 ip, len,
1031 GNUTLS_FSAN_APPEND);
1032 else
1033 ret = gnutls_x509_crq_set_subject_alt_name (crt, GNUTLS_SAN_IPADDRESS,
1034 ip, len,
1035 GNUTLS_FSAN_APPEND);
1038 if (ret < 0)
1040 fprintf (stderr, "set_subject_alt_name: %s\n", gnutls_strerror (ret));
1041 exit (1);
1045 void
1046 get_email_set (int type, void *crt)
1048 int ret = 0, i;
1050 if (batch)
1052 if (!cfg.email)
1053 return;
1055 for (i = 0; cfg.email[i] != NULL; i++)
1057 if (type == TYPE_CRT)
1058 ret =
1059 gnutls_x509_crt_set_subject_alt_name (crt,
1060 GNUTLS_SAN_RFC822NAME,
1061 cfg.email[i],
1062 strlen (cfg.email[i]),
1063 GNUTLS_FSAN_APPEND);
1064 else
1065 ret =
1066 gnutls_x509_crq_set_subject_alt_name (crt,
1067 GNUTLS_SAN_RFC822NAME,
1068 cfg.email[i],
1069 strlen (cfg.email[i]),
1070 GNUTLS_FSAN_APPEND);
1072 if (ret < 0)
1073 break;
1076 else
1078 const char *p;
1080 p = read_str ("Enter the e-mail of the subject of the certificate: ");
1081 if (!p)
1082 return;
1084 if (type == TYPE_CRT)
1085 ret =
1086 gnutls_x509_crt_set_subject_alt_name (crt, GNUTLS_SAN_RFC822NAME, p,
1087 strlen (p),
1088 GNUTLS_FSAN_APPEND);
1089 else
1090 ret =
1091 gnutls_x509_crq_set_subject_alt_name (crt, GNUTLS_SAN_RFC822NAME, p,
1092 strlen (p),
1093 GNUTLS_FSAN_APPEND);
1096 if (ret < 0)
1098 fprintf (stderr, "set_subject_alt_name: %s\n", gnutls_strerror (ret));
1099 exit (1);
1104 void
1105 get_dc_set (int type, void *crt)
1107 int ret = 0, i;
1109 if (batch)
1111 if (!cfg.dc)
1112 return;
1114 for (i = 0; cfg.dc[i] != NULL; i++)
1116 if (type == TYPE_CRT)
1117 ret = gnutls_x509_crt_set_dn_by_oid (crt, GNUTLS_OID_LDAP_DC,
1118 0, cfg.dc[i], strlen (cfg.dc[i]));
1119 else
1120 ret = gnutls_x509_crq_set_dn_by_oid (crt, GNUTLS_OID_LDAP_DC,
1121 0, cfg.dc[i], strlen (cfg.dc[i]));
1123 if (ret < 0)
1124 break;
1127 else
1129 const char *p;
1133 p = read_str ("Enter the subject's domain component (DC): ");
1134 if (!p)
1135 return;
1137 if (type == TYPE_CRT)
1138 ret = gnutls_x509_crt_set_dn_by_oid (crt, GNUTLS_OID_LDAP_DC,
1139 0, p, strlen (p));
1140 else
1141 ret = gnutls_x509_crq_set_dn_by_oid (crt, GNUTLS_OID_LDAP_DC,
1142 0, p, strlen (p));
1144 while(p != NULL);
1147 if (ret < 0)
1149 fprintf (stderr, "set_dn_by_oid: %s\n", gnutls_strerror (ret));
1150 exit (1);
1154 void
1155 get_dns_name_set (int type, void *crt)
1157 int ret = 0, i;
1159 if (batch)
1161 if (!cfg.dns_name)
1162 return;
1164 for (i = 0; cfg.dns_name[i] != NULL; i++)
1166 if (type == TYPE_CRT)
1167 ret =
1168 gnutls_x509_crt_set_subject_alt_name (crt, GNUTLS_SAN_DNSNAME,
1169 cfg.dns_name[i],
1170 strlen (cfg.dns_name[i]),
1171 GNUTLS_FSAN_APPEND);
1172 else
1173 ret =
1174 gnutls_x509_crq_set_subject_alt_name (crt, GNUTLS_SAN_DNSNAME,
1175 cfg.dns_name[i],
1176 strlen (cfg.dns_name[i]),
1177 GNUTLS_FSAN_APPEND);
1179 if (ret < 0)
1180 break;
1183 else
1185 const char *p;
1190 read_str ("Enter a dnsName of the subject of the certificate: ");
1191 if (!p)
1192 return;
1194 if (type == TYPE_CRT)
1195 ret = gnutls_x509_crt_set_subject_alt_name
1196 (crt, GNUTLS_SAN_DNSNAME, p, strlen (p), GNUTLS_FSAN_APPEND);
1197 else
1198 ret = gnutls_x509_crq_set_subject_alt_name
1199 (crt, GNUTLS_SAN_DNSNAME, p, strlen (p), GNUTLS_FSAN_APPEND);
1201 while (p);
1204 if (ret < 0)
1206 fprintf (stderr, "set_subject_alt_name: %s\n", gnutls_strerror (ret));
1207 exit (1);
1213 get_sign_status (int server)
1215 const char *msg;
1217 if (batch)
1219 return cfg.signing_key;
1221 else
1223 if (server)
1224 msg =
1225 "Will the certificate be used for signing (DHE and RSA-EXPORT ciphersuites)? (y/N): ";
1226 else
1227 msg =
1228 "Will the certificate be used for signing (required for TLS)? (y/N): ";
1229 return read_yesno (msg);
1234 get_encrypt_status (int server)
1236 const char *msg;
1238 if (batch)
1240 return cfg.encryption_key;
1242 else
1244 if (server)
1245 msg =
1246 "Will the certificate be used for encryption (RSA ciphersuites)? (y/N): ";
1247 else
1248 msg =
1249 "Will the certificate be used for encryption (not required for TLS)? (y/N): ";
1250 return read_yesno (msg);
1255 get_cert_sign_status (void)
1257 if (batch)
1259 return cfg.cert_sign_key;
1261 else
1263 return
1264 read_yesno
1265 ("Will the certificate be used to sign other certificates? (y/N): ");
1270 get_crl_sign_status (void)
1272 if (batch)
1274 return cfg.crl_sign_key;
1276 else
1278 return
1279 read_yesno ("Will the certificate be used to sign CRLs? (y/N): ");
1284 get_code_sign_status (void)
1286 if (batch)
1288 return cfg.code_sign_key;
1290 else
1292 return
1293 read_yesno ("Will the certificate be used to sign code? (y/N): ");
1298 get_ocsp_sign_status (void)
1300 if (batch)
1302 return cfg.ocsp_sign_key;
1304 else
1306 return
1307 read_yesno
1308 ("Will the certificate be used to sign OCSP requests? (y/N): ");
1313 get_time_stamp_status (void)
1315 if (batch)
1317 return cfg.time_stamping_key;
1319 else
1321 return
1322 read_yesno
1323 ("Will the certificate be used for time stamping? (y/N): ");
1328 get_ipsec_ike_status (void)
1330 if (batch)
1332 return cfg.ipsec_ike_key;
1334 else
1336 return
1337 read_yesno
1338 ("Will the certificate be used for IPsec IKE operations? (y/N): ");
1343 get_crl_next_update (void)
1345 int days;
1347 if (batch)
1349 if (cfg.crl_next_update <= 0)
1350 return 365;
1351 else
1352 return cfg.crl_next_update;
1354 else
1358 days = read_int ("The next CRL will be issued in (days): ");
1360 while (days == 0);
1361 return days;
1365 const char *
1366 get_proxy_policy (char **policy, size_t * policylen)
1368 const char *ret;
1370 if (batch)
1372 ret = cfg.proxy_policy_language;
1373 if (!ret)
1374 ret = "1.3.6.1.5.5.7.21.1";
1376 else
1380 ret = read_str ("Enter the OID of the proxy policy language: ");
1382 while (ret == NULL);
1385 *policy = NULL;
1386 *policylen = 0;
1388 if (strcmp (ret, "1.3.6.1.5.5.7.21.1") != 0 &&
1389 strcmp (ret, "1.3.6.1.5.5.7.21.2") != 0)
1391 fprintf (stderr, "Reading non-standard proxy policy not supported.\n");
1394 return ret;
1397 /* CRQ stuff.
1399 void
1400 get_country_crq_set (gnutls_x509_crq_t crq)
1402 int ret;
1404 if (batch)
1406 if (!cfg.country)
1407 return;
1408 ret =
1409 gnutls_x509_crq_set_dn_by_oid (crq,
1410 GNUTLS_OID_X520_COUNTRY_NAME, 0,
1411 cfg.country, strlen (cfg.country));
1412 if (ret < 0)
1414 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
1415 exit (1);
1418 else
1420 read_crq_set (crq, "Country name (2 chars): ",
1421 GNUTLS_OID_X520_COUNTRY_NAME);
1426 void
1427 get_organization_crq_set (gnutls_x509_crq_t crq)
1429 int ret;
1431 if (batch)
1433 if (!cfg.organization)
1434 return;
1436 ret =
1437 gnutls_x509_crq_set_dn_by_oid (crq,
1438 GNUTLS_OID_X520_ORGANIZATION_NAME,
1439 0, cfg.organization,
1440 strlen (cfg.organization));
1441 if (ret < 0)
1443 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
1444 exit (1);
1447 else
1449 read_crq_set (crq, "Organization name: ",
1450 GNUTLS_OID_X520_ORGANIZATION_NAME);
1455 void
1456 get_unit_crq_set (gnutls_x509_crq_t crq)
1458 int ret;
1460 if (batch)
1462 if (!cfg.unit)
1463 return;
1465 ret =
1466 gnutls_x509_crq_set_dn_by_oid (crq,
1467 GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME,
1468 0, cfg.unit, strlen (cfg.unit));
1469 if (ret < 0)
1471 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
1472 exit (1);
1475 else
1477 read_crq_set (crq, "Organizational unit name: ",
1478 GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME);
1483 void
1484 get_state_crq_set (gnutls_x509_crq_t crq)
1486 int ret;
1488 if (batch)
1490 if (!cfg.state)
1491 return;
1492 ret =
1493 gnutls_x509_crq_set_dn_by_oid (crq,
1494 GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME,
1495 0, cfg.state, strlen (cfg.state));
1496 if (ret < 0)
1498 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
1499 exit (1);
1502 else
1504 read_crq_set (crq, "State or province name: ",
1505 GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME);
1510 void
1511 get_locality_crq_set (gnutls_x509_crq_t crq)
1513 int ret;
1515 if (batch)
1517 if (!cfg.locality)
1518 return;
1519 ret =
1520 gnutls_x509_crq_set_dn_by_oid (crq,
1521 GNUTLS_OID_X520_LOCALITY_NAME, 0,
1522 cfg.locality, strlen (cfg.locality));
1523 if (ret < 0)
1525 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
1526 exit (1);
1529 else
1531 read_crq_set (crq, "Locality name: ", GNUTLS_OID_X520_LOCALITY_NAME);
1536 void
1537 get_cn_crq_set (gnutls_x509_crq_t crq)
1539 int ret;
1541 if (batch)
1543 if (!cfg.cn)
1544 return;
1545 ret =
1546 gnutls_x509_crq_set_dn_by_oid (crq, GNUTLS_OID_X520_COMMON_NAME,
1547 0, cfg.cn, strlen (cfg.cn));
1548 if (ret < 0)
1550 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
1551 exit (1);
1554 else
1556 read_crq_set (crq, "Common name: ", GNUTLS_OID_X520_COMMON_NAME);
1561 void
1562 get_uid_crq_set (gnutls_x509_crq_t crq)
1564 int ret;
1566 if (batch)
1568 if (!cfg.uid)
1569 return;
1570 ret = gnutls_x509_crq_set_dn_by_oid (crq, GNUTLS_OID_LDAP_UID, 0,
1571 cfg.uid, strlen (cfg.uid));
1572 if (ret < 0)
1574 fprintf (stderr, "set_dn: %s\n", gnutls_strerror (ret));
1575 exit (1);
1578 else
1580 read_crq_set (crq, "UID: ", GNUTLS_OID_LDAP_UID);
1585 void
1586 get_oid_crq_set (gnutls_x509_crq_t crq)
1588 int ret, i;
1590 if (batch)
1592 if (!cfg.dn_oid)
1593 return;
1594 for (i = 0; cfg.dn_oid[i] != NULL; i += 2)
1596 if (cfg.dn_oid[i + 1] == NULL)
1598 fprintf (stderr, "dn_oid: %s does not have an argument.\n",
1599 cfg.dn_oid[i]);
1600 exit (1);
1602 ret = gnutls_x509_crq_set_dn_by_oid (crq, cfg.dn_oid[i], 0,
1603 cfg.dn_oid[i + 1],
1604 strlen (cfg.dn_oid[i + 1]));
1606 if (ret < 0)
1608 fprintf (stderr, "set_dn_oid: %s\n", gnutls_strerror (ret));
1609 exit (1);