x509asn1.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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. /* return 0 on success, 1 on error */
  835. static int do_pubkey_field(struct Curl_easy *data, int certnum,
  836. const char *label, struct Curl_asn1Element *elem)
  837. {
  838. const char *output;
  839. CURLcode result = CURLE_OK;
  840. /* Generate a certificate information record for the public key. */
  841. output = ASN1tostr(elem, 0);
  842. if(output) {
  843. if(data->set.ssl.certinfo)
  844. result = Curl_ssl_push_certinfo(data, certnum, label, output);
  845. if(!certnum && !result)
  846. infof(data, " %s: %s", label, output);
  847. free((char *) output);
  848. }
  849. return result ? 1 : 0;
  850. }
  851. /* return 0 on success, 1 on error */
  852. static int do_pubkey(struct Curl_easy *data, int certnum,
  853. const char *algo, struct Curl_asn1Element *param,
  854. struct Curl_asn1Element *pubkey)
  855. {
  856. struct Curl_asn1Element elem;
  857. struct Curl_asn1Element pk;
  858. const char *p;
  859. /* Generate all information records for the public key. */
  860. if(strcasecompare(algo, "ecPublicKey")) {
  861. /*
  862. * ECC public key is all the data, a value of type BIT STRING mapped to
  863. * OCTET STRING and should not be parsed as an ASN.1 value.
  864. */
  865. const size_t len = ((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. size_t 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 = ((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 int 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("%x", 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: %u (0x%x)", 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(cf->conn->bits.ipv6_ip &&
  1191. Curl_inet_pton(AF_INET6, connssl->hostname, &addr))
  1192. addrlen = sizeof(struct in6_addr);
  1193. else
  1194. #endif
  1195. if(Curl_inet_pton(AF_INET, connssl->hostname, &addr))
  1196. addrlen = sizeof(struct in_addr);
  1197. /* Process extensions. */
  1198. for(p = cert.extensions.beg; p < cert.extensions.end && matched != 1;) {
  1199. p = getASN1Element(&ext, p, cert.extensions.end);
  1200. if(!p)
  1201. return CURLE_PEER_FAILED_VERIFICATION;
  1202. /* Check if extension is a subjectAlternativeName. */
  1203. ext.beg = checkOID(ext.beg, ext.end, sanOID);
  1204. if(ext.beg) {
  1205. ext.beg = getASN1Element(&elem, ext.beg, ext.end);
  1206. if(!ext.beg)
  1207. return CURLE_PEER_FAILED_VERIFICATION;
  1208. /* Skip critical if present. */
  1209. if(elem.tag == CURL_ASN1_BOOLEAN) {
  1210. ext.beg = getASN1Element(&elem, ext.beg, ext.end);
  1211. if(!ext.beg)
  1212. return CURLE_PEER_FAILED_VERIFICATION;
  1213. }
  1214. /* Parse the octet string contents: is a single sequence. */
  1215. if(!getASN1Element(&elem, elem.beg, elem.end))
  1216. return CURLE_PEER_FAILED_VERIFICATION;
  1217. /* Check all GeneralNames. */
  1218. for(q = elem.beg; matched != 1 && q < elem.end;) {
  1219. q = getASN1Element(&name, q, elem.end);
  1220. if(!q)
  1221. break;
  1222. switch(name.tag) {
  1223. case 2: /* DNS name. */
  1224. len = utf8asn1str(&dnsname, CURL_ASN1_IA5_STRING,
  1225. name.beg, name.end);
  1226. if(len > 0 && (size_t)len == strlen(dnsname))
  1227. matched = Curl_cert_hostcheck(dnsname, (size_t)len,
  1228. connssl->hostname, hostlen);
  1229. else
  1230. matched = 0;
  1231. free(dnsname);
  1232. break;
  1233. case 7: /* IP address. */
  1234. matched = (size_t)(name.end - name.beg) == addrlen &&
  1235. !memcmp(&addr, name.beg, addrlen);
  1236. break;
  1237. }
  1238. }
  1239. }
  1240. }
  1241. switch(matched) {
  1242. case 1:
  1243. /* an alternative name matched the server hostname */
  1244. infof(data, " subjectAltName: %s matched", connssl->dispname);
  1245. return CURLE_OK;
  1246. case 0:
  1247. /* an alternative name field existed, but didn't match and then
  1248. we MUST fail */
  1249. infof(data, " subjectAltName does not match %s", connssl->dispname);
  1250. return CURLE_PEER_FAILED_VERIFICATION;
  1251. }
  1252. /* Process subject. */
  1253. name.header = NULL;
  1254. name.beg = name.end = "";
  1255. q = cert.subject.beg;
  1256. /* we have to look to the last occurrence of a commonName in the
  1257. distinguished one to get the most significant one. */
  1258. while(q < cert.subject.end) {
  1259. q = getASN1Element(&dn, q, cert.subject.end);
  1260. if(!q)
  1261. break;
  1262. for(p = dn.beg; p < dn.end;) {
  1263. p = getASN1Element(&elem, p, dn.end);
  1264. if(!p)
  1265. return CURLE_PEER_FAILED_VERIFICATION;
  1266. /* We have a DN's AttributeTypeAndValue: check it in case it's a CN. */
  1267. elem.beg = checkOID(elem.beg, elem.end, cnOID);
  1268. if(elem.beg)
  1269. name = elem; /* Latch CN. */
  1270. }
  1271. }
  1272. /* Check the CN if found. */
  1273. if(!getASN1Element(&elem, name.beg, name.end))
  1274. failf(data, "SSL: unable to obtain common name from peer certificate");
  1275. else {
  1276. len = utf8asn1str(&dnsname, elem.tag, elem.beg, elem.end);
  1277. if(len < 0) {
  1278. free(dnsname);
  1279. return CURLE_OUT_OF_MEMORY;
  1280. }
  1281. if(strlen(dnsname) != (size_t) len) /* Nul byte in string ? */
  1282. failf(data, "SSL: illegal cert name field");
  1283. else if(Curl_cert_hostcheck((const char *) dnsname,
  1284. len, connssl->hostname, hostlen)) {
  1285. infof(data, " common name: %s (matched)", dnsname);
  1286. free(dnsname);
  1287. return CURLE_OK;
  1288. }
  1289. else
  1290. failf(data, "SSL: certificate subject name '%s' does not match "
  1291. "target host name '%s'", dnsname, connssl->dispname);
  1292. free(dnsname);
  1293. }
  1294. return CURLE_PEER_FAILED_VERIFICATION;
  1295. }
  1296. #endif /* WANT_VERIFYHOST */