asn1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. }
  331. optr = *oid;
  332. if (!asn1_subid_decode(ctx, &subid)) {
  333. kfree(*oid);
  334. *oid = NULL;
  335. return 0;
  336. }
  337. if (subid < 40) {
  338. optr[0] = 0;
  339. optr[1] = subid;
  340. } else if (subid < 80) {
  341. optr[0] = 1;
  342. optr[1] = subid - 40;
  343. } else {
  344. optr[0] = 2;
  345. optr[1] = subid - 80;
  346. }
  347. *len = 2;
  348. optr += 2;
  349. while (ctx->pointer < eoc) {
  350. if (++(*len) > size) {
  351. ctx->error = ASN1_ERR_DEC_BADVALUE;
  352. kfree(*oid);
  353. *oid = NULL;
  354. return 0;
  355. }
  356. if (!asn1_subid_decode(ctx, optr++)) {
  357. kfree(*oid);
  358. *oid = NULL;
  359. return 0;
  360. }
  361. }
  362. return 1;
  363. }
  364. static int
  365. compare_oid(unsigned long *oid1, unsigned int oid1len,
  366. unsigned long *oid2, unsigned int oid2len)
  367. {
  368. unsigned int i;
  369. if (oid1len != oid2len)
  370. return 0;
  371. else {
  372. for (i = 0; i < oid1len; i++) {
  373. if (oid1[i] != oid2[i])
  374. return 0;
  375. }
  376. return 1;
  377. }
  378. }
  379. /* BB check for endian conversion issues here */
  380. int
  381. decode_negTokenInit(unsigned char *security_blob, int length,
  382. enum securityEnum *secType)
  383. {
  384. struct asn1_ctx ctx;
  385. unsigned char *end;
  386. unsigned char *sequence_end;
  387. unsigned long *oid = NULL;
  388. unsigned int cls, con, tag, oidlen, rc;
  389. int use_ntlmssp = FALSE;
  390. *secType = NTLM; /* BB eventually make Kerberos or NLTMSSP the default */
  391. /* cifs_dump_mem(" Received SecBlob ", security_blob, length); */
  392. asn1_open(&ctx, security_blob, length);
  393. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  394. cFYI(1, ("Error decoding negTokenInit header"));
  395. return 0;
  396. } else if ((cls != ASN1_APL) || (con != ASN1_CON)
  397. || (tag != ASN1_EOC)) {
  398. cFYI(1, ("cls = %d con = %d tag = %d", cls, con, tag));
  399. return 0;
  400. } else {
  401. /* remember to free obj->oid */
  402. rc = asn1_header_decode(&ctx, &end, &cls, &con, &tag);
  403. if (rc) {
  404. if ((tag == ASN1_OJI) && (cls == ASN1_PRI)) {
  405. rc = asn1_oid_decode(&ctx, end, &oid, &oidlen);
  406. if (rc) {
  407. rc = compare_oid(oid, oidlen,
  408. SPNEGO_OID,
  409. SPNEGO_OID_LEN);
  410. kfree(oid);
  411. }
  412. } else
  413. rc = 0;
  414. }
  415. if (!rc) {
  416. cFYI(1, ("Error decoding negTokenInit header"));
  417. return 0;
  418. }
  419. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  420. cFYI(1, ("Error decoding negTokenInit"));
  421. return 0;
  422. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)
  423. || (tag != ASN1_EOC)) {
  424. cFYI(1,("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,("cls = %d con = %d tag = %d end = %p (%d) exit 1",
  434. cls, con, tag, end, *end));
  435. return 0;
  436. }
  437. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  438. cFYI(1, ("Error decoding 2nd part of negTokenInit"));
  439. return 0;
  440. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)
  441. || (tag != ASN1_EOC)) {
  442. cFYI(1,
  443. ("cls = %d con = %d tag = %d end = %p (%d) exit 0",
  444. cls, con, tag, end, *end));
  445. return 0;
  446. }
  447. if (asn1_header_decode
  448. (&ctx, &sequence_end, &cls, &con, &tag) == 0) {
  449. cFYI(1, ("Error decoding 2nd part of negTokenInit"));
  450. return 0;
  451. } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
  452. || (tag != ASN1_SEQ)) {
  453. cFYI(1,
  454. ("cls = %d con = %d tag = %d end = %p (%d) exit 1",
  455. cls, con, tag, end, *end));
  456. return 0;
  457. }
  458. while (!asn1_eoc_decode(&ctx, sequence_end)) {
  459. rc = asn1_header_decode(&ctx, &end, &cls, &con, &tag);
  460. if (!rc) {
  461. cFYI(1,
  462. ("Error 1 decoding negTokenInit header exit 2"));
  463. return 0;
  464. }
  465. if ((tag == ASN1_OJI) && (con == ASN1_PRI)) {
  466. rc = asn1_oid_decode(&ctx, end, &oid, &oidlen);
  467. if(rc) {
  468. cFYI(1,
  469. ("OID len = %d oid = 0x%lx 0x%lx 0x%lx 0x%lx",
  470. oidlen, *oid, *(oid + 1), *(oid + 2),
  471. *(oid + 3)));
  472. rc = compare_oid(oid, oidlen, NTLMSSP_OID,
  473. NTLMSSP_OID_LEN);
  474. kfree(oid);
  475. if (rc)
  476. use_ntlmssp = TRUE;
  477. }
  478. } else {
  479. cFYI(1,("This should be an oid what is going on? "));
  480. }
  481. }
  482. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  483. cFYI(1,
  484. ("Error decoding last part of negTokenInit exit 3"));
  485. return 0;
  486. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) { /* tag = 3 indicating mechListMIC */
  487. cFYI(1,
  488. ("Exit 4 cls = %d con = %d tag = %d end = %p (%d)",
  489. cls, con, tag, end, *end));
  490. return 0;
  491. }
  492. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  493. cFYI(1,
  494. ("Error decoding last part of negTokenInit exit 5"));
  495. return 0;
  496. } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
  497. || (tag != ASN1_SEQ)) {
  498. cFYI(1,
  499. ("Exit 6 cls = %d con = %d tag = %d end = %p (%d)",
  500. cls, con, tag, end, *end));
  501. }
  502. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  503. cFYI(1,
  504. ("Error decoding last part of negTokenInit exit 7"));
  505. return 0;
  506. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) {
  507. cFYI(1,
  508. ("Exit 8 cls = %d con = %d tag = %d end = %p (%d)",
  509. cls, con, tag, end, *end));
  510. return 0;
  511. }
  512. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  513. cFYI(1,
  514. ("Error decoding last part of negTokenInit exit 9"));
  515. return 0;
  516. } else if ((cls != ASN1_UNI) || (con != ASN1_PRI)
  517. || (tag != ASN1_GENSTR)) {
  518. cFYI(1,
  519. ("Exit 10 cls = %d con = %d tag = %d end = %p (%d)",
  520. cls, con, tag, end, *end));
  521. return 0;
  522. }
  523. cFYI(1, ("Need to call asn1_octets_decode() function for this %s", ctx.pointer)); /* is this UTF-8 or ASCII? */
  524. }
  525. /* if (use_kerberos)
  526. *secType = Kerberos
  527. else */
  528. if (use_ntlmssp) {
  529. *secType = NTLMSSP;
  530. }
  531. return 1;
  532. }