2N/A/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2N/A/*
2N/A * src/lib/krb5/asn.1/asn1_get.c
2N/A *
2N/A * Copyright 1994 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#include "asn1_get.h"
2N/A
2N/Aasn1_error_code
2N/Aasn1_get_tag_2(asn1buf *buf, taginfo *t)
2N/A{
2N/A asn1_error_code retval;
2N/A
2N/A if (buf == NULL || buf->base == NULL ||
2N/A buf->bound - buf->next + 1 <= 0) {
2N/A t->tagnum = ASN1_TAGNUM_CEILING; /* emphatically not an EOC tag */
2N/A t->asn1class = UNIVERSAL;
2N/A t->construction = PRIMITIVE;
2N/A t->length = 0;
2N/A t->indef = 0;
2N/A return 0;
2N/A }
2N/A {
2N/A /* asn1_get_id(buf, t) */
2N/A asn1_tagnum tn=0;
2N/A asn1_octet o;
2N/A
2N/A#define ASN1_CLASS_MASK 0xC0
2N/A#define ASN1_CONSTRUCTION_MASK 0x20
2N/A#define ASN1_TAG_NUMBER_MASK 0x1F
2N/A
2N/A retval = asn1buf_remove_octet(buf,&o);
2N/A if (retval)
2N/A return retval;
2N/A
2N/A t->asn1class = (asn1_class)(o&ASN1_CLASS_MASK);
2N/A t->construction = (asn1_construction)(o&ASN1_CONSTRUCTION_MASK);
2N/A if ((o&ASN1_TAG_NUMBER_MASK) != ASN1_TAG_NUMBER_MASK) {
2N/A /* low-tag-number form */
2N/A t->tagnum = (asn1_tagnum)(o&ASN1_TAG_NUMBER_MASK);
2N/A } else {
2N/A /* high-tag-number form */
2N/A do {
2N/A retval = asn1buf_remove_octet(buf,&o);
2N/A if (retval) return retval;
2N/A tn = (tn<<7) + (asn1_tagnum)(o&0x7F);
2N/A } while (o&0x80);
2N/A t->tagnum = tn;
2N/A }
2N/A }
2N/A
2N/A {
2N/A /* asn1_get_length(buf, t) */
2N/A asn1_octet o;
2N/A
2N/A t->indef = 0;
2N/A retval = asn1buf_remove_octet(buf,&o);
2N/A if (retval) return retval;
2N/A if ((o&0x80) == 0) {
2N/A t->length = (int)(o&0x7F);
2N/A } else {
2N/A int num;
2N/A int len=0;
2N/A
2N/A for (num = (int)(o&0x7F); num>0; num--) {
2N/A retval = asn1buf_remove_octet(buf,&o);
2N/A if (retval) return retval;
2N/A len = (len<<8) + (int)o;
2N/A }
2N/A if (len < 0)
2N/A return ASN1_OVERRUN;
2N/A if (!len)
2N/A t->indef = 1;
2N/A t->length = len;
2N/A }
2N/A }
2N/A if (t->indef && t->construction != CONSTRUCTED)
2N/A return ASN1_MISMATCH_INDEF;
2N/A return 0;
2N/A}
2N/A
2N/Aasn1_error_code
2N/Aasn1_get_sequence(asn1buf *buf, unsigned int *retlen, int *indef)
2N/A{
2N/A taginfo t;
2N/A asn1_error_code retval;
2N/A
2N/A retval = asn1_get_tag_2(buf, &t);
2N/A if (retval)
2N/A return retval;
2N/A if (t.asn1class != UNIVERSAL || t.construction != CONSTRUCTED ||
2N/A t.tagnum != ASN1_SEQUENCE)
2N/A return ASN1_BAD_ID;
2N/A if (retlen)
2N/A *retlen = t.length;
2N/A if (indef)
2N/A *indef = t.indef;
2N/A return 0;
2N/A}