-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathestclient.c
More file actions
1240 lines (1096 loc) · 39 KB
/
estclient.c
File metadata and controls
1240 lines (1096 loc) · 39 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*------------------------------------------------------------------
* estclient.c - Example application that utilizes libest.a for
* EST client operations. This module utilizes OpenSSL
* for SSL and crypto services.
*
*
* November, 2012
*
* Copyright (c) 2012-2013 by cisco Systems, Inc.
* All rights reserved.
**------------------------------------------------------------------
*/
/* Main routine */
#include "stdio.h"
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/crypto.h>
#include <strings.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <est.h>
#include "../util/utils.h"
#include "../util/crl.c" // TODO include properly into Makefile
#define EST_UT_MAX_CMD_LEN 255
#define MAX_SERVER_LEN 255
#define MAX_FILENAME_LEN 255
#define MAX_CN 64
/*
* The CRL retrieval code in crl.c and client_manual_cert_verify() need this BIO to send errors to
*/
BIO *bio_err = NULL;
/*
* Global variables to hold command line options
*/
static char est_http_uid[MAX_UID_LEN];
static char est_http_pwd[MAX_PWD_LEN];
static char est_srp_uid[MAX_UID_LEN];
static char est_srp_pwd[MAX_PWD_LEN];
static char subj_cn[MAX_CN];
static char est_server[MAX_SERVER_LEN];
static char est_auth_token[MAX_AUTH_TOKEN_LEN+1];
static int est_port;
static int verbose = 0;
static int enable_crl = 0;
static int require_crl = 0;
static int use_cdp = 0;
static char default_cdp[MAX_FILENAME_LEN] = "";
static int srp = 0;
static int token_auth_mode = 0;
static int pem_out = 0;
static char csr_file[MAX_FILENAME_LEN];
static char priv_key_file[MAX_FILENAME_LEN];
static char client_key_file[MAX_FILENAME_LEN];
static char client_cert_file[MAX_FILENAME_LEN];
static int read_timeout = EST_SSL_READ_TIMEOUT_DEF;
static unsigned char *new_pkey = NULL;
static int new_pkey_len = 0;
static unsigned char *cacerts = NULL;
static int cacerts_len = 0;
static char out_dir[MAX_FILENAME_LEN];
static int enroll = 0;
static int getcsr = 0;
static int getcert = 0;
static int reenroll = 0;
static int force_pop = 0;
static unsigned char *c_cert = NULL;
static unsigned char *c_key = NULL;
static int c_cert_len = 0;
static int c_key_len = 0;
EVP_PKEY *priv_key = NULL;
EVP_PKEY *client_priv_key = NULL;
X509 *client_cert = NULL;
/*
* This is a simple callback used to override the default
* logging facility in libest.
*/
static void test_logger_stdout (char *format, va_list l)
{
vprintf(format, l);
fflush(stdout);
}
static void print_version ()
{
printf("Using %s\n", SSLeay_version(SSLEAY_VERSION));
}
static void show_usage_and_exit (void)
{
printf("estclient \n");
printf("Usage:\n");
fprintf(stderr, "\nAvailable EST client options\n"
" -v Verbose operation\n"
" -g Get CA certificate from EST server\n"
" -e Enroll with EST server and request a cert\n"
" -a Get CSR attributes from EST server\n"
" -z Force binding the PoP by including the challengePassword in the CSR\n"
" -r Re-enroll with EST server and request a cert, must use -c option\n"
" -c <certfile> Identity certificate to use for the TLS session, also the cert that will\n"
" be used when doing a re-enroll operation\n"
" -k <keyfile> Use with -c option to specify private key for the identity cert\n"
" -x <keyfile> Use existing private key in the given file for signing the CSR\n"
" -y <csrfile> Use existing CSR in the given file\n"
" -s <server> Enrollment server IP address\n"
" -p <port> TCP port number for enrollment server\n"
" -l[+] Enable CRL checks; +: require strict CRL checking\n"
" -L Use CDPs, which may be specified in certificates, for CRL checking\n"
" --cdp <url> Default CDP; URLs may start with 'http://' or 'file:'\n"
" -o <dir> Directory where pkcs7 certs will be written\n"
" -w <count> Timeout in seconds to wait for server response (default=10)\n" //EST_SSL_READ_TIMEOUT_DEF
" -f Runs EST Client in FIPS MODE = ON\n"
" -u <string> Specify user name for HTTP authentication\n"
" -h <string> Specify password for HTTP authentication\n"
" -? Print this help message and exit\n"
" --common-name <string> Specify the common name to use in the Suject Name field of the new certificate.\n"
" 127.0.0.1 will be used if this option is not specified\n"
" --pem-output Convert the new certificate to PEM format\n"
" --srp Enable TLS-SRP cipher suites. Use with --srp-user and --srp-password options\n"
" --srp-user <string> Specify the SRP user name\n"
" --srp-password <string> Specify the SRP password\n"
" --auth-token <string> Specify the token to be used with HTTP token authentication.\n"
"\n");
exit(255);
}
/*
* When the -x option isn't used from the CLI, we will implicitly generate
* an RSA key to be used to sign the CSR.
*/
static unsigned char * generate_private_key (int *key_len)
{
RSA *rsa = RSA_new();
BIGNUM *bn = BN_new();
BIO *out;
unsigned char *tdata;
unsigned char *key_data;
BN_set_word(bn, 0x10001);
RSA_generate_key_ex(rsa, SRP_MINIMAL_N, bn, NULL);
out = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPrivateKey(out, rsa, NULL, NULL, 0, NULL, NULL);
*key_len = BIO_get_mem_data(out, &tdata);
key_data = malloc(*key_len + 1);
memcpy(key_data, tdata, *key_len);
BIO_free(out);
RSA_free(rsa);
BN_free(bn);
return (key_data);
}
/*
* Takes as input the name of the file to write the cert to on the
* local file system (full path name expected).
* The cert_data argument should contain the PKCS7 base64 encoded
* certificate, with the cert_len argument specifying the length
* of the cert. This routine will either write the cert to the
* local file system "as is", or it will convert the cert to
* PEM format and write it as a PEM file.
*/
static void save_cert (char *file_name, unsigned char *cert_data, int cert_len)
{
int pem_len;
unsigned char *pem;
char full_file_name[MAX_FILENAME_LEN];
if (pem_out) {
pem_len = est_convert_p7b64_to_pem(cert_data, cert_len, &pem);
if (pem_len > 0) {
snprintf(full_file_name, MAX_FILENAME_LEN, "%s.%s", file_name, "pem");
write_binary_file(full_file_name, pem, pem_len);
free(pem);
}
} else {
snprintf(full_file_name, MAX_FILENAME_LEN, "%s.%s", file_name, "pkcs7");
write_binary_file(full_file_name, cert_data, cert_len);
}
}
/*
* auth_credentials_token_cb() is the application layer callback function that will
* return a token based authentication credential when called. It's registered
* with the EST Client using the est_client_set_auth_cred_cb().
* The test function is required to set some global values in order to make this
* callback operate the way that the test case wants.
* - auth_cred_force_error = tell this function to force a response code error
* - test_token = pointer to a hard coded string that is the token string to return
*
* This callback must provide the token credentials in a heap based buffer, and
* ownership of that buffer is implicitly transferred to the ET client library upon
* return.
*/
static
EST_HTTP_AUTH_CRED_RC auth_credentials_token_cb (EST_HTTP_AUTH_HDR *auth_credentials)
{
char *token_ptr = NULL;
int token_len = 0;
printf("\nHTTP Token authentication credential callback invoked from EST client library\n");
if (auth_credentials->mode == AUTH_TOKEN) {
/*
* If the test_token is set to anything, then we need to allocate
* space from the heap and copy in the value.
*/
if (est_auth_token[0] != '\0') {
token_len = strlen(est_auth_token);
if (token_len == 0) {
printf("\nError determining length of token string used for credentials\n");
return EST_HTTP_AUTH_CRED_NOT_AVAILABLE;
}
token_ptr = malloc(token_len+1);
if (token_ptr == NULL){
printf("\nError allocating token string used for credentials\n");
return EST_HTTP_AUTH_CRED_NOT_AVAILABLE;
}
strncpy(token_ptr, est_auth_token, strlen(est_auth_token));
token_ptr[token_len] = '\0';
}
/*
* If we made it this far, token_ptr is pointing to a string
* containing the token to be returned. Assign it and return success
*/
auth_credentials->auth_token = token_ptr;
printf("Returning access token = %s\n\n", auth_credentials->auth_token);
return (EST_HTTP_AUTH_CRED_SUCCESS);
}
return (EST_HTTP_AUTH_CRED_NOT_AVAILABLE);
}
static int client_manual_cert_verify (X509 *cur_cert, int openssl_cert_error)
{
if (openssl_cert_error == X509_V_ERR_UNABLE_TO_GET_CRL && !require_crl) { // this part could now be handled by the library alone
return 1; // accepted
}
int approve = 0;
/*
* Print out the specifics of this cert
*/
printf("%s: OpenSSL/EST server cert verification failed with the following error: openssl_cert_error = %d (%s)\n",
__FUNCTION__, openssl_cert_error,
X509_verify_cert_error_string(openssl_cert_error));
printf("Failing Cert:\n");
X509_print_fp(stdout, cur_cert);
/*
* Next call prints out the signature which can be used as the fingerprint
* This fingerprint can be checked against the anticipated value to determine
* whether or not the server's cert should be approved.
*/
X509_signature_print(bio_err, cur_cert->sig_alg, cur_cert->signature);
return approve;
}
/* read_csr() is a helper function that reads a PEM encoded
CSR from a file and converts its contents to an OpenSSL X509_REQ.
The csr_file argument is the name of the file containing the PEM encoded CSR.
This function reads the given file and converts its PEM encoded contents to
the OpenSSL X509_REQ structure. This function will return NULL if the PEM/DER
data is corrupted or unable to be parsed by the OpenSSL library.
This function will allocate memory for the X509_REQ data. You must free the
memory in your application when it's no longer needed by calling X509_REQ_free().
See also the more general est_read_x509_request function.
returns X509_REQ*
*/
static X509_REQ *read_csr (char *csr_file)
{
BIO *csrin;
X509_REQ *csr;
/*
* Read in the csr
*/
csrin = BIO_new(BIO_s_file_internal());
if (BIO_read_filename(csrin, csr_file) <= 0) {
printf("\nUnable to read CSR file %s\n", csr_file);
return (NULL);
}
/*
* This reads in the csr file, which is expected to be PEM encoded
*/
csr = PEM_read_bio_X509_REQ(csrin, NULL, NULL, NULL);
if (csr == NULL) {
printf("\nError while reading PEM encoded CSR file %s\n", csr_file);
ERR_print_errors_fp(stderr);
return (NULL);
}
BIO_free(csrin);
return (csr);
}
static int simple_enroll_attempt (EST_CTX *ectx)
{
int pkcs7_len = 0;
int rv;
char file_name[MAX_FILENAME_LEN];
unsigned char *new_client_cert;
X509_REQ *csr = NULL;
if (force_pop) {
rv = est_client_force_pop(ectx);
if (rv != EST_ERR_NONE) {
printf("\nFailed to enable force PoP");
}
}
if (csr_file[0]) {
csr = read_csr(csr_file);
if (csr == NULL) {
rv = EST_ERR_PEM_READ;
}else {
rv = est_client_enroll_csr(ectx, csr, &pkcs7_len, NULL);
}
}else {
rv = est_client_enroll(ectx, subj_cn, &pkcs7_len, priv_key);
}
if (csr) {
X509_REQ_free(csr);
}
if (verbose) {
printf("\nenrollment rv = %d (%s) with pkcs7 length = %d\n",
rv, EST_ERR_NUM_TO_STR(rv), pkcs7_len);
}
if (rv == EST_ERR_NONE) {
/*
* client library has obtained the new client certificate.
* now retrieve it from the library
*/
new_client_cert = malloc(pkcs7_len);
if (new_client_cert == NULL) {
if (verbose) {
printf("\nmalloc of destination buffer for enrollment cert failed\n");
}
return (EST_ERR_MALLOC);
}
rv = est_client_copy_enrolled_cert(ectx, new_client_cert);
if (verbose) {
printf("\nenrollment copy rv = %d\n", rv);
}
if (rv == EST_ERR_NONE) {
/*
* Enrollment copy worked, dump the pkcs7 cert to stdout
*/
if (verbose) {
dumpbin(new_client_cert, pkcs7_len);
}
}
snprintf(file_name, MAX_FILENAME_LEN, "%s/newcert", out_dir);
save_cert(file_name, new_client_cert, pkcs7_len);
free(new_client_cert);
}
return (rv);
}
/*
* Routine used to CSR for est_client_enroll_csr testcases
*/
static int populate_x509_csr (X509_REQ *req, EVP_PKEY *pkey, char *cn)
{
X509_NAME *subj;
/* Setup version number */
if (!X509_REQ_set_version(req, 0L)) {
printf("\nUnable to set X509 version#\n");
return (-1);
}
/*
* Add Common Name entry
*/
subj = X509_REQ_get_subject_name(req);
if (!X509_NAME_add_entry_by_txt(subj, "CN", MBSTRING_ASC,
(unsigned char*)cn, -1, -1, 0)) {
printf("\nUnable to create X509 Common Name entry\n");
return (-1);
}
/*
* Set the public key on the request
*/
if (!X509_REQ_set_pubkey(req, pkey)) {
printf("\nUnable to set X509 public key\n");
return (-1);
}
return (0);
}
static EVP_PKEY *read_private_key (char *key_file)
{
BIO *keyin;
EVP_PKEY *priv_key;
/*
* Read in the private key
*/
keyin = BIO_new(BIO_s_file_internal());
if (BIO_read_filename(keyin, key_file) <= 0) {
printf("\nUnable to read private key file %s\n", key_file);
return (NULL);
}
/*
* This reads in the private key file, which is expected to be a PEM
* encoded private key. If using DER encoding, you would invoke
* d2i_PrivateKey_bio() instead.
*/
priv_key = PEM_read_bio_PrivateKey(keyin, NULL, NULL, NULL);
if (priv_key == NULL) {
printf("\nError while reading PEM encoded private key file %s\n", key_file);
ERR_print_errors_fp(stderr);
return (NULL);
}
BIO_free(keyin);
return (priv_key);
}
static int regular_csr_attempt (EST_CTX *ectx)
{
int rv;
unsigned char *attr_data = NULL;
int attr_len;
char file_name[MAX_FILENAME_LEN];
/*
* Just get the CSR attributes
*/
rv = est_client_get_csrattrs(ectx, &attr_data, &attr_len);
if (rv != EST_ERR_NONE) {
printf("\nWarning: CSR attributes were not available");
} else {
snprintf(file_name, MAX_FILENAME_LEN, "%s/csr.base64", out_dir);
write_binary_file(file_name, attr_data, attr_len);
}
return (rv);
}
static int regular_enroll_attempt (EST_CTX *ectx)
{
int pkcs7_len = 0;
int rv;
char file_name[MAX_FILENAME_LEN];
unsigned char *new_client_cert;
unsigned char *attr_data = NULL;
unsigned char *der_ptr = NULL;
int attr_len, der_len, nid;
X509_REQ *csr;
/*
* We need to get the CSR attributes first, which allows libest
* to know if the challengePassword needs to be included in the
* CSR.
*/
rv = est_client_get_csrattrs(ectx, &attr_data, &attr_len);
if (rv != EST_ERR_NONE) {
printf("\nWarning: CSR attributes were not available");
return (rv);
}
/* Generate a CSR */
csr = X509_REQ_new();
if (csr == NULL) {
printf("\nFailed to get X509_REQ");
return (EST_ERR_NO_CSR);
}
rv = populate_x509_csr(csr, priv_key, "EST-client");
if (rv) {
printf("\nFailed to populate X509_REQ");
return (EST_ERR_X509_PUBKEY);
}
rv = est_decode_attributes_helper((char*)attr_data, attr_len, &der_ptr, &der_len);
if (rv != EST_ERR_NONE) {
printf("\nFailed to decode attributes");
return (rv);
}
while (der_len) {
rv = est_get_attributes_helper(&der_ptr, &der_len, &nid);
if (rv == EST_ERR_NONE) {
/*
* This switch can be enhanced to include all NID values
* of interest by the client/server. In addition the last
* parameter can be enhanced to provide the character string
* type information that is included with the NID.
*
* Presently only character string types are supported, but at
* some point OID or groups of strings/OIDs may need to be
* supported.
*
* Note that challenge password should not be included here
* as it is handled by libest client code.
*/
switch (nid) {
case NID_commonName:
/* add the attribute to the request */
rv = est_add_attributes_helper(csr, nid, "test\n", 0);
break;
case NID_pkcs9_emailAddress:
/* add the attribute to the request */
rv = est_add_attributes_helper(csr, nid, "bubba@notmyemail.com\0", 0);
break;
case NID_undef:
printf("\nNID is undefined; skipping it\n");
break;
default:
rv = est_add_attributes_helper(csr, nid, "", 0);
break;
}
if (rv != EST_ERR_NONE) {
printf("\n Error adding NID=%d", nid);
}
}
}
X509_REQ_print_fp(stderr, csr);
rv = est_client_enroll_csr(ectx, csr, &pkcs7_len, priv_key);
if (verbose) {
printf("\nenrollment rv = %d (%s) with pkcs7 length = %d\n",
rv, EST_ERR_NUM_TO_STR(rv), pkcs7_len);
}
if (rv == EST_ERR_NONE) {
/*
* client library has obtained the new client certificate.
* now retrieve it from the library
*/
new_client_cert = malloc(pkcs7_len);
if (new_client_cert == NULL) {
if (verbose) {
printf("\nmalloc of destination buffer for enrollment cert failed\n");
}
return (EST_ERR_MALLOC);
}
rv = est_client_copy_enrolled_cert(ectx, new_client_cert);
if (verbose) {
printf("\nenrollment copy rv = %d\n", rv);
}
if (rv == EST_ERR_NONE) {
/*
* Enrollment copy worked, dump the pkcs7 cert to stdout
*/
if (verbose) {
dumpbin(new_client_cert, pkcs7_len);
}
}
snprintf(file_name, MAX_FILENAME_LEN, "%s/newcert", out_dir);
save_cert(file_name, new_client_cert, pkcs7_len);
free(new_client_cert);
}
return (rv);
}
static void retry_enroll_delay (int retry_delay, time_t retry_time)
{
if (retry_delay != 0) {
if (verbose) {
printf("\nwaiting for retry period specified by server\n");
}
if (verbose) {
printf("\nduration can be set on estserver with -m <retry-period> (min is 60 seconds)\n");
}
sleep(retry_delay);
} else {
/*
* received a time_t value instead. Calculate the amount of time to wait.
* If it's in the past, then indicate that and proceed to the retry.
* If it's within 2 minutes from now, then go ahead and wait.
* If it's beyond 2 minutes from not, print out the date that was received and exit.
* If both values returned (retry_delay and retry_time) are both zero, this is
* incorrect. Output an message and exit.
*/
if (retry_time != 0) {
time_t current_time;
double secs_to_wait;
time(¤t_time);
secs_to_wait = difftime(retry_time, current_time);
if (secs_to_wait <= 0) {
if (verbose) {
printf("\nSpecified delay time is in the past. Proceed on to retry \n");
}
} else if (secs_to_wait <= 60 * 2) {
if (verbose) {
printf("\nSpecified delay time is 2 minutes or less. Wait the specified time before retry \n");
}
sleep(secs_to_wait);
} else {
if (verbose) {
printf("\nSpecified delay time is more than 2 minutes in the future. printing out the delay time and terminating\n");
}
printf(" Delay time received from the server is: %s \n", ctime(&retry_time));
return;
}
} else {
if (verbose) {
printf("\nERROR: both retry after values returned are zero\n");
}
return;
}
}
}
static STACK_OF(X509_CRL) *lookup_crls_cb(X509_STORE_CTX *ctx, X509_NAME *nm)
{
return load_crls_from_cdps(ctx, nm, default_cdp);
}
static void do_operation ()
{
EST_CTX *ectx;
unsigned char *pkcs7;
int pkcs7_len = 0;
int rv;
char file_name[MAX_FILENAME_LEN];
unsigned char *new_client_cert;
int retry_delay = 0;
time_t retry_time = 0;
char *operation;
ectx = est_client_init(cacerts, cacerts_len,
EST_CERT_FORMAT_PEM,
client_manual_cert_verify);
if (!ectx) {
printf("\nUnable to initialize EST context. Aborting!!!\n");
exit(1);
}
if (enable_crl) {
rv = est_enable_crl(ectx);
if (rv != EST_ERR_NONE) {
printf("\nUnable to enable CRL checking. Aborting!!!\n");
exit(1);
}
}
rv = est_set_crl_strictness_lookup(ectx, require_crl, use_cdp ? lookup_crls_cb : NULL);
if (rv != EST_ERR_NONE) {
printf("\nUnable to set CRL checking. Aborting!!!\n");
exit(1);
}
if (rv != EST_ERR_NONE) {
printf("\nUnable to enable CRL checking. Aborting!!!\n");
printf("EST error code %d (%s)\n", rv, EST_ERR_NUM_TO_STR(rv));
exit(1);
}
rv = est_client_set_read_timeout(ectx, read_timeout);
if (rv != EST_ERR_NONE) {
printf("\nUnable to configure read timeout from server. Aborting!!!\n");
printf("EST error code %d (%s)\n", rv, EST_ERR_NUM_TO_STR(rv));
exit(1);
}
rv = est_client_set_auth(ectx, est_http_uid, est_http_pwd, client_cert, client_priv_key);
if (rv != EST_ERR_NONE) {
printf("\nUnable to configure client authentication. Aborting!!!\n");
printf("EST error code %d (%s)\n", rv, EST_ERR_NUM_TO_STR(rv));
exit(1);
}
if (srp) {
rv = est_client_enable_srp(ectx, 1024, est_srp_uid, est_srp_pwd);
if (rv != EST_ERR_NONE) {
printf("\nUnable to enable SRP. Aborting!!!\n");
exit(1);
}
}
if (token_auth_mode) {
rv = est_client_set_auth_cred_cb(ectx, auth_credentials_token_cb);
if (rv != EST_ERR_NONE) {
printf("\nUnable to register token auth callback. Aborting!!!\n");
exit(1);
}
}
est_client_set_server(ectx, est_server, est_port);
if (getcert) {
operation = "Get CA Cert";
rv = est_client_get_cacerts(ectx, &pkcs7_len);
if (rv == EST_ERR_NONE) {
if (verbose) {
printf("\nGet CA Cert success\n");
}
/*
* allocate a buffer to retrieve the CA certs
* and get them copied in
*/
pkcs7 = malloc(pkcs7_len);
rv = est_client_copy_cacerts(ectx, pkcs7);
/*
* Dump the retrieved cert to stdout
*/
if (verbose) {
dumpbin(pkcs7, pkcs7_len);
}
/*
* Generate the output file name, which contains the thread ID
* and iteration number.
*/
snprintf(file_name, MAX_FILENAME_LEN, "%s/cacert.pkcs7", out_dir);
write_binary_file(file_name, pkcs7, pkcs7_len);
free(pkcs7);
}
}
if (enroll && getcsr) {
operation = "Regular enrollment with server-defined attributes";
rv = regular_enroll_attempt(ectx);
if (rv == EST_ERR_CA_ENROLL_RETRY) {
/*
* go get the retry period
*/
rv = est_client_copy_retry_after(ectx, &retry_delay, &retry_time);
if (verbose) {
printf("\nretry after period copy rv = %d "
"Retry-After delay seconds = %d "
"Retry-After delay time = %s\n",
rv, retry_delay, ctime(&retry_time) );
}
if (rv == EST_ERR_NONE) {
retry_enroll_delay(retry_delay, retry_time);
}
/*
* now that we're back, try to enroll again
*/
rv = regular_enroll_attempt(ectx);
}
} else if (enroll && !getcsr) {
operation = "Simple enrollment without server-defined attributes";
rv = simple_enroll_attempt(ectx);
if (rv == EST_ERR_CA_ENROLL_RETRY) {
/*
* go get the retry period
*/
rv = est_client_copy_retry_after(ectx, &retry_delay, &retry_time);
if (verbose) {
printf("\nretry after period copy rv = %d "
"Retry-After delay seconds = %d "
"Retry-After delay time = %s\n",
rv, retry_delay, ctime(&retry_time) );
}
if (rv == EST_ERR_NONE) {
retry_enroll_delay(retry_delay, retry_time);
}
/*
* now that we're back, try to enroll again
*/
rv = simple_enroll_attempt(ectx);
}
} else if (!enroll && getcsr) {
operation = "Get CSR attribues";
rv = regular_csr_attempt(ectx);
}
/* Split reenroll from enroll to allow both messages to be sent */
if (reenroll) {
operation = "Re-enrollment";
rv = est_client_reenroll(ectx, client_cert, &pkcs7_len, client_priv_key);
if (verbose) {
printf("\nreenroll rv = %d (%s) with pkcs7 length = %d\n",
rv, EST_ERR_NUM_TO_STR(rv), pkcs7_len);
}
if (rv == EST_ERR_NONE) {
/*
* client library has obtained the new client certificate.
* now retrieve it from the library
*/
new_client_cert = malloc(pkcs7_len);
if (new_client_cert == NULL) {
if (verbose) {
printf("\nmalloc of destination buffer for reenroll cert failed\n");
}
}
rv = est_client_copy_enrolled_cert(ectx, new_client_cert);
if (verbose) {
printf("\nreenroll copy rv = %d\n", rv);
}
if (rv == EST_ERR_NONE) {
/*
* Enrollment copy worked, dump the pkcs7 cert to stdout
*/
if (verbose) {
dumpbin(new_client_cert, pkcs7_len);
}
}
/*
* Generate the output file name, which contains the thread ID
* and iteration number.
*/
snprintf(file_name, MAX_FILENAME_LEN, "%s/newcert", out_dir);
save_cert(file_name, new_client_cert, pkcs7_len);
free(new_client_cert);
}
}
if (rv != EST_ERR_NONE) {
/*
* something went wrong.
*/
printf("\n%s failed with code %d (%s)\n",
operation, rv, EST_ERR_NUM_TO_STR(rv));
}
est_destroy(ectx);
ERR_clear_error();
ERR_remove_thread_state(NULL);
}
int main (int argc, char **argv)
{
signed char c;
int set_fips_return = 0;
char file_name[MAX_FILENAME_LEN];
BIO *keyin;
BIO *certin;
static struct option long_options[] = {
{ "trustanchor", 1, 0, 0 },
{ "cdp", 1, 0, 0 },
{ "srp", 0, 0, 0 },
{ "srp-user", 1, 0, 0 },
{ "srp-password", 1, 0, 0 },
{ "auth-token", 1, 0, 0 },
{ "common-name", 1, 0, 0 },
{ "pem-output", 0, 0, 0 },
{ NULL, 0, NULL, 0 }
};
int option_index = 0;
int trustanchor = 1; /* default to require a trust anchor */
char *trustanchor_file = NULL;
bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
if (!bio_err) {
printf("\nBIO not working\n");
exit(1);
}
est_http_uid[0] = 0x0;
est_http_pwd[0] = 0x0;
/*
* Set the default common name to put into the Subject field
*/
strncpy(subj_cn, "127.0.0.1", MAX_CN);
memset(csr_file, 0, 1);
memset(priv_key_file, 0, 1);
memset(client_key_file, 0, 1);
memset(client_cert_file, 0, 1);
memset(out_dir, 0, 1);
while ((c = getopt_long(argc, argv, "?zfvagerx:y:k:s:p:o:c:w:u:h:l::L", long_options, &option_index)) != -1) {
switch (c) {
case 0:
#if 0
printf("option %s", long_options[option_index].name);
if (optarg) {
printf(" with arg %s", optarg);
}
printf("\n");
#endif
if (!strncmp(long_options[option_index].name, "trustanchor", strlen("trustanchor"))) {
if (!strncmp(optarg, "no", strlen("no"))) {
trustanchor = 0;
} else {
trustanchor_file = optarg;
}
}
if (!strcmp(long_options[option_index].name, "cdp")) {
use_cdp = 1;
strncpy(default_cdp, optarg, MAX_FILENAME_LEN);
}
if (!strncmp(long_options[option_index].name, "srp", strlen("srp"))) {
srp = 1;
}
if (!strncmp(long_options[option_index].name, "srp-user", strlen("srp-user"))) {
strncpy(est_srp_uid, optarg, MAX_UID_LEN);
}
if (!strncmp(long_options[option_index].name, "srp-password", strlen("srp-password"))) {
strncpy(est_srp_pwd, optarg, MAX_PWD_LEN);
}
if (!strncmp(long_options[option_index].name,"auth-token", strlen("auth-token"))) {
strncpy(est_auth_token, optarg, MAX_AUTH_TOKEN_LEN);
token_auth_mode = 1;
}
if (!strncmp(long_options[option_index].name, "common-name", strlen("common-name"))) {
strncpy(subj_cn, optarg, MAX_CN);
}
if (!strncmp(long_options[option_index].name, "pem-output", strlen("pem-output"))) {
pem_out = 1;
}
break;
case 'v':
verbose = 1;
break;
case 'l':
enable_crl = 1;
if (optarg) {
if (!strcmp(optarg, "+")) {
require_crl = 1;
} else {
fprintf(stderr, "Unrecognized -l option parameter '%s'. Aborting!!!\n", optarg);
exit(1);
}
}
break;
case 'L':
use_cdp = 1;
break;
case 'z':
force_pop = 1;
break;
case 'a':
getcsr = 1;
break;
case 'g':
getcert = 1;
break;
case 'e':
enroll = 1;
break;
case 'r':
reenroll = 1;
break;
case 'u':
strncpy(est_http_uid, optarg, MAX_UID_LEN);
break;
case 'h':
strncpy(est_http_pwd, optarg, MAX_PWD_LEN);
break;
case 's':