<commit_before>/*
 * p15emu-westcos.c: pkcs15 emulation for westcos card
 *
  * Copyright (C) 2009 <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 "config.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "internal.h"
#include "pkcs15.h"
#include "cardctl.h"
#include "common/compat_strlcpy.h"

int sc_pkcs15emu_westcos_init_ex(sc_pkcs15_card_t *, sc_pkcs15emu_opt_t *);

static int sc_pkcs15emu_westcos_init(sc_pkcs15_card_t * p15card)
{
	int i, r;
	int modulus_length = 0, usage = 0;
	char buf[256];
	sc_card_t *card = p15card->card;
	sc_serial_number_t serial;
	sc_path_t path;
	sc_file_t *file = NULL;
	sc_format_path("3F00", &path);
	r = sc_select_file(card, &path, &file);
	if (r)
		goto out;
	if (file)
		sc_file_free(file);
	file = NULL;
	if (p15card->label != NULL)
		free(p15card->label);
	p15card->label = strdup("westcos");
	if (p15card->manufacturer_id != NULL)
		free(p15card->manufacturer_id);
	p15card->manufacturer_id = strdup("CEV");

	/* get serial number */
	r = sc_card_ctl(card, SC_CARDCTL_GET_SERIALNR, &serial);
	r = sc_bin_to_hex(serial.value, serial.len, buf, sizeof(buf), 0);
	if (r)
		goto out;
	if (p15card->serial_number != NULL)
		free(p15card->serial_number);
	p15card->serial_number = strdup(buf);
	p15card->version = buf[6];
	p15card->flags = SC_PKCS15_CARD_FLAG_LOGIN_REQUIRED;
	sc_format_path("AAAA", &path);
	r = sc_select_file(card, &path, &file);
	if (r) 
	{
		goto out;
	}
	else
	{
		for (i = 0; i < 1; i++) {
			unsigned int flags;
			struct sc_pkcs15_pin_info pin_info;
			struct sc_pkcs15_object pin_obj;
			memset(&pin_info, 0, sizeof(pin_info));
			memset(&pin_obj, 0, sizeof(pin_obj));
			flags = SC_PKCS15_PIN_FLAG_INITIALIZED;
			if (i == 1) {
				flags |=
					SC_PKCS15_PIN_FLAG_UNBLOCK_DISABLED |
					SC_PKCS15_PIN_FLAG_UNBLOCKING_PIN;
			}
			pin_info.auth_id.len = 1;
			pin_info.auth_id.value[0] = i + 1;
			pin_info.reference = i;
			pin_info.flags = flags;
			pin_info.type = SC_PKCS15_PIN_TYPE_BCD;
			pin_info.min_length = 4;
			pin_info.stored_length = 8;
			pin_info.max_length = 8;
			pin_info.pad_char = 0xff;
			pin_info.path = path;
			pin_info.tries_left = -1;
			if (i == 1)
				strlcpy(pin_obj.label, "Unblock",
					sizeof(pin_obj.label));

			else
				strlcpy(pin_obj.label, "User",
					sizeof(pin_obj.label));
			pin_obj.flags =
				SC_PKCS15_CO_FLAG_MODIFIABLE |
				SC_PKCS15_CO_FLAG_PRIVATE;
			r = sc_pkcs15emu_add_pin_obj(p15card, &pin_obj,
							 &pin_info);
			if (r)
				goto out;
		}
	}
	
	if (file)
		sc_file_free(file);
	file = NULL;
	sc_format_path("0002", &path);
	r = sc_select_file(card, &path, &file);
	if (r) 
	{
		goto out;
	}
	else
	{
		struct sc_pkcs15_cert_info cert_info;
		struct sc_pkcs15_object cert_obj;
		struct sc_pkcs15_pubkey_info pubkey_info;
		struct sc_pkcs15_object pubkey_obj;
		struct sc_pkcs15_pubkey *pkey = NULL;
		memset(&cert_info, 0, sizeof(cert_info));
		memset(&cert_obj, 0, sizeof(cert_obj));
		cert_info.id.len = 1;
		cert_info.id.value[0] = 0x45;
		cert_info.authority = 0;
		cert_info.path = path;
		r = sc_pkcs15_read_certificate(p15card, &cert_info,
					       (sc_pkcs15_cert_t
						**) (&cert_obj.data));
		if (!r) {
			sc_pkcs15_cert_t *cert =
			    (sc_pkcs15_cert_t *) (cert_obj.data);
			strlcpy(cert_obj.label, "User certificat",
				sizeof(cert_obj.label));
			cert_obj.flags = SC_PKCS15_CO_FLAG_MODIFIABLE;
			r = sc_pkcs15emu_add_x509_cert(p15card, &cert_obj,
						       &cert_info);
			if (r)
				goto out;
			pkey = &cert->key;
		}
		memset(&pubkey_info, 0, sizeof(pubkey_info));
		memset(&pubkey_obj, 0, sizeof(pubkey_obj));
		pubkey_info.id.len = 1;
		pubkey_info.id.value[0] = 0x45;
		pubkey_info.modulus_length = modulus_length;
		pubkey_info.key_reference = 1;
		pubkey_info.native = 1;
		pubkey_info.usage =
		    SC_PKCS15_PRKEY_USAGE_VERIFY |
		    SC_PKCS15_PRKEY_USAGE_VERIFYRECOVER |
		    SC_PKCS15_PRKEY_USAGE_ENCRYPT |
		    SC_PKCS15_PRKEY_USAGE_WRAP;
		pubkey_info.path = path;
		strlcpy(pubkey_obj.label, "Public Key",
			sizeof(pubkey_obj.label));
		pubkey_obj.auth_id.len = 1;
		pubkey_obj.auth_id.value[0] = 1;
		pubkey_obj.flags = SC_PKCS15_CO_FLAG_PRIVATE;
		pubkey_obj.type = SC_PKCS15_TYPE_PUBKEY_RSA;
		if (pkey == NULL) {
			pubkey_obj.data = &pubkey_info;
			r = sc_pkcs15_read_pubkey(p15card, &pubkey_obj, &pkey);
			if (r)
				goto out;

			//force rechargement clef et maj infos lors de sc_pkcs15emu_add_rsa_pubkey (sinon modulus = 0)
			pubkey_obj.flags = 0;
		}
		if (pkey->algorithm == SC_ALGORITHM_RSA) {
			modulus_length = (int)(pkey->u.rsa.modulus.len * 8);
		}
		pubkey_info.modulus_length = modulus_length;
		pubkey_obj.data = pkey;
		r = sc_pkcs15emu_add_rsa_pubkey(p15card, &pubkey_obj,
						&pubkey_info);
		if (r < 0)
			goto out;
	}
	if (!usage) {
		usage =
		    SC_PKCS15_PRKEY_USAGE_SIGN | SC_PKCS15_PRKEY_USAGE_DECRYPT |
		    SC_PKCS15_PRKEY_USAGE_NONREPUDIATION;
	}
	if (file)
		sc_file_free(file);
	file = NULL;
	sc_format_path("0001", &path);
	r = sc_select_file(card, &path, &file);
	if (r) 
	{
		goto out;
	}
	else
	{
		struct sc_pkcs15_prkey_info prkey_info;
		struct sc_pkcs15_object prkey_obj;
		memset(&prkey_info, 0, sizeof(prkey_info));<commit_msg>westcos pkcs15 emulate certificat or public key not both<commit_after>/*
 * p15emu-westcos.c: pkcs15 emulation for westcos card
 *
  * Copyright (C) 2009 <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 "config.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "internal.h"
#include "pkcs15.h"
#include "cardctl.h"
#include "common/compat_strlcpy.h"

int sc_pkcs15emu_westcos_init_ex(sc_pkcs15_card_t *, sc_pkcs15emu_opt_t *);

static int sc_pkcs15emu_westcos_init(sc_pkcs15_card_t * p15card)
{
	int i, r;
	int modulus_length = 0, usage = 0;
	char buf[256];
	sc_card_t *card = p15card->card;
	sc_serial_number_t serial;
	sc_path_t path;
	sc_file_t *file = NULL;
	sc_format_path("3F00", &path);
	r = sc_select_file(card, &path, &file);
	if (r)
		goto out;
	if (file)
		sc_file_free(file);
	file = NULL;
	if (p15card->label != NULL)
		free(p15card->label);
	p15card->label = strdup("westcos");
	if (p15card->manufacturer_id != NULL)
		free(p15card->manufacturer_id);
	p15card->manufacturer_id = strdup("CEV");

	/* get serial number */
	r = sc_card_ctl(card, SC_CARDCTL_GET_SERIALNR, &serial);
	r = sc_bin_to_hex(serial.value, serial.len, buf, sizeof(buf), 0);
	if (r)
		goto out;
	if (p15card->serial_number != NULL)
		free(p15card->serial_number);
	p15card->serial_number = strdup(buf);
	p15card->version = buf[6];
	p15card->flags = SC_PKCS15_CARD_FLAG_LOGIN_REQUIRED;
	sc_format_path("AAAA", &path);
	r = sc_select_file(card, &path, &file);
	if (r) 
	{
		goto out;
	}
	else
	{
		for (i = 0; i < 1; i++) {
			unsigned int flags;
			struct sc_pkcs15_pin_info pin_info;
			struct sc_pkcs15_object pin_obj;
			memset(&pin_info, 0, sizeof(pin_info));
			memset(&pin_obj, 0, sizeof(pin_obj));
			flags = SC_PKCS15_PIN_FLAG_INITIALIZED;
			if (i == 1) {
				flags |=
					SC_PKCS15_PIN_FLAG_UNBLOCK_DISABLED |
					SC_PKCS15_PIN_FLAG_UNBLOCKING_PIN;
			}
			pin_info.auth_id.len = 1;
			pin_info.auth_id.value[0] = i + 1;
			pin_info.reference = i;
			pin_info.flags = flags;
			pin_info.type = SC_PKCS15_PIN_TYPE_BCD;
			pin_info.min_length = 4;
			pin_info.stored_length = 8;
			pin_info.max_length = 8;
			pin_info.pad_char = 0xff;
			pin_info.path = path;
			pin_info.tries_left = -1;
			if (i == 1)
				strlcpy(pin_obj.label, "Unblock",
					sizeof(pin_obj.label));

			else
				strlcpy(pin_obj.label, "User",
					sizeof(pin_obj.label));
			pin_obj.flags =
				SC_PKCS15_CO_FLAG_MODIFIABLE |
				SC_PKCS15_CO_FLAG_PRIVATE;
			r = sc_pkcs15emu_add_pin_obj(p15card, &pin_obj,
							 &pin_info);
			if (r)
				goto out;
		}
	}
	
	if (file)
		sc_file_free(file);
	file = NULL;
	sc_format_path("0002", &path);
	r = sc_select_file(card, &path, &file);
	if (r) 
	{
		goto out;
	}
	else
	{
		// certificat file
		struct sc_pkcs15_cert_info cert_info;
		struct sc_pkcs15_object cert_obj;
		struct sc_pkcs15_pubkey_info pubkey_info;
		struct sc_pkcs15_object pubkey_obj;
		struct sc_pkcs15_pubkey *pkey = NULL;
		memset(&cert_info, 0, sizeof(cert_info));
		memset(&cert_obj, 0, sizeof(cert_obj));
		cert_info.id.len = 1;
		cert_info.id.value[0] = 0x45;
		cert_info.authority = 0;
		cert_info.path = path;
		r = sc_pkcs15_read_certificate(p15card, &cert_info,
					       (sc_pkcs15_cert_t
						**) (&cert_obj.data));
		if (!r) {
			sc_pkcs15_cert_t *cert =
			    (sc_pkcs15_cert_t *) (cert_obj.data);
			strlcpy(cert_obj.label, "User certificat",
				sizeof(cert_obj.label));
			cert_obj.flags = SC_PKCS15_CO_FLAG_MODIFIABLE;
			r = sc_pkcs15emu_add_x509_cert(p15card, &cert_obj,
						       &cert_info);
			if (r)
				goto out;
			pkey = &cert->key;
			
			if (pkey->algorithm == SC_ALGORITHM_RSA) {
				modulus_length = (int)(pkey->u.rsa.modulus.len * 8);
			}

		}
		else
		{
			// or public key
			memset(&pubkey_info, 0, sizeof(pubkey_info));
			memset(&pubkey_obj, 0, sizeof(pubkey_obj));
			pubkey_info.id.len = 1;
			pubkey_info.id.value[0] = 0x45;
			pubkey_info.modulus_length = modulus_length;
			pubkey_info.key_reference = 1;
			pubkey_info.native = 1;
			pubkey_info.usage =
			    SC_PKCS15_PRKEY_USAGE_VERIFY |
			    SC_PKCS15_PRKEY_USAGE_VERIFYRECOVER |
			    SC_PKCS15_PRKEY_USAGE_ENCRYPT |
			    SC_PKCS15_PRKEY_USAGE_WRAP;
			pubkey_info.path = path;
			strlcpy(pubkey_obj.label, "Public Key",
				sizeof(pubkey_obj.label));
			pubkey_obj.auth_id.len = 1;
			pubkey_obj.auth_id.value[0] = 1;
			pubkey_obj.flags = SC_PKCS15_CO_FLAG_PRIVATE;
			pubkey_obj.type = SC_PKCS15_TYPE_PUBKEY_RSA;
			if (pkey == NULL) {
				pubkey_obj.data = &pubkey_info;
				r = sc_pkcs15_read_pubkey(p15card, &pubkey_obj, &pkey);
				if (r)
					goto out;
				// not sure if necessary
				pubkey_obj.flags = 0;
			}
			if (pkey->algorithm == SC_ALGORITHM_RSA) {
				modulus_length = (int)(pkey->u.rsa.modulus.len * 8);
			}
			pubkey_info.modulus_length = modulus_length;
			pubkey_obj.data = pkey;
			r = sc_pkcs15emu_add_rsa_pubkey(p15card, &pubkey_obj,
							&pubkey_info);
			if (r < 0)
				goto out;
		}
	}
	if (!usage) {
		usage =
		    SC_PKCS15_PRKEY_USAGE_SIGN | SC_PKCS15_PRKEY_USAGE_DECRYPT |
		    SC_PKCS15_PRKEY_USAGE_NONREPUDIATION;
	}
	if (file)
		sc_file_free(file);
	file = NULL;
	sc_format_path("0001", &path);
	r = sc_select_file(card, &path, &file);
	if (r) 
	{
		goto out;
	}
	else
	{
		struct sc_pkcs15_prkey_info prkey_info;
		struct sc_pkcs15_object prkey_obj;
		memset(&prkey_info, 0, sizeof(prkey_info));
{'fraction_non_alphanumeric': 0.11659830919896208, 'fraction_numerical': 0.03381602075834938, 'mean_word_length': 3.9128289473684212, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 43, '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': '5262174', 'n_tokens_mistral': 5666, 'n_tokens_neox': 4915, 'n_words': 997}