crypto.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * Copyright (c) 2006-2012 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.com)
  7. * Christine Caulfield (ccaulfie@redhat.com)
  8. * Jan Friesse (jfriesse@redhat.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <config.h>
  37. #include <assert.h>
  38. #include <pthread.h>
  39. #include <sys/mman.h>
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #include <sys/socket.h>
  43. #include <netdb.h>
  44. #include <sys/un.h>
  45. #include <sys/ioctl.h>
  46. #include <sys/param.h>
  47. #include <netinet/in.h>
  48. #include <arpa/inet.h>
  49. #include <unistd.h>
  50. #include <fcntl.h>
  51. #include <stdlib.h>
  52. #include <stdio.h>
  53. #include <errno.h>
  54. #include <sched.h>
  55. #include <time.h>
  56. #include <sys/time.h>
  57. #include <sys/poll.h>
  58. #include <limits.h>
  59. #include <corosync/sq.h>
  60. #include <corosync/swab.h>
  61. #include <corosync/list.h>
  62. #include <qb/qbdefs.h>
  63. #include <qb/qbloop.h>
  64. #define LOGSYS_UTILS_ONLY 1
  65. #include <corosync/logsys.h>
  66. #include <corosync/totem/totem.h>
  67. #include "crypto.h"
  68. #include "util.h"
  69. #include <nss.h>
  70. #include <pk11pub.h>
  71. #include <pkcs11.h>
  72. #include <prerror.h>
  73. struct crypto_instance {
  74. PK11SymKey *nss_sym_key;
  75. PK11SymKey *nss_sym_key_sign;
  76. unsigned char private_key[1024];
  77. unsigned int private_key_len;
  78. void (*log_printf_func) (
  79. int level,
  80. int subsys,
  81. const char *function,
  82. const char *file,
  83. int line,
  84. const char *format,
  85. ...)__attribute__((format(printf, 6, 7)));
  86. int log_level_security;
  87. int log_level_notice;
  88. int log_level_error;
  89. int log_subsys_id;
  90. };
  91. #define log_printf(level, format, args...) \
  92. do { \
  93. instance->log_printf_func ( \
  94. level, instance->log_subsys_id, \
  95. __FUNCTION__, __FILE__, __LINE__, \
  96. (const char *)format, ##args); \
  97. } while (0);
  98. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  99. do { \
  100. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  101. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  102. instance->totemudp_log_printf ( \
  103. level, instance->log_subsys_id, \
  104. __FUNCTION__, __FILE__, __LINE__, \
  105. fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  106. } while(0)
  107. static unsigned char *copy_from_iovec(
  108. const struct iovec *iov,
  109. unsigned int iov_len,
  110. size_t *buf_size)
  111. {
  112. int i;
  113. size_t bufptr;
  114. size_t buflen = 0;
  115. unsigned char *newbuf;
  116. for (i=0; i<iov_len; i++)
  117. buflen += iov[i].iov_len;
  118. newbuf = malloc(buflen);
  119. if (!newbuf)
  120. return NULL;
  121. bufptr=0;
  122. for (i=0; i<iov_len; i++) {
  123. memcpy(newbuf+bufptr, iov[i].iov_base, iov[i].iov_len);
  124. bufptr += iov[i].iov_len;
  125. }
  126. *buf_size = buflen;
  127. return newbuf;
  128. }
  129. static void copy_to_iovec(
  130. struct iovec *iov,
  131. unsigned int iov_len,
  132. const unsigned char *buf,
  133. size_t buf_size)
  134. {
  135. int i;
  136. size_t copylen;
  137. size_t bufptr = 0;
  138. bufptr=0;
  139. for (i=0; i<iov_len; i++) {
  140. copylen = iov[i].iov_len;
  141. if (bufptr + copylen > buf_size) {
  142. copylen = buf_size - bufptr;
  143. }
  144. memcpy(iov[i].iov_base, buf+bufptr, copylen);
  145. bufptr += copylen;
  146. if (iov[i].iov_len != copylen) {
  147. iov[i].iov_len = copylen;
  148. return;
  149. }
  150. }
  151. }
  152. static void init_nss_crypto(struct crypto_instance *instance)
  153. {
  154. PK11SlotInfo* aes_slot = NULL;
  155. PK11SlotInfo* sha1_slot = NULL;
  156. SECItem key_item;
  157. SECStatus rv;
  158. log_printf(instance->log_level_notice,
  159. "Initializing transmit/receive security: NSS AES256CBC/SHA1HMAC (mode %u).", 0);
  160. rv = NSS_NoDB_Init(".");
  161. if (rv != SECSuccess)
  162. {
  163. log_printf(instance->log_level_security, "NSS initialization failed (err %d)",
  164. PR_GetError());
  165. goto out;
  166. }
  167. aes_slot = PK11_GetBestSlot(CKM_AES_CBC_PAD, NULL);
  168. if (aes_slot == NULL)
  169. {
  170. log_printf(instance->log_level_security, "Unable to find security slot (err %d)",
  171. PR_GetError());
  172. goto out;
  173. }
  174. sha1_slot = PK11_GetBestSlot(CKM_SHA_1_HMAC, NULL);
  175. if (sha1_slot == NULL)
  176. {
  177. log_printf(instance->log_level_security, "Unable to find security slot (err %d)",
  178. PR_GetError());
  179. goto out;
  180. }
  181. /*
  182. * Make the private key into a SymKey that we can use
  183. */
  184. key_item.type = siBuffer;
  185. key_item.data = instance->private_key;
  186. key_item.len = 32; /* Use 256 bits */
  187. instance->nss_sym_key = PK11_ImportSymKey(aes_slot,
  188. CKM_AES_CBC_PAD,
  189. PK11_OriginUnwrap, CKA_ENCRYPT|CKA_DECRYPT,
  190. &key_item, NULL);
  191. if (instance->nss_sym_key == NULL)
  192. {
  193. log_printf(instance->log_level_security, "Failure to import key into NSS (err %d)",
  194. PR_GetError());
  195. goto out;
  196. }
  197. instance->nss_sym_key_sign = PK11_ImportSymKey(sha1_slot,
  198. CKM_SHA_1_HMAC,
  199. PK11_OriginUnwrap, CKA_SIGN,
  200. &key_item, NULL);
  201. if (instance->nss_sym_key_sign == NULL) {
  202. log_printf(instance->log_level_security, "Failure to import key into NSS (err %d)",
  203. PR_GetError());
  204. goto out;
  205. }
  206. out:
  207. return;
  208. }
  209. static int encrypt_and_sign_nss (
  210. struct crypto_instance *instance,
  211. unsigned char *buf,
  212. size_t *buf_len,
  213. const struct iovec *iovec,
  214. unsigned int iov_len)
  215. {
  216. PK11Context* enc_context = NULL;
  217. SECStatus rv1, rv2;
  218. int tmp1_outlen;
  219. unsigned int tmp2_outlen;
  220. unsigned char *inbuf;
  221. unsigned char *data;
  222. unsigned char *outdata;
  223. size_t datalen;
  224. SECItem no_params;
  225. SECItem iv_item;
  226. struct crypto_security_header *header;
  227. SECItem *nss_sec_param;
  228. unsigned char nss_iv_data[16];
  229. SECStatus rv;
  230. no_params.type = siBuffer;
  231. no_params.data = 0;
  232. no_params.len = 0;
  233. tmp1_outlen = tmp2_outlen = 0;
  234. inbuf = copy_from_iovec(iovec, iov_len, &datalen);
  235. if (!inbuf) {
  236. log_printf(instance->log_level_security, "malloc error copying buffer from iovec");
  237. return -1;
  238. }
  239. data = inbuf + sizeof (struct crypto_security_header);
  240. datalen -= sizeof (struct crypto_security_header);
  241. outdata = buf + sizeof (struct crypto_security_header);
  242. header = (struct crypto_security_header *)buf;
  243. rv = PK11_GenerateRandom (
  244. nss_iv_data,
  245. sizeof (nss_iv_data));
  246. if (rv != SECSuccess) {
  247. log_printf(instance->log_level_security,
  248. "Failure to generate a random number %d",
  249. PR_GetError());
  250. }
  251. memcpy(header->salt, nss_iv_data, sizeof(nss_iv_data));
  252. iv_item.type = siBuffer;
  253. iv_item.data = nss_iv_data;
  254. iv_item.len = sizeof (nss_iv_data);
  255. nss_sec_param = PK11_ParamFromIV (
  256. CKM_AES_CBC_PAD,
  257. &iv_item);
  258. if (nss_sec_param == NULL) {
  259. log_printf(instance->log_level_security,
  260. "Failure to set up PKCS11 param (err %d)",
  261. PR_GetError());
  262. free (inbuf);
  263. return (-1);
  264. }
  265. /*
  266. * Create cipher context for encryption
  267. */
  268. enc_context = PK11_CreateContextBySymKey (
  269. CKM_AES_CBC_PAD,
  270. CKA_ENCRYPT,
  271. instance->nss_sym_key,
  272. nss_sec_param);
  273. if (!enc_context) {
  274. char err[1024];
  275. PR_GetErrorText(err);
  276. err[PR_GetErrorTextLength()] = 0;
  277. log_printf(instance->log_level_security,
  278. "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d): %s",
  279. CKM_AES_CBC_PAD,
  280. PR_GetError(), err);
  281. free(inbuf);
  282. return -1;
  283. }
  284. rv1 = PK11_CipherOp(enc_context, outdata,
  285. &tmp1_outlen, FRAME_SIZE_MAX - sizeof(struct crypto_security_header),
  286. data, datalen);
  287. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  288. FRAME_SIZE_MAX - tmp1_outlen);
  289. PK11_DestroyContext(enc_context, PR_TRUE);
  290. *buf_len = tmp1_outlen + tmp2_outlen;
  291. free(inbuf);
  292. // memcpy(&outdata[*buf_len], nss_iv_data, sizeof(nss_iv_data));
  293. if (rv1 != SECSuccess || rv2 != SECSuccess)
  294. goto out;
  295. /* Now do the digest */
  296. enc_context = PK11_CreateContextBySymKey(CKM_SHA_1_HMAC,
  297. CKA_SIGN, instance->nss_sym_key_sign, &no_params);
  298. if (!enc_context) {
  299. char err[1024];
  300. PR_GetErrorText(err);
  301. err[PR_GetErrorTextLength()] = 0;
  302. log_printf(instance->log_level_security, "encrypt: PK11_CreateContext failed (digest) err %d: %s",
  303. PR_GetError(), err);
  304. return -1;
  305. }
  306. PK11_DigestBegin(enc_context);
  307. rv1 = PK11_DigestOp(enc_context, outdata - 16, *buf_len + 16);
  308. rv2 = PK11_DigestFinal(enc_context, header->hash_digest, &tmp2_outlen, sizeof(header->hash_digest));
  309. PK11_DestroyContext(enc_context, PR_TRUE);
  310. if (rv1 != SECSuccess || rv2 != SECSuccess)
  311. goto out;
  312. *buf_len = *buf_len + sizeof(struct crypto_security_header);
  313. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  314. return 0;
  315. out:
  316. return -1;
  317. }
  318. static int authenticate_and_decrypt_nss (
  319. struct crypto_instance *instance,
  320. struct iovec *iov,
  321. unsigned int iov_len)
  322. {
  323. PK11Context* enc_context = NULL;
  324. SECStatus rv1, rv2;
  325. int tmp1_outlen;
  326. unsigned int tmp2_outlen;
  327. unsigned char outbuf[FRAME_SIZE_MAX];
  328. unsigned char digest[CRYPTO_HMAC_HASH_SIZE];
  329. unsigned char *outdata;
  330. int result_len;
  331. unsigned char *data;
  332. unsigned char *inbuf;
  333. size_t datalen;
  334. struct crypto_security_header *header = (struct crypto_security_header *)iov[0].iov_base;
  335. SECItem no_params;
  336. SECItem ivdata;
  337. no_params.type = siBuffer;
  338. no_params.data = 0;
  339. no_params.len = 0;
  340. tmp1_outlen = tmp2_outlen = 0;
  341. if (iov_len > 1) {
  342. inbuf = copy_from_iovec(iov, iov_len, &datalen);
  343. if (!inbuf) {
  344. log_printf(instance->log_level_security, "malloc error copying buffer from iovec");
  345. return -1;
  346. }
  347. }
  348. else {
  349. inbuf = (unsigned char *)iov[0].iov_base;
  350. datalen = iov[0].iov_len;
  351. }
  352. data = inbuf + sizeof (struct crypto_security_header) - 16;
  353. datalen = datalen - sizeof (struct crypto_security_header) + 16;
  354. outdata = outbuf + sizeof (struct crypto_security_header);
  355. /* Check the digest */
  356. enc_context = PK11_CreateContextBySymKey (
  357. CKM_SHA_1_HMAC, CKA_SIGN,
  358. instance->nss_sym_key_sign,
  359. &no_params);
  360. if (!enc_context) {
  361. char err[1024];
  362. PR_GetErrorText(err);
  363. err[PR_GetErrorTextLength()] = 0;
  364. log_printf(instance->log_level_security, "PK11_CreateContext failed (check digest) err %d: %s",
  365. PR_GetError(), err);
  366. free (inbuf);
  367. return -1;
  368. }
  369. PK11_DigestBegin(enc_context);
  370. rv1 = PK11_DigestOp(enc_context, data, datalen);
  371. rv2 = PK11_DigestFinal(enc_context, digest, &tmp2_outlen, sizeof(digest));
  372. PK11_DestroyContext(enc_context, PR_TRUE);
  373. if (rv1 != SECSuccess || rv2 != SECSuccess) {
  374. log_printf(instance->log_level_security, "Digest check failed");
  375. return -1;
  376. }
  377. if (memcmp(digest, header->hash_digest, tmp2_outlen) != 0) {
  378. log_printf(instance->log_level_error, "Digest does not match");
  379. return -1;
  380. }
  381. /*
  382. * Get rid of salt
  383. */
  384. data += 16;
  385. datalen -= 16;
  386. /* Create cipher context for decryption */
  387. ivdata.type = siBuffer;
  388. ivdata.data = header->salt;
  389. ivdata.len = sizeof(header->salt);
  390. enc_context = PK11_CreateContextBySymKey(
  391. CKM_AES_CBC_PAD,
  392. CKA_DECRYPT,
  393. instance->nss_sym_key, &ivdata);
  394. if (!enc_context) {
  395. log_printf(instance->log_level_security,
  396. "PK11_CreateContext (decrypt) failed (err %d)",
  397. PR_GetError());
  398. return -1;
  399. }
  400. rv1 = PK11_CipherOp(enc_context, outdata, &tmp1_outlen,
  401. sizeof(outbuf) - sizeof (struct crypto_security_header),
  402. data, datalen);
  403. if (rv1 != SECSuccess) {
  404. log_printf(instance->log_level_security,
  405. "PK11_CipherOp (decrypt) failed (err %d)",
  406. PR_GetError());
  407. }
  408. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  409. sizeof(outbuf) - tmp1_outlen);
  410. PK11_DestroyContext(enc_context, PR_TRUE);
  411. result_len = tmp1_outlen + tmp2_outlen + sizeof (struct crypto_security_header);
  412. /* Copy it back to the buffer */
  413. copy_to_iovec(iov, iov_len, outbuf, result_len);
  414. if (iov_len > 1)
  415. free(inbuf);
  416. if (rv1 != SECSuccess || rv2 != SECSuccess)
  417. return -1;
  418. return 0;
  419. }
  420. int crypto_encrypt_and_sign (
  421. struct crypto_instance *instance,
  422. unsigned char *buf,
  423. size_t *buf_len,
  424. const struct iovec *iovec,
  425. unsigned int iov_len)
  426. {
  427. return (encrypt_and_sign_nss(instance, buf, buf_len, iovec, iov_len));
  428. }
  429. int crypto_authenticate_and_decrypt (struct crypto_instance *instance,
  430. struct iovec *iov,
  431. unsigned int iov_len)
  432. {
  433. unsigned char type;
  434. unsigned char *endbuf = (unsigned char *)iov[iov_len-1].iov_base;
  435. /*
  436. * Get the encryption type and remove it from the buffer
  437. */
  438. type = endbuf[iov[iov_len-1].iov_len-1];
  439. iov[iov_len-1].iov_len -= 1;
  440. return (authenticate_and_decrypt_nss(instance, iov, iov_len));
  441. }
  442. struct crypto_instance *crypto_init(
  443. const unsigned char *private_key,
  444. unsigned int private_key_len,
  445. void (*log_printf_func) (
  446. int level,
  447. int subsys,
  448. const char *function,
  449. const char *file,
  450. int line,
  451. const char *format,
  452. ...)__attribute__((format(printf, 6, 7))),
  453. int log_level_security,
  454. int log_level_notice,
  455. int log_level_error,
  456. int log_subsys_id)
  457. {
  458. struct crypto_instance *instance;
  459. instance = malloc(sizeof(*instance));
  460. if (instance == NULL) {
  461. return (NULL);
  462. }
  463. memset(instance, 0, sizeof(struct crypto_instance));
  464. memcpy(instance->private_key, private_key, private_key_len);
  465. instance->private_key_len = private_key_len;
  466. instance->log_printf_func = log_printf_func;
  467. instance->log_level_security = log_level_security;
  468. instance->log_level_notice = log_level_notice;
  469. instance->log_level_error = log_level_error;
  470. instance->log_subsys_id = log_subsys_id;
  471. init_nss_crypto(instance);
  472. return (instance);
  473. }