]> andersk Git - openssh.git/blobdiff - scard.c
- jakob@cvs.openbsd.org 2001/08/02 16:14:05
[openssh.git] / scard.c
diff --git a/scard.c b/scard.c
index 25776d2e61d81969f9cc93f8031ef4524cb8c60b..6e6de2f329501a9cb7c5997b32bc0dba7b1bfc8b 100644 (file)
--- a/scard.c
+++ b/scard.c
@@ -24,7 +24,7 @@
 
 #ifdef SMARTCARD
 #include "includes.h"
-RCSID("$OpenBSD: scard.c,v 1.6 2001/07/25 11:59:35 markus Exp $");
+RCSID("$OpenBSD: scard.c,v 1.13 2001/08/02 16:14:05 jakob Exp $");
 
 #include <openssl/engine.h>
 #include <sectok.h>
@@ -43,7 +43,7 @@ RCSID("$OpenBSD: scard.c,v 1.6 2001/07/25 11:59:35 markus Exp $");
 #define MAX_BUF_SIZE 256
 
 static int sc_fd = -1;
-static int sc_reader_num = -1;
+static char *sc_reader_id = NULL;
 static int cla = 0x00; /* class */
 
 /* interface to libsectok */
@@ -51,22 +51,29 @@ static int cla = 0x00;      /* class */
 static int 
 sc_open(void)
 {
-       u_char atr[256];
        int sw;
 
        if (sc_fd >= 0)
                return sc_fd;
 
-       sc_fd = sectok_open(sc_reader_num, 0, &sw);
+       sc_fd = sectok_friendly_open(sc_reader_id, STONOWAIT, &sw);
        if (sc_fd < 0) {
                error("sectok_open failed: %s", sectok_get_sw(sw));
-               return -1;
+               return SCARD_ERROR_FAIL;
+       }
+       if (! sectok_cardpresent(sc_fd)) {
+               debug("smartcard in reader %s not present, skipping",
+                   sc_reader_id);
+               sc_close();
+               return SCARD_ERROR_NOCARD;
        }
-       if (sectok_reset(sc_fd, 0, atr, &sw) <= 0) {
+       if (sectok_reset(sc_fd, 0, NULL, &sw) <= 0) {
                error("sectok_reset failed: %s", sectok_get_sw(sw));
                sc_fd = -1;
-               return sc_fd;
+               return SCARD_ERROR_FAIL;
        }
+       if ((cla = cyberflex_inq_class(sc_fd)) < 0)
+               cla = 0;
 
        debug("sc_open ok %d", sc_fd);
        return sc_fd;
@@ -75,29 +82,11 @@ sc_open(void)
 static int 
 sc_enable_applet(void)
 {
-       u_char contID[2], aid[MAX_BUF_SIZE];
-       int i, len, sw, aid_len;
+       static u_char aid[] = {0xfc, 0x53, 0x73, 0x68, 0x2e, 0x62, 0x69, 0x6e};
+       int sw = 0;
 
-       len = sw = 0;
-       contID[0] = 0x77;
-       contID[1] = 0x78;
-
-       if (sectok_selectfile(sc_fd, cla, root_fid, &sw) < 0) {
-               error("sectok_selectfile root_fid failed: %s",
-                   sectok_get_sw(sw));
-               sc_close();
-               return -1;
-       }
-       if (sectok_selectfile(sc_fd, cla, contID, &sw) < 0) {
-               error("sectok_selectfile failed: %s", sectok_get_sw(sw));
-               sc_close();
-               return -1;
-       }
-       /* send applet id */
-       for (i = 0; i < sizeof(aid); i++)
-               aid[i] = 0x77;
-       aid_len = 5;
-       sectok_apdu(sc_fd, cla, 0xa4, 0x04, 0, aid_len, aid, 0, NULL, &sw);
+       /* select applet id */
+       sectok_apdu(sc_fd, cla, 0xa4, 0x04, 0, sizeof aid, aid, 0, NULL, &sw);
        if (!sectok_swOK(sw)) {
                error("sectok_apdu failed: %s", sectok_get_sw(sw));
                sc_close();
@@ -109,13 +98,19 @@ sc_enable_applet(void)
 static int 
 sc_init(void)
 {
-       if (sc_open() < 0) {
+       int status;
+
+       status = sc_open();
+       if (status == SCARD_ERROR_NOCARD) {
+               return SCARD_ERROR_NOCARD;
+       }
+       if (status < 0 ) {
                error("sc_open failed");
-               return -1;
+               return status;
        }
        if (sc_enable_applet() < 0) {
                error("sc_enable_applet failed");
-               return -1;
+               return SCARD_ERROR_APPLET;
        }
        return 0;
 }
@@ -125,13 +120,15 @@ sc_read_pubkey(Key * k)
 {
        u_char buf[2], *n;
        char *p;
-       int len, sw;
+       int len, sw, status;
 
        len = sw = 0;
 
-       if (sc_fd < 0)
-               if (sc_init() < 0)
-                       return -1;
+       if (sc_fd < 0) {
+               status = sc_init();
+               if (status < 0 )
+                       return status;
+       }
 
        /* get key size */
        sectok_apdu(sc_fd, CLA_SSH, INS_GET_KEYLENGTH, 0, 0, 0, NULL,
@@ -182,14 +179,16 @@ static int
 sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding)
 {
        u_char *padded = NULL;
-       int sw, len, olen;
+       int sw, len, olen, status;
 
        debug("sc_private_decrypt called");
 
        olen = len = sw = 0;
-       if (sc_fd < 0)
-               if (sc_init() < 0)
+       if (sc_fd < 0) {
+               status = sc_init();
+               if (status < 0 )
                        goto err;
+       }
        if (padding != RSA_PKCS1_PADDING)
                goto err;
 
@@ -216,19 +215,21 @@ sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding)
 err:
        if (padded)
                xfree(padded);
-       return olen;
+       return (olen >= 0 ? olen : status);
 }
 
 static int
 sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding)
 {
        u_char *padded = NULL;
-       int sw, len;
+       int sw, len, status;
 
        len = sw = 0;
-       if (sc_fd < 0)
-               if (sc_init() < 0)
+       if (sc_fd < 0) {
+               status = sc_init();
+               if (status < 0 )
                        goto err;
+       }
        if (padding != RSA_PKCS1_PADDING)
                goto err;
 
@@ -258,9 +259,23 @@ sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding)
 err:
        if (padded)
                xfree(padded);
-       return len;
+       return (len >= 0 ? len : status);
+}
+
+/* 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;
 }
 
+
 /* engine for overloading private key operations */
 
 static ENGINE *smart_engine = NULL;
@@ -290,13 +305,16 @@ sc_get_engine(void)
        smart_rsa.rsa_priv_enc  = sc_private_encrypt;
        smart_rsa.rsa_priv_dec  = sc_private_decrypt;
 
+       /* save original */
+       orig_finish             = def->finish;
+       smart_rsa.finish        = sc_finish;
+
        /* just use the OpenSSL version */
        smart_rsa.rsa_pub_enc   = def->rsa_pub_enc;
        smart_rsa.rsa_pub_dec   = def->rsa_pub_dec;
        smart_rsa.rsa_mod_exp   = def->rsa_mod_exp;
        smart_rsa.bn_mod_exp    = def->bn_mod_exp;
        smart_rsa.init          = def->init;
-       smart_rsa.finish        = def->finish;
        smart_rsa.flags         = def->flags;
        smart_rsa.app_data      = def->app_data;
        smart_rsa.rsa_sign      = def->rsa_sign;
@@ -325,16 +343,25 @@ sc_close(void)
 }
 
 Key *
-sc_get_key(int num)
+sc_get_key(const char *id)
 {
        Key *k;
+       int status;
+
+       if (sc_reader_id != NULL)
+               xfree(sc_reader_id);
+       sc_reader_id = xstrdup(id);
 
-       sc_reader_num = num;
        k = key_new(KEY_RSA);
        if (k == NULL) {
                return NULL;
        }
-       if (sc_read_pubkey(k) < 0) {
+       status = sc_read_pubkey(k);
+       if (status == SCARD_ERROR_NOCARD) {
+               key_free(k);
+               return NULL;
+       }
+       if (status < 0 ) {
                error("sc_read_pubkey failed");
                key_free(k);
                return NULL;
@@ -342,4 +369,4 @@ sc_get_key(int num)
        return k;
        sc_close();
 }
-#endif
+#endif /* SMARTCARD */
This page took 0.051878 seconds and 4 git commands to generate.