2004-07-14 21:13:54 +00:00
|
|
|
/*
|
|
|
|
* PKCS15 emulation layer for EstEID card.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004, Martin Paljak <martin@paljak.pri.ee>
|
|
|
|
* Copyright (C) 2004, Bud P. Bruegger <bud@comune.grosseto.it>
|
|
|
|
* Copyright (C) 2004, Antonino Iacono <ant_iacono@tin.it>
|
|
|
|
* Copyright (C) 2003, Olaf Kirch <okir@suse.de>
|
|
|
|
*
|
|
|
|
* 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 "internal.h"
|
|
|
|
#include "pkcs15.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "esteid.h"
|
2008-03-06 16:06:59 +00:00
|
|
|
#include <compat_strlcpy.h>
|
2004-07-14 21:13:54 +00:00
|
|
|
|
2009-01-16 17:48:26 +00:00
|
|
|
#ifdef ENABLE_ICONV
|
2009-01-15 23:20:48 +00:00
|
|
|
#include <iconv.h>
|
|
|
|
#endif
|
|
|
|
|
2004-12-15 17:34:15 +00:00
|
|
|
int sc_pkcs15emu_esteid_init_ex(sc_pkcs15_card_t *, sc_pkcs15emu_opt_t *);
|
|
|
|
|
2004-07-14 21:13:54 +00:00
|
|
|
static void
|
|
|
|
set_string (char **strp, const char *value)
|
|
|
|
{
|
2005-02-03 22:44:19 +00:00
|
|
|
if (*strp)
|
2005-03-18 20:36:52 +00:00
|
|
|
free (*strp);
|
2005-02-03 22:44:19 +00:00
|
|
|
*strp = value ? strdup (value) : NULL;
|
2004-07-14 21:13:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
select_esteid_df (sc_card_t * card)
|
|
|
|
{
|
2005-02-03 22:44:19 +00:00
|
|
|
int r;
|
|
|
|
sc_path_t tmppath;
|
2006-01-23 17:37:10 +00:00
|
|
|
sc_format_path ("3F00EEEE", &tmppath);
|
2005-02-03 22:44:19 +00:00
|
|
|
tmppath.type = SC_PATH_TYPE_PATH;
|
|
|
|
r = sc_select_file (card, &tmppath, NULL);
|
|
|
|
SC_TEST_RET (card->ctx, r, "esteid select DF failed");
|
|
|
|
return r;
|
2004-07-14 21:13:54 +00:00
|
|
|
}
|
|
|
|
|
2004-12-15 17:34:15 +00:00
|
|
|
static int
|
2004-07-14 21:13:54 +00:00
|
|
|
sc_pkcs15emu_esteid_init (sc_pkcs15_card_t * p15card)
|
|
|
|
{
|
2005-02-03 22:44:19 +00:00
|
|
|
sc_card_t *card = p15card->card;
|
2009-01-16 17:48:26 +00:00
|
|
|
#ifdef ENABLE_ICONV
|
2009-01-15 23:20:48 +00:00
|
|
|
iconv_t iso_utf;
|
|
|
|
char *inptr, *outptr;
|
|
|
|
size_t inbytes, outbytes, result;
|
|
|
|
unsigned char label[64], name1[32], name2[32];
|
|
|
|
#endif
|
|
|
|
unsigned char buff[128];
|
2005-02-03 22:44:19 +00:00
|
|
|
int r, i, flags;
|
|
|
|
sc_path_t tmppath;
|
|
|
|
|
2005-06-29 11:32:48 +00:00
|
|
|
set_string (&p15card->label, "ID-kaart");
|
2005-02-03 22:44:19 +00:00
|
|
|
set_string (&p15card->manufacturer_id, "AS Sertifitseerimiskeskus");
|
|
|
|
|
2009-01-15 23:20:48 +00:00
|
|
|
/* Select application directory */
|
2005-02-11 20:09:34 +00:00
|
|
|
sc_format_path ("3f00eeee5044", &tmppath);
|
2005-02-03 22:44:19 +00:00
|
|
|
tmppath.type = SC_PATH_TYPE_PATH;
|
|
|
|
r = sc_select_file (card, &tmppath, NULL);
|
|
|
|
SC_TEST_RET (card->ctx, r, "select esteid PD failed");
|
2009-01-15 23:20:48 +00:00
|
|
|
|
|
|
|
/* read the serial (document number) */
|
|
|
|
r = sc_read_record (card, SC_ESTEID_PD_DOCUMENT_NR, buff, sizeof(buff), SC_RECORD_BY_REC_NR);
|
2005-02-03 22:44:19 +00:00
|
|
|
SC_TEST_RET (card->ctx, r, "read document number failed");
|
|
|
|
buff[r] = '\0';
|
|
|
|
set_string (&p15card->serial_number, (const char *) buff);
|
|
|
|
|
2009-01-16 17:48:26 +00:00
|
|
|
#ifdef ENABLE_ICONV
|
2009-01-15 23:20:48 +00:00
|
|
|
/* Read the name of the cardholder and convert it into UTF-8 */
|
|
|
|
iso_utf = iconv_open ("UTF-8", "ISO-8859-1");
|
|
|
|
if (iso_utf == (iconv_t) -1)
|
|
|
|
return SC_ERROR_INTERNAL;
|
|
|
|
|
|
|
|
r = sc_read_record (card, SC_ESTEID_PD_GIVEN_NAMES1, buff, sizeof(buff), SC_RECORD_BY_REC_NR);
|
|
|
|
SC_TEST_RET (card->ctx, r, "read name1 failed");
|
|
|
|
inptr = buff;
|
|
|
|
outptr = name1;
|
|
|
|
inbytes = r;
|
|
|
|
outbytes = 32;
|
|
|
|
result = iconv(iso_utf, &inptr, &inbytes, &outptr, &outbytes);
|
|
|
|
if (result == (size_t) -1)
|
|
|
|
return SC_ERROR_INTERNAL;
|
|
|
|
*outptr = '\0';
|
|
|
|
|
|
|
|
r = sc_read_record (card, SC_ESTEID_PD_SURNAME, buff, sizeof(buff), SC_RECORD_BY_REC_NR);
|
|
|
|
SC_TEST_RET (card->ctx, r, "read name2 failed");
|
|
|
|
inptr = buff;
|
|
|
|
outptr = name2;
|
|
|
|
inbytes = r;
|
|
|
|
outbytes = 32;
|
|
|
|
result = iconv(iso_utf, &inptr, &inbytes, &outptr, &outbytes);
|
|
|
|
if (result == (size_t) -1)
|
|
|
|
return SC_ERROR_INTERNAL;
|
|
|
|
*outptr = '\0';
|
|
|
|
|
|
|
|
snprintf(label, sizeof(label), "%s %s", name1, name2);
|
|
|
|
set_string (&p15card->label, label);
|
|
|
|
#endif
|
2005-02-03 22:44:19 +00:00
|
|
|
p15card->flags = SC_PKCS15_CARD_FLAG_PRN_GENERATION
|
|
|
|
| SC_PKCS15_CARD_FLAG_EID_COMPLIANT
|
|
|
|
| SC_PKCS15_CARD_FLAG_READONLY;
|
|
|
|
|
2005-02-07 17:03:47 +00:00
|
|
|
/* EstEID uses 1024b RSA */
|
2005-02-03 22:44:19 +00:00
|
|
|
card->algorithm_count = 0;
|
|
|
|
flags = SC_ALGORITHM_RSA_PAD_PKCS1;
|
|
|
|
_sc_card_add_rsa_alg (card, 1024, flags, 0);
|
|
|
|
|
|
|
|
/* add certificates */
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
static const char *esteid_cert_names[2] = {
|
|
|
|
"Isikutuvastus",
|
|
|
|
"Allkirjastamine"};
|
|
|
|
static char const *esteid_cert_paths[2] = {
|
|
|
|
"3f00eeeeaace",
|
|
|
|
"3f00eeeeddce"};
|
|
|
|
static int esteid_cert_ids[2] = {1, 2};
|
|
|
|
|
|
|
|
struct sc_pkcs15_cert_info cert_info;
|
|
|
|
struct sc_pkcs15_object cert_obj;
|
|
|
|
|
|
|
|
memset(&cert_info, 0, sizeof(cert_info));
|
|
|
|
memset(&cert_obj, 0, sizeof(cert_obj));
|
|
|
|
|
|
|
|
cert_info.id.value[0] = esteid_cert_ids[i];
|
|
|
|
cert_info.id.len = 1;
|
|
|
|
sc_format_path(esteid_cert_paths[i], &cert_info.path);
|
2006-07-12 08:12:38 +00:00
|
|
|
strlcpy(cert_obj.label, esteid_cert_names[i], sizeof(cert_obj.label));
|
2005-02-03 22:44:19 +00:00
|
|
|
r = sc_pkcs15emu_add_x509_cert(p15card, &cert_obj, &cert_info);
|
|
|
|
if (r < 0)
|
|
|
|
return SC_ERROR_INTERNAL;
|
|
|
|
}
|
2005-02-02 21:18:54 +00:00
|
|
|
|
2005-02-03 22:44:19 +00:00
|
|
|
/* the file with key pin info (tries left) */
|
|
|
|
sc_format_path ("3f000016", &tmppath);
|
2009-01-15 23:20:48 +00:00
|
|
|
r = sc_select_file (card, &tmppath, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return SC_ERROR_INTERNAL;
|
2005-02-03 22:44:19 +00:00
|
|
|
|
|
|
|
/* add pins */
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
unsigned char tries_left;
|
|
|
|
static const char *esteid_pin_names[3] = {
|
2009-01-15 23:20:48 +00:00
|
|
|
"PIN1",
|
|
|
|
"PIN2",
|
2005-02-03 22:44:19 +00:00
|
|
|
"PUK" };
|
|
|
|
|
|
|
|
static const int esteid_pin_min[3] = {4, 5, 8};
|
|
|
|
static const int esteid_pin_ref[3] = {1, 2, 0};
|
2005-03-24 16:54:10 +00:00
|
|
|
static const int esteid_pin_authid[3] = {1, 2, 3};
|
2005-02-03 22:44:19 +00:00
|
|
|
static const int esteid_pin_flags[3] = {0, 0, SC_PKCS15_PIN_FLAG_UNBLOCKING_PIN};
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
/* read the number of tries left for the PIN */
|
|
|
|
r = sc_read_record (card, i + 1, buff, 128, SC_RECORD_BY_REC_NR);
|
|
|
|
if (r < 0)
|
|
|
|
return SC_ERROR_INTERNAL;
|
|
|
|
tries_left = buff[5];
|
|
|
|
|
|
|
|
pin_info.auth_id.len = 1;
|
2005-03-24 16:54:10 +00:00
|
|
|
pin_info.auth_id.value[0] = esteid_pin_authid[i];
|
2005-02-03 22:44:19 +00:00
|
|
|
pin_info.reference = esteid_pin_ref[i];
|
|
|
|
pin_info.flags = esteid_pin_flags[i];
|
|
|
|
pin_info.type = SC_PKCS15_PIN_TYPE_ASCII_NUMERIC;
|
|
|
|
pin_info.min_length = esteid_pin_min[i];
|
|
|
|
pin_info.stored_length = 12;
|
|
|
|
pin_info.max_length = 12;
|
|
|
|
pin_info.pad_char = '\0';
|
|
|
|
pin_info.tries_left = (int)tries_left;
|
|
|
|
|
2006-07-12 08:12:38 +00:00
|
|
|
strlcpy(pin_obj.label, esteid_pin_names[i], sizeof(pin_obj.label));
|
2005-02-03 22:44:19 +00:00
|
|
|
pin_obj.flags = esteid_pin_flags[i];
|
2005-03-24 16:57:00 +00:00
|
|
|
|
2006-01-23 17:37:10 +00:00
|
|
|
/* Link normal PINs with PUK */
|
2005-03-24 16:57:00 +00:00
|
|
|
if (i < 2) {
|
2005-03-24 16:54:10 +00:00
|
|
|
pin_obj.auth_id.len = 1;
|
|
|
|
pin_obj.auth_id.value[0] = 3;
|
2005-03-24 16:57:00 +00:00
|
|
|
}
|
|
|
|
|
2005-02-03 22:44:19 +00:00
|
|
|
r = sc_pkcs15emu_add_pin_obj(p15card, &pin_obj, &pin_info);
|
|
|
|
if (r < 0)
|
|
|
|
return SC_ERROR_INTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add private keys */
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
static int prkey_pin[2] = {1, 2};
|
|
|
|
static int prkey_usage[2] = {
|
|
|
|
SC_PKCS15_PRKEY_USAGE_ENCRYPT
|
|
|
|
| SC_PKCS15_PRKEY_USAGE_DECRYPT
|
|
|
|
| SC_PKCS15_PRKEY_USAGE_SIGN
|
|
|
|
| SC_PKCS15_PRKEY_USAGE_SIGNRECOVER
|
|
|
|
| SC_PKCS15_PRKEY_USAGE_WRAP
|
|
|
|
| SC_PKCS15_PRKEY_USAGE_UNWRAP,
|
|
|
|
SC_PKCS15_PRKEY_USAGE_NONREPUDIATION};
|
|
|
|
|
|
|
|
static const char *prkey_name[2] = {
|
|
|
|
"Isikutuvastus",
|
|
|
|
"Allkirjastamine"};
|
|
|
|
|
|
|
|
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] = prkey_pin[i];
|
|
|
|
prkey_info.usage = prkey_usage[i];
|
|
|
|
prkey_info.native = 1;
|
|
|
|
prkey_info.key_reference = i + 1;
|
|
|
|
prkey_info.modulus_length= 1024;
|
|
|
|
|
2006-07-12 08:12:38 +00:00
|
|
|
strlcpy(prkey_obj.label, prkey_name[i], sizeof(prkey_obj.label));
|
2005-02-03 22:44:19 +00:00
|
|
|
prkey_obj.auth_id.len = 1;
|
|
|
|
prkey_obj.auth_id.value[0] = prkey_pin[i];
|
|
|
|
prkey_obj.user_consent = (i == 1) ? 1 : 0;
|
2006-09-03 15:58:36 +00:00
|
|
|
prkey_obj.flags = SC_PKCS15_CO_FLAG_PRIVATE;
|
2005-02-03 22:44:19 +00:00
|
|
|
|
|
|
|
r = sc_pkcs15emu_add_rsa_prkey(p15card, &prkey_obj, &prkey_info);
|
|
|
|
if (r < 0)
|
|
|
|
return SC_ERROR_INTERNAL;
|
|
|
|
}
|
2009-01-15 23:20:48 +00:00
|
|
|
return SC_SUCCESS;
|
2004-07-14 21:13:54 +00:00
|
|
|
}
|
2004-10-08 21:29:55 +00:00
|
|
|
|
|
|
|
static int esteid_detect_card(sc_pkcs15_card_t *p15card)
|
|
|
|
{
|
2007-03-21 09:32:36 +00:00
|
|
|
if (p15card->card->type == SC_CARD_TYPE_MCRD_ESTEID)
|
2005-02-06 19:40:40 +00:00
|
|
|
return SC_SUCCESS;
|
|
|
|
return SC_ERROR_WRONG_CARD;
|
2004-10-08 21:29:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int sc_pkcs15emu_esteid_init_ex(sc_pkcs15_card_t *p15card,
|
|
|
|
sc_pkcs15emu_opt_t *opts)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (opts && opts->flags & SC_PKCS15EMU_FLAGS_NO_CHECK)
|
|
|
|
return sc_pkcs15emu_esteid_init(p15card);
|
|
|
|
else {
|
|
|
|
int r = esteid_detect_card(p15card);
|
|
|
|
if (r)
|
|
|
|
return SC_ERROR_WRONG_CARD;
|
|
|
|
return sc_pkcs15emu_esteid_init(p15card);
|
|
|
|
}
|
|
|
|
}
|