summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Lidén Borell <samuel@kodafritt.se>2014-08-04 14:08:16 (GMT)
committerSamuel Lidén Borell <samuel@kodafritt.se>2014-08-04 14:08:16 (GMT)
commit657bd7b4058125dcc02375cd92b14b73d2d63fa3 (patch)
treefa5c38c1a7fce61a63ce17512cf3ae05bd8b077e
parent7cc3510bc3731e876d3c5a187ccc03389a990c8b (diff)
downloadfribid-657bd7b4058125dcc02375cd92b14b73d2d63fa3.zip
fribid-657bd7b4058125dcc02375cd92b14b73d2d63fa3.tar.gz
fribid-657bd7b4058125dcc02375cd92b14b73d2d63fa3.tar.bz2
Rewrite most initializers to not require relocations
This patch changes most initializers to not require relocations, except for the ASN1 stuff in client/request.c and baseClass in plugin/npobject.c
-rw-r--r--client/certutil.c2
-rw-r--r--client/gtk.c78
-rw-r--r--client/pkcs11.c18
-rw-r--r--client/pkcs12.c44
-rw-r--r--client/posix.c2
-rw-r--r--client/prefs.c11
-rw-r--r--plugin/pluginutil.c26
7 files changed, 97 insertions, 84 deletions
diff --git a/client/certutil.c b/client/certutil.c
index 7c8cdc8..7b0c81f 100644
--- a/client/certutil.c
+++ b/client/certutil.c
@@ -41,7 +41,7 @@
static char *error_string = NULL;
typedef struct {
- const char *name;
+ const char name[7]; /* length of "STREET" + terminator */
const int nid;
} DNAttrInfo;
diff --git a/client/gtk.c b/client/gtk.c
index 78b459c..5f670a2 100644
--- a/client/gtk.c
+++ b/client/gtk.c
@@ -47,38 +47,50 @@
#define _(string) gettext(string)
#define translatable(string) (string)
-static const char *const errorStrings[] = {
- // TokenError_Success
- NULL,
- // TokenError_Unknown
- translatable("An unknown error occurred"),
- // TokenError_NotImplemented
- translatable("Not implemented yet"),
- // TokenError_MessageTooLong
- translatable("Message to sign is too long"),
- // TokenError_SignatureFailure
- translatable("Failed to create signature"),
-
- // File errors
- // TokenError_FileNotReadable
- translatable("The file could not be read"),
- // TokenError_CantCreateFile
- translatable("The file could not be saved"),
- // TokenError_CantWriteToFile
- translatable("The file could not be written to, even though it could be opened"),
- // TokenError_BadFile
- translatable("Invalid file format"),
- // TokenError_BadPassword,
- translatable("Incorrect password"),
-
- // Smart card errors
- // TokenError_BadPin
- translatable("Incorrect PIN"),
-
- // Key generation errors
- //TokenError_NoRandomState,
- translatable("No random state available (/dev/(u)random must exist)"),
-};
+static const char *getErrorString(TokenError errorCode) {
+ const char *s = NULL;
+ switch (errorCode) {
+ case TokenError_Success:
+ return NULL;
+ case TokenError_Unknown:
+ s = translatable("An unknown error occurred");
+ break;
+ case TokenError_NotImplemented:
+ s = translatable("Not implemented yet");
+ break;
+ case TokenError_MessageTooLong:
+ s = translatable("Message to sign is too long");
+ break;
+ case TokenError_SignatureFailure:
+ s = translatable("Failed to create signature");
+ break;
+ /* File errors */
+ case TokenError_FileNotReadable:
+ s = translatable("The file could not be read");
+ break;
+ case TokenError_CantCreateFile:
+ s = translatable("The file could not be saved");
+ break;
+ case TokenError_CantWriteToFile:
+ s = translatable("The file could not be written to, even though it could be opened");
+ break;
+ case TokenError_BadFile:
+ s = translatable("Invalid file format");
+ break;
+ case TokenError_BadPassword:
+ s = translatable("Incorrect password");
+ break;
+ /* Smart card errors */
+ case TokenError_BadPin:
+ s = translatable("Incorrect PIN");
+ break;
+ /* Key generation errors */
+ case TokenError_NoRandomState:
+ s = translatable("No random state available (/dev/(u)random must exist)");
+ break;
+ }
+ return gettext(s);
+}
void platform_init(int *argc, char ***argv) {
@@ -707,7 +719,7 @@ void platform_showError(TokenError error) {
assert(error != TokenError_Success);
int lastErrno = errno;
- const char *text = gettext(errorStrings[error]);
+ const char *text = getErrorString(error);
char *longText;
switch (error) {
diff --git a/client/pkcs11.c b/client/pkcs11.c
index a2f16f3..d388ed4 100644
--- a/client/pkcs11.c
+++ b/client/pkcs11.c
@@ -250,19 +250,15 @@ static void _backend_free(Backend *backend) {
EVP_cleanup();
}
-/* Backend functions */
-static const Backend backend_template = {
- .init = _backend_init,
- .scan = _backend_scan,
- .free = _backend_free,
- .freeToken = _backend_freeToken,
- .getBase64Chain = _backend_getBase64Chain,
- .sign = _backend_sign,
-};
Backend *pkcs11_getBackend(void) {
- Backend *backend = malloc(sizeof(Backend));
- memcpy(backend, &backend_template, sizeof(Backend));
+ Backend *backend = calloc(1, sizeof(Backend));
+ backend->init = _backend_init;
+ backend->free = _backend_free;
+ backend->freeToken = _backend_freeToken;
+ backend->scan = _backend_scan;
+ backend->getBase64Chain = _backend_getBase64Chain;
+ backend->sign = _backend_sign;
return backend;
}
diff --git a/client/pkcs12.c b/client/pkcs12.c
index 6a35f7a..376c27b 100644
--- a/client/pkcs12.c
+++ b/client/pkcs12.c
@@ -385,14 +385,18 @@ typedef struct CertReq {
* Adds a key usage extension to the list of extensions in a request.
*/
static X509_EXTENSION *makeKeyUsageExt(KeyUsage keyUsage) {
- static const char *const keyUsages[] = {
- NULL, /* Issuing */
- "nonRepudiation", /* Signing */
- "digitalSignature", /* Authentication (yes, this is correct!) */
- };
-
- return X509V3_EXT_conf_nid(NULL, NULL,
- NID_key_usage, (char*)keyUsages[keyUsage]);
+ char *ku_str;
+ switch (keyUsage) {
+ case KeyUsage_Signing:
+ ku_str = "nonRepudiation";
+ break;
+ case KeyUsage_Authentication:
+ ku_str = "digitalSignature";
+ break;
+ default:
+ ku_str = NULL;
+ }
+ return X509V3_EXT_conf_nid(NULL, NULL, NID_key_usage, ku_str);
}
static TokenError saveKeys(const CertReq *reqs, const char *hostname,
@@ -842,22 +846,16 @@ TokenError _backend_storeCertificates(const char *p7data, size_t length,
return error;
}
-
-/* Backend functions */
-static const Backend backend_template = {
- .init = _backend_init,
- .free = _backend_free,
- .freeToken = _backend_freeToken,
- .addFile = _backend_addFile,
- .createRequest = _backend_createRequest,
- .storeCertificates = _backend_storeCertificates,
- .getBase64Chain = _backend_getBase64Chain,
- .sign = _backend_sign,
-};
-
Backend *pkcs12_getBackend(void) {
- Backend *backend = malloc(sizeof(Backend));
- memcpy(backend, &backend_template, sizeof(Backend));
+ Backend *backend = calloc(1, sizeof(Backend));
+ backend->init = _backend_init;
+ backend->free = _backend_free;
+ backend->freeToken = _backend_freeToken;
+ backend->addFile = _backend_addFile;
+ backend->createRequest = _backend_createRequest;
+ backend->storeCertificates = _backend_storeCertificates;
+ backend->getBase64Chain = _backend_getBase64Chain;
+ backend->sign = _backend_sign;
return backend;
}
diff --git a/client/posix.c b/client/posix.c
index 28a82d0..90c6639 100644
--- a/client/posix.c
+++ b/client/posix.c
@@ -64,7 +64,7 @@ struct PlatformDirIter {
* @param mode Either Platform_OpenRead or Platform_OpenCreate
*/
FILE *platform_openLocked(const char *filename, PlatformOpenMode mode) {
- static const char *const stdio_modes[] = { "rb", "wb" };
+ static const char stdio_modes[][3] = { "rb", "wb" };
static const int open_flags[] = { O_RDONLY, O_WRONLY|O_CREAT|O_EXCL };
static const int lock_flags[] = { F_RDLCK, F_WRLCK };
diff --git a/client/prefs.c b/client/prefs.c
index e0a01ed..5b6b7d1 100644
--- a/client/prefs.c
+++ b/client/prefs.c
@@ -25,16 +25,23 @@
#include "../common/defines.h"
#include "platform.h"
+/* These variables are assigned in prefs_load and read from
+ various places in the client. */
#if ENABLE_PKCS11
-const char *prefs_pkcs11_module = DEFAULT_PKCS11_MODULE;
+const char *prefs_pkcs11_module;
#endif
-const char *prefs_bankid_emulatedversion = NULL;
+const char *prefs_bankid_emulatedversion;
/**
* Loads the preferences from ~/.config/fribid/config
*/
void prefs_load(void) {
PlatformConfig *cfg = platform_openConfig("fribid", "config");
+
+ /* Set defaults */
+ prefs_pkcs11_module = DEFAULT_PKCS11_MODULE;
+ prefs_bankid_emulatedversion = NULL;
+
if (cfg) {
char *s;
/* Which PKCS#11 module to use */
diff --git a/plugin/pluginutil.c b/plugin/pluginutil.c
index 1b13588..c29d9a2 100644
--- a/plugin/pluginutil.c
+++ b/plugin/pluginutil.c
@@ -70,22 +70,28 @@ static bool getProperty(NPP instance, NPObject *obj, const char *name, NPVariant
return NPN_GetProperty(instance, obj, ident, result);
}
-static char *getWindowProperty(NPP instance, const char *const identifiers[]) {
+/**
+ * Returns a property from the window Javascript object.
+ * The identifiers parameter must point to a sequence of null terminated
+ * strings followed by an extra null terminator. E.g.
+ * "document\0location\0href\0"
+ */
+static char *getWindowProperty(NPP instance, const char *identifiers) {
NPObject *obj;
NPN_GetValue(instance, NPNVWindowNPObject, &obj);
if (!obj) return NULL;
- const char *const *identifier = &identifiers[0];
+ const char *identifier = identifiers;
while (1) {
NPVariant value;
- bool ok = getProperty(instance, obj, *identifier, &value);
+ bool ok = getProperty(instance, obj, identifier, &value);
NPN_ReleaseObject(obj);
if (!ok) return NULL;
- identifier++;
+ identifier += strlen(identifier)+1;
if (*identifier) {
// Expecting an object
if (!NPVARIANT_IS_OBJECT(value)) {
@@ -94,7 +100,7 @@ static char *getWindowProperty(NPP instance, const char *const identifiers[]) {
}
obj = NPVARIANT_TO_OBJECT(value);
} else {
- // Expecting a string
+ // Reached end. Expecting a string
if (!NPVARIANT_IS_STRING(value)) {
NPN_ReleaseVariantValue(&value);
return NULL;
@@ -108,17 +114,11 @@ static char *getWindowProperty(NPP instance, const char *const identifiers[]) {
}
char *getDocumentURL(NPP instance) {
- static const char *const identifiers[] = {
- "document", "location", "href", NULL
- };
- return getWindowProperty(instance, identifiers);
+ return getWindowProperty(instance, "document\0location\0href\0");
}
char *getDocumentHostname(NPP instance) {
- static const char *const identifiers[] = {
- "document", "location", "hostname", NULL
- };
- return getWindowProperty(instance, identifiers);
+ return getWindowProperty(instance, "document\0location\0hostname\0");
}
/**