scard-opensc.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright (c) 2002 Juha Yrj�l�. All rights reserved.
* Copyright (c) 2001 Markus Friedl.
*
* 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
*
* 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"
#if defined(SMARTCARD) && defined(USE_OPENSC)
#include "key.h"
#include "log.h"
#include "xmalloc.h"
#include "readpass.h"
#include "scard.h"
#define USE_ENGINE
#else
#endif
#ifdef USE_ENGINE
#define sc_get_rsa sc_get_engine
#else
#define sc_get_rsa sc_get_rsa_method
#endif
static int sc_reader_id;
struct sc_priv_data
{
struct sc_pkcs15_id cert_id;
int ref_count;
};
void
sc_close(void)
{
if (p15card) {
}
if (card) {
sc_disconnect_card(card, 0);
}
if (ctx) {
}
}
static int
sc_init(void)
{
int r;
if (r)
goto err;
if (r)
goto err;
if (r)
goto err;
return 0;
err:
sc_close();
return r;
}
/* private key operations */
static int
{
int r;
struct sc_priv_data *priv;
struct sc_pkcs15_object *key_obj;
struct sc_pkcs15_prkey_info *key;
struct sc_pkcs15_object *pin_obj;
struct sc_pkcs15_pin_info *pin;
return -1;
sc_close();
r = sc_init();
if (r) {
goto err;
}
}
if (r) {
error("Unable to find private key from SmartCard: %s",
sc_strerror(r));
goto err;
}
&pin_obj);
if (r) {
error("Unable to find PIN object from SmartCard: %s",
sc_strerror(r));
goto err;
}
if (r) {
goto err;
}
if (r) {
error("PIN code verification failed: %s",
sc_strerror(r));
goto err;
}
}
*key_obj_out = key_obj;
return 0;
err:
sc_close();
return -1;
}
static int
int padding)
{
struct sc_pkcs15_object *key_obj;
int r;
if (padding != RSA_PKCS1_PADDING)
return -1;
if (r)
return -1;
if (r < 0) {
goto err;
}
return r;
err:
sc_close();
return -1;
}
static int
{
struct sc_pkcs15_object *key_obj;
int r;
unsigned long flags = 0;
if (r)
return -1;
/* FIXME: length of sigret correct? */
/* FIXME: check 'type' and modify flags accordingly */
if (r < 0) {
error("sc_pkcs15_compute_signature() failed: %s",
sc_strerror(r));
goto err;
}
*siglen = r;
return 1;
err:
sc_close();
return 0;
}
static int
int padding)
{
error("Private key encryption not supported");
return -1;
}
/* called on free */
static int
{
struct sc_priv_data *priv;
sc_close();
}
if (orig_finish)
return 1;
}
/* engine for overloading private key operations */
static RSA_METHOD *
sc_get_rsa_method(void)
{
static RSA_METHOD smart_rsa;
/* use the OpenSSL version */
/* overload */
/* save original */
return &smart_rsa;
}
#ifdef USE_ENGINE
static ENGINE *
sc_get_engine(void)
{
fatal("ENGINE_new failed");
return smart_engine;
}
#endif
static void
{
struct sc_priv_data *priv;
return;
}
static int
{
int r;
u8 *p;
char *tmp;
if (r) {
goto err;
}
r = -1;
goto err;
}
log("Unable to parse X.509 certificate");
r = -1;
goto err;
}
log("Public key is of unknown type");
r = -1;
goto err;
}
k->flags = KEY_FLAG_EXT;
return 0;
err:
if (cert)
if (pubkey)
if (x509)
return r;
}
Key **
{
int i, r, real_count = 0, key_count;
*p = 0;
p++;
}
if (r != 1)
goto err;
sc_close();
r = sc_init();
if (r) {
goto err;
}
}
if (r < 0)
goto err;
key_count = 1;
} else {
certs, 32);
if (r == 0) {
log("No certificates found on smartcard");
r = -1;
goto err;
} else if (r < 0) {
error("Certificate enumeration failed: %s",
sc_strerror(r));
goto err;
}
key_count = r;
}
/* FIXME: only keep entries with a corresponding private key */
for (i = 0; i < key_count; i++) {
if (k == NULL)
break;
r = sc_read_pubkey(k, certs[i]);
if (r) {
key_free(k);
continue;
}
keys[real_count] = k;
real_count++;
if (k == NULL)
break;
keys[real_count] = k;
real_count++;
}
return keys;
err:
sc_close();
return NULL;
}
int
{
error("key uploading not yet supported");
return -1;
}
#endif /* SMARTCARD */
#pragma ident "%Z%%M% %I% %E% SMI"