4
0

totemcrypto.c 20 KB

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