x509asn1.c 40 KB

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