crypto.c 13 KB

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