Skip to content

Commit

Permalink
tools: Split openpgp-tool's prettify_* functions into another file.
Browse files Browse the repository at this point in the history
No change to source code other than the relocation.
In preparation for unit testing.
  • Loading branch information
vpelletier authored and Jakuje committed Sep 19, 2021
1 parent d2fddc4 commit 3496768
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 207 deletions.
5 changes: 3 additions & 2 deletions src/tools/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ EXTRA_DIST = Makefile.mak versioninfo-tools.rc.in versioninfo-opensc-notify.rc.i

noinst_HEADERS = util.h fread_to_eof.h \
egk-tool-cmdline.h goid-tool-cmdline.h npa-tool-cmdline.h \
opensc-asn1-cmdline.h opensc-notify-cmdline.h pkcs11-register-cmdline.h
opensc-asn1-cmdline.h opensc-notify-cmdline.h pkcs11-register-cmdline.h \
openpgp-tool-helpers.h
noinst_PROGRAMS = sceac-example
bin_PROGRAMS = opensc-tool opensc-explorer opensc-notify \
pkcs15-tool pkcs15-crypt pkcs11-tool pkcs11-register \
Expand Down Expand Up @@ -77,7 +78,7 @@ netkey_tool_SOURCES = netkey-tool.c
netkey_tool_LDADD = $(OPTIONAL_OPENSSL_LIBS)
westcos_tool_SOURCES = westcos-tool.c util.c
westcos_tool_LDADD = $(OPTIONAL_OPENSSL_LIBS)
openpgp_tool_SOURCES = openpgp-tool.c util.c
openpgp_tool_SOURCES = openpgp-tool.c util.c openpgp-tool-helpers.c
openpgp_tool_LDADD = $(OPTIONAL_OPENSSL_LIBS)
iasecc_tool_SOURCES = iasecc-tool.c util.c
iasecc_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 @@ -57,6 +57,11 @@ pkcs15-tool.exe: pkcs15-tool.obj $(TOPDIR)\src\pkcs11\pkcs11-display.obj
link $(LINKFLAGS) /pdb:$*.pdb /out:$@ $*.obj $(TOPDIR)\src\pkcs11\pkcs11-display.obj $(OBJECTS) $(LIBS) $(OPENSSL_LIB) gdi32.lib shell32.lib User32.lib ws2_32.lib shlwapi.lib
mt -manifest exe.manifest -outputresource:$@;1

openpgp-tool.exe: openpgp-tool-helpers.obj $(LIBS)
cl $(COPTS) /c $*.c
link $(LINKFLAGS) /pdb:$*.pdb /out:$@ $*.obj openpgp-tool-helpers.obj $(OBJECTS) $(LIBS) gdi32.lib shell32.lib User32.lib ws2_32.lib shlwapi.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 shlwapi.lib
Expand Down
222 changes: 222 additions & 0 deletions src/tools/openpgp-tool-helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
/*
* openpgp-tool-helpers.c: OpenPGP card utility
*
* Copyright (C) 2012-2020 Peter Marschall <[email protected]>
*
* 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 "config.h"

#include <stdio.h>
#include <time.h>
#include "openpgp-tool-helpers.h"
#include "util.h"


/* prettify hex */
char *prettify_hex(u8 *data, size_t length, char *buffer, size_t buflen)
{
if (data != NULL) {
int r = sc_bin_to_hex(data, length, buffer, buflen, ':');

if (r == SC_SUCCESS)
return buffer;
}
return NULL;
}


/* prettify algorithm parameters */
char *prettify_algorithm(u8 *data, size_t length)
{
if (data != NULL && length >= 1) {
static char result[64]; /* large enough */

if (data[0] == 0x01 && length >= 5) { /* RSA */
unsigned short modulus = (data[1] << 8) + data[2];
snprintf(result, sizeof(result), "RSA%u", modulus);
return result;
}
else if (data[0] == 0x12) { /* ECDH */
strcpy(result, "ECDH");
return result;
}
else if (data[0] == 0x13) { /* ECDSA */
strcpy(result, "ECDSA");
return result;
}
}
return NULL;
}


/* prettify date/time */
char *prettify_date(u8 *data, size_t length)
{
if (data != NULL && length == 4) {
time_t time = (time_t) (data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]);
struct tm tm;
static char result[64]; /* large enough */

#ifdef _WIN32
if (0 != gmtime_s(&tm, &time))
return NULL;
#else
if (NULL == gmtime_r(&time, &tm))
return NULL;
#endif
strftime(result, sizeof(result), "%Y-%m-%d %H:%M:%S", &tm);
return result;
}
return NULL;
}


#define BCD2CHAR(x) (((((x) & 0xF0) >> 4) * 10) + ((x) & 0x0F))

/* prettify OpenPGP card version */
char *prettify_version(u8 *data, size_t length)
{
if (data != NULL && length >= 2) {
static char result[10]; /* large enough for even 2*3 digits + separator */
int major = BCD2CHAR(data[0]);
int minor = BCD2CHAR(data[1]);

sprintf(result, "%d.%d", major, minor);
return result;
}
return NULL;
}


/* prettify manufacturer */
char *prettify_manufacturer(u8 *data, size_t length)
{
if (data != NULL && length >= 2) {
unsigned int manuf = (data[0] << 8) + data[1];

switch (manuf) {
case 0x0001: return "PPC Card Systems";
case 0x0002: return "Prism";
case 0x0003: return "OpenFortress";
case 0x0004: return "Wewid";
case 0x0005: return "ZeitControl";
case 0x0006: return "Yubico";
case 0x0007: return "OpenKMS";
case 0x0008: return "LogoEmail";
case 0x0009: return "Fidesmo";
case 0x000A: return "Dangerous Things";
case 0x000B: return "Feitian Technologies";

case 0x002A: return "Magrathea";
case 0x0042: return "GnuPG e.V.";

case 0x1337: return "Warsaw Hackerspace";
case 0x2342: return "warpzone"; /* hackerspace Muenster. */
case 0x4354: return "Confidential Technologies"; /* cotech.de */
case 0x5443: return "TIF-IT e.V.";
case 0x63AF: return "Trustica";
case 0xBA53: return "c-base e.V.";
case 0xBD0E: return "Paranoidlabs";
case 0xF517: return "FSIJ";
case 0xF5EC: return "F-Secure";

/* 0x0000 and 0xFFFF are defined as test cards per spec,
0xFF00 to 0xFFFE are assigned for use with randomly created
serial numbers. */
case 0x0000:
case 0xffff: return "test card";
default: return (manuf & 0xff00) == 0xff00 ? "unmanaged S/N range" : "unknown";
}
}
return NULL;
}


/* prettify pure serial number */
char *prettify_serialnumber(u8 *data, size_t length)
{
if (data != NULL && length >= 4) {
static char result[15]; /* large enough for even 2*3 digits + separator */
sprintf(result, "%02X%02X%02X%02X", data[0], data[1], data[2], data[3]);
return result;
}
return NULL;
}


/* prettify card holder's name */
char *prettify_name(u8 *data, size_t length)
{
if (data != NULL && length > 0) {
char *src = (char *) data;
char *dst = (char *) data;

while (*src != '\0' && length > 0) {
*dst = *src++;
length--;
if (*dst == '<') {
if (length > 0 && *src == '<') {
src++;
length--;
}
*dst = ' ';
}
dst++;
}
*dst = '\0';
return (char *) data;
}
return NULL;
}


/* prettify language */
char *prettify_language(u8 *data, size_t length)
{
if (data != NULL && length > 0) {
char *str = (char *) data;

switch (strlen(str)) {
case 8: memmove(str+7, str+6, 1+strlen(str+6));
str[6] = ',';
/* fall through */
case 6: memmove(str+5, str+4, 1+strlen(str+4));
str[4] = ',';
/* fall through */
case 4: memmove(str+3, str+2, 1+strlen(str+2));
str[2] = ',';
/* fall through */
case 2: return str;
}
}
return NULL;
}


/* convert the raw ISO-5218 SEX value to an english word */
char *prettify_gender(u8 *data, size_t length)
{
if (data != NULL && length > 0) {
switch (*data) {
case '0': return "unknown";
case '1': return "male";
case '2': return "female";
case '9': return "not announced";
}
}
return NULL;
}
38 changes: 38 additions & 0 deletions src/tools/openpgp-tool-helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* openpgp-tool-helpers.h: OpenPGP card utility
*
* Copyright (C) 2012-2020 Peter Marschall <[email protected]>
*
* 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 OPENPGP_TOOL_HELPERS_H
#define OPENPGP_TOOL_HELPERS_H

#include "util.h"


char *prettify_hex(u8 *data, size_t length, char *buffer, size_t buflen);
char *prettify_algorithm(u8 *data, size_t length);
char *prettify_date(u8 *data, size_t length);
char *prettify_version(u8 *data, size_t length);
char *prettify_manufacturer(u8 *data, size_t length);
char *prettify_serialnumber(u8 *data, size_t length);
char *prettify_name(u8 *data, size_t length);
char *prettify_language(u8 *data, size_t length);
char *prettify_gender(u8 *data, size_t length);


#endif /* OPENPGP_TOOL_HELPERS_H */
Loading

0 comments on commit 3496768

Please sign in to comment.