From c92e343c53743180c8854cb10da8bf522dd43d76 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Tue, 2 Dec 2008 20:49:19 +0000 Subject: Load certificates by key ID if finding by CKA_ID. Use standard function for returning a string. --- ckcapi.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'ckcapi.c') 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; +} /* ---------------------------------------------------------------- */ -- cgit v1.2.3