trval.c: Added usage message, better options parsing, and support for
authorTheodore Tso <tytso@mit.edu>
Fri, 22 Jan 1999 08:53:16 +0000 (08:53 +0000)
committerTheodore Tso <tytso@mit.edu>
Fri, 22 Jan 1999 08:53:16 +0000 (08:53 +0000)
two new options: -hex, which will accept the asn.1 encoding in hex
(i.e., from a packet dump), and -krb5, which instructs the trval to
print out the krb5 interpretations of the data types.  Trval will now
also try to interpret the field names of known krb5 structures if
-krb5 is specified.

git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@11118 dc483132-0cff-0310-8789-dd5450dbe970

src/tests/asn.1/ChangeLog
src/tests/asn.1/trval.c

index 01caaf65846170fd8fff7640d17d1d0babe8966a..74d8d3b65713e317bad24ff93395fb56842ae9a7 100644 (file)
@@ -1,3 +1,13 @@
+Fri Jan 22 02:08:59 1999  Theodore Y. Ts'o  <tytso@mit.edu>
+
+       * trval.c: Added usage message, better options parsing, and
+               support for two new options: -hex, which will accept the
+               asn.1 encoding in hex (i.e., from a packet dump), and
+               -krb5, which instructs the trval to print out the krb5
+               interpretations of the data types.  Trval will now also
+               try to interpret the field names of known krb5 structures
+               if -krb5 is specified.
+
 1998-11-13  Theodore Ts'o  <tytso@rsts-11.mit.edu>
 
        * Makefile.in: Set the myfulldir and mydir variables (which are
index 15beb46f7cd052e3fd9306cdb0da25bf411a4826..ed393dbe5bbd908d9655de8f5ebc4829e9f5d97c 100644 (file)
@@ -95,11 +95,13 @@ int print_primitive_length = 1;
 int print_skip_context = 0;
 int print_skip_tagnum = 0;
 int print_context_shortcut = 0;
+int do_hex = 0;
 #ifdef KRB5
 int print_krb5_types = 0;
-int print_skip_krb5_tagnum = 0;
 #endif
 
+int current_appl_type = -1;
+
 void print_tag_type();
 int trval(), trval2(), decode_len(), do_cons(), do_prim();
 
@@ -107,24 +109,53 @@ int trval(), trval2(), decode_len(), do_cons(), do_prim();
 
 #ifdef STANDALONE
 
+void usage()
+{
+       fprintf(stderr, "Usage: trval [--types] [--krb5] [--hex] [file]\n");
+       exit(1);
+}
+
+/*
+ * Returns true if the option was selected.  Allow "-option" and
+ * "--option" syntax, since we used to accept only "-option"
+ */
+int check_option(word, option)
+       char *word;
+       char *option;
+{
+       if (word[0] != '-')
+               return 0;
+       if (word[1] == '-')
+               word++;
+       if (strcmp(word+1, option))
+               return 0;
+       return 1;
+}
+
 int main(argc, argv)
        int argc;
        char **argv;
 {
        int optflg = 1;
        FILE *fp;
-       int r;
+       int r = 0;
        
        while (--argc > 0) {
                argv++;
                if (optflg && *(argv)[0] == '-') {
-                       if (!strcmp(*argv,"-types"))
+                       if (check_option(*argv, "help"))
+                               usage();
+                       else if (check_option(*argv, "types"))
                                print_types = 1;
-                       else if (!strcmp(*argv,"-notypes"))
+                       else if (check_option(*argv, "notypes"))
                                print_types = 0;
-                       else {
+                       else if (check_option(*argv, "krb5"))
+                               print_krb5_types = 1;
+                       else if (check_option(*argv, "hex")) {
+                               do_hex = 1;
+                       } else {
                                fprintf(stderr,"trval: unknown option: %s\n", *argv);
-                               exit(1);
+                               usage();
                        }
                } else {
                        optflg = 0;
@@ -142,6 +173,17 @@ int main(argc, argv)
 }
 #endif
 
+int convert_nibble(ch)
+{
+    if (isdigit(ch))
+       return (ch - '0');
+    if (ch >= 'a' && ch <= 'f')
+       return (ch - 'a' + 10);
+    if (ch >= 'A' && ch <= 'F')
+       return (ch - 'A' + 10);
+    return -1;
+}
+
 int trval(fin, fout)
        FILE    *fin;
        FILE    *fout;
@@ -149,7 +191,7 @@ int trval(fin, fout)
        unsigned char *p;
        int maxlen;
        int len;
-       int cc;
+       int cc, cc2, n1, n2;
        int r;
        int rlen;
        
@@ -161,6 +203,16 @@ int trval(fin, fout)
                        maxlen += BUFSIZ;
                        p = (unsigned char *)realloc(p, maxlen);
                }
+               if (do_hex) {
+                       if (cc == ' ' || cc == '\n' || cc == '\t')
+                               continue;
+                       cc2 = fgetc(fin);
+                       if (cc2 == EOF)
+                               break;
+                       n1 = convert_nibble(cc);
+                       n2 = convert_nibble(cc2);
+                       cc = (n1 << 4) + n2;
+               }
                p[len++] = cc;
        }
        fprintf(fout, "<%d>", len);
@@ -179,7 +231,8 @@ int trval2(fp, enc, len, lev, rlen)
 {
        int l, eid, elen, xlen, r, rlen2;
        int rlen_ext = 0;
-       
+
+       r = OK;
 
        if (len < 2) {
                fprintf(fp, "missing id and length octets (%d)\n", len);
@@ -314,9 +367,12 @@ int *rlen;
     int r = 0;
     int rlen2;
     int rlent;
+    int save_appl;
 
+    save_appl = current_appl_type;
     for (n = 0, rlent = 0; n < len; n+=rlen2, rlent+=rlen2) {
        r = trval2(fp, enc+n, len-n, lev, &rlen2);
+       current_appl_type = save_appl;
        if (r != OK) return(r);
     }
     if (rlent != len) {
@@ -328,13 +384,218 @@ int *rlen;
     return(r);
 }
 
+struct typestring_table {
+       int     k1, k2;
+       char    *str;
+       int     new_appl;
+};
+
+char *lookup_typestring(table, key1, key2)
+       struct typestring_table *table;
+       int     key1, key2;
+{
+       struct typestring_table *ent;
+
+       for (ent = table; ent->k1 > 0; ent++) {
+               if ((ent->k1 == key1) &&
+                   (ent->k2 == key2)) {
+                       if (ent->new_appl)
+                               current_appl_type = ent->new_appl;
+                       return ent->str;
+               }
+       }
+       return 0;
+}
+
+
+struct typestring_table univ_types[] = {
+       { PRIM_BOOL, -1, "Boolean"},
+       { PRIM_INT, -1, "Integer"},
+       { PRIM_BITS, -1, "Bit String"},
+       { PRIM_OCTS, -1, "Octet String"},
+       { PRIM_NULL, -1, "Null"},
+       { PRIM_OID, -1, "Object Identifier"},
+       { PRIM_ODE, -1, "Object Descriptor"},
+       { CONS_EXTN, -1, "External"},
+       { PRIM_REAL, -1, "Real"},
+       { PRIM_ENUM, -1, "Enumerated type"},
+       { PRIM_ENCR, -1, "Encrypted"},
+       { CONS_SEQ, -1, "Sequence/Sequence Of"},
+       { CONS_SET, -1, "Set/Set Of"},
+       { DEFN_NUMS, -1, "Numeric String"},
+       { DEFN_PRTS, -1, "Printable String"},
+       { DEFN_T61S, -1, "T.61 String"},
+       { DEFN_VTXS, -1, "Videotex String"},
+       { DEFN_IA5S, -1, "IA5 String"},
+       { DEFN_UTCT, -1, "UTCTime"},
+       { DEFN_GENT, -1, "Generalized Time"},
+       { DEFN_GFXS, -1, "Graphics string (ISO2375)"},
+       { DEFN_VISS, -1, "Visible string"},
+       { DEFN_GENS, -1, "General string"},
+       { DEFN_CHRS, -1, "Character string"},
+       { -1, -1, 0}
+       };
+
+#ifdef KRB5
+struct typestring_table krb5_types[] = {
+       { 1, -1, "Krb5 Ticket"},
+       { 2, -1, "Krb5 Autenticator"},
+       { 3, -1, "Krb5 Encrypted ticket part"},
+       { 10, -1, "Krb5 AS-REQ packet"},
+       { 11, -1, "Krb5 AS-REP packet"},
+       { 12, -1, "Krb5 TGS-REQ packet"},
+       { 13, -1, "Krb5 TGS-REP packet"},
+       { 14, -1, "Krb5 AP-REQ packet"},
+       { 15, -1, "Krb5 AP-REP packet"},
+       { 20, -1, "Krb5 SAFE packet"},
+       { 21, -1, "Krb5 PRIV packet"},
+       { 22, -1, "Krb5 CRED packet"},
+       { 30, -1, "Krb5 ERROR packet"},
+       { 25, -1, "Krb5 Encrypted AS-REQ part"},
+       { 26, -1, "Krb5 Encrypted TGS-REQ part"},
+       { 27, -1, "Krb5 Encrypted AP-REP part"},
+       { 28, -1, "Krb5 Encrypted PRIV part"},
+       { 29, -1, "Krb5 Encrypted CRED part"},
+       { -1, -1, 0}
+};
+
+struct typestring_table krb5_fields[] = {
+       { 1000, 0, "name-type"}, /* PrincipalName */
+       { 1000, 1, "name-string"},
+
+       { 1001, 0, "etype"},    /* Encrypted data */
+       { 1001, 1, "kvno"},     
+       { 1001, 2, "cipher"},
+
+       { 1002, 0, "addr-type"},        /* HostAddress */
+       { 1002, 1, "address"},  
+
+       { 1003, 0, "addr-type"},        /* HostAddresses */
+       { 1003, 1, "address"},  
+
+       { 1004, 0, "ad-type"},  /* AuthorizationData */
+       { 1004, 1, "ad-data"},  
+
+       { 1005, 0, "keytype"},  /* EncryptionKey */
+       { 1005, 1, "keyvalue"}, 
+
+       { 1006, 0, "cksumtype"},        /* Checksum */
+       { 1006, 1, "checksum"},
+
+       { 1007, 0, "kdc-options"},      /* KDC-REQ-BODY */
+       { 1007, 1, "cname", 1000},      
+       { 1007, 2, "realm"},
+       { 1007, 3, "sname", 1000},      
+       { 1007, 4, "from"},
+       { 1007, 5, "till"},     
+       { 1007, 6, "rtime"},
+       { 1007, 7, "nonce"},
+       { 1007, 8, "etype"},
+       { 1007, 9, "addresses", 1003},
+       { 1007, 10, "enc-authorization-data", 1004},
+       { 1007, 11, "additional-tickets"},
+
+       { 1008, 1, "padata-type"},      /* PA-DATA */
+       { 1008, 2, "pa-data"},
+
+       { 1009, 0, "user-data"},        /* KRB-SAFE-BODY */
+       { 1009, 1, "timestamp"},        
+       { 1009, 2, "usec"},
+       { 1009, 3, "seq-number"},       
+       { 1009, 4, "s-address", 1002},
+       { 1009, 5, "r-address", 1002},  
+
+       { 1, 0, "tkt-vno"},     /* Ticket */
+       { 1, 1, "realm"},
+       { 1, 2, "sname", 1000},
+       { 1, 3, "tkt-enc-part", 1001},
+
+       { 2, 0, "authenticator-vno"}, /* Authenticator */
+       { 2, 1, "crealm"},
+       { 2, 2, "cname", 1000},
+       { 2, 3, "cksum"},
+       { 2, 4, "cusec"},
+       { 2, 5, "ctime"},
+       { 2, 6, "subkey"},
+       { 2, 7, "seq-number"},
+       { 2, 8, "authorization-data", 1004},
+
+       { 10, 1, "pvno"},       /* AS-REQ */
+       { 10, 2, "msg-type"},
+       { 10, 3, "padata", 1008},
+       { 10, 4, "req-body", 1007},
+
+       { 11, 0, "pvno"},       /* AS-REP */
+       { 11, 1, "msg-type"},
+       { 11, 2, "padata", 1008},
+       { 11, 3, "crealm"},
+       { 11, 4, "cname", 1000},
+       { 11, 5, "ticket"},
+       { 11, 6, "enc-part", 1001},
+
+       { 12, 1, "pvno"},       /* TGS-REQ */
+       { 12, 2, "msg-type"},
+       { 12, 3, "padata", 1008},
+       { 12, 4, "req-body", 1007},
+
+       { 13, 0, "pvno"},       /* TGS-REP */
+       { 13, 1, "msg-type"},
+       { 13, 2, "padata", 1008},
+       { 13, 3, "crealm"},
+       { 13, 4, "cname", 1000},
+       { 13, 5, "ticket"},
+       { 13, 6, "enc-part", 1001},
+
+       { 14, 0, "pvno"},       /* AP-REQ */
+       { 14, 1, "msg-type"},
+       { 14, 2, "ap-options"},
+       { 14, 3, "ticket"},
+       { 14, 4, "authenticator", 1001},
+
+       { 15, 0, "pvno"},       /* AP-REP */
+       { 15, 1, "msg-type"},
+       { 15, 2, "enc-part", 1001},
+       
+       { 20, 0, "pvno"},       /* KRB-SAFE */
+       { 20, 1, "msg-type"},
+       { 20, 2, "safe-body", 1009},
+       { 20, 3, "cksum", 1006},
+
+       { 21, 0, "pvno"},       /* KRB-PRIV */
+       { 21, 1, "msg-type"},
+       { 21, 2, "enc-part", 1001},
+
+       { 22, 0, "pvno"},       /* KRB-CRED */
+       { 22, 1, "msg-type"},
+       { 22, 2, "tickets"},
+       { 22, 3, "enc-part", 1001},
+
+       { 30, 0, "pvno"},       /* KRB-ERROR */
+       { 30, 1, "msg-type"},
+       { 30, 2, "ctime"},
+       { 30, 3, "cusec"},
+       { 30, 4, "stime"},
+       { 30, 5, "susec"},
+       { 30, 6, "error-code"},
+       { 30, 7, "crealm"},
+       { 30, 8, "cname", 1000},
+       { 30, 9, "realm"},
+       { 30, 10, "sname", 1000},
+       { 30, 11, "e-text"},
+       { 30, 12, "e-data"},
+       
+       { -1, -1, 0}
+};
+#endif
+
 void print_tag_type(fp, eid, lev)
-       FILE *fp;
-       int     eid;
-       int     lev;
+        FILE *fp;
+        int     eid;
+        int     lev;
 {
        int     tag = eid & ID_TAG;
        int     do_space = 1;
+       char    *str;
 
        fprintf(fp, "[");
        
@@ -346,14 +607,29 @@ void print_tag_type(fp, eid, lev)
                        fprintf(fp, "UNIV %d", tag);
                break;
        case CLASS_APPL:
+               current_appl_type = tag;
 #ifdef KRB5
-               if (print_krb5_types && print_skip_krb5_tagnum)
-                       do_space = 0;
-               else
+               if (print_krb5_types) {
+                       str = lookup_typestring(krb5_types, tag, -1);
+                       if (str) {
+                               fputs(str, fp);
+                               break;
+                       }
+               }
 #endif
-                       fprintf(fp, "APPL %d", tag);
+               fprintf(fp, "APPL %d", tag);
                break;
        case CLASS_CONT:
+#ifdef KRB5
+               if (print_krb5_types && current_appl_type) {
+                       str = lookup_typestring(krb5_fields,
+                                               current_appl_type, tag);
+                       if (str) {
+                               fputs(str, fp);
+                               break;
+                       }
+               }
+#endif
                if (print_skip_context && lev)
                        fprintf(fp, "%d", tag);
                else
@@ -367,63 +643,13 @@ void print_tag_type(fp, eid, lev)
        if (print_types && ((eid & ID_CLASS) == CLASS_UNIV)) {
                if (do_space)
                        fprintf(fp, " ");
-               switch(eid & ID_TAG) {
-               case PRIM_BOOL: fprintf(fp, "Boolean"); break;
-               case PRIM_INT:  fprintf(fp, "Integer"); break;
-               case PRIM_BITS: fprintf(fp, "Bit String"); break;
-               case PRIM_OCTS: fprintf(fp, "Octet String"); break;
-               case PRIM_NULL: fprintf(fp, "Null"); break;
-               case PRIM_OID:  fprintf(fp, "Object Identifier"); break;
-               case PRIM_ODE:  fprintf(fp, "Object Descriptor"); break;
-               case CONS_EXTN: fprintf(fp, "External"); break;
-               case PRIM_REAL: fprintf(fp, "Real"); break;
-               case PRIM_ENUM: fprintf(fp, "Enumerated type"); break;
-               case PRIM_ENCR: fprintf(fp, "Encrypted"); break;
-               case CONS_SEQ:  fprintf(fp, "Sequence/Sequence Of"); break;
-               case CONS_SET:  fprintf(fp, "Set/Set Of"); break;
-               case DEFN_NUMS: fprintf(fp, "Numeric String"); break;
-               case DEFN_PRTS: fprintf(fp, "Printable String"); break;
-               case DEFN_T61S: fprintf(fp, "T.61 String"); break;
-               case DEFN_VTXS: fprintf(fp, "Videotex String"); break;
-               case DEFN_IA5S: fprintf(fp, "IA5 String"); break;
-               case DEFN_UTCT: fprintf(fp, "UTCTime"); break;
-               case DEFN_GENT: fprintf(fp, "Generalized Time"); break;
-               case DEFN_GFXS: fprintf(fp, "Graphics string (ISO2375)"); break;
-               case DEFN_VISS: fprintf(fp, "Visible string"); break;
-               case DEFN_GENS: fprintf(fp, "General string"); break;
-               case DEFN_CHRS: fprintf(fp, "Character string"); break;
-               default: fprintf(fp, "UNIV %d???", eid);
-               }
+               str = lookup_typestring(univ_types, eid & ID_TAG, -1);
+               if (str)
+                       fprintf(fp, str);
+               else
+                       fprintf(fp, "UNIV %d???", eid & ID_TAG);
        }
        
-#ifdef KRB5
-       if (print_krb5_types && ((eid & ID_CLASS) == CLASS_APPL)) {
-               if (do_space)
-                       fprintf(fp, " ");
-               switch(eid & ID_TAG) {
-               case 1: fprintf(fp, "Krb5 Ticket"); break;
-               case 2: fprintf(fp, "Krb5 Autenticator"); break;
-               case 3: fprintf(fp, "Krb5 Encrypted ticket part"); break;
-               case 10: fprintf(fp, "Krb5 AS-REQ packet"); break;
-               case 11: fprintf(fp, "Krb5 AS-REP packet"); break;
-               case 12: fprintf(fp, "Krb5 TGS-REQ packet"); break;
-               case 13: fprintf(fp, "Krb5 TGS-REP packet"); break;
-               case 14: fprintf(fp, "Krb5 AP-REQ packet"); break;
-               case 15: fprintf(fp, "Krb5 AP-REP packet"); break;
-               case 20: fprintf(fp, "Krb5 SAFE packet"); break;
-               case 21: fprintf(fp, "Krb5 PRIV packet"); break;
-               case 22: fprintf(fp, "Krb5 CRED packet"); break;
-               case 30: fprintf(fp, "Krb5 ERROR packet"); break;
-               case 25: fprintf(fp, "Krb5 Encrypted AS-REQ part"); break;
-               case 26: fprintf(fp, "Krb5 Encrypted TGS-REQ part"); break;
-               case 27: fprintf(fp, "Krb5 Encrypted AP-REP part"); break;
-               case 28: fprintf(fp, "Krb5 Encrypted PRIV part"); break;
-               case 29: fprintf(fp, "Krb5 Encrypted CRED part"); break;
-               default: fprintf(fp, "APPL %d???", eid);
-               }
-       }
-#endif
-
        fprintf(fp, "] ");
        
 }