totemcrypto.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 "totemcrypto.h"
  68. #include "util.h"
  69. #include <nss.h>
  70. #include <pk11pub.h>
  71. #include <pkcs11.h>
  72. #include <prerror.h>
  73. #define CRYPTO_HMAC_HASH_SIZE 20
  74. struct crypto_security_header {
  75. unsigned char hash_digest[CRYPTO_HMAC_HASH_SIZE]; /* The hash *MUST* be first in the data structure */
  76. unsigned char salt[16]; /* random number */
  77. char msg[0];
  78. } __attribute__((packed));
  79. struct crypto_instance {
  80. PK11SymKey *nss_sym_key;
  81. PK11SymKey *nss_sym_key_sign;
  82. unsigned char private_key[1024];
  83. unsigned int private_key_len;
  84. enum crypto_crypt_t crypto_cipher_type;
  85. enum crypto_hash_t crypto_hash_type;
  86. void (*log_printf_func) (
  87. int level,
  88. int subsys,
  89. const char *function,
  90. const char *file,
  91. int line,
  92. const char *format,
  93. ...)__attribute__((format(printf, 6, 7)));
  94. int log_level_security;
  95. int log_level_notice;
  96. int log_level_error;
  97. int log_subsys_id;
  98. };
  99. CK_MECHANISM_TYPE cipher_to_nss[] = {
  100. 0, /* CRYPTO_CIPHER_TYPE_NONE */
  101. CKM_AES_CBC_PAD /* CRYPTO_CIPHER_TYPE_AES256 */
  102. };
  103. size_t cipher_key_len[] = {
  104. 0, /* CRYPTO_CIPHER_TYPE_NONE */
  105. 32, /* CRYPTO_CIPHER_TYPE_AES256 */
  106. };
  107. CK_MECHANISM_TYPE hash_to_nss[] = {
  108. 0, /* CRYPTO_HASH_TYPE_NONE */
  109. CKM_SHA_1_HMAC /* CRYPTO_HASH_TYPE_SHA1 */
  110. };
  111. #define log_printf(level, format, args...) \
  112. do { \
  113. instance->log_printf_func ( \
  114. level, instance->log_subsys_id, \
  115. __FUNCTION__, __FILE__, __LINE__, \
  116. (const char *)format, ##args); \
  117. } while (0);
  118. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  119. do { \
  120. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  121. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  122. instance->totemudp_log_printf ( \
  123. level, instance->log_subsys_id, \
  124. __FUNCTION__, __FILE__, __LINE__, \
  125. fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  126. } while(0)
  127. static void init_nss_crypto(struct crypto_instance *instance)
  128. {
  129. PK11SlotInfo* aes_slot = NULL;
  130. PK11SlotInfo* sha1_slot = NULL;
  131. SECItem key_item;
  132. SECStatus rv;
  133. log_printf(instance->log_level_notice,
  134. "Initializing transmit/receive security: NSS AES256CBC/SHA1HMAC (mode %u).", 0);
  135. rv = NSS_NoDB_Init(".");
  136. if (rv != SECSuccess)
  137. {
  138. log_printf(instance->log_level_security, "NSS initialization failed (err %d)",
  139. PR_GetError());
  140. goto out;
  141. }
  142. /*
  143. * TODO: use instance info!
  144. */
  145. aes_slot = PK11_GetBestSlot(cipher_to_nss[instance->crypto_cipher_type], NULL);
  146. if (aes_slot == NULL)
  147. {
  148. log_printf(instance->log_level_security, "Unable to find security slot (err %d)",
  149. PR_GetError());
  150. goto out;
  151. }
  152. sha1_slot = PK11_GetBestSlot(hash_to_nss[instance->crypto_hash_type], NULL);
  153. if (sha1_slot == NULL)
  154. {
  155. log_printf(instance->log_level_security, "Unable to find security slot (err %d)",
  156. PR_GetError());
  157. goto out;
  158. }
  159. /*
  160. * Make the private key into a SymKey that we can use
  161. */
  162. key_item.type = siBuffer;
  163. key_item.data = instance->private_key;
  164. key_item.len = cipher_key_len[instance->crypto_cipher_type];
  165. instance->nss_sym_key = PK11_ImportSymKey(aes_slot,
  166. cipher_to_nss[instance->crypto_cipher_type],
  167. PK11_OriginUnwrap, CKA_ENCRYPT|CKA_DECRYPT,
  168. &key_item, NULL);
  169. if (instance->nss_sym_key == NULL)
  170. {
  171. log_printf(instance->log_level_security, "Failure to import key into NSS (err %d)",
  172. PR_GetError());
  173. goto out;
  174. }
  175. instance->nss_sym_key_sign = PK11_ImportSymKey(sha1_slot,
  176. hash_to_nss[instance->crypto_hash_type],
  177. PK11_OriginUnwrap, CKA_SIGN,
  178. &key_item, NULL);
  179. if (instance->nss_sym_key_sign == NULL) {
  180. log_printf(instance->log_level_security, "Failure to import key into NSS (err %d)",
  181. PR_GetError());
  182. goto out;
  183. }
  184. out:
  185. return;
  186. }
  187. static int encrypt_and_sign_nss (
  188. struct crypto_instance *instance,
  189. const unsigned char *buf_in,
  190. const size_t buf_in_len,
  191. unsigned char *buf_out,
  192. size_t *buf_out_len)
  193. {
  194. PK11Context* enc_context = NULL;
  195. SECStatus rv1, rv2;
  196. int tmp1_outlen;
  197. unsigned int tmp2_outlen;
  198. unsigned char *outdata;
  199. SECItem no_params;
  200. SECItem iv_item;
  201. struct crypto_security_header *header;
  202. SECItem *nss_sec_param;
  203. unsigned char nss_iv_data[16];
  204. SECStatus rv;
  205. no_params.type = siBuffer;
  206. no_params.data = 0;
  207. no_params.len = 0;
  208. tmp1_outlen = tmp2_outlen = 0;
  209. outdata = buf_out + sizeof (struct crypto_security_header);
  210. header = (struct crypto_security_header *)buf_out;
  211. rv = PK11_GenerateRandom (
  212. nss_iv_data,
  213. sizeof (nss_iv_data));
  214. if (rv != SECSuccess) {
  215. log_printf(instance->log_level_security,
  216. "Failure to generate a random number %d",
  217. PR_GetError());
  218. }
  219. memcpy(header->salt, nss_iv_data, sizeof(nss_iv_data));
  220. iv_item.type = siBuffer;
  221. iv_item.data = nss_iv_data;
  222. iv_item.len = sizeof (nss_iv_data);
  223. nss_sec_param = PK11_ParamFromIV (
  224. cipher_to_nss[instance->crypto_cipher_type],
  225. &iv_item);
  226. if (nss_sec_param == NULL) {
  227. log_printf(instance->log_level_security,
  228. "Failure to set up PKCS11 param (err %d)",
  229. PR_GetError());
  230. return (-1);
  231. }
  232. /*
  233. * Create cipher context for encryption
  234. */
  235. enc_context = PK11_CreateContextBySymKey (
  236. cipher_to_nss[instance->crypto_cipher_type],
  237. CKA_ENCRYPT,
  238. instance->nss_sym_key,
  239. nss_sec_param);
  240. if (!enc_context) {
  241. char err[1024];
  242. PR_GetErrorText(err);
  243. err[PR_GetErrorTextLength()] = 0;
  244. log_printf(instance->log_level_security,
  245. "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d): %s",
  246. (int)cipher_to_nss[instance->crypto_cipher_type],
  247. PR_GetError(), err);
  248. return -1;
  249. }
  250. rv1 = PK11_CipherOp(enc_context, outdata,
  251. &tmp1_outlen, FRAME_SIZE_MAX - sizeof(struct crypto_security_header),
  252. (unsigned char *)buf_in, buf_in_len);
  253. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  254. FRAME_SIZE_MAX - tmp1_outlen);
  255. PK11_DestroyContext(enc_context, PR_TRUE);
  256. *buf_out_len = tmp1_outlen + tmp2_outlen;
  257. if (rv1 != SECSuccess || rv2 != SECSuccess)
  258. goto out;
  259. /* Now do the digest */
  260. enc_context = PK11_CreateContextBySymKey(hash_to_nss[instance->crypto_hash_type],
  261. CKA_SIGN, instance->nss_sym_key_sign, &no_params);
  262. if (!enc_context) {
  263. char err[1024];
  264. PR_GetErrorText(err);
  265. err[PR_GetErrorTextLength()] = 0;
  266. log_printf(instance->log_level_security, "encrypt: PK11_CreateContext failed (digest) err %d: %s",
  267. PR_GetError(), err);
  268. return -1;
  269. }
  270. PK11_DigestBegin(enc_context);
  271. rv1 = PK11_DigestOp(enc_context, outdata - 16, *buf_out_len + 16);
  272. rv2 = PK11_DigestFinal(enc_context, header->hash_digest, &tmp2_outlen, sizeof(header->hash_digest));
  273. PK11_DestroyContext(enc_context, PR_TRUE);
  274. if (rv1 != SECSuccess || rv2 != SECSuccess)
  275. goto out;
  276. *buf_out_len = *buf_out_len + sizeof(struct crypto_security_header);
  277. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  278. return 0;
  279. out:
  280. return -1;
  281. }
  282. static int authenticate_and_decrypt_nss (
  283. struct crypto_instance *instance,
  284. unsigned char *buf,
  285. int *buf_len)
  286. {
  287. PK11Context* enc_context = NULL;
  288. SECStatus rv1, rv2;
  289. int tmp1_outlen;
  290. unsigned int tmp2_outlen;
  291. unsigned char outbuf[FRAME_SIZE_MAX];
  292. unsigned char digest[CRYPTO_HMAC_HASH_SIZE];
  293. unsigned char *outdata;
  294. int result_len;
  295. unsigned char *data;
  296. unsigned char *inbuf;
  297. size_t datalen;
  298. struct crypto_security_header *header = (struct crypto_security_header *)buf;
  299. SECItem no_params;
  300. SECItem ivdata;
  301. no_params.type = siBuffer;
  302. no_params.data = 0;
  303. no_params.len = 0;
  304. tmp1_outlen = tmp2_outlen = 0;
  305. inbuf = (unsigned char *)buf;
  306. datalen = *buf_len;
  307. data = inbuf + sizeof (struct crypto_security_header) - 16;
  308. datalen = datalen - sizeof (struct crypto_security_header) + 16;
  309. outdata = outbuf + sizeof (struct crypto_security_header);
  310. /* Check the digest */
  311. enc_context = PK11_CreateContextBySymKey (
  312. hash_to_nss[instance->crypto_hash_type], CKA_SIGN,
  313. instance->nss_sym_key_sign,
  314. &no_params);
  315. if (!enc_context) {
  316. char err[1024];
  317. PR_GetErrorText(err);
  318. err[PR_GetErrorTextLength()] = 0;
  319. log_printf(instance->log_level_security, "PK11_CreateContext failed (check digest) err %d: %s",
  320. PR_GetError(), err);
  321. return -1;
  322. }
  323. PK11_DigestBegin(enc_context);
  324. rv1 = PK11_DigestOp(enc_context, data, datalen);
  325. rv2 = PK11_DigestFinal(enc_context, digest, &tmp2_outlen, sizeof(digest));
  326. PK11_DestroyContext(enc_context, PR_TRUE);
  327. if (rv1 != SECSuccess || rv2 != SECSuccess) {
  328. log_printf(instance->log_level_security, "Digest check failed");
  329. return -1;
  330. }
  331. if (memcmp(digest, header->hash_digest, tmp2_outlen) != 0) {
  332. log_printf(instance->log_level_error, "Digest does not match");
  333. return -1;
  334. }
  335. /*
  336. * Get rid of salt
  337. */
  338. data += 16;
  339. datalen -= 16;
  340. /* Create cipher context for decryption */
  341. ivdata.type = siBuffer;
  342. ivdata.data = header->salt;
  343. ivdata.len = sizeof(header->salt);
  344. enc_context = PK11_CreateContextBySymKey(
  345. cipher_to_nss[instance->crypto_cipher_type],
  346. CKA_DECRYPT,
  347. instance->nss_sym_key, &ivdata);
  348. if (!enc_context) {
  349. log_printf(instance->log_level_security,
  350. "PK11_CreateContext (decrypt) failed (err %d)",
  351. PR_GetError());
  352. return -1;
  353. }
  354. rv1 = PK11_CipherOp(enc_context, outdata, &tmp1_outlen,
  355. sizeof(outbuf) - sizeof (struct crypto_security_header),
  356. data, datalen);
  357. if (rv1 != SECSuccess) {
  358. log_printf(instance->log_level_security,
  359. "PK11_CipherOp (decrypt) failed (err %d)",
  360. PR_GetError());
  361. }
  362. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  363. sizeof(outbuf) - tmp1_outlen);
  364. PK11_DestroyContext(enc_context, PR_TRUE);
  365. result_len = tmp1_outlen + tmp2_outlen + sizeof (struct crypto_security_header);
  366. memset(buf, 0, *buf_len);
  367. memcpy(buf, outdata, result_len);
  368. *buf_len = result_len;
  369. if (rv1 != SECSuccess || rv2 != SECSuccess)
  370. return -1;
  371. return 0;
  372. }
  373. size_t crypto_sec_header_size(const char *crypto_hash_type)
  374. {
  375. /*
  376. * TODO: add switch / size mapping
  377. */
  378. return sizeof(struct crypto_security_header);
  379. }
  380. int crypto_encrypt_and_sign (
  381. struct crypto_instance *instance,
  382. const unsigned char *buf_in,
  383. const size_t buf_in_len,
  384. unsigned char *buf_out,
  385. size_t *buf_out_len)
  386. {
  387. return (encrypt_and_sign_nss(instance, buf_in, buf_in_len, buf_out, buf_out_len));
  388. }
  389. int crypto_authenticate_and_decrypt (struct crypto_instance *instance,
  390. unsigned char *buf,
  391. int *buf_len)
  392. {
  393. return (authenticate_and_decrypt_nss(instance, buf, buf_len));
  394. }
  395. struct crypto_instance *crypto_init(
  396. const unsigned char *private_key,
  397. unsigned int private_key_len,
  398. const char *crypto_cipher_type,
  399. const char *crypto_hash_type,
  400. void (*log_printf_func) (
  401. int level,
  402. int subsys,
  403. const char *function,
  404. const char *file,
  405. int line,
  406. const char *format,
  407. ...)__attribute__((format(printf, 6, 7))),
  408. int log_level_security,
  409. int log_level_notice,
  410. int log_level_error,
  411. int log_subsys_id)
  412. {
  413. struct crypto_instance *instance;
  414. instance = malloc(sizeof(*instance));
  415. if (instance == NULL) {
  416. return (NULL);
  417. }
  418. memset(instance, 0, sizeof(struct crypto_instance));
  419. memcpy(instance->private_key, private_key, private_key_len);
  420. instance->private_key_len = private_key_len;
  421. if (strcmp(crypto_cipher_type, "none") == 0) {
  422. instance->crypto_cipher_type = CRYPTO_CIPHER_TYPE_NONE;
  423. } else if (strcmp(crypto_cipher_type, "aes256") == 0) {
  424. instance->crypto_cipher_type = CRYPTO_CIPHER_TYPE_AES256;
  425. }
  426. if (strcmp(crypto_hash_type, "none") == 0) {
  427. instance->crypto_hash_type = CRYPTO_HASH_TYPE_NONE;
  428. } else if (strcmp(crypto_hash_type, "sha1") == 0) {
  429. instance->crypto_hash_type = CRYPTO_HASH_TYPE_SHA1;
  430. }
  431. instance->log_printf_func = log_printf_func;
  432. instance->log_level_security = log_level_security;
  433. instance->log_level_notice = log_level_notice;
  434. instance->log_level_error = log_level_error;
  435. instance->log_subsys_id = log_subsys_id;
  436. init_nss_crypto(instance);
  437. return (instance);
  438. }