1 /* 2 * Copyright (c) 1998 - 1999 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 the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include "krb5_locl.h" 35 36 RCSID("$Id: codec.c,v 1.6 1999/12/02 17:05:08 joda Exp $"); 37 38 /* these functions does what the normal asn.1-functions does, but 39 converts the keytype to/from the on-the-wire enctypes */ 40 41 #if 1 42 #define DECODE(T, K) return decode_ ## T(data, length, t, len) 43 #define ENCODE(T, K) return encode_ ## T(data, length, t, len) 44 #else 45 #define DECODE(T, K) \ 46 { \ 47 krb5_error_code ret; \ 48 ret = decode_ ## T((void*)data, length, t, len); \ 49 if(ret) \ 50 return ret; \ 51 if(K) \ 52 ret = krb5_decode_keyblock(context, (K), 1); \ 53 return ret; \ 54 } 55 56 #define ENCODE(T, K) \ 57 { \ 58 krb5_error_code ret = 0; \ 59 if(K) \ 60 ret = krb5_decode_keyblock(context, (K), 0); \ 61 if(ret) \ 62 return ret; \ 63 return encode_ ## T(data, length, t, len); \ 64 } 65 #endif 66 67 krb5_error_code 68 krb5_decode_EncTicketPart (krb5_context context, 69 const void *data, 70 size_t length, 71 EncTicketPart *t, 72 size_t *len) 73 { 74 DECODE(EncTicketPart, &t->key); 75 } 76 77 krb5_error_code 78 krb5_encode_EncTicketPart (krb5_context context, 79 void *data, 80 size_t length, 81 EncTicketPart *t, 82 size_t *len) 83 { 84 ENCODE(EncTicketPart, &t->key); 85 } 86 87 krb5_error_code 88 krb5_decode_EncASRepPart (krb5_context context, 89 const void *data, 90 size_t length, 91 EncASRepPart *t, 92 size_t *len) 93 { 94 DECODE(EncASRepPart, &t->key); 95 } 96 97 krb5_error_code 98 krb5_encode_EncASRepPart (krb5_context context, 99 void *data, 100 size_t length, 101 EncASRepPart *t, 102 size_t *len) 103 { 104 ENCODE(EncASRepPart, &t->key); 105 } 106 107 krb5_error_code 108 krb5_decode_EncTGSRepPart (krb5_context context, 109 const void *data, 110 size_t length, 111 EncTGSRepPart *t, 112 size_t *len) 113 { 114 DECODE(EncTGSRepPart, &t->key); 115 } 116 117 krb5_error_code 118 krb5_encode_EncTGSRepPart (krb5_context context, 119 void *data, 120 size_t length, 121 EncTGSRepPart *t, 122 size_t *len) 123 { 124 ENCODE(EncTGSRepPart, &t->key); 125 } 126 127 krb5_error_code 128 krb5_decode_EncAPRepPart (krb5_context context, 129 const void *data, 130 size_t length, 131 EncAPRepPart *t, 132 size_t *len) 133 { 134 DECODE(EncAPRepPart, t->subkey); 135 } 136 137 krb5_error_code 138 krb5_encode_EncAPRepPart (krb5_context context, 139 void *data, 140 size_t length, 141 EncAPRepPart *t, 142 size_t *len) 143 { 144 ENCODE(EncAPRepPart, t->subkey); 145 } 146 147 krb5_error_code 148 krb5_decode_Authenticator (krb5_context context, 149 const void *data, 150 size_t length, 151 Authenticator *t, 152 size_t *len) 153 { 154 DECODE(Authenticator, t->subkey); 155 } 156 157 krb5_error_code 158 krb5_encode_Authenticator (krb5_context context, 159 void *data, 160 size_t length, 161 Authenticator *t, 162 size_t *len) 163 { 164 ENCODE(Authenticator, t->subkey); 165 } 166 167 krb5_error_code 168 krb5_decode_EncKrbCredPart (krb5_context context, 169 const void *data, 170 size_t length, 171 EncKrbCredPart *t, 172 size_t *len) 173 { 174 #if 1 175 return decode_EncKrbCredPart(data, length, t, len); 176 #else 177 krb5_error_code ret; 178 int i; 179 ret = decode_EncKrbCredPart((void*)data, length, t, len); 180 if(ret) 181 return ret; 182 for(i = 0; i < t->ticket_info.len; i++) 183 if((ret = krb5_decode_keyblock(context, &t->ticket_info.val[i].key, 1))) 184 break; 185 return ret; 186 #endif 187 } 188 189 krb5_error_code 190 krb5_encode_EncKrbCredPart (krb5_context context, 191 void *data, 192 size_t length, 193 EncKrbCredPart *t, 194 size_t *len) 195 { 196 #if 0 197 krb5_error_code ret = 0; 198 int i; 199 200 for(i = 0; i < t->ticket_info.len; i++) 201 if((ret = krb5_decode_keyblock(context, &t->ticket_info.val[i].key, 0))) 202 break; 203 if(ret) return ret; 204 #endif 205 return encode_EncKrbCredPart (data, length, t, len); 206 } 207 208 krb5_error_code 209 krb5_decode_ETYPE_INFO (krb5_context context, 210 const void *data, 211 size_t length, 212 ETYPE_INFO *t, 213 size_t *len) 214 { 215 #if 1 216 return decode_ETYPE_INFO(data, length, t, len); 217 #else 218 krb5_error_code ret; 219 int i; 220 221 ret = decode_ETYPE_INFO((void*)data, length, t, len); 222 if(ret) 223 return ret; 224 for(i = 0; i < t->len; i++) { 225 if((ret = krb5_decode_keytype(context, &t->val[i].etype, 1))) 226 break; 227 } 228 return ret; 229 #endif 230 } 231 232 krb5_error_code 233 krb5_encode_ETYPE_INFO (krb5_context context, 234 void *data, 235 size_t length, 236 ETYPE_INFO *t, 237 size_t *len) 238 { 239 #if 0 240 krb5_error_code ret = 0; 241 242 int i; 243 /* XXX this will break, since we need one key-info for each enctype */ 244 /* XXX or do we? */ 245 for(i = 0; i < t->len; i++) 246 if((ret = krb5_decode_keytype(context, &t->val[i].etype, 0))) 247 break; 248 if(ret) return ret; 249 #endif 250 return encode_ETYPE_INFO (data, length, t, len); 251 } 252