PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/tests/llsechandler_basic_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 1068 lines | 781 code | 169 blank | 118 comment | 21 complexity | 9e6d3c09757ab300c166be65a3aa509b MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llsechandler_basic_test.cpp
  3. * @author Roxie
  4. * @date 2009-02-10
  5. * @brief Test the 'basic' sec handler functions
  6. *
  7. * $LicenseInfo:firstyear=2005&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. #include "../llviewerprecompiledheaders.h"
  29. #include "../test/lltut.h"
  30. #include "../llsecapi.h"
  31. #include "../llsechandler_basic.h"
  32. #include "../../llxml/llcontrol.h"
  33. #include "../llviewernetwork.h"
  34. #include "lluuid.h"
  35. #include "llxorcipher.h"
  36. #include "apr_base64.h"
  37. #include <vector>
  38. #include <ios>
  39. #include <llsdserialize.h>
  40. #include <openssl/pem.h>
  41. #include <openssl/err.h>
  42. #include <openssl/evp.h>
  43. #include "llxorcipher.h"
  44. #include <openssl/ossl_typ.h>
  45. #include <openssl/x509.h>
  46. #include <openssl/x509v3.h>
  47. #include <openssl/pem.h>
  48. #include <openssl/asn1.h>
  49. #include <openssl/rand.h>
  50. #include <openssl/err.h>
  51. #include "../llmachineid.h"
  52. #define ensure_throws(str, exc_type, cert, func, ...) \
  53. try \
  54. { \
  55. func(__VA_ARGS__); \
  56. fail("throws, " str); \
  57. } \
  58. catch(exc_type& except) \
  59. { \
  60. ensure("Exception cert is incorrect for " str, except.getCert() == cert); \
  61. }
  62. extern bool _cert_hostname_wildcard_match(const std::string& hostname, const std::string& wildcard_string);
  63. //----------------------------------------------------------------------------
  64. // Mock objects for the dependencies of the code we're testing
  65. std::string gFirstName;
  66. std::string gLastName;
  67. LLControlGroup::LLControlGroup(const std::string& name)
  68. : LLInstanceTracker<LLControlGroup, std::string>(name) {}
  69. LLControlGroup::~LLControlGroup() {}
  70. BOOL LLControlGroup::declareString(const std::string& name,
  71. const std::string& initial_val,
  72. const std::string& comment,
  73. BOOL persist) {return TRUE;}
  74. void LLControlGroup::setString(const std::string& name, const std::string& val){}
  75. std::string LLControlGroup::getString(const std::string& name)
  76. {
  77. if (name == "FirstName")
  78. return gFirstName;
  79. else if (name == "LastName")
  80. return gLastName;
  81. return "";
  82. }
  83. // Stub for --no-verify-ssl-cert
  84. BOOL LLControlGroup::getBOOL(const std::string& name) { return FALSE; }
  85. LLSD LLCredential::getLoginParams()
  86. {
  87. LLSD result = LLSD::emptyMap();
  88. // legacy credential
  89. result["passwd"] = "$1$testpasssd";
  90. result["first"] = "myfirst";
  91. result["last"] ="mylast";
  92. return result;
  93. }
  94. void LLCredential::identifierType(std::string &idType)
  95. {
  96. }
  97. void LLCredential::authenticatorType(std::string &idType)
  98. {
  99. }
  100. LLControlGroup gSavedSettings("test");
  101. unsigned char gMACAddress[MAC_ADDRESS_BYTES] = {77,21,46,31,89,2};
  102. S32 LLMachineID::getUniqueID(unsigned char *unique_id, size_t len)
  103. {
  104. memcpy(unique_id, gMACAddress, len);
  105. return 1;
  106. }
  107. S32 LLMachineID::init() { return 1; }
  108. // -------------------------------------------------------------------------------------------
  109. // TUT
  110. // -------------------------------------------------------------------------------------------
  111. namespace tut
  112. {
  113. // Test wrapper declaration : wrapping nothing for the moment
  114. struct sechandler_basic_test
  115. {
  116. std::string mPemTestCert, mPemRootCert, mPemIntermediateCert, mPemChildCert, mSha1RSATestCert, mSha1RSATestCA;
  117. std::string mDerFormat;
  118. X509 *mX509TestCert, *mX509RootCert, *mX509IntermediateCert, *mX509ChildCert;
  119. sechandler_basic_test()
  120. {
  121. LLMachineID::init();
  122. OpenSSL_add_all_algorithms();
  123. OpenSSL_add_all_ciphers();
  124. OpenSSL_add_all_digests();
  125. ERR_load_crypto_strings();
  126. gFirstName = "";
  127. gLastName = "";
  128. LLFile::remove("test_password.dat");
  129. LLFile::remove("sechandler_settings.tmp");
  130. mPemTestCert = "-----BEGIN CERTIFICATE-----\n"
  131. "MIIEuDCCA6CgAwIBAgIBBDANBgkqhkiG9w0BAQUFADCBtDELMAkGA1UEBhMCQlIx\n"
  132. "EzARBgNVBAoTCklDUC1CcmFzaWwxPTA7BgNVBAsTNEluc3RpdHV0byBOYWNpb25h\n"
  133. "bCBkZSBUZWNub2xvZ2lhIGRhIEluZm9ybWFjYW8gLSBJVEkxETAPBgNVBAcTCEJy\n"
  134. "YXNpbGlhMQswCQYDVQQIEwJERjExMC8GA1UEAxMoQXV0b3JpZGFkZSBDZXJ0aWZp\n"
  135. "Y2Fkb3JhIFJhaXogQnJhc2lsZWlyYTAeFw0wMTExMzAxMjU4MDBaFw0xMTExMzAy\n"
  136. "MzU5MDBaMIG0MQswCQYDVQQGEwJCUjETMBEGA1UEChMKSUNQLUJyYXNpbDE9MDsG\n"
  137. "A1UECxM0SW5zdGl0dXRvIE5hY2lvbmFsIGRlIFRlY25vbG9naWEgZGEgSW5mb3Jt\n"
  138. "YWNhbyAtIElUSTERMA8GA1UEBxMIQnJhc2lsaWExCzAJBgNVBAgTAkRGMTEwLwYD\n"
  139. "VQQDEyhBdXRvcmlkYWRlIENlcnRpZmljYWRvcmEgUmFpeiBCcmFzaWxlaXJhMIIB\n"
  140. "IjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwPMudwX/hvm+Uh2b/lQAcHVA\n"
  141. "isamaLkWdkwP9/S/tOKIgRrL6Oy+ZIGlOUdd6uYtk9Ma/3pUpgcfNAj0vYm5gsyj\n"
  142. "Qo9emsc+x6m4VWwk9iqMZSCK5EQkAq/Ut4n7KuLE1+gdftwdIgxfUsPt4CyNrY50\n"
  143. "QV57KM2UT8x5rrmzEjr7TICGpSUAl2gVqe6xaii+bmYR1QrmWaBSAG59LrkrjrYt\n"
  144. "bRhFboUDe1DK+6T8s5L6k8c8okpbHpa9veMztDVC9sPJ60MWXh6anVKo1UcLcbUR\n"
  145. "yEeNvZneVRKAAU6ouwdjDvwlsaKydFKwed0ToQ47bmUKgcm+wV3eTRk36UOnTwID\n"
  146. "AQABo4HSMIHPME4GA1UdIARHMEUwQwYFYEwBAQAwOjA4BggrBgEFBQcCARYsaHR0\n"
  147. "cDovL2FjcmFpei5pY3BicmFzaWwuZ292LmJyL0RQQ2FjcmFpei5wZGYwPQYDVR0f\n"
  148. "BDYwNDAyoDCgLoYsaHR0cDovL2FjcmFpei5pY3BicmFzaWwuZ292LmJyL0xDUmFj\n"
  149. "cmFpei5jcmwwHQYDVR0OBBYEFIr68VeEERM1kEL6V0lUaQ2kxPA3MA8GA1UdEwEB\n"
  150. "/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgEGMA0GCSqGSIb3DQEBBQUAA4IBAQAZA5c1\n"
  151. "U/hgIh6OcgLAfiJgFWpvmDZWqlV30/bHFpj8iBobJSm5uDpt7TirYh1Uxe3fQaGl\n"
  152. "YjJe+9zd+izPRbBqXPVQA34EXcwk4qpWuf1hHriWfdrx8AcqSqr6CuQFwSr75Fos\n"
  153. "SzlwDADa70mT7wZjAmQhnZx2xJ6wfWlT9VQfS//JYeIc7Fue2JNLd00UOSMMaiK/\n"
  154. "t79enKNHEA2fupH3vEigf5Eh4bVAN5VohrTm6MY53x7XQZZr1ME7a55lFEnSeT0u\n"
  155. "mlOAjR2mAbvSM5X5oSZNrmetdzyTj2flCM8CC7MLab0kkdngRIlUBGHF1/S5nmPb\n"
  156. "K+9A46sd33oqK8n8\n"
  157. "-----END CERTIFICATE-----\n";
  158. mPemRootCert = "-----BEGIN CERTIFICATE-----\n"
  159. "MIIB0TCCATqgAwIBAgIJANaTqrzEvHaRMA0GCSqGSIb3DQEBBAUAMBsxGTAXBgNV\n"
  160. "BAMTEFJveGllcyB0ZXN0IHJvb3QwHhcNMDkwNDE1MjEwNzQ3WhcNMTAwNDE1MjEw\n"
  161. "NzQ3WjAbMRkwFwYDVQQDExBSb3hpZXMgdGVzdCByb290MIGfMA0GCSqGSIb3DQEB\n"
  162. "AQUAA4GNADCBiQKBgQCpo5nDW6RNz9IHUVZd7Tw2XAQiBniDF4xH0N1w7sUYTiFq\n"
  163. "21mABsnOPJD3ra+MtOsXPHcaljm661JjTD8L40v5sfEbqDUPcOw76ClrPqnuAeyT\n"
  164. "38qk8DHku/mT8YdprevGZdVcUXQg3vosVzOL93HOOHK+u61mEEoM9W5xoNVEdQID\n"
  165. "AQABox0wGzAMBgNVHRMEBTADAQH/MAsGA1UdDwQEAwIBBjANBgkqhkiG9w0BAQQF\n"
  166. "AAOBgQAzn0aW/+zWPmcTbvxonyiYYUr9b4SOB/quhAkT8KT4ir1dcZAXRR59+kEn\n"
  167. "HSTu1FAodV0gvESqyobftF5hZ1XMxdJqGu//xP+YCwlv244G/0pp7KLI8ihNO2+N\n"
  168. "lPBUJgbo++ZkhiE1jotZi9Ay0Oedh3s/AfbMZPyfpJ23ll6+BA==\n"
  169. "-----END CERTIFICATE-----\n";
  170. mPemIntermediateCert = "-----BEGIN CERTIFICATE-----\n"
  171. "MIIBzzCCATigAwIBAgIBATANBgkqhkiG9w0BAQQFADAbMRkwFwYDVQQDExBSb3hp\n"
  172. "ZXMgdGVzdCByb290MB4XDTA5MDQxNTIxMzE1NloXDTEwMDQxNTIxMzE1NlowITEf\n"
  173. "MB0GA1UEAxMWUm94aWVzIGludGVybWVkaWF0ZSBDQTCBnzANBgkqhkiG9w0BAQEF\n"
  174. "AAOBjQAwgYkCgYEA15MM0W1R37rx/24Q2Qkb5bSiQZxTUcQAhJ2pA8mwUucXuCVt\n"
  175. "6ayI2TuN32nkjmsCgUkiT/bdXWp0OJo7/MXRIFeUNMCRxrpeFnxuigYEqbIXAdN6\n"
  176. "qu/vdG2X4PRv/v9Ijrju4cBEiKIldIgOurWEIfXEsVSFP2XmFQHesF04qDcCAwEA\n"
  177. "AaMdMBswDAYDVR0TBAUwAwEB/zALBgNVHQ8EBAMCAQYwDQYJKoZIhvcNAQEEBQAD\n"
  178. "gYEAYljikYgak3W1jSo0vYthNHUy3lBVAKzDhpM96lY5OuXFslpCRX42zNL8X3kN\n"
  179. "U/4IaJUVtZqx8WsUXl1eXHzBCaXCftapV4Ir6cENLIsXCdXs8paFYzN5nPJA5GYU\n"
  180. "zWgkSEl1MEhNIc+bJW34vwi29EjrAShAhsIZ84Mt/lvD3Pc=\n"
  181. "-----END CERTIFICATE-----\n";
  182. mPemChildCert = "-----BEGIN CERTIFICATE-----\n"
  183. "MIIB5DCCAU0CBEnm9eUwDQYJKoZIhvcNAQEEBQAwITEfMB0GA1UEAxMWUm94aWVz\n"
  184. "IGludGVybWVkaWF0ZSBDQTAeFw0wOTA0MTYwMDAzNDlaFw0xMDA0MTYwMDAzNDla\n"
  185. "MCAxHjAcBgNVBAMTFWVuaWFjNjMubGluZGVubGFiLmNvbTCBnzANBgkqhkiG9w0B\n"
  186. "AQEFAAOBjQAwgYkCgYEAp9I5rofEzbjNht+9QejfnsIlEPqSxskoWKCG255TesWR\n"
  187. "RTmw9wafHQQkJk/VIsaU4RMBYHkknGbHX2dGvMHmKZoWUPSQ/8FZz09o0Qx3TNUZ\n"
  188. "l7KlGOD2d1c7ZxXDPqlLC6QW8DrE1/8zfwJ5cbYBXc8e7OKdSZeRrnwHyw4Q8r8C\n"
  189. "AwEAAaMvMC0wEwYDVR0lBAwwCgYIKwYBBQUHAwEwCQYDVR0TBAIwADALBgNVHQ8E\n"
  190. "BAMCBaAwDQYJKoZIhvcNAQEEBQADgYEAIG0M5tqYlXyMiGKPZfXy/R3M3ZZOapDk\n"
  191. "W0dsXJYXAc35ftwtn0VYu9CNnZCcli17/d+AKhkK8a/oGPazqudjFF6WLJLTXaY9\n"
  192. "NmhkJcOPADXkbyQPUPXzLe4YRrkEQeGhzMb4rKDQ1TKAcXfs0Y068pTpsixNSxja\n"
  193. "NhAUUcve5Is=\n"
  194. "-----END CERTIFICATE-----\n";
  195. mDerFormat = "MIIEuDCCA6CgAwIBAgIBBDANBgkqhkiG9w0BAQUFADCBtDELMAkGA1UEBhMCQlIxEzARBgNVBAoT"
  196. "CklDUC1CcmFzaWwxPTA7BgNVBAsTNEluc3RpdHV0byBOYWNpb25hbCBkZSBUZWNub2xvZ2lhIGRh"
  197. "IEluZm9ybWFjYW8gLSBJVEkxETAPBgNVBAcTCEJyYXNpbGlhMQswCQYDVQQIEwJERjExMC8GA1UE"
  198. "AxMoQXV0b3JpZGFkZSBDZXJ0aWZpY2Fkb3JhIFJhaXogQnJhc2lsZWlyYTAeFw0wMTExMzAxMjU4"
  199. "MDBaFw0xMTExMzAyMzU5MDBaMIG0MQswCQYDVQQGEwJCUjETMBEGA1UEChMKSUNQLUJyYXNpbDE9"
  200. "MDsGA1UECxM0SW5zdGl0dXRvIE5hY2lvbmFsIGRlIFRlY25vbG9naWEgZGEgSW5mb3JtYWNhbyAt"
  201. "IElUSTERMA8GA1UEBxMIQnJhc2lsaWExCzAJBgNVBAgTAkRGMTEwLwYDVQQDEyhBdXRvcmlkYWRl"
  202. "IENlcnRpZmljYWRvcmEgUmFpeiBCcmFzaWxlaXJhMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB"
  203. "CgKCAQEAwPMudwX/hvm+Uh2b/lQAcHVAisamaLkWdkwP9/S/tOKIgRrL6Oy+ZIGlOUdd6uYtk9Ma"
  204. "/3pUpgcfNAj0vYm5gsyjQo9emsc+x6m4VWwk9iqMZSCK5EQkAq/Ut4n7KuLE1+gdftwdIgxfUsPt"
  205. "4CyNrY50QV57KM2UT8x5rrmzEjr7TICGpSUAl2gVqe6xaii+bmYR1QrmWaBSAG59LrkrjrYtbRhF"
  206. "boUDe1DK+6T8s5L6k8c8okpbHpa9veMztDVC9sPJ60MWXh6anVKo1UcLcbURyEeNvZneVRKAAU6o"
  207. "uwdjDvwlsaKydFKwed0ToQ47bmUKgcm+wV3eTRk36UOnTwIDAQABo4HSMIHPME4GA1UdIARHMEUw"
  208. "QwYFYEwBAQAwOjA4BggrBgEFBQcCARYsaHR0cDovL2FjcmFpei5pY3BicmFzaWwuZ292LmJyL0RQ"
  209. "Q2FjcmFpei5wZGYwPQYDVR0fBDYwNDAyoDCgLoYsaHR0cDovL2FjcmFpei5pY3BicmFzaWwuZ292"
  210. "LmJyL0xDUmFjcmFpei5jcmwwHQYDVR0OBBYEFIr68VeEERM1kEL6V0lUaQ2kxPA3MA8GA1UdEwEB"
  211. "/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgEGMA0GCSqGSIb3DQEBBQUAA4IBAQAZA5c1U/hgIh6OcgLA"
  212. "fiJgFWpvmDZWqlV30/bHFpj8iBobJSm5uDpt7TirYh1Uxe3fQaGlYjJe+9zd+izPRbBqXPVQA34E"
  213. "Xcwk4qpWuf1hHriWfdrx8AcqSqr6CuQFwSr75FosSzlwDADa70mT7wZjAmQhnZx2xJ6wfWlT9VQf"
  214. "S//JYeIc7Fue2JNLd00UOSMMaiK/t79enKNHEA2fupH3vEigf5Eh4bVAN5VohrTm6MY53x7XQZZr"
  215. "1ME7a55lFEnSeT0umlOAjR2mAbvSM5X5oSZNrmetdzyTj2flCM8CC7MLab0kkdngRIlUBGHF1/S5"
  216. "nmPbK+9A46sd33oqK8n8";
  217. mSha1RSATestCert = "-----BEGIN CERTIFICATE-----\n"
  218. "MIIDFDCCAn2gAwIBAgIDDqqYMA0GCSqGSIb3DQEBBQUAME4xCzAJBgNVBAYTAlVT\n"
  219. "MRAwDgYDVQQKEwdFcXVpZmF4MS0wKwYDVQQLEyRFcXVpZmF4IFNlY3VyZSBDZXJ0\n"
  220. "aWZpY2F0ZSBBdXRob3JpdHkwHhcNMTAwMTA1MDAzNjMwWhcNMTEwMTA3MjAyMTE0\n"
  221. "WjCBnjEpMCcGA1UEBRMgQmNmc0RBRkl1U0YwdFpWVm5vOFJKbjVUbW9hNGR2Wkgx\n"
  222. "CzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1TYW4g\n"
  223. "RnJhbmNpc2NvMR0wGwYDVQQKExRMaW5kZW4gUmVzZWFyY2ggSW5jLjEYMBYGA1UE\n"
  224. "AxQPKi5saW5kZW5sYWIuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD2\n"
  225. "14Jdko8v6GB33hHbW+lNQyloFQtc2h4ykjf+fYPJ27dw6tQO2if7N3k/5XDkwC1N\n"
  226. "krGgE9vt3iecCPgasue6k67Zyfj9HbEP2D+j38eROudrsxLaRFDQx50BvZ5YMNl3\n"
  227. "4zQCj8/gCMsuq8cvaP9/rbJTUpgYWFGLsm8yAYOgWwIDAQABo4GuMIGrMA4GA1Ud\n"
  228. "DwEB/wQEAwIE8DAdBgNVHQ4EFgQUIBK/JB9AyqquSEbkzt2Zux6v9sYwOgYDVR0f\n"
  229. "BDMwMTAvoC2gK4YpaHR0cDovL2NybC5nZW90cnVzdC5jb20vY3Jscy9zZWN1cmVj\n"
  230. "YS5jcmwwHwYDVR0jBBgwFoAUSOZo+SvSspXXR9gjIBBPM5iQn9QwHQYDVR0lBBYw\n"
  231. "FAYIKwYBBQUHAwEGCCsGAQUFBwMCMA0GCSqGSIb3DQEBBQUAA4GBAKKR84+hvLuB\n"
  232. "pop9VG7HQPIyEKtZq3Nnk+UlJGfjGY3csLWSFmxU727r5DzdEP1W1PwF3rxuoKcZ\n"
  233. "4nJJpKdzoGVujgBMP2U/J0PJvU7D8U3Zqu7nrXAjOHj7iVnvJ3EKJ1bvwXaisgPN\n"
  234. "wt21kKfGnA4OlhJtJ6VQvUkcF12I3pTP\n"
  235. "-----END CERTIFICATE-----\n";
  236. mSha1RSATestCA = "-----BEGIN CERTIFICATE-----\n"
  237. "MIIDIDCCAomgAwIBAgIENd70zzANBgkqhkiG9w0BAQUFADBOMQswCQYDVQQGEwJV\n"
  238. "UzEQMA4GA1UEChMHRXF1aWZheDEtMCsGA1UECxMkRXF1aWZheCBTZWN1cmUgQ2Vy\n"
  239. "dGlmaWNhdGUgQXV0aG9yaXR5MB4XDTk4MDgyMjE2NDE1MVoXDTE4MDgyMjE2NDE1\n"
  240. "MVowTjELMAkGA1UEBhMCVVMxEDAOBgNVBAoTB0VxdWlmYXgxLTArBgNVBAsTJEVx\n"
  241. "dWlmYXggU2VjdXJlIENlcnRpZmljYXRlIEF1dGhvcml0eTCBnzANBgkqhkiG9w0B\n"
  242. "AQEFAAOBjQAwgYkCgYEAwV2xWGcIYu6gmi0fCG2RFGiYCh7+2gRvE4RiIcPRfM6f\n"
  243. "BeC4AfBONOziipUEZKzxa1NfBbPLZ4C/QgKO/t0BCezhABRP/PvwDN1Dulsr4R+A\n"
  244. "cJkVV5MW8Q+XarfCaCMczE1ZMKxRHjuvK9buY0V7xdlfUNLjUA86iOe/FP3gx7kC\n"
  245. "AwEAAaOCAQkwggEFMHAGA1UdHwRpMGcwZaBjoGGkXzBdMQswCQYDVQQGEwJVUzEQ\n"
  246. "MA4GA1UEChMHRXF1aWZheDEtMCsGA1UECxMkRXF1aWZheCBTZWN1cmUgQ2VydGlm\n"
  247. "aWNhdGUgQXV0aG9yaXR5MQ0wCwYDVQQDEwRDUkwxMBoGA1UdEAQTMBGBDzIwMTgw\n"
  248. "ODIyMTY0MTUxWjALBgNVHQ8EBAMCAQYwHwYDVR0jBBgwFoAUSOZo+SvSspXXR9gj\n"
  249. "IBBPM5iQn9QwHQYDVR0OBBYEFEjmaPkr0rKV10fYIyAQTzOYkJ/UMAwGA1UdEwQF\n"
  250. "MAMBAf8wGgYJKoZIhvZ9B0EABA0wCxsFVjMuMGMDAgbAMA0GCSqGSIb3DQEBBQUA\n"
  251. "A4GBAFjOKer89961zgK5F7WF0bnj4JXMJTENAKaSbn+2kmOeUJXRmm/kEd5jhW6Y\n"
  252. "7qj/WsjTVbJmcVfewCHrPSqnI0kBBIZCe/zuf6IWUrVnZ9NA2zsmWLIodz2uFHdh\n"
  253. "1voqZiegDfqnc1zqcPGUIWVEX/r87yloqaKHee9570+sB3c4\n"
  254. "-----END CERTIFICATE-----\n";
  255. mX509TestCert = NULL;
  256. mX509RootCert = NULL;
  257. mX509IntermediateCert = NULL;
  258. mX509ChildCert = NULL;
  259. BIO * validation_bio = BIO_new_mem_buf((void*)mPemTestCert.c_str(), mPemTestCert.length());
  260. PEM_read_bio_X509(validation_bio, &mX509TestCert, 0, NULL);
  261. BIO_free(validation_bio);
  262. validation_bio = BIO_new_mem_buf((void*)mPemRootCert.c_str(), mPemRootCert.length());
  263. PEM_read_bio_X509(validation_bio, &mX509RootCert, 0, NULL);
  264. BIO_free(validation_bio);
  265. validation_bio = BIO_new_mem_buf((void*)mPemIntermediateCert.c_str(), mPemIntermediateCert.length());
  266. PEM_read_bio_X509(validation_bio, &mX509IntermediateCert, 0, NULL);
  267. BIO_free(validation_bio);
  268. validation_bio = BIO_new_mem_buf((void*)mPemChildCert.c_str(), mPemChildCert.length());
  269. PEM_read_bio_X509(validation_bio, &mX509ChildCert, 0, NULL);
  270. BIO_free(validation_bio);
  271. }
  272. ~sechandler_basic_test()
  273. {
  274. LLFile::remove("test_password.dat");
  275. LLFile::remove("sechandler_settings.tmp");
  276. LLFile::remove("mycertstore.pem");
  277. X509_free(mX509TestCert);
  278. X509_free(mX509RootCert);
  279. X509_free(mX509IntermediateCert);
  280. X509_free(mX509ChildCert);
  281. }
  282. };
  283. // Tut templating thingamagic: test group, object and test instance
  284. typedef test_group<sechandler_basic_test> sechandler_basic_test_factory;
  285. typedef sechandler_basic_test_factory::object sechandler_basic_test_object;
  286. tut::sechandler_basic_test_factory tut_test("LLSecHandler");
  287. // ---------------------------------------------------------------------------------------
  288. // Test functions
  289. // ---------------------------------------------------------------------------------------
  290. // test cert data retrieval
  291. template<> template<>
  292. void sechandler_basic_test_object::test<1>()
  293. {
  294. char buffer[4096];
  295. LLPointer<LLCertificate> test_cert = new LLBasicCertificate(mPemTestCert);
  296. ensure_equals("Resultant pem is correct",
  297. mPemTestCert, test_cert->getPem());
  298. std::vector<U8> binary_cert = test_cert->getBinary();
  299. apr_base64_encode(buffer, (const char *)&binary_cert[0], binary_cert.size());
  300. ensure_equals("Der Format is correct", memcmp(buffer, mDerFormat.c_str(), mDerFormat.length()), 0);
  301. LLSD llsd_cert;
  302. test_cert->getLLSD(llsd_cert);
  303. std::ostringstream llsd_value;
  304. llsd_value << LLSDOStreamer<LLSDNotationFormatter>(llsd_cert) << std::endl;
  305. std::string llsd_cert_str = llsd_value.str();
  306. ensure_equals("Issuer Name/commonName",
  307. (std::string)llsd_cert["issuer_name"]["commonName"], "Autoridade Certificadora Raiz Brasileira");
  308. ensure_equals("Issure Name/countryName", (std::string)llsd_cert["issuer_name"]["countryName"], "BR");
  309. ensure_equals("Issuer Name/localityName", (std::string)llsd_cert["issuer_name"]["localityName"], "Brasilia");
  310. ensure_equals("Issuer Name/org name", (std::string)llsd_cert["issuer_name"]["organizationName"], "ICP-Brasil");
  311. ensure_equals("IssuerName/org unit",
  312. (std::string)llsd_cert["issuer_name"]["organizationalUnitName"], "Instituto Nacional de Tecnologia da Informacao - ITI");
  313. ensure_equals("IssuerName/state", (std::string)llsd_cert["issuer_name"]["stateOrProvinceName"], "DF");
  314. ensure_equals("Issuer name string",
  315. (std::string)llsd_cert["issuer_name_string"], "CN=Autoridade Certificadora Raiz Brasileira,ST=DF,"
  316. "L=Brasilia,OU=Instituto Nacional de Tecnologia da Informacao - ITI,O=ICP-Brasil,C=BR");
  317. ensure_equals("subject Name/commonName",
  318. (std::string)llsd_cert["subject_name"]["commonName"], "Autoridade Certificadora Raiz Brasileira");
  319. ensure_equals("subject Name/countryName", (std::string)llsd_cert["subject_name"]["countryName"], "BR");
  320. ensure_equals("subject Name/localityName", (std::string)llsd_cert["subject_name"]["localityName"], "Brasilia");
  321. ensure_equals("subject Name/org name", (std::string)llsd_cert["subject_name"]["organizationName"], "ICP-Brasil");
  322. ensure_equals("subjectName/org unit",
  323. (std::string)llsd_cert["subject_name"]["organizationalUnitName"], "Instituto Nacional de Tecnologia da Informacao - ITI");
  324. ensure_equals("subjectName/state", (std::string)llsd_cert["subject_name"]["stateOrProvinceName"], "DF");
  325. ensure_equals("subject name string",
  326. (std::string)llsd_cert["subject_name_string"], "CN=Autoridade Certificadora Raiz Brasileira,ST=DF,"
  327. "L=Brasilia,OU=Instituto Nacional de Tecnologia da Informacao - ITI,O=ICP-Brasil,C=BR");
  328. ensure_equals("md5 digest", (std::string)llsd_cert["md5_digest"], "96:89:7d:61:d1:55:2b:27:e2:5a:39:b4:2a:6c:44:6f");
  329. ensure_equals("serial number", (std::string)llsd_cert["serial_number"], "04");
  330. // sha1 digest is giving a weird value, and I've no idea why...feh
  331. //ensure_equals("sha1 digest", (std::string)llsd_cert["sha1_digest"], "8e:fd:ca:bc:93:e6:1e:92:5d:4d:1d:ed:18:1a:43:20:a4:67:a1:39");
  332. ensure_equals("valid from", (std::string)llsd_cert["valid_from"], "2001-11-30T12:58:00Z");
  333. ensure_equals("valid to", (std::string)llsd_cert["valid_to"], "2011-11-30T23:59:00Z");
  334. LLSD expectedKeyUsage = LLSD::emptyArray();
  335. expectedKeyUsage.append(LLSD((std::string)"certSigning"));
  336. expectedKeyUsage.append(LLSD((std::string)"crlSigning"));
  337. ensure("key usage", valueCompareLLSD(llsd_cert["keyUsage"], expectedKeyUsage));
  338. ensure("basic constraints", (bool)llsd_cert["basicConstraints"]["CA"]);
  339. ensure("x509 is equal", !X509_cmp(mX509TestCert, test_cert->getOpenSSLX509()));
  340. }
  341. // test protected data
  342. template<> template<>
  343. void sechandler_basic_test_object::test<2>()
  344. {
  345. std::string protected_data = "sUSh3wj77NG9oAMyt3XIhaej3KLZhLZWFZvI6rIGmwUUOmmelrRg0NI9rkOj8ZDpTPxpwToaBT5u"
  346. "GQhakdaGLJznr9bHr4/6HIC1bouKj4n2rs4TL6j2WSjto114QdlNfLsE8cbbE+ghww58g8SeyLQO"
  347. "nyzXoz+/PBz0HD5SMFDuObccoPW24gmqYySz8YoEWhSwO0pUtEEqOjVRsAJgF5wLAtJZDeuilGsq"
  348. "4ZT9Y4wZ9Rh8nnF3fDUL6IGamHe1ClXM1jgBu10F6UMhZbnH4C3aJ2E9+LiOntU+l3iCb2MpkEpr"
  349. "82r2ZAMwIrpnirL/xoYoyz7MJQYwUuMvBPToZJrxNSsjI+S2Z+I3iEJAELMAAA==";
  350. std::vector<U8> binary_data(apr_base64_decode_len(protected_data.c_str()));
  351. apr_base64_decode_binary(&binary_data[0], protected_data.c_str());
  352. LLXORCipher cipher(gMACAddress, MAC_ADDRESS_BYTES);
  353. cipher.decrypt(&binary_data[0], 16);
  354. unsigned char unique_id[MAC_ADDRESS_BYTES];
  355. LLMachineID::getUniqueID(unique_id, sizeof(unique_id));
  356. LLXORCipher cipher2(unique_id, sizeof(unique_id));
  357. cipher2.encrypt(&binary_data[0], 16);
  358. std::ofstream temp_file("sechandler_settings.tmp", std::ofstream::binary);
  359. temp_file.write((const char *)&binary_data[0], binary_data.size());
  360. temp_file.close();
  361. LLPointer<LLSecAPIBasicHandler> handler = new LLSecAPIBasicHandler("sechandler_settings.tmp",
  362. "test_password.dat");
  363. handler->init();
  364. // data retrieval for existing data
  365. LLSD data = handler->getProtectedData("test_data_type", "test_data_id");
  366. ensure_equals("retrieve existing data1", (std::string)data["data1"], "test_data_1");
  367. ensure_equals("retrieve existing data2", (std::string)data["data2"], "test_data_2");
  368. ensure_equals("retrieve existing data3", (std::string)data["data3"]["elem1"], "test element1");
  369. // data storage
  370. LLSD store_data = LLSD::emptyMap();
  371. store_data["store_data1"] = "test_store_data1";
  372. store_data["store_data2"] = 27;
  373. store_data["store_data3"] = LLSD::emptyMap();
  374. store_data["store_data3"]["subelem1"] = "test_subelem1";
  375. handler->setProtectedData("test_data_type", "test_data_id1", store_data);
  376. data = handler->getProtectedData("test_data_type", "test_data_id");
  377. data = handler->getProtectedData("test_data_type", "test_data_id");
  378. // verify no overwrite of existing data
  379. ensure_equals("verify no overwrite 1", (std::string)data["data1"], "test_data_1");
  380. ensure_equals("verify no overwrite 2", (std::string)data["data2"], "test_data_2");
  381. ensure_equals("verify no overwrite 3", (std::string)data["data3"]["elem1"], "test element1");
  382. // verify written data is good
  383. data = handler->getProtectedData("test_data_type", "test_data_id1");
  384. ensure_equals("verify stored data1", (std::string)data["store_data1"], "test_store_data1");
  385. ensure_equals("verify stored data2", (int)data["store_data2"], 27);
  386. ensure_equals("verify stored data3", (std::string)data["store_data3"]["subelem1"], "test_subelem1");
  387. // verify overwrite works
  388. handler->setProtectedData("test_data_type", "test_data_id", store_data);
  389. data = handler->getProtectedData("test_data_type", "test_data_id");
  390. ensure_equals("verify overwrite stored data1", (std::string)data["store_data1"], "test_store_data1");
  391. ensure_equals("verify overwrite stored data2", (int)data["store_data2"], 27);
  392. ensure_equals("verify overwrite stored data3", (std::string)data["store_data3"]["subelem1"], "test_subelem1");
  393. // verify other datatype doesn't conflict
  394. store_data["store_data3"] = "test_store_data3";
  395. store_data["store_data4"] = 28;
  396. store_data["store_data5"] = LLSD::emptyMap();
  397. store_data["store_data5"]["subelem2"] = "test_subelem2";
  398. handler->setProtectedData("test_data_type1", "test_data_id", store_data);
  399. data = handler->getProtectedData("test_data_type1", "test_data_id");
  400. ensure_equals("verify datatype stored data3", (std::string)data["store_data3"], "test_store_data3");
  401. ensure_equals("verify datatype stored data4", (int)data["store_data4"], 28);
  402. ensure_equals("verify datatype stored data5", (std::string)data["store_data5"]["subelem2"], "test_subelem2");
  403. // test data not found
  404. data = handler->getProtectedData("test_data_type1", "test_data_not_found");
  405. ensure("not found", data.isUndefined());
  406. // cause a 'write' by using 'LLPointer' to delete then instantiate a handler
  407. handler = NULL;
  408. handler = new LLSecAPIBasicHandler("sechandler_settings.tmp", "test_password.dat");
  409. handler->init();
  410. data = handler->getProtectedData("test_data_type1", "test_data_id");
  411. ensure_equals("verify datatype stored data3a", (std::string)data["store_data3"], "test_store_data3");
  412. ensure_equals("verify datatype stored data4a", (int)data["store_data4"], 28);
  413. ensure_equals("verify datatype stored data5a", (std::string)data["store_data5"]["subelem2"], "test_subelem2");
  414. // rewrite the initial file to verify reloads
  415. handler = NULL;
  416. std::ofstream temp_file2("sechandler_settings.tmp", std::ofstream::binary);
  417. temp_file2.write((const char *)&binary_data[0], binary_data.size());
  418. temp_file2.close();
  419. // cause a 'write'
  420. handler = new LLSecAPIBasicHandler("sechandler_settings.tmp", "test_password.dat");
  421. handler->init();
  422. data = handler->getProtectedData("test_data_type1", "test_data_id");
  423. ensure("not found", data.isUndefined());
  424. handler->deleteProtectedData("test_data_type", "test_data_id");
  425. ensure("Deleted data not found", handler->getProtectedData("test_data_type", "test_data_id").isUndefined());
  426. LLFile::remove("sechandler_settings.tmp");
  427. handler = new LLSecAPIBasicHandler("sechandler_settings.tmp", "test_password.dat");
  428. handler->init();
  429. data = handler->getProtectedData("test_data_type1", "test_data_id");
  430. ensure("not found", data.isUndefined());
  431. handler = NULL;
  432. ensure(LLFile::isfile("sechandler_settings.tmp"));
  433. }
  434. // test credenitals
  435. template<> template<>
  436. void sechandler_basic_test_object::test<3>()
  437. {
  438. LLPointer<LLSecAPIBasicHandler> handler = new LLSecAPIBasicHandler("sechandler_settings.tmp", "test_password.dat");
  439. handler->init();
  440. LLSD my_id = LLSD::emptyMap();
  441. LLSD my_authenticator = LLSD::emptyMap();
  442. my_id["type"] = "test_type";
  443. my_id["username"] = "testuser@lindenlab.com";
  444. my_authenticator["type"] = "test_auth";
  445. my_authenticator["creds"] = "12345";
  446. // test creation of credentials
  447. LLPointer<LLCredential> my_cred = handler->createCredential("my_grid", my_id, my_authenticator);
  448. // test retrieval of credential components
  449. ensure_equals("basic credential creation: identifier", my_id, my_cred->getIdentifier());
  450. ensure_equals("basic credential creation: authenticator", my_authenticator, my_cred->getAuthenticator());
  451. ensure_equals("basic credential creation: grid", "my_grid", my_cred->getGrid());
  452. // test setting/overwriting of credential components
  453. my_id["first_name"] = "firstname";
  454. my_id.erase("username");
  455. my_authenticator.erase("creds");
  456. my_authenticator["hash"] = "6563245";
  457. my_cred->setCredentialData(my_id, my_authenticator);
  458. ensure_equals("set credential data: identifier", my_id, my_cred->getIdentifier());
  459. ensure_equals("set credential data: authenticator", my_authenticator, my_cred->getAuthenticator());
  460. ensure_equals("set credential data: grid", "my_grid", my_cred->getGrid());
  461. // test loading of a credential, that hasn't been saved, without
  462. // any legacy saved credential data
  463. LLPointer<LLCredential> my_new_cred = handler->loadCredential("my_grid2");
  464. ensure("unknown credential load test", my_new_cred->getIdentifier().isMap());
  465. ensure("unknown credential load test", !my_new_cred->getIdentifier().has("type"));
  466. ensure("unknown credential load test", my_new_cred->getAuthenticator().isMap());
  467. ensure("unknown credential load test", !my_new_cred->getAuthenticator().has("type"));
  468. // test saving of a credential
  469. handler->saveCredential(my_cred, true);
  470. // test loading of a known credential
  471. my_new_cred = handler->loadCredential("my_grid");
  472. ensure_equals("load a known credential: identifier", my_id, my_new_cred->getIdentifier());
  473. ensure_equals("load a known credential: authenticator",my_authenticator, my_new_cred->getAuthenticator());
  474. ensure_equals("load a known credential: grid", "my_grid", my_cred->getGrid());
  475. // test deletion of a credential
  476. handler->deleteCredential(my_new_cred);
  477. ensure("delete credential: identifier", my_new_cred->getIdentifier().isUndefined());
  478. ensure("delete credentialt: authenticator", my_new_cred->getIdentifier().isUndefined());
  479. ensure_equals("delete credential: grid", "my_grid", my_cred->getGrid());
  480. // load unknown cred
  481. my_new_cred = handler->loadCredential("my_grid");
  482. ensure("deleted credential load test", my_new_cred->getIdentifier().isMap());
  483. ensure("deleted credential load test", !my_new_cred->getIdentifier().has("type"));
  484. ensure("deleted credential load test", my_new_cred->getAuthenticator().isMap());
  485. ensure("deleted credential load test", !my_new_cred->getAuthenticator().has("type"));
  486. // test loading of an unknown credential with legacy saved username, but without
  487. // saved password
  488. gFirstName = "myfirstname";
  489. gLastName = "mylastname";
  490. my_new_cred = handler->loadCredential("my_legacy_grid");
  491. ensure_equals("legacy credential with no password: type",
  492. (const std::string)my_new_cred->getIdentifier()["type"], "agent");
  493. ensure_equals("legacy credential with no password: first_name",
  494. (const std::string)my_new_cred->getIdentifier()["first_name"], "myfirstname");
  495. ensure_equals("legacy credential with no password: last_name",
  496. (const std::string)my_new_cred->getIdentifier()["last_name"], "mylastname");
  497. ensure("legacy credential with no password: no authenticator", my_new_cred->getAuthenticator().isUndefined());
  498. // test loading of an unknown credential with legacy saved password and username
  499. std::string hashed_password = "fSQcLG03eyIWJmkzfyYaKm81dSweLmsxeSAYKGE7fSQ=";
  500. int length = apr_base64_decode_len(hashed_password.c_str());
  501. std::vector<char> decoded_password(length);
  502. apr_base64_decode(&decoded_password[0], hashed_password.c_str());
  503. LLXORCipher cipher(gMACAddress, MAC_ADDRESS_BYTES);
  504. cipher.decrypt((U8*)&decoded_password[0], length);
  505. unsigned char unique_id[MAC_ADDRESS_BYTES];
  506. LLMachineID::getUniqueID(unique_id, sizeof(unique_id));
  507. LLXORCipher cipher2(unique_id, sizeof(unique_id));
  508. cipher2.encrypt((U8*)&decoded_password[0], length);
  509. llofstream password_file("test_password.dat", std::ofstream::binary);
  510. password_file.write(&decoded_password[0], length);
  511. password_file.close();
  512. my_new_cred = handler->loadCredential("my_legacy_grid2");
  513. ensure_equals("legacy credential with password: type",
  514. (const std::string)my_new_cred->getIdentifier()["type"], "agent");
  515. ensure_equals("legacy credential with password: first_name",
  516. (const std::string)my_new_cred->getIdentifier()["first_name"], "myfirstname");
  517. ensure_equals("legacy credential with password: last_name",
  518. (const std::string)my_new_cred->getIdentifier()["last_name"], "mylastname");
  519. LLSD legacy_authenticator = my_new_cred->getAuthenticator();
  520. ensure_equals("legacy credential with password: type",
  521. (std::string)legacy_authenticator["type"],
  522. "hash");
  523. ensure_equals("legacy credential with password: algorithm",
  524. (std::string)legacy_authenticator["algorithm"],
  525. "md5");
  526. ensure_equals("legacy credential with password: algorithm",
  527. (std::string)legacy_authenticator["secret"],
  528. "01234567890123456789012345678901");
  529. // test creation of credentials
  530. my_cred = handler->createCredential("mysavedgrid", my_id, my_authenticator);
  531. // test save without saving authenticator.
  532. handler->saveCredential(my_cred, FALSE);
  533. my_new_cred = handler->loadCredential("mysavedgrid");
  534. ensure_equals("saved credential without auth",
  535. (const std::string)my_new_cred->getIdentifier()["type"], "test_type");
  536. ensure("no authenticator values were saved", my_new_cred->getAuthenticator().isUndefined());
  537. }
  538. // test cert vector
  539. template<> template<>
  540. void sechandler_basic_test_object::test<4>()
  541. {
  542. // validate create from empty vector
  543. LLPointer<LLBasicCertificateVector> test_vector = new LLBasicCertificateVector();
  544. ensure_equals("when loading with nothing, we should result in no certs in vector", test_vector->size(), 0);
  545. test_vector->add(new LLBasicCertificate(mPemTestCert));
  546. ensure_equals("one element in vector", test_vector->size(), 1);
  547. test_vector->add(new LLBasicCertificate(mPemChildCert));
  548. ensure_equals("two elements in vector after add", test_vector->size(), 2);
  549. test_vector->add(new LLBasicCertificate(mPemChildCert));
  550. ensure_equals("two elements in vector after re-add", test_vector->size(), 2);
  551. // validate order
  552. X509* test_cert = (*test_vector)[0]->getOpenSSLX509();
  553. ensure("first cert added remains first cert", !X509_cmp(test_cert, mX509TestCert));
  554. X509_free(test_cert);
  555. test_cert = (*test_vector)[1]->getOpenSSLX509();
  556. ensure("adding a duplicate cert", !X509_cmp(test_cert, mX509ChildCert));
  557. X509_free(test_cert);
  558. //
  559. // validate iterator
  560. //
  561. LLBasicCertificateVector::iterator current_cert = test_vector->begin();
  562. LLBasicCertificateVector::iterator copy_current_cert = current_cert;
  563. // operator++(int)
  564. ensure("validate iterator++ element in vector is expected cert", *current_cert++ == (*test_vector)[0]);
  565. ensure("validate 2nd iterator++ element in vector is expected cert", *current_cert++ == (*test_vector)[1]);
  566. ensure("validate end iterator++", current_cert == test_vector->end());
  567. // copy
  568. ensure("validate copy iterator element in vector is expected cert", *copy_current_cert == (*test_vector)[0]);
  569. // operator--(int)
  570. current_cert--;
  571. ensure("validate iterator-- element in vector is expected cert", *current_cert-- == (*test_vector)[1]);
  572. ensure("validate iterator-- element in vector is expected cert", *current_cert == (*test_vector)[0]);
  573. ensure("begin iterator is equal", current_cert == test_vector->begin());
  574. // operator++
  575. ensure("validate ++iterator element in vector is expected cert", *++current_cert == (*test_vector)[1]);
  576. ensure("end of cert vector after ++iterator", ++current_cert == test_vector->end());
  577. // operator--
  578. ensure("validate --iterator element in vector is expected cert", *--current_cert == (*test_vector)[1]);
  579. ensure("validate 2nd --iterator element in vector is expected cert", *--current_cert == (*test_vector)[0]);
  580. // validate remove
  581. // validate create from empty vector
  582. test_vector = new LLBasicCertificateVector();
  583. test_vector->add(new LLBasicCertificate(mPemTestCert));
  584. test_vector->add(new LLBasicCertificate(mPemChildCert));
  585. test_vector->erase(test_vector->begin());
  586. ensure_equals("one element in store after remove", test_vector->size(), 1);
  587. test_cert = (*test_vector)[0]->getOpenSSLX509();
  588. ensure("validate cert was removed", !X509_cmp(test_cert, mX509ChildCert));
  589. X509_free(test_cert);
  590. // validate insert
  591. test_vector->insert(test_vector->begin(), new LLBasicCertificate(mPemChildCert));
  592. test_cert = (*test_vector)[0]->getOpenSSLX509();
  593. ensure("validate cert was inserted", !X509_cmp(test_cert, mX509ChildCert));
  594. X509_free(test_cert);
  595. //validate find
  596. LLSD find_info = LLSD::emptyMap();
  597. test_vector->insert(test_vector->begin(), new LLBasicCertificate(mPemRootCert));
  598. find_info["issuer_name"] = LLSD::emptyMap();
  599. find_info["issuer_name"]["commonName"] = "Roxies intermediate CA";
  600. find_info["md5_digest"] = "97:24:c7:4c:d4:ba:2d:0e:9c:a1:18:8e:3a:c6:1f:c3";
  601. current_cert = test_vector->find(find_info);
  602. ensure("found", current_cert != test_vector->end());
  603. ensure("found cert", (*current_cert).get() == (*test_vector)[1].get());
  604. find_info["sha1_digest"] = "bad value";
  605. current_cert =test_vector->find(find_info);
  606. ensure("didn't find cert", current_cert == test_vector->end());
  607. }
  608. // test cert store
  609. template<> template<>
  610. void sechandler_basic_test_object::test<5>()
  611. {
  612. // validate load with nothing
  613. LLFile::remove("mycertstore.pem");
  614. LLPointer<LLBasicCertificateStore> test_store = new LLBasicCertificateStore("mycertstore.pem");
  615. ensure_equals("when loading with nothing, we should result in no certs in store", test_store->size(), 0);
  616. // validate load with empty file
  617. test_store->save();
  618. test_store = NULL;
  619. test_store = new LLBasicCertificateStore("mycertstore.pem");
  620. ensure_equals("when loading with nothing, we should result in no certs in store", test_store->size(), 0);
  621. test_store=NULL;
  622. // instantiate a cert store from a file
  623. llofstream certstorefile("mycertstore.pem", std::ios::out);
  624. certstorefile << mPemChildCert << std::endl << mPemTestCert << std::endl;
  625. certstorefile.close();
  626. // validate loaded certs
  627. test_store = new LLBasicCertificateStore("mycertstore.pem");
  628. ensure_equals("two elements in store", test_store->size(), 2);
  629. // operator[]
  630. X509* test_cert = (*test_store)[0]->getOpenSSLX509();
  631. ensure("validate first element in store is expected cert", !X509_cmp(test_cert, mX509ChildCert));
  632. X509_free(test_cert);
  633. test_cert = (*test_store)[1]->getOpenSSLX509();
  634. ensure("validate second element in store is expected cert", !X509_cmp(test_cert, mX509TestCert));
  635. X509_free(test_cert);
  636. // validate save
  637. LLFile::remove("mycertstore.pem");
  638. test_store->save();
  639. test_store = NULL;
  640. test_store = new LLBasicCertificateStore("mycertstore.pem");
  641. ensure_equals("two elements in store after save", test_store->size(), 2);
  642. LLCertificateStore::iterator current_cert = test_store->begin();
  643. test_cert = (*current_cert)->getOpenSSLX509();
  644. ensure("validate first element in store is expected cert", !X509_cmp(test_cert, mX509ChildCert));
  645. current_cert++;
  646. X509_free(test_cert);
  647. test_cert = (*current_cert)->getOpenSSLX509();
  648. ensure("validate second element in store is expected cert", !X509_cmp(test_cert, mX509TestCert));
  649. X509_free(test_cert);
  650. current_cert++;
  651. ensure("end of cert store", current_cert == test_store->end());
  652. }
  653. // cert name wildcard matching
  654. template<> template<>
  655. void sechandler_basic_test_object::test<6>()
  656. {
  657. ensure("simple name match",
  658. _cert_hostname_wildcard_match("foo", "foo"));
  659. ensure("simple name match, with end period",
  660. _cert_hostname_wildcard_match("foo.", "foo."));
  661. ensure("simple name match, with begin period",
  662. _cert_hostname_wildcard_match(".foo", ".foo"));
  663. ensure("simple name match, with mismatched period cn",
  664. _cert_hostname_wildcard_match("foo.", "foo"));
  665. ensure("simple name match, with mismatched period hostname",
  666. _cert_hostname_wildcard_match("foo", "foo."));
  667. ensure("simple name match, with subdomain",
  668. _cert_hostname_wildcard_match("foo.bar", "foo.bar"));
  669. ensure("stutter name match",
  670. _cert_hostname_wildcard_match("foobbbbfoo", "foo*bbbfoo"));
  671. ensure("simple name match, with beginning wildcard",
  672. _cert_hostname_wildcard_match("foobar", "*bar"));
  673. ensure("simple name match, with ending wildcard",
  674. _cert_hostname_wildcard_match("foobar", "foo*"));
  675. ensure("simple name match, with beginning null wildcard",
  676. _cert_hostname_wildcard_match("foobar", "*foobar"));
  677. ensure("simple name match, with ending null wildcard",
  678. _cert_hostname_wildcard_match("foobar", "foobar*"));
  679. ensure("simple name match, with embedded wildcard",
  680. _cert_hostname_wildcard_match("foobar", "f*r"));
  681. ensure("simple name match, with embedded null wildcard",
  682. _cert_hostname_wildcard_match("foobar", "foo*bar"));
  683. ensure("simple name match, with dual embedded wildcard",
  684. _cert_hostname_wildcard_match("foobar", "f*o*ar"));
  685. ensure("simple name mismatch",
  686. !_cert_hostname_wildcard_match("bar", "foo"));
  687. ensure("simple name mismatch, with end period",
  688. !_cert_hostname_wildcard_match("foobar.", "foo."));
  689. ensure("simple name mismatch, with begin period",
  690. !_cert_hostname_wildcard_match(".foobar", ".foo"));
  691. ensure("simple name mismatch, with subdomain",
  692. !_cert_hostname_wildcard_match("foobar.bar", "foo.bar"));
  693. ensure("simple name mismatch, with beginning wildcard",
  694. !_cert_hostname_wildcard_match("foobara", "*bar"));
  695. ensure("simple name mismatch, with ending wildcard",
  696. !_cert_hostname_wildcard_match("oobar", "foo*"));
  697. ensure("simple name mismatch, with embedded wildcard",
  698. !_cert_hostname_wildcard_match("oobar", "f*r"));
  699. ensure("simple name mismatch, with dual embedded wildcard",
  700. !_cert_hostname_wildcard_match("foobar", "f*d*ar"));
  701. ensure("simple wildcard",
  702. _cert_hostname_wildcard_match("foobar", "*"));
  703. ensure("long domain",
  704. _cert_hostname_wildcard_match("foo.bar.com", "foo.bar.com"));
  705. ensure("long domain with multiple wildcards",
  706. _cert_hostname_wildcard_match("foo.bar.com", "*.b*r.com"));
  707. ensure("end periods",
  708. _cert_hostname_wildcard_match("foo.bar.com.", "*.b*r.com."));
  709. ensure("match end period",
  710. _cert_hostname_wildcard_match("foo.bar.com.", "*.b*r.com"));
  711. ensure("match end period2",
  712. _cert_hostname_wildcard_match("foo.bar.com", "*.b*r.com."));
  713. ensure("wildcard mismatch",
  714. !_cert_hostname_wildcard_match("bar.com", "*.bar.com"));
  715. ensure("wildcard match",
  716. _cert_hostname_wildcard_match("foo.bar.com", "*.bar.com"));
  717. ensure("wildcard match",
  718. _cert_hostname_wildcard_match("foo.foo.bar.com", "*.bar.com"));
  719. ensure("wildcard match",
  720. _cert_hostname_wildcard_match("foo.foo.bar.com", "*.*.com"));
  721. ensure("wildcard mismatch",
  722. !_cert_hostname_wildcard_match("foo.foo.bar.com", "*.foo.com"));
  723. }
  724. // test cert chain
  725. template<> template<>
  726. void sechandler_basic_test_object::test<7>()
  727. {
  728. // validate create from empty chain
  729. LLPointer<LLBasicCertificateChain> test_chain = new LLBasicCertificateChain(NULL);
  730. ensure_equals("when loading with nothing, we should result in no certs in chain", test_chain->size(), 0);
  731. // Single cert in the chain.
  732. X509_STORE_CTX *test_store = X509_STORE_CTX_new();
  733. test_store->cert = mX509ChildCert;
  734. test_store->untrusted = NULL;
  735. test_chain = new LLBasicCertificateChain(test_store);
  736. X509_STORE_CTX_free(test_store);
  737. ensure_equals("two elements in store", test_chain->size(), 1);
  738. X509* test_cert = (*test_chain)[0]->getOpenSSLX509();
  739. ensure("validate first element in store is expected cert", !X509_cmp(test_cert, mX509ChildCert));
  740. X509_free(test_cert);
  741. // cert + CA
  742. test_store = X509_STORE_CTX_new();
  743. test_store->cert = mX509ChildCert;
  744. test_store->untrusted = sk_X509_new_null();
  745. sk_X509_push(test_store->untrusted, mX509IntermediateCert);
  746. test_chain = new LLBasicCertificateChain(test_store);
  747. X509_STORE_CTX_free(test_store);
  748. ensure_equals("two elements in store", test_chain->size(), 2);
  749. test_cert = (*test_chain)[0]->getOpenSSLX509();
  750. ensure("validate first element in store is expected cert", !X509_cmp(test_cert, mX509ChildCert));
  751. X509_free(test_cert);
  752. test_cert = (*test_chain)[1]->getOpenSSLX509();
  753. ensure("validate second element in store is expected cert", !X509_cmp(test_cert, mX509IntermediateCert));
  754. X509_free(test_cert);
  755. // cert + nonrelated
  756. test_store = X509_STORE_CTX_new();
  757. test_store->cert = mX509ChildCert;
  758. test_store->untrusted = sk_X509_new_null();
  759. sk_X509_push(test_store->untrusted, mX509TestCert);
  760. test_chain = new LLBasicCertificateChain(test_store);
  761. X509_STORE_CTX_free(test_store);
  762. ensure_equals("two elements in store", test_chain->size(), 1);
  763. test_cert = (*test_chain)[0]->getOpenSSLX509();
  764. ensure("validate first element in store is expected cert", !X509_cmp(test_cert, mX509ChildCert));
  765. X509_free(test_cert);
  766. // cert + CA + nonrelated
  767. test_store = X509_STORE_CTX_new();
  768. test_store->cert = mX509ChildCert;
  769. test_store->untrusted = sk_X509_new_null();
  770. sk_X509_push(test_store->untrusted, mX509IntermediateCert);
  771. sk_X509_push(test_store->untrusted, mX509TestCert);
  772. test_chain = new LLBasicCertificateChain(test_store);
  773. X509_STORE_CTX_free(test_store);
  774. ensure_equals("two elements in store", test_chain->size(), 2);
  775. test_cert = (*test_chain)[0]->getOpenSSLX509();
  776. ensure("validate first element in store is expected cert", !X509_cmp(test_cert, mX509ChildCert));
  777. X509_free(test_cert);
  778. test_cert = (*test_chain)[1]->getOpenSSLX509();
  779. ensure("validate second element in store is expected cert", !X509_cmp(test_cert, mX509IntermediateCert));
  780. X509_free(test_cert);
  781. // cert + intermediate + CA
  782. test_store = X509_STORE_CTX_new();
  783. test_store->cert = mX509ChildCert;
  784. test_store->untrusted = sk_X509_new_null();
  785. sk_X509_push(test_store->untrusted, mX509IntermediateCert);
  786. sk_X509_push(test_store->untrusted, mX509RootCert);
  787. test_chain = new LLBasicCertificateChain(test_store);
  788. X509_STORE_CTX_free(test_store);
  789. ensure_equals("three elements in store", test_chain->size(), 3);
  790. test_cert = (*test_chain)[0]->getOpenSSLX509();
  791. ensure("validate first element in store is expected cert", !X509_cmp(test_cert, mX509ChildCert));
  792. X509_free(test_cert);
  793. test_cert = (*test_chain)[1]->getOpenSSLX509();
  794. ensure("validate second element in store is expected cert", !X509_cmp(test_cert, mX509IntermediateCert));
  795. X509_free(test_cert);
  796. test_cert = (*test_chain)[2]->getOpenSSLX509();
  797. ensure("validate second element in store is expected cert", !X509_cmp(test_cert, mX509RootCert));
  798. X509_free(test_cert);
  799. }
  800. // test cert validation
  801. template<> template<>
  802. void sechandler_basic_test_object::test<8>()
  803. {
  804. // start with a trusted store with our known root cert
  805. LLFile::remove("mycertstore.pem");
  806. LLPointer<LLBasicCertificateStore> test_store = new LLBasicCertificateStore("mycertstore.pem");
  807. test_store->add(new LLBasicCertificate(mX509RootCert));
  808. LLSD validation_params;
  809. // validate basic trust for a chain containing only the intermediate cert. (1 deep)
  810. LLPointer<LLBasicCertificateChain> test_chain = new LLBasicCertificateChain(NULL);
  811. test_chain->add(new LLBasicCertificate(mX509IntermediateCert));
  812. test_store->validate(0, test_chain, validation_params);
  813. // add the root certificate to the chain and revalidate
  814. test_chain->add(new LLBasicCertificate(mX509RootCert));
  815. test_store->validate(0, test_chain, validation_params);
  816. // add the child cert at the head of the chain, and revalidate (3 deep chain)
  817. test_chain->insert(test_chain->begin(), new LLBasicCertificate(mX509ChildCert));
  818. test_store->validate(0, test_chain, validation_params);
  819. // basic failure cases
  820. test_chain = new LLBasicCertificateChain(NULL);
  821. //validate with only the child cert in chain, but child cert was previously
  822. // trusted
  823. test_chain->add(new LLBasicCertificate(mX509ChildCert));
  824. // validate without the trust flag.
  825. test_store->validate(VALIDATION_POLICY_TRUSTED, test_chain, validation_params);
  826. // Validate with child cert but no parent, and no parent in CA store
  827. test_store = new LLBasicCertificateStore("mycertstore.pem");
  828. ensure_throws("no CA, with only a child cert",
  829. LLCertValidationTrustException,
  830. (*test_chain)[0],
  831. test_store->validate,
  832. VALIDATION_POLICY_TRUSTED,
  833. test_chain,
  834. validation_params);
  835. // validate without the trust flag.
  836. test_store->validate(0, test_chain, validation_params);
  837. // clear out the store
  838. test_store = new LLBasicCertificateStore("mycertstore.pem");
  839. // append the intermediate cert
  840. test_chain->add(new LLBasicCertificate(mX509IntermediateCert));
  841. ensure_throws("no CA, with child and intermediate certs",
  842. LLCertValidationTrustException,
  843. (*test_chain)[1],
  844. test_store->validate,
  845. VALIDATION_POLICY_TRUSTED,
  846. test_chain,
  847. validation_params);
  848. // validate without the trust flag
  849. test_store->validate(0, test_chain, validation_params);
  850. // Test time validity
  851. LLSD child_info;
  852. ((*test_chain)[0])->getLLSD(child_info);
  853. validation_params = LLSD::emptyMap();
  854. validation_params[CERT_VALIDATION_DATE] = LLDate(child_info[CERT_VALID_FROM].asDate().secondsSinceEpoch() + 1.0);
  855. test_store->validate(VALIDATION_POLICY_TIME, test_chain, validation_params);
  856. validation_params = LLSD::emptyMap();
  857. validation_params[CERT_VALIDATION_DATE] = child_info[CERT_VALID_FROM].asDate();
  858. validation_params[CERT_VALIDATION_DATE] = LLDate(child_info[CERT_VALID_FROM].asDate().secondsSinceEpoch() - 1.0);
  859. // test not yet valid
  860. ensure_throws("Child cert not yet valid" ,
  861. LLCertValidationExpirationException,
  862. (*test_chain)[0],
  863. test_store->validate,
  864. VALIDATION_POLICY_TIME,
  865. test_chain,
  866. validation_params);
  867. validation_params = LLSD::emptyMap();
  868. validation_params[CERT_VALIDATION_DATE] = LLDate(child_info[CERT_VALID_TO].asDate().secondsSinceEpoch() + 1.0);
  869. // test cert expired
  870. ensure_throws("Child cert expired",
  871. LLCertValidationExpirationException,
  872. (*test_chain)[0],
  873. test_store->validate,
  874. VALIDATION_POLICY_TIME,
  875. test_chain,
  876. validation_params);
  877. // test SSL KU
  878. // validate basic trust for a chain containing child and intermediate.
  879. test_chain = new LLBasicCertificateChain(NULL);
  880. test_chain->add(new LLBasicCertificate(mX509ChildCert));
  881. test_chain->add(new LLBasicCertificate(mX509IntermediateCert));
  882. test_store->validate(VALIDATION_POLICY_SSL_KU, test_chain, validation_params);
  883. test_chain = new LLBasicCertificateChain(NULL);
  884. test_chain->add(new LLBasicCertificate(mX509TestCert));
  885. test_store = new LLBasicCertificateStore("mycertstore.pem");
  886. ensure_throws("Cert doesn't have ku",
  887. LLCertKeyUsageValidationException,
  888. (*test_chain)[0],
  889. test_store->validate,
  890. VALIDATION_POLICY_SSL_KU,
  891. test_chain,
  892. validation_params);
  893. // test sha1RSA validation
  894. test_chain = new LLBasicCertificateChain(NULL);
  895. test_chain->add(new LLBasicCertificate(mSha1RSATestCert));
  896. test_chain->add(new LLBasicCertificate(mSha1RSATestCA));
  897. test_store->validate(0, test_chain, validation_params);
  898. }
  899. };