2002-12-18 10:17:01 +00:00
|
|
|
/*
|
|
|
|
* pkcs15-data.c: PKCS #15 data object functions
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 Danny De Cock <daniel.decock@postbox.be>
|
|
|
|
*
|
|
|
|
* This source file was inspired by pkcs15-cert.c.
|
|
|
|
*
|
|
|
|
* 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
|
2010-03-04 08:14:36 +00:00
|
|
|
|
2002-12-18 10:17:01 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2010-03-04 08:14:36 +00:00
|
|
|
#include "internal.h"
|
|
|
|
#include "asn1.h"
|
|
|
|
#include "pkcs15.h"
|
2016-01-20 09:45:06 +00:00
|
|
|
#include "common/compat_strnlen.h"
|
2010-03-04 08:14:36 +00:00
|
|
|
|
2013-03-11 12:04:10 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
sc_pkcs15_read_data_object(struct sc_pkcs15_card *p15card,
|
|
|
|
const struct sc_pkcs15_data_info *info,
|
|
|
|
struct sc_pkcs15_data **data_object_out)
|
2002-12-18 10:17:01 +00:00
|
|
|
{
|
2019-11-04 20:22:38 +00:00
|
|
|
struct sc_context *ctx = p15card->card->ctx;
|
2002-12-18 10:17:01 +00:00
|
|
|
struct sc_pkcs15_data *data_object;
|
2013-03-11 12:04:10 +00:00
|
|
|
struct sc_pkcs15_der der;
|
|
|
|
int r;
|
2012-05-29 09:29:44 +00:00
|
|
|
|
2013-03-11 12:04:10 +00:00
|
|
|
LOG_FUNC_CALLED(ctx);
|
|
|
|
if (!info || !data_object_out)
|
|
|
|
LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
|
2002-12-18 10:17:01 +00:00
|
|
|
|
2013-03-11 12:04:10 +00:00
|
|
|
if (!info->data.value) {
|
2014-02-25 08:07:09 +00:00
|
|
|
r = sc_pkcs15_read_file(p15card, &info->path, (unsigned char **) &info->data.value, (size_t *) &info->data.len);
|
2013-03-11 12:04:10 +00:00
|
|
|
LOG_TEST_RET(ctx, r, "Cannot get DATA object data");
|
2002-12-18 10:17:01 +00:00
|
|
|
}
|
2012-05-29 09:29:44 +00:00
|
|
|
|
2020-09-07 10:59:45 +00:00
|
|
|
r = sc_der_copy(&der, &info->data);
|
|
|
|
LOG_TEST_RET(ctx, r, "Cannot allocate memory for der value");
|
2015-05-07 16:52:57 +00:00
|
|
|
|
2013-03-11 12:04:10 +00:00
|
|
|
data_object = calloc(sizeof(struct sc_pkcs15_data), 1);
|
2015-05-07 16:52:57 +00:00
|
|
|
if (!data_object) {
|
|
|
|
free(der.value);
|
2013-03-11 12:04:10 +00:00
|
|
|
LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "Cannot allocate memory for data object");
|
2015-05-07 16:52:57 +00:00
|
|
|
}
|
2013-03-11 12:04:10 +00:00
|
|
|
|
|
|
|
data_object->data = der.value;
|
|
|
|
data_object->data_len = der.len;
|
2002-12-18 10:17:01 +00:00
|
|
|
*data_object_out = data_object;
|
2013-03-11 12:04:10 +00:00
|
|
|
|
|
|
|
LOG_FUNC_RETURN(ctx,SC_SUCCESS);
|
2002-12-18 10:17:01 +00:00
|
|
|
}
|
|
|
|
|
2013-03-11 12:04:10 +00:00
|
|
|
|
2002-12-19 14:26:54 +00:00
|
|
|
static const struct sc_asn1_entry c_asn1_data[] = {
|
2006-01-20 20:52:36 +00:00
|
|
|
{ "data", SC_ASN1_PKCS15_OBJECT, 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-12-18 10:17:01 +00:00
|
|
|
};
|
|
|
|
static const struct sc_asn1_entry c_asn1_com_data_attr[] = {
|
2006-01-20 20:52:36 +00:00
|
|
|
{ "appName", SC_ASN1_UTF8STRING, SC_ASN1_TAG_UTF8STRING, SC_ASN1_OPTIONAL, NULL, NULL },
|
|
|
|
{ "appOID", SC_ASN1_OBJECT, SC_ASN1_TAG_OBJECT, SC_ASN1_OPTIONAL, NULL, NULL },
|
2005-08-05 07:24:43 +00:00
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
2002-12-18 10:17:01 +00:00
|
|
|
};
|
|
|
|
static const struct sc_asn1_entry c_asn1_type_data_attr[] = {
|
2006-01-20 20:52:36 +00:00
|
|
|
{ "path", SC_ASN1_PATH, 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-12-18 10:17:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int sc_pkcs15_decode_dodf_entry(struct sc_pkcs15_card *p15card,
|
|
|
|
struct sc_pkcs15_object *obj,
|
|
|
|
const u8 ** buf, size_t *buflen)
|
|
|
|
{
|
2005-03-08 20:59:35 +00:00
|
|
|
sc_context_t *ctx = p15card->card->ctx;
|
2002-12-18 10:17:01 +00:00
|
|
|
struct sc_pkcs15_data_info info;
|
2002-12-19 14:26:54 +00:00
|
|
|
struct sc_asn1_entry asn1_com_data_attr[3],
|
|
|
|
asn1_type_data_attr[2],
|
|
|
|
asn1_data[2];
|
2002-12-18 10:17:01 +00:00
|
|
|
struct sc_asn1_pkcs15_object data_obj = { obj, asn1_com_data_attr, NULL,
|
|
|
|
asn1_type_data_attr };
|
2019-03-03 14:52:57 +00:00
|
|
|
size_t label_len = sizeof(info.app_label) - 1;
|
2002-12-18 10:17:01 +00:00
|
|
|
int r;
|
2002-12-19 14:26:54 +00:00
|
|
|
|
2019-03-03 14:52:57 +00:00
|
|
|
memset(info.app_label, 0, sizeof(info.app_label));
|
|
|
|
|
2002-12-18 10:17:01 +00:00
|
|
|
sc_copy_asn1_entry(c_asn1_com_data_attr, asn1_com_data_attr);
|
|
|
|
sc_copy_asn1_entry(c_asn1_type_data_attr, asn1_type_data_attr);
|
|
|
|
sc_copy_asn1_entry(c_asn1_data, asn1_data);
|
2012-05-29 09:29:44 +00:00
|
|
|
|
2002-12-19 14:26:54 +00:00
|
|
|
sc_format_asn1_entry(asn1_com_data_attr + 0, &info.app_label, &label_len, 0);
|
|
|
|
sc_format_asn1_entry(asn1_com_data_attr + 1, &info.app_oid, NULL, 0);
|
|
|
|
sc_format_asn1_entry(asn1_type_data_attr + 0, &info.path, NULL, 0);
|
2002-12-18 10:17:01 +00:00
|
|
|
sc_format_asn1_entry(asn1_data + 0, &data_obj, NULL, 0);
|
|
|
|
|
2004-07-23 20:29:05 +00:00
|
|
|
/* Fill in defaults */
|
|
|
|
memset(&info, 0, sizeof(info));
|
libopensc: 'init', 'format', 'compare', 'is-valid' OID procedures
In a reason of number of bugs(*) that concern the OID management,
the general usage OID procedures 'init', 'format', 'compare', 'is-valid' are introduced.
These procedures should be used by all actors: libopensc, pkcs15, pkcs11, tools, ....
(*)
This bug reported by Andreas Schwier :
https://github.com/OpenSC/OpenSC/commit/8e75d971cb7eadfef9b5b50adb3cb6d18e641ed2#commitcomment-1792477
In pkcs15-algo sc_asn1_get_algorithm_info() can return the OID without ending '-1's:
https://github.com/OpenSC/OpenSC/blob/staging/src/libopensc/pkcs15-algo.c#L452
https://github.com/OpenSC/OpenSC/blob/staging/src/libopensc/pkcs15-algo.c#L459
2012-09-01 22:12:57 +00:00
|
|
|
sc_init_oid(&info.app_oid);
|
2004-07-23 20:29:05 +00:00
|
|
|
|
2002-12-18 10:17:01 +00:00
|
|
|
r = sc_asn1_decode(ctx, asn1_data, *buf, *buflen, buf, buflen);
|
|
|
|
if (r == SC_ERROR_ASN1_END_OF_CONTENTS)
|
|
|
|
return r;
|
2018-11-23 20:54:14 +00:00
|
|
|
LOG_TEST_RET(ctx, r, "ASN.1 decoding failed");
|
2011-01-18 16:31:41 +00:00
|
|
|
|
2019-10-31 08:38:27 +00:00
|
|
|
if (!p15card->app || !p15card->app->ddo.aid.len) {
|
|
|
|
if (!p15card->file_app) {
|
|
|
|
return SC_ERROR_INTERNAL;
|
|
|
|
}
|
2011-01-18 16:31:41 +00:00
|
|
|
r = sc_pkcs15_make_absolute_path(&p15card->file_app->path, &info.path);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
info.path.aid = p15card->app->ddo.aid;
|
|
|
|
}
|
|
|
|
|
2002-12-18 10:17:01 +00:00
|
|
|
obj->type = SC_PKCS15_TYPE_DATA_OBJECT;
|
|
|
|
obj->data = malloc(sizeof(info));
|
|
|
|
if (obj->data == NULL)
|
2018-11-22 22:10:49 +00:00
|
|
|
LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY);
|
2002-12-18 10:17:01 +00:00
|
|
|
memcpy(obj->data, &info, sizeof(info));
|
|
|
|
|
2012-05-29 09:29:44 +00:00
|
|
|
return SC_SUCCESS;
|
2002-12-18 10:17:01 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 20:59:35 +00:00
|
|
|
int sc_pkcs15_encode_dodf_entry(sc_context_t *ctx,
|
2002-12-18 10:17:01 +00:00
|
|
|
const struct sc_pkcs15_object *obj,
|
|
|
|
u8 **buf, size_t *bufsize)
|
|
|
|
{
|
2002-12-19 14:26:54 +00:00
|
|
|
struct sc_asn1_entry asn1_com_data_attr[4],
|
|
|
|
asn1_type_data_attr[2],
|
|
|
|
asn1_data[2];
|
|
|
|
struct sc_pkcs15_data_info *info;
|
2002-12-18 10:17:01 +00:00
|
|
|
struct sc_asn1_pkcs15_object data_obj = { (struct sc_pkcs15_object *) obj,
|
|
|
|
asn1_com_data_attr, NULL,
|
|
|
|
asn1_type_data_attr };
|
2002-12-19 14:26:54 +00:00
|
|
|
size_t label_len;
|
|
|
|
|
|
|
|
info = (struct sc_pkcs15_data_info *) obj->data;
|
2015-10-05 06:23:02 +00:00
|
|
|
label_len = strnlen(info->app_label, sizeof info->app_label);
|
2002-12-19 14:26:54 +00:00
|
|
|
|
2002-12-18 10:17:01 +00:00
|
|
|
sc_copy_asn1_entry(c_asn1_com_data_attr, asn1_com_data_attr);
|
|
|
|
sc_copy_asn1_entry(c_asn1_type_data_attr, asn1_type_data_attr);
|
|
|
|
sc_copy_asn1_entry(c_asn1_data, asn1_data);
|
2012-05-29 09:29:44 +00:00
|
|
|
|
libopensc: 'init', 'format', 'compare', 'is-valid' OID procedures
In a reason of number of bugs(*) that concern the OID management,
the general usage OID procedures 'init', 'format', 'compare', 'is-valid' are introduced.
These procedures should be used by all actors: libopensc, pkcs15, pkcs11, tools, ....
(*)
This bug reported by Andreas Schwier :
https://github.com/OpenSC/OpenSC/commit/8e75d971cb7eadfef9b5b50adb3cb6d18e641ed2#commitcomment-1792477
In pkcs15-algo sc_asn1_get_algorithm_info() can return the OID without ending '-1's:
https://github.com/OpenSC/OpenSC/blob/staging/src/libopensc/pkcs15-algo.c#L452
https://github.com/OpenSC/OpenSC/blob/staging/src/libopensc/pkcs15-algo.c#L459
2012-09-01 22:12:57 +00:00
|
|
|
if (label_len)
|
|
|
|
sc_format_asn1_entry(asn1_com_data_attr + 0, &info->app_label, &label_len, 1);
|
|
|
|
|
|
|
|
if (sc_valid_oid(&info->app_oid))
|
|
|
|
sc_format_asn1_entry(asn1_com_data_attr + 1, &info->app_oid, NULL, 1);
|
|
|
|
|
2002-12-19 14:26:54 +00:00
|
|
|
sc_format_asn1_entry(asn1_type_data_attr + 0, &info->path, NULL, 1);
|
|
|
|
sc_format_asn1_entry(asn1_data + 0, &data_obj, NULL, 1);
|
2002-12-18 10:17:01 +00:00
|
|
|
|
2002-12-19 14:26:54 +00:00
|
|
|
return sc_asn1_encode(ctx, asn1_data, buf, bufsize);
|
2002-12-18 10:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sc_pkcs15_free_data_object(struct sc_pkcs15_data *data_object)
|
|
|
|
{
|
2005-08-05 07:24:43 +00:00
|
|
|
if (data_object == NULL)
|
|
|
|
return;
|
2002-12-18 10:17:01 +00:00
|
|
|
|
|
|
|
free(data_object->data);
|
|
|
|
free(data_object);
|
|
|
|
}
|
2004-12-18 14:14:57 +00:00
|
|
|
|
2013-03-11 12:04:10 +00:00
|
|
|
void sc_pkcs15_free_data_info(struct sc_pkcs15_data_info *info)
|
2004-12-18 14:14:57 +00:00
|
|
|
{
|
2013-03-11 12:04:10 +00:00
|
|
|
if (info && info->data.value && info->data.len)
|
|
|
|
free(info->data.value);
|
|
|
|
|
|
|
|
free(info);
|
2004-12-18 14:14:57 +00:00
|
|
|
}
|