identification.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Copyright (C) 2009-2015 Tobias Brunner
  3. * Copyright (C) 2005-2009 Martin Willi
  4. * Copyright (C) 2005 Jan Hutter
  5. * HSR Hochschule fuer Technik Rapperswil
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. */
  17. /**
  18. * @defgroup identification identification
  19. * @{ @ingroup utils
  20. */
  21. #ifndef IDENTIFICATION_H_
  22. #define IDENTIFICATION_H_
  23. typedef enum id_type_t id_type_t;
  24. typedef struct identification_t identification_t;
  25. typedef enum id_match_t id_match_t;
  26. typedef enum id_part_t id_part_t;
  27. #include <utils/chunk.h>
  28. #include <collections/enumerator.h>
  29. /**
  30. * Matches returned from identification_t.match
  31. */
  32. enum id_match_t {
  33. /* no match */
  34. ID_MATCH_NONE = 0,
  35. /* match to %any ID */
  36. ID_MATCH_ANY = 1,
  37. /* match with maximum allowed wildcards */
  38. ID_MATCH_MAX_WILDCARDS = 2,
  39. /* match with only one wildcard */
  40. ID_MATCH_ONE_WILDCARD = 19,
  41. /* perfect match, won't get better */
  42. ID_MATCH_PERFECT = 20,
  43. };
  44. /**
  45. * enum names for id_match_t.
  46. */
  47. extern enum_name_t *id_match_names;
  48. /**
  49. * ID Types in a ID payload.
  50. */
  51. enum id_type_t {
  52. /**
  53. * private type which matches any other id.
  54. */
  55. ID_ANY = 0,
  56. /**
  57. * ID data is a single four (4) octet IPv4 address.
  58. */
  59. ID_IPV4_ADDR = 1,
  60. /**
  61. * ID data is a fully-qualified domain name string.
  62. * An example of a ID_FQDN is "example.com".
  63. * The string MUST not contain any terminators (e.g., NULL, CR, etc.).
  64. */
  65. ID_FQDN = 2,
  66. /**
  67. * ID data is a fully-qualified RFC822 email address string.
  68. * An example of an ID_RFC822_ADDR is "jsmith@example.com".
  69. * The string MUST NOT contain any terminators.
  70. */
  71. ID_USER_FQDN = 3, /* IKEv1 only */
  72. ID_RFC822_ADDR = 3, /* IKEv2 only */
  73. /**
  74. * ID data is an IPv4 subnet (IKEv1 only)
  75. */
  76. ID_IPV4_ADDR_SUBNET = 4,
  77. /**
  78. * ID data is a single sixteen (16) octet IPv6 address.
  79. */
  80. ID_IPV6_ADDR = 5,
  81. /**
  82. * ID data is an IPv6 subnet (IKEv1 only)
  83. */
  84. ID_IPV6_ADDR_SUBNET = 6,
  85. /**
  86. * ID data is an IPv4 address range (IKEv1 only)
  87. */
  88. ID_IPV4_ADDR_RANGE = 7,
  89. /**
  90. * ID data is an IPv6 address range (IKEv1 only)
  91. */
  92. ID_IPV6_ADDR_RANGE = 8,
  93. /**
  94. * ID data is the binary DER encoding of an ASN.1 X.501 Distinguished Name
  95. */
  96. ID_DER_ASN1_DN = 9,
  97. /**
  98. * ID data is the binary DER encoding of an ASN.1 X.509 GeneralName
  99. */
  100. ID_DER_ASN1_GN = 10,
  101. /**
  102. * ID data is an opaque octet stream which may be used to pass vendor-
  103. * specific information necessary to do certain proprietary
  104. * types of identification.
  105. */
  106. ID_KEY_ID = 11,
  107. /**
  108. * Private ID type which represents a GeneralName of type URI
  109. */
  110. ID_DER_ASN1_GN_URI = 201,
  111. };
  112. /**
  113. * enum names for id_type_t.
  114. */
  115. extern enum_name_t *id_type_names;
  116. /**
  117. * Type of an ID sub part.
  118. */
  119. enum id_part_t {
  120. /** Username part of an RFC822_ADDR */
  121. ID_PART_USERNAME,
  122. /** Domain part of an RFC822_ADDR */
  123. ID_PART_DOMAIN,
  124. /** Top-Level domain of a FQDN */
  125. ID_PART_TLD,
  126. /** Second-Level domain of a FQDN */
  127. ID_PART_SLD,
  128. /** Another Level domain of a FQDN */
  129. ID_PART_ALD,
  130. /** Country RDN of a DN */
  131. ID_PART_RDN_C,
  132. /** CommonName RDN of a DN */
  133. ID_PART_RDN_CN,
  134. /** Description RDN of a DN */
  135. ID_PART_RDN_D,
  136. /** Email RDN of a DN */
  137. ID_PART_RDN_E,
  138. /** EmployeeNumber RDN of a DN */
  139. ID_PART_RDN_EN,
  140. /** GivenName RDN of a DN */
  141. ID_PART_RDN_G,
  142. /** Initials RDN of a DN */
  143. ID_PART_RDN_I,
  144. /** DN Qualifier RDN of a DN */
  145. ID_PART_RDN_DNQ,
  146. /** dmdName RDN of a DN */
  147. ID_PART_RDN_DMDN,
  148. /** Pseudonym RDN of a DN */
  149. ID_PART_RDN_PN,
  150. /** UniqueIdentifier RDN of a DN */
  151. ID_PART_RDN_ID,
  152. /** Locality RDN of a DN */
  153. ID_PART_RDN_L,
  154. /** Name RDN of a DN */
  155. ID_PART_RDN_N,
  156. /** Organization RDN of a DN */
  157. ID_PART_RDN_O,
  158. /** OrganizationUnit RDN of a DN */
  159. ID_PART_RDN_OU,
  160. /** Surname RDN of a DN */
  161. ID_PART_RDN_S,
  162. /** SerialNumber RDN of a DN */
  163. ID_PART_RDN_SN,
  164. /** StateOrProvince RDN of a DN */
  165. ID_PART_RDN_ST,
  166. /** Title RDN of a DN */
  167. ID_PART_RDN_T,
  168. };
  169. /**
  170. * Generic identification, such as used in ID payload.
  171. *
  172. * @todo Support for ID_DER_ASN1_GN is minimal right now. Comparison
  173. * between them and ID_IPV4_ADDR/RFC822_ADDR would be nice.
  174. */
  175. struct identification_t {
  176. /**
  177. * Get the encoding of this id, to send over
  178. * the network.
  179. *
  180. * Result points to internal data, do not free.
  181. *
  182. * @return a chunk containing the encoded bytes
  183. */
  184. chunk_t (*get_encoding) (identification_t *this);
  185. /**
  186. * Get the type of this identification.
  187. *
  188. * @return id_type_t
  189. */
  190. id_type_t (*get_type) (identification_t *this);
  191. /**
  192. * Create a hash value for this identification_t object.
  193. *
  194. * @param inc optional value for incremental hashing
  195. * @return hash value
  196. */
  197. u_int (*hash) (identification_t *this, u_int inc);
  198. /**
  199. * Check if two identification_t objects are equal.
  200. *
  201. * @param other other identification_t object
  202. * @return TRUE if the IDs are equal
  203. */
  204. bool (*equals) (identification_t *this, identification_t *other);
  205. /**
  206. * Check if an ID matches a wildcard ID.
  207. *
  208. * An identification_t may contain wildcards, such as
  209. * *.strongswan.org. This call checks if a given ID
  210. * (e.g. tester.strongswan.org) belongs to a such wildcard
  211. * ID. Returns > 0 if
  212. * - IDs are identical
  213. * - other is of type ID_ANY
  214. * - other contains a wildcard and matches this
  215. *
  216. * The larger the return value is, the better is the match. Zero means
  217. * no match at all, 1 means a bad match, and 2 a slightly better match.
  218. *
  219. * @param other the ID containing one or more wildcards
  220. * @return match value as described above
  221. */
  222. id_match_t (*matches) (identification_t *this, identification_t *other);
  223. /**
  224. * Check if an ID is a wildcard ID.
  225. *
  226. * If the ID represents multiple IDs (with wildcards, or
  227. * as the type ID_ANY), TRUE is returned. If it is unique,
  228. * FALSE is returned.
  229. *
  230. * @return TRUE if ID contains wildcards
  231. */
  232. bool (*contains_wildcards) (identification_t *this);
  233. /**
  234. * Create an enumerator over subparts of an identity.
  235. *
  236. * Some identities are built from several parts, e.g. an E-Mail consists
  237. * of a username and a domain part, or a DistinguishedName contains several
  238. * RDNs.
  239. * For identity without subtypes (support), an empty enumerator is
  240. * returned.
  241. *
  242. * @return an enumerator over (id_part_t type, chunk_t data)
  243. */
  244. enumerator_t* (*create_part_enumerator)(identification_t *this);
  245. /**
  246. * Clone a identification_t instance.
  247. *
  248. * @return clone of this
  249. */
  250. identification_t *(*clone) (identification_t *this);
  251. /**
  252. * Destroys a identification_t object.
  253. */
  254. void (*destroy) (identification_t *this);
  255. };
  256. /**
  257. * Creates an identification_t object from a string.
  258. *
  259. * The input string may be e.g. one of the following:
  260. * - ID_IPV4_ADDR: 192.168.0.1
  261. * - ID_IPV6_ADDR: 2001:0db8:85a3:08d3:1319:8a2e:0370:7345
  262. * - ID_FQDN: www.strongswan.org (optionally with a prepended @)
  263. * - ID_RFC822_ADDR: alice@wonderland.org
  264. * - ID_DER_ASN1_DN: C=CH, O=Linux strongSwan, CN=bob
  265. *
  266. * In favour of pluto, domainnames are prepended with an @, since
  267. * pluto resolves domainnames without an @ to IPv4 addresses. Since
  268. * we use a separate host_t class for addresses, this doesn't
  269. * make sense for us.
  270. *
  271. * A distinguished name may contain one or more of the following RDNs:
  272. * ND, UID, DC, CN, S, SN, serialNumber, C, L, ST, O, OU, T, D,
  273. * N, G, I, dnQualifier, ID, EN, EmployeeNumber, E, Email, emailAddress, UN,
  274. * unstructuredName, TCGID.
  275. *
  276. * To skip automatic type detection the following prefixes may be used to
  277. * enforce a specific type: ipv4:, ipv6:, rfc822:, email:, userfqdn:, fqdn:,
  278. * dns:, asn1dn:, asn1gn: and keyid:. If a # follows the :, the remaining data
  279. * is interpreted as hex encoded binary data for that ID, otherwise the raw
  280. * string following the prefix is used as identity data, without conversion.
  281. * To specify a non-standard ID type, the numerical type may be prefixed
  282. * between curly backets, building a prefix. For instance the "{1}:" prefix
  283. * defines an ID_IPV4_ADDR type.
  284. *
  285. * This constructor never returns NULL. If it does not find a suitable
  286. * conversion function, it will copy the string to an ID_KEY_ID.
  287. *
  288. * @param string input string, which will be converted
  289. * @return identification_t
  290. */
  291. identification_t * identification_create_from_string(char *string);
  292. /**
  293. * Creates an identification from a chunk of data, guessing its type.
  294. *
  295. * @param data identification data
  296. * @return identification_t
  297. */
  298. identification_t * identification_create_from_data(chunk_t data);
  299. /**
  300. * Creates an identification_t object from an encoded chunk.
  301. *
  302. * @param type type of this id, such as ID_IPV4_ADDR
  303. * @param encoded encoded bytes, such as from identification_t.get_encoding
  304. * @return identification_t
  305. */
  306. identification_t * identification_create_from_encoding(id_type_t type, chunk_t encoded);
  307. /**
  308. * Creates an identification_t object from a sockaddr struct
  309. *
  310. * @param sockaddr sockaddr struct which contains family and address
  311. * @return identification_t
  312. */
  313. identification_t * identification_create_from_sockaddr(sockaddr_t *sockaddr);
  314. /**
  315. * printf hook function for identification_t.
  316. *
  317. * Arguments are:
  318. * identification_t *identification
  319. */
  320. int identification_printf_hook(printf_hook_data_t *data,
  321. printf_hook_spec_t *spec, const void *const *args);
  322. #endif /** IDENTIFICATION_H_ @}*/