/ lib / krb5 / string-to-key-test.c
string-to-key-test.c
  1  /*
  2   * Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan
  3   * (Royal Institute of Technology, Stockholm, Sweden).
  4   * All rights reserved.
  5   *
  6   * Redistribution and use in source and binary forms, with or without
  7   * modification, are permitted provided that the following conditions
  8   * are met:
  9   *
 10   * 1. Redistributions of source code must retain the above copyright
 11   *    notice, this list of conditions and the following disclaimer.
 12   *
 13   * 2. Redistributions in binary form must reproduce the above copyright
 14   *    notice, this list of conditions and the following disclaimer in the
 15   *    documentation and/or other materials provided with the distribution.
 16   *
 17   * 3. Neither the name of KTH nor the names of its contributors may be
 18   *    used to endorse or promote products derived from this software without
 19   *    specific prior written permission.
 20   *
 21   * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
 22   * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
 25   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
 32  
 33  #include "krb5_locl.h"
 34  #include <err.h>
 35  
 36  enum { MAXSIZE = 24 };
 37  
 38  static struct testcase {
 39      const char *principal_name;
 40      const char *password;
 41      krb5_enctype enctype;
 42      unsigned char res[MAXSIZE];
 43  } tests[] = {
 44  #ifdef HEIM_KRB5_DES
 45      {"@", "", ETYPE_DES_CBC_MD5,
 46       {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xf1}},
 47      {"nisse@FOO.SE", "hej", ETYPE_DES_CBC_MD5,
 48       {0xfe, 0x67, 0xbf, 0x9e, 0x57, 0x6b, 0xfe, 0x52}},
 49      {"assar/liten@FOO.SE", "hemligt", ETYPE_DES_CBC_MD5,
 50       {0x5b, 0x9b, 0xcb, 0xf2, 0x97, 0x43, 0xc8, 0x40}},
 51      {"raeburn@ATHENA.MIT.EDU", "password", ETYPE_DES_CBC_MD5,
 52       {0xcb, 0xc2, 0x2f, 0xae, 0x23, 0x52, 0x98, 0xe3}},
 53      {"danny@WHITEHOUSE.GOV", "potatoe", ETYPE_DES_CBC_MD5,
 54       {0xdf, 0x3d, 0x32, 0xa7, 0x4f, 0xd9, 0x2a, 0x01}},
 55      {"buckaroo@EXAMPLE.COM", "penny", ETYPE_DES_CBC_MD5,
 56       {0x94, 0x43, 0xa2, 0xe5, 0x32, 0xfd, 0xc4, 0xf1}},
 57      {"Juri\xc5\xa1i\xc4\x87@ATHENA.MIT.EDU", "\xc3\x9f", ETYPE_DES_CBC_MD5,
 58       {0x62, 0xc8, 0x1a, 0x52, 0x32, 0xb5, 0xe6, 0x9d}},
 59      {"AAAAAAAA", "11119999", ETYPE_DES_CBC_MD5,
 60       {0x98, 0x40, 0x54, 0xd0, 0xf1, 0xa7, 0x3e, 0x31}},
 61      {"FFFFAAAA", "NNNN6666", ETYPE_DES_CBC_MD5,
 62       {0xc4, 0xbf, 0x6b, 0x25, 0xad, 0xf7, 0xa4, 0xf8}},
 63  #endif
 64  #if 0
 65      {"@", "", ETYPE_DES3_CBC_SHA1,
 66       {0xce, 0xa2, 0x2f, 0x9b, 0x52, 0x2c, 0xb0, 0x15, 0x6e, 0x6b, 0x64,
 67        0x73, 0x62, 0x64, 0x73, 0x4f, 0x6e, 0x73, 0xce, 0xa2, 0x2f, 0x9b,
 68        0x52, 0x57}},
 69  #endif
 70  #ifdef HEIM_KRB5_DES3
 71      {"nisse@FOO.SE", "hej", ETYPE_DES3_CBC_SHA1,
 72       {0x0e, 0xbc, 0x23, 0x9d, 0x68, 0x46, 0xf2, 0xd5, 0x51, 0x98, 0x5b,
 73        0x57, 0xc1, 0x57, 0x01, 0x79, 0x04, 0xc4, 0xe9, 0xfe, 0xc1, 0x0e,
 74        0x13, 0xd0}},
 75      {"assar/liten@FOO.SE", "hemligt", ETYPE_DES3_CBC_SHA1,
 76       {0x7f, 0x40, 0x67, 0xb9, 0xbc, 0xc4, 0x40, 0xfb, 0x43, 0x73, 0xd9,
 77        0xd3, 0xcd, 0x7c, 0xc7, 0x67, 0xe6, 0x79, 0x94, 0xd0, 0xa8, 0x34,
 78        0xdf, 0x62}},
 79      {"raeburn@ATHENA.MIT.EDU", "password", ETYPE_DES3_CBC_SHA1,
 80       {0x85, 0x0b, 0xb5, 0x13, 0x58, 0x54, 0x8c, 0xd0, 0x5e, 0x86, 0x76, 0x8c, 0x31, 0x3e, 0x3b, 0xfe, 0xf7, 0x51, 0x19, 0x37, 0xdc, 0xf7, 0x2c, 0x3e}},
 81      {"danny@WHITEHOUSE.GOV", "potatoe", ETYPE_DES3_CBC_SHA1,
 82       {0xdf, 0xcd, 0x23, 0x3d, 0xd0, 0xa4, 0x32, 0x04, 0xea, 0x6d, 0xc4, 0x37, 0xfb, 0x15, 0xe0, 0x61, 0xb0, 0x29, 0x79, 0xc1, 0xf7, 0x4f, 0x37, 0x7a}},
 83      {"buckaroo@EXAMPLE.COM", "penny", ETYPE_DES3_CBC_SHA1,
 84       {0x6d, 0x2f, 0xcd, 0xf2, 0xd6, 0xfb, 0xbc, 0x3d, 0xdc, 0xad, 0xb5, 0xda, 0x57, 0x10, 0xa2, 0x34, 0x89, 0xb0, 0xd3, 0xb6, 0x9d, 0x5d, 0x9d, 0x4a}},
 85      {"Juri\xc5\xa1i\xc4\x87@ATHENA.MIT.EDU", "\xc3\x9f", ETYPE_DES3_CBC_SHA1,
 86       {0x16, 0xd5, 0xa4, 0x0e, 0x1c, 0xe3, 0xba, 0xcb, 0x61, 0xb9, 0xdc, 0xe0, 0x04, 0x70, 0x32, 0x4c, 0x83, 0x19, 0x73, 0xa7, 0xb9, 0x52, 0xfe, 0xb0}},
 87  #endif
 88  #ifdef HEIM_KRB5_ARCFOUR
 89      {"does/not@MATTER", "foo", ETYPE_ARCFOUR_HMAC_MD5,
 90       {0xac, 0x8e, 0x65, 0x7f, 0x83, 0xdf, 0x82, 0xbe,
 91        0xea, 0x5d, 0x43, 0xbd, 0xaf, 0x78, 0x00, 0xcc}},
 92  #endif
 93      {NULL}
 94  };
 95  
 96  int
 97  main(int argc, char **argv)
 98  {
 99      struct testcase *t;
100      krb5_context context;
101      krb5_error_code ret;
102      int val = 0;
103  
104      ret = krb5_init_context (&context);
105      if (ret)
106  	errx (1, "krb5_init_context failed: %d", ret);
107  
108      /* to enable realm-less principal name above */
109  
110      krb5_set_default_realm(context, "");
111  
112      for (t = tests; t->principal_name; ++t) {
113  	krb5_keyblock key;
114  	krb5_principal principal;
115  	int i;
116  
117  	ret = krb5_parse_name (context, t->principal_name, &principal);
118  	if (ret)
119  	    krb5_err (context, 1, ret, "krb5_parse_name %s",
120  		      t->principal_name);
121  	ret = krb5_string_to_key (context, t->enctype, t->password,
122  				  principal, &key);
123  	if (ret)
124  	    krb5_err (context, 1, ret, "krb5_string_to_key");
125  	krb5_free_principal (context, principal);
126  	if (memcmp (key.keyvalue.data, t->res, key.keyvalue.length) != 0) {
127  	    const unsigned char *p = key.keyvalue.data;
128  
129  	    printf ("string_to_key(%s, %s) failed\n",
130  		    t->principal_name, t->password);
131  	    printf ("should be: ");
132  	    for (i = 0; i < key.keyvalue.length; ++i)
133  		printf ("%02x", t->res[i]);
134  	    printf ("\nresult was: ");
135  	    for (i = 0; i < key.keyvalue.length; ++i)
136  		printf ("%02x", p[i]);
137  	    printf ("\n");
138  	    val = 1;
139  	}
140  	krb5_free_keyblock_contents(context, &key);
141      }
142      krb5_free_context(context);
143      return val;
144  }