asn1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. static unsigned long SPNEGO_OID[7] = { 1, 3, 6, 1, 5, 5, 2 };
  74. static unsigned long NTLMSSP_OID[10] = { 1, 3, 6, 1, 4, 1, 311, 2, 2, 10 };
  75. /*
  76. * ASN.1 context.
  77. */
  78. struct asn1_ctx {
  79. int error; /* Error condition */
  80. unsigned char *pointer; /* Octet just to be decoded */
  81. unsigned char *begin; /* First octet */
  82. unsigned char *end; /* Octet after last octet */
  83. };
  84. /*
  85. * Octet string (not null terminated)
  86. */
  87. struct asn1_octstr {
  88. unsigned char *data;
  89. unsigned int len;
  90. };
  91. static void
  92. asn1_open(struct asn1_ctx *ctx, unsigned char *buf, unsigned int len)
  93. {
  94. ctx->begin = buf;
  95. ctx->end = buf + len;
  96. ctx->pointer = buf;
  97. ctx->error = ASN1_ERR_NOERROR;
  98. }
  99. static unsigned char
  100. asn1_octet_decode(struct asn1_ctx *ctx, unsigned char *ch)
  101. {
  102. if (ctx->pointer >= ctx->end) {
  103. ctx->error = ASN1_ERR_DEC_EMPTY;
  104. return 0;
  105. }
  106. *ch = *(ctx->pointer)++;
  107. return 1;
  108. }
  109. static unsigned char
  110. asn1_tag_decode(struct asn1_ctx *ctx, unsigned int *tag)
  111. {
  112. unsigned char ch;
  113. *tag = 0;
  114. do {
  115. if (!asn1_octet_decode(ctx, &ch))
  116. return 0;
  117. *tag <<= 7;
  118. *tag |= ch & 0x7F;
  119. } while ((ch & 0x80) == 0x80);
  120. return 1;
  121. }
  122. static unsigned char
  123. asn1_id_decode(struct asn1_ctx *ctx,
  124. unsigned int *cls, unsigned int *con, unsigned int *tag)
  125. {
  126. unsigned char ch;
  127. if (!asn1_octet_decode(ctx, &ch))
  128. return 0;
  129. *cls = (ch & 0xC0) >> 6;
  130. *con = (ch & 0x20) >> 5;
  131. *tag = (ch & 0x1F);
  132. if (*tag == 0x1F) {
  133. if (!asn1_tag_decode(ctx, tag))
  134. return 0;
  135. }
  136. return 1;
  137. }
  138. static unsigned char
  139. asn1_length_decode(struct asn1_ctx *ctx, unsigned int *def, unsigned int *len)
  140. {
  141. unsigned char ch, cnt;
  142. if (!asn1_octet_decode(ctx, &ch))
  143. return 0;
  144. if (ch == 0x80)
  145. *def = 0;
  146. else {
  147. *def = 1;
  148. if (ch < 0x80)
  149. *len = ch;
  150. else {
  151. cnt = (unsigned char) (ch & 0x7F);
  152. *len = 0;
  153. while (cnt > 0) {
  154. if (!asn1_octet_decode(ctx, &ch))
  155. return 0;
  156. *len <<= 8;
  157. *len |= ch;
  158. cnt--;
  159. }
  160. }
  161. }
  162. return 1;
  163. }
  164. static unsigned char
  165. asn1_header_decode(struct asn1_ctx *ctx,
  166. unsigned char **eoc,
  167. unsigned int *cls, unsigned int *con, unsigned int *tag)
  168. {
  169. unsigned int def = 0;
  170. unsigned int len = 0;
  171. if (!asn1_id_decode(ctx, cls, con, tag))
  172. return 0;
  173. if (!asn1_length_decode(ctx, &def, &len))
  174. return 0;
  175. if (def)
  176. *eoc = ctx->pointer + len;
  177. else
  178. *eoc = NULL;
  179. return 1;
  180. }
  181. static unsigned char
  182. asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
  183. {
  184. unsigned char ch;
  185. if (eoc == NULL) {
  186. if (!asn1_octet_decode(ctx, &ch))
  187. return 0;
  188. if (ch != 0x00) {
  189. ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
  190. return 0;
  191. }
  192. if (!asn1_octet_decode(ctx, &ch))
  193. return 0;
  194. if (ch != 0x00) {
  195. ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
  196. return 0;
  197. }
  198. return 1;
  199. } else {
  200. if (ctx->pointer != eoc) {
  201. ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
  202. return 0;
  203. }
  204. return 1;
  205. }
  206. }
  207. /* static unsigned char asn1_null_decode(struct asn1_ctx *ctx,
  208. unsigned char *eoc)
  209. {
  210. ctx->pointer = eoc;
  211. return 1;
  212. }
  213. static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
  214. unsigned char *eoc, long *integer)
  215. {
  216. unsigned char ch;
  217. unsigned int len;
  218. if (!asn1_octet_decode(ctx, &ch))
  219. return 0;
  220. *integer = (signed char) ch;
  221. len = 1;
  222. while (ctx->pointer < eoc) {
  223. if (++len > sizeof(long)) {
  224. ctx->error = ASN1_ERR_DEC_BADVALUE;
  225. return 0;
  226. }
  227. if (!asn1_octet_decode(ctx, &ch))
  228. return 0;
  229. *integer <<= 8;
  230. *integer |= ch;
  231. }
  232. return 1;
  233. }
  234. static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
  235. unsigned char *eoc,
  236. unsigned int *integer)
  237. {
  238. unsigned char ch;
  239. unsigned int len;
  240. if (!asn1_octet_decode(ctx, &ch))
  241. return 0;
  242. *integer = ch;
  243. if (ch == 0)
  244. len = 0;
  245. else
  246. len = 1;
  247. while (ctx->pointer < eoc) {
  248. if (++len > sizeof(unsigned int)) {
  249. ctx->error = ASN1_ERR_DEC_BADVALUE;
  250. return 0;
  251. }
  252. if (!asn1_octet_decode(ctx, &ch))
  253. return 0;
  254. *integer <<= 8;
  255. *integer |= ch;
  256. }
  257. return 1;
  258. }
  259. static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
  260. unsigned char *eoc,
  261. unsigned long *integer)
  262. {
  263. unsigned char ch;
  264. unsigned int len;
  265. if (!asn1_octet_decode(ctx, &ch))
  266. return 0;
  267. *integer = ch;
  268. if (ch == 0)
  269. len = 0;
  270. else
  271. len = 1;
  272. while (ctx->pointer < eoc) {
  273. if (++len > sizeof(unsigned long)) {
  274. ctx->error = ASN1_ERR_DEC_BADVALUE;
  275. return 0;
  276. }
  277. if (!asn1_octet_decode(ctx, &ch))
  278. return 0;
  279. *integer <<= 8;
  280. *integer |= ch;
  281. }
  282. return 1;
  283. }
  284. static unsigned char
  285. asn1_octets_decode(struct asn1_ctx *ctx,
  286. unsigned char *eoc,
  287. unsigned char **octets, unsigned int *len)
  288. {
  289. unsigned char *ptr;
  290. *len = 0;
  291. *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
  292. if (*octets == NULL) {
  293. return 0;
  294. }
  295. ptr = *octets;
  296. while (ctx->pointer < eoc) {
  297. if (!asn1_octet_decode(ctx, (unsigned char *) ptr++)) {
  298. kfree(*octets);
  299. *octets = NULL;
  300. return 0;
  301. }
  302. (*len)++;
  303. }
  304. return 1;
  305. } */
  306. static unsigned char
  307. asn1_subid_decode(struct asn1_ctx *ctx, unsigned long *subid)
  308. {
  309. unsigned char ch;
  310. *subid = 0;
  311. do {
  312. if (!asn1_octet_decode(ctx, &ch))
  313. return 0;
  314. *subid <<= 7;
  315. *subid |= ch & 0x7F;
  316. } while ((ch & 0x80) == 0x80);
  317. return 1;
  318. }
  319. static int
  320. asn1_oid_decode(struct asn1_ctx *ctx,
  321. unsigned char *eoc, unsigned long **oid, unsigned int *len)
  322. {
  323. unsigned long subid;
  324. unsigned int size;
  325. unsigned long *optr;
  326. size = eoc - ctx->pointer + 1;
  327. *oid = kmalloc(size * sizeof(unsigned long), GFP_ATOMIC);
  328. if (*oid == NULL)
  329. return 0;
  330. optr = *oid;
  331. if (!asn1_subid_decode(ctx, &subid)) {
  332. kfree(*oid);
  333. *oid = NULL;
  334. return 0;
  335. }
  336. if (subid < 40) {
  337. optr[0] = 0;
  338. optr[1] = subid;
  339. } else if (subid < 80) {
  340. optr[0] = 1;
  341. optr[1] = subid - 40;
  342. } else {
  343. optr[0] = 2;
  344. optr[1] = subid - 80;
  345. }
  346. *len = 2;
  347. optr += 2;
  348. while (ctx->pointer < eoc) {
  349. if (++(*len) > size) {
  350. ctx->error = ASN1_ERR_DEC_BADVALUE;
  351. kfree(*oid);
  352. *oid = NULL;
  353. return 0;
  354. }
  355. if (!asn1_subid_decode(ctx, optr++)) {
  356. kfree(*oid);
  357. *oid = NULL;
  358. return 0;
  359. }
  360. }
  361. return 1;
  362. }
  363. static int
  364. compare_oid(unsigned long *oid1, unsigned int oid1len,
  365. unsigned long *oid2, unsigned int oid2len)
  366. {
  367. unsigned int i;
  368. if (oid1len != oid2len)
  369. return 0;
  370. else {
  371. for (i = 0; i < oid1len; i++) {
  372. if (oid1[i] != oid2[i])
  373. return 0;
  374. }
  375. return 1;
  376. }
  377. }
  378. /* BB check for endian conversion issues here */
  379. int
  380. decode_negTokenInit(unsigned char *security_blob, int length,
  381. enum securityEnum *secType)
  382. {
  383. struct asn1_ctx ctx;
  384. unsigned char *end;
  385. unsigned char *sequence_end;
  386. unsigned long *oid = NULL;
  387. unsigned int cls, con, tag, oidlen, rc;
  388. int use_ntlmssp = FALSE;
  389. *secType = NTLM; /* BB eventually make Kerberos or NLTMSSP the default*/
  390. /* cifs_dump_mem(" Received SecBlob ", security_blob, length); */
  391. asn1_open(&ctx, security_blob, length);
  392. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  393. cFYI(1, ("Error decoding negTokenInit header"));
  394. return 0;
  395. } else if ((cls != ASN1_APL) || (con != ASN1_CON)
  396. || (tag != ASN1_EOC)) {
  397. cFYI(1, ("cls = %d con = %d tag = %d", cls, con, tag));
  398. return 0;
  399. } else {
  400. /* remember to free obj->oid */
  401. rc = asn1_header_decode(&ctx, &end, &cls, &con, &tag);
  402. if (rc) {
  403. if ((tag == ASN1_OJI) && (cls == ASN1_PRI)) {
  404. rc = asn1_oid_decode(&ctx, end, &oid, &oidlen);
  405. if (rc) {
  406. rc = compare_oid(oid, oidlen,
  407. SPNEGO_OID,
  408. SPNEGO_OID_LEN);
  409. kfree(oid);
  410. }
  411. } else
  412. rc = 0;
  413. }
  414. if (!rc) {
  415. cFYI(1, ("Error decoding negTokenInit header"));
  416. return 0;
  417. }
  418. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  419. cFYI(1, ("Error decoding negTokenInit"));
  420. return 0;
  421. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)
  422. || (tag != ASN1_EOC)) {
  423. cFYI(1,
  424. ("cls = %d con = %d tag = %d end = %p (%d) exit 0",
  425. cls, con, tag, end, *end));
  426. return 0;
  427. }
  428. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  429. cFYI(1, ("Error decoding negTokenInit"));
  430. return 0;
  431. } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
  432. || (tag != ASN1_SEQ)) {
  433. cFYI(1,
  434. ("cls = %d con = %d tag = %d end = %p (%d) exit 1",
  435. cls, con, tag, end, *end));
  436. return 0;
  437. }
  438. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  439. cFYI(1, ("Error decoding 2nd part of negTokenInit"));
  440. return 0;
  441. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)
  442. || (tag != ASN1_EOC)) {
  443. cFYI(1,
  444. ("cls = %d con = %d tag = %d end = %p (%d) exit 0",
  445. cls, con, tag, end, *end));
  446. return 0;
  447. }
  448. if (asn1_header_decode
  449. (&ctx, &sequence_end, &cls, &con, &tag) == 0) {
  450. cFYI(1, ("Error decoding 2nd part of negTokenInit"));
  451. return 0;
  452. } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
  453. || (tag != ASN1_SEQ)) {
  454. cFYI(1,
  455. ("cls = %d con = %d tag = %d end = %p (%d) exit 1",
  456. cls, con, tag, end, *end));
  457. return 0;
  458. }
  459. while (!asn1_eoc_decode(&ctx, sequence_end)) {
  460. rc = asn1_header_decode(&ctx, &end, &cls, &con, &tag);
  461. if (!rc) {
  462. cFYI(1,
  463. ("Error decoding negTokenInit hdr exit2"));
  464. return 0;
  465. }
  466. if ((tag == ASN1_OJI) && (con == ASN1_PRI)) {
  467. rc = asn1_oid_decode(&ctx, end, &oid, &oidlen);
  468. if (rc) {
  469. cFYI(1,
  470. ("OID len = %d oid = 0x%lx 0x%lx "
  471. "0x%lx 0x%lx",
  472. oidlen, *oid, *(oid + 1),
  473. *(oid + 2), *(oid + 3)));
  474. rc = compare_oid(oid, oidlen,
  475. NTLMSSP_OID, NTLMSSP_OID_LEN);
  476. kfree(oid);
  477. if (rc)
  478. use_ntlmssp = TRUE;
  479. }
  480. } else {
  481. cFYI(1, ("Should be an oid what is going on?"));
  482. }
  483. }
  484. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  485. cFYI(1,
  486. ("Error decoding last part negTokenInit exit3"));
  487. return 0;
  488. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) {
  489. /* tag = 3 indicating mechListMIC */
  490. cFYI(1,
  491. ("Exit 4 cls = %d con = %d tag = %d end = %p (%d)",
  492. cls, con, tag, end, *end));
  493. return 0;
  494. }
  495. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  496. cFYI(1,
  497. ("Error decoding last part negTokenInit exit5"));
  498. return 0;
  499. } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
  500. || (tag != ASN1_SEQ)) {
  501. cFYI(1, ("cls = %d con = %d tag = %d end = %p (%d)",
  502. cls, con, tag, end, *end));
  503. }
  504. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  505. cFYI(1,
  506. ("Error decoding last part negTokenInit exit 7"));
  507. return 0;
  508. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) {
  509. cFYI(1,
  510. ("Exit 8 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,
  516. ("Error decoding last part negTokenInit exit9"));
  517. return 0;
  518. } else if ((cls != ASN1_UNI) || (con != ASN1_PRI)
  519. || (tag != ASN1_GENSTR)) {
  520. cFYI(1,
  521. ("Exit10 cls = %d con = %d tag = %d end = %p (%d)",
  522. cls, con, tag, end, *end));
  523. return 0;
  524. }
  525. cFYI(1, ("Need to call asn1_octets_decode() function for %s",
  526. ctx.pointer)); /* is this UTF-8 or ASCII? */
  527. }
  528. /* if (use_kerberos)
  529. *secType = Kerberos
  530. else */
  531. if (use_ntlmssp) {
  532. *secType = NTLMSSP;
  533. }
  534. return 1;
  535. }