dn.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. /*
  2. * Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation
  3. *
  4. * Author: Nikos Mavrogiannopoulos
  5. *
  6. * This file is part of GNUTLS.
  7. *
  8. * The GNUTLS library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  21. * USA
  22. *
  23. */
  24. #include <gnutls_int.h>
  25. #include <libtasn1.h>
  26. #include <gnutls_datum.h>
  27. #include <gnutls_global.h>
  28. #include <gnutls_errors.h>
  29. #include <gnutls_str.h>
  30. #include <common.h>
  31. #include <gnutls_num.h>
  32. #include <dn.h>
  33. /* This file includes all the required to parse an X.509 Distriguished
  34. * Name (you need a parser just to read a name in the X.509 protoocols!!!)
  35. */
  36. /* Converts the given OID to an ldap acceptable string or
  37. * a dotted OID.
  38. */
  39. static const char *
  40. oid2ldap_string (const char *oid)
  41. {
  42. const char *ret;
  43. ret = MHD__gnutls_x509_oid2ldap_string (oid);
  44. if (ret)
  45. return ret;
  46. /* else return the OID in dotted format */
  47. return oid;
  48. }
  49. /* Escapes a string following the rules from RFC2253.
  50. */
  51. static char *
  52. str_escape (char *str, char *buffer, unsigned int buffer_size)
  53. {
  54. int str_length, j, i;
  55. if (str == NULL || buffer == NULL)
  56. return NULL;
  57. str_length = MIN (strlen (str), buffer_size - 1);
  58. for (i = j = 0; i < str_length; i++)
  59. {
  60. if (str[i] == ',' || str[i] == '+' || str[i] == '"'
  61. || str[i] == '\\' || str[i] == '<' || str[i] == '>'
  62. || str[i] == ';')
  63. buffer[j++] = '\\';
  64. buffer[j++] = str[i];
  65. }
  66. /* null terminate the string */
  67. buffer[j] = 0;
  68. return buffer;
  69. }
  70. /* Parses an X509 DN in the MHD__asn1_struct, and puts the output into
  71. * the string buf. The output is an LDAP encoded DN.
  72. *
  73. * MHD__asn1_rdn_name must be a string in the form "tbsCertificate.issuer.rdnSequence".
  74. * That is to point in the rndSequence.
  75. */
  76. int
  77. MHD__gnutls_x509_parse_dn (ASN1_TYPE MHD__asn1_struct,
  78. const char *MHD__asn1_rdn_name, char *buf,
  79. size_t * sizeof_buf)
  80. {
  81. MHD_gtls_string out_str;
  82. int k2, k1, result;
  83. char tmpbuffer1[MAX_NAME_SIZE];
  84. char tmpbuffer2[MAX_NAME_SIZE];
  85. char tmpbuffer3[MAX_NAME_SIZE];
  86. opaque value[MAX_STRING_LEN], *value2 = NULL;
  87. char *escaped = NULL;
  88. const char *ldap_desc;
  89. char oid[128];
  90. int len, printable;
  91. char *string = NULL;
  92. size_t sizeof_string, sizeof_escaped;
  93. if (sizeof_buf == NULL)
  94. {
  95. MHD_gnutls_assert ();
  96. return GNUTLS_E_INVALID_REQUEST;
  97. }
  98. if (*sizeof_buf > 0 && buf)
  99. buf[0] = 0;
  100. else
  101. *sizeof_buf = 0;
  102. MHD_gtls_string_init (&out_str, MHD_gnutls_malloc, MHD_gnutls_realloc,
  103. MHD_gnutls_free);
  104. k1 = 0;
  105. do
  106. {
  107. k1++;
  108. /* create a string like "tbsCertList.issuer.rdnSequence.?1"
  109. */
  110. if (MHD__asn1_rdn_name[0] != 0)
  111. snprintf (tmpbuffer1, sizeof (tmpbuffer1), "%s.?%u",
  112. MHD__asn1_rdn_name, k1);
  113. else
  114. snprintf (tmpbuffer1, sizeof (tmpbuffer1), "?%u", k1);
  115. len = sizeof (value) - 1;
  116. result =
  117. MHD__asn1_read_value (MHD__asn1_struct, tmpbuffer1, value, &len);
  118. if (result == ASN1_ELEMENT_NOT_FOUND)
  119. {
  120. break;
  121. }
  122. if (result != ASN1_VALUE_NOT_FOUND)
  123. {
  124. MHD_gnutls_assert ();
  125. result = MHD_gtls_asn2err (result);
  126. goto cleanup;
  127. }
  128. k2 = 0;
  129. do
  130. { /* Move to the attibute type and values
  131. */
  132. k2++;
  133. if (tmpbuffer1[0] != 0)
  134. snprintf (tmpbuffer2, sizeof (tmpbuffer2), "%s.?%u", tmpbuffer1,
  135. k2);
  136. else
  137. snprintf (tmpbuffer2, sizeof (tmpbuffer2), "?%u", k2);
  138. /* Try to read the RelativeDistinguishedName attributes.
  139. */
  140. len = sizeof (value) - 1;
  141. result =
  142. MHD__asn1_read_value (MHD__asn1_struct, tmpbuffer2, value, &len);
  143. if (result == ASN1_ELEMENT_NOT_FOUND)
  144. break;
  145. if (result != ASN1_VALUE_NOT_FOUND)
  146. {
  147. MHD_gnutls_assert ();
  148. result = MHD_gtls_asn2err (result);
  149. goto cleanup;
  150. }
  151. /* Read the OID
  152. */
  153. MHD_gtls_str_cpy (tmpbuffer3, sizeof (tmpbuffer3), tmpbuffer2);
  154. MHD_gtls_str_cat (tmpbuffer3, sizeof (tmpbuffer3), ".type");
  155. len = sizeof (oid) - 1;
  156. result =
  157. MHD__asn1_read_value (MHD__asn1_struct, tmpbuffer3, oid, &len);
  158. if (result == ASN1_ELEMENT_NOT_FOUND)
  159. break;
  160. else if (result != ASN1_SUCCESS)
  161. {
  162. MHD_gnutls_assert ();
  163. result = MHD_gtls_asn2err (result);
  164. goto cleanup;
  165. }
  166. /* Read the Value
  167. */
  168. MHD_gtls_str_cpy (tmpbuffer3, sizeof (tmpbuffer3), tmpbuffer2);
  169. MHD_gtls_str_cat (tmpbuffer3, sizeof (tmpbuffer3), ".value");
  170. len = 0;
  171. result =
  172. MHD__asn1_read_value (MHD__asn1_struct, tmpbuffer3, NULL, &len);
  173. value2 = MHD_gnutls_malloc (len);
  174. if (value2 == NULL)
  175. {
  176. MHD_gnutls_assert ();
  177. result = GNUTLS_E_MEMORY_ERROR;
  178. goto cleanup;
  179. }
  180. result =
  181. MHD__asn1_read_value (MHD__asn1_struct, tmpbuffer3, value2, &len);
  182. if (result != ASN1_SUCCESS)
  183. {
  184. MHD_gnutls_assert ();
  185. result = MHD_gtls_asn2err (result);
  186. goto cleanup;
  187. }
  188. #define STR_APPEND(y) if ((result=MHD_gtls_string_append_str( &out_str, y)) < 0) { \
  189. MHD_gnutls_assert(); \
  190. goto cleanup; \
  191. }
  192. /* The encodings of adjoining RelativeDistinguishedNames are separated
  193. * by a comma character (',' ASCII 44).
  194. */
  195. /* Where there is a multi-valued RDN, the outputs from adjoining
  196. * AttributeTypeAndValues are separated by a plus ('+' ASCII 43)
  197. * character.
  198. */
  199. if (k1 != 1)
  200. { /* the first time do not append a comma */
  201. if (k2 != 1)
  202. { /* adjoining multi-value RDN */
  203. STR_APPEND ("+");
  204. }
  205. else
  206. {
  207. STR_APPEND (",");
  208. }
  209. }
  210. ldap_desc = oid2ldap_string (oid);
  211. printable = MHD__gnutls_x509_oid_data_printable (oid);
  212. sizeof_escaped = 2 * len + 1;
  213. escaped = MHD_gnutls_malloc (sizeof_escaped);
  214. if (escaped == NULL)
  215. {
  216. MHD_gnutls_assert ();
  217. result = GNUTLS_E_MEMORY_ERROR;
  218. goto cleanup;
  219. }
  220. sizeof_string = 2 * len + 2; /* in case it is not printable */
  221. string = MHD_gnutls_malloc (sizeof_string);
  222. if (string == NULL)
  223. {
  224. MHD_gnutls_assert ();
  225. result = GNUTLS_E_MEMORY_ERROR;
  226. goto cleanup;
  227. }
  228. STR_APPEND (ldap_desc);
  229. STR_APPEND ("=");
  230. result = 0;
  231. if (printable)
  232. result =
  233. MHD__gnutls_x509_oid_data2string (oid,
  234. value2, len,
  235. string, &sizeof_string);
  236. if (!printable || result < 0)
  237. result =
  238. MHD__gnutls_x509_data2hex ((const unsigned char *) value2, len,
  239. (unsigned char *) string,
  240. &sizeof_string);
  241. if (result < 0)
  242. {
  243. MHD_gnutls_assert ();
  244. MHD__gnutls_x509_log
  245. ("Found OID: '%s' with value '%s'\n",
  246. oid, MHD_gtls_bin2hex (value2, len, escaped,
  247. sizeof_escaped));
  248. goto cleanup;
  249. }
  250. STR_APPEND (str_escape (string, escaped, sizeof_escaped));
  251. MHD_gnutls_free (string);
  252. string = NULL;
  253. MHD_gnutls_free (escaped);
  254. escaped = NULL;
  255. MHD_gnutls_free (value2);
  256. value2 = NULL;
  257. }
  258. while (1);
  259. }
  260. while (1);
  261. if (out_str.length >= (unsigned int) *sizeof_buf)
  262. {
  263. MHD_gnutls_assert ();
  264. *sizeof_buf = out_str.length + 1;
  265. result = GNUTLS_E_SHORT_MEMORY_BUFFER;
  266. goto cleanup;
  267. }
  268. if (buf)
  269. {
  270. memcpy (buf, out_str.data, out_str.length);
  271. buf[out_str.length] = 0;
  272. }
  273. *sizeof_buf = out_str.length;
  274. result = 0;
  275. cleanup:
  276. MHD_gnutls_free (value2);
  277. MHD_gnutls_free (string);
  278. MHD_gnutls_free (escaped);
  279. MHD_gtls_string_clear (&out_str);
  280. return result;
  281. }
  282. /* Parses an X509 DN in the MHD__asn1_struct, and searches for the
  283. * given OID in the DN.
  284. *
  285. * If raw_flag == 0, the output will be encoded in the LDAP way. (#hex for non printable)
  286. * Otherwise the raw DER data are returned.
  287. *
  288. * MHD__asn1_rdn_name must be a string in the form "tbsCertificate.issuer.rdnSequence".
  289. * That is to point in the rndSequence.
  290. *
  291. * indx specifies which OID to return. Ie 0 means return the first specified
  292. * OID found, 1 the second etc.
  293. */
  294. int
  295. MHD__gnutls_x509_parse_dn_oid (ASN1_TYPE MHD__asn1_struct,
  296. const char *MHD__asn1_rdn_name,
  297. const char *given_oid, int indx,
  298. unsigned int raw_flag,
  299. void *buf, size_t * sizeof_buf)
  300. {
  301. int k2, k1, result;
  302. char tmpbuffer1[MAX_NAME_SIZE];
  303. char tmpbuffer2[MAX_NAME_SIZE];
  304. char tmpbuffer3[MAX_NAME_SIZE];
  305. opaque value[256];
  306. char oid[128];
  307. int len, printable;
  308. int i = 0;
  309. char *cbuf = buf;
  310. if (cbuf == NULL)
  311. *sizeof_buf = 0;
  312. else
  313. cbuf[0] = 0;
  314. k1 = 0;
  315. do
  316. {
  317. k1++;
  318. /* create a string like "tbsCertList.issuer.rdnSequence.?1"
  319. */
  320. if (MHD__asn1_rdn_name[0] != 0)
  321. snprintf (tmpbuffer1, sizeof (tmpbuffer1), "%s.?%u",
  322. MHD__asn1_rdn_name, k1);
  323. else
  324. snprintf (tmpbuffer1, sizeof (tmpbuffer1), "?%u", k1);
  325. len = sizeof (value) - 1;
  326. result =
  327. MHD__asn1_read_value (MHD__asn1_struct, tmpbuffer1, value, &len);
  328. if (result == ASN1_ELEMENT_NOT_FOUND)
  329. {
  330. MHD_gnutls_assert ();
  331. break;
  332. }
  333. if (result != ASN1_VALUE_NOT_FOUND)
  334. {
  335. MHD_gnutls_assert ();
  336. result = MHD_gtls_asn2err (result);
  337. goto cleanup;
  338. }
  339. k2 = 0;
  340. do
  341. { /* Move to the attibute type and values
  342. */
  343. k2++;
  344. if (tmpbuffer1[0] != 0)
  345. snprintf (tmpbuffer2, sizeof (tmpbuffer2), "%s.?%u", tmpbuffer1,
  346. k2);
  347. else
  348. snprintf (tmpbuffer2, sizeof (tmpbuffer2), "?%u", k2);
  349. /* Try to read the RelativeDistinguishedName attributes.
  350. */
  351. len = sizeof (value) - 1;
  352. result =
  353. MHD__asn1_read_value (MHD__asn1_struct, tmpbuffer2, value, &len);
  354. if (result == ASN1_ELEMENT_NOT_FOUND)
  355. {
  356. break;
  357. }
  358. if (result != ASN1_VALUE_NOT_FOUND)
  359. {
  360. MHD_gnutls_assert ();
  361. result = MHD_gtls_asn2err (result);
  362. goto cleanup;
  363. }
  364. /* Read the OID
  365. */
  366. MHD_gtls_str_cpy (tmpbuffer3, sizeof (tmpbuffer3), tmpbuffer2);
  367. MHD_gtls_str_cat (tmpbuffer3, sizeof (tmpbuffer3), ".type");
  368. len = sizeof (oid) - 1;
  369. result =
  370. MHD__asn1_read_value (MHD__asn1_struct, tmpbuffer3, oid, &len);
  371. if (result == ASN1_ELEMENT_NOT_FOUND)
  372. break;
  373. else if (result != ASN1_SUCCESS)
  374. {
  375. MHD_gnutls_assert ();
  376. result = MHD_gtls_asn2err (result);
  377. goto cleanup;
  378. }
  379. if (strcmp (oid, given_oid) == 0 && indx == i++)
  380. { /* Found the OID */
  381. /* Read the Value
  382. */
  383. MHD_gtls_str_cpy (tmpbuffer3, sizeof (tmpbuffer3), tmpbuffer2);
  384. MHD_gtls_str_cat (tmpbuffer3, sizeof (tmpbuffer3), ".value");
  385. len = *sizeof_buf;
  386. result =
  387. MHD__asn1_read_value (MHD__asn1_struct, tmpbuffer3, buf,
  388. &len);
  389. if (result != ASN1_SUCCESS)
  390. {
  391. MHD_gnutls_assert ();
  392. if (result == ASN1_MEM_ERROR)
  393. *sizeof_buf = len;
  394. result = MHD_gtls_asn2err (result);
  395. goto cleanup;
  396. }
  397. if (raw_flag != 0)
  398. {
  399. if ((unsigned) len > *sizeof_buf)
  400. {
  401. *sizeof_buf = len;
  402. result = GNUTLS_E_SHORT_MEMORY_BUFFER;
  403. goto cleanup;
  404. }
  405. *sizeof_buf = len;
  406. return 0;
  407. }
  408. else
  409. { /* parse data. raw_flag == 0 */
  410. printable = MHD__gnutls_x509_oid_data_printable (oid);
  411. if (printable == 1)
  412. result =
  413. MHD__gnutls_x509_oid_data2string (oid, buf, len,
  414. cbuf, sizeof_buf);
  415. else
  416. result =
  417. MHD__gnutls_x509_data2hex (buf, len,
  418. (unsigned char *) cbuf,
  419. sizeof_buf);
  420. if (result < 0)
  421. {
  422. MHD_gnutls_assert ();
  423. goto cleanup;
  424. }
  425. return 0;
  426. } /* raw_flag == 0 */
  427. }
  428. }
  429. while (1);
  430. }
  431. while (1);
  432. MHD_gnutls_assert ();
  433. result = GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
  434. cleanup:
  435. return result;
  436. }
  437. /* Parses an X509 DN in the MHD__asn1_struct, and returns the requested
  438. * DN OID.
  439. *
  440. * MHD__asn1_rdn_name must be a string in the form "tbsCertificate.issuer.rdnSequence".
  441. * That is to point in the rndSequence.
  442. *
  443. * indx specifies which OID to return. Ie 0 means return the first specified
  444. * OID found, 1 the second etc.
  445. */
  446. int
  447. MHD__gnutls_x509_get_dn_oid (ASN1_TYPE MHD__asn1_struct,
  448. const char *MHD__asn1_rdn_name,
  449. int indx, void *_oid, size_t * sizeof_oid)
  450. {
  451. int k2, k1, result;
  452. char tmpbuffer1[MAX_NAME_SIZE];
  453. char tmpbuffer2[MAX_NAME_SIZE];
  454. char tmpbuffer3[MAX_NAME_SIZE];
  455. char value[256];
  456. char oid[128];
  457. int len;
  458. int i = 0;
  459. k1 = 0;
  460. do
  461. {
  462. k1++;
  463. /* create a string like "tbsCertList.issuer.rdnSequence.?1"
  464. */
  465. if (MHD__asn1_rdn_name[0] != 0)
  466. snprintf (tmpbuffer1, sizeof (tmpbuffer1), "%s.?%u",
  467. MHD__asn1_rdn_name, k1);
  468. else
  469. snprintf (tmpbuffer1, sizeof (tmpbuffer1), "?%u", k1);
  470. len = sizeof (value) - 1;
  471. result =
  472. MHD__asn1_read_value (MHD__asn1_struct, tmpbuffer1, value, &len);
  473. if (result == ASN1_ELEMENT_NOT_FOUND)
  474. {
  475. MHD_gnutls_assert ();
  476. break;
  477. }
  478. if (result != ASN1_VALUE_NOT_FOUND)
  479. {
  480. MHD_gnutls_assert ();
  481. result = MHD_gtls_asn2err (result);
  482. goto cleanup;
  483. }
  484. k2 = 0;
  485. do
  486. { /* Move to the attibute type and values
  487. */
  488. k2++;
  489. if (tmpbuffer1[0] != 0)
  490. snprintf (tmpbuffer2, sizeof (tmpbuffer2), "%s.?%u", tmpbuffer1,
  491. k2);
  492. else
  493. snprintf (tmpbuffer2, sizeof (tmpbuffer2), "?%u", k2);
  494. /* Try to read the RelativeDistinguishedName attributes.
  495. */
  496. len = sizeof (value) - 1;
  497. result =
  498. MHD__asn1_read_value (MHD__asn1_struct, tmpbuffer2, value, &len);
  499. if (result == ASN1_ELEMENT_NOT_FOUND)
  500. {
  501. break;
  502. }
  503. if (result != ASN1_VALUE_NOT_FOUND)
  504. {
  505. MHD_gnutls_assert ();
  506. result = MHD_gtls_asn2err (result);
  507. goto cleanup;
  508. }
  509. /* Read the OID
  510. */
  511. MHD_gtls_str_cpy (tmpbuffer3, sizeof (tmpbuffer3), tmpbuffer2);
  512. MHD_gtls_str_cat (tmpbuffer3, sizeof (tmpbuffer3), ".type");
  513. len = sizeof (oid) - 1;
  514. result =
  515. MHD__asn1_read_value (MHD__asn1_struct, tmpbuffer3, oid, &len);
  516. if (result == ASN1_ELEMENT_NOT_FOUND)
  517. break;
  518. else if (result != ASN1_SUCCESS)
  519. {
  520. MHD_gnutls_assert ();
  521. result = MHD_gtls_asn2err (result);
  522. goto cleanup;
  523. }
  524. if (indx == i++)
  525. { /* Found the OID */
  526. len = strlen (oid) + 1;
  527. if (*sizeof_oid < (unsigned) len)
  528. {
  529. *sizeof_oid = len;
  530. MHD_gnutls_assert ();
  531. return GNUTLS_E_SHORT_MEMORY_BUFFER;
  532. }
  533. memcpy (_oid, oid, len);
  534. *sizeof_oid = len - 1;
  535. return 0;
  536. }
  537. }
  538. while (1);
  539. }
  540. while (1);
  541. MHD_gnutls_assert ();
  542. result = GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
  543. cleanup:
  544. return result;
  545. }
  546. /* This will encode and write the AttributeTypeAndValue field.
  547. * 'multi' must be zero if writing an AttributeTypeAndValue, and 1 if Attribute.
  548. * In all cases only one value is written.
  549. */
  550. int
  551. MHD__gnutls_x509_encode_and_write_attribute (const char *given_oid,
  552. ASN1_TYPE MHD__asn1_struct,
  553. const char *where,
  554. const void *_data,
  555. int sizeof_data, int multi)
  556. {
  557. const char *val_name;
  558. const opaque *data = _data;
  559. char tmp[128];
  560. ASN1_TYPE c2;
  561. int result;
  562. /* Find how to encode the data.
  563. */
  564. val_name =
  565. MHD__asn1_find_structure_from_oid (MHD__gnutls_get_pkix (), given_oid);
  566. if (val_name == NULL)
  567. {
  568. MHD_gnutls_assert ();
  569. return GNUTLS_E_X509_UNSUPPORTED_OID;
  570. }
  571. MHD_gtls_str_cpy (tmp, sizeof (tmp), "PKIX1.");
  572. MHD_gtls_str_cat (tmp, sizeof (tmp), val_name);
  573. result = MHD__asn1_create_element (MHD__gnutls_get_pkix (), tmp, &c2);
  574. if (result != ASN1_SUCCESS)
  575. {
  576. MHD_gnutls_assert ();
  577. return MHD_gtls_asn2err (result);
  578. }
  579. tmp[0] = 0;
  580. if ((result = MHD__gnutls_x509_oid_data_choice (given_oid)) > 0)
  581. {
  582. char *string_type;
  583. int i;
  584. string_type = "printableString";
  585. /* Check if the data is plain ascii, and use
  586. * the UTF8 string type if not.
  587. */
  588. for (i = 0; i < sizeof_data; i++)
  589. {
  590. if (!isascii (data[i]))
  591. {
  592. string_type = "utf8String";
  593. break;
  594. }
  595. }
  596. /* if the type is a CHOICE then write the
  597. * type we'll use.
  598. */
  599. result = MHD__asn1_write_value (c2, "", string_type, 1);
  600. if (result != ASN1_SUCCESS)
  601. {
  602. MHD_gnutls_assert ();
  603. MHD__asn1_delete_structure (&c2);
  604. return MHD_gtls_asn2err (result);
  605. }
  606. MHD_gtls_str_cpy (tmp, sizeof (tmp), string_type);
  607. }
  608. result = MHD__asn1_write_value (c2, tmp, data, sizeof_data);
  609. if (result != ASN1_SUCCESS)
  610. {
  611. MHD_gnutls_assert ();
  612. MHD__asn1_delete_structure (&c2);
  613. return MHD_gtls_asn2err (result);
  614. }
  615. /* write the data (value)
  616. */
  617. MHD_gtls_str_cpy (tmp, sizeof (tmp), where);
  618. MHD_gtls_str_cat (tmp, sizeof (tmp), ".value");
  619. if (multi != 0)
  620. { /* if not writing an AttributeTypeAndValue, but an Attribute */
  621. MHD_gtls_str_cat (tmp, sizeof (tmp), "s"); /* values */
  622. result = MHD__asn1_write_value (MHD__asn1_struct, tmp, "NEW", 1);
  623. if (result != ASN1_SUCCESS)
  624. {
  625. MHD_gnutls_assert ();
  626. return MHD_gtls_asn2err (result);
  627. }
  628. MHD_gtls_str_cat (tmp, sizeof (tmp), ".?LAST");
  629. }
  630. result =
  631. MHD__gnutls_x509_der_encode_and_copy (c2, "", MHD__asn1_struct, tmp, 0);
  632. if (result < 0)
  633. {
  634. MHD_gnutls_assert ();
  635. return result;
  636. }
  637. /* write the type
  638. */
  639. MHD_gtls_str_cpy (tmp, sizeof (tmp), where);
  640. MHD_gtls_str_cat (tmp, sizeof (tmp), ".type");
  641. result = MHD__asn1_write_value (MHD__asn1_struct, tmp, given_oid, 1);
  642. if (result != ASN1_SUCCESS)
  643. {
  644. MHD_gnutls_assert ();
  645. return MHD_gtls_asn2err (result);
  646. }
  647. return 0;
  648. }
  649. /* This will write the AttributeTypeAndValue field. The data must be already DER encoded.
  650. * In all cases only one value is written.
  651. */
  652. static int
  653. MHD__gnutls_x509_write_attribute (const char *given_oid,
  654. ASN1_TYPE MHD__asn1_struct,
  655. const char *where, const void *_data,
  656. int sizeof_data)
  657. {
  658. char tmp[128];
  659. int result;
  660. /* write the data (value)
  661. */
  662. MHD_gtls_str_cpy (tmp, sizeof (tmp), where);
  663. MHD_gtls_str_cat (tmp, sizeof (tmp), ".value");
  664. result = MHD__asn1_write_value (MHD__asn1_struct, tmp, _data, sizeof_data);
  665. if (result < 0)
  666. {
  667. MHD_gnutls_assert ();
  668. return MHD_gtls_asn2err (result);
  669. }
  670. /* write the type
  671. */
  672. MHD_gtls_str_cpy (tmp, sizeof (tmp), where);
  673. MHD_gtls_str_cat (tmp, sizeof (tmp), ".type");
  674. result = MHD__asn1_write_value (MHD__asn1_struct, tmp, given_oid, 1);
  675. if (result != ASN1_SUCCESS)
  676. {
  677. MHD_gnutls_assert ();
  678. return MHD_gtls_asn2err (result);
  679. }
  680. return 0;
  681. }
  682. /* Decodes an X.509 Attribute (if multi==1) or an AttributeTypeAndValue
  683. * otherwise.
  684. *
  685. * octet_string should be non zero if we are to decode octet strings after
  686. * decoding.
  687. *
  688. * The output is allocated and stored in value.
  689. */
  690. int
  691. MHD__gnutls_x509_decode_and_read_attribute (ASN1_TYPE MHD__asn1_struct,
  692. const char *where, char *oid,
  693. int oid_size,
  694. MHD_gnutls_datum_t * value,
  695. int multi, int octet_string)
  696. {
  697. char tmpbuffer[128];
  698. int len, result;
  699. /* Read the OID
  700. */
  701. MHD_gtls_str_cpy (tmpbuffer, sizeof (tmpbuffer), where);
  702. MHD_gtls_str_cat (tmpbuffer, sizeof (tmpbuffer), ".type");
  703. len = oid_size - 1;
  704. result = MHD__asn1_read_value (MHD__asn1_struct, tmpbuffer, oid, &len);
  705. if (result != ASN1_SUCCESS)
  706. {
  707. MHD_gnutls_assert ();
  708. result = MHD_gtls_asn2err (result);
  709. return result;
  710. }
  711. /* Read the Value
  712. */
  713. MHD_gtls_str_cpy (tmpbuffer, sizeof (tmpbuffer), where);
  714. MHD_gtls_str_cat (tmpbuffer, sizeof (tmpbuffer), ".value");
  715. if (multi)
  716. MHD_gtls_str_cat (tmpbuffer, sizeof (tmpbuffer), "s.?1"); /* .values.?1 */
  717. result =
  718. MHD__gnutls_x509_read_value (MHD__asn1_struct, tmpbuffer, value,
  719. octet_string);
  720. if (result < 0)
  721. {
  722. MHD_gnutls_assert ();
  723. return result;
  724. }
  725. return 0;
  726. }
  727. /* Sets an X509 DN in the MHD__asn1_struct, and puts the given OID in the DN.
  728. * The input is assumed to be raw data.
  729. *
  730. * MHD__asn1_rdn_name must be a string in the form "tbsCertificate.issuer".
  731. * That is to point before the rndSequence.
  732. *
  733. */
  734. int
  735. MHD__gnutls_x509_set_dn_oid (ASN1_TYPE MHD__asn1_struct,
  736. const char *MHD__asn1_name,
  737. const char *given_oid, int raw_flag,
  738. const char *name, int sizeof_name)
  739. {
  740. int result;
  741. char tmp[MAX_NAME_SIZE], MHD__asn1_rdn_name[MAX_NAME_SIZE];
  742. if (sizeof_name == 0 || name == NULL)
  743. {
  744. MHD_gnutls_assert ();
  745. return GNUTLS_E_INVALID_REQUEST;
  746. }
  747. /* create the rdnSequence
  748. */
  749. result =
  750. MHD__asn1_write_value (MHD__asn1_struct, MHD__asn1_name, "rdnSequence",
  751. 1);
  752. if (result != ASN1_SUCCESS)
  753. {
  754. MHD_gnutls_assert ();
  755. return MHD_gtls_asn2err (result);
  756. }
  757. MHD_gtls_str_cpy (MHD__asn1_rdn_name, sizeof (MHD__asn1_rdn_name),
  758. MHD__asn1_name);
  759. MHD_gtls_str_cat (MHD__asn1_rdn_name, sizeof (MHD__asn1_rdn_name),
  760. ".rdnSequence");
  761. /* create a new element
  762. */
  763. result =
  764. MHD__asn1_write_value (MHD__asn1_struct, MHD__asn1_rdn_name, "NEW", 1);
  765. if (result != ASN1_SUCCESS)
  766. {
  767. MHD_gnutls_assert ();
  768. return MHD_gtls_asn2err (result);
  769. }
  770. MHD_gtls_str_cpy (tmp, sizeof (tmp), MHD__asn1_rdn_name);
  771. MHD_gtls_str_cat (tmp, sizeof (tmp), ".?LAST");
  772. /* create the set with only one element
  773. */
  774. result = MHD__asn1_write_value (MHD__asn1_struct, tmp, "NEW", 1);
  775. if (result != ASN1_SUCCESS)
  776. {
  777. MHD_gnutls_assert ();
  778. return MHD_gtls_asn2err (result);
  779. }
  780. /* Encode and write the data
  781. */
  782. MHD_gtls_str_cpy (tmp, sizeof (tmp), MHD__asn1_rdn_name);
  783. MHD_gtls_str_cat (tmp, sizeof (tmp), ".?LAST.?LAST");
  784. if (!raw_flag)
  785. {
  786. result =
  787. MHD__gnutls_x509_encode_and_write_attribute (given_oid,
  788. MHD__asn1_struct,
  789. tmp, name, sizeof_name,
  790. 0);
  791. }
  792. else
  793. {
  794. result =
  795. MHD__gnutls_x509_write_attribute (given_oid, MHD__asn1_struct,
  796. tmp, name, sizeof_name);
  797. }
  798. if (result < 0)
  799. {
  800. MHD_gnutls_assert ();
  801. return result;
  802. }
  803. return 0;
  804. }
  805. /**
  806. * MHD_gnutls_x509_rdn_get - This function parses an RDN sequence and returns a string
  807. * @idn: should contain a DER encoded RDN sequence
  808. * @buf: a pointer to a structure to hold the peer's name
  809. * @sizeof_buf: holds the size of @buf
  810. *
  811. * This function will return the name of the given RDN sequence. The
  812. * name will be in the form "C=xxxx,O=yyyy,CN=zzzz" as described in
  813. * RFC2253.
  814. *
  815. * If the provided buffer is not long enough, returns
  816. * GNUTLS_E_SHORT_MEMORY_BUFFER and *sizeof_buf will be updated. On
  817. * success 0 is returned.
  818. *
  819. **/
  820. int
  821. MHD_gnutls_x509_rdn_get (const MHD_gnutls_datum_t * idn,
  822. char *buf, size_t * sizeof_buf)
  823. {
  824. int result;
  825. ASN1_TYPE dn = ASN1_TYPE_EMPTY;
  826. if (sizeof_buf == 0)
  827. {
  828. MHD_gnutls_assert ();
  829. return GNUTLS_E_INVALID_REQUEST;
  830. }
  831. if (buf)
  832. buf[0] = 0;
  833. if ((result =
  834. MHD__asn1_create_element (MHD__gnutls_get_pkix (),
  835. "PKIX1.Name", &dn)) != ASN1_SUCCESS)
  836. {
  837. MHD_gnutls_assert ();
  838. return MHD_gtls_asn2err (result);
  839. }
  840. result = MHD__asn1_der_decoding (&dn, idn->data, idn->size, NULL);
  841. if (result != ASN1_SUCCESS)
  842. {
  843. /* couldn't decode DER */
  844. MHD_gnutls_assert ();
  845. MHD__asn1_delete_structure (&dn);
  846. return MHD_gtls_asn2err (result);
  847. }
  848. result = MHD__gnutls_x509_parse_dn (dn, "rdnSequence", buf, sizeof_buf);
  849. MHD__asn1_delete_structure (&dn);
  850. return result;
  851. }
  852. /**
  853. * MHD_gnutls_x509_rdn_get_by_oid - This function parses an RDN sequence and returns a string
  854. * @idn: should contain a DER encoded RDN sequence
  855. * @oid: an Object Identifier
  856. * @indx: In case multiple same OIDs exist in the RDN indicates which
  857. * to send. Use 0 for the first one.
  858. * @raw_flag: If non zero then the raw DER data are returned.
  859. * @buf: a pointer to a structure to hold the peer's name
  860. * @sizeof_buf: holds the size of @buf
  861. *
  862. * This function will return the name of the given Object identifier,
  863. * of the RDN sequence. The name will be encoded using the rules
  864. * from RFC2253.
  865. *
  866. * Returns GNUTLS_E_SHORT_MEMORY_BUFFER and updates *sizeof_buf if
  867. * the provided buffer is not long enough, and 0 on success.
  868. *
  869. **/
  870. int
  871. MHD_gnutls_x509_rdn_get_by_oid (const MHD_gnutls_datum_t * idn,
  872. const char *oid, int indx,
  873. unsigned int raw_flag, void *buf,
  874. size_t * sizeof_buf)
  875. {
  876. int result;
  877. ASN1_TYPE dn = ASN1_TYPE_EMPTY;
  878. if (sizeof_buf == 0)
  879. {
  880. return GNUTLS_E_INVALID_REQUEST;
  881. }
  882. if ((result =
  883. MHD__asn1_create_element (MHD__gnutls_get_pkix (),
  884. "PKIX1.Name", &dn)) != ASN1_SUCCESS)
  885. {
  886. MHD_gnutls_assert ();
  887. return MHD_gtls_asn2err (result);
  888. }
  889. result = MHD__asn1_der_decoding (&dn, idn->data, idn->size, NULL);
  890. if (result != ASN1_SUCCESS)
  891. {
  892. /* couldn't decode DER */
  893. MHD_gnutls_assert ();
  894. MHD__asn1_delete_structure (&dn);
  895. return MHD_gtls_asn2err (result);
  896. }
  897. result =
  898. MHD__gnutls_x509_parse_dn_oid (dn, "rdnSequence", oid, indx,
  899. raw_flag, buf, sizeof_buf);
  900. MHD__asn1_delete_structure (&dn);
  901. return result;
  902. }
  903. /**
  904. * MHD_gnutls_x509_rdn_get_oid - This function parses an RDN sequence and returns an OID.
  905. * @idn: should contain a DER encoded RDN sequence
  906. * @indx: Indicates which OID to return. Use 0 for the first one.
  907. * @oid: a pointer to a structure to hold the peer's name OID
  908. * @sizeof_oid: holds the size of @oid
  909. *
  910. * This function will return the specified Object identifier, of the
  911. * RDN sequence.
  912. *
  913. * Returns GNUTLS_E_SHORT_MEMORY_BUFFER and updates *sizeof_buf if
  914. * the provided buffer is not long enough, and 0 on success.
  915. *
  916. **/
  917. int
  918. MHD_gnutls_x509_rdn_get_oid (const MHD_gnutls_datum_t * idn,
  919. int indx, void *buf, size_t * sizeof_buf)
  920. {
  921. int result;
  922. ASN1_TYPE dn = ASN1_TYPE_EMPTY;
  923. if (sizeof_buf == 0)
  924. {
  925. return GNUTLS_E_INVALID_REQUEST;
  926. }
  927. if ((result =
  928. MHD__asn1_create_element (MHD__gnutls_get_pkix (),
  929. "PKIX1.Name", &dn)) != ASN1_SUCCESS)
  930. {
  931. MHD_gnutls_assert ();
  932. return MHD_gtls_asn2err (result);
  933. }
  934. result = MHD__asn1_der_decoding (&dn, idn->data, idn->size, NULL);
  935. if (result != ASN1_SUCCESS)
  936. {
  937. /* couldn't decode DER */
  938. MHD_gnutls_assert ();
  939. MHD__asn1_delete_structure (&dn);
  940. return MHD_gtls_asn2err (result);
  941. }
  942. result =
  943. MHD__gnutls_x509_get_dn_oid (dn, "rdnSequence", indx, buf, sizeof_buf);
  944. MHD__asn1_delete_structure (&dn);
  945. return result;
  946. }
  947. /*
  948. * Compares the DER encoded part of a DN.
  949. *
  950. * FIXME: use a real DN comparison algorithm.
  951. *
  952. * Returns 1 if the DN's match and zero if they don't match. Otherwise
  953. * a negative value is returned to indicate error.
  954. */
  955. int
  956. MHD__gnutls_x509_compare_raw_dn (const MHD_gnutls_datum_t * dn1,
  957. const MHD_gnutls_datum_t * dn2)
  958. {
  959. if (dn1->size != dn2->size)
  960. {
  961. MHD_gnutls_assert ();
  962. return 0;
  963. }
  964. if (memcmp (dn1->data, dn2->data, dn2->size) != 0)
  965. {
  966. MHD_gnutls_assert ();
  967. return 0;
  968. }
  969. return 1; /* they match */
  970. }