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
39 #include "../pluto/constants.h"
40 #include "../pluto/defs.h"
41 #include "../pluto/log.h"
42 #include "../pluto/oid.h"
43 #include "../pluto/asn1.h"
44 #include "../pluto/pkcs1.h"
45 #include "../pluto/pkcs7.h"
46 #include "../pluto/certs.h"
47 #include "../pluto/fetch.h"
48 #include "../pluto/rnd.h"
55 * definition of some defaults
58 /* default name of DER-encoded PKCS#1 private key file */
59 #define DEFAULT_FILENAME_PKCS1 "myKey.der"
61 /* default name of DER-encoded PKCS#10 certificate request file */
62 #define DEFAULT_FILENAME_PKCS10 "myReq.der"
64 /* default name of DER-encoded PKCS#7 file */
65 #define DEFAULT_FILENAME_PKCS7 "pkcs7.der"
67 /* default name of DER-encoded self-signed X.509 certificate file */
68 #define DEFAULT_FILENAME_CERT_SELF "selfCert.der"
70 /* default name of DER-encoded X.509 certificate file */
71 #define DEFAULT_FILENAME_CERT "myCert.der"
73 /* default name of DER-encoded CA cert file used for key encipherment */
74 #define DEFAULT_FILENAME_CACERT_ENC "caCert.der"
76 /* default name of the der encoded CA cert file used for signature verification */
77 #define DEFAULT_FILENAME_CACERT_SIG "caCert.der"
79 /* default prefix of the der encoded CA certificates received from the SCEP server */
80 #define DEFAULT_FILENAME_PREFIX_CACERT "caCert.der"
82 /* default certificate validity */
83 #define DEFAULT_CERT_VALIDITY 5 * 3600 * 24 * 365 /* seconds */
85 /* default polling time interval in SCEP manual mode */
86 #define DEFAULT_POLL_INTERVAL 20 /* seconds */
88 /* default key length for self-generated RSA keys */
89 #define DEFAULT_RSA_KEY_LENGTH 2048 /* bits */
91 /* default distinguished name */
92 #define DEFAULT_DN "C=CH, O=Linux strongSwan, CN="
94 /* challenge password buffer size */
95 #define MAX_PASSWORD_LENGTH 256
97 /* Max length of filename for tempfile */
98 #define MAX_TEMP_FILENAME_LENGTH 256
101 /* current scepclient version */
102 static const char *scepclient_version
= "1.0";
104 /* by default the CRL policy is lenient */
105 bool strict_crl_policy
= FALSE
;
107 /* by default pluto does not check crls dynamically */
108 long crl_check_interval
= 0;
110 /* by default pluto logs out after every smartcard use */
111 bool pkcs11_keep_state
= FALSE
;
118 RSA_private_key_t
*private_key
= NULL
;
123 chunk_t challengePassword
;
124 chunk_t serialNumber
;
127 chunk_t issuerAndSubject
;
128 chunk_t getCertInitial
;
129 chunk_t scep_response
;
132 x509cert_t
*x509_signer
= NULL
;
133 x509cert_t
*x509_ca_enc
= NULL
;
134 x509cert_t
*x509_ca_sig
= NULL
;
135 generalName_t
*subjectAltNames
= NULL
;
136 pkcs10_t
*pkcs10
= NULL
;
139 * @brief exit scepclient
141 * The log is closed and leaks are reported
142 * if LEAK_DETECTIVE is activated
144 * @param status 0 = OK, 1 = general discomfort
147 exit_scepclient(err_t message
, ...)
149 if (private_key
!= NULL
)
151 free_RSA_private_content(private_key
);
156 freeanychunk(subject
);
157 freeanychunk(serialNumber
);
158 freeanychunk(transID
);
159 freeanychunk(fingerprint
);
160 freeanychunk(issuerAndSubject
);
161 freeanychunk(getCertInitial
);
162 if (scep_response
.ptr
!= NULL
)
163 free(scep_response
.ptr
);
165 free_generalNames(subjectAltNames
, TRUE
);
166 if (x509_signer
!= NULL
)
167 x509_signer
->subjectAltName
= NULL
;
169 free_x509cert(x509_signer
);
170 free_x509cert(x509_ca_enc
);
171 free_x509cert(x509_ca_sig
);
174 #ifdef LEAK_DETECTIVE
176 #endif /* LEAK_DETECTIVE */
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
);
196 * @brief prints the program version and exits
202 printf("scepclient %s\n", scepclient_version
);
203 exit_scepclient(NULL
);
207 * @brief prints the usage of the program to the stderr output
209 * If message is set, program is exitet with 1 (error)
210 * @param message message in case of an error
213 usage(const char *message
)
216 "Usage: scepclient\n"
217 " --help (-h) show usage and exit\n"
218 " --version (-v) show version and exit\n"
219 " --quiet (-q) do not write log output to stderr\n"
220 " --in (-i) <type>[=<filename>] use <filename> of <type> for input \n"
221 " <type> = pkcs1 | cacert-enc | cacert-sig\n"
222 " - if no pkcs1 input is defined, a \n"
223 " RSA key will be generated\n"
224 " - if no filename is given, default is used\n"
225 " --out (-o) <type>[=<filename>] write output of <type> to <filename>\n"
226 " multiple outputs are allowed\n"
227 " <type> = pkcs1 | pkcs10 | pkcs7 | cert-self | cert | cacert\n"
228 " - type cacert defines filename prefix of\n"
229 " received CA certificate(s)\n"
230 " - if no filename is given, default is used\n"
231 " --optionsfrom (-+) <filename> reads additional options from given file\n"
232 " --force (-f) force existing file(s)\n"
234 "Options for key generation (pkcs1):\n"
235 " --keylength (-k) <bits> key length for RSA key generation\n"
236 "(default: 2048 bits)\n"
238 "Options for validity:\n"
239 " --days (-D) <days> validity in days\n"
240 " --startdate (-S) <YYMMDDHHMMSS>Z not valid before date\n"
241 " --enddate (-E) <YYMMDDHHMMSS>Z not valid after date\n"
243 "Options for request generation (pkcs10):\n"
244 " --dn (-d) <dn> comma separated list of distinguished names\n"
245 " --subjectAltName (-s) <t>=<v> include subjectAltName in certificate request\n"
246 " <t> = email | dns | ip \n"
247 " --password (-p) <pw> challenge password\n"
248 " - if pw is '%%prompt', password gets prompted for\n"
249 " --algorithm (-a) <algo> use specified algorithm for PKCS#7 encryption\n"
250 " <algo> = des-cbc | 3des-cbc (default: 3des-cbc)\n"
252 "Options for enrollment (cert):\n"
253 " --url (-u) <url> url of the SCEP server\n"
254 " --method (-m) post | get http request type\n"
255 " --interval (-t) <seconds> manual mode poll interval in seconds (default 20s)\n"
256 " --maxpolltime (-x) <seconds> max poll time in seconds when in manual mode\n"
257 " (default: unlimited)\n"
260 "Debugging output:\n"
261 " --debug-all (-A) show everything except private\n"
262 " --debug-parsing (-P) show parsing relevant stuff\n"
263 " --debug-raw (-R) show raw hex dumps\n"
264 " --debug-control (-C) show control flow output\n"
265 " --debug-controlmore (-M) show more control flow\n"
266 " --debug-private (-X) show sensitive data (private keys, etc.)\n"
269 exit_scepclient(message
);
273 * @brief main of scepclient
275 * @param argc number of arguments
276 * @param argv pointer to the argument values
278 int main(int argc
, char **argv
)
280 /* external values */
281 extern char * optarg
;
284 /* type of input and output files */
295 /* filetype to read from, defaults to "generate a key" */
296 scep_filetype_t filetype_in
= 0;
298 /* filetype to write to, no default here */
299 scep_filetype_t filetype_out
= 0;
302 char *file_in_pkcs1
= DEFAULT_FILENAME_PKCS1
;
303 char *file_in_cacert_enc
= DEFAULT_FILENAME_CACERT_ENC
;
304 char *file_in_cacert_sig
= DEFAULT_FILENAME_CACERT_SIG
;
307 char *file_out_pkcs1
= DEFAULT_FILENAME_PKCS1
;
308 char *file_out_pkcs10
= DEFAULT_FILENAME_PKCS10
;
309 char *file_out_pkcs7
= DEFAULT_FILENAME_PKCS7
;
310 char *file_out_cert_self
= DEFAULT_FILENAME_CERT_SELF
;
311 char *file_out_cert
= DEFAULT_FILENAME_CERT
;
312 char *file_out_prefix_cacert
= DEFAULT_FILENAME_PREFIX_CACERT
;
314 /* by default user certificate is requested */
315 bool request_ca_certificate
= FALSE
;
317 /* by default existing files are not overwritten */
320 /* length of RSA key in bits */
321 u_int rsa_keylength
= DEFAULT_RSA_KEY_LENGTH
;
323 /* validity of self-signed certificate */
324 time_t validity
= DEFAULT_CERT_VALIDITY
;
325 time_t notBefore
= 0;
328 /* distinguished name for requested certificate, ASCII format */
329 char *distinguishedName
= NULL
;
331 /* challenge password */
332 char challenge_password_buffer
[MAX_PASSWORD_LENGTH
];
334 /* symmetric encryption algorithm used by pkcs7, default is 3DES */
335 int pkcs7_symmetric_cipher
= OID_3DES_EDE_CBC
;
337 /* digest algorithm used by pkcs7, default is MD5 */
338 int pkcs7_digest_alg
= OID_MD5
;
340 /* signature algorithm used by pkcs10, default is MD5 with RSA encryption */
341 int pkcs10_signature_alg
= OID_MD5
;
343 /* URL of the SCEP-Server */
344 char *scep_url
= NULL
;
346 /* http request method, default is GET */
347 fetch_request_t request_type
= FETCH_GET
;
349 /* poll interval time in manual mode in seconds */
350 u_int poll_interval
= DEFAULT_POLL_INTERVAL
;
352 /* maximum poll time */
353 u_int max_poll_time
= 0;
357 /* initialize global variables */
360 serialNumber
= empty_chunk
;
361 transID
= empty_chunk
;
362 fingerprint
= empty_chunk
;
363 issuerAndSubject
= empty_chunk
;
364 challengePassword
= empty_chunk
;
365 getCertInitial
= empty_chunk
;
366 scep_response
= empty_chunk
;
367 log_to_stderr
= TRUE
;
371 static const struct option long_opts
[] = {
372 /* name, has_arg, flag, val */
373 { "help", no_argument
, NULL
, 'h' },
374 { "version", no_argument
, NULL
, 'v' },
375 { "optionsfrom", required_argument
, NULL
, '+' },
376 { "quiet", no_argument
, NULL
, 'q' },
377 { "in", required_argument
, NULL
, 'i' },
378 { "out", required_argument
, NULL
, 'o' },
379 { "force", no_argument
, NULL
, 'f' },
380 { "keylength", required_argument
, NULL
, 'k' },
381 { "dn", required_argument
, NULL
, 'd' },
382 { "days", required_argument
, NULL
, 'D' },
383 { "startdate", required_argument
, NULL
, 'S' },
384 { "enddate", required_argument
, NULL
, 'E' },
385 { "subjectAltName", required_argument
, NULL
, 's' },
386 { "password", required_argument
, NULL
, 'p' },
387 { "algorithm", required_argument
, NULL
, 'a' },
388 { "url", required_argument
, NULL
, 'u' },
389 { "method", required_argument
, NULL
, 'm' },
390 { "interval", required_argument
, NULL
, 't' },
391 { "maxpolltime", required_argument
, NULL
, 'x' },
393 { "debug-all", no_argument
, NULL
, 'A' },
394 { "debug-parsing", no_argument
, NULL
, 'P'},
395 { "debug-raw", no_argument
, NULL
, 'R'},
396 { "debug-control", no_argument
, NULL
, 'C'},
397 { "debug-controlmore", no_argument
, NULL
, 'M'},
398 { "debug-private", no_argument
, NULL
, 'X'},
403 /* parse next option */
404 int c
= getopt_long(argc
, argv
, "hv+:qi:o:fk:d:s:p:a:u:m:t:x:APRCMS", long_opts
, NULL
);
408 case EOF
: /* end of flags */
411 case 'h': /* --help */
414 case 'v': /* --version */
417 case 'q': /* --quiet */
418 log_to_stderr
= FALSE
;
421 case 'i': /* --in <type> [= <filename>] */
423 char *filename
= strstr(optarg
, "=");
427 /* replace '=' by '\0' */
429 /* set pointer to start of filename */
432 if (strcasecmp("pkcs1", optarg
) == 0)
434 filetype_in
|= PKCS1
;
436 file_in_pkcs1
= filename
;
438 else if (strcasecmp("cacert-enc", optarg
) == 0)
440 filetype_in
|= CACERT_ENC
;
442 file_in_cacert_enc
= filename
;
444 else if (strcasecmp("cacert-sig", optarg
) == 0)
446 filetype_in
|= CACERT_SIG
;
448 file_in_cacert_sig
= filename
;
452 usage("invalid --in file type");
457 case 'o': /* --out <type> [= <filename>] */
459 char *filename
= strstr(optarg
, "=");
463 /* replace '=' by '\0' */
465 /* set pointer to start of filename */
468 if (strcasecmp("pkcs1", optarg
) == 0)
470 filetype_out
|= PKCS1
;
472 file_out_pkcs1
= filename
;
474 else if (strcasecmp("pkcs10", optarg
) == 0)
476 filetype_out
|= PKCS10
;
478 file_out_pkcs10
= filename
;
480 else if (strcasecmp("pkcs7", optarg
) == 0)
482 filetype_out
|= PKCS7
;
484 file_out_pkcs7
= filename
;
486 else if (strcasecmp("cert-self", optarg
) == 0)
488 filetype_out
|= CERT_SELF
;
490 file_out_cert_self
= filename
;
492 else if (strcasecmp("cert", optarg
) == 0)
494 filetype_out
|= CERT
;
496 file_out_cert
= filename
;
498 else if (strcasecmp("cacert", optarg
) == 0)
500 request_ca_certificate
= TRUE
;
502 file_out_prefix_cacert
= filename
;
506 usage("invalid --out file type");
511 case 'f': /* --force */
515 case '+': /* --optionsfrom <filename> */
516 optionsfrom(optarg
, &argc
, &argv
, optind
, stderr
);
517 /* does not return on error */
520 case 'k': /* --keylength <length> */
524 rsa_keylength
= atoi(optarg
);
525 if (rsa_keylength
== 0)
526 usage("invalid keylength");
528 /* check if key length is a multiple of 8 bits */
529 q
= div(rsa_keylength
, 2*BITS_PER_BYTE
);
532 exit_scepclient("keylength is not a multiple of %d bits!"
538 case 'D': /* --days */
539 if (optarg
== NULL
|| !isdigit(optarg
[0]))
540 usage("missing number of days");
543 long days
= strtol(optarg
, &endptr
, 0);
545 if (*endptr
!= '\0' || endptr
== optarg
547 usage("<days> must be a positive number");
548 validity
= 24*3600*days
;
552 case 'S': /* --startdate */
553 if (optarg
== NULL
|| strlen(optarg
) != 13 || optarg
[12] != 'Z')
554 usage("date format must be YYMMDDHHMMSSZ");
556 chunk_t date
= { optarg
, 13 };
557 notBefore
= asn1totime(&date
, ASN1_UTCTIME
);
561 case 'E': /* --enddate */
562 if (optarg
== NULL
|| strlen(optarg
) != 13 || optarg
[12] != 'Z')
563 usage("date format must be YYMMDDHHMMSSZ");
565 chunk_t date
= { optarg
, 13 };
566 notAfter
= asn1totime(&date
, ASN1_UTCTIME
);
571 if (distinguishedName
)
572 usage("only one distinguished name allowed");
573 distinguishedName
= optarg
;
576 case 's': /* --subjectAltName */
579 char *value
= strstr(optarg
, "=");
583 /* replace '=' by '\0' */
585 /* set pointer to start of value */
589 if (!strcasecmp("email", optarg
))
590 kind
= GN_RFC822_NAME
;
591 else if (!strcasecmp("dns", optarg
))
593 else if (!strcasecmp("ip", optarg
))
594 kind
= GN_IP_ADDRESS
;
597 usage("invalid --subjectAltName type");
600 pkcs10_add_subjectAltName(&subjectAltNames
, kind
, value
);
604 case 'p': /* --password */
605 if (challengePassword
.len
> 0)
606 usage("only one challenge password allowed");
608 if (strcasecmp("%prompt", optarg
) == 0)
610 printf("Challenge password: ");
611 if (fgets(challenge_password_buffer
, sizeof(challenge_password_buffer
)-1, stdin
))
613 challengePassword
.ptr
= challenge_password_buffer
;
614 /* discard the terminating '\n' from the input */
615 challengePassword
.len
= strlen(challenge_password_buffer
) - 1;
619 usage("challenge password could not be read");
624 challengePassword
.ptr
= optarg
;
625 challengePassword
.len
= strlen(optarg
);
629 case 'u': /* -- url */
631 usage("only one URL argument allowed");
635 case 'm': /* --method */
636 if (strcasecmp("post", optarg
) == 0)
637 request_type
= FETCH_POST
;
638 else if (strcasecmp("get", optarg
) == 0)
639 request_type
= FETCH_GET
;
641 usage("invalid http request method specified");
644 case 't': /* --interval */
645 poll_interval
= atoi(optarg
);
646 if (poll_interval
<= 0)
647 usage("invalid interval specified");
650 case 'x': /* --maxpolltime */
651 max_poll_time
= atoi(optarg
);
652 if (max_poll_time
< 0)
653 usage("invalid maxpolltime specified");
656 case 'a': /*--algorithm */
657 if (strcasecmp("des-cbc", optarg
) == 0)
658 pkcs7_symmetric_cipher
= OID_DES_CBC
;
659 else if (strcasecmp("3des-cbc", optarg
) == 0)
660 pkcs7_symmetric_cipher
= OID_3DES_EDE_CBC
;
662 usage("invalid encryption algorithm specified");
665 case 'A': /* --debug-all */
666 base_debugging
|= DBG_ALL
;
668 case 'P': /* debug parsing */
669 base_debugging
|= DBG_PARSING
;
671 case 'R': /* debug raw */
672 base_debugging
|= DBG_RAW
;
674 case 'C': /* debug control */
675 base_debugging
|= DBG_CONTROL
;
677 case 'M': /* debug control more */
678 base_debugging
|= DBG_CONTROLMORE
;
680 case 'X': /* debug private */
681 base_debugging
|= DBG_PRIVATE
;
685 usage("unknown option");
687 /* break from loop */
691 init_log("scepclient");
692 cur_debugging
= base_debugging
;
696 if ((filetype_out
== 0) && (!request_ca_certificate
))
697 usage ("--out filetype required");
699 if (request_ca_certificate
&& (filetype_out
> 0 || filetype_in
> 0))
700 usage("in CA certificate request, no other --in or --out option allowed");
702 /* check if url is given, if cert output defined */
703 if (((filetype_out
& CERT
) || request_ca_certificate
) && !scep_url
)
704 usage("URL of SCEP server required");
706 /* check for sanity of --in/--out */
707 if (!filetype_in
&& (filetype_in
> filetype_out
))
708 usage("cannot generate --out of given --in!");
711 * input of PKCS#1 file
713 private_key
= alloc_thing(RSA_private_key_t
, "RSA_private_key_t");
715 if (filetype_in
& PKCS1
) /* load an RSA key pair from file */
717 prompt_pass_t pass
= { "", FALSE
, STDIN_FILENO
};
718 const char *path
= concatenate_paths(PRIVATE_KEY_PATH
, file_in_pkcs1
);
720 ugh
= load_rsa_private_key(path
, &pass
, private_key
);
722 else /* generate an RSA key pair */
724 ugh
= generate_rsa_private_key(rsa_keylength
, private_key
);
727 exit_scepclient(ugh
);
729 /* check for minimum key length */
730 if ((private_key
->pub
.k
) < RSA_MIN_OCTETS
)
732 exit_scepclient("length of RSA key has to be at least %d bits"
733 ,RSA_MIN_OCTETS
* BITS_PER_BYTE
);
737 * input of PKCS#10 file
739 if (filetype_in
& PKCS10
)
741 /* user wants to load a pkcs10 request
742 * operation is not yet supported
743 * would require a PKCS#10 parsing function
745 pkcs10 = pkcs10_read_from_file(file_in_pkcs10);
752 chunk_t dn
= empty_chunk
;
756 if (distinguishedName
== NULL
)
759 int n
= sprintf(buf
, DEFAULT_DN
);
761 /* set the common name to the hostname */
762 if (gethostname(buf
+ n
, BUF_LEN
- n
) || strlen(buf
) == n
)
764 exit_scepclient("no hostname defined, use "
765 "--dn <distinguished name> option");
767 distinguishedName
= buf
;
771 DBG_log("dn: '%s'", distinguishedName
);
773 ugh
= atodn(distinguishedName
, &dn
);
775 exit_scepclient(ugh
);
777 clonetochunk(subject
, dn
.ptr
, dn
.len
, "subject dn");
780 DBG_log("building pkcs10 object:")
782 pkcs10
= pkcs10_build(private_key
, subject
, challengePassword
783 , subjectAltNames
, pkcs10_signature_alg
);
784 scep_generate_pkcs10_fingerprint(pkcs10
->request
, &fingerprint
);
785 plog(" fingerprint: %.*s", (int)fingerprint
.len
, fingerprint
.ptr
);
789 * output of PKCS#10 file
791 if (filetype_out
& PKCS10
)
793 const char *path
= concatenate_paths(REQ_PATH
, file_out_pkcs10
);
795 if (!write_chunk(path
, "pkcs10", pkcs10
->request
, 0022, force
))
796 exit_scepclient("could not write pkcs10 file '%s'", path
);
798 filetype_out
&= ~PKCS10
; /* delete PKCS10 flag */
802 exit_scepclient(NULL
); /* no further output required */
805 * output of PKCS#1 file
807 if (filetype_out
& PKCS1
)
809 const char *path
= concatenate_paths(PRIVATE_KEY_PATH
, file_out_pkcs1
);
812 DBG_log("building pkcs1 object:")
814 pkcs1
= pkcs1_build_private_key(private_key
);
816 if (!write_chunk(path
, "pkcs1", pkcs1
, 0066, force
))
817 exit_scepclient("could not write pkcs1 file '%s'", path
);
819 filetype_out
&= ~PKCS1
; /* delete PKCS1 flag */
823 exit_scepclient(NULL
); /* no further output required */
825 scep_generate_transaction_id((const RSA_public_key_t
*)private_key
826 , &transID
, &serialNumber
);
827 plog(" transaction ID: %.*s", (int)transID
.len
, transID
.ptr
);
829 /* generate a self-signed X.509 certificate */
830 x509_signer
= alloc_thing(x509cert_t
, "signer cert");
831 *x509_signer
= empty_x509cert
;
832 x509_signer
->serialNumber
= serialNumber
;
833 x509_signer
->sigAlg
= OID_SHA1_WITH_RSA
;
834 x509_signer
->issuer
= subject
;
835 x509_signer
->notBefore
= (notBefore
)? notBefore
837 x509_signer
->notAfter
= (notAfter
)? notAfter
838 : x509_signer
->notBefore
+ validity
;
839 x509_signer
->subject
= subject
;
840 x509_signer
->subjectAltName
= subjectAltNames
;
842 build_x509cert(x509_signer
, (const RSA_public_key_t
*)private_key
846 * output of self-signed X.509 certificate file
848 if (filetype_out
& CERT_SELF
)
850 const char *path
= concatenate_paths(HOST_CERT_PATH
, file_out_cert_self
);
852 if (!write_chunk(path
, "self-signed cert", x509_signer
->certificate
, 0022, force
))
853 exit_scepclient("could not write self-signed cert file '%s'", path
);
855 filetype_out
&= ~CERT_SELF
; /* delete CERT_SELF flag */
859 exit_scepclient(NULL
); /* no further output required */
862 * load ca encryption certificate
865 const char *path
= concatenate_paths(CA_CERT_PATH
, file_in_cacert_enc
);
868 if (!load_cert(path
, "encryption cacert", &cert
))
869 exit_scepclient("could not load encryption cacert file '%s'", path
);
870 x509_ca_enc
= cert
.u
.x509
;
874 * input of PKCS#7 file
876 if (filetype_in
& PKCS7
)
878 /* user wants to load a pkcs7 encrypted request
879 * operation is not yet supported!
880 * would require additional parsing of transaction-id
882 pkcs7 = pkcs7_read_from_file(file_in_pkcs7);
889 DBG_log("building pkcs7 request")
891 pkcs7
= scep_build_request(pkcs10
->request
892 , transID
, SCEP_PKCSReq_MSG
893 , x509_ca_enc
, pkcs7_symmetric_cipher
894 , x509_signer
, pkcs7_digest_alg
, private_key
);
898 * output pkcs7 encrypted and signed certificate request
900 if (filetype_out
& PKCS7
)
902 const char *path
= concatenate_paths(REQ_PATH
, file_out_pkcs7
);
904 if (!write_chunk(path
, "pkcs7 encrypted request", pkcs7
, 0022, force
))
905 exit_scepclient("could not write pkcs7 file '%s'", path
);
907 filetype_out
&= ~PKCS7
; /* delete PKCS7 flag */
911 exit_scepclient(NULL
); /* no further output required */
914 * output certificate fetch from SCEP server
916 if (filetype_out
& CERT
)
918 const char *path
= concatenate_paths(CA_CERT_PATH
, file_in_cacert_sig
);
922 x509cert_t
*certs
= NULL
;
923 chunk_t envelopedData
= empty_chunk
;
924 chunk_t certData
= empty_chunk
;
925 contentInfo_t data
= empty_contentInfo
;
926 scep_attributes_t attrs
= empty_scep_attributes
;
928 if (!load_cert(path
, "signature cacert", &cert
))
929 exit_scepclient("could not load signature cacert file '%s'", path
);
930 x509_ca_sig
= cert
.u
.x509
;
932 if (!scep_http_request(scep_url
, pkcs7
, SCEP_PKI_OPERATION
933 , request_type
, &scep_response
))
935 exit_scepclient("did not receive a valid scep response");
937 ugh
= scep_parse_response(scep_response
, transID
, &data
, &attrs
940 exit_scepclient(ugh
);
942 /* in case of manual mode, we are going into a polling loop */
943 if (attrs
.pkiStatus
== SCEP_PENDING
)
945 plog(" scep request pending, polling every %d seconds"
948 issuerAndSubject
= asn1_wrap(ASN1_SEQUENCE
, "cc"
949 , x509_ca_sig
->subject
952 while (attrs
.pkiStatus
== SCEP_PENDING
)
954 if (max_poll_time
> 0
955 && (time(NULL
) - poll_start
>= max_poll_time
))
957 exit_scepclient("maximum poll time reached: %d seconds"
961 DBG_log("going to sleep for %d seconds", poll_interval
)
963 sleep(poll_interval
);
964 free(scep_response
.ptr
);
967 DBG_log("fingerprint: %.*s", (int)fingerprint
.len
, fingerprint
.ptr
);
968 DBG_log("transaction ID: %.*s", (int)transID
.len
, transID
.ptr
)
971 freeanychunk(getCertInitial
);
972 getCertInitial
= scep_build_request(issuerAndSubject
973 , transID
, SCEP_GetCertInitial_MSG
974 , x509_ca_enc
, pkcs7_symmetric_cipher
975 , x509_signer
, pkcs7_digest_alg
, private_key
);
977 if (!scep_http_request(scep_url
, getCertInitial
, SCEP_PKI_OPERATION
978 , request_type
, &scep_response
))
980 exit_scepclient("did not receive a valid scep response");
982 ugh
= scep_parse_response(scep_response
, transID
, &data
, &attrs
985 exit_scepclient(ugh
);
988 if (attrs
.pkiStatus
!= SCEP_SUCCESS
)
990 exit_scepclient("reply status is not 'SUCCESS'");
993 envelopedData
= data
.content
;
995 if (data
.type
!= OID_PKCS7_DATA
996 || !parse_asn1_simple_object(&envelopedData
, ASN1_OCTET_STRING
, 0, "data"))
998 exit_scepclient("contentInfo is not of type 'data'");
1000 if (!pkcs7_parse_envelopedData(envelopedData
, &certData
1001 , serialNumber
, private_key
))
1003 exit_scepclient("could not decrypt envelopedData");
1005 if (!pkcs7_parse_signedData(certData
, NULL
, &certs
, NULL
, NULL
))
1007 exit_scepclient("error parsing the scep response");
1009 freeanychunk(certData
);
1011 /* store the end entity certificate */
1012 path
= concatenate_paths(HOST_CERT_PATH
, file_out_cert
);
1013 while (certs
!= NULL
)
1015 bool stored
= FALSE
;
1016 x509cert_t
*cert
= certs
;
1021 exit_scepclient("multiple certs received, only first stored");
1022 if (!write_chunk(path
, "requested cert", cert
->certificate
, 0022, force
))
1023 exit_scepclient("could not write cert file '%s'", path
);
1026 certs
= certs
->next
;
1027 free_x509cert(cert
);
1029 filetype_out
&= ~CERT
; /* delete CERT flag */
1032 exit_scepclient(NULL
);
1033 return -1; /* should never be reached */