1 /*
2  * src/lib/krb5/asn.1/asn1_get.c
3  *
4  * Copyright 1994 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 
27 #include "asn1_get.h"
28 
29 asn1_error_code
asn1_get_tag_2(asn1buf * buf,taginfo * t)30 asn1_get_tag_2(asn1buf *buf, taginfo *t)
31 {
32     asn1_error_code retval;
33 
34     if (buf == NULL || buf->base == NULL ||
35 	buf->bound - buf->next + 1 <= 0) {
36 	t->tagnum = ASN1_TAGNUM_CEILING; /* emphatically not an EOC tag */
37 	t->asn1class = UNIVERSAL;
38 	t->construction = PRIMITIVE;
39 	t->length = 0;
40 	t->indef = 0;
41 	return 0;
42     }
43     {
44 	/* asn1_get_id(buf, t) */
45 	asn1_tagnum tn=0;
46 	asn1_octet o;
47 
48 #define ASN1_CLASS_MASK 0xC0
49 #define ASN1_CONSTRUCTION_MASK 0x20
50 #define ASN1_TAG_NUMBER_MASK 0x1F
51 
52 	retval = asn1buf_remove_octet(buf,&o);
53 	if (retval)
54 	    return retval;
55 
56 	t->asn1class = (asn1_class)(o&ASN1_CLASS_MASK);
57 	t->construction = (asn1_construction)(o&ASN1_CONSTRUCTION_MASK);
58 	if ((o&ASN1_TAG_NUMBER_MASK) != ASN1_TAG_NUMBER_MASK){
59 	    /* low-tag-number form */
60 	    t->tagnum = (asn1_tagnum)(o&ASN1_TAG_NUMBER_MASK);
61 	} else {
62 	    /* high-tag-number form */
63 	    do {
64 		retval = asn1buf_remove_octet(buf,&o);
65 		if (retval) return retval;
66 		tn = (tn<<7) + (asn1_tagnum)(o&0x7F);
67 	    }while(o&0x80);
68 	    t->tagnum = tn;
69 	}
70     }
71 
72     {
73 	/* asn1_get_length(buf, t) */
74 	asn1_octet o;
75 
76 	t->indef = 0;
77 	retval = asn1buf_remove_octet(buf,&o);
78 	if (retval) return retval;
79 	if ((o&0x80) == 0) {
80 	    t->length = (int)(o&0x7F);
81 	} else {
82 	    int num;
83 	    int len=0;
84 
85 	    for (num = (int)(o&0x7F); num>0; num--) {
86 		retval = asn1buf_remove_octet(buf,&o);
87 		if(retval) return retval;
88 		len = (len<<8) + (int)o;
89 	    }
90 	    if (len < 0)
91 		return ASN1_OVERRUN;
92 	    if (!len)
93 		t->indef = 1;
94 	    t->length = len;
95 	}
96     }
97     if (t->indef && t->construction != CONSTRUCTED)
98 	return ASN1_MISMATCH_INDEF;
99     return 0;
100 }
101 
asn1_get_sequence(asn1buf * buf,unsigned int * retlen,int * indef)102 asn1_error_code asn1_get_sequence(asn1buf *buf, unsigned int *retlen, int *indef)
103 {
104     taginfo t;
105     asn1_error_code retval;
106 
107     retval = asn1_get_tag_2(buf, &t);
108     if (retval)
109 	return retval;
110     if (t.asn1class != UNIVERSAL || t.construction != CONSTRUCTED ||
111 	t.tagnum != ASN1_SEQUENCE)
112 	return ASN1_BAD_ID;
113     if (retlen)
114 	*retlen = t.length;
115     if (indef)
116 	*indef = t.indef;
117     return 0;
118 }
119