pkcs15: new 'data' member in 'sc_pkcs15_data_info'

New member keeps the value of the PKCS#15 DATA object.

Internal pkcs15 procedure that reads DATA object is modified
to check if requested data are already vailable in 'data-info',
an only then try to read the content of dedicated on-card file.

For some emulated PKCS#15 systems value of DATA object is kept as 'direct' value
in a proprietary attribute files and so the common read procedure could not be used.

; some efforts to unify layout of code source.
This commit is contained in:
Viktor Tarasov 2013-03-11 13:04:10 +01:00
parent a24ecd0733
commit 743ca1cedc
2 changed files with 82 additions and 77 deletions

View File

@ -39,35 +39,39 @@ static const struct sc_asn1_entry c_asn1_data_object[] = {
{ NULL, 0, 0, 0, NULL, NULL }
};
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)
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)
{
int r;
struct sc_context *ctx = p15card->card->ctx;
struct sc_pkcs15_data *data_object;
u8 *data = NULL;
size_t len;
struct sc_pkcs15_der der;
int r;
if (p15card == NULL || info == NULL || data_object_out == NULL)
return SC_ERROR_INVALID_ARGUMENTS;
SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_VERBOSE);
LOG_FUNC_CALLED(ctx);
if (!info || !data_object_out)
LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
r = sc_pkcs15_read_file(p15card, &info->path, &data, &len);
if (r)
return r;
data_object = malloc(sizeof(struct sc_pkcs15_data));
if (data_object == NULL) {
free(data);
return SC_ERROR_OUT_OF_MEMORY;
if (!info->data.value) {
r = sc_pkcs15_read_file(p15card, &info->path, &info->data.value, &info->data.len);
LOG_TEST_RET(ctx, r, "Cannot get DATA object data");
}
memset(data_object, 0, sizeof(struct sc_pkcs15_data));
data_object->data = data;
data_object->data_len = len;
sc_der_copy(&der, &info->data);
data_object = calloc(sizeof(struct sc_pkcs15_data), 1);
if (!data_object && !der.value)
LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "Cannot allocate memory for data object");
data_object->data = der.value;
data_object->data_len = der.len;
*data_object_out = data_object;
return SC_SUCCESS;
LOG_FUNC_RETURN(ctx,SC_SUCCESS);
}
static const struct sc_asn1_entry c_asn1_data[] = {
{ "data", SC_ASN1_PKCS15_OBJECT, SC_ASN1_TAG_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
{ NULL, 0, 0, 0, NULL, NULL }
@ -173,7 +177,10 @@ void sc_pkcs15_free_data_object(struct sc_pkcs15_data *data_object)
free(data_object);
}
void sc_pkcs15_free_data_info(sc_pkcs15_data_info_t *data)
void sc_pkcs15_free_data_info(struct sc_pkcs15_data_info *info)
{
free(data);
if (info && info->data.value && info->data.len)
free(info->data.value);
free(info);
}

View File

@ -202,7 +202,7 @@ struct sc_pkcs15_prkey_dsa {
struct sc_pkcs15_ec_parameters {
char *named_curve;
struct sc_object_id id;
sc_pkcs15_der_t der;
struct sc_pkcs15_der der;
size_t field_length; /* in bits */
};
@ -214,13 +214,13 @@ struct sc_pkcs15_gost_parameters {
struct sc_pkcs15_pubkey_ec {
struct sc_pkcs15_ec_parameters params;
sc_pkcs15_der_t ecpointQ; /* note this is der */
struct sc_pkcs15_der ecpointQ; /* note this is der */
};
struct sc_pkcs15_prkey_ec {
struct sc_pkcs15_ec_parameters params;
sc_pkcs15_bignum_t privateD; /* note this is bignum */
sc_pkcs15_der_t ecpointQ; /* note this is der */
struct sc_pkcs15_der ecpointQ; /* note this is der */
};
struct sc_pkcs15_pubkey_gostr3410 {
@ -246,7 +246,7 @@ struct sc_pkcs15_pubkey {
} u;
/* DER encoded raw key */
sc_pkcs15_der_t data;
struct sc_pkcs15_der data;
};
typedef struct sc_pkcs15_pubkey sc_pkcs15_pubkey_t;
@ -300,7 +300,7 @@ struct sc_pkcs15_cert_info {
/* identifiers [2] SEQUENCE OF CredentialIdentifier{{KeyIdentifiers}} */
struct sc_path path;
sc_pkcs15_der_t value;
struct sc_pkcs15_der value;
};
typedef struct sc_pkcs15_cert_info sc_pkcs15_cert_info_t;
@ -320,6 +320,8 @@ struct sc_pkcs15_data_info {
struct sc_object_id app_oid;
struct sc_path path;
struct sc_pkcs15_der data;
};
typedef struct sc_pkcs15_data_info sc_pkcs15_data_info_t;
@ -618,7 +620,7 @@ typedef struct sc_pkcs15_card {
#define SC_PKCS15_TOKEN_PRN_GENERATION 0x04
#define SC_PKCS15_TOKEN_EID_COMPLIANT 0x08
/* flags suitable for sc_pkcs15_card_t */
/* flags suitable for struct sc_pkcs15_card */
#define SC_PKCS15_CARD_FLAG_EMULATED 0x02000000
/* sc_pkcs15_bind: Binds a card object to a PKCS #15 card object
@ -637,13 +639,13 @@ int sc_pkcs15_get_objects_cond(struct sc_pkcs15_card *card, unsigned int type,
int (* func)(struct sc_pkcs15_object *, void *),
void *func_arg,
struct sc_pkcs15_object **ret, size_t ret_count);
int sc_pkcs15_find_object_by_id(sc_pkcs15_card_t *, unsigned int,
int sc_pkcs15_find_object_by_id(struct sc_pkcs15_card *, unsigned int,
const sc_pkcs15_id_t *,
sc_pkcs15_object_t **);
struct sc_pkcs15_object **);
struct sc_pkcs15_card * sc_pkcs15_card_new(void);
void sc_pkcs15_card_free(struct sc_pkcs15_card *p15card);
void sc_pkcs15_card_clear(sc_pkcs15_card_t *p15card);
void sc_pkcs15_card_clear(struct sc_pkcs15_card *p15card);
int sc_pkcs15_decipher(struct sc_pkcs15_card *p15card,
const struct sc_pkcs15_object *prkey_obj,
@ -661,43 +663,39 @@ int sc_pkcs15_compute_signature(struct sc_pkcs15_card *p15card,
size_t inlen, u8 *out, size_t outlen);
int sc_pkcs15_read_pubkey(struct sc_pkcs15_card *,
const struct sc_pkcs15_object *,
struct sc_pkcs15_pubkey **);
const struct sc_pkcs15_object *, struct sc_pkcs15_pubkey **);
int sc_pkcs15_decode_pubkey_rsa(struct sc_context *,
struct sc_pkcs15_pubkey_rsa *,
const u8 *, size_t);
struct sc_pkcs15_pubkey_rsa *, const u8 *, size_t);
int sc_pkcs15_encode_pubkey_rsa(struct sc_context *,
struct sc_pkcs15_pubkey_rsa *, u8 **, size_t *);
struct sc_pkcs15_pubkey_rsa *, u8 **, size_t *);
int sc_pkcs15_decode_pubkey_dsa(struct sc_context *,
struct sc_pkcs15_pubkey_dsa *,
const u8 *, size_t);
struct sc_pkcs15_pubkey_dsa *, const u8 *, size_t);
int sc_pkcs15_encode_pubkey_dsa(struct sc_context *,
struct sc_pkcs15_pubkey_dsa *, u8 **, size_t *);
int sc_pkcs15_decode_pubkey_gostr3410(sc_context_t *,
struct sc_pkcs15_pubkey_dsa *, u8 **, size_t *);
int sc_pkcs15_decode_pubkey_gostr3410(struct sc_context *,
struct sc_pkcs15_pubkey_gostr3410 *, const u8 *, size_t);
int sc_pkcs15_encode_pubkey_gostr3410(sc_context_t *,
int sc_pkcs15_encode_pubkey_gostr3410(struct sc_context *,
struct sc_pkcs15_pubkey_gostr3410 *, u8 **, size_t *);
int sc_pkcs15_decode_pubkey_ec(struct sc_context *,
struct sc_pkcs15_pubkey_ec *, const u8 *, size_t);
struct sc_pkcs15_pubkey_ec *, const u8 *, size_t);
int sc_pkcs15_encode_pubkey_ec(struct sc_context *,
struct sc_pkcs15_pubkey_ec *, u8 **, size_t *);
struct sc_pkcs15_pubkey_ec *, u8 **, size_t *);
int sc_pkcs15_decode_pubkey(struct sc_context *,
struct sc_pkcs15_pubkey *, const u8 *, size_t);
struct sc_pkcs15_pubkey *, const u8 *, size_t);
int sc_pkcs15_encode_pubkey(struct sc_context *,
struct sc_pkcs15_pubkey *, u8 **, size_t *);
struct sc_pkcs15_pubkey *, u8 **, size_t *);
void sc_pkcs15_erase_pubkey(struct sc_pkcs15_pubkey *);
void sc_pkcs15_free_pubkey(struct sc_pkcs15_pubkey *);
int sc_pkcs15_pubkey_from_prvkey(struct sc_context *, struct sc_pkcs15_prkey *,
struct sc_pkcs15_pubkey **);
struct sc_pkcs15_pubkey **);
int sc_pkcs15_pubkey_from_cert(struct sc_context *, struct sc_pkcs15_der *,
struct sc_pkcs15_pubkey **);
struct sc_pkcs15_pubkey **);
int sc_pkcs15_pubkey_from_spki_filename(struct sc_context *,
char *, sc_pkcs15_pubkey_t ** );
char *, struct sc_pkcs15_pubkey ** );
int sc_pkcs15_pubkey_from_spki(struct sc_context *,
sc_pkcs15_pubkey_t **, u8 *, size_t, int);
struct sc_pkcs15_pubkey **, u8 *, size_t, int);
int sc_pkcs15_encode_prkey(struct sc_context *,
struct sc_pkcs15_prkey *,
u8 **, size_t *);
struct sc_pkcs15_prkey *, u8 **, size_t *);
void sc_pkcs15_free_prkey(struct sc_pkcs15_prkey *prkey);
void sc_pkcs15_free_key_params(struct sc_pkcs15_key_params *params);
@ -738,9 +736,9 @@ int sc_pkcs15_find_prkey_by_id_usage(struct sc_pkcs15_card *card,
const struct sc_pkcs15_id *id,
unsigned int usage,
struct sc_pkcs15_object **out);
int sc_pkcs15_find_prkey_by_reference(sc_pkcs15_card_t *,
int sc_pkcs15_find_prkey_by_reference(struct sc_pkcs15_card *,
const sc_path_t *, int,
sc_pkcs15_object_t **);
struct sc_pkcs15_object **);
int sc_pkcs15_find_pubkey_by_id(struct sc_pkcs15_card *card,
const struct sc_pkcs15_id *id,
struct sc_pkcs15_object **out);
@ -778,13 +776,13 @@ int sc_pkcs15_find_pin_by_flags(struct sc_pkcs15_card *p15card,
void sc_pkcs15_pincache_add(struct sc_pkcs15_card *, struct sc_pkcs15_object *,
const u8 *, size_t);
int sc_pkcs15_pincache_revalidate(struct sc_pkcs15_card *p15card,
const sc_pkcs15_object_t *obj);
const struct sc_pkcs15_object *obj);
void sc_pkcs15_pincache_clear(struct sc_pkcs15_card *p15card);
int sc_pkcs15_encode_dir(struct sc_context *ctx,
struct sc_pkcs15_card *card,
u8 **buf, size_t *buflen);
int sc_pkcs15_parse_tokeninfo(sc_context_t *ctx,
int sc_pkcs15_parse_tokeninfo(struct sc_context *ctx,
sc_pkcs15_tokeninfo_t *ti,
const u8 *buf, size_t blen);
int sc_pkcs15_encode_tokeninfo(struct sc_context *ctx,
@ -853,7 +851,7 @@ int sc_pkcs15_add_unusedspace(struct sc_pkcs15_card *p15card,
const sc_path_t *path, const sc_pkcs15_id_t *auth_id);
int sc_pkcs15_parse_unusedspace(const u8 * buf, size_t buflen,
struct sc_pkcs15_card *card);
int sc_pkcs15_encode_unusedspace(sc_context_t *ctx,
int sc_pkcs15_encode_unusedspace(struct sc_context *ctx,
struct sc_pkcs15_card *p15card,
u8 **buf, size_t *buflen);
@ -866,7 +864,7 @@ void sc_pkcs15_free_pubkey_info(sc_pkcs15_pubkey_info_t *key);
void sc_pkcs15_free_cert_info(sc_pkcs15_cert_info_t *cert);
void sc_pkcs15_free_data_info(sc_pkcs15_data_info_t *data);
void sc_pkcs15_free_auth_info(sc_pkcs15_auth_info_t *auth_info);
void sc_pkcs15_free_object(sc_pkcs15_object_t *obj);
void sc_pkcs15_free_object(struct sc_pkcs15_object *obj);
/* Generic file i/o */
int sc_pkcs15_read_file(struct sc_pkcs15_card *p15card,
@ -887,7 +885,7 @@ int sc_pkcs15_compare_id(const struct sc_pkcs15_id *id1,
const char *sc_pkcs15_print_id(const struct sc_pkcs15_id *id);
void sc_pkcs15_format_id(const char *id_in, struct sc_pkcs15_id *id_out);
int sc_pkcs15_hex_string_to_id(const char *in, struct sc_pkcs15_id *out);
int sc_der_copy(sc_pkcs15_der_t *, const sc_pkcs15_der_t *);
int sc_der_copy(struct sc_pkcs15_der *, const struct sc_pkcs15_der *);
int sc_pkcs15_get_object_id(const struct sc_pkcs15_object *, struct sc_pkcs15_id *);
int sc_pkcs15_get_guid(struct sc_pkcs15_card *, const struct sc_pkcs15_object *, unsigned,
char *, size_t);
@ -940,8 +938,8 @@ typedef struct sc_pkcs15_search_key {
const char * label;
} sc_pkcs15_search_key_t;
int sc_pkcs15_search_objects(sc_pkcs15_card_t *, sc_pkcs15_search_key_t *,
sc_pkcs15_object_t **, size_t);
int sc_pkcs15_search_objects(struct sc_pkcs15_card *, sc_pkcs15_search_key_t *,
struct sc_pkcs15_object **, size_t);
/* This structure is passed to the new sc_pkcs15emu_*_init functions */
typedef struct sc_pkcs15emu_opt {
@ -951,26 +949,26 @@ typedef struct sc_pkcs15emu_opt {
#define SC_PKCS15EMU_FLAGS_NO_CHECK 0x00000001
extern int sc_pkcs15_bind_synthetic(sc_pkcs15_card_t *);
extern int sc_pkcs15_bind_synthetic(struct sc_pkcs15_card *);
extern int sc_pkcs15_is_emulation_only(sc_card_t *);
int sc_pkcs15emu_object_add(sc_pkcs15_card_t *, unsigned int,
const sc_pkcs15_object_t *, const void *);
int sc_pkcs15emu_object_add(struct sc_pkcs15_card *, unsigned int,
const struct sc_pkcs15_object *, const void *);
/* some wrapper functions for sc_pkcs15emu_object_add */
int sc_pkcs15emu_add_pin_obj(sc_pkcs15_card_t *,
const sc_pkcs15_object_t *, const sc_pkcs15_auth_info_t *);
int sc_pkcs15emu_add_rsa_prkey(sc_pkcs15_card_t *,
const sc_pkcs15_object_t *, const sc_pkcs15_prkey_info_t *);
int sc_pkcs15emu_add_rsa_pubkey(sc_pkcs15_card_t *,
const sc_pkcs15_object_t *, const sc_pkcs15_pubkey_info_t *);
int sc_pkcs15emu_add_ec_prkey(sc_pkcs15_card_t *,
const sc_pkcs15_object_t *, const sc_pkcs15_prkey_info_t *);
int sc_pkcs15emu_add_ec_pubkey(sc_pkcs15_card_t *,
const sc_pkcs15_object_t *, const sc_pkcs15_pubkey_info_t *);
int sc_pkcs15emu_add_x509_cert(sc_pkcs15_card_t *,
const sc_pkcs15_object_t *, const sc_pkcs15_cert_info_t *);
int sc_pkcs15emu_add_data_object(sc_pkcs15_card_t *,
const sc_pkcs15_object_t *, const sc_pkcs15_data_info_t *);
int sc_pkcs15emu_add_pin_obj(struct sc_pkcs15_card *,
const struct sc_pkcs15_object *, const sc_pkcs15_auth_info_t *);
int sc_pkcs15emu_add_rsa_prkey(struct sc_pkcs15_card *,
const struct sc_pkcs15_object *, const sc_pkcs15_prkey_info_t *);
int sc_pkcs15emu_add_rsa_pubkey(struct sc_pkcs15_card *,
const struct sc_pkcs15_object *, const sc_pkcs15_pubkey_info_t *);
int sc_pkcs15emu_add_ec_prkey(struct sc_pkcs15_card *,
const struct sc_pkcs15_object *, const sc_pkcs15_prkey_info_t *);
int sc_pkcs15emu_add_ec_pubkey(struct sc_pkcs15_card *,
const struct sc_pkcs15_object *, const sc_pkcs15_pubkey_info_t *);
int sc_pkcs15emu_add_x509_cert(struct sc_pkcs15_card *,
const struct sc_pkcs15_object *, const sc_pkcs15_cert_info_t *);
int sc_pkcs15emu_add_data_object(struct sc_pkcs15_card *,
const struct sc_pkcs15_object *, const sc_pkcs15_data_info_t *);
#ifdef __cplusplus
}