totemcrypto.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. * Fabio M. Di Nitto (fdinitto@redhat.com)
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include "config.h"
  38. #include <nss.h>
  39. #include <pk11pub.h>
  40. #include <pkcs11.h>
  41. #include <prerror.h>
  42. #include <blapit.h>
  43. #include <hasht.h>
  44. #define LOGSYS_UTILS_ONLY 1
  45. #include <corosync/logsys.h>
  46. #include <corosync/totem/totem.h>
  47. #include "totemcrypto.h"
  48. /*
  49. * define onwire crypto header
  50. */
  51. struct crypto_config_header {
  52. uint8_t crypto_cipher_type;
  53. uint8_t crypto_hash_type;
  54. uint8_t __pad0;
  55. uint8_t __pad1;
  56. } __attribute__((packed));
  57. /*
  58. * crypto definitions and conversion tables
  59. */
  60. #define SALT_SIZE 16
  61. enum crypto_crypt_t {
  62. CRYPTO_CIPHER_TYPE_NONE = 0,
  63. CRYPTO_CIPHER_TYPE_AES256 = 1
  64. };
  65. CK_MECHANISM_TYPE cipher_to_nss[] = {
  66. 0, /* CRYPTO_CIPHER_TYPE_NONE */
  67. CKM_AES_CBC_PAD /* CRYPTO_CIPHER_TYPE_AES256 */
  68. };
  69. size_t cipher_key_len[] = {
  70. 0, /* CRYPTO_CIPHER_TYPE_NONE */
  71. 32, /* CRYPTO_CIPHER_TYPE_AES256 */
  72. };
  73. size_t cypher_block_len[] = {
  74. 0, /* CRYPTO_CIPHER_TYPE_NONE */
  75. AES_BLOCK_SIZE /* CRYPTO_CIPHER_TYPE_AES256 */
  76. };
  77. /*
  78. * hash definitions and conversion tables
  79. */
  80. enum crypto_hash_t {
  81. CRYPTO_HASH_TYPE_NONE = 0,
  82. CRYPTO_HASH_TYPE_MD5 = 1,
  83. CRYPTO_HASH_TYPE_SHA1 = 2,
  84. CRYPTO_HASH_TYPE_SHA256 = 3,
  85. CRYPTO_HASH_TYPE_SHA384 = 4,
  86. CRYPTO_HASH_TYPE_SHA512 = 5
  87. };
  88. CK_MECHANISM_TYPE hash_to_nss[] = {
  89. 0, /* CRYPTO_HASH_TYPE_NONE */
  90. CKM_MD5_HMAC, /* CRYPTO_HASH_TYPE_MD5 */
  91. CKM_SHA_1_HMAC, /* CRYPTO_HASH_TYPE_SHA1 */
  92. CKM_SHA256_HMAC, /* CRYPTO_HASH_TYPE_SHA256 */
  93. CKM_SHA384_HMAC, /* CRYPTO_HASH_TYPE_SHA384 */
  94. CKM_SHA512_HMAC /* CRYPTO_HASH_TYPE_SHA512 */
  95. };
  96. size_t hash_len[] = {
  97. 0, /* CRYPTO_HASH_TYPE_NONE */
  98. MD5_LENGTH, /* CRYPTO_HASH_TYPE_MD5 */
  99. SHA1_LENGTH, /* CRYPTO_HASH_TYPE_SHA1 */
  100. SHA256_LENGTH, /* CRYPTO_HASH_TYPE_SHA256 */
  101. SHA384_LENGTH, /* CRYPTO_HASH_TYPE_SHA384 */
  102. SHA512_LENGTH /* CRYPTO_HASH_TYPE_SHA512 */
  103. };
  104. size_t hash_block_len[] = {
  105. 0, /* CRYPTO_HASH_TYPE_NONE */
  106. MD5_BLOCK_LENGTH, /* CRYPTO_HASH_TYPE_MD5 */
  107. SHA1_BLOCK_LENGTH, /* CRYPTO_HASH_TYPE_SHA1 */
  108. SHA256_BLOCK_LENGTH, /* CRYPTO_HASH_TYPE_SHA256 */
  109. SHA384_BLOCK_LENGTH, /* CRYPTO_HASH_TYPE_SHA384 */
  110. SHA512_BLOCK_LENGTH /* CRYPTO_HASH_TYPE_SHA512 */
  111. };
  112. struct crypto_instance {
  113. PK11SymKey *nss_sym_key;
  114. PK11SymKey *nss_sym_key_sign;
  115. unsigned char private_key[1024];
  116. unsigned int private_key_len;
  117. enum crypto_crypt_t crypto_cipher_type;
  118. enum crypto_hash_t crypto_hash_type;
  119. unsigned int crypto_header_size;
  120. void (*log_printf_func) (
  121. int level,
  122. int subsys,
  123. const char *function,
  124. const char *file,
  125. int line,
  126. const char *format,
  127. ...)__attribute__((format(printf, 6, 7)));
  128. int log_level_security;
  129. int log_level_notice;
  130. int log_level_error;
  131. int log_subsys_id;
  132. };
  133. #define log_printf(level, format, args...) \
  134. do { \
  135. instance->log_printf_func ( \
  136. level, instance->log_subsys_id, \
  137. __FUNCTION__, __FILE__, __LINE__, \
  138. (const char *)format, ##args); \
  139. } while (0);
  140. /*
  141. * crypt/decrypt functions
  142. */
  143. static int string_to_crypto_cipher_type(const char* crypto_cipher_type)
  144. {
  145. if (strcmp(crypto_cipher_type, "none") == 0) {
  146. return CRYPTO_CIPHER_TYPE_NONE;
  147. } else if (strcmp(crypto_cipher_type, "aes256") == 0) {
  148. return CRYPTO_CIPHER_TYPE_AES256;
  149. }
  150. return CRYPTO_CIPHER_TYPE_AES256;
  151. }
  152. static int init_nss_crypto(struct crypto_instance *instance)
  153. {
  154. PK11SlotInfo* crypt_slot = NULL;
  155. SECItem crypt_param;
  156. if (!cipher_to_nss[instance->crypto_cipher_type]) {
  157. return 0;
  158. }
  159. crypt_param.type = siBuffer;
  160. crypt_param.data = instance->private_key;
  161. crypt_param.len = cipher_key_len[instance->crypto_cipher_type];
  162. crypt_slot = PK11_GetBestSlot(cipher_to_nss[instance->crypto_cipher_type], NULL);
  163. if (crypt_slot == NULL) {
  164. log_printf(instance->log_level_security, "Unable to find security slot (err %d)",
  165. PR_GetError());
  166. return -1;
  167. }
  168. instance->nss_sym_key = PK11_ImportSymKey(crypt_slot,
  169. cipher_to_nss[instance->crypto_cipher_type],
  170. PK11_OriginUnwrap, CKA_ENCRYPT|CKA_DECRYPT,
  171. &crypt_param, NULL);
  172. if (instance->nss_sym_key == NULL) {
  173. log_printf(instance->log_level_security, "Failure to import key into NSS (err %d)",
  174. PR_GetError());
  175. return -1;
  176. }
  177. PK11_FreeSlot(crypt_slot);
  178. return 0;
  179. }
  180. static int encrypt_nss(
  181. struct crypto_instance *instance,
  182. const unsigned char *buf_in,
  183. const size_t buf_in_len,
  184. unsigned char *buf_out,
  185. size_t *buf_out_len)
  186. {
  187. PK11Context* crypt_context = NULL;
  188. SECItem crypt_param;
  189. SECItem *nss_sec_param = NULL;
  190. int tmp1_outlen = 0;
  191. unsigned int tmp2_outlen = 0;
  192. unsigned char *salt = buf_out;
  193. unsigned char *data = buf_out + SALT_SIZE;
  194. int err = -1;
  195. if (!cipher_to_nss[instance->crypto_cipher_type]) {
  196. memcpy(buf_out, buf_in, buf_in_len);
  197. *buf_out_len = buf_in_len;
  198. return 0;
  199. }
  200. if (PK11_GenerateRandom (salt, SALT_SIZE) != SECSuccess) {
  201. log_printf(instance->log_level_security,
  202. "Failure to generate a random number %d",
  203. PR_GetError());
  204. goto out;
  205. }
  206. crypt_param.type = siBuffer;
  207. crypt_param.data = salt;
  208. crypt_param.len = SALT_SIZE;
  209. nss_sec_param = PK11_ParamFromIV (cipher_to_nss[instance->crypto_cipher_type],
  210. &crypt_param);
  211. if (nss_sec_param == NULL) {
  212. log_printf(instance->log_level_security,
  213. "Failure to set up PKCS11 param (err %d)",
  214. PR_GetError());
  215. goto out;
  216. }
  217. /*
  218. * Create cipher context for encryption
  219. */
  220. crypt_context = PK11_CreateContextBySymKey (cipher_to_nss[instance->crypto_cipher_type],
  221. CKA_ENCRYPT,
  222. instance->nss_sym_key,
  223. nss_sec_param);
  224. if (!crypt_context) {
  225. log_printf(instance->log_level_security,
  226. "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d)",
  227. (int)cipher_to_nss[instance->crypto_cipher_type],
  228. PR_GetError());
  229. goto out;
  230. }
  231. if (PK11_CipherOp(crypt_context, data,
  232. &tmp1_outlen,
  233. FRAME_SIZE_MAX - instance->crypto_header_size,
  234. (unsigned char *)buf_in, buf_in_len) != SECSuccess) {
  235. log_printf(instance->log_level_security,
  236. "PK11_CipherOp failed (encrypt) crypt_type=%d (err %d)",
  237. (int)cipher_to_nss[instance->crypto_cipher_type],
  238. PR_GetError());
  239. goto out;
  240. }
  241. if (PK11_DigestFinal(crypt_context, data + tmp1_outlen,
  242. &tmp2_outlen, FRAME_SIZE_MAX - tmp1_outlen) != SECSuccess) {
  243. log_printf(instance->log_level_security,
  244. "PK11_DigestFinal failed (encrypt) crypt_type=%d (err %d)",
  245. (int)cipher_to_nss[instance->crypto_cipher_type],
  246. PR_GetError());
  247. goto out;
  248. }
  249. *buf_out_len = tmp1_outlen + tmp2_outlen + SALT_SIZE;
  250. err = 0;
  251. out:
  252. if (crypt_context) {
  253. PK11_DestroyContext(crypt_context, PR_TRUE);
  254. }
  255. if (nss_sec_param) {
  256. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  257. }
  258. return err;
  259. }
  260. static int decrypt_nss (
  261. struct crypto_instance *instance,
  262. unsigned char *buf,
  263. int *buf_len)
  264. {
  265. PK11Context* decrypt_context = NULL;
  266. SECItem decrypt_param;
  267. int tmp1_outlen = 0;
  268. unsigned int tmp2_outlen = 0;
  269. unsigned char *salt = buf;
  270. unsigned char *data = salt + SALT_SIZE;
  271. int datalen = *buf_len - SALT_SIZE;
  272. unsigned char outbuf[FRAME_SIZE_MAX];
  273. int outbuf_len;
  274. int err = -1;
  275. if (!cipher_to_nss[instance->crypto_cipher_type]) {
  276. return 0;
  277. }
  278. /* Create cipher context for decryption */
  279. decrypt_param.type = siBuffer;
  280. decrypt_param.data = salt;
  281. decrypt_param.len = SALT_SIZE;
  282. decrypt_context = PK11_CreateContextBySymKey(cipher_to_nss[instance->crypto_cipher_type],
  283. CKA_DECRYPT,
  284. instance->nss_sym_key, &decrypt_param);
  285. if (!decrypt_context) {
  286. log_printf(instance->log_level_security,
  287. "PK11_CreateContext (decrypt) failed (err %d)",
  288. PR_GetError());
  289. goto out;
  290. }
  291. if (PK11_CipherOp(decrypt_context, outbuf, &tmp1_outlen,
  292. sizeof(outbuf), data, datalen) != SECSuccess) {
  293. log_printf(instance->log_level_security,
  294. "PK11_CipherOp (decrypt) failed (err %d)",
  295. PR_GetError());
  296. goto out;
  297. }
  298. if (PK11_DigestFinal(decrypt_context, outbuf + tmp1_outlen, &tmp2_outlen,
  299. sizeof(outbuf) - tmp1_outlen) != SECSuccess) {
  300. log_printf(instance->log_level_security,
  301. "PK11_DigestFinal (decrypt) failed (err %d)",
  302. PR_GetError());
  303. goto out;
  304. }
  305. outbuf_len = tmp1_outlen + tmp2_outlen;
  306. memset(buf, 0, *buf_len);
  307. memcpy(buf, outbuf, outbuf_len);
  308. *buf_len = outbuf_len;
  309. err = 0;
  310. out:
  311. if (decrypt_context) {
  312. PK11_DestroyContext(decrypt_context, PR_TRUE);
  313. }
  314. return err;
  315. }
  316. /*
  317. * hash/hmac/digest functions
  318. */
  319. static int string_to_crypto_hash_type(const char* crypto_hash_type)
  320. {
  321. if (strcmp(crypto_hash_type, "none") == 0) {
  322. return CRYPTO_HASH_TYPE_NONE;
  323. } else if (strcmp(crypto_hash_type, "md5") == 0) {
  324. return CRYPTO_HASH_TYPE_MD5;
  325. } else if (strcmp(crypto_hash_type, "sha1") == 0) {
  326. return CRYPTO_HASH_TYPE_SHA1;
  327. } else if (strcmp(crypto_hash_type, "sha256") == 0) {
  328. return CRYPTO_HASH_TYPE_SHA256;
  329. } else if (strcmp(crypto_hash_type, "sha384") == 0) {
  330. return CRYPTO_HASH_TYPE_SHA384;
  331. } else if (strcmp(crypto_hash_type, "sha512") == 0) {
  332. return CRYPTO_HASH_TYPE_SHA512;
  333. }
  334. return CRYPTO_HASH_TYPE_SHA1;
  335. }
  336. static int init_nss_hash(struct crypto_instance *instance)
  337. {
  338. PK11SlotInfo* hash_slot = NULL;
  339. SECItem hash_param;
  340. if (!hash_to_nss[instance->crypto_hash_type]) {
  341. return 0;
  342. }
  343. hash_param.type = siBuffer;
  344. hash_param.data = 0;
  345. hash_param.len = 0;
  346. hash_slot = PK11_GetBestSlot(hash_to_nss[instance->crypto_hash_type], NULL);
  347. if (hash_slot == NULL) {
  348. log_printf(instance->log_level_security, "Unable to find security slot (err %d)",
  349. PR_GetError());
  350. return -1;
  351. }
  352. instance->nss_sym_key_sign = PK11_ImportSymKey(hash_slot,
  353. hash_to_nss[instance->crypto_hash_type],
  354. PK11_OriginUnwrap, CKA_SIGN,
  355. &hash_param, NULL);
  356. if (instance->nss_sym_key_sign == NULL) {
  357. log_printf(instance->log_level_security, "Failure to import key into NSS (err %d)",
  358. PR_GetError());
  359. return -1;
  360. }
  361. PK11_FreeSlot(hash_slot);
  362. return 0;
  363. }
  364. static int calculate_nss_hash(
  365. struct crypto_instance *instance,
  366. const unsigned char *buf,
  367. const size_t buf_len,
  368. unsigned char *hash)
  369. {
  370. PK11Context* hash_context = NULL;
  371. SECItem hash_param;
  372. unsigned int hash_tmp_outlen = 0;
  373. unsigned char hash_block[hash_block_len[instance->crypto_hash_type]];
  374. int err = -1;
  375. /* Now do the digest */
  376. hash_param.type = siBuffer;
  377. hash_param.data = 0;
  378. hash_param.len = 0;
  379. hash_context = PK11_CreateContextBySymKey(hash_to_nss[instance->crypto_hash_type],
  380. CKA_SIGN,
  381. instance->nss_sym_key_sign,
  382. &hash_param);
  383. if (!hash_context) {
  384. log_printf(instance->log_level_security,
  385. "PK11_CreateContext failed (hash) hash_type=%d (err %d)",
  386. (int)hash_to_nss[instance->crypto_hash_type],
  387. PR_GetError());
  388. goto out;
  389. }
  390. if (PK11_DigestBegin(hash_context) != SECSuccess) {
  391. log_printf(instance->log_level_security,
  392. "PK11_DigestBegin failed (hash) hash_type=%d (err %d)",
  393. (int)hash_to_nss[instance->crypto_hash_type],
  394. PR_GetError());
  395. goto out;
  396. }
  397. if (PK11_DigestOp(hash_context,
  398. buf,
  399. buf_len) != SECSuccess) {
  400. log_printf(instance->log_level_security,
  401. "PK11_DigestOp failed (hash) hash_type=%d (err %d)",
  402. (int)hash_to_nss[instance->crypto_hash_type],
  403. PR_GetError());
  404. goto out;
  405. }
  406. if (PK11_DigestFinal(hash_context,
  407. hash_block,
  408. &hash_tmp_outlen,
  409. hash_block_len[instance->crypto_hash_type]) != SECSuccess) {
  410. log_printf(instance->log_level_security,
  411. "PK11_DigestFinale failed (hash) hash_type=%d (err %d)",
  412. (int)hash_to_nss[instance->crypto_hash_type],
  413. PR_GetError());
  414. goto out;
  415. }
  416. memcpy(hash, hash_block, hash_len[instance->crypto_hash_type]);
  417. err = 0;
  418. out:
  419. if (hash_context) {
  420. PK11_DestroyContext(hash_context, PR_TRUE);
  421. }
  422. return err;
  423. }
  424. /*
  425. * global/glue nss functions
  426. */
  427. static int init_nss_db(struct crypto_instance *instance)
  428. {
  429. if ((!cipher_to_nss[instance->crypto_cipher_type]) &&
  430. (!hash_to_nss[instance->crypto_hash_type])) {
  431. return 0;
  432. }
  433. if (NSS_NoDB_Init(".") != SECSuccess) {
  434. log_printf(instance->log_level_security, "NSS DB initialization failed (err %d)",
  435. PR_GetError());
  436. return -1;
  437. }
  438. return 0;
  439. }
  440. static int init_nss(struct crypto_instance *instance,
  441. const char *crypto_cipher_type,
  442. const char *crypto_hash_type)
  443. {
  444. log_printf(instance->log_level_notice,
  445. "Initializing transmit/receive security (NSS) crypto: %s hash: %s",
  446. crypto_cipher_type, crypto_hash_type);
  447. if (init_nss_db(instance) < 0) {
  448. return -1;
  449. }
  450. if (init_nss_crypto(instance) < 0) {
  451. return -1;
  452. }
  453. if (init_nss_hash(instance) < 0) {
  454. return -1;
  455. }
  456. return 0;
  457. }
  458. static int encrypt_and_sign_nss (
  459. struct crypto_instance *instance,
  460. const unsigned char *buf_in,
  461. const size_t buf_in_len,
  462. unsigned char *buf_out,
  463. size_t *buf_out_len)
  464. {
  465. unsigned char *hash = buf_out;
  466. unsigned char *data = hash + hash_len[instance->crypto_hash_type];
  467. if (encrypt_nss(instance, buf_in, buf_in_len, data, buf_out_len) < 0) {
  468. return -1;
  469. }
  470. if (hash_to_nss[instance->crypto_hash_type]) {
  471. if (calculate_nss_hash(instance, data, *buf_out_len, hash) < 0) {
  472. return -1;
  473. }
  474. *buf_out_len = *buf_out_len + hash_len[instance->crypto_hash_type];
  475. }
  476. return 0;
  477. }
  478. static int authenticate_and_decrypt_nss (
  479. struct crypto_instance *instance,
  480. unsigned char *buf,
  481. int *buf_len)
  482. {
  483. if (hash_to_nss[instance->crypto_hash_type]) {
  484. unsigned char tmp_hash[hash_len[instance->crypto_hash_type]];
  485. unsigned char *hash = buf;
  486. unsigned char *data = hash + hash_len[instance->crypto_hash_type];
  487. int datalen = *buf_len - hash_len[instance->crypto_hash_type];
  488. if (calculate_nss_hash(instance, data, datalen, tmp_hash) < 0) {
  489. return -1;
  490. }
  491. if (memcmp(tmp_hash, hash, hash_len[instance->crypto_hash_type]) != 0) {
  492. log_printf(instance->log_level_error, "Digest does not match");
  493. return -1;
  494. }
  495. memmove(buf, data, datalen);
  496. *buf_len = datalen;
  497. }
  498. if (decrypt_nss(instance, buf, buf_len) < 0) {
  499. return -1;
  500. }
  501. return 0;
  502. }
  503. /*
  504. * exported API
  505. */
  506. size_t crypto_sec_header_size(
  507. const char *crypto_cipher_type,
  508. const char *crypto_hash_type)
  509. {
  510. int crypto_cipher = string_to_crypto_cipher_type(crypto_cipher_type);
  511. int crypto_hash = string_to_crypto_hash_type(crypto_hash_type);
  512. size_t hdr_size = 0;
  513. hdr_size = sizeof(struct crypto_config_header);
  514. if (crypto_hash) {
  515. hdr_size += hash_len[crypto_hash];
  516. }
  517. if (crypto_cipher) {
  518. hdr_size += SALT_SIZE;
  519. hdr_size += cypher_block_len[crypto_cipher];
  520. }
  521. return hdr_size;
  522. }
  523. int crypto_encrypt_and_sign (
  524. struct crypto_instance *instance,
  525. const unsigned char *buf_in,
  526. const size_t buf_in_len,
  527. unsigned char *buf_out,
  528. size_t *buf_out_len)
  529. {
  530. struct crypto_config_header *cch = (struct crypto_config_header *)buf_out;
  531. int err;
  532. cch->crypto_cipher_type = instance->crypto_cipher_type;
  533. cch->crypto_hash_type = instance->crypto_hash_type;
  534. cch->__pad0 = 0;
  535. cch->__pad1 = 0;
  536. buf_out += sizeof(struct crypto_config_header);
  537. err = encrypt_and_sign_nss(instance,
  538. buf_in, buf_in_len,
  539. buf_out, buf_out_len);
  540. *buf_out_len = *buf_out_len + sizeof(struct crypto_config_header);
  541. return err;
  542. }
  543. int crypto_authenticate_and_decrypt (struct crypto_instance *instance,
  544. unsigned char *buf,
  545. int *buf_len)
  546. {
  547. struct crypto_config_header *cch = (struct crypto_config_header *)buf;
  548. /*
  549. * decode crypto config of incoming packets
  550. */
  551. if (cch->crypto_cipher_type != instance->crypto_cipher_type) {
  552. log_printf(instance->log_level_security,
  553. "Incoming packet has different crypto type. Rejecting");
  554. return -1;
  555. }
  556. if (cch->crypto_hash_type != instance->crypto_hash_type) {
  557. log_printf(instance->log_level_security,
  558. "Incoming packet has different hash type. Rejecting");
  559. return -1;
  560. }
  561. if ((cch->__pad0 != 0) || (cch->__pad1 != 0)) {
  562. log_printf(instance->log_level_security,
  563. "Incoming packet appears to have features not supported by this version of corosync. Rejecting");
  564. return -1;
  565. }
  566. /*
  567. * invalidate config header and kill it
  568. */
  569. cch = NULL;
  570. *buf_len -= sizeof(struct crypto_config_header);
  571. memmove(buf, buf + sizeof(struct crypto_config_header), *buf_len);
  572. return authenticate_and_decrypt_nss(instance, buf, buf_len);
  573. }
  574. struct crypto_instance *crypto_init(
  575. const unsigned char *private_key,
  576. unsigned int private_key_len,
  577. const char *crypto_cipher_type,
  578. const char *crypto_hash_type,
  579. void (*log_printf_func) (
  580. int level,
  581. int subsys,
  582. const char *function,
  583. const char *file,
  584. int line,
  585. const char *format,
  586. ...)__attribute__((format(printf, 6, 7))),
  587. int log_level_security,
  588. int log_level_notice,
  589. int log_level_error,
  590. int log_subsys_id)
  591. {
  592. struct crypto_instance *instance;
  593. instance = malloc(sizeof(*instance));
  594. if (instance == NULL) {
  595. return (NULL);
  596. }
  597. memset(instance, 0, sizeof(struct crypto_instance));
  598. memcpy(instance->private_key, private_key, private_key_len);
  599. instance->private_key_len = private_key_len;
  600. instance->crypto_cipher_type = string_to_crypto_cipher_type(crypto_cipher_type);
  601. instance->crypto_hash_type = string_to_crypto_hash_type(crypto_hash_type);
  602. instance->crypto_header_size = crypto_sec_header_size(crypto_cipher_type, crypto_hash_type);
  603. instance->log_printf_func = log_printf_func;
  604. instance->log_level_security = log_level_security;
  605. instance->log_level_notice = log_level_notice;
  606. instance->log_level_error = log_level_error;
  607. instance->log_subsys_id = log_subsys_id;
  608. if (init_nss(instance, crypto_cipher_type, crypto_hash_type) < 0) {
  609. free(instance);
  610. return(NULL);
  611. }
  612. return (instance);
  613. }