-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenssl_cert.cpp
More file actions
5781 lines (5437 loc) · 178 KB
/
openssl_cert.cpp
File metadata and controls
5781 lines (5437 loc) · 178 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
/***************************************************************************
openssl_cert.c - description
-------------------
begin : Tue May 14 2002
copyright : (C) 2002 by ARRL
author : Jon Bloom
email : jbloom@arrl.org
revision : $Id$
***************************************************************************/
/* Routines to massage X.509 certs for tQSL. See openssl_cert.h for overview */
/* 2004-04-10 Fixed tqsl_add_bag_attribute error for OpenSSL > 0.96
(Thanks to Kenji, JJ1BDX for the fix)
*/
/* Portions liberally copied from OpenSSL's apps source code */
/* ====================================================================
* Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* openssl-core@openssl.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
#define OPENSSL_CERT_SOURCE
#define TQSLLIB_DEF
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <zlib.h>
#include <errno.h>
#include <ctype.h>
#include <stdlib.h>
#ifdef _WIN32
#include <direct.h>
#define MKDIR(x, y) _wmkdir(x)
#else
#define MKDIR(x, y) mkdir(x, y)
#include <unistd.h>
#include <dirent.h>
#endif
#define OPENSSL_SUPPRESS_DEPRECATED // Suppress warnings for deprecated functions
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#undef X509_NAME //http://www.mail-archive.com/openssl-users@openssl.org/msg59116.html
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/pkcs12.h>
#include <openssl/opensslv.h>
#include <openssl/bn.h>
#include <openssl/rsa.h>
#include <openssl/pkcs12.h>
/* Ugly workaround for Openssl 1.0 bug per:
* http://rt.openssl.org/Ticket/Display.html?user=guest&pass=guest&id=2123
*/
#if (OPENSSL_VERSION_NUMBER == 0x10000003L)
#define i2d_ASN1_SET i2d_ASN1_SET_buggy
#define d2i_ASN1_SET d2i_ASN1_SET_buggy
#define ASN1_seq_unpack ASN1_seq_unpack_buggy
#define ASN1_seq_pack ASN1_seq_pack_buggy
#include <openssl/asn1.h>
#undef i2d_ASN1_SET
#undef d2i_ASN1_SET
#undef ASN1_seq_unpack
#undef ASN1_seq_pack
#ifdef __cplusplus
extern "C" {
#endif
int i2d_ASN1_SET(void *a, unsigned char **pp,
i2d_of_void *i2d, int ex_tag, int ex_class,
int is_set);
void *d2i_ASN1_SET(void *a, const unsigned char **pp,
long length, d2i_of_void *d2i,
void (*free_func)(void* p), int ex_tag,
int ex_class);
void *ASN1_seq_unpack(const unsigned char *buf, int len,
d2i_of_void *d2i, void (*free_func)(void* dummy));
unsigned char *ASN1_seq_pack(void *safes, i2d_of_void *i2d,
unsigned char **buf, int *len);
#ifdef __cplusplus
}
#endif
#endif // OpenSSL v1.0
// Work with OpenSSL 1.1.0 and later
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
#ifndef M_PKCS12_bag_type
# define M_PKCS12_bag_type PKCS12_bag_type
#endif
#ifndef M_PKCS12_cert_bag_type
# define M_PKCS12_cert_bag_type PKCS12_cert_bag_type
#endif
#ifndef M_PKCS12_crl_bag_type
# define M_PKCS12_crl_bag_type PKCS12_cert_bag_type
#endif
#ifndef M_PKCS12_certbag2x509
# define M_PKCS12_certbag2x509 PKCS12_SAFEBAG_get1_cert
#endif
#ifndef M_PKCS12_decrypt_skey
# define M_PKCS12_decrypt_skey PKCS12_decrypt_skey
#endif
#ifndef M_PKCS12_unpack_authsafes
# define M_PKCS12_unpack_authsafes PKCS12_unpack_authsafes
#endif
#ifndef M_PKCS12_pack_authsafes
# define M_PKCS12_pack_authsafes PKCS12_pack_authsafes
#endif
#ifndef PKCS12_get_attr
# define PKCS12_get_attr PKCS12_SAFEBAG_get0_attr
#endif
#ifndef PKCS12_bag_type
# define PKCS12_bag_type PKCS12_SAFEBAG_get_nid
#endif
#ifndef PKCS12_cert_bag_type
# define PKCS12_cert_bag_type PKCS12_SAFEBAG_get_bag_nid
#endif
#if !defined(PKCS12_x5092certbag) && !defined(LIBRESSL_VERSION_NUMBER)
# define PKCS12_x5092certbag PKCS12_SAFEBAG_create_cert
#endif
#ifndef PKCS12_x509crl2certbag
# define PKCS12_x509crl2certbag PKCS12_SAFEBAG_create_crl
#endif
#ifndef X509_STORE_CTX_trusted_stack
# define X509_STORE_CTX_trusted_stack X509_STORE_CTX_set0_trusted_stack
#endif
#ifndef X509_get_notAfter
# define X509_get_notAfter X509_get0_notAfter
#endif
#ifndef X509_get_notBefore
# define X509_get_notBefore X509_get0_notBefore
#endif
#if !defined (PKCS12_MAKE_SHKEYBAG) && !defined(LIBRESSL_VERSION_NUMBER)
# define PKCS12_MAKE_SHKEYBAG PKCS12_SAFEBAG_create_pkcs8_encrypt
#endif
#ifndef X509_V_FLAG_CB_ISSUER_CHECK
# define X509_V_FLAG_CB_ISSUER_CHECK 0x0
#endif
#else
# define ASN1_STRING_get0_data ASN1_STRING_data
#endif
#include <map>
#include <vector>
#include <set>
#include <string>
#include <fstream>
#include <iostream>
#include "tqsllib.h"
#include "tqslerrno.h"
#include "xml.h"
#include "winstrdefs.h"
#ifdef _MSC_VER //is a visual studio compiler
#include "windirent.h"
#endif
#define tqsl_malloc malloc
#define tqsl_realloc realloc
#define tqsl_calloc calloc
#define tqsl_free free
#define TQSL_OBJ_TO_API(x) (reinterpret_cast<void *>((x)))
#define TQSL_API_TO_OBJ(x, type) ((type)(x))
#define TQSL_API_TO_CERT(x) TQSL_API_TO_OBJ((x), tqsl_cert *)
#include "openssl_cert.h"
using std::vector;
using std::map;
using std::set;
using std::string;
using std::ofstream;
using std::ios;
using std::endl;
using std::exception;
using tqsllib::XMLElement;
using tqsllib::XMLElementList;
#ifdef _WIN32
#define TQSL_OPEN_READ L"rb"
#define TQSL_OPEN_WRITE L"wb"
#define TQSL_OPEN_APPEND L"ab"
#else
#define TQSL_OPEN_READ "r"
#define TQSL_OPEN_WRITE "w"
#define TQSL_OPEN_APPEND "a"
#endif
#if (OPENSSL_VERSION_NUMBER & 0xfffff000) >= 0x10000000L
#define uni2asc OPENSSL_uni2asc
#define asc2uni OPENSSL_asc2uni
#endif
static char *tqsl_trim(char *buf);
static int tqsl_check_parm(const char *p, const char *parmName);
static TQSL_CERT_REQ *tqsl_copy_cert_req(TQSL_CERT_REQ *userreq);
static TQSL_CERT_REQ *tqsl_free_cert_req(TQSL_CERT_REQ *req, int seterr);
static char *tqsl_make_key_path(const char *callsign, char *path, int size);
static int tqsl_make_key_list(vector< map<string, string> > & keys);
static int tqsl_find_matching_key(X509 *cert, EVP_PKEY **keyp, TQSL_CERT_REQ **crq, const char *password, int (*cb)(char *, int, void *), void *);
static char *tqsl_make_cert_path(const char *filename, char *path, int size);
static char *tqsl_make_backup_path(const char *filename, char *path, int size);
static int tqsl_get_cert_ext(X509 *cert, const char *ext, unsigned char *userbuf, int *buflen, int *crit);
CLIENT_STATIC int tqsl_get_asn1_date(const ASN1_TIME *tm, tQSL_Date *date);
static char *tqsl_sign_base64_data(tQSL_Cert cert, char *b64data);
static int fixed_password_callback(char *buf, int bufsiz, int verify, void *userdata);
static int prompted_password_callback(char *buf, int bufsiz, int verify, void *userfunc);
static int tqsl_check_crq_field(tQSL_Cert cert, char *buf, int bufsiz);
static bool safe_strncpy(char *dest, const char *src, int size);
static int tqsl_ssl_error_is_nofile();
static int tqsl_unlock_key(const char *pem, EVP_PKEY **keyp, const char *password, int (*cb)(char *, int, void *), void *);
static int tqsl_replace_key(const char *callsign, const char *path, map<string, string>& newfields, int (*cb)(int, const char *, void *), void *userdata);
static int tqsl_self_signed_is_ok(int ok, X509_STORE_CTX *ctx);
static int tqsl_expired_is_ok(int ok, X509_STORE_CTX *ctx);
static int tqsl_clear_deleted(const char *callsign, const char *path, EVP_PKEY *cert_key);
static int tqsl_key_exists(const char *callsign, EVP_PKEY *cert_key);
static int tqsl_open_key_file(const char *path);
static int tqsl_read_key(map<string, string>& fields);
static void tqsl_close_key_file(void);
extern const char* tqsl_openssl_error(void);
/* Private data structures */
typedef struct {
long id;
X509 *cert;
EVP_PKEY *key;
TQSL_CERT_REQ *crq;
char *pubkey;
char *privkey;
unsigned char keyonly;
} tqsl_cert;
typedef struct {
long id;
X509 *cert;
} tqsl_crq;
static tqsl_cert * tqsl_cert_new();
static void tqsl_cert_free(tqsl_cert *p);
static int tqsl_cert_check(tqsl_cert *p, bool needcert = true);
struct tqsl_loadcert_handler {
int type;
int (*func)(const char *pem, X509 *x, int(*cb)(int type, const char *, void *), void *);
};
static int tqsl_handle_root_cert(const char *, X509 *, int (*cb)(int, const char *, void *), void *);
static int tqsl_handle_ca_cert(const char *, X509 *, int (*cb)(int, const char *, void *), void *);
static int tqsl_handle_user_cert(const char *, X509 *, int (*cb)(int, const char *, void *), void *);
static struct tqsl_loadcert_handler tqsl_loadcert_handlers[] = {
{ TQSL_CERT_CB_ROOT, &tqsl_handle_root_cert },
{ TQSL_CERT_CB_CA, &tqsl_handle_ca_cert },
{ TQSL_CERT_CB_USER, &tqsl_handle_user_cert }
};
static const char *notypes[] = { "" };
/*
static tqsl_adifFieldDefinitions tqsl_cert_file_fields[] = {
{ "TQSL_CERT", "", TQSL_ADIF_RANGE_TYPE_NONE, 0, 0, 0, NULL, NULL },
{ "TQSL_CERT_USER", "", TQSL_ADIF_RANGE_TYPE_NONE, 2000, 0, 0, NULL, &tqsl_load_user_cert },
{ "TQSL_CERT_CA", "", TQSL_ADIF_RANGE_TYPE_NONE, 2000, 0, 0, NULL, &tqsl_load_ca_cert },
{ "TQSL_CERT_ROOT", "", TQSL_ADIF_RANGE_TYPE_NONE, 2000, 0, 0, NULL, &tqsl_load_root_cert },
};
*/
static unsigned char tqsl_static_buf[2001];
static char ImportCall[256];
#if !defined(__APPLE__) && !defined(_WIN32) && !defined(__clang__)
#pragma GCC diagnostic ignored "-Wformat-truncation"
#endif
static unsigned char *
tqsl_static_alloc(size_t size) {
if (size > sizeof tqsl_static_buf)
return NULL;
strncpy(reinterpret_cast<char *>(tqsl_static_buf), "<EMPTY>", sizeof tqsl_static_buf);
return tqsl_static_buf;
}
namespace tqsllib {
int
tqsl_import_cert(const char *data, certtype type, int(*cb)(int, const char *, void *), void *userdata) {
BIO *bio;
X509 *cert;
int stat;
struct tqsl_loadcert_handler *handler = &(tqsl_loadcert_handlers[type]);
/* This is a certificate, supposedly. Let's make sure */
tqslTrace("tqsl_import_cert", NULL);
bio = BIO_new_mem_buf(reinterpret_cast<void *>(const_cast<char *>(data)), strlen(data));
if (bio == NULL) {
tqslTrace("tqsl_import_cert", "BIO mem buf error %s", tqsl_openssl_error());
tQSL_Error = TQSL_OPENSSL_ERROR;
return 1;
}
cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
BIO_free(bio);
if (cert == NULL) {
tqslTrace("tqsl_import_cert", "BIO read error, err=%s", tqsl_openssl_error());
tQSL_Error = TQSL_OPENSSL_ERROR;
return 1;
}
/* It's a certificate. Let's try to add it. Any errors will be
* reported via the callback (if any) but will not be fatal unless
* the callback says so.
*/
ImportCall[0] = '\0';
tQSL_ImportSerial = 0;
stat = (*(handler->func))(data, cert, cb, userdata);
X509_free(cert);
if (stat) {
if (tQSL_Error == TQSL_CERT_ERROR) {
return 1;
}
if (cb != NULL) {
stat = (*cb)(handler->type | TQSL_CERT_CB_RESULT | TQSL_CERT_CB_ERROR, tqsl_getErrorString_v(tQSL_Error), userdata);
if (stat) {
tqslTrace("tqsl_import_cert", "import error %d", tQSL_Error);
return 1;
} else {
tqslTrace("tqsl_import_cert", "import error. Handler suppressed.");
}
} else {
/* No callback -- any errors are fatal */
tqslTrace("tqsl_import_cert", "import error %d", tQSL_Error);
return 1;
}
return stat;
}
strncpy(tQSL_ImportCall, ImportCall, sizeof tQSL_ImportCall);
return 0;
}
int
tqsl_get_pem_serial(const char *pem, long *serial) {
BIO *bio;
X509 *cert;
tqslTrace("tqsl_get_pem_serial", NULL);
if (tqsl_init())
return 1;
if (pem == NULL || serial == NULL) {
tqslTrace("tqsl_get_pem_serial", "arg error pem=0x%lx, serial=0x%lx", pem, serial);
tQSL_Error = TQSL_ARGUMENT_ERROR;
return 1;
}
bio = BIO_new_mem_buf(reinterpret_cast<void *>(const_cast<char *>(pem)), strlen(pem));
if (bio == NULL) {
tqslTrace("tqsl_get_pem_serial", "mem buf error %s", tqsl_openssl_error());
tQSL_Error = TQSL_OPENSSL_ERROR;
return 1;
}
cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
BIO_free(bio);
if (cert == NULL) {
tqslTrace("tqsl_get_pem_serial", "cert read error %s", tqsl_openssl_error());
tQSL_Error = TQSL_OPENSSL_ERROR;
return 1;
}
*serial = ASN1_INTEGER_get(X509_get_serialNumber(cert));
return 0;
}
} // namespace tqsllib
/********** PUBLIC API FUNCTIONS ***********/
DLLEXPORT int CALLCONVENTION
tqsl_createCertRequest(const char *filename, TQSL_CERT_REQ *userreq,
int (*pwcb)(char *pwbuf, int pwsize, void *), void *userdata) {
TQSL_CERT_REQ *req = NULL;
EVP_PKEY *key = NULL;
X509_REQ *xr = NULL;
X509_NAME *subj = NULL;
int nid, len;
int rval = 1;
FILE *out = NULL;
BIO *bio = NULL;
const EVP_MD *digest = NULL;
char buf[200];
char path[TQSL_MAX_PATH_LEN];
char *cp;
const EVP_CIPHER *cipher = NULL;
char *password;
const char *type;
tqslTrace("tqsl_createCertRequest", NULL);
if (tqsl_init())
return 1;
if (filename == NULL || userreq == NULL) {
tqslTrace("tqsl_createCertRequest", "arg error filename=0x%lx, userreq=0x%lx", filename, userreq);
tQSL_Error = TQSL_ARGUMENT_ERROR;
return 1;
}
if (userreq->signer != NULL && (!tqsl_cert_check(TQSL_API_TO_CERT(userreq->signer))
|| TQSL_API_TO_CERT(userreq->signer)->key == NULL)) {
tqslTrace("tqsl_createCertRequest", "arg error signer/key");
tQSL_Error = TQSL_ARGUMENT_ERROR;
return 1;
}
if ((req = tqsl_copy_cert_req(userreq)) == NULL) {
tqslTrace("tqsl_createCertRequest", "Error copying %d", tQSL_Error);
goto end;
}
/* Check parameters for validity */
tqsl_trim(req->providerName);
tqsl_trim(req->providerUnit);
tqsl_trim(req->name);
if (tqsl_check_parm(req->name, "Name")) {
tqslTrace("tqsl_createCertRequest", "check_parm Name");
goto end;
}
tqsl_trim(req->callSign);
if (tqsl_check_parm(req->callSign, "Call Sign")) {
tqslTrace("tqsl_createCertRequest", "check_parm Call Sign");
goto end;
}
tqsl_trim(req->address1);
if (tqsl_check_parm(req->address1, "Address")) {
tqslTrace("tqsl_createCertRequest", "check_parm Address1");
goto end;
}
tqsl_trim(req->address2);
tqsl_trim(req->city);
if (tqsl_check_parm(req->city, "City")) {
tqslTrace("tqsl_createCertRequest", "check_parm City");
goto end;
}
tqsl_trim(req->state);
tqsl_trim(req->country);
if (tqsl_check_parm(req->country, "Country")) {
tqslTrace("tqsl_createCertRequest", "check_parm Country");
goto end;
}
tqsl_trim(req->postalCode);
tqsl_trim(req->emailAddress);
if (tqsl_check_parm(req->emailAddress, "Email address")) {
tqslTrace("tqsl_createCertRequest", "check_parm email");
goto end;
}
if ((cp = strchr(req->emailAddress, '@')) == NULL || strchr(cp, '.') == NULL) {
strncpy(tQSL_CustomError, "Invalid email address", sizeof tQSL_CustomError);
tQSL_Error = TQSL_CUSTOM_ERROR;
tqslTrace("tqsl_createCertRequest", "check_parm email: %s %s", req->emailAddress, tQSL_CustomError);
goto end;
}
if (!tqsl_isDateValid(&(req->qsoNotBefore))) {
strncpy(tQSL_CustomError, "Invalid date (qsoNotBefore)", sizeof tQSL_CustomError);
tqslTrace("tqsl_createCertRequest", "check_parm not before: %s %s", req->qsoNotBefore, tQSL_CustomError);
tQSL_Error = TQSL_CUSTOM_ERROR;
goto end;
}
if (!tqsl_isDateNull(&(req->qsoNotAfter))) {
if (!tqsl_isDateValid(&(req->qsoNotAfter))) {
strncpy(tQSL_CustomError, "Invalid date (qsoNotAfter)", sizeof tQSL_CustomError);
tqslTrace("tqsl_createCertRequest", "check_parm not after: %s %s", req->qsoNotAfter, tQSL_CustomError);
tQSL_Error = TQSL_CUSTOM_ERROR;
goto end;
}
if (tqsl_compareDates(&(req->qsoNotAfter), &(req->qsoNotBefore)) < 0) {
strncpy(tQSL_CustomError, "qsoNotAfter date is earlier than qsoNotBefore", sizeof tQSL_CustomError);
tqslTrace("tqsl_createCertRequest", "check_parm not after: %s %s", req->qsoNotAfter, tQSL_CustomError);
tQSL_Error = TQSL_CUSTOM_ERROR;
goto end;
}
}
/* Try opening the output stream */
#ifdef _WIN32
wchar_t* wfilename = utf8_to_wchar(filename);
if ((out = _wfopen(wfilename, TQSL_OPEN_WRITE)) == NULL) {
free_wchar(wfilename);
#else
if ((out = fopen(filename, TQSL_OPEN_WRITE)) == NULL) {
#endif
strncpy(tQSL_ErrorFile, filename, sizeof tQSL_ErrorFile);
tqslTrace("tqsl_createCertRequest", "Open file - system error %s", strerror(errno));
tQSL_Error = TQSL_SYSTEM_ERROR;
tQSL_Errno = errno;
goto end;
}
#ifdef _WIN32
free_wchar(wfilename);
#endif
if (fputs("\ntQSL certificate request\n\n", out) == EOF) {
strncpy(tQSL_ErrorFile, filename, sizeof tQSL_ErrorFile);
tqslTrace("tqsl_createCertRequest", "Write request file - system error %s", strerror(errno));
tQSL_Error = TQSL_SYSTEM_ERROR;
tQSL_Errno = errno;
goto end;
}
tqsl_write_adif_field(out, "eoh", 0, NULL, 0);
type = (req->signer != NULL) ? (req->renew ? "TQSL_CRQ_RENEWAL" : "TQSL_CRQ_ADDITIONAL") : "TQSL_CRQ_NEW";
int libmaj, libmin, configmaj, configmin;
tqsl_getVersion(&libmaj, &libmin);
tqsl_getConfigVersion(&configmaj, &configmin);
snprintf(buf, sizeof buf, "TQSL: %d.%d.%d, Lib: V%d.%d, Config: %d.%d", TQSL_VERSION_MAJOR,
TQSL_VERSION_MINOR, TQSL_VERSION_UPDATE, libmaj, libmin, configmaj, configmin);
tqsl_write_adif_field(out, "TQSL_IDENT", 0, (unsigned char *)buf, -1);
tqsl_write_adif_field(out, type, 0, NULL, 0);
tqsl_write_adif_field(out, "TQSL_CRQ_PROVIDER", 0, (unsigned char *)req->providerName, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_PROVIDER_UNIT", 0, (unsigned char *)req->providerUnit, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_EMAIL", 0, (unsigned char *)req->emailAddress, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_NAME", 0, (unsigned char *)req->name, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_ADDRESS1", 0, (unsigned char *)req->address1, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_ADDRESS2", 0, (unsigned char *)req->address2, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_CITY", 0, (unsigned char *)req->city, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_STATE", 0, (unsigned char *)req->state, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_POSTAL", 0, (unsigned char *)req->postalCode, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_COUNTRY", 0, (unsigned char *)req->country, -1);
snprintf(buf, sizeof buf, "%d", req->dxccEntity);
tqsl_write_adif_field(out, "TQSL_CRQ_DXCC_ENTITY", 0, (unsigned char *)buf, -1);
tqsl_convertDateToText(&(req->qsoNotBefore), buf, sizeof buf);
tqsl_write_adif_field(out, "TQSL_CRQ_QSO_NOT_BEFORE", 0, (unsigned char *)buf, -1);
if (!tqsl_isDateNull(&(req->qsoNotAfter))) {
tqsl_convertDateToText(&(req->qsoNotAfter), buf, sizeof buf);
tqsl_write_adif_field(out, "TQSL_CRQ_QSO_NOT_AFTER", 0, (unsigned char *)buf, -1);
}
/* Generate a new key pair */
if ((key = tqsl_new_rsa_key(1024)) == NULL) {
tqslTrace("tqsl_createCertRequest", "key create error %d", tQSL_Error);
goto end;
}
/* Make the X.509 certificate request */
if ((xr = X509_REQ_new()) == NULL) {
tqslTrace("tqsl_createCertRequest", "req create error %s", tqsl_openssl_error());
goto err;
}
if (!X509_REQ_set_version(xr, 0L)) {
tqslTrace("tqsl_createCertRequest", "version set error %s", tqsl_openssl_error());
goto err;
}
subj = X509_REQ_get_subject_name(xr);
nid = OBJ_txt2nid("AROcallsign");
if (nid != NID_undef)
X509_NAME_add_entry_by_NID(subj, nid, MBSTRING_ASC, (unsigned char *)req->callSign, -1, -1, 0);
nid = OBJ_txt2nid("commonName");
if (nid != NID_undef)
X509_NAME_add_entry_by_NID(subj, nid, MBSTRING_ASC, (unsigned char *)req->name, -1, -1, 0);
nid = OBJ_txt2nid("emailAddress");
if (nid != NID_undef)
X509_NAME_add_entry_by_NID(subj, nid, MBSTRING_ASC, (unsigned char *)req->emailAddress, -1, -1, 0);
X509_REQ_set_pubkey(xr, key);
if ((digest = EVP_sha256()) == NULL) {
tqslTrace("tqsl_createCertRequest", "evp_sha256 error %s", tqsl_openssl_error());
goto err;
}
if (!X509_REQ_sign(xr, key, digest)) {
tqslTrace("tqsl_createCertRequest", "req_sign error %s", tqsl_openssl_error());
goto err;
}
if ((bio = BIO_new(BIO_s_mem())) == NULL) {
tqslTrace("tqsl_createCertRequest", "bio_new error %s", tqsl_openssl_error());
goto err;
}
if (!PEM_write_bio_X509_REQ(bio, xr)) {
tqslTrace("tqsl_createCertRequest", "write_bio error %s", tqsl_openssl_error());
goto err;
}
len = static_cast<int>(BIO_get_mem_data(bio, &cp));
tqsl_write_adif_field(out, "TQSL_CRQ_REQUEST", 0, (unsigned char *)cp, len);
if (req->signer != NULL) {
char *b64;
char ibuf[256];
if ((b64 = tqsl_sign_base64_data(req->signer, cp)) == NULL) {
fclose(out);
tqslTrace("tqsl_createCertRequest", "tqsl_sign_base64 error %s", tqsl_openssl_error());
goto end;
}
tqsl_write_adif_field(out, "TQSL_CRQ_SIGNATURE", 0, (unsigned char *)b64, -1);
tqsl_getCertificateIssuer(req->signer, ibuf, sizeof ibuf);
tqsl_write_adif_field(out, "TQSL_CRQ_SIGNATURE_CERT_ISSUER", 0, (unsigned char *)ibuf, -1);
snprintf(ibuf, sizeof ibuf, "%ld", ASN1_INTEGER_get(X509_get_serialNumber(TQSL_API_TO_CERT(req->signer)->cert)));
tqsl_write_adif_field(out, "TQSL_CRQ_SIGNATURE_CERT_SERIAL", 0, (unsigned char *)ibuf, -1);
}
BIO_free(bio);
bio = NULL;
tqsl_write_adif_field(out, "eor", 0, NULL, 0);
if (fclose(out) == EOF) {
strncpy(tQSL_ErrorFile, filename, sizeof tQSL_ErrorFile);
tQSL_Error = TQSL_SYSTEM_ERROR;
tQSL_Errno = errno;
tqslTrace("tqsl_createCertRequest", "write error %d", errno);
goto end;
}
out = NULL;
/* Write the key to the key store */
if (!tqsl_make_key_path(req->callSign, path, sizeof path)) {
tqslTrace("tqsl_createCertRequest", "make_key_path error %d", errno);
goto end;
}
#ifdef _WIN32
wchar_t* wpath = utf8_to_wchar(path);
if ((out = _wfopen(wpath, TQSL_OPEN_APPEND)) == NULL) {
free_wchar(wpath);
#else
if ((out = fopen(path, TQSL_OPEN_APPEND)) == NULL) {
#endif
strncpy(tQSL_ErrorFile, path, sizeof tQSL_ErrorFile);
tQSL_Error = TQSL_SYSTEM_ERROR;
tQSL_Errno = errno;
tqslTrace("tqsl_createCertRequest", "opening file error %s", strerror(errno));
goto end;
}
#ifdef _WIN32
free_wchar(wpath);
#endif
tqsl_write_adif_field(out, "TQSL_CRQ_PROVIDER", 0, (unsigned char *)req->providerName, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_PROVIDER_UNIT", 0, (unsigned char *)req->providerUnit, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_EMAIL", 0, (unsigned char *)req->emailAddress, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_ADDRESS1", 0, (unsigned char *)req->address1, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_ADDRESS2", 0, (unsigned char *)req->address2, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_CITY", 0, (unsigned char *)req->city, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_STATE", 0, (unsigned char *)req->state, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_POSTAL", 0, (unsigned char *)req->postalCode, -1);
tqsl_write_adif_field(out, "TQSL_CRQ_COUNTRY", 0, (unsigned char *)req->country, -1);
tqsl_write_adif_field(out, "CALLSIGN", 0, (unsigned char *)req->callSign, -1);
snprintf(buf, sizeof buf, "%d", req->dxccEntity);
tqsl_write_adif_field(out, "TQSL_CRQ_DXCC_ENTITY", 0, (unsigned char *)buf, -1);
tqsl_convertDateToText(&(req->qsoNotBefore), buf, sizeof buf);
tqsl_write_adif_field(out, "TQSL_CRQ_QSO_NOT_BEFORE", 0, (unsigned char *)buf, -1);
if (!tqsl_isDateNull(&(req->qsoNotAfter))) {
tqsl_convertDateToText(&(req->qsoNotAfter), buf, sizeof buf);
tqsl_write_adif_field(out, "TQSL_CRQ_QSO_NOT_AFTER", 0, (unsigned char *)buf, -1);
}
if ((bio = BIO_new(BIO_s_mem())) == NULL) {
tqslTrace("tqsl_createCertRequest", "bio_new error %s", tqsl_openssl_error());
goto err;
}
password = const_cast<char *>(req->password);
if (password == NULL && pwcb != NULL) {
if ((*pwcb)(buf, TQSL_MAX_PW_LENGTH, userdata)) {
tqslTrace("tqsl_createCertRequest", "password abort");
tQSL_Error = TQSL_OPERATOR_ABORT;
goto end;
}
password = buf;
}
if (password != NULL && *password != '\0') {
if ((cipher = EVP_des_ede3_cbc()) == NULL) {
tqslTrace("tqsl_createCertRequest", "password error");
goto err;
}
len = strlen(password);
} else {
password = NULL;
len = 0;
}
if (!PEM_write_bio_PrivateKey(bio, key, cipher, (unsigned char *)password, len, NULL, NULL)) {
tqslTrace("tqsl_createCertRequest", "write priv key error %s", tqsl_openssl_error());
goto err;
}
len = static_cast<int>(BIO_get_mem_data(bio, &cp));
tqsl_write_adif_field(out, "PRIVATE_KEY", 0, (unsigned char *)cp, len);
BIO_free(bio);
if ((bio = BIO_new(BIO_s_mem())) == NULL) {
tqslTrace("tqsl_createCertRequest", "bio_new error %s", tqsl_openssl_error());
goto err;
}
if (!PEM_write_bio_PUBKEY(bio, key)) {
tqslTrace("tqsl_createCertRequest", "write pubkey %s", tqsl_openssl_error());
goto err;
}
len = static_cast<int>(BIO_get_mem_data(bio, &cp));
tqsl_write_adif_field(out, "PUBLIC_KEY", 0, (unsigned char *)cp, len);
BIO_free(bio);
bio = NULL;
tqsl_write_adif_field(out, "eor", 0, NULL, 0);
if (fclose(out) == EOF) {
tQSL_Error = TQSL_SYSTEM_ERROR;
tQSL_Errno = errno;
tqslTrace("tqsl_createCertRequest", "write file error %s", strerror(tQSL_Errno));
goto end;
}
out = NULL;
rval = 0;
goto end;
err:
tQSL_Error = TQSL_OPENSSL_ERROR;
end:
if (bio != NULL)
BIO_free(bio);
if (out != NULL)
fclose(out);
if (xr != NULL)
X509_REQ_free(xr);
if (key != NULL)
EVP_PKEY_free(key);
if (req != NULL)
tqsl_free_cert_req(req, 0);
return rval;
}
DLLEXPORT int CALLCONVENTION
tqsl_getSelectedCertificate(tQSL_Cert *cert, const tQSL_Cert **certlist,
int idx) {
tqslTrace("tqsl_getSelectedCertificate", NULL);
if (tqsl_init())
return 1;
if (certlist == NULL || cert == NULL || idx < 0) {
tqslTrace("tqsl_getSelectedCertificate", "arg error certlist=0x%lx, cert=0x%lx, idx=%d", certlist, cert, idx);
tQSL_Error = TQSL_ARGUMENT_ERROR;
return 1;
}
*cert = (*certlist)[idx];
return 0;
}
DLLEXPORT int CALLCONVENTION
tqsl_isCertificateExpired(tQSL_Cert cert, int *status) {
tqslTrace("tqsl_isCertificateExpired", NULL);
if (tqsl_init())
return 1;
if (cert == NULL || status == NULL || !tqsl_cert_check(TQSL_API_TO_CERT(cert), false)) {
tqslTrace("tqsl_isCertificateExpired", "arg error cert=0x%lx status=0x%lx", cert, status);
tQSL_Error = TQSL_ARGUMENT_ERROR;
if (status) *status = false;
return 1;
}
int keyonly;
if (tqsl_getCertificateKeyOnly(cert, &keyonly) == 0 && keyonly) {
*status = false;
return 0;
}
long serial = 0;
tqsl_getCertificateSerial(cert, &serial);
int sts = tqsl_getCertificateStatus(serial);
if (sts == TQSL_CERT_STATUS_EXP || sts == TQSL_CERT_STATUS_INV) {
*status = true;
return 0;
}
*status = false;
/* Check for expired */
time_t t = time(0);
struct tm *tm = gmtime(&t);
tQSL_Date d;
d.year = tm->tm_year + 1900;
d.month = tm->tm_mon + 1;
d.day = tm->tm_mday;
const ASN1_TIME *ctm;
if ((ctm = X509_get_notAfter(TQSL_API_TO_CERT(cert)->cert)) == NULL) {
*status = true;
return 0;
} else {
tQSL_Date cert_na;
tqsl_get_asn1_date(ctm, &cert_na);
if (tqsl_compareDates(&cert_na, &d) < 0) {
*status = true;
return 0;
}
}
return 0;
}
static TQSL_X509_STACK *xcerts = NULL;
DLLEXPORT int CALLCONVENTION
tqsl_isCertificateSuperceded(tQSL_Cert cert, int *status) {
char path[TQSL_MAX_PATH_LEN];
int i;
X509 *x = NULL;
char *cp;
vector< map<string, string> > keylist;
vector< map<string, string> >::iterator it;
set<string> superceded_certs;
int len;
bool superceded = false;
char buf[256];
tqslTrace("tqsl_isCertificateSuperceded", NULL);
if (tqsl_init())
return 1;
if (cert == NULL || status == NULL || !tqsl_cert_check(TQSL_API_TO_CERT(cert), false)) {
tqslTrace("tqsl_isCertificateSuperceded", "arg error cert=0x%lx, status=0x%lx", cert, status);
tQSL_Error = TQSL_ARGUMENT_ERROR;
return 1;
}
*status = false;
int keyonly;
if (tqsl_getCertificateKeyOnly(cert, &keyonly) == 0 && keyonly) {
return 0;
}
long serial = 0;
tqsl_getCertificateSerial(cert, &serial);
if (tqsl_getCertificateStatus(serial) == TQSL_CERT_STATUS_SUP) {
*status = true;
tqslTrace("tqsl_isCertificateSuperceded", "returning true");
return 0;
}
/* Get the certs from the cert store */
tqsl_make_cert_path("user", path, sizeof path);
if (xcerts == NULL)
xcerts = tqsl_ssl_load_certs_from_file(path);
if (xcerts == NULL) {
if (tQSL_Error == TQSL_OPENSSL_ERROR) {
tqslTrace("tqsl_isCertificateSuperceded", "openssl error loading certs %d", tQSL_Error);
return 1;
}
}
/* Make a list of superceded certs */
for (i = 0; i < sk_X509_num(xcerts); i++) {
x = sk_X509_value(xcerts, i);
len = sizeof buf-1;
if (!tqsl_get_cert_ext(x, "supercededCertificate", (unsigned char *)buf, &len, NULL)) {
buf[len] = 0;
string sup = buf;
superceded_certs.insert(sup);
/* Fix - the extension as inserted by ARRL
* reads ".../Email=lotw@arrl.org", not
* the expected ".../emailAddress=".
* save both forms in case this gets
* changed at the LoTW site
*/
size_t pos = sup.find("/Email");
if (pos != string::npos) {
sup.replace(pos, 6, "/emailAddress");
superceded_certs.insert(sup);
}
}
}
// "supercededCertificate" extension is <issuer>;<serial>
cp = X509_NAME_oneline(X509_get_issuer_name(TQSL_API_TO_CERT(cert)->cert), buf, sizeof(buf));
if (cp == NULL) {
superceded = false;
tqslTrace("tqsl_isCertificateSuperceded", "returning false");
} else {
string sup = buf;
sup += ";";
long serial = 0;
tqsl_getCertificateSerial(cert, &serial);
snprintf(buf, sizeof buf, "%ld", serial);
sup += buf;
if (superceded_certs.find(sup) != superceded_certs.end()) {
tqslTrace("tqsl_isCertificateSuperceded", "returning true");
superceded = true;
}
}
*status = superceded;
return 0;
}
DLLEXPORT int CALLCONVENTION
tqsl_selectCertificates(tQSL_Cert **certlist, int *ncerts,
const char *callsign, int dxcc, const tQSL_Date *date, const TQSL_PROVIDER *issuer, int flags) {
int withkeys = flags & TQSL_SELECT_CERT_WITHKEYS;
TQSL_X509_STACK *selcerts = NULL;
char path[TQSL_MAX_PATH_LEN];
int i;
X509 *x;
int rval = 1;
tqsl_cert *cp;
TQSL_CERT_REQ *crq;
BIO *bio = NULL;
EVP_PKEY *pubkey = NULL;
EVP_PKEY *curkey = NULL;
vector< map<string, string> > keylist;
vector< map<string, string> >::iterator it;
bool keyerror = false;
int savedError;
int savedErrno;
tqslTrace("tqsl_selectCertificates", "callsign=%s, dxcc=%d, flags=%d", callsign ? callsign : "NULL", dxcc, flags);
if (tqsl_init())