2001-11-06 18:34:19 +00:00
|
|
|
/*
|
2002-01-13 23:56:13 +00:00
|
|
|
* pkcs15-pin.c: PKCS #15 PIN functions
|
2001-11-01 15:43:20 +00:00
|
|
|
*
|
2006-12-19 21:31:17 +00:00
|
|
|
* Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi>
|
2001-11-06 18:34:19 +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,
|
2001-11-01 15:43:20 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2001-11-06 18:34:19 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-11-01 15:43:20 +00:00
|
|
|
*
|
2001-11-06 18:34:19 +00:00
|
|
|
* 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
|
2001-11-01 15:43:20 +00:00
|
|
|
*/
|
|
|
|
|
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
|
2010-03-04 08:14:36 +00:00
|
|
|
|
2001-11-01 15:43:20 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2010-03-04 08:14:36 +00:00
|
|
|
#include "internal.h"
|
|
|
|
#include "asn1.h"
|
|
|
|
#include "pkcs15.h"
|
2017-05-24 10:46:42 +00:00
|
|
|
#include "ui/notify.h"
|
2010-03-04 08:14:36 +00:00
|
|
|
|
2016-07-24 16:42:01 +00:00
|
|
|
int _sc_pkcs15_verify_pin(struct sc_pkcs15_card *, struct sc_pkcs15_object *,
|
|
|
|
const unsigned char *, size_t);
|
|
|
|
|
2002-01-17 23:47:03 +00:00
|
|
|
static const struct sc_asn1_entry c_asn1_com_ao_attr[] = {
|
2006-01-20 20:52:36 +00:00
|
|
|
{ "authId", SC_ASN1_PKCS15_ID, SC_ASN1_TAG_OCTET_STRING, 0, NULL, NULL },
|
2005-08-05 07:24:43 +00:00
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
2002-01-17 23:47:03 +00:00
|
|
|
};
|
2012-05-31 08:37:38 +00:00
|
|
|
|
|
|
|
/* PIN attributes */
|
2002-01-17 23:47:03 +00:00
|
|
|
static const struct sc_asn1_entry c_asn1_pin_attr[] = {
|
2006-01-20 20:52:36 +00:00
|
|
|
{ "pinFlags", SC_ASN1_BIT_FIELD, SC_ASN1_TAG_BIT_STRING, 0, NULL, NULL },
|
|
|
|
{ "pinType", SC_ASN1_ENUMERATED, SC_ASN1_TAG_ENUMERATED, 0, NULL, NULL },
|
|
|
|
{ "minLength", SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, 0, NULL, NULL },
|
|
|
|
{ "storedLength", SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, 0, NULL, NULL },
|
|
|
|
{ "maxLength", SC_ASN1_INTEGER, SC_ASN1_TAG_INTEGER, SC_ASN1_OPTIONAL, NULL, NULL },
|
2005-08-05 07:24:43 +00:00
|
|
|
{ "pinReference", SC_ASN1_INTEGER, SC_ASN1_CTX | 0, SC_ASN1_OPTIONAL, NULL, NULL },
|
2006-01-20 20:52:36 +00:00
|
|
|
{ "padChar", SC_ASN1_OCTET_STRING, SC_ASN1_TAG_OCTET_STRING, SC_ASN1_OPTIONAL, NULL, NULL },
|
|
|
|
{ "lastPinChange",SC_ASN1_GENERALIZEDTIME, SC_ASN1_TAG_GENERALIZEDTIME, SC_ASN1_OPTIONAL, NULL, NULL },
|
|
|
|
{ "path", SC_ASN1_PATH, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
|
2005-08-05 07:24:43 +00:00
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
2002-01-17 23:47:03 +00:00
|
|
|
};
|
|
|
|
static const struct sc_asn1_entry c_asn1_type_pin_attr[] = {
|
2006-01-20 20:52:36 +00:00
|
|
|
{ "pinAttributes", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
|
2005-08-05 07:24:43 +00:00
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
2002-01-17 23:47:03 +00:00
|
|
|
};
|
2012-05-31 08:37:38 +00:00
|
|
|
|
|
|
|
/* Auth Key attributes */
|
|
|
|
static const struct sc_asn1_entry c_asn1_authkey_attr[] = {
|
|
|
|
{ "derivedKey", SC_ASN1_BOOLEAN, SC_ASN1_TAG_BOOLEAN, SC_ASN1_OPTIONAL, NULL, NULL },
|
|
|
|
{ "authKeyId", SC_ASN1_PKCS15_ID, SC_ASN1_TAG_OCTET_STRING, 0, NULL, NULL },
|
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
|
|
|
};
|
|
|
|
static const struct sc_asn1_entry c_asn1_type_authkey_attr[] = {
|
|
|
|
{ "authKeyAttributes", SC_ASN1_STRUCT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
|
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
|
|
|
};
|
|
|
|
static const struct sc_asn1_entry c_asn1_auth_type[] = {
|
|
|
|
{ "authType", SC_ASN1_CHOICE, 0, 0, NULL, NULL },
|
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
|
|
|
};
|
|
|
|
static const struct sc_asn1_entry c_asn1_auth_type_choice[] = {
|
2016-07-24 16:42:01 +00:00
|
|
|
{ "pin", SC_ASN1_PKCS15_OBJECT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
|
|
|
|
{ "biometricTemplate", SC_ASN1_PKCS15_OBJECT, SC_ASN1_CTX | 0 | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
|
|
|
|
{ "authKey", SC_ASN1_PKCS15_OBJECT, SC_ASN1_CTX | 1 | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL, NULL },
|
2005-08-05 07:24:43 +00:00
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
2002-01-17 23:47:03 +00:00
|
|
|
};
|
|
|
|
|
2016-07-24 16:42:01 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
sc_pkcs15_decode_aodf_entry(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *obj,
|
|
|
|
const u8 ** buf, size_t *buflen)
|
2001-11-01 15:43:20 +00:00
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
sc_context_t *ctx = p15card->card->ctx;
|
2011-06-05 15:46:25 +00:00
|
|
|
struct sc_pkcs15_auth_info info;
|
2001-12-21 23:34:47 +00:00
|
|
|
int r;
|
2011-06-05 15:46:25 +00:00
|
|
|
size_t flags_len = sizeof(info.attrs.pin.flags);
|
2012-05-31 08:37:38 +00:00
|
|
|
size_t derived_len = sizeof(info.attrs.authkey.derived);
|
2005-01-19 16:17:32 +00:00
|
|
|
size_t padchar_len = 1;
|
2012-05-31 08:37:38 +00:00
|
|
|
struct sc_asn1_entry asn1_com_ao_attr[2];
|
|
|
|
struct sc_asn1_entry asn1_pin_attr[10], asn1_type_pin_attr[2];
|
|
|
|
struct sc_asn1_entry asn1_authkey_attr[3], asn1_type_authkey_attr[2];
|
|
|
|
struct sc_asn1_entry asn1_auth_type[2];
|
|
|
|
struct sc_asn1_entry asn1_auth_type_choice[4];
|
2002-03-03 00:32:28 +00:00
|
|
|
struct sc_asn1_pkcs15_object pin_obj = { obj, asn1_com_ao_attr, NULL, asn1_type_pin_attr };
|
2012-05-31 08:37:38 +00:00
|
|
|
struct sc_asn1_pkcs15_object authkey_obj = { obj, asn1_com_ao_attr, NULL, asn1_type_authkey_attr };
|
|
|
|
|
2011-01-17 13:11:21 +00:00
|
|
|
SC_FUNC_CALLED(ctx, SC_LOG_DEBUG_ASN1);
|
2012-05-31 08:37:38 +00:00
|
|
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_auth_type, asn1_auth_type);
|
|
|
|
sc_copy_asn1_entry(c_asn1_auth_type_choice, asn1_auth_type_choice);
|
|
|
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_com_ao_attr, asn1_com_ao_attr);
|
|
|
|
|
2005-01-19 16:17:32 +00:00
|
|
|
sc_copy_asn1_entry(c_asn1_type_pin_attr, asn1_type_pin_attr);
|
|
|
|
sc_copy_asn1_entry(c_asn1_pin_attr, asn1_pin_attr);
|
2001-12-21 23:34:47 +00:00
|
|
|
|
2012-05-31 08:37:38 +00:00
|
|
|
sc_copy_asn1_entry(c_asn1_type_authkey_attr, asn1_type_authkey_attr);
|
|
|
|
sc_copy_asn1_entry(c_asn1_authkey_attr, asn1_authkey_attr);
|
2002-01-17 23:47:03 +00:00
|
|
|
|
2012-05-31 08:37:38 +00:00
|
|
|
sc_format_asn1_entry(asn1_auth_type + 0, asn1_auth_type_choice, NULL, 0);
|
|
|
|
sc_format_asn1_entry(asn1_auth_type_choice + 0, &pin_obj, NULL, 0); /* 'pin' */
|
|
|
|
sc_format_asn1_entry(asn1_auth_type_choice + 2, &authkey_obj, NULL, 0); /* 'authKey' */
|
2002-01-17 23:47:03 +00:00
|
|
|
|
2012-05-31 08:37:38 +00:00
|
|
|
/* pinAttributes */
|
|
|
|
sc_format_asn1_entry(asn1_type_pin_attr + 0, asn1_pin_attr, NULL, 0);
|
2011-06-05 15:46:25 +00:00
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 0, &info.attrs.pin.flags, &flags_len, 0);
|
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 1, &info.attrs.pin.type, NULL, 0);
|
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 2, &info.attrs.pin.min_length, NULL, 0);
|
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 3, &info.attrs.pin.stored_length, NULL, 0);
|
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 4, &info.attrs.pin.max_length, NULL, 0);
|
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 5, &info.attrs.pin.reference, NULL, 0);
|
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 6, &info.attrs.pin.pad_char, &padchar_len, 0);
|
2012-05-31 08:37:38 +00:00
|
|
|
|
|
|
|
/* authKeyAttributes */
|
|
|
|
sc_format_asn1_entry(asn1_type_authkey_attr + 0, asn1_authkey_attr, NULL, 0);
|
|
|
|
sc_format_asn1_entry(asn1_authkey_attr + 0, &info.attrs.authkey.derived, &derived_len, 0);
|
|
|
|
sc_format_asn1_entry(asn1_authkey_attr + 1, &info.attrs.authkey.skey_id, NULL, 0);
|
|
|
|
|
2005-01-19 16:17:32 +00:00
|
|
|
/* We don't support lastPinChange yet. */
|
2002-01-24 16:02:54 +00:00
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 8, &info.path, NULL, 0);
|
2002-01-17 23:47:03 +00:00
|
|
|
|
2002-01-24 16:02:54 +00:00
|
|
|
sc_format_asn1_entry(asn1_com_ao_attr + 0, &info.auth_id, NULL, 0);
|
2002-01-17 23:47:03 +00:00
|
|
|
|
2005-01-19 16:17:32 +00:00
|
|
|
/* Fill in defaults */
|
|
|
|
memset(&info, 0, sizeof(info));
|
2003-10-31 12:31:54 +00:00
|
|
|
info.tries_left = -1;
|
2016-09-01 00:17:29 +00:00
|
|
|
info.logged_in = SC_PIN_STATE_UNKNOWN;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
2012-05-31 08:37:38 +00:00
|
|
|
r = sc_asn1_decode(ctx, asn1_auth_type, *buf, *buflen, buf, buflen);
|
2002-01-24 16:02:54 +00:00
|
|
|
if (r == SC_ERROR_ASN1_END_OF_CONTENTS)
|
|
|
|
return r;
|
2010-03-15 12:17:13 +00:00
|
|
|
SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, r, "ASN.1 decoding failed");
|
2011-06-05 15:46:25 +00:00
|
|
|
|
2012-05-31 08:37:38 +00:00
|
|
|
if (asn1_auth_type_choice[0].flags & SC_ASN1_PRESENT) {
|
2014-01-19 18:13:00 +00:00
|
|
|
sc_log(ctx, "AuthType: PIN");
|
2012-05-31 08:37:38 +00:00
|
|
|
obj->type = SC_PKCS15_TYPE_AUTH_PIN;
|
|
|
|
info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN;
|
|
|
|
info.auth_method = SC_AC_CHV;
|
|
|
|
|
|
|
|
if (info.attrs.pin.max_length == 0) {
|
|
|
|
if (p15card->card->max_pin_len != 0)
|
|
|
|
info.attrs.pin.max_length = p15card->card->max_pin_len;
|
|
|
|
else if (info.attrs.pin.stored_length != 0)
|
|
|
|
info.attrs.pin.max_length = info.attrs.pin.type != SC_PKCS15_PIN_TYPE_BCD ?
|
|
|
|
info.attrs.pin.stored_length : 2 * info.attrs.pin.stored_length;
|
|
|
|
else
|
|
|
|
info.attrs.pin.max_length = 8; /* shouldn't happen */
|
|
|
|
}
|
2009-12-18 13:33:03 +00:00
|
|
|
|
2012-05-31 08:37:38 +00:00
|
|
|
/* OpenSC 0.11.4 and older encoded "pinReference" as a negative
|
|
|
|
value. Fixed in 0.11.5 we need to add a hack, so old cards
|
|
|
|
continue to work.
|
|
|
|
The same invalid encoding has some models of the proprietary PKCS#15 cards.
|
|
|
|
*/
|
|
|
|
if (info.attrs.pin.reference < 0)
|
|
|
|
info.attrs.pin.reference += 256;
|
|
|
|
|
|
|
|
if (info.attrs.pin.flags & SC_PKCS15_PIN_FLAG_LOCAL) {
|
|
|
|
/* In OpenSC pkcs#15 framework 'path' is mandatory for the 'Local' PINs.
|
|
|
|
* If 'path' do not present in PinAttributes, derive it from the PKCS#15 context. */
|
|
|
|
if (!info.path.len) {
|
|
|
|
/* Give priority to AID defined in the application DDO */
|
|
|
|
if (p15card->app && p15card->app->ddo.aid.len)
|
|
|
|
info.path.aid = p15card->app->ddo.aid;
|
|
|
|
else if (p15card->file_app->path.len)
|
|
|
|
info.path = p15card->file_app->path;
|
|
|
|
}
|
2011-01-11 10:12:42 +00:00
|
|
|
}
|
2012-05-31 08:37:38 +00:00
|
|
|
sc_debug(ctx, SC_LOG_DEBUG_ASN1, "decoded PIN(ref:%X,path:%s)", info.attrs.pin.reference, sc_print_path(&info.path));
|
|
|
|
}
|
|
|
|
else if (asn1_auth_type_choice[1].flags & SC_ASN1_PRESENT) {
|
|
|
|
SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_NOT_SUPPORTED, "BIO authentication object not yet supported");
|
|
|
|
}
|
|
|
|
else if (asn1_auth_type_choice[2].flags & SC_ASN1_PRESENT) {
|
2014-01-19 18:13:00 +00:00
|
|
|
sc_log(ctx, "AuthType: AuthKey");
|
2012-05-31 08:37:38 +00:00
|
|
|
obj->type = SC_PKCS15_TYPE_AUTH_AUTHKEY;
|
|
|
|
info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_AUTH_KEY;
|
|
|
|
info.auth_method = SC_AC_AUT;
|
|
|
|
if (!(asn1_authkey_attr[0].flags & SC_ASN1_PRESENT))
|
|
|
|
info.attrs.authkey.derived = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_NOT_SUPPORTED, "unknown authentication type");
|
2011-01-11 10:12:42 +00:00
|
|
|
}
|
2002-01-24 16:02:54 +00:00
|
|
|
|
2012-05-31 08:37:38 +00:00
|
|
|
obj->data = malloc(sizeof(info));
|
|
|
|
if (obj->data == NULL)
|
|
|
|
SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_OUT_OF_MEMORY);
|
2011-01-17 13:11:21 +00:00
|
|
|
memcpy(obj->data, &info, sizeof(info));
|
2012-05-31 08:37:38 +00:00
|
|
|
|
2011-01-17 13:11:21 +00:00
|
|
|
SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_ASN1, SC_SUCCESS);
|
2001-11-01 15:43:20 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_pkcs15_encode_aodf_entry(sc_context_t *ctx,
|
2002-01-17 23:47:03 +00:00
|
|
|
const struct sc_pkcs15_object *obj,
|
|
|
|
u8 **buf, size_t *buflen)
|
|
|
|
{
|
|
|
|
struct sc_asn1_entry asn1_com_ao_attr[2], asn1_pin_attr[10], asn1_type_pin_attr[2];
|
2012-05-31 08:37:38 +00:00
|
|
|
struct sc_asn1_entry asn1_auth_type[2];
|
|
|
|
struct sc_asn1_entry asn1_auth_type_choice[4];
|
2011-06-05 15:46:25 +00:00
|
|
|
struct sc_pkcs15_auth_info *info = (struct sc_pkcs15_auth_info *) obj->data;
|
2002-03-03 00:32:28 +00:00
|
|
|
struct sc_asn1_pkcs15_object pin_obj = { (struct sc_pkcs15_object *) obj,
|
2005-01-19 16:17:32 +00:00
|
|
|
asn1_com_ao_attr, NULL, asn1_type_pin_attr };
|
2002-01-17 23:47:03 +00:00
|
|
|
int r;
|
2002-05-26 12:31:23 +00:00
|
|
|
size_t flags_len;
|
2005-01-19 16:17:32 +00:00
|
|
|
size_t padchar_len = 1;
|
2002-01-17 23:47:03 +00:00
|
|
|
|
2011-06-05 15:46:25 +00:00
|
|
|
if (info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
|
|
|
|
2012-05-31 08:37:38 +00:00
|
|
|
sc_copy_asn1_entry(c_asn1_auth_type, asn1_auth_type);
|
|
|
|
sc_copy_asn1_entry(c_asn1_auth_type_choice, asn1_auth_type_choice);
|
2016-07-24 16:42:01 +00:00
|
|
|
sc_copy_asn1_entry(c_asn1_type_pin_attr, asn1_type_pin_attr);
|
|
|
|
sc_copy_asn1_entry(c_asn1_pin_attr, asn1_pin_attr);
|
|
|
|
sc_copy_asn1_entry(c_asn1_com_ao_attr, asn1_com_ao_attr);
|
2002-01-17 23:47:03 +00:00
|
|
|
|
2012-05-31 08:37:38 +00:00
|
|
|
sc_format_asn1_entry(asn1_auth_type + 0, asn1_auth_type_choice, NULL, 1);
|
|
|
|
sc_format_asn1_entry(asn1_auth_type_choice + 0, &pin_obj, NULL, 1);
|
2002-01-17 23:47:03 +00:00
|
|
|
|
|
|
|
sc_format_asn1_entry(asn1_type_pin_attr + 0, asn1_pin_attr, NULL, 1);
|
|
|
|
|
2011-06-05 15:46:25 +00:00
|
|
|
flags_len = sizeof(info->attrs.pin.flags);
|
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 0, &info->attrs.pin.flags, &flags_len, 1);
|
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 1, &info->attrs.pin.type, NULL, 1);
|
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 2, &info->attrs.pin.min_length, NULL, 1);
|
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 3, &info->attrs.pin.stored_length, NULL, 1);
|
|
|
|
if (info->attrs.pin.max_length > 0)
|
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 4, &info->attrs.pin.max_length, NULL, 1);
|
|
|
|
if (info->attrs.pin.reference >= 0)
|
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 5, &info->attrs.pin.reference, NULL, 1);
|
2002-01-17 23:47:03 +00:00
|
|
|
/* FIXME: check if pad_char present */
|
2011-06-05 15:46:25 +00:00
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 6, &info->attrs.pin.pad_char, &padchar_len, 1);
|
|
|
|
sc_format_asn1_entry(asn1_pin_attr + 8, &info->path, NULL, info->path.len ? 1 : 0);
|
2002-01-17 23:47:03 +00:00
|
|
|
|
2011-06-05 15:46:25 +00:00
|
|
|
sc_format_asn1_entry(asn1_com_ao_attr + 0, &info->auth_id, NULL, 1);
|
2002-01-17 23:47:03 +00:00
|
|
|
|
2012-05-31 08:37:38 +00:00
|
|
|
r = sc_asn1_encode(ctx, asn1_auth_type, buf, buflen);
|
2002-01-17 23:47:03 +00:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-05-31 08:37:38 +00:00
|
|
|
|
2016-07-24 16:42:01 +00:00
|
|
|
static int
|
|
|
|
_validate_pin(struct sc_pkcs15_card *p15card, struct sc_pkcs15_auth_info *auth_info, size_t pinlen)
|
2005-03-09 12:46:31 +00:00
|
|
|
{
|
|
|
|
size_t max_length;
|
2017-02-22 08:32:18 +00:00
|
|
|
if (p15card == NULL) {
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
}
|
2010-02-20 22:04:07 +00:00
|
|
|
|
2011-06-05 15:46:25 +00:00
|
|
|
/* Ignore validation of the non-PIN authentication objects */
|
|
|
|
if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
|
|
|
|
return SC_SUCCESS;
|
|
|
|
|
2012-05-31 08:37:38 +00:00
|
|
|
/* prevent buffer overflow from hostile card */
|
2011-06-05 15:46:25 +00:00
|
|
|
if (auth_info->attrs.pin.stored_length > SC_MAX_PIN_SIZE)
|
2005-03-09 12:46:31 +00:00
|
|
|
return SC_ERROR_BUFFER_TOO_SMALL;
|
2005-04-07 19:45:39 +00:00
|
|
|
|
2005-03-09 12:46:31 +00:00
|
|
|
/* if we use pinpad, no more checks are needed */
|
2017-03-23 15:45:31 +00:00
|
|
|
if ((p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD
|
|
|
|
|| p15card->card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH)
|
|
|
|
&& !pinlen)
|
2005-03-09 12:46:31 +00:00
|
|
|
return SC_SUCCESS;
|
2012-05-31 08:37:38 +00:00
|
|
|
|
2005-03-09 12:46:31 +00:00
|
|
|
/* If pin is given, make sure it is within limits */
|
2011-06-05 15:46:25 +00:00
|
|
|
max_length = auth_info->attrs.pin.max_length != 0 ? auth_info->attrs.pin.max_length : SC_MAX_PIN_SIZE;
|
|
|
|
if (pinlen > max_length || pinlen < auth_info->attrs.pin.min_length)
|
2005-03-09 12:46:31 +00:00
|
|
|
return SC_ERROR_INVALID_PIN_LENGTH;
|
2005-04-07 19:45:39 +00:00
|
|
|
|
2005-03-09 12:46:31 +00:00
|
|
|
return SC_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2002-12-23 18:47:27 +00:00
|
|
|
/*
|
|
|
|
* Verify a PIN.
|
|
|
|
*
|
|
|
|
* If the code given to us has zero length, this means we
|
|
|
|
* should ask the card reader to obtain the PIN from the
|
|
|
|
* reader's PIN pad
|
|
|
|
*/
|
2016-07-24 16:42:01 +00:00
|
|
|
int
|
|
|
|
sc_pkcs15_verify_pin(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *pin_obj,
|
|
|
|
const unsigned char *pincode, size_t pinlen)
|
|
|
|
{
|
|
|
|
struct sc_context *ctx = p15card->card->ctx;
|
2017-04-07 07:22:11 +00:00
|
|
|
struct sc_pkcs15_auth_info *auth_info = (struct sc_pkcs15_auth_info *)pin_obj->data;
|
2016-07-24 16:42:01 +00:00
|
|
|
int r;
|
|
|
|
|
|
|
|
LOG_FUNC_CALLED(ctx);
|
|
|
|
|
2017-04-07 07:22:11 +00:00
|
|
|
/*
|
|
|
|
* if pin cache is disabled, we can get here with no PIN data.
|
|
|
|
* in this case, to avoid error or unnecessary pin prompting on pinpad,
|
|
|
|
* check if the PIN has been already verified and the access condition
|
|
|
|
* is still open on card.
|
|
|
|
*/
|
|
|
|
if (pinlen == 0) {
|
|
|
|
r = sc_pkcs15_get_pin_info(p15card, pin_obj);
|
|
|
|
|
|
|
|
if (r == SC_SUCCESS && auth_info->logged_in == SC_PIN_STATE_LOGGED_IN)
|
|
|
|
LOG_FUNC_RETURN(ctx, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
r = _validate_pin(p15card, auth_info, pinlen);
|
|
|
|
|
2017-02-01 10:16:46 +00:00
|
|
|
if (r)
|
|
|
|
LOG_FUNC_RETURN(ctx, r);
|
|
|
|
|
2016-07-24 16:42:01 +00:00
|
|
|
r = _sc_pkcs15_verify_pin(p15card, pin_obj, pincode, pinlen);
|
2017-04-07 07:22:11 +00:00
|
|
|
|
2016-07-24 16:42:01 +00:00
|
|
|
if (r == SC_SUCCESS)
|
|
|
|
sc_pkcs15_pincache_add(p15card, pin_obj, pincode, pinlen);
|
|
|
|
|
|
|
|
LOG_FUNC_RETURN(ctx, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
_sc_pkcs15_verify_pin(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *pin_obj,
|
|
|
|
const unsigned char *pincode, size_t pinlen)
|
2017-03-01 16:53:44 +00:00
|
|
|
{
|
|
|
|
return sc_pkcs15_verify_pin_with_session_pin(p15card, pin_obj, pincode,
|
|
|
|
pinlen, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify a PIN and generate a session PIN
|
|
|
|
*
|
|
|
|
* If the code given to us has zero length, this means we
|
|
|
|
* should ask the card reader to obtain the PIN from the
|
|
|
|
* reader's PIN pad
|
|
|
|
*/
|
|
|
|
int sc_pkcs15_verify_pin_with_session_pin(struct sc_pkcs15_card *p15card,
|
|
|
|
struct sc_pkcs15_object *pin_obj,
|
|
|
|
const unsigned char *pincode, size_t pinlen,
|
|
|
|
const unsigned char *sessionpin, size_t *sessionpinlen)
|
2001-11-01 15:43:20 +00:00
|
|
|
{
|
2010-02-20 22:04:07 +00:00
|
|
|
struct sc_context *ctx = p15card->card->ctx;
|
2011-06-05 15:46:25 +00:00
|
|
|
struct sc_pkcs15_auth_info *auth_info = (struct sc_pkcs15_auth_info *)pin_obj->data;
|
2001-11-01 15:43:20 +00:00
|
|
|
int r;
|
2005-03-08 20:59:35 +00:00
|
|
|
sc_card_t *card;
|
2005-03-09 12:46:31 +00:00
|
|
|
struct sc_pin_cmd_data data;
|
2002-12-23 18:47:27 +00:00
|
|
|
|
2014-01-19 17:42:25 +00:00
|
|
|
LOG_FUNC_CALLED(ctx);
|
2017-03-14 19:02:30 +00:00
|
|
|
sc_log(ctx,
|
|
|
|
"PIN(type:%X; method:%X; value(%p:%"SC_FORMAT_LEN_SIZE_T"u)",
|
|
|
|
auth_info->auth_type, auth_info->auth_method,
|
|
|
|
pincode, pinlen);
|
2017-05-24 10:46:42 +00:00
|
|
|
card = p15card->card;
|
2011-06-05 15:46:25 +00:00
|
|
|
|
2017-05-24 10:46:42 +00:00
|
|
|
if (pinlen > SC_MAX_PIN_SIZE) {
|
|
|
|
sc_notify_id(card->ctx, &card->reader->atr, p15card,
|
|
|
|
NOTIFY_PIN_BAD);
|
2014-01-19 17:42:25 +00:00
|
|
|
LOG_TEST_RET(ctx, SC_ERROR_INVALID_PIN_LENGTH, "Invalid PIN size");
|
2017-05-24 10:46:42 +00:00
|
|
|
}
|
2008-05-09 22:22:41 +00:00
|
|
|
|
2002-12-23 18:47:27 +00:00
|
|
|
/* Initialize arguments */
|
2005-03-09 12:46:31 +00:00
|
|
|
memset(&data, 0, sizeof(data));
|
2011-06-05 15:46:25 +00:00
|
|
|
data.pin_type = auth_info->auth_method;
|
2002-12-23 18:47:27 +00:00
|
|
|
|
2012-05-31 08:37:38 +00:00
|
|
|
if (auth_info->auth_type == SC_PKCS15_PIN_AUTH_TYPE_PIN) {
|
|
|
|
data.pin_reference = auth_info->attrs.pin.reference;
|
|
|
|
data.pin1.min_length = auth_info->attrs.pin.min_length;
|
|
|
|
data.pin1.max_length = auth_info->attrs.pin.max_length;
|
|
|
|
data.pin1.pad_length = auth_info->attrs.pin.stored_length;
|
|
|
|
data.pin1.pad_char = auth_info->attrs.pin.pad_char;
|
|
|
|
data.pin1.data = pincode;
|
|
|
|
data.pin1.len = pinlen;
|
|
|
|
|
|
|
|
if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_NEEDS_PADDING)
|
|
|
|
data.flags |= SC_PIN_CMD_NEED_PADDING;
|
|
|
|
|
|
|
|
switch (auth_info->attrs.pin.type) {
|
|
|
|
case SC_PKCS15_PIN_TYPE_BCD:
|
|
|
|
data.pin1.encoding = SC_PIN_ENCODING_BCD;
|
|
|
|
break;
|
|
|
|
case SC_PKCS15_PIN_TYPE_ASCII_NUMERIC:
|
|
|
|
data.pin1.encoding = SC_PIN_ENCODING_ASCII;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* assume/hope the card driver knows how to encode the pin */
|
|
|
|
data.pin1.encoding = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (auth_info->auth_type == SC_PKCS15_PIN_AUTH_TYPE_AUTH_KEY) {
|
|
|
|
struct sc_pkcs15_object *skey_obj = NULL;
|
|
|
|
struct sc_pkcs15_id *skey_id = &auth_info->attrs.authkey.skey_id;
|
|
|
|
struct sc_pkcs15_skey_info *skey_info = NULL;
|
|
|
|
|
|
|
|
r = sc_pkcs15_find_skey_by_id(p15card, skey_id, &skey_obj);
|
|
|
|
if (r) {
|
2014-01-19 18:13:00 +00:00
|
|
|
sc_log(ctx, "cannot find secret key with id:%s", sc_pkcs15_print_id(skey_id));
|
|
|
|
LOG_FUNC_RETURN(ctx, r);
|
2012-05-31 08:37:38 +00:00
|
|
|
}
|
|
|
|
skey_info = (struct sc_pkcs15_skey_info *)skey_obj->data;
|
2002-12-23 18:47:27 +00:00
|
|
|
|
2014-01-19 18:13:00 +00:00
|
|
|
sc_log(ctx, "found secret key '%s'", skey_obj->label);
|
2012-05-31 08:37:38 +00:00
|
|
|
data.pin_reference = skey_info->key_reference;
|
2005-01-08 10:20:21 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 10:46:42 +00:00
|
|
|
if ((p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD
|
|
|
|
|| p15card->card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH)) {
|
|
|
|
if (!pincode && !pinlen)
|
|
|
|
data.flags |= SC_PIN_CMD_USE_PINPAD;
|
2016-08-27 13:29:56 +00:00
|
|
|
|
2011-06-05 15:46:25 +00:00
|
|
|
if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_SO_PIN)
|
2005-03-09 12:46:31 +00:00
|
|
|
data.pin1.prompt = "Please enter SO PIN";
|
2002-12-23 18:47:27 +00:00
|
|
|
else
|
2005-03-09 12:46:31 +00:00
|
|
|
data.pin1.prompt = "Please enter PIN";
|
2002-12-23 18:47:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 16:53:44 +00:00
|
|
|
if (card->caps & SC_CARD_CAP_SESSION_PIN && sessionpin && sessionpinlen) {
|
|
|
|
/* session pin is requested and supported with standard verification*/
|
|
|
|
data.cmd = SC_PIN_CMD_GET_SESSION_PIN;
|
|
|
|
memcpy(&data.pin2, &data.pin1, sizeof (data.pin1));
|
|
|
|
data.pin2.data = sessionpin;
|
|
|
|
data.pin2.len = *sessionpinlen;
|
|
|
|
} else {
|
|
|
|
/* perform a standard verify */
|
|
|
|
data.cmd = SC_PIN_CMD_VERIFY;
|
|
|
|
if (sessionpinlen)
|
|
|
|
*sessionpinlen = 0;
|
|
|
|
}
|
|
|
|
|
2012-05-31 08:37:38 +00:00
|
|
|
r = sc_lock(card);
|
2014-01-19 18:13:00 +00:00
|
|
|
LOG_TEST_RET(ctx, r, "sc_lock() failed");
|
|
|
|
|
2012-05-31 08:37:38 +00:00
|
|
|
/* the path in the pin object is optional */
|
2015-09-23 13:36:56 +00:00
|
|
|
if ((auth_info->path.len > 0) || ((auth_info->path.aid.len > 0))) {
|
2012-05-31 08:37:38 +00:00
|
|
|
r = sc_select_file(card, &auth_info->path, NULL);
|
|
|
|
if (r)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-06-05 15:46:25 +00:00
|
|
|
r = sc_pin_cmd(card, &data, &auth_info->tries_left);
|
2014-01-19 18:13:00 +00:00
|
|
|
sc_log(ctx, "PIN cmd result %i", r);
|
2017-03-01 16:53:44 +00:00
|
|
|
if (r == SC_SUCCESS) {
|
|
|
|
sc_pkcs15_pincache_add(p15card, pin_obj, pincode, pinlen);
|
2017-10-30 15:32:57 +00:00
|
|
|
if (data.cmd == SC_PIN_CMD_GET_SESSION_PIN && sessionpinlen) {
|
2017-03-01 16:53:44 +00:00
|
|
|
*sessionpinlen = data.pin2.len;
|
|
|
|
}
|
|
|
|
} else {
|
2017-05-24 10:46:42 +00:00
|
|
|
sc_notify_id(card->ctx, &card->reader->atr, p15card,
|
|
|
|
NOTIFY_PIN_BAD);
|
2017-10-30 15:32:57 +00:00
|
|
|
if (data.cmd == SC_PIN_CMD_GET_SESSION_PIN && sessionpinlen) {
|
2017-03-01 16:53:44 +00:00
|
|
|
*sessionpinlen = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-05-24 10:46:42 +00:00
|
|
|
|
|
|
|
if (auth_info->auth_type == SC_PKCS15_PIN_AUTH_TYPE_PIN
|
|
|
|
&& auth_info->auth_method != SC_AC_SESSION) {
|
|
|
|
sc_notify_id(card->ctx, &card->reader->atr, p15card,
|
|
|
|
r == SC_SUCCESS ? NOTIFY_PIN_GOOD : NOTIFY_PIN_BAD);
|
|
|
|
}
|
|
|
|
|
2005-08-13 13:04:02 +00:00
|
|
|
out:
|
2001-12-25 20:45:48 +00:00
|
|
|
sc_unlock(card);
|
2014-01-19 18:13:00 +00:00
|
|
|
LOG_FUNC_RETURN(ctx, r);
|
2001-11-01 15:43:20 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 16:42:01 +00:00
|
|
|
|
|
|
|
|
2002-12-23 18:47:27 +00:00
|
|
|
/*
|
|
|
|
* Change a PIN.
|
|
|
|
*/
|
2001-11-01 15:43:20 +00:00
|
|
|
int sc_pkcs15_change_pin(struct sc_pkcs15_card *p15card,
|
2010-03-10 09:23:01 +00:00
|
|
|
struct sc_pkcs15_object *pin_obj,
|
2002-03-03 00:32:28 +00:00
|
|
|
const u8 *oldpin, size_t oldpinlen,
|
|
|
|
const u8 *newpin, size_t newpinlen)
|
2001-11-01 15:43:20 +00:00
|
|
|
{
|
2014-01-19 18:13:00 +00:00
|
|
|
struct sc_context *ctx = p15card->card->ctx;
|
2004-04-23 17:29:24 +00:00
|
|
|
struct sc_pin_cmd_data data;
|
2011-06-05 15:46:25 +00:00
|
|
|
struct sc_pkcs15_auth_info *auth_info = (struct sc_pkcs15_auth_info *)pin_obj->data;
|
2016-10-10 20:21:46 +00:00
|
|
|
struct sc_card *card;
|
2014-01-19 18:13:00 +00:00
|
|
|
int r;
|
2012-05-31 08:37:38 +00:00
|
|
|
|
2014-01-19 18:13:00 +00:00
|
|
|
LOG_FUNC_CALLED(ctx);
|
2011-06-05 15:46:25 +00:00
|
|
|
if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
|
2014-01-19 18:13:00 +00:00
|
|
|
LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
|
2011-06-05 15:46:25 +00:00
|
|
|
|
2005-03-09 12:46:31 +00:00
|
|
|
/* make sure the pins are in valid range */
|
2014-01-19 18:13:00 +00:00
|
|
|
r = _validate_pin(p15card, auth_info, oldpinlen);
|
|
|
|
LOG_TEST_RET(ctx, r, "Old PIN value do not conform PIN policy");
|
|
|
|
|
|
|
|
r = _validate_pin(p15card, auth_info, newpinlen);
|
|
|
|
LOG_TEST_RET(ctx, r, "New PIN value do not conform PIN policy");
|
2003-01-06 19:36:23 +00:00
|
|
|
|
2001-11-01 15:43:20 +00:00
|
|
|
card = p15card->card;
|
2001-12-25 20:45:48 +00:00
|
|
|
r = sc_lock(card);
|
2014-01-19 18:13:00 +00:00
|
|
|
LOG_TEST_RET(ctx, r, "sc_lock() failed");
|
2004-05-20 09:09:30 +00:00
|
|
|
/* the path in the pin object is optional */
|
2015-09-25 09:56:32 +00:00
|
|
|
if ((auth_info->path.len > 0) || ((auth_info->path.aid.len > 0))) {
|
2011-06-05 15:46:25 +00:00
|
|
|
r = sc_select_file(card, &auth_info->path, NULL);
|
2005-08-13 13:04:02 +00:00
|
|
|
if (r)
|
|
|
|
goto out;
|
2001-12-25 20:45:48 +00:00
|
|
|
}
|
2004-04-23 17:29:24 +00:00
|
|
|
|
|
|
|
/* set pin_cmd data */
|
|
|
|
memset(&data, 0, sizeof(data));
|
|
|
|
data.cmd = SC_PIN_CMD_CHANGE;
|
|
|
|
data.pin_type = SC_AC_CHV;
|
2011-06-05 15:46:25 +00:00
|
|
|
data.pin_reference = auth_info->attrs.pin.reference;
|
2004-04-23 17:29:24 +00:00
|
|
|
data.pin1.data = oldpin;
|
|
|
|
data.pin1.len = oldpinlen;
|
2011-06-05 15:46:25 +00:00
|
|
|
data.pin1.pad_char = auth_info->attrs.pin.pad_char;
|
|
|
|
data.pin1.min_length = auth_info->attrs.pin.min_length;
|
|
|
|
data.pin1.max_length = auth_info->attrs.pin.max_length;
|
|
|
|
data.pin1.pad_length = auth_info->attrs.pin.stored_length;
|
2004-04-23 17:29:24 +00:00
|
|
|
data.pin2.data = newpin;
|
|
|
|
data.pin2.len = newpinlen;
|
2011-06-05 15:46:25 +00:00
|
|
|
data.pin2.pad_char = auth_info->attrs.pin.pad_char;
|
|
|
|
data.pin2.min_length = auth_info->attrs.pin.min_length;
|
|
|
|
data.pin2.max_length = auth_info->attrs.pin.max_length;
|
|
|
|
data.pin2.pad_length = auth_info->attrs.pin.stored_length;
|
2004-04-23 17:29:24 +00:00
|
|
|
|
2011-06-05 15:46:25 +00:00
|
|
|
if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_NEEDS_PADDING)
|
2004-04-23 17:29:24 +00:00
|
|
|
data.flags |= SC_PIN_CMD_NEED_PADDING;
|
|
|
|
|
2011-06-05 15:46:25 +00:00
|
|
|
switch (auth_info->attrs.pin.type) {
|
2005-01-08 10:20:21 +00:00
|
|
|
case SC_PKCS15_PIN_TYPE_BCD:
|
|
|
|
data.pin1.encoding = SC_PIN_ENCODING_BCD;
|
|
|
|
data.pin2.encoding = SC_PIN_ENCODING_BCD;
|
|
|
|
break;
|
|
|
|
case SC_PKCS15_PIN_TYPE_ASCII_NUMERIC:
|
|
|
|
data.pin1.encoding = SC_PIN_ENCODING_ASCII;
|
|
|
|
data.pin2.encoding = SC_PIN_ENCODING_ASCII;
|
|
|
|
break;
|
|
|
|
}
|
2012-05-31 08:37:38 +00:00
|
|
|
|
|
|
|
if((!oldpin || !newpin)
|
2017-03-23 15:45:31 +00:00
|
|
|
&& (p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD
|
|
|
|
|| p15card->card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH)) {
|
2005-03-09 12:46:31 +00:00
|
|
|
data.flags |= SC_PIN_CMD_USE_PINPAD;
|
2011-06-05 15:46:25 +00:00
|
|
|
if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_SO_PIN) {
|
2005-03-09 12:46:31 +00:00
|
|
|
data.pin1.prompt = "Please enter SO PIN";
|
|
|
|
data.pin2.prompt = "Please enter new SO PIN";
|
2014-01-19 18:13:00 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-03-09 12:46:31 +00:00
|
|
|
data.pin1.prompt = "Please enter PIN";
|
|
|
|
data.pin2.prompt = "Please enter new PIN";
|
|
|
|
}
|
|
|
|
}
|
2005-01-08 10:20:21 +00:00
|
|
|
|
2011-06-05 15:46:25 +00:00
|
|
|
r = sc_pin_cmd(card, &data, &auth_info->tries_left);
|
2009-10-22 08:59:59 +00:00
|
|
|
if (r == SC_SUCCESS)
|
2010-03-10 09:23:01 +00:00
|
|
|
sc_pkcs15_pincache_add(p15card, pin_obj, newpin, newpinlen);
|
2004-04-23 17:29:24 +00:00
|
|
|
|
2005-08-13 13:04:02 +00:00
|
|
|
out:
|
2001-12-25 20:45:48 +00:00
|
|
|
sc_unlock(card);
|
2001-12-02 19:21:46 +00:00
|
|
|
return r;
|
2001-11-01 15:43:20 +00:00
|
|
|
}
|
2003-04-14 14:51:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Unblock a PIN.
|
|
|
|
*/
|
|
|
|
int sc_pkcs15_unblock_pin(struct sc_pkcs15_card *p15card,
|
2010-03-10 09:23:01 +00:00
|
|
|
struct sc_pkcs15_object *pin_obj,
|
2003-04-14 14:51:42 +00:00
|
|
|
const u8 *puk, size_t puklen,
|
|
|
|
const u8 *newpin, size_t newpinlen)
|
|
|
|
{
|
2014-01-19 18:13:00 +00:00
|
|
|
struct sc_context *ctx = p15card->card->ctx;
|
2004-04-23 17:29:24 +00:00
|
|
|
struct sc_pin_cmd_data data;
|
2010-03-10 09:23:01 +00:00
|
|
|
struct sc_pkcs15_object *puk_obj;
|
2011-06-05 15:46:25 +00:00
|
|
|
struct sc_pkcs15_auth_info *puk_info = NULL;
|
|
|
|
struct sc_pkcs15_auth_info *auth_info = (struct sc_pkcs15_auth_info *)pin_obj->data;
|
2014-01-19 18:13:00 +00:00
|
|
|
struct sc_card *card = p15card->card;
|
|
|
|
int r;
|
2011-06-05 15:46:25 +00:00
|
|
|
|
2014-01-19 18:13:00 +00:00
|
|
|
LOG_FUNC_CALLED(ctx);
|
2011-06-05 15:46:25 +00:00
|
|
|
if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN)
|
2014-01-19 18:13:00 +00:00
|
|
|
LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_SUPPORTED);
|
2003-04-14 14:51:42 +00:00
|
|
|
|
2005-03-09 12:46:31 +00:00
|
|
|
/* make sure the pins are in valid range */
|
2014-01-19 18:13:00 +00:00
|
|
|
r = _validate_pin(p15card, auth_info, newpinlen);
|
|
|
|
LOG_TEST_RET(ctx, r, "New PIN value do not conform PIN policy");
|
2003-04-14 14:51:42 +00:00
|
|
|
|
2005-01-18 21:42:00 +00:00
|
|
|
/* get pin_info object of the puk (this is a little bit complicated
|
|
|
|
* as we don't have the id of the puk (at least now))
|
2005-03-09 12:46:31 +00:00
|
|
|
* note: for compatibility reasons we give no error if no puk object
|
2005-01-18 21:42:00 +00:00
|
|
|
* is found */
|
2010-03-10 09:23:01 +00:00
|
|
|
/* first step: try to get the pkcs15 object of the puk */
|
|
|
|
r = sc_pkcs15_find_pin_by_auth_id(p15card, &pin_obj->auth_id, &puk_obj);
|
|
|
|
if (r >= 0 && puk_obj) {
|
|
|
|
/* second step: get the pkcs15 info object of the puk */
|
2011-06-05 15:46:25 +00:00
|
|
|
puk_info = (struct sc_pkcs15_auth_info *)puk_obj->data;
|
2005-01-18 21:42:00 +00:00
|
|
|
}
|
2014-01-19 18:13:00 +00:00
|
|
|
|
2005-01-18 21:42:00 +00:00
|
|
|
if (!puk_info) {
|
2014-01-19 18:13:00 +00:00
|
|
|
sc_log(ctx, "Unable to get puk object, using pin object instead!");
|
2011-06-05 15:46:25 +00:00
|
|
|
puk_info = auth_info;
|
2005-01-18 21:42:00 +00:00
|
|
|
}
|
2015-04-18 20:13:12 +00:00
|
|
|
/* make sure the puk is in valid range */
|
|
|
|
r = _validate_pin(p15card, puk_info, puklen);
|
|
|
|
LOG_TEST_RET(ctx, r, "PIN do not conforms PIN policy");
|
2005-01-18 21:42:00 +00:00
|
|
|
|
2003-04-14 14:51:42 +00:00
|
|
|
r = sc_lock(card);
|
2014-01-19 18:13:00 +00:00
|
|
|
LOG_TEST_RET(ctx, r, "sc_lock() failed");
|
|
|
|
|
2004-05-20 09:09:30 +00:00
|
|
|
/* the path in the pin object is optional */
|
2015-09-25 09:56:32 +00:00
|
|
|
if ((auth_info->path.len > 0) || ((auth_info->path.aid.len > 0))) {
|
2011-06-05 15:46:25 +00:00
|
|
|
r = sc_select_file(card, &auth_info->path, NULL);
|
2005-08-13 13:04:02 +00:00
|
|
|
if (r)
|
|
|
|
goto out;
|
2003-04-14 14:51:42 +00:00
|
|
|
}
|
2003-04-25 10:03:09 +00:00
|
|
|
|
2004-04-23 17:29:24 +00:00
|
|
|
/* set pin_cmd data */
|
|
|
|
memset(&data, 0, sizeof(data));
|
|
|
|
data.cmd = SC_PIN_CMD_UNBLOCK;
|
|
|
|
data.pin_type = SC_AC_CHV;
|
2011-06-05 15:46:25 +00:00
|
|
|
data.pin_reference = auth_info->attrs.pin.reference;
|
2004-04-23 17:29:24 +00:00
|
|
|
data.pin1.data = puk;
|
|
|
|
data.pin1.len = puklen;
|
2011-06-05 15:46:25 +00:00
|
|
|
data.pin1.pad_char = auth_info->attrs.pin.pad_char;
|
|
|
|
data.pin1.min_length = auth_info->attrs.pin.min_length;
|
|
|
|
data.pin1.max_length = auth_info->attrs.pin.max_length;
|
|
|
|
data.pin1.pad_length = auth_info->attrs.pin.stored_length;
|
2004-04-23 17:29:24 +00:00
|
|
|
data.pin2.data = newpin;
|
|
|
|
data.pin2.len = newpinlen;
|
2011-06-05 15:46:25 +00:00
|
|
|
data.pin2.pad_char = puk_info->attrs.pin.pad_char;
|
|
|
|
data.pin2.min_length = puk_info->attrs.pin.min_length;
|
|
|
|
data.pin2.max_length = puk_info->attrs.pin.max_length;
|
|
|
|
data.pin2.pad_length = puk_info->attrs.pin.stored_length;
|
2004-04-23 17:29:24 +00:00
|
|
|
|
2011-06-05 15:46:25 +00:00
|
|
|
if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_NEEDS_PADDING)
|
2004-04-23 17:29:24 +00:00
|
|
|
data.flags |= SC_PIN_CMD_NEED_PADDING;
|
|
|
|
|
2011-06-05 15:46:25 +00:00
|
|
|
switch (auth_info->attrs.pin.type) {
|
2005-01-08 10:20:21 +00:00
|
|
|
case SC_PKCS15_PIN_TYPE_BCD:
|
|
|
|
data.pin1.encoding = SC_PIN_ENCODING_BCD;
|
|
|
|
break;
|
|
|
|
case SC_PKCS15_PIN_TYPE_ASCII_NUMERIC:
|
|
|
|
data.pin1.encoding = SC_PIN_ENCODING_ASCII;
|
2005-01-18 21:42:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-06-05 15:46:25 +00:00
|
|
|
switch (puk_info->attrs.pin.type) {
|
2005-01-18 21:42:00 +00:00
|
|
|
case SC_PKCS15_PIN_TYPE_BCD:
|
|
|
|
data.pin2.encoding = SC_PIN_ENCODING_BCD;
|
|
|
|
break;
|
|
|
|
case SC_PKCS15_PIN_TYPE_ASCII_NUMERIC:
|
2005-01-08 10:20:21 +00:00
|
|
|
data.pin2.encoding = SC_PIN_ENCODING_ASCII;
|
|
|
|
break;
|
|
|
|
}
|
2012-05-31 08:37:38 +00:00
|
|
|
|
2017-03-23 15:45:31 +00:00
|
|
|
if((p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD
|
|
|
|
|| p15card->card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH)) {
|
2005-03-09 12:46:31 +00:00
|
|
|
data.flags |= SC_PIN_CMD_USE_PINPAD;
|
2011-06-05 15:46:25 +00:00
|
|
|
if (auth_info->attrs.pin.flags & SC_PKCS15_PIN_FLAG_SO_PIN) {
|
2005-03-09 12:46:31 +00:00
|
|
|
data.pin1.prompt = "Please enter PUK";
|
|
|
|
data.pin2.prompt = "Please enter new SO PIN";
|
2014-01-19 18:13:00 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-03-09 12:46:31 +00:00
|
|
|
data.pin1.prompt = "Please enter PUK";
|
|
|
|
data.pin2.prompt = "Please enter new PIN";
|
|
|
|
}
|
|
|
|
}
|
2005-01-08 10:20:21 +00:00
|
|
|
|
2011-06-05 15:46:25 +00:00
|
|
|
r = sc_pin_cmd(card, &data, &auth_info->tries_left);
|
2009-10-22 08:59:59 +00:00
|
|
|
if (r == SC_SUCCESS)
|
2010-03-10 09:23:01 +00:00
|
|
|
sc_pkcs15_pincache_add(p15card, pin_obj, newpin, newpinlen);
|
2004-04-23 17:29:24 +00:00
|
|
|
|
2005-08-13 13:04:02 +00:00
|
|
|
out:
|
2003-04-14 14:51:42 +00:00
|
|
|
sc_unlock(card);
|
2014-01-19 18:13:00 +00:00
|
|
|
LOG_FUNC_RETURN(ctx, r);
|
2003-04-14 14:51:42 +00:00
|
|
|
}
|
2004-12-18 14:14:57 +00:00
|
|
|
|
2016-06-10 11:41:18 +00:00
|
|
|
int sc_pkcs15_get_pin_info(struct sc_pkcs15_card *p15card,
|
|
|
|
struct sc_pkcs15_object *pin_obj)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
struct sc_pin_cmd_data data;
|
|
|
|
struct sc_card *card = p15card->card;
|
|
|
|
struct sc_context *ctx = card->ctx;
|
|
|
|
struct sc_pkcs15_auth_info *pin_info = (struct sc_pkcs15_auth_info *) pin_obj->data;
|
|
|
|
|
|
|
|
LOG_FUNC_CALLED(ctx);
|
|
|
|
|
|
|
|
r = sc_lock(card);
|
|
|
|
if (r != SC_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (pin_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN) {
|
|
|
|
r = SC_ERROR_INVALID_DATA;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to update PIN info from card */
|
|
|
|
memset(&data, 0, sizeof(data));
|
|
|
|
data.cmd = SC_PIN_CMD_GET_INFO;
|
2018-04-19 02:04:00 +00:00
|
|
|
data.pin_type = pin_info->auth_method;
|
2016-06-10 11:41:18 +00:00
|
|
|
data.pin_reference = pin_info->attrs.pin.reference;
|
|
|
|
|
|
|
|
r = sc_pin_cmd(card, &data, NULL);
|
|
|
|
if (r == SC_SUCCESS) {
|
|
|
|
if (data.pin1.max_tries > 0)
|
|
|
|
pin_info->max_tries = data.pin1.max_tries;
|
|
|
|
/* tries_left must be supported or sc_pin_cmd should not return SC_SUCCESS */
|
|
|
|
pin_info->tries_left = data.pin1.tries_left;
|
|
|
|
pin_info->logged_in = data.pin1.logged_in;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
sc_unlock(card);
|
|
|
|
LOG_FUNC_RETURN(ctx, r);
|
|
|
|
}
|
|
|
|
|
2014-01-19 18:13:00 +00:00
|
|
|
|
2011-06-05 15:46:25 +00:00
|
|
|
void sc_pkcs15_free_auth_info(sc_pkcs15_auth_info_t *auth_info)
|
2004-12-18 14:14:57 +00:00
|
|
|
{
|
2011-06-05 15:46:25 +00:00
|
|
|
free(auth_info);
|
2004-12-18 14:14:57 +00:00
|
|
|
}
|
2009-10-22 08:59:59 +00:00
|
|
|
|
2010-02-20 22:04:07 +00:00
|
|
|
|
2009-10-22 08:59:59 +00:00
|
|
|
/* Add a PIN to the PIN cache related to the card. Some operations can trigger re-authentication later. */
|
2010-03-10 09:23:01 +00:00
|
|
|
void sc_pkcs15_pincache_add(struct sc_pkcs15_card *p15card, struct sc_pkcs15_object *pin_obj,
|
2009-10-22 08:59:59 +00:00
|
|
|
const u8 *pin, size_t pinlen)
|
|
|
|
{
|
2010-02-20 22:04:07 +00:00
|
|
|
struct sc_context *ctx = p15card->card->ctx;
|
2011-06-05 15:46:25 +00:00
|
|
|
struct sc_pkcs15_auth_info *auth_info = (struct sc_pkcs15_auth_info *)pin_obj->data;
|
2011-01-27 14:25:35 +00:00
|
|
|
struct sc_pkcs15_object *obj = NULL;
|
2010-02-20 22:04:07 +00:00
|
|
|
int r;
|
2009-10-22 08:59:59 +00:00
|
|
|
|
2014-01-19 18:13:00 +00:00
|
|
|
LOG_FUNC_CALLED(ctx);
|
2009-10-22 08:59:59 +00:00
|
|
|
|
2016-08-27 13:29:56 +00:00
|
|
|
if (!pin || !pinlen) {
|
|
|
|
sc_log(ctx, "No cache for zero length PIN");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (!p15card->opts.use_pin_cache) {
|
2014-01-19 18:13:00 +00:00
|
|
|
sc_log(ctx, "PIN caching not enabled");
|
2009-10-22 08:59:59 +00:00
|
|
|
return;
|
2010-02-20 22:04:07 +00:00
|
|
|
}
|
2012-05-31 08:37:38 +00:00
|
|
|
else if (auth_info->auth_type != SC_PKCS15_PIN_AUTH_TYPE_PIN) {
|
2014-01-19 18:13:00 +00:00
|
|
|
sc_log(ctx, "only 'PIN' auth. object can be cached");
|
2012-05-31 08:37:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-10-22 08:59:59 +00:00
|
|
|
|
2011-03-07 16:15:41 +00:00
|
|
|
/* If the PIN protects an object with user consent, don't cache it */
|
|
|
|
|
|
|
|
obj = p15card->obj_list;
|
|
|
|
while (obj != NULL) {
|
|
|
|
/* Compare 'sc_pkcs15_object.auth_id' with 'sc_pkcs15_pin_info.auth_id'.
|
|
|
|
* In accordance with PKCS#15 "6.1.8 CommonObjectAttributes" and
|
|
|
|
* "6.1.16 CommonAuthenticationObjectAttributes" with the exception that
|
|
|
|
* "CommonObjectAttributes.accessControlRules" are not taken into account. */
|
|
|
|
|
2011-06-05 15:46:25 +00:00
|
|
|
if (sc_pkcs15_compare_id(&obj->auth_id, &auth_info->auth_id)) {
|
2011-03-07 16:15:41 +00:00
|
|
|
/* Caching is refused, if the protected object requires user consent */
|
2012-08-07 18:53:44 +00:00
|
|
|
if (!p15card->opts.pin_cache_ignore_user_consent) {
|
2011-03-07 16:15:41 +00:00
|
|
|
if (obj->user_consent > 0) {
|
2014-01-19 18:13:00 +00:00
|
|
|
sc_log(ctx, "caching refused (user consent)");
|
2011-03-07 16:15:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-08-07 18:53:44 +00:00
|
|
|
}
|
2011-01-27 14:25:35 +00:00
|
|
|
}
|
2011-03-07 16:15:41 +00:00
|
|
|
|
|
|
|
obj = obj->next;
|
2009-10-22 08:59:59 +00:00
|
|
|
}
|
|
|
|
|
2011-10-05 09:20:04 +00:00
|
|
|
r = sc_pkcs15_allocate_object_content(ctx, pin_obj, pin, pinlen);
|
2010-02-20 22:04:07 +00:00
|
|
|
if (r != SC_SUCCESS) {
|
2014-01-19 18:13:00 +00:00
|
|
|
sc_log(ctx, "Failed to allocate object content");
|
2010-02-20 22:04:07 +00:00
|
|
|
return;
|
2012-05-31 08:37:38 +00:00
|
|
|
}
|
2010-02-20 22:04:07 +00:00
|
|
|
|
2010-03-10 09:23:01 +00:00
|
|
|
pin_obj->usage_counter = 0;
|
2014-01-19 18:13:00 +00:00
|
|
|
sc_log(ctx, "PIN(%s) cached", pin_obj->label);
|
2009-10-22 08:59:59 +00:00
|
|
|
}
|
2010-02-20 22:04:07 +00:00
|
|
|
|
2009-10-22 08:59:59 +00:00
|
|
|
/* Validate the PIN code associated with an object */
|
2016-07-24 16:42:01 +00:00
|
|
|
int
|
|
|
|
sc_pkcs15_pincache_revalidate(struct sc_pkcs15_card *p15card, const sc_pkcs15_object_t *obj)
|
2009-10-22 08:59:59 +00:00
|
|
|
{
|
2010-02-20 22:04:07 +00:00
|
|
|
struct sc_context *ctx = p15card->card->ctx;
|
2009-10-22 08:59:59 +00:00
|
|
|
sc_pkcs15_object_t *pin_obj;
|
2010-02-21 19:54:59 +00:00
|
|
|
int r;
|
2009-10-22 08:59:59 +00:00
|
|
|
|
2014-01-19 18:13:00 +00:00
|
|
|
LOG_FUNC_CALLED(ctx);
|
2009-10-22 08:59:59 +00:00
|
|
|
if (!p15card->opts.use_pin_cache)
|
|
|
|
return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
|
|
|
|
|
2014-01-19 18:13:00 +00:00
|
|
|
/* Apps that do not support CK_ALWAYS_AUTHENTICATE
|
|
|
|
* may need pin_cache_ignore_user_consent = 1 */
|
2012-08-07 18:53:44 +00:00
|
|
|
if (!p15card->opts.pin_cache_ignore_user_consent) {
|
|
|
|
if (obj->user_consent)
|
2009-10-22 08:59:59 +00:00
|
|
|
return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
|
2012-08-07 18:53:44 +00:00
|
|
|
}
|
2009-10-22 08:59:59 +00:00
|
|
|
|
2017-03-23 15:45:31 +00:00
|
|
|
if ((p15card->card->reader->capabilities & SC_READER_CAP_PIN_PAD
|
|
|
|
|| p15card->card->caps & SC_CARD_CAP_PROTECTED_AUTHENTICATION_PATH))
|
2009-10-22 08:59:59 +00:00
|
|
|
return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
|
|
|
|
|
|
|
|
r = sc_pkcs15_find_pin_by_auth_id(p15card, &obj->auth_id, &pin_obj);
|
|
|
|
if (r != SC_SUCCESS) {
|
2014-01-19 18:13:00 +00:00
|
|
|
sc_log(ctx, "Could not find pin object for auth_id %s", sc_pkcs15_print_id(&obj->auth_id));
|
2009-10-22 08:59:59 +00:00
|
|
|
return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
|
|
|
|
}
|
2012-05-31 08:37:38 +00:00
|
|
|
|
2010-02-20 22:04:07 +00:00
|
|
|
if (pin_obj->usage_counter >= p15card->opts.pin_cache_counter) {
|
2010-12-06 18:33:17 +00:00
|
|
|
sc_pkcs15_free_object_content(pin_obj);
|
2010-02-20 22:04:07 +00:00
|
|
|
return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
|
2009-10-22 08:59:59 +00:00
|
|
|
}
|
2010-02-20 22:04:07 +00:00
|
|
|
|
|
|
|
if (!pin_obj->content.value || !pin_obj->content.len)
|
|
|
|
return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
|
|
|
|
|
2010-09-01 06:23:43 +00:00
|
|
|
pin_obj->usage_counter++;
|
2016-07-24 16:42:01 +00:00
|
|
|
r = _sc_pkcs15_verify_pin(p15card, pin_obj, pin_obj->content.value, pin_obj->content.len);
|
2010-02-20 22:04:07 +00:00
|
|
|
if (r != SC_SUCCESS) {
|
2012-05-31 08:37:38 +00:00
|
|
|
/* Ensure that wrong PIN isn't used again */
|
2010-12-06 18:33:17 +00:00
|
|
|
sc_pkcs15_free_object_content(pin_obj);
|
|
|
|
|
2014-01-19 18:13:00 +00:00
|
|
|
sc_log(ctx, "Verify PIN error %i", r);
|
2010-02-20 22:04:07 +00:00
|
|
|
return SC_ERROR_SECURITY_STATUS_NOT_SATISFIED;
|
|
|
|
}
|
|
|
|
|
2014-01-19 18:13:00 +00:00
|
|
|
LOG_FUNC_RETURN(ctx, SC_SUCCESS);
|
2009-10-22 08:59:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sc_pkcs15_pincache_clear(struct sc_pkcs15_card *p15card)
|
|
|
|
{
|
2010-02-20 22:04:07 +00:00
|
|
|
struct sc_pkcs15_object *objs[32];
|
|
|
|
int i, r;
|
|
|
|
|
2010-03-15 12:17:13 +00:00
|
|
|
SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_NORMAL);
|
2010-02-20 22:04:07 +00:00
|
|
|
r = sc_pkcs15_get_objects(p15card, SC_PKCS15_TYPE_AUTH_PIN, objs, 32);
|
|
|
|
for (i = 0; i < r; i++)
|
|
|
|
sc_pkcs15_free_object_content(objs[i]);
|
2009-12-03 12:51:12 +00:00
|
|
|
}
|
2010-02-20 22:04:07 +00:00
|
|
|
|