Skip to content

Commit

Permalink
sc-hsm-tool: use fread_to_eof()
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Braun committed Jun 20, 2019
1 parent c945c08 commit a98d1c5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/tools/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ openpgp_tool_SOURCES = openpgp-tool.c util.c
openpgp_tool_LDADD = $(OPTIONAL_OPENSSL_LIBS)
iasecc_tool_SOURCES = iasecc-tool.c util.c
iasecc_tool_LDADD = $(OPTIONAL_OPENSSL_LIBS)
sc_hsm_tool_SOURCES = sc-hsm-tool.c util.c
sc_hsm_tool_SOURCES = sc-hsm-tool.c fread_to_eof.c util.c
sc_hsm_tool_LDADD = $(OPTIONAL_OPENSSL_LIBS)
dnie_tool_SOURCES = dnie-tool.c util.c
dnie_tool_LDADD = $(OPTIONAL_OPENSSL_LIBS)
Expand Down
5 changes: 5 additions & 0 deletions src/tools/Makefile.mak
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ pkcs11-register.exe: pkcs11-register-cmdline.obj fread_to_eof.obj $(LIBS)
link $(LINKFLAGS) /pdb:$*.pdb /out:$@ $*.obj pkcs11-register-cmdline.obj fread_to_eof.obj versioninfo-tools.res $(LIBS) gdi32.lib shell32.lib User32.lib ws2_32.lib
mt -manifest exe.manifest -outputresource:$@;1

sc-hsm-tool.c.exe: sc-hsm-tool.obj fread_to_eof.obj $(OBJECTS) $(LIBS)
cl $(COPTS) /c $<
link $(LINKFLAGS) /pdb:$*.pdb /out:$@ $*.obj $(OBJECTS) $(LIBS) $(OPENSSL_LIB) gdi32.lib shell32.lib User32.lib ws2_32.lib
mt -manifest exe.manifest -outputresource:$@;1

.c.exe:
cl $(COPTS) /c $<
link $(LINKFLAGS) /pdb:$*.pdb /out:$@ $*.obj $(OBJECTS) $(LIBS) $(OPENSSL_LIB) gdi32.lib shell32.lib User32.lib ws2_32.lib
Expand Down
36 changes: 9 additions & 27 deletions src/tools/sc-hsm-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "libopensc/asn1.h"
#include "libopensc/log.h"
#include "libopensc/card-sc-hsm.h"
#include "fread_to_eof.h"
#include "util.h"

static const char *app_name = "sc-hsm-tool";
Expand Down Expand Up @@ -1882,44 +1883,25 @@ static int register_public_key_with_card(sc_context_t *ctx, sc_card_t *card, con

static int register_public_key(sc_context_t *ctx, sc_card_t *card, const char *inf)
{
FILE *in;
struct stat sb;
u8 *pka;
u8 tag = SC_ASN1_TAG_CONSTRUCTED | SC_ASN1_TAG_SEQUENCE; /* 0x30 */
unsigned int cla_out, tag_out;
const u8 *buf;
const u8 *pk;
const u8 *devcert;
const u8 *dicacert;
size_t taglen, pk_len, devcert_len, dicacert_len;
size_t pkalen, taglen, pk_len, devcert_len, dicacert_len;
int r;

if (!(in = fopen(inf, "rb"))) {
perror(inf);
return -1;
}
if (fstat(fileno(in), &sb)) {
perror("cannot fstat");
fclose(in);
/* read .pka file */
if (fread_to_eof(inf, &pka, &pkalen)) {
return -1;
}
if (sb.st_size == 0) {
if (pkalen == 0) {
fprintf(stderr, "File is empty\n");
fclose(in);
return -1;
}
if (!(pka = malloc(sb.st_size))) {
fprintf(stderr, "Malloc failed\n");
fclose(in);
return -1;
}
if (fread(pka, 1, sb.st_size, in) != (size_t)sb.st_size) {
perror(inf);
free(pka);
fclose(in);
return -1;
}
fclose(in);
if (pka[0] != tag) {
fprintf(stderr, "File does not contain a public key with certificates\n");
free(pka);
Expand All @@ -1928,14 +1910,14 @@ static int register_public_key(sc_context_t *ctx, sc_card_t *card, const char *i

/* read ASN.1 sequence */
buf = pka;
if ((r = sc_asn1_read_tag(&buf, sb.st_size, &cla_out, &tag_out, &taglen)) < 0) {
if ((r = sc_asn1_read_tag(&buf, pkalen, &cla_out, &tag_out, &taglen)) < 0) {
fprintf(stderr, "Error reading ASN.1 sequence: %s\n", sc_strerror(r));
free(pka);
return -1;
}

/* read public key */
if ((r = sc_asn1_read_tag(&buf, sb.st_size, &cla_out, &tag_out, &taglen)) < 0) {
if ((r = sc_asn1_read_tag(&buf, pkalen, &cla_out, &tag_out, &taglen)) < 0) {
fprintf(stderr, "Error reading ASN.1 sequence: %s\n", sc_strerror(r));
free(pka);
return -1;
Expand All @@ -1945,7 +1927,7 @@ static int register_public_key(sc_context_t *ctx, sc_card_t *card, const char *i
buf += taglen;

/* read device certificate */
if ((r = sc_asn1_read_tag(&buf, sb.st_size, &cla_out, &tag_out, &taglen)) < 0) {
if ((r = sc_asn1_read_tag(&buf, pkalen, &cla_out, &tag_out, &taglen)) < 0) {
fprintf(stderr, "Error reading ASN.1 sequence: %s\n", sc_strerror(r));
free(pka);
return -1;
Expand All @@ -1955,7 +1937,7 @@ static int register_public_key(sc_context_t *ctx, sc_card_t *card, const char *i
buf += taglen;

/* read device CA */
if ((r = sc_asn1_read_tag(&buf, sb.st_size, &cla_out, &tag_out, &taglen)) < 0) {
if ((r = sc_asn1_read_tag(&buf, pkalen, &cla_out, &tag_out, &taglen)) < 0) {
fprintf(stderr, "Error reading ASN.1 sequence: %s\n", sc_strerror(r));
free(pka);
return -1;
Expand Down

0 comments on commit a98d1c5

Please sign in to comment.