17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * src/lib/krb5/asn.1/asn1_get.c
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Copyright 1994 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 #include "asn1_get.h"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate asn1_error_code
asn1_get_tag_2(asn1buf * buf,taginfo * t)307c478bd9Sstevel@tonic-gate asn1_get_tag_2(asn1buf *buf, taginfo *t)
317c478bd9Sstevel@tonic-gate {
327c478bd9Sstevel@tonic-gate     asn1_error_code retval;
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate     if (buf == NULL || buf->base == NULL ||
357c478bd9Sstevel@tonic-gate 	buf->bound - buf->next + 1 <= 0) {
367c478bd9Sstevel@tonic-gate 	t->tagnum = ASN1_TAGNUM_CEILING; /* emphatically not an EOC tag */
377c478bd9Sstevel@tonic-gate 	t->asn1class = UNIVERSAL;
387c478bd9Sstevel@tonic-gate 	t->construction = PRIMITIVE;
397c478bd9Sstevel@tonic-gate 	t->length = 0;
407c478bd9Sstevel@tonic-gate 	t->indef = 0;
417c478bd9Sstevel@tonic-gate 	return 0;
427c478bd9Sstevel@tonic-gate     }
437c478bd9Sstevel@tonic-gate     {
447c478bd9Sstevel@tonic-gate 	/* asn1_get_id(buf, t) */
457c478bd9Sstevel@tonic-gate 	asn1_tagnum tn=0;
467c478bd9Sstevel@tonic-gate 	asn1_octet o;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define ASN1_CLASS_MASK 0xC0
497c478bd9Sstevel@tonic-gate #define ASN1_CONSTRUCTION_MASK 0x20
507c478bd9Sstevel@tonic-gate #define ASN1_TAG_NUMBER_MASK 0x1F
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	retval = asn1buf_remove_octet(buf,&o);
537c478bd9Sstevel@tonic-gate 	if (retval)
547c478bd9Sstevel@tonic-gate 	    return retval;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	t->asn1class = (asn1_class)(o&ASN1_CLASS_MASK);
577c478bd9Sstevel@tonic-gate 	t->construction = (asn1_construction)(o&ASN1_CONSTRUCTION_MASK);
587c478bd9Sstevel@tonic-gate 	if ((o&ASN1_TAG_NUMBER_MASK) != ASN1_TAG_NUMBER_MASK){
597c478bd9Sstevel@tonic-gate 	    /* low-tag-number form */
607c478bd9Sstevel@tonic-gate 	    t->tagnum = (asn1_tagnum)(o&ASN1_TAG_NUMBER_MASK);
617c478bd9Sstevel@tonic-gate 	} else {
627c478bd9Sstevel@tonic-gate 	    /* high-tag-number form */
637c478bd9Sstevel@tonic-gate 	    do {
647c478bd9Sstevel@tonic-gate 		retval = asn1buf_remove_octet(buf,&o);
657c478bd9Sstevel@tonic-gate 		if (retval) return retval;
667c478bd9Sstevel@tonic-gate 		tn = (tn<<7) + (asn1_tagnum)(o&0x7F);
67*159d09a2SMark Phalan 	    }while(o&0x80);
687c478bd9Sstevel@tonic-gate 	    t->tagnum = tn;
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate     }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate     {
737c478bd9Sstevel@tonic-gate 	/* asn1_get_length(buf, t) */
747c478bd9Sstevel@tonic-gate 	asn1_octet o;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	t->indef = 0;
777c478bd9Sstevel@tonic-gate 	retval = asn1buf_remove_octet(buf,&o);
787c478bd9Sstevel@tonic-gate 	if (retval) return retval;
797c478bd9Sstevel@tonic-gate 	if ((o&0x80) == 0) {
807c478bd9Sstevel@tonic-gate 	    t->length = (int)(o&0x7F);
817c478bd9Sstevel@tonic-gate 	} else {
827c478bd9Sstevel@tonic-gate 	    int num;
837c478bd9Sstevel@tonic-gate 	    int len=0;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	    for (num = (int)(o&0x7F); num>0; num--) {
867c478bd9Sstevel@tonic-gate 		retval = asn1buf_remove_octet(buf,&o);
877c478bd9Sstevel@tonic-gate 		if(retval) return retval;
887c478bd9Sstevel@tonic-gate 		len = (len<<8) + (int)o;
897c478bd9Sstevel@tonic-gate 	    }
907c478bd9Sstevel@tonic-gate 	    if (len < 0)
917c478bd9Sstevel@tonic-gate 		return ASN1_OVERRUN;
927c478bd9Sstevel@tonic-gate 	    if (!len)
937c478bd9Sstevel@tonic-gate 		t->indef = 1;
947c478bd9Sstevel@tonic-gate 	    t->length = len;
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate     }
977c478bd9Sstevel@tonic-gate     if (t->indef && t->construction != CONSTRUCTED)
987c478bd9Sstevel@tonic-gate 	return ASN1_MISMATCH_INDEF;
997c478bd9Sstevel@tonic-gate     return 0;
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
asn1_get_sequence(asn1buf * buf,unsigned int * retlen,int * indef)1027c478bd9Sstevel@tonic-gate asn1_error_code asn1_get_sequence(asn1buf *buf, unsigned int *retlen, int *indef)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate     taginfo t;
1057c478bd9Sstevel@tonic-gate     asn1_error_code retval;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate     retval = asn1_get_tag_2(buf, &t);
1087c478bd9Sstevel@tonic-gate     if (retval)
1097c478bd9Sstevel@tonic-gate 	return retval;
1107c478bd9Sstevel@tonic-gate     if (t.asn1class != UNIVERSAL || t.construction != CONSTRUCTED ||
1117c478bd9Sstevel@tonic-gate 	t.tagnum != ASN1_SEQUENCE)
1127c478bd9Sstevel@tonic-gate 	return ASN1_BAD_ID;
1137c478bd9Sstevel@tonic-gate     if (retlen)
1147c478bd9Sstevel@tonic-gate 	*retlen = t.length;
1157c478bd9Sstevel@tonic-gate     if (indef)
1167c478bd9Sstevel@tonic-gate 	*indef = t.indef;
1177c478bd9Sstevel@tonic-gate     return 0;
1187c478bd9Sstevel@tonic-gate }
119