2 * Copyright (C) 2005 Jan Hutter, Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * @brief scepclient main program
22 * @mainpage SCEP for Linux strongSwan
24 * Documentation of SCEP for Linux StrongSwan
41 #include <asn1/asn1.h>
43 #include <utils/optionsfrom.h>
44 #include <utils/enumerator.h>
46 #include "../pluto/constants.h"
47 #include "../pluto/defs.h"
48 #include "../pluto/log.h"
49 #include "../pluto/pkcs1.h"
50 #include "../pluto/pkcs7.h"
51 #include "../pluto/certs.h"
58 * definition of some defaults
61 /* default name of DER-encoded PKCS#1 private key file */
62 #define DEFAULT_FILENAME_PKCS1 "myKey.der"
64 /* default name of DER-encoded PKCS#10 certificate request file */
65 #define DEFAULT_FILENAME_PKCS10 "myReq.der"
67 /* default name of DER-encoded PKCS#7 file */
68 #define DEFAULT_FILENAME_PKCS7 "pkcs7.der"
70 /* default name of DER-encoded self-signed X.509 certificate file */
71 #define DEFAULT_FILENAME_CERT_SELF "selfCert.der"
73 /* default name of DER-encoded X.509 certificate file */
74 #define DEFAULT_FILENAME_CERT "myCert.der"
76 /* default name of DER-encoded CA cert file used for key encipherment */
77 #define DEFAULT_FILENAME_CACERT_ENC "caCert.der"
79 /* default name of the der encoded CA cert file used for signature verification */
80 #define DEFAULT_FILENAME_CACERT_SIG "caCert.der"
82 /* default prefix of the der encoded CA certificates received from the SCEP server */
83 #define DEFAULT_FILENAME_PREFIX_CACERT "caCert.der"
85 /* default certificate validity */
86 #define DEFAULT_CERT_VALIDITY 5 * 3600 * 24 * 365 /* seconds */
88 /* default polling time interval in SCEP manual mode */
89 #define DEFAULT_POLL_INTERVAL 20 /* seconds */
91 /* default key length for self-generated RSA keys */
92 #define DEFAULT_RSA_KEY_LENGTH 2048 /* bits */
94 /* default distinguished name */
95 #define DEFAULT_DN "C=CH, O=Linux strongSwan, CN="
97 /* challenge password buffer size */
98 #define MAX_PASSWORD_LENGTH 256
100 /* Max length of filename for tempfile */
101 #define MAX_TEMP_FILENAME_LENGTH 256
104 /* current scepclient version */
105 static const char *scepclient_version
= "1.0";
107 /* by default the CRL policy is lenient */
108 bool strict_crl_policy
= FALSE
;
110 /* by default pluto does not check crls dynamically */
111 long crl_check_interval
= 0;
113 /* by default pluto logs out after every smartcard use */
114 bool pkcs11_keep_state
= FALSE
;
116 /* options read by optionsfrom */
123 RSA_private_key_t
*private_key
= NULL
;
128 chunk_t challengePassword
;
129 chunk_t serialNumber
;
132 chunk_t issuerAndSubject
;
133 chunk_t getCertInitial
;
134 chunk_t scep_response
;
137 x509cert_t
*x509_signer
= NULL
;
138 x509cert_t
*x509_ca_enc
= NULL
;
139 x509cert_t
*x509_ca_sig
= NULL
;
140 generalName_t
*subjectAltNames
= NULL
;
141 pkcs10_t
*pkcs10
= NULL
;
144 * @brief exit scepclient
146 * @param status 0 = OK, 1 = general discomfort
149 exit_scepclient(err_t message
, ...)
153 if (private_key
!= NULL
)
155 free_RSA_private_content(private_key
);
161 free(serialNumber
.ptr
);
163 free(fingerprint
.ptr
);
164 free(issuerAndSubject
.ptr
);
165 free(getCertInitial
.ptr
);
166 free(scep_response
.ptr
);
168 free_generalNames(subjectAltNames
, TRUE
);
169 if (x509_signer
!= NULL
)
171 x509_signer
->subjectAltName
= NULL
;
173 free_x509cert(x509_signer
);
174 free_x509cert(x509_ca_enc
);
175 free_x509cert(x509_ca_sig
);
177 options
->destroy(options
);
179 /* print any error message to stderr */
180 if (message
!= NULL
&& *message
!= '\0')
183 char m
[LOG_WIDTH
]; /* longer messages will be truncated */
185 va_start(args
, message
);
186 vsnprintf(m
, sizeof(m
), message
, args
);
189 fprintf(stderr
, "error: %s\n", m
);
198 * @brief prints the program version and exits
204 printf("scepclient %s\n", scepclient_version
);
205 exit_scepclient(NULL
);
209 * @brief prints the usage of the program to the stderr output
211 * If message is set, program is exitet with 1 (error)
212 * @param message message in case of an error
215 usage(const char *message
)
218 "Usage: scepclient\n"
219 " --help (-h) show usage and exit\n"
220 " --version (-v) show version and exit\n"
221 " --quiet (-q) do not write log output to stderr\n"
222 " --in (-i) <type>[=<filename>] use <filename> of <type> for input \n"
223 " <type> = pkcs1 | cacert-enc | cacert-sig\n"
224 " - if no pkcs1 input is defined, a \n"
225 " RSA key will be generated\n"
226 " - if no filename is given, default is used\n"
227 " --out (-o) <type>[=<filename>] write output of <type> to <filename>\n"
228 " multiple outputs are allowed\n"
229 " <type> = pkcs1 | pkcs10 | pkcs7 | cert-self | cert | cacert\n"
230 " - type cacert defines filename prefix of\n"
231 " received CA certificate(s)\n"
232 " - if no filename is given, default is used\n"
233 " --optionsfrom (-+) <filename> reads additional options from given file\n"
234 " --force (-f) force existing file(s)\n"
236 "Options for key generation (pkcs1):\n"
237 " --keylength (-k) <bits> key length for RSA key generation\n"
238 "(default: 2048 bits)\n"
240 "Options for validity:\n"
241 " --days (-D) <days> validity in days\n"
242 " --startdate (-S) <YYMMDDHHMMSS>Z not valid before date\n"
243 " --enddate (-E) <YYMMDDHHMMSS>Z not valid after date\n"
245 "Options for request generation (pkcs10):\n"
246 " --dn (-d) <dn> comma separated list of distinguished names\n"
247 " --subjectAltName (-s) <t>=<v> include subjectAltName in certificate request\n"
248 " <t> = email | dns | ip \n"
249 " --password (-p) <pw> challenge password\n"
250 " - if pw is '%%prompt', password gets prompted for\n"
251 " --algorithm (-a) <algo> use specified algorithm for PKCS#7 encryption\n"
252 " <algo> = des-cbc | 3des-cbc | aes128-cbc |\n"
253 " aes192-cbc | aes256-cbc (default: 3des-cbc)\n"
255 "Options for enrollment (cert):\n"
256 " --url (-u) <url> url of the SCEP server\n"
257 " --method (-m) post | get http request type\n"
258 " --interval (-t) <seconds> manual mode poll interval in seconds (default 20s)\n"
259 " --maxpolltime (-x) <seconds> max poll time in seconds when in manual mode\n"
260 " (default: unlimited)\n"
263 "Debugging output:\n"
264 " --debug-all (-A) show everything except private\n"
265 " --debug-parsing (-P) show parsing relevant stuff\n"
266 " --debug-raw (-R) show raw hex dumps\n"
267 " --debug-control (-C) show control flow output\n"
268 " --debug-controlmore (-M) show more control flow\n"
269 " --debug-private (-X) show sensitive data (private keys, etc.)\n"
272 exit_scepclient(message
);
278 static void print_plugins()
280 char buf
[BUF_LEN
], *plugin
;
282 enumerator_t
*enumerator
;
284 enumerator
= lib
->plugins
->create_plugin_enumerator(lib
->plugins
);
285 while (len
< BUF_LEN
&& enumerator
->enumerate(enumerator
, &plugin
))
287 len
+= snprintf(&buf
[len
], BUF_LEN
-len
, "%s ", plugin
);
289 enumerator
->destroy(enumerator
);
290 DBG1(" loaded plugins: %s", buf
);
294 * @brief main of scepclient
296 * @param argc number of arguments
297 * @param argv pointer to the argument values
299 int main(int argc
, char **argv
)
301 /* external values */
302 extern char * optarg
;
305 /* type of input and output files */
316 /* filetype to read from, defaults to "generate a key" */
317 scep_filetype_t filetype_in
= 0;
319 /* filetype to write to, no default here */
320 scep_filetype_t filetype_out
= 0;
323 char *file_in_pkcs1
= DEFAULT_FILENAME_PKCS1
;
324 char *file_in_cacert_enc
= DEFAULT_FILENAME_CACERT_ENC
;
325 char *file_in_cacert_sig
= DEFAULT_FILENAME_CACERT_SIG
;
328 char *file_out_pkcs1
= DEFAULT_FILENAME_PKCS1
;
329 char *file_out_pkcs10
= DEFAULT_FILENAME_PKCS10
;
330 char *file_out_pkcs7
= DEFAULT_FILENAME_PKCS7
;
331 char *file_out_cert_self
= DEFAULT_FILENAME_CERT_SELF
;
332 char *file_out_cert
= DEFAULT_FILENAME_CERT
;
333 char *file_out_prefix_cacert
= DEFAULT_FILENAME_PREFIX_CACERT
;
335 /* by default user certificate is requested */
336 bool request_ca_certificate
= FALSE
;
338 /* by default existing files are not overwritten */
341 /* length of RSA key in bits */
342 u_int rsa_keylength
= DEFAULT_RSA_KEY_LENGTH
;
344 /* validity of self-signed certificate */
345 time_t validity
= DEFAULT_CERT_VALIDITY
;
346 time_t notBefore
= 0;
349 /* distinguished name for requested certificate, ASCII format */
350 char *distinguishedName
= NULL
;
352 /* challenge password */
353 char challenge_password_buffer
[MAX_PASSWORD_LENGTH
];
355 /* symmetric encryption algorithm used by pkcs7, default is 3DES */
356 int pkcs7_symmetric_cipher
= OID_3DES_EDE_CBC
;
358 /* digest algorithm used by pkcs7, default is SHA-1 */
359 int pkcs7_digest_alg
= OID_SHA1
;
361 /* signature algorithm used by pkcs10, default is SHA-1 with RSA encryption */
362 int pkcs10_signature_alg
= OID_SHA1
;
364 /* URL of the SCEP-Server */
365 char *scep_url
= NULL
;
367 /* http request method, default is GET */
368 bool http_get_request
= TRUE
;
370 /* poll interval time in manual mode in seconds */
371 u_int poll_interval
= DEFAULT_POLL_INTERVAL
;
373 /* maximum poll time */
374 u_int max_poll_time
= 0;
378 /* initialize global variables */
381 serialNumber
= chunk_empty
;
382 transID
= chunk_empty
;
383 fingerprint
= chunk_empty
;
384 issuerAndSubject
= chunk_empty
;
385 challengePassword
= chunk_empty
;
386 getCertInitial
= chunk_empty
;
387 scep_response
= chunk_empty
;
388 log_to_stderr
= TRUE
;
390 /* initialize library and optionsfrom */
391 library_init(STRONGSWAN_CONF
);
392 options
= options_create();
396 static const struct option long_opts
[] = {
397 /* name, has_arg, flag, val */
398 { "help", no_argument
, NULL
, 'h' },
399 { "version", no_argument
, NULL
, 'v' },
400 { "optionsfrom", required_argument
, NULL
, '+' },
401 { "quiet", no_argument
, NULL
, 'q' },
402 { "in", required_argument
, NULL
, 'i' },
403 { "out", required_argument
, NULL
, 'o' },
404 { "force", no_argument
, NULL
, 'f' },
405 { "keylength", required_argument
, NULL
, 'k' },
406 { "dn", required_argument
, NULL
, 'd' },
407 { "days", required_argument
, NULL
, 'D' },
408 { "startdate", required_argument
, NULL
, 'S' },
409 { "enddate", required_argument
, NULL
, 'E' },
410 { "subjectAltName", required_argument
, NULL
, 's' },
411 { "password", required_argument
, NULL
, 'p' },
412 { "algorithm", required_argument
, NULL
, 'a' },
413 { "url", required_argument
, NULL
, 'u' },
414 { "method", required_argument
, NULL
, 'm' },
415 { "interval", required_argument
, NULL
, 't' },
416 { "maxpolltime", required_argument
, NULL
, 'x' },
418 { "debug-all", no_argument
, NULL
, 'A' },
419 { "debug-parsing", no_argument
, NULL
, 'P'},
420 { "debug-raw", no_argument
, NULL
, 'R'},
421 { "debug-control", no_argument
, NULL
, 'C'},
422 { "debug-controlmore", no_argument
, NULL
, 'M'},
423 { "debug-private", no_argument
, NULL
, 'X'},
428 /* parse next option */
429 int c
= getopt_long(argc
, argv
, "hv+:qi:o:fk:d:s:p:a:u:m:t:x:APRCMS", long_opts
, NULL
);
433 case EOF
: /* end of flags */
436 case 'h': /* --help */
439 case 'v': /* --version */
442 case 'q': /* --quiet */
443 log_to_stderr
= FALSE
;
446 case 'i': /* --in <type> [= <filename>] */
448 char *filename
= strstr(optarg
, "=");
452 /* replace '=' by '\0' */
454 /* set pointer to start of filename */
457 if (strcaseeq("pkcs1", optarg
))
459 filetype_in
|= PKCS1
;
461 file_in_pkcs1
= filename
;
463 else if (strcaseeq("cacert-enc", optarg
))
465 filetype_in
|= CACERT_ENC
;
467 file_in_cacert_enc
= filename
;
469 else if (strcaseeq("cacert-sig", optarg
))
471 filetype_in
|= CACERT_SIG
;
473 file_in_cacert_sig
= filename
;
477 usage("invalid --in file type");
482 case 'o': /* --out <type> [= <filename>] */
484 char *filename
= strstr(optarg
, "=");
488 /* replace '=' by '\0' */
490 /* set pointer to start of filename */
493 if (strcaseeq("pkcs1", optarg
))
495 filetype_out
|= PKCS1
;
497 file_out_pkcs1
= filename
;
499 else if (strcaseeq("pkcs10", optarg
))
501 filetype_out
|= PKCS10
;
503 file_out_pkcs10
= filename
;
505 else if (strcaseeq("pkcs7", optarg
))
507 filetype_out
|= PKCS7
;
509 file_out_pkcs7
= filename
;
511 else if (strcaseeq("cert-self", optarg
))
513 filetype_out
|= CERT_SELF
;
515 file_out_cert_self
= filename
;
517 else if (strcaseeq("cert", optarg
))
519 filetype_out
|= CERT
;
521 file_out_cert
= filename
;
523 else if (strcaseeq("cacert", optarg
))
525 request_ca_certificate
= TRUE
;
527 file_out_prefix_cacert
= filename
;
531 usage("invalid --out file type");
536 case 'f': /* --force */
540 case '+': /* --optionsfrom <filename> */
541 if (!options
->from(options
, optarg
, &argc
, &argv
, optind
))
543 exit_scepclient("optionsfrom failed");
547 case 'k': /* --keylength <length> */
551 rsa_keylength
= atoi(optarg
);
552 if (rsa_keylength
== 0)
553 usage("invalid keylength");
555 /* check if key length is a multiple of 8 bits */
556 q
= div(rsa_keylength
, 2*BITS_PER_BYTE
);
559 exit_scepclient("keylength is not a multiple of %d bits!"
565 case 'D': /* --days */
566 if (optarg
== NULL
|| !isdigit(optarg
[0]))
567 usage("missing number of days");
570 long days
= strtol(optarg
, &endptr
, 0);
572 if (*endptr
!= '\0' || endptr
== optarg
574 usage("<days> must be a positive number");
575 validity
= 24*3600*days
;
579 case 'S': /* --startdate */
580 if (optarg
== NULL
|| strlen(optarg
) != 13 || optarg
[12] != 'Z')
581 usage("date format must be YYMMDDHHMMSSZ");
583 chunk_t date
= { optarg
, 13 };
584 notBefore
= asn1_to_time(&date
, ASN1_UTCTIME
);
588 case 'E': /* --enddate */
589 if (optarg
== NULL
|| strlen(optarg
) != 13 || optarg
[12] != 'Z')
590 usage("date format must be YYMMDDHHMMSSZ");
592 chunk_t date
= { optarg
, 13 };
593 notAfter
= asn1_to_time(&date
, ASN1_UTCTIME
);
598 if (distinguishedName
)
599 usage("only one distinguished name allowed");
600 distinguishedName
= optarg
;
603 case 's': /* --subjectAltName */
606 char *value
= strstr(optarg
, "=");
610 /* replace '=' by '\0' */
612 /* set pointer to start of value */
616 if (strcaseeq("email", optarg
))
618 kind
= GN_RFC822_NAME
;
620 else if (strcaseeq("dns", optarg
))
624 else if (strcaseeq("ip", optarg
))
626 kind
= GN_IP_ADDRESS
;
630 usage("invalid --subjectAltName type");
633 pkcs10_add_subjectAltName(&subjectAltNames
, kind
, value
);
637 case 'p': /* --password */
638 if (challengePassword
.len
> 0)
640 usage("only one challenge password allowed");
642 if (strcaseeq("%prompt", optarg
))
644 printf("Challenge password: ");
645 if (fgets(challenge_password_buffer
, sizeof(challenge_password_buffer
)-1, stdin
))
647 challengePassword
.ptr
= challenge_password_buffer
;
648 /* discard the terminating '\n' from the input */
649 challengePassword
.len
= strlen(challenge_password_buffer
) - 1;
653 usage("challenge password could not be read");
658 challengePassword
.ptr
= optarg
;
659 challengePassword
.len
= strlen(optarg
);
663 case 'u': /* -- url */
666 usage("only one URL argument allowed");
671 case 'm': /* --method */
672 if (strcaseeq("get", optarg
))
674 http_get_request
= TRUE
;
676 else if (strcaseeq("post", optarg
))
678 http_get_request
= FALSE
;
682 usage("invalid http request method specified");
686 case 't': /* --interval */
687 poll_interval
= atoi(optarg
);
688 if (poll_interval
<= 0)
690 usage("invalid interval specified");
694 case 'x': /* --maxpolltime */
695 max_poll_time
= atoi(optarg
);
696 if (max_poll_time
< 0)
698 usage("invalid maxpolltime specified");
702 case 'a': /*--algorithm */
703 if (strcaseeq("des-cbc", optarg
))
705 pkcs7_symmetric_cipher
= OID_DES_CBC
;
707 else if (strcaseeq("3des-cbc", optarg
))
709 pkcs7_symmetric_cipher
= OID_3DES_EDE_CBC
;
711 else if (strcaseeq("aes128-cbc", optarg
))
713 pkcs7_symmetric_cipher
= OID_AES128_CBC
;
715 else if (strcaseeq("aes192-cbc", optarg
))
717 pkcs7_symmetric_cipher
= OID_AES192_CBC
;
719 else if (strcaseeq("aes256-cbc", optarg
))
721 pkcs7_symmetric_cipher
= OID_AES256_CBC
;
725 usage("invalid encryption algorithm specified");
729 case 'A': /* --debug-all */
730 base_debugging
|= DBG_ALL
;
732 case 'P': /* debug parsing */
733 base_debugging
|= DBG_PARSING
;
735 case 'R': /* debug raw */
736 base_debugging
|= DBG_RAW
;
738 case 'C': /* debug control */
739 base_debugging
|= DBG_CONTROL
;
741 case 'M': /* debug control more */
742 base_debugging
|= DBG_CONTROLMORE
;
744 case 'X': /* debug private */
745 base_debugging
|= DBG_PRIVATE
;
749 usage("unknown option");
751 /* break from loop */
754 cur_debugging
= base_debugging
;
756 init_log("scepclient");
758 /* load plugins, further infrastructure may need it */
759 lib
->plugins
->load(lib
->plugins
, IPSEC_PLUGINDIR
,
760 lib
->settings
->get_str(lib
->settings
, "scepclient.load", PLUGINS
));
763 if ((filetype_out
== 0) && (!request_ca_certificate
))
765 usage ("--out filetype required");
767 if (request_ca_certificate
&& (filetype_out
> 0 || filetype_in
> 0))
769 usage("in CA certificate request, no other --in or --out option allowed");
772 /* check if url is given, if cert output defined */
773 if (((filetype_out
& CERT
) || request_ca_certificate
) && !scep_url
)
775 usage("URL of SCEP server required");
778 /* check for sanity of --in/--out */
779 if (!filetype_in
&& (filetype_in
> filetype_out
))
781 usage("cannot generate --out of given --in!");
785 * input of PKCS#1 file
787 private_key
= malloc_thing(RSA_private_key_t
);
789 if (filetype_in
& PKCS1
) /* load an RSA key pair from file */
791 prompt_pass_t pass
= { "", FALSE
, STDIN_FILENO
};
792 char *path
= concatenate_paths(PRIVATE_KEY_PATH
, file_in_pkcs1
);
794 ugh
= load_rsa_private_key(path
, &pass
, private_key
);
796 else /* generate an RSA key pair */
798 ugh
= generate_rsa_private_key(rsa_keylength
, private_key
);
801 exit_scepclient(ugh
);
803 /* check for minimum key length */
804 if ((private_key
->pub
.k
) < RSA_MIN_OCTETS
)
806 exit_scepclient("length of RSA key has to be at least %d bits"
807 ,RSA_MIN_OCTETS
* BITS_PER_BYTE
);
811 * input of PKCS#10 file
813 if (filetype_in
& PKCS10
)
815 /* user wants to load a pkcs10 request
816 * operation is not yet supported
817 * would require a PKCS#10 parsing function
819 pkcs10 = pkcs10_read_from_file(file_in_pkcs10);
826 chunk_t dn
= chunk_empty
;
830 if (distinguishedName
== NULL
)
833 int n
= sprintf(buf
, DEFAULT_DN
);
835 /* set the common name to the hostname */
836 if (gethostname(buf
+ n
, BUF_LEN
- n
) || strlen(buf
) == n
)
838 exit_scepclient("no hostname defined, use "
839 "--dn <distinguished name> option");
841 distinguishedName
= buf
;
845 DBG_log("dn: '%s'", distinguishedName
);
847 ugh
= atodn(distinguishedName
, &dn
);
850 exit_scepclient(ugh
);
853 subject
= chunk_clone(dn
);
856 DBG_log("building pkcs10 object:")
858 pkcs10
= pkcs10_build(private_key
, subject
, challengePassword
859 , subjectAltNames
, pkcs10_signature_alg
);
860 scep_generate_pkcs10_fingerprint(pkcs10
->request
, &fingerprint
);
861 plog(" fingerprint: %.*s", (int)fingerprint
.len
, fingerprint
.ptr
);
865 * output of PKCS#10 file
867 if (filetype_out
& PKCS10
)
869 char *path
= concatenate_paths(REQ_PATH
, file_out_pkcs10
);
871 if (!chunk_write(pkcs10
->request
, path
, "pkcs10", 0022, force
))
872 exit_scepclient("could not write pkcs10 file '%s'", path
);
874 filetype_out
&= ~PKCS10
; /* delete PKCS10 flag */
879 exit_scepclient(NULL
); /* no further output required */
883 * output of PKCS#1 file
885 if (filetype_out
& PKCS1
)
887 char *path
= concatenate_paths(PRIVATE_KEY_PATH
, file_out_pkcs1
);
890 DBG_log("building pkcs1 object:")
892 pkcs1
= pkcs1_build_private_key(private_key
);
894 if (!chunk_write(pkcs1
, path
, "pkcs1", 0066, force
))
895 exit_scepclient("could not write pkcs1 file '%s'", path
);
897 filetype_out
&= ~PKCS1
; /* delete PKCS1 flag */
902 exit_scepclient(NULL
); /* no further output required */
905 scep_generate_transaction_id((const RSA_public_key_t
*)private_key
906 , &transID
, &serialNumber
);
907 plog(" transaction ID: %.*s", (int)transID
.len
, transID
.ptr
);
909 /* generate a self-signed X.509 certificate */
910 x509_signer
= malloc_thing(x509cert_t
);
911 *x509_signer
= empty_x509cert
;
912 x509_signer
->serialNumber
= serialNumber
;
913 x509_signer
->sigAlg
= OID_SHA1_WITH_RSA
;
914 x509_signer
->issuer
= subject
;
915 x509_signer
->notBefore
= (notBefore
)? notBefore
917 x509_signer
->notAfter
= (notAfter
)? notAfter
918 : x509_signer
->notBefore
+ validity
;
919 x509_signer
->subject
= subject
;
920 x509_signer
->subjectAltName
= subjectAltNames
;
922 build_x509cert(x509_signer
, (const RSA_public_key_t
*)private_key
926 * output of self-signed X.509 certificate file
928 if (filetype_out
& CERT_SELF
)
930 char *path
= concatenate_paths(HOST_CERT_PATH
, file_out_cert_self
);
932 if (!chunk_write(x509_signer
->certificate
, path
, "self-signed cert", 0022, force
))
933 exit_scepclient("could not write self-signed cert file '%s'", path
);
935 filetype_out
&= ~CERT_SELF
; /* delete CERT_SELF flag */
940 exit_scepclient(NULL
); /* no further output required */
944 * load ca encryption certificate
947 char *path
= concatenate_paths(CA_CERT_PATH
, file_in_cacert_enc
);
950 if (!load_cert(path
, "encryption cacert", &cert
))
952 exit_scepclient("could not load encryption cacert file '%s'", path
);
954 x509_ca_enc
= cert
.u
.x509
;
958 * input of PKCS#7 file
960 if (filetype_in
& PKCS7
)
962 /* user wants to load a pkcs7 encrypted request
963 * operation is not yet supported!
964 * would require additional parsing of transaction-id
966 pkcs7 = pkcs7_read_from_file(file_in_pkcs7);
973 DBG_log("building pkcs7 request")
975 pkcs7
= scep_build_request(pkcs10
->request
976 , transID
, SCEP_PKCSReq_MSG
977 , x509_ca_enc
, pkcs7_symmetric_cipher
978 , x509_signer
, pkcs7_digest_alg
, private_key
);
982 * output pkcs7 encrypted and signed certificate request
984 if (filetype_out
& PKCS7
)
986 char *path
= concatenate_paths(REQ_PATH
, file_out_pkcs7
);
988 if (!chunk_write(pkcs7
, path
, "pkcs7 encrypted request", 0022, force
))
989 exit_scepclient("could not write pkcs7 file '%s'", path
);
991 filetype_out
&= ~PKCS7
; /* delete PKCS7 flag */
996 exit_scepclient(NULL
); /* no further output required */
1000 * output certificate fetch from SCEP server
1002 if (filetype_out
& CERT
)
1004 char *path
= concatenate_paths(CA_CERT_PATH
, file_in_cacert_sig
);
1008 x509cert_t
*certs
= NULL
;
1009 chunk_t envelopedData
= chunk_empty
;
1010 chunk_t certData
= chunk_empty
;
1011 contentInfo_t data
= empty_contentInfo
;
1012 scep_attributes_t attrs
= empty_scep_attributes
;
1014 if (!load_cert(path
, "signature cacert", &cert
))
1015 exit_scepclient("could not load signature cacert file '%s'", path
);
1016 x509_ca_sig
= cert
.u
.x509
;
1018 if (!scep_http_request(scep_url
, pkcs7
, SCEP_PKI_OPERATION
,
1019 http_get_request
, &scep_response
))
1021 exit_scepclient("did not receive a valid scep response");
1023 ugh
= scep_parse_response(scep_response
, transID
, &data
, &attrs
1027 exit_scepclient(ugh
);
1030 /* in case of manual mode, we are going into a polling loop */
1031 if (attrs
.pkiStatus
== SCEP_PENDING
)
1033 plog(" scep request pending, polling every %d seconds"
1036 issuerAndSubject
= asn1_wrap(ASN1_SEQUENCE
, "cc"
1037 , x509_ca_sig
->subject
1040 while (attrs
.pkiStatus
== SCEP_PENDING
)
1042 if (max_poll_time
> 0
1043 && (time(NULL
) - poll_start
>= max_poll_time
))
1045 exit_scepclient("maximum poll time reached: %d seconds"
1049 DBG_log("going to sleep for %d seconds", poll_interval
)
1051 sleep(poll_interval
);
1052 free(scep_response
.ptr
);
1055 DBG_log("fingerprint: %.*s", (int)fingerprint
.len
, fingerprint
.ptr
);
1056 DBG_log("transaction ID: %.*s", (int)transID
.len
, transID
.ptr
)
1059 chunk_free(&getCertInitial
);
1060 getCertInitial
= scep_build_request(issuerAndSubject
1061 , transID
, SCEP_GetCertInitial_MSG
1062 , x509_ca_enc
, pkcs7_symmetric_cipher
1063 , x509_signer
, pkcs7_digest_alg
, private_key
);
1065 if (!scep_http_request(scep_url
, getCertInitial
, SCEP_PKI_OPERATION
,
1066 http_get_request
, &scep_response
))
1068 exit_scepclient("did not receive a valid scep response");
1070 ugh
= scep_parse_response(scep_response
, transID
, &data
, &attrs
1074 exit_scepclient(ugh
);
1078 if (attrs
.pkiStatus
!= SCEP_SUCCESS
)
1080 exit_scepclient("reply status is not 'SUCCESS'");
1083 envelopedData
= data
.content
;
1085 if (data
.type
!= OID_PKCS7_DATA
1086 || !asn1_parse_simple_object(&envelopedData
, ASN1_OCTET_STRING
, 0, "data"))
1088 exit_scepclient("contentInfo is not of type 'data'");
1090 if (!pkcs7_parse_envelopedData(envelopedData
, &certData
1091 , serialNumber
, private_key
))
1093 exit_scepclient("could not decrypt envelopedData");
1095 if (!pkcs7_parse_signedData(certData
, NULL
, &certs
, NULL
, NULL
))
1097 exit_scepclient("error parsing the scep response");
1099 chunk_free(&certData
);
1101 /* store the end entity certificate */
1102 path
= concatenate_paths(HOST_CERT_PATH
, file_out_cert
);
1103 while (certs
!= NULL
)
1105 bool stored
= FALSE
;
1106 x509cert_t
*cert
= certs
;
1111 exit_scepclient("multiple certs received, only first stored");
1112 if (!chunk_write(cert
->certificate
, path
, "requested cert", 0022, force
))
1113 exit_scepclient("could not write cert file '%s'", path
);
1116 certs
= certs
->next
;
1117 free_x509cert(cert
);
1119 filetype_out
&= ~CERT
; /* delete CERT flag */
1122 exit_scepclient(NULL
);
1123 return -1; /* should never be reached */