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
|
/*
* $LynxId: tidy_tls.c,v 1.5 2010/06/20 19:25:46 tom Exp $
* Copyright 2008, Thomas E. Dickey
* with fix Copyright 2008 by Thomas Viehmann
*
* Required libraries:
* libgnutls
* libcrypt
*/
#include <tidy_tls.h>
#include <gnutls/x509.h>
#include <gcrypt.h>
#include <libtasn1.h> /* ASN1_SUCCESS,etc */
#include <string.h>
#define typeCalloc(type) (type *) calloc(1, sizeof(type))
static int last_error = 0;
/* ugly, but hey, we could just use a more sane api, too */
#define GetDnByOID(target, oid, thewhat) \
len = sizeof(target); \
if (! thewhat) \
gnutls_x509_crt_get_dn_by_oid(xcert, oid, 0, 0, target, &len); \
else \
gnutls_x509_crt_get_issuer_dn_by_oid(xcert, oid, 0, 0, target, &len)
/* thewhat: which DN to get 0 = subject, 1 = issuer */
static int ExtractCertificate(const gnutls_datum_t * cert, X509_NAME * result, int thewhat)
{
gnutls_x509_crt_t xcert;
int rc;
size_t len;
if ((rc = gnutls_x509_crt_init(&xcert)) >= 0) {
if ((rc = gnutls_x509_crt_import(xcert, cert, GNUTLS_X509_FMT_DER)) >= 0) {
GetDnByOID(result->country,
GNUTLS_OID_X520_COUNTRY_NAME, thewhat);
GetDnByOID(result->organization,
GNUTLS_OID_X520_ORGANIZATION_NAME, thewhat);
GetDnByOID(result->organizational_unit_name,
GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME, thewhat);
GetDnByOID(result->common_name,
GNUTLS_OID_X520_COMMON_NAME, thewhat);
GetDnByOID(result->locality_name,
GNUTLS_OID_X520_LOCALITY_NAME, thewhat);
GetDnByOID(result->state_or_province_name,
GNUTLS_OID_X520_STATE_OR_PROVINCE_NAME, thewhat);
GetDnByOID(result->email,
GNUTLS_OID_PKCS9_EMAIL, thewhat);
rc = 0;
}
gnutls_x509_crt_deinit(xcert);
}
return rc;
}
/*
* (stub)
* ERR_error_string() generates a human-readable string representing the
* error code 'e', and places it at 'buffer', which must be at least 120 bytes
* long. If 'buffer' is NULL, the error string is placed in a static buffer.
*/
const char *ERR_error_string(unsigned long e, char *buffer)
{
(void) buffer;
return gnutls_strerror(-e);
}
/*
* (stub)
* Return the earliest error code from the thread's error queue and remove the
* entry.
*/
unsigned long ERR_get_error(void)
{
unsigned long rc;
rc = -last_error;
last_error = 0;
return rc;
}
/*
* Put 'num' cryptographically strong pseudo-random bytes into 'buffer'.
*/
int RAND_bytes(unsigned char *buffer, int num)
{
gcry_randomize(buffer, num, GCRY_VERY_STRONG_RANDOM);
return 1;
}
/*
* (stub)
* Generate a default path for the random seed file. 'buffer' points to a
* buffer of size 'len' in which to store the filename.
*/
const char *RAND_file_name(char *buffer, size_t len)
{
(void) buffer;
(void) len;
return "";
}
/*
* (stub)
* Read a number of bytes from file 'name' and adds them to the PRNG. If
* 'maxbytes' is non-negative, up to to 'maxbytes' are read; if 'maxbytes' is
* -1, the complete file is read.
*/
int RAND_load_file(const char *name, long maxbytes)
{
(void) name;
return maxbytes;
}
/*
* (stub)
* Mix the 'num' bytes at 'buffer' into the PRNG state.
*/
void RAND_seed(const void *buffer, int num)
{
(void) buffer;
(void) num;
}
/*
* (stub)
* Return 1 if the PRNG has been seeded with enough data, 0 otherwise.
*/
int RAND_status(void)
{
return 1;
}
/*
* (stub)
* Write a number of random bytes (currently 1024) to file 'name' which can be
* used to initialize the PRNG by calling RAND_load_file() in a later session.
*/
int RAND_write_file(const char *name)
{
(void) name;
return 0;
}
/*
* Return the number of secret bits used for cipher. If 'bits' is not NULL, it
* contains the number of bits processed by the chosen algorithm. If cipher is
* NULL, 0 is returned.
*/
int SSL_CIPHER_get_bits(SSL_CIPHER * cipher, int *bits)
{
int result = 0;
if (cipher) {
result = (8 * gnutls_cipher_get_key_size(cipher->encrypts));
}
if (bits)
*bits = result;
return result;
}
/*
* Return a pointer to the name of 'cipher'. If 'cipher' is NULL the constant
* value "NONE" is returned.
*/
const char *SSL_CIPHER_get_name(SSL_CIPHER * cipher)
{
const char *result = "NONE";
if (cipher) {
result = gnutls_cipher_suite_get_name(cipher->key_xchg,
cipher->encrypts,
cipher->msg_code);
}
return result;
}
/*
* Return the protocol version for cipher, currently "SSLv2", "SSLv3", or
* "TLSv1". If cipher is NULL, "(NONE)" is returned.
*/
const char *SSL_CIPHER_get_version(SSL_CIPHER * cipher)
{
const char *result = "NONE";
if (cipher) {
if ((result = gnutls_protocol_get_name(cipher->protocol)) == 0)
result = "unknown";
}
return result;
}
/*
* Free an allocated SSL_CTX object.
*/
void SSL_CTX_free(SSL_CTX * ctx)
{
free(ctx->method);
free(ctx);
}
/*
* Create a new SSL_CTX object as framework for TLS/SSL enabled functions.
*/
SSL_CTX *SSL_CTX_new(SSL_METHOD * method)
{
SSL_CTX *ctx;
ctx = typeCalloc(SSL_CTX);
ctx->method = method;
return ctx;
}
/*
* See SSL_CTX_load_verify_locations() - this sets the paths for that and
* SSL_CTX_set_verify() to their default values. GNU TLS does not have a
* comparable feature (stub).
*/
int SSL_CTX_set_default_verify_paths(SSL_CTX * ctx)
{
(void) ctx;
return 0;
}
/*
* SSL_CTX_set_options() adds the options set via bitmask in options to
* ctx. Options already set before are not cleared.
*/
unsigned long SSL_CTX_set_options(SSL_CTX * ctx, unsigned long options)
{
ctx->options |= options;
return ctx->options;
}
/*
* Set peer certificate verification parameters.
*/
void SSL_CTX_set_verify(SSL_CTX * ctx, int verify_mode,
int (*verify_callback) (int, X509_STORE_CTX *))
{
ctx->verify_mode = verify_mode;
ctx->verify_callback = verify_callback;
}
static void RemoveProtocol(SSL * ssl, int protocol)
{
int j, k;
int changed = 0;
int *protocols = ssl->ctx->method->priority.protocol;
for (j = k = 0; j < GNUTLS_MAX_ALGORITHM_NUM;) {
if (protocols[k] == protocol) {
if (++k >= GNUTLS_MAX_ALGORITHM_NUM)
break;
changed = 1;
} else {
protocols[j++] = protocols[k++];
}
}
if (changed) {
gnutls_protocol_set_priority(ssl->gnutls_state, protocols);
}
}
/*
* Initiate the TLS/SSL handshake with an TLS/SSL server.
*/
int SSL_connect(SSL * ssl)
{
X509_STORE_CTX *store;
int rc;
if (ssl->options & SSL_OP_NO_TLSv1)
RemoveProtocol(ssl, GNUTLS_TLS1);
if (ssl->options & SSL_OP_NO_SSLv3)
RemoveProtocol(ssl, GNUTLS_SSL3);
rc = gnutls_handshake(ssl->gnutls_state);
ssl->last_error = rc;
if (rc < 0) {
last_error = rc;
return 0;
}
store = typeCalloc(X509_STORE_CTX);
store->ssl = ssl;
store->cert_list = SSL_get_peer_certificate(ssl);
if (ssl->verify_callback) {
ssl->verify_callback(1, store);
}
ssl->state = SSL_ST_OK;
free(store);
/* FIXME: deal with error from callback */
return 1;
}
/*
* Free an allocated SSL structure.
*/
void SSL_free(SSL * ssl)
{
gnutls_certificate_free_credentials(ssl->gnutls_cred);
gnutls_deinit(ssl->gnutls_state);
free(ssl);
}
/*
* Get SSL_CIPHER data of a connection.
*/
SSL_CIPHER *SSL_get_current_cipher(SSL * ssl)
{
SSL_CIPHER *result = 0;
if (ssl) {
result = &(ssl->ciphersuite);
result->protocol = gnutls_protocol_get_version(ssl->gnutls_state);
result->encrypts = gnutls_cipher_get(ssl->gnutls_state);
result->key_xchg = gnutls_kx_get(ssl->gnutls_state);
result->msg_code = gnutls_mac_get(ssl->gnutls_state);
result->compress = gnutls_compression_get(ssl->gnutls_state);
result->cert = gnutls_certificate_type_get(ssl->gnutls_state);
}
return result;
}
/*
* Get the X509 certificate of the peer.
*/
X509 *SSL_get_peer_certificate(SSL * ssl)
{
gnutls_datum_t *result;
unsigned list_size = 0;
result = (gnutls_datum_t *) gnutls_certificate_get_peers(ssl->gnutls_state,
&list_size);
return (X509 *) result;
}
/*
* Initialize SSL library by registering algorithms.
*/
int SSL_library_init(void)
{
gnutls_global_init();
return 1;
}
/*
* (stub)
* OpenSSL uses this to prepare for ERR_get_error() calls.
*/
void SSL_load_error_strings(void)
{
}
/*
* Create a new SSL structure for a connection.
*/
SSL *SSL_new(SSL_CTX * ctx)
{
SSL *ssl;
int rc;
if ((ssl = typeCalloc(SSL)) != 0) {
rc = gnutls_certificate_allocate_credentials(&ssl->gnutls_cred);
if (rc < 0) {
last_error = rc;
free(ssl);
ssl = 0;
} else {
gnutls_init(&ssl->gnutls_state, ctx->method->connend);
gnutls_protocol_set_priority(ssl->gnutls_state,
ctx->method->priority.protocol);
gnutls_cipher_set_priority(ssl->gnutls_state,
ctx->method->priority.encrypts);
gnutls_compression_set_priority(ssl->gnutls_state,
ctx->method->priority.compress);
gnutls_kx_set_priority(ssl->gnutls_state,
ctx->method->priority.key_xchg);
gnutls_mac_set_priority(ssl->gnutls_state,
ctx->method->priority.msg_code);
gnutls_credentials_set(ssl->gnutls_state, GNUTLS_CRD_CERTIFICATE,
ssl->gnutls_cred);
if (ctx->certfile)
gnutls_certificate_set_x509_trust_file(ssl->gnutls_cred,
ctx->certfile,
ctx->certfile_type);
if (ctx->keyfile)
gnutls_certificate_set_x509_key_file(ssl->gnutls_cred,
ctx->certfile,
ctx->keyfile,
ctx->keyfile_type);
ssl->ctx = ctx;
ssl->verify_mode = ctx->verify_mode;
ssl->verify_callback = ctx->verify_callback;
ssl->options = ctx->options;
ssl->rfd = (gnutls_transport_ptr_t) (-1);
ssl->wfd = (gnutls_transport_ptr_t) (-1);
}
}
return ssl;
}
/*
* Read 'length' bytes into 'buffer' from the given SSL connection.
* Returns the number of bytes read, or zero on error.
*/
int SSL_read(SSL * ssl, void *buffer, int length)
{
int rc;
rc = gnutls_record_recv(ssl->gnutls_state, buffer, length);
ssl->last_error = rc;
if (rc < 0) {
last_error = rc;
rc = 0;
}
return rc;
}
/*
* Connect the SSL object with a file descriptor.
* This always returns 1 (success) since GNU TLS does not check for errors.
*/
int SSL_set_fd(SSL * ssl, int fd)
{
gnutls_transport_set_ptr(ssl->gnutls_state,
(gnutls_transport_ptr_t) (fd));
return 1;
}
/*
* Write 'length' bytes from 'buffer' to the given SSL connection.
*/
int SSL_write(SSL * ssl, const void *buffer, int length)
{
int rc;
rc = gnutls_record_send(ssl->gnutls_state, buffer, length);
ssl->last_error = rc;
if (rc < 0) {
last_error = rc;
rc = 0;
}
return rc;
}
/*
* Return method-data for SSL verion 3, with the option of rollback to SSL
* version 2.
*/
SSL_METHOD *SSLv23_client_method(void)
{
SSL_METHOD *m;
if ((m = typeCalloc(SSL_METHOD)) != 0) {
/*
* List the protocols in decreasing order of priority.
*/
m->priority.protocol[0] = GNUTLS_TLS1;
m->priority.protocol[1] = GNUTLS_SSL3;
m->priority.protocol[2] = 0;
/*
* List the cipher algorithms in decreasing order of priority.
*/
m->priority.encrypts[0] = GNUTLS_CIPHER_AES_128_CBC;
m->priority.encrypts[1] = GNUTLS_CIPHER_3DES_CBC;
m->priority.encrypts[2] = GNUTLS_CIPHER_AES_256_CBC;
m->priority.encrypts[3] = GNUTLS_CIPHER_ARCFOUR_128;
m->priority.encrypts[4] = 0;
/*
* List the compression algorithms in decreasing order of priority.
*/
m->priority.compress[0] = GNUTLS_COMP_ZLIB;
m->priority.compress[1] = GNUTLS_COMP_NULL;
m->priority.compress[2] = 0;
/*
* List the key exchange algorithms in decreasing order of priority.
*/
m->priority.key_xchg[0] = GNUTLS_KX_DHE_RSA;
m->priority.key_xchg[1] = GNUTLS_KX_RSA;
m->priority.key_xchg[2] = GNUTLS_KX_DHE_DSS;
m->priority.key_xchg[3] = 0;
/*
* List message authentication code (MAC) algorithms in decreasing
* order of priority to specify via gnutls_mac_set_priority().
*/
m->priority.msg_code[0] = GNUTLS_MAC_SHA1;
m->priority.msg_code[1] = GNUTLS_MAC_MD5;
m->priority.msg_code[2] = 0;
/*
* For gnutls_init, says we're a client.
*/
m->connend = GNUTLS_CLIENT;
}
return m;
}
static int add_name(char *target, int len, const char *tag, const char *data)
{
if (*data != '\0') {
int need = strlen(tag) + 2;
target += strlen(target);
if (need < len) {
strcat(target, "/");
strcat(target, tag);
strcat(target, "=");
len -= need;
target += need;
}
need = strlen(data);
if (need >= len - 1)
need = len - 1;
strncat(target, data, need)[need] = '\0';
}
return len;
}
#define ADD_NAME(tag, data) len = add_name(target, len, tag, data);
/*
* Convert the X509 name 'source' to a string in the given buffer 'target',
* whose length is 'len'. Return a pointer to the buffer.
*/
char *X509_NAME_oneline(X509_NAME * source, char *target, int len)
{
if (target && (len > 0)) {
*target = '\0';
if (source) {
ADD_NAME("C", source->country);
ADD_NAME("ST", source->state_or_province_name);
ADD_NAME("L", source->locality_name);
ADD_NAME("O", source->organization);
ADD_NAME("OU", source->organizational_unit_name);
ADD_NAME("CN", source->common_name);
ADD_NAME("Email", source->email);
}
}
return target;
}
/*
* Extract the certificate's issuer-name data.
*/
X509_NAME *X509_get_issuer_name(const X509 * cert)
{
X509_NAME *result;
if ((result = typeCalloc(X509_NAME)) != 0) {
if (ExtractCertificate(cert, result, 1) < 0) {
free(result);
result = 0;
}
}
return result;
}
/*
* Extract the certificate's subject-name data.
*/
X509_NAME *X509_get_subject_name(const X509 * cert)
{
X509_NAME *result;
if ((result = typeCalloc(X509_NAME)) != 0) {
if (ExtractCertificate(cert, result, 0) < 0) {
free(result);
result = 0;
}
}
return result;
}
|