x509asn1.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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.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_WOLFSSL) || 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 struct 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(struct Curl_asn1Element *elem,
  99. const char *beg, const char *end)
  100. WARN_UNUSED_RESULT;
  101. static const char *getASN1Element(struct Curl_asn1Element *elem,
  102. const char *beg, const char *end)
  103. {
  104. unsigned char b;
  105. unsigned long len;
  106. struct 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 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 NULL; /* Long tag values not supported here. */
  122. elem->tag = b;
  123. /* Process length. */
  124. if(beg >= end)
  125. return 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 NULL;
  134. elem->beg = beg;
  135. while(beg < end && *beg) {
  136. beg = getASN1Element(&lelem, beg, end);
  137. if(!beg)
  138. return NULL;
  139. }
  140. if(beg >= end)
  141. return NULL;
  142. elem->end = beg;
  143. return beg + 1;
  144. }
  145. else if((unsigned)b > (size_t)(end - beg))
  146. return NULL; /* Does not fit in source. */
  147. else {
  148. /* Get long length. */
  149. len = 0;
  150. do {
  151. if(len & 0xFF000000L)
  152. return 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 NULL; /* Element data does not fit in source. */
  158. elem->beg = beg;
  159. elem->end = beg + len;
  160. return elem->end;
  161. }
  162. /*
  163. * Search the null terminated OID or OID identifier in local table.
  164. * Return the table entry pointer or NULL if not found.
  165. */
  166. static const struct Curl_OID *searchOID(const char *oid)
  167. {
  168. const struct Curl_OID *op;
  169. for(op = OIDtable; op->numoid; op++)
  170. if(!strcmp(op->numoid, oid) || strcasecompare(op->textoid, oid))
  171. return op;
  172. return NULL;
  173. }
  174. /*
  175. * Convert an ASN.1 Boolean value into its string representation. Return the
  176. * dynamically allocated string, or NULL if source is not an ASN.1 Boolean
  177. * value.
  178. */
  179. static const char *bool2str(const char *beg, const char *end)
  180. {
  181. if(end - beg != 1)
  182. return NULL;
  183. return strdup(*beg? "TRUE": "FALSE");
  184. }
  185. /*
  186. * Convert an ASN.1 octet string to a printable string.
  187. * Return the dynamically allocated string, or NULL if an error occurs.
  188. */
  189. static const char *octet2str(const char *beg, const char *end)
  190. {
  191. size_t n = end - beg;
  192. char *buf = NULL;
  193. if(n <= (SIZE_T_MAX - 1) / 3) {
  194. buf = malloc(3 * n + 1);
  195. if(buf)
  196. for(n = 0; beg < end; n += 3)
  197. msnprintf(buf + n, 4, "%02x:", *(const unsigned char *) beg++);
  198. }
  199. return buf;
  200. }
  201. static const char *bit2str(const char *beg, const char *end)
  202. {
  203. /* Convert an ASN.1 bit string to a printable string.
  204. Return the dynamically allocated string, or NULL if an error occurs. */
  205. if(++beg > end)
  206. return NULL;
  207. return octet2str(beg, end);
  208. }
  209. /*
  210. * Convert an ASN.1 integer value into its string representation.
  211. * Return the dynamically allocated string, or NULL if source is not an
  212. * ASN.1 integer value.
  213. */
  214. static const char *int2str(const char *beg, const char *end)
  215. {
  216. unsigned long val = 0;
  217. size_t n = end - beg;
  218. if(!n)
  219. return NULL;
  220. if(n > 4)
  221. return octet2str(beg, end);
  222. /* Represent integers <= 32-bit as a single value. */
  223. if(*beg & 0x80)
  224. val = ~val;
  225. do
  226. val = (val << 8) | *(const unsigned char *) beg++;
  227. while(beg < end);
  228. return curl_maprintf("%s%lx", val >= 10? "0x": "", val);
  229. }
  230. /*
  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. */
  237. static ssize_t
  238. utf8asn1str(char **to, int type, const char *from, const char *end)
  239. {
  240. size_t inlength = end - from;
  241. int size = 1;
  242. size_t outlength;
  243. char *buf;
  244. *to = NULL;
  245. switch(type) {
  246. case CURL_ASN1_BMP_STRING:
  247. size = 2;
  248. break;
  249. case CURL_ASN1_UNIVERSAL_STRING:
  250. size = 4;
  251. break;
  252. case CURL_ASN1_NUMERIC_STRING:
  253. case CURL_ASN1_PRINTABLE_STRING:
  254. case CURL_ASN1_TELETEX_STRING:
  255. case CURL_ASN1_IA5_STRING:
  256. case CURL_ASN1_VISIBLE_STRING:
  257. case CURL_ASN1_UTF8_STRING:
  258. break;
  259. default:
  260. return -1; /* Conversion not supported. */
  261. }
  262. if(inlength % size)
  263. return -1; /* Length inconsistent with character size. */
  264. if(inlength / size > (SIZE_T_MAX - 1) / 4)
  265. return -1; /* Too big. */
  266. buf = malloc(4 * (inlength / size) + 1);
  267. if(!buf)
  268. return -1; /* Not enough memory. */
  269. if(type == CURL_ASN1_UTF8_STRING) {
  270. /* Just copy. */
  271. outlength = inlength;
  272. if(outlength)
  273. memcpy(buf, from, outlength);
  274. }
  275. else {
  276. for(outlength = 0; from < end;) {
  277. int charsize;
  278. unsigned int wc;
  279. wc = 0;
  280. switch(size) {
  281. case 4:
  282. wc = (wc << 8) | *(const unsigned char *) from++;
  283. wc = (wc << 8) | *(const unsigned char *) from++;
  284. /* FALLTHROUGH */
  285. case 2:
  286. wc = (wc << 8) | *(const unsigned char *) from++;
  287. /* FALLTHROUGH */
  288. default: /* case 1: */
  289. wc = (wc << 8) | *(const unsigned char *) from++;
  290. }
  291. charsize = 1;
  292. if(wc >= 0x00000080) {
  293. if(wc >= 0x00000800) {
  294. if(wc >= 0x00010000) {
  295. if(wc >= 0x00200000) {
  296. free(buf);
  297. return -1; /* Invalid char. size for target encoding. */
  298. }
  299. buf[outlength + 3] = (char) (0x80 | (wc & 0x3F));
  300. wc = (wc >> 6) | 0x00010000;
  301. charsize++;
  302. }
  303. buf[outlength + 2] = (char) (0x80 | (wc & 0x3F));
  304. wc = (wc >> 6) | 0x00000800;
  305. charsize++;
  306. }
  307. buf[outlength + 1] = (char) (0x80 | (wc & 0x3F));
  308. wc = (wc >> 6) | 0x000000C0;
  309. charsize++;
  310. }
  311. buf[outlength] = (char) wc;
  312. outlength += charsize;
  313. }
  314. }
  315. buf[outlength] = '\0';
  316. *to = buf;
  317. return outlength;
  318. }
  319. /*
  320. * Convert an ASN.1 String into its UTF-8 string representation.
  321. * Return the dynamically allocated string, or NULL if an error occurs.
  322. */
  323. static const char *string2str(int type, const char *beg, const char *end)
  324. {
  325. char *buf;
  326. if(utf8asn1str(&buf, type, beg, end) < 0)
  327. return NULL;
  328. return buf;
  329. }
  330. /*
  331. * Decimal ASCII encode unsigned integer `x' into the buflen sized buffer at
  332. * buf. Return the total number of encoded digits, even if larger than
  333. * `buflen'.
  334. */
  335. static size_t encodeUint(char *buf, size_t buflen, unsigned int x)
  336. {
  337. size_t i = 0;
  338. unsigned int y = x / 10;
  339. if(y) {
  340. i = encodeUint(buf, buflen, y);
  341. x -= y * 10;
  342. }
  343. if(i < buflen)
  344. buf[i] = (char) ('0' + x);
  345. i++;
  346. if(i < buflen)
  347. buf[i] = '\0'; /* Store a terminator if possible. */
  348. return i;
  349. }
  350. /*
  351. * Convert an ASN.1 OID into its dotted string representation.
  352. * Store the result in th `n'-byte buffer at `buf'.
  353. * Return the converted string length, or 0 on errors.
  354. */
  355. static size_t encodeOID(char *buf, size_t buflen,
  356. const char *beg, const char *end)
  357. {
  358. size_t i;
  359. unsigned int x;
  360. unsigned int y;
  361. /* Process the first two numbers. */
  362. y = *(const unsigned char *) beg++;
  363. x = y / 40;
  364. y -= x * 40;
  365. i = encodeUint(buf, buflen, x);
  366. if(i < buflen)
  367. buf[i] = '.';
  368. i++;
  369. if(i >= buflen)
  370. i += encodeUint(NULL, 0, y);
  371. else
  372. i += encodeUint(buf + i, buflen - i, y);
  373. /* Process the trailing numbers. */
  374. while(beg < end) {
  375. if(i < buflen)
  376. buf[i] = '.';
  377. i++;
  378. x = 0;
  379. do {
  380. if(x & 0xFF000000)
  381. return 0;
  382. y = *(const unsigned char *) beg++;
  383. x = (x << 7) | (y & 0x7F);
  384. } while(y & 0x80);
  385. if(i >= buflen)
  386. i += encodeUint(NULL, 0, x);
  387. else
  388. i += encodeUint(buf + i, buflen - i, x);
  389. }
  390. if(i < buflen)
  391. buf[i] = '\0';
  392. return i;
  393. }
  394. /*
  395. * Convert an ASN.1 OID into its dotted or symbolic string representation.
  396. * Return the dynamically allocated string, or NULL if an error occurs.
  397. */
  398. static const char *OID2str(const char *beg, const char *end, bool symbolic)
  399. {
  400. char *buf = NULL;
  401. if(beg < end) {
  402. size_t buflen = encodeOID(NULL, 0, beg, end);
  403. if(buflen) {
  404. buf = malloc(buflen + 1); /* one extra for the zero byte */
  405. if(buf) {
  406. encodeOID(buf, buflen, beg, end);
  407. buf[buflen] = '\0';
  408. if(symbolic) {
  409. const struct Curl_OID *op = searchOID(buf);
  410. if(op) {
  411. free(buf);
  412. buf = strdup(op->textoid);
  413. }
  414. }
  415. }
  416. }
  417. }
  418. return buf;
  419. }
  420. static const char *GTime2str(const char *beg, const char *end)
  421. {
  422. const char *tzp;
  423. const char *fracp;
  424. char sec1, sec2;
  425. size_t fracl;
  426. size_t tzl;
  427. const char *sep = "";
  428. /* Convert an ASN.1 Generalized time to a printable string.
  429. Return the dynamically allocated string, or NULL if an error occurs. */
  430. for(fracp = beg; fracp < end && *fracp >= '0' && *fracp <= '9'; fracp++)
  431. ;
  432. /* Get seconds digits. */
  433. sec1 = '0';
  434. switch(fracp - beg - 12) {
  435. case 0:
  436. sec2 = '0';
  437. break;
  438. case 2:
  439. sec1 = fracp[-2];
  440. /* FALLTHROUGH */
  441. case 1:
  442. sec2 = fracp[-1];
  443. break;
  444. default:
  445. return NULL;
  446. }
  447. /* Scan for timezone, measure fractional seconds. */
  448. tzp = fracp;
  449. fracl = 0;
  450. if(fracp < end && (*fracp == '.' || *fracp == ',')) {
  451. fracp++;
  452. do
  453. tzp++;
  454. while(tzp < end && *tzp >= '0' && *tzp <= '9');
  455. /* Strip leading zeroes in fractional seconds. */
  456. for(fracl = tzp - fracp - 1; fracl && fracp[fracl - 1] == '0'; fracl--)
  457. ;
  458. }
  459. /* Process timezone. */
  460. if(tzp >= end)
  461. ; /* Nothing to do. */
  462. else if(*tzp == 'Z') {
  463. tzp = " GMT";
  464. end = tzp + 4;
  465. }
  466. else {
  467. sep = " ";
  468. tzp++;
  469. }
  470. tzl = end - tzp;
  471. return curl_maprintf("%.4s-%.2s-%.2s %.2s:%.2s:%c%c%s%.*s%s%.*s",
  472. beg, beg + 4, beg + 6,
  473. beg + 8, beg + 10, sec1, sec2,
  474. fracl? ".": "", fracl, fracp,
  475. sep, tzl, tzp);
  476. }
  477. /*
  478. * Convert an ASN.1 UTC time to a printable string.
  479. * Return the dynamically allocated string, or NULL if an error occurs.
  480. */
  481. static const char *UTime2str(const char *beg, const char *end)
  482. {
  483. const char *tzp;
  484. size_t tzl;
  485. const char *sec;
  486. for(tzp = beg; tzp < end && *tzp >= '0' && *tzp <= '9'; tzp++)
  487. ;
  488. /* Get the seconds. */
  489. sec = beg + 10;
  490. switch(tzp - sec) {
  491. case 0:
  492. sec = "00";
  493. case 2:
  494. break;
  495. default:
  496. return NULL;
  497. }
  498. /* Process timezone. */
  499. if(tzp >= end)
  500. return NULL;
  501. if(*tzp == 'Z') {
  502. tzp = "GMT";
  503. end = tzp + 3;
  504. }
  505. else
  506. tzp++;
  507. tzl = end - tzp;
  508. return curl_maprintf("%u%.2s-%.2s-%.2s %.2s:%.2s:%.2s %.*s",
  509. 20 - (*beg >= '5'), beg, beg + 2, beg + 4,
  510. beg + 6, beg + 8, sec,
  511. tzl, tzp);
  512. }
  513. /*
  514. * Convert an ASN.1 element to a printable string.
  515. * Return the dynamically allocated string, or NULL if an error occurs.
  516. */
  517. static const char *ASN1tostr(struct Curl_asn1Element *elem, int type)
  518. {
  519. if(elem->constructed)
  520. return NULL; /* No conversion of structured elements. */
  521. if(!type)
  522. type = elem->tag; /* Type not forced: use element tag as type. */
  523. switch(type) {
  524. case CURL_ASN1_BOOLEAN:
  525. return bool2str(elem->beg, elem->end);
  526. case CURL_ASN1_INTEGER:
  527. case CURL_ASN1_ENUMERATED:
  528. return int2str(elem->beg, elem->end);
  529. case CURL_ASN1_BIT_STRING:
  530. return bit2str(elem->beg, elem->end);
  531. case CURL_ASN1_OCTET_STRING:
  532. return octet2str(elem->beg, elem->end);
  533. case CURL_ASN1_NULL:
  534. return strdup("");
  535. case CURL_ASN1_OBJECT_IDENTIFIER:
  536. return OID2str(elem->beg, elem->end, TRUE);
  537. case CURL_ASN1_UTC_TIME:
  538. return UTime2str(elem->beg, elem->end);
  539. case CURL_ASN1_GENERALIZED_TIME:
  540. return GTime2str(elem->beg, elem->end);
  541. case CURL_ASN1_UTF8_STRING:
  542. case CURL_ASN1_NUMERIC_STRING:
  543. case CURL_ASN1_PRINTABLE_STRING:
  544. case CURL_ASN1_TELETEX_STRING:
  545. case CURL_ASN1_IA5_STRING:
  546. case CURL_ASN1_VISIBLE_STRING:
  547. case CURL_ASN1_UNIVERSAL_STRING:
  548. case CURL_ASN1_BMP_STRING:
  549. return string2str(type, elem->beg, elem->end);
  550. }
  551. return NULL; /* Unsupported. */
  552. }
  553. /*
  554. * ASCII encode distinguished name at `dn' into the `buflen'-sized buffer at
  555. * `buf'. Return the total string length, even if larger than `buflen'.
  556. */
  557. static ssize_t encodeDN(char *buf, size_t buflen, struct Curl_asn1Element *dn)
  558. {
  559. struct Curl_asn1Element rdn;
  560. struct Curl_asn1Element atv;
  561. struct Curl_asn1Element oid;
  562. struct Curl_asn1Element value;
  563. size_t l = 0;
  564. const char *p1;
  565. const char *p2;
  566. const char *p3;
  567. const char *str;
  568. for(p1 = dn->beg; p1 < dn->end;) {
  569. p1 = getASN1Element(&rdn, p1, dn->end);
  570. if(!p1)
  571. return -1;
  572. for(p2 = rdn.beg; p2 < rdn.end;) {
  573. p2 = getASN1Element(&atv, p2, rdn.end);
  574. if(!p2)
  575. return -1;
  576. p3 = getASN1Element(&oid, atv.beg, atv.end);
  577. if(!p3)
  578. return -1;
  579. if(!getASN1Element(&value, p3, atv.end))
  580. return -1;
  581. str = ASN1tostr(&oid, 0);
  582. if(!str)
  583. return -1;
  584. /* Encode delimiter.
  585. If attribute has a short uppercase name, delimiter is ", ". */
  586. if(l) {
  587. for(p3 = str; isupper(*p3); p3++)
  588. ;
  589. for(p3 = (*p3 || p3 - str > 2)? "/": ", "; *p3; p3++) {
  590. if(l < buflen)
  591. buf[l] = *p3;
  592. l++;
  593. }
  594. }
  595. /* Encode attribute name. */
  596. for(p3 = str; *p3; p3++) {
  597. if(l < buflen)
  598. buf[l] = *p3;
  599. l++;
  600. }
  601. free((char *) str);
  602. /* Generate equal sign. */
  603. if(l < buflen)
  604. buf[l] = '=';
  605. l++;
  606. /* Generate value. */
  607. str = ASN1tostr(&value, 0);
  608. if(!str)
  609. return -1;
  610. for(p3 = str; *p3; p3++) {
  611. if(l < buflen)
  612. buf[l] = *p3;
  613. l++;
  614. }
  615. free((char *) str);
  616. }
  617. }
  618. return l;
  619. }
  620. /*
  621. * Convert an ASN.1 distinguished name into a printable string.
  622. * Return the dynamically allocated string, or NULL if an error occurs.
  623. */
  624. static const char *DNtostr(struct Curl_asn1Element *dn)
  625. {
  626. char *buf = NULL;
  627. ssize_t buflen = encodeDN(NULL, 0, dn);
  628. if(buflen >= 0) {
  629. buf = malloc(buflen + 1);
  630. if(buf) {
  631. encodeDN(buf, buflen + 1, dn);
  632. buf[buflen] = '\0';
  633. }
  634. }
  635. return buf;
  636. }
  637. /*
  638. * ASN.1 parse an X509 certificate into structure subfields.
  639. * Syntax is assumed to have already been checked by the SSL backend.
  640. * See RFC 5280.
  641. */
  642. int Curl_parseX509(struct Curl_X509certificate *cert,
  643. const char *beg, const char *end)
  644. {
  645. struct Curl_asn1Element elem;
  646. struct Curl_asn1Element tbsCertificate;
  647. const char *ccp;
  648. static const char defaultVersion = 0; /* v1. */
  649. cert->certificate.header = NULL;
  650. cert->certificate.beg = beg;
  651. cert->certificate.end = end;
  652. /* Get the sequence content. */
  653. if(!getASN1Element(&elem, beg, end))
  654. return -1; /* Invalid bounds/size. */
  655. beg = elem.beg;
  656. end = elem.end;
  657. /* Get tbsCertificate. */
  658. beg = getASN1Element(&tbsCertificate, beg, end);
  659. if(!beg)
  660. return -1;
  661. /* Skip the signatureAlgorithm. */
  662. beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
  663. if(!beg)
  664. return -1;
  665. /* Get the signatureValue. */
  666. if(!getASN1Element(&cert->signature, beg, end))
  667. return -1;
  668. /* Parse TBSCertificate. */
  669. beg = tbsCertificate.beg;
  670. end = tbsCertificate.end;
  671. /* Get optional version, get serialNumber. */
  672. cert->version.header = NULL;
  673. cert->version.beg = &defaultVersion;
  674. cert->version.end = &defaultVersion + sizeof(defaultVersion);
  675. beg = getASN1Element(&elem, beg, end);
  676. if(!beg)
  677. return -1;
  678. if(elem.tag == 0) {
  679. if(!getASN1Element(&cert->version, elem.beg, elem.end))
  680. return -1;
  681. beg = getASN1Element(&elem, beg, end);
  682. if(!beg)
  683. return -1;
  684. }
  685. cert->serialNumber = elem;
  686. /* Get signature algorithm. */
  687. beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
  688. /* Get issuer. */
  689. beg = getASN1Element(&cert->issuer, beg, end);
  690. if(!beg)
  691. return -1;
  692. /* Get notBefore and notAfter. */
  693. beg = getASN1Element(&elem, beg, end);
  694. if(!beg)
  695. return -1;
  696. ccp = getASN1Element(&cert->notBefore, elem.beg, elem.end);
  697. if(!ccp)
  698. return -1;
  699. if(!getASN1Element(&cert->notAfter, ccp, elem.end))
  700. return -1;
  701. /* Get subject. */
  702. beg = getASN1Element(&cert->subject, beg, end);
  703. if(!beg)
  704. return -1;
  705. /* Get subjectPublicKeyAlgorithm and subjectPublicKey. */
  706. beg = getASN1Element(&cert->subjectPublicKeyInfo, beg, end);
  707. if(!beg)
  708. return -1;
  709. ccp = getASN1Element(&cert->subjectPublicKeyAlgorithm,
  710. cert->subjectPublicKeyInfo.beg,
  711. cert->subjectPublicKeyInfo.end);
  712. if(!ccp)
  713. return -1;
  714. if(!getASN1Element(&cert->subjectPublicKey, ccp,
  715. cert->subjectPublicKeyInfo.end))
  716. return -1;
  717. /* Get optional issuerUiqueID, subjectUniqueID and extensions. */
  718. cert->issuerUniqueID.tag = cert->subjectUniqueID.tag = 0;
  719. cert->extensions.tag = elem.tag = 0;
  720. cert->issuerUniqueID.header = cert->subjectUniqueID.header = NULL;
  721. cert->issuerUniqueID.beg = cert->issuerUniqueID.end = "";
  722. cert->subjectUniqueID.beg = cert->subjectUniqueID.end = "";
  723. cert->extensions.header = NULL;
  724. cert->extensions.beg = cert->extensions.end = "";
  725. if(beg < end) {
  726. beg = getASN1Element(&elem, beg, end);
  727. if(!beg)
  728. return -1;
  729. }
  730. if(elem.tag == 1) {
  731. cert->issuerUniqueID = elem;
  732. if(beg < end) {
  733. beg = getASN1Element(&elem, beg, end);
  734. if(!beg)
  735. return -1;
  736. }
  737. }
  738. if(elem.tag == 2) {
  739. cert->subjectUniqueID = elem;
  740. if(beg < end) {
  741. beg = getASN1Element(&elem, beg, end);
  742. if(!beg)
  743. return -1;
  744. }
  745. }
  746. if(elem.tag == 3)
  747. if(!getASN1Element(&cert->extensions, elem.beg, elem.end))
  748. return -1;
  749. return 0;
  750. }
  751. /*
  752. * Copy at most 64-characters, terminate with a newline and returns the
  753. * effective number of stored characters.
  754. */
  755. static size_t copySubstring(char *to, const char *from)
  756. {
  757. size_t i;
  758. for(i = 0; i < 64; i++) {
  759. to[i] = *from;
  760. if(!*from++)
  761. break;
  762. }
  763. to[i++] = '\n';
  764. return i;
  765. }
  766. static const char *dumpAlgo(struct Curl_asn1Element *param,
  767. const char *beg, const char *end)
  768. {
  769. struct Curl_asn1Element oid;
  770. /* Get algorithm parameters and return algorithm name. */
  771. beg = getASN1Element(&oid, beg, end);
  772. if(!beg)
  773. return NULL;
  774. param->header = NULL;
  775. param->tag = 0;
  776. param->beg = param->end = end;
  777. if(beg < end)
  778. if(!getASN1Element(param, beg, end))
  779. return NULL;
  780. return OID2str(oid.beg, oid.end, TRUE);
  781. }
  782. static void do_pubkey_field(struct Curl_easy *data, int certnum,
  783. const char *label, struct Curl_asn1Element *elem)
  784. {
  785. const char *output;
  786. /* Generate a certificate information record for the public key. */
  787. output = ASN1tostr(elem, 0);
  788. if(output) {
  789. if(data->set.ssl.certinfo)
  790. Curl_ssl_push_certinfo(data, certnum, label, output);
  791. if(!certnum)
  792. infof(data, " %s: %s\n", label, output);
  793. free((char *) output);
  794. }
  795. }
  796. static void do_pubkey(struct Curl_easy *data, int certnum,
  797. const char *algo, struct Curl_asn1Element *param,
  798. struct Curl_asn1Element *pubkey)
  799. {
  800. struct Curl_asn1Element elem;
  801. struct Curl_asn1Element pk;
  802. const char *p;
  803. /* Generate all information records for the public key. */
  804. /* Get the public key (single element). */
  805. if(!getASN1Element(&pk, pubkey->beg + 1, pubkey->end))
  806. return;
  807. if(strcasecompare(algo, "rsaEncryption")) {
  808. const char *q;
  809. unsigned long len;
  810. p = getASN1Element(&elem, pk.beg, pk.end);
  811. if(!p)
  812. return;
  813. /* Compute key length. */
  814. for(q = elem.beg; !*q && q < elem.end; q++)
  815. ;
  816. len = (unsigned long)((elem.end - q) * 8);
  817. if(len) {
  818. unsigned int i;
  819. for(i = *(unsigned char *) q; !(i & 0x80); i <<= 1)
  820. len--;
  821. }
  822. if(len > 32)
  823. elem.beg = q; /* Strip leading zero bytes. */
  824. if(!certnum)
  825. infof(data, " RSA Public Key (%lu bits)\n", len);
  826. if(data->set.ssl.certinfo) {
  827. q = curl_maprintf("%lu", len);
  828. if(q) {
  829. Curl_ssl_push_certinfo(data, certnum, "RSA Public Key", q);
  830. free((char *) q);
  831. }
  832. }
  833. /* Generate coefficients. */
  834. do_pubkey_field(data, certnum, "rsa(n)", &elem);
  835. if(!getASN1Element(&elem, p, pk.end))
  836. return;
  837. do_pubkey_field(data, certnum, "rsa(e)", &elem);
  838. }
  839. else if(strcasecompare(algo, "dsa")) {
  840. p = getASN1Element(&elem, param->beg, param->end);
  841. if(p) {
  842. do_pubkey_field(data, certnum, "dsa(p)", &elem);
  843. p = getASN1Element(&elem, p, param->end);
  844. if(p) {
  845. do_pubkey_field(data, certnum, "dsa(q)", &elem);
  846. if(getASN1Element(&elem, p, param->end)) {
  847. do_pubkey_field(data, certnum, "dsa(g)", &elem);
  848. do_pubkey_field(data, certnum, "dsa(pub_key)", &pk);
  849. }
  850. }
  851. }
  852. }
  853. else if(strcasecompare(algo, "dhpublicnumber")) {
  854. p = getASN1Element(&elem, param->beg, param->end);
  855. if(p) {
  856. do_pubkey_field(data, certnum, "dh(p)", &elem);
  857. if(getASN1Element(&elem, param->beg, param->end)) {
  858. do_pubkey_field(data, certnum, "dh(g)", &elem);
  859. do_pubkey_field(data, certnum, "dh(pub_key)", &pk);
  860. }
  861. }
  862. }
  863. }
  864. CURLcode Curl_extract_certinfo(struct Curl_easy *data,
  865. int certnum,
  866. const char *beg,
  867. const char *end)
  868. {
  869. struct Curl_X509certificate cert;
  870. struct Curl_asn1Element param;
  871. const char *ccp;
  872. char *cp1;
  873. size_t cl1;
  874. char *cp2;
  875. CURLcode result;
  876. unsigned long version;
  877. size_t i;
  878. size_t j;
  879. if(!data->set.ssl.certinfo)
  880. if(certnum)
  881. return CURLE_OK;
  882. /* Prepare the certificate information for curl_easy_getinfo(). */
  883. /* Extract the certificate ASN.1 elements. */
  884. if(Curl_parseX509(&cert, beg, end))
  885. return CURLE_PEER_FAILED_VERIFICATION;
  886. /* Subject. */
  887. ccp = DNtostr(&cert.subject);
  888. if(!ccp)
  889. return CURLE_OUT_OF_MEMORY;
  890. if(data->set.ssl.certinfo)
  891. Curl_ssl_push_certinfo(data, certnum, "Subject", ccp);
  892. if(!certnum)
  893. infof(data, "%2d Subject: %s\n", certnum, ccp);
  894. free((char *) ccp);
  895. /* Issuer. */
  896. ccp = DNtostr(&cert.issuer);
  897. if(!ccp)
  898. return CURLE_OUT_OF_MEMORY;
  899. if(data->set.ssl.certinfo)
  900. Curl_ssl_push_certinfo(data, certnum, "Issuer", ccp);
  901. if(!certnum)
  902. infof(data, " Issuer: %s\n", ccp);
  903. free((char *) ccp);
  904. /* Version (always fits in less than 32 bits). */
  905. version = 0;
  906. for(ccp = cert.version.beg; ccp < cert.version.end; ccp++)
  907. version = (version << 8) | *(const unsigned char *) ccp;
  908. if(data->set.ssl.certinfo) {
  909. ccp = curl_maprintf("%lx", version);
  910. if(!ccp)
  911. return CURLE_OUT_OF_MEMORY;
  912. Curl_ssl_push_certinfo(data, certnum, "Version", ccp);
  913. free((char *) ccp);
  914. }
  915. if(!certnum)
  916. infof(data, " Version: %lu (0x%lx)\n", version + 1, version);
  917. /* Serial number. */
  918. ccp = ASN1tostr(&cert.serialNumber, 0);
  919. if(!ccp)
  920. return CURLE_OUT_OF_MEMORY;
  921. if(data->set.ssl.certinfo)
  922. Curl_ssl_push_certinfo(data, certnum, "Serial Number", ccp);
  923. if(!certnum)
  924. infof(data, " Serial Number: %s\n", ccp);
  925. free((char *) ccp);
  926. /* Signature algorithm .*/
  927. ccp = dumpAlgo(&param, cert.signatureAlgorithm.beg,
  928. cert.signatureAlgorithm.end);
  929. if(!ccp)
  930. return CURLE_OUT_OF_MEMORY;
  931. if(data->set.ssl.certinfo)
  932. Curl_ssl_push_certinfo(data, certnum, "Signature Algorithm", ccp);
  933. if(!certnum)
  934. infof(data, " Signature Algorithm: %s\n", ccp);
  935. free((char *) ccp);
  936. /* Start Date. */
  937. ccp = ASN1tostr(&cert.notBefore, 0);
  938. if(!ccp)
  939. return CURLE_OUT_OF_MEMORY;
  940. if(data->set.ssl.certinfo)
  941. Curl_ssl_push_certinfo(data, certnum, "Start Date", ccp);
  942. if(!certnum)
  943. infof(data, " Start Date: %s\n", ccp);
  944. free((char *) ccp);
  945. /* Expire Date. */
  946. ccp = ASN1tostr(&cert.notAfter, 0);
  947. if(!ccp)
  948. return CURLE_OUT_OF_MEMORY;
  949. if(data->set.ssl.certinfo)
  950. Curl_ssl_push_certinfo(data, certnum, "Expire Date", ccp);
  951. if(!certnum)
  952. infof(data, " Expire Date: %s\n", ccp);
  953. free((char *) ccp);
  954. /* Public Key Algorithm. */
  955. ccp = dumpAlgo(&param, cert.subjectPublicKeyAlgorithm.beg,
  956. cert.subjectPublicKeyAlgorithm.end);
  957. if(!ccp)
  958. return CURLE_OUT_OF_MEMORY;
  959. if(data->set.ssl.certinfo)
  960. Curl_ssl_push_certinfo(data, certnum, "Public Key Algorithm", ccp);
  961. if(!certnum)
  962. infof(data, " Public Key Algorithm: %s\n", ccp);
  963. do_pubkey(data, certnum, ccp, &param, &cert.subjectPublicKey);
  964. free((char *) ccp);
  965. /* Signature. */
  966. ccp = ASN1tostr(&cert.signature, 0);
  967. if(!ccp)
  968. return CURLE_OUT_OF_MEMORY;
  969. if(data->set.ssl.certinfo)
  970. Curl_ssl_push_certinfo(data, certnum, "Signature", ccp);
  971. if(!certnum)
  972. infof(data, " Signature: %s\n", ccp);
  973. free((char *) ccp);
  974. /* Generate PEM certificate. */
  975. result = Curl_base64_encode(data, cert.certificate.beg,
  976. cert.certificate.end - cert.certificate.beg,
  977. &cp1, &cl1);
  978. if(result)
  979. return result;
  980. /* Compute the number of characters in final certificate string. Format is:
  981. -----BEGIN CERTIFICATE-----\n
  982. <max 64 base64 characters>\n
  983. .
  984. .
  985. .
  986. -----END CERTIFICATE-----\n
  987. */
  988. i = 28 + cl1 + (cl1 + 64 - 1) / 64 + 26;
  989. cp2 = malloc(i + 1);
  990. if(!cp2) {
  991. free(cp1);
  992. return CURLE_OUT_OF_MEMORY;
  993. }
  994. /* Build the certificate string. */
  995. i = copySubstring(cp2, "-----BEGIN CERTIFICATE-----");
  996. for(j = 0; j < cl1; j += 64)
  997. i += copySubstring(cp2 + i, cp1 + j);
  998. i += copySubstring(cp2 + i, "-----END CERTIFICATE-----");
  999. cp2[i] = '\0';
  1000. free(cp1);
  1001. if(data->set.ssl.certinfo)
  1002. Curl_ssl_push_certinfo(data, certnum, "Cert", cp2);
  1003. if(!certnum)
  1004. infof(data, "%s\n", cp2);
  1005. free(cp2);
  1006. return CURLE_OK;
  1007. }
  1008. #endif /* USE_GSKIT or USE_NSS or USE_GNUTLS or USE_WOLFSSL or USE_SCHANNEL */
  1009. #if defined(USE_GSKIT)
  1010. static const char *checkOID(const char *beg, const char *end,
  1011. const char *oid)
  1012. {
  1013. struct Curl_asn1Element e;
  1014. const char *ccp;
  1015. const char *p;
  1016. bool matched;
  1017. /* Check if first ASN.1 element at `beg' is the given OID.
  1018. Return a pointer in the source after the OID if found, else NULL. */
  1019. ccp = getASN1Element(&e, beg, end);
  1020. if(!ccp || e.tag != CURL_ASN1_OBJECT_IDENTIFIER)
  1021. return NULL;
  1022. p = OID2str(e.beg, e.end, FALSE);
  1023. if(!p)
  1024. return NULL;
  1025. matched = !strcmp(p, oid);
  1026. free((char *) p);
  1027. return matched? ccp: NULL;
  1028. }
  1029. CURLcode Curl_verifyhost(struct Curl_easy *data, struct connectdata *conn,
  1030. const char *beg, const char *end)
  1031. {
  1032. struct Curl_X509certificate cert;
  1033. struct Curl_asn1Element dn;
  1034. struct Curl_asn1Element elem;
  1035. struct Curl_asn1Element ext;
  1036. struct Curl_asn1Element name;
  1037. const char *p;
  1038. const char *q;
  1039. char *dnsname;
  1040. int matched = -1;
  1041. size_t addrlen = (size_t) -1;
  1042. ssize_t len;
  1043. const char *const hostname = SSL_IS_PROXY()?
  1044. conn->http_proxy.host.name : conn->host.name;
  1045. const char *const dispname = SSL_IS_PROXY()?
  1046. conn->http_proxy.host.dispname : conn->host.dispname;
  1047. #ifdef ENABLE_IPV6
  1048. struct in6_addr addr;
  1049. #else
  1050. struct in_addr addr;
  1051. #endif
  1052. /* Verify that connection server matches info in X509 certificate at
  1053. `beg'..`end'. */
  1054. if(!SSL_CONN_CONFIG(verifyhost))
  1055. return CURLE_OK;
  1056. if(Curl_parseX509(&cert, beg, end))
  1057. return CURLE_PEER_FAILED_VERIFICATION;
  1058. /* Get the server IP address. */
  1059. #ifdef ENABLE_IPV6
  1060. if(conn->bits.ipv6_ip && Curl_inet_pton(AF_INET6, hostname, &addr))
  1061. addrlen = sizeof(struct in6_addr);
  1062. else
  1063. #endif
  1064. if(Curl_inet_pton(AF_INET, hostname, &addr))
  1065. addrlen = sizeof(struct in_addr);
  1066. /* Process extensions. */
  1067. for(p = cert.extensions.beg; p < cert.extensions.end && matched != 1;) {
  1068. p = getASN1Element(&ext, p, cert.extensions.end);
  1069. if(!p)
  1070. return CURLE_PEER_FAILED_VERIFICATION;
  1071. /* Check if extension is a subjectAlternativeName. */
  1072. ext.beg = checkOID(ext.beg, ext.end, sanOID);
  1073. if(ext.beg) {
  1074. ext.beg = getASN1Element(&elem, ext.beg, ext.end);
  1075. if(!ext.beg)
  1076. return CURLE_PEER_FAILED_VERIFICATION;
  1077. /* Skip critical if present. */
  1078. if(elem.tag == CURL_ASN1_BOOLEAN) {
  1079. ext.beg = getASN1Element(&elem, ext.beg, ext.end);
  1080. if(!ext.beg)
  1081. return CURLE_PEER_FAILED_VERIFICATION;
  1082. }
  1083. /* Parse the octet string contents: is a single sequence. */
  1084. if(!getASN1Element(&elem, elem.beg, elem.end))
  1085. return CURLE_PEER_FAILED_VERIFICATION;
  1086. /* Check all GeneralNames. */
  1087. for(q = elem.beg; matched != 1 && q < elem.end;) {
  1088. q = getASN1Element(&name, q, elem.end);
  1089. if(!q)
  1090. break;
  1091. switch(name.tag) {
  1092. case 2: /* DNS name. */
  1093. len = utf8asn1str(&dnsname, CURL_ASN1_IA5_STRING,
  1094. name.beg, name.end);
  1095. if(len > 0 && (size_t)len == strlen(dnsname))
  1096. matched = Curl_cert_hostcheck(dnsname, hostname);
  1097. else
  1098. matched = 0;
  1099. free(dnsname);
  1100. break;
  1101. case 7: /* IP address. */
  1102. matched = (size_t) (name.end - name.beg) == addrlen &&
  1103. !memcmp(&addr, name.beg, addrlen);
  1104. break;
  1105. }
  1106. }
  1107. }
  1108. }
  1109. switch(matched) {
  1110. case 1:
  1111. /* an alternative name matched the server hostname */
  1112. infof(data, "\t subjectAltName: %s matched\n", dispname);
  1113. return CURLE_OK;
  1114. case 0:
  1115. /* an alternative name field existed, but didn't match and then
  1116. we MUST fail */
  1117. infof(data, "\t subjectAltName does not match %s\n", dispname);
  1118. return CURLE_PEER_FAILED_VERIFICATION;
  1119. }
  1120. /* Process subject. */
  1121. name.header = NULL;
  1122. name.beg = name.end = "";
  1123. q = cert.subject.beg;
  1124. /* we have to look to the last occurrence of a commonName in the
  1125. distinguished one to get the most significant one. */
  1126. while(q < cert.subject.end) {
  1127. q = getASN1Element(&dn, q, cert.subject.end);
  1128. if(!q)
  1129. break;
  1130. for(p = dn.beg; p < dn.end;) {
  1131. p = getASN1Element(&elem, p, dn.end);
  1132. if(!p)
  1133. return CURLE_PEER_FAILED_VERIFICATION;
  1134. /* We have a DN's AttributeTypeAndValue: check it in case it's a CN. */
  1135. elem.beg = checkOID(elem.beg, elem.end, cnOID);
  1136. if(elem.beg)
  1137. name = elem; /* Latch CN. */
  1138. }
  1139. }
  1140. /* Check the CN if found. */
  1141. if(!getASN1Element(&elem, name.beg, name.end))
  1142. failf(data, "SSL: unable to obtain common name from peer certificate");
  1143. else {
  1144. len = utf8asn1str(&dnsname, elem.tag, elem.beg, elem.end);
  1145. if(len < 0) {
  1146. free(dnsname);
  1147. return CURLE_OUT_OF_MEMORY;
  1148. }
  1149. if(strlen(dnsname) != (size_t) len) /* Nul byte in string ? */
  1150. failf(data, "SSL: illegal cert name field");
  1151. else if(Curl_cert_hostcheck((const char *) dnsname, hostname)) {
  1152. infof(data, "\t common name: %s (matched)\n", dnsname);
  1153. free(dnsname);
  1154. return CURLE_OK;
  1155. }
  1156. else
  1157. failf(data, "SSL: certificate subject name '%s' does not match "
  1158. "target host name '%s'", dnsname, dispname);
  1159. free(dnsname);
  1160. }
  1161. return CURLE_PEER_FAILED_VERIFICATION;
  1162. }
  1163. #endif /* USE_GSKIT */