totemcrypto.c 25 KB

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