- added sc_pkcs15_read_pubkey to retrieve public key from a public key file

git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@281 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
okir 2002-03-08 19:47:26 +00:00
parent 4fcf38e98e
commit c94fb9a1d5
4 changed files with 94 additions and 24 deletions

View File

@ -32,6 +32,7 @@ extern "C" {
#define SC_PKCS15_PIN_MAGIC 0x31415926
#define SC_PKCS15_MAX_PINS 2
#define SC_PKCS15_MAX_PRKEYS 2
#define SC_PKCS15_MAX_PUBKEYS 2
#define SC_PKCS15_MAX_LABEL_SIZE 32
#define SC_PKCS15_MAX_ID_SIZE 16
#define SC_PKCS15_MAX_DFS 4
@ -256,6 +257,12 @@ int sc_pkcs15_compute_signature(struct sc_pkcs15_card *p15card,
void sc_pkcs15_print_card(const struct sc_pkcs15_card *card);
int sc_pkcs15_read_pubkey(struct sc_pkcs15_card *card,
const struct sc_pkcs15_pubkey_info *info,
struct sc_pkcs15_pubkey_rsa **out);
int sc_pkcs15_parse_pubkey_rsa(struct sc_context *ctx,
struct sc_pkcs15_pubkey_rsa *pubkey);
void sc_pkcs15_print_cert_info(const struct sc_pkcs15_cert_info *cert);
int sc_pkcs15_read_certificate(struct sc_pkcs15_card *card,
const struct sc_pkcs15_cert_info *info,

View File

@ -29,29 +29,6 @@
#include <unistd.h>
#include <assert.h>
static int parse_pubkey_rsa(struct sc_context *ctx, struct sc_pkcs15_pubkey_rsa *key)
{
struct sc_asn1_entry asn1_pubkey_rsa[] = {
{ "modulus", SC_ASN1_OCTET_STRING, ASN1_INTEGER, SC_ASN1_ALLOC, &key->modulus, &key->modulus_len },
{ "publicExponent", SC_ASN1_INTEGER, ASN1_INTEGER, 0, &key->exponent },
{ NULL }
};
const u8 *obj;
size_t objlen;
int r;
obj = sc_asn1_verify_tag(ctx, key->data, key->data_len, ASN1_SEQUENCE | SC_ASN1_CONS,
&objlen);
if (obj == NULL) {
error(ctx, "RSA public key not found\n");
return SC_ERROR_INVALID_ASN1_OBJECT;
}
r = sc_asn1_decode(ctx, asn1_pubkey_rsa, obj, objlen, NULL, NULL);
SC_TEST_RET(ctx, r, "ASN.1 parsing failed");
return 0;
}
struct asn1_algorithm_id {
struct sc_object_id id;
};
@ -123,7 +100,7 @@ static int parse_x509_cert(struct sc_context *ctx, const u8 *buf, size_t buflen,
key->data = pk;
key->data_len = pklen;
/* FIXME: ignore the object id for now, and presume it's RSA */
r = parse_pubkey_rsa(ctx, key);
r = sc_pkcs15_parse_pubkey_rsa(ctx, key);
if (r) {
free(key->data);
return SC_ERROR_INVALID_ASN1_OBJECT;

View File

@ -152,3 +152,82 @@ int sc_pkcs15_encode_pukdf_entry(struct sc_context *ctx,
return r;
}
int
sc_pkcs15_parse_pubkey_rsa(struct sc_context *ctx, struct sc_pkcs15_pubkey_rsa *key)
{
struct sc_asn1_entry asn1_pubkey_rsa[] = {
{ "modulus", SC_ASN1_OCTET_STRING, ASN1_INTEGER, SC_ASN1_ALLOC, &key->modulus, &key->modulus_len },
{ "publicExponent", SC_ASN1_INTEGER, ASN1_INTEGER, 0, &key->exponent },
{ NULL }
};
const u8 *obj;
size_t objlen;
int r;
obj = sc_asn1_verify_tag(ctx, key->data, key->data_len, ASN1_SEQUENCE | SC_ASN1_CONS,
&objlen);
if (obj == NULL) {
error(ctx, "RSA public key not found\n");
return SC_ERROR_INVALID_ASN1_OBJECT;
}
r = sc_asn1_decode(ctx, asn1_pubkey_rsa, obj, objlen, NULL, NULL);
SC_TEST_RET(ctx, r, "ASN.1 parsing failed");
return 0;
}
int
sc_pkcs15_read_pubkey(struct sc_pkcs15_card *p15card,
const struct sc_pkcs15_pubkey_info *info,
struct sc_pkcs15_pubkey_rsa **out)
{
struct sc_file *file;
struct sc_pkcs15_pubkey_rsa *pubkey;
u8 *data;
size_t len;
int r;
assert(p15card != NULL && info != NULL && out != NULL);
SC_FUNC_CALLED(p15card->card->ctx, 1);
r = sc_pkcs15_read_cached_file(p15card, &info->path, &data, &len);
if (r) {
r = sc_lock(p15card->card);
SC_TEST_RET(p15card->card->ctx, r, "sc_lock() failed");
r = sc_select_file(p15card->card, &info->path, &file);
if (r) {
sc_unlock(p15card->card);
return r;
}
len = file->size;
sc_file_free(file);
data = malloc(len);
if (data == NULL) {
sc_unlock(p15card->card);
return SC_ERROR_OUT_OF_MEMORY;
}
r = sc_read_binary(p15card->card, 0, data, len, 0);
if (r < 0) {
sc_unlock(p15card->card);
free(data);
return r;
}
len = len;
sc_unlock(p15card->card);
}
pubkey = malloc(sizeof(struct sc_pkcs15_pubkey_rsa));
if (pubkey == NULL) {
free(data);
return SC_ERROR_OUT_OF_MEMORY;
}
memset(pubkey, 0, sizeof(struct sc_pkcs15_pubkey_rsa));
pubkey->data = data;
pubkey->data_len = len;
if (sc_pkcs15_parse_pubkey_rsa(p15card->card->ctx, pubkey)) {
free(data);
free(pubkey);
return SC_ERROR_INVALID_ASN1_OBJECT;
}
*out = pubkey;
return 0;
}

View File

@ -32,6 +32,7 @@ extern "C" {
#define SC_PKCS15_PIN_MAGIC 0x31415926
#define SC_PKCS15_MAX_PINS 2
#define SC_PKCS15_MAX_PRKEYS 2
#define SC_PKCS15_MAX_PUBKEYS 2
#define SC_PKCS15_MAX_LABEL_SIZE 32
#define SC_PKCS15_MAX_ID_SIZE 16
#define SC_PKCS15_MAX_DFS 4
@ -256,6 +257,12 @@ int sc_pkcs15_compute_signature(struct sc_pkcs15_card *p15card,
void sc_pkcs15_print_card(const struct sc_pkcs15_card *card);
int sc_pkcs15_read_pubkey(struct sc_pkcs15_card *card,
const struct sc_pkcs15_pubkey_info *info,
struct sc_pkcs15_pubkey_rsa **out);
int sc_pkcs15_parse_pubkey_rsa(struct sc_context *ctx,
struct sc_pkcs15_pubkey_rsa *pubkey);
void sc_pkcs15_print_cert_info(const struct sc_pkcs15_cert_info *cert);
int sc_pkcs15_read_certificate(struct sc_pkcs15_card *card,
const struct sc_pkcs15_cert_info *info,