gcns.c: read Italian healthcare smart card

This commit is contained in:
giomba 2022-01-06 16:44:02 +01:00
parent c902e19921
commit 606edadb13
4 changed files with 115 additions and 1 deletions

3
.clang-format Normal file
View File

@ -0,0 +1,3 @@
BasedOnStyle: Google
IndentWidth: 4

1
.gitignore vendored
View File

@ -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

View File

@ -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

107
src/tools/gcns.c Normal file
View File

@ -0,0 +1,107 @@
/*
* gcns.c: A reader of Italian healtcare smartcards with libopensc
*
* Copyright (C) 2022 Giovan Battista Rolandi <giomba@linux.it>
* based on previous work by
* Copyright (C) 2001 Juha Yrjölä <juha.yrjola@iki.fi>
*
* 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 <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#ifdef ENABLE_READLINE
#include <readline/history.h>
#include <readline/readline.h>
#endif
#if !defined(_WIN32)
#include <arpa/inet.h> /* for htons() */
#endif
#include <getopt.h>
#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 */
}