2002-03-03 00:32:28 +00:00
|
|
|
|
/*
|
|
|
|
|
* pkcs15-pubkey.c: PKCS #15 public key functions
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2002 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,
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2002-04-05 10:44:51 +00:00
|
|
|
|
#include "internal.h"
|
|
|
|
|
#include "pkcs15.h"
|
|
|
|
|
#include "asn1.h"
|
2002-03-03 00:32:28 +00:00
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
static const struct sc_asn1_entry c_asn1_com_key_attr[] = {
|
2005-08-05 07:24:43 +00:00
|
|
|
|
{ "iD", SC_ASN1_PKCS15_ID, ASN1_OCTET_STRING, 0, NULL, NULL },
|
|
|
|
|
{ "usage", SC_ASN1_BIT_FIELD, ASN1_BIT_STRING, 0, NULL, NULL },
|
|
|
|
|
{ "native", SC_ASN1_BOOLEAN, ASN1_BOOLEAN, SC_ASN1_OPTIONAL, NULL, NULL },
|
|
|
|
|
{ "accessFlags", SC_ASN1_BIT_FIELD, ASN1_BIT_STRING, SC_ASN1_OPTIONAL, NULL, NULL },
|
|
|
|
|
{ "keyReference",SC_ASN1_INTEGER, ASN1_INTEGER, SC_ASN1_OPTIONAL, NULL, NULL },
|
|
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
2002-03-03 00:32:28 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const struct sc_asn1_entry c_asn1_com_pubkey_attr[] = {
|
|
|
|
|
/* FIXME */
|
2005-08-05 07:24:43 +00:00
|
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
2002-03-03 00:32:28 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const struct sc_asn1_entry c_asn1_rsakey_attr[] = {
|
2005-08-05 07:24:43 +00:00
|
|
|
|
{ "value", SC_ASN1_PATH, ASN1_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
|
|
|
|
|
{ "modulusLength", SC_ASN1_INTEGER, ASN1_INTEGER, 0, NULL, NULL },
|
|
|
|
|
{ "keyInfo", SC_ASN1_INTEGER, ASN1_INTEGER, SC_ASN1_OPTIONAL, NULL, NULL },
|
|
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
2002-03-03 00:32:28 +00:00
|
|
|
|
};
|
|
|
|
|
|
2002-04-18 09:10:54 +00:00
|
|
|
|
static const struct sc_asn1_entry c_asn1_rsa_type_attr[] = {
|
2005-08-05 07:24:43 +00:00
|
|
|
|
{ "publicRSAKeyAttributes", SC_ASN1_STRUCT, ASN1_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
|
|
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
2002-03-03 00:32:28 +00:00
|
|
|
|
};
|
|
|
|
|
|
2002-04-18 09:10:54 +00:00
|
|
|
|
static const struct sc_asn1_entry c_asn1_dsakey_attr[] = {
|
2005-08-05 07:24:43 +00:00
|
|
|
|
{ "value", SC_ASN1_PATH, ASN1_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
|
|
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
2002-04-18 09:10:54 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const struct sc_asn1_entry c_asn1_dsa_type_attr[] = {
|
2005-08-05 07:24:43 +00:00
|
|
|
|
{ "publicDSAKeyAttributes", SC_ASN1_STRUCT, ASN1_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
|
|
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
2002-04-18 09:10:54 +00:00
|
|
|
|
};
|
|
|
|
|
|
2003-11-26 15:37:29 +00:00
|
|
|
|
static const struct sc_asn1_entry c_asn1_pubkey_choice[] = {
|
2005-08-05 07:24:43 +00:00
|
|
|
|
{ "publicRSAKey", SC_ASN1_PKCS15_OBJECT, ASN1_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
|
|
|
|
|
{ "publicDSAKey", SC_ASN1_PKCS15_OBJECT, 2 | SC_ASN1_CTX | SC_ASN1_CONS, 0, NULL, NULL },
|
|
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
2003-11-26 15:37:29 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const struct sc_asn1_entry c_asn1_pubkey[] = {
|
2005-08-05 07:24:43 +00:00
|
|
|
|
{ "publicKey", SC_ASN1_CHOICE, 0, 0, NULL, NULL },
|
|
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
2003-11-26 15:37:29 +00:00
|
|
|
|
};
|
2002-03-03 00:32:28 +00:00
|
|
|
|
|
|
|
|
|
int sc_pkcs15_decode_pukdf_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-03-03 00:32:28 +00:00
|
|
|
|
struct sc_pkcs15_pubkey_info info;
|
|
|
|
|
int r;
|
2002-05-26 12:31:23 +00:00
|
|
|
|
size_t usage_len = sizeof(info.usage);
|
|
|
|
|
size_t af_len = sizeof(info.access_flags);
|
2002-03-03 00:32:28 +00:00
|
|
|
|
struct sc_asn1_entry asn1_com_key_attr[6], asn1_com_pubkey_attr[1];
|
2002-04-18 09:10:54 +00:00
|
|
|
|
struct sc_asn1_entry asn1_rsakey_attr[4], asn1_rsa_type_attr[2];
|
|
|
|
|
struct sc_asn1_entry asn1_dsakey_attr[2], asn1_dsa_type_attr[2];
|
2003-11-26 15:37:29 +00:00
|
|
|
|
struct sc_asn1_entry asn1_pubkey_choice[3];
|
|
|
|
|
struct sc_asn1_entry asn1_pubkey[2];
|
2002-04-18 09:10:54 +00:00
|
|
|
|
struct sc_asn1_pkcs15_object rsakey_obj = { obj, asn1_com_key_attr,
|
|
|
|
|
asn1_com_pubkey_attr, asn1_rsa_type_attr };
|
|
|
|
|
struct sc_asn1_pkcs15_object dsakey_obj = { obj, asn1_com_key_attr,
|
|
|
|
|
asn1_com_pubkey_attr, asn1_dsa_type_attr };
|
2002-03-03 00:32:28 +00:00
|
|
|
|
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_pubkey, asn1_pubkey);
|
2003-11-26 15:37:29 +00:00
|
|
|
|
sc_copy_asn1_entry(c_asn1_pubkey_choice, asn1_pubkey_choice);
|
2002-04-18 09:10:54 +00:00
|
|
|
|
sc_copy_asn1_entry(c_asn1_rsa_type_attr, asn1_rsa_type_attr);
|
2002-03-03 00:32:28 +00:00
|
|
|
|
sc_copy_asn1_entry(c_asn1_rsakey_attr, asn1_rsakey_attr);
|
2002-04-18 09:10:54 +00:00
|
|
|
|
sc_copy_asn1_entry(c_asn1_dsa_type_attr, asn1_dsa_type_attr);
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_dsakey_attr, asn1_dsakey_attr);
|
2002-03-03 00:32:28 +00:00
|
|
|
|
sc_copy_asn1_entry(c_asn1_com_pubkey_attr, asn1_com_pubkey_attr);
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_com_key_attr, asn1_com_key_attr);
|
|
|
|
|
|
2003-11-26 15:37:29 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_pubkey_choice + 0, &rsakey_obj, NULL, 0);
|
|
|
|
|
sc_format_asn1_entry(asn1_pubkey_choice + 1, &dsakey_obj, NULL, 0);
|
2002-03-03 00:32:28 +00:00
|
|
|
|
|
2002-04-18 09:10:54 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_rsa_type_attr + 0, asn1_rsakey_attr, NULL, 0);
|
2002-03-03 00:32:28 +00:00
|
|
|
|
|
|
|
|
|
sc_format_asn1_entry(asn1_rsakey_attr + 0, &info.path, NULL, 0);
|
|
|
|
|
sc_format_asn1_entry(asn1_rsakey_attr + 1, &info.modulus_length, NULL, 0);
|
|
|
|
|
|
2002-04-18 09:10:54 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_dsa_type_attr + 0, asn1_dsakey_attr, NULL, 0);
|
|
|
|
|
|
|
|
|
|
sc_format_asn1_entry(asn1_dsakey_attr + 0, &info.path, NULL, 0);
|
|
|
|
|
|
2002-03-03 00:32:28 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_com_key_attr + 0, &info.id, NULL, 0);
|
|
|
|
|
sc_format_asn1_entry(asn1_com_key_attr + 1, &info.usage, &usage_len, 0);
|
|
|
|
|
sc_format_asn1_entry(asn1_com_key_attr + 2, &info.native, NULL, 0);
|
|
|
|
|
sc_format_asn1_entry(asn1_com_key_attr + 3, &info.access_flags, &af_len, 0);
|
|
|
|
|
sc_format_asn1_entry(asn1_com_key_attr + 4, &info.key_reference, NULL, 0);
|
|
|
|
|
|
2003-11-26 15:37:29 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_pubkey + 0, asn1_pubkey_choice, NULL, 0);
|
|
|
|
|
|
2002-03-03 00:32:28 +00:00
|
|
|
|
/* Fill in defaults */
|
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
|
info.key_reference = -1;
|
|
|
|
|
info.native = 1;
|
|
|
|
|
|
2003-11-26 15:37:29 +00:00
|
|
|
|
r = sc_asn1_decode(ctx, asn1_pubkey, *buf, *buflen, buf, buflen);
|
2002-03-03 00:32:28 +00:00
|
|
|
|
if (r == SC_ERROR_ASN1_END_OF_CONTENTS)
|
|
|
|
|
return r;
|
|
|
|
|
SC_TEST_RET(ctx, r, "ASN.1 decoding failed");
|
2003-11-26 15:37:29 +00:00
|
|
|
|
if (asn1_pubkey_choice[0].flags & SC_ASN1_PRESENT) {
|
2002-04-18 09:10:54 +00:00
|
|
|
|
obj->type = SC_PKCS15_TYPE_PUBKEY_RSA;
|
|
|
|
|
} else {
|
|
|
|
|
obj->type = SC_PKCS15_TYPE_PUBKEY_DSA;
|
|
|
|
|
}
|
2002-03-03 00:32:28 +00:00
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2005-03-08 20:59:35 +00:00
|
|
|
|
int sc_pkcs15_encode_pukdf_entry(sc_context_t *ctx,
|
2002-03-03 00:32:28 +00:00
|
|
|
|
const struct sc_pkcs15_object *obj,
|
|
|
|
|
u8 **buf, size_t *buflen)
|
|
|
|
|
{
|
|
|
|
|
struct sc_asn1_entry asn1_com_key_attr[6], asn1_com_pubkey_attr[1];
|
2002-04-18 09:10:54 +00:00
|
|
|
|
struct sc_asn1_entry asn1_rsakey_attr[4], asn1_rsa_type_attr[2];
|
|
|
|
|
struct sc_asn1_entry asn1_dsakey_attr[2], asn1_dsa_type_attr[2];
|
2003-11-26 15:37:29 +00:00
|
|
|
|
struct sc_asn1_entry asn1_pubkey_choice[3];
|
|
|
|
|
struct sc_asn1_entry asn1_pubkey[2];
|
2002-03-03 00:32:28 +00:00
|
|
|
|
struct sc_pkcs15_pubkey_info *pubkey =
|
|
|
|
|
(struct sc_pkcs15_pubkey_info *) obj->data;
|
2002-04-18 09:10:54 +00:00
|
|
|
|
struct sc_asn1_pkcs15_object rsakey_obj = { (struct sc_pkcs15_object *) obj,
|
|
|
|
|
asn1_com_key_attr,
|
|
|
|
|
asn1_com_pubkey_attr, asn1_rsa_type_attr };
|
|
|
|
|
struct sc_asn1_pkcs15_object dsakey_obj = { (struct sc_pkcs15_object *) obj,
|
2002-03-03 00:32:28 +00:00
|
|
|
|
asn1_com_key_attr,
|
2002-04-18 09:10:54 +00:00
|
|
|
|
asn1_com_pubkey_attr, asn1_dsa_type_attr };
|
2002-03-03 00:32:28 +00:00
|
|
|
|
int r;
|
2002-05-26 12:31:23 +00:00
|
|
|
|
size_t af_len, usage_len;
|
2002-03-03 00:32:28 +00:00
|
|
|
|
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_pubkey, asn1_pubkey);
|
2003-11-26 15:37:29 +00:00
|
|
|
|
sc_copy_asn1_entry(c_asn1_pubkey_choice, asn1_pubkey_choice);
|
2002-04-18 09:10:54 +00:00
|
|
|
|
sc_copy_asn1_entry(c_asn1_rsa_type_attr, asn1_rsa_type_attr);
|
2002-03-03 00:32:28 +00:00
|
|
|
|
sc_copy_asn1_entry(c_asn1_rsakey_attr, asn1_rsakey_attr);
|
2002-04-18 09:10:54 +00:00
|
|
|
|
sc_copy_asn1_entry(c_asn1_dsa_type_attr, asn1_dsa_type_attr);
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_dsakey_attr, asn1_dsakey_attr);
|
2002-03-03 00:32:28 +00:00
|
|
|
|
sc_copy_asn1_entry(c_asn1_com_pubkey_attr, asn1_com_pubkey_attr);
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_com_key_attr, asn1_com_key_attr);
|
|
|
|
|
|
2002-04-18 09:10:54 +00:00
|
|
|
|
switch (obj->type) {
|
|
|
|
|
case SC_PKCS15_TYPE_PUBKEY_RSA:
|
2003-11-26 15:37:29 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_pubkey_choice + 0, &rsakey_obj, NULL, 1);
|
2002-04-18 09:10:54 +00:00
|
|
|
|
|
|
|
|
|
sc_format_asn1_entry(asn1_rsa_type_attr + 0, asn1_rsakey_attr, NULL, 1);
|
|
|
|
|
|
|
|
|
|
sc_format_asn1_entry(asn1_rsakey_attr + 0, &pubkey->path, NULL, 1);
|
|
|
|
|
sc_format_asn1_entry(asn1_rsakey_attr + 1, &pubkey->modulus_length, NULL, 1);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SC_PKCS15_TYPE_PUBKEY_DSA:
|
2003-11-26 15:37:29 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_pubkey_choice + 1, &dsakey_obj, NULL, 1);
|
2002-03-03 00:32:28 +00:00
|
|
|
|
|
2002-04-18 09:10:54 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_dsa_type_attr + 0, asn1_dsakey_attr, NULL, 1);
|
2002-03-03 00:32:28 +00:00
|
|
|
|
|
2002-04-18 09:10:54 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_dsakey_attr + 0, &pubkey->path, NULL, 1);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(ctx, "Unsupported public key type: %X\n", obj->type);
|
2002-04-18 09:10:54 +00:00
|
|
|
|
SC_FUNC_RETURN(ctx, 0, SC_ERROR_INTERNAL);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-03-03 00:32:28 +00:00
|
|
|
|
|
|
|
|
|
sc_format_asn1_entry(asn1_com_key_attr + 0, &pubkey->id, NULL, 1);
|
2003-04-16 20:52:26 +00:00
|
|
|
|
usage_len = sizeof(pubkey->usage);
|
2002-03-03 00:32:28 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_com_key_attr + 1, &pubkey->usage, &usage_len, 1);
|
|
|
|
|
if (pubkey->native == 0)
|
|
|
|
|
sc_format_asn1_entry(asn1_com_key_attr + 2, &pubkey->native, NULL, 1);
|
|
|
|
|
if (pubkey->access_flags) {
|
2003-04-16 20:52:26 +00:00
|
|
|
|
af_len = sizeof(pubkey->access_flags);
|
2002-03-03 00:32:28 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_com_key_attr + 3, &pubkey->access_flags, &af_len, 1);
|
|
|
|
|
}
|
|
|
|
|
if (pubkey->key_reference >= 0)
|
|
|
|
|
sc_format_asn1_entry(asn1_com_key_attr + 4, &pubkey->key_reference, NULL, 1);
|
2003-11-26 15:37:29 +00:00
|
|
|
|
sc_format_asn1_entry(asn1_pubkey + 0, asn1_pubkey_choice, NULL, 1);
|
|
|
|
|
|
2002-03-03 00:32:28 +00:00
|
|
|
|
r = sc_asn1_encode(ctx, asn1_pubkey, buf, buflen);
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2002-03-08 19:47:26 +00:00
|
|
|
|
|
2002-04-15 13:42:10 +00:00
|
|
|
|
static struct sc_asn1_entry c_asn1_public_key[2] = {
|
2005-08-05 07:24:43 +00:00
|
|
|
|
{ "publicKeyCoefficients", SC_ASN1_STRUCT, ASN1_SEQUENCE | SC_ASN1_CONS, 0, NULL, NULL },
|
|
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
2002-04-15 13:42:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct sc_asn1_entry c_asn1_rsa_pub_coefficients[3] = {
|
2005-08-05 07:24:43 +00:00
|
|
|
|
{ "modulus", SC_ASN1_OCTET_STRING, ASN1_INTEGER, SC_ASN1_ALLOC|SC_ASN1_UNSIGNED, NULL, NULL },
|
|
|
|
|
{ "exponent", SC_ASN1_OCTET_STRING, ASN1_INTEGER, SC_ASN1_ALLOC|SC_ASN1_UNSIGNED, NULL, NULL },
|
|
|
|
|
{ NULL, 0, 0, 0, NULL, NULL }
|
2002-04-15 13:42:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct sc_asn1_entry c_asn1_dsa_pub_coefficients[5] = {
|
2005-08-05 07:24:43 +00:00
|
|
|
|
{ "publicKey",SC_ASN1_OCTET_STRING, ASN1_INTEGER, SC_ASN1_ALLOC|SC_ASN1_UNSIGNED, NULL, NULL },
|
|
|
|
|
{ "paramP", SC_ASN1_OCTET_STRING, ASN1_INTEGER, SC_ASN1_ALLOC|SC_ASN1_UNSIGNED, NULL, NULL },
|
|
|
|
|
{ "paramQ", SC_ASN1_OCTET_STRING, ASN1_INTEGER, SC_ASN1_ALLOC|SC_ASN1_UNSIGNED, NULL, NULL },
|
|
|
|
|
{ "paramG", SC_ASN1_OCTET_STRING, ASN1_INTEGER, SC_ASN1_ALLOC|SC_ASN1_UNSIGNED, NULL, NULL },
|
|
|
|
|
{ NULL, 0, 0, 0, NULL, NULL },
|
2002-04-15 13:42:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
2002-03-08 19:47:26 +00:00
|
|
|
|
int
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_pkcs15_decode_pubkey_rsa(sc_context_t *ctx,
|
2002-04-17 18:33:27 +00:00
|
|
|
|
struct sc_pkcs15_pubkey_rsa *key,
|
|
|
|
|
const u8 *buf, size_t buflen)
|
2002-03-08 19:47:26 +00:00
|
|
|
|
{
|
2002-04-15 13:42:10 +00:00
|
|
|
|
struct sc_asn1_entry asn1_public_key[2];
|
|
|
|
|
struct sc_asn1_entry asn1_rsa_coeff[3];
|
2002-03-08 19:47:26 +00:00
|
|
|
|
int r;
|
|
|
|
|
|
2002-04-15 13:42:10 +00:00
|
|
|
|
sc_copy_asn1_entry(c_asn1_public_key, asn1_public_key);
|
|
|
|
|
sc_format_asn1_entry(asn1_public_key + 0, asn1_rsa_coeff, NULL, 0);
|
|
|
|
|
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_rsa_pub_coefficients, asn1_rsa_coeff);
|
|
|
|
|
sc_format_asn1_entry(asn1_rsa_coeff + 0,
|
|
|
|
|
&key->modulus.data, &key->modulus.len, 0);
|
|
|
|
|
sc_format_asn1_entry(asn1_rsa_coeff + 1,
|
|
|
|
|
&key->exponent.data, &key->exponent.len, 0);
|
|
|
|
|
|
2002-04-17 18:33:27 +00:00
|
|
|
|
r = sc_asn1_decode(ctx, asn1_public_key, buf, buflen, NULL, NULL);
|
2003-01-20 09:56:27 +00:00
|
|
|
|
SC_TEST_RET(ctx, r, "ASN.1 parsing of public key failed");
|
2002-03-08 19:47:26 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-15 13:42:10 +00:00
|
|
|
|
int
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_pkcs15_encode_pubkey_rsa(sc_context_t *ctx,
|
2002-04-15 13:42:10 +00:00
|
|
|
|
struct sc_pkcs15_pubkey_rsa *key,
|
|
|
|
|
u8 **buf, size_t *buflen)
|
|
|
|
|
{
|
|
|
|
|
struct sc_asn1_entry asn1_public_key[2];
|
|
|
|
|
struct sc_asn1_entry asn1_rsa_pub_coeff[3];
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_public_key, asn1_public_key);
|
|
|
|
|
sc_format_asn1_entry(asn1_public_key + 0, asn1_rsa_pub_coeff, NULL, 1);
|
|
|
|
|
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_rsa_pub_coefficients, asn1_rsa_pub_coeff);
|
|
|
|
|
sc_format_asn1_entry(asn1_rsa_pub_coeff + 0,
|
|
|
|
|
key->modulus.data, &key->modulus.len, 1);
|
|
|
|
|
sc_format_asn1_entry(asn1_rsa_pub_coeff + 1,
|
|
|
|
|
key->exponent.data, &key->exponent.len, 1);
|
|
|
|
|
|
|
|
|
|
r = sc_asn1_encode(ctx, asn1_public_key, buf, buflen);
|
|
|
|
|
SC_TEST_RET(ctx, r, "ASN.1 encoding failed");
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-17 08:59:52 +00:00
|
|
|
|
int
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_pkcs15_decode_pubkey_dsa(sc_context_t *ctx,
|
2002-04-17 08:59:52 +00:00
|
|
|
|
struct sc_pkcs15_pubkey_dsa *key,
|
|
|
|
|
const u8 *buf, size_t buflen)
|
|
|
|
|
{
|
|
|
|
|
struct sc_asn1_entry asn1_public_key[2];
|
2002-04-18 10:58:48 +00:00
|
|
|
|
struct sc_asn1_entry asn1_dsa_pub_coeff[5];
|
2002-04-17 08:59:52 +00:00
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_public_key, asn1_public_key);
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_dsa_pub_coefficients, asn1_dsa_pub_coeff);
|
|
|
|
|
|
|
|
|
|
sc_format_asn1_entry(asn1_public_key + 0, asn1_dsa_pub_coeff, NULL, 1);
|
|
|
|
|
sc_format_asn1_entry(asn1_dsa_pub_coeff + 0,
|
|
|
|
|
&key->pub.data, &key->pub.len, 0);
|
|
|
|
|
sc_format_asn1_entry(asn1_dsa_pub_coeff + 1,
|
|
|
|
|
&key->g.data, &key->g.len, 0);
|
|
|
|
|
sc_format_asn1_entry(asn1_dsa_pub_coeff + 2,
|
|
|
|
|
&key->p.data, &key->p.len, 0);
|
|
|
|
|
sc_format_asn1_entry(asn1_dsa_pub_coeff + 3,
|
|
|
|
|
&key->q.data, &key->q.len, 0);
|
|
|
|
|
|
|
|
|
|
r = sc_asn1_decode(ctx, asn1_public_key, buf, buflen,
|
|
|
|
|
NULL, NULL);
|
|
|
|
|
SC_TEST_RET(ctx, r, "ASN.1 decoding failed");
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-15 13:42:10 +00:00
|
|
|
|
int
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_pkcs15_encode_pubkey_dsa(sc_context_t *ctx,
|
2002-04-15 13:42:10 +00:00
|
|
|
|
struct sc_pkcs15_pubkey_dsa *key,
|
|
|
|
|
u8 **buf, size_t *buflen)
|
|
|
|
|
{
|
|
|
|
|
struct sc_asn1_entry asn1_public_key[2];
|
2002-04-18 10:58:48 +00:00
|
|
|
|
struct sc_asn1_entry asn1_dsa_pub_coeff[5];
|
2002-04-15 13:42:10 +00:00
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_public_key, asn1_public_key);
|
|
|
|
|
sc_copy_asn1_entry(c_asn1_dsa_pub_coefficients, asn1_dsa_pub_coeff);
|
|
|
|
|
|
|
|
|
|
sc_format_asn1_entry(asn1_public_key + 0, asn1_dsa_pub_coeff, NULL, 1);
|
|
|
|
|
sc_format_asn1_entry(asn1_dsa_pub_coeff + 0,
|
|
|
|
|
key->pub.data, &key->pub.len, 1);
|
|
|
|
|
sc_format_asn1_entry(asn1_dsa_pub_coeff + 1,
|
|
|
|
|
key->g.data, &key->g.len, 1);
|
|
|
|
|
sc_format_asn1_entry(asn1_dsa_pub_coeff + 2,
|
|
|
|
|
key->p.data, &key->p.len, 1);
|
|
|
|
|
sc_format_asn1_entry(asn1_dsa_pub_coeff + 3,
|
|
|
|
|
key->q.data, &key->q.len, 1);
|
|
|
|
|
|
|
|
|
|
r = sc_asn1_encode(ctx, asn1_public_key, buf, buflen);
|
|
|
|
|
SC_TEST_RET(ctx, r, "ASN.1 encoding failed");
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_pkcs15_encode_pubkey(sc_context_t *ctx,
|
2002-04-15 13:42:10 +00:00
|
|
|
|
struct sc_pkcs15_pubkey *key,
|
|
|
|
|
u8 **buf, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
if (key->algorithm == SC_ALGORITHM_RSA)
|
|
|
|
|
return sc_pkcs15_encode_pubkey_rsa(ctx, &key->u.rsa, buf, len);
|
|
|
|
|
if (key->algorithm == SC_ALGORITHM_DSA)
|
|
|
|
|
return sc_pkcs15_encode_pubkey_dsa(ctx, &key->u.dsa, buf, len);
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(ctx, "Encoding of public key type %u not supported\n",
|
2002-04-15 13:42:10 +00:00
|
|
|
|
key->algorithm);
|
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-17 18:33:27 +00:00
|
|
|
|
int
|
2005-03-08 20:59:35 +00:00
|
|
|
|
sc_pkcs15_decode_pubkey(sc_context_t *ctx,
|
2002-04-17 18:33:27 +00:00
|
|
|
|
struct sc_pkcs15_pubkey *key,
|
|
|
|
|
const u8 *buf, size_t len)
|
|
|
|
|
{
|
|
|
|
|
if (key->algorithm == SC_ALGORITHM_RSA)
|
|
|
|
|
return sc_pkcs15_decode_pubkey_rsa(ctx, &key->u.rsa, buf, len);
|
|
|
|
|
if (key->algorithm == SC_ALGORITHM_DSA)
|
|
|
|
|
return sc_pkcs15_decode_pubkey_dsa(ctx, &key->u.dsa, buf, len);
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(ctx, "Decoding of public key type %u not supported\n",
|
2002-04-17 18:33:27 +00:00
|
|
|
|
key->algorithm);
|
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-15 13:42:10 +00:00
|
|
|
|
/*
|
|
|
|
|
* Read public key.
|
|
|
|
|
*/
|
2002-03-08 19:47:26 +00:00
|
|
|
|
int
|
|
|
|
|
sc_pkcs15_read_pubkey(struct sc_pkcs15_card *p15card,
|
2002-04-17 18:33:27 +00:00
|
|
|
|
const struct sc_pkcs15_object *obj,
|
|
|
|
|
struct sc_pkcs15_pubkey **out)
|
2002-03-08 19:47:26 +00:00
|
|
|
|
{
|
2002-04-17 18:33:27 +00:00
|
|
|
|
const struct sc_pkcs15_pubkey_info *info;
|
|
|
|
|
struct sc_pkcs15_pubkey *pubkey;
|
2002-03-08 19:47:26 +00:00
|
|
|
|
u8 *data;
|
|
|
|
|
size_t len;
|
2002-04-17 18:33:27 +00:00
|
|
|
|
int algorithm, r;
|
2002-03-08 19:47:26 +00:00
|
|
|
|
|
2002-04-17 18:33:27 +00:00
|
|
|
|
assert(p15card != NULL && obj != NULL && out != NULL);
|
2002-03-08 19:47:26 +00:00
|
|
|
|
SC_FUNC_CALLED(p15card->card->ctx, 1);
|
2002-04-17 08:59:52 +00:00
|
|
|
|
|
2002-04-17 18:33:27 +00:00
|
|
|
|
switch (obj->type) {
|
|
|
|
|
case SC_PKCS15_TYPE_PUBKEY_RSA:
|
|
|
|
|
algorithm = SC_ALGORITHM_RSA;
|
|
|
|
|
break;
|
|
|
|
|
case SC_PKCS15_TYPE_PUBKEY_DSA:
|
|
|
|
|
algorithm = SC_ALGORITHM_DSA;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(p15card->card->ctx, "Unsupported public key type.");
|
2002-04-17 18:33:27 +00:00
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
|
|
|
|
info = (const struct sc_pkcs15_pubkey_info *) obj->data;
|
|
|
|
|
|
2002-04-19 09:22:44 +00:00
|
|
|
|
r = sc_pkcs15_read_file(p15card, &info->path, &data, &len, NULL);
|
2002-04-17 08:59:52 +00:00
|
|
|
|
if (r < 0) {
|
2003-08-25 14:21:18 +00:00
|
|
|
|
sc_error(p15card->card->ctx, "Failed to read public key file.");
|
2002-04-17 08:59:52 +00:00
|
|
|
|
return r;
|
2002-03-08 19:47:26 +00:00
|
|
|
|
}
|
2002-04-17 08:59:52 +00:00
|
|
|
|
|
2004-12-12 21:51:51 +00:00
|
|
|
|
pubkey = (struct sc_pkcs15_pubkey *) calloc(1, sizeof(struct sc_pkcs15_pubkey));
|
2002-03-08 19:47:26 +00:00
|
|
|
|
if (pubkey == NULL) {
|
|
|
|
|
free(data);
|
|
|
|
|
return SC_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
}
|
2002-04-17 18:33:27 +00:00
|
|
|
|
pubkey->algorithm = algorithm;
|
2002-04-15 13:42:10 +00:00
|
|
|
|
pubkey->data.value = data;
|
|
|
|
|
pubkey->data.len = len;
|
2002-04-17 18:33:27 +00:00
|
|
|
|
if (sc_pkcs15_decode_pubkey(p15card->card->ctx, pubkey, data, len)) {
|
2002-03-08 19:47:26 +00:00
|
|
|
|
free(data);
|
|
|
|
|
free(pubkey);
|
|
|
|
|
return SC_ERROR_INVALID_ASN1_OBJECT;
|
|
|
|
|
}
|
|
|
|
|
*out = pubkey;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2002-03-15 10:01:25 +00:00
|
|
|
|
|
2002-04-17 18:33:27 +00:00
|
|
|
|
void sc_pkcs15_erase_pubkey(struct sc_pkcs15_pubkey *key)
|
2002-03-15 10:01:25 +00:00
|
|
|
|
{
|
|
|
|
|
assert(key != NULL);
|
2002-04-17 18:33:27 +00:00
|
|
|
|
switch (key->algorithm) {
|
|
|
|
|
case SC_ALGORITHM_RSA:
|
|
|
|
|
free(key->u.rsa.modulus.data);
|
2004-12-12 21:51:51 +00:00
|
|
|
|
free(key->u.rsa.exponent.data);
|
2002-04-17 18:33:27 +00:00
|
|
|
|
break;
|
|
|
|
|
case SC_ALGORITHM_DSA:
|
|
|
|
|
free(key->u.dsa.pub.data);
|
|
|
|
|
free(key->u.dsa.g.data);
|
|
|
|
|
free(key->u.dsa.p.data);
|
|
|
|
|
free(key->u.dsa.q.data);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-04-15 13:42:10 +00:00
|
|
|
|
free(key->data.value);
|
2005-09-17 10:44:45 +00:00
|
|
|
|
sc_mem_clear(key, sizeof(*key));
|
2002-04-17 18:33:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sc_pkcs15_free_pubkey(struct sc_pkcs15_pubkey *key)
|
|
|
|
|
{
|
|
|
|
|
sc_pkcs15_erase_pubkey(key);
|
2002-03-15 10:01:25 +00:00
|
|
|
|
free(key);
|
|
|
|
|
}
|
2004-12-18 14:14:57 +00:00
|
|
|
|
|
|
|
|
|
void sc_pkcs15_free_pubkey_info(sc_pkcs15_pubkey_info_t *key)
|
|
|
|
|
{
|
2004-12-27 13:22:06 +00:00
|
|
|
|
if (key->subject)
|
|
|
|
|
free(key->subject);
|
2004-12-18 14:14:57 +00:00
|
|
|
|
free(key);
|
|
|
|
|
}
|