totemcrypto.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. return 0;
  178. }
  179. static int encrypt_nss(
  180. struct crypto_instance *instance,
  181. const unsigned char *buf_in,
  182. const size_t buf_in_len,
  183. unsigned char *buf_out,
  184. size_t *buf_out_len)
  185. {
  186. PK11Context* crypt_context = NULL;
  187. SECItem crypt_param;
  188. SECItem *nss_sec_param = NULL;
  189. int tmp1_outlen = 0;
  190. unsigned int tmp2_outlen = 0;
  191. unsigned char *salt = buf_out;
  192. unsigned char *data = buf_out + SALT_SIZE;
  193. int err = -1;
  194. if (!cipher_to_nss[instance->crypto_cipher_type]) {
  195. memcpy(buf_out, buf_in, buf_in_len);
  196. *buf_out_len = buf_in_len;
  197. return 0;
  198. }
  199. if (PK11_GenerateRandom (salt, SALT_SIZE) != SECSuccess) {
  200. log_printf(instance->log_level_security,
  201. "Failure to generate a random number %d",
  202. PR_GetError());
  203. goto out;
  204. }
  205. crypt_param.type = siBuffer;
  206. crypt_param.data = salt;
  207. crypt_param.len = SALT_SIZE;
  208. nss_sec_param = PK11_ParamFromIV (cipher_to_nss[instance->crypto_cipher_type],
  209. &crypt_param);
  210. if (nss_sec_param == NULL) {
  211. log_printf(instance->log_level_security,
  212. "Failure to set up PKCS11 param (err %d)",
  213. PR_GetError());
  214. goto out;
  215. }
  216. /*
  217. * Create cipher context for encryption
  218. */
  219. crypt_context = PK11_CreateContextBySymKey (cipher_to_nss[instance->crypto_cipher_type],
  220. CKA_ENCRYPT,
  221. instance->nss_sym_key,
  222. nss_sec_param);
  223. if (!crypt_context) {
  224. log_printf(instance->log_level_security,
  225. "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d)",
  226. (int)cipher_to_nss[instance->crypto_cipher_type],
  227. PR_GetError());
  228. goto out;
  229. }
  230. if (PK11_CipherOp(crypt_context, data,
  231. &tmp1_outlen,
  232. FRAME_SIZE_MAX - instance->crypto_header_size,
  233. (unsigned char *)buf_in, buf_in_len) != SECSuccess) {
  234. log_printf(instance->log_level_security,
  235. "PK11_CipherOp failed (encrypt) crypt_type=%d (err %d)",
  236. (int)cipher_to_nss[instance->crypto_cipher_type],
  237. PR_GetError());
  238. goto out;
  239. }
  240. if (PK11_DigestFinal(crypt_context, data + tmp1_outlen,
  241. &tmp2_outlen, FRAME_SIZE_MAX - tmp1_outlen) != SECSuccess) {
  242. log_printf(instance->log_level_security,
  243. "PK11_DigestFinal failed (encrypt) crypt_type=%d (err %d)",
  244. (int)cipher_to_nss[instance->crypto_cipher_type],
  245. PR_GetError());
  246. goto out;
  247. }
  248. *buf_out_len = tmp1_outlen + tmp2_outlen + SALT_SIZE;
  249. err = 0;
  250. out:
  251. if (crypt_context) {
  252. PK11_DestroyContext(crypt_context, PR_TRUE);
  253. }
  254. if (nss_sec_param) {
  255. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  256. }
  257. return err;
  258. }
  259. static int decrypt_nss (
  260. struct crypto_instance *instance,
  261. unsigned char *buf,
  262. int *buf_len)
  263. {
  264. PK11Context* decrypt_context = NULL;
  265. SECItem decrypt_param;
  266. int tmp1_outlen = 0;
  267. unsigned int tmp2_outlen = 0;
  268. unsigned char *salt = buf;
  269. unsigned char *data = salt + SALT_SIZE;
  270. int datalen = *buf_len - SALT_SIZE;
  271. unsigned char outbuf[FRAME_SIZE_MAX];
  272. int outbuf_len;
  273. int err = -1;
  274. if (!cipher_to_nss[instance->crypto_cipher_type]) {
  275. return 0;
  276. }
  277. /* Create cipher context for decryption */
  278. decrypt_param.type = siBuffer;
  279. decrypt_param.data = salt;
  280. decrypt_param.len = SALT_SIZE;
  281. decrypt_context = PK11_CreateContextBySymKey(cipher_to_nss[instance->crypto_cipher_type],
  282. CKA_DECRYPT,
  283. instance->nss_sym_key, &decrypt_param);
  284. if (!decrypt_context) {
  285. log_printf(instance->log_level_security,
  286. "PK11_CreateContext (decrypt) failed (err %d)",
  287. PR_GetError());
  288. goto out;
  289. }
  290. if (PK11_CipherOp(decrypt_context, outbuf, &tmp1_outlen,
  291. sizeof(outbuf), data, datalen) != SECSuccess) {
  292. log_printf(instance->log_level_security,
  293. "PK11_CipherOp (decrypt) failed (err %d)",
  294. PR_GetError());
  295. goto out;
  296. }
  297. if (PK11_DigestFinal(decrypt_context, outbuf + tmp1_outlen, &tmp2_outlen,
  298. sizeof(outbuf) - tmp1_outlen) != SECSuccess) {
  299. log_printf(instance->log_level_security,
  300. "PK11_DigestFinal (decrypt) failed (err %d)",
  301. PR_GetError());
  302. goto out;
  303. }
  304. outbuf_len = tmp1_outlen + tmp2_outlen;
  305. memset(buf, 0, *buf_len);
  306. memcpy(buf, outbuf, outbuf_len);
  307. *buf_len = outbuf_len;
  308. err = 0;
  309. out:
  310. if (decrypt_context) {
  311. PK11_DestroyContext(decrypt_context, PR_TRUE);
  312. }
  313. return err;
  314. }
  315. /*
  316. * hash/hmac/digest functions
  317. */
  318. static int string_to_crypto_hash_type(const char* crypto_hash_type)
  319. {
  320. if (strcmp(crypto_hash_type, "none") == 0) {
  321. return CRYPTO_HASH_TYPE_NONE;
  322. } else if (strcmp(crypto_hash_type, "md5") == 0) {
  323. return CRYPTO_HASH_TYPE_MD5;
  324. } else if (strcmp(crypto_hash_type, "sha1") == 0) {
  325. return CRYPTO_HASH_TYPE_SHA1;
  326. } else if (strcmp(crypto_hash_type, "sha256") == 0) {
  327. return CRYPTO_HASH_TYPE_SHA256;
  328. } else if (strcmp(crypto_hash_type, "sha384") == 0) {
  329. return CRYPTO_HASH_TYPE_SHA384;
  330. } else if (strcmp(crypto_hash_type, "sha512") == 0) {
  331. return CRYPTO_HASH_TYPE_SHA512;
  332. }
  333. return CRYPTO_HASH_TYPE_SHA1;
  334. }
  335. static int init_nss_hash(struct crypto_instance *instance)
  336. {
  337. PK11SlotInfo* hash_slot = NULL;
  338. SECItem hash_param;
  339. if (!hash_to_nss[instance->crypto_hash_type]) {
  340. return 0;
  341. }
  342. hash_param.type = siBuffer;
  343. hash_param.data = 0;
  344. hash_param.len = 0;
  345. hash_slot = PK11_GetBestSlot(hash_to_nss[instance->crypto_hash_type], NULL);
  346. if (hash_slot == NULL) {
  347. log_printf(instance->log_level_security, "Unable to find security slot (err %d)",
  348. PR_GetError());
  349. return -1;
  350. }
  351. instance->nss_sym_key_sign = PK11_ImportSymKey(hash_slot,
  352. hash_to_nss[instance->crypto_hash_type],
  353. PK11_OriginUnwrap, CKA_SIGN,
  354. &hash_param, NULL);
  355. if (instance->nss_sym_key_sign == NULL) {
  356. log_printf(instance->log_level_security, "Failure to import key into NSS (err %d)",
  357. PR_GetError());
  358. return -1;
  359. }
  360. return 0;
  361. }
  362. static int calculate_nss_hash(
  363. struct crypto_instance *instance,
  364. const unsigned char *buf,
  365. const size_t buf_len,
  366. unsigned char *hash)
  367. {
  368. PK11Context* hash_context = NULL;
  369. SECItem hash_param;
  370. unsigned int hash_tmp_outlen = 0;
  371. unsigned char hash_block[hash_block_len[instance->crypto_hash_type]];
  372. int err = -1;
  373. /* Now do the digest */
  374. hash_param.type = siBuffer;
  375. hash_param.data = 0;
  376. hash_param.len = 0;
  377. hash_context = PK11_CreateContextBySymKey(hash_to_nss[instance->crypto_hash_type],
  378. CKA_SIGN,
  379. instance->nss_sym_key_sign,
  380. &hash_param);
  381. if (!hash_context) {
  382. log_printf(instance->log_level_security,
  383. "PK11_CreateContext failed (hash) hash_type=%d (err %d)",
  384. (int)hash_to_nss[instance->crypto_hash_type],
  385. PR_GetError());
  386. goto out;
  387. }
  388. if (PK11_DigestBegin(hash_context) != SECSuccess) {
  389. log_printf(instance->log_level_security,
  390. "PK11_DigestBegin failed (hash) hash_type=%d (err %d)",
  391. (int)hash_to_nss[instance->crypto_hash_type],
  392. PR_GetError());
  393. goto out;
  394. }
  395. if (PK11_DigestOp(hash_context,
  396. buf,
  397. buf_len) != SECSuccess) {
  398. log_printf(instance->log_level_security,
  399. "PK11_DigestOp failed (hash) hash_type=%d (err %d)",
  400. (int)hash_to_nss[instance->crypto_hash_type],
  401. PR_GetError());
  402. goto out;
  403. }
  404. if (PK11_DigestFinal(hash_context,
  405. hash_block,
  406. &hash_tmp_outlen,
  407. hash_block_len[instance->crypto_hash_type]) != SECSuccess) {
  408. log_printf(instance->log_level_security,
  409. "PK11_DigestFinale failed (hash) hash_type=%d (err %d)",
  410. (int)hash_to_nss[instance->crypto_hash_type],
  411. PR_GetError());
  412. goto out;
  413. }
  414. memcpy(hash, hash_block, hash_len[instance->crypto_hash_type]);
  415. err = 0;
  416. out:
  417. if (hash_context) {
  418. PK11_DestroyContext(hash_context, PR_TRUE);
  419. }
  420. return err;
  421. }
  422. /*
  423. * global/glue nss functions
  424. */
  425. static int init_nss_db(struct crypto_instance *instance)
  426. {
  427. if ((!cipher_to_nss[instance->crypto_cipher_type]) &&
  428. (!hash_to_nss[instance->crypto_hash_type])) {
  429. return 0;
  430. }
  431. if (NSS_NoDB_Init(".") != SECSuccess) {
  432. log_printf(instance->log_level_security, "NSS DB initialization failed (err %d)",
  433. PR_GetError());
  434. return -1;
  435. }
  436. return 0;
  437. }
  438. static int init_nss(struct crypto_instance *instance,
  439. const char *crypto_cipher_type,
  440. const char *crypto_hash_type)
  441. {
  442. log_printf(instance->log_level_notice,
  443. "Initializing transmit/receive security (NSS) crypto: %s hash: %s",
  444. crypto_cipher_type, crypto_hash_type);
  445. if (init_nss_db(instance) < 0) {
  446. return -1;
  447. }
  448. if (init_nss_crypto(instance) < 0) {
  449. return -1;
  450. }
  451. if (init_nss_hash(instance) < 0) {
  452. return -1;
  453. }
  454. return 0;
  455. }
  456. static int encrypt_and_sign_nss (
  457. struct crypto_instance *instance,
  458. const unsigned char *buf_in,
  459. const size_t buf_in_len,
  460. unsigned char *buf_out,
  461. size_t *buf_out_len)
  462. {
  463. unsigned char *hash = buf_out;
  464. unsigned char *data = hash + hash_len[instance->crypto_hash_type];
  465. if (encrypt_nss(instance, buf_in, buf_in_len, data, buf_out_len) < 0) {
  466. return -1;
  467. }
  468. if (hash_to_nss[instance->crypto_hash_type]) {
  469. if (calculate_nss_hash(instance, data, *buf_out_len, hash) < 0) {
  470. return -1;
  471. }
  472. *buf_out_len = *buf_out_len + hash_len[instance->crypto_hash_type];
  473. }
  474. return 0;
  475. }
  476. static int authenticate_and_decrypt_nss (
  477. struct crypto_instance *instance,
  478. unsigned char *buf,
  479. int *buf_len)
  480. {
  481. if (hash_to_nss[instance->crypto_hash_type]) {
  482. unsigned char tmp_hash[hash_len[instance->crypto_hash_type]];
  483. unsigned char *hash = buf;
  484. unsigned char *data = hash + hash_len[instance->crypto_hash_type];
  485. int datalen = *buf_len - hash_len[instance->crypto_hash_type];
  486. if (calculate_nss_hash(instance, data, datalen, tmp_hash) < 0) {
  487. return -1;
  488. }
  489. if (memcmp(tmp_hash, hash, hash_len[instance->crypto_hash_type]) != 0) {
  490. log_printf(instance->log_level_error, "Digest does not match");
  491. return -1;
  492. }
  493. memmove(buf, data, datalen);
  494. *buf_len = datalen;
  495. }
  496. if (decrypt_nss(instance, buf, buf_len) < 0) {
  497. return -1;
  498. }
  499. return 0;
  500. }
  501. /*
  502. * exported API
  503. */
  504. size_t crypto_sec_header_size(
  505. const char *crypto_cipher_type,
  506. const char *crypto_hash_type)
  507. {
  508. int crypto_cipher = string_to_crypto_cipher_type(crypto_cipher_type);
  509. int crypto_hash = string_to_crypto_hash_type(crypto_hash_type);
  510. size_t hdr_size = 0;
  511. hdr_size = sizeof(struct crypto_config_header);
  512. if (crypto_hash) {
  513. hdr_size += hash_len[crypto_hash];
  514. }
  515. if (crypto_cipher) {
  516. hdr_size += SALT_SIZE;
  517. hdr_size += cypher_block_len[crypto_cipher];
  518. }
  519. return hdr_size;
  520. }
  521. int crypto_encrypt_and_sign (
  522. struct crypto_instance *instance,
  523. const unsigned char *buf_in,
  524. const size_t buf_in_len,
  525. unsigned char *buf_out,
  526. size_t *buf_out_len)
  527. {
  528. struct crypto_config_header *cch = (struct crypto_config_header *)buf_out;
  529. int err;
  530. cch->crypto_cipher_type = instance->crypto_cipher_type;
  531. cch->crypto_hash_type = instance->crypto_hash_type;
  532. cch->__pad0 = 0;
  533. cch->__pad1 = 0;
  534. buf_out += sizeof(struct crypto_config_header);
  535. err = encrypt_and_sign_nss(instance,
  536. buf_in, buf_in_len,
  537. buf_out, buf_out_len);
  538. *buf_out_len = *buf_out_len + sizeof(struct crypto_config_header);
  539. return err;
  540. }
  541. int crypto_authenticate_and_decrypt (struct crypto_instance *instance,
  542. unsigned char *buf,
  543. int *buf_len)
  544. {
  545. int err = 0;
  546. struct crypto_config_header *cch = (struct crypto_config_header *)buf;
  547. /*
  548. * decode crypto config of incoming packets
  549. */
  550. if (cch->crypto_cipher_type != instance->crypto_cipher_type) {
  551. log_printf(instance->log_level_security,
  552. "Incoming packet has different crypto type. Rejecting");
  553. return -1;
  554. }
  555. if (cch->crypto_hash_type != instance->crypto_hash_type) {
  556. log_printf(instance->log_level_security,
  557. "Incoming packet has different hash type. Rejecting");
  558. return -1;
  559. }
  560. if ((cch->__pad0 != 0) || (cch->__pad1 != 0)) {
  561. log_printf(instance->log_level_security,
  562. "Incoming packet appears to have features not supported by this version of corosync. Rejecting");
  563. return -1;
  564. }
  565. /*
  566. * invalidate config header and kill it
  567. */
  568. cch = NULL;
  569. *buf_len -= sizeof(struct crypto_config_header);
  570. memmove(buf, buf + sizeof(struct crypto_config_header), *buf_len);
  571. return authenticate_and_decrypt_nss(instance, buf, buf_len);
  572. }
  573. struct crypto_instance *crypto_init(
  574. const unsigned char *private_key,
  575. unsigned int private_key_len,
  576. const char *crypto_cipher_type,
  577. const char *crypto_hash_type,
  578. void (*log_printf_func) (
  579. int level,
  580. int subsys,
  581. const char *function,
  582. const char *file,
  583. int line,
  584. const char *format,
  585. ...)__attribute__((format(printf, 6, 7))),
  586. int log_level_security,
  587. int log_level_notice,
  588. int log_level_error,
  589. int log_subsys_id)
  590. {
  591. struct crypto_instance *instance;
  592. instance = malloc(sizeof(*instance));
  593. if (instance == NULL) {
  594. return (NULL);
  595. }
  596. memset(instance, 0, sizeof(struct crypto_instance));
  597. memcpy(instance->private_key, private_key, private_key_len);
  598. instance->private_key_len = private_key_len;
  599. instance->crypto_cipher_type = string_to_crypto_cipher_type(crypto_cipher_type);
  600. instance->crypto_hash_type = string_to_crypto_hash_type(crypto_hash_type);
  601. instance->crypto_header_size = crypto_sec_header_size(crypto_cipher_type, crypto_hash_type);
  602. instance->log_printf_func = log_printf_func;
  603. instance->log_level_security = log_level_security;
  604. instance->log_level_notice = log_level_notice;
  605. instance->log_level_error = log_level_error;
  606. instance->log_subsys_id = log_subsys_id;
  607. if (init_nss(instance, crypto_cipher_type, crypto_hash_type) < 0) {
  608. free(instance);
  609. return(NULL);
  610. }
  611. return (instance);
  612. }