From 4b10986670143467e1987e9d876b07dda9d5e2ce Mon Sep 17 00:00:00 2001 From: Juan Lang Date: Tue, 14 Jun 2005 11:38:36 +0000 Subject: [PATCH] Add CertAlgIdToOID and CertOIDToAlgId, with tests. --- dlls/crypt32/crypt32.spec | 4 +- dlls/crypt32/main.c | 88 ++++++++++++++++++++++++ dlls/crypt32/tests/.cvsignore | 1 + dlls/crypt32/tests/Makefile.in | 1 + dlls/crypt32/tests/main.c | 120 +++++++++++++++++++++++++++++++++ 5 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 dlls/crypt32/tests/main.c diff --git a/dlls/crypt32/crypt32.spec b/dlls/crypt32/crypt32.spec index 11fdc1818b9..c2976a793bd 100644 --- a/dlls/crypt32/crypt32.spec +++ b/dlls/crypt32/crypt32.spec @@ -9,7 +9,7 @@ @ stub CertAddEnhancedKeyUsageIdentifier @ stub CertAddSerializedElementToStore @ stub CertAddStoreToCollection -@ stub CertAlgIdToOID +@ stdcall CertAlgIdToOID(long) @ stdcall CertCloseStore(ptr long) @ stub CertCompareCertificate @ stub CertCompareCertificateName @@ -56,7 +56,7 @@ @ stub CertIsRDNAttrsInCertificateName @ stub CertNameToStrA @ stub CertNameToStrW -@ stub CertOIDToAlgId +@ stdcall CertOIDToAlgId(str) @ stdcall CertOpenStore(str long long long ptr) @ stdcall CertOpenSystemStoreA(long str) @ stdcall CertOpenSystemStoreW(long wstr) diff --git a/dlls/crypt32/main.c b/dlls/crypt32/main.c index 32df7475d02..821dcc3aaf3 100644 --- a/dlls/crypt32/main.c +++ b/dlls/crypt32/main.c @@ -1,5 +1,6 @@ /* * Copyright 2002 Mike McCormack for CodeWeavers + * Copyright 2005 Juan Lang * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -191,3 +192,90 @@ BOOL WINAPI CryptRegisterDefaultOIDFunction(DWORD dwEncodingType, debugstr_w(pwszDll)); return FALSE; } + +struct OIDToAlgID +{ + LPCSTR oid; + DWORD algID; +}; + +static const struct OIDToAlgID oidToAlgID[] = { + { szOID_RSA_RSA, CALG_RSA_KEYX }, + { szOID_RSA_MD2RSA, CALG_MD2 }, + { szOID_RSA_MD4RSA, CALG_MD4 }, + { szOID_RSA_MD5RSA, CALG_MD5 }, + { szOID_RSA_SHA1RSA, CALG_SHA }, + { szOID_RSA_DH, CALG_DH_SF }, + { szOID_RSA_SMIMEalgESDH, CALG_DH_EPHEM }, + { szOID_RSA_SMIMEalgCMS3DESwrap, CALG_3DES }, + { szOID_RSA_SMIMEalgCMSRC2wrap, CALG_RC2 }, + { szOID_RSA_MD2, CALG_MD2 }, + { szOID_RSA_MD4, CALG_MD4 }, + { szOID_RSA_MD5, CALG_MD5 }, + { szOID_RSA_RC2CBC, CALG_RC2 }, + { szOID_RSA_RC4, CALG_RC4 }, + { szOID_RSA_DES_EDE3_CBC, CALG_3DES }, + { szOID_ANSI_X942_DH, CALG_DH_SF }, + { szOID_X957_DSA, CALG_DSS_SIGN }, + { szOID_X957_SHA1DSA, CALG_SHA }, + { szOID_OIWSEC_md4RSA, CALG_MD4 }, + { szOID_OIWSEC_md5RSA, CALG_MD5 }, + { szOID_OIWSEC_md4RSA2, CALG_MD4 }, + { szOID_OIWSEC_desCBC, CALG_DES }, + { szOID_OIWSEC_dsa, CALG_DSS_SIGN }, + { szOID_OIWSEC_shaDSA, CALG_SHA }, + { szOID_OIWSEC_shaRSA, CALG_SHA }, + { szOID_OIWSEC_sha, CALG_SHA }, + { szOID_OIWSEC_rsaXchg, CALG_RSA_KEYX }, + { szOID_OIWSEC_sha1, CALG_SHA }, + { szOID_OIWSEC_dsaSHA1, CALG_SHA }, + { szOID_OIWSEC_sha1RSASign, CALG_SHA }, + { szOID_OIWDIR_md2RSA, CALG_MD2 }, + { szOID_INFOSEC_mosaicUpdatedSig, CALG_SHA }, + { szOID_INFOSEC_mosaicKMandUpdSig, CALG_DSS_SIGN }, +}; + +LPCSTR WINAPI CertAlgIdToOID(DWORD dwAlgId) +{ + switch (dwAlgId) + { + case CALG_RSA_KEYX: + return szOID_RSA_RSA; + case CALG_DH_EPHEM: + return szOID_RSA_SMIMEalgESDH; + case CALG_MD2: + return szOID_RSA_MD2; + case CALG_MD4: + return szOID_RSA_MD4; + case CALG_MD5: + return szOID_RSA_MD5; + case CALG_RC2: + return szOID_RSA_RC2CBC; + case CALG_RC4: + return szOID_RSA_RC4; + case CALG_3DES: + return szOID_RSA_DES_EDE3_CBC; + case CALG_DH_SF: + return szOID_ANSI_X942_DH; + case CALG_DSS_SIGN: + return szOID_X957_DSA; + case CALG_DES: + return szOID_OIWSEC_desCBC; + case CALG_SHA: + return szOID_OIWSEC_sha1; + default: + return NULL; + } +} + +DWORD WINAPI CertOIDToAlgId(LPCSTR pszObjId) +{ + int i; + + for (i = 0; i < sizeof(oidToAlgID) / sizeof(oidToAlgID[0]); i++) + { + if (!strcmp(pszObjId, oidToAlgID[i].oid)) + return oidToAlgID[i].algID; + } + return 0; +} diff --git a/dlls/crypt32/tests/.cvsignore b/dlls/crypt32/tests/.cvsignore index 734619bef6e..227ae6a6870 100644 --- a/dlls/crypt32/tests/.cvsignore +++ b/dlls/crypt32/tests/.cvsignore @@ -1,4 +1,5 @@ Makefile encode.ok +main.ok protectdata.ok testlist.c diff --git a/dlls/crypt32/tests/Makefile.in b/dlls/crypt32/tests/Makefile.in index 957f1ddd3d3..0a625429fa2 100644 --- a/dlls/crypt32/tests/Makefile.in +++ b/dlls/crypt32/tests/Makefile.in @@ -7,6 +7,7 @@ IMPORTS = crypt32 CTESTS = \ encode.c \ + main.c \ protectdata.c @MAKE_TEST_RULES@ diff --git a/dlls/crypt32/tests/main.c b/dlls/crypt32/tests/main.c new file mode 100644 index 00000000000..fc5b0af3f1f --- /dev/null +++ b/dlls/crypt32/tests/main.c @@ -0,0 +1,120 @@ +/* + * Miscellaneous crypt32 tests + * + * Copyright 2005 Juan Lang + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include +#include +#include + +#include "wine/test.h" + +struct OIDToAlgID +{ + LPCSTR oid; + DWORD algID; +}; + +static const struct OIDToAlgID oidToAlgID[] = { + { szOID_RSA_RSA, CALG_RSA_KEYX }, + { szOID_RSA_MD2RSA, CALG_MD2 }, + { szOID_RSA_MD4RSA, CALG_MD4 }, + { szOID_RSA_MD5RSA, CALG_MD5 }, + { szOID_RSA_SHA1RSA, CALG_SHA }, + { szOID_RSA_DH, CALG_DH_SF }, + { szOID_RSA_SMIMEalgESDH, CALG_DH_EPHEM }, + { szOID_RSA_SMIMEalgCMS3DESwrap, CALG_3DES }, + { szOID_RSA_SMIMEalgCMSRC2wrap, CALG_RC2 }, + { szOID_RSA_MD2, CALG_MD2 }, + { szOID_RSA_MD4, CALG_MD4 }, + { szOID_RSA_MD5, CALG_MD5 }, + { szOID_RSA_RC2CBC, CALG_RC2 }, + { szOID_RSA_RC4, CALG_RC4 }, + { szOID_RSA_DES_EDE3_CBC, CALG_3DES }, + { szOID_ANSI_X942_DH, CALG_DH_SF }, + { szOID_X957_DSA, CALG_DSS_SIGN }, + { szOID_X957_SHA1DSA, CALG_SHA }, + { szOID_OIWSEC_md4RSA, CALG_MD4 }, + { szOID_OIWSEC_md5RSA, CALG_MD5 }, + { szOID_OIWSEC_md4RSA2, CALG_MD4 }, + { szOID_OIWSEC_desCBC, CALG_DES }, + { szOID_OIWSEC_dsa, CALG_DSS_SIGN }, + { szOID_OIWSEC_shaDSA, CALG_SHA }, + { szOID_OIWSEC_shaRSA, CALG_SHA }, + { szOID_OIWSEC_sha, CALG_SHA }, + { szOID_OIWSEC_rsaXchg, CALG_RSA_KEYX }, + { szOID_OIWSEC_sha1, CALG_SHA }, + { szOID_OIWSEC_dsaSHA1, CALG_SHA }, + { szOID_OIWSEC_sha1RSASign, CALG_SHA }, + { szOID_OIWDIR_md2RSA, CALG_MD2 }, + { szOID_INFOSEC_mosaicUpdatedSig, CALG_SHA }, + { szOID_INFOSEC_mosaicKMandUpdSig, CALG_DSS_SIGN }, +}; + +static const struct OIDToAlgID algIDToOID[] = { + { szOID_RSA_RSA, CALG_RSA_KEYX }, + { szOID_RSA_SMIMEalgESDH, CALG_DH_EPHEM }, + { szOID_RSA_MD2, CALG_MD2 }, + { szOID_RSA_MD4, CALG_MD4 }, + { szOID_RSA_MD5, CALG_MD5 }, + { szOID_RSA_RC2CBC, CALG_RC2 }, + { szOID_RSA_RC4, CALG_RC4 }, + { szOID_RSA_DES_EDE3_CBC, CALG_3DES }, + { szOID_ANSI_X942_DH, CALG_DH_SF }, + { szOID_X957_DSA, CALG_DSS_SIGN }, + { szOID_OIWSEC_desCBC, CALG_DES }, + { szOID_OIWSEC_sha1, CALG_SHA }, +}; + +static void testOIDToAlgID(void) +{ + int i; + + for (i = 0; i < sizeof(oidToAlgID) / sizeof(oidToAlgID[0]); i++) + { + DWORD alg = CertOIDToAlgId(oidToAlgID[i].oid); + + ok(alg == oidToAlgID[i].algID, + "Expected %ld, got %ld\n", oidToAlgID[i].algID, alg); + } +} + +static void testAlgIDToOID(void) +{ + int i; + + for (i = 0; i < sizeof(algIDToOID) / sizeof(algIDToOID[0]); i++) + { + LPCSTR oid = CertAlgIdToOID(algIDToOID[i].algID); + + if ((!oid || !algIDToOID[i].oid) && oid != algIDToOID[i].oid && + algIDToOID[i].algID) + printf("Expected %s, got %s\n", algIDToOID[i].oid, oid); + else if (oid && algIDToOID[i].oid && strcmp(oid, algIDToOID[i].oid)) + printf("Expected %s, got %s\n", algIDToOID[i].oid, oid); + } +} + +START_TEST(main) +{ + testOIDToAlgID(); + testAlgIDToOID(); +}