x509asn1.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #if defined(USE_GSKIT) || defined(USE_NSS) || defined(USE_GNUTLS) || \
  24. defined(USE_CYASSL) || defined(USE_SCHANNEL)
  25. #include <curl/curl.h>
  26. #include "urldata.h"
  27. #include "strcase.h"
  28. #include "hostcheck.h"
  29. #include "vtls/vtls.h"
  30. #include "sendf.h"
  31. #include "inet_pton.h"
  32. #include "curl_base64.h"
  33. #include "x509asn1.h"
  34. /* The last 3 #include files should be in this order */
  35. #include "curl_printf.h"
  36. #include "curl_memory.h"
  37. #include "memdebug.h"
  38. /* ASN.1 OIDs. */
  39. static const char cnOID[] = "2.5.4.3"; /* Common name. */
  40. static const char sanOID[] = "2.5.29.17"; /* Subject alternative name. */
  41. static const curl_OID OIDtable[] = {
  42. { "1.2.840.10040.4.1", "dsa" },
  43. { "1.2.840.10040.4.3", "dsa-with-sha1" },
  44. { "1.2.840.10045.2.1", "ecPublicKey" },
  45. { "1.2.840.10045.3.0.1", "c2pnb163v1" },
  46. { "1.2.840.10045.4.1", "ecdsa-with-SHA1" },
  47. { "1.2.840.10046.2.1", "dhpublicnumber" },
  48. { "1.2.840.113549.1.1.1", "rsaEncryption" },
  49. { "1.2.840.113549.1.1.2", "md2WithRSAEncryption" },
  50. { "1.2.840.113549.1.1.4", "md5WithRSAEncryption" },
  51. { "1.2.840.113549.1.1.5", "sha1WithRSAEncryption" },
  52. { "1.2.840.113549.1.1.10", "RSASSA-PSS" },
  53. { "1.2.840.113549.1.1.14", "sha224WithRSAEncryption" },
  54. { "1.2.840.113549.1.1.11", "sha256WithRSAEncryption" },
  55. { "1.2.840.113549.1.1.12", "sha384WithRSAEncryption" },
  56. { "1.2.840.113549.1.1.13", "sha512WithRSAEncryption" },
  57. { "1.2.840.113549.2.2", "md2" },
  58. { "1.2.840.113549.2.5", "md5" },
  59. { "1.3.14.3.2.26", "sha1" },
  60. { cnOID, "CN" },
  61. { "2.5.4.4", "SN" },
  62. { "2.5.4.5", "serialNumber" },
  63. { "2.5.4.6", "C" },
  64. { "2.5.4.7", "L" },
  65. { "2.5.4.8", "ST" },
  66. { "2.5.4.9", "streetAddress" },
  67. { "2.5.4.10", "O" },
  68. { "2.5.4.11", "OU" },
  69. { "2.5.4.12", "title" },
  70. { "2.5.4.13", "description" },
  71. { "2.5.4.17", "postalCode" },
  72. { "2.5.4.41", "name" },
  73. { "2.5.4.42", "givenName" },
  74. { "2.5.4.43", "initials" },
  75. { "2.5.4.44", "generationQualifier" },
  76. { "2.5.4.45", "X500UniqueIdentifier" },
  77. { "2.5.4.46", "dnQualifier" },
  78. { "2.5.4.65", "pseudonym" },
  79. { "1.2.840.113549.1.9.1", "emailAddress" },
  80. { "2.5.4.72", "role" },
  81. { sanOID, "subjectAltName" },
  82. { "2.5.29.18", "issuerAltName" },
  83. { "2.5.29.19", "basicConstraints" },
  84. { "2.16.840.1.101.3.4.2.4", "sha224" },
  85. { "2.16.840.1.101.3.4.2.1", "sha256" },
  86. { "2.16.840.1.101.3.4.2.2", "sha384" },
  87. { "2.16.840.1.101.3.4.2.3", "sha512" },
  88. { (const char *) NULL, (const char *) NULL }
  89. };
  90. /*
  91. * Lightweight ASN.1 parser.
  92. * In particular, it does not check for syntactic/lexical errors.
  93. * It is intended to support certificate information gathering for SSL backends
  94. * that offer a mean to get certificates as a whole, but do not supply
  95. * entry points to get particular certificate sub-fields.
  96. * Please note there is no pretention here to rewrite a full SSL library.
  97. */
  98. static const char *getASN1Element(curl_asn1Element *elem,
  99. const char *beg, const char *end)
  100. WARN_UNUSED_RESULT;
  101. static const char *getASN1Element(curl_asn1Element *elem,
  102. const char *beg, const char *end)
  103. {
  104. unsigned char b;
  105. unsigned long len;
  106. curl_asn1Element lelem;
  107. /* Get a single ASN.1 element into `elem', parse ASN.1 string at `beg'
  108. ending at `end'.
  109. Returns a pointer in source string after the parsed element, or NULL
  110. if an error occurs. */
  111. if(!beg || !end || beg >= end || !*beg ||
  112. (size_t)(end - beg) > CURL_ASN1_MAX)
  113. return (const char *) NULL;
  114. /* Process header byte. */
  115. elem->header = beg;
  116. b = (unsigned char) *beg++;
  117. elem->constructed = (b & 0x20) != 0;
  118. elem->class = (b >> 6) & 3;
  119. b &= 0x1F;
  120. if(b == 0x1F)
  121. return (const char *) NULL; /* Long tag values not supported here. */
  122. elem->tag = b;
  123. /* Process length. */
  124. if(beg >= end)
  125. return (const char *) NULL;
  126. b = (unsigned char) *beg++;
  127. if(!(b & 0x80))
  128. len = b;
  129. else if(!(b &= 0x7F)) {
  130. /* Unspecified length. Since we have all the data, we can determine the
  131. effective length by skipping element until an end element is found. */
  132. if(!elem->constructed)
  133. return (const char *) NULL;
  134. elem->beg = beg;
  135. while(beg < end && *beg) {
  136. beg = getASN1Element(&lelem, beg, end);
  137. if(!beg)
  138. return (const char *) NULL;
  139. }
  140. if(beg >= end)
  141. return (const char *) NULL;
  142. elem->end = beg;
  143. return beg + 1;
  144. }
  145. else if((unsigned)b > (size_t)(end - beg))
  146. return (const char *) NULL; /* Does not fit in source. */
  147. else {
  148. /* Get long length. */
  149. len = 0;
  150. do {
  151. if(len & 0xFF000000L)
  152. return (const char *) NULL; /* Lengths > 32 bits are not supported. */
  153. len = (len << 8) | (unsigned char) *beg++;
  154. } while(--b);
  155. }
  156. if(len > (size_t)(end - beg))
  157. return (const char *) NULL; /* Element data does not fit in source. */
  158. elem->beg = beg;
  159. elem->end = beg + len;
  160. return elem->end;
  161. }
  162. static const curl_OID * searchOID(const char *oid)
  163. {
  164. const curl_OID *op;
  165. /* Search the null terminated OID or OID identifier in local table.
  166. Return the table entry pointer or NULL if not found. */
  167. for(op = OIDtable; op->numoid; op++)
  168. if(!strcmp(op->numoid, oid) || strcasecompare(op->textoid, oid))
  169. return op;
  170. return (const curl_OID *) NULL;
  171. }
  172. static const char *bool2str(const char *beg, const char *end)
  173. {
  174. /* Convert an ASN.1 Boolean value into its string representation.
  175. Return the dynamically allocated string, or NULL if source is not an
  176. ASN.1 Boolean value. */
  177. if(end - beg != 1)
  178. return (const char *) NULL;
  179. return strdup(*beg? "TRUE": "FALSE");
  180. }
  181. static const char *octet2str(const char *beg, const char *end)
  182. {
  183. size_t n = end - beg;
  184. char *buf = NULL;
  185. /* Convert an ASN.1 octet string to a printable string.
  186. Return the dynamically allocated string, or NULL if an error occurs. */
  187. if(n <= (SIZE_T_MAX - 1) / 3) {
  188. buf = malloc(3 * n + 1);
  189. if(buf)
  190. for(n = 0; beg < end; n += 3)
  191. snprintf(buf + n, 4, "%02x:", *(const unsigned char *) beg++);
  192. }
  193. return buf;
  194. }
  195. static const char *bit2str(const char *beg, const char *end)
  196. {
  197. /* Convert an ASN.1 bit string to a printable string.
  198. Return the dynamically allocated string, or NULL if an error occurs. */
  199. if(++beg > end)
  200. return (const char *) NULL;
  201. return octet2str(beg, end);
  202. }
  203. static const char *int2str(const char *beg, const char *end)
  204. {
  205. unsigned long val = 0;
  206. size_t n = end - beg;
  207. /* Convert an ASN.1 integer value into its string representation.
  208. Return the dynamically allocated string, or NULL if source is not an
  209. ASN.1 integer value. */
  210. if(!n)
  211. return (const char *) NULL;
  212. if(n > 4)
  213. return octet2str(beg, end);
  214. /* Represent integers <= 32-bit as a single value. */
  215. if(*beg & 0x80)
  216. val = ~val;
  217. do
  218. val = (val << 8) | *(const unsigned char *) beg++;
  219. while(beg < end);
  220. return curl_maprintf("%s%lx", val >= 10? "0x": "", val);
  221. }
  222. static ssize_t
  223. utf8asn1str(char **to, int type, const char *from, const char *end)
  224. {
  225. size_t inlength = end - from;
  226. int size = 1;
  227. size_t outlength;
  228. int charsize;
  229. unsigned int wc;
  230. char *buf;
  231. /* Perform a lazy conversion from an ASN.1 typed string to UTF8. Allocate the
  232. destination buffer dynamically. The allocation size will normally be too
  233. large: this is to avoid buffer overflows.
  234. Terminate the string with a nul byte and return the converted
  235. string length. */
  236. *to = (char *) NULL;
  237. switch(type) {
  238. case CURL_ASN1_BMP_STRING:
  239. size = 2;
  240. break;
  241. case CURL_ASN1_UNIVERSAL_STRING:
  242. size = 4;
  243. break;
  244. case CURL_ASN1_NUMERIC_STRING:
  245. case CURL_ASN1_PRINTABLE_STRING:
  246. case CURL_ASN1_TELETEX_STRING:
  247. case CURL_ASN1_IA5_STRING:
  248. case CURL_ASN1_VISIBLE_STRING:
  249. case CURL_ASN1_UTF8_STRING:
  250. break;
  251. default:
  252. return -1; /* Conversion not supported. */
  253. }
  254. if(inlength % size)
  255. return -1; /* Length inconsistent with character size. */
  256. if(inlength / size > (SIZE_T_MAX - 1) / 4)
  257. return -1; /* Too big. */
  258. buf = malloc(4 * (inlength / size) + 1);
  259. if(!buf)
  260. return -1; /* Not enough memory. */
  261. if(type == CURL_ASN1_UTF8_STRING) {
  262. /* Just copy. */
  263. outlength = inlength;
  264. if(outlength)
  265. memcpy(buf, from, outlength);
  266. }
  267. else {
  268. for(outlength = 0; from < end;) {
  269. wc = 0;
  270. switch(size) {
  271. case 4:
  272. wc = (wc << 8) | *(const unsigned char *) from++;
  273. wc = (wc << 8) | *(const unsigned char *) from++;
  274. /* FALLTHROUGH */
  275. case 2:
  276. wc = (wc << 8) | *(const unsigned char *) from++;
  277. /* FALLTHROUGH */
  278. default: /* case 1: */
  279. wc = (wc << 8) | *(const unsigned char *) from++;
  280. }
  281. charsize = 1;
  282. if(wc >= 0x00000080) {
  283. if(wc >= 0x00000800) {
  284. if(wc >= 0x00010000) {
  285. if(wc >= 0x00200000) {
  286. free(buf);
  287. return -1; /* Invalid char. size for target encoding. */
  288. }
  289. buf[outlength + 3] = (char) (0x80 | (wc & 0x3F));
  290. wc = (wc >> 6) | 0x00010000;
  291. charsize++;
  292. }
  293. buf[outlength + 2] = (char) (0x80 | (wc & 0x3F));
  294. wc = (wc >> 6) | 0x00000800;
  295. charsize++;
  296. }
  297. buf[outlength + 1] = (char) (0x80 | (wc & 0x3F));
  298. wc = (wc >> 6) | 0x000000C0;
  299. charsize++;
  300. }
  301. buf[outlength] = (char) wc;
  302. outlength += charsize;
  303. }
  304. }
  305. buf[outlength] = '\0';
  306. *to = buf;
  307. return outlength;
  308. }
  309. static const char *string2str(int type, const char *beg, const char *end)
  310. {
  311. char *buf;
  312. /* Convert an ASN.1 String into its UTF-8 string representation.
  313. Return the dynamically allocated string, or NULL if an error occurs. */
  314. if(utf8asn1str(&buf, type, beg, end) < 0)
  315. return (const char *) NULL;
  316. return buf;
  317. }
  318. static int encodeUint(char *buf, int n, unsigned int x)
  319. {
  320. int i = 0;
  321. unsigned int y = x / 10;
  322. /* Decimal ASCII encode unsigned integer `x' in the `n'-byte buffer at `buf'.
  323. Return the total number of encoded digits, even if larger than `n'. */
  324. if(y) {
  325. i += encodeUint(buf, n, y);
  326. x -= y * 10;
  327. }
  328. if(i < n)
  329. buf[i] = (char) ('0' + x);
  330. i++;
  331. if(i < n)
  332. buf[i] = '\0'; /* Store a terminator if possible. */
  333. return i;
  334. }
  335. static int encodeOID(char *buf, int n, const char *beg, const char *end)
  336. {
  337. int i = 0;
  338. unsigned int x;
  339. unsigned int y;
  340. /* Convert an ASN.1 OID into its dotted string representation.
  341. Store the result in th `n'-byte buffer at `buf'.
  342. Return the converted string length, or -1 if an error occurs. */
  343. /* Process the first two numbers. */
  344. y = *(const unsigned char *) beg++;
  345. x = y / 40;
  346. y -= x * 40;
  347. i += encodeUint(buf + i, n - i, x);
  348. if(i < n)
  349. buf[i] = '.';
  350. i++;
  351. i += encodeUint(buf + i, n - i, y);
  352. /* Process the trailing numbers. */
  353. while(beg < end) {
  354. if(i < n)
  355. buf[i] = '.';
  356. i++;
  357. x = 0;
  358. do {
  359. if(x & 0xFF000000)
  360. return -1;
  361. y = *(const unsigned char *) beg++;
  362. x = (x << 7) | (y & 0x7F);
  363. } while(y & 0x80);
  364. i += encodeUint(buf + i, n - i, x);
  365. }
  366. if(i < n)
  367. buf[i] = '\0';
  368. return i;
  369. }
  370. static const char *OID2str(const char *beg, const char *end, bool symbolic)
  371. {
  372. char *buf = (char *) NULL;
  373. const curl_OID * op;
  374. int n;
  375. /* Convert an ASN.1 OID into its dotted or symbolic string representation.
  376. Return the dynamically allocated string, or NULL if an error occurs. */
  377. if(beg < end) {
  378. n = encodeOID((char *) NULL, -1, beg, end);
  379. if(n >= 0) {
  380. buf = malloc(n + 1);
  381. if(buf) {
  382. encodeOID(buf, n, beg, end);
  383. buf[n] = '\0';
  384. if(symbolic) {
  385. op = searchOID(buf);
  386. if(op) {
  387. free(buf);
  388. buf = strdup(op->textoid);
  389. }
  390. }
  391. }
  392. }
  393. }
  394. return buf;
  395. }
  396. static const char *GTime2str(const char *beg, const char *end)
  397. {
  398. const char *tzp;
  399. const char *fracp;
  400. char sec1, sec2;
  401. size_t fracl;
  402. size_t tzl;
  403. const char *sep = "";
  404. /* Convert an ASN.1 Generalized time to a printable string.
  405. Return the dynamically allocated string, or NULL if an error occurs. */
  406. for(fracp = beg; fracp < end && *fracp >= '0' && *fracp <= '9'; fracp++)
  407. ;
  408. /* Get seconds digits. */
  409. sec1 = '0';
  410. switch(fracp - beg - 12) {
  411. case 0:
  412. sec2 = '0';
  413. break;
  414. case 2:
  415. sec1 = fracp[-2];
  416. /* FALLTHROUGH */
  417. case 1:
  418. sec2 = fracp[-1];
  419. break;
  420. default:
  421. return (const char *) NULL;
  422. }
  423. /* Scan for timezone, measure fractional seconds. */
  424. tzp = fracp;
  425. fracl = 0;
  426. if(fracp < end && (*fracp == '.' || *fracp == ',')) {
  427. fracp++;
  428. do
  429. tzp++;
  430. while(tzp < end && *tzp >= '0' && *tzp <= '9');
  431. /* Strip leading zeroes in fractional seconds. */
  432. for(fracl = tzp - fracp - 1; fracl && fracp[fracl - 1] == '0'; fracl--)
  433. ;
  434. }
  435. /* Process timezone. */
  436. if(tzp >= end)
  437. ; /* Nothing to do. */
  438. else if(*tzp == 'Z') {
  439. tzp = " GMT";
  440. end = tzp + 4;
  441. }
  442. else {
  443. sep = " ";
  444. tzp++;
  445. }
  446. tzl = end - tzp;
  447. return curl_maprintf("%.4s-%.2s-%.2s %.2s:%.2s:%c%c%s%.*s%s%.*s",
  448. beg, beg + 4, beg + 6,
  449. beg + 8, beg + 10, sec1, sec2,
  450. fracl? ".": "", fracl, fracp,
  451. sep, tzl, tzp);
  452. }
  453. static const char *UTime2str(const char *beg, const char *end)
  454. {
  455. const char *tzp;
  456. size_t tzl;
  457. const char *sec;
  458. /* Convert an ASN.1 UTC time to a printable string.
  459. Return the dynamically allocated string, or NULL if an error occurs. */
  460. for(tzp = beg; tzp < end && *tzp >= '0' && *tzp <= '9'; tzp++)
  461. ;
  462. /* Get the seconds. */
  463. sec = beg + 10;
  464. switch(tzp - sec) {
  465. case 0:
  466. sec = "00";
  467. case 2:
  468. break;
  469. default:
  470. return (const char *) NULL;
  471. }
  472. /* Process timezone. */
  473. if(tzp >= end)
  474. return (const char *) NULL;
  475. if(*tzp == 'Z') {
  476. tzp = "GMT";
  477. end = tzp + 3;
  478. }
  479. else
  480. tzp++;
  481. tzl = end - tzp;
  482. return curl_maprintf("%u%.2s-%.2s-%.2s %.2s:%.2s:%.2s %.*s",
  483. 20 - (*beg >= '5'), beg, beg + 2, beg + 4,
  484. beg + 6, beg + 8, sec,
  485. tzl, tzp);
  486. }
  487. static const char *ASN1tostr(curl_asn1Element *elem, int type)
  488. {
  489. /* Convert an ASN.1 element to a printable string.
  490. Return the dynamically allocated string, or NULL if an error occurs. */
  491. if(elem->constructed)
  492. return (const char *) NULL; /* No conversion of structured elements. */
  493. if(!type)
  494. type = elem->tag; /* Type not forced: use element tag as type. */
  495. switch(type) {
  496. case CURL_ASN1_BOOLEAN:
  497. return bool2str(elem->beg, elem->end);
  498. case CURL_ASN1_INTEGER:
  499. case CURL_ASN1_ENUMERATED:
  500. return int2str(elem->beg, elem->end);
  501. case CURL_ASN1_BIT_STRING:
  502. return bit2str(elem->beg, elem->end);
  503. case CURL_ASN1_OCTET_STRING:
  504. return octet2str(elem->beg, elem->end);
  505. case CURL_ASN1_NULL:
  506. return strdup("");
  507. case CURL_ASN1_OBJECT_IDENTIFIER:
  508. return OID2str(elem->beg, elem->end, TRUE);
  509. case CURL_ASN1_UTC_TIME:
  510. return UTime2str(elem->beg, elem->end);
  511. case CURL_ASN1_GENERALIZED_TIME:
  512. return GTime2str(elem->beg, elem->end);
  513. case CURL_ASN1_UTF8_STRING:
  514. case CURL_ASN1_NUMERIC_STRING:
  515. case CURL_ASN1_PRINTABLE_STRING:
  516. case CURL_ASN1_TELETEX_STRING:
  517. case CURL_ASN1_IA5_STRING:
  518. case CURL_ASN1_VISIBLE_STRING:
  519. case CURL_ASN1_UNIVERSAL_STRING:
  520. case CURL_ASN1_BMP_STRING:
  521. return string2str(type, elem->beg, elem->end);
  522. }
  523. return (const char *) NULL; /* Unsupported. */
  524. }
  525. static ssize_t encodeDN(char *buf, size_t n, curl_asn1Element *dn)
  526. {
  527. curl_asn1Element rdn;
  528. curl_asn1Element atv;
  529. curl_asn1Element oid;
  530. curl_asn1Element value;
  531. size_t l = 0;
  532. const char *p1;
  533. const char *p2;
  534. const char *p3;
  535. const char *str;
  536. /* ASCII encode distinguished name at `dn' into the `n'-byte buffer at `buf'.
  537. Return the total string length, even if larger than `n'. */
  538. for(p1 = dn->beg; p1 < dn->end;) {
  539. p1 = getASN1Element(&rdn, p1, dn->end);
  540. if(!p1)
  541. return -1;
  542. for(p2 = rdn.beg; p2 < rdn.end;) {
  543. p2 = getASN1Element(&atv, p2, rdn.end);
  544. if(!p2)
  545. return -1;
  546. p3 = getASN1Element(&oid, atv.beg, atv.end);
  547. if(!p3)
  548. return -1;
  549. if(!getASN1Element(&value, p3, atv.end))
  550. return -1;
  551. str = ASN1tostr(&oid, 0);
  552. if(!str)
  553. return -1;
  554. /* Encode delimiter.
  555. If attribute has a short uppercase name, delimiter is ", ". */
  556. if(l) {
  557. for(p3 = str; isupper(*p3); p3++)
  558. ;
  559. for(p3 = (*p3 || p3 - str > 2)? "/": ", "; *p3; p3++) {
  560. if(l < n)
  561. buf[l] = *p3;
  562. l++;
  563. }
  564. }
  565. /* Encode attribute name. */
  566. for(p3 = str; *p3; p3++) {
  567. if(l < n)
  568. buf[l] = *p3;
  569. l++;
  570. }
  571. free((char *) str);
  572. /* Generate equal sign. */
  573. if(l < n)
  574. buf[l] = '=';
  575. l++;
  576. /* Generate value. */
  577. str = ASN1tostr(&value, 0);
  578. if(!str)
  579. return -1;
  580. for(p3 = str; *p3; p3++) {
  581. if(l < n)
  582. buf[l] = *p3;
  583. l++;
  584. }
  585. free((char *) str);
  586. }
  587. }
  588. return l;
  589. }
  590. static const char *DNtostr(curl_asn1Element *dn)
  591. {
  592. char *buf = (char *) NULL;
  593. ssize_t n = encodeDN(buf, 0, dn);
  594. /* Convert an ASN.1 distinguished name into a printable string.
  595. Return the dynamically allocated string, or NULL if an error occurs. */
  596. if(n >= 0) {
  597. buf = malloc(n + 1);
  598. if(buf) {
  599. encodeDN(buf, n + 1, dn);
  600. buf[n] = '\0';
  601. }
  602. }
  603. return (const char *) buf;
  604. }
  605. /*
  606. * X509 parser.
  607. */
  608. int Curl_parseX509(curl_X509certificate *cert,
  609. const char *beg, const char *end)
  610. {
  611. curl_asn1Element elem;
  612. curl_asn1Element tbsCertificate;
  613. const char *ccp;
  614. static const char defaultVersion = 0; /* v1. */
  615. /* ASN.1 parse an X509 certificate into structure subfields.
  616. Syntax is assumed to have already been checked by the SSL backend.
  617. See RFC 5280. */
  618. cert->certificate.header = NULL;
  619. cert->certificate.beg = beg;
  620. cert->certificate.end = end;
  621. /* Get the sequence content. */
  622. if(!getASN1Element(&elem, beg, end))
  623. return -1; /* Invalid bounds/size. */
  624. beg = elem.beg;
  625. end = elem.end;
  626. /* Get tbsCertificate. */
  627. beg = getASN1Element(&tbsCertificate, beg, end);
  628. if(!beg)
  629. return -1;
  630. /* Skip the signatureAlgorithm. */
  631. beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
  632. if(!beg)
  633. return -1;
  634. /* Get the signatureValue. */
  635. if(!getASN1Element(&cert->signature, beg, end))
  636. return -1;
  637. /* Parse TBSCertificate. */
  638. beg = tbsCertificate.beg;
  639. end = tbsCertificate.end;
  640. /* Get optional version, get serialNumber. */
  641. cert->version.header = NULL;
  642. cert->version.beg = &defaultVersion;
  643. cert->version.end = &defaultVersion + sizeof(defaultVersion);
  644. beg = getASN1Element(&elem, beg, end);
  645. if(!beg)
  646. return -1;
  647. if(elem.tag == 0) {
  648. if(!getASN1Element(&cert->version, elem.beg, elem.end))
  649. return -1;
  650. beg = getASN1Element(&elem, beg, end);
  651. if(!beg)
  652. return -1;
  653. }
  654. cert->serialNumber = elem;
  655. /* Get signature algorithm. */
  656. beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
  657. /* Get issuer. */
  658. beg = getASN1Element(&cert->issuer, beg, end);
  659. if(!beg)
  660. return -1;
  661. /* Get notBefore and notAfter. */
  662. beg = getASN1Element(&elem, beg, end);
  663. if(!beg)
  664. return -1;
  665. ccp = getASN1Element(&cert->notBefore, elem.beg, elem.end);
  666. if(!ccp)
  667. return -1;
  668. if(!getASN1Element(&cert->notAfter, ccp, elem.end))
  669. return -1;
  670. /* Get subject. */
  671. beg = getASN1Element(&cert->subject, beg, end);
  672. if(!beg)
  673. return -1;
  674. /* Get subjectPublicKeyAlgorithm and subjectPublicKey. */
  675. beg = getASN1Element(&cert->subjectPublicKeyInfo, beg, end);
  676. if(!beg)
  677. return -1;
  678. ccp = getASN1Element(&cert->subjectPublicKeyAlgorithm,
  679. cert->subjectPublicKeyInfo.beg,
  680. cert->subjectPublicKeyInfo.end);
  681. if(!ccp)
  682. return -1;
  683. if(!getASN1Element(&cert->subjectPublicKey, ccp,
  684. cert->subjectPublicKeyInfo.end))
  685. return -1;
  686. /* Get optional issuerUiqueID, subjectUniqueID and extensions. */
  687. cert->issuerUniqueID.tag = cert->subjectUniqueID.tag = 0;
  688. cert->extensions.tag = elem.tag = 0;
  689. cert->issuerUniqueID.header = cert->subjectUniqueID.header = NULL;
  690. cert->issuerUniqueID.beg = cert->issuerUniqueID.end = "";
  691. cert->subjectUniqueID.beg = cert->subjectUniqueID.end = "";
  692. cert->extensions.header = NULL;
  693. cert->extensions.beg = cert->extensions.end = "";
  694. if(beg < end) {
  695. beg = getASN1Element(&elem, beg, end);
  696. if(!beg)
  697. return -1;
  698. }
  699. if(elem.tag == 1) {
  700. cert->issuerUniqueID = elem;
  701. if(beg < end) {
  702. beg = getASN1Element(&elem, beg, end);
  703. if(!beg)
  704. return -1;
  705. }
  706. }
  707. if(elem.tag == 2) {
  708. cert->subjectUniqueID = elem;
  709. if(beg < end) {
  710. beg = getASN1Element(&elem, beg, end);
  711. if(!beg)
  712. return -1;
  713. }
  714. }
  715. if(elem.tag == 3)
  716. if(!getASN1Element(&cert->extensions, elem.beg, elem.end))
  717. return -1;
  718. return 0;
  719. }
  720. static size_t copySubstring(char *to, const char *from)
  721. {
  722. size_t i;
  723. /* Copy at most 64-characters, terminate with a newline and returns the
  724. effective number of stored characters. */
  725. for(i = 0; i < 64; i++) {
  726. to[i] = *from;
  727. if(!*from++)
  728. break;
  729. }
  730. to[i++] = '\n';
  731. return i;
  732. }
  733. static const char *dumpAlgo(curl_asn1Element *param,
  734. const char *beg, const char *end)
  735. {
  736. curl_asn1Element oid;
  737. /* Get algorithm parameters and return algorithm name. */
  738. beg = getASN1Element(&oid, beg, end);
  739. if(!beg)
  740. return NULL;
  741. param->header = NULL;
  742. param->tag = 0;
  743. param->beg = param->end = end;
  744. if(beg < end)
  745. if(!getASN1Element(param, beg, end))
  746. return NULL;
  747. return OID2str(oid.beg, oid.end, TRUE);
  748. }
  749. static void do_pubkey_field(struct Curl_easy *data, int certnum,
  750. const char *label, curl_asn1Element *elem)
  751. {
  752. const char *output;
  753. /* Generate a certificate information record for the public key. */
  754. output = ASN1tostr(elem, 0);
  755. if(output) {
  756. if(data->set.ssl.certinfo)
  757. Curl_ssl_push_certinfo(data, certnum, label, output);
  758. if(!certnum)
  759. infof(data, " %s: %s\n", label, output);
  760. free((char *) output);
  761. }
  762. }
  763. static void do_pubkey(struct Curl_easy *data, int certnum,
  764. const char *algo, curl_asn1Element *param,
  765. curl_asn1Element *pubkey)
  766. {
  767. curl_asn1Element elem;
  768. curl_asn1Element pk;
  769. const char *p;
  770. const char *q;
  771. unsigned long len;
  772. unsigned int i;
  773. /* Generate all information records for the public key. */
  774. /* Get the public key (single element). */
  775. if(!getASN1Element(&pk, pubkey->beg + 1, pubkey->end))
  776. return;
  777. if(strcasecompare(algo, "rsaEncryption")) {
  778. p = getASN1Element(&elem, pk.beg, pk.end);
  779. if(!p)
  780. return;
  781. /* Compute key length. */
  782. for(q = elem.beg; !*q && q < elem.end; q++)
  783. ;
  784. len = (unsigned long)((elem.end - q) * 8);
  785. if(len)
  786. for(i = *(unsigned char *) q; !(i & 0x80); i <<= 1)
  787. len--;
  788. if(len > 32)
  789. elem.beg = q; /* Strip leading zero bytes. */
  790. if(!certnum)
  791. infof(data, " RSA Public Key (%lu bits)\n", len);
  792. if(data->set.ssl.certinfo) {
  793. q = curl_maprintf("%lu", len);
  794. if(q) {
  795. Curl_ssl_push_certinfo(data, certnum, "RSA Public Key", q);
  796. free((char *) q);
  797. }
  798. }
  799. /* Generate coefficients. */
  800. do_pubkey_field(data, certnum, "rsa(n)", &elem);
  801. if(!getASN1Element(&elem, p, pk.end))
  802. return;
  803. do_pubkey_field(data, certnum, "rsa(e)", &elem);
  804. }
  805. else if(strcasecompare(algo, "dsa")) {
  806. p = getASN1Element(&elem, param->beg, param->end);
  807. if(p) {
  808. do_pubkey_field(data, certnum, "dsa(p)", &elem);
  809. p = getASN1Element(&elem, p, param->end);
  810. if(p) {
  811. do_pubkey_field(data, certnum, "dsa(q)", &elem);
  812. if(getASN1Element(&elem, p, param->end)) {
  813. do_pubkey_field(data, certnum, "dsa(g)", &elem);
  814. do_pubkey_field(data, certnum, "dsa(pub_key)", &pk);
  815. }
  816. }
  817. }
  818. }
  819. else if(strcasecompare(algo, "dhpublicnumber")) {
  820. p = getASN1Element(&elem, param->beg, param->end);
  821. if(p) {
  822. do_pubkey_field(data, certnum, "dh(p)", &elem);
  823. if(getASN1Element(&elem, param->beg, param->end)) {
  824. do_pubkey_field(data, certnum, "dh(g)", &elem);
  825. do_pubkey_field(data, certnum, "dh(pub_key)", &pk);
  826. }
  827. }
  828. }
  829. }
  830. CURLcode Curl_extract_certinfo(struct connectdata *conn,
  831. int certnum,
  832. const char *beg,
  833. const char *end)
  834. {
  835. curl_X509certificate cert;
  836. struct Curl_easy *data = conn->data;
  837. curl_asn1Element param;
  838. const char *ccp;
  839. char *cp1;
  840. size_t cl1;
  841. char *cp2;
  842. CURLcode result;
  843. unsigned long version;
  844. size_t i;
  845. size_t j;
  846. if(!data->set.ssl.certinfo)
  847. if(certnum)
  848. return CURLE_OK;
  849. /* Prepare the certificate information for curl_easy_getinfo(). */
  850. /* Extract the certificate ASN.1 elements. */
  851. if(Curl_parseX509(&cert, beg, end))
  852. return CURLE_PEER_FAILED_VERIFICATION;
  853. /* Subject. */
  854. ccp = DNtostr(&cert.subject);
  855. if(!ccp)
  856. return CURLE_OUT_OF_MEMORY;
  857. if(data->set.ssl.certinfo)
  858. Curl_ssl_push_certinfo(data, certnum, "Subject", ccp);
  859. if(!certnum)
  860. infof(data, "%2d Subject: %s\n", certnum, ccp);
  861. free((char *) ccp);
  862. /* Issuer. */
  863. ccp = DNtostr(&cert.issuer);
  864. if(!ccp)
  865. return CURLE_OUT_OF_MEMORY;
  866. if(data->set.ssl.certinfo)
  867. Curl_ssl_push_certinfo(data, certnum, "Issuer", ccp);
  868. if(!certnum)
  869. infof(data, " Issuer: %s\n", ccp);
  870. free((char *) ccp);
  871. /* Version (always fits in less than 32 bits). */
  872. version = 0;
  873. for(ccp = cert.version.beg; ccp < cert.version.end; ccp++)
  874. version = (version << 8) | *(const unsigned char *) ccp;
  875. if(data->set.ssl.certinfo) {
  876. ccp = curl_maprintf("%lx", version);
  877. if(!ccp)
  878. return CURLE_OUT_OF_MEMORY;
  879. Curl_ssl_push_certinfo(data, certnum, "Version", ccp);
  880. free((char *) ccp);
  881. }
  882. if(!certnum)
  883. infof(data, " Version: %lu (0x%lx)\n", version + 1, version);
  884. /* Serial number. */
  885. ccp = ASN1tostr(&cert.serialNumber, 0);
  886. if(!ccp)
  887. return CURLE_OUT_OF_MEMORY;
  888. if(data->set.ssl.certinfo)
  889. Curl_ssl_push_certinfo(data, certnum, "Serial Number", ccp);
  890. if(!certnum)
  891. infof(data, " Serial Number: %s\n", ccp);
  892. free((char *) ccp);
  893. /* Signature algorithm .*/
  894. ccp = dumpAlgo(&param, cert.signatureAlgorithm.beg,
  895. cert.signatureAlgorithm.end);
  896. if(!ccp)
  897. return CURLE_OUT_OF_MEMORY;
  898. if(data->set.ssl.certinfo)
  899. Curl_ssl_push_certinfo(data, certnum, "Signature Algorithm", ccp);
  900. if(!certnum)
  901. infof(data, " Signature Algorithm: %s\n", ccp);
  902. free((char *) ccp);
  903. /* Start Date. */
  904. ccp = ASN1tostr(&cert.notBefore, 0);
  905. if(!ccp)
  906. return CURLE_OUT_OF_MEMORY;
  907. if(data->set.ssl.certinfo)
  908. Curl_ssl_push_certinfo(data, certnum, "Start Date", ccp);
  909. if(!certnum)
  910. infof(data, " Start Date: %s\n", ccp);
  911. free((char *) ccp);
  912. /* Expire Date. */
  913. ccp = ASN1tostr(&cert.notAfter, 0);
  914. if(!ccp)
  915. return CURLE_OUT_OF_MEMORY;
  916. if(data->set.ssl.certinfo)
  917. Curl_ssl_push_certinfo(data, certnum, "Expire Date", ccp);
  918. if(!certnum)
  919. infof(data, " Expire Date: %s\n", ccp);
  920. free((char *) ccp);
  921. /* Public Key Algorithm. */
  922. ccp = dumpAlgo(&param, cert.subjectPublicKeyAlgorithm.beg,
  923. cert.subjectPublicKeyAlgorithm.end);
  924. if(!ccp)
  925. return CURLE_OUT_OF_MEMORY;
  926. if(data->set.ssl.certinfo)
  927. Curl_ssl_push_certinfo(data, certnum, "Public Key Algorithm", ccp);
  928. if(!certnum)
  929. infof(data, " Public Key Algorithm: %s\n", ccp);
  930. do_pubkey(data, certnum, ccp, &param, &cert.subjectPublicKey);
  931. free((char *) ccp);
  932. /* TODO: extensions. */
  933. /* Signature. */
  934. ccp = ASN1tostr(&cert.signature, 0);
  935. if(!ccp)
  936. return CURLE_OUT_OF_MEMORY;
  937. if(data->set.ssl.certinfo)
  938. Curl_ssl_push_certinfo(data, certnum, "Signature", ccp);
  939. if(!certnum)
  940. infof(data, " Signature: %s\n", ccp);
  941. free((char *) ccp);
  942. /* Generate PEM certificate. */
  943. result = Curl_base64_encode(data, cert.certificate.beg,
  944. cert.certificate.end - cert.certificate.beg,
  945. &cp1, &cl1);
  946. if(result)
  947. return result;
  948. /* Compute the number of characters in final certificate string. Format is:
  949. -----BEGIN CERTIFICATE-----\n
  950. <max 64 base64 characters>\n
  951. .
  952. .
  953. .
  954. -----END CERTIFICATE-----\n
  955. */
  956. i = 28 + cl1 + (cl1 + 64 - 1) / 64 + 26;
  957. cp2 = malloc(i + 1);
  958. if(!cp2) {
  959. free(cp1);
  960. return CURLE_OUT_OF_MEMORY;
  961. }
  962. /* Build the certificate string. */
  963. i = copySubstring(cp2, "-----BEGIN CERTIFICATE-----");
  964. for(j = 0; j < cl1; j += 64)
  965. i += copySubstring(cp2 + i, cp1 + j);
  966. i += copySubstring(cp2 + i, "-----END CERTIFICATE-----");
  967. cp2[i] = '\0';
  968. free(cp1);
  969. if(data->set.ssl.certinfo)
  970. Curl_ssl_push_certinfo(data, certnum, "Cert", cp2);
  971. if(!certnum)
  972. infof(data, "%s\n", cp2);
  973. free(cp2);
  974. return CURLE_OK;
  975. }
  976. #endif /* USE_GSKIT or USE_NSS or USE_GNUTLS or USE_CYASSL or USE_SCHANNEL */
  977. #if defined(USE_GSKIT)
  978. static const char *checkOID(const char *beg, const char *end,
  979. const char *oid)
  980. {
  981. curl_asn1Element e;
  982. const char *ccp;
  983. const char *p;
  984. bool matched;
  985. /* Check if first ASN.1 element at `beg' is the given OID.
  986. Return a pointer in the source after the OID if found, else NULL. */
  987. ccp = getASN1Element(&e, beg, end);
  988. if(!ccp || e.tag != CURL_ASN1_OBJECT_IDENTIFIER)
  989. return (const char *) NULL;
  990. p = OID2str(e.beg, e.end, FALSE);
  991. if(!p)
  992. return (const char *) NULL;
  993. matched = !strcmp(p, oid);
  994. free((char *) p);
  995. return matched? ccp: (const char *) NULL;
  996. }
  997. CURLcode Curl_verifyhost(struct connectdata *conn,
  998. const char *beg, const char *end)
  999. {
  1000. struct Curl_easy *data = conn->data;
  1001. curl_X509certificate cert;
  1002. curl_asn1Element dn;
  1003. curl_asn1Element elem;
  1004. curl_asn1Element ext;
  1005. curl_asn1Element name;
  1006. const char *p;
  1007. const char *q;
  1008. char *dnsname;
  1009. int matched = -1;
  1010. size_t addrlen = (size_t) -1;
  1011. ssize_t len;
  1012. const char * const hostname = SSL_IS_PROXY()? conn->http_proxy.host.name:
  1013. conn->host.name;
  1014. const char * const dispname = SSL_IS_PROXY()?
  1015. conn->http_proxy.host.dispname:
  1016. conn->host.dispname;
  1017. #ifdef ENABLE_IPV6
  1018. struct in6_addr addr;
  1019. #else
  1020. struct in_addr addr;
  1021. #endif
  1022. /* Verify that connection server matches info in X509 certificate at
  1023. `beg'..`end'. */
  1024. if(!SSL_CONN_CONFIG(verifyhost))
  1025. return CURLE_OK;
  1026. if(Curl_parseX509(&cert, beg, end))
  1027. return CURLE_PEER_FAILED_VERIFICATION;
  1028. /* Get the server IP address. */
  1029. #ifdef ENABLE_IPV6
  1030. if(conn->bits.ipv6_ip && Curl_inet_pton(AF_INET6, hostname, &addr))
  1031. addrlen = sizeof(struct in6_addr);
  1032. else
  1033. #endif
  1034. if(Curl_inet_pton(AF_INET, hostname, &addr))
  1035. addrlen = sizeof(struct in_addr);
  1036. /* Process extensions. */
  1037. for(p = cert.extensions.beg; p < cert.extensions.end && matched != 1;) {
  1038. p = getASN1Element(&ext, p, cert.extensions.end);
  1039. if(!p)
  1040. return CURLE_PEER_FAILED_VERIFICATION;
  1041. /* Check if extension is a subjectAlternativeName. */
  1042. ext.beg = checkOID(ext.beg, ext.end, sanOID);
  1043. if(ext.beg) {
  1044. ext.beg = getASN1Element(&elem, ext.beg, ext.end);
  1045. if(!ext.beg)
  1046. return CURLE_PEER_FAILED_VERIFICATION;
  1047. /* Skip critical if present. */
  1048. if(elem.tag == CURL_ASN1_BOOLEAN) {
  1049. ext.beg = getASN1Element(&elem, ext.beg, ext.end);
  1050. if(!ext.beg)
  1051. return CURLE_PEER_FAILED_VERIFICATION;
  1052. }
  1053. /* Parse the octet string contents: is a single sequence. */
  1054. if(!getASN1Element(&elem, elem.beg, elem.end))
  1055. return CURLE_PEER_FAILED_VERIFICATION;
  1056. /* Check all GeneralNames. */
  1057. for(q = elem.beg; matched != 1 && q < elem.end;) {
  1058. q = getASN1Element(&name, q, elem.end);
  1059. if(!q)
  1060. break;
  1061. switch(name.tag) {
  1062. case 2: /* DNS name. */
  1063. len = utf8asn1str(&dnsname, CURL_ASN1_IA5_STRING,
  1064. name.beg, name.end);
  1065. if(len > 0 && (size_t)len == strlen(dnsname))
  1066. matched = Curl_cert_hostcheck(dnsname, hostname);
  1067. else
  1068. matched = 0;
  1069. free(dnsname);
  1070. break;
  1071. case 7: /* IP address. */
  1072. matched = (size_t) (name.end - name.beg) == addrlen &&
  1073. !memcmp(&addr, name.beg, addrlen);
  1074. break;
  1075. }
  1076. }
  1077. }
  1078. }
  1079. switch(matched) {
  1080. case 1:
  1081. /* an alternative name matched the server hostname */
  1082. infof(data, "\t subjectAltName: %s matched\n", dispname);
  1083. return CURLE_OK;
  1084. case 0:
  1085. /* an alternative name field existed, but didn't match and then
  1086. we MUST fail */
  1087. infof(data, "\t subjectAltName does not match %s\n", dispname);
  1088. return CURLE_PEER_FAILED_VERIFICATION;
  1089. }
  1090. /* Process subject. */
  1091. name.header = NULL;
  1092. name.beg = name.end = "";
  1093. q = cert.subject.beg;
  1094. /* we have to look to the last occurrence of a commonName in the
  1095. distinguished one to get the most significant one. */
  1096. while(q < cert.subject.end) {
  1097. q = getASN1Element(&dn, q, cert.subject.end);
  1098. if(!q)
  1099. break;
  1100. for(p = dn.beg; p < dn.end;) {
  1101. p = getASN1Element(&elem, p, dn.end);
  1102. if(!p)
  1103. return CURLE_PEER_FAILED_VERIFICATION;
  1104. /* We have a DN's AttributeTypeAndValue: check it in case it's a CN. */
  1105. elem.beg = checkOID(elem.beg, elem.end, cnOID);
  1106. if(elem.beg)
  1107. name = elem; /* Latch CN. */
  1108. }
  1109. }
  1110. /* Check the CN if found. */
  1111. if(!getASN1Element(&elem, name.beg, name.end))
  1112. failf(data, "SSL: unable to obtain common name from peer certificate");
  1113. else {
  1114. len = utf8asn1str(&dnsname, elem.tag, elem.beg, elem.end);
  1115. if(len < 0) {
  1116. free(dnsname);
  1117. return CURLE_OUT_OF_MEMORY;
  1118. }
  1119. if(strlen(dnsname) != (size_t) len) /* Nul byte in string ? */
  1120. failf(data, "SSL: illegal cert name field");
  1121. else if(Curl_cert_hostcheck((const char *) dnsname, hostname)) {
  1122. infof(data, "\t common name: %s (matched)\n", dnsname);
  1123. free(dnsname);
  1124. return CURLE_OK;
  1125. }
  1126. else
  1127. failf(data, "SSL: certificate subject name '%s' does not match "
  1128. "target host name '%s'", dnsname, dispname);
  1129. free(dnsname);
  1130. }
  1131. return CURLE_PEER_FAILED_VERIFICATION;
  1132. }
  1133. #endif /* USE_GSKIT */