Update gnulib files.
[shishi.git] / lib / asn1.c
blobd7b77a3c1bfd522a1a7336775b6a72f76ecc7472
1 /* asn1.c --- Utilities to manipulate RFC 1510 ASN.1 types.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Simon Josefsson
4 * This file is part of Shishi.
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.
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.
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
23 #include <libtasn1.h>
24 #define _SHISHI_HAS_LIBTASN1_H 1
25 #include "internal.h"
26 #include "asn1.h"
28 #define ASN1NAME "KerberosV5Spec2."
30 /* Generated by asn1Parser from ASN.1 module. */
31 extern const ASN1_ARRAY_TYPE shishi_asn1_tab[];
33 /* Prototype in asn1.h, used by init.c. */
34 int
35 _shishi_asn1_init (Shishi * handle)
37 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE] = "";
38 int asn1_result;
40 if (!asn1_check_version (LIBTASN1_VERSION))
42 shishi_warn (handle, "asn1_check-version(%s) failed: %s",
43 LIBTASN1_VERSION, asn1_check_version (NULL));
44 return SHISHI_ASN1_ERROR;
47 if (!asn1_check_version ("0.2.5"))
48 shishi_warn (handle, "libtasn1 >= 0.2.5 preferred, you may see bugs.");
50 asn1_result = asn1_array2tree (shishi_asn1_tab,
51 &handle->asn1, errorDescription);
52 if (asn1_result != ASN1_SUCCESS)
54 shishi_warn (handle, "asn1_array2tree() failed: %s\n",
55 libtasn1_strerror (asn1_result));
56 shishi_warn (handle, "%s", errorDescription);
57 return SHISHI_ASN1_ERROR;
60 return SHISHI_OK;
63 int
64 shishi_asn1_number_of_elements (Shishi * handle, Shishi_asn1 node,
65 const char *field, size_t * n)
67 int rc;
68 int tmp;
70 rc = asn1_number_of_elements (node, field, &tmp);
71 *n = tmp;
72 if (rc != ASN1_SUCCESS)
74 if (rc == ASN1_ELEMENT_NOT_FOUND)
75 return SHISHI_ASN1_NO_ELEMENT;
76 else
77 return SHISHI_ASN1_ERROR;
80 return SHISHI_OK;
83 int
84 shishi_asn1_empty_p (Shishi * handle, Shishi_asn1 node, const char *field)
86 int rc;
87 int datalen;
89 datalen = 0;
90 rc = asn1_read_value (node, field, NULL, &datalen);
91 if (rc == ASN1_VALUE_NOT_FOUND)
92 return 1;
94 return 0;
97 /**
98 * shishi_asn1_read_inline:
99 * @handle: shishi handle as allocated by shishi_init().
100 * @node: ASN.1 variable to read field from.
101 * @field: name of field in @node to read.
102 * @data: pre-allocated output buffer that will hold ASN.1 field data.
103 * @datalen: on input, maximum size of output buffer,
104 * on output, actual size of output buffer.
106 * Extract data stored in a ASN.1 field into a fixed size buffer
107 * allocated by caller.
109 * Note that since it is difficult to predict the length of the field,
110 * it is often better to use shishi_asn1_read() instead.
112 * Return value: Returns SHISHI_OK if successful,
113 * SHISHI_ASN1_NO_ELEMENT if the element do not exist,
114 * SHISHI_ASN1_NO_VALUE if the field has no value, ot
115 * SHISHI_ASN1_ERROR otherwise.
118 shishi_asn1_read_inline (Shishi * handle, Shishi_asn1 node,
119 const char *field, char *data, size_t * datalen)
121 int rc;
123 rc = asn1_read_value (node, field, (unsigned char *) data, (int *) datalen);
124 if (rc != ASN1_SUCCESS)
126 shishi_error_set (handle, libtasn1_strerror (rc));
127 if (rc == ASN1_ELEMENT_NOT_FOUND)
128 return SHISHI_ASN1_NO_ELEMENT;
129 else if (rc == ASN1_VALUE_NOT_FOUND)
130 return SHISHI_ASN1_NO_VALUE;
131 else
132 return SHISHI_ASN1_ERROR;
135 return SHISHI_OK;
139 * shishi_asn1_read:
140 * @handle: shishi handle as allocated by shishi_init().
141 * @node: ASN.1 variable to read field from.
142 * @field: name of field in @node to read.
143 * @data: newly allocated output buffer that will hold ASN.1 field data.
144 * @datalen: actual size of output buffer.
146 * Extract data stored in a ASN.1 field into a newly allocated buffer.
147 * The buffer will always be zero terminated, even though @datalen
148 * will not include the added zero.
150 * Return value: Returns SHISHI_OK if successful,
151 * SHISHI_ASN1_NO_ELEMENT if the element do not exist,
152 * SHISHI_ASN1_NO_VALUE if the field has no value, ot
153 * SHISHI_ASN1_ERROR otherwise.
156 shishi_asn1_read (Shishi * handle,
157 Shishi_asn1 node, const char *field,
158 char **data, size_t * datalen)
160 int rc;
161 int len = 0;
163 rc = asn1_read_value (node, field, NULL, &len);
164 if (rc != ASN1_SUCCESS && rc != ASN1_MEM_ERROR)
166 shishi_error_set (handle, libtasn1_strerror (rc));
167 if (rc == ASN1_ELEMENT_NOT_FOUND)
168 return SHISHI_ASN1_NO_ELEMENT;
169 else if (rc == ASN1_VALUE_NOT_FOUND)
170 return SHISHI_ASN1_NO_VALUE;
171 else
172 return SHISHI_ASN1_ERROR;
175 if (data)
177 size_t dlen = (size_t) len;
179 *data = xmalloc (len + 1);
181 if (len > 0)
183 rc = shishi_asn1_read_inline (handle, node, field, *data, &dlen);
184 if (rc != SHISHI_OK)
185 return rc;
188 (*data)[len] = '\0';
191 if (datalen)
192 *datalen = (size_t) len;
194 return SHISHI_OK;
198 * shishi_asn1_read_optional:
199 * @handle: shishi handle as allocated by shishi_init().
200 * @node: ASN.1 variable to read field from.
201 * @field: name of field in @node to read.
202 * @data: newly allocated output buffer that will hold ASN.1 field data.
203 * @datalen: actual size of output buffer.
205 * Extract data stored in a ASN.1 field into a newly allocated buffer.
206 * If the field does not exist (i.e., SHISHI_ASN1_NO_ELEMENT), this
207 * function set datalen to 0 and succeeds. Can be useful to read
208 * ASN.1 fields which are marked OPTIONAL in the grammar, if you want
209 * to avoid special error handling in your code.
211 * Return value: Returns SHISHI_OK if successful,
212 * SHISHI_ASN1_NO_VALUE if the field has no value, ot
213 * SHISHI_ASN1_ERROR otherwise.
216 shishi_asn1_read_optional (Shishi * handle,
217 Shishi_asn1 node, const char *field,
218 char **data, size_t * datalen)
220 int rc;
222 rc = shishi_asn1_read (handle, node, field, data, datalen);
223 if (rc != SHISHI_OK && rc != SHISHI_ASN1_NO_ELEMENT)
224 return rc;
226 if (rc == SHISHI_ASN1_NO_ELEMENT)
227 if (datalen)
228 *datalen = 0;
230 return SHISHI_OK;
233 #define C2I(buf) ((buf[3] & 0xFF) | \
234 ((buf[2] & 0xFF) << 8) | \
235 ((buf[1] & 0xFF) << 16) | \
236 ((buf[0] & 0xFF) << 24))
239 shishi_asn1_read_int32 (Shishi * handle, Shishi_asn1 node,
240 const char *field, int32_t * i)
242 char buf[4];
243 size_t buflen;
244 int rc;
246 memset (buf, 0, sizeof (buf));
247 buflen = sizeof (buf);
248 rc = shishi_asn1_read_inline (handle, node, field, buf, &buflen);
249 if (rc != SHISHI_OK)
250 return rc;
252 if (buflen < 4)
254 memset (buf, 0, sizeof (buf));
255 rc = shishi_asn1_read_inline (handle, node, field,
256 &buf[4 - buflen], &buflen);
257 if (rc != SHISHI_OK)
258 return rc;
260 *i = C2I (buf);
262 return SHISHI_OK;
266 shishi_asn1_read_uint32 (Shishi * handle, Shishi_asn1 node,
267 const char *field, uint32_t * i)
269 return shishi_asn1_read_int32 (handle, node, field, (int32_t *) i);
273 shishi_asn1_read_integer (Shishi * handle, Shishi_asn1 node,
274 const char *field, int *i)
276 return shishi_asn1_read_int32 (handle, node, field, (int32_t *) i);
280 shishi_asn1_read_bitstring (Shishi * handle, Shishi_asn1 node,
281 const char *field, uint32_t * flags)
283 char *buf;
284 size_t buflen;
285 size_t i;
286 int res;
288 res = shishi_asn1_read (handle, node, field, &buf, &buflen);
289 if (res != SHISHI_OK)
290 return res;
292 if (buflen < 4)
293 return SHISHI_ASN1_ERROR;
295 *flags = 0;
296 for (i = 0; i < 4; i++)
298 *flags |= (((buf[i] >> 7) & 0x01) |
299 ((buf[i] >> 5) & 0x02) |
300 ((buf[i] >> 3) & 0x04) |
301 ((buf[i] >> 1) & 0x08) |
302 ((buf[i] << 1) & 0x10) |
303 ((buf[i] << 3) & 0x20) |
304 ((buf[i] << 5) & 0x40) | ((buf[i] << 7) & 0x80)) << (8 * i);
307 return SHISHI_OK;
311 shishi_asn1_write (Shishi * handle, Shishi_asn1 node,
312 const char *field, const char *data, size_t datalen)
314 int rc;
316 rc = asn1_write_value (node, field,
317 (const unsigned char *) data, (int) datalen);
318 if (rc != ASN1_SUCCESS)
320 shishi_error_set (handle, libtasn1_strerror (rc));
321 return SHISHI_ASN1_ERROR;
324 return SHISHI_OK;
328 shishi_asn1_write_uint32 (Shishi * handle, Shishi_asn1 node,
329 const char *field, uint32_t n)
331 char *buf;
332 int res;
334 asprintf (&buf, "%lu", n);
335 res = shishi_asn1_write (handle, node, field, buf, 0);
336 free (buf);
337 if (res != SHISHI_OK)
338 return res;
340 return SHISHI_OK;
344 shishi_asn1_write_int32 (Shishi * handle, Shishi_asn1 node,
345 const char *field, int32_t n)
347 char *buf;
348 int res;
350 asprintf (&buf, "%ld", n);
351 res = shishi_asn1_write (handle, node, field, buf, 0);
352 free (buf);
353 if (res != SHISHI_OK)
354 return res;
356 return SHISHI_OK;
360 shishi_asn1_write_integer (Shishi * handle, Shishi_asn1 node,
361 const char *field, int n)
363 return shishi_asn1_write_int32 (handle, node, field, (int32_t) n);
367 shishi_asn1_write_bitstring (Shishi * handle, Shishi_asn1 node,
368 const char *field, uint32_t flags)
370 char buf[4];
371 size_t i;
372 int res;
374 /* XXX
375 Cannot handle bit strings longer than 32 bits.
376 Currently not needed though. */
378 for (i = 0; i < 4; i++)
380 buf[i] = ((((flags >> (8 * i)) >> 7) & 0x01) |
381 (((flags >> (8 * i)) >> 5) & 0x02) |
382 (((flags >> (8 * i)) >> 3) & 0x04) |
383 (((flags >> (8 * i)) >> 1) & 0x08) |
384 (((flags >> (8 * i)) << 1) & 0x10) |
385 (((flags >> (8 * i)) << 3) & 0x20) |
386 (((flags >> (8 * i)) << 5) & 0x40) |
387 (((flags >> (8 * i)) << 7) & 0x80));
390 res = shishi_asn1_write (handle, node, field, buf, 32);
391 if (res != SHISHI_OK)
392 return res;
394 return SHISHI_OK;
398 * shishi_asn1_done:
399 * @handle: shishi handle as allocated by shishi_init().
400 * @node: ASN.1 node to dellocate.
402 * Deallocate resources associated with ASN.1 structure. Note that
403 * the node must not be used after this call.
405 void
406 shishi_asn1_done (Shishi * handle, Shishi_asn1 node)
409 int rc;
411 if (node)
413 rc = asn1_delete_structure (&node);
414 if (rc != ASN1_SUCCESS)
415 shishi_error_printf (handle, "Cannot dellocate ASN.1 structure: %s",
416 libtasn1_strerror (rc));
420 static Shishi_asn1
421 asn1_new (Shishi * handle, const char *field, const char *name)
423 ASN1_TYPE node = ASN1_TYPE_EMPTY;
424 int res;
426 res = asn1_create_element (handle->asn1, field, &node);
427 if (res != ASN1_SUCCESS)
429 shishi_error_set (handle, libtasn1_strerror (res));
430 return NULL;
433 return (Shishi_asn1) node;
437 * shishi_asn1_pa_enc_ts_enc:
438 * @handle: shishi handle as allocated by shishi_init().
440 * Create new ASN.1 structure for PA-ENC-TS-ENC.
442 * Return value: Returns ASN.1 structure.
444 Shishi_asn1
445 shishi_asn1_pa_enc_ts_enc (Shishi * handle)
447 return asn1_new (handle, ASN1NAME "PA-ENC-TS-ENC", "PA-ENC-TS-ENC");
451 * shishi_asn1_encrypteddata:
452 * @handle: shishi handle as allocated by shishi_init().
454 * Create new ASN.1 structure for EncryptedData
456 * Return value: Returns ASN.1 structure.
458 Shishi_asn1
459 shishi_asn1_encrypteddata (Shishi * handle)
461 return asn1_new (handle, ASN1NAME "EncryptedData", "EncryptedData");
465 * shishi_asn1_padata:
466 * @handle: shishi handle as allocated by shishi_init().
468 * Create new ASN.1 structure for PA-DATA.
470 * Return value: Returns ASN.1 structure.
472 Shishi_asn1
473 shishi_asn1_padata (Shishi * handle)
475 return asn1_new (handle, ASN1NAME "PA-DATA", "PA-DATA");
479 * shishi_asn1_methoddata:
480 * @handle: shishi handle as allocated by shishi_init().
482 * Create new ASN.1 structure for METHOD-DATA.
484 * Return value: Returns ASN.1 structure.
486 Shishi_asn1
487 shishi_asn1_methoddata (Shishi * handle)
489 return asn1_new (handle, ASN1NAME "METHOD-DATA", "METHOD-DATA");
493 * shishi_asn1_etype_info:
494 * @handle: shishi handle as allocated by shishi_init().
496 * Create new ASN.1 structure for ETYPE-INFO.
498 * Return value: Returns ASN.1 structure.
500 Shishi_asn1
501 shishi_asn1_etype_info (Shishi * handle)
503 return asn1_new (handle, ASN1NAME "ETYPE-INFO", "ETYPE-INFO");
507 * shishi_asn1_etype_info2:
508 * @handle: shishi handle as allocated by shishi_init().
510 * Create new ASN.1 structure for ETYPE-INFO2.
512 * Return value: Returns ASN.1 structure.
514 Shishi_asn1
515 shishi_asn1_etype_info2 (Shishi * handle)
517 return asn1_new (handle, ASN1NAME "ETYPE-INFO2", "ETYPE-INFO2");
521 * shishi_asn1_asreq:
522 * @handle: shishi handle as allocated by shishi_init().
524 * Create new ASN.1 structure for AS-REQ.
526 * Return value: Returns ASN.1 structure.
528 Shishi_asn1
529 shishi_asn1_asreq (Shishi * handle)
531 return asn1_new (handle, ASN1NAME "AS-REQ", "KDC-REQ");
535 * shishi_asn1_asrep:
536 * @handle: shishi handle as allocated by shishi_init().
538 * Create new ASN.1 structure for AS-REP.
540 * Return value: Returns ASN.1 structure.
542 Shishi_asn1
543 shishi_asn1_asrep (Shishi * handle)
545 return asn1_new (handle, ASN1NAME "AS-REP", "KDC-REP");
549 * shishi_asn1_tgsreq:
550 * @handle: shishi handle as allocated by shishi_init().
552 * Create new ASN.1 structure for TGS-REQ.
554 * Return value: Returns ASN.1 structure.
556 Shishi_asn1
557 shishi_asn1_tgsreq (Shishi * handle)
559 return asn1_new (handle, ASN1NAME "TGS-REQ", "KDC-REQ");
563 * shishi_asn1_tgsrep:
564 * @handle: shishi handle as allocated by shishi_init().
566 * Create new ASN.1 structure for TGS-REP.
568 * Return value: Returns ASN.1 structure.
570 Shishi_asn1
571 shishi_asn1_tgsrep (Shishi * handle)
573 return asn1_new (handle, ASN1NAME "TGS-REP", "KDC-REP");
577 * shishi_asn1_apreq:
578 * @handle: shishi handle as allocated by shishi_init().
580 * Create new ASN.1 structure for AP-REQ.
582 * Return value: Returns ASN.1 structure.
584 Shishi_asn1
585 shishi_asn1_apreq (Shishi * handle)
587 return asn1_new (handle, ASN1NAME "AP-REQ", "AP-REQ");
591 * shishi_asn1_aprep:
592 * @handle: shishi handle as allocated by shishi_init().
594 * Create new ASN.1 structure for AP-REP.
596 * Return value: Returns ASN.1 structure.
598 Shishi_asn1
599 shishi_asn1_aprep (Shishi * handle)
601 return asn1_new (handle, ASN1NAME "AP-REP", "AP-REP");
605 * shishi_asn1_encapreppart:
606 * @handle: shishi handle as allocated by shishi_init().
608 * Create new ASN.1 structure for AP-REP.
610 * Return value: Returns ASN.1 structure.
612 Shishi_asn1
613 shishi_asn1_encapreppart (Shishi * handle)
615 return asn1_new (handle, ASN1NAME "EncAPRepPart", "EncAPRepPart");
619 * shishi_asn1_ticket:
620 * @handle: shishi handle as allocated by shishi_init().
622 * Create new ASN.1 structure for Ticket.
624 * Return value: Returns ASN.1 structure.
626 Shishi_asn1
627 shishi_asn1_ticket (Shishi * handle)
629 return asn1_new (handle, ASN1NAME "Ticket", "Ticket");
633 * shishi_asn1_encticketpart:
634 * @handle: shishi handle as allocated by shishi_init().
636 * Create new ASN.1 structure for EncTicketPart.
638 * Return value: Returns ASN.1 structure.
640 Shishi_asn1
641 shishi_asn1_encticketpart (Shishi * handle)
643 return asn1_new (handle, ASN1NAME "EncTicketPart", "EncTicketPart");
647 * shishi_asn1_authenticator:
648 * @handle: shishi handle as allocated by shishi_init().
650 * Create new ASN.1 structure for Authenticator.
652 * Return value: Returns ASN.1 structure.
654 Shishi_asn1
655 shishi_asn1_authenticator (Shishi * handle)
657 return asn1_new (handle, ASN1NAME "Authenticator", "Authenticator");
661 * shishi_asn1_enckdcreppart:
662 * @handle: shishi handle as allocated by shishi_init().
664 * Create new ASN.1 structure for EncKDCRepPart.
666 * Return value: Returns ASN.1 structure.
668 Shishi_asn1
669 shishi_asn1_enckdcreppart (Shishi * handle)
671 return asn1_new (handle, ASN1NAME "EncKDCRepPart", "EncKDCRepPart");
675 * shishi_asn1_encasreppart:
676 * @handle: shishi handle as allocated by shishi_init().
678 * Create new ASN.1 structure for EncASRepPart.
680 * Return value: Returns ASN.1 structure.
682 Shishi_asn1
683 shishi_asn1_encasreppart (Shishi * handle)
685 return asn1_new (handle, ASN1NAME "EncASRepPart", "EncKDCRepPart");
689 * shishi_asn1_krberror:
690 * @handle: shishi handle as allocated by shishi_init().
692 * Create new ASN.1 structure for KRB-ERROR.
694 * Return value: Returns ASN.1 structure.
696 Shishi_asn1
697 shishi_asn1_krberror (Shishi * handle)
699 return asn1_new (handle, ASN1NAME "KRB-ERROR", "KRB-ERROR");
703 * shishi_asn1_krbsafe:
704 * @handle: shishi handle as allocated by shishi_init().
706 * Create new ASN.1 structure for KRB-SAFE.
708 * Return value: Returns ASN.1 structure.
710 Shishi_asn1
711 shishi_asn1_krbsafe (Shishi * handle)
713 return asn1_new (handle, ASN1NAME "KRB-SAFE", "KRB-SAFE");
717 * shishi_asn1_priv:
718 * @handle: shishi handle as allocated by shishi_init().
720 * Create new ASN.1 structure for KRB-PRIV.
722 * Return value: Returns ASN.1 structure.
724 Shishi_asn1
725 shishi_asn1_priv (Shishi * handle)
727 return asn1_new (handle, ASN1NAME "KRB-PRIV", "KRB-PRIV");
731 * shishi_asn1_encprivpart:
732 * @handle: shishi handle as allocated by shishi_init().
734 * Create new ASN.1 structure for EncKrbPrivPart.
736 * Return value: Returns ASN.1 structure.
738 Shishi_asn1
739 shishi_asn1_encprivpart (Shishi * handle)
741 return asn1_new (handle, ASN1NAME "EncKrbPrivPart", "EncKrbPrivPart");
745 * shishi_asn1_to_der_field:
746 * @handle: shishi handle as allocated by shishi_init().
747 * @node: ASN.1 data that have field to extract.
748 * @field: name of field in @node to extract.
749 * @der: output array that holds DER encoding of @field in @node.
750 * @len: output variable with length of @der output array.
752 * Extract newly allocated DER representation of specified ASN.1 field.
754 * Return value: Returns SHISHI_OK if successful, or SHISHI_ASN1_ERROR
755 * if DER encoding fails (common reasons for this is that the ASN.1
756 * is missing required values).
759 shishi_asn1_to_der_field (Shishi * handle, Shishi_asn1 node,
760 const char *field, char **der, size_t * len)
762 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE] = "";
763 int mylen = 0;
764 int rc;
766 rc = asn1_der_coding (node, field, NULL, &mylen, errorDescription);
767 if (rc != ASN1_MEM_ERROR)
769 shishi_error_set (handle, errorDescription);
770 return SHISHI_ASN1_ERROR;
773 *der = xmalloc (mylen);
775 rc = asn1_der_coding (node, field, *der, &mylen, errorDescription);
776 if (rc != ASN1_SUCCESS)
778 shishi_error_set (handle, errorDescription);
779 return SHISHI_ASN1_ERROR;
782 if (strcmp (field, "req-body") == 0)
784 unsigned char class;
785 int derlen, derlen2;
786 unsigned long tag;
787 signed long lenlen;
789 /* XXX when encoding a field inside a SEQUENCE, libtasn1 appear
790 to include the tag from the SEQUENCE in the encoding of a
791 particular field. This appear wrong, so we frob it here.
792 This typically happens when encoding req-body in KDC-REQ for
793 TGS checksums. */
795 rc = asn1_get_tag_der (*der, mylen, &class, &derlen, &tag);
796 if (rc != ASN1_SUCCESS)
798 shishi_error_set (handle, errorDescription);
799 return SHISHI_ASN1_ERROR;
802 lenlen = asn1_get_length_der(*der + derlen, mylen - derlen, &derlen2);
803 if (lenlen < 0)
804 return SHISHI_ASN1_ERROR;
806 if (derlen + derlen2 < mylen)
808 mylen -= derlen + derlen2;
809 memmove (*der, *der + derlen + derlen2, mylen);
813 *len = mylen;
815 return SHISHI_OK;
819 * shishi_asn1_to_der:
820 * @handle: shishi handle as allocated by shishi_init().
821 * @node: ASN.1 data to convert to DER.
822 * @der: output array that holds DER encoding of @node.
823 * @len: output variable with length of @der output array.
825 * Extract newly allocated DER representation of specified ASN.1 data.
827 * Return value: Returns SHISHI_OK if successful, or SHISHI_ASN1_ERROR
828 * if DER encoding fails (common reasons for this is that the ASN.1
829 * is missing required values).
832 shishi_asn1_to_der (Shishi * handle, Shishi_asn1 node, char **der,
833 size_t * len)
835 return shishi_asn1_to_der_field (handle, node, "", der, len);
838 static Shishi_asn1
839 der2asn1 (Shishi * handle,
840 const char *fieldname,
841 const char *nodename, const char *der, size_t derlen)
843 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE] = "";
844 Shishi_asn1 structure = NULL;
845 int asn1_result = ASN1_SUCCESS;
847 asn1_result = asn1_create_element (handle->asn1, fieldname, &structure);
848 if (asn1_result != ASN1_SUCCESS)
850 shishi_error_set (handle, libtasn1_strerror (asn1_result));
851 return NULL;
854 asn1_result = asn1_der_decoding (&structure, (const unsigned char *) der,
855 (int) derlen, errorDescription);
856 if (asn1_result != ASN1_SUCCESS)
858 asn1_delete_structure (&structure);
859 shishi_error_set (handle, errorDescription);
860 return NULL;
863 return structure;
867 * shishi_asn1_msgtype:
868 * @handle: shishi handle as allocated by shishi_init().
869 * @node: ASN.1 type to get msg type for.
871 * Determine msg-type of ASN.1 type of a packet. Currently this uses
872 * the msg-type field instead of the APPLICATION tag, but this may be
873 * changed in the future.
875 * Return value: Returns msg-type of ASN.1 type, 0 on failure.
877 Shishi_msgtype
878 shishi_asn1_msgtype (Shishi * handle, Shishi_asn1 node)
880 asn1_retCode rc;
881 uint32_t msgtype;
883 /* XXX Use APPLICATION tag instead. */
884 rc = shishi_asn1_read_uint32 (handle, node, "msg-type", &msgtype);
885 if (rc != SHISHI_OK)
886 return 0;
888 return msgtype;
892 * shishi_der_msgtype:
893 * @handle: shishi handle as allocated by shishi_init().
894 * @der: input character array with DER encoding.
895 * @derlen: length of input character array with DER encoding.
897 * Determine msg-type of DER coded data of a packet.
899 * Return value: Returns msg-type of DER data, 0 on failure.
901 Shishi_msgtype
902 shishi_der_msgtype (Shishi * handle, const char *der, size_t derlen)
904 /* XXX Doesn't handle APPLICATION TAGS > 31. */
905 if (derlen > 1 && *der >= 0x60 && (unsigned char) *der <= 0x7F)
906 return *der - 0x60;
907 else
908 return 0;
912 * shishi_der2asn1:
913 * @handle: shishi handle as allocated by shishi_init().
914 * @der: input character array with DER encoding.
915 * @derlen: length of input character array with DER encoding.
917 * Convert arbitrary DER data of a packet to a ASN.1 type.
919 * Return value: Returns newly allocate ASN.1 corresponding to DER
920 * data, or %NULL on failure.
922 Shishi_asn1
923 shishi_der2asn1 (Shishi * handle, const char *der, size_t derlen)
925 Shishi_asn1 node;
927 switch (shishi_der_msgtype (handle, der, derlen))
929 case SHISHI_MSGTYPE_AS_REQ:
930 node = shishi_der2asn1_asreq (handle, der, derlen);
931 break;
933 case SHISHI_MSGTYPE_AS_REP:
934 node = shishi_der2asn1_asrep (handle, der, derlen);
935 break;
937 case SHISHI_MSGTYPE_TGS_REQ:
938 node = shishi_der2asn1_tgsreq (handle, der, derlen);
939 break;
941 case SHISHI_MSGTYPE_TGS_REP:
942 node = shishi_der2asn1_tgsrep (handle, der, derlen);
943 break;
945 case SHISHI_MSGTYPE_AP_REQ:
946 node = shishi_der2asn1_apreq (handle, der, derlen);
947 break;
949 case SHISHI_MSGTYPE_AP_REP:
950 node = shishi_der2asn1_aprep (handle, der, derlen);
951 break;
953 case SHISHI_MSGTYPE_SAFE:
954 node = shishi_der2asn1_krbsafe (handle, der, derlen);
955 break;
957 case SHISHI_MSGTYPE_PRIV:
958 node = shishi_der2asn1_priv (handle, der, derlen);
959 break;
961 case SHISHI_MSGTYPE_CRED:
962 /* node = shishi_der2asn1_cred (handle, der, derlen); */
963 break;
965 case SHISHI_MSGTYPE_ERROR:
966 node = shishi_der2asn1_krberror (handle, der, derlen);
967 break;
969 default:
970 node = NULL;
971 break;
974 return node;
978 * shishi_der2asn1_padata:
979 * @handle: shishi handle as allocated by shishi_init().
980 * @der: input character array with DER encoding.
981 * @derlen: length of input character array with DER encoding.
983 * Decode DER encoding of PA-DATA and create a ASN.1 structure.
985 * Return value: Returns ASN.1 structure corresponding to DER data.
987 Shishi_asn1
988 shishi_der2asn1_padata (Shishi * handle, const char *der, size_t derlen)
990 return der2asn1 (handle, ASN1NAME "PA-DATA", "PA-DATA", der, derlen);
994 * shishi_der2asn1_methoddata:
995 * @handle: shishi handle as allocated by shishi_init().
996 * @der: input character array with DER encoding.
997 * @derlen: length of input character array with DER encoding.
999 * Decode DER encoding of METHOD-DATA and create a ASN.1 structure.
1001 * Return value: Returns ASN.1 structure corresponding to DER data.
1003 Shishi_asn1
1004 shishi_der2asn1_methoddata (Shishi * handle, const char *der, size_t derlen)
1006 return der2asn1 (handle, ASN1NAME "METHOD-DATA", "METHOD-DATA", der, derlen);
1010 * shishi_der2asn1_etype_info:
1011 * @handle: shishi handle as allocated by shishi_init().
1012 * @der: input character array with DER encoding.
1013 * @derlen: length of input character array with DER encoding.
1015 * Decode DER encoding of ETYPE-INFO and create a ASN.1 structure.
1017 * Return value: Returns ASN.1 structure corresponding to DER data.
1019 Shishi_asn1
1020 shishi_der2asn1_etype_info (Shishi * handle, const char *der, size_t derlen)
1022 return der2asn1 (handle, ASN1NAME "ETYPE-INFO", "ETYPE-INFO", der, derlen);
1026 * shishi_der2asn1_etype_info2:
1027 * @handle: shishi handle as allocated by shishi_init().
1028 * @der: input character array with DER encoding.
1029 * @derlen: length of input character array with DER encoding.
1031 * Decode DER encoding of ETYPE-INFO2 and create a ASN.1 structure.
1033 * Return value: Returns ASN.1 structure corresponding to DER data.
1035 Shishi_asn1
1036 shishi_der2asn1_etype_info2 (Shishi * handle, const char *der, size_t derlen)
1038 return der2asn1 (handle, ASN1NAME "ETYPE-INFO2", "ETYPE-INFO2", der, derlen);
1042 * shishi_der2asn1_ticket:
1043 * @handle: shishi handle as allocated by shishi_init().
1044 * @der: input character array with DER encoding.
1045 * @derlen: length of input character array with DER encoding.
1047 * Decode DER encoding of Ticket and create a ASN.1 structure.
1049 * Return value: Returns ASN.1 structure corresponding to DER data.
1051 Shishi_asn1
1052 shishi_der2asn1_ticket (Shishi * handle, const char *der, size_t derlen)
1054 return der2asn1 (handle, ASN1NAME "Ticket", "Ticket", der, derlen);
1058 * shishi_der2asn1_encticketpart:
1059 * @handle: shishi handle as allocated by shishi_init().
1060 * @der: input character array with DER encoding.
1061 * @derlen: length of input character array with DER encoding.
1063 * Decode DER encoding of EncTicketPart and create a ASN.1 structure.
1065 * Return value: Returns ASN.1 structure corresponding to DER data.
1067 Shishi_asn1
1068 shishi_der2asn1_encticketpart (Shishi * handle, const char *der,
1069 size_t derlen)
1071 return der2asn1 (handle, ASN1NAME "EncTicketPart", "EncTicketPart",
1072 der, derlen);
1076 * shishi_der2asn1_asreq:
1077 * @handle: shishi handle as allocated by shishi_init().
1078 * @der: input character array with DER encoding.
1079 * @derlen: length of input character array with DER encoding.
1081 * Decode DER encoding of AS-REQ and create a ASN.1 structure.
1083 * Return value: Returns ASN.1 structure corresponding to DER data.
1085 Shishi_asn1
1086 shishi_der2asn1_asreq (Shishi * handle, const char *der, size_t derlen)
1088 return der2asn1 (handle, ASN1NAME "AS-REQ", "KDC-REQ", der, derlen);
1092 * shishi_der2asn1_tgsreq:
1093 * @handle: shishi handle as allocated by shishi_init().
1094 * @der: input character array with DER encoding.
1095 * @derlen: length of input character array with DER encoding.
1097 * Decode DER encoding of TGS-REQ and create a ASN.1 structure.
1099 * Return value: Returns ASN.1 structure corresponding to DER data.
1101 Shishi_asn1
1102 shishi_der2asn1_tgsreq (Shishi * handle, const char *der, size_t derlen)
1104 return der2asn1 (handle, ASN1NAME "TGS-REQ", "KDC-REQ", der, derlen);
1108 * shishi_der2asn1_asrep:
1109 * @handle: shishi handle as allocated by shishi_init().
1110 * @der: input character array with DER encoding.
1111 * @derlen: length of input character array with DER encoding.
1113 * Decode DER encoding of AS-REP and create a ASN.1 structure.
1115 * Return value: Returns ASN.1 structure corresponding to DER data.
1117 Shishi_asn1
1118 shishi_der2asn1_asrep (Shishi * handle, const char *der, size_t derlen)
1120 return der2asn1 (handle, ASN1NAME "AS-REP", "KDC-REP", der, derlen);
1124 * shishi_der2asn1_tgsrep:
1125 * @handle: shishi handle as allocated by shishi_init().
1126 * @der: input character array with DER encoding.
1127 * @derlen: length of input character array with DER encoding.
1129 * Decode DER encoding of TGS-REP and create a ASN.1 structure.
1131 * Return value: Returns ASN.1 structure corresponding to DER data.
1133 Shishi_asn1
1134 shishi_der2asn1_tgsrep (Shishi * handle, const char *der, size_t derlen)
1136 return der2asn1 (handle, ASN1NAME "TGS-REP", "KDC-REP", der, derlen);
1140 * shishi_der2asn1_kdcrep:
1141 * @handle: shishi handle as allocated by shishi_init().
1142 * @der: input character array with DER encoding.
1143 * @derlen: length of input character array with DER encoding.
1145 * Decode DER encoding of KDC-REP and create a ASN.1 structure.
1147 * Return value: Returns ASN.1 structure corresponding to DER data.
1149 Shishi_asn1
1150 shishi_der2asn1_kdcrep (Shishi * handle, const char *der, size_t derlen)
1152 return der2asn1 (handle, ASN1NAME "KDC-REP", "KDC-REP", der, derlen);
1156 * shishi_der2asn1_encasreppart:
1157 * @handle: shishi handle as allocated by shishi_init().
1158 * @der: input character array with DER encoding.
1159 * @derlen: length of input character array with DER encoding.
1161 * Decode DER encoding of EncASRepPart and create a ASN.1 structure.
1163 * Return value: Returns ASN.1 structure corresponding to DER data.
1165 Shishi_asn1
1166 shishi_der2asn1_encasreppart (Shishi * handle, const char *der, size_t derlen)
1168 return der2asn1 (handle, ASN1NAME "EncASRepPart", "EncKDCRepPart",
1169 der, derlen);
1173 * shishi_der2asn1_enctgsreppart:
1174 * @handle: shishi handle as allocated by shishi_init().
1175 * @der: input character array with DER encoding.
1176 * @derlen: length of input character array with DER encoding.
1178 * Decode DER encoding of EncTGSRepPart and create a ASN.1 structure.
1180 * Return value: Returns ASN.1 structure corresponding to DER data.
1182 Shishi_asn1
1183 shishi_der2asn1_enctgsreppart (Shishi * handle, const char *der,
1184 size_t derlen)
1186 return der2asn1 (handle, ASN1NAME "EncTGSRepPart", "EncKDCRepPart",
1187 der, derlen);
1191 * shishi_der2asn1_enckdcreppart:
1192 * @handle: shishi handle as allocated by shishi_init().
1193 * @der: input character array with DER encoding.
1194 * @derlen: length of input character array with DER encoding.
1196 * Decode DER encoding of EncKDCRepPart and create a ASN.1 structure.
1198 * Return value: Returns ASN.1 structure corresponding to DER data.
1200 Shishi_asn1
1201 shishi_der2asn1_enckdcreppart (Shishi * handle, const char *der,
1202 size_t derlen)
1204 return der2asn1 (handle, ASN1NAME "EncKDCRepPart", "EncKDCRepPart",
1205 der, derlen);
1209 * shishi_der2asn1_authenticator:
1210 * @handle: shishi handle as allocated by shishi_init().
1211 * @der: input character array with DER encoding.
1212 * @derlen: length of input character array with DER encoding.
1214 * Decode DER encoding of Authenticator and create a ASN.1 structure.
1216 * Return value: Returns ASN.1 structure corresponding to DER data.
1218 Shishi_asn1
1219 shishi_der2asn1_authenticator (Shishi * handle, const char *der,
1220 size_t derlen)
1222 return der2asn1 (handle, ASN1NAME "Authenticator", "Authenticator",
1223 der, derlen);
1227 * shishi_der2asn1_krberror:
1228 * @handle: shishi handle as allocated by shishi_init().
1229 * @der: input character array with DER encoding.
1230 * @derlen: length of input character array with DER encoding.
1232 * Decode DER encoding of KRB-ERROR and create a ASN.1 structure.
1234 * Return value: Returns ASN.1 structure corresponding to DER data.
1236 Shishi_asn1
1237 shishi_der2asn1_krberror (Shishi * handle, const char *der, size_t derlen)
1239 return der2asn1 (handle, ASN1NAME "KRB-ERROR", "KRB-ERROR", der, derlen);
1243 * shishi_der2asn1_krbsafe:
1244 * @handle: shishi handle as allocated by shishi_init().
1245 * @der: input character array with DER encoding.
1246 * @derlen: length of input character array with DER encoding.
1248 * Decode DER encoding of KRB-SAFE and create a ASN.1 structure.
1250 * Return value: Returns ASN.1 structure corresponding to DER data.
1252 Shishi_asn1
1253 shishi_der2asn1_krbsafe (Shishi * handle, const char *der, size_t derlen)
1255 return der2asn1 (handle, ASN1NAME "KRB-SAFE", "KRB-SAFE", der, derlen);
1259 * shishi_der2asn1_priv:
1260 * @handle: shishi handle as allocated by shishi_init().
1261 * @der: input character array with DER encoding.
1262 * @derlen: length of input character array with DER encoding.
1264 * Decode DER encoding of KRB-PRIV and create a ASN.1 structure.
1266 * Return value: Returns ASN.1 structure corresponding to DER data.
1268 Shishi_asn1
1269 shishi_der2asn1_priv (Shishi * handle, const char *der, size_t derlen)
1271 return der2asn1 (handle, ASN1NAME "KRB-PRIV", "KRB-PRIV", der, derlen);
1275 * shishi_der2asn1_encprivpart:
1276 * @handle: shishi handle as allocated by shishi_init().
1277 * @der: input character array with DER encoding.
1278 * @derlen: length of input character array with DER encoding.
1280 * Decode DER encoding of EncKrbPrivPart and create a ASN.1 structure.
1282 * Return value: Returns ASN.1 structure corresponding to DER data.
1284 Shishi_asn1
1285 shishi_der2asn1_encprivpart (Shishi * handle, const char *der, size_t derlen)
1287 return der2asn1 (handle, ASN1NAME "EncKrbPrivPart", "EncKrbPrivPart",
1288 der, derlen);
1292 * shishi_der2asn1_apreq:
1293 * @handle: shishi handle as allocated by shishi_init().
1294 * @der: input character array with DER encoding.
1295 * @derlen: length of input character array with DER encoding.
1297 * Decode DER encoding of AP-REQ and create a ASN.1 structure.
1299 * Return value: Returns ASN.1 structure corresponding to DER data.
1301 Shishi_asn1
1302 shishi_der2asn1_apreq (Shishi * handle, const char *der, size_t derlen)
1304 return der2asn1 (handle, ASN1NAME "AP-REQ", "AP-REQ", der, derlen);
1308 * shishi_der2asn1_aprep:
1309 * @handle: shishi handle as allocated by shishi_init().
1310 * @der: input character array with DER encoding.
1311 * @derlen: length of input character array with DER encoding.
1313 * Decode DER encoding of AP-REP and create a ASN.1 structure.
1315 * Return value: Returns ASN.1 structure corresponding to DER data.
1317 Shishi_asn1
1318 shishi_der2asn1_aprep (Shishi * handle, const char *der, size_t derlen)
1320 return der2asn1 (handle, ASN1NAME "AP-REP", "AP-REP", der, derlen);
1324 * shishi_der2asn1_encapreppart:
1325 * @handle: shishi handle as allocated by shishi_init().
1326 * @der: input character array with DER encoding.
1327 * @derlen: length of input character array with DER encoding.
1329 * Decode DER encoding of EncAPRepPart and create a ASN.1 structure.
1331 * Return value: Returns ASN.1 structure corresponding to DER data.
1333 Shishi_asn1
1334 shishi_der2asn1_encapreppart (Shishi * handle, const char *der, size_t derlen)
1336 return der2asn1 (handle, ASN1NAME "EncAPRepPart", "EncAPRepPart",
1337 der, derlen);
1341 * shishi_der2asn1_kdcreq:
1342 * @handle: shishi handle as allocated by shishi_init().
1343 * @der: input character array with DER encoding.
1344 * @derlen: length of input character array with DER encoding.
1346 * Decode DER encoding of AS-REQ, TGS-REQ or KDC-REQ and create a
1347 * ASN.1 structure.
1349 * Return value: Returns ASN.1 structure corresponding to DER data.
1351 Shishi_asn1
1352 shishi_der2asn1_kdcreq (Shishi * handle, const char *der, size_t derlen)
1354 Shishi_asn1 structure = NULL;
1356 structure = shishi_der2asn1_asreq (handle, der, derlen);
1357 if (structure == NULL)
1359 printf ("der2asn1_kdcreq: not asreq\n");
1360 shishi_error_printf (handle, "Could not DER decode AS-REQ\n");
1362 structure = shishi_der2asn1_tgsreq (handle, der, derlen);
1363 if (structure == NULL)
1365 printf ("der2asn1_kdcreq: not tgsreq\n");
1366 shishi_error_printf (handle, "Could not DER decode TGS-REQ\n");
1368 structure = shishi_der2asn1_kdcreq (handle, der, derlen);
1369 if (structure == NULL)
1371 printf ("der2asn1_kdcreq: not kdcreq\n");
1372 shishi_error_printf (handle, "Could not DER decode KDC-REQ\n");
1374 return NULL;
1376 else
1377 printf ("der2asn1_kdcreq: kdcreq!!\n");
1381 return structure;