2N/A/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2N/A/*
2N/A * src/lib/krb5/asn.1/asn1_decode.c
2N/A *
2N/A * Copyright 1994, 2003 by the Massachusetts Institute of Technology.
2N/A * All Rights Reserved.
2N/A *
2N/A * Export of this software from the United States of America may
2N/A * require a specific license from the United States Government.
2N/A * It is the responsibility of any person or organization contemplating
2N/A * export to obtain such a license before exporting.
2N/A *
2N/A * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
2N/A * distribute this software and its documentation for any purpose and
2N/A * without fee is hereby granted, provided that the above copyright
2N/A * notice appear in all copies and that both that copyright notice and
2N/A * this permission notice appear in supporting documentation, and that
2N/A * the name of M.I.T. not be used in advertising or publicity pertaining
2N/A * to distribution of the software without specific, written prior
2N/A * permission. Furthermore if you modify this software you must label
2N/A * your software as modified software and not distribute it in such a
2N/A * fashion that it might be confused with the original M.I.T. software.
2N/A * M.I.T. makes no representations about the suitability of
2N/A * this software for any purpose. It is provided "as is" without express
2N/A * or implied warranty.
2N/A */
2N/A
2N/A/* ASN.1 primitive decoders */
2N/A#include "k5-int.h" /* for krb5int_gmt_mktime */
2N/A#include "asn1_decode.h"
2N/A#include "asn1_get.h"
2N/A#include <stdio.h>
2N/A#ifdef HAVE_SYS_TIME_H
2N/A#include <sys/time.h>
2N/A#ifdef TIME_WITH_SYS_TIME
2N/A#include <time.h>
2N/A#endif
2N/A#else
2N/A#include <time.h>
2N/A#endif
2N/A
2N/A#define setup() \
2N/A asn1_error_code retval; \
2N/A taginfo tinfo
2N/A
2N/A#define asn1class (tinfo.asn1class)
2N/A#define construction (tinfo.construction)
2N/A#define tagnum (tinfo.tagnum)
2N/A#define length (tinfo.length)
2N/A
2N/A#define tag(type) \
2N/A retval = asn1_get_tag_2(buf,&tinfo); \
2N/A if (retval) return retval; \
2N/A if (asn1class != UNIVERSAL || construction != PRIMITIVE || tagnum != type) \
2N/A return ASN1_BAD_ID
2N/A
2N/A#define cleanup() \
2N/A return 0
2N/A
2N/Aasn1_error_code
2N/Aasn1_decode_integer(asn1buf *buf, long int *val)
2N/A{
2N/A setup();
2N/A asn1_octet o;
2N/A long n = 0; /* initialize to keep gcc happy */
2N/A unsigned int i;
2N/A
2N/A tag(ASN1_INTEGER);
2N/A
2N/A for (i = 0; i < length; i++) {
2N/A retval = asn1buf_remove_octet(buf, &o);
2N/A if (retval) return retval;
2N/A if (!i) {
2N/A n = (0x80 & o) ? -1 : 0; /* grab sign bit */
2N/A if (n < 0 && length > sizeof (long))
2N/A return ASN1_OVERFLOW;
2N/A else if (length > sizeof (long) + 1) /* allow extra octet for positive */
2N/A return ASN1_OVERFLOW;
2N/A }
2N/A n = (n << 8) | o;
2N/A }
2N/A *val = n;
2N/A cleanup();
2N/A}
2N/A
2N/Aasn1_error_code
2N/Aasn1_decode_unsigned_integer(asn1buf *buf, long unsigned int *val)
2N/A{
2N/A setup();
2N/A asn1_octet o;
2N/A unsigned long n;
2N/A unsigned int i;
2N/A
2N/A tag(ASN1_INTEGER);
2N/A
2N/A for (i = 0, n = 0; i < length; i++) {
2N/A retval = asn1buf_remove_octet(buf, &o);
2N/A if (retval) return retval;
2N/A if (!i) {
2N/A if (0x80 & o)
2N/A return ASN1_OVERFLOW;
2N/A else if (length > sizeof (long) + 1)
2N/A return ASN1_OVERFLOW;
2N/A }
2N/A n = (n << 8) | o;
2N/A }
2N/A *val = n;
2N/A cleanup();
2N/A}
2N/A
2N/A/*
2N/A * asn1_decode_maybe_unsigned
2N/A *
2N/A * This is needed because older releases of MIT krb5 have signed
2N/A * sequence numbers. We want to accept both signed and unsigned
2N/A * sequence numbers, in the range -2^31..2^32-1, mapping negative
2N/A * numbers into their positive equivalents in the same way that C's
2N/A * normal integer conversions do, i.e., would preserve bits on a
2N/A * two's-complement architecture.
2N/A */
2N/Aasn1_error_code
2N/Aasn1_decode_maybe_unsigned(asn1buf *buf, unsigned long *val)
2N/A{
2N/A setup();
2N/A asn1_octet o;
2N/A unsigned long n, bitsremain;
2N/A unsigned int i;
2N/A
2N/A tag(ASN1_INTEGER);
2N/A o = 0;
2N/A n = 0;
2N/A bitsremain = ~0UL;
2N/A for (i = 0; i < length; i++) {
2N/A /* Accounts for u_long width not being a multiple of 8. */
2N/A if (bitsremain < 0xff) return ASN1_OVERFLOW;
2N/A retval = asn1buf_remove_octet(buf, &o);
2N/A if (retval) return retval;
2N/A if (bitsremain == ~0UL) {
2N/A if (i == 0)
2N/A n = (o & 0x80) ? ~0UL : 0UL; /* grab sign bit */
2N/A /*
2N/A * Skip leading zero or 0xFF octets to humor non-compliant encoders.
2N/A */
2N/A if (n == 0 && o == 0)
2N/A continue;
2N/A if (n == ~0UL && o == 0xff)
2N/A continue;
2N/A }
2N/A n = (n << 8) | o;
2N/A bitsremain >>= 8;
2N/A }
2N/A *val = n;
2N/A cleanup();
2N/A}
2N/A
2N/Aasn1_error_code
2N/Aasn1_decode_oid(asn1buf *buf, unsigned int *retlen, asn1_octet **val)
2N/A{
2N/A setup();
2N/A tag(ASN1_OBJECTIDENTIFIER);
2N/A retval = asn1buf_remove_octetstring(buf, length, val);
2N/A if (retval) return retval;
2N/A *retlen = length;
2N/A cleanup();
2N/A}
2N/A
2N/Aasn1_error_code
2N/Aasn1_decode_octetstring(asn1buf *buf, unsigned int *retlen, asn1_octet **val)
2N/A{
2N/A setup();
2N/A tag(ASN1_OCTETSTRING);
2N/A retval = asn1buf_remove_octetstring(buf,length,val);
2N/A if (retval) return retval;
2N/A *retlen = length;
2N/A cleanup();
2N/A}
2N/A
2N/Aasn1_error_code
2N/Aasn1_decode_charstring(asn1buf *buf, unsigned int *retlen, char **val)
2N/A{
2N/A setup();
2N/A tag(ASN1_OCTETSTRING);
2N/A retval = asn1buf_remove_charstring(buf,length,val);
2N/A if (retval) return retval;
2N/A *retlen = length;
2N/A cleanup();
2N/A}
2N/A
2N/A
2N/Aasn1_error_code
2N/Aasn1_decode_generalstring(asn1buf *buf, unsigned int *retlen, char **val)
2N/A{
2N/A setup();
2N/A tag(ASN1_GENERALSTRING);
2N/A retval = asn1buf_remove_charstring(buf,length,val);
2N/A if (retval) return retval;
2N/A *retlen = length;
2N/A cleanup();
2N/A}
2N/A
2N/A
2N/Aasn1_error_code
2N/Aasn1_decode_null(asn1buf *buf)
2N/A{
2N/A setup();
2N/A tag(ASN1_NULL);
2N/A if (length != 0) return ASN1_BAD_LENGTH;
2N/A cleanup();
2N/A}
2N/A
2N/Aasn1_error_code
2N/Aasn1_decode_printablestring(asn1buf *buf, int *retlen, char **val)
2N/A{
2N/A setup();
2N/A tag(ASN1_PRINTABLESTRING);
2N/A retval = asn1buf_remove_charstring(buf,length,val);
2N/A if (retval) return retval;
2N/A *retlen = length;
2N/A cleanup();
2N/A}
2N/A
2N/Aasn1_error_code
2N/Aasn1_decode_ia5string(asn1buf *buf, int *retlen, char **val)
2N/A{
2N/A setup();
2N/A tag(ASN1_IA5STRING);
2N/A retval = asn1buf_remove_charstring(buf,length,val);
2N/A if (retval) return retval;
2N/A *retlen = length;
2N/A cleanup();
2N/A}
2N/A
2N/Aasn1_error_code
2N/Aasn1_decode_generaltime(asn1buf *buf, time_t *val)
2N/A{
2N/A setup();
2N/A char *s;
2N/A struct tm ts;
2N/A time_t t;
2N/A
2N/A tag(ASN1_GENERALTIME);
2N/A
2N/A if (length != 15) return ASN1_BAD_LENGTH;
2N/A retval = asn1buf_remove_charstring(buf,15,&s);
2N/A if (retval) return retval;
2N/A /* Time encoding: YYYYMMDDhhmmssZ */
2N/A if (s[14] != 'Z') {
2N/A free(s);
2N/A return ASN1_BAD_FORMAT;
2N/A }
2N/A if (s[0] == '1' && !memcmp("19700101000000Z", s, 15)) {
2N/A t = 0;
2N/A free(s);
2N/A goto done;
2N/A }
2N/A#define c2i(c) ((c)-'0')
2N/A ts.tm_year = 1000*c2i(s[0]) + 100*c2i(s[1]) + 10*c2i(s[2]) + c2i(s[3])
2N/A - 1900;
2N/A ts.tm_mon = 10*c2i(s[4]) + c2i(s[5]) - 1;
2N/A ts.tm_mday = 10*c2i(s[6]) + c2i(s[7]);
2N/A ts.tm_hour = 10*c2i(s[8]) + c2i(s[9]);
2N/A ts.tm_min = 10*c2i(s[10]) + c2i(s[11]);
2N/A ts.tm_sec = 10*c2i(s[12]) + c2i(s[13]);
2N/A ts.tm_isdst = -1;
2N/A t = krb5int_gmt_mktime(&ts);
2N/A free(s);
2N/A
2N/A if (t == -1) return ASN1_BAD_TIMEFORMAT;
2N/A
2N/Adone:
2N/A *val = t;
2N/A cleanup();
2N/A}
2N/A
2N/Aasn1_error_code
2N/Aasn1_decode_boolean(asn1buf *buf, unsigned *val)
2N/A{
2N/A setup();
2N/A asn1_octet bval;
2N/A
2N/A tag(ASN1_BOOLEAN);
2N/A
2N/A retval = asn1buf_remove_octet(buf, &bval);
2N/A if (retval) return retval;
2N/A
2N/A *val = (bval != 0x00);
2N/A
2N/A cleanup();
2N/A}