x509asn1.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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) || 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'.
  555. *
  556. * Returns the total string length, even if larger than `buflen' or -1 on
  557. * error.
  558. */
  559. static ssize_t encodeDN(char *buf, size_t buflen, struct Curl_asn1Element *dn)
  560. {
  561. struct Curl_asn1Element rdn;
  562. struct Curl_asn1Element atv;
  563. struct Curl_asn1Element oid;
  564. struct Curl_asn1Element value;
  565. size_t l = 0;
  566. const char *p1;
  567. const char *p2;
  568. const char *p3;
  569. const char *str;
  570. for(p1 = dn->beg; p1 < dn->end;) {
  571. p1 = getASN1Element(&rdn, p1, dn->end);
  572. if(!p1)
  573. return -1;
  574. for(p2 = rdn.beg; p2 < rdn.end;) {
  575. p2 = getASN1Element(&atv, p2, rdn.end);
  576. if(!p2)
  577. return -1;
  578. p3 = getASN1Element(&oid, atv.beg, atv.end);
  579. if(!p3)
  580. return -1;
  581. if(!getASN1Element(&value, p3, atv.end))
  582. return -1;
  583. str = ASN1tostr(&oid, 0);
  584. if(!str)
  585. return -1;
  586. /* Encode delimiter.
  587. If attribute has a short uppercase name, delimiter is ", ". */
  588. if(l) {
  589. for(p3 = str; isupper(*p3); p3++)
  590. ;
  591. for(p3 = (*p3 || p3 - str > 2)? "/": ", "; *p3; p3++) {
  592. if(l < buflen)
  593. buf[l] = *p3;
  594. l++;
  595. }
  596. }
  597. /* Encode attribute name. */
  598. for(p3 = str; *p3; p3++) {
  599. if(l < buflen)
  600. buf[l] = *p3;
  601. l++;
  602. }
  603. free((char *) str);
  604. /* Generate equal sign. */
  605. if(l < buflen)
  606. buf[l] = '=';
  607. l++;
  608. /* Generate value. */
  609. str = ASN1tostr(&value, 0);
  610. if(!str)
  611. return -1;
  612. for(p3 = str; *p3; p3++) {
  613. if(l < buflen)
  614. buf[l] = *p3;
  615. l++;
  616. }
  617. free((char *) str);
  618. }
  619. }
  620. return l;
  621. }
  622. /*
  623. * Convert an ASN.1 distinguished name into a printable string.
  624. * Return the dynamically allocated string, or NULL if an error occurs.
  625. */
  626. static const char *DNtostr(struct Curl_asn1Element *dn)
  627. {
  628. char *buf = NULL;
  629. ssize_t buflen = encodeDN(NULL, 0, dn);
  630. if(buflen >= 0) {
  631. buf = malloc(buflen + 1);
  632. if(buf) {
  633. if(encodeDN(buf, buflen + 1, dn) == -1) {
  634. free(buf);
  635. return NULL;
  636. }
  637. buf[buflen] = '\0';
  638. }
  639. }
  640. return buf;
  641. }
  642. /*
  643. * ASN.1 parse an X509 certificate into structure subfields.
  644. * Syntax is assumed to have already been checked by the SSL backend.
  645. * See RFC 5280.
  646. */
  647. int Curl_parseX509(struct Curl_X509certificate *cert,
  648. const char *beg, const char *end)
  649. {
  650. struct Curl_asn1Element elem;
  651. struct Curl_asn1Element tbsCertificate;
  652. const char *ccp;
  653. static const char defaultVersion = 0; /* v1. */
  654. cert->certificate.header = NULL;
  655. cert->certificate.beg = beg;
  656. cert->certificate.end = end;
  657. /* Get the sequence content. */
  658. if(!getASN1Element(&elem, beg, end))
  659. return -1; /* Invalid bounds/size. */
  660. beg = elem.beg;
  661. end = elem.end;
  662. /* Get tbsCertificate. */
  663. beg = getASN1Element(&tbsCertificate, beg, end);
  664. if(!beg)
  665. return -1;
  666. /* Skip the signatureAlgorithm. */
  667. beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
  668. if(!beg)
  669. return -1;
  670. /* Get the signatureValue. */
  671. if(!getASN1Element(&cert->signature, beg, end))
  672. return -1;
  673. /* Parse TBSCertificate. */
  674. beg = tbsCertificate.beg;
  675. end = tbsCertificate.end;
  676. /* Get optional version, get serialNumber. */
  677. cert->version.header = NULL;
  678. cert->version.beg = &defaultVersion;
  679. cert->version.end = &defaultVersion + sizeof(defaultVersion);
  680. beg = getASN1Element(&elem, beg, end);
  681. if(!beg)
  682. return -1;
  683. if(elem.tag == 0) {
  684. if(!getASN1Element(&cert->version, elem.beg, elem.end))
  685. return -1;
  686. beg = getASN1Element(&elem, beg, end);
  687. if(!beg)
  688. return -1;
  689. }
  690. cert->serialNumber = elem;
  691. /* Get signature algorithm. */
  692. beg = getASN1Element(&cert->signatureAlgorithm, beg, end);
  693. /* Get issuer. */
  694. beg = getASN1Element(&cert->issuer, beg, end);
  695. if(!beg)
  696. return -1;
  697. /* Get notBefore and notAfter. */
  698. beg = getASN1Element(&elem, beg, end);
  699. if(!beg)
  700. return -1;
  701. ccp = getASN1Element(&cert->notBefore, elem.beg, elem.end);
  702. if(!ccp)
  703. return -1;
  704. if(!getASN1Element(&cert->notAfter, ccp, elem.end))
  705. return -1;
  706. /* Get subject. */
  707. beg = getASN1Element(&cert->subject, beg, end);
  708. if(!beg)
  709. return -1;
  710. /* Get subjectPublicKeyAlgorithm and subjectPublicKey. */
  711. beg = getASN1Element(&cert->subjectPublicKeyInfo, beg, end);
  712. if(!beg)
  713. return -1;
  714. ccp = getASN1Element(&cert->subjectPublicKeyAlgorithm,
  715. cert->subjectPublicKeyInfo.beg,
  716. cert->subjectPublicKeyInfo.end);
  717. if(!ccp)
  718. return -1;
  719. if(!getASN1Element(&cert->subjectPublicKey, ccp,
  720. cert->subjectPublicKeyInfo.end))
  721. return -1;
  722. /* Get optional issuerUiqueID, subjectUniqueID and extensions. */
  723. cert->issuerUniqueID.tag = cert->subjectUniqueID.tag = 0;
  724. cert->extensions.tag = elem.tag = 0;
  725. cert->issuerUniqueID.header = cert->subjectUniqueID.header = NULL;
  726. cert->issuerUniqueID.beg = cert->issuerUniqueID.end = "";
  727. cert->subjectUniqueID.beg = cert->subjectUniqueID.end = "";
  728. cert->extensions.header = NULL;
  729. cert->extensions.beg = cert->extensions.end = "";
  730. if(beg < end) {
  731. beg = getASN1Element(&elem, beg, end);
  732. if(!beg)
  733. return -1;
  734. }
  735. if(elem.tag == 1) {
  736. cert->issuerUniqueID = elem;
  737. if(beg < end) {
  738. beg = getASN1Element(&elem, beg, end);
  739. if(!beg)
  740. return -1;
  741. }
  742. }
  743. if(elem.tag == 2) {
  744. cert->subjectUniqueID = elem;
  745. if(beg < end) {
  746. beg = getASN1Element(&elem, beg, end);
  747. if(!beg)
  748. return -1;
  749. }
  750. }
  751. if(elem.tag == 3)
  752. if(!getASN1Element(&cert->extensions, elem.beg, elem.end))
  753. return -1;
  754. return 0;
  755. }
  756. /*
  757. * Copy at most 64-characters, terminate with a newline and returns the
  758. * effective number of stored characters.
  759. */
  760. static size_t copySubstring(char *to, const char *from)
  761. {
  762. size_t i;
  763. for(i = 0; i < 64; i++) {
  764. to[i] = *from;
  765. if(!*from++)
  766. break;
  767. }
  768. to[i++] = '\n';
  769. return i;
  770. }
  771. static const char *dumpAlgo(struct Curl_asn1Element *param,
  772. const char *beg, const char *end)
  773. {
  774. struct Curl_asn1Element oid;
  775. /* Get algorithm parameters and return algorithm name. */
  776. beg = getASN1Element(&oid, beg, end);
  777. if(!beg)
  778. return NULL;
  779. param->header = NULL;
  780. param->tag = 0;
  781. param->beg = param->end = end;
  782. if(beg < end)
  783. if(!getASN1Element(param, beg, end))
  784. return NULL;
  785. return OID2str(oid.beg, oid.end, TRUE);
  786. }
  787. /* return 0 on success, 1 on error */
  788. static int do_pubkey_field(struct Curl_easy *data, int certnum,
  789. const char *label, struct Curl_asn1Element *elem)
  790. {
  791. const char *output;
  792. CURLcode result = CURLE_OK;
  793. /* Generate a certificate information record for the public key. */
  794. output = ASN1tostr(elem, 0);
  795. if(output) {
  796. if(data->set.ssl.certinfo)
  797. result = Curl_ssl_push_certinfo(data, certnum, label, output);
  798. if(!certnum && !result)
  799. infof(data, " %s: %s", label, output);
  800. free((char *) output);
  801. }
  802. return result ? 1 : 0;
  803. }
  804. /* return 0 on success, 1 on error */
  805. static int do_pubkey(struct Curl_easy *data, int certnum,
  806. const char *algo, struct Curl_asn1Element *param,
  807. struct Curl_asn1Element *pubkey)
  808. {
  809. struct Curl_asn1Element elem;
  810. struct Curl_asn1Element pk;
  811. const char *p;
  812. /* Generate all information records for the public key. */
  813. /* Get the public key (single element). */
  814. if(!getASN1Element(&pk, pubkey->beg + 1, pubkey->end))
  815. return 1;
  816. if(strcasecompare(algo, "rsaEncryption")) {
  817. const char *q;
  818. unsigned long len;
  819. p = getASN1Element(&elem, pk.beg, pk.end);
  820. if(!p)
  821. return 1;
  822. /* Compute key length. */
  823. for(q = elem.beg; !*q && q < elem.end; q++)
  824. ;
  825. len = (unsigned long)((elem.end - q) * 8);
  826. if(len) {
  827. unsigned int i;
  828. for(i = *(unsigned char *) q; !(i & 0x80); i <<= 1)
  829. len--;
  830. }
  831. if(len > 32)
  832. elem.beg = q; /* Strip leading zero bytes. */
  833. if(!certnum)
  834. infof(data, " RSA Public Key (%lu bits)", len);
  835. if(data->set.ssl.certinfo) {
  836. q = curl_maprintf("%lu", len);
  837. if(q) {
  838. CURLcode result =
  839. Curl_ssl_push_certinfo(data, certnum, "RSA Public Key", q);
  840. free((char *) q);
  841. if(result)
  842. return 1;
  843. }
  844. }
  845. /* Generate coefficients. */
  846. if(do_pubkey_field(data, certnum, "rsa(n)", &elem))
  847. return 1;
  848. if(!getASN1Element(&elem, p, pk.end))
  849. return 1;
  850. if(do_pubkey_field(data, certnum, "rsa(e)", &elem))
  851. return 1;
  852. }
  853. else if(strcasecompare(algo, "dsa")) {
  854. p = getASN1Element(&elem, param->beg, param->end);
  855. if(p) {
  856. if(do_pubkey_field(data, certnum, "dsa(p)", &elem))
  857. return 1;
  858. p = getASN1Element(&elem, p, param->end);
  859. if(p) {
  860. if(do_pubkey_field(data, certnum, "dsa(q)", &elem))
  861. return 1;
  862. if(getASN1Element(&elem, p, param->end)) {
  863. if(do_pubkey_field(data, certnum, "dsa(g)", &elem))
  864. return 1;
  865. if(do_pubkey_field(data, certnum, "dsa(pub_key)", &pk))
  866. return 1;
  867. }
  868. }
  869. }
  870. }
  871. else if(strcasecompare(algo, "dhpublicnumber")) {
  872. p = getASN1Element(&elem, param->beg, param->end);
  873. if(p) {
  874. if(do_pubkey_field(data, certnum, "dh(p)", &elem))
  875. return 1;
  876. if(getASN1Element(&elem, param->beg, param->end)) {
  877. if(do_pubkey_field(data, certnum, "dh(g)", &elem))
  878. return 1;
  879. if(do_pubkey_field(data, certnum, "dh(pub_key)", &pk))
  880. return 1;
  881. }
  882. }
  883. }
  884. return 0;
  885. }
  886. CURLcode Curl_extract_certinfo(struct Curl_easy *data,
  887. int certnum,
  888. const char *beg,
  889. const char *end)
  890. {
  891. struct Curl_X509certificate cert;
  892. struct Curl_asn1Element param;
  893. const char *ccp;
  894. char *cp1;
  895. size_t cl1;
  896. char *cp2;
  897. CURLcode result = CURLE_OK;
  898. unsigned long version;
  899. size_t i;
  900. size_t j;
  901. if(!data->set.ssl.certinfo)
  902. if(certnum)
  903. return CURLE_OK;
  904. /* Prepare the certificate information for curl_easy_getinfo(). */
  905. /* Extract the certificate ASN.1 elements. */
  906. if(Curl_parseX509(&cert, beg, end))
  907. return CURLE_PEER_FAILED_VERIFICATION;
  908. /* Subject. */
  909. ccp = DNtostr(&cert.subject);
  910. if(!ccp)
  911. return CURLE_OUT_OF_MEMORY;
  912. if(data->set.ssl.certinfo) {
  913. result = Curl_ssl_push_certinfo(data, certnum, "Subject", ccp);
  914. if(result)
  915. return result;
  916. }
  917. if(!certnum)
  918. infof(data, "%2d Subject: %s", certnum, ccp);
  919. free((char *) ccp);
  920. /* Issuer. */
  921. ccp = DNtostr(&cert.issuer);
  922. if(!ccp)
  923. return CURLE_OUT_OF_MEMORY;
  924. if(data->set.ssl.certinfo) {
  925. result = Curl_ssl_push_certinfo(data, certnum, "Issuer", ccp);
  926. }
  927. if(!certnum)
  928. infof(data, " Issuer: %s", ccp);
  929. free((char *) ccp);
  930. if(result)
  931. return result;
  932. /* Version (always fits in less than 32 bits). */
  933. version = 0;
  934. for(ccp = cert.version.beg; ccp < cert.version.end; ccp++)
  935. version = (version << 8) | *(const unsigned char *) ccp;
  936. if(data->set.ssl.certinfo) {
  937. ccp = curl_maprintf("%lx", version);
  938. if(!ccp)
  939. return CURLE_OUT_OF_MEMORY;
  940. result = Curl_ssl_push_certinfo(data, certnum, "Version", ccp);
  941. free((char *) ccp);
  942. if(result)
  943. return result;
  944. }
  945. if(!certnum)
  946. infof(data, " Version: %lu (0x%lx)", version + 1, version);
  947. /* Serial number. */
  948. ccp = ASN1tostr(&cert.serialNumber, 0);
  949. if(!ccp)
  950. return CURLE_OUT_OF_MEMORY;
  951. if(data->set.ssl.certinfo)
  952. result = Curl_ssl_push_certinfo(data, certnum, "Serial Number", ccp);
  953. if(!certnum)
  954. infof(data, " Serial Number: %s", ccp);
  955. free((char *) ccp);
  956. if(result)
  957. return result;
  958. /* Signature algorithm .*/
  959. ccp = dumpAlgo(&param, cert.signatureAlgorithm.beg,
  960. cert.signatureAlgorithm.end);
  961. if(!ccp)
  962. return CURLE_OUT_OF_MEMORY;
  963. if(data->set.ssl.certinfo)
  964. result = Curl_ssl_push_certinfo(data, certnum, "Signature Algorithm", ccp);
  965. if(!certnum)
  966. infof(data, " Signature Algorithm: %s", ccp);
  967. free((char *) ccp);
  968. if(result)
  969. return result;
  970. /* Start Date. */
  971. ccp = ASN1tostr(&cert.notBefore, 0);
  972. if(!ccp)
  973. return CURLE_OUT_OF_MEMORY;
  974. if(data->set.ssl.certinfo)
  975. result = Curl_ssl_push_certinfo(data, certnum, "Start Date", ccp);
  976. if(!certnum)
  977. infof(data, " Start Date: %s", ccp);
  978. free((char *) ccp);
  979. if(result)
  980. return result;
  981. /* Expire Date. */
  982. ccp = ASN1tostr(&cert.notAfter, 0);
  983. if(!ccp)
  984. return CURLE_OUT_OF_MEMORY;
  985. if(data->set.ssl.certinfo)
  986. result = Curl_ssl_push_certinfo(data, certnum, "Expire Date", ccp);
  987. if(!certnum)
  988. infof(data, " Expire Date: %s", ccp);
  989. free((char *) ccp);
  990. if(result)
  991. return result;
  992. /* Public Key Algorithm. */
  993. ccp = dumpAlgo(&param, cert.subjectPublicKeyAlgorithm.beg,
  994. cert.subjectPublicKeyAlgorithm.end);
  995. if(!ccp)
  996. return CURLE_OUT_OF_MEMORY;
  997. if(data->set.ssl.certinfo)
  998. result = Curl_ssl_push_certinfo(data, certnum, "Public Key Algorithm",
  999. ccp);
  1000. if(!result) {
  1001. int ret;
  1002. if(!certnum)
  1003. infof(data, " Public Key Algorithm: %s", ccp);
  1004. ret = do_pubkey(data, certnum, ccp, &param, &cert.subjectPublicKey);
  1005. if(ret)
  1006. result = CURLE_OUT_OF_MEMORY; /* the most likely error */
  1007. }
  1008. free((char *) ccp);
  1009. if(result)
  1010. return result;
  1011. /* Signature. */
  1012. ccp = ASN1tostr(&cert.signature, 0);
  1013. if(!ccp)
  1014. return CURLE_OUT_OF_MEMORY;
  1015. if(data->set.ssl.certinfo)
  1016. result = Curl_ssl_push_certinfo(data, certnum, "Signature", ccp);
  1017. if(!certnum)
  1018. infof(data, " Signature: %s", ccp);
  1019. free((char *) ccp);
  1020. if(result)
  1021. return result;
  1022. /* Generate PEM certificate. */
  1023. result = Curl_base64_encode(data, cert.certificate.beg,
  1024. cert.certificate.end - cert.certificate.beg,
  1025. &cp1, &cl1);
  1026. if(result)
  1027. return result;
  1028. /* Compute the number of characters in final certificate string. Format is:
  1029. -----BEGIN CERTIFICATE-----\n
  1030. <max 64 base64 characters>\n
  1031. .
  1032. .
  1033. .
  1034. -----END CERTIFICATE-----\n
  1035. */
  1036. i = 28 + cl1 + (cl1 + 64 - 1) / 64 + 26;
  1037. cp2 = malloc(i + 1);
  1038. if(!cp2) {
  1039. free(cp1);
  1040. return CURLE_OUT_OF_MEMORY;
  1041. }
  1042. /* Build the certificate string. */
  1043. i = copySubstring(cp2, "-----BEGIN CERTIFICATE-----");
  1044. for(j = 0; j < cl1; j += 64)
  1045. i += copySubstring(cp2 + i, cp1 + j);
  1046. i += copySubstring(cp2 + i, "-----END CERTIFICATE-----");
  1047. cp2[i] = '\0';
  1048. free(cp1);
  1049. if(data->set.ssl.certinfo)
  1050. result = Curl_ssl_push_certinfo(data, certnum, "Cert", cp2);
  1051. if(!certnum)
  1052. infof(data, "%s", cp2);
  1053. free(cp2);
  1054. return result;
  1055. }
  1056. #endif /* USE_GSKIT or USE_NSS or USE_GNUTLS or USE_WOLFSSL or USE_SCHANNEL
  1057. * or USE_SECTRANSP */
  1058. #if defined(USE_GSKIT)
  1059. static const char *checkOID(const char *beg, const char *end,
  1060. const char *oid)
  1061. {
  1062. struct Curl_asn1Element e;
  1063. const char *ccp;
  1064. const char *p;
  1065. bool matched;
  1066. /* Check if first ASN.1 element at `beg' is the given OID.
  1067. Return a pointer in the source after the OID if found, else NULL. */
  1068. ccp = getASN1Element(&e, beg, end);
  1069. if(!ccp || e.tag != CURL_ASN1_OBJECT_IDENTIFIER)
  1070. return NULL;
  1071. p = OID2str(e.beg, e.end, FALSE);
  1072. if(!p)
  1073. return NULL;
  1074. matched = !strcmp(p, oid);
  1075. free((char *) p);
  1076. return matched? ccp: NULL;
  1077. }
  1078. CURLcode Curl_verifyhost(struct Curl_easy *data, struct connectdata *conn,
  1079. const char *beg, const char *end)
  1080. {
  1081. struct Curl_X509certificate cert;
  1082. struct Curl_asn1Element dn;
  1083. struct Curl_asn1Element elem;
  1084. struct Curl_asn1Element ext;
  1085. struct Curl_asn1Element name;
  1086. const char *p;
  1087. const char *q;
  1088. char *dnsname;
  1089. int matched = -1;
  1090. size_t addrlen = (size_t) -1;
  1091. ssize_t len;
  1092. const char * const hostname = SSL_HOST_NAME();
  1093. const char * const dispname = SSL_HOST_DISPNAME();
  1094. #ifdef ENABLE_IPV6
  1095. struct in6_addr addr;
  1096. #else
  1097. struct in_addr addr;
  1098. #endif
  1099. /* Verify that connection server matches info in X509 certificate at
  1100. `beg'..`end'. */
  1101. if(!SSL_CONN_CONFIG(verifyhost))
  1102. return CURLE_OK;
  1103. if(Curl_parseX509(&cert, beg, end))
  1104. return CURLE_PEER_FAILED_VERIFICATION;
  1105. /* Get the server IP address. */
  1106. #ifdef ENABLE_IPV6
  1107. if(conn->bits.ipv6_ip && Curl_inet_pton(AF_INET6, hostname, &addr))
  1108. addrlen = sizeof(struct in6_addr);
  1109. else
  1110. #endif
  1111. if(Curl_inet_pton(AF_INET, hostname, &addr))
  1112. addrlen = sizeof(struct in_addr);
  1113. /* Process extensions. */
  1114. for(p = cert.extensions.beg; p < cert.extensions.end && matched != 1;) {
  1115. p = getASN1Element(&ext, p, cert.extensions.end);
  1116. if(!p)
  1117. return CURLE_PEER_FAILED_VERIFICATION;
  1118. /* Check if extension is a subjectAlternativeName. */
  1119. ext.beg = checkOID(ext.beg, ext.end, sanOID);
  1120. if(ext.beg) {
  1121. ext.beg = getASN1Element(&elem, ext.beg, ext.end);
  1122. if(!ext.beg)
  1123. return CURLE_PEER_FAILED_VERIFICATION;
  1124. /* Skip critical if present. */
  1125. if(elem.tag == CURL_ASN1_BOOLEAN) {
  1126. ext.beg = getASN1Element(&elem, ext.beg, ext.end);
  1127. if(!ext.beg)
  1128. return CURLE_PEER_FAILED_VERIFICATION;
  1129. }
  1130. /* Parse the octet string contents: is a single sequence. */
  1131. if(!getASN1Element(&elem, elem.beg, elem.end))
  1132. return CURLE_PEER_FAILED_VERIFICATION;
  1133. /* Check all GeneralNames. */
  1134. for(q = elem.beg; matched != 1 && q < elem.end;) {
  1135. q = getASN1Element(&name, q, elem.end);
  1136. if(!q)
  1137. break;
  1138. switch(name.tag) {
  1139. case 2: /* DNS name. */
  1140. len = utf8asn1str(&dnsname, CURL_ASN1_IA5_STRING,
  1141. name.beg, name.end);
  1142. if(len > 0 && (size_t)len == strlen(dnsname))
  1143. matched = Curl_cert_hostcheck(dnsname, hostname);
  1144. else
  1145. matched = 0;
  1146. free(dnsname);
  1147. break;
  1148. case 7: /* IP address. */
  1149. matched = (size_t) (name.end - name.beg) == addrlen &&
  1150. !memcmp(&addr, name.beg, addrlen);
  1151. break;
  1152. }
  1153. }
  1154. }
  1155. }
  1156. switch(matched) {
  1157. case 1:
  1158. /* an alternative name matched the server hostname */
  1159. infof(data, " subjectAltName: %s matched", dispname);
  1160. return CURLE_OK;
  1161. case 0:
  1162. /* an alternative name field existed, but didn't match and then
  1163. we MUST fail */
  1164. infof(data, " subjectAltName does not match %s", dispname);
  1165. return CURLE_PEER_FAILED_VERIFICATION;
  1166. }
  1167. /* Process subject. */
  1168. name.header = NULL;
  1169. name.beg = name.end = "";
  1170. q = cert.subject.beg;
  1171. /* we have to look to the last occurrence of a commonName in the
  1172. distinguished one to get the most significant one. */
  1173. while(q < cert.subject.end) {
  1174. q = getASN1Element(&dn, q, cert.subject.end);
  1175. if(!q)
  1176. break;
  1177. for(p = dn.beg; p < dn.end;) {
  1178. p = getASN1Element(&elem, p, dn.end);
  1179. if(!p)
  1180. return CURLE_PEER_FAILED_VERIFICATION;
  1181. /* We have a DN's AttributeTypeAndValue: check it in case it's a CN. */
  1182. elem.beg = checkOID(elem.beg, elem.end, cnOID);
  1183. if(elem.beg)
  1184. name = elem; /* Latch CN. */
  1185. }
  1186. }
  1187. /* Check the CN if found. */
  1188. if(!getASN1Element(&elem, name.beg, name.end))
  1189. failf(data, "SSL: unable to obtain common name from peer certificate");
  1190. else {
  1191. len = utf8asn1str(&dnsname, elem.tag, elem.beg, elem.end);
  1192. if(len < 0) {
  1193. free(dnsname);
  1194. return CURLE_OUT_OF_MEMORY;
  1195. }
  1196. if(strlen(dnsname) != (size_t) len) /* Nul byte in string ? */
  1197. failf(data, "SSL: illegal cert name field");
  1198. else if(Curl_cert_hostcheck((const char *) dnsname, hostname)) {
  1199. infof(data, " common name: %s (matched)", dnsname);
  1200. free(dnsname);
  1201. return CURLE_OK;
  1202. }
  1203. else
  1204. failf(data, "SSL: certificate subject name '%s' does not match "
  1205. "target host name '%s'", dnsname, dispname);
  1206. free(dnsname);
  1207. }
  1208. return CURLE_PEER_FAILED_VERIFICATION;
  1209. }
  1210. #endif /* USE_GSKIT */