Fix non-existent public key/cert problem

Right at the moment the token library segfaults if the public key
isn't found.  This is because the serial number gets set to NULL and
the ATTRIB macro tries to do strlen(NULL).  Fix this, but also ensure
that any failure to load the public key/cert results in the token
being removed from the exported list.

Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
diff --git a/cache.c b/cache.c
index a461259..8b30672 100644
--- a/cache.c
+++ b/cache.c
@@ -193,17 +193,18 @@
 	 * private key */
 	for (i = 1; i < section_count; i++) {
 		struct kv *kv;
+		int ret;
 
 		kv = kv_get(&s[i], INI_PUBLIC_KEY);
 		if (!kv) {
 			kv = kv_get(&s[i], INI_CERT);
 			if (kv)
-				crypto_load_cert(i, kv->value);
+				ret = crypto_load_cert(i, kv->value);
 		} else {
-			crypto_load_public_key(i, kv->value);
+			ret = crypto_load_public_key(i, kv->value);
 		}
-		if (!kv) {
-			fprintf(stderr, "key '%s' has no '%s' or '%s' value\n",
+		if (!kv || ret) {
+			fprintf(stderr, "token '%s' has no valid '%s' or '%s' value\n",
 				s[i].section, INI_PUBLIC_KEY, INI_CERT);
 			/*
 			 * a bit nasty, but removing the section
diff --git a/pkcs11.c b/pkcs11.c
index 79eb2c5..7f90821 100644
--- a/pkcs11.c
+++ b/pkcs11.c
@@ -15,7 +15,8 @@
 #define min(x, y) ((x) < (y)?(x):(y))
 #define ATTRIB(x, y) do {				\
 	memset(x, ' ', sizeof(x));			\
-	memcpy(x, y, min(strlen(y), sizeof(x)));	\
+	if (y)						\
+		memcpy(x, y, min(strlen(y), sizeof(x)));\
 } while(0)
 
 static CK_FUNCTION_LIST module_functions;