-
Notifications
You must be signed in to change notification settings - Fork 30
/
ssl_utils.c
2387 lines (1986 loc) · 63.7 KB
/
ssl_utils.c
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
/*
* ssl_utils.c
*
* Routines for interacting directly with SSL, X509 certificates, etc.
*/
#include "myproxy_common.h" /* all needed headers included here */
#ifndef MAXPATHLEN
#define MAXPATHLEN 4096
#endif
#define PEM_CALLBACK(func) func, NULL
#define PEM_NO_CALLBACK NULL, NULL
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#define EVP_PKEY_id(k) (k)->type
#endif
#ifndef MYPROXY_PRIVKEY_CIPHER
#define MYPROXY_PRIVKEY_CIPHER() EVP_aes_256_cbc()
#endif
/**********************************************************************
*
* Constants
*
*/
#define PROXY_DEFAULT_LIFETIME -1L /* magic # for lifetime */
/* of signing cert */
/**********************************************************************
*
* Internal data structures
*
*/
struct _ssl_credentials
{
X509 *certificate;
EVP_PKEY *private_key;
STACK_OF(X509) *certificate_chain;
globus_gsi_proxy_handle_t proxy_req;
};
struct _ssl_proxy_restrictions
{
/* 0 = unrestricted, 1 = limited */
int limited_proxy;
/* Proxy lifetime in seconds, 0 means default, -1 means maximum */
long lifetime;
};
/**********************************************************************
*
* Internal variables.
*
*/
/*
* Holder for pass phrase so callback function can find it.
*/
static const char *_ssl_pass_phrase = NULL;
/**********************************************************************
*
* Internal functions.
*
*/
/*
* ssl_error_to_verror()
*
* Transfer an error description out of the ssl error handler to verror.
*/
void
ssl_error_to_verror()
{
unsigned long error;
ERR_STATE *error_state;
const char *error_data;
int error_number;
while ((error = ERR_peek_error()) != 0)
{
/* Find data for last error */
error_state = ERR_get_state();
error_number = (error_state->bottom + 1) % ERR_NUM_ERRORS;
error_data = error_state->err_data[error_number];
/* Now add to verror state */
verror_put_string("%s", ERR_error_string(error, NULL));
if (error_data != NULL)
{
verror_put_string("%s", error_data);
}
/* Pop error off of stack */
ERR_get_error();
}
ERR_clear_error();
}
/*
* globus_error_to_verror()
*
* Transfer an error description out of the Globus error handler to verror.
*/
void
globus_error_to_verror(globus_result_t result)
{
globus_object_t *error;
char *desc;
error = globus_error_get(result);
if (!error) return;
desc = globus_error_print_chain(error);
globus_object_free(error);
if (!desc) return;
verror_put_string("%s", desc);
free(desc);
}
/*
* bio_from_buffer()
*
* Given a buffer of length buffer_len, return a memory bio with the
* contents of the buffer.
*
* Returns pointer to bio on success, NULL on error.
*/
static BIO *
bio_from_buffer(const unsigned char *buffer,
int buffer_len)
{
BIO *bio = NULL;
assert(buffer != NULL);
bio = BIO_new(BIO_s_mem());
if (bio == NULL)
{
verror_put_string("Failed creating memory BIO");
ssl_error_to_verror();
goto error;
}
if (BIO_write(bio, (unsigned char *) buffer, buffer_len) == SSL_ERROR)
{
verror_put_string("Failed writing buffer to BIO");
ssl_error_to_verror();
BIO_free(bio);
bio = NULL;
goto error;
}
error:
return bio;
}
/*
* bio_to_buffer()
*
* Given a bio return the contents of the bio in a buffer.
* pbuffer is set to point to the allocated buffer, and pbuffer_len
* is filled in with the buffer length. Caller should free *pbuffer.
*
* Returns SSL_SUCCESS or SSL_ERROR.
*/
static int
bio_to_buffer(BIO *bio,
unsigned char **pbuffer,
int *pbuffer_len)
{
char *buffer = NULL;
int buffer_len;
int return_status = SSL_ERROR;
assert(bio != NULL);
buffer_len = BIO_pending(bio);
buffer = malloc(buffer_len+1);
memset(buffer, '\0', buffer_len+1);
if (buffer == NULL)
{
verror_put_string("Failed dumping BIO to buffer (malloc() failed)");
verror_put_errno(errno);
goto error;
}
if (BIO_read(bio, buffer, buffer_len) == SSL_ERROR)
{
verror_put_string("Failed dumping BIO to buffer (BIO_read() failed)");
ssl_error_to_verror();
goto error;
}
/* Success */
*pbuffer = (unsigned char *)buffer;
*pbuffer_len = buffer_len;
return_status = SSL_SUCCESS;
error:
if (return_status == SSL_ERROR)
{
if (buffer != NULL)
{
free(buffer);
}
}
return return_status;
}
/*
* ssl_cert_chain_free()
*
* Free the given certificate chain and all it contents.
*/
static void
ssl_cert_chain_free(STACK_OF(X509) *cert_chain)
{
if (cert_chain != NULL)
{
sk_X509_pop_free(cert_chain, X509_free);
}
}
/*
* ssl_credentials_free_contents()
*
* Free all the contents of the given credentials without freeing
* the credentials structure itself.
*/
static void
ssl_credentials_free_contents(SSL_CREDENTIALS *creds)
{
if (creds != NULL)
{
if (creds->certificate != NULL)
{
X509_free(creds->certificate);
}
if (creds->private_key != NULL)
{
EVP_PKEY_free(creds->private_key);
}
if (creds->certificate_chain != NULL)
{
ssl_cert_chain_free(creds->certificate_chain);
}
}
}
static int
creds_from_bio(BIO *bio, SSL_CREDENTIALS **creds)
{
STACK_OF(X509) *cert_chain = NULL;
X509 *cert = NULL;
unsigned char number_of_certs;
int cert_index;
int return_status = SSL_ERROR;
if (BIO_read(bio, &number_of_certs, sizeof(number_of_certs)) == SSL_ERROR) {
verror_put_string("Failed unpacking chain from buffer"
"(reading number of certificates)");
ssl_error_to_verror();
return SSL_ERROR;
}
if (number_of_certs == 0) {
verror_put_string("Failed unpacking chain from buffer"
"(number of certificates is zero)");
ssl_error_to_verror();
return SSL_ERROR;
}
cert = d2i_X509_bio(bio, NULL /* make new cert */);
if (cert == NULL) {
verror_put_string("Failed unpacking chain from buffer"
"(reading user's certificate)");
ssl_error_to_verror();
goto end;
}
/* Now read the certificate chain */
cert_chain = sk_X509_new_null();
for (cert_index = 1; cert_index < number_of_certs; cert_index++) {
X509 *x509;
x509 = d2i_X509_bio(bio, NULL /* make new cert */);
if (x509 == NULL) {
verror_put_string("Failed unpacking chain from buffer"
"(reading certificate)");
ssl_error_to_verror();
goto end;
}
if (sk_X509_push(cert_chain, x509) == SSL_ERROR) {
verror_put_string("Failed unpacking chain from buffer"
"(building a new chain)");
ssl_error_to_verror();
X509_free(x509);
goto end;
}
}
*creds = ssl_credentials_new();
if (*creds == NULL) {
verror_put_string("Failed unpacking chain from buffer"
"(building a new chain)");
goto end;
}
(*creds)->certificate_chain = cert_chain;
cert_chain = NULL;
(*creds)->certificate = cert;
cert = NULL;
return_status = SSL_SUCCESS;
end:
if (cert)
X509_free(cert);
if (cert_chain)
ssl_cert_chain_free(cert_chain);
return return_status;
}
static int
creds_to_bio(SSL_CREDENTIALS *chain, BIO **bio)
{
unsigned char number_of_certs;
BIO *output_bio = NULL;
int index;
int return_status = SSL_ERROR;
output_bio = BIO_new(BIO_s_mem());
if (output_bio == NULL) {
verror_put_string("BIO_new() failed");
ssl_error_to_verror();
return SSL_ERROR;
}
number_of_certs = 1;
if (chain->certificate_chain != NULL)
number_of_certs += sk_X509_num(chain->certificate_chain);
if (BIO_write(output_bio, &number_of_certs,sizeof(number_of_certs)) == SSL_ERROR) {
verror_put_string("Failed dumping chain to buffer"
"(BIO_write() failed)");
ssl_error_to_verror();
goto end;
}
if (i2d_X509_bio(output_bio, chain->certificate) == SSL_ERROR) {
verror_put_string("Failed dumping chain to buffer "
"(write of user's certificate failed)");
ssl_error_to_verror();
goto end;
}
for (index = 0; index < sk_X509_num(chain->certificate_chain); index++) {
X509 *cert;
cert = (X509 *) sk_X509_value(chain->certificate_chain, index);
if (i2d_X509_bio(output_bio, cert) == SSL_ERROR) {
verror_put_string("Failed dumping chain to buffer "
"(write of cert chain failed)");
ssl_error_to_verror();
goto end;
}
}
*bio = output_bio;
output_bio = NULL;
return_status = SSL_SUCCESS;
end:
if (output_bio)
BIO_free(output_bio);
return return_status;
}
/*
* my_init()
*
* Do any needed initialization for these routines.
* Should be called first. Can be called multiple times.
*/
static void
my_init()
{
static int my_inited = 0;
if (my_inited == 0)
{
my_inited = 1;
/* Initialize the ssleay libraries */
SSL_load_error_strings();
SSL_library_init();
globus_module_activate(GLOBUS_GSI_PROXY_MODULE);
globus_module_activate(GLOBUS_GSI_CREDENTIAL_MODULE);
globus_module_activate(GLOBUS_GSI_SYSCONFIG_MODULE);
globus_module_activate(GLOBUS_GSI_CERT_UTILS_MODULE);
}
}
/*
* my_pass_phrase_callback()
*
* Callback from PEM_read_PrivateKey() in ssl_load_user_key()
* to return the passphrase stored in _ssl_pass_phrase.
*/
static int
my_pass_phrase_callback(char *buffer,
int buffer_len,
int verify /* Ignored */,
void *u)
{
/* SSL libs supply these, make sure they are reasonable */
assert(buffer != NULL);
assert(buffer_len > 0);
if (_ssl_pass_phrase == NULL)
{
strcpy(buffer, "");
}
else
{
strncpy(buffer, _ssl_pass_phrase, buffer_len);
buffer[buffer_len - 1] = '\0';
}
return strlen(buffer);
}
/*
* ssl_x509_request_to_buffer()
*
* Dump the given X509 request structure to an allocated buffer.
*
* Returns SSL_SUCCESS or SSL_ERROR
*/
static int
ssl_x509_request_to_buffer(X509_REQ *request,
unsigned char **buffer,
int *buffer_length)
{
int return_status = SSL_ERROR;
BIO *bio = NULL;
assert(request != NULL);
assert(buffer != NULL);
assert(buffer_length != NULL);
bio = BIO_new(BIO_s_mem());
if (bio == NULL)
{
verror_put_string("Failed dumping X509 request to buffer (BIO_new() failed)");
ssl_error_to_verror();
goto error;
}
if (i2d_X509_REQ_bio(bio, request) == SSL_ERROR)
{
verror_put_string("Failed dumping X509 request to buffer");
ssl_error_to_verror();
goto error;
}
if (bio_to_buffer(bio, buffer, buffer_length) == SSL_ERROR)
{
goto error;
}
/* Success */
return_status = SSL_SUCCESS;
error:
if (bio != NULL)
{
BIO_free(bio);
}
return return_status;
}
/**********************************************************************
*
* API Functions
*
*/
void
ssl_credentials_destroy(SSL_CREDENTIALS *creds)
{
my_init();
if (creds != NULL)
{
ssl_credentials_free_contents(creds);
free(creds);
}
}
int
ssl_proxy_file_destroy(const char *proxyfile)
{
FILE *fp;
long offset, i;
char zero = '\0';
struct stat s;
int return_status = SSL_ERROR;
assert(proxyfile != NULL);
fp = fopen(proxyfile, "r+");
if (!fp) {
verror_put_string("fopen(%s): %s\n", proxyfile, strerror(errno));
goto error;
}
/* Don't get fooled into zeroing out the wrong file via tricks
with links and the like. */
if (fstat(fileno(fp), &s) < 0) {
verror_put_string("fstat(%s): %s\n", proxyfile, strerror(errno));
goto error;
}
if (S_ISDIR(s.st_mode)) {
verror_put_string("proxy file %s is a directory!\n", proxyfile);
goto error;
}
if (!S_ISREG(s.st_mode)) {
verror_put_string("proxy file %s is not a regular file!\n",
proxyfile);
goto error;
}
if (s.st_nlink != 1) {
verror_put_string("proxy file %s has links!\n", proxyfile);
goto error;
}
if (fseek(fp, 0L, SEEK_END) < 0) {
verror_put_string("fseek(%s): %s\n", proxyfile, strerror(errno));
goto error;
}
offset = ftell(fp);
if (offset < 0) {
verror_put_string("ftell(%s): %s\n", proxyfile, strerror(errno));
goto error;
}
if (fseek(fp, 0L, SEEK_SET) < 0) {
verror_put_string("fseek(%s): %s\n", proxyfile, strerror(errno));
goto error;
}
for (i=0; i < offset; i++) {
if (fwrite(&zero, 1, 1, fp) != 1) {
verror_put_string("fwrite(%s): %s\n", proxyfile,
strerror(errno));
goto error;
}
}
return_status = SSL_SUCCESS;
error:
if (fp) fclose(fp);
if (unlink(proxyfile) < 0) { /* always try to unlink it, even on error */
verror_put_string("unlink: %s\n", strerror(errno));
return SSL_ERROR;
}
return return_status;
}
int
ssl_certificate_load_from_file(SSL_CREDENTIALS *creds,
const char *path)
{
FILE *cert_file = NULL;
X509 *cert = NULL;
int return_status = SSL_ERROR;
STACK_OF(X509) *cert_chain = NULL;
assert(creds != NULL);
assert(path != NULL);
my_init();
cert_file = fopen(path, "r");
if (cert_file == NULL)
{
verror_put_string("Error opening certificate file %s", path);
verror_put_errno(errno);
goto error;
}
if ((cert = PEM_read_X509(cert_file, NULL, PEM_NO_CALLBACK)) == NULL)
{
verror_put_string("Error reading certificate %s", path);
ssl_error_to_verror();
goto error;
}
if (creds->certificate != NULL)
{
X509_free(creds->certificate);
}
creds->certificate = cert;
/* Ok, now read the certificate chain */
/* Create empty stack */
cert_chain = sk_X509_new_null();
while (1)
{
cert = NULL;
ERR_clear_error(); /* clear any prior OpenSSL errors */
if ((cert = PEM_read_X509(cert_file, NULL, PEM_NO_CALLBACK)) == NULL)
{
/*
* If we just can't find a start line then we've reached EOF.
*/
if (ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE)
{
/* Just EOF, clear error and break out of loop */
ERR_clear_error();
break;
}
/* Actual error */
verror_put_string("Error parsing certificate chain");
ssl_error_to_verror();
goto error;
}
/* Add to chain */
if (sk_X509_insert(cert_chain, cert,
sk_X509_num(cert_chain)) == SSL_ERROR)
{
verror_put_string("Error parsing certificate chain");
ssl_error_to_verror();
goto error;
}
} /* while(1) */
creds->certificate_chain = cert_chain;
/* Success */
return_status = SSL_SUCCESS;
error:
if (cert_file != NULL)
{
fclose(cert_file);
}
return return_status;
}
int
ssl_certificate_push(SSL_CREDENTIALS *creds,
X509 *cert)
{
assert(creds != NULL);
assert(cert != NULL);
/* Place the given cert on top of other certs in creds */
if (creds->certificate != NULL) {
if (creds->certificate_chain == NULL)
creds->certificate_chain = sk_X509_new_null();
if (sk_X509_insert(creds->certificate_chain, creds->certificate, 0)
== SSL_ERROR) {
verror_put_string("Error inserting certificate into creds cert chain");
ssl_error_to_verror();
return SSL_ERROR;
}
}
creds->certificate = cert;
return SSL_SUCCESS;
}
int
ssl_private_key_load_from_file(SSL_CREDENTIALS *creds,
const char *path,
const char *pass_phrase,
const char *pass_phrase_prompt)
{
FILE *key_file = NULL;
EVP_PKEY *key = NULL;
int return_status = SSL_ERROR;
assert(creds != NULL);
assert(path != NULL);
my_init();
/*
* Put pass phrase where the callback function can find it.
*/
_ssl_pass_phrase = pass_phrase;
if (pass_phrase_prompt) EVP_set_pw_prompt((char *)pass_phrase_prompt);
key_file = fopen(path, "r");
if (key_file == NULL)
{
verror_put_string("Error opening key file %s", path);
verror_put_errno(errno);
goto error;
}
if ((key = PEM_read_PrivateKey(key_file, NULL, (pass_phrase_prompt) ?
NULL : my_pass_phrase_callback, NULL)) == NULL)
{
unsigned long error, reason;
error = ERR_peek_error();
reason = ERR_GET_REASON(error);
/* If this is a bad password, return a better error message */
if (reason == EVP_R_BAD_DECRYPT ||
reason == PEM_R_BAD_PASSWORD_READ)
{
verror_put_string("Bad password");
}
else
{
verror_put_string("Error reading private key %s", path);
ssl_error_to_verror();
}
goto error;
}
if (creds->private_key != NULL)
{
EVP_PKEY_free(creds->private_key);
}
creds->private_key = key;
/* Success */
return_status = SSL_SUCCESS;
error:
if (key_file != NULL)
{
fclose(key_file);
}
_ssl_pass_phrase = NULL;
return return_status;
}
int
ssl_private_key_store_to_file(SSL_CREDENTIALS *creds,
const char *path,
const char *pass_phrase)
{
BIO *keybio = 0;
const EVP_CIPHER *cipher;
int pass_phrase_len;
int return_status = SSL_ERROR;
keybio = BIO_new_file(path, "w");
if (!keybio) {
verror_put_string("failed to open %s", path);
goto error;
}
if (pass_phrase == NULL)
{
/* No encryption */
cipher = NULL;
pass_phrase_len = 0;
}
else
{
/* Encrypt with pass phrase */
cipher = MYPROXY_PRIVKEY_CIPHER();
pass_phrase_len = strlen(pass_phrase);
}
/* Replaced PEM_write_bio_PrivateKey() with PEM_ASN1_write_bio() because
starting with OpenSSL 1.0 PEM_write_bio_PrivateKey() wouldn't put "RSA"
in "BEGIN RSA PRIVATE KEY" that could cause some grid utilities and such
to fail. We should probably still consider reverting back to
PEM_write_bio_PrivateKey() in the future as PEM_write_bio_PrivateKey()
uses PEM_write_bio_PKCS8PrivateKey() which "uses the more more secure
PKCS#8 private key format with a high iteration count" per the CHANGES
file in the openssl tree */
if (PEM_ASN1_write_bio((int (*)())i2d_PrivateKey,
((EVP_PKEY_id(creds->private_key) == EVP_PKEY_DSA)?
PEM_STRING_DSA:PEM_STRING_RSA),
keybio, (void *)creds->private_key, cipher,
(unsigned char *) pass_phrase,
pass_phrase_len,
PEM_NO_CALLBACK) == SSL_ERROR)
{
verror_put_string("Error packing private key");
ssl_error_to_verror();
goto error;
}
return_status = SSL_SUCCESS;
error:
if (keybio) BIO_free(keybio);
return return_status;
}
int
ssl_private_key_is_encrypted(const char *path)
{
FILE *key_file = NULL;
EVP_PKEY *key = NULL;
int return_status = -1;
my_init();
key_file = fopen(path, "r");
if (key_file == NULL) {
verror_put_string("Error opening key file %s", path);
verror_put_errno(errno);
goto cleanup; /* error */
}
_ssl_pass_phrase = NULL;
ERR_clear_error();
if ((key = PEM_read_PrivateKey(key_file, NULL,
PEM_CALLBACK(my_pass_phrase_callback))) == NULL)
{
return_status = 1; /* key is encrypted */
}
else
{
return_status = 0; /* key is unencrypted */
}
cleanup:
if (key_file) fclose(key_file);
if (key) EVP_PKEY_free(key);
ERR_clear_error();
return return_status; /* key unencrypted */
}
int
ssl_proxy_from_pem(SSL_CREDENTIALS *creds,
const unsigned char *buffer,
int buffer_len,
const char *pass_phrase)
{
BIO *bio = NULL;
X509 *cert = NULL;
EVP_PKEY *key = NULL;
STACK_OF(X509) *cert_chain = NULL;
int return_status = SSL_ERROR;
assert(creds != NULL);
assert(buffer != NULL);
my_init();
/*
* Put pass phrase where the callback function can find it.
*/
_ssl_pass_phrase = pass_phrase;
ERR_clear_error();
bio = bio_from_buffer(buffer, buffer_len);
if (bio == NULL)
{
goto error;
}
/*
* Proxy file contains proxy certificate followed by proxy
* private key, followed by the certificate chain.
*/
/* Read proxy certificate */
if (PEM_read_bio_X509(bio, &cert, PEM_NO_CALLBACK) == NULL)
{
verror_put_string("Error parsing proxy certificate");
ssl_error_to_verror();
goto error;
}
/* Read proxy private key */
if ((key = PEM_read_bio_PrivateKey(bio, NULL,
PEM_CALLBACK(my_pass_phrase_callback))) == NULL)
{
unsigned long error, reason;
error = ERR_peek_error();
reason = ERR_GET_REASON(error);
/* If this is a bad password, return a better error message */
if (reason == EVP_R_BAD_DECRYPT ||
reason == PEM_R_BAD_PASSWORD_READ)
{
verror_put_string("Bad password");
}
else
{
verror_put_string("Error parsing private key");
ssl_error_to_verror();
}
goto error;
}
/* Ok, now read the certificate chain */
/* Create empty stack */
cert_chain = sk_X509_new_null();
while (1)
{
X509 *certificate = NULL;
ERR_clear_error(); /* clear any prior OpenSSL errors */
if (PEM_read_bio_X509(bio, &certificate,
PEM_NO_CALLBACK) == NULL)
{
/*
* If we just can't find a start line then we've reached EOF.
*/
if (ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE)
{
/* Just EOF, clear error and break out of loop */
ERR_clear_error();
break;
}
/* Actual error */
verror_put_string("Error parsing certificate chain from proxy");
ssl_error_to_verror();
goto error;
}
/* Add to chain */
if (sk_X509_insert(cert_chain, certificate,
sk_X509_num(cert_chain)) == SSL_ERROR)
{
verror_put_string("Error parsing certificate chain from proxy");
ssl_error_to_verror();
goto error;
}
} /* while(1) */
/*
* Ok, everything has been successfully read, now store it into
* creds, removing any existing contents.
*/
ssl_credentials_free_contents(creds);
creds->private_key = key;
creds->certificate = cert;
creds->certificate_chain = cert_chain;