totemcrypto.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <config.h>
  37. #include <assert.h>
  38. #include <pthread.h>
  39. #include <sys/mman.h>
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #include <sys/socket.h>
  43. #include <netdb.h>
  44. #include <sys/un.h>
  45. #include <sys/ioctl.h>
  46. #include <sys/param.h>
  47. #include <netinet/in.h>
  48. #include <arpa/inet.h>
  49. #include <unistd.h>
  50. #include <fcntl.h>
  51. #include <stdlib.h>
  52. #include <stdio.h>
  53. #include <errno.h>
  54. #include <sched.h>
  55. #include <time.h>
  56. #include <sys/time.h>
  57. #include <sys/poll.h>
  58. #include <limits.h>
  59. #include <corosync/sq.h>
  60. #include <corosync/swab.h>
  61. #include <corosync/list.h>
  62. #include <qb/qbdefs.h>
  63. #include <qb/qbloop.h>
  64. #define LOGSYS_UTILS_ONLY 1
  65. #include <corosync/logsys.h>
  66. #include <corosync/totem/totem.h>
  67. #include "totemcrypto.h"
  68. #include "util.h"
  69. #include <nss.h>
  70. #include <pk11pub.h>
  71. #include <pkcs11.h>
  72. #include <prerror.h>
  73. #include <blapit.h>
  74. #define SALT_SIZE 16
  75. struct crypto_config_header {
  76. uint8_t crypto_cipher_type;
  77. uint8_t crypto_hash_type;
  78. } __attribute__((packed));
  79. enum crypto_crypt_t {
  80. CRYPTO_CIPHER_TYPE_NONE = 0,
  81. CRYPTO_CIPHER_TYPE_AES256 = 1
  82. };
  83. CK_MECHANISM_TYPE cipher_to_nss[] = {
  84. 0, /* CRYPTO_CIPHER_TYPE_NONE */
  85. CKM_AES_CBC_PAD /* CRYPTO_CIPHER_TYPE_AES256 */
  86. };
  87. size_t cipher_key_len[] = {
  88. 0, /* CRYPTO_CIPHER_TYPE_NONE */
  89. 32, /* CRYPTO_CIPHER_TYPE_AES256 */
  90. };
  91. size_t cypher_block_len[] = {
  92. 0, /* CRYPTO_CIPHER_TYPE_NONE */
  93. AES_BLOCK_SIZE /* CRYPTO_CIPHER_TYPE_AES256 */
  94. };
  95. enum crypto_hash_t {
  96. CRYPTO_HASH_TYPE_NONE = 0,
  97. CRYPTO_HASH_TYPE_SHA1 = 1
  98. };
  99. CK_MECHANISM_TYPE hash_to_nss[] = {
  100. 0, /* CRYPTO_HASH_TYPE_NONE */
  101. CKM_SHA_1_HMAC /* CRYPTO_HASH_TYPE_SHA1 */
  102. };
  103. size_t hash_len[] = {
  104. 0, /* CRYPTO_HASH_TYPE_NONE */
  105. SHA1_LENGTH /* CRYPTO_HASH_TYPE_SHA1 */
  106. };
  107. size_t hash_block_len[] = {
  108. 0, /* CRYPTO_HASH_TYPE_NONE */
  109. SHA1_BLOCK_LENGTH /* CRYPTO_HASH_TYPE_SHA1 */
  110. };
  111. struct crypto_instance {
  112. PK11SymKey *nss_sym_key;
  113. PK11SymKey *nss_sym_key_sign;
  114. unsigned char private_key[1024];
  115. unsigned int private_key_len;
  116. enum crypto_crypt_t crypto_cipher_type;
  117. enum crypto_hash_t crypto_hash_type;
  118. void (*log_printf_func) (
  119. int level,
  120. int subsys,
  121. const char *function,
  122. const char *file,
  123. int line,
  124. const char *format,
  125. ...)__attribute__((format(printf, 6, 7)));
  126. int log_level_security;
  127. int log_level_notice;
  128. int log_level_error;
  129. int log_subsys_id;
  130. };
  131. #define log_printf(level, format, args...) \
  132. do { \
  133. instance->log_printf_func ( \
  134. level, instance->log_subsys_id, \
  135. __FUNCTION__, __FILE__, __LINE__, \
  136. (const char *)format, ##args); \
  137. } while (0);
  138. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  139. do { \
  140. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  141. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  142. instance->totemudp_log_printf ( \
  143. level, instance->log_subsys_id, \
  144. __FUNCTION__, __FILE__, __LINE__, \
  145. fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  146. } while(0)
  147. static int init_nss_crypto(struct crypto_instance *instance,
  148. const char *crypto_cipher_type,
  149. const char *crypto_hash_type)
  150. {
  151. PK11SlotInfo* crypt_slot = NULL;
  152. PK11SlotInfo* hash_slot = NULL;
  153. SECItem crypt_param;
  154. SECItem hash_param;
  155. if ((!cipher_to_nss[instance->crypto_cipher_type]) &&
  156. (!hash_to_nss[instance->crypto_hash_type])) {
  157. log_printf(instance->log_level_notice,
  158. "Initializing transmit/receive security: NONE");
  159. return 0;
  160. }
  161. log_printf(instance->log_level_notice,
  162. "Initializing transmit/receive security: NSS crypto: %s hash: %s",
  163. crypto_cipher_type, crypto_hash_type);
  164. if (NSS_NoDB_Init(".") != SECSuccess) {
  165. log_printf(instance->log_level_security, "NSS initialization failed (err %d)",
  166. PR_GetError());
  167. goto out;
  168. }
  169. if (cipher_to_nss[instance->crypto_cipher_type]) {
  170. crypt_param.type = siBuffer;
  171. crypt_param.data = instance->private_key;
  172. crypt_param.len = cipher_key_len[instance->crypto_cipher_type];
  173. crypt_slot = PK11_GetBestSlot(cipher_to_nss[instance->crypto_cipher_type], NULL);
  174. if (crypt_slot == NULL) {
  175. log_printf(instance->log_level_security, "Unable to find security slot (err %d)",
  176. PR_GetError());
  177. goto out;
  178. }
  179. instance->nss_sym_key = PK11_ImportSymKey(crypt_slot,
  180. cipher_to_nss[instance->crypto_cipher_type],
  181. PK11_OriginUnwrap, CKA_ENCRYPT|CKA_DECRYPT,
  182. &crypt_param, NULL);
  183. if (instance->nss_sym_key == NULL) {
  184. log_printf(instance->log_level_security, "Failure to import key into NSS (err %d)",
  185. PR_GetError());
  186. goto out;
  187. }
  188. }
  189. if (hash_to_nss[instance->crypto_hash_type]) {
  190. hash_param.type = siBuffer;
  191. hash_param.data = 0;
  192. hash_param.len = 0;
  193. hash_slot = PK11_GetBestSlot(hash_to_nss[instance->crypto_hash_type], NULL);
  194. if (hash_slot == NULL) {
  195. log_printf(instance->log_level_security, "Unable to find security slot (err %d)",
  196. PR_GetError());
  197. goto out;
  198. }
  199. instance->nss_sym_key_sign = PK11_ImportSymKey(hash_slot,
  200. hash_to_nss[instance->crypto_hash_type],
  201. PK11_OriginUnwrap, CKA_SIGN,
  202. &hash_param, NULL);
  203. if (instance->nss_sym_key_sign == NULL) {
  204. log_printf(instance->log_level_security, "Failure to import key into NSS (err %d)",
  205. PR_GetError());
  206. goto out;
  207. }
  208. }
  209. return 0;
  210. out:
  211. return -1;
  212. }
  213. static int encrypt_and_sign_nss (
  214. struct crypto_instance *instance,
  215. const unsigned char *buf_in,
  216. const size_t buf_in_len,
  217. unsigned char *buf_out,
  218. size_t *buf_out_len)
  219. {
  220. PK11Context* enc_context = NULL;
  221. SECItem crypt_param;
  222. SECItem hash_param;
  223. SECItem *nss_sec_param = NULL;
  224. unsigned char *outdata;
  225. int tmp1_outlen = 0;
  226. unsigned int tmp2_outlen = 0;
  227. unsigned char salt[SALT_SIZE];
  228. unsigned char hash_block[hash_block_len[instance->crypto_hash_type]];
  229. outdata = buf_out + hash_len[instance->crypto_hash_type];
  230. if (!cipher_to_nss[instance->crypto_cipher_type]) {
  231. memcpy(outdata, buf_in, buf_in_len);
  232. *buf_out_len = buf_in_len;
  233. goto only_hash;
  234. }
  235. if (PK11_GenerateRandom (salt, SALT_SIZE) != SECSuccess) {
  236. log_printf(instance->log_level_security,
  237. "Failure to generate a random number %d",
  238. PR_GetError());
  239. goto out;
  240. }
  241. memcpy(outdata, salt, SALT_SIZE);
  242. crypt_param.type = siBuffer;
  243. crypt_param.data = salt;
  244. crypt_param.len = SALT_SIZE;
  245. nss_sec_param = PK11_ParamFromIV (cipher_to_nss[instance->crypto_cipher_type],
  246. &crypt_param);
  247. if (nss_sec_param == NULL) {
  248. log_printf(instance->log_level_security,
  249. "Failure to set up PKCS11 param (err %d)",
  250. PR_GetError());
  251. goto out;
  252. }
  253. /*
  254. * Create cipher context for encryption
  255. */
  256. enc_context = PK11_CreateContextBySymKey (cipher_to_nss[instance->crypto_cipher_type],
  257. CKA_ENCRYPT,
  258. instance->nss_sym_key,
  259. nss_sec_param);
  260. if (!enc_context) {
  261. log_printf(instance->log_level_security,
  262. "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d)",
  263. (int)cipher_to_nss[instance->crypto_cipher_type],
  264. PR_GetError());
  265. goto out;
  266. }
  267. if (PK11_CipherOp(enc_context, outdata + SALT_SIZE,
  268. &tmp1_outlen,
  269. FRAME_SIZE_MAX - (sizeof(struct crypto_config_header) + hash_len[instance->crypto_hash_type] + SALT_SIZE),
  270. (unsigned char *)buf_in, buf_in_len) != SECSuccess) {
  271. log_printf(instance->log_level_security,
  272. "PK11_CipherOp failed (encrypt) crypt_type=%d (err %d)",
  273. (int)cipher_to_nss[instance->crypto_cipher_type],
  274. PR_GetError());
  275. goto out;
  276. }
  277. if (PK11_DigestFinal(enc_context, outdata + SALT_SIZE + tmp1_outlen,
  278. &tmp2_outlen, FRAME_SIZE_MAX - tmp1_outlen) != SECSuccess) {
  279. log_printf(instance->log_level_security,
  280. "PK11_DigestFinal 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 (enc_context) {
  286. PK11_DestroyContext(enc_context, PR_TRUE);
  287. enc_context = 0;
  288. }
  289. *buf_out_len = tmp1_outlen + tmp2_outlen + SALT_SIZE;
  290. only_hash:
  291. if (!hash_to_nss[instance->crypto_hash_type]) {
  292. goto no_hash;
  293. }
  294. /* Now do the digest */
  295. hash_param.type = siBuffer;
  296. hash_param.data = 0;
  297. hash_param.len = 0;
  298. enc_context = PK11_CreateContextBySymKey(hash_to_nss[instance->crypto_hash_type],
  299. CKA_SIGN,
  300. instance->nss_sym_key_sign,
  301. &hash_param);
  302. if (!enc_context) {
  303. log_printf(instance->log_level_security,
  304. "PK11_CreateContext failed (hash) hash_type=%d (err %d)",
  305. (int)hash_to_nss[instance->crypto_hash_type],
  306. PR_GetError());
  307. goto out;
  308. }
  309. if (PK11_DigestBegin(enc_context) != SECSuccess) {
  310. log_printf(instance->log_level_security,
  311. "PK11_DigestBegin failed (hash) hash_type=%d (err %d)",
  312. (int)hash_to_nss[instance->crypto_hash_type],
  313. PR_GetError());
  314. goto out;
  315. }
  316. if (PK11_DigestOp(enc_context,
  317. outdata,
  318. *buf_out_len) != SECSuccess) {
  319. log_printf(instance->log_level_security,
  320. "PK11_DigestOp failed (hash) hash_type=%d (err %d)",
  321. (int)hash_to_nss[instance->crypto_hash_type],
  322. PR_GetError());
  323. goto out;
  324. }
  325. if (PK11_DigestFinal(enc_context,
  326. hash_block,
  327. &tmp2_outlen,
  328. hash_block_len[instance->crypto_hash_type]) != SECSuccess) {
  329. log_printf(instance->log_level_security,
  330. "PK11_DigestFinale failed (hash) hash_type=%d (err %d)",
  331. (int)hash_to_nss[instance->crypto_hash_type],
  332. PR_GetError());
  333. goto out;
  334. }
  335. if (enc_context) {
  336. PK11_DestroyContext(enc_context, PR_TRUE);
  337. enc_context = 0;
  338. }
  339. memcpy(buf_out, hash_block, hash_len[instance->crypto_hash_type]);
  340. *buf_out_len = *buf_out_len + hash_len[instance->crypto_hash_type];
  341. no_hash:
  342. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  343. return 0;
  344. out:
  345. if (enc_context) {
  346. PK11_DestroyContext(enc_context, PR_TRUE);
  347. }
  348. if (nss_sec_param) {
  349. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  350. }
  351. return -1;
  352. }
  353. static int authenticate_and_decrypt_nss (
  354. struct crypto_instance *instance,
  355. unsigned char *buf,
  356. int *buf_len)
  357. {
  358. PK11Context* enc_context = NULL;
  359. SECItem crypt_param;
  360. SECItem hash_param;
  361. unsigned char hash_block[hash_block_len[instance->crypto_hash_type]];
  362. int tmp1_outlen = 0;
  363. unsigned int tmp2_outlen = 0;
  364. unsigned char *data;
  365. size_t datalen;
  366. unsigned char outbuf[FRAME_SIZE_MAX];
  367. int result_len;
  368. data = buf + hash_len[instance->crypto_hash_type];
  369. datalen = *buf_len - hash_len[instance->crypto_hash_type];
  370. if (!hash_to_nss[instance->crypto_hash_type]) {
  371. goto only_decrypt;
  372. }
  373. hash_param.type = siBuffer;
  374. hash_param.data = 0;
  375. hash_param.len = 0;
  376. /* Check the digest */
  377. enc_context = PK11_CreateContextBySymKey (hash_to_nss[instance->crypto_hash_type],
  378. CKA_SIGN,
  379. instance->nss_sym_key_sign,
  380. &hash_param);
  381. if (!enc_context) {
  382. log_printf(instance->log_level_security,
  383. "PK11_CreateContext failed (check digest) err %d",
  384. PR_GetError());
  385. goto out;
  386. }
  387. if (PK11_DigestBegin(enc_context) != SECSuccess) {
  388. log_printf(instance->log_level_security,
  389. "PK11_DigestBegin failed (check digest) err %d",
  390. PR_GetError());
  391. goto out;
  392. }
  393. if (PK11_DigestOp(enc_context, data, datalen) != SECSuccess) {
  394. log_printf(instance->log_level_security,
  395. "PK11_DigestOp failed (check digest) err %d",
  396. PR_GetError());
  397. goto out;
  398. }
  399. if (PK11_DigestFinal(enc_context, hash_block,
  400. &tmp2_outlen, hash_block_len[instance->crypto_hash_type]) != SECSuccess) {
  401. log_printf(instance->log_level_security,
  402. "PK11_DigestFinal failed (check digest) err %d",
  403. PR_GetError());
  404. goto out;
  405. }
  406. if (enc_context) {
  407. PK11_DestroyContext(enc_context, PR_TRUE);
  408. enc_context = 0;
  409. }
  410. if (memcmp(hash_block, buf, tmp2_outlen) != 0) {
  411. log_printf(instance->log_level_error, "Digest does not match");
  412. goto out;
  413. }
  414. only_decrypt:
  415. if (!cipher_to_nss[instance->crypto_cipher_type]) {
  416. memcpy(outbuf, data, datalen);
  417. result_len = datalen;
  418. goto no_decrypt;
  419. }
  420. /* Create cipher context for decryption */
  421. crypt_param.type = siBuffer;
  422. crypt_param.data = data;
  423. crypt_param.len = SALT_SIZE;
  424. /*
  425. * Get rid of salt
  426. */
  427. data += SALT_SIZE;
  428. datalen -= SALT_SIZE;
  429. enc_context = PK11_CreateContextBySymKey(cipher_to_nss[instance->crypto_cipher_type],
  430. CKA_DECRYPT,
  431. instance->nss_sym_key, &crypt_param);
  432. if (!enc_context) {
  433. log_printf(instance->log_level_security,
  434. "PK11_CreateContext (decrypt) failed (err %d)",
  435. PR_GetError());
  436. goto out;
  437. }
  438. if (PK11_CipherOp(enc_context, outbuf, &tmp1_outlen,
  439. sizeof(outbuf), data, datalen) != SECSuccess) {
  440. log_printf(instance->log_level_security,
  441. "PK11_CipherOp (decrypt) failed (err %d)",
  442. PR_GetError());
  443. goto out;
  444. }
  445. if (PK11_DigestFinal(enc_context, outbuf + tmp1_outlen, &tmp2_outlen,
  446. sizeof(outbuf) - tmp1_outlen) != SECSuccess) {
  447. log_printf(instance->log_level_security,
  448. "PK11_DigestFinal (decrypt) failed (err %d)",
  449. PR_GetError());
  450. goto out;
  451. }
  452. if (enc_context) {
  453. PK11_DestroyContext(enc_context, PR_TRUE);
  454. enc_context = 0;
  455. }
  456. result_len = tmp1_outlen + tmp2_outlen;
  457. no_decrypt:
  458. memset(buf, 0, *buf_len);
  459. memcpy(buf, outbuf, result_len);
  460. *buf_len = result_len;
  461. return 0;
  462. out:
  463. if (enc_context) {
  464. PK11_DestroyContext(enc_context, PR_TRUE);
  465. }
  466. return -1;
  467. }
  468. static int string_to_crypto_cipher_type(const char* crypto_cipher_type)
  469. {
  470. if (strcmp(crypto_cipher_type, "none") == 0) {
  471. return CRYPTO_CIPHER_TYPE_NONE;
  472. } else if (strcmp(crypto_cipher_type, "aes256") == 0) {
  473. return CRYPTO_CIPHER_TYPE_AES256;
  474. }
  475. return CRYPTO_CIPHER_TYPE_NONE;
  476. }
  477. static int string_to_crypto_hash_type(const char* crypto_hash_type)
  478. {
  479. if (strcmp(crypto_hash_type, "none") == 0) {
  480. return CRYPTO_HASH_TYPE_NONE;
  481. } else if (strcmp(crypto_hash_type, "sha1") == 0) {
  482. return CRYPTO_HASH_TYPE_SHA1;
  483. }
  484. return CRYPTO_HASH_TYPE_NONE;
  485. }
  486. size_t crypto_sec_header_size(
  487. const char *crypto_cipher_type,
  488. const char *crypto_hash_type)
  489. {
  490. int crypto_cipher = string_to_crypto_cipher_type(crypto_cipher_type);
  491. int crypto_hash = string_to_crypto_hash_type(crypto_hash_type);
  492. size_t hdr_size = 0;
  493. hdr_size = sizeof(struct crypto_config_header);
  494. if (crypto_hash) {
  495. hdr_size += hash_len[crypto_hash];
  496. }
  497. if (crypto_cipher) {
  498. hdr_size += SALT_SIZE;
  499. hdr_size += cypher_block_len[crypto_cipher];
  500. }
  501. return hdr_size;
  502. }
  503. int crypto_encrypt_and_sign (
  504. struct crypto_instance *instance,
  505. const unsigned char *buf_in,
  506. const size_t buf_in_len,
  507. unsigned char *buf_out,
  508. size_t *buf_out_len)
  509. {
  510. int err = 0;
  511. struct crypto_config_header *cch;
  512. cch = (struct crypto_config_header *)buf_out;
  513. cch->crypto_cipher_type = instance->crypto_cipher_type;
  514. cch->crypto_hash_type = instance->crypto_hash_type;
  515. if ((!cipher_to_nss[instance->crypto_cipher_type]) &&
  516. (!hash_to_nss[instance->crypto_hash_type])) {
  517. memcpy(buf_out + sizeof(struct crypto_config_header), buf_in, buf_in_len);
  518. *buf_out_len = buf_in_len;
  519. err = 0;
  520. } else {
  521. err = encrypt_and_sign_nss(instance,
  522. buf_in, buf_in_len,
  523. buf_out + sizeof(struct crypto_config_header),
  524. buf_out_len);
  525. }
  526. *buf_out_len = *buf_out_len + sizeof(struct crypto_config_header);
  527. return err;
  528. }
  529. int crypto_authenticate_and_decrypt (struct crypto_instance *instance,
  530. unsigned char *buf,
  531. int *buf_len)
  532. {
  533. int err = 0;
  534. struct crypto_config_header *cch;
  535. cch = (struct crypto_config_header *)buf;
  536. /*
  537. * decode crypto config of incoming packets
  538. */
  539. if (cch->crypto_cipher_type != instance->crypto_cipher_type) {
  540. log_printf(instance->log_level_security,
  541. "Incoming packet has different crypto type. Rejecting");
  542. return -1;
  543. }
  544. if (cch->crypto_hash_type != instance->crypto_hash_type) {
  545. log_printf(instance->log_level_security,
  546. "Incoming packet has different hash type. Rejecting");
  547. return -1;
  548. }
  549. /*
  550. * invalidate config header
  551. */
  552. cch = NULL;
  553. /*
  554. * and kill it
  555. */
  556. *buf_len = *buf_len - sizeof(struct crypto_config_header);
  557. memmove(buf, buf + sizeof(struct crypto_config_header), *buf_len);
  558. /*
  559. * if crypto is totally disabled, there is no work for us
  560. */
  561. if ((!cipher_to_nss[instance->crypto_cipher_type]) &&
  562. (!hash_to_nss[instance->crypto_hash_type])) {
  563. err = 0;
  564. } else {
  565. err = authenticate_and_decrypt_nss(instance, buf, buf_len);
  566. }
  567. return err;
  568. }
  569. struct crypto_instance *crypto_init(
  570. const unsigned char *private_key,
  571. unsigned int private_key_len,
  572. const char *crypto_cipher_type,
  573. const char *crypto_hash_type,
  574. void (*log_printf_func) (
  575. int level,
  576. int subsys,
  577. const char *function,
  578. const char *file,
  579. int line,
  580. const char *format,
  581. ...)__attribute__((format(printf, 6, 7))),
  582. int log_level_security,
  583. int log_level_notice,
  584. int log_level_error,
  585. int log_subsys_id)
  586. {
  587. struct crypto_instance *instance;
  588. instance = malloc(sizeof(*instance));
  589. if (instance == NULL) {
  590. return (NULL);
  591. }
  592. memset(instance, 0, sizeof(struct crypto_instance));
  593. memcpy(instance->private_key, private_key, private_key_len);
  594. instance->private_key_len = private_key_len;
  595. instance->crypto_cipher_type = string_to_crypto_cipher_type(crypto_cipher_type);
  596. instance->crypto_hash_type = string_to_crypto_hash_type(crypto_hash_type);
  597. instance->log_printf_func = log_printf_func;
  598. instance->log_level_security = log_level_security;
  599. instance->log_level_notice = log_level_notice;
  600. instance->log_level_error = log_level_error;
  601. instance->log_subsys_id = log_subsys_id;
  602. if (init_nss_crypto(instance, crypto_cipher_type, crypto_hash_type) < 0) {
  603. free(instance);
  604. return(NULL);
  605. }
  606. return (instance);
  607. }