totemcrypto.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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. #include <blapit.h>
  74. #include <hasht.h>
  75. #define SALT_SIZE 16
  76. struct crypto_config_header {
  77. uint8_t crypto_cipher_type;
  78. uint8_t crypto_hash_type;
  79. uint8_t __pad0;
  80. uint8_t __pad1;
  81. } __attribute__((packed));
  82. enum crypto_crypt_t {
  83. CRYPTO_CIPHER_TYPE_NONE = 0,
  84. CRYPTO_CIPHER_TYPE_AES256 = 1
  85. };
  86. CK_MECHANISM_TYPE cipher_to_nss[] = {
  87. 0, /* CRYPTO_CIPHER_TYPE_NONE */
  88. CKM_AES_CBC_PAD /* CRYPTO_CIPHER_TYPE_AES256 */
  89. };
  90. size_t cipher_key_len[] = {
  91. 0, /* CRYPTO_CIPHER_TYPE_NONE */
  92. 32, /* CRYPTO_CIPHER_TYPE_AES256 */
  93. };
  94. size_t cypher_block_len[] = {
  95. 0, /* CRYPTO_CIPHER_TYPE_NONE */
  96. AES_BLOCK_SIZE /* CRYPTO_CIPHER_TYPE_AES256 */
  97. };
  98. enum crypto_hash_t {
  99. CRYPTO_HASH_TYPE_NONE = 0,
  100. CRYPTO_HASH_TYPE_MD5 = 1,
  101. CRYPTO_HASH_TYPE_SHA1 = 2,
  102. CRYPTO_HASH_TYPE_SHA256 = 3,
  103. CRYPTO_HASH_TYPE_SHA384 = 4,
  104. CRYPTO_HASH_TYPE_SHA512 = 5
  105. };
  106. CK_MECHANISM_TYPE hash_to_nss[] = {
  107. 0, /* CRYPTO_HASH_TYPE_NONE */
  108. CKM_MD5_HMAC, /* CRYPTO_HASH_TYPE_MD5 */
  109. CKM_SHA_1_HMAC, /* CRYPTO_HASH_TYPE_SHA1 */
  110. CKM_SHA256_HMAC, /* CRYPTO_HASH_TYPE_SHA256 */
  111. CKM_SHA384_HMAC, /* CRYPTO_HASH_TYPE_SHA384 */
  112. CKM_SHA512_HMAC /* CRYPTO_HASH_TYPE_SHA512 */
  113. };
  114. size_t hash_len[] = {
  115. 0, /* CRYPTO_HASH_TYPE_NONE */
  116. MD5_LENGTH, /* CRYPTO_HASH_TYPE_MD5 */
  117. SHA1_LENGTH, /* CRYPTO_HASH_TYPE_SHA1 */
  118. SHA256_LENGTH, /* CRYPTO_HASH_TYPE_SHA256 */
  119. SHA384_LENGTH, /* CRYPTO_HASH_TYPE_SHA384 */
  120. SHA512_LENGTH /* CRYPTO_HASH_TYPE_SHA512 */
  121. };
  122. size_t hash_block_len[] = {
  123. 0, /* CRYPTO_HASH_TYPE_NONE */
  124. MD5_BLOCK_LENGTH, /* CRYPTO_HASH_TYPE_MD5 */
  125. SHA1_BLOCK_LENGTH, /* CRYPTO_HASH_TYPE_SHA1 */
  126. SHA256_BLOCK_LENGTH, /* CRYPTO_HASH_TYPE_SHA256 */
  127. SHA384_BLOCK_LENGTH, /* CRYPTO_HASH_TYPE_SHA384 */
  128. SHA512_BLOCK_LENGTH /* CRYPTO_HASH_TYPE_SHA512 */
  129. };
  130. struct crypto_instance {
  131. PK11SymKey *nss_sym_key;
  132. PK11SymKey *nss_sym_key_sign;
  133. unsigned char private_key[1024];
  134. unsigned int private_key_len;
  135. enum crypto_crypt_t crypto_cipher_type;
  136. enum crypto_hash_t crypto_hash_type;
  137. void (*log_printf_func) (
  138. int level,
  139. int subsys,
  140. const char *function,
  141. const char *file,
  142. int line,
  143. const char *format,
  144. ...)__attribute__((format(printf, 6, 7)));
  145. int log_level_security;
  146. int log_level_notice;
  147. int log_level_error;
  148. int log_subsys_id;
  149. };
  150. #define log_printf(level, format, args...) \
  151. do { \
  152. instance->log_printf_func ( \
  153. level, instance->log_subsys_id, \
  154. __FUNCTION__, __FILE__, __LINE__, \
  155. (const char *)format, ##args); \
  156. } while (0);
  157. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  158. do { \
  159. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  160. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  161. instance->totemudp_log_printf ( \
  162. level, instance->log_subsys_id, \
  163. __FUNCTION__, __FILE__, __LINE__, \
  164. fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  165. } while(0)
  166. static int init_nss_crypto(struct crypto_instance *instance,
  167. const char *crypto_cipher_type,
  168. const char *crypto_hash_type)
  169. {
  170. PK11SlotInfo* crypt_slot = NULL;
  171. PK11SlotInfo* hash_slot = NULL;
  172. SECItem crypt_param;
  173. SECItem hash_param;
  174. if ((!cipher_to_nss[instance->crypto_cipher_type]) &&
  175. (!hash_to_nss[instance->crypto_hash_type])) {
  176. log_printf(instance->log_level_notice,
  177. "Initializing transmit/receive security: NONE");
  178. return 0;
  179. }
  180. log_printf(instance->log_level_notice,
  181. "Initializing transmit/receive security: NSS crypto: %s hash: %s",
  182. crypto_cipher_type, crypto_hash_type);
  183. if (NSS_NoDB_Init(".") != SECSuccess) {
  184. log_printf(instance->log_level_security, "NSS initialization failed (err %d)",
  185. PR_GetError());
  186. goto out;
  187. }
  188. if (cipher_to_nss[instance->crypto_cipher_type]) {
  189. crypt_param.type = siBuffer;
  190. crypt_param.data = instance->private_key;
  191. crypt_param.len = cipher_key_len[instance->crypto_cipher_type];
  192. crypt_slot = PK11_GetBestSlot(cipher_to_nss[instance->crypto_cipher_type], NULL);
  193. if (crypt_slot == NULL) {
  194. log_printf(instance->log_level_security, "Unable to find security slot (err %d)",
  195. PR_GetError());
  196. goto out;
  197. }
  198. instance->nss_sym_key = PK11_ImportSymKey(crypt_slot,
  199. cipher_to_nss[instance->crypto_cipher_type],
  200. PK11_OriginUnwrap, CKA_ENCRYPT|CKA_DECRYPT,
  201. &crypt_param, NULL);
  202. if (instance->nss_sym_key == NULL) {
  203. log_printf(instance->log_level_security, "Failure to import key into NSS (err %d)",
  204. PR_GetError());
  205. goto out;
  206. }
  207. }
  208. if (hash_to_nss[instance->crypto_hash_type]) {
  209. hash_param.type = siBuffer;
  210. hash_param.data = 0;
  211. hash_param.len = 0;
  212. hash_slot = PK11_GetBestSlot(hash_to_nss[instance->crypto_hash_type], NULL);
  213. if (hash_slot == NULL) {
  214. log_printf(instance->log_level_security, "Unable to find security slot (err %d)",
  215. PR_GetError());
  216. goto out;
  217. }
  218. instance->nss_sym_key_sign = PK11_ImportSymKey(hash_slot,
  219. hash_to_nss[instance->crypto_hash_type],
  220. PK11_OriginUnwrap, CKA_SIGN,
  221. &hash_param, NULL);
  222. if (instance->nss_sym_key_sign == NULL) {
  223. log_printf(instance->log_level_security, "Failure to import key into NSS (err %d)",
  224. PR_GetError());
  225. goto out;
  226. }
  227. }
  228. return 0;
  229. out:
  230. return -1;
  231. }
  232. static int encrypt_and_sign_nss (
  233. struct crypto_instance *instance,
  234. const unsigned char *buf_in,
  235. const size_t buf_in_len,
  236. unsigned char *buf_out,
  237. size_t *buf_out_len)
  238. {
  239. PK11Context* enc_context = NULL;
  240. SECItem crypt_param;
  241. SECItem hash_param;
  242. SECItem *nss_sec_param = NULL;
  243. unsigned char *outdata;
  244. int tmp1_outlen = 0;
  245. unsigned int tmp2_outlen = 0;
  246. unsigned char salt[SALT_SIZE];
  247. unsigned char hash_block[hash_block_len[instance->crypto_hash_type]];
  248. outdata = buf_out + hash_len[instance->crypto_hash_type];
  249. if (!cipher_to_nss[instance->crypto_cipher_type]) {
  250. memcpy(outdata, buf_in, buf_in_len);
  251. *buf_out_len = buf_in_len;
  252. goto only_hash;
  253. }
  254. if (PK11_GenerateRandom (salt, SALT_SIZE) != SECSuccess) {
  255. log_printf(instance->log_level_security,
  256. "Failure to generate a random number %d",
  257. PR_GetError());
  258. goto out;
  259. }
  260. memcpy(outdata, salt, SALT_SIZE);
  261. crypt_param.type = siBuffer;
  262. crypt_param.data = salt;
  263. crypt_param.len = SALT_SIZE;
  264. nss_sec_param = PK11_ParamFromIV (cipher_to_nss[instance->crypto_cipher_type],
  265. &crypt_param);
  266. if (nss_sec_param == NULL) {
  267. log_printf(instance->log_level_security,
  268. "Failure to set up PKCS11 param (err %d)",
  269. PR_GetError());
  270. goto out;
  271. }
  272. /*
  273. * Create cipher context for encryption
  274. */
  275. enc_context = PK11_CreateContextBySymKey (cipher_to_nss[instance->crypto_cipher_type],
  276. CKA_ENCRYPT,
  277. instance->nss_sym_key,
  278. nss_sec_param);
  279. if (!enc_context) {
  280. log_printf(instance->log_level_security,
  281. "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d)",
  282. (int)cipher_to_nss[instance->crypto_cipher_type],
  283. PR_GetError());
  284. goto out;
  285. }
  286. if (PK11_CipherOp(enc_context, outdata + SALT_SIZE,
  287. &tmp1_outlen,
  288. FRAME_SIZE_MAX - (sizeof(struct crypto_config_header) + hash_len[instance->crypto_hash_type] + SALT_SIZE),
  289. (unsigned char *)buf_in, buf_in_len) != SECSuccess) {
  290. log_printf(instance->log_level_security,
  291. "PK11_CipherOp failed (encrypt) crypt_type=%d (err %d)",
  292. (int)cipher_to_nss[instance->crypto_cipher_type],
  293. PR_GetError());
  294. goto out;
  295. }
  296. if (PK11_DigestFinal(enc_context, outdata + SALT_SIZE + tmp1_outlen,
  297. &tmp2_outlen, FRAME_SIZE_MAX - tmp1_outlen) != SECSuccess) {
  298. log_printf(instance->log_level_security,
  299. "PK11_DigestFinal failed (encrypt) crypt_type=%d (err %d)",
  300. (int)cipher_to_nss[instance->crypto_cipher_type],
  301. PR_GetError());
  302. goto out;
  303. }
  304. if (enc_context) {
  305. PK11_DestroyContext(enc_context, PR_TRUE);
  306. enc_context = 0;
  307. }
  308. *buf_out_len = tmp1_outlen + tmp2_outlen + SALT_SIZE;
  309. only_hash:
  310. if (!hash_to_nss[instance->crypto_hash_type]) {
  311. goto no_hash;
  312. }
  313. /* Now do the digest */
  314. hash_param.type = siBuffer;
  315. hash_param.data = 0;
  316. hash_param.len = 0;
  317. enc_context = PK11_CreateContextBySymKey(hash_to_nss[instance->crypto_hash_type],
  318. CKA_SIGN,
  319. instance->nss_sym_key_sign,
  320. &hash_param);
  321. if (!enc_context) {
  322. log_printf(instance->log_level_security,
  323. "PK11_CreateContext failed (hash) hash_type=%d (err %d)",
  324. (int)hash_to_nss[instance->crypto_hash_type],
  325. PR_GetError());
  326. goto out;
  327. }
  328. if (PK11_DigestBegin(enc_context) != SECSuccess) {
  329. log_printf(instance->log_level_security,
  330. "PK11_DigestBegin failed (hash) hash_type=%d (err %d)",
  331. (int)hash_to_nss[instance->crypto_hash_type],
  332. PR_GetError());
  333. goto out;
  334. }
  335. if (PK11_DigestOp(enc_context,
  336. outdata,
  337. *buf_out_len) != SECSuccess) {
  338. log_printf(instance->log_level_security,
  339. "PK11_DigestOp failed (hash) hash_type=%d (err %d)",
  340. (int)hash_to_nss[instance->crypto_hash_type],
  341. PR_GetError());
  342. goto out;
  343. }
  344. if (PK11_DigestFinal(enc_context,
  345. hash_block,
  346. &tmp2_outlen,
  347. hash_block_len[instance->crypto_hash_type]) != SECSuccess) {
  348. log_printf(instance->log_level_security,
  349. "PK11_DigestFinale failed (hash) hash_type=%d (err %d)",
  350. (int)hash_to_nss[instance->crypto_hash_type],
  351. PR_GetError());
  352. goto out;
  353. }
  354. if (enc_context) {
  355. PK11_DestroyContext(enc_context, PR_TRUE);
  356. enc_context = 0;
  357. }
  358. memcpy(buf_out, hash_block, hash_len[instance->crypto_hash_type]);
  359. *buf_out_len = *buf_out_len + hash_len[instance->crypto_hash_type];
  360. no_hash:
  361. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  362. return 0;
  363. out:
  364. if (enc_context) {
  365. PK11_DestroyContext(enc_context, PR_TRUE);
  366. }
  367. if (nss_sec_param) {
  368. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  369. }
  370. return -1;
  371. }
  372. static int authenticate_and_decrypt_nss (
  373. struct crypto_instance *instance,
  374. unsigned char *buf,
  375. int *buf_len)
  376. {
  377. PK11Context* enc_context = NULL;
  378. SECItem crypt_param;
  379. SECItem hash_param;
  380. unsigned char hash_block[hash_block_len[instance->crypto_hash_type]];
  381. int tmp1_outlen = 0;
  382. unsigned int tmp2_outlen = 0;
  383. unsigned char *data;
  384. size_t datalen;
  385. unsigned char outbuf[FRAME_SIZE_MAX];
  386. int result_len;
  387. data = buf + hash_len[instance->crypto_hash_type];
  388. datalen = *buf_len - hash_len[instance->crypto_hash_type];
  389. if (!hash_to_nss[instance->crypto_hash_type]) {
  390. goto only_decrypt;
  391. }
  392. hash_param.type = siBuffer;
  393. hash_param.data = 0;
  394. hash_param.len = 0;
  395. /* Check the digest */
  396. enc_context = PK11_CreateContextBySymKey (hash_to_nss[instance->crypto_hash_type],
  397. CKA_SIGN,
  398. instance->nss_sym_key_sign,
  399. &hash_param);
  400. if (!enc_context) {
  401. log_printf(instance->log_level_security,
  402. "PK11_CreateContext failed (check digest) err %d",
  403. PR_GetError());
  404. goto out;
  405. }
  406. if (PK11_DigestBegin(enc_context) != SECSuccess) {
  407. log_printf(instance->log_level_security,
  408. "PK11_DigestBegin failed (check digest) err %d",
  409. PR_GetError());
  410. goto out;
  411. }
  412. if (PK11_DigestOp(enc_context, data, datalen) != SECSuccess) {
  413. log_printf(instance->log_level_security,
  414. "PK11_DigestOp failed (check digest) err %d",
  415. PR_GetError());
  416. goto out;
  417. }
  418. if (PK11_DigestFinal(enc_context, hash_block,
  419. &tmp2_outlen, hash_block_len[instance->crypto_hash_type]) != SECSuccess) {
  420. log_printf(instance->log_level_security,
  421. "PK11_DigestFinal failed (check digest) err %d",
  422. PR_GetError());
  423. goto out;
  424. }
  425. if (enc_context) {
  426. PK11_DestroyContext(enc_context, PR_TRUE);
  427. enc_context = 0;
  428. }
  429. if (memcmp(hash_block, buf, tmp2_outlen) != 0) {
  430. log_printf(instance->log_level_error, "Digest does not match");
  431. goto out;
  432. }
  433. only_decrypt:
  434. if (!cipher_to_nss[instance->crypto_cipher_type]) {
  435. memcpy(outbuf, data, datalen);
  436. result_len = datalen;
  437. goto no_decrypt;
  438. }
  439. /* Create cipher context for decryption */
  440. crypt_param.type = siBuffer;
  441. crypt_param.data = data;
  442. crypt_param.len = SALT_SIZE;
  443. /*
  444. * Get rid of salt
  445. */
  446. data += SALT_SIZE;
  447. datalen -= SALT_SIZE;
  448. enc_context = PK11_CreateContextBySymKey(cipher_to_nss[instance->crypto_cipher_type],
  449. CKA_DECRYPT,
  450. instance->nss_sym_key, &crypt_param);
  451. if (!enc_context) {
  452. log_printf(instance->log_level_security,
  453. "PK11_CreateContext (decrypt) failed (err %d)",
  454. PR_GetError());
  455. goto out;
  456. }
  457. if (PK11_CipherOp(enc_context, outbuf, &tmp1_outlen,
  458. sizeof(outbuf), data, datalen) != SECSuccess) {
  459. log_printf(instance->log_level_security,
  460. "PK11_CipherOp (decrypt) failed (err %d)",
  461. PR_GetError());
  462. goto out;
  463. }
  464. if (PK11_DigestFinal(enc_context, outbuf + tmp1_outlen, &tmp2_outlen,
  465. sizeof(outbuf) - tmp1_outlen) != SECSuccess) {
  466. log_printf(instance->log_level_security,
  467. "PK11_DigestFinal (decrypt) failed (err %d)",
  468. PR_GetError());
  469. goto out;
  470. }
  471. if (enc_context) {
  472. PK11_DestroyContext(enc_context, PR_TRUE);
  473. enc_context = 0;
  474. }
  475. result_len = tmp1_outlen + tmp2_outlen;
  476. no_decrypt:
  477. memset(buf, 0, *buf_len);
  478. memcpy(buf, outbuf, result_len);
  479. *buf_len = result_len;
  480. return 0;
  481. out:
  482. if (enc_context) {
  483. PK11_DestroyContext(enc_context, PR_TRUE);
  484. }
  485. return -1;
  486. }
  487. static int string_to_crypto_cipher_type(const char* crypto_cipher_type)
  488. {
  489. if (strcmp(crypto_cipher_type, "none") == 0) {
  490. return CRYPTO_CIPHER_TYPE_NONE;
  491. } else if (strcmp(crypto_cipher_type, "aes256") == 0) {
  492. return CRYPTO_CIPHER_TYPE_AES256;
  493. }
  494. return CRYPTO_CIPHER_TYPE_AES256;
  495. }
  496. static int string_to_crypto_hash_type(const char* crypto_hash_type)
  497. {
  498. if (strcmp(crypto_hash_type, "none") == 0) {
  499. return CRYPTO_HASH_TYPE_NONE;
  500. } else if (strcmp(crypto_hash_type, "md5") == 0) {
  501. return CRYPTO_HASH_TYPE_MD5;
  502. } else if (strcmp(crypto_hash_type, "sha1") == 0) {
  503. return CRYPTO_HASH_TYPE_SHA1;
  504. } else if (strcmp(crypto_hash_type, "sha256") == 0) {
  505. return CRYPTO_HASH_TYPE_SHA256;
  506. } else if (strcmp(crypto_hash_type, "sha384") == 0) {
  507. return CRYPTO_HASH_TYPE_SHA384;
  508. } else if (strcmp(crypto_hash_type, "sha512") == 0) {
  509. return CRYPTO_HASH_TYPE_SHA512;
  510. }
  511. return CRYPTO_HASH_TYPE_SHA1;
  512. }
  513. size_t crypto_sec_header_size(
  514. const char *crypto_cipher_type,
  515. const char *crypto_hash_type)
  516. {
  517. int crypto_cipher = string_to_crypto_cipher_type(crypto_cipher_type);
  518. int crypto_hash = string_to_crypto_hash_type(crypto_hash_type);
  519. size_t hdr_size = 0;
  520. hdr_size = sizeof(struct crypto_config_header);
  521. if (crypto_hash) {
  522. hdr_size += hash_len[crypto_hash];
  523. }
  524. if (crypto_cipher) {
  525. hdr_size += SALT_SIZE;
  526. hdr_size += cypher_block_len[crypto_cipher];
  527. }
  528. return hdr_size;
  529. }
  530. int crypto_encrypt_and_sign (
  531. struct crypto_instance *instance,
  532. const unsigned char *buf_in,
  533. const size_t buf_in_len,
  534. unsigned char *buf_out,
  535. size_t *buf_out_len)
  536. {
  537. int err = 0;
  538. struct crypto_config_header *cch;
  539. cch = (struct crypto_config_header *)buf_out;
  540. cch->crypto_cipher_type = instance->crypto_cipher_type;
  541. cch->crypto_hash_type = instance->crypto_hash_type;
  542. cch->__pad0 = 0;
  543. cch->__pad1 = 0;
  544. if ((!cipher_to_nss[instance->crypto_cipher_type]) &&
  545. (!hash_to_nss[instance->crypto_hash_type])) {
  546. memcpy(buf_out + sizeof(struct crypto_config_header), buf_in, buf_in_len);
  547. *buf_out_len = buf_in_len;
  548. err = 0;
  549. } else {
  550. err = encrypt_and_sign_nss(instance,
  551. buf_in, buf_in_len,
  552. buf_out + sizeof(struct crypto_config_header),
  553. buf_out_len);
  554. }
  555. *buf_out_len = *buf_out_len + sizeof(struct crypto_config_header);
  556. return err;
  557. }
  558. int crypto_authenticate_and_decrypt (struct crypto_instance *instance,
  559. unsigned char *buf,
  560. int *buf_len)
  561. {
  562. int err = 0;
  563. struct crypto_config_header *cch;
  564. cch = (struct crypto_config_header *)buf;
  565. /*
  566. * decode crypto config of incoming packets
  567. */
  568. if (cch->crypto_cipher_type != instance->crypto_cipher_type) {
  569. log_printf(instance->log_level_security,
  570. "Incoming packet has different crypto type. Rejecting");
  571. return -1;
  572. }
  573. if (cch->crypto_hash_type != instance->crypto_hash_type) {
  574. log_printf(instance->log_level_security,
  575. "Incoming packet has different hash type. Rejecting");
  576. return -1;
  577. }
  578. if ((cch->__pad0 != 0) || (cch->__pad1 != 0)) {
  579. log_printf(instance->log_level_security,
  580. "Incoming packet appears to have features not supported by this version of corosync. Rejecting");
  581. return -1;
  582. }
  583. /*
  584. * invalidate config header
  585. */
  586. cch = NULL;
  587. /*
  588. * and kill it
  589. */
  590. *buf_len = *buf_len - sizeof(struct crypto_config_header);
  591. memmove(buf, buf + sizeof(struct crypto_config_header), *buf_len);
  592. /*
  593. * if crypto is totally disabled, there is no work for us
  594. */
  595. if ((!cipher_to_nss[instance->crypto_cipher_type]) &&
  596. (!hash_to_nss[instance->crypto_hash_type])) {
  597. err = 0;
  598. } else {
  599. err = authenticate_and_decrypt_nss(instance, buf, buf_len);
  600. }
  601. return err;
  602. }
  603. struct crypto_instance *crypto_init(
  604. const unsigned char *private_key,
  605. unsigned int private_key_len,
  606. const char *crypto_cipher_type,
  607. const char *crypto_hash_type,
  608. void (*log_printf_func) (
  609. int level,
  610. int subsys,
  611. const char *function,
  612. const char *file,
  613. int line,
  614. const char *format,
  615. ...)__attribute__((format(printf, 6, 7))),
  616. int log_level_security,
  617. int log_level_notice,
  618. int log_level_error,
  619. int log_subsys_id)
  620. {
  621. struct crypto_instance *instance;
  622. instance = malloc(sizeof(*instance));
  623. if (instance == NULL) {
  624. return (NULL);
  625. }
  626. memset(instance, 0, sizeof(struct crypto_instance));
  627. memcpy(instance->private_key, private_key, private_key_len);
  628. instance->private_key_len = private_key_len;
  629. instance->crypto_cipher_type = string_to_crypto_cipher_type(crypto_cipher_type);
  630. instance->crypto_hash_type = string_to_crypto_hash_type(crypto_hash_type);
  631. instance->log_printf_func = log_printf_func;
  632. instance->log_level_security = log_level_security;
  633. instance->log_level_notice = log_level_notice;
  634. instance->log_level_error = log_level_error;
  635. instance->log_subsys_id = log_subsys_id;
  636. if (init_nss_crypto(instance, crypto_cipher_type, crypto_hash_type) < 0) {
  637. free(instance);
  638. return(NULL);
  639. }
  640. return (instance);
  641. }