From 5fd600489448baba847dc23bdfe6b3d8c99fbba0 Mon Sep 17 00:00:00 2001 From: David Woodhouse Date: Mon, 15 Aug 2016 16:28:17 +0100 Subject: [PATCH] Bug 1162897, Add PK11_GetTokenURI() and use it from certutil, r=rrelyea --- cmd/certutil/certutil.c | 3 +++ lib/nss/nss.def | 1 + lib/pk11wrap/pk11pub.h | 1 + lib/pk11wrap/pk11slot.c | 58 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/cmd/certutil/certutil.c b/cmd/certutil/certutil.c index 24acdbcb41..fbc752c1b8 100644 --- a/cmd/certutil/certutil.c +++ b/cmd/certutil/certutil.c @@ -1002,9 +1002,12 @@ ListModules(void) /* look at each slot*/ for (le = list->head; le; le = le->next) { + char *token_uri = PK11_GetTokenURI(le->slot); printf("\n"); printf(" slot: %s\n", PK11_GetSlotName(le->slot)); printf(" token: %s\n", PK11_GetTokenName(le->slot)); + printf(" uri: %s\n", token_uri); + PORT_Free(token_uri); } PK11_FreeSlotList(list); diff --git a/lib/nss/nss.def b/lib/nss/nss.def index e84260d7cd..f52237b05a 100644 --- a/lib/nss/nss.def +++ b/lib/nss/nss.def @@ -1108,6 +1108,7 @@ PK11_HasAttributeSet; ;+ global: CERT_GetCertIsPerm; CERT_GetCertIsTemp; +PK11_GetTokenURI; ;+ local: ;+ *; ;+}; diff --git a/lib/pk11wrap/pk11pub.h b/lib/pk11wrap/pk11pub.h index 3bef897a40..b318650822 100644 --- a/lib/pk11wrap/pk11pub.h +++ b/lib/pk11wrap/pk11pub.h @@ -76,6 +76,7 @@ PRBool PK11_IsReadOnly(PK11SlotInfo *slot); PRBool PK11_IsInternal(PK11SlotInfo *slot); PRBool PK11_IsInternalKeySlot(PK11SlotInfo *slot); char *PK11_GetTokenName(PK11SlotInfo *slot); +char *PK11_GetTokenURI(PK11SlotInfo *slot); char *PK11_GetSlotName(PK11SlotInfo *slot); PRBool PK11_NeedLogin(PK11SlotInfo *slot); PRBool PK11_IsFriendly(PK11SlotInfo *slot); diff --git a/lib/pk11wrap/pk11slot.c b/lib/pk11wrap/pk11slot.c index cc8d426975..7efe9c3c4d 100644 --- a/lib/pk11wrap/pk11slot.c +++ b/lib/pk11wrap/pk11slot.c @@ -18,6 +18,7 @@ #include "dev3hack.h" #include "pkim.h" #include "utilpars.h" +#include "pkcs11uri.h" /************************************************************* * local static and global data @@ -1687,6 +1688,63 @@ PK11_GetTokenName(PK11SlotInfo *slot) return slot->token_name; } +char * +PK11_GetTokenURI(PK11SlotInfo *slot) +{ + PK11URI *uri; + char *ret = NULL; + char label[32 + 1], manufacturer[32 + 1], serial[16 + 1], model[16 + 1]; + PK11URIAttribute attrs[4]; + size_t nattrs = 0; + + PK11_MakeString(NULL, label, (char *)slot->tokenInfo.label, + sizeof(slot->tokenInfo.label)); + if (*label != '\0') { + attrs[nattrs].name = PK11URI_PATTR_TOKEN; + attrs[nattrs].value = label; + nattrs++; + } + + PK11_MakeString(NULL, manufacturer, (char *)slot->tokenInfo.manufacturerID, + sizeof(slot->tokenInfo.manufacturerID)); + if (*manufacturer != '\0') { + attrs[nattrs].name = PK11URI_PATTR_MANUFACTURER; + attrs[nattrs].value = manufacturer; + nattrs++; + } + + PK11_MakeString(NULL, serial, (char *)slot->tokenInfo.serialNumber, + sizeof(slot->tokenInfo.serialNumber)); + if (*serial != '\0') { + attrs[nattrs].name = PK11URI_PATTR_SERIAL; + attrs[nattrs].value = serial; + nattrs++; + } + + PK11_MakeString(NULL, model, (char *)slot->tokenInfo.model, + sizeof(slot->tokenInfo.model)); + if (*model != '\0') { + attrs[nattrs].name = PK11URI_PATTR_MODEL; + attrs[nattrs].value = model; + nattrs++; + } + + uri = PK11URI_CreateURI(attrs, nattrs, NULL, 0); + if (uri == NULL) { + PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); + return NULL; + } + + ret = PK11URI_FormatURI(NULL, uri); + PK11URI_DestroyURI(uri); + + if (ret == NULL) { + PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); + } + + return ret; +} + char * PK11_GetSlotName(PK11SlotInfo *slot) {