totemcrypto.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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. #define MAX_WRAPPED_KEY_LEN 128
  183. /*
  184. * crypt/decrypt functions
  185. */
  186. static int string_to_crypto_cipher_type(const char* crypto_cipher_type)
  187. {
  188. if (strcmp(crypto_cipher_type, "none") == 0) {
  189. return CRYPTO_CIPHER_TYPE_NONE;
  190. } else if (strcmp(crypto_cipher_type, "aes256") == 0) {
  191. return CRYPTO_CIPHER_TYPE_AES256;
  192. } else if (strcmp(crypto_cipher_type, "aes192") == 0) {
  193. return CRYPTO_CIPHER_TYPE_AES192;
  194. } else if (strcmp(crypto_cipher_type, "aes128") == 0) {
  195. return CRYPTO_CIPHER_TYPE_AES128;
  196. } else if (strcmp(crypto_cipher_type, "3des") == 0) {
  197. return CRYPTO_CIPHER_TYPE_3DES;
  198. }
  199. return CRYPTO_CIPHER_TYPE_AES256;
  200. }
  201. static PK11SymKey *import_symmetric_key(struct crypto_instance *instance, enum sym_key_type key_type)
  202. {
  203. SECItem key_item;
  204. PK11SlotInfo *slot;
  205. PK11SymKey *res_key;
  206. CK_MECHANISM_TYPE cipher;
  207. CK_ATTRIBUTE_TYPE operation;
  208. CK_MECHANISM_TYPE wrap_mechanism;
  209. int wrap_key_len;
  210. PK11SymKey *wrap_key;
  211. PK11Context *wrap_key_crypt_context;
  212. SECItem tmp_sec_item;
  213. SECItem wrapped_key;
  214. int wrapped_key_len;
  215. unsigned char wrapped_key_data[MAX_WRAPPED_KEY_LEN];
  216. memset(&key_item, 0, sizeof(key_item));
  217. slot = NULL;
  218. wrap_key = NULL;
  219. res_key = NULL;
  220. wrap_key_crypt_context = NULL;
  221. key_item.type = siBuffer;
  222. key_item.data = instance->private_key;
  223. switch (key_type) {
  224. case SYM_KEY_TYPE_CRYPT:
  225. key_item.len = cipher_key_len[instance->crypto_cipher_type];
  226. cipher = cipher_to_nss[instance->crypto_cipher_type];
  227. operation = CKA_ENCRYPT|CKA_DECRYPT;
  228. break;
  229. case SYM_KEY_TYPE_HASH:
  230. key_item.len = instance->private_key_len;
  231. cipher = hash_to_nss[instance->crypto_hash_type];
  232. operation = CKA_SIGN;
  233. break;
  234. }
  235. slot = PK11_GetBestSlot(cipher, NULL);
  236. if (slot == NULL) {
  237. log_printf(instance->log_level_security, "Unable to find security slot (%d): %s",
  238. PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT));
  239. goto exit_res_key;
  240. }
  241. /*
  242. * Without FIPS it would be possible to just use
  243. * res_key = PK11_ImportSymKey(slot, cipher, PK11_OriginUnwrap, operation, &key_item, NULL);
  244. * with FIPS NSS Level 2 certification has to be "workarounded" (so it becomes Level 1) by using
  245. * following method:
  246. * 1. Generate wrap key
  247. * 2. Encrypt authkey with wrap key
  248. * 3. Unwrap encrypted authkey using wrap key
  249. */
  250. /*
  251. * Generate wrapping key
  252. */
  253. wrap_mechanism = PK11_GetBestWrapMechanism(slot);
  254. wrap_key_len = PK11_GetBestKeyLength(slot, wrap_mechanism);
  255. wrap_key = PK11_KeyGen(slot, wrap_mechanism, NULL, wrap_key_len, NULL);
  256. if (wrap_key == NULL) {
  257. log_printf(instance->log_level_security, "Unable to generate wrapping key (%d): %s",
  258. PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT));
  259. goto exit_res_key;
  260. }
  261. /*
  262. * Encrypt authkey with wrapping key
  263. */
  264. /*
  265. * Initialization of IV is not needed because PK11_GetBestWrapMechanism should return ECB mode
  266. */
  267. memset(&tmp_sec_item, 0, sizeof(tmp_sec_item));
  268. wrap_key_crypt_context = PK11_CreateContextBySymKey(wrap_mechanism, CKA_ENCRYPT,
  269. wrap_key, &tmp_sec_item);
  270. if (wrap_key_crypt_context == NULL) {
  271. log_printf(instance->log_level_security, "Unable to create encrypt context (%d): %s",
  272. PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT));
  273. goto exit_res_key;
  274. }
  275. wrapped_key_len = (int)sizeof(wrapped_key_data);
  276. if (PK11_CipherOp(wrap_key_crypt_context, wrapped_key_data, &wrapped_key_len,
  277. sizeof(wrapped_key_data), key_item.data, key_item.len) != SECSuccess) {
  278. log_printf(instance->log_level_security, "Unable to encrypt authkey (%d): %s",
  279. PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT));
  280. goto exit_res_key;
  281. }
  282. if (PK11_Finalize(wrap_key_crypt_context) != SECSuccess) {
  283. log_printf(instance->log_level_security, "Unable to finalize encryption of authkey (%d): %s",
  284. PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT));
  285. goto exit_res_key;
  286. }
  287. /*
  288. * Finally unwrap sym key
  289. */
  290. memset(&tmp_sec_item, 0, sizeof(tmp_sec_item));
  291. wrapped_key.data = wrapped_key_data;
  292. wrapped_key.len = wrapped_key_len;
  293. res_key = PK11_UnwrapSymKey(wrap_key, wrap_mechanism, &tmp_sec_item, &wrapped_key,
  294. cipher, operation, key_item.len);
  295. if (res_key == NULL) {
  296. log_printf(instance->log_level_security, "Failure to import key into NSS (%d): %s",
  297. PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT));
  298. goto exit_res_key;
  299. }
  300. exit_res_key:
  301. if (wrap_key_crypt_context != NULL) {
  302. PK11_DestroyContext(wrap_key_crypt_context, PR_TRUE);
  303. }
  304. if (wrap_key != NULL) {
  305. PK11_FreeSymKey(wrap_key);
  306. }
  307. if (slot != NULL) {
  308. PK11_FreeSlot(slot);
  309. }
  310. return (res_key);
  311. }
  312. static int init_nss_crypto(struct crypto_instance *instance)
  313. {
  314. if (!cipher_to_nss[instance->crypto_cipher_type]) {
  315. return 0;
  316. }
  317. instance->nss_sym_key = import_symmetric_key(instance, SYM_KEY_TYPE_CRYPT);
  318. if (instance->nss_sym_key == NULL) {
  319. return -1;
  320. }
  321. return 0;
  322. }
  323. static int encrypt_nss(
  324. struct crypto_instance *instance,
  325. const unsigned char *buf_in,
  326. const size_t buf_in_len,
  327. unsigned char *buf_out,
  328. size_t *buf_out_len)
  329. {
  330. PK11Context* crypt_context = NULL;
  331. SECItem crypt_param;
  332. SECItem *nss_sec_param = NULL;
  333. int tmp1_outlen = 0;
  334. unsigned int tmp2_outlen = 0;
  335. unsigned char *salt = buf_out;
  336. unsigned char *data = buf_out + SALT_SIZE;
  337. int err = -1;
  338. if (!cipher_to_nss[instance->crypto_cipher_type]) {
  339. memcpy(buf_out, buf_in, buf_in_len);
  340. *buf_out_len = buf_in_len;
  341. return 0;
  342. }
  343. if (PK11_GenerateRandom (salt, SALT_SIZE) != SECSuccess) {
  344. log_printf(instance->log_level_security,
  345. "Failure to generate a random number %d",
  346. PR_GetError());
  347. goto out;
  348. }
  349. crypt_param.type = siBuffer;
  350. crypt_param.data = salt;
  351. crypt_param.len = SALT_SIZE;
  352. nss_sec_param = PK11_ParamFromIV (cipher_to_nss[instance->crypto_cipher_type],
  353. &crypt_param);
  354. if (nss_sec_param == NULL) {
  355. log_printf(instance->log_level_security,
  356. "Failure to set up PKCS11 param (err %d)",
  357. PR_GetError());
  358. goto out;
  359. }
  360. /*
  361. * Create cipher context for encryption
  362. */
  363. crypt_context = PK11_CreateContextBySymKey (cipher_to_nss[instance->crypto_cipher_type],
  364. CKA_ENCRYPT,
  365. instance->nss_sym_key,
  366. nss_sec_param);
  367. if (!crypt_context) {
  368. log_printf(instance->log_level_security,
  369. "PK11_CreateContext failed (encrypt) crypt_type=%d (%d): %s",
  370. (int)cipher_to_nss[instance->crypto_cipher_type],
  371. PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT));
  372. goto out;
  373. }
  374. if (PK11_CipherOp(crypt_context, data,
  375. &tmp1_outlen,
  376. FRAME_SIZE_MAX - instance->crypto_header_size,
  377. (unsigned char *)buf_in, buf_in_len) != SECSuccess) {
  378. log_printf(instance->log_level_security,
  379. "PK11_CipherOp failed (encrypt) crypt_type=%d (err %d)",
  380. (int)cipher_to_nss[instance->crypto_cipher_type],
  381. PR_GetError());
  382. goto out;
  383. }
  384. if (PK11_DigestFinal(crypt_context, data + tmp1_outlen,
  385. &tmp2_outlen, FRAME_SIZE_MAX - tmp1_outlen) != SECSuccess) {
  386. log_printf(instance->log_level_security,
  387. "PK11_DigestFinal failed (encrypt) crypt_type=%d (err %d)",
  388. (int)cipher_to_nss[instance->crypto_cipher_type],
  389. PR_GetError());
  390. goto out;
  391. }
  392. *buf_out_len = tmp1_outlen + tmp2_outlen + SALT_SIZE;
  393. err = 0;
  394. out:
  395. if (crypt_context) {
  396. PK11_DestroyContext(crypt_context, PR_TRUE);
  397. }
  398. if (nss_sec_param) {
  399. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  400. }
  401. return err;
  402. }
  403. static int decrypt_nss (
  404. struct crypto_instance *instance,
  405. unsigned char *buf,
  406. int *buf_len)
  407. {
  408. PK11Context* decrypt_context = NULL;
  409. SECItem decrypt_param;
  410. int tmp1_outlen = 0;
  411. unsigned int tmp2_outlen = 0;
  412. unsigned char *salt = buf;
  413. unsigned char *data = salt + SALT_SIZE;
  414. int datalen = *buf_len - SALT_SIZE;
  415. unsigned char outbuf[FRAME_SIZE_MAX];
  416. int outbuf_len;
  417. int err = -1;
  418. if (!cipher_to_nss[instance->crypto_cipher_type]) {
  419. return 0;
  420. }
  421. /* Create cipher context for decryption */
  422. decrypt_param.type = siBuffer;
  423. decrypt_param.data = salt;
  424. decrypt_param.len = SALT_SIZE;
  425. decrypt_context = PK11_CreateContextBySymKey(cipher_to_nss[instance->crypto_cipher_type],
  426. CKA_DECRYPT,
  427. instance->nss_sym_key, &decrypt_param);
  428. if (!decrypt_context) {
  429. log_printf(instance->log_level_security,
  430. "PK11_CreateContext (decrypt) failed (err %d)",
  431. PR_GetError());
  432. goto out;
  433. }
  434. if (PK11_CipherOp(decrypt_context, outbuf, &tmp1_outlen,
  435. sizeof(outbuf), data, datalen) != SECSuccess) {
  436. log_printf(instance->log_level_security,
  437. "PK11_CipherOp (decrypt) failed (err %d)",
  438. PR_GetError());
  439. goto out;
  440. }
  441. if (PK11_DigestFinal(decrypt_context, outbuf + tmp1_outlen, &tmp2_outlen,
  442. sizeof(outbuf) - tmp1_outlen) != SECSuccess) {
  443. log_printf(instance->log_level_security,
  444. "PK11_DigestFinal (decrypt) failed (err %d)",
  445. PR_GetError());
  446. goto out;
  447. }
  448. outbuf_len = tmp1_outlen + tmp2_outlen;
  449. memset(buf, 0, *buf_len);
  450. memcpy(buf, outbuf, outbuf_len);
  451. *buf_len = outbuf_len;
  452. err = 0;
  453. out:
  454. if (decrypt_context) {
  455. PK11_DestroyContext(decrypt_context, PR_TRUE);
  456. }
  457. return err;
  458. }
  459. /*
  460. * hash/hmac/digest functions
  461. */
  462. static int string_to_crypto_hash_type(const char* crypto_hash_type)
  463. {
  464. if (strcmp(crypto_hash_type, "none") == 0) {
  465. return CRYPTO_HASH_TYPE_NONE;
  466. } else if (strcmp(crypto_hash_type, "md5") == 0) {
  467. return CRYPTO_HASH_TYPE_MD5;
  468. } else if (strcmp(crypto_hash_type, "sha1") == 0) {
  469. return CRYPTO_HASH_TYPE_SHA1;
  470. } else if (strcmp(crypto_hash_type, "sha256") == 0) {
  471. return CRYPTO_HASH_TYPE_SHA256;
  472. } else if (strcmp(crypto_hash_type, "sha384") == 0) {
  473. return CRYPTO_HASH_TYPE_SHA384;
  474. } else if (strcmp(crypto_hash_type, "sha512") == 0) {
  475. return CRYPTO_HASH_TYPE_SHA512;
  476. }
  477. return CRYPTO_HASH_TYPE_SHA1;
  478. }
  479. static int init_nss_hash(struct crypto_instance *instance)
  480. {
  481. if (!hash_to_nss[instance->crypto_hash_type]) {
  482. return 0;
  483. }
  484. instance->nss_sym_key_sign = import_symmetric_key(instance, SYM_KEY_TYPE_HASH);
  485. if (instance->nss_sym_key_sign == NULL) {
  486. return -1;
  487. }
  488. return 0;
  489. }
  490. static int calculate_nss_hash(
  491. struct crypto_instance *instance,
  492. const unsigned char *buf,
  493. const size_t buf_len,
  494. unsigned char *hash)
  495. {
  496. PK11Context* hash_context = NULL;
  497. SECItem hash_param;
  498. unsigned int hash_tmp_outlen = 0;
  499. unsigned char hash_block[hash_block_len[instance->crypto_hash_type]];
  500. int err = -1;
  501. /* Now do the digest */
  502. hash_param.type = siBuffer;
  503. hash_param.data = 0;
  504. hash_param.len = 0;
  505. hash_context = PK11_CreateContextBySymKey(hash_to_nss[instance->crypto_hash_type],
  506. CKA_SIGN,
  507. instance->nss_sym_key_sign,
  508. &hash_param);
  509. if (!hash_context) {
  510. log_printf(instance->log_level_security,
  511. "PK11_CreateContext failed (hash) hash_type=%d (err %d)",
  512. (int)hash_to_nss[instance->crypto_hash_type],
  513. PR_GetError());
  514. goto out;
  515. }
  516. if (PK11_DigestBegin(hash_context) != SECSuccess) {
  517. log_printf(instance->log_level_security,
  518. "PK11_DigestBegin failed (hash) hash_type=%d (err %d)",
  519. (int)hash_to_nss[instance->crypto_hash_type],
  520. PR_GetError());
  521. goto out;
  522. }
  523. if (PK11_DigestOp(hash_context,
  524. buf,
  525. buf_len) != SECSuccess) {
  526. log_printf(instance->log_level_security,
  527. "PK11_DigestOp failed (hash) hash_type=%d (err %d)",
  528. (int)hash_to_nss[instance->crypto_hash_type],
  529. PR_GetError());
  530. goto out;
  531. }
  532. if (PK11_DigestFinal(hash_context,
  533. hash_block,
  534. &hash_tmp_outlen,
  535. hash_block_len[instance->crypto_hash_type]) != SECSuccess) {
  536. log_printf(instance->log_level_security,
  537. "PK11_DigestFinale failed (hash) hash_type=%d (err %d)",
  538. (int)hash_to_nss[instance->crypto_hash_type],
  539. PR_GetError());
  540. goto out;
  541. }
  542. memcpy(hash, hash_block, hash_len[instance->crypto_hash_type]);
  543. err = 0;
  544. out:
  545. if (hash_context) {
  546. PK11_DestroyContext(hash_context, PR_TRUE);
  547. }
  548. return err;
  549. }
  550. /*
  551. * global/glue nss functions
  552. */
  553. static int init_nss_db(struct crypto_instance *instance)
  554. {
  555. if ((!cipher_to_nss[instance->crypto_cipher_type]) &&
  556. (!hash_to_nss[instance->crypto_hash_type])) {
  557. return 0;
  558. }
  559. if (NSS_NoDB_Init(".") != SECSuccess) {
  560. log_printf(instance->log_level_security, "NSS DB initialization failed (err %d)",
  561. PR_GetError());
  562. return -1;
  563. }
  564. return 0;
  565. }
  566. static int init_nss(struct crypto_instance *instance,
  567. const char *crypto_cipher_type,
  568. const char *crypto_hash_type)
  569. {
  570. log_printf(instance->log_level_notice,
  571. "Initializing transmit/receive security (NSS) crypto: %s hash: %s",
  572. crypto_cipher_type, crypto_hash_type);
  573. if (init_nss_db(instance) < 0) {
  574. return -1;
  575. }
  576. if (init_nss_crypto(instance) < 0) {
  577. return -1;
  578. }
  579. if (init_nss_hash(instance) < 0) {
  580. return -1;
  581. }
  582. return 0;
  583. }
  584. static int encrypt_and_sign_nss_2_3 (
  585. struct crypto_instance *instance,
  586. const unsigned char *buf_in,
  587. const size_t buf_in_len,
  588. unsigned char *buf_out,
  589. size_t *buf_out_len)
  590. {
  591. if (encrypt_nss(instance,
  592. buf_in, buf_in_len,
  593. buf_out + sizeof(struct crypto_config_header), buf_out_len) < 0) {
  594. return -1;
  595. }
  596. *buf_out_len += sizeof(struct crypto_config_header);
  597. if (hash_to_nss[instance->crypto_hash_type]) {
  598. if (calculate_nss_hash(instance, buf_out, *buf_out_len, buf_out + *buf_out_len) < 0) {
  599. return -1;
  600. }
  601. *buf_out_len += hash_len[instance->crypto_hash_type];
  602. }
  603. return 0;
  604. }
  605. static int authenticate_nss_2_3 (
  606. struct crypto_instance *instance,
  607. unsigned char *buf,
  608. int *buf_len)
  609. {
  610. if (hash_to_nss[instance->crypto_hash_type]) {
  611. unsigned char tmp_hash[hash_len[instance->crypto_hash_type]];
  612. int datalen = *buf_len - hash_len[instance->crypto_hash_type];
  613. if (calculate_nss_hash(instance, buf, datalen, tmp_hash) < 0) {
  614. return -1;
  615. }
  616. if (memcmp(tmp_hash, buf + datalen, hash_len[instance->crypto_hash_type]) != 0) {
  617. log_printf(instance->log_level_error, "Digest does not match");
  618. return -1;
  619. }
  620. *buf_len = datalen;
  621. }
  622. return 0;
  623. }
  624. static int decrypt_nss_2_3 (
  625. struct crypto_instance *instance,
  626. unsigned char *buf,
  627. int *buf_len)
  628. {
  629. *buf_len -= sizeof(struct crypto_config_header);
  630. if (decrypt_nss(instance, buf + sizeof(struct crypto_config_header), buf_len) < 0) {
  631. return -1;
  632. }
  633. return 0;
  634. }
  635. /*
  636. * exported API
  637. */
  638. size_t crypto_sec_header_size(
  639. const char *crypto_cipher_type,
  640. const char *crypto_hash_type)
  641. {
  642. int crypto_cipher = string_to_crypto_cipher_type(crypto_cipher_type);
  643. int crypto_hash = string_to_crypto_hash_type(crypto_hash_type);
  644. size_t hdr_size = 0;
  645. int block_size = 0;
  646. hdr_size = sizeof(struct crypto_config_header);
  647. if (crypto_hash) {
  648. hdr_size += hash_len[crypto_hash];
  649. }
  650. if (crypto_cipher) {
  651. hdr_size += SALT_SIZE;
  652. if (cypher_block_len[crypto_cipher]) {
  653. block_size = cypher_block_len[crypto_cipher];
  654. } else {
  655. block_size = PK11_GetBlockSize(crypto_cipher, NULL);
  656. if (block_size < 0) {
  657. /*
  658. * failsafe. we can potentially lose up to 63
  659. * byte per packet, but better than fragmenting
  660. */
  661. block_size = 64;
  662. }
  663. }
  664. hdr_size += (block_size * 2);
  665. }
  666. return hdr_size;
  667. }
  668. /*
  669. * 2.0 packet format:
  670. * crypto_cipher_type | crypto_hash_type | __pad0 | __pad1 | hash | salt | data
  671. * only data is encrypted, hash only covers salt + data
  672. *
  673. * 2.2/2.3 packet format
  674. * fake_crypto_cipher_type | fake_crypto_hash_type | __pad0 | __pad1 | salt | data | hash
  675. * only data is encrypted, hash covers the whole packet
  676. *
  677. * we need to leave fake_* unencrypted for older versions of corosync to reject the packets,
  678. * we need to leave __pad0|1 unencrypted for performance reasons (saves at least 2 memcpy and
  679. * and extra buffer but values are hashed and verified.
  680. */
  681. int crypto_encrypt_and_sign (
  682. struct crypto_instance *instance,
  683. const unsigned char *buf_in,
  684. const size_t buf_in_len,
  685. unsigned char *buf_out,
  686. size_t *buf_out_len)
  687. {
  688. struct crypto_config_header *cch = (struct crypto_config_header *)buf_out;
  689. int err;
  690. cch->crypto_cipher_type = CRYPTO_CIPHER_TYPE_2_3;
  691. cch->crypto_hash_type = CRYPTO_HASH_TYPE_2_3;
  692. cch->__pad0 = 0;
  693. cch->__pad1 = 0;
  694. err = encrypt_and_sign_nss_2_3(instance,
  695. buf_in, buf_in_len,
  696. buf_out, buf_out_len);
  697. return err;
  698. }
  699. int crypto_authenticate_and_decrypt (struct crypto_instance *instance,
  700. unsigned char *buf,
  701. int *buf_len)
  702. {
  703. struct crypto_config_header *cch = (struct crypto_config_header *)buf;
  704. if (cch->crypto_cipher_type != CRYPTO_CIPHER_TYPE_2_3) {
  705. log_printf(instance->log_level_security,
  706. "Incoming packet has different crypto type. Rejecting");
  707. return -1;
  708. }
  709. if (cch->crypto_hash_type != CRYPTO_HASH_TYPE_2_3) {
  710. log_printf(instance->log_level_security,
  711. "Incoming packet has different hash type. Rejecting");
  712. return -1;
  713. }
  714. /*
  715. * authenticate packet first
  716. */
  717. if (authenticate_nss_2_3(instance, buf, buf_len) != 0) {
  718. return -1;
  719. }
  720. /*
  721. * now we can "trust" the padding bytes/future features
  722. */
  723. if ((cch->__pad0 != 0) || (cch->__pad1 != 0)) {
  724. log_printf(instance->log_level_security,
  725. "Incoming packet appears to have features not supported by this version of corosync. Rejecting");
  726. return -1;
  727. }
  728. /*
  729. * decrypt
  730. */
  731. if (decrypt_nss_2_3(instance, buf, buf_len) != 0) {
  732. return -1;
  733. }
  734. /*
  735. * invalidate config header and kill it
  736. */
  737. cch = NULL;
  738. memmove(buf, buf + sizeof(struct crypto_config_header), *buf_len);
  739. return 0;
  740. }
  741. struct crypto_instance *crypto_init(
  742. const unsigned char *private_key,
  743. unsigned int private_key_len,
  744. const char *crypto_cipher_type,
  745. const char *crypto_hash_type,
  746. void (*log_printf_func) (
  747. int level,
  748. int subsys,
  749. const char *function,
  750. const char *file,
  751. int line,
  752. const char *format,
  753. ...)__attribute__((format(printf, 6, 7))),
  754. int log_level_security,
  755. int log_level_notice,
  756. int log_level_error,
  757. int log_subsys_id)
  758. {
  759. struct crypto_instance *instance;
  760. instance = malloc(sizeof(*instance));
  761. if (instance == NULL) {
  762. return (NULL);
  763. }
  764. memset(instance, 0, sizeof(struct crypto_instance));
  765. memcpy(instance->private_key, private_key, private_key_len);
  766. instance->private_key_len = private_key_len;
  767. instance->crypto_cipher_type = string_to_crypto_cipher_type(crypto_cipher_type);
  768. instance->crypto_hash_type = string_to_crypto_hash_type(crypto_hash_type);
  769. instance->crypto_header_size = crypto_sec_header_size(crypto_cipher_type, crypto_hash_type);
  770. instance->log_printf_func = log_printf_func;
  771. instance->log_level_security = log_level_security;
  772. instance->log_level_notice = log_level_notice;
  773. instance->log_level_error = log_level_error;
  774. instance->log_subsys_id = log_subsys_id;
  775. if (init_nss(instance, crypto_cipher_type, crypto_hash_type) < 0) {
  776. free(instance);
  777. return(NULL);
  778. }
  779. return (instance);
  780. }