asn1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * The ASB.1/BER parsing code is derived from ip_nat_snmp_basic.c which was in
  3. * turn derived from the gxsnmp package by Gregory McLean & Jochen Friedrich
  4. *
  5. * Copyright (c) 2000 RP Internet (www.rpi.net.au).
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/slab.h>
  24. #include "cifspdu.h"
  25. #include "cifsglob.h"
  26. #include "cifs_debug.h"
  27. #include "cifsproto.h"
  28. /*****************************************************************************
  29. *
  30. * Basic ASN.1 decoding routines (gxsnmp author Dirk Wisse)
  31. *
  32. *****************************************************************************/
  33. /* Class */
  34. #define ASN1_UNI 0 /* Universal */
  35. #define ASN1_APL 1 /* Application */
  36. #define ASN1_CTX 2 /* Context */
  37. #define ASN1_PRV 3 /* Private */
  38. /* Tag */
  39. #define ASN1_EOC 0 /* End Of Contents or N/A */
  40. #define ASN1_BOL 1 /* Boolean */
  41. #define ASN1_INT 2 /* Integer */
  42. #define ASN1_BTS 3 /* Bit String */
  43. #define ASN1_OTS 4 /* Octet String */
  44. #define ASN1_NUL 5 /* Null */
  45. #define ASN1_OJI 6 /* Object Identifier */
  46. #define ASN1_OJD 7 /* Object Description */
  47. #define ASN1_EXT 8 /* External */
  48. #define ASN1_SEQ 16 /* Sequence */
  49. #define ASN1_SET 17 /* Set */
  50. #define ASN1_NUMSTR 18 /* Numerical String */
  51. #define ASN1_PRNSTR 19 /* Printable String */
  52. #define ASN1_TEXSTR 20 /* Teletext String */
  53. #define ASN1_VIDSTR 21 /* Video String */
  54. #define ASN1_IA5STR 22 /* IA5 String */
  55. #define ASN1_UNITIM 23 /* Universal Time */
  56. #define ASN1_GENTIM 24 /* General Time */
  57. #define ASN1_GRASTR 25 /* Graphical String */
  58. #define ASN1_VISSTR 26 /* Visible String */
  59. #define ASN1_GENSTR 27 /* General String */
  60. /* Primitive / Constructed methods*/
  61. #define ASN1_PRI 0 /* Primitive */
  62. #define ASN1_CON 1 /* Constructed */
  63. /*
  64. * Error codes.
  65. */
  66. #define ASN1_ERR_NOERROR 0
  67. #define ASN1_ERR_DEC_EMPTY 2
  68. #define ASN1_ERR_DEC_EOC_MISMATCH 3
  69. #define ASN1_ERR_DEC_LENGTH_MISMATCH 4
  70. #define ASN1_ERR_DEC_BADVALUE 5
  71. #define SPNEGO_OID_LEN 7
  72. #define NTLMSSP_OID_LEN 10
  73. #define KRB5_OID_LEN 7
  74. #define MSKRB5_OID_LEN 7
  75. static unsigned long SPNEGO_OID[7] = { 1, 3, 6, 1, 5, 5, 2 };
  76. static unsigned long NTLMSSP_OID[10] = { 1, 3, 6, 1, 4, 1, 311, 2, 2, 10 };
  77. static unsigned long KRB5_OID[7] = { 1, 2, 840, 113554, 1, 2, 2 };
  78. static unsigned long MSKRB5_OID[7] = { 1, 2, 840, 48018, 1, 2, 2 };
  79. /*
  80. * ASN.1 context.
  81. */
  82. struct asn1_ctx {
  83. int error; /* Error condition */
  84. unsigned char *pointer; /* Octet just to be decoded */
  85. unsigned char *begin; /* First octet */
  86. unsigned char *end; /* Octet after last octet */
  87. };
  88. /*
  89. * Octet string (not null terminated)
  90. */
  91. struct asn1_octstr {
  92. unsigned char *data;
  93. unsigned int len;
  94. };
  95. static void
  96. asn1_open(struct asn1_ctx *ctx, unsigned char *buf, unsigned int len)
  97. {
  98. ctx->begin = buf;
  99. ctx->end = buf + len;
  100. ctx->pointer = buf;
  101. ctx->error = ASN1_ERR_NOERROR;
  102. }
  103. static unsigned char
  104. asn1_octet_decode(struct asn1_ctx *ctx, unsigned char *ch)
  105. {
  106. if (ctx->pointer >= ctx->end) {
  107. ctx->error = ASN1_ERR_DEC_EMPTY;
  108. return 0;
  109. }
  110. *ch = *(ctx->pointer)++;
  111. return 1;
  112. }
  113. static unsigned char
  114. asn1_tag_decode(struct asn1_ctx *ctx, unsigned int *tag)
  115. {
  116. unsigned char ch;
  117. *tag = 0;
  118. do {
  119. if (!asn1_octet_decode(ctx, &ch))
  120. return 0;
  121. *tag <<= 7;
  122. *tag |= ch & 0x7F;
  123. } while ((ch & 0x80) == 0x80);
  124. return 1;
  125. }
  126. static unsigned char
  127. asn1_id_decode(struct asn1_ctx *ctx,
  128. unsigned int *cls, unsigned int *con, unsigned int *tag)
  129. {
  130. unsigned char ch;
  131. if (!asn1_octet_decode(ctx, &ch))
  132. return 0;
  133. *cls = (ch & 0xC0) >> 6;
  134. *con = (ch & 0x20) >> 5;
  135. *tag = (ch & 0x1F);
  136. if (*tag == 0x1F) {
  137. if (!asn1_tag_decode(ctx, tag))
  138. return 0;
  139. }
  140. return 1;
  141. }
  142. static unsigned char
  143. asn1_length_decode(struct asn1_ctx *ctx, unsigned int *def, unsigned int *len)
  144. {
  145. unsigned char ch, cnt;
  146. if (!asn1_octet_decode(ctx, &ch))
  147. return 0;
  148. if (ch == 0x80)
  149. *def = 0;
  150. else {
  151. *def = 1;
  152. if (ch < 0x80)
  153. *len = ch;
  154. else {
  155. cnt = (unsigned char) (ch & 0x7F);
  156. *len = 0;
  157. while (cnt > 0) {
  158. if (!asn1_octet_decode(ctx, &ch))
  159. return 0;
  160. *len <<= 8;
  161. *len |= ch;
  162. cnt--;
  163. }
  164. }
  165. }
  166. /* don't trust len bigger than ctx buffer */
  167. if (*len > ctx->end - ctx->pointer)
  168. return 0;
  169. return 1;
  170. }
  171. static unsigned char
  172. asn1_header_decode(struct asn1_ctx *ctx,
  173. unsigned char **eoc,
  174. unsigned int *cls, unsigned int *con, unsigned int *tag)
  175. {
  176. unsigned int def = 0;
  177. unsigned int len = 0;
  178. if (!asn1_id_decode(ctx, cls, con, tag))
  179. return 0;
  180. if (!asn1_length_decode(ctx, &def, &len))
  181. return 0;
  182. /* primitive shall be definite, indefinite shall be constructed */
  183. if (*con == ASN1_PRI && !def)
  184. return 0;
  185. if (def)
  186. *eoc = ctx->pointer + len;
  187. else
  188. *eoc = NULL;
  189. return 1;
  190. }
  191. static unsigned char
  192. asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
  193. {
  194. unsigned char ch;
  195. if (eoc == NULL) {
  196. if (!asn1_octet_decode(ctx, &ch))
  197. return 0;
  198. if (ch != 0x00) {
  199. ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
  200. return 0;
  201. }
  202. if (!asn1_octet_decode(ctx, &ch))
  203. return 0;
  204. if (ch != 0x00) {
  205. ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
  206. return 0;
  207. }
  208. return 1;
  209. } else {
  210. if (ctx->pointer != eoc) {
  211. ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
  212. return 0;
  213. }
  214. return 1;
  215. }
  216. }
  217. /* static unsigned char asn1_null_decode(struct asn1_ctx *ctx,
  218. unsigned char *eoc)
  219. {
  220. ctx->pointer = eoc;
  221. return 1;
  222. }
  223. static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
  224. unsigned char *eoc, long *integer)
  225. {
  226. unsigned char ch;
  227. unsigned int len;
  228. if (!asn1_octet_decode(ctx, &ch))
  229. return 0;
  230. *integer = (signed char) ch;
  231. len = 1;
  232. while (ctx->pointer < eoc) {
  233. if (++len > sizeof(long)) {
  234. ctx->error = ASN1_ERR_DEC_BADVALUE;
  235. return 0;
  236. }
  237. if (!asn1_octet_decode(ctx, &ch))
  238. return 0;
  239. *integer <<= 8;
  240. *integer |= ch;
  241. }
  242. return 1;
  243. }
  244. static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
  245. unsigned char *eoc,
  246. unsigned int *integer)
  247. {
  248. unsigned char ch;
  249. unsigned int len;
  250. if (!asn1_octet_decode(ctx, &ch))
  251. return 0;
  252. *integer = ch;
  253. if (ch == 0)
  254. len = 0;
  255. else
  256. len = 1;
  257. while (ctx->pointer < eoc) {
  258. if (++len > sizeof(unsigned int)) {
  259. ctx->error = ASN1_ERR_DEC_BADVALUE;
  260. return 0;
  261. }
  262. if (!asn1_octet_decode(ctx, &ch))
  263. return 0;
  264. *integer <<= 8;
  265. *integer |= ch;
  266. }
  267. return 1;
  268. }
  269. static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
  270. unsigned char *eoc,
  271. unsigned long *integer)
  272. {
  273. unsigned char ch;
  274. unsigned int len;
  275. if (!asn1_octet_decode(ctx, &ch))
  276. return 0;
  277. *integer = ch;
  278. if (ch == 0)
  279. len = 0;
  280. else
  281. len = 1;
  282. while (ctx->pointer < eoc) {
  283. if (++len > sizeof(unsigned long)) {
  284. ctx->error = ASN1_ERR_DEC_BADVALUE;
  285. return 0;
  286. }
  287. if (!asn1_octet_decode(ctx, &ch))
  288. return 0;
  289. *integer <<= 8;
  290. *integer |= ch;
  291. }
  292. return 1;
  293. }
  294. static unsigned char
  295. asn1_octets_decode(struct asn1_ctx *ctx,
  296. unsigned char *eoc,
  297. unsigned char **octets, unsigned int *len)
  298. {
  299. unsigned char *ptr;
  300. *len = 0;
  301. *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
  302. if (*octets == NULL) {
  303. return 0;
  304. }
  305. ptr = *octets;
  306. while (ctx->pointer < eoc) {
  307. if (!asn1_octet_decode(ctx, (unsigned char *) ptr++)) {
  308. kfree(*octets);
  309. *octets = NULL;
  310. return 0;
  311. }
  312. (*len)++;
  313. }
  314. return 1;
  315. } */
  316. static unsigned char
  317. asn1_subid_decode(struct asn1_ctx *ctx, unsigned long *subid)
  318. {
  319. unsigned char ch;
  320. *subid = 0;
  321. do {
  322. if (!asn1_octet_decode(ctx, &ch))
  323. return 0;
  324. *subid <<= 7;
  325. *subid |= ch & 0x7F;
  326. } while ((ch & 0x80) == 0x80);
  327. return 1;
  328. }
  329. static int
  330. asn1_oid_decode(struct asn1_ctx *ctx,
  331. unsigned char *eoc, unsigned long **oid, unsigned int *len)
  332. {
  333. unsigned long subid;
  334. unsigned int size;
  335. unsigned long *optr;
  336. size = eoc - ctx->pointer + 1;
  337. /* first subid actually encodes first two subids */
  338. if (size < 2 || size > UINT_MAX/sizeof(unsigned long))
  339. return 0;
  340. *oid = kmalloc(size * sizeof(unsigned long), GFP_ATOMIC);
  341. if (*oid == NULL)
  342. return 0;
  343. optr = *oid;
  344. if (!asn1_subid_decode(ctx, &subid)) {
  345. kfree(*oid);
  346. *oid = NULL;
  347. return 0;
  348. }
  349. if (subid < 40) {
  350. optr[0] = 0;
  351. optr[1] = subid;
  352. } else if (subid < 80) {
  353. optr[0] = 1;
  354. optr[1] = subid - 40;
  355. } else {
  356. optr[0] = 2;
  357. optr[1] = subid - 80;
  358. }
  359. *len = 2;
  360. optr += 2;
  361. while (ctx->pointer < eoc) {
  362. if (++(*len) > size) {
  363. ctx->error = ASN1_ERR_DEC_BADVALUE;
  364. kfree(*oid);
  365. *oid = NULL;
  366. return 0;
  367. }
  368. if (!asn1_subid_decode(ctx, optr++)) {
  369. kfree(*oid);
  370. *oid = NULL;
  371. return 0;
  372. }
  373. }
  374. return 1;
  375. }
  376. static int
  377. compare_oid(unsigned long *oid1, unsigned int oid1len,
  378. unsigned long *oid2, unsigned int oid2len)
  379. {
  380. unsigned int i;
  381. if (oid1len != oid2len)
  382. return 0;
  383. else {
  384. for (i = 0; i < oid1len; i++) {
  385. if (oid1[i] != oid2[i])
  386. return 0;
  387. }
  388. return 1;
  389. }
  390. }
  391. /* BB check for endian conversion issues here */
  392. int
  393. decode_negTokenInit(unsigned char *security_blob, int length,
  394. enum securityEnum *secType)
  395. {
  396. struct asn1_ctx ctx;
  397. unsigned char *end;
  398. unsigned char *sequence_end;
  399. unsigned long *oid = NULL;
  400. unsigned int cls, con, tag, oidlen, rc;
  401. bool use_ntlmssp = false;
  402. bool use_kerberos = false;
  403. bool use_mskerberos = false;
  404. *secType = NTLM; /* BB eventually make Kerberos or NLTMSSP the default*/
  405. /* cifs_dump_mem(" Received SecBlob ", security_blob, length); */
  406. asn1_open(&ctx, security_blob, length);
  407. /* GSSAPI header */
  408. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  409. cFYI(1, ("Error decoding negTokenInit header"));
  410. return 0;
  411. } else if ((cls != ASN1_APL) || (con != ASN1_CON)
  412. || (tag != ASN1_EOC)) {
  413. cFYI(1, ("cls = %d con = %d tag = %d", cls, con, tag));
  414. return 0;
  415. }
  416. /* Check for SPNEGO OID -- remember to free obj->oid */
  417. rc = asn1_header_decode(&ctx, &end, &cls, &con, &tag);
  418. if (rc) {
  419. if ((tag == ASN1_OJI) && (con == ASN1_PRI) &&
  420. (cls == ASN1_UNI)) {
  421. rc = asn1_oid_decode(&ctx, end, &oid, &oidlen);
  422. if (rc) {
  423. rc = compare_oid(oid, oidlen, SPNEGO_OID,
  424. SPNEGO_OID_LEN);
  425. kfree(oid);
  426. }
  427. } else
  428. rc = 0;
  429. }
  430. /* SPNEGO OID not present or garbled -- bail out */
  431. if (!rc) {
  432. cFYI(1, ("Error decoding negTokenInit header"));
  433. return 0;
  434. }
  435. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  436. cFYI(1, ("Error decoding negTokenInit"));
  437. return 0;
  438. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)
  439. || (tag != ASN1_EOC)) {
  440. cFYI(1,
  441. ("cls = %d con = %d tag = %d end = %p (%d) exit 0",
  442. cls, con, tag, end, *end));
  443. return 0;
  444. }
  445. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  446. cFYI(1, ("Error decoding negTokenInit"));
  447. return 0;
  448. } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
  449. || (tag != ASN1_SEQ)) {
  450. cFYI(1,
  451. ("cls = %d con = %d tag = %d end = %p (%d) exit 1",
  452. cls, con, tag, end, *end));
  453. return 0;
  454. }
  455. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  456. cFYI(1, ("Error decoding 2nd part of negTokenInit"));
  457. return 0;
  458. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)
  459. || (tag != ASN1_EOC)) {
  460. cFYI(1,
  461. ("cls = %d con = %d tag = %d end = %p (%d) exit 0",
  462. cls, con, tag, end, *end));
  463. return 0;
  464. }
  465. if (asn1_header_decode
  466. (&ctx, &sequence_end, &cls, &con, &tag) == 0) {
  467. cFYI(1, ("Error decoding 2nd part of negTokenInit"));
  468. return 0;
  469. } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
  470. || (tag != ASN1_SEQ)) {
  471. cFYI(1,
  472. ("cls = %d con = %d tag = %d end = %p (%d) exit 1",
  473. cls, con, tag, end, *end));
  474. return 0;
  475. }
  476. while (!asn1_eoc_decode(&ctx, sequence_end)) {
  477. rc = asn1_header_decode(&ctx, &end, &cls, &con, &tag);
  478. if (!rc) {
  479. cFYI(1,
  480. ("Error decoding negTokenInit hdr exit2"));
  481. return 0;
  482. }
  483. if ((tag == ASN1_OJI) && (con == ASN1_PRI)) {
  484. if (asn1_oid_decode(&ctx, end, &oid, &oidlen)) {
  485. cFYI(1, ("OID len = %d oid = 0x%lx 0x%lx "
  486. "0x%lx 0x%lx", oidlen, *oid,
  487. *(oid + 1), *(oid + 2), *(oid + 3)));
  488. if (compare_oid(oid, oidlen, MSKRB5_OID,
  489. MSKRB5_OID_LEN) &&
  490. !use_kerberos)
  491. use_mskerberos = true;
  492. else if (compare_oid(oid, oidlen, KRB5_OID,
  493. KRB5_OID_LEN) &&
  494. !use_mskerberos)
  495. use_kerberos = true;
  496. else if (compare_oid(oid, oidlen, NTLMSSP_OID,
  497. NTLMSSP_OID_LEN))
  498. use_ntlmssp = true;
  499. kfree(oid);
  500. }
  501. } else {
  502. cFYI(1, ("Should be an oid what is going on?"));
  503. }
  504. }
  505. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  506. cFYI(1, ("Error decoding last part negTokenInit exit3"));
  507. return 0;
  508. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) {
  509. /* tag = 3 indicating mechListMIC */
  510. cFYI(1, ("Exit 4 cls = %d con = %d tag = %d end = %p (%d)",
  511. cls, con, tag, end, *end));
  512. return 0;
  513. }
  514. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  515. cFYI(1, ("Error decoding last part negTokenInit exit5"));
  516. return 0;
  517. } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
  518. || (tag != ASN1_SEQ)) {
  519. cFYI(1, ("cls = %d con = %d tag = %d end = %p (%d)",
  520. cls, con, tag, end, *end));
  521. }
  522. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  523. cFYI(1, ("Error decoding last part negTokenInit exit 7"));
  524. return 0;
  525. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) {
  526. cFYI(1, ("Exit 8 cls = %d con = %d tag = %d end = %p (%d)",
  527. cls, con, tag, end, *end));
  528. return 0;
  529. }
  530. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  531. cFYI(1, ("Error decoding last part negTokenInit exit9"));
  532. return 0;
  533. } else if ((cls != ASN1_UNI) || (con != ASN1_PRI)
  534. || (tag != ASN1_GENSTR)) {
  535. cFYI(1, ("Exit10 cls = %d con = %d tag = %d end = %p (%d)",
  536. cls, con, tag, end, *end));
  537. return 0;
  538. }
  539. cFYI(1, ("Need to call asn1_octets_decode() function for %s",
  540. ctx.pointer)); /* is this UTF-8 or ASCII? */
  541. if (use_kerberos)
  542. *secType = Kerberos;
  543. else if (use_mskerberos)
  544. *secType = MSKerberos;
  545. else if (use_ntlmssp)
  546. *secType = NTLMSSP;
  547. return 1;
  548. }