totemcrypto.c 25 KB

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