2001-07-04 13:35:00 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001 Markus Friedl. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
2001-09-18 12:01:11 +08:00
|
|
|
#ifdef SMARTCARD
|
2002-03-22 11:33:43 +08:00
|
|
|
RCSID("$OpenBSD: scard.c,v 1.20 2002/03/21 16:57:15 markus Exp $");
|
2001-07-04 13:35:00 +08:00
|
|
|
#include <openssl/engine.h>
|
2002-03-22 11:33:43 +08:00
|
|
|
#include <openssl/evp.h>
|
2001-07-04 13:35:00 +08:00
|
|
|
#include <sectok.h>
|
|
|
|
|
|
|
|
#include "key.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "xmalloc.h"
|
|
|
|
#include "scard.h"
|
2002-03-22 11:33:43 +08:00
|
|
|
#include "readpass.h"
|
2001-07-04 13:35:00 +08:00
|
|
|
|
2002-03-22 11:28:11 +08:00
|
|
|
#ifdef OPENSSL_VERSION_NUMBER
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
|
|
|
|
#define RSA_get_default_openssl_method RSA_get_default_method
|
|
|
|
#define DSA_get_default_openssl_method DSA_get_default_method
|
|
|
|
#define DH_get_default_openssl_method DH_get_default_method
|
|
|
|
#define ENGINE_set_BN_mod_exp(x,y)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2001-07-04 13:35:00 +08:00
|
|
|
#define CLA_SSH 0x05
|
|
|
|
#define INS_DECRYPT 0x10
|
|
|
|
#define INS_GET_KEYLENGTH 0x20
|
|
|
|
#define INS_GET_PUBKEY 0x30
|
|
|
|
#define INS_GET_RESPONSE 0xc0
|
|
|
|
|
|
|
|
#define MAX_BUF_SIZE 256
|
|
|
|
|
|
|
|
static int sc_fd = -1;
|
2001-08-07 05:35:51 +08:00
|
|
|
static char *sc_reader_id = NULL;
|
2001-07-04 13:35:00 +08:00
|
|
|
static int cla = 0x00; /* class */
|
|
|
|
|
|
|
|
/* interface to libsectok */
|
|
|
|
|
2001-12-21 11:45:46 +08:00
|
|
|
static int
|
2001-07-14 10:13:26 +08:00
|
|
|
sc_open(void)
|
2001-07-04 13:35:00 +08:00
|
|
|
{
|
|
|
|
int sw;
|
|
|
|
|
|
|
|
if (sc_fd >= 0)
|
|
|
|
return sc_fd;
|
|
|
|
|
2001-08-07 05:35:51 +08:00
|
|
|
sc_fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw);
|
2001-07-04 13:35:00 +08:00
|
|
|
if (sc_fd < 0) {
|
2001-07-14 10:13:26 +08:00
|
|
|
error("sectok_open failed: %s", sectok_get_sw(sw));
|
2001-08-07 05:22:10 +08:00
|
|
|
return SCARD_ERROR_FAIL;
|
|
|
|
}
|
|
|
|
if (! sectok_cardpresent(sc_fd)) {
|
2001-08-07 05:35:51 +08:00
|
|
|
debug("smartcard in reader %s not present, skipping",
|
|
|
|
sc_reader_id);
|
2001-08-07 05:33:44 +08:00
|
|
|
sc_close();
|
2001-08-07 05:22:10 +08:00
|
|
|
return SCARD_ERROR_NOCARD;
|
2001-07-04 13:35:00 +08:00
|
|
|
}
|
2001-08-07 05:10:52 +08:00
|
|
|
if (sectok_reset(sc_fd, 0, NULL, &sw) <= 0) {
|
2001-07-04 13:35:00 +08:00
|
|
|
error("sectok_reset failed: %s", sectok_get_sw(sw));
|
|
|
|
sc_fd = -1;
|
2001-08-07 05:22:10 +08:00
|
|
|
return SCARD_ERROR_FAIL;
|
2001-07-04 13:35:00 +08:00
|
|
|
}
|
2001-08-07 05:10:52 +08:00
|
|
|
if ((cla = cyberflex_inq_class(sc_fd)) < 0)
|
|
|
|
cla = 0;
|
2001-07-14 10:13:26 +08:00
|
|
|
|
2001-07-04 13:35:00 +08:00
|
|
|
debug("sc_open ok %d", sc_fd);
|
|
|
|
return sc_fd;
|
|
|
|
}
|
|
|
|
|
2001-12-21 11:45:46 +08:00
|
|
|
static int
|
2001-07-04 13:35:00 +08:00
|
|
|
sc_enable_applet(void)
|
|
|
|
{
|
2001-08-07 05:10:52 +08:00
|
|
|
static u_char aid[] = {0xfc, 0x53, 0x73, 0x68, 0x2e, 0x62, 0x69, 0x6e};
|
|
|
|
int sw = 0;
|
2001-07-04 13:35:00 +08:00
|
|
|
|
2001-08-07 05:10:52 +08:00
|
|
|
/* select applet id */
|
|
|
|
sectok_apdu(sc_fd, cla, 0xa4, 0x04, 0, sizeof aid, aid, 0, NULL, &sw);
|
2001-07-04 13:35:00 +08:00
|
|
|
if (!sectok_swOK(sw)) {
|
|
|
|
error("sectok_apdu failed: %s", sectok_get_sw(sw));
|
2001-07-14 10:13:26 +08:00
|
|
|
sc_close();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-12-21 11:45:46 +08:00
|
|
|
static int
|
2001-07-14 10:13:26 +08:00
|
|
|
sc_init(void)
|
|
|
|
{
|
2001-08-07 05:22:10 +08:00
|
|
|
int status;
|
|
|
|
|
|
|
|
status = sc_open();
|
|
|
|
if (status == SCARD_ERROR_NOCARD) {
|
|
|
|
return SCARD_ERROR_NOCARD;
|
|
|
|
}
|
|
|
|
if (status < 0 ) {
|
2001-07-14 10:13:26 +08:00
|
|
|
error("sc_open failed");
|
2001-08-07 05:22:10 +08:00
|
|
|
return status;
|
2001-07-14 10:13:26 +08:00
|
|
|
}
|
|
|
|
if (sc_enable_applet() < 0) {
|
|
|
|
error("sc_enable_applet failed");
|
2001-08-07 05:22:10 +08:00
|
|
|
return SCARD_ERROR_APPLET;
|
2001-07-04 13:35:00 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-12-21 11:45:46 +08:00
|
|
|
static int
|
2001-07-04 13:35:00 +08:00
|
|
|
sc_read_pubkey(Key * k)
|
|
|
|
{
|
|
|
|
u_char buf[2], *n;
|
|
|
|
char *p;
|
2001-09-18 13:45:44 +08:00
|
|
|
int len, sw, status = -1;
|
2001-07-04 13:35:00 +08:00
|
|
|
|
|
|
|
len = sw = 0;
|
2001-09-28 17:51:54 +08:00
|
|
|
n = NULL;
|
2001-07-04 13:35:00 +08:00
|
|
|
|
2001-08-07 05:22:10 +08:00
|
|
|
if (sc_fd < 0) {
|
|
|
|
status = sc_init();
|
|
|
|
if (status < 0 )
|
2001-09-18 13:45:44 +08:00
|
|
|
goto err;
|
2001-08-07 05:22:10 +08:00
|
|
|
}
|
2001-07-14 10:13:26 +08:00
|
|
|
|
2001-07-04 13:35:00 +08:00
|
|
|
/* get key size */
|
|
|
|
sectok_apdu(sc_fd, CLA_SSH, INS_GET_KEYLENGTH, 0, 0, 0, NULL,
|
2001-12-21 11:45:46 +08:00
|
|
|
sizeof(buf), buf, &sw);
|
2001-07-04 13:35:00 +08:00
|
|
|
if (!sectok_swOK(sw)) {
|
|
|
|
error("could not obtain key length: %s", sectok_get_sw(sw));
|
2001-09-18 13:45:44 +08:00
|
|
|
goto err;
|
2001-07-04 13:35:00 +08:00
|
|
|
}
|
|
|
|
len = (buf[0] << 8) | buf[1];
|
|
|
|
len /= 8;
|
|
|
|
debug("INS_GET_KEYLENGTH: len %d sw %s", len, sectok_get_sw(sw));
|
|
|
|
|
|
|
|
n = xmalloc(len);
|
|
|
|
/* get n */
|
|
|
|
sectok_apdu(sc_fd, CLA_SSH, INS_GET_PUBKEY, 0, 0, 0, NULL, len, n, &sw);
|
|
|
|
if (!sectok_swOK(sw)) {
|
|
|
|
error("could not obtain public key: %s", sectok_get_sw(sw));
|
2001-09-18 13:45:44 +08:00
|
|
|
goto err;
|
2001-07-04 13:35:00 +08:00
|
|
|
}
|
2001-09-18 13:45:44 +08:00
|
|
|
|
2001-07-04 13:35:00 +08:00
|
|
|
debug("INS_GET_KEYLENGTH: sw %s", sectok_get_sw(sw));
|
|
|
|
|
|
|
|
if (BN_bin2bn(n, len, k->rsa->n) == NULL) {
|
|
|
|
error("c_read_pubkey: BN_bin2bn failed");
|
2001-09-18 13:45:44 +08:00
|
|
|
goto err;
|
2001-07-04 13:35:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* currently the java applet just stores 'n' */
|
|
|
|
if (!BN_set_word(k->rsa->e, 35)) {
|
|
|
|
error("c_read_pubkey: BN_set_word(e, 35) failed");
|
2001-09-18 13:45:44 +08:00
|
|
|
goto err;
|
2001-07-04 13:35:00 +08:00
|
|
|
}
|
|
|
|
|
2001-09-18 13:45:44 +08:00
|
|
|
status = 0;
|
2001-07-04 13:35:00 +08:00
|
|
|
p = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX);
|
|
|
|
debug("fingerprint %d %s", key_size(k), p);
|
|
|
|
xfree(p);
|
|
|
|
|
2001-09-18 13:45:44 +08:00
|
|
|
err:
|
|
|
|
if (n != NULL)
|
|
|
|
xfree(n);
|
|
|
|
sc_close();
|
|
|
|
return status;
|
2001-07-04 13:35:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* private key operations */
|
|
|
|
|
|
|
|
static int
|
2002-03-22 11:33:43 +08:00
|
|
|
sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa,
|
2002-03-22 11:28:11 +08:00
|
|
|
int padding)
|
2001-07-04 13:35:00 +08:00
|
|
|
{
|
|
|
|
u_char *padded = NULL;
|
2001-09-18 13:45:44 +08:00
|
|
|
int sw, len, olen, status = -1;
|
2001-07-04 13:35:00 +08:00
|
|
|
|
|
|
|
debug("sc_private_decrypt called");
|
|
|
|
|
|
|
|
olen = len = sw = 0;
|
2001-08-07 05:22:10 +08:00
|
|
|
if (sc_fd < 0) {
|
|
|
|
status = sc_init();
|
|
|
|
if (status < 0 )
|
2001-07-14 10:13:26 +08:00
|
|
|
goto err;
|
2001-08-07 05:22:10 +08:00
|
|
|
}
|
2001-07-04 13:35:00 +08:00
|
|
|
if (padding != RSA_PKCS1_PADDING)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
len = BN_num_bytes(rsa->n);
|
|
|
|
padded = xmalloc(len);
|
|
|
|
|
2002-03-22 11:28:11 +08:00
|
|
|
sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, (u_char *)from,
|
|
|
|
0, NULL, &sw);
|
2001-07-04 13:35:00 +08:00
|
|
|
if (!sectok_swOK(sw)) {
|
|
|
|
error("sc_private_decrypt: INS_DECRYPT failed: %s",
|
|
|
|
sectok_get_sw(sw));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
sectok_apdu(sc_fd, CLA_SSH, INS_GET_RESPONSE, 0, 0, 0, NULL,
|
2001-12-21 11:45:46 +08:00
|
|
|
len, padded, &sw);
|
2001-07-04 13:35:00 +08:00
|
|
|
if (!sectok_swOK(sw)) {
|
|
|
|
error("sc_private_decrypt: INS_GET_RESPONSE failed: %s",
|
|
|
|
sectok_get_sw(sw));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
olen = RSA_padding_check_PKCS1_type_2(to, len, padded + 1, len - 1,
|
|
|
|
len);
|
|
|
|
err:
|
|
|
|
if (padded)
|
|
|
|
xfree(padded);
|
2001-09-18 13:45:44 +08:00
|
|
|
sc_close();
|
2001-08-07 05:22:10 +08:00
|
|
|
return (olen >= 0 ? olen : status);
|
2001-07-04 13:35:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-03-22 11:33:43 +08:00
|
|
|
sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa,
|
2002-03-22 11:28:11 +08:00
|
|
|
int padding)
|
2001-07-04 13:35:00 +08:00
|
|
|
{
|
|
|
|
u_char *padded = NULL;
|
2001-09-18 13:45:44 +08:00
|
|
|
int sw, len, status = -1;
|
2001-07-04 13:35:00 +08:00
|
|
|
|
|
|
|
len = sw = 0;
|
2001-08-07 05:22:10 +08:00
|
|
|
if (sc_fd < 0) {
|
|
|
|
status = sc_init();
|
|
|
|
if (status < 0 )
|
2001-07-14 10:13:26 +08:00
|
|
|
goto err;
|
2001-08-07 05:22:10 +08:00
|
|
|
}
|
2001-07-04 13:35:00 +08:00
|
|
|
if (padding != RSA_PKCS1_PADDING)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
debug("sc_private_encrypt called");
|
|
|
|
len = BN_num_bytes(rsa->n);
|
|
|
|
padded = xmalloc(len);
|
|
|
|
|
2002-03-22 11:28:11 +08:00
|
|
|
if (RSA_padding_add_PKCS1_type_1(padded, len, (u_char *)from, flen) <= 0) {
|
2001-07-04 13:35:00 +08:00
|
|
|
error("RSA_padding_add_PKCS1_type_1 failed");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
sectok_apdu(sc_fd, CLA_SSH, INS_DECRYPT, 0, 0, len, padded, 0, NULL, &sw);
|
|
|
|
if (!sectok_swOK(sw)) {
|
|
|
|
error("sc_private_decrypt: INS_DECRYPT failed: %s",
|
|
|
|
sectok_get_sw(sw));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
sectok_apdu(sc_fd, CLA_SSH, INS_GET_RESPONSE, 0, 0, 0, NULL,
|
2001-12-21 11:45:46 +08:00
|
|
|
len, to, &sw);
|
2001-07-04 13:35:00 +08:00
|
|
|
if (!sectok_swOK(sw)) {
|
|
|
|
error("sc_private_decrypt: INS_GET_RESPONSE failed: %s",
|
|
|
|
sectok_get_sw(sw));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
err:
|
|
|
|
if (padded)
|
|
|
|
xfree(padded);
|
2001-09-18 13:45:44 +08:00
|
|
|
sc_close();
|
2001-08-07 05:22:10 +08:00
|
|
|
return (len >= 0 ? len : status);
|
2001-07-04 13:35:00 +08:00
|
|
|
}
|
|
|
|
|
2001-08-07 05:42:00 +08:00
|
|
|
/* called on free */
|
|
|
|
|
|
|
|
static int (*orig_finish)(RSA *rsa) = NULL;
|
|
|
|
|
|
|
|
static int
|
|
|
|
sc_finish(RSA *rsa)
|
|
|
|
{
|
|
|
|
if (orig_finish)
|
|
|
|
orig_finish(rsa);
|
|
|
|
sc_close();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-04 13:35:00 +08:00
|
|
|
/* engine for overloading private key operations */
|
|
|
|
|
|
|
|
static ENGINE *smart_engine = NULL;
|
2002-03-22 11:28:11 +08:00
|
|
|
static RSA_METHOD smart_rsa;
|
2001-07-04 13:35:00 +08:00
|
|
|
|
|
|
|
ENGINE *
|
|
|
|
sc_get_engine(void)
|
|
|
|
{
|
2002-03-22 11:28:11 +08:00
|
|
|
const RSA_METHOD *def;
|
2001-07-04 13:35:00 +08:00
|
|
|
|
|
|
|
def = RSA_get_default_openssl_method();
|
|
|
|
|
2002-03-22 11:28:11 +08:00
|
|
|
/* use the OpenSSL version */
|
|
|
|
memcpy(&smart_rsa, def, sizeof(smart_rsa));
|
|
|
|
|
|
|
|
smart_rsa.name = "sectok";
|
|
|
|
|
2001-07-04 13:35:00 +08:00
|
|
|
/* overload */
|
|
|
|
smart_rsa.rsa_priv_enc = sc_private_encrypt;
|
|
|
|
smart_rsa.rsa_priv_dec = sc_private_decrypt;
|
|
|
|
|
2001-08-07 05:42:00 +08:00
|
|
|
/* save original */
|
|
|
|
orig_finish = def->finish;
|
|
|
|
smart_rsa.finish = sc_finish;
|
|
|
|
|
2002-01-22 20:09:22 +08:00
|
|
|
if ((smart_engine = ENGINE_new()) == NULL)
|
|
|
|
fatal("ENGINE_new failed");
|
2001-07-04 13:35:00 +08:00
|
|
|
|
|
|
|
ENGINE_set_id(smart_engine, "sectok");
|
|
|
|
ENGINE_set_name(smart_engine, "libsectok");
|
2002-03-22 11:28:11 +08:00
|
|
|
|
2001-07-04 13:35:00 +08:00
|
|
|
ENGINE_set_RSA(smart_engine, &smart_rsa);
|
|
|
|
ENGINE_set_DSA(smart_engine, DSA_get_default_openssl_method());
|
|
|
|
ENGINE_set_DH(smart_engine, DH_get_default_openssl_method());
|
|
|
|
ENGINE_set_RAND(smart_engine, RAND_SSLeay());
|
|
|
|
ENGINE_set_BN_mod_exp(smart_engine, BN_mod_exp);
|
|
|
|
|
|
|
|
return smart_engine;
|
|
|
|
}
|
|
|
|
|
2001-07-14 10:13:26 +08:00
|
|
|
void
|
|
|
|
sc_close(void)
|
|
|
|
{
|
|
|
|
if (sc_fd >= 0) {
|
|
|
|
sectok_close(sc_fd);
|
|
|
|
sc_fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-04 13:35:00 +08:00
|
|
|
Key *
|
2001-08-07 05:35:51 +08:00
|
|
|
sc_get_key(const char *id)
|
2001-07-04 13:35:00 +08:00
|
|
|
{
|
|
|
|
Key *k;
|
2001-08-07 05:25:38 +08:00
|
|
|
int status;
|
2001-07-04 13:35:00 +08:00
|
|
|
|
2001-08-07 05:35:51 +08:00
|
|
|
if (sc_reader_id != NULL)
|
|
|
|
xfree(sc_reader_id);
|
|
|
|
sc_reader_id = xstrdup(id);
|
|
|
|
|
2001-07-04 13:35:00 +08:00
|
|
|
k = key_new(KEY_RSA);
|
|
|
|
if (k == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-08-07 05:25:38 +08:00
|
|
|
status = sc_read_pubkey(k);
|
|
|
|
if (status == SCARD_ERROR_NOCARD) {
|
|
|
|
key_free(k);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (status < 0 ) {
|
2001-07-04 13:35:00 +08:00
|
|
|
error("sc_read_pubkey failed");
|
|
|
|
key_free(k);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return k;
|
|
|
|
}
|
2002-03-22 11:33:43 +08:00
|
|
|
|
|
|
|
#define NUM_RSA_KEY_ELEMENTS 5+1
|
|
|
|
#define COPY_RSA_KEY(x, i) \
|
|
|
|
do { \
|
|
|
|
len = BN_num_bytes(prv->rsa->x); \
|
|
|
|
elements[i] = xmalloc(len); \
|
|
|
|
debug("#bytes %d", len); \
|
|
|
|
if (BN_bn2bin(prv->rsa->x, elements[i]) < 0) \
|
|
|
|
goto done; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_AUT0(char *aut0)
|
|
|
|
{
|
|
|
|
const EVP_MD *evp_md = EVP_sha1();
|
|
|
|
EVP_MD_CTX md;
|
|
|
|
char *pass;
|
|
|
|
|
|
|
|
pass = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
|
|
|
|
if (pass == NULL)
|
|
|
|
return -1;
|
|
|
|
EVP_DigestInit(&md, evp_md);
|
|
|
|
EVP_DigestUpdate(&md, pass, strlen(pass));
|
|
|
|
EVP_DigestFinal(&md, aut0, NULL);
|
|
|
|
memset(pass, 0, strlen(pass));
|
|
|
|
xfree(pass);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sc_put_key(Key *prv, const char *id)
|
|
|
|
{
|
|
|
|
u_char *elements[NUM_RSA_KEY_ELEMENTS];
|
|
|
|
u_char key_fid[2];
|
|
|
|
u_char DEFAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63};
|
|
|
|
u_char AUT0[EVP_MAX_MD_SIZE];
|
|
|
|
int len, status = -1, i, fd = -1, ret;
|
|
|
|
int sw = 0, cla = 0x00;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
|
|
|
|
elements[i] = NULL;
|
|
|
|
|
|
|
|
COPY_RSA_KEY(q, 0);
|
|
|
|
COPY_RSA_KEY(p, 1);
|
|
|
|
COPY_RSA_KEY(iqmp, 2);
|
|
|
|
COPY_RSA_KEY(dmq1, 3);
|
|
|
|
COPY_RSA_KEY(dmp1, 4);
|
|
|
|
COPY_RSA_KEY(n, 5);
|
|
|
|
len = BN_num_bytes(prv->rsa->n);
|
|
|
|
fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw);
|
|
|
|
if (fd < 0) {
|
|
|
|
error("sectok_open failed: %s", sectok_get_sw(sw));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (! sectok_cardpresent(fd)) {
|
|
|
|
error("smartcard in reader %s not present",
|
|
|
|
sc_reader_id);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
ret = sectok_reset(fd, 0, NULL, &sw);
|
|
|
|
if (ret <= 0) {
|
|
|
|
error("sectok_reset failed: %s", sectok_get_sw(sw));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if ((cla = cyberflex_inq_class(fd)) < 0) {
|
|
|
|
error("cyberflex_inq_class failed");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
memcpy(AUT0, DEFAUT0, sizeof(DEFAUT0));
|
|
|
|
if (cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
|
|
|
|
if (get_AUT0(AUT0) < 0 ||
|
|
|
|
cyberflex_verify_AUT0(fd, cla, AUT0, sizeof(DEFAUT0)) < 0) {
|
|
|
|
error("cyberflex_verify_AUT0 failed");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
key_fid[0] = 0x00;
|
|
|
|
key_fid[1] = 0x12;
|
|
|
|
if (cyberflex_load_rsa_priv(fd, cla, key_fid, 5, 8*len, elements,
|
|
|
|
&sw) < 0) {
|
|
|
|
error("cyberflex_load_rsa_priv failed: %s", sectok_get_sw(sw));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (!sectok_swOK(sw))
|
|
|
|
goto done;
|
|
|
|
log("cyberflex_load_rsa_priv done");
|
|
|
|
key_fid[0] = 0x73;
|
|
|
|
key_fid[1] = 0x68;
|
|
|
|
if (cyberflex_load_rsa_pub(fd, cla, key_fid, len, elements[5],
|
|
|
|
&sw) < 0) {
|
|
|
|
error("cyberflex_load_rsa_pub failed: %s", sectok_get_sw(sw));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (!sectok_swOK(sw))
|
|
|
|
goto done;
|
|
|
|
log("cyberflex_load_rsa_pub done");
|
|
|
|
status = 0;
|
|
|
|
|
|
|
|
done:
|
|
|
|
memset(elements[0], '\0', BN_num_bytes(prv->rsa->q));
|
|
|
|
memset(elements[1], '\0', BN_num_bytes(prv->rsa->p));
|
|
|
|
memset(elements[2], '\0', BN_num_bytes(prv->rsa->iqmp));
|
|
|
|
memset(elements[3], '\0', BN_num_bytes(prv->rsa->dmq1));
|
|
|
|
memset(elements[4], '\0', BN_num_bytes(prv->rsa->dmp1));
|
|
|
|
memset(elements[5], '\0', BN_num_bytes(prv->rsa->n));
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_RSA_KEY_ELEMENTS; i++)
|
|
|
|
if (elements[i])
|
|
|
|
xfree(elements[i]);
|
|
|
|
if (fd != -1)
|
|
|
|
sectok_close(fd);
|
|
|
|
return (status);
|
|
|
|
}
|
2001-08-07 05:59:25 +08:00
|
|
|
#endif /* SMARTCARD */
|