You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

360 lines
9.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2012, Google Inc.
  4. * Copyright 2012, RTFM, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  20. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  23. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  25. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #if HAVE_CONFIG_H
  29. #include "config.h"
  30. #endif // HAVE_CONFIG_H
  31. #if HAVE_NSS_SSL_H
  32. #include "talk/base/nssidentity.h"
  33. #include <string>
  34. #include "cert.h"
  35. #include "cryptohi.h"
  36. #include "keyhi.h"
  37. #include "nss.h"
  38. #include "pk11pub.h"
  39. #include "sechash.h"
  40. #include "talk/base/base64.h"
  41. #include "talk/base/logging.h"
  42. #include "talk/base/helpers.h"
  43. #include "talk/base/nssstreamadapter.h"
  44. namespace talk_base {
  45. NSSKeyPair::~NSSKeyPair() {
  46. if (privkey_)
  47. SECKEY_DestroyPrivateKey(privkey_);
  48. if (pubkey_)
  49. SECKEY_DestroyPublicKey(pubkey_);
  50. }
  51. NSSKeyPair *NSSKeyPair::Generate() {
  52. SECKEYPrivateKey *privkey = NULL;
  53. SECKEYPublicKey *pubkey = NULL;
  54. PK11RSAGenParams rsaparams;
  55. rsaparams.keySizeInBits = 1024;
  56. rsaparams.pe = 0x010001; // 65537 -- a common RSA public exponent.
  57. privkey = PK11_GenerateKeyPair(NSSContext::GetSlot(),
  58. CKM_RSA_PKCS_KEY_PAIR_GEN,
  59. &rsaparams, &pubkey, PR_FALSE,
  60. PR_TRUE, NULL);
  61. if (!privkey) {
  62. LOG(LS_ERROR) << "Couldn't generate key pair";
  63. return NULL;
  64. }
  65. return new NSSKeyPair(privkey, pubkey);
  66. }
  67. // Just make a copy.
  68. NSSKeyPair *NSSKeyPair::GetReference() {
  69. SECKEYPrivateKey *privkey = SECKEY_CopyPrivateKey(privkey_);
  70. if (!privkey)
  71. return NULL;
  72. SECKEYPublicKey *pubkey = SECKEY_CopyPublicKey(pubkey_);
  73. if (!pubkey) {
  74. SECKEY_DestroyPrivateKey(privkey);
  75. return NULL;
  76. }
  77. return new NSSKeyPair(privkey, pubkey);
  78. }
  79. NSSCertificate *NSSCertificate::FromPEMString(const std::string &pem_string,
  80. int *pem_length) {
  81. // Find the inner body. We need this to fulfill the contract of
  82. // returning pem_length
  83. size_t header = pem_string.find("-----BEGIN CERTIFICATE-----");
  84. if (header == std::string::npos)
  85. return NULL;
  86. size_t body = pem_string.find("\n", header);
  87. if (body == std::string::npos)
  88. return NULL;
  89. size_t trailer = pem_string.find("-----END CERTIFICATE-----");
  90. if (trailer == std::string::npos)
  91. return NULL;
  92. std::string inner = pem_string.substr(body + 1, trailer - (body + 1));
  93. std::string der = Base64::Decode(inner, Base64::DO_PARSE_WHITE |
  94. Base64::DO_PAD_ANY |
  95. Base64::DO_TERM_BUFFER);
  96. if (der.empty())
  97. return NULL;
  98. SECItem der_cert;
  99. der_cert.data = reinterpret_cast<unsigned char *>(const_cast<char *>(
  100. der.data()));
  101. der_cert.len = der.size();
  102. CERTCertificate *cert = CERT_NewTempCertificate(CERT_GetDefaultCertDB(),
  103. &der_cert, NULL, PR_FALSE, PR_TRUE);
  104. if (!cert)
  105. return NULL;
  106. // Success
  107. if (pem_length)
  108. *pem_length = inner.size();
  109. return new NSSCertificate(cert);
  110. }
  111. NSSCertificate *NSSCertificate::GetReference() const {
  112. CERTCertificate *certificate = CERT_DupCertificate(certificate_);
  113. if (!certificate)
  114. return NULL;
  115. return new NSSCertificate(certificate);
  116. }
  117. std::string NSSCertificate::ToPEMString() const {
  118. return "";
  119. }
  120. bool NSSCertificate::GetDigestLength(const std::string &algorithm,
  121. std::size_t *length) {
  122. const SECHashObject *ho;
  123. if (!GetDigestObject(algorithm, &ho))
  124. return false;
  125. *length = ho->length;
  126. return true;
  127. }
  128. bool NSSCertificate::ComputeDigest(const std::string &algorithm,
  129. unsigned char *digest, std::size_t size,
  130. std::size_t *length) const {
  131. const SECHashObject *ho;
  132. if (!GetDigestObject(algorithm, &ho))
  133. return false;
  134. if (size < ho->length) // Sanity check for fit
  135. return false;
  136. SECStatus rv = HASH_HashBuf(ho->type, digest,
  137. certificate_->derCert.data,
  138. certificate_->derCert.len);
  139. if (rv != SECSuccess)
  140. return false;
  141. *length = ho->length;
  142. return true;
  143. }
  144. bool NSSCertificate::Equals(const NSSCertificate *tocompare) const {
  145. if (!certificate_->derCert.len)
  146. return false;
  147. if (!tocompare->certificate_->derCert.len)
  148. return false;
  149. if (certificate_->derCert.len != tocompare->certificate_->derCert.len)
  150. return false;
  151. return memcmp(certificate_->derCert.data,
  152. tocompare->certificate_->derCert.data,
  153. certificate_->derCert.len) == 0;
  154. }
  155. bool NSSCertificate::GetDigestObject(const std::string &algorithm,
  156. const SECHashObject **hop) {
  157. const SECHashObject *ho;
  158. HASH_HashType hash_type;
  159. if (algorithm == DIGEST_SHA_1) {
  160. hash_type = HASH_AlgSHA1;
  161. // HASH_AlgSHA224 is not supported in the chromium linux build system.
  162. #if 0
  163. } else if (algorithm == DIGEST_SHA_224) {
  164. hash_type = HASH_AlgSHA224;
  165. #endif
  166. } else if (algorithm == DIGEST_SHA_256) {
  167. hash_type = HASH_AlgSHA256;
  168. } else if (algorithm == DIGEST_SHA_384) {
  169. hash_type = HASH_AlgSHA384;
  170. } else if (algorithm == DIGEST_SHA_512) {
  171. hash_type = HASH_AlgSHA512;
  172. } else {
  173. return false;
  174. }
  175. ho = HASH_GetHashObject(hash_type);
  176. ASSERT(ho->length >= 20); // Can't happen
  177. *hop = ho;
  178. return true;
  179. }
  180. NSSIdentity *NSSIdentity::Generate(const std::string &common_name) {
  181. std::string subject_name_string = "CN=" + common_name;
  182. CERTName *subject_name = CERT_AsciiToName(
  183. const_cast<char *>(subject_name_string.c_str()));
  184. NSSIdentity *identity = NULL;
  185. CERTSubjectPublicKeyInfo *spki = NULL;
  186. CERTCertificateRequest *certreq = NULL;
  187. CERTValidity *validity;
  188. CERTCertificate *certificate = NULL;
  189. NSSKeyPair *keypair = NSSKeyPair::Generate();
  190. SECItem inner_der;
  191. SECStatus rv;
  192. PLArenaPool* arena;
  193. SECItem signed_cert;
  194. PRTime not_before, not_after;
  195. PRTime now = PR_Now();
  196. PRTime one_day;
  197. inner_der.len = 0;
  198. inner_der.data = NULL;
  199. if (!keypair) {
  200. LOG(LS_ERROR) << "Couldn't generate key pair";
  201. goto fail;
  202. }
  203. if (!subject_name) {
  204. LOG(LS_ERROR) << "Couldn't convert subject name " << subject_name;
  205. goto fail;
  206. }
  207. spki = SECKEY_CreateSubjectPublicKeyInfo(keypair->pubkey());
  208. if (!spki) {
  209. LOG(LS_ERROR) << "Couldn't create SPKI";
  210. goto fail;
  211. }
  212. certreq = CERT_CreateCertificateRequest(subject_name, spki, NULL);
  213. if (!certreq) {
  214. LOG(LS_ERROR) << "Couldn't create certificate signing request";
  215. goto fail;
  216. }
  217. one_day = 86400;
  218. one_day *= PR_USEC_PER_SEC;
  219. not_before = now - one_day;
  220. not_after = now + 30 * one_day;
  221. validity = CERT_CreateValidity(not_before, not_after);
  222. if (!validity) {
  223. LOG(LS_ERROR) << "Couldn't create validity";
  224. goto fail;
  225. }
  226. unsigned long serial;
  227. // Note: This serial in principle could collide, but it's unlikely
  228. rv = PK11_GenerateRandom(reinterpret_cast<unsigned char *>(&serial),
  229. sizeof(serial));
  230. if (rv != SECSuccess) {
  231. LOG(LS_ERROR) << "Couldn't generate random serial";
  232. goto fail;
  233. }
  234. certificate = CERT_CreateCertificate(serial, subject_name, validity, certreq);
  235. if (!certificate) {
  236. LOG(LS_ERROR) << "Couldn't create certificate";
  237. goto fail;
  238. }
  239. arena = certificate->arena;
  240. rv = SECOID_SetAlgorithmID(arena, &certificate->signature,
  241. SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION, NULL);
  242. if (rv != SECSuccess)
  243. goto fail;
  244. // Set version to X509v3.
  245. *(certificate->version.data) = 2;
  246. certificate->version.len = 1;
  247. if (!SEC_ASN1EncodeItem(arena, &inner_der, certificate,
  248. SEC_ASN1_GET(CERT_CertificateTemplate)))
  249. goto fail;
  250. rv = SEC_DerSignData(arena, &signed_cert, inner_der.data, inner_der.len,
  251. keypair->privkey(),
  252. SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION);
  253. if (rv != SECSuccess) {
  254. LOG(LS_ERROR) << "Couldn't sign certificate";
  255. goto fail;
  256. }
  257. certificate->derCert = signed_cert;
  258. identity = new NSSIdentity(keypair, new NSSCertificate(certificate));
  259. goto done;
  260. fail:
  261. delete keypair;
  262. CERT_DestroyCertificate(certificate);
  263. done:
  264. if (subject_name) CERT_DestroyName(subject_name);
  265. if (spki) SECKEY_DestroySubjectPublicKeyInfo(spki);
  266. if (certreq) CERT_DestroyCertificateRequest(certreq);
  267. if (validity) CERT_DestroyValidity(validity);
  268. return identity;
  269. }
  270. NSSIdentity *NSSIdentity::GetReference() const {
  271. NSSKeyPair *keypair = keypair_->GetReference();
  272. if (!keypair)
  273. return NULL;
  274. NSSCertificate *certificate = certificate_->GetReference();
  275. if (!certificate) {
  276. delete keypair;
  277. return NULL;
  278. }
  279. return new NSSIdentity(keypair, certificate);
  280. }
  281. NSSCertificate &NSSIdentity::certificate() const {
  282. return *certificate_;
  283. }
  284. } // talk_base namespace
  285. #endif // HAVE_NSS_SSL_H