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