2009-09-12 11:46:00 +00:00
|
|
|
/*
|
2010-06-11 07:36:01 +00:00
|
|
|
* pkcs15-westcos.c: pkcs15 emulation for westcos card
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 francois.leblanc@cev-sa.com
|
2009-09-12 11:46:00 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2015-04-22 21:55:33 +00:00
|
|
|
#if HAVE_CONFIG_H
|
2010-03-04 08:14:36 +00:00
|
|
|
#include "config.h"
|
2015-04-22 21:55:33 +00:00
|
|
|
#endif
|
2009-09-12 11:46:00 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2010-03-04 08:14:36 +00:00
|
|
|
#include "internal.h"
|
2009-09-12 11:46:00 +00:00
|
|
|
#include "pkcs15.h"
|
|
|
|
#include "cardctl.h"
|
2010-03-04 08:14:36 +00:00
|
|
|
#include "common/compat_strlcpy.h"
|
2009-09-12 11:46:00 +00:00
|
|
|
|
|
|
|
static int sc_pkcs15emu_westcos_init(sc_pkcs15_card_t * p15card)
|
|
|
|
{
|
|
|
|
int i, r;
|
2011-02-05 22:12:14 +00:00
|
|
|
int modulus_length = 0;
|
2009-10-25 20:22:11 +00:00
|
|
|
char buf[256];
|
2009-09-12 11:46:00 +00:00
|
|
|
sc_card_t *card = p15card->card;
|
|
|
|
sc_serial_number_t serial;
|
|
|
|
sc_path_t path;
|
|
|
|
sc_format_path("3F00", &path);
|
2015-10-14 20:46:55 +00:00
|
|
|
r = sc_select_file(card, &path, NULL);
|
2009-09-12 11:46:00 +00:00
|
|
|
if (r)
|
|
|
|
goto out;
|
2020-06-10 10:35:19 +00:00
|
|
|
free(p15card->tokeninfo->label);
|
2010-10-05 15:44:58 +00:00
|
|
|
p15card->tokeninfo->label = strdup("westcos");
|
2020-06-10 10:35:19 +00:00
|
|
|
free(p15card->tokeninfo->manufacturer_id);
|
2010-10-05 15:44:58 +00:00
|
|
|
p15card->tokeninfo->manufacturer_id = strdup("CEV");
|
2009-09-12 11:46:00 +00:00
|
|
|
|
|
|
|
/* get serial number */
|
|
|
|
r = sc_card_ctl(card, SC_CARDCTL_GET_SERIALNR, &serial);
|
2017-10-31 09:12:12 +00:00
|
|
|
if (r)
|
|
|
|
goto out;
|
2009-09-12 11:46:00 +00:00
|
|
|
r = sc_bin_to_hex(serial.value, serial.len, buf, sizeof(buf), 0);
|
|
|
|
if (r)
|
|
|
|
goto out;
|
2020-06-10 10:35:19 +00:00
|
|
|
free(p15card->tokeninfo->serial_number);
|
2010-10-05 15:44:58 +00:00
|
|
|
p15card->tokeninfo->serial_number = strdup(buf);
|
2009-09-12 11:46:00 +00:00
|
|
|
sc_format_path("AAAA", &path);
|
2015-10-14 20:46:55 +00:00
|
|
|
r = sc_select_file(card, &path, NULL);
|
2009-12-03 07:18:19 +00:00
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-04 23:43:05 +00:00
|
|
|
for (i = 0; i <= 1; i++) {
|
2009-09-12 11:46:00 +00:00
|
|
|
unsigned int flags;
|
2011-06-05 15:46:25 +00:00
|
|
|
struct sc_pkcs15_auth_info pin_info;
|
2009-09-12 11:46:00 +00:00
|
|
|
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 |=
|
2009-12-03 07:18:19 +00:00
|
|
|
SC_PKCS15_PIN_FLAG_UNBLOCK_DISABLED |
|
|
|
|
SC_PKCS15_PIN_FLAG_UNBLOCKING_PIN;
|
2009-09-12 11:46:00 +00:00
|
|
|
}
|
2011-06-05 15:46:25 +00:00
|
|
|
pin_info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN;
|
2009-09-12 11:46:00 +00:00
|
|
|
pin_info.auth_id.len = 1;
|
|
|
|
pin_info.auth_id.value[0] = i + 1;
|
2011-06-05 15:46:25 +00:00
|
|
|
pin_info.attrs.pin.reference = i;
|
|
|
|
pin_info.attrs.pin.flags = flags;
|
|
|
|
pin_info.attrs.pin.type = SC_PKCS15_PIN_TYPE_BCD;
|
|
|
|
pin_info.attrs.pin.min_length = 4;
|
|
|
|
pin_info.attrs.pin.stored_length = 8;
|
|
|
|
pin_info.attrs.pin.max_length = 8;
|
|
|
|
pin_info.attrs.pin.pad_char = 0xff;
|
2009-09-12 11:46:00 +00:00
|
|
|
pin_info.path = path;
|
|
|
|
pin_info.tries_left = -1;
|
2016-08-11 16:26:01 +00:00
|
|
|
pin_info.logged_in = SC_PIN_STATE_UNKNOWN;
|
2009-09-12 11:46:00 +00:00
|
|
|
if (i == 1)
|
|
|
|
strlcpy(pin_obj.label, "Unblock",
|
|
|
|
sizeof(pin_obj.label));
|
|
|
|
|
2016-08-13 22:55:13 +00:00
|
|
|
else {
|
2009-09-12 11:46:00 +00:00
|
|
|
strlcpy(pin_obj.label, "User",
|
|
|
|
sizeof(pin_obj.label));
|
2016-08-13 22:55:13 +00:00
|
|
|
pin_obj.auth_id.len = 1;
|
|
|
|
pin_obj.auth_id.value[0] = 2;
|
|
|
|
}
|
2009-09-12 11:46:00 +00:00
|
|
|
pin_obj.flags =
|
2009-12-03 07:18:19 +00:00
|
|
|
SC_PKCS15_CO_FLAG_MODIFIABLE |
|
|
|
|
SC_PKCS15_CO_FLAG_PRIVATE;
|
2009-09-12 11:46:00 +00:00
|
|
|
r = sc_pkcs15emu_add_pin_obj(p15card, &pin_obj,
|
2009-12-03 07:18:19 +00:00
|
|
|
&pin_info);
|
2009-09-12 11:46:00 +00:00
|
|
|
if (r)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2009-12-03 07:18:19 +00:00
|
|
|
|
2009-09-12 11:46:00 +00:00
|
|
|
sc_format_path("0002", &path);
|
2015-10-14 20:46:55 +00:00
|
|
|
r = sc_select_file(card, &path, NULL);
|
2009-12-03 07:18:19 +00:00
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-22 10:19:35 +00:00
|
|
|
/* certificate file */
|
2009-09-12 11:46:00 +00:00
|
|
|
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;
|
2021-02-25 09:40:11 +00:00
|
|
|
sc_pkcs15_cert_t *cert = NULL;
|
|
|
|
|
2009-09-12 11:46:00 +00:00
|
|
|
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;
|
2021-02-25 09:40:11 +00:00
|
|
|
r = sc_pkcs15_read_certificate(p15card, &cert_info, &cert);
|
|
|
|
cert_obj.data = (void *) cert;
|
2009-09-12 11:46:00 +00:00
|
|
|
if (!r) {
|
2020-08-29 21:19:37 +00:00
|
|
|
strlcpy(cert_obj.label, "User certificate",
|
2009-09-12 11:46:00 +00:00
|
|
|
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;
|
2010-10-25 20:58:02 +00:00
|
|
|
pkey = cert->key;
|
2010-05-07 12:49:53 +00:00
|
|
|
|
|
|
|
if (pkey->algorithm == SC_ALGORITHM_RSA) {
|
|
|
|
modulus_length = (int)(pkey->u.rsa.modulus.len * 8);
|
|
|
|
}
|
2009-09-12 11:46:00 +00:00
|
|
|
|
|
|
|
}
|
2010-05-07 12:49:53 +00:00
|
|
|
else
|
|
|
|
{
|
2010-06-11 07:35:45 +00:00
|
|
|
/* or public key */
|
2010-05-07 12:49:53 +00:00
|
|
|
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;
|
2010-06-11 07:35:45 +00:00
|
|
|
/* not sure if necessary */
|
2010-05-07 12:49:53 +00:00
|
|
|
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;
|
2009-09-12 11:46:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sc_format_path("0001", &path);
|
2015-10-14 20:46:55 +00:00
|
|
|
r = sc_select_file(card, &path, NULL);
|
2009-12-03 07:18:19 +00:00
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-12 11:46:00 +00:00
|
|
|
struct sc_pkcs15_prkey_info prkey_info;
|
|
|
|
struct sc_pkcs15_object prkey_obj;
|
|
|
|
memset(&prkey_info, 0, sizeof(prkey_info));
|
|
|
|
memset(&prkey_obj, 0, sizeof(prkey_obj));
|
|
|
|
prkey_info.id.len = 1;
|
|
|
|
prkey_info.id.value[0] = 0x45;
|
|
|
|
prkey_info.usage =
|
2009-12-03 07:18:19 +00:00
|
|
|
SC_PKCS15_PRKEY_USAGE_SIGN | SC_PKCS15_PRKEY_USAGE_DECRYPT
|
|
|
|
| SC_PKCS15_PRKEY_USAGE_NONREPUDIATION;
|
2009-09-12 11:46:00 +00:00
|
|
|
prkey_info.native = 1;
|
|
|
|
prkey_info.key_reference = 1;
|
|
|
|
prkey_info.modulus_length = modulus_length;
|
|
|
|
prkey_info.path = path;
|
|
|
|
strlcpy(prkey_obj.label, "Private Key",
|
|
|
|
sizeof(prkey_obj.label));
|
|
|
|
prkey_obj.flags = SC_PKCS15_CO_FLAG_PRIVATE;
|
|
|
|
prkey_obj.auth_id.len = 1;
|
|
|
|
prkey_obj.auth_id.value[0] = 1;
|
|
|
|
r = sc_pkcs15emu_add_rsa_prkey(p15card, &prkey_obj,
|
2009-12-03 07:18:19 +00:00
|
|
|
&prkey_info);
|
2009-09-12 11:46:00 +00:00
|
|
|
if (r < 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
r = 0;
|
2009-12-03 07:18:19 +00:00
|
|
|
out:
|
2009-09-12 11:46:00 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int westcos_detect_card(sc_pkcs15_card_t * p15card)
|
|
|
|
{
|
|
|
|
sc_card_t *card = p15card->card;
|
|
|
|
sc_context_t *ctx = card->ctx;
|
2011-02-05 17:42:49 +00:00
|
|
|
const char *name = "WESTCOS";
|
2018-11-22 08:31:29 +00:00
|
|
|
sc_log(ctx,
|
2010-03-15 12:17:13 +00:00
|
|
|
"westcos_detect_card (%s)", card->name);
|
2009-09-12 11:46:00 +00:00
|
|
|
if (strncmp(card->name, name, strlen(name)))
|
|
|
|
return SC_ERROR_WRONG_CARD;
|
|
|
|
return SC_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sc_pkcs15emu_westcos_init_ex(sc_pkcs15_card_t * p15card,
|
2019-03-26 14:14:25 +00:00
|
|
|
struct sc_aid *aid)
|
2009-09-12 11:46:00 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
sc_card_t *card = p15card->card;
|
|
|
|
sc_context_t *ctx = card->ctx;
|
2018-11-22 08:31:29 +00:00
|
|
|
sc_log(ctx,
|
2010-03-15 12:17:13 +00:00
|
|
|
"sc_pkcs15_init_func_ex westcos\n");
|
2009-09-12 11:46:00 +00:00
|
|
|
r = westcos_detect_card(p15card);
|
|
|
|
if (r)
|
|
|
|
return SC_ERROR_WRONG_CARD;
|
|
|
|
return sc_pkcs15emu_westcos_init(p15card);
|
|
|
|
}
|