1 /* asn1.c --- Utilities to manipulate RFC 1510 ASN.1 types.
2  * Copyright (C) 2002-2013 Simon Josefsson
3  *
4  * This file is part of Shishi.
5  *
6  * Shishi is free software; you can redistribute it and/or modify it it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Shishi is distributed in the hope that it will be useful, but but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Shishi; if not, see http://www.gnu.org/licenses or write
18  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19  * Floor, Boston, MA 02110-1301, USA
20  *
21  */
22 
23 /* Normally internal.h pulls in config.h, but since internal.h also
24    pulls in shishi.h, and we need to pull in libtasn1.h before
25    shishi.h to get Shishi_asn1 definition correct, we need to pull in
26    config.h here to avoid libtasn1.h pulling in system header files
27    without having parsed config.h first. */
28 #include <config.h>
29 #include <libtasn1.h>
30 #include "internal.h"
31 #include "asn1.h"
32 
33 #define ASN1NAME "KerberosV5Spec2."
34 
35 /* Generated by asn1Parser from ASN.1 module. */
36 extern const ASN1_ARRAY_TYPE shishi_asn1_tab[];
37 
38 /* Prototype in asn1.h, used by init.c. */
39 int
_shishi_asn1_init(Shishi * handle)40 _shishi_asn1_init (Shishi * handle)
41 {
42   char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE] = "";
43   int asn1_result;
44 
45   if (!asn1_check_version (ASN1_VERSION))
46     {
47       shishi_warn (handle, "asn1_check-version(%s) failed: %s",
48 		   ASN1_VERSION, asn1_check_version (NULL));
49       return SHISHI_ASN1_ERROR;
50     }
51 
52   if (!asn1_check_version ("0.2.5"))
53     shishi_warn (handle, "libtasn1 >= 0.2.5 preferred, you may see bugs.");
54 
55   asn1_result = asn1_array2tree (shishi_asn1_tab,
56 				 &handle->asn1, errorDescription);
57   if (asn1_result != ASN1_SUCCESS)
58     {
59       shishi_warn (handle, "asn1_array2tree() failed: %s\n",
60 		   asn1_strerror (asn1_result));
61       shishi_warn (handle, "%s", errorDescription);
62       return SHISHI_ASN1_ERROR;
63     }
64 
65   return SHISHI_OK;
66 }
67 
68 int
shishi_asn1_number_of_elements(Shishi * handle,Shishi_asn1 node,const char * field,size_t * n)69 shishi_asn1_number_of_elements (Shishi * handle, Shishi_asn1 node,
70 				const char *field, size_t * n)
71 {
72   int rc;
73   int tmp;
74 
75   rc = asn1_number_of_elements (node, field, &tmp);
76   *n = tmp;
77   if (rc != ASN1_SUCCESS)
78     {
79       if (rc == ASN1_ELEMENT_NOT_FOUND)
80 	return SHISHI_ASN1_NO_ELEMENT;
81       else
82 	return SHISHI_ASN1_ERROR;
83     }
84 
85   return SHISHI_OK;
86 }
87 
88 int
shishi_asn1_empty_p(Shishi * handle,Shishi_asn1 node,const char * field)89 shishi_asn1_empty_p (Shishi * handle, Shishi_asn1 node, const char *field)
90 {
91   int rc;
92   int datalen;
93 
94   datalen = 0;
95   rc = asn1_read_value (node, field, NULL, &datalen);
96   if (rc == ASN1_VALUE_NOT_FOUND)
97     return 1;
98 
99   return 0;
100 }
101 
102 /**
103  * shishi_asn1_read_inline:
104  * @handle: shishi handle as allocated by shishi_init().
105  * @node: ASN.1 variable to read field from.
106  * @field: name of field in @node to read.
107  * @data: pre-allocated output buffer that will hold ASN.1 field data.
108  * @datalen: on input, maximum size of output buffer,
109  *             on output, actual size of output buffer.
110  *
111  * Extract data stored in a ASN.1 field into a fixed size buffer
112  * allocated by caller.
113  *
114  * Note that since it is difficult to predict the length of the field,
115  * it is often better to use shishi_asn1_read() instead.
116  *
117  * Return value: Returns SHISHI_OK if successful,
118  *   SHISHI_ASN1_NO_ELEMENT if the element do not exist,
119  *   SHISHI_ASN1_NO_VALUE if the field has no value, ot
120  *   SHISHI_ASN1_ERROR otherwise.
121  **/
122 int
shishi_asn1_read_inline(Shishi * handle,Shishi_asn1 node,const char * field,char * data,size_t * datalen)123 shishi_asn1_read_inline (Shishi * handle, Shishi_asn1 node,
124 			 const char *field, char *data, size_t * datalen)
125 {
126   int rc;
127 
128   rc = asn1_read_value (node, field, (unsigned char *) data, (int *) datalen);
129   if (rc != ASN1_SUCCESS)
130     {
131       shishi_error_set (handle, asn1_strerror (rc));
132       if (rc == ASN1_ELEMENT_NOT_FOUND)
133 	return SHISHI_ASN1_NO_ELEMENT;
134       else if (rc == ASN1_VALUE_NOT_FOUND)
135 	return SHISHI_ASN1_NO_VALUE;
136       else
137 	return SHISHI_ASN1_ERROR;
138     }
139 
140   return SHISHI_OK;
141 }
142 
143 /**
144  * shishi_asn1_read:
145  * @handle: shishi handle as allocated by shishi_init().
146  * @node: ASN.1 variable to read field from.
147  * @field: name of field in @node to read.
148  * @data: newly allocated output buffer that will hold ASN.1 field data.
149  * @datalen: actual size of output buffer.
150  *
151  * Extract data stored in a ASN.1 field into a newly allocated buffer.
152  * The buffer will always be zero terminated, even though @datalen
153  * will not include the added zero.
154  *
155  * Return value: Returns SHISHI_OK if successful,
156  *   SHISHI_ASN1_NO_ELEMENT if the element do not exist,
157  *   SHISHI_ASN1_NO_VALUE if the field has no value, ot
158  *   SHISHI_ASN1_ERROR otherwise.
159  **/
160 int
shishi_asn1_read(Shishi * handle,Shishi_asn1 node,const char * field,char ** data,size_t * datalen)161 shishi_asn1_read (Shishi * handle,
162 		  Shishi_asn1 node, const char *field,
163 		  char **data, size_t * datalen)
164 {
165   int rc;
166   int len = 0;
167 
168   rc = asn1_read_value (node, field, NULL, &len);
169   if (rc != ASN1_SUCCESS && rc != ASN1_MEM_ERROR)
170     {
171       shishi_error_set (handle, asn1_strerror (rc));
172       if (rc == ASN1_ELEMENT_NOT_FOUND)
173 	return SHISHI_ASN1_NO_ELEMENT;
174       else if (rc == ASN1_VALUE_NOT_FOUND)
175 	return SHISHI_ASN1_NO_VALUE;
176       else
177 	return SHISHI_ASN1_ERROR;
178     }
179 
180   if (data)
181     {
182       size_t dlen = (size_t) len;
183 
184       *data = xmalloc (len + 1);
185 
186       if (len > 0)
187 	{
188 	  rc = shishi_asn1_read_inline (handle, node, field, *data, &dlen);
189 	  if (rc != SHISHI_OK)
190 	    return rc;
191 	}
192 
193       (*data)[len] = '\0';
194     }
195 
196   if (datalen)
197     *datalen = (size_t) len;
198 
199   return SHISHI_OK;
200 }
201 
202 /**
203  * shishi_asn1_read_optional:
204  * @handle: shishi handle as allocated by shishi_init().
205  * @node: ASN.1 variable to read field from.
206  * @field: name of field in @node to read.
207  * @data: newly allocated output buffer that will hold ASN.1 field data.
208  * @datalen: actual size of output buffer.
209  *
210  * Extract data stored in a ASN.1 field into a newly allocated buffer.
211  * If the field does not exist (i.e., SHISHI_ASN1_NO_ELEMENT), this
212  * function set datalen to 0 and succeeds.  Can be useful to read
213  * ASN.1 fields which are marked OPTIONAL in the grammar, if you want
214  * to avoid special error handling in your code.
215  *
216  * Return value: Returns SHISHI_OK if successful,
217  *   SHISHI_ASN1_NO_VALUE if the field has no value, ot
218  *   SHISHI_ASN1_ERROR otherwise.
219  **/
220 int
shishi_asn1_read_optional(Shishi * handle,Shishi_asn1 node,const char * field,char ** data,size_t * datalen)221 shishi_asn1_read_optional (Shishi * handle,
222 			   Shishi_asn1 node, const char *field,
223 			   char **data, size_t * datalen)
224 {
225   int rc;
226 
227   rc = shishi_asn1_read (handle, node, field, data, datalen);
228   if (rc != SHISHI_OK && rc != SHISHI_ASN1_NO_ELEMENT)
229     return rc;
230 
231   if (rc == SHISHI_ASN1_NO_ELEMENT)
232     if (datalen)
233       *datalen = 0;
234 
235   return SHISHI_OK;
236 }
237 
238 #define C2I(buf) ((buf[3] & 0xFF) |		\
239 		  ((buf[2] & 0xFF) << 8) |	\
240 		  ((buf[1] & 0xFF) << 16) |	\
241 		  ((buf[0] & 0xFF) << 24))
242 
243 int
shishi_asn1_read_int32(Shishi * handle,Shishi_asn1 node,const char * field,int32_t * i)244 shishi_asn1_read_int32 (Shishi * handle, Shishi_asn1 node,
245 			const char *field, int32_t * i)
246 {
247   char buf[4];
248   size_t buflen;
249   int rc;
250 
251   memset (buf, 0, sizeof (buf));
252   buflen = sizeof (buf);
253   rc = shishi_asn1_read_inline (handle, node, field, buf, &buflen);
254   if (rc != SHISHI_OK)
255     return rc;
256 
257   if (buflen < 4)
258     {
259       memset (buf, 0, sizeof (buf));
260       rc = shishi_asn1_read_inline (handle, node, field,
261 				    &buf[4 - buflen], &buflen);
262       if (rc != SHISHI_OK)
263 	return rc;
264     }
265   *i = C2I (buf);
266 
267   return SHISHI_OK;
268 }
269 
270 int
shishi_asn1_read_uint32(Shishi * handle,Shishi_asn1 node,const char * field,uint32_t * i)271 shishi_asn1_read_uint32 (Shishi * handle, Shishi_asn1 node,
272 			 const char *field, uint32_t * i)
273 {
274   return shishi_asn1_read_int32 (handle, node, field, (int32_t *) i);
275 }
276 
277 int
shishi_asn1_read_integer(Shishi * handle,Shishi_asn1 node,const char * field,int * i)278 shishi_asn1_read_integer (Shishi * handle, Shishi_asn1 node,
279 			  const char *field, int *i)
280 {
281   return shishi_asn1_read_int32 (handle, node, field, (int32_t *) i);
282 }
283 
284 int
shishi_asn1_read_bitstring(Shishi * handle,Shishi_asn1 node,const char * field,uint32_t * flags)285 shishi_asn1_read_bitstring (Shishi * handle, Shishi_asn1 node,
286 			    const char *field, uint32_t * flags)
287 {
288   char *buf;
289   size_t buflen;
290   size_t i;
291   int res;
292 
293   res = shishi_asn1_read (handle, node, field, &buf, &buflen);
294   if (res != SHISHI_OK)
295     return res;
296 
297   if (buflen < 4)
298     return SHISHI_ASN1_ERROR;
299 
300   *flags = 0;
301   for (i = 0; i < 4; i++)
302     {
303       *flags |= (((buf[i] >> 7) & 0x01) |
304 		 ((buf[i] >> 5) & 0x02) |
305 		 ((buf[i] >> 3) & 0x04) |
306 		 ((buf[i] >> 1) & 0x08) |
307 		 ((buf[i] << 1) & 0x10) |
308 		 ((buf[i] << 3) & 0x20) |
309 		 ((buf[i] << 5) & 0x40) | ((buf[i] << 7) & 0x80)) << (8 * i);
310     }
311 
312   return SHISHI_OK;
313 }
314 
315 int
shishi_asn1_write(Shishi * handle,Shishi_asn1 node,const char * field,const char * data,size_t datalen)316 shishi_asn1_write (Shishi * handle, Shishi_asn1 node,
317 		   const char *field, const char *data, size_t datalen)
318 {
319   int rc;
320 
321   rc = asn1_write_value (node, field,
322 			 (const unsigned char *) data, (int) datalen);
323   if (rc != ASN1_SUCCESS)
324     {
325       shishi_error_set (handle, asn1_strerror (rc));
326       return SHISHI_ASN1_ERROR;
327     }
328 
329   return SHISHI_OK;
330 }
331 
332 int
shishi_asn1_write_uint32(Shishi * handle,Shishi_asn1 node,const char * field,uint32_t n)333 shishi_asn1_write_uint32 (Shishi * handle, Shishi_asn1 node,
334 			  const char *field, uint32_t n)
335 {
336   char *buf;
337   int res;
338 
339   asprintf (&buf, "%lu", (unsigned long) n);
340   res = shishi_asn1_write (handle, node, field, buf, 0);
341   free (buf);
342   if (res != SHISHI_OK)
343     return res;
344 
345   return SHISHI_OK;
346 }
347 
348 int
shishi_asn1_write_int32(Shishi * handle,Shishi_asn1 node,const char * field,int32_t n)349 shishi_asn1_write_int32 (Shishi * handle, Shishi_asn1 node,
350 			 const char *field, int32_t n)
351 {
352   char *buf;
353   int res;
354 
355   asprintf (&buf, "%ld", (signed long) n);
356   res = shishi_asn1_write (handle, node, field, buf, 0);
357   free (buf);
358   if (res != SHISHI_OK)
359     return res;
360 
361   return SHISHI_OK;
362 }
363 
364 int
shishi_asn1_write_integer(Shishi * handle,Shishi_asn1 node,const char * field,int n)365 shishi_asn1_write_integer (Shishi * handle, Shishi_asn1 node,
366 			   const char *field, int n)
367 {
368   return shishi_asn1_write_int32 (handle, node, field, (int32_t) n);
369 }
370 
371 int
shishi_asn1_write_bitstring(Shishi * handle,Shishi_asn1 node,const char * field,uint32_t flags)372 shishi_asn1_write_bitstring (Shishi * handle, Shishi_asn1 node,
373 			     const char *field, uint32_t flags)
374 {
375   char buf[4];
376   size_t i;
377   int res;
378 
379   /* XXX
380      Cannot handle bit strings longer than 32 bits.
381      Currently not needed though. */
382 
383   for (i = 0; i < 4; i++)
384     {
385       buf[i] = ((((flags >> (8 * i)) >> 7) & 0x01) |
386 		(((flags >> (8 * i)) >> 5) & 0x02) |
387 		(((flags >> (8 * i)) >> 3) & 0x04) |
388 		(((flags >> (8 * i)) >> 1) & 0x08) |
389 		(((flags >> (8 * i)) << 1) & 0x10) |
390 		(((flags >> (8 * i)) << 3) & 0x20) |
391 		(((flags >> (8 * i)) << 5) & 0x40) |
392 		(((flags >> (8 * i)) << 7) & 0x80));
393     }
394 
395   res = shishi_asn1_write (handle, node, field, buf, 32);
396   if (res != SHISHI_OK)
397     return res;
398 
399   return SHISHI_OK;
400 }
401 
402 /**
403  * shishi_asn1_done:
404  * @handle: shishi handle as allocated by shishi_init().
405  * @node: ASN.1 node to dellocate.
406  *
407  * Deallocate resources associated with ASN.1 structure.  Note that
408  * the node must not be used after this call.
409  **/
410 void
shishi_asn1_done(Shishi * handle,Shishi_asn1 node)411 shishi_asn1_done (Shishi * handle, Shishi_asn1 node)
412 {
413 
414   int rc;
415 
416   if (node)
417     {
418       rc = asn1_delete_structure (&node);
419       if (rc != ASN1_SUCCESS)
420 	shishi_error_printf (handle, "Cannot dellocate ASN.1 structure: %s",
421 			     asn1_strerror (rc));
422     }
423 }
424 
425 static Shishi_asn1
asn1_new(Shishi * handle,const char * field,const char * name)426 asn1_new (Shishi * handle, const char *field, const char *name)
427 {
428   ASN1_TYPE node = ASN1_TYPE_EMPTY;
429   int res;
430 
431   res = asn1_create_element (handle->asn1, field, &node);
432   if (res != ASN1_SUCCESS)
433     {
434       shishi_error_set (handle, asn1_strerror (res));
435       return NULL;
436     }
437 
438   return (Shishi_asn1) node;
439 }
440 
441 /**
442  * shishi_asn1_pa_enc_ts_enc:
443  * @handle: shishi handle as allocated by shishi_init().
444  *
445  * Create new ASN.1 structure for PA-ENC-TS-ENC.
446  *
447  * Return value: Returns ASN.1 structure.
448  **/
449 Shishi_asn1
shishi_asn1_pa_enc_ts_enc(Shishi * handle)450 shishi_asn1_pa_enc_ts_enc (Shishi * handle)
451 {
452   return asn1_new (handle, ASN1NAME "PA-ENC-TS-ENC", "PA-ENC-TS-ENC");
453 }
454 
455 /**
456  * shishi_asn1_encrypteddata:
457  * @handle: shishi handle as allocated by shishi_init().
458  *
459  * Create new ASN.1 structure for EncryptedData
460  *
461  * Return value: Returns ASN.1 structure.
462  **/
463 Shishi_asn1
shishi_asn1_encrypteddata(Shishi * handle)464 shishi_asn1_encrypteddata (Shishi * handle)
465 {
466   return asn1_new (handle, ASN1NAME "EncryptedData", "EncryptedData");
467 }
468 
469 /**
470  * shishi_asn1_padata:
471  * @handle: shishi handle as allocated by shishi_init().
472  *
473  * Create new ASN.1 structure for PA-DATA.
474  *
475  * Return value: Returns ASN.1 structure.
476  **/
477 Shishi_asn1
shishi_asn1_padata(Shishi * handle)478 shishi_asn1_padata (Shishi * handle)
479 {
480   return asn1_new (handle, ASN1NAME "PA-DATA", "PA-DATA");
481 }
482 
483 /**
484  * shishi_asn1_methoddata:
485  * @handle: shishi handle as allocated by shishi_init().
486  *
487  * Create new ASN.1 structure for METHOD-DATA.
488  *
489  * Return value: Returns ASN.1 structure.
490  **/
491 Shishi_asn1
shishi_asn1_methoddata(Shishi * handle)492 shishi_asn1_methoddata (Shishi * handle)
493 {
494   return asn1_new (handle, ASN1NAME "METHOD-DATA", "METHOD-DATA");
495 }
496 
497 /**
498  * shishi_asn1_etype_info:
499  * @handle: shishi handle as allocated by shishi_init().
500  *
501  * Create new ASN.1 structure for ETYPE-INFO.
502  *
503  * Return value: Returns ASN.1 structure.
504  **/
505 Shishi_asn1
shishi_asn1_etype_info(Shishi * handle)506 shishi_asn1_etype_info (Shishi * handle)
507 {
508   return asn1_new (handle, ASN1NAME "ETYPE-INFO", "ETYPE-INFO");
509 }
510 
511 /**
512  * shishi_asn1_etype_info2:
513  * @handle: shishi handle as allocated by shishi_init().
514  *
515  * Create new ASN.1 structure for ETYPE-INFO2.
516  *
517  * Return value: Returns ASN.1 structure.
518  **/
519 Shishi_asn1
shishi_asn1_etype_info2(Shishi * handle)520 shishi_asn1_etype_info2 (Shishi * handle)
521 {
522   return asn1_new (handle, ASN1NAME "ETYPE-INFO2", "ETYPE-INFO2");
523 }
524 
525 /**
526  * shishi_asn1_asreq:
527  * @handle: shishi handle as allocated by shishi_init().
528  *
529  * Create new ASN.1 structure for AS-REQ.
530  *
531  * Return value: Returns ASN.1 structure.
532  **/
533 Shishi_asn1
shishi_asn1_asreq(Shishi * handle)534 shishi_asn1_asreq (Shishi * handle)
535 {
536   return asn1_new (handle, ASN1NAME "AS-REQ", "KDC-REQ");
537 }
538 
539 /**
540  * shishi_asn1_asrep:
541  * @handle: shishi handle as allocated by shishi_init().
542  *
543  * Create new ASN.1 structure for AS-REP.
544  *
545  * Return value: Returns ASN.1 structure.
546  **/
547 Shishi_asn1
shishi_asn1_asrep(Shishi * handle)548 shishi_asn1_asrep (Shishi * handle)
549 {
550   return asn1_new (handle, ASN1NAME "AS-REP", "KDC-REP");
551 }
552 
553 /**
554  * shishi_asn1_tgsreq:
555  * @handle: shishi handle as allocated by shishi_init().
556  *
557  * Create new ASN.1 structure for TGS-REQ.
558  *
559  * Return value: Returns ASN.1 structure.
560  **/
561 Shishi_asn1
shishi_asn1_tgsreq(Shishi * handle)562 shishi_asn1_tgsreq (Shishi * handle)
563 {
564   return asn1_new (handle, ASN1NAME "TGS-REQ", "KDC-REQ");
565 }
566 
567 /**
568  * shishi_asn1_tgsrep:
569  * @handle: shishi handle as allocated by shishi_init().
570  *
571  * Create new ASN.1 structure for TGS-REP.
572  *
573  * Return value: Returns ASN.1 structure.
574  **/
575 Shishi_asn1
shishi_asn1_tgsrep(Shishi * handle)576 shishi_asn1_tgsrep (Shishi * handle)
577 {
578   return asn1_new (handle, ASN1NAME "TGS-REP", "KDC-REP");
579 }
580 
581 /**
582  * shishi_asn1_apreq:
583  * @handle: shishi handle as allocated by shishi_init().
584  *
585  * Create new ASN.1 structure for AP-REQ.
586  *
587  * Return value: Returns ASN.1 structure.
588  **/
589 Shishi_asn1
shishi_asn1_apreq(Shishi * handle)590 shishi_asn1_apreq (Shishi * handle)
591 {
592   return asn1_new (handle, ASN1NAME "AP-REQ", "AP-REQ");
593 }
594 
595 /**
596  * shishi_asn1_aprep:
597  * @handle: shishi handle as allocated by shishi_init().
598  *
599  * Create new ASN.1 structure for AP-REP.
600  *
601  * Return value: Returns ASN.1 structure.
602  **/
603 Shishi_asn1
shishi_asn1_aprep(Shishi * handle)604 shishi_asn1_aprep (Shishi * handle)
605 {
606   return asn1_new (handle, ASN1NAME "AP-REP", "AP-REP");
607 }
608 
609 /**
610  * shishi_asn1_encapreppart:
611  * @handle: shishi handle as allocated by shishi_init().
612  *
613  * Create new ASN.1 structure for AP-REP.
614  *
615  * Return value: Returns ASN.1 structure.
616  **/
617 Shishi_asn1
shishi_asn1_encapreppart(Shishi * handle)618 shishi_asn1_encapreppart (Shishi * handle)
619 {
620   return asn1_new (handle, ASN1NAME "EncAPRepPart", "EncAPRepPart");
621 }
622 
623 /**
624  * shishi_asn1_ticket:
625  * @handle: shishi handle as allocated by shishi_init().
626  *
627  * Create new ASN.1 structure for Ticket.
628  *
629  * Return value: Returns ASN.1 structure.
630  **/
631 Shishi_asn1
shishi_asn1_ticket(Shishi * handle)632 shishi_asn1_ticket (Shishi * handle)
633 {
634   return asn1_new (handle, ASN1NAME "Ticket", "Ticket");
635 }
636 
637 /**
638  * shishi_asn1_encticketpart:
639  * @handle: shishi handle as allocated by shishi_init().
640  *
641  * Create new ASN.1 structure for EncTicketPart.
642  *
643  * Return value: Returns ASN.1 structure.
644  **/
645 Shishi_asn1
shishi_asn1_encticketpart(Shishi * handle)646 shishi_asn1_encticketpart (Shishi * handle)
647 {
648   return asn1_new (handle, ASN1NAME "EncTicketPart", "EncTicketPart");
649 }
650 
651 /**
652  * shishi_asn1_authenticator:
653  * @handle: shishi handle as allocated by shishi_init().
654  *
655  * Create new ASN.1 structure for Authenticator.
656  *
657  * Return value: Returns ASN.1 structure.
658  **/
659 Shishi_asn1
shishi_asn1_authenticator(Shishi * handle)660 shishi_asn1_authenticator (Shishi * handle)
661 {
662   return asn1_new (handle, ASN1NAME "Authenticator", "Authenticator");
663 }
664 
665 /**
666  * shishi_asn1_enckdcreppart:
667  * @handle: shishi handle as allocated by shishi_init().
668  *
669  * Create new ASN.1 structure for EncKDCRepPart.
670  *
671  * Return value: Returns ASN.1 structure.
672  **/
673 Shishi_asn1
shishi_asn1_enckdcreppart(Shishi * handle)674 shishi_asn1_enckdcreppart (Shishi * handle)
675 {
676   return asn1_new (handle, ASN1NAME "EncKDCRepPart", "EncKDCRepPart");
677 }
678 
679 /**
680  * shishi_asn1_encasreppart:
681  * @handle: shishi handle as allocated by shishi_init().
682  *
683  * Create new ASN.1 structure for EncASRepPart.
684  *
685  * Return value: Returns ASN.1 structure.
686  **/
687 Shishi_asn1
shishi_asn1_encasreppart(Shishi * handle)688 shishi_asn1_encasreppart (Shishi * handle)
689 {
690   return asn1_new (handle, ASN1NAME "EncASRepPart", "EncKDCRepPart");
691 }
692 
693 /**
694  * shishi_asn1_krberror:
695  * @handle: shishi handle as allocated by shishi_init().
696  *
697  * Create new ASN.1 structure for KRB-ERROR.
698  *
699  * Return value: Returns ASN.1 structure.
700  **/
701 Shishi_asn1
shishi_asn1_krberror(Shishi * handle)702 shishi_asn1_krberror (Shishi * handle)
703 {
704   return asn1_new (handle, ASN1NAME "KRB-ERROR", "KRB-ERROR");
705 }
706 
707 /**
708  * shishi_asn1_krbsafe:
709  * @handle: shishi handle as allocated by shishi_init().
710  *
711  * Create new ASN.1 structure for KRB-SAFE.
712  *
713  * Return value: Returns ASN.1 structure.
714  **/
715 Shishi_asn1
shishi_asn1_krbsafe(Shishi * handle)716 shishi_asn1_krbsafe (Shishi * handle)
717 {
718   return asn1_new (handle, ASN1NAME "KRB-SAFE", "KRB-SAFE");
719 }
720 
721 /**
722  * shishi_asn1_priv:
723  * @handle: shishi handle as allocated by shishi_init().
724  *
725  * Create new ASN.1 structure for KRB-PRIV.
726  *
727  * Return value: Returns ASN.1 structure.
728  **/
729 Shishi_asn1
shishi_asn1_priv(Shishi * handle)730 shishi_asn1_priv (Shishi * handle)
731 {
732   return asn1_new (handle, ASN1NAME "KRB-PRIV", "KRB-PRIV");
733 }
734 
735 /**
736  * shishi_asn1_encprivpart:
737  * @handle: shishi handle as allocated by shishi_init().
738  *
739  * Create new ASN.1 structure for EncKrbPrivPart.
740  *
741  * Return value: Returns ASN.1 structure.
742  **/
743 Shishi_asn1
shishi_asn1_encprivpart(Shishi * handle)744 shishi_asn1_encprivpart (Shishi * handle)
745 {
746   return asn1_new (handle, ASN1NAME "EncKrbPrivPart", "EncKrbPrivPart");
747 }
748 
749 /**
750  * shishi_asn1_to_der_field:
751  * @handle: shishi handle as allocated by shishi_init().
752  * @node: ASN.1 data that have field to extract.
753  * @field: name of field in @node to extract.
754  * @der: output array that holds DER encoding of @field in @node.
755  * @len: output variable with length of @der output array.
756  *
757  * Extract newly allocated DER representation of specified ASN.1 field.
758  *
759  * Return value: Returns SHISHI_OK if successful, or SHISHI_ASN1_ERROR
760  *   if DER encoding fails (common reasons for this is that the ASN.1
761  *   is missing required values).
762  **/
763 int
shishi_asn1_to_der_field(Shishi * handle,Shishi_asn1 node,const char * field,char ** der,size_t * len)764 shishi_asn1_to_der_field (Shishi * handle, Shishi_asn1 node,
765 			  const char *field, char **der, size_t * len)
766 {
767   char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE] = "";
768   int mylen = 0;
769   int rc;
770 
771   rc = asn1_der_coding (node, field, NULL, &mylen, errorDescription);
772   if (rc != ASN1_MEM_ERROR)
773     {
774       shishi_error_set (handle, errorDescription);
775       return SHISHI_ASN1_ERROR;
776     }
777 
778   *der = xmalloc (mylen);
779 
780   rc = asn1_der_coding (node, field, *der, &mylen, errorDescription);
781   if (rc != ASN1_SUCCESS)
782     {
783       shishi_error_set (handle, errorDescription);
784       return SHISHI_ASN1_ERROR;
785     }
786 
787   if (strcmp (field, "req-body") == 0)
788     {
789       unsigned char class;
790       int derlen, derlen2;
791       unsigned long tag;
792       signed long lenlen;
793 
794       /* XXX when encoding a field inside a SEQUENCE, libtasn1 appear
795          to include the tag from the SEQUENCE in the encoding of a
796          particular field.  This appear wrong, so we frob it here.
797          This typically happens when encoding req-body in KDC-REQ for
798          TGS checksums.  */
799 
800       rc = asn1_get_tag_der ((unsigned char *) *der, mylen, &class,
801 			     &derlen, &tag);
802       if (rc != ASN1_SUCCESS)
803 	{
804 	  shishi_error_set (handle, errorDescription);
805 	  return SHISHI_ASN1_ERROR;
806 	}
807 
808       lenlen = asn1_get_length_der ((unsigned char *) *der + derlen,
809 				    mylen - derlen, &derlen2);
810       if (lenlen < 0)
811 	return SHISHI_ASN1_ERROR;
812 
813       if (derlen + derlen2 < mylen)
814 	{
815 	  mylen -= derlen + derlen2;
816 	  memmove (*der, *der + derlen + derlen2, mylen);
817 	}
818     }
819 
820   *len = mylen;
821 
822   return SHISHI_OK;
823 }
824 
825 /**
826  * shishi_asn1_to_der:
827  * @handle: shishi handle as allocated by shishi_init().
828  * @node: ASN.1 data to convert to DER.
829  * @der: output array that holds DER encoding of @node.
830  * @len: output variable with length of @der output array.
831  *
832  * Extract newly allocated DER representation of specified ASN.1 data.
833  *
834  * Return value: Returns SHISHI_OK if successful, or SHISHI_ASN1_ERROR
835  *   if DER encoding fails (common reasons for this is that the ASN.1
836  *   is missing required values).
837  **/
838 int
shishi_asn1_to_der(Shishi * handle,Shishi_asn1 node,char ** der,size_t * len)839 shishi_asn1_to_der (Shishi * handle, Shishi_asn1 node, char **der,
840 		    size_t * len)
841 {
842   return shishi_asn1_to_der_field (handle, node, "", der, len);
843 }
844 
845 static Shishi_asn1
der2asn1(Shishi * handle,const char * fieldname,const char * nodename,const char * der,size_t derlen)846 der2asn1 (Shishi * handle,
847 	  const char *fieldname,
848 	  const char *nodename, const char *der, size_t derlen)
849 {
850   char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE] = "";
851   Shishi_asn1 structure = NULL;
852   int asn1_result = ASN1_SUCCESS;
853 
854   asn1_result = asn1_create_element (handle->asn1, fieldname, &structure);
855   if (asn1_result != ASN1_SUCCESS)
856     {
857       shishi_error_set (handle, asn1_strerror (asn1_result));
858       return NULL;
859     }
860 
861   asn1_result = asn1_der_decoding (&structure, (const unsigned char *) der,
862 				   (int) derlen, errorDescription);
863   if (asn1_result != ASN1_SUCCESS)
864     {
865       asn1_delete_structure (&structure);
866       shishi_error_set (handle, errorDescription);
867       return NULL;
868     }
869 
870   return structure;
871 }
872 
873 /**
874  * shishi_asn1_msgtype:
875  * @handle: shishi handle as allocated by shishi_init().
876  * @node: ASN.1 type to get msg type for.
877  *
878  * Determine msg-type of ASN.1 type of a packet. Currently this uses
879  * the msg-type field instead of the APPLICATION tag, but this may be
880  * changed in the future.
881  *
882  * Return value: Returns msg-type of ASN.1 type, 0 on failure.
883  **/
884 Shishi_msgtype
shishi_asn1_msgtype(Shishi * handle,Shishi_asn1 node)885 shishi_asn1_msgtype (Shishi * handle, Shishi_asn1 node)
886 {
887   asn1_retCode rc;
888   uint32_t msgtype;
889 
890   /* XXX Use APPLICATION tag instead. */
891   rc = shishi_asn1_read_uint32 (handle, node, "msg-type", &msgtype);
892   if (rc != SHISHI_OK)
893     return 0;
894 
895   return msgtype;
896 }
897 
898 /**
899  * shishi_der_msgtype:
900  * @handle: shishi handle as allocated by shishi_init().
901  * @der: input character array with DER encoding.
902  * @derlen: length of input character array with DER encoding.
903  *
904  * Determine msg-type of DER coded data of a packet.
905  *
906  * Return value: Returns msg-type of DER data, 0 on failure.
907  **/
908 Shishi_msgtype
shishi_der_msgtype(Shishi * handle,const char * der,size_t derlen)909 shishi_der_msgtype (Shishi * handle, const char *der, size_t derlen)
910 {
911   /* XXX Doesn't handle APPLICATION TAGS > 31. */
912   if (derlen > 1 && *der >= 0x60 && (unsigned char) *der <= 0x7F)
913     return *der - 0x60;
914   else
915     return 0;
916 }
917 
918 /**
919  * shishi_der2asn1:
920  * @handle: shishi handle as allocated by shishi_init().
921  * @der: input character array with DER encoding.
922  * @derlen: length of input character array with DER encoding.
923  *
924  * Convert arbitrary DER data of a packet to a ASN.1 type.
925  *
926  * Return value: Returns newly allocate ASN.1 corresponding to DER
927  *   data, or %NULL on failure.
928  **/
929 Shishi_asn1
shishi_der2asn1(Shishi * handle,const char * der,size_t derlen)930 shishi_der2asn1 (Shishi * handle, const char *der, size_t derlen)
931 {
932   Shishi_asn1 node = NULL;
933 
934   switch (shishi_der_msgtype (handle, der, derlen))
935     {
936     case SHISHI_MSGTYPE_AS_REQ:
937       node = shishi_der2asn1_asreq (handle, der, derlen);
938       break;
939 
940     case SHISHI_MSGTYPE_AS_REP:
941       node = shishi_der2asn1_asrep (handle, der, derlen);
942       break;
943 
944     case SHISHI_MSGTYPE_TGS_REQ:
945       node = shishi_der2asn1_tgsreq (handle, der, derlen);
946       break;
947 
948     case SHISHI_MSGTYPE_TGS_REP:
949       node = shishi_der2asn1_tgsrep (handle, der, derlen);
950       break;
951 
952     case SHISHI_MSGTYPE_AP_REQ:
953       node = shishi_der2asn1_apreq (handle, der, derlen);
954       break;
955 
956     case SHISHI_MSGTYPE_AP_REP:
957       node = shishi_der2asn1_aprep (handle, der, derlen);
958       break;
959 
960     case SHISHI_MSGTYPE_SAFE:
961       node = shishi_der2asn1_krbsafe (handle, der, derlen);
962       break;
963 
964     case SHISHI_MSGTYPE_PRIV:
965       node = shishi_der2asn1_priv (handle, der, derlen);
966       break;
967 
968     case SHISHI_MSGTYPE_CRED:
969       /* node = shishi_der2asn1_cred (handle, der, derlen); */
970       break;
971 
972     case SHISHI_MSGTYPE_ERROR:
973       node = shishi_der2asn1_krberror (handle, der, derlen);
974       break;
975 
976     case SHISHI_MSGTYPE_RESERVED16:
977     case SHISHI_MSGTYPE_RESERVED17:
978     default:
979       node = NULL;
980       break;
981     }
982 
983   return node;
984 }
985 
986 /**
987  * shishi_der2asn1_padata:
988  * @handle: shishi handle as allocated by shishi_init().
989  * @der: input character array with DER encoding.
990  * @derlen: length of input character array with DER encoding.
991  *
992  * Decode DER encoding of PA-DATA and create a ASN.1 structure.
993  *
994  * Return value: Returns ASN.1 structure corresponding to DER data.
995  **/
996 Shishi_asn1
shishi_der2asn1_padata(Shishi * handle,const char * der,size_t derlen)997 shishi_der2asn1_padata (Shishi * handle, const char *der, size_t derlen)
998 {
999   return der2asn1 (handle, ASN1NAME "PA-DATA", "PA-DATA", der, derlen);
1000 }
1001 
1002 /**
1003  * shishi_der2asn1_methoddata:
1004  * @handle: shishi handle as allocated by shishi_init().
1005  * @der: input character array with DER encoding.
1006  * @derlen: length of input character array with DER encoding.
1007  *
1008  * Decode DER encoding of METHOD-DATA and create a ASN.1 structure.
1009  *
1010  * Return value: Returns ASN.1 structure corresponding to DER data.
1011  **/
1012 Shishi_asn1
shishi_der2asn1_methoddata(Shishi * handle,const char * der,size_t derlen)1013 shishi_der2asn1_methoddata (Shishi * handle, const char *der, size_t derlen)
1014 {
1015   return der2asn1 (handle, ASN1NAME "METHOD-DATA", "METHOD-DATA", der,
1016 		   derlen);
1017 }
1018 
1019 /**
1020  * shishi_der2asn1_etype_info:
1021  * @handle: shishi handle as allocated by shishi_init().
1022  * @der: input character array with DER encoding.
1023  * @derlen: length of input character array with DER encoding.
1024  *
1025  * Decode DER encoding of ETYPE-INFO and create a ASN.1 structure.
1026  *
1027  * Return value: Returns ASN.1 structure corresponding to DER data.
1028  **/
1029 Shishi_asn1
shishi_der2asn1_etype_info(Shishi * handle,const char * der,size_t derlen)1030 shishi_der2asn1_etype_info (Shishi * handle, const char *der, size_t derlen)
1031 {
1032   return der2asn1 (handle, ASN1NAME "ETYPE-INFO", "ETYPE-INFO", der, derlen);
1033 }
1034 
1035 /**
1036  * shishi_der2asn1_etype_info2:
1037  * @handle: shishi handle as allocated by shishi_init().
1038  * @der: input character array with DER encoding.
1039  * @derlen: length of input character array with DER encoding.
1040  *
1041  * Decode DER encoding of ETYPE-INFO2 and create a ASN.1 structure.
1042  *
1043  * Return value: Returns ASN.1 structure corresponding to DER data.
1044  **/
1045 Shishi_asn1
shishi_der2asn1_etype_info2(Shishi * handle,const char * der,size_t derlen)1046 shishi_der2asn1_etype_info2 (Shishi * handle, const char *der, size_t derlen)
1047 {
1048   return der2asn1 (handle, ASN1NAME "ETYPE-INFO2", "ETYPE-INFO2", der,
1049 		   derlen);
1050 }
1051 
1052 /**
1053  * shishi_der2asn1_ticket:
1054  * @handle: shishi handle as allocated by shishi_init().
1055  * @der: input character array with DER encoding.
1056  * @derlen: length of input character array with DER encoding.
1057  *
1058  * Decode DER encoding of Ticket and create a ASN.1 structure.
1059  *
1060  * Return value: Returns ASN.1 structure corresponding to DER data.
1061  **/
1062 Shishi_asn1
shishi_der2asn1_ticket(Shishi * handle,const char * der,size_t derlen)1063 shishi_der2asn1_ticket (Shishi * handle, const char *der, size_t derlen)
1064 {
1065   return der2asn1 (handle, ASN1NAME "Ticket", "Ticket", der, derlen);
1066 }
1067 
1068 /**
1069  * shishi_der2asn1_encticketpart:
1070  * @handle: shishi handle as allocated by shishi_init().
1071  * @der: input character array with DER encoding.
1072  * @derlen: length of input character array with DER encoding.
1073  *
1074  * Decode DER encoding of EncTicketPart and create a ASN.1 structure.
1075  *
1076  * Return value: Returns ASN.1 structure corresponding to DER data.
1077  **/
1078 Shishi_asn1
shishi_der2asn1_encticketpart(Shishi * handle,const char * der,size_t derlen)1079 shishi_der2asn1_encticketpart (Shishi * handle, const char *der,
1080 			       size_t derlen)
1081 {
1082   return der2asn1 (handle, ASN1NAME "EncTicketPart", "EncTicketPart",
1083 		   der, derlen);
1084 }
1085 
1086 /**
1087  * shishi_der2asn1_asreq:
1088  * @handle: shishi handle as allocated by shishi_init().
1089  * @der: input character array with DER encoding.
1090  * @derlen: length of input character array with DER encoding.
1091  *
1092  * Decode DER encoding of AS-REQ and create a ASN.1 structure.
1093  *
1094  * Return value: Returns ASN.1 structure corresponding to DER data.
1095  **/
1096 Shishi_asn1
shishi_der2asn1_asreq(Shishi * handle,const char * der,size_t derlen)1097 shishi_der2asn1_asreq (Shishi * handle, const char *der, size_t derlen)
1098 {
1099   return der2asn1 (handle, ASN1NAME "AS-REQ", "KDC-REQ", der, derlen);
1100 }
1101 
1102 /**
1103  * shishi_der2asn1_tgsreq:
1104  * @handle: shishi handle as allocated by shishi_init().
1105  * @der: input character array with DER encoding.
1106  * @derlen: length of input character array with DER encoding.
1107  *
1108  * Decode DER encoding of TGS-REQ and create a ASN.1 structure.
1109  *
1110  * Return value: Returns ASN.1 structure corresponding to DER data.
1111  **/
1112 Shishi_asn1
shishi_der2asn1_tgsreq(Shishi * handle,const char * der,size_t derlen)1113 shishi_der2asn1_tgsreq (Shishi * handle, const char *der, size_t derlen)
1114 {
1115   return der2asn1 (handle, ASN1NAME "TGS-REQ", "KDC-REQ", der, derlen);
1116 }
1117 
1118 /**
1119  * shishi_der2asn1_asrep:
1120  * @handle: shishi handle as allocated by shishi_init().
1121  * @der: input character array with DER encoding.
1122  * @derlen: length of input character array with DER encoding.
1123  *
1124  * Decode DER encoding of AS-REP and create a ASN.1 structure.
1125  *
1126  * Return value: Returns ASN.1 structure corresponding to DER data.
1127  **/
1128 Shishi_asn1
shishi_der2asn1_asrep(Shishi * handle,const char * der,size_t derlen)1129 shishi_der2asn1_asrep (Shishi * handle, const char *der, size_t derlen)
1130 {
1131   return der2asn1 (handle, ASN1NAME "AS-REP", "KDC-REP", der, derlen);
1132 }
1133 
1134 /**
1135  * shishi_der2asn1_tgsrep:
1136  * @handle: shishi handle as allocated by shishi_init().
1137  * @der: input character array with DER encoding.
1138  * @derlen: length of input character array with DER encoding.
1139  *
1140  * Decode DER encoding of TGS-REP and create a ASN.1 structure.
1141  *
1142  * Return value: Returns ASN.1 structure corresponding to DER data.
1143  **/
1144 Shishi_asn1
shishi_der2asn1_tgsrep(Shishi * handle,const char * der,size_t derlen)1145 shishi_der2asn1_tgsrep (Shishi * handle, const char *der, size_t derlen)
1146 {
1147   return der2asn1 (handle, ASN1NAME "TGS-REP", "KDC-REP", der, derlen);
1148 }
1149 
1150 /**
1151  * shishi_der2asn1_kdcrep:
1152  * @handle: shishi handle as allocated by shishi_init().
1153  * @der: input character array with DER encoding.
1154  * @derlen: length of input character array with DER encoding.
1155  *
1156  * Decode DER encoding of KDC-REP and create a ASN.1 structure.
1157  *
1158  * Return value: Returns ASN.1 structure corresponding to DER data.
1159  **/
1160 Shishi_asn1
shishi_der2asn1_kdcrep(Shishi * handle,const char * der,size_t derlen)1161 shishi_der2asn1_kdcrep (Shishi * handle, const char *der, size_t derlen)
1162 {
1163   return der2asn1 (handle, ASN1NAME "KDC-REP", "KDC-REP", der, derlen);
1164 }
1165 
1166 /**
1167  * shishi_der2asn1_encasreppart:
1168  * @handle: shishi handle as allocated by shishi_init().
1169  * @der: input character array with DER encoding.
1170  * @derlen: length of input character array with DER encoding.
1171  *
1172  * Decode DER encoding of EncASRepPart and create a ASN.1 structure.
1173  *
1174  * Return value: Returns ASN.1 structure corresponding to DER data.
1175  **/
1176 Shishi_asn1
shishi_der2asn1_encasreppart(Shishi * handle,const char * der,size_t derlen)1177 shishi_der2asn1_encasreppart (Shishi * handle, const char *der, size_t derlen)
1178 {
1179   return der2asn1 (handle, ASN1NAME "EncASRepPart", "EncKDCRepPart",
1180 		   der, derlen);
1181 }
1182 
1183 /**
1184  * shishi_der2asn1_enctgsreppart:
1185  * @handle: shishi handle as allocated by shishi_init().
1186  * @der: input character array with DER encoding.
1187  * @derlen: length of input character array with DER encoding.
1188  *
1189  * Decode DER encoding of EncTGSRepPart and create a ASN.1 structure.
1190  *
1191  * Return value: Returns ASN.1 structure corresponding to DER data.
1192  **/
1193 Shishi_asn1
shishi_der2asn1_enctgsreppart(Shishi * handle,const char * der,size_t derlen)1194 shishi_der2asn1_enctgsreppart (Shishi * handle, const char *der,
1195 			       size_t derlen)
1196 {
1197   return der2asn1 (handle, ASN1NAME "EncTGSRepPart", "EncKDCRepPart",
1198 		   der, derlen);
1199 }
1200 
1201 /**
1202  * shishi_der2asn1_enckdcreppart:
1203  * @handle: shishi handle as allocated by shishi_init().
1204  * @der: input character array with DER encoding.
1205  * @derlen: length of input character array with DER encoding.
1206  *
1207  * Decode DER encoding of EncKDCRepPart and create a ASN.1 structure.
1208  *
1209  * Return value: Returns ASN.1 structure corresponding to DER data.
1210  **/
1211 Shishi_asn1
shishi_der2asn1_enckdcreppart(Shishi * handle,const char * der,size_t derlen)1212 shishi_der2asn1_enckdcreppart (Shishi * handle, const char *der,
1213 			       size_t derlen)
1214 {
1215   return der2asn1 (handle, ASN1NAME "EncKDCRepPart", "EncKDCRepPart",
1216 		   der, derlen);
1217 }
1218 
1219 /**
1220  * shishi_der2asn1_authenticator:
1221  * @handle: shishi handle as allocated by shishi_init().
1222  * @der: input character array with DER encoding.
1223  * @derlen: length of input character array with DER encoding.
1224  *
1225  * Decode DER encoding of Authenticator and create a ASN.1 structure.
1226  *
1227  * Return value: Returns ASN.1 structure corresponding to DER data.
1228  **/
1229 Shishi_asn1
shishi_der2asn1_authenticator(Shishi * handle,const char * der,size_t derlen)1230 shishi_der2asn1_authenticator (Shishi * handle, const char *der,
1231 			       size_t derlen)
1232 {
1233   return der2asn1 (handle, ASN1NAME "Authenticator", "Authenticator",
1234 		   der, derlen);
1235 }
1236 
1237 /**
1238  * shishi_der2asn1_krberror:
1239  * @handle: shishi handle as allocated by shishi_init().
1240  * @der: input character array with DER encoding.
1241  * @derlen: length of input character array with DER encoding.
1242  *
1243  * Decode DER encoding of KRB-ERROR and create a ASN.1 structure.
1244  *
1245  * Return value: Returns ASN.1 structure corresponding to DER data.
1246  **/
1247 Shishi_asn1
shishi_der2asn1_krberror(Shishi * handle,const char * der,size_t derlen)1248 shishi_der2asn1_krberror (Shishi * handle, const char *der, size_t derlen)
1249 {
1250   return der2asn1 (handle, ASN1NAME "KRB-ERROR", "KRB-ERROR", der, derlen);
1251 }
1252 
1253 /**
1254  * shishi_der2asn1_krbsafe:
1255  * @handle: shishi handle as allocated by shishi_init().
1256  * @der: input character array with DER encoding.
1257  * @derlen: length of input character array with DER encoding.
1258  *
1259  * Decode DER encoding of KRB-SAFE and create a ASN.1 structure.
1260  *
1261  * Return value: Returns ASN.1 structure corresponding to DER data.
1262  **/
1263 Shishi_asn1
shishi_der2asn1_krbsafe(Shishi * handle,const char * der,size_t derlen)1264 shishi_der2asn1_krbsafe (Shishi * handle, const char *der, size_t derlen)
1265 {
1266   return der2asn1 (handle, ASN1NAME "KRB-SAFE", "KRB-SAFE", der, derlen);
1267 }
1268 
1269 /**
1270  * shishi_der2asn1_priv:
1271  * @handle: shishi handle as allocated by shishi_init().
1272  * @der: input character array with DER encoding.
1273  * @derlen: length of input character array with DER encoding.
1274  *
1275  * Decode DER encoding of KRB-PRIV and create a ASN.1 structure.
1276  *
1277  * Return value: Returns ASN.1 structure corresponding to DER data.
1278  **/
1279 Shishi_asn1
shishi_der2asn1_priv(Shishi * handle,const char * der,size_t derlen)1280 shishi_der2asn1_priv (Shishi * handle, const char *der, size_t derlen)
1281 {
1282   return der2asn1 (handle, ASN1NAME "KRB-PRIV", "KRB-PRIV", der, derlen);
1283 }
1284 
1285 /**
1286  * shishi_der2asn1_encprivpart:
1287  * @handle: shishi handle as allocated by shishi_init().
1288  * @der: input character array with DER encoding.
1289  * @derlen: length of input character array with DER encoding.
1290  *
1291  * Decode DER encoding of EncKrbPrivPart and create a ASN.1 structure.
1292  *
1293  * Return value: Returns ASN.1 structure corresponding to DER data.
1294  **/
1295 Shishi_asn1
shishi_der2asn1_encprivpart(Shishi * handle,const char * der,size_t derlen)1296 shishi_der2asn1_encprivpart (Shishi * handle, const char *der, size_t derlen)
1297 {
1298   return der2asn1 (handle, ASN1NAME "EncKrbPrivPart", "EncKrbPrivPart",
1299 		   der, derlen);
1300 }
1301 
1302 /**
1303  * shishi_der2asn1_apreq:
1304  * @handle: shishi handle as allocated by shishi_init().
1305  * @der: input character array with DER encoding.
1306  * @derlen: length of input character array with DER encoding.
1307  *
1308  * Decode DER encoding of AP-REQ and create a ASN.1 structure.
1309  *
1310  * Return value: Returns ASN.1 structure corresponding to DER data.
1311  **/
1312 Shishi_asn1
shishi_der2asn1_apreq(Shishi * handle,const char * der,size_t derlen)1313 shishi_der2asn1_apreq (Shishi * handle, const char *der, size_t derlen)
1314 {
1315   return der2asn1 (handle, ASN1NAME "AP-REQ", "AP-REQ", der, derlen);
1316 }
1317 
1318 /**
1319  * shishi_der2asn1_aprep:
1320  * @handle: shishi handle as allocated by shishi_init().
1321  * @der: input character array with DER encoding.
1322  * @derlen: length of input character array with DER encoding.
1323  *
1324  * Decode DER encoding of AP-REP and create a ASN.1 structure.
1325  *
1326  * Return value: Returns ASN.1 structure corresponding to DER data.
1327  **/
1328 Shishi_asn1
shishi_der2asn1_aprep(Shishi * handle,const char * der,size_t derlen)1329 shishi_der2asn1_aprep (Shishi * handle, const char *der, size_t derlen)
1330 {
1331   return der2asn1 (handle, ASN1NAME "AP-REP", "AP-REP", der, derlen);
1332 }
1333 
1334 /**
1335  * shishi_der2asn1_encapreppart:
1336  * @handle: shishi handle as allocated by shishi_init().
1337  * @der: input character array with DER encoding.
1338  * @derlen: length of input character array with DER encoding.
1339  *
1340  * Decode DER encoding of EncAPRepPart and create a ASN.1 structure.
1341  *
1342  * Return value: Returns ASN.1 structure corresponding to DER data.
1343  **/
1344 Shishi_asn1
shishi_der2asn1_encapreppart(Shishi * handle,const char * der,size_t derlen)1345 shishi_der2asn1_encapreppart (Shishi * handle, const char *der, size_t derlen)
1346 {
1347   return der2asn1 (handle, ASN1NAME "EncAPRepPart", "EncAPRepPart",
1348 		   der, derlen);
1349 }
1350 
1351 /**
1352  * shishi_der2asn1_kdcreq:
1353  * @handle: shishi handle as allocated by shishi_init().
1354  * @der: input character array with DER encoding.
1355  * @derlen: length of input character array with DER encoding.
1356  *
1357  * Decode DER encoding of AS-REQ, TGS-REQ or KDC-REQ and create a
1358  * ASN.1 structure.
1359  *
1360  * Return value: Returns ASN.1 structure corresponding to DER data.
1361  **/
1362 Shishi_asn1
shishi_der2asn1_kdcreq(Shishi * handle,const char * der,size_t derlen)1363 shishi_der2asn1_kdcreq (Shishi * handle, const char *der, size_t derlen)
1364 {
1365   Shishi_asn1 structure = NULL;
1366 
1367   structure = shishi_der2asn1_asreq (handle, der, derlen);
1368   if (structure == NULL)
1369     {
1370       printf ("der2asn1_kdcreq: not asreq\n");
1371       shishi_error_printf (handle, "Could not DER decode AS-REQ\n");
1372 
1373       structure = shishi_der2asn1_tgsreq (handle, der, derlen);
1374       if (structure == NULL)
1375 	{
1376 	  printf ("der2asn1_kdcreq: not tgsreq\n");
1377 	  shishi_error_printf (handle, "Could not DER decode TGS-REQ\n");
1378 
1379 	  structure = shishi_der2asn1_kdcreq (handle, der, derlen);
1380 	  if (structure == NULL)
1381 	    {
1382 	      printf ("der2asn1_kdcreq: not kdcreq\n");
1383 	      shishi_error_printf (handle, "Could not DER decode KDC-REQ\n");
1384 
1385 	      return NULL;
1386 	    }
1387 	  else
1388 	    printf ("der2asn1_kdcreq: kdcreq!!\n");
1389 	}
1390     }
1391 
1392   return structure;
1393 }
1394 
1395 /**
1396  * shishi_asn1_print:
1397  * @handle: shishi handle as allocated by shishi_init().
1398  * @node: ASN.1 data that have field to extract.
1399  * @fh: file descriptor to print to, e.g. stdout.
1400  *
1401  * Print ASN.1 structure in human readable form, typically for
1402  * debugging purposes.
1403  **/
1404 void
shishi_asn1_print(Shishi * handle,Shishi_asn1 node,FILE * fh)1405 shishi_asn1_print (Shishi * handle, Shishi_asn1 node, FILE * fh)
1406 {
1407   asn1_print_structure (fh, node, "", ASN1_PRINT_NAME_TYPE_VALUE);
1408 }
1409