totemcrypto.c 20 KB

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