docsis_main.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * DOCSIS configuration file encoder.
  3. * Copyright (c) 2001 Cornel Ciocirlan, ctrl@users.sourceforge.net.
  4. * Copyright (c) 2002 Evvolve Media SRL,office@evvolve.com
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  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. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * DOCSIS is a registered trademark of Cablelabs, http://www.cablelabs.com
  21. */
  22. #include "docsis.h"
  23. #include "docsis_symtable.h"
  24. #include "docsis_globals.h"
  25. #include "ethermac.h"
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #ifdef WIN32
  31. #include <io.h>
  32. #include "inet_aton.h"
  33. #else
  34. #include <unistd.h>
  35. #endif
  36. #include <fcntl.h>
  37. extern unsigned int line; /* defined in docsis_lex.l */
  38. unsigned int add_cm_mic ( unsigned char *tlvbuf, unsigned int tlvbuflen )
  39. {
  40. MD5_CTX mdContext;
  41. unsigned char digest[16];
  42. MD5Init(&mdContext );
  43. MD5Update(&mdContext, tlvbuf, tlvbuflen );
  44. MD5Final(digest,&mdContext);
  45. tlvbuf[tlvbuflen]= 6;
  46. tlvbuf[tlvbuflen+1]=16;
  47. memcpy ( tlvbuf+tlvbuflen+2, digest, 16);
  48. return (tlvbuflen+18); /* we added the CM Message Integrity Check */
  49. }
  50. unsigned int add_eod_and_pad ( unsigned char *tlvbuf, unsigned int tlvbuflen )
  51. {
  52. int nr_pads;
  53. tlvbuf[tlvbuflen]=255;
  54. tlvbuflen = tlvbuflen +1;
  55. nr_pads = 4 - (tlvbuflen-4 * ((unsigned int) floor ( (double) (tlvbuflen/4))));
  56. if ( nr_pads < 0 ) {
  57. printf ( "Illegal number of pads\n");
  58. exit (-1);
  59. }
  60. memset(&tlvbuf[tlvbuflen], 0, nr_pads );
  61. return (tlvbuflen+nr_pads);
  62. }
  63. unsigned int add_cmts_mic (unsigned char *tlvbuf, unsigned int tlvbuflen, unsigned char *key, int keylen )
  64. {
  65. int i;
  66. register unsigned char *cp,*dp;
  67. /* Only these configuration tlv's must be used to calculate the CMTS MIC */
  68. #define NR_TLVS 17
  69. unsigned char digest_order[NR_TLVS] = { 1,2,3,4,17,43,6,18,19,20,22,23,24,25,28,29,26 };
  70. unsigned char *cmts_tlvs;
  71. unsigned char digest[17];
  72. cmts_tlvs = (unsigned char *) malloc ( tlvbuflen+1 );
  73. dp = cmts_tlvs;
  74. for (i=0; i<NR_TLVS; i++ ) {
  75. cp = tlvbuf;
  76. while ( (unsigned int) (cp - tlvbuf) < tlvbuflen ) {
  77. if ( cp[0]==digest_order[i] ) {
  78. memcpy ( dp, cp, cp[1]+2 );
  79. dp = dp + cp[1] +2;
  80. cp = cp + cp[1] +2;
  81. } else {
  82. cp = cp + cp[1] +2;
  83. }
  84. }
  85. }
  86. printf ("##### Calculating CMTS MIC using TLVs:\n");
  87. decode_main_aggregate ( cmts_tlvs, dp-cmts_tlvs);
  88. printf ("##### End of CMTS MIC TLVs\n");
  89. hmac_md5 ( cmts_tlvs, dp-cmts_tlvs, key, keylen, digest) ;
  90. md5_print_digest(digest);
  91. tlvbuf[tlvbuflen]=7; /* CMTS MIC */
  92. tlvbuf[tlvbuflen+1]=16; /* length of MD5 digest */
  93. memcpy ( &tlvbuf[tlvbuflen+2], digest, 16);
  94. return (tlvbuflen+18);
  95. }
  96. void usage(char *prog_name )
  97. {
  98. printf( "DOCSIS Configuration File creator, version %s.%s\n", VERSION,PATCHLEVEL);
  99. printf ("Copyright (c) 2000 Cornel Ciocirlan, ctrl@users.sourceforge.net\n");
  100. printf( "Use to encode: \n\t %s -e <modem_cfg_file> <key_file> <output_file>\n",prog_name);
  101. printf( "or to decode: \n\t %s -d <binary_file>\n",prog_name);
  102. printf ( "\nWhere:\n<modem_cfg_file>\t= name of the text (human readable) configuration file\n<key_file>\t\t= text file containing the authentication key to be used\n\t\t\t for the CMTS MIC (Message Integrity Check).\n<output_file> \t\t= name of the output file where you want the binary data\n\t\t\t to be written to (if it doesn't exist it is created).\n\t\t\t This file can be TFTP-downloaded by DOCSIS-compliant\n\t\t\t cable modems\n<binary_file>\t\t= name of the binary file you want to decode\n");
  103. exit ( -10 );
  104. }
  105. int main(int argc,char *argv[] )
  106. {
  107. unsigned char key[65];
  108. FILE *cf,*kf,*of;
  109. unsigned char *buffer;
  110. unsigned int buflen=0,keylen=0;
  111. int i;
  112. #ifdef WIN32
  113. int nReturnCode;
  114. WSADATA wsaData;
  115. WORD wVersionRequired = MAKEWORD(1, 1);
  116. // WinSock DLL initialization
  117. nReturnCode = WSAStartup(wVersionRequired, &wsaData);
  118. if (nReturnCode != 0) {
  119. printf("error: WinSock initialization failure\n");
  120. exit(1);
  121. }
  122. // WinSock version check
  123. if (wsaData.wVersion != wVersionRequired) {
  124. printf("error: WinSock version unavailable\n");
  125. WSACleanup();
  126. exit(1);
  127. }
  128. init_mib();
  129. #endif
  130. init_snmp("snmpapp");
  131. init_global_symtable();
  132. if (! ds_get_boolean (DS_LIBRARY_ID,DS_LIB_PRINT_NUMERIC_OIDS)) {
  133. ds_toggle_boolean(DS_LIBRARY_ID, DS_LIB_PRINT_NUMERIC_OIDS);
  134. } /* we want OIDs to appear in numeric form */
  135. if (! ds_get_boolean (DS_LIBRARY_ID,DS_LIB_PRINT_NUMERIC_ENUM)) {
  136. ds_toggle_boolean(DS_LIBRARY_ID, DS_LIB_PRINT_NUMERIC_ENUM);
  137. } /* we want OIDs to appear in numeric form */
  138. if (! ds_get_boolean (DS_LIBRARY_ID,DS_LIB_PRINT_FULL_OID)) {
  139. ds_toggle_boolean(DS_LIBRARY_ID, DS_LIB_PRINT_FULL_OID);
  140. } /* we want to full numeric OID to be printed, including prefix */
  141. /* if (! ds_get_boolean (DS_LIBRARY_ID,DS_LIB_QUICK_PRINT)) {
  142. ds_toggle_boolean(DS_LIBRARY_ID, DS_LIB_QUICK_PRINT);
  143. } *//* quick print for easier parsing */
  144. if (! ds_get_boolean (DS_LIBRARY_ID,DS_LIB_DONT_BREAKDOWN_OIDS)) {
  145. ds_toggle_boolean(DS_LIBRARY_ID, DS_LIB_DONT_BREAKDOWN_OIDS);
  146. }
  147. if (! ds_get_boolean (DS_LIBRARY_ID, DS_LIB_RANDOM_ACCESS)) {
  148. ds_toggle_boolean(DS_LIBRARY_ID, DS_LIB_RANDOM_ACCESS);
  149. #ifdef DEBUG
  150. printf ("/* Random OID access: %d */\n", ds_get_boolean(DS_LIBRARY_ID,DS_LIB_RANDOM_ACCESS));
  151. #endif /* DEBUG */
  152. } /* so we can use sysContact.0 instead of system.sysContact.0 */
  153. memset (prog_name,0,255);
  154. strncpy (prog_name, argv[0], 254);
  155. if (! (argc==3 || argc==5)) usage (prog_name);
  156. if ( !strcmp (argv[1],"-e")) {
  157. if ( argc != 5 ) usage(prog_name);
  158. } else if ( !strcmp (argv[1],"-d")) {
  159. if (argc != 3 ) {
  160. usage(prog_name);
  161. } else {
  162. decode_file (argv[2]);
  163. }
  164. }
  165. if ( !strcmp (argv[2],argv[4]) ) {
  166. printf ("Error: source file is same as destination file\n");
  167. exit (-100); /* we don't overwrite the source file */
  168. }
  169. #ifdef WIN32
  170. if ( (cf = fopen ( argv[2],"rb" ))== NULL ) {
  171. #else
  172. if ( (cf = fopen ( argv[2],"r" ))== NULL ) {
  173. #endif
  174. printf ("%s: Can't open config file %s\n",argv[0],argv[1]);
  175. exit(-5);
  176. }
  177. #ifdef WIN32
  178. if ( (kf = fopen ( argv[3],"rb" ))== NULL ) {
  179. #else
  180. if ( (kf = fopen ( argv[3],"r" ))== NULL ) {
  181. #endif
  182. printf ("%s: Can't open keyfile %s\n",argv[0],argv[2]);
  183. exit(-5);
  184. }
  185. keylen = fread (key,sizeof(unsigned char), 64, kf);
  186. if (keylen < 1) {
  187. printf ("%s: error: key must be at least 1 char long\n",argv[0]);
  188. exit (-101);
  189. }
  190. while (key[keylen-1] == 10 || key[keylen-1]==13) {
  191. keylen--; /* eliminate trailing \n or \r */
  192. }
  193. parse_input_file(cf);
  194. /* decode_tlvlist(global_tlvlist, 0); */
  195. if ( global_tlvlist == NULL ) {
  196. printf ( "Error parsing config file %s\n", argv[1]);
  197. exit ( 70 );
  198. }
  199. for ( i=0; i<global_tlvlist->tlv_count; i++ ) {
  200. buflen+=2; /* docs_code and len */
  201. buflen+=global_tlvlist->tlvlist[i]->tlv_len;
  202. }
  203. buflen+=255;
  204. buffer = (unsigned char *) malloc ( buflen );
  205. buflen = flatten_tlvlist(buffer, global_tlvlist);
  206. decode_main_aggregate ( buffer, buflen);
  207. buflen = add_cm_mic ( buffer, buflen );
  208. buflen = add_cmts_mic ( buffer, buflen,key,keylen );
  209. buflen = add_eod_and_pad ( buffer, buflen );
  210. printf ("Final buffer content:\n");
  211. decode_main_aggregate ( buffer, buflen );
  212. #ifdef WIN32
  213. if ( (of = fopen ( argv[4],"wb" )) == NULL ) {
  214. #else
  215. if ( (of = fopen ( argv[4],"w" )) == NULL ) {
  216. #endif
  217. printf ("%s: Can't open output file %s\n",argv[0],argv[3]);
  218. exit(-5);
  219. }
  220. fwrite ( buffer, sizeof(unsigned char), buflen, of);
  221. return 0;
  222. }
  223. int init_global_symtable(void)
  224. {
  225. global_symtable = (symbol_type *) malloc(sizeof(symbol_type)*NUM_IDENTIFIERS); if (global_symtable == NULL) {
  226. printf ( "Error allocating memory!\n");
  227. exit (255);
  228. }
  229. memcpy(global_symtable, symtable, sizeof(symbol_type)*NUM_IDENTIFIERS);
  230. return 1;
  231. }
  232. void decode_file(char *file)
  233. {
  234. int ifd;
  235. unsigned char *buffer;
  236. unsigned int buflen=0;
  237. int rv=0;
  238. struct stat st;
  239. #ifdef WIN32
  240. if ( (ifd = open (file, O_RDONLY|O_BINARY))==-1 ) {
  241. #else
  242. if ( (ifd = open (file, O_RDONLY))==-1 ) {
  243. #endif
  244. printf("Error opening file %s: %s",file,strerror(errno));
  245. exit (-1);
  246. }
  247. if ((rv=fstat(ifd, &st))) {
  248. printf("stat on file %s: %s",file,strerror(errno));
  249. exit(-1);
  250. }
  251. buffer = (unsigned char *) malloc (st.st_size*sizeof(unsigned char)+1);
  252. buflen = read(ifd, buffer, st.st_size);
  253. decode_main_aggregate ( buffer, buflen );
  254. exit(0);
  255. }