Coverage Report

Created: 2025-10-24 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mosquitto/lib/net_mosq_ocsp.c
Line
Count
Source
1
/*
2
Copyright (c) 2009-2021 Roger Light <roger@atchoo.org>
3
Copyright (c) 2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG), Dr. Lars Voelker <lars.voelker@bmw.de>
4
5
All rights reserved. This program and the accompanying materials
6
are made available under the terms of the Eclipse Public License 2.0
7
and Eclipse Distribution License v1.0 which accompany this distribution.
8
9
The Eclipse Public License is available at
10
   https://www.eclipse.org/legal/epl-2.0/
11
and the Eclipse Distribution License is available at
12
  http://www.eclipse.org/org/documents/edl-v10.php.
13
14
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
15
16
Contributors:
17
   Dr. Lars Voelker, BMW AG
18
*/
19
20
/*
21
COPYRIGHT AND PERMISSION NOTICE of curl on which the ocsp code is based:
22
23
Copyright (c) 1996 - 2016, Daniel Stenberg, <daniel@haxx.se>, and many
24
contributors, see the THANKS file.
25
26
All rights reserved.
27
28
Permission to use, copy, modify, and distribute this software for any purpose
29
with or without fee is hereby granted, provided that the above copyright
30
notice and this permission notice appear in all copies.
31
32
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN
35
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
36
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
37
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
38
OR OTHER DEALINGS IN THE SOFTWARE.
39
40
Except as contained in this notice, the name of a copyright holder shall not
41
be used in advertising or otherwise to promote the sale, use or other dealings
42
in this Software without prior written authorization of the copyright holder.
43
*/
44
45
#include "config.h"
46
47
#ifdef WITH_TLS
48
#include <logging_mosq.h>
49
#include <mosquitto_internal.h>
50
#include <net_mosq.h>
51
52
#include <openssl/safestack.h>
53
#include <openssl/tls1.h>
54
#include <openssl/ssl.h>
55
#include <openssl/ocsp.h>
56
57
58
int mosquitto__verify_ocsp_status_cb(SSL *ssl, void *arg)
59
0
{
60
0
  struct mosquitto *mosq = (struct mosquitto *)arg;
61
0
  int ocsp_status, result2, i;
62
0
  unsigned char *p;
63
0
  const unsigned char *cp;
64
0
  OCSP_RESPONSE *rsp = NULL;
65
0
  OCSP_BASICRESP *br = NULL;
66
0
  X509_STORE     *st = NULL;
67
0
  STACK_OF(X509) *ch = NULL;
68
0
  long len;
69
70
0
  UNUSED(ssl);
71
72
0
  len = SSL_get_tlsext_status_ocsp_resp(mosq->ssl, &p);
73
0
  log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: SSL_get_tlsext_status_ocsp_resp returned %ld bytes", len);
74
75
  /* the following functions expect a const pointer */
76
0
  cp = (const unsigned char *)p;
77
78
0
  if(!cp || len <= 0){
79
0
    log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: no response");
80
0
    goto end;
81
0
  }
82
83
84
0
  rsp = d2i_OCSP_RESPONSE(NULL, &cp, len);
85
0
  if(rsp==NULL){
86
0
    log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: invalid response");
87
0
    goto end;
88
0
  }
89
90
0
  ocsp_status = OCSP_response_status(rsp);
91
0
  if(ocsp_status != OCSP_RESPONSE_STATUS_SUCCESSFUL){
92
0
    log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: invalid status: %s (%d)",
93
0
        OCSP_response_status_str(ocsp_status), ocsp_status);
94
0
    goto end;
95
0
  }
96
97
0
  br = OCSP_response_get1_basic(rsp);
98
0
  if(!br){
99
0
    log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: invalid response");
100
0
    goto end;
101
0
  }
102
103
0
  ch = SSL_get_peer_cert_chain(mosq->ssl);
104
0
  if(sk_X509_num(ch) <= 0){
105
0
    log__printf(mosq, MOSQ_LOG_ERR, "OCSP: we did not receive certificates of the server (num: %d)", sk_X509_num(ch));
106
0
    goto end;
107
0
  }
108
109
0
  st = SSL_CTX_get_cert_store(mosq->ssl_ctx);
110
111
  /* Note:
112
   * Other checkers often fix problems in OpenSSL before 1.0.2a (e.g. libcurl).
113
   * For all currently supported versions of the OpenSSL project, this is not needed anymore.
114
   */
115
116
0
  if((result2=OCSP_basic_verify(br, ch, st, 0)) <= 0){
117
0
    log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: response verification failed (error: %d)", result2);
118
0
    goto end;
119
0
  }
120
121
0
  for(i = 0; i < OCSP_resp_count(br); i++){
122
0
    int cert_status, crl_reason;
123
0
    OCSP_SINGLERESP *single = NULL;
124
125
0
    ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
126
127
0
    single = OCSP_resp_get0(br, i);
128
0
    if(!single){
129
0
      continue;
130
0
    }
131
132
0
    cert_status = OCSP_single_get0_status(single, &crl_reason, &rev, &thisupd, &nextupd);
133
134
0
    log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: SSL certificate status: %s (%d)",
135
0
        OCSP_cert_status_str(cert_status), cert_status);
136
137
0
    switch(cert_status){
138
0
      case V_OCSP_CERTSTATUS_GOOD:
139
        /* Note: A OCSP stapling result will be accepted up to 5 minutes after it expired! */
140
0
        if(!OCSP_check_validity(thisupd, nextupd, 300L, -1L)){
141
0
          log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: OCSP response has expired");
142
0
          goto end;
143
0
        }
144
0
        break;
145
146
0
      case V_OCSP_CERTSTATUS_REVOKED:
147
0
        log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: SSL certificate revocation reason: %s (%d)",
148
0
            OCSP_crl_reason_str(crl_reason), crl_reason);
149
0
        goto end;
150
151
0
      case V_OCSP_CERTSTATUS_UNKNOWN:
152
0
        goto end;
153
154
0
      default:
155
0
        log__printf(mosq, MOSQ_LOG_DEBUG, "OCSP: SSL certificate revocation status unknown");
156
0
        goto end;
157
0
    }
158
0
  }
159
160
0
  if(br!=NULL){
161
0
    OCSP_BASICRESP_free(br);
162
0
  }
163
0
  if(rsp!=NULL){
164
0
    OCSP_RESPONSE_free(rsp);
165
0
  }
166
0
  return 1; /* OK */
167
168
0
end:
169
0
  if(br!=NULL){
170
0
    OCSP_BASICRESP_free(br);
171
0
  }
172
0
  if(rsp!=NULL){
173
0
    OCSP_RESPONSE_free(rsp);
174
0
  }
175
0
  return 0; /* Not OK */
176
0
}
177
#endif