17c478bd9Sstevel@tonic-gate /* Coding Buffer Specifications */
27c478bd9Sstevel@tonic-gate #ifndef __ASN1BUF_H__
37c478bd9Sstevel@tonic-gate #define __ASN1BUF_H__
47c478bd9Sstevel@tonic-gate 
57c478bd9Sstevel@tonic-gate #include "k5-int.h"
67c478bd9Sstevel@tonic-gate #include "krbasn1.h"
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate typedef struct code_buffer_rep {
97c478bd9Sstevel@tonic-gate   char *base, *bound, *next;
107c478bd9Sstevel@tonic-gate } asn1buf;
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate /**************** Private Procedures ****************/
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate int asn1buf_size
167c478bd9Sstevel@tonic-gate 	(const asn1buf *buf);
177c478bd9Sstevel@tonic-gate /* requires  *buf has been created and not destroyed
181da57d55SToomas Soome    effects   Returns the total size
197c478bd9Sstevel@tonic-gate 	(in octets) of buf's octet buffer. */
207c478bd9Sstevel@tonic-gate #define asn1buf_size(buf) \
217c478bd9Sstevel@tonic-gate   (((buf) == NULL || (buf)->base == NULL) \
227c478bd9Sstevel@tonic-gate    ? 0 \
237c478bd9Sstevel@tonic-gate    : ((buf)->bound - (buf)->base + 1))
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate int asn1buf_free
267c478bd9Sstevel@tonic-gate 	(const asn1buf *buf);
277c478bd9Sstevel@tonic-gate /* requires  *buf is allocated
287c478bd9Sstevel@tonic-gate    effects   Returns the number of unused, allocated octets in *buf. */
297c478bd9Sstevel@tonic-gate #define asn1buf_free(buf) \
307c478bd9Sstevel@tonic-gate   (((buf) == NULL || (buf)->base == NULL) \
317c478bd9Sstevel@tonic-gate    ? 0 \
327c478bd9Sstevel@tonic-gate    : ((buf)->bound - (buf)->next + 1))
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate asn1_error_code asn1buf_ensure_space
367c478bd9Sstevel@tonic-gate 	(asn1buf *buf, const unsigned int amount);
377c478bd9Sstevel@tonic-gate /* requires  *buf is allocated
387c478bd9Sstevel@tonic-gate    modifies  *buf
397c478bd9Sstevel@tonic-gate    effects  If buf has less than amount octets of free space, then it is
407c478bd9Sstevel@tonic-gate             expanded to have at least amount octets of free space.
417c478bd9Sstevel@tonic-gate             Returns ENOMEM memory is exhausted. */
427c478bd9Sstevel@tonic-gate #define asn1buf_ensure_space(buf,amount) \
437c478bd9Sstevel@tonic-gate   ((asn1buf_free(buf) < (amount)) \
447c478bd9Sstevel@tonic-gate    ? (asn1buf_expand((buf), (amount)-asn1buf_free(buf))) \
457c478bd9Sstevel@tonic-gate    : 0)
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate asn1_error_code asn1buf_expand
497c478bd9Sstevel@tonic-gate 	(asn1buf *buf, unsigned int inc);
507c478bd9Sstevel@tonic-gate /* requires  *buf is allocated
517c478bd9Sstevel@tonic-gate    modifies  *buf
527c478bd9Sstevel@tonic-gate    effects   Expands *buf by allocating space for inc more octets.
537c478bd9Sstevel@tonic-gate              Returns ENOMEM if memory is exhausted. */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate int asn1buf_len
567c478bd9Sstevel@tonic-gate 	(const asn1buf *buf);
577c478bd9Sstevel@tonic-gate /* requires  *buf is allocated
587c478bd9Sstevel@tonic-gate    effects   Returns the length of the encoding in *buf. */
597c478bd9Sstevel@tonic-gate #define asn1buf_len(buf)	((buf)->next - (buf)->base)
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /****** End of private procedures *****/
621da57d55SToomas Soome 
637c478bd9Sstevel@tonic-gate /*
641da57d55SToomas Soome   Overview
651da57d55SToomas Soome 
667c478bd9Sstevel@tonic-gate     The coding buffer is an array of char (to match a krb5_data structure)
677c478bd9Sstevel@tonic-gate      with 3 reference pointers:
687c478bd9Sstevel@tonic-gate      1) base - The bottom of the octet array.  Used for memory management
697c478bd9Sstevel@tonic-gate                operations on the array (e.g. alloc, realloc, free).
707c478bd9Sstevel@tonic-gate      2) next - Points to the next available octet position in the array.
717c478bd9Sstevel@tonic-gate                During encoding, this is the next free position, and it
727c478bd9Sstevel@tonic-gate                  advances as octets are added to the array.
737c478bd9Sstevel@tonic-gate 	       During decoding, this is the next unread position, and it
747c478bd9Sstevel@tonic-gate                  advances as octets are read from the array.
757c478bd9Sstevel@tonic-gate      3) bound - Points to the top of the array. Used for bounds-checking.
761da57d55SToomas Soome 
777c478bd9Sstevel@tonic-gate     All pointers to encoding buffers should be initalized to NULL.
781da57d55SToomas Soome 
797c478bd9Sstevel@tonic-gate   Operations
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate     asn1buf_create
827c478bd9Sstevel@tonic-gate     asn1buf_wrap_data
837c478bd9Sstevel@tonic-gate     asn1buf_destroy
847c478bd9Sstevel@tonic-gate     asn1buf_insert_octet
857c478bd9Sstevel@tonic-gate     asn1buf_insert_charstring
867c478bd9Sstevel@tonic-gate     asn1buf_remove_octet
877c478bd9Sstevel@tonic-gate     asn1buf_remove_charstring
887c478bd9Sstevel@tonic-gate     asn1buf_unparse
897c478bd9Sstevel@tonic-gate     asn1buf_hex_unparse
907c478bd9Sstevel@tonic-gate     asn12krb5_buf
917c478bd9Sstevel@tonic-gate     asn1buf_remains
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate     (asn1buf_size)
947c478bd9Sstevel@tonic-gate     (asn1buf_free)
957c478bd9Sstevel@tonic-gate     (asn1buf_ensure_space)
967c478bd9Sstevel@tonic-gate     (asn1buf_expand)
977c478bd9Sstevel@tonic-gate     (asn1buf_len)
987c478bd9Sstevel@tonic-gate */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate asn1_error_code asn1buf_create
1017c478bd9Sstevel@tonic-gate 	(asn1buf **buf);
1027c478bd9Sstevel@tonic-gate /* effects   Creates a new encoding buffer pointed to by *buf.
1037c478bd9Sstevel@tonic-gate              Returns ENOMEM if the buffer can't be created. */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate asn1_error_code asn1buf_wrap_data
1067c478bd9Sstevel@tonic-gate 	(asn1buf *buf, const krb5_data *code);
1077c478bd9Sstevel@tonic-gate /* requires  *buf has already been allocated
1087c478bd9Sstevel@tonic-gate    effects   Turns *buf into a "wrapper" for *code.  i.e. *buf is set up
1097c478bd9Sstevel@tonic-gate               such that its bottom is the beginning of *code, and its top
1107c478bd9Sstevel@tonic-gate 	      is the top of *code.
1117c478bd9Sstevel@tonic-gate 	     Returns ASN1_MISSING_FIELD if code is empty. */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate asn1_error_code asn1buf_imbed
1141da57d55SToomas Soome 	(asn1buf *subbuf, const asn1buf *buf,
1157c478bd9Sstevel@tonic-gate 		   const unsigned int length,
1167c478bd9Sstevel@tonic-gate 		   const int indef);
1177c478bd9Sstevel@tonic-gate /* requires  *subbuf and *buf are allocated
1187c478bd9Sstevel@tonic-gate    effects   *subbuf becomes a sub-buffer of *buf.  *subbuf begins
1197c478bd9Sstevel@tonic-gate               at *buf's current position and is length octets long.
1207c478bd9Sstevel@tonic-gate               (Unless this would exceed the bounds of *buf -- in
1217c478bd9Sstevel@tonic-gate 	      that case, ASN1_OVERRUN is returned)  *subbuf's current
1227c478bd9Sstevel@tonic-gate 	      position starts at the beginning of *subbuf. */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate asn1_error_code asn1buf_sync
1251da57d55SToomas Soome 	(asn1buf *buf, asn1buf *subbuf, const asn1_class Class,
1267c478bd9Sstevel@tonic-gate 		   const asn1_tagnum lasttag,
1277c478bd9Sstevel@tonic-gate 		   const unsigned int length, const int indef,
1287c478bd9Sstevel@tonic-gate 		   const int seqindef);
1297c478bd9Sstevel@tonic-gate /* requires  *subbuf is a sub-buffer of *buf, as created by asn1buf_imbed.
1307c478bd9Sstevel@tonic-gate              lasttag is the last tagnumber read.
1317c478bd9Sstevel@tonic-gate    effects   Synchronizes *buf's current position to match that of *subbuf. */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate asn1_error_code asn1buf_skiptail
1347c478bd9Sstevel@tonic-gate 	(asn1buf *buf, const unsigned int length,
1357c478bd9Sstevel@tonic-gate 		   const int indef);
1367c478bd9Sstevel@tonic-gate /* requires  *buf is a subbuffer used in a decoding of a
1377c478bd9Sstevel@tonic-gate              constructed indefinite sequence.
1387c478bd9Sstevel@tonic-gate    effects   skips trailing fields. */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate asn1_error_code asn1buf_destroy
1417c478bd9Sstevel@tonic-gate 	(asn1buf **buf);
1427c478bd9Sstevel@tonic-gate /* effects   Deallocates **buf, sets *buf to NULL. */
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /* requires  *buf is allocated
1457c478bd9Sstevel@tonic-gate    effects   Inserts o into the buffer *buf, expanding the buffer if
1467c478bd9Sstevel@tonic-gate              necessary.  Returns ENOMEM memory is exhausted. */
1477c478bd9Sstevel@tonic-gate #if ((__GNUC__ >= 2) && !defined(ASN1BUF_OMIT_INLINE_FUNCS))
asn1buf_insert_octet(asn1buf * buf,const int o)148*4de637fbSToomas Soome static __inline__ asn1_error_code asn1buf_insert_octet(asn1buf *buf, const int o)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate   asn1_error_code retval;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate   retval = asn1buf_ensure_space(buf,1U);
1537c478bd9Sstevel@tonic-gate   if(retval) return retval;
1547c478bd9Sstevel@tonic-gate   *(buf->next) = (char)o;
1557c478bd9Sstevel@tonic-gate   (buf->next)++;
1567c478bd9Sstevel@tonic-gate   return 0;
1577c478bd9Sstevel@tonic-gate }
158*4de637fbSToomas Soome #else
159*4de637fbSToomas Soome asn1_error_code asn1buf_insert_octet
160*4de637fbSToomas Soome 	(asn1buf *buf, const int o);
1617c478bd9Sstevel@tonic-gate #endif
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate asn1_error_code asn1buf_insert_octetstring
1647c478bd9Sstevel@tonic-gate 	(asn1buf *buf, const unsigned int len, const asn1_octet *s);
1657c478bd9Sstevel@tonic-gate /* requires  *buf is allocated
1667c478bd9Sstevel@tonic-gate    modifies  *buf
1677c478bd9Sstevel@tonic-gate    effects   Inserts the contents of s (an octet array of length len)
1687c478bd9Sstevel@tonic-gate               into the buffer *buf, expanding the buffer if necessary.
1697c478bd9Sstevel@tonic-gate 	     Returns ENOMEM if memory is exhausted. */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate asn1_error_code asn1buf_insert_charstring
1727c478bd9Sstevel@tonic-gate 	(asn1buf *buf, const unsigned int len, const char *s);
1737c478bd9Sstevel@tonic-gate /* requires  *buf is allocated
1747c478bd9Sstevel@tonic-gate    modifies  *buf
1757c478bd9Sstevel@tonic-gate    effects   Inserts the contents of s (a character array of length len)
1767c478bd9Sstevel@tonic-gate               into the buffer *buf, expanding the buffer if necessary.
1777c478bd9Sstevel@tonic-gate 	     Returns ENOMEM if memory is exhausted. */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate asn1_error_code asn1buf_remove_octet
1807c478bd9Sstevel@tonic-gate 	(asn1buf *buf, asn1_octet *o);
1817c478bd9Sstevel@tonic-gate /* requires  *buf is allocated
1827c478bd9Sstevel@tonic-gate    effects   Returns *buf's current octet in *o and advances to
1837c478bd9Sstevel@tonic-gate               the next octet.
1847c478bd9Sstevel@tonic-gate 	     Returns ASN1_OVERRUN if *buf has already been exhausted. */
1857c478bd9Sstevel@tonic-gate #define asn1buf_remove_octet(buf,o) \
1867c478bd9Sstevel@tonic-gate   (((buf)->next > (buf)->bound) \
1877c478bd9Sstevel@tonic-gate    ? ASN1_OVERRUN \
1887c478bd9Sstevel@tonic-gate    : ((*(o) = (asn1_octet)(*(((buf)->next)++))),0))
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate asn1_error_code asn1buf_remove_octetstring
1917c478bd9Sstevel@tonic-gate 	(asn1buf *buf, const unsigned int len, asn1_octet **s);
1927c478bd9Sstevel@tonic-gate /* requires  *buf is allocated
1937c478bd9Sstevel@tonic-gate    effects   Removes the next len octets of *buf and returns them in **s.
1947c478bd9Sstevel@tonic-gate 	     Returns ASN1_OVERRUN if there are fewer than len unread octets
1957c478bd9Sstevel@tonic-gate 	      left in *buf.
1967c478bd9Sstevel@tonic-gate 	     Returns ENOMEM if *s could not be allocated. */
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate asn1_error_code asn1buf_remove_charstring
1997c478bd9Sstevel@tonic-gate 	(asn1buf *buf, const unsigned int len,
2007c478bd9Sstevel@tonic-gate 					  char **s);
2017c478bd9Sstevel@tonic-gate /* requires  *buf is allocated
2027c478bd9Sstevel@tonic-gate    effects   Removes the next len octets of *buf and returns them in **s.
2037c478bd9Sstevel@tonic-gate 	     Returns ASN1_OVERRUN if there are fewer than len unread octets
2047c478bd9Sstevel@tonic-gate 	      left in *buf.
2057c478bd9Sstevel@tonic-gate 	     Returns ENOMEM if *s could not be allocated. */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate asn1_error_code asn1buf_unparse
2087c478bd9Sstevel@tonic-gate 	(const asn1buf *buf, char **s);
2097c478bd9Sstevel@tonic-gate /* modifies  *s
2107c478bd9Sstevel@tonic-gate    effects   Returns a human-readable representation of *buf in *s,
2117c478bd9Sstevel@tonic-gate              where each octet in *buf is represented by a character in *s. */
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate asn1_error_code asn1buf_hex_unparse
2147c478bd9Sstevel@tonic-gate 	(const asn1buf *buf, char **s);
2157c478bd9Sstevel@tonic-gate /* modifies  *s
2167c478bd9Sstevel@tonic-gate    effects   Returns a human-readable representation of *buf in *s,
2177c478bd9Sstevel@tonic-gate              where each octet in *buf is represented by a 2-digit
2187c478bd9Sstevel@tonic-gate 	     hexadecimal number in *s. */
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate asn1_error_code asn12krb5_buf
2217c478bd9Sstevel@tonic-gate 	(const asn1buf *buf, krb5_data **code);
2227c478bd9Sstevel@tonic-gate /* modifies  *code
2237c478bd9Sstevel@tonic-gate    effects   Instantiates **code with the krb5_data representation of **buf. */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate int asn1buf_remains
2277c478bd9Sstevel@tonic-gate 	(asn1buf *buf, int indef);
2287c478bd9Sstevel@tonic-gate /* requires  *buf is a buffer containing an asn.1 structure or array
2297c478bd9Sstevel@tonic-gate    modifies  *buf
2307c478bd9Sstevel@tonic-gate    effects   Returns the number of unprocessed octets remaining in *buf. */
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate #endif
233