summaryrefslogtreecommitdiff
path: root/libs/libmosquitto/src/net_mosq_ocsp.c
blob: 32f2a3e3210cbee44a0a24a7a63f034cb449bb8c (plain)
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
/*
Copyright (c) 2009-2014 Roger Light <roger@atchoo.org>
Copyright (c) 2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG), Dr. Lars Voelker <lars.voelker@bmw.de>

All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.

The Eclipse Public License is available at
   http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
  http://www.eclipse.org/org/documents/edl-v10.php.

Contributors:
   Dr. Lars Voelker, BMW AG
*/

/*
COPYRIGHT AND PERMISSION NOTICE of curl on which the ocsp code is based:

Copyright (c) 1996 - 2016, Daniel Stenberg, <daniel@haxx.se>, and many
contributors, see the THANKS file.

All rights reserved.

Permission to use, copy, modify, and distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright
notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.
*/

#ifdef WITH_TLS
#include <logging_mosq.h>
#include <mosquitto_internal.h>
#include <net_mosq.h>

#include <openssl/safestack.h>
#include <openssl/tls1.h>
#include <openssl/ssl.h>
#include <openssl/ocsp.h>

int mosquitto__verify_ocsp_status_cb(SSL * ssl, void *arg)
{
	struct mosquitto *mosq = (struct mosquitto *)arg;
	int ocsp_status, result2, i;
	unsigned char *p;
	const unsigned char *cp;
	OCSP_RESPONSE *rsp = NULL;
	OCSP_BASICRESP *br = NULL;
	X509_STORE     *st = NULL;
	STACK_OF(X509) *ch = NULL;

	long len = SSL_get_tlsext_status_ocsp_resp(mosq->ssl, &p);
	log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: SSL_get_tlsext_status_ocsp_resp returned %ld bytes", len);

	// the following functions expect a const pointer
	cp = (const unsigned char *)p;

	if (!cp || len <= 0) {
		log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: no response");
		goto end;
	}


	rsp = d2i_OCSP_RESPONSE(NULL, &cp, len);
	if (rsp==NULL) {
		log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: invalid response");
		goto end;
	}

	ocsp_status = OCSP_response_status(rsp);
	if(ocsp_status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
		log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: invalid status: %s (%d)",
			       OCSP_response_status_str(ocsp_status), ocsp_status);
		goto end;
	}

	br = OCSP_response_get1_basic(rsp);
	if (!br) {
		log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: invalid response");
		goto end;
	}

	ch = SSL_get_peer_cert_chain(mosq->ssl);
	if (sk_X509_num(ch) <= 0) {
		log__printf(mosq, MOSQ_LOG_ERR, "OCSP: we did not receive certificates of the server (num: %d)", sk_X509_num(ch));
		goto end;
	}

	st = SSL_CTX_get_cert_store(mosq->ssl_ctx);

	// Note:
	//    Other checkers often fix problems in OpenSSL before 1.0.2a (e.g. libcurl).
	//    For all currently supported versions of the OpenSSL project, this is not needed anymore.

	if ((result2=OCSP_basic_verify(br, ch, st, 0)) <= 0) {
		log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: response verification failed (error: %d)", result2);
		goto end;
	}

	for(i = 0; i < OCSP_resp_count(br); i++) {
		int cert_status, crl_reason;
		OCSP_SINGLERESP *single = NULL;

		ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;

		single = OCSP_resp_get0(br, i);
		if(!single)
			continue;

		cert_status = OCSP_single_get0_status(single, &crl_reason, &rev, &thisupd, &nextupd);

		log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: SSL certificate status: %s (%d)",
				OCSP_cert_status_str(cert_status), cert_status);

		switch(cert_status) {
			case V_OCSP_CERTSTATUS_GOOD:
				// Note: A OCSP stapling result will be accepted up to 5 minutes after it expired!
				if(!OCSP_check_validity(thisupd, nextupd, 300L, -1L)) {
					log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: OCSP response has expired");
					goto end;
				}
				break;

			case V_OCSP_CERTSTATUS_REVOKED:
				log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: SSL certificate revocation reason: %s (%d)",
					OCSP_crl_reason_str(crl_reason), crl_reason);
				goto end;

			case V_OCSP_CERTSTATUS_UNKNOWN:
				goto end;

			default:
				log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: SSL certificate revocation status unknown");
				goto end;
		}
	}

	if (br!=NULL)  OCSP_BASICRESP_free(br);
	if (rsp!=NULL) OCSP_RESPONSE_free(rsp);
	return 1; // OK

end:
	if (br!=NULL)  OCSP_BASICRESP_free(br);
	if (rsp!=NULL) OCSP_RESPONSE_free(rsp);
	return 0; // Not OK
}
#endif