4
0

totemcrypto.c 21 KB

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