seg_contract.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * Copyright (C) 2014-2015 Andreas Steffen
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include "seg_contract.h"
  16. #include "seg_env.h"
  17. #include "ietf/ietf_attr_pa_tnc_error.h"
  18. #include "tcg/seg/tcg_seg_attr_seg_env.h"
  19. #include <utils/debug.h>
  20. #include <bio/bio_writer.h>
  21. #include <tncif_pa_subtypes.h>
  22. typedef struct private_seg_contract_t private_seg_contract_t;
  23. /**
  24. * Private data of a seg_contract_t object.
  25. */
  26. struct private_seg_contract_t {
  27. /**
  28. * Public seg_contract_t interface.
  29. */
  30. seg_contract_t public;
  31. /**
  32. * PA-TNC message type
  33. */
  34. pen_type_t msg_type;
  35. /**
  36. * Maximum PA-TNC attribute size
  37. */
  38. uint32_t max_attr_size;
  39. /**
  40. * Maximum PA-TNC attribute segment size
  41. */
  42. uint32_t max_seg_size;
  43. /**
  44. * Maximum PA-TNC attribute segment size
  45. */
  46. uint32_t last_base_attr_id;
  47. /**
  48. * List of attribute segment envelopes
  49. */
  50. linked_list_t *seg_envs;
  51. /**
  52. * Is this a null contract?
  53. */
  54. bool is_null;
  55. /**
  56. * Contract role
  57. */
  58. bool is_issuer;
  59. /**
  60. * Issuer ID (either IMV or IMC ID)
  61. */
  62. TNC_UInt32 issuer_id;
  63. /**
  64. * Responder ID (either IMC or IMV ID)
  65. */
  66. TNC_UInt32 responder_id;
  67. /**
  68. * IMC/IMV role
  69. */
  70. bool is_imc;
  71. };
  72. METHOD(seg_contract_t, get_msg_type, pen_type_t,
  73. private_seg_contract_t *this)
  74. {
  75. return this->msg_type;
  76. }
  77. METHOD(seg_contract_t, set_max_size, void,
  78. private_seg_contract_t *this, uint32_t max_attr_size, uint32_t max_seg_size)
  79. {
  80. this->max_attr_size = max_attr_size;
  81. this->max_seg_size = max_seg_size;
  82. this->is_null = max_attr_size == SEG_CONTRACT_MAX_SIZE_VALUE &&
  83. max_seg_size == SEG_CONTRACT_MAX_SIZE_VALUE;
  84. }
  85. METHOD(seg_contract_t, get_max_size, void,
  86. private_seg_contract_t *this, uint32_t *max_attr_size, uint32_t *max_seg_size)
  87. {
  88. if (max_attr_size)
  89. {
  90. *max_attr_size = this->max_attr_size;
  91. }
  92. if (max_seg_size)
  93. {
  94. *max_seg_size = this->max_seg_size;
  95. }
  96. }
  97. METHOD(seg_contract_t, check_size, bool,
  98. private_seg_contract_t *this, pa_tnc_attr_t *attr, bool *oversize)
  99. {
  100. chunk_t attr_value;
  101. size_t attr_len;
  102. *oversize = FALSE;
  103. if (this->is_null)
  104. {
  105. /* null segmentation contract */
  106. return FALSE;
  107. }
  108. attr->build(attr);
  109. attr_value = attr->get_value(attr);
  110. attr_len = PA_TNC_ATTR_HEADER_SIZE + attr_value.len;
  111. if (attr_len > this->max_attr_size)
  112. {
  113. /* oversize attribute */
  114. *oversize = TRUE;
  115. return FALSE;
  116. }
  117. if (this->max_seg_size == SEG_CONTRACT_NO_FRAGMENTATION)
  118. {
  119. /* no fragmentation wanted */
  120. return FALSE;
  121. }
  122. return attr_value.len > this->max_seg_size + TCG_SEG_ATTR_SEG_ENV_HEADER;
  123. }
  124. METHOD(seg_contract_t, first_segment, pa_tnc_attr_t*,
  125. private_seg_contract_t *this, pa_tnc_attr_t *attr, size_t max_attr_len)
  126. {
  127. seg_env_t *seg_env;
  128. seg_env = seg_env_create(++this->last_base_attr_id, attr,
  129. this->max_seg_size);
  130. if (!seg_env)
  131. {
  132. return NULL;
  133. }
  134. this->seg_envs->insert_last(this->seg_envs, seg_env);
  135. return seg_env->first_segment(seg_env, max_attr_len);
  136. }
  137. METHOD(seg_contract_t, next_segment, pa_tnc_attr_t*,
  138. private_seg_contract_t *this, uint32_t base_attr_id)
  139. {
  140. pa_tnc_attr_t *seg_env_attr = NULL;
  141. seg_env_t *seg_env;
  142. bool last_segment = FALSE;
  143. enumerator_t *enumerator;
  144. enumerator = this->seg_envs->create_enumerator(this->seg_envs);
  145. while (enumerator->enumerate(enumerator, &seg_env))
  146. {
  147. if (seg_env->get_base_attr_id(seg_env) == base_attr_id)
  148. {
  149. seg_env_attr = seg_env->next_segment(seg_env, &last_segment);
  150. if (!seg_env_attr)
  151. {
  152. break;
  153. }
  154. if (last_segment)
  155. {
  156. this->seg_envs->remove_at(this->seg_envs, enumerator);
  157. seg_env->destroy(seg_env);
  158. }
  159. break;
  160. }
  161. }
  162. enumerator->destroy(enumerator);
  163. return seg_env_attr;
  164. }
  165. METHOD(seg_contract_t, add_segment, pa_tnc_attr_t*,
  166. private_seg_contract_t *this, pa_tnc_attr_t *attr, pa_tnc_attr_t **error,
  167. bool *more)
  168. {
  169. tcg_seg_attr_seg_env_t *seg_env_attr;
  170. seg_env_t *current, *seg_env = NULL;
  171. pa_tnc_attr_t *base_attr;
  172. pen_type_t error_code;
  173. uint32_t base_attr_id;
  174. uint8_t flags;
  175. chunk_t segment_data, msg_info;
  176. enumerator_t *enumerator;
  177. seg_env_attr = (tcg_seg_attr_seg_env_t*)attr;
  178. base_attr_id = seg_env_attr->get_base_attr_id(seg_env_attr);
  179. segment_data = seg_env_attr->get_segment(seg_env_attr, &flags);
  180. *more = flags & SEG_ENV_FLAG_MORE;
  181. *error = NULL;
  182. enumerator = this->seg_envs->create_enumerator(this->seg_envs);
  183. while (enumerator->enumerate(enumerator, &current))
  184. {
  185. if (current->get_base_attr_id(current) == base_attr_id)
  186. {
  187. seg_env = current;
  188. this->seg_envs->remove_at(this->seg_envs, enumerator);
  189. break;
  190. }
  191. }
  192. enumerator->destroy(enumerator);
  193. if (flags & SEG_ENV_FLAG_START)
  194. {
  195. if (seg_env)
  196. {
  197. DBG1(DBG_TNC, "base attribute ID %d is already in use",
  198. base_attr_id);
  199. this->seg_envs->insert_last(this->seg_envs, seg_env);
  200. return NULL;
  201. }
  202. DBG2(DBG_TNC, "received first segment for base attribute ID %d "
  203. "(%d bytes)", base_attr_id, segment_data.len);
  204. seg_env = seg_env_create_from_data(base_attr_id, segment_data,
  205. this->max_seg_size, error);
  206. if (!seg_env)
  207. {
  208. return NULL;
  209. }
  210. }
  211. else
  212. {
  213. if (!seg_env)
  214. {
  215. DBG1(DBG_TNC, "base attribute ID %d not found", base_attr_id);
  216. return NULL;
  217. }
  218. DBG2(DBG_TNC, "received %s segment for base attribute ID %d "
  219. "(%d bytes)", (*more) ? "next" : "last", base_attr_id,
  220. segment_data.len);
  221. if (!seg_env->add_segment(seg_env, segment_data, error))
  222. {
  223. seg_env->destroy(seg_env);
  224. return NULL;
  225. }
  226. }
  227. base_attr = seg_env->get_base_attr(seg_env);
  228. if (*more)
  229. {
  230. /* reinsert into list since more segments are to come */
  231. this->seg_envs->insert_last(this->seg_envs, seg_env);
  232. }
  233. else
  234. {
  235. /* added the last segment */
  236. if (!base_attr)
  237. {
  238. /* base attribute waits for more data */
  239. DBG1(DBG_TNC, "insufficient bytes for PA-TNC attribute value");
  240. msg_info = seg_env->get_base_attr_info(seg_env);
  241. error_code = pen_type_create(PEN_IETF, PA_ERROR_INVALID_PARAMETER);
  242. *error = ietf_attr_pa_tnc_error_create_with_offset(error_code,
  243. msg_info, PA_TNC_ATTR_INFO_SIZE);
  244. }
  245. seg_env->destroy(seg_env);
  246. }
  247. return base_attr;
  248. }
  249. METHOD(seg_contract_t, is_issuer, bool,
  250. private_seg_contract_t *this)
  251. {
  252. return this->is_issuer;
  253. }
  254. METHOD(seg_contract_t, is_null, bool,
  255. private_seg_contract_t *this)
  256. {
  257. return this->is_null;
  258. }
  259. METHOD(seg_contract_t, set_responder, void,
  260. private_seg_contract_t *this, TNC_UInt32 responder_id)
  261. {
  262. this->responder_id = responder_id;
  263. }
  264. METHOD(seg_contract_t, get_responder, TNC_UInt32,
  265. private_seg_contract_t *this)
  266. {
  267. return this->responder_id;
  268. }
  269. METHOD(seg_contract_t, get_issuer, TNC_UInt32,
  270. private_seg_contract_t *this)
  271. {
  272. return this->issuer_id;
  273. }
  274. METHOD(seg_contract_t, clone_, seg_contract_t*,
  275. private_seg_contract_t *this)
  276. {
  277. private_seg_contract_t *clone;
  278. clone = malloc_thing(private_seg_contract_t);
  279. memcpy(clone, this, sizeof(private_seg_contract_t));
  280. clone->seg_envs = linked_list_create();
  281. return &clone->public;
  282. }
  283. METHOD(seg_contract_t, get_info_string, void,
  284. private_seg_contract_t *this, char *buf, size_t len, bool request)
  285. {
  286. enum_name_t *pa_subtype_names;
  287. uint32_t msg_vid, msg_subtype;
  288. char *pos = buf;
  289. int written;
  290. /* nul-terminate the string buffer */
  291. buf[--len] = '\0';
  292. if (this->is_issuer && request)
  293. {
  294. written = snprintf(pos, len, "%s %lu requests",
  295. this->is_imc ? "IMC" : "IMV", this->issuer_id);
  296. }
  297. else
  298. {
  299. written = snprintf(pos, len, "%s %lu received",
  300. this->is_imc ? "IMC" : "IMV",
  301. this->is_issuer ? this->issuer_id :
  302. this->responder_id);
  303. }
  304. if (written < 0 || written > len)
  305. {
  306. return;
  307. }
  308. pos += written;
  309. len -= written;
  310. written = snprintf(pos, len, " a %ssegmentation contract%s ",
  311. this->is_null ? "null" : "", request ?
  312. (this->is_issuer ? "" : " request") : " response");
  313. if (written < 0 || written > len)
  314. {
  315. return;
  316. }
  317. pos += written;
  318. len -= written;
  319. if ((!this->is_issuer && this->issuer_id != TNC_IMVID_ANY) ||
  320. ( this->is_issuer && this->responder_id != TNC_IMVID_ANY))
  321. {
  322. written = snprintf(pos, len, "from %s %lu ",
  323. this->is_imc ? "IMV" : "IMC",
  324. this->is_issuer ? this->responder_id :
  325. this->issuer_id);
  326. if (written < 0 || written > len)
  327. {
  328. return;
  329. }
  330. pos += written;
  331. len -= written;
  332. }
  333. msg_vid = this->msg_type.vendor_id;
  334. msg_subtype = this->msg_type.type;
  335. pa_subtype_names = get_pa_subtype_names(msg_vid);
  336. if (pa_subtype_names)
  337. {
  338. written = snprintf(pos, len, "for PA message type '%N/%N' "
  339. "0x%06x/0x%08x", pen_names, msg_vid,
  340. pa_subtype_names, msg_subtype, msg_vid,
  341. msg_subtype);
  342. }
  343. else
  344. {
  345. written = snprintf(pos, len, "for PA message type '%N' "
  346. "0x%06x/0x%08x", pen_names, msg_vid,
  347. msg_vid, msg_subtype);
  348. }
  349. if (written < 0 || written > len)
  350. {
  351. return;
  352. }
  353. pos += written;
  354. len -= written;
  355. if (!this->is_null)
  356. {
  357. written = snprintf(pos, len, "\n maximum attribute size of %u bytes "
  358. "with ", this->max_attr_size);
  359. if (written < 0 || written > len)
  360. {
  361. return;
  362. }
  363. pos += written;
  364. len -= written;
  365. if (this->max_seg_size == SEG_CONTRACT_MAX_SIZE_VALUE)
  366. {
  367. written = snprintf(pos, len, "no segmentation");
  368. }
  369. else
  370. {
  371. written = snprintf(pos, len, "maximum segment size of %u bytes",
  372. this->max_seg_size);
  373. }
  374. }
  375. }
  376. METHOD(seg_contract_t, destroy, void,
  377. private_seg_contract_t *this)
  378. {
  379. this->seg_envs->destroy_offset(this->seg_envs, offsetof(seg_env_t, destroy));
  380. free(this);
  381. }
  382. /**
  383. * See header
  384. */
  385. seg_contract_t *seg_contract_create(pen_type_t msg_type,
  386. uint32_t max_attr_size,
  387. uint32_t max_seg_size,
  388. bool is_issuer, TNC_UInt32 issuer_id,
  389. bool is_imc)
  390. {
  391. private_seg_contract_t *this;
  392. INIT(this,
  393. .public = {
  394. .get_msg_type = _get_msg_type,
  395. .set_max_size = _set_max_size,
  396. .get_max_size = _get_max_size,
  397. .check_size = _check_size,
  398. .first_segment = _first_segment,
  399. .next_segment = _next_segment,
  400. .add_segment = _add_segment,
  401. .is_issuer = _is_issuer,
  402. .is_null = _is_null,
  403. .set_responder = _set_responder,
  404. .get_responder = _get_responder,
  405. .get_issuer = _get_issuer,
  406. .clone = _clone_,
  407. .get_info_string = _get_info_string,
  408. .destroy = _destroy,
  409. },
  410. .msg_type = msg_type,
  411. .max_attr_size = max_attr_size,
  412. .max_seg_size = max_seg_size,
  413. .seg_envs = linked_list_create(),
  414. .is_issuer = is_issuer,
  415. .issuer_id = issuer_id,
  416. .responder_id = is_imc ? TNC_IMVID_ANY : TNC_IMCID_ANY,
  417. .is_imc = is_imc,
  418. .is_null = max_attr_size == SEG_CONTRACT_MAX_SIZE_VALUE &&
  419. max_seg_size == SEG_CONTRACT_MAX_SIZE_VALUE,
  420. );
  421. return &this->public;
  422. }