diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..a7058423 --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +BasedOnStyle: Google +IndentWidth: 4 + diff --git a/.gitignore b/.gitignore index 747974b1..72f5de4f 100644 --- a/.gitignore +++ b/.gitignore @@ -80,6 +80,7 @@ src/tools/pkcs15-init src/tools/eidenv src/tools/opensc-explorer src/tools/cardos-info +src/tools/gcns src/tools/sceac-example src/tools/opensc-notify src/tools/opensc-notify.plist diff --git a/src/tools/Makefile.am b/src/tools/Makefile.am index fe67ad01..87969352 100644 --- a/src/tools/Makefile.am +++ b/src/tools/Makefile.am @@ -29,7 +29,8 @@ noinst_HEADERS = util.h fread_to_eof.h \ noinst_PROGRAMS = sceac-example bin_PROGRAMS = opensc-tool opensc-explorer opensc-notify \ pkcs15-tool pkcs15-crypt pkcs11-tool pkcs11-register \ - cardos-tool eidenv openpgp-tool iasecc-tool egk-tool opensc-asn1 goid-tool + cardos-tool eidenv openpgp-tool iasecc-tool egk-tool opensc-asn1 goid-tool \ + gcns if ENABLE_OPENSSL bin_PROGRAMS += cryptoflex-tool pkcs15-init netkey-tool piv-tool \ westcos-tool sc-hsm-tool dnie-tool gids-tool npa-tool @@ -52,6 +53,8 @@ piv_tool_SOURCES = piv-tool.c util.c piv_tool_LDADD = $(OPTIONAL_OPENSSL_LIBS) opensc_explorer_SOURCES = opensc-explorer.c util.c opensc_explorer_LDADD = $(OPTIONAL_READLINE_LIBS) +gcns_SOURCES = gcns.c util.c +gcns_LDADD = $(OPTIONAL_READLINE_LIBS) pkcs15_tool_SOURCES = pkcs15-tool.c util.c ../pkcs11/pkcs11-display.c ../pkcs11/pkcs11-display.h pkcs15_tool_LDADD = $(OPTIONAL_OPENSSL_LIBS) pkcs11_tool_SOURCES = pkcs11-tool.c util.c diff --git a/src/tools/gcns.c b/src/tools/gcns.c new file mode 100644 index 00000000..59e594fa --- /dev/null +++ b/src/tools/gcns.c @@ -0,0 +1,107 @@ +/* + * gcns.c: A reader of Italian healtcare smartcards with libopensc + * + * Copyright (C) 2022 Giovan Battista Rolandi + * based on previous work by + * Copyright (C) 2001 Juha Yrjölä + * + * 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 + */ + +#include +#include +#include +#include +#include + +#include "config.h" +#ifdef ENABLE_READLINE +#include +#include +#endif +#if !defined(_WIN32) +#include /* for htons() */ +#endif + +#include + +#include "common/compat_strlcpy.h" +#include "libopensc/asn1.h" +#include "libopensc/cardctl.h" +#include "libopensc/cards.h" +#include "libopensc/internal.h" +#include "libopensc/iso7816.h" +#include "libopensc/log.h" +#include "libopensc/opensc.h" +#include "util.h" + +static int opt_wait = 0; +static const char *opt_reader = NULL; +static sc_context_t *ctx = NULL; +static sc_card_t *card = NULL; + +static void die(int ret) { + if (card) { + sc_disconnect_card(card); + } + if (ctx) sc_release_context(ctx); + exit(ret); +} + +int main(int argc, char *argv[]) { + int r, err = 0; + sc_context_param_t ctx_param; + int lcycle = SC_CARDCTRL_LIFECYCLE_ADMIN; + + printf("OpenSC version: %s\n", sc_get_version()); + + memset(&ctx_param, 0, sizeof(ctx_param)); + ctx_param.ver = 0; + + r = sc_context_create(&ctx, &ctx_param); + if (r) { + fprintf(stderr, "Failed to establish context: %s\n", sc_strerror(r)); + return 1; + } + + ctx->flags |= SC_CTX_FLAG_ENABLE_DEFAULT_DRIVER; + + err = util_connect_card_ex(ctx, &card, opt_reader, opt_wait, 0, 0); + if (err) goto end; + + r = sc_lock(card); + if (r == SC_SUCCESS) + r = sc_card_ctl(card, SC_CARDCTL_LIFECYCLE_SET, &lcycle); + sc_unlock(card); + if (r && r != SC_ERROR_NOT_SUPPORTED) + printf("unable to change lifecycle: %s\n", sc_strerror(r)); + + u8 buffer[2048]; + sc_path_t path; + sc_format_path("3F0011001102", &path); + r = sc_select_file(card, &path, NULL); + if (r) { + fprintf(stderr, "no select file 3F0011001102\n"); + } + r = sc_read_binary(card, 0, buffer, 0x180, 0); + if (r) { + fprintf(stderr, "no read binary: %d\n", r); + } + util_hex_dump_asc(stdout, buffer, r, 0); +end: + die(err); + + return 0; /* not reached */ +}