#include "config.h"
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include "libopensc/opensc.h"
#include "libopensc/cardctl.h"
#include "libopensc/internal.h"
#include "libopensc/log.h"
#include "libopensc/cards.h"
#include "libopensc/asn1.h"
#include "pkcs15-init.h"
#include "profile.h"
static int openpgp_erase(struct sc_profile *profile, sc_pkcs15_card_t *p15card)
{
return SC_ERROR_NOT_SUPPORTED;
}
static int openpgp_create_dir(sc_profile_t *profile, sc_pkcs15_card_t *p15card,
sc_file_t *df)
{
return SC_ERROR_NOT_SUPPORTED;
}
static int openpgp_select_pin_reference(sc_profile_t *profile,
sc_pkcs15_card_t *p15card, sc_pkcs15_auth_info_t *auth_info)
{
return SC_ERROR_NOT_SUPPORTED;
}
static int openpgp_create_pin(sc_profile_t *profile, sc_pkcs15_card_t *p15card,
sc_file_t *df, sc_pkcs15_object_t *pin_obj,
const u8 *pin, size_t pin_len, const u8 *puk, size_t puk_len)
{
return SC_ERROR_NOT_SUPPORTED;
}
static int openpgp_create_key(sc_profile_t *profile, sc_pkcs15_card_t *p15card,
sc_pkcs15_object_t *obj)
{
LOG_FUNC_CALLED(p15card->card->ctx);
LOG_FUNC_RETURN(p15card->card->ctx, SC_SUCCESS);
}
static int openpgp_store_key(sc_profile_t *profile, sc_pkcs15_card_t *p15card,
sc_pkcs15_object_t *obj, sc_pkcs15_prkey_t *key)
{
sc_card_t *card = p15card->card;
sc_pkcs15_prkey_info_t *kinfo = (sc_pkcs15_prkey_info_t *) obj->data;
sc_cardctl_openpgp_keystore_info_t key_info;
int r;
LOG_FUNC_CALLED(card->ctx);
switch(obj->type)
{
case SC_PKCS15_TYPE_PRKEY_RSA:
memset(&key_info, 0, sizeof(sc_cardctl_openpgp_keystore_info_t));
key_info.algorithm = SC_OPENPGP_KEYALGO_RSA;
key_info.key_id = kinfo->id.value[0];
key_info.u.rsa.e = key->u.rsa.exponent.data;
key_info.u.rsa.e_len = key->u.rsa.exponent.len * 8;
key_info.u.rsa.p = key->u.rsa.p.data;
key_info.u.rsa.p_len = key->u.rsa.p.len;
key_info.u.rsa.q = key->u.rsa.q.data;
key_info.u.rsa.q_len = key->u.rsa.q.len;
key_info.u.rsa.n = key->u.rsa.modulus.data;
key_info.u.rsa.n_len = key->u.rsa.modulus.len * 8;
r = sc_card_ctl(card, SC_CARDCTL_OPENPGP_STORE_KEY, &key_info);
break;
case SC_PKCS15_TYPE_PRKEY_EC:
if (card->type < SC_CARD_TYPE_OPENPGP_V3) {
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "only RSA is supported on this card");
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
}
memset(&key_info, 0, sizeof(sc_cardctl_openpgp_keystore_info_t));
key_info.algorithm = (kinfo->id.value[0] == SC_OPENPGP_KEY_ENCR)
? SC_OPENPGP_KEYALGO_ECDH
: SC_OPENPGP_KEYALGO_ECDSA;
key_info.key_id = kinfo->id.value[0];
key_info.u.ec.privateD = key->u.ec.privateD.data;
key_info.u.ec.privateD_len = key->u.ec.privateD.len;
key_info.u.ec.ecpoint = key->u.ec.ecpointQ.value;
key_info.u.ec.ecpoint_len = key->u.ec.ecpointQ.len;
r = sc_card_ctl(card, SC_CARDCTL_OPENPGP_STORE_KEY, &key_info);
break;
default:
r = SC_ERROR_NOT_SUPPORTED;
sc_log(card->ctx, "%s: Key generation failed: Unknown/unsupported key type.", strerror(r));
}
LOG_FUNC_RETURN(card->ctx, r);
}
static int openpgp_generate_key_rsa(sc_card_t *card, sc_pkcs15_object_t *obj,
sc_pkcs15_pubkey_t *pubkey)
{
sc_context_t *ctx = card->ctx;
sc_cardctl_openpgp_keygen_info_t key_info;
sc_pkcs15_prkey_info_t *required = (sc_pkcs15_prkey_info_t *)obj->data;
sc_pkcs15_id_t *kid = &(required->id);
int r;
LOG_FUNC_CALLED(ctx);
memset(&key_info, 0, sizeof(key_info));
sc_log(ctx, "Key ID to be generated: %s", sc_dump_hex(kid->value, kid->len));
if (kid->len == 1 && kid->value[0] == 0x45) {
sc_log(ctx, "Authentication key is to be generated.");
key_info.key_id = 3;
}
if (!key_info.key_id && (kid->len > 1 || kid->value[0] > 3)) {
sc_log(ctx, "Key ID must be 1, 2 or 3!");
LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
}
if (!key_info.key_id)
key_info.key_id = kid->value[0];
if (obj->type != SC_PKCS15_TYPE_PRKEY_RSA) {
sc_log(card->ctx, "only RSA is currently supported");
return SC_ERROR_NOT_SUPPORTED;
}
key_info.algorithm = SC_OPENPGP_KEYALGO_RSA;
key_info.u.rsa.modulus_len = required->modulus_length;
key_info.u.rsa.modulus = calloc(required->modulus_length >> 3, 1);
if (key_info.u.rsa.modulus == NULL)
LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_ENOUGH_MEMORY);
key_info.u.rsa.exponent_len = 32;
key_info.u.rsa.exponent = calloc(BYTES4BITS(key_info.u.rsa.exponent_len), 1);
if (key_info.u.rsa.exponent == NULL) {
free(key_info.u.rsa.modulus);
LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_ENOUGH_MEMORY);
}
r = sc_card_ctl(card, SC_CARDCTL_OPENPGP_GENERATE_KEY, &key_info);
LOG_TEST_GOTO_ERR(card->ctx, r, "on-card EC key generation failed");
pubkey->algorithm = SC_ALGORITHM_RSA;
sc_log(ctx, "Set output modulus info");
pubkey->u.rsa.modulus.len = key_info.u.rsa.modulus_len;
pubkey->u.rsa.modulus.data = calloc(key_info.u.rsa.modulus_len, 1);
if (pubkey->u.rsa.modulus.data == NULL)
goto err;
memcpy(pubkey->u.rsa.modulus.data, key_info.u.rsa.modulus, key_info.u.rsa.modulus_len);
sc_log(ctx, "Set output exponent info");
pubkey->u.rsa.exponent.len = key_info.u.rsa.exponent_len;
pubkey->u.rsa.exponent.data = calloc(BYTES4BITS(key_info.u.rsa.exponent_len), 1);
if (pubkey->u.rsa.exponent.data == NULL)
goto err;
memcpy(pubkey->u.rsa.exponent.data, key_info.u.rsa.exponent, BYTES4BITS(key_info.u.rsa.exponent_len));
err:
free(key_info.u.rsa.modulus);
free(key_info.u.rsa.exponent);
LOG_FUNC_RETURN(ctx, r);
}
static int openpgp_generate_key_ec(sc_card_t *card, sc_pkcs15_object_t *obj,
sc_pkcs15_pubkey_t *pubkey)
{
sc_context_t *ctx = card->ctx;
sc_cardctl_openpgp_keygen_info_t key_info;
sc_pkcs15_prkey_info_t *required = (sc_pkcs15_prkey_info_t *)obj->data;
sc_pkcs15_id_t *kid = &(required->id);
const struct sc_ec_parameters *info_ec =
(struct sc_ec_parameters *) required->params.data;
unsigned int i;
int r;
LOG_FUNC_CALLED(ctx);
memset(&key_info, 0, sizeof(key_info));
sc_log(ctx, "Key ID to be generated: %s", sc_dump_hex(kid->value, kid->len));
if (kid->len == 1 && kid->value[0] == 0x45) {
sc_log(ctx, "Authentication key is to be generated.");
key_info.key_id = 3;
}
if (!key_info.key_id && (kid->len > 1 || kid->value[0] > 3)) {
sc_log(ctx, "Key ID must be 1, 2 or 3!");
LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
}
if (!key_info.key_id)
key_info.key_id = kid->value[0];
key_info.algorithm = (key_info.key_id == SC_OPENPGP_KEY_ENCR)
? SC_OPENPGP_KEYALGO_ECDH
: SC_OPENPGP_KEYALGO_ECDSA;
if (info_ec->der.len > 2)
key_info.u.ec.oid_len = info_ec->der.value[1];
else
LOG_FUNC_RETURN(ctx, SC_ERROR_INVALID_ARGUMENTS);
for (i=0; (i < key_info.u.ec.oid_len) && (i+2 < info_ec->der.len); i++){
key_info.u.ec.oid.value[i] = info_ec->der.value[i+2];
}
key_info.u.ec.oid.value[key_info.u.ec.oid_len] = -1;
key_info.u.ec.ecpoint_len = required->field_length;
key_info.u.ec.ecpoint = malloc(key_info.u.ec.ecpoint_len);
if (key_info.u.ec.ecpoint == NULL)
LOG_FUNC_RETURN(ctx, SC_ERROR_NOT_ENOUGH_MEMORY);
r = sc_card_ctl(card, SC_CARDCTL_OPENPGP_GENERATE_KEY, &key_info);
LOG_TEST_GOTO_ERR(card->ctx, r, "on-card EC key generation failed");
sc_log(ctx, "Set output ecpoint info");
pubkey->algorithm = SC_ALGORITHM_EC;
pubkey->u.ec.ecpointQ.len = key_info.u.ec.ecpoint_len;
pubkey->u.ec.ecpointQ.value = malloc(key_info.u.ec.ecpoint_len);
if (pubkey->u.ec.ecpointQ.value == NULL)
goto err;
memcpy(pubkey->u.ec.ecpointQ.value, key_info.u.ec.ecpoint, key_info.u.ec.ecpoint_len);
err:
if (key_info.u.ec.ecpoint)
free(key_info.u.ec.ecpoint);
LOG_FUNC_RETURN(ctx, r);
}
static int openpgp_generate_key(sc_profile_t *profile, sc_pkcs15_card_t *p15card,
sc_pkcs15_object_t *obj, sc_pkcs15_pubkey_t *pubkey)
{
sc_card_t *card = p15card->card;
sc_context_t *ctx = card->ctx;
int r;
LOG_FUNC_CALLED(ctx);
switch(obj->type)
{
case SC_PKCS15_TYPE_PRKEY_RSA:
r = openpgp_generate_key_rsa(card, obj, pubkey);
break;
case SC_PKCS15_TYPE_PRKEY_EC:
if (card->type < SC_CARD_TYPE_OPENPGP_V3) {
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "only RSA is supported on this card");
return SC_ERROR_NOT_SUPPORTED;
}
r = openpgp_generate_key_ec(card, obj, pubkey);
break;
default:
r = SC_ERROR_NOT_SUPPORTED;
sc_log(card->ctx, "%s: Key generation failed: Unknown/unsupported key type.", strerror(r));
}
LOG_FUNC_RETURN(ctx, r);
}
static int openpgp_emu_update_any_df(sc_profile_t *profile, sc_pkcs15_card_t *p15card,
unsigned operation, sc_pkcs15_object_t *obj)
{
LOG_FUNC_CALLED(p15card->card->ctx);
LOG_FUNC_RETURN(p15card->card->ctx, SC_SUCCESS);
}
static int openpgp_emu_update_tokeninfo(sc_profile_t *profile, sc_pkcs15_card_t *p15card,
sc_pkcs15_tokeninfo_t *tokeninfo)
{
LOG_FUNC_CALLED(p15card->card->ctx);
LOG_FUNC_RETURN(p15card->card->ctx, SC_SUCCESS);
}
static int openpgp_store_data(struct sc_pkcs15_card *p15card, struct sc_profile *profile,
struct sc_pkcs15_object *obj, struct sc_pkcs15_der *content,
struct sc_path *path)
{
sc_card_t *card = p15card->card;
sc_context_t *ctx = card->ctx;
sc_file_t *file;
sc_pkcs15_cert_info_t *cinfo;
sc_pkcs15_id_t *cid;
sc_pkcs15_data_info_t *dinfo;
u8 buf[254];
int r;
LOG_FUNC_CALLED(card->ctx);
switch (obj->type & SC_PKCS15_TYPE_CLASS_MASK) {
case SC_PKCS15_TYPE_PRKEY:
case SC_PKCS15_TYPE_PUBKEY:
r = SC_SUCCESS;
break;
case SC_PKCS15_TYPE_CERT:
cinfo = (sc_pkcs15_cert_info_t *) obj->data;
cid = &(cinfo->id);
if (cid->len != 1) {
sc_log(card->ctx, "ID=%s is not valid.", sc_dump_hex(cid->value, cid->len));
LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
}
if (cid->value[0] != 3) {
sc_log(card->ctx,
"This version does not support certificate ID = %d (only ID=3 is supported).",
cid->value[0]);
LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
}
sc_format_path("7F21", path);
r = sc_select_file(card, path, &file);
LOG_TEST_RET(card->ctx, r, "Cannot select cert file");
r = sc_pkcs15init_authenticate(profile, p15card, file, SC_AC_OP_UPDATE);
sc_log(card->ctx,
"Data to write is %"SC_FORMAT_LEN_SIZE_T"u long",
content->len);
if (r >= 0 && content->len)
r = sc_put_data(p15card->card, 0x7F21, (const unsigned char *) content->value, content->len);
break;
case SC_PKCS15_TYPE_DATA_OBJECT:
dinfo = (sc_pkcs15_data_info_t *) obj->data;
sc_log(ctx, "===== App label %s", dinfo->app_label);
sc_log(ctx, "About to write to DO 0101");
sc_format_path("0101", path);
r = sc_select_file(card, path, &file);
LOG_TEST_RET(card->ctx, r, "Cannot select private DO");
r = sc_read_binary(card, 0, buf, sizeof(buf), 0);
if (r < 0) {
sc_log(ctx, "Cannot read DO 0101");
break;
}
if (r > 0) {
sc_log(ctx, "DO 0101 is full.");
r = SC_ERROR_NOT_ENOUGH_MEMORY;
break;
}
r = sc_pkcs15init_authenticate(profile, p15card, file, SC_AC_OP_UPDATE);
if (r >= 0 && content->len) {
r = sc_update_binary(p15card->card, 0,
(const unsigned char *) content->value,
content->len, 0);
}
break;
default:
r = SC_ERROR_NOT_IMPLEMENTED;
}
LOG_FUNC_RETURN(card->ctx, r);
}
static struct sc_pkcs15init_operations sc_pkcs15init_openpgp_operations = {
openpgp_erase,
NULL,
openpgp_create_dir,
NULL,
openpgp_select_pin_reference,
openpgp_create_pin,
NULL,
openpgp_create_key,
openpgp_store_key,
openpgp_generate_key,
NULL, NULL,
NULL,
NULL,
NULL,
openpgp_emu_update_any_df,
openpgp_emu_update_tokeninfo,
NULL,
openpgp_store_data,
NULL
};
struct sc_pkcs15init_operations *sc_pkcs15init_get_openpgp_ops(void)
{
return &sc_pkcs15init_openpgp_operations;
}