totemcrypto.c 22 KB

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