diff --git a/src/libopensc/internal.h b/src/libopensc/internal.h index 4c26f951..c749570b 100644 --- a/src/libopensc/internal.h +++ b/src/libopensc/internal.h @@ -2,6 +2,7 @@ * internal.h: Internal definitions for libopensc * * Copyright (C) 2001, 2002 Juha Yrjölä + * 2005 The OpenSC project * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -88,6 +89,25 @@ int sc_asn1_read_tag(const u8 ** buf, size_t buflen, unsigned int *cla_out, scconf_block *_get_conf_block(sc_context_t *ctx, const char *name1, const char *name2, int priority); + +/********************************************************************/ +/* pkcs1 padding/encoding functions */ +/********************************************************************/ + +int sc_pkcs1_add_01_padding(const u8 *in, size_t in_len, u8 *out, + size_t *out_len, size_t mod_length); +int sc_pkcs1_strip_01_padding(const u8 *in_dat, size_t in_len, u8 *out_dat, + size_t *out_len); +int sc_pkcs1_strip_02_padding(const u8 *data, size_t len, u8 *out_dat, + size_t *out_len); +int sc_pkcs1_add_digest_info_prefix(unsigned int algorithm, const u8 *in_dat, + size_t in_len, u8 *out_dat, size_t *out_len); +int sc_pkcs1_strip_digest_info_prefix(unsigned int *algorithm, + const u8 *in_dat, size_t in_len, u8 *out_dat, size_t *out_len); +int sc_pkcs1_encode(sc_context_t *ctx, unsigned long flags, + const u8 *in, size_t in_len, u8 *out, size_t *out_len, size_t mod_len); +int sc_strip_zero_padding(const u8 *in,size_t in_len, u8 *out, size_t *out_len); + #ifdef __cplusplus } #endif diff --git a/src/libopensc/opensc.h b/src/libopensc/opensc.h index efbd40fb..b033c270 100644 --- a/src/libopensc/opensc.h +++ b/src/libopensc/opensc.h @@ -2,6 +2,7 @@ * opensc.h: OpenSC library header file * * Copyright (C) 2001, 2002 Juha Yrjölä + * 2005 The OpenSC project * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -775,7 +776,10 @@ int sc_lock(sc_card_t *card); */ int sc_unlock(sc_card_t *card); -/* ISO 7816-4 related functions */ + +/********************************************************************/ +/* ISO 7816-4 related functions */ +/********************************************************************/ /** * Does the equivalent of ISO 7816-4 command SELECT FILE. @@ -821,7 +825,10 @@ int sc_put_data(sc_card_t *, unsigned int, const u8 *, size_t); int sc_get_challenge(sc_card_t *card, u8 * rndout, size_t len); -/* ISO 7816-8 related functions */ +/********************************************************************/ +/* ISO 7816-8 related functions */ +/********************************************************************/ + int sc_restore_security_env(sc_card_t *card, int se_num); int sc_set_security_env(sc_card_t *card, const struct sc_security_env *env, int se_num); @@ -841,21 +848,12 @@ int sc_reset_retry_counter(sc_card_t *card, unsigned int type, int ref, const u8 *puk, size_t puklen, const u8 *newref, size_t newlen); int sc_build_pin(u8 *buf, size_t buflen, struct sc_pin_cmd_pin *pin, int pad); -/* pkcs1 padding/encoding functions */ -int sc_pkcs1_add_01_padding(const u8 *in, size_t in_len, u8 *out, - size_t *out_len, size_t mod_length); -int sc_pkcs1_strip_01_padding(const u8 *in_dat, size_t in_len, u8 *out_dat, - size_t *out_len); -int sc_pkcs1_strip_02_padding(const u8 *data, size_t len, u8 *out_dat, - size_t *out_len); -int sc_pkcs1_add_digest_info_prefix(unsigned int algorithm, const u8 *in_dat, - size_t in_len, u8 *out_dat, size_t *out_len); -int sc_pkcs1_strip_digest_info_prefix(unsigned int *algorithm, - const u8 *in_dat, size_t in_len, u8 *out_dat, size_t *out_len); -int sc_pkcs1_encode(sc_context_t *ctx, unsigned long flags, - const u8 *in, size_t in_len, u8 *out, size_t *out_len, size_t mod_len); -int sc_strip_zero_padding(const u8 *in,size_t in_len, u8 *out, size_t *out_len); -/* ISO 7816-9 */ + + +/********************************************************************/ +/* ISO 7816-9 related functions */ +/********************************************************************/ + int sc_create_file(sc_card_t *card, sc_file_t *file); int sc_delete_file(sc_card_t *card, const sc_path_t *path); @@ -886,8 +884,27 @@ int sc_compare_path(const sc_path_t *, const sc_path_t *); int sc_append_path(sc_path_t *dest, const sc_path_t *src); int sc_append_path_id(sc_path_t *dest, const u8 *id, size_t idlen); int sc_append_file_id(sc_path_t *dest, unsigned int fid); + + +/********************************************************************/ +/* miscellaneous functions */ +/********************************************************************/ + int sc_hex_to_bin(const char *in, u8 *out, size_t *outlen); int sc_bin_to_hex(const u8 *, size_t, char *, size_t, int separator); +/** + * Converts a given OID in ascii form to a internal sc_object_id object + * @param oid OUT sc_object_id object for the result + * @param in ascii string with the oid ("1.2.3.4.5...") + * @return SC_SUCCESS or an error value if an error occurred. + */ +int sc_format_oid(struct sc_object_id *oid, const char *in); +/** + * Compares two sc_object_id objects + * @param oid1 the first sc_object_id object + * @param oid2 the second sc_object_id object + * @return 0 if the oids are equal and a non-zero value otherwise + */ int sc_compare_oid(const struct sc_object_id *oid1, const struct sc_object_id *oid2); /** diff --git a/src/libopensc/sc.c b/src/libopensc/sc.c index ab0810ab..d828fb1b 100644 --- a/src/libopensc/sc.c +++ b/src/libopensc/sc.c @@ -28,6 +28,7 @@ #include "internal.h" #include +#include #include #include #include @@ -110,6 +111,33 @@ int sc_bin_to_hex(const u8 *in, size_t in_len, char *out, size_t out_len, return 0; } +int sc_format_oid(struct sc_object_id *oid, const char *in) +{ + int ii, ret = SC_ERROR_INVALID_ARGUMENTS; + const char *p; + char *q; + + if (oid == NULL || in == NULL) + return ret; + /* init oid */ + for (ii=0; iivalue[ii] = -1; + + p = in; + + for (ii=0; ii < SC_MAX_OBJECT_ID_OCTETS; ii++) { + oid->value[ii] = strtol(p, &q, 10); + if (!*q) + break; + if (!(q[0] == '.' && isdigit(q[1]))) { + return ret; + } + p = q + 1; + } + + return SC_SUCCESS; +} + int sc_compare_oid(const struct sc_object_id *oid1, const struct sc_object_id *oid2) { int i; diff --git a/src/tools/pkcs11-tool.c b/src/tools/pkcs11-tool.c index bb5b6121..1d5b5dd4 100644 --- a/src/tools/pkcs11-tool.c +++ b/src/tools/pkcs11-tool.c @@ -1808,7 +1808,7 @@ read_object(CK_SLOT_ID slot, CK_SESSION_HANDLE session) } if (opt_application_id != NULL) { - parse_application_id(&oid, opt_application_id); + sc_format_oid(&oid, opt_application_id); FILL_ATTR(attrs[nn_attrs], CKA_OBJECT_ID, (unsigned char *)oid.value, sizeof(oid.value)); nn_attrs++; diff --git a/src/tools/pkcs15-init.c b/src/tools/pkcs15-init.c index f9b6d803..abed1d0c 100644 --- a/src/tools/pkcs15-init.c +++ b/src/tools/pkcs15-init.c @@ -1036,7 +1036,7 @@ do_store_data_object(struct sc_profile *profile) args.label = opt_label; args.app_label = "pkcs15-init"; - parse_application_id(&args.app_oid, opt_application_id); + sc_format_oid(&args.app_oid, opt_application_id); r = do_read_data_object(opt_infile, &data, &datalen); if (r >= 0) { @@ -1192,7 +1192,7 @@ do_delete_objects(struct sc_profile *profile, unsigned int opt_delete_flags) sc_pkcs15_object_t *obj; if (opt_application_id == NULL) fatal("Specify the --application-id for the data object to be deleted\n"); - parse_application_id(&app_oid, opt_application_id); + sc_format_oid(&app_oid, opt_application_id); r = sc_pkcs15_find_data_object_by_app_oid(p15card, &app_oid, &obj); if (r >= 0) { diff --git a/src/tools/pkcs15-tool.c b/src/tools/pkcs15-tool.c index 7c75ac03..aa392639 100644 --- a/src/tools/pkcs15-tool.c +++ b/src/tools/pkcs15-tool.c @@ -307,7 +307,7 @@ static int read_data_object(void) } count = r; - r = parse_application_id(&oid, opt_data); + r = sc_format_oid(&oid, opt_data); if (r == SC_SUCCESS) { while (oid.value[oid_len] >= 0) oid_len++; } diff --git a/src/tools/util.c b/src/tools/util.c index 912d3198..330b8286 100644 --- a/src/tools/util.c +++ b/src/tools/util.c @@ -271,32 +271,3 @@ warn(const char *fmt, ...) va_end(ap); } - -int parse_application_id(struct sc_object_id *oid, char *oid_str) -{ - int ii, ret = SC_ERROR_INVALID_ARGUMENTS; - char *p, *q; - - if (!oid) - return ret; - /* init oid */ - for (ii=0; iivalue[ii] = -1; - - if (!(p = oid_str)) - return ret; - - for (ii=0; ii < SC_MAX_OBJECT_ID_OCTETS; ii++) { - oid->value[ii] = strtol(p, &q, 10); - if (!*q) - break; - if (!(q[0] == '.' && isdigit(q[1]))) { - return ret; - } - p = q + 1; - } - - return SC_SUCCESS; -} - - diff --git a/src/tools/util.h b/src/tools/util.h index 06a83a8a..342f6ead 100644 --- a/src/tools/util.h +++ b/src/tools/util.h @@ -40,7 +40,6 @@ void fatal(const char *fmt, ...); /* All singing all dancing card connect routine */ int connect_card(struct sc_context *, struct sc_card **, int reader_id, int slot_id, int wait, int verbose); -int parse_application_id(struct sc_object_id *oid, char *oid_str); #ifdef __cplusplus }