x509asn1.c 39 KB

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