1 /* 2 * Copyright (c) 1997 - 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 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 /* $Id: der.h,v 1.22 2001/09/27 16:20:35 assar Exp $ */ 35 36 #ifndef __DER_H__ 37 #define __DER_H__ 38 39 #include <time.h> 40 41 typedef enum {UNIV = 0, APPL = 1, CONTEXT = 2 , PRIVATE = 3} Der_class; 42 43 typedef enum {PRIM = 0, CONS = 1} Der_type; 44 45 /* Universal tags */ 46 47 enum { 48 UT_Boolean = 1, 49 UT_Integer = 2, 50 UT_BitString = 3, 51 UT_OctetString = 4, 52 UT_Null = 5, 53 UT_OID = 6, 54 UT_Enumerated = 10, 55 UT_Sequence = 16, 56 UT_Set = 17, 57 UT_PrintableString = 19, 58 UT_IA5String = 22, 59 UT_UTCTime = 23, 60 UT_GeneralizedTime = 24, 61 UT_VisibleString = 26, 62 UT_GeneralString = 27 63 }; 64 65 #define ASN1_INDEFINITE 0xdce0deed 66 67 #ifndef HAVE_TIMEGM 68 time_t timegm (struct tm *); 69 #endif 70 71 int time2generalizedtime (time_t t, octet_string *s); 72 73 int der_get_int (const unsigned char *p, size_t len, int *ret, size_t *size); 74 int der_get_length (const unsigned char *p, size_t len, 75 size_t *val, size_t *size); 76 int der_get_general_string (const unsigned char *p, size_t len, 77 general_string *str, size_t *size); 78 int der_get_octet_string (const unsigned char *p, size_t len, 79 octet_string *data, size_t *size); 80 int der_get_oid (const unsigned char *p, size_t len, 81 oid *data, size_t *size); 82 int der_get_tag (const unsigned char *p, size_t len, 83 Der_class *class, Der_type *type, 84 int *tag, size_t *size); 85 86 int der_match_tag (const unsigned char *p, size_t len, 87 Der_class class, Der_type type, 88 int tag, size_t *size); 89 int der_match_tag_and_length (const unsigned char *p, size_t len, 90 Der_class class, Der_type type, int tag, 91 size_t *length_ret, size_t *size); 92 93 int decode_integer (const unsigned char*, size_t, int*, size_t*); 94 int decode_unsigned (const unsigned char*, size_t, unsigned*, size_t*); 95 int decode_enumerated (const unsigned char*, size_t, unsigned*, size_t*); 96 int decode_general_string (const unsigned char*, size_t, 97 general_string*, size_t*); 98 int decode_oid (const unsigned char *p, size_t len, 99 oid *k, size_t *size); 100 int decode_octet_string (const unsigned char*, size_t, octet_string*, size_t*); 101 int decode_generalized_time (const unsigned char*, size_t, time_t*, size_t*); 102 103 int der_put_int (unsigned char *p, size_t len, int val, size_t*); 104 int der_put_length (unsigned char *p, size_t len, size_t val, size_t*); 105 int der_put_general_string (unsigned char *p, size_t len, 106 const general_string *str, size_t*); 107 int der_put_octet_string (unsigned char *p, size_t len, 108 const octet_string *data, size_t*); 109 int der_put_oid (unsigned char *p, size_t len, 110 const oid *data, size_t *size); 111 int der_put_tag (unsigned char *p, size_t len, Der_class class, Der_type type, 112 int tag, size_t*); 113 int der_put_length_and_tag (unsigned char*, size_t, size_t, 114 Der_class, Der_type, int, size_t*); 115 116 int encode_integer (unsigned char *p, size_t len, 117 const int *data, size_t*); 118 int encode_unsigned (unsigned char *p, size_t len, 119 const unsigned *data, size_t*); 120 int encode_enumerated (unsigned char *p, size_t len, 121 const unsigned *data, size_t*); 122 int encode_general_string (unsigned char *p, size_t len, 123 const general_string *data, size_t*); 124 int encode_octet_string (unsigned char *p, size_t len, 125 const octet_string *k, size_t*); 126 int encode_oid (unsigned char *p, size_t len, 127 const oid *k, size_t*); 128 int encode_generalized_time (unsigned char *p, size_t len, 129 const time_t *t, size_t*); 130 131 void free_integer (int *num); 132 void free_general_string (general_string *str); 133 void free_octet_string (octet_string *k); 134 void free_oid (oid *k); 135 void free_generalized_time (time_t *t); 136 137 size_t length_len (size_t len); 138 size_t length_integer (const int *data); 139 size_t length_unsigned (const unsigned *data); 140 size_t length_enumerated (const unsigned *data); 141 size_t length_general_string (const general_string *data); 142 size_t length_octet_string (const octet_string *k); 143 size_t length_oid (const oid *k); 144 size_t length_generalized_time (const time_t *t); 145 146 int copy_general_string (const general_string *from, general_string *to); 147 int copy_octet_string (const octet_string *from, octet_string *to); 148 int copy_oid (const oid *from, oid *to); 149 150 int fix_dce(size_t reallen, size_t *len); 151 152 #endif /* __DER_H__ */ 153