engines/e_capi.c: TCHAR support (cumilative update from master).

This commit is contained in:
Andy Polyakov 2014-02-02 00:17:38 +01:00
parent 0693dd954e
commit 2ad673c611

View File

@ -54,22 +54,27 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <openssl/crypto.h> #include <openssl/crypto.h>
#include <openssl/buffer.h>
#include <openssl/bn.h>
#ifdef OPENSSL_SYS_WIN32 #ifdef OPENSSL_SYS_WIN32
#ifndef OPENSSL_NO_CAPIENG #ifndef OPENSSL_NO_CAPIENG
#include <openssl/buffer.h>
#include <openssl/bn.h>
#include <openssl/rsa.h> #include <openssl/rsa.h>
#include <windows.h>
#ifndef _WIN32_WINNT #ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400 #define _WIN32_WINNT 0x0400
#endif #endif
#include <windows.h>
#include <wincrypt.h> #include <wincrypt.h>
#include <malloc.h>
#ifndef alloca
# define alloca _alloca
#endif
/* /*
* This module uses several "new" interfaces, among which is * This module uses several "new" interfaces, among which is
@ -858,7 +863,7 @@ int capi_rsa_sign(int dtype, const unsigned char *m, unsigned int m_len,
/* Finally sign it */ /* Finally sign it */
slen = RSA_size(rsa); slen = RSA_size(rsa);
if(!CryptSignHashA(hash, capi_key->keyspec, NULL, 0, sigret, &slen)) if(!CryptSignHash(hash, capi_key->keyspec, NULL, 0, sigret, &slen))
{ {
CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_ERROR_SIGNING_HASH); CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_ERROR_SIGNING_HASH);
capi_addlasterror(); capi_addlasterror();
@ -996,7 +1001,7 @@ static DSA_SIG *capi_dsa_do_sign(const unsigned char *digest, int dlen,
/* Finally sign it */ /* Finally sign it */
slen = sizeof(csigbuf); slen = sizeof(csigbuf);
if(!CryptSignHashA(hash, capi_key->keyspec, NULL, 0, csigbuf, &slen)) if(!CryptSignHash(hash, capi_key->keyspec, NULL, 0, csigbuf, &slen))
{ {
CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_ERROR_SIGNING_HASH); CAPIerr(CAPI_F_CAPI_DSA_DO_SIGN, CAPI_R_ERROR_SIGNING_HASH);
capi_addlasterror(); capi_addlasterror();
@ -1068,7 +1073,7 @@ static void capi_adderror(DWORD err)
ERR_add_error_data(2, "Error code= 0x", errstr); ERR_add_error_data(2, "Error code= 0x", errstr);
} }
static char *wide_to_asc(LPWSTR wstr) static char *wide_to_asc(LPCWSTR wstr)
{ {
char *str; char *str;
int len_0,sz; int len_0,sz;
@ -1099,10 +1104,10 @@ static char *wide_to_asc(LPWSTR wstr)
static int capi_get_provname(CAPI_CTX *ctx, LPSTR *pname, DWORD *ptype, DWORD idx) static int capi_get_provname(CAPI_CTX *ctx, LPSTR *pname, DWORD *ptype, DWORD idx)
{ {
LPSTR name;
DWORD len, err; DWORD len, err;
LPTSTR name;
CAPI_trace(ctx, "capi_get_provname, index=%d\n", idx); CAPI_trace(ctx, "capi_get_provname, index=%d\n", idx);
if (!CryptEnumProvidersA(idx, NULL, 0, ptype, NULL, &len)) if (!CryptEnumProviders(idx, NULL, 0, ptype, NULL, &len))
{ {
err = GetLastError(); err = GetLastError();
if (err == ERROR_NO_MORE_ITEMS) if (err == ERROR_NO_MORE_ITEMS)
@ -1111,8 +1116,11 @@ static int capi_get_provname(CAPI_CTX *ctx, LPSTR *pname, DWORD *ptype, DWORD id
capi_adderror(err); capi_adderror(err);
return 0; return 0;
} }
name = OPENSSL_malloc(len); if (sizeof(TCHAR) != sizeof(char))
if (!CryptEnumProvidersA(idx, NULL, 0, ptype, name, &len)) name = alloca(len);
else
name = OPENSSL_malloc(len);
if (!CryptEnumProviders(idx, NULL, 0, ptype, name, &len))
{ {
err = GetLastError(); err = GetLastError();
if (err == ERROR_NO_MORE_ITEMS) if (err == ERROR_NO_MORE_ITEMS)
@ -1121,8 +1129,11 @@ static int capi_get_provname(CAPI_CTX *ctx, LPSTR *pname, DWORD *ptype, DWORD id
capi_adderror(err); capi_adderror(err);
return 0; return 0;
} }
*pname = name; if (sizeof(TCHAR) != sizeof(char))
CAPI_trace(ctx, "capi_get_provname, returned name=%s, type=%d\n", name, *ptype); *pname = wide_to_asc((WCHAR *)name);
else
*pname = (char *)name;
CAPI_trace(ctx, "capi_get_provname, returned name=%s, type=%d\n", *pname, *ptype);
return 1; return 1;
} }
@ -1153,8 +1164,26 @@ static int capi_list_containers(CAPI_CTX *ctx, BIO *out)
HCRYPTPROV hprov; HCRYPTPROV hprov;
DWORD err, idx, flags, buflen = 0, clen; DWORD err, idx, flags, buflen = 0, clen;
LPSTR cname; LPSTR cname;
LPTSTR cspname = NULL;
CAPI_trace(ctx, "Listing containers CSP=%s, type = %d\n", ctx->cspname, ctx->csptype); CAPI_trace(ctx, "Listing containers CSP=%s, type = %d\n", ctx->cspname, ctx->csptype);
if (!CryptAcquireContextA(&hprov, NULL, ctx->cspname, ctx->csptype, CRYPT_VERIFYCONTEXT)) if (ctx->cspname && sizeof(TCHAR)!=sizeof(char))
{
if ((clen=MultiByteToWideChar(CP_ACP,0,ctx->cspname,-1,NULL,0)))
{
cspname = alloca(clen*sizeof(WCHAR));
MultiByteToWideChar(CP_ACP,0,ctx->cspname,-1,(WCHAR *)cspname,clen);
}
if (!cspname)
{
CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, ERR_R_MALLOC_FAILURE);
capi_addlasterror();
return 0;
}
}
else
cspname = (TCHAR *)ctx->cspname;
if (!CryptAcquireContext(&hprov, NULL, cspname, ctx->csptype, CRYPT_VERIFYCONTEXT))
{ {
CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, CAPI_R_CRYPTACQUIRECONTEXT_ERROR); CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, CAPI_R_CRYPTACQUIRECONTEXT_ERROR);
capi_addlasterror(); capi_addlasterror();
@ -1186,7 +1215,7 @@ static int capi_list_containers(CAPI_CTX *ctx, BIO *out)
flags = CRYPT_FIRST; flags = CRYPT_FIRST;
else else
flags = 0; flags = 0;
if(!CryptGetProvParam(hprov, PP_ENUMCONTAINERS, cname, &clen, flags)) if(!CryptGetProvParam(hprov, PP_ENUMCONTAINERS, (BYTE *)cname, &clen, flags))
{ {
err = GetLastError(); err = GetLastError();
if (err == ERROR_NO_MORE_ITEMS) if (err == ERROR_NO_MORE_ITEMS)
@ -1382,7 +1411,6 @@ int capi_list_certs(CAPI_CTX *ctx, BIO *out, char *id)
{ {
for(idx = 0;;idx++) for(idx = 0;;idx++)
{ {
LPWSTR fname = NULL;
cert = CertEnumCertificatesInStore(hstore, cert); cert = CertEnumCertificatesInStore(hstore, cert);
if (!cert) if (!cert)
break; break;
@ -1429,16 +1457,28 @@ static PCCERT_CONTEXT capi_find_cert(CAPI_CTX *ctx, const char *id, HCERTSTORE h
} }
} }
static CAPI_KEY *capi_get_key(CAPI_CTX *ctx, const char *contname, char *provname, DWORD ptype, DWORD keyspec) static CAPI_KEY *capi_get_key(CAPI_CTX *ctx, const TCHAR *contname, TCHAR *provname, DWORD ptype, DWORD keyspec)
{ {
CAPI_KEY *key; CAPI_KEY *key;
DWORD dwFlags = 0; DWORD dwFlags = 0;
key = OPENSSL_malloc(sizeof(CAPI_KEY)); key = OPENSSL_malloc(sizeof(CAPI_KEY));
CAPI_trace(ctx, "capi_get_key, contname=%s, provname=%s, type=%d\n", if (sizeof(TCHAR)==sizeof(char))
CAPI_trace(ctx, "capi_get_key, contname=%s, provname=%s, type=%d\n",
contname, provname, ptype); contname, provname, ptype);
if(ctx->store_flags & CERT_SYSTEM_STORE_LOCAL_MACHINE) else if (ctx && ctx->debug_level>=CAPI_DBG_TRACE && ctx->debug_file)
dwFlags = CRYPT_MACHINE_KEYSET; {
if (!CryptAcquireContextA(&key->hprov, contname, provname, ptype, dwFlags)) /* above 'if' is optimization to minimize malloc-ations */
char *_contname = wide_to_asc((WCHAR *)contname);
char *_provname = wide_to_asc((WCHAR *)provname);
CAPI_trace(ctx, "capi_get_key, contname=%s, provname=%s, type=%d\n",
_contname, _provname, ptype);
if (_provname) OPENSSL_free(_provname);
if (_contname) OPENSSL_free(_contname);
}
if(ctx->store_flags & CERT_SYSTEM_STORE_LOCAL_MACHINE)
dwFlags = CRYPT_MACHINE_KEYSET;
if (!CryptAcquireContext(&key->hprov, contname, provname, ptype, dwFlags))
{ {
CAPIerr(CAPI_F_CAPI_GET_KEY, CAPI_R_CRYPTACQUIRECONTEXT_ERROR); CAPIerr(CAPI_F_CAPI_GET_KEY, CAPI_R_CRYPTACQUIRECONTEXT_ERROR);
capi_addlasterror(); capi_addlasterror();
@ -1468,12 +1508,19 @@ static CAPI_KEY *capi_get_cert_key(CAPI_CTX *ctx, PCCERT_CONTEXT cert)
pinfo = capi_get_prov_info(ctx, cert); pinfo = capi_get_prov_info(ctx, cert);
if (!pinfo) if (!pinfo)
goto err; goto err;
provname = wide_to_asc(pinfo->pwszProvName); if (sizeof(TCHAR) != sizeof(char))
contname = wide_to_asc(pinfo->pwszContainerName); key = capi_get_key(ctx, (TCHAR *)pinfo->pwszContainerName,
if (!provname || !contname) (TCHAR *)pinfo->pwszProvName,
goto err;
key = capi_get_key(ctx, contname, provname,
pinfo->dwProvType, pinfo->dwKeySpec); pinfo->dwProvType, pinfo->dwKeySpec);
else
{
provname = wide_to_asc(pinfo->pwszProvName);
contname = wide_to_asc(pinfo->pwszContainerName);
if (!provname || !contname)
goto err;
key = capi_get_key(ctx, (TCHAR *)contname, (TCHAR *)provname,
pinfo->dwProvType, pinfo->dwKeySpec);
}
err: err:
if (pinfo) if (pinfo)
@ -1507,8 +1554,25 @@ CAPI_KEY *capi_find_key(CAPI_CTX *ctx, const char *id)
break; break;
case CAPI_LU_CONTNAME: case CAPI_LU_CONTNAME:
key = capi_get_key(ctx, id, ctx->cspname, ctx->csptype, if (sizeof(TCHAR)!=sizeof(char))
ctx->keytype); {
WCHAR *contname, *provname;
DWORD len;
if ((len=MultiByteToWideChar(CP_ACP,0,id,-1,NULL,0)) &&
(contname=alloca(len*sizeof(WCHAR)),
MultiByteToWideChar(CP_ACP,0,id,-1,contname,len)) &&
(len=MultiByteToWideChar(CP_ACP,0,ctx->cspname,-1,NULL,0)) &&
(provname=alloca(len*sizeof(WCHAR)),
MultiByteToWideChar(CP_ACP,0,ctx->cspname,-1,provname,len)))
key = capi_get_key(ctx,(TCHAR *)contname,
(TCHAR *)provname,
ctx->csptype,ctx->keytype);
}
else
key = capi_get_key(ctx, (TCHAR *)id,
(TCHAR *)ctx->cspname,
ctx->csptype,ctx->keytype);
break; break;
} }
@ -1576,7 +1640,21 @@ static int capi_ctx_set_provname(CAPI_CTX *ctx, LPSTR pname, DWORD type, int che
if (check) if (check)
{ {
HCRYPTPROV hprov; HCRYPTPROV hprov;
if (!CryptAcquireContextA(&hprov, NULL, pname, type, LPTSTR name=NULL;
if (sizeof(TCHAR)!=sizeof(char))
{
DWORD len;
if ((len=MultiByteToWideChar(CP_ACP,0,pname,-1,NULL,0)))
{
name = alloca(len*sizeof(WCHAR));
MultiByteToWideChar(CP_ACP,0,pname,-1,(WCHAR *)name,len);
}
}
else
name = (TCHAR *)pname;
if (!name || !CryptAcquireContext(&hprov, NULL, name, type,
CRYPT_VERIFYCONTEXT)) CRYPT_VERIFYCONTEXT))
{ {
CAPIerr(CAPI_F_CAPI_CTX_SET_PROVNAME, CAPI_R_CRYPTACQUIRECONTEXT_ERROR); CAPIerr(CAPI_F_CAPI_CTX_SET_PROVNAME, CAPI_R_CRYPTACQUIRECONTEXT_ERROR);