2
0

x509asn1.c 40 KB

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