totemcrypto.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. #define CRYPTO_HMAC_HASH_SIZE 20
  74. #define SALT_SIZE 16
  75. struct crypto_security_header {
  76. unsigned char hash_digest[CRYPTO_HMAC_HASH_SIZE]; /* The hash *MUST* be first in the data structure */
  77. unsigned char salt[SALT_SIZE]; /* random number */
  78. char msg[0];
  79. } __attribute__((packed));
  80. struct crypto_config_header {
  81. uint8_t crypto_cipher_type;
  82. uint8_t crypto_hash_type;
  83. } __attribute__((packed));
  84. struct crypto_instance {
  85. PK11SymKey *nss_sym_key;
  86. PK11SymKey *nss_sym_key_sign;
  87. unsigned char private_key[1024];
  88. unsigned int private_key_len;
  89. enum crypto_crypt_t crypto_cipher_type;
  90. enum crypto_hash_t crypto_hash_type;
  91. void (*log_printf_func) (
  92. int level,
  93. int subsys,
  94. const char *function,
  95. const char *file,
  96. int line,
  97. const char *format,
  98. ...)__attribute__((format(printf, 6, 7)));
  99. int log_level_security;
  100. int log_level_notice;
  101. int log_level_error;
  102. int log_subsys_id;
  103. };
  104. CK_MECHANISM_TYPE cipher_to_nss[] = {
  105. 0, /* CRYPTO_CIPHER_TYPE_NONE */
  106. CKM_AES_CBC_PAD /* CRYPTO_CIPHER_TYPE_AES256 */
  107. };
  108. size_t cipher_key_len[] = {
  109. 0, /* CRYPTO_CIPHER_TYPE_NONE */
  110. 32, /* CRYPTO_CIPHER_TYPE_AES256 */
  111. };
  112. CK_MECHANISM_TYPE hash_to_nss[] = {
  113. 0, /* CRYPTO_HASH_TYPE_NONE */
  114. CKM_SHA_1_HMAC /* CRYPTO_HASH_TYPE_SHA1 */
  115. };
  116. #define log_printf(level, format, args...) \
  117. do { \
  118. instance->log_printf_func ( \
  119. level, instance->log_subsys_id, \
  120. __FUNCTION__, __FILE__, __LINE__, \
  121. (const char *)format, ##args); \
  122. } while (0);
  123. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  124. do { \
  125. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  126. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  127. instance->totemudp_log_printf ( \
  128. level, instance->log_subsys_id, \
  129. __FUNCTION__, __FILE__, __LINE__, \
  130. fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  131. } while(0)
  132. static int init_nss_crypto(struct crypto_instance *instance,
  133. const char *crypto_cipher_type,
  134. const char *crypto_hash_type)
  135. {
  136. PK11SlotInfo* crypt_slot = NULL;
  137. PK11SlotInfo* hash_slot = NULL;
  138. SECItem crypt_param;
  139. SECItem hash_param;
  140. if ((!cipher_to_nss[instance->crypto_cipher_type]) &&
  141. (!hash_to_nss[instance->crypto_hash_type])) {
  142. log_printf(instance->log_level_notice,
  143. "Initializing transmit/receive security: NONE");
  144. return 0;
  145. }
  146. log_printf(instance->log_level_notice,
  147. "Initializing transmit/receive security: NSS crypto: %s hash: %s",
  148. crypto_cipher_type, crypto_hash_type);
  149. if (NSS_NoDB_Init(".") != SECSuccess) {
  150. log_printf(instance->log_level_security, "NSS initialization failed (err %d)",
  151. PR_GetError());
  152. goto out;
  153. }
  154. if (cipher_to_nss[instance->crypto_cipher_type]) {
  155. crypt_param.type = siBuffer;
  156. crypt_param.data = instance->private_key;
  157. crypt_param.len = cipher_key_len[instance->crypto_cipher_type];
  158. crypt_slot = PK11_GetBestSlot(cipher_to_nss[instance->crypto_cipher_type], NULL);
  159. if (crypt_slot == NULL) {
  160. log_printf(instance->log_level_security, "Unable to find security slot (err %d)",
  161. PR_GetError());
  162. goto out;
  163. }
  164. instance->nss_sym_key = PK11_ImportSymKey(crypt_slot,
  165. cipher_to_nss[instance->crypto_cipher_type],
  166. PK11_OriginUnwrap, CKA_ENCRYPT|CKA_DECRYPT,
  167. &crypt_param, NULL);
  168. if (instance->nss_sym_key == NULL) {
  169. log_printf(instance->log_level_security, "Failure to import key into NSS (err %d)",
  170. PR_GetError());
  171. goto out;
  172. }
  173. }
  174. if (hash_to_nss[instance->crypto_hash_type]) {
  175. hash_param.type = siBuffer;
  176. hash_param.data = 0;
  177. hash_param.len = 0;
  178. hash_slot = PK11_GetBestSlot(hash_to_nss[instance->crypto_hash_type], NULL);
  179. if (hash_slot == NULL) {
  180. log_printf(instance->log_level_security, "Unable to find security slot (err %d)",
  181. PR_GetError());
  182. goto out;
  183. }
  184. instance->nss_sym_key_sign = PK11_ImportSymKey(hash_slot,
  185. hash_to_nss[instance->crypto_hash_type],
  186. PK11_OriginUnwrap, CKA_SIGN,
  187. &hash_param, NULL);
  188. if (instance->nss_sym_key_sign == NULL) {
  189. log_printf(instance->log_level_security, "Failure to import key into NSS (err %d)",
  190. PR_GetError());
  191. goto out;
  192. }
  193. }
  194. return 0;
  195. out:
  196. return -1;
  197. }
  198. static int encrypt_and_sign_nss (
  199. struct crypto_instance *instance,
  200. const unsigned char *buf_in,
  201. const size_t buf_in_len,
  202. unsigned char *buf_out,
  203. size_t *buf_out_len)
  204. {
  205. PK11Context* enc_context = NULL;
  206. SECItem crypt_param;
  207. SECItem hash_param;
  208. SECItem *nss_sec_param = NULL;
  209. struct crypto_security_header *header;
  210. unsigned char *outdata;
  211. int tmp1_outlen = 0;
  212. unsigned int tmp2_outlen = 0;
  213. outdata = buf_out + sizeof (struct crypto_security_header);
  214. header = (struct crypto_security_header *)buf_out;
  215. memset(header->salt, 0, SALT_SIZE);
  216. if (!cipher_to_nss[instance->crypto_cipher_type]) {
  217. memcpy(outdata, buf_in, buf_in_len);
  218. *buf_out_len = buf_in_len;
  219. goto only_hash;
  220. }
  221. if (PK11_GenerateRandom (header->salt, SALT_SIZE) != SECSuccess) {
  222. log_printf(instance->log_level_security,
  223. "Failure to generate a random number %d",
  224. PR_GetError());
  225. goto out;
  226. }
  227. crypt_param.type = siBuffer;
  228. crypt_param.data = header->salt;
  229. crypt_param.len = SALT_SIZE;
  230. nss_sec_param = PK11_ParamFromIV (cipher_to_nss[instance->crypto_cipher_type],
  231. &crypt_param);
  232. if (nss_sec_param == NULL) {
  233. log_printf(instance->log_level_security,
  234. "Failure to set up PKCS11 param (err %d)",
  235. PR_GetError());
  236. goto out;
  237. }
  238. /*
  239. * Create cipher context for encryption
  240. */
  241. enc_context = PK11_CreateContextBySymKey (cipher_to_nss[instance->crypto_cipher_type],
  242. CKA_ENCRYPT,
  243. instance->nss_sym_key,
  244. nss_sec_param);
  245. if (!enc_context) {
  246. log_printf(instance->log_level_security,
  247. "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d)",
  248. (int)cipher_to_nss[instance->crypto_cipher_type],
  249. PR_GetError());
  250. goto out;
  251. }
  252. if (PK11_CipherOp(enc_context, outdata,
  253. &tmp1_outlen, FRAME_SIZE_MAX - sizeof(struct crypto_security_header),
  254. (unsigned char *)buf_in, buf_in_len) != SECSuccess) {
  255. log_printf(instance->log_level_security,
  256. "PK11_CipherOp failed (encrypt) crypt_type=%d (err %d)",
  257. (int)cipher_to_nss[instance->crypto_cipher_type],
  258. PR_GetError());
  259. goto out;
  260. }
  261. if (PK11_DigestFinal(enc_context, outdata + tmp1_outlen,
  262. &tmp2_outlen, FRAME_SIZE_MAX - tmp1_outlen) != SECSuccess) {
  263. log_printf(instance->log_level_security,
  264. "PK11_DigestFinal failed (encrypt) crypt_type=%d (err %d)",
  265. (int)cipher_to_nss[instance->crypto_cipher_type],
  266. PR_GetError());
  267. goto out;
  268. }
  269. PK11_DestroyContext(enc_context, PR_TRUE);
  270. *buf_out_len = tmp1_outlen + tmp2_outlen;
  271. only_hash:
  272. if (!hash_to_nss[instance->crypto_hash_type]) {
  273. goto no_hash;
  274. }
  275. /* Now do the digest */
  276. hash_param.type = siBuffer;
  277. hash_param.data = 0;
  278. hash_param.len = 0;
  279. enc_context = PK11_CreateContextBySymKey(hash_to_nss[instance->crypto_hash_type],
  280. CKA_SIGN,
  281. instance->nss_sym_key_sign,
  282. &hash_param);
  283. if (!enc_context) {
  284. log_printf(instance->log_level_security,
  285. "PK11_CreateContext failed (hash) hash_type=%d (err %d)",
  286. (int)hash_to_nss[instance->crypto_hash_type],
  287. PR_GetError());
  288. goto out;
  289. }
  290. if (PK11_DigestBegin(enc_context) != SECSuccess) {
  291. log_printf(instance->log_level_security,
  292. "PK11_DigestBegin failed (hash) hash_type=%d (err %d)",
  293. (int)hash_to_nss[instance->crypto_hash_type],
  294. PR_GetError());
  295. goto out;
  296. }
  297. if (PK11_DigestOp(enc_context,
  298. outdata - SALT_SIZE,
  299. *buf_out_len + SALT_SIZE) != SECSuccess) {
  300. log_printf(instance->log_level_security,
  301. "PK11_DigestOp failed (hash) hash_type=%d (err %d)",
  302. (int)hash_to_nss[instance->crypto_hash_type],
  303. PR_GetError());
  304. goto out;
  305. }
  306. if (PK11_DigestFinal(enc_context,
  307. header->hash_digest,
  308. &tmp2_outlen,
  309. sizeof(header->hash_digest)) != SECSuccess) {
  310. log_printf(instance->log_level_security,
  311. "PK11_DigestFinale failed (hash) hash_type=%d (err %d)",
  312. (int)hash_to_nss[instance->crypto_hash_type],
  313. PR_GetError());
  314. goto out;
  315. }
  316. PK11_DestroyContext(enc_context, PR_TRUE);
  317. no_hash:
  318. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  319. *buf_out_len = *buf_out_len + sizeof(struct crypto_security_header);
  320. return 0;
  321. out:
  322. if (enc_context) {
  323. PK11_DestroyContext(enc_context, PR_TRUE);
  324. }
  325. if (nss_sec_param) {
  326. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  327. }
  328. return -1;
  329. }
  330. static int authenticate_and_decrypt_nss (
  331. struct crypto_instance *instance,
  332. unsigned char *buf,
  333. int *buf_len)
  334. {
  335. PK11Context* enc_context = NULL;
  336. int tmp1_outlen = 0;
  337. unsigned int tmp2_outlen = 0;
  338. unsigned char outbuf[FRAME_SIZE_MAX];
  339. unsigned char digest[CRYPTO_HMAC_HASH_SIZE];
  340. unsigned char *outdata;
  341. int result_len;
  342. unsigned char *data;
  343. size_t datalen;
  344. struct crypto_security_header *header = (struct crypto_security_header *)buf;
  345. SECItem crypt_param;
  346. SECItem hash_param;
  347. datalen = *buf_len;
  348. data = buf + sizeof (struct crypto_security_header) - SALT_SIZE;
  349. datalen = datalen - sizeof (struct crypto_security_header) + SALT_SIZE;
  350. outdata = outbuf + sizeof (struct crypto_security_header);
  351. if (!hash_to_nss[instance->crypto_hash_type]) {
  352. goto only_decrypt;
  353. }
  354. hash_param.type = siBuffer;
  355. hash_param.data = 0;
  356. hash_param.len = 0;
  357. /* Check the digest */
  358. enc_context = PK11_CreateContextBySymKey (hash_to_nss[instance->crypto_hash_type],
  359. CKA_SIGN,
  360. instance->nss_sym_key_sign,
  361. &hash_param);
  362. if (!enc_context) {
  363. log_printf(instance->log_level_security,
  364. "PK11_CreateContext failed (check digest) err %d",
  365. PR_GetError());
  366. goto out;
  367. }
  368. if (PK11_DigestBegin(enc_context) != SECSuccess) {
  369. log_printf(instance->log_level_security,
  370. "PK11_DigestBegin failed (check digest) err %d",
  371. PR_GetError());
  372. goto out;
  373. }
  374. if (PK11_DigestOp(enc_context, data, datalen) != SECSuccess) {
  375. log_printf(instance->log_level_security,
  376. "PK11_DigestOp failed (check digest) err %d",
  377. PR_GetError());
  378. goto out;
  379. }
  380. if (PK11_DigestFinal(enc_context, digest,
  381. &tmp2_outlen, sizeof(digest)) != SECSuccess) {
  382. log_printf(instance->log_level_security,
  383. "PK11_DigestFinal failed (check digest) err %d",
  384. PR_GetError());
  385. goto out;
  386. }
  387. PK11_DestroyContext(enc_context, PR_TRUE);
  388. if (memcmp(digest, header->hash_digest, tmp2_outlen) != 0) {
  389. log_printf(instance->log_level_error, "Digest does not match");
  390. goto out;
  391. }
  392. only_decrypt:
  393. if (!cipher_to_nss[instance->crypto_cipher_type]) {
  394. memcpy(outbuf, buf + sizeof (struct crypto_security_header), *buf_len - sizeof (struct crypto_security_header));
  395. outdata = outbuf;
  396. result_len = *buf_len - sizeof (struct crypto_security_header);
  397. goto no_decrypt;
  398. }
  399. /*
  400. * Get rid of salt
  401. */
  402. data += SALT_SIZE;
  403. datalen -= SALT_SIZE;
  404. /* Create cipher context for decryption */
  405. crypt_param.type = siBuffer;
  406. crypt_param.data = header->salt;
  407. crypt_param.len = SALT_SIZE;
  408. enc_context = PK11_CreateContextBySymKey(cipher_to_nss[instance->crypto_cipher_type],
  409. CKA_DECRYPT,
  410. instance->nss_sym_key, &crypt_param);
  411. if (!enc_context) {
  412. log_printf(instance->log_level_security,
  413. "PK11_CreateContext (decrypt) failed (err %d)",
  414. PR_GetError());
  415. goto out;
  416. }
  417. if (PK11_CipherOp(enc_context, outdata, &tmp1_outlen,
  418. sizeof(outbuf) - sizeof (struct crypto_security_header),
  419. data, datalen) != SECSuccess) {
  420. log_printf(instance->log_level_security,
  421. "PK11_CipherOp (decrypt) failed (err %d)",
  422. PR_GetError());
  423. goto out;
  424. }
  425. if (PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  426. sizeof(outbuf) - tmp1_outlen) != SECSuccess) {
  427. log_printf(instance->log_level_security,
  428. "PK11_DigestFinal (decrypt) failed (err %d)",
  429. PR_GetError());
  430. goto out;
  431. }
  432. PK11_DestroyContext(enc_context, PR_TRUE);
  433. result_len = tmp1_outlen + tmp2_outlen + sizeof (struct crypto_security_header);
  434. no_decrypt:
  435. memset(buf, 0, *buf_len);
  436. memcpy(buf, outdata, result_len);
  437. *buf_len = result_len;
  438. return 0;
  439. out:
  440. if (enc_context) {
  441. PK11_DestroyContext(enc_context, PR_TRUE);
  442. }
  443. return -1;
  444. }
  445. static int string_to_crypto_cipher_type(const char* crypto_cipher_type)
  446. {
  447. if (strcmp(crypto_cipher_type, "none") == 0) {
  448. return CRYPTO_CIPHER_TYPE_NONE;
  449. } else if (strcmp(crypto_cipher_type, "aes256") == 0) {
  450. return CRYPTO_CIPHER_TYPE_AES256;
  451. }
  452. return CRYPTO_CIPHER_TYPE_NONE;
  453. }
  454. static int string_to_crypto_hash_type(const char* crypto_hash_type)
  455. {
  456. if (strcmp(crypto_hash_type, "none") == 0) {
  457. return CRYPTO_HASH_TYPE_NONE;
  458. } else if (strcmp(crypto_hash_type, "sha1") == 0) {
  459. return CRYPTO_HASH_TYPE_SHA1;
  460. }
  461. return CRYPTO_HASH_TYPE_NONE;
  462. }
  463. size_t crypto_sec_header_size(
  464. const char *crypto_cipher_type,
  465. const char *crypto_hash_type)
  466. {
  467. int crypto_cipher = string_to_crypto_cipher_type(crypto_cipher_type);
  468. int crypto_hash = string_to_crypto_hash_type(crypto_hash_type);
  469. if ((!crypto_cipher) && (!crypto_hash)) {
  470. return 2;
  471. }
  472. /*
  473. * TODO: crypto_cipher_type determines the crypto BLOCK size
  474. * crypto_hash_type determines the HASH_SIZE
  475. */
  476. return sizeof(struct crypto_security_header);
  477. }
  478. int crypto_encrypt_and_sign (
  479. struct crypto_instance *instance,
  480. const unsigned char *buf_in,
  481. const size_t buf_in_len,
  482. unsigned char *buf_out,
  483. size_t *buf_out_len)
  484. {
  485. int err = 0;
  486. /*
  487. * if crypto is totally disabled, let's skip complex parsing
  488. */
  489. if ((!cipher_to_nss[instance->crypto_cipher_type]) &&
  490. (!hash_to_nss[instance->crypto_hash_type])) {
  491. memcpy(buf_out, buf_in, buf_in_len);
  492. *buf_out_len = buf_in_len;
  493. err = 0;
  494. } else {
  495. err = encrypt_and_sign_nss(instance,
  496. buf_in, buf_in_len,
  497. buf_out, buf_out_len);
  498. }
  499. /*
  500. * Add 2 bytes to the tail of each packet to
  501. * propagate crypto info for this packet.
  502. */
  503. if (!err) {
  504. size_t out_len = *buf_out_len;
  505. struct crypto_config_header *cch;
  506. cch = (struct crypto_config_header *)&buf_out[*buf_out_len];
  507. cch->crypto_cipher_type = instance->crypto_cipher_type;
  508. cch->crypto_hash_type = instance->crypto_hash_type;
  509. *buf_out_len = *buf_out_len + 2;
  510. }
  511. return err;
  512. }
  513. int crypto_authenticate_and_decrypt (struct crypto_instance *instance,
  514. unsigned char *buf,
  515. int *buf_len)
  516. {
  517. int err = 0;
  518. struct crypto_config_header *cch;
  519. cch = (struct crypto_config_header *)&buf[*buf_len - 2];
  520. /*
  521. * decode crypto config of incoming packets
  522. */
  523. if (cch->crypto_cipher_type != instance->crypto_cipher_type) {
  524. log_printf(instance->log_level_security,
  525. "Incoming packet has different crypto type. Rejecting");
  526. return -1;
  527. }
  528. if (cch->crypto_hash_type != instance->crypto_hash_type) {
  529. log_printf(instance->log_level_security,
  530. "Incoming packet has different hash type. Rejecting");
  531. return -1;
  532. }
  533. /*
  534. * if crypto is totally disabled, there is no work for us
  535. */
  536. if ((!cipher_to_nss[instance->crypto_cipher_type]) &&
  537. (!hash_to_nss[instance->crypto_hash_type])) {
  538. *buf_len = *buf_len - 2;
  539. err = 0;
  540. } else {
  541. *buf_len = *buf_len - 2;
  542. err = authenticate_and_decrypt_nss(instance, buf, buf_len);
  543. }
  544. return err;
  545. }
  546. struct crypto_instance *crypto_init(
  547. const unsigned char *private_key,
  548. unsigned int private_key_len,
  549. const char *crypto_cipher_type,
  550. const char *crypto_hash_type,
  551. void (*log_printf_func) (
  552. int level,
  553. int subsys,
  554. const char *function,
  555. const char *file,
  556. int line,
  557. const char *format,
  558. ...)__attribute__((format(printf, 6, 7))),
  559. int log_level_security,
  560. int log_level_notice,
  561. int log_level_error,
  562. int log_subsys_id)
  563. {
  564. struct crypto_instance *instance;
  565. instance = malloc(sizeof(*instance));
  566. if (instance == NULL) {
  567. return (NULL);
  568. }
  569. memset(instance, 0, sizeof(struct crypto_instance));
  570. memcpy(instance->private_key, private_key, private_key_len);
  571. instance->private_key_len = private_key_len;
  572. instance->crypto_cipher_type = string_to_crypto_cipher_type(crypto_cipher_type);
  573. instance->crypto_hash_type = string_to_crypto_hash_type(crypto_hash_type);
  574. instance->log_printf_func = log_printf_func;
  575. instance->log_level_security = log_level_security;
  576. instance->log_level_notice = log_level_notice;
  577. instance->log_level_error = log_level_error;
  578. instance->log_subsys_id = log_subsys_id;
  579. if (init_nss_crypto(instance, crypto_cipher_type, crypto_hash_type) < 0) {
  580. free(instance);
  581. return(NULL);
  582. }
  583. return (instance);
  584. }