[pkg-opensc-commit] [opensc] 38/295: New: jpki card driver

Eric Dorland eric at moszumanska.debian.org
Sat Jun 24 21:11:14 UTC 2017


This is an automated email from the git hooks/post-receive script.

eric pushed a commit to branch master
in repository opensc.

commit 33a16b4f4e6ad7de3eb223aeccb12510c0c4145f
Author: HAMANO Tsukasa <hamano at osstech.co.jp>
Date:   Thu Jun 16 16:35:54 2016 +0900

    New: jpki card driver
    
    VTA: cosmetic touch and rebase to one commit
    close PR #801
---
 src/libopensc/Makefile.am   |   7 +-
 src/libopensc/Makefile.mak  |   4 +-
 src/libopensc/card-jpki.c   | 366 ++++++++++++++++++++++++++++++++++++++++++++
 src/libopensc/cards.h       |   4 +
 src/libopensc/ctx.c         |   1 +
 src/libopensc/jpki.h        |  43 ++++++
 src/libopensc/pkcs15-jpki.c | 190 +++++++++++++++++++++++
 src/libopensc/pkcs15-syn.c  |   1 +
 src/libopensc/pkcs15-syn.h  |   1 +
 9 files changed, 612 insertions(+), 5 deletions(-)

diff --git a/src/libopensc/Makefile.am b/src/libopensc/Makefile.am
index 9d0ad08..18d8600 100644
--- a/src/libopensc/Makefile.am
+++ b/src/libopensc/Makefile.am
@@ -11,7 +11,8 @@ noinst_HEADERS = cards.h ctbcs.h internal.h esteid.h muscle.h muscle-filesystem.
 	cardctl.h asn1.h log.h \
 	errors.h types.h compression.h itacns.h iso7816.h \
 	authentic.h iasecc.h iasecc-sdo.h sm.h card-sc-hsm.h \
-	pace.h cwa14890.h cwa-dnie.h card-gids.h aux-data.h
+	pace.h cwa14890.h cwa-dnie.h card-gids.h aux-data.h \
+	jpki.h
 
 AM_CPPFLAGS = -DOPENSC_CONF_PATH=\"$(sysconfdir)/opensc.conf\" \
 	-I$(top_srcdir)/src
@@ -41,14 +42,14 @@ libopensc_la_SOURCES = \
 	card-itacns.c card-authentic.c \
 	card-iasecc.c iasecc-sdo.c iasecc-sm.c card-sc-hsm.c \
 	card-dnie.c cwa14890.c cwa-dnie.c \
-	card-isoApplet.c card-masktech.c card-gids.c \
+	card-isoApplet.c card-masktech.c card-gids.c card-jpki.c \
 	\
 	pkcs15-openpgp.c pkcs15-infocamere.c pkcs15-starcert.c \
 	pkcs15-tcos.c pkcs15-esteid.c pkcs15-postecert.c pkcs15-gemsafeGPK.c \
 	pkcs15-actalis.c pkcs15-atrust-acos.c pkcs15-tccardos.c pkcs15-piv.c \
 	pkcs15-esinit.c pkcs15-westcos.c pkcs15-pteid.c pkcs15-oberthur.c \
 	pkcs15-itacns.c pkcs15-gemsafeV1.c pkcs15-sc-hsm.c \
-	pkcs15-dnie.c pkcs15-gids.c pkcs15-iasecc.c \
+	pkcs15-dnie.c pkcs15-gids.c pkcs15-iasecc.c pkcs15-jpki.c \
 	compression.c p15card-helper.c sm.c \
 	aux-data.c \
 	libopensc.exports
diff --git a/src/libopensc/Makefile.mak b/src/libopensc/Makefile.mak
index 3f0b9b2..0b8124b 100644
--- a/src/libopensc/Makefile.mak
+++ b/src/libopensc/Makefile.mak
@@ -24,14 +24,14 @@ OBJECTS			= \
 	card-itacns.obj card-authentic.obj \
 	card-iasecc.obj iasecc-sdo.obj iasecc-sm.obj cwa-dnie.obj cwa14890.obj \
 	card-sc-hsm.obj card-dnie.obj card-isoApplet.obj \
-	card-masktech.obj card-gids.obj \
+	card-masktech.obj card-gids.obj card-jpki.obj \
 	\
 	pkcs15-openpgp.obj pkcs15-infocamere.obj pkcs15-starcert.obj \
 	pkcs15-tcos.obj pkcs15-esteid.obj pkcs15-postecert.obj pkcs15-gemsafeGPK.obj \
 	pkcs15-actalis.obj pkcs15-atrust-acos.obj pkcs15-tccardos.obj pkcs15-piv.obj \
 	pkcs15-esinit.obj pkcs15-westcos.obj pkcs15-pteid.obj pkcs15-oberthur.obj \
 	pkcs15-itacns.obj pkcs15-gemsafeV1.obj pkcs15-sc-hsm.obj \
-	pkcs15-dnie.obj pkcs15-gids.obj pkcs15-iasecc.obj \
+	pkcs15-dnie.obj pkcs15-gids.obj pkcs15-iasecc.obj pkcs15-jpki.obj \
 	compression.obj p15card-helper.obj sm.obj \
 	aux-data.obj \
 	$(TOPDIR)\win32\versioninfo.res
diff --git a/src/libopensc/card-jpki.c b/src/libopensc/card-jpki.c
new file mode 100644
index 0000000..137d12c
--- /dev/null
+++ b/src/libopensc/card-jpki.c
@@ -0,0 +1,366 @@
+/*
+ * card-jpki.c: Support for JPKI(Japanese Individual Number Cards).
+ *
+ * Copyright (C) 2016, HAMANO Tsukasa <hamano at osstech.co.jp>
+ *
+ * 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
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <string.h>
+#include <stdlib.h>
+
+#include "internal.h"
+#include "jpki.h"
+
+static struct sc_atr_table jpki_atrs[] = {
+	{"3b:e0:00:ff:81:31:fe:45:14", NULL, NULL,
+	 SC_CARD_TYPE_JPKI_BASE, 0, NULL},
+	{NULL, NULL, NULL, 0, 0, NULL}
+};
+
+static struct sc_card_operations jpki_ops;
+static struct sc_card_driver jpki_drv = {
+	"JPKI(Japanese Individual Number Cards)",
+	"jpki",
+	&jpki_ops,
+	NULL, 0, NULL
+};
+
+int jpki_select_ap(struct sc_card *card)
+{
+	int rc;
+	sc_path_t path;
+
+	LOG_FUNC_CALLED(card->ctx);
+
+	/* Select JPKI application */
+	sc_format_path(AID_JPKI, &path);
+	path.type = SC_PATH_TYPE_DF_NAME;
+	rc = sc_select_file(card, &path, NULL);
+	LOG_TEST_RET(card->ctx, rc, "select JPKI AP failed");
+
+	LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
+}
+
+static int
+jpki_match_card(struct sc_card *card)
+{
+	int i, rc;
+
+	i = _sc_match_atr(card, jpki_atrs, &card->type);
+	if (i >= 0) {
+		return 1;
+	}
+
+	rc = jpki_select_ap(card);
+	if (rc == SC_SUCCESS) {
+		card->type = SC_CARD_TYPE_JPKI_BASE;
+		return 1;
+	}
+	return 0;
+}
+
+static int
+jpki_finish(sc_card_t * card)
+{
+	struct jpki_private_data *drvdata = JPKI_DRVDATA(card);
+
+	LOG_FUNC_CALLED(card->ctx);
+
+	if (drvdata) {
+		free(drvdata);
+		card->drv_data = NULL;
+	}
+	LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
+}
+
+static int
+jpki_init(struct sc_card *card)
+{
+	struct jpki_private_data *drvdata;
+	sc_file_t *mf;
+	int flags;
+
+	LOG_FUNC_CALLED(card->ctx);
+
+	drvdata = malloc(sizeof (struct jpki_private_data));
+	if (!drvdata)
+		LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
+
+	memset(drvdata, 0, sizeof (struct jpki_private_data));
+
+	/* create virtual MF */
+	mf = sc_file_new();
+	if (!mf) {
+		free(drvdata);
+		LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
+	}
+	sc_format_path("3f00", &mf->path);
+	mf->type = SC_FILE_TYPE_DF;
+	mf->shareable = 0;
+	mf->ef_structure = SC_FILE_EF_UNKNOWN;
+	mf->size = 0;
+	mf->id = 0x3f00;
+	mf->status = SC_FILE_STATUS_ACTIVATED;
+	sc_file_add_acl_entry(mf, SC_AC_OP_SELECT, SC_AC_NONE, 0);
+	sc_file_add_acl_entry(mf, SC_AC_OP_LIST_FILES, SC_AC_NONE, 0);
+	sc_file_add_acl_entry(mf, SC_AC_OP_LOCK, SC_AC_NEVER, 0);
+	sc_file_add_acl_entry(mf, SC_AC_OP_DELETE, SC_AC_NEVER, 0);
+	sc_file_add_acl_entry(mf, SC_AC_OP_CREATE, SC_AC_NEVER, 0);
+	drvdata->mf = mf;
+	drvdata->selected = SELECT_MF;
+
+	card->name = "jpki";
+	card->drv_data = drvdata;
+
+	flags = SC_ALGORITHM_RSA_HASH_NONE | SC_ALGORITHM_RSA_PAD_PKCS1;
+	_sc_card_add_rsa_alg(card, 2048, flags, 0);
+
+	LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
+}
+
+static int
+jpki_select_file(struct sc_card *card,
+		 const struct sc_path *path, struct sc_file **file_out)
+{
+	struct jpki_private_data *drvdata = JPKI_DRVDATA(card);
+	int rc;
+	sc_apdu_t apdu;
+	struct sc_file *file = NULL;
+
+	LOG_FUNC_CALLED(card->ctx);
+	sc_log(card->ctx, "jpki_select_file: path=%s, len=%d",
+			sc_print_path(path), path->len);
+	if (path->len == 2 && memcmp(path->value, "\x3F\x00", 2) == 0) {
+		drvdata->selected = SELECT_MF;
+		if (file_out) {
+			sc_file_dup(file_out, drvdata->mf);
+			if (*file_out == NULL) {
+				LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
+			}
+		}
+		return 0;
+	}
+
+	sc_format_apdu(card, &apdu, SC_APDU_CASE_3_SHORT, 0xA4, 0, 0);
+	switch (path->type) {
+	case SC_PATH_TYPE_FILE_ID:
+		apdu.p1 = 2;
+		break;
+	case SC_PATH_TYPE_DF_NAME:
+		apdu.p1 = 4;
+		break;
+	default:
+		LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
+	}
+	apdu.p2 = 0x0C;
+	apdu.data = path->value;
+	apdu.datalen = path->len;
+	apdu.lc = path->len;
+
+	rc = sc_transmit_apdu(card, &apdu);
+	LOG_TEST_RET(card->ctx, rc, "APDU transmit failed");
+	rc = sc_check_sw(card, apdu.sw1, apdu.sw2);
+	LOG_TEST_RET(card->ctx, rc, "SW Check failed");
+	if (!file_out) {
+		LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
+	}
+
+	/* read size of auth certificate file */
+	if (path->len == 2 && memcmp(path->value, "\x00\x0a", 2) == 0) {
+		u8 buf[4];
+		rc = sc_read_binary(card, 0, buf, 4, 0);
+		LOG_TEST_RET(card->ctx, rc, "SW Check failed");
+		file = sc_file_new();
+		if (!file) {
+			LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
+		}
+		file->path = *path;
+		file->size = (buf[2] << 8 | buf[3]) + 4;
+		*file_out = file;
+	}
+
+	LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
+}
+
+static int
+jpki_read_binary(sc_card_t * card, unsigned int idx,
+		 u8 * buf, size_t count, unsigned long flags)
+{
+	struct sc_card_driver *iso_drv = sc_get_iso7816_driver();
+	const struct sc_card_operations *iso_ops = iso_drv->ops;
+	int rc;
+
+	LOG_FUNC_CALLED(card->ctx);
+
+	rc = iso_ops->read_binary(card, idx, buf, count, flags);
+	LOG_FUNC_RETURN(card->ctx, rc);
+}
+
+static int
+jpki_pin_cmd(sc_card_t * card, struct sc_pin_cmd_data *data, int *tries_left)
+{
+	int rc;
+	sc_path_t path;
+	sc_apdu_t apdu;
+
+	LOG_FUNC_CALLED(card->ctx);
+
+	if (tries_left) {
+		*tries_left = -1;
+	}
+
+	switch (data->pin_reference) {
+	case 1:
+		sc_format_path(JPKI_AUTH_PIN, &path);
+		path.type = SC_PATH_TYPE_FILE_ID;
+		rc = sc_select_file(card, &path, NULL);
+		break;
+	case 2:
+		sc_format_path(JPKI_SIGN_PIN, &path);
+		path.type = SC_PATH_TYPE_FILE_ID;
+		rc = sc_select_file(card, &path, NULL);
+		break;
+	default:
+		sc_log(card->ctx, "Unknown PIN reference: %d", data->pin_reference);
+		LOG_FUNC_RETURN(card->ctx, SC_ERROR_INVALID_ARGUMENTS);
+	}
+	LOG_TEST_RET(card->ctx, rc, "SELECT_FILE error");
+
+	switch (data->cmd) {
+	case SC_PIN_CMD_VERIFY:
+		sc_format_apdu(card, &apdu, SC_APDU_CASE_3, 0x20, 0x00, 0x80);
+		apdu.data = data->pin1.data;
+		apdu.datalen = data->pin1.len;
+		apdu.lc = data->pin1.len;
+		rc = sc_transmit_apdu(card, &apdu);
+		LOG_TEST_RET(card->ctx, rc, "APDU transmit failed");
+		rc = sc_check_sw(card, apdu.sw1, apdu.sw2);
+		LOG_TEST_RET(card->ctx, rc, "VERIFY failed");
+		break;
+	case SC_PIN_CMD_GET_INFO:
+		sc_format_apdu(card, &apdu, SC_APDU_CASE_1, 0x20, 0x00, 0x80);
+		rc = sc_transmit_apdu(card, &apdu);
+		LOG_TEST_RET(card->ctx, rc, "APDU transmit failed");
+		if (apdu.sw1 != 0x63) {
+			sc_log(card->ctx, "VERIFY GET_INFO error");
+			LOG_FUNC_RETURN(card->ctx, SC_ERROR_CARD_CMD_FAILED);
+		}
+		if (tries_left) {
+			*tries_left = apdu.sw2 - 0xC0;
+		}
+		break;
+	default:
+		sc_log(card->ctx, "Card does not support PIN command: %d", data->cmd);
+		LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
+	}
+
+	LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
+}
+
+static int
+jpki_set_security_env(sc_card_t * card,
+		const sc_security_env_t * env, int se_num)
+{
+	int rc;
+	sc_path_t path;
+
+	LOG_FUNC_CALLED(card->ctx);
+	sc_log(card->ctx,
+		"flags=%08x op=%d alg=%d algf=%08x algr=%08x kr0=%02x, krfl=%d",
+		env->flags, env->operation, env->algorithm,
+		env->algorithm_flags, env->algorithm_ref, env->key_ref[0],
+		env->key_ref_len);
+
+	switch (env->operation) {
+	case SC_SEC_OPERATION_SIGN:
+		break;
+	default:
+		LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
+	}
+
+	switch (env->key_ref[0]) {
+	case 1:
+		sc_format_path(JPKI_AUTH_KEY, &path);
+		break;
+	case 2:
+		sc_format_path(JPKI_SIGN_KEY, &path);
+		break;
+	default:
+		LOG_FUNC_RETURN(card->ctx, SC_ERROR_NOT_SUPPORTED);
+	}
+	path.type = SC_PATH_TYPE_FILE_ID;
+	rc = sc_select_file(card, &path, NULL);
+	LOG_TEST_RET(card->ctx, rc, "select key failed");
+
+	LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
+}
+
+static int
+jpki_compute_signature(sc_card_t * card,
+		const u8 * data, size_t datalen, u8 * out, size_t outlen)
+{
+	int rc;
+	sc_apdu_t apdu;
+	unsigned char resp[SC_MAX_APDU_BUFFER_SIZE];
+
+	LOG_FUNC_CALLED(card->ctx);
+
+	sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x2A, 0x00, 0x80);
+	apdu.cla = 0x80;
+	apdu.data = data;
+	apdu.datalen = datalen;
+	apdu.lc = datalen;
+	apdu.resp = resp;
+	apdu.resplen = sizeof(resp);
+	apdu.le = 0;
+	rc = sc_transmit_apdu(card, &apdu);
+	LOG_TEST_RET(card->ctx, rc, "APDU transmit failed");
+	rc = sc_check_sw(card, apdu.sw1, apdu.sw2);
+	LOG_TEST_RET(card->ctx, rc, "SW Check failed");
+	if (apdu.resplen > outlen) {
+		LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
+	}
+	memcpy(out, resp, apdu.resplen);
+	LOG_FUNC_RETURN(card->ctx, apdu.resplen);
+}
+
+static struct sc_card_driver *
+sc_get_driver(void)
+{
+	struct sc_card_driver *iso_drv = sc_get_iso7816_driver();
+
+	jpki_ops = *iso_drv->ops;
+	jpki_ops.match_card = jpki_match_card;
+	jpki_ops.init = jpki_init;
+	jpki_ops.finish = jpki_finish;
+	jpki_ops.select_file = jpki_select_file;
+	jpki_ops.read_binary = jpki_read_binary;
+	jpki_ops.pin_cmd = jpki_pin_cmd;
+	jpki_ops.set_security_env = jpki_set_security_env;
+	jpki_ops.compute_signature = jpki_compute_signature;
+
+	return &jpki_drv;
+}
+
+struct sc_card_driver *
+sc_get_jpki_driver(void)
+{
+	return sc_get_driver();
+}
diff --git a/src/libopensc/cards.h b/src/libopensc/cards.h
index 4a813f9..94ec1d4 100644
--- a/src/libopensc/cards.h
+++ b/src/libopensc/cards.h
@@ -225,6 +225,9 @@ enum {
 	SC_CARD_TYPE_GIDS_GENERIC,
 	SC_CARD_TYPE_GIDS_V1,
 	SC_CARD_TYPE_GIDS_V2,
+
+	/* JPKI cards */
+	SC_CARD_TYPE_JPKI_BASE = 31000,
 };
 
 extern sc_card_driver_t *sc_get_default_driver(void);
@@ -264,6 +267,7 @@ extern sc_card_driver_t *sc_get_dnie_driver(void);
 extern sc_card_driver_t *sc_get_isoApplet_driver(void);
 extern sc_card_driver_t *sc_get_masktech_driver(void);
 extern sc_card_driver_t *sc_get_gids_driver(void);
+extern sc_card_driver_t *sc_get_jpki_driver(void);
 
 #ifdef __cplusplus
 }
diff --git a/src/libopensc/ctx.c b/src/libopensc/ctx.c
index e30aaff..bdcb5ab 100644
--- a/src/libopensc/ctx.c
+++ b/src/libopensc/ctx.c
@@ -118,6 +118,7 @@ static const struct _sc_driver_entry internal_card_drivers[] = {
 	{ "gids",	(void *(*)(void)) sc_get_gids_driver },
 #endif
 	{ "openpgp",	(void *(*)(void)) sc_get_openpgp_driver },
+	{ "jpki",	(void *(*)(void)) sc_get_jpki_driver },
 	/* The default driver should be last, as it handles all the
 	 * unrecognized cards. */
 	{ "default",	(void *(*)(void)) sc_get_default_driver },
diff --git a/src/libopensc/jpki.h b/src/libopensc/jpki.h
new file mode 100644
index 0000000..79aad6a
--- /dev/null
+++ b/src/libopensc/jpki.h
@@ -0,0 +1,43 @@
+/*
+ * jpki.h: Support for JPKI(Japanese Individual Number Cards).
+ *
+ * Copyright (C) 2016, HAMANO Tsukasa <hamano at osstech.co.jp>
+ *
+ * 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
+ */
+
+#ifndef _OPENSC_JPKI_H
+#define _OPENSC_JPKI_H
+
+#define SELECT_MF 0
+#define SELECT_JPKI_AP 1
+
+#define AID_JPKI "D392f000260100000001"
+#define JPKI_AUTH_KEY "0017"
+#define JPKI_AUTH_PIN "0018"
+
+#define JPKI_SIGN_KEY "001A"
+#define JPKI_SIGN_PIN "001B"
+
+#define JPKI_DRVDATA(card) ((struct jpki_private_data *) ((card)->drv_data))
+
+struct jpki_private_data {
+	sc_file_t *mf;
+	int selected;
+};
+
+int jpki_select_ap(struct sc_card *card);
+
+#endif
diff --git a/src/libopensc/pkcs15-jpki.c b/src/libopensc/pkcs15-jpki.c
new file mode 100644
index 0000000..ecf2152
--- /dev/null
+++ b/src/libopensc/pkcs15-jpki.c
@@ -0,0 +1,190 @@
+/*
+ * PKCS15 emulation layer for JPKI(Japanese Individual Number Cards).
+ *
+ * Copyright (C) 2016, HAMANO Tsukasa <hamano at osstech.co.jp>
+ *
+ * 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
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "common/compat_strlcpy.h"
+#include "common/compat_strlcat.h"
+
+#include "internal.h"
+#include "pkcs15.h"
+#include "jpki.h"
+
+int sc_pkcs15emu_jpki_init_ex(sc_pkcs15_card_t *, struct sc_aid *,
+		sc_pkcs15emu_opt_t *);
+
+static int
+sc_pkcs15emu_jpki_init(sc_pkcs15_card_t * p15card)
+{
+	sc_card_t *card = p15card->card;
+	struct jpki_private_data *drvdata = JPKI_DRVDATA(card);
+	int i, rc;
+
+	LOG_FUNC_CALLED(p15card->card->ctx);
+
+	p15card->tokeninfo->label = strdup("JPKI");
+	p15card->tokeninfo->manufacturer_id = strdup("JPKI");
+	/* set NULL until we found serial number */
+	p15card->tokeninfo->serial_number = NULL;
+
+	/* Select application directory */
+	if (drvdata->selected != SELECT_JPKI_AP) {
+		rc = jpki_select_ap(card);
+		LOG_TEST_RET(card->ctx, rc, "select AP failed");
+		drvdata->selected = SELECT_JPKI_AP;
+	}
+
+	/* add certificates */
+	for (i = 0; i < 2; i++) {
+		static const char *jpki_cert_names[2] = {
+			"User Authentication Certificate",
+			"Digital Signature Certificate"
+		};
+		static char const *jpki_cert_paths[2] = {
+			"000A",
+			"0001"
+		};
+		static int jpki_cert_ids[2] = { 1, 2 };
+
+		struct sc_pkcs15_cert_info cert_info;
+		struct sc_pkcs15_object cert_obj;
+		memset(&cert_info, 0, sizeof (cert_info));
+		memset(&cert_obj, 0, sizeof (cert_obj));
+
+		cert_info.id.value[0] = jpki_cert_ids[i];
+		cert_info.id.len = 1;
+		sc_format_path(jpki_cert_paths[i], &cert_info.path);
+		cert_info.path.type = SC_PATH_TYPE_FILE_ID;
+
+		strlcpy(cert_obj.label, jpki_cert_names[i], sizeof (cert_obj.label));
+		cert_obj.flags = 0;
+
+		rc = sc_pkcs15emu_add_x509_cert(p15card, &cert_obj, &cert_info);
+		if (rc < 0)
+			LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
+
+	}
+
+	/* add pins */
+	for (i = 0; i < 2; i++) {
+		static const char *jpki_pin_names[2] = {
+			"User Authentication PIN",
+			"Digital Signature PIN"
+		};
+		static const int jpki_pin_min[2] = { 4, 6 };
+		static const int jpki_pin_max[2] = { 4, 16 };
+		static const int jpki_pin_ref[2] = { 1, 2 };
+		static const int jpki_pin_authid[2] = { 1, 2 };
+		static const int jpki_pin_flags[2] = { 0, 0 };
+		static const int jpki_pin_max_tries[2] = { 5, 3 };
+
+		struct sc_pkcs15_auth_info pin_info;
+		struct sc_pkcs15_object pin_obj;
+		struct sc_pin_cmd_data pin_cmd_data;
+		memset(&pin_info, 0, sizeof (pin_info));
+		memset(&pin_obj, 0, sizeof (pin_obj));
+		memset(&pin_cmd_data, 0, sizeof(pin_cmd_data));
+
+		pin_info.auth_id.len = 1;
+		pin_info.auth_id.value[0] = jpki_pin_authid[i];
+		pin_info.auth_type = SC_PKCS15_PIN_AUTH_TYPE_PIN;
+		pin_info.attrs.pin.reference = jpki_pin_ref[i];
+		pin_info.attrs.pin.flags = jpki_pin_flags[i];
+		pin_info.attrs.pin.type = SC_PKCS15_PIN_TYPE_ASCII_NUMERIC;
+		pin_info.attrs.pin.min_length = jpki_pin_min[i];
+		pin_info.attrs.pin.stored_length = 0;
+		pin_info.attrs.pin.max_length = jpki_pin_max[i];
+		pin_info.attrs.pin.pad_char = '\0';
+		pin_info.max_tries = jpki_pin_max_tries[i];
+		pin_info.tries_left = -1;
+
+		pin_cmd_data.cmd = SC_PIN_CMD_GET_INFO;
+		pin_cmd_data.pin_type = SC_AC_CHV;
+		pin_cmd_data.pin_reference = jpki_pin_ref[i];
+		rc = sc_pin_cmd(card, &pin_cmd_data, &pin_info.tries_left);
+		LOG_TEST_RET(card->ctx, rc, "sc_pin_cmd failed");
+		strlcpy(pin_obj.label, jpki_pin_names[i], sizeof(pin_obj.label));
+		pin_obj.flags = jpki_pin_flags[i];
+
+		rc = sc_pkcs15emu_add_pin_obj(p15card, &pin_obj, &pin_info);
+		if (rc < 0)
+			LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
+	}
+
+	/* add private keys */
+	for (i = 0; i < 2; i++) {
+		static int prkey_pin[2] = { 1, 2 };
+		static int prkey_usage[2] = {
+			SC_PKCS15_PRKEY_USAGE_ENCRYPT |
+			SC_PKCS15_PRKEY_USAGE_DECRYPT |
+			SC_PKCS15_PRKEY_USAGE_SIGN,
+			SC_PKCS15_PRKEY_USAGE_NONREPUDIATION
+		};
+		static const char *prkey_name[2] = {
+			"User Authentication Key",
+			"Digital Signature Key"
+		};
+
+		struct sc_pkcs15_prkey_info prkey_info;
+		struct sc_pkcs15_object prkey_obj;
+
+		memset(&prkey_info, 0, sizeof (prkey_info));
+		memset(&prkey_obj, 0, sizeof (prkey_obj));
+
+		prkey_info.id.len = 1;
+		prkey_info.id.value[0] = prkey_pin[i];
+		prkey_info.usage = prkey_usage[i];
+		prkey_info.native = 1;
+		prkey_info.key_reference = i + 1;
+		prkey_info.modulus_length = 2048;
+
+		strlcpy(prkey_obj.label, prkey_name[i], sizeof (prkey_obj.label));
+		prkey_obj.auth_id.len = 1;
+		prkey_obj.auth_id.value[0] = prkey_pin[i];
+		prkey_obj.user_consent = 0;
+		prkey_obj.flags = SC_PKCS15_CO_FLAG_PRIVATE;
+
+		rc = sc_pkcs15emu_add_rsa_prkey(p15card, &prkey_obj, &prkey_info);
+		if (rc < 0)
+			LOG_FUNC_RETURN(card->ctx, SC_ERROR_INTERNAL);
+	}
+	LOG_FUNC_RETURN(card->ctx, SC_SUCCESS);
+}
+
+int
+sc_pkcs15emu_jpki_init_ex(sc_pkcs15_card_t * p15card,
+			  struct sc_aid *aid, sc_pkcs15emu_opt_t * opts)
+{
+	if (opts && opts->flags & SC_PKCS15EMU_FLAGS_NO_CHECK) {
+		return sc_pkcs15emu_jpki_init(p15card);
+	}
+	else {
+		if (p15card->card->type != SC_CARD_TYPE_JPKI_BASE)
+			return SC_ERROR_WRONG_CARD;
+
+		return sc_pkcs15emu_jpki_init(p15card);
+	}
+}
diff --git a/src/libopensc/pkcs15-syn.c b/src/libopensc/pkcs15-syn.c
index c18b0fe..a89fc48 100644
--- a/src/libopensc/pkcs15-syn.c
+++ b/src/libopensc/pkcs15-syn.c
@@ -56,6 +56,7 @@ struct sc_pkcs15_emulator_handler builtin_emulators[] = {
 	{ "dnie",       sc_pkcs15emu_dnie_init_ex   },
 	{ "gids",       sc_pkcs15emu_gids_init_ex   },
 	{ "iasecc",	sc_pkcs15emu_iasecc_init_ex   },
+	{ "jpki",	sc_pkcs15emu_jpki_init_ex },
 	{ NULL, NULL }
 };
 
diff --git a/src/libopensc/pkcs15-syn.h b/src/libopensc/pkcs15-syn.h
index 8a52aa6..8fa7912 100644
--- a/src/libopensc/pkcs15-syn.h
+++ b/src/libopensc/pkcs15-syn.h
@@ -50,6 +50,7 @@ int sc_pkcs15emu_sc_hsm_init_ex(sc_pkcs15_card_t *,	struct sc_aid *, sc_pkcs15em
 int sc_pkcs15emu_dnie_init_ex(sc_pkcs15_card_t *,	struct sc_aid *, sc_pkcs15emu_opt_t *);
 int sc_pkcs15emu_gids_init_ex(sc_pkcs15_card_t *,	struct sc_aid *, sc_pkcs15emu_opt_t *);
 int sc_pkcs15emu_iasecc_init_ex(sc_pkcs15_card_t *,	struct sc_aid *, sc_pkcs15emu_opt_t *);
+int sc_pkcs15emu_jpki_init_ex(sc_pkcs15_card_t *,	struct sc_aid *, sc_pkcs15emu_opt_t *);
 
 struct sc_pkcs15_emulator_handler {
 	const char *name;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-opensc/opensc.git



More information about the pkg-opensc-commit mailing list