2
0

x509asn1.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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_cfilter *cf,
  1159. struct Curl_easy *data,
  1160. const char *beg, const char *end)
  1161. {
  1162. struct ssl_connect_data *connssl = cf->ctx;
  1163. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  1164. struct Curl_X509certificate cert;
  1165. struct Curl_asn1Element dn;
  1166. struct Curl_asn1Element elem;
  1167. struct Curl_asn1Element ext;
  1168. struct Curl_asn1Element name;
  1169. const char *p;
  1170. const char *q;
  1171. char *dnsname;
  1172. int matched = -1;
  1173. size_t addrlen = (size_t) -1;
  1174. ssize_t len;
  1175. size_t hostlen;
  1176. #ifdef ENABLE_IPV6
  1177. struct in6_addr addr;
  1178. #else
  1179. struct in_addr addr;
  1180. #endif
  1181. /* Verify that connection server matches info in X509 certificate at
  1182. `beg'..`end'. */
  1183. if(!conn_config->verifyhost)
  1184. return CURLE_OK;
  1185. if(Curl_parseX509(&cert, beg, end))
  1186. return CURLE_PEER_FAILED_VERIFICATION;
  1187. hostlen = strlen(connssl->hostname);
  1188. /* Get the server IP address. */
  1189. #ifdef ENABLE_IPV6
  1190. if(conn->bits.ipv6_ip && Curl_inet_pton(AF_INET6, connssl->hostname, &addr))
  1191. addrlen = sizeof(struct in6_addr);
  1192. else
  1193. #endif
  1194. if(Curl_inet_pton(AF_INET, connssl->hostname, &addr))
  1195. addrlen = sizeof(struct in_addr);
  1196. /* Process extensions. */
  1197. for(p = cert.extensions.beg; p < cert.extensions.end && matched != 1;) {
  1198. p = getASN1Element(&ext, p, cert.extensions.end);
  1199. if(!p)
  1200. return CURLE_PEER_FAILED_VERIFICATION;
  1201. /* Check if extension is a subjectAlternativeName. */
  1202. ext.beg = checkOID(ext.beg, ext.end, sanOID);
  1203. if(ext.beg) {
  1204. ext.beg = getASN1Element(&elem, ext.beg, ext.end);
  1205. if(!ext.beg)
  1206. return CURLE_PEER_FAILED_VERIFICATION;
  1207. /* Skip critical if present. */
  1208. if(elem.tag == CURL_ASN1_BOOLEAN) {
  1209. ext.beg = getASN1Element(&elem, ext.beg, ext.end);
  1210. if(!ext.beg)
  1211. return CURLE_PEER_FAILED_VERIFICATION;
  1212. }
  1213. /* Parse the octet string contents: is a single sequence. */
  1214. if(!getASN1Element(&elem, elem.beg, elem.end))
  1215. return CURLE_PEER_FAILED_VERIFICATION;
  1216. /* Check all GeneralNames. */
  1217. for(q = elem.beg; matched != 1 && q < elem.end;) {
  1218. q = getASN1Element(&name, q, elem.end);
  1219. if(!q)
  1220. break;
  1221. switch(name.tag) {
  1222. case 2: /* DNS name. */
  1223. len = utf8asn1str(&dnsname, CURL_ASN1_IA5_STRING,
  1224. name.beg, name.end);
  1225. if(len > 0 && (size_t)len == strlen(dnsname))
  1226. matched = Curl_cert_hostcheck(dnsname, (size_t)len,
  1227. connssl->hostname, hostlen);
  1228. else
  1229. matched = 0;
  1230. free(dnsname);
  1231. break;
  1232. case 7: /* IP address. */
  1233. matched = (size_t) (name.end - name.beg) == addrlen &&
  1234. !memcmp(&addr, name.beg, addrlen);
  1235. break;
  1236. }
  1237. }
  1238. }
  1239. }
  1240. switch(matched) {
  1241. case 1:
  1242. /* an alternative name matched the server hostname */
  1243. infof(data, " subjectAltName: %s matched", connssl->dispname);
  1244. return CURLE_OK;
  1245. case 0:
  1246. /* an alternative name field existed, but didn't match and then
  1247. we MUST fail */
  1248. infof(data, " subjectAltName does not match %s", connssl->dispname);
  1249. return CURLE_PEER_FAILED_VERIFICATION;
  1250. }
  1251. /* Process subject. */
  1252. name.header = NULL;
  1253. name.beg = name.end = "";
  1254. q = cert.subject.beg;
  1255. /* we have to look to the last occurrence of a commonName in the
  1256. distinguished one to get the most significant one. */
  1257. while(q < cert.subject.end) {
  1258. q = getASN1Element(&dn, q, cert.subject.end);
  1259. if(!q)
  1260. break;
  1261. for(p = dn.beg; p < dn.end;) {
  1262. p = getASN1Element(&elem, p, dn.end);
  1263. if(!p)
  1264. return CURLE_PEER_FAILED_VERIFICATION;
  1265. /* We have a DN's AttributeTypeAndValue: check it in case it's a CN. */
  1266. elem.beg = checkOID(elem.beg, elem.end, cnOID);
  1267. if(elem.beg)
  1268. name = elem; /* Latch CN. */
  1269. }
  1270. }
  1271. /* Check the CN if found. */
  1272. if(!getASN1Element(&elem, name.beg, name.end))
  1273. failf(data, "SSL: unable to obtain common name from peer certificate");
  1274. else {
  1275. len = utf8asn1str(&dnsname, elem.tag, elem.beg, elem.end);
  1276. if(len < 0) {
  1277. free(dnsname);
  1278. return CURLE_OUT_OF_MEMORY;
  1279. }
  1280. if(strlen(dnsname) != (size_t) len) /* Nul byte in string ? */
  1281. failf(data, "SSL: illegal cert name field");
  1282. else if(Curl_cert_hostcheck((const char *) dnsname,
  1283. len, connssl->hostname, hostlen)) {
  1284. infof(data, " common name: %s (matched)", dnsname);
  1285. free(dnsname);
  1286. return CURLE_OK;
  1287. }
  1288. else
  1289. failf(data, "SSL: certificate subject name '%s' does not match "
  1290. "target host name '%s'", dnsname, connssl->dispname);
  1291. free(dnsname);
  1292. }
  1293. return CURLE_PEER_FAILED_VERIFICATION;
  1294. }
  1295. #endif /* WANT_VERIFYHOST */