-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathestserver.c
More file actions
2763 lines (2534 loc) · 86.1 KB
/
estserver.c
File metadata and controls
2763 lines (2534 loc) · 86.1 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
/*------------------------------------------------------------------
* estserver.c - Example application that utilizes libest.so for
* EST server operations. libest does not manage
* sockets and pthreads. This responsibility is
* placed on the application. This module shows
* a fairly trivial example of how to setup a
* listening socket and server EST requests.
*
* November, 2012
*
* Copyright (c) 2012-2013, 2016, 2017, 2018 by cisco Systems, Inc.
* All rights reserved.
**------------------------------------------------------------------
*/
#include <stdio.h>
#ifndef WIN32
#include <pthread.h>
#endif
#include <stdint.h>
#ifndef DISABLE_TSEARCH
#include <search.h>
#endif
#ifdef WIN32
#include "../windows_util/getopt.h"
#include <windows.h>
#else
#include <getopt.h>
#endif
#include <openssl/err.h>
#include <openssl/engine.h>
#include <openssl/conf.h>
#include <openssl/ssl.h>
#include <openssl/bio.h>
#include <openssl/crypto.h>
#include <openssl/pem.h>
#include <openssl/md5.h>
#include <est.h>
#include "ossl_srv.h"
#include "../util/utils.h"
#include "../util/simple_server.h"
#include "../util/jsmn.h"
/*
* Abstract OpenSSL threading platform callbacks
*/
#ifdef WIN32
#define MUTEX_TYPE HANDLE
#define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL)
#define MUTEX_CLEANUP(x) CloseHandle(x)
#define MUTEX_LOCK(x) WaitForSingleObject((x), INFINITE)
#define MUTEX_UNLOCK(x) ReleaseMutex(x)
#define THREAD_ID GetCurrentThreadId()
#define snprintf _snprintf
#else
#define MUTEX_TYPE pthread_mutex_t
#define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL)
#define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x))
#define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
#define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
#define THREAD_ID pthread_self()
#endif
#define MAX_SERVER_LEN 255
#define MAX_FILENAME_LEN 255
#define MAX_REALM_LEN 32
#define DEFAULT_ENHCD_CERT_PWD "cisco"
#define DEFAULT_ENHCD_CERT_LOCAL_PKI_NID NID_commonName
/*
* The OpenSSL CA needs this BIO to send errors too
*/
BIO *bio_err = NULL;
/*
* These are the command line options with defaults provided below
*/
static int verbose = 0;
static int write_csr = 0;
static int crl = 0;
static int pop = 0;
static int v6 = 0;
static int srp = 0;
static int enforce_csr = 0;
static int manual_enroll = 0;
int coap_mode = 0;
#if HAVE_LIBCOAP
static int dtls_handshake_timeout = EST_DTLS_HANDSHAKE_TIMEOUT_DEF;
static int dtls_handshake_mtu = EST_DTLS_HANDSHAKE_MTU_DEF;
static int dtls_session_max = EST_DTLS_SESSION_MAX_DEF;
#endif
static int port_num = 8085;
static int http_digest_auth = 0;
static int http_basic_auth = 0;
static int http_token_auth = 0;
static int http_auth_disable = 0;
static int disable_forced_http_auth = 0;
static int enable_enhcd_cert_auth = 0;
static int set_cert_auth_ah_pwd = 0;
static EST_ECA_CSR_CHECK_FLAG enhcd_cert_csr_check_on = ECA_CSR_CHECK_OFF;
static int set_cert_auth_local_nid= 0;
static int set_cert_auth_mfg_name = 0;
static int set_enhcd_cert_truststore = 0;
static int set_cert_auth_mfg_nid = 0;
static int set_fips_return = 0;
static unsigned long set_fips_error = 0;
static int test_app_data = 0xDEADBEEF;
static char priv_key_pwd[MAX_PWD_LEN];
#if ENABLE_BRSKI
static int brski_mode = 0;
static int brski_ca_certs_len;
static unsigned char *brski_ca_certs;
static char masa_root_ca_file[EST_MAX_FILE_LEN + 1];
static char masa_priv_key_file[EST_MAX_FILE_LEN + 1];
static X509 *masa_ca_root;
static EVP_PKEY *masa_ca_priv_key;
static int masa_ca_enabled = 0;
#endif
static int perf_timers_on = 0;
char certfile[EST_MAX_FILE_LEN];
char keyfile[EST_MAX_FILE_LEN];
char cert_auth_ah_pwd[MAX_PWD_LEN + 1];
char local_nid[MAX_PWD_LEN + 1];
char mfg_name[MFG_NAME_MAX_LEN + 1];
char mfg_truststore_file[EST_MAX_FILE_LEN];
char mfg_nid[MAX_PWD_LEN + 1];
char realm[MAX_REALM];
unsigned char *cacerts_raw = NULL;
int cacerts_len = 0;
unsigned char *trustcerts = NULL;
int trustcerts_len = 0;
unsigned char *enhcd_cert_truststore = NULL;
int enhcd_cert_truststore_len = 0;
SRP_VBASE *srp_db = NULL;
static char valid_token_value[MAX_AUTH_TOKEN_LEN + 1];
/*
* This is the single EST context we need for operating
* the EST server. Only a single context is required.
*/
EST_CTX *ectx;
/*
* We hard-code the DH parameters here. THIS SHOULD NOT
* be done in a real application. The DH parameters need
* to be generated at the time of product installation so
* that every instance of the product in the field has
* unique parameters. Otherwise DH key exchange would be
* vulnerable to attack.
* See the OpenSSL documentation on generating DH parameters
* for more information.
*/
static DH *get_dh1024dsa ()
{
static unsigned char dh1024_p[]={
0xC8,0x00,0xF7,0x08,0x07,0x89,0x4D,0x90,0x53,0xF3,0xD5,0x00,
0x21,0x1B,0xF7,0x31,0xA6,0xA2,0xDA,0x23,0x9A,0xC7,0x87,0x19,
0x3B,0x47,0xB6,0x8C,0x04,0x6F,0xFF,0xC6,0x9B,0xB8,0x65,0xD2,
0xC2,0x5F,0x31,0x83,0x4A,0xA7,0x5F,0x2F,0x88,0x38,0xB6,0x55,
0xCF,0xD9,0x87,0x6D,0x6F,0x9F,0xDA,0xAC,0xA6,0x48,0xAF,0xFC,
0x33,0x84,0x37,0x5B,0x82,0x4A,0x31,0x5D,0xE7,0xBD,0x52,0x97,
0xA1,0x77,0xBF,0x10,0x9E,0x37,0xEA,0x64,0xFA,0xCA,0x28,0x8D,
0x9D,0x3B,0xD2,0x6E,0x09,0x5C,0x68,0xC7,0x45,0x90,0xFD,0xBB,
0x70,0xC9,0x3A,0xBB,0xDF,0xD4,0x21,0x0F,0xC4,0x6A,0x3C,0xF6,
0x61,0xCF,0x3F,0xD6,0x13,0xF1,0x5F,0xBC,0xCF,0xBC,0x26,0x9E,
0xBC,0x0B,0xBD,0xAB,0x5D,0xC9,0x54,0x39,
};
static unsigned char dh1024_g[]={
0x3B,0x40,0x86,0xE7,0xF3,0x6C,0xDE,0x67,0x1C,0xCC,0x80,0x05,
0x5A,0xDF,0xFE,0xBD,0x20,0x27,0x74,0x6C,0x24,0xC9,0x03,0xF3,
0xE1,0x8D,0xC3,0x7D,0x98,0x27,0x40,0x08,0xB8,0x8C,0x6A,0xE9,
0xBB,0x1A,0x3A,0xD6,0x86,0x83,0x5E,0x72,0x41,0xCE,0x85,0x3C,
0xD2,0xB3,0xFC,0x13,0xCE,0x37,0x81,0x9E,0x4C,0x1C,0x7B,0x65,
0xD3,0xE6,0xA6,0x00,0xF5,0x5A,0x95,0x43,0x5E,0x81,0xCF,0x60,
0xA2,0x23,0xFC,0x36,0xA7,0x5D,0x7A,0x4C,0x06,0x91,0x6E,0xF6,
0x57,0xEE,0x36,0xCB,0x06,0xEA,0xF5,0x3D,0x95,0x49,0xCB,0xA7,
0xDD,0x81,0xDF,0x80,0x09,0x4A,0x97,0x4D,0xA8,0x22,0x72,0xA1,
0x7F,0xC4,0x70,0x56,0x70,0xE8,0x20,0x10,0x18,0x8F,0x2E,0x60,
0x07,0xE7,0x68,0x1A,0x82,0x5D,0x32,0xA2,
};
DH *dh;
#ifndef HAVE_OLD_OPENSSL
BIGNUM *p, *g;
#endif
if ((dh = DH_new()) == NULL) {
return (NULL);
}
#ifdef HAVE_OLD_OPENSSL
dh->p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL);
dh->g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL);
if ((dh->p == NULL) || (dh->g == NULL)) {
DH_free(dh);
return (NULL);
}
dh->length = 160;
return (dh);
#else
p = BN_bin2bn(dh1024_p, sizeof(dh1024_p), NULL);
g = BN_bin2bn(dh1024_g, sizeof(dh1024_g), NULL);
if ((p == NULL) || (g == NULL)) {
DH_free(dh);
return (NULL);
}
DH_set0_pqg(dh, p, NULL, g);
return (dh);
#endif
}
static int string_password_cb (char *buf, int size, int wflag, void *data)
{
strncpy(buf,priv_key_pwd, size);
return(strnlen(buf, size));
}
static void print_version (FILE *fp)
{
fprintf(fp, "Using %s\n", SSLeay_version(SSLEAY_VERSION));
}
static void show_usage_and_exit (void)
{
fprintf(stderr, "\nAvailable EST server options\n"
" -v Verbose operation\n"
" -c <file> PEM file to use for server cert\n"
" -k <file> PEM file to use for server key\n"
" -r <value> HTTP realm to present to clients. Max is 32 characters.\n"
" -l Enable CRL checks\n"
" -t Enable check for binding client PoP to the TLS UID\n"
" -m <seconds> Simulate manual CA enrollment\n"
" -n Disable HTTP authentication (TLS client auth required)\n"
" -o Disable HTTP authentication when TLS client auth succeeds\n"
" -h Use HTTP Digest auth instead of Basic auth\n"
" -b Use HTTP Basic auth. Causes explicit call to set Basic auth\n"
" -p <num> TCP port number to listen on\n"
#ifndef DISABLE_PTHREADS
" -d <seconds> Sleep timer to auto-shut the server\n"
#endif
" -f Runs EST Server in FIPS MODE = ON\n"
" -6 Enable IPv6\n"
" -w Dump the CSR to '/tmp/csr.p10' allowing for manual attribute capture on server\n"
" -? Print this help message and exit\n"
" --keypass_stdin Specify en-/decryption of private key, password read from STDIN\n"
" --keypass_arg Specify en-/decryption of private key, password read from argument\n"
" --srp <file> Enable TLS-SRP authentication of client using the specified SRP parameters file\n"
" --enforce-csr Enable CSR attributes enforcement. The client must provide all the attributes in the CSR.\n"
" --token <value> Use HTTP Bearer Token auth.\n"
" --enhcd_cert_auth Enable Enhanced Certificate Auth mode\n"
" --enhcd_cert_local_nid <nid> Sets the local PKI domain subject field NID to \n"
" grab from the peer cert. If not set the\n"
" commonName NID will be used\n"
" --cert_auth_ah_pwd <value> Specify the auth header password to use\n"
" in Enhanced Certificate Auth mode\n"
" --cert_auth_csr_check_on Enable the CSR check during Enhanced Cert Auth\n"
" --enhcd_cert_mfg_name <name> Sets name of the manufacturer to be registered\n"
" This name is required when registering a manufacturer\n"
" --enhcd_cert_mfg_truststore <file> Specifies a truststore file for an Enhanced\n"
" Certificate Auth manufacturer to select the\n"
" subject field based upon. This truststore is\n"
" required when registering a manufacturer\n"
" --enhcd_cert_mfg_nid <nid> Sets the subject field NID to\n"
" grab from the peer cert when that cert came\n"
" from the manufacturer. If not set the\n"
" commonName NID will be used\n"
#if ENABLE_BRSKI
" --enable-brski Enable BRSKI bootstrapping support.\n"
#endif
#ifdef HAVE_LIBCOAP
" --enable-coap Enable EST over CoAP support.\n"
" --dtls-handshake-timeout Set the intial value of the DTLS handshake timeout.\n"
" --dtls-handshake-mtu Set the MTU used during DTLS handshake phase.\n"
" --dtls-session-max Set the maximum number of DTLS sessions.\n"
#endif
" --perf-timers-on Enable the performance timers in server\n"
"\n");
exit(255);
}
#ifndef DISABLE_TSEARCH
/*
* The functions in this section implement a simple lookup table
* to correlate incoming cert requests after a retry operation.
* We use this to simulate the manual-enrollment mode on the CA.
*
* FIXME: we need a cleanup routine to clear the tree when this
* server shuts down. Currently any remaining entries
* in the table will not be released, resulting in a memory
* leak in the valgrind output.
*/
typedef struct {
unsigned char *data; //this will hold the pub key from the cert request
int length;
} LOOKUP_ENTRY;
LOOKUP_ENTRY *lookup_root = NULL;
/*
* Used to compare two entries in the lookup table to correlate
* incoming cert requests in the case of a retry operation.
* We use the public key from the cert as the index into the
* lookup table.
*/
int compare (const void *pa, const void *pb)
{
LOOKUP_ENTRY *a = (LOOKUP_ENTRY *) pa;
LOOKUP_ENTRY *b = (LOOKUP_ENTRY *) pb;
if (a->length > b->length) {
return 1;
}
if (a->length < b->length) {
return -1;
}
return (memcmp(a->data, b->data, a->length));
}
/*
* We use a simple lookup table to simulate manual enrollment
* of certs by the CA. This is the case where an operator
* needs to review each cert request and approve it (e.g.
* auto-enrollment is off).
*
* Return 1 if a match was found and the enrollment operation
* should proceed. Return 0 if no match was found, in which
* case we'll add the public key from the cert request into
* our lookup table so it can be correlated later.
*/
int lookup_pkcs10_request (unsigned char *pkcs10, int p10_len)
{
X509_REQ *req = NULL;
BIO *in = NULL;
BIO *out = NULL;
BIO *b64;
EVP_PKEY *pkey = NULL;
BUF_MEM *bptr;
int rv;
LOOKUP_ENTRY *l;
LOOKUP_ENTRY *n;
/*
* Decode the request into an X509_REQ structure
*/
b64 = BIO_new(BIO_f_base64());
in = BIO_new_mem_buf(pkcs10, p10_len);
in = BIO_push(b64, in);
if ((req = d2i_X509_REQ_bio(in, NULL)) == NULL) {
/* Unable to parse the request, just let this fall through
* and the enrollment will fail */
rv = 1;
goto DONE;
}
/*
* Get the public key from the request, this will be our index into
* the lookup table. Frankly, I'm not sure how a real CA
* would do this lookup. But this should be good enough for
* testing the retry-after logic.
*/
#ifdef HAVE_OLD_OPENSSL
pkey = X509_PUBKEY_get(req->req_info->pubkey);
#else
pkey = X509_PUBKEY_get(X509_REQ_get_X509_PUBKEY(req));
#endif
if (!pkey) {
rv = 1;
goto DONE;
}
out = BIO_new(BIO_s_mem());
PEM_write_bio_PUBKEY(out, pkey);
BIO_get_mem_ptr(out, &bptr);
/*
* see if we can find a match for this public key
*/
n = malloc(sizeof(LOOKUP_ENTRY));
n->data = malloc(bptr->length);
n->length = bptr->length;
memcpy(n->data, bptr->data, n->length);
l = tfind(n, (void **) &lookup_root, compare);
if (l) {
/* We have a match, allow the enrollment */
rv = 1;
tdelete(n, (void **) &lookup_root, compare);
if (verbose)
printf("\nRemoving key from lookup table:\n");
dumpbin((unsigned char*) n->data, n->length);
free(n->data);
free(n);
} else {
/* Not a match, add it to the list and return */
l = tsearch(n, (void **) &lookup_root, compare);
rv = 0;
if (verbose)
printf("\nAdding key to lookup table:\n");
dumpbin((unsigned char*) n->data, n->length);
}
DONE: if (out)
BIO_free_all(out);
if (in)
BIO_free_all(in);
if (req)
X509_REQ_free(req);
if (pkey)
EVP_PKEY_free(pkey);
return (rv);
}
#else
/*
* The functions in this section implement a simple lookup table
* to correlate incoming cert requests after a retry operation
* without the use of the search library. We use this to simulate
* the manual-enrollment mode on the CA.
*
* FIXME: we need a cleanup routine to clear the tree when this
* server shuts down. Currently any remaining entries
* in the table will not be released, resulting in a memory
* leak in the valgrind output.
*/
struct lookup_entry {
unsigned char *data; //this will hold the pub key from the cert request
int length;
struct lookup_entry * next;
};
typedef struct lookup_entry LOOKUP_ENTRY;
/*
* This is the head of our linked list
*/
struct lookup_entry *lookup_root = NULL;
static int compare(const void *pa, const void *pb)
{
LOOKUP_ENTRY *a = (LOOKUP_ENTRY *)pa;
struct lookup_entry *b = (LOOKUP_ENTRY *)pb;
if (a->length > b->length) return 1;
if (a->length < b->length) return -1;
return (memcmp(a->data, b->data, a->length));
}
static void free_lookup(void *node)
{
LOOKUP_ENTRY *n = (LOOKUP_ENTRY *)node;
if (n->data) free(n->data);
free(n);
}
static LOOKUP_ENTRY * search_list(LOOKUP_ENTRY *head, LOOKUP_ENTRY* target) {
LOOKUP_ENTRY * tmp = head;
LOOKUP_ENTRY * tmp_prev = NULL;
while (tmp && compare(tmp, target)) {
tmp_prev = tmp;
tmp = tmp->next;
}
if (tmp == NULL) {
return NULL;
}
return tmp;
}
static LOOKUP_ENTRY * delete_lookup_entry(LOOKUP_ENTRY *head, LOOKUP_ENTRY * target)
{
LOOKUP_ENTRY *tmp = head;
LOOKUP_ENTRY *tmp_prev = NULL;
/* look for the node that matches d, but also remember the node
that points to it, tmp_prev, so that we can create a new link
*/
while (tmp && compare(tmp, target))
{
tmp_prev = tmp;
tmp = tmp->next;
}
/* did we fail to find the node? */
if (tmp == NULL)
return NULL;
/* otherwise, remove the node */
if (tmp == head)
{
/* remove head of list */
head = head->next;
}
else
{
tmp_prev->next = tmp->next;
}
/* free matching node */
free_lookup(tmp);
return head;
}
static void add_entry(LOOKUP_ENTRY * head, LOOKUP_ENTRY *new_node) {
LOOKUP_ENTRY * tmp = head;
if (tmp->next == NULL) {
tmp->next = new_node;
}
else {
while (TRUE) {
if (tmp->next == NULL) {
tmp->next = new_node;
break;
}
tmp = tmp->next;
}
}
}
static void destroy_lookup_table(LOOKUP_ENTRY * head) {
LOOKUP_ENTRY * tmp;
while (head) {
tmp = head;
head = head->next;
free_lookup(tmp);
}
}
/*
* We use a simple lookup table to simulate manual enrollment
* of certs by the CA. This is the case where an operator
* needs to review each cert request and approve it (e.g.
* auto-enrollment is off).
*
* Return 1 if a match was found and the enrollment operation
* should proceed. Return 0 if no match was found, in which
* case we'll add the public key from the cert request into
* our lookup table so it can be correlated later.
*
* Windows: Rewriting to forgo the use of search.h API
* lookup table will be implemented as a basic linked list
*/
static int lookup_pkcs10_request(unsigned char *pkcs10, int p10_len)
{
X509_REQ *req = NULL;
BIO *in = NULL;
BIO *out = NULL;
BIO *b64;
EVP_PKEY *pkey = NULL;
BUF_MEM *bptr;
int rv;
LOOKUP_ENTRY *l;
LOOKUP_ENTRY *n;
/*
* Decode the request into an X509_REQ structure
*/
b64 = BIO_new(BIO_f_base64());
in = BIO_new_mem_buf(pkcs10, p10_len);
in = BIO_push(b64, in);
if ((req = d2i_X509_REQ_bio(in, NULL)) == NULL) {
/* Unable to parse the request, just let this fall through
* and the enrollment will fail */
rv = 1;
goto DONE;
}
/*
* Get the public key from the request, this will be our index into
* the lookup table. Frankly, I'm not sure how a real CA
* would do this lookup. But this should be good enough for
* testing the retry-after logic.
*/
#ifdef HAVE_OLD_OPENSSL
pkey = X509_PUBKEY_get(req->req_info->pubkey);
#else
pkey = X509_PUBKEY_get(X509_REQ_get_X509_PUBKEY(req));
#endif
if (!pkey) {
rv = 1;
goto DONE;
}
out = BIO_new(BIO_s_mem());
PEM_write_bio_PUBKEY(out, pkey);
BIO_get_mem_ptr(out, &bptr);
/*
* see if we can find a match for this public key
*/
n = malloc(sizeof(LOOKUP_ENTRY));
n->data = malloc(bptr->length);
n->length = bptr->length;
memcpy(n->data, bptr->data, n->length);
n->next = NULL;
l = search_list(lookup_root, n);
if (l) {
/* We have a match, allow the enrollment */
rv = 1;
lookup_root = delete_lookup_entry(lookup_root, n);
printf("\nRemoving key from lookup table:\n");
dumpbin((char*)n->data, n->length);
free(n->data);
free(n);
}
else {
/* Not a match, add it to the list and return */
if (lookup_root == NULL) {
/*
* Initialize the list
*/
lookup_root = n;
}
else {
add_entry(lookup_root, n);
}
rv = 0;
printf("\nAdding key to lookup table:\n");
dumpbin((char*)n->data, n->length);
}
DONE:
if (out)
BIO_free_all(out);
if (in)
BIO_free_all(in);
if (req)
X509_REQ_free(req);
if (pkey)
EVP_PKEY_free(pkey);
return (rv);
}
#endif
/*
* Trivial utility function to extract the string
* value of the subject name from a cert.
*/
static void extract_sub_name (X509 *cert, char *name, int len)
{
X509_NAME *subject_nm;
BIO *out;
BUF_MEM *bm;
subject_nm = X509_get_subject_name(cert);
out = BIO_new(BIO_s_mem());
X509_NAME_print_ex(out, subject_nm, 0, XN_FLAG_SEP_SPLUS_SPC);
BIO_get_mem_ptr(out, &bm);
strncpy(name, bm->data, len);
if (bm->length < len) {
name[bm->length] = 0;
} else {
name[len] = 0;
}
BIO_free(out);
}
/****************************************************************************************
* The following functions are the callbacks used by libest to bind
* the EST stack to the HTTP/SSL layer and the CA server.
***************************************************************************************/
#ifndef WIN32
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
#else
static CRITICAL_SECTION enrollment_critical_section;
#endif
#define MAX_CERT_LEN 8192
/*
* Callback function used by EST stack to process a PKCS10
* enrollment request with the CA. The parameters are:
*
* pkcs10 Contains the CSR that should be sent to
* the CA to be signed.
* pkcs10_len Length of the CSR char array
* pcks7 Should contain the signed PKCS7 certificate
* from the CA server. You'll need allocate
* space and copy the cert into this char array.
* pkcs7_len Length of the pkcs7 char array, you will set this.
* user_id If HTTP authentication was used to identify the
* EST client, this will contain the user ID supplied
* by the client.
* peer_cert If the EST client presented a certificate to identify
* itself during the TLS handshake, this parameter will
* contain that certificate.
* path_seg If the incoming request contains a path segment it
* is extracted from the URI and passed here. Typically
* used to mux between multiple CAs or to identify a
* specific profile to use by the CA.
* app_data an optional pointer to information that is to be
* used by the application layer.
*
*/
int process_pkcs10_enrollment (unsigned char * pkcs10, int p10_len,
unsigned char **pkcs7, int *pkcs7_len,
char *user_id, X509 *peer_cert, char *path_seg,
void *app_data)
{
BIO *result = NULL;
char *buf;
#ifndef WIN32
int rc;
#endif
char sn[64];
char file_name[MAX_FILENAME_LEN];
fprintf(stderr, "Entering %s\n", __FUNCTION__);
if (verbose) {
/*
* Informational only
*/
if (user_id) {
/*
* Should be safe to log the user ID here since HTTP auth
* has succeeded at this point.
*/
printf("\n%s - User ID is %s\n", __FUNCTION__, user_id);
}
if (peer_cert) {
memset(sn, 0, 64);
extract_sub_name(peer_cert, sn, 64);
printf("\n%s - Peer cert CN is %s\n", __FUNCTION__, sn);
}
if (app_data) {
printf("ex_data value is %x\n", *((unsigned int *) app_data));
}
if (path_seg) {
printf("\nPath segment was included in enrollment URI. "
"Path Segment = %s\n", path_seg);
}
}
/*
* If we're simulating manual certificate enrollment,
* the CA will not automatically sign the cert request.
* We'll attempt to lookup in our local table if this
* cert has already been sent to us, if not, add it
* to the table and send the 'retry' message back to the
* client. But if this cert request has been seen in the
* past, then we'll continue with the enrollment.
* To summarize, we're simulating manual enrollment by
* forcing the client to request twice, and we'll automatically
* enroll on the second request.
*/
if (manual_enroll) {
if (lookup_pkcs10_request(pkcs10, p10_len)) {
/*
* We've seen this cert request in the past.
* Remove it from the lookup table and allow
* the enrollment to continue.
* Fall-thru to enrollment logic below
*/
} else {
/*
* Couldn't find this request, it's the first time
* we've seen it. Therefore, send the retry
* response.
*/
return (EST_ERR_CA_ENROLL_RETRY);
}
}
#ifndef WIN32
rc = pthread_mutex_lock(&m);
if (rc) {
printf("\nmutex lock failed rc=%d", rc);
exit(1);
}
#else
EnterCriticalSection(&enrollment_critical_section);
#endif
if (write_csr) {
/*
* Dump out pkcs10 to a file, this will contain a list of the OIDs in the CSR.
*/
snprintf(file_name, MAX_FILENAME_LEN, "/tmp/csr.p10");
write_binary_file(file_name, pkcs10, p10_len);
}
result = ossl_simple_enroll(pkcs10, p10_len);
#ifndef WIN32
rc = pthread_mutex_unlock(&m);
if (rc) {
printf("\nmutex unlock failed rc=%d", rc);
exit(1);
}
#else
LeaveCriticalSection(&enrollment_critical_section);
#endif
/*
* The result is a BIO containing the pkcs7 signed certificate
* Need to convert it to char and copy the results so we can
* free the BIO.
*/
*pkcs7_len = BIO_get_mem_data(result, (char**) &buf);
if (*pkcs7_len > 0 && *pkcs7_len < MAX_CERT_LEN) {
*pkcs7 = malloc(*pkcs7_len);
memcpy(*pkcs7, buf, *pkcs7_len);
}
BIO_free_all(result);
return EST_ERR_NONE;
}
/*
* Callback function used by EST to generate a private key
*
* p_priv_key contains a pointer to the key we will populate
*/
static int generate_private_key (EVP_PKEY **p_priv_key)
{
EVP_PKEY *priv_key = NULL;
RSA *rsa = NULL;
BIGNUM *bn = NULL;
BIO *out = NULL;
int rv = EST_ERR_NONE;
if (!p_priv_key) {
rv = EST_ERR_INVALID_PARAMETERS;
goto end;
}
rsa = RSA_new();
if (!rsa) {
rv = EST_ERR_MALLOC;
printf("***ESTCLIENT [ERROR][generate_private_key]--> Failed to allocate RSA struct");
goto end;
}
bn = BN_new();
if (!bn) {
rv = EST_ERR_MALLOC;
printf("***ESTCLIENT [ERROR][generate_private_key]--> Failed to allocate BN struct");
goto end;
}
BN_set_word(bn, 0x10001);
RSA_generate_key_ex(rsa, 4096, bn, NULL);
out = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPrivateKey(out,rsa,NULL,NULL,0,NULL,NULL);
priv_key = PEM_read_bio_PrivateKey(out, NULL, NULL, NULL);
if (priv_key == NULL) {
rv = EST_ERR_PEM_READ;
printf("Error while reading PEM encoded private key BIO: ");
goto end;
}
*p_priv_key = priv_key;
end:
if (out) {
BIO_free(out);
}
if (rsa) {
RSA_free(rsa);
}
if (bn) {
BN_free(bn);
}
return rv;
}
/*
* Callback function used by EST stack to process a PKCS10
* enrollment request with the CA. The parameters are:
*
* pkcs10 Contains the CSR that should be sent to
* the CA to be signed.
* pkcs10_len Length of the CSR char array
* pcks7 Should contain the signed PKCS7 certificate
* from the CA server. You'll need allocate
* space and copy the cert into this char array.
* pkcs7_len Length of the pkcs7 char array, you will set this.
* pkcs8 Should contain the signed PKCS8 key
* from the EST server context. You'll need allocate
* space and copy the cert into this char array.
* pkcs8_len Length of the pkcs8 char array, you will set this.
* user_id If HTTP authentication was used to identify the
* EST client, this will contain the user ID supplied
* by the client.
* peer_cert If the EST client presented a certificate to identify
* itself during the TLS handshake, this parameter will
* contain that certificate.
* path_seg If the incoming request contains a path segment it
* is extracted from the URI and passed here. Typically
* used to mux between multiple CAs or to identify a
* specific profile to use by the CA.
* app_data an optional pointer to information that is to be
* used by the application layer.
*
*/
static int process_srvr_side_keygen_pkcs10_enrollment (unsigned char * pkcs10, int p10_len,
unsigned char **pkcs7, int *pkcs7_len,
unsigned char **pkcs8, int *pkcs8_len,
char *user_id, X509 *peer_cert, char *path_seg,
void *app_data)
{
BIO *result = NULL;
char *buf;
#ifndef WIN32
int rc;
#endif
char sn[64];
char file_name[MAX_FILENAME_LEN];
if (verbose) {
/*
* Informational only
*/
if (user_id) {
/*
* Should be safe to log the user ID here since HTTP auth
* has succeeded at this point.
*/
printf("\n%s - User ID is %s\n", __FUNCTION__, user_id);
}
if (peer_cert) {
memset(sn, 0, 64);
extract_sub_name(peer_cert, sn, 64);
printf("\n%s - Peer cert CN is %s\n", __FUNCTION__, sn);
}
if (app_data) {
printf("ex_data value is %x\n", *((unsigned int *) app_data));
}
if (path_seg) {
printf("\nPath segment was included in enrollment URI. "
"Path Segment = %s\n", path_seg);
}
}
/*
* If we're simulating manual certificate enrollment,
* the CA will not automatically sign the cert request.
* We'll attempt to lookup in our local table if this
* cert has already been sent to us, if not, add it
* to the table and send the 'retry' message back to the
* client. But if this cert request has been seen in the
* past, then we'll continue with the enrollment.
* To summarize, we're simulating manual enrollment by
* forcing the client to request twice, and we'll automatically
* enroll on the second request.
*/
if (manual_enroll) {
if (lookup_pkcs10_request(pkcs10, p10_len)) {
/*
* We've seen this cert request in the past.
* Remove it from the lookup table and allow
* the enrollment to continue.
* Fall-thru to enrollment logic below
*/
} else {
/*
* Couldn't find this request, it's the first time
* we've seen it. Therefore, send the retry
* response.
*/
return (EST_ERR_CA_ENROLL_RETRY);
}
}
#ifndef WIN32
rc = pthread_mutex_lock(&m);
if (rc) {
printf("\nmutex lock failed rc=%d", rc);
exit(1);
}
#else
EnterCriticalSection(&enrollment_critical_section);
#endif
if (write_csr) {
/*
* Dump out pkcs10 to a file, this will contain a list of the OIDs in the CSR.
*/
snprintf(file_name, MAX_FILENAME_LEN, "/tmp/csr.p10");
write_binary_file(file_name, pkcs10, p10_len);
}
result = ossl_simple_enroll(pkcs10, p10_len);
#ifndef WIN32
rc = pthread_mutex_unlock(&m);
if (rc) {
printf("\nmutex unlock failed rc=%d", rc);
exit(1);
}
#else
LeaveCriticalSection(&enrollment_critical_section);