summaryrefslogtreecommitdiff
path: root/ckcapi.c
diff options
context:
space:
mode:
authorStef Walter <stef@memberwebs.com>2008-12-02 20:49:19 +0000
committerStef Walter <stef@memberwebs.com>2008-12-02 20:49:19 +0000
commitc92e343c53743180c8854cb10da8bf522dd43d76 (patch)
tree3eb5527afeab8ef34cc3319cd8928c12614e8f65 /ckcapi.c
parente7d5c6b4b04bb34172788fba5532b89e70a1f5ac (diff)
Load certificates by key ID if finding by CKA_ID. Use standard function for returning a string.
Diffstat (limited to 'ckcapi.c')
-rw-r--r--ckcapi.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/ckcapi.c b/ckcapi.c
index 1b6654e..9ea7815 100644
--- a/ckcapi.c
+++ b/ckcapi.c
@@ -160,6 +160,55 @@ ckcapi_return_data(CK_VOID_PTR dst, CK_ULONG_PTR dlen,
return CKR_OK;
}
+CK_RV
+ckcapi_return_string(CK_VOID_PTR dst, CK_ULONG_PTR dlen,
+ WCHAR* string)
+{
+ DWORD error;
+ int result;
+
+ SetLastError(0);
+
+ /*
+ * Sadly WideCharToMultiByte doesn't handle zero
+ * length strings properly. So we have to special
+ * case this part.
+ */
+ if(!string[0])
+ return ckcapi_return_data(dst, dlen, "\0", 1);
+
+ result = WideCharToMultiByte(CP_UTF8, 0, string, -1,
+ dst, dst ? *dlen : 0, NULL, NULL);
+
+
+ /* An error result somehow */
+ if(!result)
+ {
+ error = GetLastError();
+ switch(error)
+ {
+ /* If buffer was too short, calculate good buffer length */
+ case ERROR_INSUFFICIENT_BUFFER:
+ result = WideCharToMultiByte(CP_UTF8, 0, string, -1,
+ NULL, 0, NULL, NULL);
+ if(!result)
+ return CKR_GENERAL_ERROR;
+ *dlen = result;
+ return CKR_BUFFER_TOO_SMALL;
+
+ /* A strange zero length success */
+ case ERROR_SUCCESS:
+ break;
+
+ /* All other errors are not handleable */
+ default:
+ return CKR_GENERAL_ERROR;
+ }
+ }
+
+ *dlen = result;
+ return CKR_OK;
+}
/* ---------------------------------------------------------------- */