x509asn1.c 35 KB

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