* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifdef SMARTCARD
#include "includes.h"
-RCSID("$OpenBSD: scard.c,v 1.9 2001/07/31 08:41:10 jakob Exp $");
+#ifdef SMARTCARD
+RCSID("$OpenBSD: scard.c,v 1.15 2001/09/28 09:49:31 djm Exp $");
#include <openssl/engine.h>
#include <sectok.h>
#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 */
if (sc_fd >= 0)
return sc_fd;
- sc_fd = sectok_open(sc_reader_num, STONOWAIT, &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 SCARD_ERROR_FAIL;
}
if (! sectok_cardpresent(sc_fd)) {
- debug("smartcard in reader %d not present, skipping",
- sc_reader_num);
+ debug("smartcard in reader %s not present, skipping",
+ sc_reader_id);
+ sc_close();
return SCARD_ERROR_NOCARD;
}
if (sectok_reset(sc_fd, 0, NULL, &sw) <= 0) {
{
u_char buf[2], *n;
char *p;
- int len, sw, status;
+ int len, sw, status = -1;
len = sw = 0;
+ n = NULL;
if (sc_fd < 0) {
status = sc_init();
if (status < 0 )
- return status;
+ goto err;
}
/* get key size */
sizeof(buf), buf, &sw);
if (!sectok_swOK(sw)) {
error("could not obtain key length: %s", sectok_get_sw(sw));
- sc_close();
- return -1;
+ goto err;
}
len = (buf[0] << 8) | buf[1];
len /= 8;
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));
- xfree(n);
- return -1;
+ goto err;
}
+
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");
- xfree(n);
- sc_close();
- return -1;
+ goto err;
}
- xfree(n);
/* 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");
- return -1;
+ goto err;
}
+ status = 0;
p = key_fingerprint(k, SSH_FP_MD5, SSH_FP_HEX);
debug("fingerprint %d %s", key_size(k), p);
xfree(p);
- return 0;
+err:
+ if (n != NULL)
+ xfree(n);
+ sc_close();
+ return status;
}
/* private key operations */
sc_private_decrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding)
{
u_char *padded = NULL;
- int sw, len, olen, status;
+ int sw, len, olen, status = -1;
debug("sc_private_decrypt called");
if (!sectok_swOK(sw)) {
error("sc_private_decrypt: INS_DECRYPT failed: %s",
sectok_get_sw(sw));
- sc_close();
goto err;
}
sectok_apdu(sc_fd, CLA_SSH, INS_GET_RESPONSE, 0, 0, 0, NULL,
if (!sectok_swOK(sw)) {
error("sc_private_decrypt: INS_GET_RESPONSE failed: %s",
sectok_get_sw(sw));
- sc_close();
goto err;
}
olen = RSA_padding_check_PKCS1_type_2(to, len, padded + 1, len - 1,
err:
if (padded)
xfree(padded);
+ sc_close();
return (olen >= 0 ? olen : status);
}
sc_private_encrypt(int flen, u_char *from, u_char *to, RSA *rsa, int padding)
{
u_char *padded = NULL;
- int sw, len, status;
+ int sw, len, status = -1;
len = sw = 0;
if (sc_fd < 0) {
if (!sectok_swOK(sw)) {
error("sc_private_decrypt: INS_DECRYPT failed: %s",
sectok_get_sw(sw));
- sc_close();
goto err;
}
sectok_apdu(sc_fd, CLA_SSH, INS_GET_RESPONSE, 0, 0, 0, NULL,
if (!sectok_swOK(sw)) {
error("sc_private_decrypt: INS_GET_RESPONSE failed: %s",
sectok_get_sw(sw));
- sc_close();
goto err;
}
err:
if (padded)
xfree(padded);
+ sc_close();
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;
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;
}
Key *
-sc_get_key(int num)
+sc_get_key(const char *id)
{
Key *k;
int status;
- sc_reader_num = num;
+ if (sc_reader_id != NULL)
+ xfree(sc_reader_id);
+ sc_reader_id = xstrdup(id);
+
k = key_new(KEY_RSA);
if (k == NULL) {
return NULL;
return NULL;
}
return k;
- sc_close();
}
-#endif
+#endif /* SMARTCARD */