Coverage Report

Created: 2023-03-26 07:33

/src/gnutls/lib/urls.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright © 2014 Nikos Mavrogiannopoulos
3
 *
4
 * Author: Nikos Mavrogiannopoulos
5
 *
6
 * GnuTLS is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public License
8
 * as published by the Free Software Foundation; either version 2.1 of
9
 * the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful, but
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
18
 *
19
 */
20
21
#include "gnutls_int.h"
22
#include "errors.h"
23
#include "str.h"
24
#include "urls.h"
25
#include "system-keys.h"
26
#include <c-strcase.h>
27
28
0
#define MAX_CUSTOM_URLS 8
29
30
gnutls_custom_url_st _gnutls_custom_urls[MAX_CUSTOM_URLS];
31
unsigned _gnutls_custom_urls_size = 0;
32
33
/**
34
 * gnutls_url_is_supported:
35
 * @url: A URI to be tested
36
 *
37
 * Check whether the provided @url is supported.  Depending on the system libraries
38
 * GnuTLS may support pkcs11, tpmkey or other URLs.
39
 *
40
 * Returns: return non-zero if the given URL is supported, and zero if
41
 * it is not known.
42
 *
43
 * Since: 3.1.0
44
 **/
45
unsigned gnutls_url_is_supported(const char *url)
46
0
{
47
0
  unsigned i;
48
49
0
  for (i = 0; i < _gnutls_custom_urls_size; i++) {
50
0
    if (c_strncasecmp
51
0
        (url, _gnutls_custom_urls[i].name,
52
0
         _gnutls_custom_urls[i].name_size) == 0)
53
0
      return 1;
54
0
  }
55
56
#ifdef ENABLE_PKCS11
57
  if (c_strncasecmp(url, PKCS11_URL, sizeof(PKCS11_URL) - 1) == 0)
58
    return 1;
59
#endif
60
#ifdef HAVE_TROUSERS
61
  if (c_strncasecmp(url, TPMKEY_URL, sizeof(TPMKEY_URL) - 1) == 0)
62
    return 1;
63
#endif
64
0
  if (c_strncasecmp(url, SYSTEM_URL, sizeof(SYSTEM_URL) - 1) == 0)
65
0
    return _gnutls_system_url_is_supported(url);
66
67
0
  return 0;
68
0
}
69
70
int _gnutls_url_is_known(const char *url)
71
0
{
72
0
  unsigned i;
73
74
0
  if (c_strncasecmp(url, PKCS11_URL, sizeof(PKCS11_URL) - 1) == 0)
75
0
    return 1;
76
0
  else if (c_strncasecmp(url, TPMKEY_URL, sizeof(TPMKEY_URL) - 1) == 0)
77
0
    return 1;
78
0
  else if (c_strncasecmp(url, SYSTEM_URL, sizeof(SYSTEM_URL) - 1) == 0)
79
0
    return 1;
80
0
  else {
81
0
    for (i = 0; i < _gnutls_custom_urls_size; i++) {
82
0
      if (c_strncasecmp
83
0
          (url, _gnutls_custom_urls[i].name,
84
0
           _gnutls_custom_urls[i].name_size) == 0)
85
0
        return 1;
86
0
    }
87
88
0
    return 0;
89
0
  }
90
0
}
91
92
/**
93
 * gnutls_register_custom_url:
94
 * @st: A %gnutls_custom_url_st structure
95
 *
96
 * Register a custom URL. This will affect the following functions:
97
 * gnutls_url_is_supported(), gnutls_privkey_import_url(),
98
 * gnutls_pubkey_import_url, gnutls_x509_crt_import_url() 
99
 * and all functions that depend on
100
 * them, e.g., gnutls_certificate_set_x509_key_file2().
101
 *
102
 * The provided structure and callback functions must be valid throughout
103
 * the lifetime of the process. The registration of an existing URL type
104
 * will fail with %GNUTLS_E_INVALID_REQUEST. Since GnuTLS 3.5.0 this function
105
 * can be used to override the builtin URLs.
106
 *
107
 * This function is not thread safe.
108
 *
109
 * Returns: returns zero if the given structure was imported or a negative value otherwise.
110
 *
111
 * Since: 3.4.0
112
 **/
113
int gnutls_register_custom_url(const gnutls_custom_url_st * st)
114
0
{
115
0
  unsigned i;
116
117
0
  for (i = 0; i < _gnutls_custom_urls_size; i++) {
118
0
    if (_gnutls_custom_urls[i].name_size == st->name_size &&
119
0
        strcmp(_gnutls_custom_urls[i].name, st->name) == 0) {
120
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
121
0
    }
122
0
  }
123
124
0
  if (_gnutls_custom_urls_size < MAX_CUSTOM_URLS - 1) {
125
0
    memcpy(&_gnutls_custom_urls[_gnutls_custom_urls_size], st,
126
0
           sizeof(*st));
127
0
    _gnutls_custom_urls_size++;
128
0
    return 0;
129
0
  } else {
130
0
    return gnutls_assert_val(GNUTLS_E_UNIMPLEMENTED_FEATURE);
131
0
  }
132
0
}
133
134
/*-
135
 * _gnutls_get_raw_issuer:
136
 * @url: A PKCS 11 url identifying a token
137
 * @cert: is the certificate to find issuer for
138
 * @issuer: Will hold the issuer if any in an allocated buffer.
139
 * @flags: Use zero or flags from %GNUTLS_PKCS11_OBJ_FLAG.
140
 *
141
 * This function will return the issuer of a given certificate in
142
 * DER format.
143
 *
144
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
145
 *   negative error value.
146
 *
147
 * Since: 3.4.0
148
 -*/
149
int _gnutls_get_raw_issuer(const char *url, gnutls_x509_crt_t cert,
150
         gnutls_datum_t * issuer, unsigned int flags)
151
0
{
152
0
  unsigned i;
153
154
#ifdef ENABLE_PKCS11
155
  if (c_strncasecmp(url, PKCS11_URL, PKCS11_URL_SIZE) == 0) {
156
    return gnutls_pkcs11_get_raw_issuer(url, cert, issuer,
157
                GNUTLS_X509_FMT_DER, flags);
158
  }
159
#endif
160
0
  for (i = 0; i < _gnutls_custom_urls_size; i++) {
161
0
    if (c_strncasecmp
162
0
        (url, _gnutls_custom_urls[i].name,
163
0
         _gnutls_custom_urls[i].name_size) == 0) {
164
0
      if (_gnutls_custom_urls[i].get_issuer) {
165
0
        return _gnutls_custom_urls[i].get_issuer(url,
166
0
                   cert,
167
0
                   issuer,
168
0
                   flags);
169
0
      }
170
0
      break;
171
0
    }
172
0
  }
173
174
0
  return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
175
0
}