2001-11-06 18:34:19 +00:00
|
|
|
|
/*
|
2002-01-17 11:44:27 +00:00
|
|
|
|
* pkcs15-cert.c: PKCS #15 certificate functions
|
2001-11-01 15:43:20 +00:00
|
|
|
|
*
|
2001-11-06 18:34:19 +00:00
|
|
|
|
* Copyright (C) 2001 Juha Yrj<EFBFBD>l<EFBFBD> <juha.yrjola@iki.fi>
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
2001-12-25 20:45:48 +00:00
|
|
|
|
#include "sc-internal.h"
|
2001-11-24 13:32:52 +00:00
|
|
|
|
#include "opensc-pkcs15.h"
|
2001-12-13 21:19:11 +00:00
|
|
|
|
#include "sc-log.h"
|
2001-11-06 18:34:19 +00:00
|
|
|
|
#include "sc-asn1.h"
|
2001-11-01 15:43:20 +00:00
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
2001-12-11 14:52:17 +00:00
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
2001-11-01 15:43:20 +00:00
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
2001-12-29 19:03:46 +00:00
|
|
|
|
static int parse_rsa_pubkey(struct sc_context *ctx, struct sc_pkcs15_rsa_pubkey *key)
|
2001-11-01 15:43:20 +00:00
|
|
|
|
{
|
2002-01-10 12:33:56 +00:00
|
|
|
|
struct sc_asn1_entry asn1_rsa_pubkey[] = {
|
2001-12-29 19:03:46 +00:00
|
|
|
|
{ "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");
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return SC_ERROR_INVALID_ASN1_OBJECT;
|
|
|
|
|
}
|
2002-01-07 18:23:34 +00:00
|
|
|
|
r = sc_asn1_decode(ctx, asn1_rsa_pubkey, obj, objlen, NULL, NULL);
|
2001-12-29 19:03:46 +00:00
|
|
|
|
SC_TEST_RET(ctx, r, "ASN.1 parsing failed");
|
|
|
|
|
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-12-29 19:03:46 +00:00
|
|
|
|
struct asn1_algorithm_id {
|
|
|
|
|
struct sc_object_id id;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int parse_algorithm_id(struct sc_context *ctx, void *arg, const u8 *obj,
|
|
|
|
|
size_t objlen, int depth)
|
2001-11-01 15:43:20 +00:00
|
|
|
|
{
|
2001-12-29 19:03:46 +00:00
|
|
|
|
struct asn1_algorithm_id *alg_id = (struct asn1_algorithm_id *) arg;
|
2002-01-10 12:33:56 +00:00
|
|
|
|
struct sc_asn1_entry asn1_alg_id[] = {
|
2001-12-29 19:03:46 +00:00
|
|
|
|
{ "algorithm", SC_ASN1_OBJECT, ASN1_OBJECT, 0, &alg_id->id },
|
|
|
|
|
{ "parameters", SC_ASN1_STRUCT, 0, SC_ASN1_OPTIONAL, NULL },
|
|
|
|
|
{ NULL }
|
|
|
|
|
};
|
|
|
|
|
int r;
|
|
|
|
|
|
2002-01-07 18:23:34 +00:00
|
|
|
|
r = sc_asn1_decode(ctx, asn1_alg_id, obj, objlen, NULL, NULL);
|
2001-12-29 19:03:46 +00:00
|
|
|
|
SC_TEST_RET(ctx, r, "ASN.1 parsing failed");
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int parse_x509_cert(struct sc_context *ctx, const u8 *buf, size_t buflen, struct sc_pkcs15_cert *cert)
|
|
|
|
|
{
|
|
|
|
|
int r;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
struct sc_pkcs15_rsa_pubkey *key = &cert->key;
|
2001-12-29 19:03:46 +00:00
|
|
|
|
struct asn1_algorithm_id pk_alg, sig_alg;
|
|
|
|
|
u8 *pk = NULL;
|
|
|
|
|
size_t pklen = 0;
|
2002-01-10 12:33:56 +00:00
|
|
|
|
struct sc_asn1_entry asn1_version[] = {
|
2001-12-29 19:03:46 +00:00
|
|
|
|
{ "version", SC_ASN1_INTEGER, ASN1_INTEGER, 0, &cert->version },
|
|
|
|
|
{ NULL }
|
|
|
|
|
};
|
2002-01-10 12:33:56 +00:00
|
|
|
|
struct sc_asn1_entry asn1_pkinfo[] = {
|
2001-12-30 21:17:34 +00:00
|
|
|
|
{ "algorithm", SC_ASN1_CALLBACK, ASN1_SEQUENCE | SC_ASN1_CONS, 0, (void *) parse_algorithm_id, &pk_alg },
|
2001-12-29 19:03:46 +00:00
|
|
|
|
{ "subjectPublicKey", SC_ASN1_BIT_STRING_NI, ASN1_BIT_STRING, SC_ASN1_ALLOC, &pk, &pklen },
|
|
|
|
|
{ NULL }
|
|
|
|
|
};
|
2002-01-10 12:33:56 +00:00
|
|
|
|
struct sc_asn1_entry asn1_tbscert[] = {
|
2001-12-29 19:03:46 +00:00
|
|
|
|
{ "version", SC_ASN1_STRUCT, SC_ASN1_CTX | 0 | SC_ASN1_CONS, 0, asn1_version },
|
|
|
|
|
{ "serialNumber", SC_ASN1_INTEGER, ASN1_INTEGER, 0, &cert->serial },
|
|
|
|
|
{ "signature", SC_ASN1_STRUCT, ASN1_SEQUENCE | SC_ASN1_CONS, 0, NULL },
|
|
|
|
|
{ "issuer", SC_ASN1_STRUCT, ASN1_SEQUENCE | SC_ASN1_CONS, 0, NULL },
|
|
|
|
|
{ "validity", SC_ASN1_STRUCT, ASN1_SEQUENCE | SC_ASN1_CONS, 0, NULL },
|
|
|
|
|
{ "subject", SC_ASN1_STRUCT, ASN1_SEQUENCE | SC_ASN1_CONS, 0, NULL },
|
|
|
|
|
{ "subjectPublicKeyInfo",SC_ASN1_STRUCT, ASN1_SEQUENCE | SC_ASN1_CONS, 0, asn1_pkinfo },
|
|
|
|
|
{ NULL }
|
|
|
|
|
};
|
2002-01-10 12:33:56 +00:00
|
|
|
|
struct sc_asn1_entry asn1_cert[] = {
|
2001-12-29 19:03:46 +00:00
|
|
|
|
{ "tbsCertificate", SC_ASN1_STRUCT, ASN1_SEQUENCE | SC_ASN1_CONS, 0, asn1_tbscert },
|
2001-12-30 21:17:34 +00:00
|
|
|
|
{ "signatureAlgorithm", SC_ASN1_CALLBACK, ASN1_SEQUENCE | SC_ASN1_CONS, 0, (void *) parse_algorithm_id, &sig_alg },
|
2001-12-29 19:03:46 +00:00
|
|
|
|
{ "signatureValue", SC_ASN1_BIT_STRING,ASN1_BIT_STRING, 0, NULL, 0 },
|
|
|
|
|
{ NULL }
|
|
|
|
|
};
|
|
|
|
|
const u8 *obj;
|
|
|
|
|
size_t objlen;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2001-12-29 19:03:46 +00:00
|
|
|
|
obj = sc_asn1_verify_tag(ctx, buf, buflen, ASN1_SEQUENCE | SC_ASN1_CONS,
|
|
|
|
|
&objlen);
|
|
|
|
|
if (obj == NULL) {
|
|
|
|
|
error(ctx, "X.509 certificate not found\n");
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return SC_ERROR_INVALID_ASN1_OBJECT;
|
|
|
|
|
}
|
2001-12-29 19:03:46 +00:00
|
|
|
|
cert->data_len = objlen + (obj - buf);
|
2002-01-07 18:23:34 +00:00
|
|
|
|
r = sc_asn1_decode(ctx, asn1_cert, obj, objlen, NULL, NULL);
|
2001-12-29 19:03:46 +00:00
|
|
|
|
SC_TEST_RET(ctx, r, "ASN.1 parsing failed");
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2001-12-29 19:03:46 +00:00
|
|
|
|
cert->version++;
|
|
|
|
|
pklen >>= 3; /* convert number of bits to bytes */
|
|
|
|
|
key->data = pk;
|
|
|
|
|
key->data_len = pklen;
|
|
|
|
|
/* FIXME: ignore the object id for now, and presume it's RSA */
|
|
|
|
|
r = parse_rsa_pubkey(ctx, key);
|
2001-11-05 19:39:18 +00:00
|
|
|
|
if (r) {
|
2001-12-29 19:03:46 +00:00
|
|
|
|
free(key->data);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return SC_ERROR_INVALID_ASN1_OBJECT;
|
2001-11-05 19:39:18 +00:00
|
|
|
|
}
|
2001-12-29 19:03:46 +00:00
|
|
|
|
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sc_pkcs15_read_certificate(struct sc_pkcs15_card *p15card,
|
|
|
|
|
const struct sc_pkcs15_cert_info *info,
|
|
|
|
|
struct sc_pkcs15_cert **cert_out)
|
|
|
|
|
{
|
2002-01-24 16:02:54 +00:00
|
|
|
|
int r;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
struct sc_pkcs15_cert *cert;
|
2002-02-20 09:56:47 +00:00
|
|
|
|
struct sc_file *file = NULL;
|
2002-01-24 16:02:54 +00:00
|
|
|
|
u8 *data = NULL;
|
|
|
|
|
size_t len;
|
|
|
|
|
|
2001-11-01 15:43:20 +00:00
|
|
|
|
assert(p15card != NULL && info != NULL && cert_out != NULL);
|
2001-12-21 23:34:47 +00:00
|
|
|
|
SC_FUNC_CALLED(p15card->card->ctx, 1);
|
2002-01-24 16:02:54 +00:00
|
|
|
|
r = sc_pkcs15_read_cached_file(p15card, &info->path, &data, &len);
|
2001-12-11 14:52:17 +00:00
|
|
|
|
if (r) {
|
2001-12-25 20:45:48 +00:00
|
|
|
|
r = sc_lock(p15card->card);
|
|
|
|
|
SC_TEST_RET(p15card->card->ctx, r, "sc_lock() failed");
|
2001-12-22 23:51:12 +00:00
|
|
|
|
r = sc_select_file(p15card->card, &info->path, &file);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
if (r) {
|
|
|
|
|
sc_unlock(p15card->card);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return r;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
}
|
2002-02-20 09:56:47 +00:00
|
|
|
|
len = file->size;
|
|
|
|
|
sc_file_free(file);
|
|
|
|
|
data = malloc(len);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
if (data == NULL) {
|
|
|
|
|
sc_unlock(p15card->card);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
return SC_ERROR_OUT_OF_MEMORY;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
}
|
2002-02-20 09:56:47 +00:00
|
|
|
|
r = sc_read_binary(p15card->card, 0, data, len, 0);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
if (r < 0) {
|
2001-12-25 20:45:48 +00:00
|
|
|
|
sc_unlock(p15card->card);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
free(data);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2002-02-20 09:56:47 +00:00
|
|
|
|
len = len;
|
2001-12-25 20:45:48 +00:00
|
|
|
|
sc_unlock(p15card->card);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
cert = malloc(sizeof(struct sc_pkcs15_cert));
|
|
|
|
|
if (cert == NULL) {
|
|
|
|
|
free(data);
|
|
|
|
|
return SC_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
memset(cert, 0, sizeof(struct sc_pkcs15_cert));
|
2001-12-29 19:03:46 +00:00
|
|
|
|
if (parse_x509_cert(p15card->card->ctx, data, len, cert)) {
|
2001-11-01 15:43:20 +00:00
|
|
|
|
free(data);
|
|
|
|
|
free(cert);
|
|
|
|
|
return SC_ERROR_INVALID_ASN1_OBJECT;
|
|
|
|
|
}
|
|
|
|
|
cert->data = data;
|
|
|
|
|
*cert_out = cert;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-01-10 12:33:56 +00:00
|
|
|
|
static const struct sc_asn1_entry c_asn1_cred_ident[] = {
|
|
|
|
|
{ "idType", SC_ASN1_INTEGER, ASN1_INTEGER, 0, NULL },
|
|
|
|
|
{ "idValue", SC_ASN1_OCTET_STRING, ASN1_OCTET_STRING, 0, NULL },
|
|
|
|
|
{ NULL }
|
|
|
|
|
};
|
|
|
|
|
static const struct sc_asn1_entry c_asn1_com_cert_attr[] = {
|
|
|
|
|
{ "iD", SC_ASN1_PKCS15_ID, ASN1_OCTET_STRING, 0, NULL },
|
|
|
|
|
{ "authority", SC_ASN1_BOOLEAN, ASN1_BOOLEAN, SC_ASN1_OPTIONAL, NULL },
|
|
|
|
|
{ "identifier", SC_ASN1_STRUCT, ASN1_SEQUENCE | SC_ASN1_CONS, SC_ASN1_OPTIONAL, NULL },
|
|
|
|
|
/* FIXME: Add rest of the optional fields */
|
|
|
|
|
{ NULL }
|
|
|
|
|
};
|
|
|
|
|
static const struct sc_asn1_entry c_asn1_x509_cert_attr[] = {
|
|
|
|
|
{ "value", SC_ASN1_PATH, ASN1_SEQUENCE | SC_ASN1_CONS, 0, NULL },
|
|
|
|
|
{ NULL }
|
|
|
|
|
};
|
|
|
|
|
static const struct sc_asn1_entry c_asn1_type_cert_attr[] = {
|
|
|
|
|
{ "x509CertificateAttributes", SC_ASN1_STRUCT, ASN1_SEQUENCE | SC_ASN1_CONS, 0, NULL },
|
|
|
|
|
{ NULL }
|
|
|
|
|
};
|
|
|
|
|
static const struct sc_asn1_entry c_asn1_cert[] = {
|
|
|
|
|
{ "x509Certificate", SC_ASN1_PKCS15_OBJECT, ASN1_SEQUENCE | SC_ASN1_CONS, 0, NULL },
|
|
|
|
|
{ NULL }
|
|
|
|
|
};
|
|
|
|
|
|
2002-01-24 16:02:54 +00:00
|
|
|
|
int sc_pkcs15_decode_cdf_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
|
|
|
|
{
|
2002-01-24 16:02:54 +00:00
|
|
|
|
struct sc_context *ctx = p15card->card->ctx;
|
|
|
|
|
struct sc_pkcs15_cert_info info;
|
2002-01-10 12:33:56 +00:00
|
|
|
|
struct sc_asn1_entry asn1_cred_ident[3], asn1_com_cert_attr[4],
|
|
|
|
|
asn1_x509_cert_attr[2], asn1_type_cert_attr[2],
|
|
|
|
|
asn1_cert[2];
|
2002-01-24 16:02:54 +00:00
|
|
|
|
struct sc_asn1_pkcs15_object cert_obj = { &info.com_attr, asn1_com_cert_attr, NULL,
|
2002-01-10 12:33:56 +00:00
|
|
|
|
asn1_type_cert_attr };
|
2001-12-19 21:58:04 +00:00
|
|
|
|
u8 id_value[128];
|
|
|
|
|
int id_type, id_value_len = sizeof(id_value);
|
|
|
|
|
int r;
|
2002-01-10 12:33:56 +00:00
|
|
|
|
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_cred_ident, asn1_cred_ident);
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_com_cert_attr, asn1_com_cert_attr);
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_x509_cert_attr, asn1_x509_cert_attr);
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_type_cert_attr, asn1_type_cert_attr);
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_cert, asn1_cert);
|
|
|
|
|
|
|
|
|
|
sc_format_asn1_entry(asn1_cred_ident + 0, &id_type, NULL, 0);
|
|
|
|
|
sc_format_asn1_entry(asn1_cred_ident + 1, &id_value, &id_value_len, 0);
|
2002-01-24 16:02:54 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_com_cert_attr + 0, &info.id, NULL, 0);
|
|
|
|
|
sc_format_asn1_entry(asn1_com_cert_attr + 1, &info.authority, NULL, 0);
|
2002-01-10 12:33:56 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_com_cert_attr + 2, asn1_cred_ident, NULL, 0);
|
2002-01-24 16:02:54 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_x509_cert_attr + 0, &info.path, NULL, 0);
|
2002-01-10 12:33:56 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_type_cert_attr + 0, asn1_x509_cert_attr, NULL, 0);
|
|
|
|
|
sc_format_asn1_entry(asn1_cert + 0, &cert_obj, NULL, 0);
|
2001-12-19 21:58:04 +00:00
|
|
|
|
|
2002-01-24 16:02:54 +00:00
|
|
|
|
/* Fill in defaults */
|
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
|
info.authority = 0;
|
|
|
|
|
|
2002-01-07 18:23:34 +00:00
|
|
|
|
r = sc_asn1_decode(ctx, asn1_cert, *buf, *buflen, buf, buflen);
|
2002-01-24 16:02:54 +00:00
|
|
|
|
if (r == SC_ERROR_ASN1_END_OF_CONTENTS)
|
|
|
|
|
return r;
|
|
|
|
|
SC_TEST_RET(ctx, r, "ASN.1 decoding failed");
|
|
|
|
|
obj->type = SC_PKCS15_TYPE_CERT_X509;
|
|
|
|
|
obj->data = malloc(sizeof(info));
|
|
|
|
|
if (obj->data == NULL)
|
|
|
|
|
SC_FUNC_RETURN(ctx, 0, SC_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
memcpy(obj->data, &info, sizeof(info));
|
|
|
|
|
|
|
|
|
|
/* Legacy code */
|
|
|
|
|
if (p15card->cert_count >= SC_PKCS15_MAX_CERTS)
|
|
|
|
|
return SC_ERROR_TOO_MANY_OBJECTS;
|
|
|
|
|
p15card->cert_info[p15card->cert_count] = info;
|
|
|
|
|
p15card->cert_count++;
|
2001-12-19 21:58:04 +00:00
|
|
|
|
|
2002-01-24 16:02:54 +00:00
|
|
|
|
return 0;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-01-17 23:47:03 +00:00
|
|
|
|
int sc_pkcs15_encode_cdf_entry(struct sc_context *ctx,
|
2002-01-16 23:59:18 +00:00
|
|
|
|
const struct sc_pkcs15_object *obj,
|
|
|
|
|
u8 **buf, size_t *bufsize)
|
2002-01-10 12:33:56 +00:00
|
|
|
|
{
|
|
|
|
|
struct sc_asn1_entry asn1_cred_ident[3], asn1_com_cert_attr[4],
|
|
|
|
|
asn1_x509_cert_attr[2], asn1_type_cert_attr[2],
|
|
|
|
|
asn1_cert[2];
|
2002-01-16 23:59:18 +00:00
|
|
|
|
struct sc_pkcs15_cert_info *infop =
|
|
|
|
|
(struct sc_pkcs15_cert_info *) obj->data;
|
|
|
|
|
const struct sc_asn1_pkcs15_object cert_obj = { &infop->com_attr, asn1_com_cert_attr, NULL,
|
2002-01-10 12:33:56 +00:00
|
|
|
|
asn1_type_cert_attr };
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_cred_ident, asn1_cred_ident);
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_com_cert_attr, asn1_com_cert_attr);
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_x509_cert_attr, asn1_x509_cert_attr);
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_type_cert_attr, asn1_type_cert_attr);
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_cert, asn1_cert);
|
|
|
|
|
|
2002-01-16 23:59:18 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_com_cert_attr + 0, (void *) &infop->id, NULL, 1);
|
|
|
|
|
if (infop->authority)
|
|
|
|
|
sc_format_asn1_entry(asn1_com_cert_attr + 1, (void *) &infop->authority, NULL, 1);
|
|
|
|
|
sc_format_asn1_entry(asn1_x509_cert_attr + 0, (void *) &infop->path, NULL, 1);
|
2002-01-10 12:33:56 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_type_cert_attr + 0, (void *) asn1_x509_cert_attr, NULL, 1);
|
|
|
|
|
sc_format_asn1_entry(asn1_cert + 0, (void *) &cert_obj, NULL, 1);
|
|
|
|
|
|
2002-01-17 23:47:03 +00:00
|
|
|
|
r = sc_asn1_encode(ctx, asn1_cert, buf, bufsize);
|
2002-01-10 12:33:56 +00:00
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-01 15:43:20 +00:00
|
|
|
|
void sc_pkcs15_print_cert_info(const struct sc_pkcs15_cert_info *cert)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
printf("X.509 Certificate [%s]\n", cert->com_attr.label);
|
|
|
|
|
printf("\tFlags : %d\n", cert->com_attr.flags);
|
2001-11-26 16:14:22 +00:00
|
|
|
|
printf("\tAuthority: %s\n", cert->authority ? "yes" : "no");
|
2001-11-01 15:43:20 +00:00
|
|
|
|
printf("\tPath : ");
|
|
|
|
|
for (i = 0; i < cert->path.len; i++)
|
|
|
|
|
printf("%02X", cert->path.value[i]);
|
|
|
|
|
printf("\n");
|
|
|
|
|
printf("\tID : ");
|
|
|
|
|
sc_pkcs15_print_id(&cert->id);
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
2002-01-24 16:02:54 +00:00
|
|
|
|
int sc_pkcs15_enum_certificates(struct sc_pkcs15_card *p15card)
|
2001-11-01 15:43:20 +00:00
|
|
|
|
{
|
2002-01-24 16:02:54 +00:00
|
|
|
|
int r = 0, i, j;
|
2002-01-13 23:56:13 +00:00
|
|
|
|
const int df_types[] = {
|
|
|
|
|
SC_PKCS15_CDF, SC_PKCS15_CDF_TRUSTED, SC_PKCS15_CDF_USEFUL
|
|
|
|
|
};
|
|
|
|
|
const int nr_types = sizeof(df_types)/sizeof(df_types[0]);
|
|
|
|
|
|
2002-01-24 16:02:54 +00:00
|
|
|
|
assert(p15card != NULL);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
|
2002-01-24 16:02:54 +00:00
|
|
|
|
if (p15card->cert_count)
|
|
|
|
|
return p15card->cert_count; /* already enumerated */
|
|
|
|
|
r = sc_lock(p15card->card);
|
|
|
|
|
SC_TEST_RET(p15card->card->ctx, r, "sc_lock() failed");
|
2002-01-13 23:56:13 +00:00
|
|
|
|
for (j = 0; j < nr_types; j++) {
|
2002-01-24 16:02:54 +00:00
|
|
|
|
int type = df_types[j];
|
|
|
|
|
for (i = 0; i < p15card->df[type].count; i++) {
|
|
|
|
|
r = sc_pkcs15_parse_df(p15card, &p15card->df[type], i);
|
2002-01-13 23:56:13 +00:00
|
|
|
|
if (r != 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2001-11-01 15:43:20 +00:00
|
|
|
|
if (r != 0)
|
2001-12-25 20:45:48 +00:00
|
|
|
|
break;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
2002-01-24 16:02:54 +00:00
|
|
|
|
sc_unlock(p15card->card);
|
2001-12-25 20:45:48 +00:00
|
|
|
|
if (r != 0)
|
|
|
|
|
return r;
|
2002-01-24 16:02:54 +00:00
|
|
|
|
return p15card->cert_count;
|
2001-11-01 15:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sc_pkcs15_free_certificate(struct sc_pkcs15_cert *cert)
|
|
|
|
|
{
|
|
|
|
|
assert(cert != NULL);
|
2001-11-05 19:39:18 +00:00
|
|
|
|
|
|
|
|
|
free(cert->key.data);
|
2001-12-20 13:57:58 +00:00
|
|
|
|
free(cert->key.modulus);
|
2001-11-01 15:43:20 +00:00
|
|
|
|
free(cert->data);
|
|
|
|
|
free(cert);
|
|
|
|
|
}
|
2001-11-21 21:19:58 +00:00
|
|
|
|
|
|
|
|
|
int sc_pkcs15_find_cert_by_id(struct sc_pkcs15_card *card,
|
|
|
|
|
const struct sc_pkcs15_id *id,
|
|
|
|
|
struct sc_pkcs15_cert_info **cert_out)
|
|
|
|
|
{
|
|
|
|
|
int r, i;
|
|
|
|
|
|
|
|
|
|
r = sc_pkcs15_enum_certificates(card);
|
|
|
|
|
if (r < 0)
|
|
|
|
|
return r;
|
|
|
|
|
for (i = 0; i < card->cert_count; i++) {
|
|
|
|
|
struct sc_pkcs15_cert_info *cert = &card->cert_info[i];
|
|
|
|
|
if (sc_pkcs15_compare_id(&cert->id, id) == 1) {
|
|
|
|
|
*cert_out = cert;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return SC_ERROR_OBJECT_NOT_FOUND;
|
|
|
|
|
}
|