summaryrefslogtreecommitdiffstats
path: root/lib/pin.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pin.c')
-rw-r--r--lib/pin.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/lib/pin.c b/lib/pin.c
new file mode 100644
index 0000000..2c6f06e
--- /dev/null
+++ b/lib/pin.c
@@ -0,0 +1,88 @@
+/*
+ * GnuTLS PIN support for PKCS#11 or TPM
+ * Copyright (C) 2010-2012 Free Software Foundation, Inc.
+ *
+ * Authors: Nikos Mavrogiannopoulos
+ *
+ * The GnuTLS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>
+ */
+
+#include "gnutls_int.h"
+#include <gnutls/pkcs11.h>
+#include <pin.h>
+#include "errors.h"
+
+gnutls_pin_callback_t _gnutls_pin_func;
+void *_gnutls_pin_data;
+
+/**
+ * gnutls_pkcs11_set_pin_function:
+ * @fn: The PIN callback, a gnutls_pin_callback_t() function.
+ * @userdata: data to be supplied to callback
+ *
+ * This function will set a callback function to be used when a PIN is
+ * required for PKCS 11 operations. See
+ * gnutls_pin_callback_t() on how the callback should behave.
+ *
+ * Since: 2.12.0
+ **/
+void
+gnutls_pkcs11_set_pin_function(gnutls_pin_callback_t fn, void *userdata)
+{
+ _gnutls_pin_func = fn;
+ _gnutls_pin_data = userdata;
+}
+
+/**
+ * gnutls_pkcs11_get_pin_function:
+ * @userdata: data to be supplied to callback
+ *
+ * This function will return the callback function set using
+ * gnutls_pkcs11_set_pin_function().
+ *
+ * Returns: The function set or NULL otherwise.
+ *
+ * Since: 3.1.0
+ **/
+gnutls_pin_callback_t gnutls_pkcs11_get_pin_function(void **userdata)
+{
+ if (_gnutls_pin_func != NULL) {
+ *userdata = _gnutls_pin_data;
+ return _gnutls_pin_func;
+ }
+ return NULL;
+}
+
+int
+_gnutls_retrieve_pin(struct pin_info_st *pin_info, const char *url, const char *label,
+ unsigned flags,
+ char *pin, unsigned pin_size)
+{
+ int ret;
+
+ if (pin_info && pin_info->cb)
+ ret =
+ pin_info->cb(pin_info->data, 0,
+ (char *) url, label, flags,
+ pin, pin_size);
+ else if (_gnutls_pin_func)
+ ret =
+ _gnutls_pin_func(_gnutls_pin_data, 0,
+ (char *) url, label, flags,
+ pin, pin_size);
+ else
+ ret = gnutls_assert_val(GNUTLS_E_PKCS11_PIN_ERROR);
+
+ return ret;
+}