4
0

totemcrypto.c 27 KB

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