2002-04-02 13:26:42 +00:00
|
|
|
|
/*
|
|
|
|
|
* MioCOS specific operation for PKCS15 initialization
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <string.h>
|
2002-04-05 13:48:00 +00:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <opensc/opensc.h>
|
|
|
|
|
#include <opensc/cardctl.h>
|
2002-04-02 13:26:42 +00:00
|
|
|
|
#include "pkcs15-init.h"
|
2002-04-04 20:42:29 +00:00
|
|
|
|
#include "profile.h"
|
2002-04-02 13:26:42 +00:00
|
|
|
|
|
2002-04-04 20:42:29 +00:00
|
|
|
|
/*
|
|
|
|
|
* Initialize the Application DF
|
|
|
|
|
*/
|
|
|
|
|
static int miocos_init_app(struct sc_profile *profile, struct sc_card *card,
|
2003-10-13 16:13:12 +00:00
|
|
|
|
struct sc_pkcs15_pin_info *pin_info,
|
2002-04-04 20:42:29 +00:00
|
|
|
|
const u8 *pin, size_t pin_len, const u8 *puk, size_t puk_len)
|
2002-04-02 13:26:42 +00:00
|
|
|
|
{
|
2002-04-04 20:42:29 +00:00
|
|
|
|
/* Create the application DF */
|
|
|
|
|
if (sc_pkcs15init_create_file(profile, card, profile->df_info->file))
|
|
|
|
|
return 1;
|
2002-04-02 13:26:42 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-04 20:42:29 +00:00
|
|
|
|
/*
|
|
|
|
|
* Store a PIN
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
miocos_new_pin(struct sc_profile *profile, struct sc_card *card,
|
|
|
|
|
struct sc_pkcs15_pin_info *info, unsigned int index,
|
|
|
|
|
const u8 *pin, size_t pin_len,
|
|
|
|
|
const u8 *puk, size_t puk_len)
|
2002-04-02 13:26:42 +00:00
|
|
|
|
{
|
2002-04-04 20:42:29 +00:00
|
|
|
|
struct sc_pkcs15_pin_info tmpinfo;
|
|
|
|
|
struct sc_cardctl_miocos_ac_info ac_info;
|
|
|
|
|
int r;
|
|
|
|
|
|
2002-04-19 18:01:49 +00:00
|
|
|
|
info->path = profile->df_info->file->path;
|
|
|
|
|
r = sc_select_file(card, &info->path, NULL);
|
2002-04-04 20:42:29 +00:00
|
|
|
|
if (r)
|
|
|
|
|
return r;
|
|
|
|
|
memset(&ac_info, 0, sizeof(ac_info));
|
|
|
|
|
info->reference = index + 1;
|
|
|
|
|
ac_info.ref = index + 1;
|
|
|
|
|
sc_profile_get_pin_info(profile, SC_PKCS15INIT_USER_PIN, &tmpinfo);
|
|
|
|
|
ac_info.max_tries = tmpinfo.tries_left;
|
|
|
|
|
sc_profile_get_pin_info(profile, SC_PKCS15INIT_USER_PUK, &tmpinfo);
|
|
|
|
|
ac_info.max_unblock_tries = tmpinfo.tries_left;
|
|
|
|
|
if (pin_len > 8)
|
|
|
|
|
pin_len = 8;
|
|
|
|
|
memcpy(ac_info.key_value, pin, pin_len);
|
|
|
|
|
if (puk_len > 8)
|
|
|
|
|
puk_len = 8;
|
2002-04-05 15:51:34 +00:00
|
|
|
|
strncpy((char *) ac_info.unblock_value, (const char *) puk, puk_len);
|
2002-04-04 20:42:29 +00:00
|
|
|
|
r = sc_card_ctl(card, SC_CARDCTL_MIOCOS_CREATE_AC, &ac_info);
|
|
|
|
|
if (r)
|
|
|
|
|
return r;
|
|
|
|
|
return 0;
|
2002-04-02 13:26:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2002-04-04 20:42:29 +00:00
|
|
|
|
* Allocate a file
|
2002-04-02 13:26:42 +00:00
|
|
|
|
*/
|
2002-04-04 20:42:29 +00:00
|
|
|
|
static int
|
|
|
|
|
miocos_new_file(struct sc_profile *profile, struct sc_card *card,
|
|
|
|
|
unsigned int type, unsigned int num,
|
|
|
|
|
struct sc_file **out)
|
2002-04-02 13:26:42 +00:00
|
|
|
|
{
|
2002-04-04 20:42:29 +00:00
|
|
|
|
struct sc_file *file;
|
|
|
|
|
struct sc_path *p;
|
|
|
|
|
char name[64], *tag, *desc;
|
|
|
|
|
|
|
|
|
|
desc = tag = NULL;
|
|
|
|
|
while (1) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case SC_PKCS15_TYPE_PRKEY_RSA:
|
|
|
|
|
desc = "RSA private key";
|
|
|
|
|
tag = "private-key";
|
|
|
|
|
break;
|
|
|
|
|
case SC_PKCS15_TYPE_PUBKEY_RSA:
|
|
|
|
|
desc = "RSA public key";
|
|
|
|
|
tag = "public-key";
|
|
|
|
|
break;
|
2002-04-19 17:02:49 +00:00
|
|
|
|
case SC_PKCS15_TYPE_PRKEY:
|
|
|
|
|
desc = "extractable private key";
|
|
|
|
|
tag = "extractable-key";
|
|
|
|
|
break;
|
2002-04-04 20:42:29 +00:00
|
|
|
|
case SC_PKCS15_TYPE_CERT:
|
|
|
|
|
desc = "certificate";
|
|
|
|
|
tag = "certificate";
|
|
|
|
|
break;
|
|
|
|
|
case SC_PKCS15_TYPE_DATA_OBJECT:
|
|
|
|
|
desc = "data object";
|
|
|
|
|
tag = "data";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (tag)
|
|
|
|
|
break;
|
|
|
|
|
/* If this is a specific type such as
|
|
|
|
|
* SC_PKCS15_TYPE_CERT_FOOBAR, fall back to
|
|
|
|
|
* the generic class (SC_PKCS15_TYPE_CERT)
|
|
|
|
|
*/
|
|
|
|
|
if (!(type & ~SC_PKCS15_TYPE_CLASS_MASK)) {
|
|
|
|
|
profile->cbs->error("File type not supported by card driver");
|
|
|
|
|
return SC_ERROR_INVALID_ARGUMENTS;
|
|
|
|
|
}
|
|
|
|
|
type &= SC_PKCS15_TYPE_CLASS_MASK;
|
2002-04-02 13:26:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-04 20:42:29 +00:00
|
|
|
|
snprintf(name, sizeof(name), "template-%s", tag);
|
|
|
|
|
if (sc_profile_get_file(profile, name, &file) < 0) {
|
|
|
|
|
profile->cbs->error("Profile doesn't define %s template (%s)\n",
|
|
|
|
|
desc, name);
|
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
2002-04-02 13:26:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-04 20:42:29 +00:00
|
|
|
|
/* Now construct file from template */
|
|
|
|
|
file->id += num;
|
|
|
|
|
|
|
|
|
|
p = &file->path;
|
|
|
|
|
*p = profile->df_info->file->path;
|
|
|
|
|
p->value[p->len++] = file->id >> 8;
|
|
|
|
|
p->value[p->len++] = file->id;
|
|
|
|
|
|
|
|
|
|
*out = file;
|
2002-04-02 13:26:42 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-04 20:42:29 +00:00
|
|
|
|
static int
|
|
|
|
|
miocos_update_private_key(struct sc_profile *profile, struct sc_card *card,
|
2002-04-15 13:42:10 +00:00
|
|
|
|
struct sc_pkcs15_prkey_rsa *rsa)
|
2002-04-04 20:42:29 +00:00
|
|
|
|
{
|
|
|
|
|
int r;
|
|
|
|
|
u8 buf[266];
|
|
|
|
|
|
|
|
|
|
memcpy(buf, "\x30\x82\x01\x06\x80\x81\x80", 7);
|
2002-04-15 13:42:10 +00:00
|
|
|
|
memcpy(buf + 7, rsa->modulus.data, 128);
|
2002-04-04 20:42:29 +00:00
|
|
|
|
memcpy(buf + 7 + 128, "\x82\x81\x80", 3);
|
2002-04-15 13:42:10 +00:00
|
|
|
|
memcpy(buf + 10 + 128, rsa->d.data, 128);
|
2002-04-04 20:42:29 +00:00
|
|
|
|
r = sc_update_binary(card, 0, buf, sizeof(buf), 0);
|
|
|
|
|
|
|
|
|
|
return r;
|
2002-04-02 13:26:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-04 20:42:29 +00:00
|
|
|
|
/*
|
|
|
|
|
* Store a private key
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
miocos_new_key(struct sc_profile *profile, struct sc_card *card,
|
2002-04-15 13:42:10 +00:00
|
|
|
|
struct sc_pkcs15_prkey *key, unsigned int index,
|
2002-04-04 20:42:29 +00:00
|
|
|
|
struct sc_pkcs15_prkey_info *info)
|
2002-04-02 13:26:42 +00:00
|
|
|
|
{
|
2002-04-04 20:42:29 +00:00
|
|
|
|
sc_file_t *keyfile;
|
2002-04-15 13:42:10 +00:00
|
|
|
|
struct sc_pkcs15_prkey_rsa *rsa;
|
2002-04-04 20:42:29 +00:00
|
|
|
|
int r;
|
|
|
|
|
|
2002-04-15 13:42:10 +00:00
|
|
|
|
if (key->algorithm != SC_ALGORITHM_RSA) {
|
2002-04-04 20:42:29 +00:00
|
|
|
|
profile->cbs->error("MioCOS supports only 1024-bit RSA keys.");
|
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
2002-04-15 13:42:10 +00:00
|
|
|
|
rsa = &key->u.rsa;
|
|
|
|
|
if (rsa->modulus.len != 128) {
|
2002-04-04 20:42:29 +00:00
|
|
|
|
profile->cbs->error("MioCOS supports only 1024-bit RSA keys.");
|
|
|
|
|
return SC_ERROR_NOT_SUPPORTED;
|
|
|
|
|
}
|
|
|
|
|
r = miocos_new_file(profile, card, SC_PKCS15_TYPE_PRKEY_RSA, index,
|
|
|
|
|
&keyfile);
|
2002-04-15 13:42:10 +00:00
|
|
|
|
if (r < 0)
|
2002-04-04 20:42:29 +00:00
|
|
|
|
return r;
|
2002-04-15 13:42:10 +00:00
|
|
|
|
|
2002-04-04 20:42:29 +00:00
|
|
|
|
info->modulus_length = 1024;
|
|
|
|
|
info->path = keyfile->path;
|
|
|
|
|
r = sc_pkcs15init_create_file(profile, card, keyfile);
|
|
|
|
|
sc_file_free(keyfile);
|
2002-04-15 13:42:10 +00:00
|
|
|
|
if (r < 0)
|
2002-04-04 20:42:29 +00:00
|
|
|
|
return r;
|
|
|
|
|
r = miocos_update_private_key(profile, card, rsa);
|
|
|
|
|
|
|
|
|
|
return r;
|
2002-04-02 13:26:42 +00:00
|
|
|
|
}
|
2002-04-04 20:42:29 +00:00
|
|
|
|
|
|
|
|
|
struct sc_pkcs15init_operations sc_pkcs15init_miocos_operations = {
|
2003-10-13 20:28:18 +00:00
|
|
|
|
NULL,
|
|
|
|
|
.init_app = miocos_init_app,
|
|
|
|
|
.new_pin = miocos_new_pin,
|
|
|
|
|
.new_key = miocos_new_key,
|
|
|
|
|
.new_file = miocos_new_file,
|
2002-04-04 20:42:29 +00:00
|
|
|
|
};
|