asn1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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/config.h>
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/mm.h>
  24. #include <linux/slab.h>
  25. #include "cifspdu.h"
  26. #include "cifsglob.h"
  27. #include "cifs_debug.h"
  28. #include "cifsproto.h"
  29. /*****************************************************************************
  30. *
  31. * Basic ASN.1 decoding routines (gxsnmp author Dirk Wisse)
  32. *
  33. *****************************************************************************/
  34. /* Class */
  35. #define ASN1_UNI 0 /* Universal */
  36. #define ASN1_APL 1 /* Application */
  37. #define ASN1_CTX 2 /* Context */
  38. #define ASN1_PRV 3 /* Private */
  39. /* Tag */
  40. #define ASN1_EOC 0 /* End Of Contents or N/A */
  41. #define ASN1_BOL 1 /* Boolean */
  42. #define ASN1_INT 2 /* Integer */
  43. #define ASN1_BTS 3 /* Bit String */
  44. #define ASN1_OTS 4 /* Octet String */
  45. #define ASN1_NUL 5 /* Null */
  46. #define ASN1_OJI 6 /* Object Identifier */
  47. #define ASN1_OJD 7 /* Object Description */
  48. #define ASN1_EXT 8 /* External */
  49. #define ASN1_SEQ 16 /* Sequence */
  50. #define ASN1_SET 17 /* Set */
  51. #define ASN1_NUMSTR 18 /* Numerical String */
  52. #define ASN1_PRNSTR 19 /* Printable String */
  53. #define ASN1_TEXSTR 20 /* Teletext String */
  54. #define ASN1_VIDSTR 21 /* Video String */
  55. #define ASN1_IA5STR 22 /* IA5 String */
  56. #define ASN1_UNITIM 23 /* Universal Time */
  57. #define ASN1_GENTIM 24 /* General Time */
  58. #define ASN1_GRASTR 25 /* Graphical String */
  59. #define ASN1_VISSTR 26 /* Visible String */
  60. #define ASN1_GENSTR 27 /* General String */
  61. /* Primitive / Constructed methods*/
  62. #define ASN1_PRI 0 /* Primitive */
  63. #define ASN1_CON 1 /* Constructed */
  64. /*
  65. * Error codes.
  66. */
  67. #define ASN1_ERR_NOERROR 0
  68. #define ASN1_ERR_DEC_EMPTY 2
  69. #define ASN1_ERR_DEC_EOC_MISMATCH 3
  70. #define ASN1_ERR_DEC_LENGTH_MISMATCH 4
  71. #define ASN1_ERR_DEC_BADVALUE 5
  72. #define SPNEGO_OID_LEN 7
  73. #define NTLMSSP_OID_LEN 10
  74. static unsigned long SPNEGO_OID[7] = { 1, 3, 6, 1, 5, 5, 2 };
  75. static unsigned long NTLMSSP_OID[10] = { 1, 3, 6, 1, 4, 1, 311, 2, 2, 10 };
  76. /*
  77. * ASN.1 context.
  78. */
  79. struct asn1_ctx {
  80. int error; /* Error condition */
  81. unsigned char *pointer; /* Octet just to be decoded */
  82. unsigned char *begin; /* First octet */
  83. unsigned char *end; /* Octet after last octet */
  84. };
  85. /*
  86. * Octet string (not null terminated)
  87. */
  88. struct asn1_octstr {
  89. unsigned char *data;
  90. unsigned int len;
  91. };
  92. static void
  93. asn1_open(struct asn1_ctx *ctx, unsigned char *buf, unsigned int len)
  94. {
  95. ctx->begin = buf;
  96. ctx->end = buf + len;
  97. ctx->pointer = buf;
  98. ctx->error = ASN1_ERR_NOERROR;
  99. }
  100. static unsigned char
  101. asn1_octet_decode(struct asn1_ctx *ctx, unsigned char *ch)
  102. {
  103. if (ctx->pointer >= ctx->end) {
  104. ctx->error = ASN1_ERR_DEC_EMPTY;
  105. return 0;
  106. }
  107. *ch = *(ctx->pointer)++;
  108. return 1;
  109. }
  110. static unsigned char
  111. asn1_tag_decode(struct asn1_ctx *ctx, unsigned int *tag)
  112. {
  113. unsigned char ch;
  114. *tag = 0;
  115. do {
  116. if (!asn1_octet_decode(ctx, &ch))
  117. return 0;
  118. *tag <<= 7;
  119. *tag |= ch & 0x7F;
  120. } while ((ch & 0x80) == 0x80);
  121. return 1;
  122. }
  123. static unsigned char
  124. asn1_id_decode(struct asn1_ctx *ctx,
  125. unsigned int *cls, unsigned int *con, unsigned int *tag)
  126. {
  127. unsigned char ch;
  128. if (!asn1_octet_decode(ctx, &ch))
  129. return 0;
  130. *cls = (ch & 0xC0) >> 6;
  131. *con = (ch & 0x20) >> 5;
  132. *tag = (ch & 0x1F);
  133. if (*tag == 0x1F) {
  134. if (!asn1_tag_decode(ctx, tag))
  135. return 0;
  136. }
  137. return 1;
  138. }
  139. static unsigned char
  140. asn1_length_decode(struct asn1_ctx *ctx, unsigned int *def, unsigned int *len)
  141. {
  142. unsigned char ch, cnt;
  143. if (!asn1_octet_decode(ctx, &ch))
  144. return 0;
  145. if (ch == 0x80)
  146. *def = 0;
  147. else {
  148. *def = 1;
  149. if (ch < 0x80)
  150. *len = ch;
  151. else {
  152. cnt = (unsigned char) (ch & 0x7F);
  153. *len = 0;
  154. while (cnt > 0) {
  155. if (!asn1_octet_decode(ctx, &ch))
  156. return 0;
  157. *len <<= 8;
  158. *len |= ch;
  159. cnt--;
  160. }
  161. }
  162. }
  163. return 1;
  164. }
  165. static unsigned char
  166. asn1_header_decode(struct asn1_ctx *ctx,
  167. unsigned char **eoc,
  168. unsigned int *cls, unsigned int *con, unsigned int *tag)
  169. {
  170. unsigned int def = 0;
  171. unsigned int len = 0;
  172. if (!asn1_id_decode(ctx, cls, con, tag))
  173. return 0;
  174. if (!asn1_length_decode(ctx, &def, &len))
  175. return 0;
  176. if (def)
  177. *eoc = ctx->pointer + len;
  178. else
  179. *eoc = NULL;
  180. return 1;
  181. }
  182. static unsigned char
  183. asn1_eoc_decode(struct asn1_ctx *ctx, unsigned char *eoc)
  184. {
  185. unsigned char ch;
  186. if (eoc == NULL) {
  187. if (!asn1_octet_decode(ctx, &ch))
  188. return 0;
  189. if (ch != 0x00) {
  190. ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
  191. return 0;
  192. }
  193. if (!asn1_octet_decode(ctx, &ch))
  194. return 0;
  195. if (ch != 0x00) {
  196. ctx->error = ASN1_ERR_DEC_EOC_MISMATCH;
  197. return 0;
  198. }
  199. return 1;
  200. } else {
  201. if (ctx->pointer != eoc) {
  202. ctx->error = ASN1_ERR_DEC_LENGTH_MISMATCH;
  203. return 0;
  204. }
  205. return 1;
  206. }
  207. }
  208. /* static unsigned char asn1_null_decode(struct asn1_ctx *ctx,
  209. unsigned char *eoc)
  210. {
  211. ctx->pointer = eoc;
  212. return 1;
  213. }
  214. static unsigned char asn1_long_decode(struct asn1_ctx *ctx,
  215. unsigned char *eoc, long *integer)
  216. {
  217. unsigned char ch;
  218. unsigned int len;
  219. if (!asn1_octet_decode(ctx, &ch))
  220. return 0;
  221. *integer = (signed char) ch;
  222. len = 1;
  223. while (ctx->pointer < eoc) {
  224. if (++len > sizeof(long)) {
  225. ctx->error = ASN1_ERR_DEC_BADVALUE;
  226. return 0;
  227. }
  228. if (!asn1_octet_decode(ctx, &ch))
  229. return 0;
  230. *integer <<= 8;
  231. *integer |= ch;
  232. }
  233. return 1;
  234. }
  235. static unsigned char asn1_uint_decode(struct asn1_ctx *ctx,
  236. unsigned char *eoc,
  237. unsigned int *integer)
  238. {
  239. unsigned char ch;
  240. unsigned int len;
  241. if (!asn1_octet_decode(ctx, &ch))
  242. return 0;
  243. *integer = ch;
  244. if (ch == 0)
  245. len = 0;
  246. else
  247. len = 1;
  248. while (ctx->pointer < eoc) {
  249. if (++len > sizeof(unsigned int)) {
  250. ctx->error = ASN1_ERR_DEC_BADVALUE;
  251. return 0;
  252. }
  253. if (!asn1_octet_decode(ctx, &ch))
  254. return 0;
  255. *integer <<= 8;
  256. *integer |= ch;
  257. }
  258. return 1;
  259. }
  260. static unsigned char asn1_ulong_decode(struct asn1_ctx *ctx,
  261. unsigned char *eoc,
  262. unsigned long *integer)
  263. {
  264. unsigned char ch;
  265. unsigned int len;
  266. if (!asn1_octet_decode(ctx, &ch))
  267. return 0;
  268. *integer = ch;
  269. if (ch == 0)
  270. len = 0;
  271. else
  272. len = 1;
  273. while (ctx->pointer < eoc) {
  274. if (++len > sizeof(unsigned long)) {
  275. ctx->error = ASN1_ERR_DEC_BADVALUE;
  276. return 0;
  277. }
  278. if (!asn1_octet_decode(ctx, &ch))
  279. return 0;
  280. *integer <<= 8;
  281. *integer |= ch;
  282. }
  283. return 1;
  284. }
  285. static unsigned char
  286. asn1_octets_decode(struct asn1_ctx *ctx,
  287. unsigned char *eoc,
  288. unsigned char **octets, unsigned int *len)
  289. {
  290. unsigned char *ptr;
  291. *len = 0;
  292. *octets = kmalloc(eoc - ctx->pointer, GFP_ATOMIC);
  293. if (*octets == NULL) {
  294. return 0;
  295. }
  296. ptr = *octets;
  297. while (ctx->pointer < eoc) {
  298. if (!asn1_octet_decode(ctx, (unsigned char *) ptr++)) {
  299. kfree(*octets);
  300. *octets = NULL;
  301. return 0;
  302. }
  303. (*len)++;
  304. }
  305. return 1;
  306. } */
  307. static unsigned char
  308. asn1_subid_decode(struct asn1_ctx *ctx, unsigned long *subid)
  309. {
  310. unsigned char ch;
  311. *subid = 0;
  312. do {
  313. if (!asn1_octet_decode(ctx, &ch))
  314. return 0;
  315. *subid <<= 7;
  316. *subid |= ch & 0x7F;
  317. } while ((ch & 0x80) == 0x80);
  318. return 1;
  319. }
  320. static int
  321. asn1_oid_decode(struct asn1_ctx *ctx,
  322. unsigned char *eoc, unsigned long **oid, unsigned int *len)
  323. {
  324. unsigned long subid;
  325. unsigned int size;
  326. unsigned long *optr;
  327. size = eoc - ctx->pointer + 1;
  328. *oid = kmalloc(size * sizeof (unsigned long), GFP_ATOMIC);
  329. if (*oid == NULL) {
  330. return 0;
  331. }
  332. optr = *oid;
  333. if (!asn1_subid_decode(ctx, &subid)) {
  334. kfree(*oid);
  335. *oid = NULL;
  336. return 0;
  337. }
  338. if (subid < 40) {
  339. optr[0] = 0;
  340. optr[1] = subid;
  341. } else if (subid < 80) {
  342. optr[0] = 1;
  343. optr[1] = subid - 40;
  344. } else {
  345. optr[0] = 2;
  346. optr[1] = subid - 80;
  347. }
  348. *len = 2;
  349. optr += 2;
  350. while (ctx->pointer < eoc) {
  351. if (++(*len) > size) {
  352. ctx->error = ASN1_ERR_DEC_BADVALUE;
  353. kfree(*oid);
  354. *oid = NULL;
  355. return 0;
  356. }
  357. if (!asn1_subid_decode(ctx, optr++)) {
  358. kfree(*oid);
  359. *oid = NULL;
  360. return 0;
  361. }
  362. }
  363. return 1;
  364. }
  365. static int
  366. compare_oid(unsigned long *oid1, unsigned int oid1len,
  367. unsigned long *oid2, unsigned int oid2len)
  368. {
  369. unsigned int i;
  370. if (oid1len != oid2len)
  371. return 0;
  372. else {
  373. for (i = 0; i < oid1len; i++) {
  374. if (oid1[i] != oid2[i])
  375. return 0;
  376. }
  377. return 1;
  378. }
  379. }
  380. /* BB check for endian conversion issues here */
  381. int
  382. decode_negTokenInit(unsigned char *security_blob, int length,
  383. enum securityEnum *secType)
  384. {
  385. struct asn1_ctx ctx;
  386. unsigned char *end;
  387. unsigned char *sequence_end;
  388. unsigned long *oid = NULL;
  389. unsigned int cls, con, tag, oidlen, rc;
  390. int use_ntlmssp = FALSE;
  391. *secType = NTLM; /* BB eventually make Kerberos or NLTMSSP the default */
  392. /* cifs_dump_mem(" Received SecBlob ", security_blob, length); */
  393. asn1_open(&ctx, security_blob, length);
  394. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  395. cFYI(1, ("Error decoding negTokenInit header "));
  396. return 0;
  397. } else if ((cls != ASN1_APL) || (con != ASN1_CON)
  398. || (tag != ASN1_EOC)) {
  399. cFYI(1, ("cls = %d con = %d tag = %d", cls, con, tag));
  400. return 0;
  401. } else {
  402. /* remember to free obj->oid */
  403. rc = asn1_header_decode(&ctx, &end, &cls, &con, &tag);
  404. if (rc) {
  405. if ((tag == ASN1_OJI) && (cls == ASN1_PRI)) {
  406. rc = asn1_oid_decode(&ctx, end, &oid, &oidlen);
  407. if (rc) {
  408. rc = compare_oid(oid, oidlen,
  409. SPNEGO_OID,
  410. SPNEGO_OID_LEN);
  411. kfree(oid);
  412. }
  413. } else
  414. rc = 0;
  415. }
  416. if (!rc) {
  417. cFYI(1, ("Error decoding negTokenInit header"));
  418. return 0;
  419. }
  420. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  421. cFYI(1, ("Error decoding negTokenInit "));
  422. return 0;
  423. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)
  424. || (tag != ASN1_EOC)) {
  425. cFYI(1,("cls = %d con = %d tag = %d end = %p (%d) exit 0",
  426. cls, con, tag, end, *end));
  427. return 0;
  428. }
  429. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  430. cFYI(1, ("Error decoding negTokenInit "));
  431. return 0;
  432. } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
  433. || (tag != ASN1_SEQ)) {
  434. cFYI(1,("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 1 decoding negTokenInit header exit 2"));
  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 0x%lx 0x%lx",
  471. oidlen, *oid, *(oid + 1), *(oid + 2),
  472. *(oid + 3)));
  473. rc = compare_oid(oid, oidlen, NTLMSSP_OID,
  474. NTLMSSP_OID_LEN);
  475. kfree(oid);
  476. if (rc)
  477. use_ntlmssp = TRUE;
  478. }
  479. } else {
  480. cFYI(1,("This should be an oid what is going on? "));
  481. }
  482. }
  483. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  484. cFYI(1,
  485. ("Error decoding last part of negTokenInit exit 3"));
  486. return 0;
  487. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) { /* tag = 3 indicating mechListMIC */
  488. cFYI(1,
  489. ("Exit 4 cls = %d con = %d tag = %d end = %p (%d)",
  490. cls, con, tag, end, *end));
  491. return 0;
  492. }
  493. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  494. cFYI(1,
  495. ("Error decoding last part of negTokenInit exit 5"));
  496. return 0;
  497. } else if ((cls != ASN1_UNI) || (con != ASN1_CON)
  498. || (tag != ASN1_SEQ)) {
  499. cFYI(1,
  500. ("Exit 6 cls = %d con = %d tag = %d end = %p (%d)",
  501. cls, con, tag, end, *end));
  502. }
  503. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  504. cFYI(1,
  505. ("Error decoding last part of negTokenInit exit 7"));
  506. return 0;
  507. } else if ((cls != ASN1_CTX) || (con != ASN1_CON)) {
  508. cFYI(1,
  509. ("Exit 8 cls = %d con = %d tag = %d end = %p (%d)",
  510. cls, con, tag, end, *end));
  511. return 0;
  512. }
  513. if (asn1_header_decode(&ctx, &end, &cls, &con, &tag) == 0) {
  514. cFYI(1,
  515. ("Error decoding last part of negTokenInit exit 9"));
  516. return 0;
  517. } else if ((cls != ASN1_UNI) || (con != ASN1_PRI)
  518. || (tag != ASN1_GENSTR)) {
  519. cFYI(1,
  520. ("Exit 10 cls = %d con = %d tag = %d end = %p (%d)",
  521. cls, con, tag, end, *end));
  522. return 0;
  523. }
  524. cFYI(1, ("Need to call asn1_octets_decode() function for this %s", ctx.pointer)); /* is this UTF-8 or ASCII? */
  525. }
  526. /* if (use_kerberos)
  527. *secType = Kerberos
  528. else */
  529. if (use_ntlmssp) {
  530. *secType = NTLMSSP;
  531. }
  532. return 1;
  533. }