- added PIN changing support

- started to work on nsplugin


git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@60 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
jey 2001-11-30 11:57:21 +00:00
parent 0217815665
commit 61ca346203
3 changed files with 145 additions and 70 deletions

View File

@ -133,7 +133,6 @@ CK_RV C_Sign(CK_SESSION_HANDLE hSession, /* the session's handle */
LOG("C_Sign(%d, 0x%x, %d, 0x%x, 0x%x)\n",
hSession, pData, ulDataLen, pSignature, pulSignatureLen);
hex_dump(pData, ulDataLen);
if (hSession < 1 || hSession > PKCS11_MAX_SESSIONS || session[hSession] == NULL)
return CKR_SESSION_HANDLE_INVALID;
@ -154,7 +153,6 @@ CK_RV C_Sign(CK_SESSION_HANDLE hSession, /* the session's handle */
}
LOG("Got signature, %d bytes (buffer was %d)\n", c, *pulSignatureLen);
hex_dump(signature, c);
memcpy(pSignature, signature, c);
*pulSignatureLen = c;

View File

@ -19,6 +19,7 @@
*/
#include <stdio.h>
#include <string.h>
#include "npapi.h"
/***********************************************************************
@ -32,6 +33,8 @@
typedef struct _PluginInstance
{
int nothing;
char *postUrl;
char *dataToSign;
} PluginInstance;
@ -55,7 +58,8 @@ NPError
NPP_GetValue(NPP instance, NPPVariable variable, void *value)
{
NPError err = NPERR_NO_ERROR;
printf("NPP_GetValue()\n");
switch (variable) {
case NPPVpluginNameString:
*((char **)value) = "Template plugin";
@ -82,14 +86,40 @@ NPP_Initialize(void)
jref
NPP_GetJavaClass()
{
return NULL;
printf("NPP_GetJavaClass()\n");
return NULL;
}
void
NPP_Shutdown(void)
{
printf("NPP_Shutdown()\n");
}
static NPError
post_data(NPP instance, const char *url, const char *target, uint32 len,
const char* buf)
{
NPError rv;
char headers[256], *sendbuf;
int hdrlen;
sprintf(headers, "Content-type: text/plain\r\n"
"Content-Length: %u\r\n\r\n", (unsigned int) len);
hdrlen = strlen(headers);
sendbuf = NPN_MemAlloc(hdrlen + len + 1);
if (sendbuf == NULL)
return NPERR_OUT_OF_MEMORY_ERROR;
memcpy(sendbuf, headers, hdrlen);
memcpy(sendbuf + hdrlen, buf, len);
sendbuf[hdrlen + len] = 0;
printf("Sending:\n---\n%s---\n", sendbuf);
printf("Url: '%s', target: '%s', len: %d\n", url, target, hdrlen + len);
rv = NPN_PostURL(instance, url, target, hdrlen + len, sendbuf, FALSE);
// NPN_MemFree(sendbuf);
return rv;
}
NPError
NPP_New(NPMIMEType pluginType,
@ -101,23 +131,35 @@ NPP_New(NPMIMEType pluginType,
NPSavedData* saved)
{
PluginInstance* This;
NPError rv;
int i;
const char *resp = "Testing...1234567890 And testing, and testing\n";
printf("NPP_New() called, attributes:\n");
for (i = 0; i < argc; i++) {
printf("'%s' = '%s'\n", argn[i], argv[i]);
}
printf("NPP_New()\n");
if (instance == NULL)
return NPERR_INVALID_INSTANCE_ERROR;
instance->pdata = NPN_MemAlloc(sizeof(PluginInstance));
This = (PluginInstance*) instance->pdata;
if (This != NULL)
return NPERR_NO_ERROR;
else
if (This == NULL)
return NPERR_OUT_OF_MEMORY_ERROR;
This->postUrl = This->dataToSign = NULL;
for (i = 0; i < argc; i++) {
if (strcmp(argn[i], "wsxaction") == 0) {
This->postUrl = strdup(argv[i]);
} else if (strcmp(argn[i], "wsxdatatosign") == 0) {
This->dataToSign = strdup(argv[i]);
} else
printf("'%s' = '%s'\n", argn[i], argv[i]);
}
if (This->postUrl == NULL)
return NPERR_GENERIC_ERROR;
printf("Posting to '%s'\n", This->postUrl);
rv = post_data(instance, This->postUrl, "_self", strlen(resp), resp);
printf("PostURL returned %d\n", rv);
return NPERR_NO_ERROR;
}
@ -126,6 +168,7 @@ NPP_Destroy(NPP instance, NPSavedData** save)
{
PluginInstance* This;
printf("NPP_Destroy()\n");
if (instance == NULL)
return NPERR_INVALID_INSTANCE_ERROR;
@ -137,11 +180,15 @@ NPP_Destroy(NPP instance, NPSavedData** save)
* that you want restored if this plugin instance is later
* recreated.
*/
if (This != NULL) {
NPN_MemFree(instance->pdata);
instance->pdata = NULL;
}
if (This == NULL)
return NPERR_NO_ERROR;
if (This->postUrl)
NPN_MemFree(This->postUrl);
if (This->dataToSign)
NPN_MemFree(This->dataToSign);
NPN_MemFree(instance->pdata);
instance->pdata = NULL;
return NPERR_NO_ERROR;
}
@ -181,7 +228,6 @@ NPP_NewStream(NPP instance,
NPBool seekable,
uint16 *stype)
{
NPByteRange range;
PluginInstance* This;
printf("NPP_NewStream()\n");
@ -223,11 +269,13 @@ NPP_WriteReady(NPP instance, NPStream *stream)
int32
NPP_Write(NPP instance, NPStream *stream, int32 offset, int32 len, void *buffer)
{
#if 0
if (instance != NULL)
{
PluginInstance* This = (PluginInstance*) instance->pdata;
}
printf("NPP_Write(offset %d, len %d)\n", offset, len);
#endif
printf("NPP_Write(offset %d, len %d)\n", (int) offset, (int) len);
return len; /* The number of bytes accepted */
}
@ -251,34 +299,17 @@ void
NPP_StreamAsFile(NPP instance, NPStream *stream, const char* fname)
{
PluginInstance* This;
FILE *inf, *outf;
unsigned char buf[1024];
int i;
if (instance != NULL)
This = (PluginInstance*) instance->pdata;
printf("NPP_StreamAsFile('%s')\n", fname);
inf = fopen(fname, "r");
if (inf == NULL)
return; /* FIXME */
i = 0;
outf = fopen("/tmp/empty.sgn", "w");
if (outf == NULL) {
fclose(inf);
return;
}
while ((i = fread(buf, 1, 1024, inf)) > 0) {
fwrite(buf, 1, i, outf);
}
fclose(outf);
fclose(inf);
}
void
NPP_Print(NPP instance, NPPrint* printInfo)
{
#if 0
if(printInfo == NULL)
return;
@ -330,4 +361,5 @@ NPP_Print(NPP instance, NPPrint* printInfo)
printInfo->print.embedPrint.platformPrint;
}
}
#endif
}

View File

@ -30,11 +30,12 @@
#define OPT_CHANGE_PIN 0x100
#define OPT_LIST_PINS 0x101
#define OPT_READER 0x102
#define OPT_PIN_ID 0x103
int opt_reader = 0, opt_pin = 0;
int opt_reader = 0;
char * opt_pin_id;
char * opt_cert = NULL;
char * opt_outfile = NULL;
char * opt_pincode = NULL;
char * opt_newpin = NULL;
char * opt_apdu = NULL;
int quiet = 0;
@ -46,14 +47,13 @@ const struct option options[] = {
{ "read-certificate", 1, 0, 'r' },
{ "list-certificates", 0, 0, 'c' },
{ "list-pins", 0, 0, OPT_LIST_PINS },
{ "change-pin", 2, 0, OPT_CHANGE_PIN },
{ "change-pin", 0, 0, OPT_CHANGE_PIN },
{ "list-private-keys", 0, 0, 'k' },
{ "reader", 1, 0, OPT_READER },
{ "output", 1, 0, 'o' },
{ "quiet", 0, 0, 'q' },
{ "debug", 0, 0, 'd' },
{ "pin", 1, 0, 'p' },
{ "pin-id", 1, &opt_pin, 0 },
{ "pin-id", 1, 0, 'p' },
{ 0, 0, 0, 0 }
};
@ -64,13 +64,12 @@ const char *option_help[] = {
"Reads certificate with ID <arg> [P15]",
"Lists certificates [P15]",
"Lists PIN codes [P15]",
"Changes the PIN code to <arg> [P15]",
"Changes the PIN code [P15]",
"Lists private keys [P15]",
"Uses reader number <arg>",
"Outputs to file <arg>",
"Quiet operation",
"Debug output -- may be supplied several times",
"Uses password (PIN) <arg>",
"The auth ID of the PIN to use [P15]",
};
@ -222,35 +221,55 @@ int list_private_keys()
return 0;
}
const char * get_pin()
char * get_pin(const char *prompt, struct sc_pkcs15_pin_info **pin_out)
{
int r;
char buf[80];
char *pincode;
struct sc_pkcs15_pin_info *pinfo;
if (opt_pincode != NULL)
return opt_pincode;
r = sc_pkcs15_enum_pins(p15card);
if (r < 0) {
fprintf(stderr, "PIN code enumeration failed: %s\n", sc_strerror(r));
return NULL;
if (pin_out != NULL)
pinfo = *pin_out;
if (pinfo == NULL && opt_pin_id == NULL) {
r = sc_pkcs15_enum_pins(p15card);
if (r < 0) {
fprintf(stderr, "PIN code enumeration failed: %s\n", sc_strerror(r));
return NULL;
}
if (r == 0) {
fprintf(stderr, "No PIN codes found.\n");
return NULL;
}
pinfo = &p15card->pin_info[0];
} else if (pinfo == NULL) {
struct sc_pkcs15_id pin_id;
sc_pkcs15_hex_string_to_id(opt_pin_id, &pin_id);
r = sc_pkcs15_find_pin_by_auth_id(p15card, &pin_id, &pinfo);
if (r) {
fprintf(stderr, "Unable to find PIN code: %s\n", sc_strerror(r));
return NULL;
}
}
if (opt_pin < 0 || opt_pin >= p15card->pin_count) {
fprintf(stderr, "Selected PIN code not found.\n");
return NULL;
}
pinfo = &p15card->pin_info[opt_pin];
sprintf(buf, "Enter PIN [%s]: ", pinfo->com_attr.label);
if (pin_out != NULL)
*pin_out = pinfo;
sprintf(buf, "%s [%s]: ", prompt, pinfo->com_attr.label);
while (1) {
pincode = getpass(buf);
if (strlen(pincode) == 0)
return NULL;
if (strlen(pincode) < pinfo->min_length ||
strlen(pincode) > pinfo->stored_length)
continue;
return pincode;
if (strlen(pincode) < pinfo->min_length) {
printf("PIN code too short, try again.\n");
continue;
}
if (strlen(pincode) > pinfo->stored_length) {
printf("PIN code too long, try again.\n");
continue;
}
return strdup(pincode);
}
}
@ -275,17 +294,46 @@ int list_pins()
int change_pin()
{
const char *pincode = opt_pincode;
char *pincode;
char *newpin;
struct sc_pkcs15_pin_info *pinfo = NULL;
int r;
if (pincode == NULL)
pincode = get_pin();
pincode = get_pin("Enter old PIN", &pinfo);
if (pincode == NULL)
return 2;
if (strlen(pincode) == 0) {
fprintf(stderr, "No PIN code supplied.\n");
return 2;
}
printf("Not working yet!\n");
while (1) {
char *newpin2;
newpin = get_pin("Enter new PIN", &pinfo);
if (newpin == NULL || strlen(newpin) == 0)
return 2;
newpin2 = get_pin("Enter new PIN again", &pinfo);
if (newpin2 == NULL || strlen(newpin2) == 0)
return 2;
if (strcmp(newpin, newpin2) == 0) {
free(newpin2);
break;
}
printf("PIN codes do not match, try again.\n");
free(newpin);
free(newpin2);
}
r = sc_pkcs15_change_pin(p15card, pinfo, pincode, strlen(pincode),
newpin, strlen(newpin));
if (r == SC_ERROR_PIN_CODE_INCORRECT) {
fprintf(stderr, "PIN code incorrect; tries left: %d\n", pinfo->tries_left);
return 3;
} else if (r) {
fprintf(stderr, "PIN code change failed: %s\n", sc_strerror(r));
return 2;
}
if (!quiet)
printf("PIN code changed successfully.\n");
return 0;
}
@ -506,10 +554,7 @@ int main(int argc, char * const argv[])
sc_debug++;
break;
case 'p':
if (optarg == NULL && opt_pincode == NULL)
opt_pincode = getpass("Enter PIN code: ");
else if (optarg != NULL)
opt_pincode = optarg;
opt_pin_id = optarg;
break;
}
}