17c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27c478bd9Sstevel@tonic-gate 
37c478bd9Sstevel@tonic-gate /*
47c478bd9Sstevel@tonic-gate  * src/lib/krb5/asn.1/asn1_decode.c
57c478bd9Sstevel@tonic-gate  *
67c478bd9Sstevel@tonic-gate  * Copyright 1994, 2003 by the Massachusetts Institute of Technology.
77c478bd9Sstevel@tonic-gate  * All Rights Reserved.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
107c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
117c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
127c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
157c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
167c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
177c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
187c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
197c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
207c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
217c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
227c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
237c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
247c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
257c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
267c478bd9Sstevel@tonic-gate  * or implied warranty.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /* ASN.1 primitive decoders */
307c478bd9Sstevel@tonic-gate #include "asn1_decode.h"
317c478bd9Sstevel@tonic-gate #include "asn1_get.h"
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #ifdef HAVE_SYS_TIME_H
347c478bd9Sstevel@tonic-gate #include <sys/time.h>
357c478bd9Sstevel@tonic-gate #ifdef TIME_WITH_SYS_TIME
367c478bd9Sstevel@tonic-gate #include <time.h>
377c478bd9Sstevel@tonic-gate #endif
387c478bd9Sstevel@tonic-gate #else
397c478bd9Sstevel@tonic-gate #include <time.h>
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #define setup()\
437c478bd9Sstevel@tonic-gate asn1_error_code retval;\
447c478bd9Sstevel@tonic-gate taginfo tinfo
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define asn1class	(tinfo.asn1class)
477c478bd9Sstevel@tonic-gate #define construction	(tinfo.construction)
487c478bd9Sstevel@tonic-gate #define tagnum		(tinfo.tagnum)
497c478bd9Sstevel@tonic-gate #define length		(tinfo.length)
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define tag(type)\
527c478bd9Sstevel@tonic-gate retval = asn1_get_tag_2(buf,&tinfo);\
537c478bd9Sstevel@tonic-gate if(retval) return retval;\
547c478bd9Sstevel@tonic-gate if(asn1class != UNIVERSAL || construction != PRIMITIVE || tagnum != type)\
557c478bd9Sstevel@tonic-gate   return ASN1_BAD_ID
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define cleanup()\
587c478bd9Sstevel@tonic-gate return 0
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate time_t gmt_mktime (struct tm *);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate asn1_error_code asn1_decode_integer(asn1buf *buf, long int *val)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate   setup();
657c478bd9Sstevel@tonic-gate   asn1_octet o;
667c478bd9Sstevel@tonic-gate   long n = 0; /* initialize to keep gcc happy */
677c478bd9Sstevel@tonic-gate   int i;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate   tag(ASN1_INTEGER);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate   for (i = 0; i < length; i++) {
727c478bd9Sstevel@tonic-gate     retval = asn1buf_remove_octet(buf, &o);
737c478bd9Sstevel@tonic-gate     if (retval) return retval;
747c478bd9Sstevel@tonic-gate     if (!i) {
757c478bd9Sstevel@tonic-gate       n = (0x80 & o) ? -1 : 0;	/* grab sign bit */
767c478bd9Sstevel@tonic-gate       if (n < 0 && length > sizeof (long))
777c478bd9Sstevel@tonic-gate 	return ASN1_OVERFLOW;
787c478bd9Sstevel@tonic-gate       else if (length > sizeof (long) + 1) /* allow extra octet for positive */
797c478bd9Sstevel@tonic-gate 	return ASN1_OVERFLOW;
807c478bd9Sstevel@tonic-gate     }
817c478bd9Sstevel@tonic-gate     n = (n << 8) | o;
827c478bd9Sstevel@tonic-gate   }
837c478bd9Sstevel@tonic-gate   *val = n;
847c478bd9Sstevel@tonic-gate   cleanup();
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate asn1_error_code asn1_decode_unsigned_integer(asn1buf *buf, long unsigned int *val)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate   setup();
907c478bd9Sstevel@tonic-gate   asn1_octet o;
917c478bd9Sstevel@tonic-gate   unsigned long n;
927c478bd9Sstevel@tonic-gate   int i;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate   tag(ASN1_INTEGER);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate   for (i = 0, n = 0; i < length; i++) {
977c478bd9Sstevel@tonic-gate     retval = asn1buf_remove_octet(buf, &o);
987c478bd9Sstevel@tonic-gate     if(retval) return retval;
997c478bd9Sstevel@tonic-gate     if (!i) {
1007c478bd9Sstevel@tonic-gate       if (0x80 & o)
1017c478bd9Sstevel@tonic-gate 	return ASN1_OVERFLOW;
1027c478bd9Sstevel@tonic-gate       else if (length > sizeof (long) + 1)
1037c478bd9Sstevel@tonic-gate 	return ASN1_OVERFLOW;
1047c478bd9Sstevel@tonic-gate     }
1057c478bd9Sstevel@tonic-gate     n = (n << 8) | o;
1067c478bd9Sstevel@tonic-gate   }
1077c478bd9Sstevel@tonic-gate   *val = n;
1087c478bd9Sstevel@tonic-gate   cleanup();
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * asn1_decode_maybe_unsigned
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  * This is needed because older releases of MIT krb5 have signed
1157c478bd9Sstevel@tonic-gate  * sequence numbers.  We want to accept both signed and unsigned
1167c478bd9Sstevel@tonic-gate  * sequence numbers, in the range -2^31..2^32-1, mapping negative
1177c478bd9Sstevel@tonic-gate  * numbers into their positive equivalents in the same way that C's
1187c478bd9Sstevel@tonic-gate  * normal integer conversions do, i.e., would preserve bits on a
1197c478bd9Sstevel@tonic-gate  * two's-complement architecture.
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate asn1_error_code asn1_decode_maybe_unsigned(asn1buf *buf, unsigned long *val)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate   setup();
1247c478bd9Sstevel@tonic-gate   asn1_octet o;
1257c478bd9Sstevel@tonic-gate   unsigned long n, bitsremain;
1267c478bd9Sstevel@tonic-gate   unsigned int i;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate   tag(ASN1_INTEGER);
1297c478bd9Sstevel@tonic-gate   o = 0;
1307c478bd9Sstevel@tonic-gate   n = 0;
1317c478bd9Sstevel@tonic-gate   bitsremain = ~0UL;
1327c478bd9Sstevel@tonic-gate   for (i = 0; i < length; i++) {
1337c478bd9Sstevel@tonic-gate     /* Accounts for u_long width not being a multiple of 8. */
1347c478bd9Sstevel@tonic-gate     if (bitsremain < 0xff) return ASN1_OVERFLOW;
1357c478bd9Sstevel@tonic-gate     retval = asn1buf_remove_octet(buf, &o);
1367c478bd9Sstevel@tonic-gate     if (retval) return retval;
1377c478bd9Sstevel@tonic-gate     if (bitsremain == ~0UL) {
1387c478bd9Sstevel@tonic-gate       if (i == 0)
1397c478bd9Sstevel@tonic-gate 	n = (o & 0x80) ? ~0UL : 0UL; /* grab sign bit */
1407c478bd9Sstevel@tonic-gate       /*
1417c478bd9Sstevel@tonic-gate        * Skip leading zero or 0xFF octets to humor non-compliant encoders.
1427c478bd9Sstevel@tonic-gate        */
1437c478bd9Sstevel@tonic-gate       if (n == 0 && o == 0)
1447c478bd9Sstevel@tonic-gate 	continue;
1457c478bd9Sstevel@tonic-gate       if (n == ~0UL && o == 0xff)
1467c478bd9Sstevel@tonic-gate 	continue;
1477c478bd9Sstevel@tonic-gate     }
1487c478bd9Sstevel@tonic-gate     n = (n << 8) | o;
1497c478bd9Sstevel@tonic-gate     bitsremain >>= 8;
1507c478bd9Sstevel@tonic-gate   }
1517c478bd9Sstevel@tonic-gate   *val = n;
1527c478bd9Sstevel@tonic-gate   cleanup();
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate asn1_error_code asn1_decode_oid(asn1buf *buf, unsigned int *retlen, asn1_octet **val)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate   setup();
1587c478bd9Sstevel@tonic-gate   tag(ASN1_OBJECTIDENTIFIER);
1597c478bd9Sstevel@tonic-gate   retval = asn1buf_remove_octetstring(buf, length, val);
1607c478bd9Sstevel@tonic-gate   if (retval) return retval;
1617c478bd9Sstevel@tonic-gate   *retlen = length;
1627c478bd9Sstevel@tonic-gate   cleanup();
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate asn1_error_code asn1_decode_octetstring(asn1buf *buf, unsigned int *retlen, asn1_octet **val)
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate   setup();
1687c478bd9Sstevel@tonic-gate   tag(ASN1_OCTETSTRING);
1697c478bd9Sstevel@tonic-gate   retval = asn1buf_remove_octetstring(buf,length,val);
1707c478bd9Sstevel@tonic-gate   if(retval) return retval;
1717c478bd9Sstevel@tonic-gate   *retlen = length;
1727c478bd9Sstevel@tonic-gate   cleanup();
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate asn1_error_code asn1_decode_charstring(asn1buf *buf, unsigned int *retlen, char **val)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate   setup();
1787c478bd9Sstevel@tonic-gate   tag(ASN1_OCTETSTRING);
1797c478bd9Sstevel@tonic-gate   retval = asn1buf_remove_charstring(buf,length,val);
1807c478bd9Sstevel@tonic-gate   if(retval) return retval;
1817c478bd9Sstevel@tonic-gate   *retlen = length;
1827c478bd9Sstevel@tonic-gate   cleanup();
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate asn1_error_code asn1_decode_generalstring(asn1buf *buf, unsigned int *retlen, char **val)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate   setup();
1897c478bd9Sstevel@tonic-gate   tag(ASN1_GENERALSTRING);
1907c478bd9Sstevel@tonic-gate   retval = asn1buf_remove_charstring(buf,length,val);
1917c478bd9Sstevel@tonic-gate   if(retval) return retval;
1927c478bd9Sstevel@tonic-gate   *retlen = length;
1937c478bd9Sstevel@tonic-gate   cleanup();
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate asn1_error_code asn1_decode_null(asn1buf *buf)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate   setup();
2007c478bd9Sstevel@tonic-gate   tag(ASN1_NULL);
2017c478bd9Sstevel@tonic-gate   if(length != 0) return ASN1_BAD_LENGTH;
2027c478bd9Sstevel@tonic-gate   cleanup();
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate asn1_error_code asn1_decode_printablestring(asn1buf *buf, int *retlen, char **val)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate   setup();
2087c478bd9Sstevel@tonic-gate   tag(ASN1_PRINTABLESTRING);
2097c478bd9Sstevel@tonic-gate   retval = asn1buf_remove_charstring(buf,length,val);
2107c478bd9Sstevel@tonic-gate   if(retval) return retval;
2117c478bd9Sstevel@tonic-gate   *retlen = length;
2127c478bd9Sstevel@tonic-gate   cleanup();
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate asn1_error_code asn1_decode_ia5string(asn1buf *buf, int *retlen, char **val)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate   setup();
2187c478bd9Sstevel@tonic-gate   tag(ASN1_IA5STRING);
2197c478bd9Sstevel@tonic-gate   retval = asn1buf_remove_charstring(buf,length,val);
2207c478bd9Sstevel@tonic-gate   if(retval) return retval;
2217c478bd9Sstevel@tonic-gate   *retlen = length;
2227c478bd9Sstevel@tonic-gate   cleanup();
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate asn1_error_code asn1_decode_generaltime(asn1buf *buf, time_t *val)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate   setup();
2287c478bd9Sstevel@tonic-gate   char *s;
2297c478bd9Sstevel@tonic-gate   struct tm ts;
2307c478bd9Sstevel@tonic-gate   time_t t;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate   tag(ASN1_GENERALTIME);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate   if(length != 15) return ASN1_BAD_LENGTH;
2357c478bd9Sstevel@tonic-gate   retval = asn1buf_remove_charstring(buf,15,&s);
2367c478bd9Sstevel@tonic-gate   /* Time encoding: YYYYMMDDhhmmssZ */
2377c478bd9Sstevel@tonic-gate   if(s[14] != 'Z') {
2387c478bd9Sstevel@tonic-gate       free(s);
2397c478bd9Sstevel@tonic-gate       return ASN1_BAD_FORMAT;
2407c478bd9Sstevel@tonic-gate   }
241*505d05c7Sgtb   if(s[0] == '1' && !memcmp("19700101000000Z", s, 15)) {
242*505d05c7Sgtb       t = 0;
243*505d05c7Sgtb       free(s);
244*505d05c7Sgtb       goto done;
245*505d05c7Sgtb   }
2467c478bd9Sstevel@tonic-gate #define c2i(c) ((c)-'0')
2477c478bd9Sstevel@tonic-gate   ts.tm_year = 1000*c2i(s[0]) + 100*c2i(s[1]) + 10*c2i(s[2]) + c2i(s[3])
2487c478bd9Sstevel@tonic-gate     - 1900;
2497c478bd9Sstevel@tonic-gate   ts.tm_mon = 10*c2i(s[4]) + c2i(s[5]) - 1;
2507c478bd9Sstevel@tonic-gate   ts.tm_mday = 10*c2i(s[6]) + c2i(s[7]);
2517c478bd9Sstevel@tonic-gate   ts.tm_hour = 10*c2i(s[8]) + c2i(s[9]);
2527c478bd9Sstevel@tonic-gate   ts.tm_min = 10*c2i(s[10]) + c2i(s[11]);
2537c478bd9Sstevel@tonic-gate   ts.tm_sec = 10*c2i(s[12]) + c2i(s[13]);
2547c478bd9Sstevel@tonic-gate   ts.tm_isdst = -1;
2557c478bd9Sstevel@tonic-gate   t = gmt_mktime(&ts);
2567c478bd9Sstevel@tonic-gate   free(s);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate   if(t == -1) return ASN1_BAD_TIMEFORMAT;
2597c478bd9Sstevel@tonic-gate 
260*505d05c7Sgtb done:
2617c478bd9Sstevel@tonic-gate   *val = t;
2627c478bd9Sstevel@tonic-gate   cleanup();
2637c478bd9Sstevel@tonic-gate }
264