<commit_before>/* libp11, a simple layer on to of PKCS#11 API
 * Copyright (C) 2005 <NAME> <<EMAIL>>
 * Copyright (C) 2016-2018 <NAME> <<EMAIL>>
 *
 *  This library 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 library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */

#include "libp11-int.h"
#include <string.h>
#include <openssl/ui.h>
#include <openssl/bn.h>

#ifdef _WIN32
#define strncasecmp strnicmp
#endif

/* The maximum length of PIN */
#define MAX_PIN_LENGTH   32

static int pkcs11_find_keys(PKCS11_SLOT_private *, CK_SESSION_HANDLE, unsigned int);
static int pkcs11_next_key(PKCS11_CTX_private *ctx, PKCS11_SLOT_private *,
	CK_SESSION_HANDLE session, CK_OBJECT_CLASS type);
static int pkcs11_init_key(PKCS11_SLOT_private *, CK_SESSION_HANDLE session,
	CK_OBJECT_HANDLE o, CK_OBJECT_CLASS type, PKCS11_KEY **);
static int pkcs11_store_key(PKCS11_SLOT_private *, EVP_PKEY *, CK_OBJECT_CLASS,
	char *, unsigned char *, size_t, PKCS11_KEY **);

/* Helper to acquire object handle from given template */
static CK_OBJECT_HANDLE pkcs11_handle_from_template(PKCS11_SLOT_private *slot,
	CK_SESSION_HANDLE session, PKCS11_TEMPLATE *tmpl)
{
	PKCS11_CTX_private *ctx = slot->ctx;
	CK_OBJECT_HANDLE object;
	CK_ULONG count;
	CK_RV rv;

	rv = CRYPTOKI_call(ctx,
		C_FindObjectsInit(session, tmpl->attrs, tmpl->nattr));
	if (rv == CKR_OK) {
		rv = CRYPTOKI_call(ctx,
			C_FindObjects(session, &object, 1, &count));
		CRYPTOKI_call(ctx, C_FindObjectsFinal(session));
	}
	pkcs11_zap_attrs(tmpl);

	if (rv == CKR_OK && count == 1)
		return object;

	return CK_INVALID_HANDLE;
}

/* Get object from a handle */
PKCS11_OBJECT_private *pkcs11_object_from_handle(PKCS11_SLOT_private *slot,
		CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object)
{
	PKCS11_CTX_private *ctx = slot->ctx;
	PKCS11_OBJECT_private *obj;
	PKCS11_OBJECT_ops *ops = NULL;
	CK_OBJECT_CLASS object_class = -1;
	CK_KEY_TYPE key_type = -1;
	CK_CERTIFICATE_TYPE cert_type = -1;
	CK_ULONG size;
	unsigned char *data;

	if (pkcs11_getattr_val(ctx, session, object, CKA_CLASS,
			(CK_BYTE *) &object_class, sizeof(object_class)))
		return NULL;

	switch (object_class) {
	case CKO_PUBLIC_KEY:
	case CKO_PRIVATE_KEY:
		if (pkcs11_getattr_val(ctx, session, object, CKA_KEY_TYPE,
				(CK_BYTE *)&key_type, sizeof(key_type)))
			return NULL;
		switch (key_type) {
		case CKK_RSA:
			ops = &pkcs11_rsa_ops;
			break;
#ifndef OPENSSL_NO_EC
		case CKK_EC:
			ops = &pkcs11_ec_ops;
			break;
#endif
		default:
			/* Ignore any keys we don't understand */
			return 0;
		}<commit_msg>Fix a crash on LLP64, incl. 64-bit Windows<commit_after>/* libp11, a simple layer on to of PKCS#11 API
 * Copyright (C) 2005 <NAME> <<EMAIL>>
 * Copyright (C) 2016-2018 <NAME> <<EMAIL>>
 *
 *  This library 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 library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */

#include "libp11-int.h"
#include <string.h>
#include <openssl/ui.h>
#include <openssl/bn.h>

#ifdef _WIN32
#define strncasecmp strnicmp
#endif

/* The maximum length of PIN */
#define MAX_PIN_LENGTH   32

static int pkcs11_find_keys(PKCS11_SLOT_private *, CK_SESSION_HANDLE, unsigned int);
static int pkcs11_next_key(PKCS11_CTX_private *ctx, PKCS11_SLOT_private *,
	CK_SESSION_HANDLE session, CK_OBJECT_CLASS type);
static int pkcs11_init_key(PKCS11_SLOT_private *, CK_SESSION_HANDLE session,
	CK_OBJECT_HANDLE o, CK_OBJECT_CLASS type, PKCS11_KEY **);
static int pkcs11_store_key(PKCS11_SLOT_private *, EVP_PKEY *, CK_OBJECT_CLASS,
	char *, unsigned char *, size_t, PKCS11_KEY **);

/* Helper to acquire object handle from given template */
static CK_OBJECT_HANDLE pkcs11_handle_from_template(PKCS11_SLOT_private *slot,
	CK_SESSION_HANDLE session, PKCS11_TEMPLATE *tmpl)
{
	PKCS11_CTX_private *ctx = slot->ctx;
	CK_OBJECT_HANDLE object;
	CK_ULONG count;
	CK_RV rv;

	rv = CRYPTOKI_call(ctx,
		C_FindObjectsInit(session, tmpl->attrs, tmpl->nattr));
	if (rv == CKR_OK) {
		rv = CRYPTOKI_call(ctx,
			C_FindObjects(session, &object, 1, &count));
		CRYPTOKI_call(ctx, C_FindObjectsFinal(session));
	}
	pkcs11_zap_attrs(tmpl);

	if (rv == CKR_OK && count == 1)
		return object;

	return CK_INVALID_HANDLE;
}

/* Get object from a handle */
PKCS11_OBJECT_private *pkcs11_object_from_handle(PKCS11_SLOT_private *slot,
		CK_SESSION_HANDLE session, CK_OBJECT_HANDLE object)
{
	PKCS11_CTX_private *ctx = slot->ctx;
	PKCS11_OBJECT_private *obj;
	PKCS11_OBJECT_ops *ops = NULL;
	CK_OBJECT_CLASS object_class = -1;
	CK_KEY_TYPE key_type = -1;
	CK_CERTIFICATE_TYPE cert_type = -1;
	size_t size;
	unsigned char *data;

	if (pkcs11_getattr_val(ctx, session, object, CKA_CLASS,
			(CK_BYTE *) &object_class, sizeof(object_class)))
		return NULL;

	switch (object_class) {
	case CKO_PUBLIC_KEY:
	case CKO_PRIVATE_KEY:
		if (pkcs11_getattr_val(ctx, session, object, CKA_KEY_TYPE,
				(CK_BYTE *)&key_type, sizeof(key_type)))
			return NULL;
		switch (key_type) {
		case CKK_RSA:
			ops = &pkcs11_rsa_ops;
			break;
#ifndef OPENSSL_NO_EC
		case CKK_EC:
			ops = &pkcs11_ec_ops;
			break;
#endif
		default:
			/* Ignore any keys we don't understand */
			return 0;
		}
{'fraction_non_alphanumeric': 0.09248463841184812, 'fraction_numerical': 0.03088073105404128, 'mean_word_length': 4.823853211009174, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1819898', 'n_tokens_mistral': 2510, 'n_tokens_neox': 2268, 'n_words': 663}