[go: up one dir, main page]

File: tls.c

package info (click to toggle)
s390-tools 2.35.0-1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 12,220 kB
  • sloc: ansic: 184,236; sh: 12,152; cpp: 4,954; makefile: 2,763; perl: 2,519; asm: 1,085; python: 697; xml: 29
file content (528 lines) | stat: -rw-r--r-- 12,682 bytes parent folder | download | duplicates (2)
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
/*
 * libkmipclient - KMIP client library
 *
 * Copyright IBM Corp. 2021
 *
 * s390-tools is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include <errno.h>
#include <string.h>
#include <sys/stat.h>

#include <openssl/err.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/opensslv.h>

#include "kmip.h"
#include "utils.h"

#ifndef OPENSSL_VERSION_PREREQ
	#if defined(OPENSSL_VERSION_MAJOR) && defined(OPENSSL_VERSION_MINOR)
		#define OPENSSL_VERSION_PREREQ(maj, min)		\
			((OPENSSL_VERSION_MAJOR << 16) +		\
			OPENSSL_VERSION_MINOR >= ((maj) << 16) + (min))
	#else
		#define OPENSSL_VERSION_PREREQ(maj, min)		\
			(OPENSSL_VERSION_NUMBER >= (((maj) << 28) |	\
			((min) << 20)))
	#endif
#endif

/**
 * Verify the pinned public key of the server of a plain TLS KMIP connection
 *
 * @param conn              the KMIP connection to free
 * @param cert_pubkey       the server certificate's public key
 * @param debug             if true, debug messages are printed
 */
static int kmip_connection_tls_verify_pinned_pubkey(
					struct kmip_connection *conn,
					EVP_PKEY *cert_pubkey, bool debug)
{
	EVP_PKEY *pinned_key = NULL;
	int rc = 0;
	FILE *fp;

	fp = fopen(conn->config.tls_pinned_pubkey, "r");
	if (fp == NULL) {
		rc = -errno;
		kmip_debug(debug, "Failed to read pinned public key '%s': %s",
				conn->config.tls_pinned_pubkey, strerror(-rc));
		return rc;
	}

	pinned_key = PEM_read_PUBKEY(fp, NULL, NULL, NULL);
	fclose(fp);

	if (pinned_key == NULL) {
		kmip_debug(debug, "PEM_read_PUBKEY failed: '%s'",
			   conn->config.tls_pinned_pubkey);
		if (debug)
			ERR_print_errors_fp(stderr);
		return -EIO;
	}

#if !OPENSSL_VERSION_PREREQ(3, 0)
	if (EVP_PKEY_cmp(pinned_key, cert_pubkey) != 1) {
#else
	if (EVP_PKEY_eq(pinned_key, cert_pubkey) != 1) {
#endif
		kmip_debug(debug, "Server public key does not match the pinned "
			   "public key '%s'", conn->config.tls_pinned_pubkey);
		rc = -EPERM;
	}

	EVP_PKEY_free(pinned_key);

	return rc;
}

/**
 * Verify the pinned server certificate key of the server of a plain TLS KMIP
 * connection
 *
 * @param conn              the KMIP connection to free
 * @param server_cert       the server certificate
 * @param debug             if true, debug messages are printed
 */
static int kmip_connection_tls_verify_pinned_cert(
					struct kmip_connection *conn,
					X509 *server_cert, bool debug)
{
	X509 *pinned_cert = NULL;
	int rc = 0;
	FILE *fp;

	fp = fopen(conn->config.tls_server_cert, "r");
	if (fp == NULL) {
		rc = -errno;
		kmip_debug(debug, "Failed to read pinned server cert: %s",
				conn->config.tls_server_cert, strerror(-rc));
		return rc;
	}

	pinned_cert = PEM_read_X509(fp, NULL, NULL, NULL);
	fclose(fp);

	if (pinned_cert == NULL) {
		kmip_debug(debug, "PEM_read_X509 failed: '%s'",
			   conn->config.tls_server_cert);
		if (debug)
			ERR_print_errors_fp(stderr);
		return -EIO;
	}

	if (X509_cmp(pinned_cert, server_cert) != 0) {
		kmip_debug(debug, "Server certificate does not match the "
			   "pinned certificate '%s'",
			   conn->config.tls_server_cert);
		rc = -EPERM;
	}

	X509_free(pinned_cert);

	return rc;
}

/**
 * Verify the issuer certificate key of the server of a plain TLS KMIP
 * connection
 *
 * @param conn              the KMIP connection to free
 * @param server_cert       the server certificate
 * @param debug             if true, debug messages are printed
 */
static int kmip_connection_tls_verify_issuer_cert(
					struct kmip_connection *conn,
					X509 *server_cert, bool debug)
{
	X509 *issuer_cert = NULL;
	int rc = 0;
	FILE *fp;

	fp = fopen(conn->config.tls_issuer_cert, "r");
	if (fp == NULL) {
		rc = -errno;
		kmip_debug(debug, "Failed to read issuer cert '%s': %s",
				conn->config.tls_issuer_cert, strerror(-rc));
		return rc;
	}

	issuer_cert = PEM_read_X509(fp, NULL, NULL, NULL);
	fclose(fp);

	if (issuer_cert == NULL) {
		kmip_debug(debug, "PEM_read_X509 failed: '%s'",
			   conn->config.tls_issuer_cert);
		if (debug)
			ERR_print_errors_fp(stderr);
		return -EIO;
	}

	if (X509_check_issued(issuer_cert, server_cert) != X509_V_OK) {
		kmip_debug(debug, "The server certificate was not issued by "
			   "certificate '%s'", conn->config.tls_issuer_cert);
		rc = -EPERM;
	}

	X509_free(issuer_cert);

	return rc;
}

/**
 * Verify the server of a plain TLS KMIP connection
 *
 * @param conn              the KMIP connection to free
 * @param debug             if true, debug messages are printed
 */
static int kmip_connection_tls_verify_server(struct kmip_connection *conn,
					     bool debug)
{
	X509 *server_cert;
	int rc;

	server_cert = SSL_get_peer_certificate(conn->plain_tls.ssl);
	if (server_cert == NULL) {
		kmip_debug(debug, "SSL_get_peer_certificate failed");
		if (debug)
			ERR_print_errors_fp(stderr);
		rc = -EIO;
		goto out;
	}

	if (conn->config.tls_issuer_cert != NULL) {
		rc = kmip_connection_tls_verify_issuer_cert(conn, server_cert,
							    debug);
		if (rc != 0) {
			kmip_debug(debug,
				   "kmip_connection_tls_verify_issuer_cert "
				   "failed");
			goto out;
		}
	}

	if (conn->config.tls_server_cert != NULL) {
		rc = kmip_connection_tls_verify_pinned_cert(conn, server_cert,
							    debug);
		if (rc != 0) {
			kmip_debug(debug,
				   "kmip_connection_tls_verify_pinned_cert "
				   "failed");
			goto out;
		}
	}

	if (conn->config.tls_pinned_pubkey != NULL) {
		rc = kmip_connection_tls_verify_pinned_pubkey(conn,
					X509_get0_pubkey(server_cert),
					debug);
		if (rc != 0) {
			kmip_debug(debug,
				   "kmip_connection_tls_pinned_pubkey failed");
			goto out;
		}
	}

	rc = 0;

out:
	if (server_cert != NULL)
		X509_free(server_cert);

	return 0;
}

/**
 * Initializes a new plain TLS connection to a KMIP server.
 *
 * @param conn              The KMIP connection
 * @param debug             if true, debug messages are printed
 *
 * @returns 0 in case of success, or a negative errno value
 */
int kmip_connection_tls_init(struct kmip_connection *conn, bool debug)
{
	char *hostname = NULL, *port = NULL, *tok;
	struct stat sb;
	int rc;

	if (conn == NULL)
		return -EINVAL;

	conn->plain_tls.ssl_ctx = SSL_CTX_new(TLS_client_method());
	if (conn->plain_tls.ssl_ctx == NULL) {
		kmip_debug(debug, "SSL_CTX_new failed");
		if (debug)
			ERR_print_errors_fp(stderr);
		return -EIO;
	}

	if (SSL_CTX_use_certificate_file(conn->plain_tls.ssl_ctx,
					 conn->config.tls_client_cert,
					 SSL_FILETYPE_PEM) != 1) {
		kmip_debug(debug, "Loading the client certificate from '%s' "
			   "failed", conn->config.tls_client_cert);
		if (debug)
			ERR_print_errors_fp(stderr);
		rc = -EIO;
		goto out;
	}

	if (SSL_CTX_use_PrivateKey(conn->plain_tls.ssl_ctx,
				   conn->config.tls_client_key) != 1) {
		kmip_debug(debug, "Setting the client key from PKEY %p "
			   "failed", conn->config.tls_client_key);
		if (debug)
			ERR_print_errors_fp(stderr);
		rc = -EIO;
		goto out;

	}

	if (conn->config.tls_ca != NULL) {
		if (stat(conn->config.tls_ca, &sb) != 0) {
			rc = -errno;
			kmip_debug(debug, "stat failed on '%s': %s",
				   conn->config.tls_ca, strerror(-rc));
			goto out;
		}

		if (S_ISDIR(sb.st_mode)) {
			if (SSL_CTX_load_verify_locations(
					conn->plain_tls.ssl_ctx, NULL,
					conn->config.tls_ca) != 1) {
				kmip_debug(debug, "Setting the verify location "
					   "to '%s' failed",
					   conn->config.tls_ca);
				if (debug)
					ERR_print_errors_fp(stderr);
				rc = -EIO;
				goto out;
			}
		} else {
			if (SSL_CTX_load_verify_locations(
					conn->plain_tls.ssl_ctx,
					conn->config.tls_ca, NULL) != 1) {
				kmip_debug(debug, "Setting the verify location "
					   "to '%s' failed",
					   conn->config.tls_ca);
				if (debug)
					ERR_print_errors_fp(stderr);
				rc = -EIO;
				goto out;
			}
		}
	}

	conn->plain_tls.bio =
			BIO_new_buffer_ssl_connect(conn->plain_tls.ssl_ctx);
	if (conn->plain_tls.bio == NULL) {
		kmip_debug(debug, "BIO_new_ssl_connect failed");
		if (debug)
			ERR_print_errors_fp(stderr);
		rc = -EIO;
		goto out;
	}

	BIO_get_ssl(conn->plain_tls.bio, &conn->plain_tls.ssl);
	if (conn->plain_tls.ssl == NULL) {
		kmip_debug(debug, "BIO_get_ssl failed");
		if (debug)
			ERR_print_errors_fp(stderr);
		rc = -EIO;
		goto out;
	}

	hostname = strdup(conn->config.server);
	if (hostname == NULL) {
		kmip_debug(debug, "strdup failed");
		rc = -ENOMEM;
		goto out;
	}

	/* Split port number from hostname, if specified */
	if (hostname[0] == '[') {
		/* IPv6 address enclosed in square brackets */
		tok = strchr(hostname, ']');
		if (tok == NULL) {
			kmip_debug(debug, "malformed IPv6 address");
			rc = -EINVAL;
			free(hostname);
			goto out;
		}
		tok++;
		if (*tok == ':') {
			port = tok + 1;
			*tok = 0;
		}
	} else {
		/* hostname or IPv4 address */
		tok = strchr(hostname, ':');
		if (tok != NULL) {
			port = tok + 1;
			*tok = 0;
		}
	}

	kmip_debug(debug, "hostname: '%s'", hostname);
	if (port == NULL) {
		port = KMIP_DEFAULT_PLAIN_TLS_PORT;
		kmip_debug(debug, "port: default (%s)", port);
	} else {
		kmip_debug(debug, "port: %s", port);
	}

	if (conn->config.tls_verify_host) {
		SSL_set_hostflags(conn->plain_tls.ssl,
				  X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
		if (SSL_set1_host(conn->plain_tls.ssl, hostname) != 1) {
			kmip_debug(debug, "SSL_set1_host failed");
			if (debug)
				ERR_print_errors_fp(stderr);
			rc = -EIO;
			goto out;
		}
	}

	SSL_set_verify(conn->plain_tls.ssl, (conn->config.tls_verify_peer ||
					     conn->config.tls_verify_host) ?
				SSL_VERIFY_PEER : SSL_VERIFY_NONE, NULL);

	if (conn->config.tls_cipher_list != NULL) {
		if (SSL_set_cipher_list(conn->plain_tls.ssl,
					conn->config.tls_cipher_list) != 1) {
			kmip_debug(debug, "SSL_set_cipher_list failed");
			if (debug)
				ERR_print_errors_fp(stderr);
			rc = -EIO;
			goto out;
		}
	}

	if (conn->config.tls13_cipher_list != NULL) {
		if (SSL_set_ciphersuites(conn->plain_tls.ssl,
					 conn->config.tls13_cipher_list) != 1) {
			kmip_debug(debug, "SSL_set_ciphersuites failed");
			if (debug)
				ERR_print_errors_fp(stderr);
			rc = -EIO;
			goto out;
		}
	}

	SSL_set_mode(conn->plain_tls.ssl, SSL_MODE_AUTO_RETRY);

	BIO_set_conn_hostname(conn->plain_tls.bio, hostname);
	BIO_set_conn_port(conn->plain_tls.bio, port);

	if (BIO_do_connect(conn->plain_tls.bio) != 1) {
		kmip_debug(debug, "BIO_do_connect failed");
		if (debug)
			ERR_print_errors_fp(stderr);
		rc = -EIO;
		goto out;
	}

	kmip_debug(debug, "TLS connection established using %s",
		   SSL_get_cipher_name(conn->plain_tls.ssl));

	rc = kmip_connection_tls_verify_server(conn, debug);
	if (rc != 0) {
		kmip_debug(debug, "kmip_connection_tls_verify_server failed");
		if (debug)
			ERR_print_errors_fp(stderr);
		rc = -EIO;
		goto out;
	}

	rc = 0;

out:
	if (rc != 0)
		kmip_connection_tls_term(conn);
	if (hostname != NULL)
		free(hostname);

	return rc;
}

/**
 * Perform a request over the KMIP connection
 *
 * @param conn     n        the KMIP connection
 * @param request           the request to send
 * @param response          On return: the received response. Must be freed by
 *                          the caller.
 *
 * @param debug             if true, debug messages are printed
 *
 * @returns 0 in case of success, or a negative errno value
 */
int kmip_connection_tls_perform(struct kmip_connection *conn,
				struct kmip_node *request,
				struct kmip_node **response,
				bool debug)
{
	size_t size;
	int rc;

	if (conn == NULL || request == NULL || response == NULL)
		return -EINVAL;

	*response = NULL;

	/* Send out the request */
	rc = kmip_encode_ttlv(request, conn->plain_tls.bio, &size, debug);
	if (rc != 0) {
		kmip_debug(debug, "kmip_encode_ttlv failed");
		goto out;
	}
	if (BIO_flush(conn->plain_tls.bio) != 1) {
		kmip_debug(debug, "BIO_flush failed");
		goto out;
	}
	kmip_debug(debug, "%lu bytes sent", size);

	/* receive the response */
	rc = kmip_decode_ttlv(conn->plain_tls.bio, NULL, response, debug);
	if (rc != 0 || *response == NULL) {
		kmip_debug(debug, "kmip_decode_ttlv failed");
		goto out;
	}

	rc = 0;

out:
	if (rc != 0) {
		if (BIO_reset(conn->plain_tls.bio) != 1)
			kmip_debug(debug, "BIO_reset failed");
	}

	return rc;
}

/**
 * Terminates a plain TLS KMIP connection.
 *
 * @param conn              the KMIP connection to free
 */
void kmip_connection_tls_term(struct kmip_connection *conn)
{
	if (conn == NULL)
		return;

	if (conn->plain_tls.bio != NULL) {
		BIO_ssl_shutdown(conn->plain_tls.bio);
		BIO_free_all(conn->plain_tls.bio);
	}
	if (conn->plain_tls.ssl_ctx != NULL)
		SSL_CTX_free(conn->plain_tls.ssl_ctx);

	conn->plain_tls.bio = NULL;
	conn->plain_tls.ssl_ctx = NULL;
	conn->plain_tls.ssl = NULL;
}