x509asn1.c 40 KB

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