Coverage Report

Created: 2023-03-26 07:33

/src/gnutls/lib/pin.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * GnuTLS PIN support for PKCS#11 or TPM
3
 * Copyright (C) 2010-2012 Free Software Foundation, Inc.
4
 * 
5
 * Authors: Nikos Mavrogiannopoulos
6
 *
7
 * The GnuTLS is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public License
9
 * as published by the Free Software Foundation; either version 2.1 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful, but
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
19
 */
20
21
#include "gnutls_int.h"
22
#include <gnutls/pkcs11.h>
23
#include <pin.h>
24
#include "errors.h"
25
26
gnutls_pin_callback_t _gnutls_pin_func;
27
void *_gnutls_pin_data;
28
29
/**
30
 * gnutls_pkcs11_set_pin_function:
31
 * @fn: The PIN callback, a gnutls_pin_callback_t() function.
32
 * @userdata: data to be supplied to callback
33
 *
34
 * This function will set a callback function to be used when a PIN is
35
 * required for PKCS 11 operations.  See
36
 * gnutls_pin_callback_t() on how the callback should behave.
37
 *
38
 * Since: 2.12.0
39
 **/
40
void gnutls_pkcs11_set_pin_function(gnutls_pin_callback_t fn, void *userdata)
41
0
{
42
0
  _gnutls_pin_func = fn;
43
0
  _gnutls_pin_data = userdata;
44
0
}
45
46
/**
47
 * gnutls_pkcs11_get_pin_function:
48
 * @userdata: data to be supplied to callback
49
 *
50
 * This function will return the callback function set using
51
 * gnutls_pkcs11_set_pin_function().
52
 *
53
 * Returns: The function set or NULL otherwise.
54
 * 
55
 * Since: 3.1.0
56
 **/
57
gnutls_pin_callback_t gnutls_pkcs11_get_pin_function(void **userdata)
58
0
{
59
0
  if (_gnutls_pin_func != NULL) {
60
0
    *userdata = _gnutls_pin_data;
61
0
    return _gnutls_pin_func;
62
0
  }
63
0
  return NULL;
64
0
}
65
66
int
67
_gnutls_retrieve_pin(struct pin_info_st *pin_info, const char *url,
68
         const char *label, unsigned flags, char *pin,
69
         unsigned pin_size)
70
0
{
71
0
  int ret;
72
73
0
  if (pin_info && pin_info->cb)
74
0
    ret =
75
0
        pin_info->cb(pin_info->data, 0,
76
0
         (char *)url, label, flags, pin, pin_size);
77
0
  else if (_gnutls_pin_func)
78
0
    ret =
79
0
        _gnutls_pin_func(_gnutls_pin_data, 0,
80
0
             (char *)url, label, flags, pin, pin_size);
81
0
  else
82
0
    ret = gnutls_assert_val(GNUTLS_E_PKCS11_PIN_ERROR);
83
84
0
  return ret;
85
0
}