2003-08-13 05:53:07 +04:00
/*
Unix SMB / CIFS implementation .
simple SPNEGO routines
Copyright ( C ) Andrew Tridgell 2001
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 2 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "includes.h"
2004-11-02 09:42:15 +03:00
# include "asn_1.h"
2003-08-13 05:53:07 +04:00
/* free an asn1 structure */
2004-11-02 09:42:15 +03:00
void asn1_free ( struct asn1_data * data )
2003-08-13 05:53:07 +04:00
{
2004-08-25 06:05:02 +04:00
talloc_free ( data - > data ) ;
2003-08-13 05:53:07 +04:00
}
/* write to the ASN1 buffer, advancing the buffer pointer */
2004-11-02 09:42:15 +03:00
BOOL asn1_write ( struct asn1_data * data , const void * p , int len )
2003-08-13 05:53:07 +04:00
{
if ( data - > has_error ) return False ;
if ( data - > length < data - > ofs + len ) {
2004-05-25 21:50:17 +04:00
uint8_t * newp ;
2005-01-07 07:39:16 +03:00
newp = talloc_realloc ( NULL , data - > data , uint8_t , data - > ofs + len ) ;
2003-08-13 05:53:07 +04:00
if ( ! newp ) {
2004-08-25 06:05:02 +04:00
asn1_free ( data ) ;
2003-08-13 05:53:07 +04:00
data - > has_error = True ;
return False ;
}
data - > data = newp ;
data - > length = data - > ofs + len ;
}
memcpy ( data - > data + data - > ofs , p , len ) ;
data - > ofs + = len ;
return True ;
}
2004-05-25 21:50:17 +04:00
/* useful fn for writing a uint8_t */
2004-11-02 09:42:15 +03:00
BOOL asn1_write_uint8 ( struct asn1_data * data , uint8_t v )
2003-08-13 05:53:07 +04:00
{
return asn1_write ( data , & v , 1 ) ;
}
/* push a tag onto the asn1 data buffer. Used for nested structures */
2004-11-02 09:42:15 +03:00
BOOL asn1_push_tag ( struct asn1_data * data , uint8_t tag )
2003-08-13 05:53:07 +04:00
{
struct nesting * nesting ;
asn1_write_uint8 ( data , tag ) ;
2005-01-27 10:08:20 +03:00
nesting = talloc ( NULL , struct nesting ) ;
2003-08-13 05:53:07 +04:00
if ( ! nesting ) {
data - > has_error = True ;
return False ;
}
nesting - > start = data - > ofs ;
nesting - > next = data - > nesting ;
data - > nesting = nesting ;
return asn1_write_uint8 ( data , 0xff ) ;
}
/* pop a tag */
2004-11-02 09:42:15 +03:00
BOOL asn1_pop_tag ( struct asn1_data * data )
2003-08-13 05:53:07 +04:00
{
struct nesting * nesting ;
size_t len ;
nesting = data - > nesting ;
if ( ! nesting ) {
data - > has_error = True ;
return False ;
}
len = data - > ofs - ( nesting - > start + 1 ) ;
/* yes, this is ugly. We don't know in advance how many bytes the length
of a tag will take , so we assumed 1 byte . If we were wrong then we
need to correct our mistake */
if ( len > 255 ) {
data - > data [ nesting - > start ] = 0x82 ;
if ( ! asn1_write_uint8 ( data , 0 ) ) return False ;
if ( ! asn1_write_uint8 ( data , 0 ) ) return False ;
memmove ( data - > data + nesting - > start + 3 , data - > data + nesting - > start + 1 , len ) ;
data - > data [ nesting - > start + 1 ] = len > > 8 ;
data - > data [ nesting - > start + 2 ] = len & 0xff ;
} else if ( len > 127 ) {
data - > data [ nesting - > start ] = 0x81 ;
if ( ! asn1_write_uint8 ( data , 0 ) ) return False ;
memmove ( data - > data + nesting - > start + 2 , data - > data + nesting - > start + 1 , len ) ;
data - > data [ nesting - > start + 1 ] = len ;
} else {
data - > data [ nesting - > start ] = len ;
}
data - > nesting = nesting - > next ;
2004-08-25 06:05:02 +04:00
talloc_free ( nesting ) ;
2003-08-13 05:53:07 +04:00
return True ;
}
2004-09-01 11:29:02 +04:00
/* "i" is the one's complement representation, as is the normal result of an
* implicit signed - > unsigned conversion */
2004-11-02 09:42:15 +03:00
static BOOL push_int_bigendian ( struct asn1_data * data , unsigned int i , BOOL negative )
2004-08-31 10:21:14 +04:00
{
uint8_t lowest = i & 0xFF ;
i = i > > 8 ;
if ( i ! = 0 )
2004-09-15 17:29:59 +04:00
if ( ! push_int_bigendian ( data , i , negative ) )
return False ;
2004-09-01 11:29:02 +04:00
if ( data - > nesting - > start + 1 = = data - > ofs ) {
/* We did not write anything yet, looking at the highest
* valued byte */
if ( negative ) {
/* Don't write leading 0xff's */
if ( lowest = = 0xFF )
2004-09-15 17:29:59 +04:00
return True ;
2004-09-01 11:29:02 +04:00
if ( ( lowest & 0x80 ) = = 0 ) {
/* The only exception for a leading 0xff is if
* the highest bit is 0 , which would indicate
* a positive value */
2004-09-15 17:29:59 +04:00
if ( ! asn1_write_uint8 ( data , 0xff ) )
return False ;
2004-09-01 11:29:02 +04:00
}
} else {
if ( lowest & 0x80 ) {
/* The highest bit of a positive integer is 1,
* this would indicate a negative number . Push
* a 0 to indicate a positive one */
2004-09-15 17:29:59 +04:00
if ( ! asn1_write_uint8 ( data , 0 ) )
return False ;
2004-09-01 11:29:02 +04:00
}
}
}
2004-08-31 10:21:14 +04:00
2004-09-15 17:29:59 +04:00
return asn1_write_uint8 ( data , lowest ) ;
2004-08-31 10:21:14 +04:00
}
2004-09-15 17:29:59 +04:00
/* write an Integer without the tag framing. Needed for example for the LDAP
* Abandon Operation */
2004-11-02 09:42:15 +03:00
BOOL asn1_write_implicit_Integer ( struct asn1_data * data , int i )
2003-08-13 05:53:07 +04:00
{
2004-09-01 11:29:02 +04:00
if ( i = = - 1 ) {
/* -1 is special as it consists of all-0xff bytes. In
push_int_bigendian this is the only case that is not
properly handled , as all 0xff bytes would be handled as
leading ones to be ignored . */
2004-09-15 17:29:59 +04:00
return asn1_write_uint8 ( data , 0xff ) ;
2004-09-01 11:29:02 +04:00
} else {
2004-09-15 17:29:59 +04:00
return push_int_bigendian ( data , i , i < 0 ) ;
2004-09-01 11:29:02 +04:00
}
2004-09-15 17:29:59 +04:00
}
/* write an integer */
2004-11-02 09:42:15 +03:00
BOOL asn1_write_Integer ( struct asn1_data * data , int i )
2004-09-15 17:29:59 +04:00
{
if ( ! asn1_push_tag ( data , ASN1_INTEGER ) ) return False ;
if ( ! asn1_write_implicit_Integer ( data , i ) ) return False ;
2003-08-13 05:53:07 +04:00
return asn1_pop_tag ( data ) ;
}
/* write an object ID to a ASN1 buffer */
2004-11-02 09:42:15 +03:00
BOOL asn1_write_OID ( struct asn1_data * data , const char * OID )
2003-08-13 05:53:07 +04:00
{
2004-06-01 14:12:52 +04:00
uint_t v , v2 ;
2003-08-13 05:53:07 +04:00
const char * p = ( const char * ) OID ;
char * newp ;
if ( ! asn1_push_tag ( data , ASN1_OID ) )
return False ;
v = strtol ( p , & newp , 10 ) ;
p = newp ;
v2 = strtol ( p , & newp , 10 ) ;
p = newp ;
if ( ! asn1_write_uint8 ( data , 40 * v + v2 ) )
return False ;
while ( * p ) {
v = strtol ( p , & newp , 10 ) ;
p = newp ;
if ( v > = ( 1 < < 28 ) ) asn1_write_uint8 ( data , 0x80 | ( ( v > > 28 ) & 0xff ) ) ;
if ( v > = ( 1 < < 21 ) ) asn1_write_uint8 ( data , 0x80 | ( ( v > > 21 ) & 0xff ) ) ;
if ( v > = ( 1 < < 14 ) ) asn1_write_uint8 ( data , 0x80 | ( ( v > > 14 ) & 0xff ) ) ;
if ( v > = ( 1 < < 7 ) ) asn1_write_uint8 ( data , 0x80 | ( ( v > > 7 ) & 0xff ) ) ;
if ( ! asn1_write_uint8 ( data , v & 0x7f ) )
return False ;
}
return asn1_pop_tag ( data ) ;
}
/* write an octet string */
2004-11-02 09:42:15 +03:00
BOOL asn1_write_OctetString ( struct asn1_data * data , const void * p , size_t length )
2003-08-13 05:53:07 +04:00
{
asn1_push_tag ( data , ASN1_OCTET_STRING ) ;
asn1_write ( data , p , length ) ;
asn1_pop_tag ( data ) ;
return ! data - > has_error ;
}
/* write a general string */
2004-11-02 09:42:15 +03:00
BOOL asn1_write_GeneralString ( struct asn1_data * data , const char * s )
2003-08-13 05:53:07 +04:00
{
asn1_push_tag ( data , ASN1_GENERAL_STRING ) ;
asn1_write ( data , s , strlen ( s ) ) ;
asn1_pop_tag ( data ) ;
return ! data - > has_error ;
}
2004-11-02 09:42:15 +03:00
BOOL asn1_write_ContextSimple ( struct asn1_data * data , uint8_t num , DATA_BLOB * blob )
2004-08-17 15:22:44 +04:00
{
asn1_push_tag ( data , ASN1_CONTEXT_SIMPLE ( num ) ) ;
asn1_write ( data , blob - > data , blob - > length ) ;
asn1_pop_tag ( data ) ;
return ! data - > has_error ;
}
2003-08-13 05:53:07 +04:00
/* write a BOOLEAN */
2004-11-02 09:42:15 +03:00
BOOL asn1_write_BOOLEAN ( struct asn1_data * data , BOOL v )
2003-08-13 05:53:07 +04:00
{
asn1_push_tag ( data , ASN1_BOOLEAN ) ;
2004-09-01 18:49:43 +04:00
asn1_write_uint8 ( data , v ? 0xFF : 0 ) ;
2003-08-13 05:53:07 +04:00
asn1_pop_tag ( data ) ;
return ! data - > has_error ;
}
2004-11-02 09:42:15 +03:00
BOOL asn1_read_BOOLEAN ( struct asn1_data * data , BOOL * v )
2004-08-12 08:55:59 +04:00
{
2004-10-10 03:58:11 +04:00
uint8_t tmp = 0 ;
2004-08-12 08:55:59 +04:00
asn1_start_tag ( data , ASN1_BOOLEAN ) ;
2004-10-10 03:58:11 +04:00
asn1_read_uint8 ( data , & tmp ) ;
if ( tmp = = 0xFF ) {
* v = True ;
} else {
* v = False ;
}
2004-08-12 08:55:59 +04:00
asn1_end_tag ( data ) ;
return ! data - > has_error ;
}
2003-08-13 05:53:07 +04:00
/* check a BOOLEAN */
2004-11-02 09:42:15 +03:00
BOOL asn1_check_BOOLEAN ( struct asn1_data * data , BOOL v )
2003-08-13 05:53:07 +04:00
{
2004-05-25 21:50:17 +04:00
uint8_t b = 0 ;
2003-08-13 05:53:07 +04:00
asn1_read_uint8 ( data , & b ) ;
if ( b ! = ASN1_BOOLEAN ) {
data - > has_error = True ;
return False ;
}
asn1_read_uint8 ( data , & b ) ;
if ( b ! = v ) {
data - > has_error = True ;
return False ;
}
return ! data - > has_error ;
}
2004-11-02 09:42:15 +03:00
/* load a struct asn1_data structure with a lump of data, ready to be parsed */
BOOL asn1_load ( struct asn1_data * data , DATA_BLOB blob )
2003-08-13 05:53:07 +04:00
{
ZERO_STRUCTP ( data ) ;
2004-08-25 06:05:02 +04:00
data - > data = talloc_memdup ( NULL , blob . data , blob . length ) ;
2003-08-13 05:53:07 +04:00
if ( ! data - > data ) {
data - > has_error = True ;
return False ;
}
data - > length = blob . length ;
return True ;
}
2004-08-12 08:55:59 +04:00
/* Peek into an ASN1 buffer, not advancing the pointer */
2004-11-02 09:42:15 +03:00
BOOL asn1_peek ( struct asn1_data * data , void * p , int len )
2003-08-13 05:53:07 +04:00
{
2004-08-12 08:55:59 +04:00
if ( len < 0 | | data - > ofs + len < data - > ofs | | data - > ofs + len < len )
2004-06-19 12:15:41 +04:00
return False ;
2004-08-12 08:55:59 +04:00
if ( data - > ofs + len > data - > length )
2003-08-13 05:53:07 +04:00
return False ;
2004-08-12 08:55:59 +04:00
2003-08-13 05:53:07 +04:00
memcpy ( p , data - > data + data - > ofs , len ) ;
return True ;
}
2004-07-06 05:28:12 +04:00
/* read from a ASN1 buffer, advancing the buffer pointer */
2004-11-02 09:42:15 +03:00
BOOL asn1_read ( struct asn1_data * data , void * p , int len )
2004-07-06 05:28:12 +04:00
{
2004-08-12 08:55:59 +04:00
if ( ! asn1_peek ( data , p , len ) ) {
2004-07-06 05:28:12 +04:00
data - > has_error = True ;
return False ;
}
2004-08-12 08:55:59 +04:00
data - > ofs + = len ;
2004-07-06 05:28:12 +04:00
return True ;
}
/* read a uint8_t from a ASN1 buffer */
2004-11-02 09:42:15 +03:00
BOOL asn1_read_uint8 ( struct asn1_data * data , uint8_t * v )
2004-08-12 08:55:59 +04:00
{
return asn1_read ( data , v , 1 ) ;
}
2004-11-02 09:42:15 +03:00
BOOL asn1_peek_uint8 ( struct asn1_data * data , uint8_t * v )
2004-07-06 05:28:12 +04:00
{
return asn1_peek ( data , v , 1 ) ;
}
2004-11-02 09:42:15 +03:00
BOOL asn1_peek_tag ( struct asn1_data * data , uint8_t tag )
2004-08-12 08:55:59 +04:00
{
uint8_t b ;
2004-08-17 14:04:25 +04:00
if ( asn1_tag_remaining ( data ) < = 0 ) {
return False ;
}
2004-08-12 08:55:59 +04:00
if ( ! asn1_peek ( data , & b , sizeof ( b ) ) )
return False ;
return ( b = = tag ) ;
}
2003-08-13 05:53:07 +04:00
/* start reading a nested asn1 structure */
2004-11-02 09:42:15 +03:00
BOOL asn1_start_tag ( struct asn1_data * data , uint8_t tag )
2003-08-13 05:53:07 +04:00
{
2004-05-25 21:50:17 +04:00
uint8_t b ;
2003-08-13 05:53:07 +04:00
struct nesting * nesting ;
if ( ! asn1_read_uint8 ( data , & b ) )
return False ;
if ( b ! = tag ) {
data - > has_error = True ;
return False ;
}
2005-01-27 10:08:20 +03:00
nesting = talloc ( NULL , struct nesting ) ;
2003-08-13 05:53:07 +04:00
if ( ! nesting ) {
data - > has_error = True ;
return False ;
}
if ( ! asn1_read_uint8 ( data , & b ) ) {
return False ;
}
if ( b & 0x80 ) {
int n = b & 0x7f ;
if ( ! asn1_read_uint8 ( data , & b ) )
return False ;
nesting - > taglen = b ;
while ( n > 1 ) {
if ( ! asn1_read_uint8 ( data , & b ) )
return False ;
nesting - > taglen = ( nesting - > taglen < < 8 ) | b ;
n - - ;
}
} else {
nesting - > taglen = b ;
}
nesting - > start = data - > ofs ;
nesting - > next = data - > nesting ;
data - > nesting = nesting ;
return ! data - > has_error ;
}
2004-08-12 08:55:59 +04:00
/* Get the length to be expected in buf */
BOOL asn1_object_length ( uint8_t * buf , size_t buf_length ,
uint8_t tag , size_t * result )
{
2004-11-02 09:42:15 +03:00
struct asn1_data data ;
2004-08-12 08:55:59 +04:00
/* Fake the asn1_load to avoid the memdup, this is just to be able to
* re - use the length - reading in asn1_start_tag */
ZERO_STRUCT ( data ) ;
data . data = buf ;
data . length = buf_length ;
if ( ! asn1_start_tag ( & data , tag ) )
return False ;
* result = asn1_tag_remaining ( & data ) + data . ofs ;
/* We can't use asn1_end_tag here, as we did not consume the complete
* tag , so asn1_end_tag would flag an error and not free nesting */
2004-08-25 06:05:02 +04:00
talloc_free ( data . nesting ) ;
2004-08-12 08:55:59 +04:00
return True ;
}
2003-08-13 05:53:07 +04:00
/* stop reading a tag */
2004-11-02 09:42:15 +03:00
BOOL asn1_end_tag ( struct asn1_data * data )
2003-08-13 05:53:07 +04:00
{
struct nesting * nesting ;
/* make sure we read it all */
if ( asn1_tag_remaining ( data ) ! = 0 ) {
data - > has_error = True ;
return False ;
}
nesting = data - > nesting ;
if ( ! nesting ) {
data - > has_error = True ;
return False ;
}
data - > nesting = nesting - > next ;
2004-08-25 06:05:02 +04:00
talloc_free ( nesting ) ;
2003-08-13 05:53:07 +04:00
return True ;
}
/* work out how many bytes are left in this nested tag */
2004-11-02 09:42:15 +03:00
int asn1_tag_remaining ( struct asn1_data * data )
2003-08-13 05:53:07 +04:00
{
if ( ! data - > nesting ) {
data - > has_error = True ;
return - 1 ;
}
return data - > nesting - > taglen - ( data - > ofs - data - > nesting - > start ) ;
}
/* read an object ID from a ASN1 buffer */
2004-11-02 09:42:15 +03:00
BOOL asn1_read_OID ( struct asn1_data * data , const char * * OID )
2003-08-13 05:53:07 +04:00
{
2004-05-25 21:50:17 +04:00
uint8_t b ;
2004-08-12 08:55:59 +04:00
char * tmp_oid = NULL ;
2003-08-13 05:53:07 +04:00
if ( ! asn1_start_tag ( data , ASN1_OID ) ) return False ;
asn1_read_uint8 ( data , & b ) ;
2004-08-25 06:05:02 +04:00
tmp_oid = talloc_asprintf ( NULL , " %u " , b / 40 ) ;
2004-08-22 09:33:07 +04:00
tmp_oid = talloc_asprintf_append ( tmp_oid , " %u " , b % 40 ) ;
2003-08-13 05:53:07 +04:00
2004-08-11 20:09:54 +04:00
while ( ! data - > has_error & & asn1_tag_remaining ( data ) > 0 ) {
2004-06-01 14:12:52 +04:00
uint_t v = 0 ;
2003-08-13 05:53:07 +04:00
do {
asn1_read_uint8 ( data , & b ) ;
v = ( v < < 7 ) | ( b & 0x7f ) ;
} while ( ! data - > has_error & & b & 0x80 ) ;
2004-08-22 09:33:07 +04:00
tmp_oid = talloc_asprintf_append ( tmp_oid , " %u " , v ) ;
2003-08-13 05:53:07 +04:00
}
asn1_end_tag ( data ) ;
2004-08-25 06:05:02 +04:00
* OID = talloc_strdup ( NULL , tmp_oid ) ;
talloc_free ( tmp_oid ) ;
2003-08-13 05:53:07 +04:00
2004-06-19 12:15:41 +04:00
return ( * OID & & ! data - > has_error ) ;
2003-08-13 05:53:07 +04:00
}
/* check that the next object ID is correct */
2004-11-02 09:42:15 +03:00
BOOL asn1_check_OID ( struct asn1_data * data , const char * OID )
2003-08-13 05:53:07 +04:00
{
2004-10-28 16:46:59 +04:00
const char * id ;
2003-08-13 05:53:07 +04:00
if ( ! asn1_read_OID ( data , & id ) ) return False ;
if ( strcmp ( id , OID ) ! = 0 ) {
data - > has_error = True ;
return False ;
}
2004-11-02 14:17:06 +03:00
talloc_free ( discard_const ( id ) ) ;
2003-08-13 05:53:07 +04:00
return True ;
}
/* read a GeneralString from a ASN1 buffer */
2004-11-02 09:42:15 +03:00
BOOL asn1_read_GeneralString ( struct asn1_data * data , char * * s )
2003-08-13 05:53:07 +04:00
{
int len ;
if ( ! asn1_start_tag ( data , ASN1_GENERAL_STRING ) ) return False ;
len = asn1_tag_remaining ( data ) ;
2004-06-19 12:15:41 +04:00
if ( len < 0 ) {
data - > has_error = True ;
return False ;
}
2005-01-06 06:06:58 +03:00
* s = talloc_size ( NULL , len + 1 ) ;
2003-08-13 05:53:07 +04:00
if ( ! * s ) {
data - > has_error = True ;
return False ;
}
asn1_read ( data , * s , len ) ;
( * s ) [ len ] = 0 ;
asn1_end_tag ( data ) ;
return ! data - > has_error ;
}
/* read a octet string blob */
2004-11-02 09:42:15 +03:00
BOOL asn1_read_OctetString ( struct asn1_data * data , DATA_BLOB * blob )
2003-08-13 05:53:07 +04:00
{
int len ;
ZERO_STRUCTP ( blob ) ;
if ( ! asn1_start_tag ( data , ASN1_OCTET_STRING ) ) return False ;
len = asn1_tag_remaining ( data ) ;
2004-06-19 12:15:41 +04:00
if ( len < 0 ) {
data - > has_error = True ;
return False ;
}
2005-06-13 13:10:17 +04:00
* blob = data_blob ( NULL , len + 1 ) ;
2003-08-13 05:53:07 +04:00
asn1_read ( data , blob - > data , len ) ;
asn1_end_tag ( data ) ;
2005-06-13 13:10:17 +04:00
blob - > length - - ;
blob - > data [ len ] = 0 ;
2004-09-23 01:50:49 +04:00
if ( data - > has_error ) {
data_blob_free ( blob ) ;
* blob = data_blob ( NULL , 0 ) ;
return False ;
}
return True ;
2003-08-13 05:53:07 +04:00
}
2004-11-02 09:42:15 +03:00
BOOL asn1_read_ContextSimple ( struct asn1_data * data , uint8_t num , DATA_BLOB * blob )
2004-08-17 15:22:44 +04:00
{
int len ;
ZERO_STRUCTP ( blob ) ;
if ( ! asn1_start_tag ( data , ASN1_CONTEXT_SIMPLE ( num ) ) ) return False ;
len = asn1_tag_remaining ( data ) ;
if ( len < 0 ) {
data - > has_error = True ;
return False ;
}
* blob = data_blob ( NULL , len ) ;
asn1_read ( data , blob - > data , len ) ;
asn1_end_tag ( data ) ;
return ! data - > has_error ;
}
2004-09-29 16:40:30 +04:00
/* read an interger without tag*/
2004-11-02 09:42:15 +03:00
BOOL asn1_read_implicit_Integer ( struct asn1_data * data , int * i )
2003-08-13 05:53:07 +04:00
{
2004-05-25 21:50:17 +04:00
uint8_t b ;
2003-08-13 05:53:07 +04:00
* i = 0 ;
2004-09-29 16:40:30 +04:00
2003-08-13 05:53:07 +04:00
while ( asn1_tag_remaining ( data ) > 0 ) {
2004-09-29 16:40:30 +04:00
if ( ! asn1_read_uint8 ( data , & b ) ) return False ;
2003-08-13 05:53:07 +04:00
* i = ( * i < < 8 ) + b ;
}
2004-09-29 16:40:30 +04:00
return ! data - > has_error ;
}
/* read an interger */
2004-11-02 09:42:15 +03:00
BOOL asn1_read_Integer ( struct asn1_data * data , int * i )
2004-09-29 16:40:30 +04:00
{
* i = 0 ;
if ( ! asn1_start_tag ( data , ASN1_INTEGER ) ) return False ;
if ( ! asn1_read_implicit_Integer ( data , i ) ) return False ;
2003-08-13 05:53:07 +04:00
return asn1_end_tag ( data ) ;
}
2004-08-12 08:55:59 +04:00
/* read an interger */
2004-11-02 09:42:15 +03:00
BOOL asn1_read_enumerated ( struct asn1_data * data , int * v )
2004-08-12 08:55:59 +04:00
{
* v = 0 ;
if ( ! asn1_start_tag ( data , ASN1_ENUMERATED ) ) return False ;
while ( asn1_tag_remaining ( data ) > 0 ) {
uint8_t b ;
asn1_read_uint8 ( data , & b ) ;
* v = ( * v < < 8 ) + b ;
}
return asn1_end_tag ( data ) ;
}
2003-08-13 05:53:07 +04:00
/* check a enumarted value is correct */
2004-11-02 09:42:15 +03:00
BOOL asn1_check_enumerated ( struct asn1_data * data , int v )
2003-08-13 05:53:07 +04:00
{
2004-05-25 21:50:17 +04:00
uint8_t b ;
2003-08-13 05:53:07 +04:00
if ( ! asn1_start_tag ( data , ASN1_ENUMERATED ) ) return False ;
asn1_read_uint8 ( data , & b ) ;
asn1_end_tag ( data ) ;
if ( v ! = b )
data - > has_error = False ;
return ! data - > has_error ;
}
/* write an enumarted value to the stream */
2004-11-02 09:42:15 +03:00
BOOL asn1_write_enumerated ( struct asn1_data * data , uint8_t v )
2003-08-13 05:53:07 +04:00
{
if ( ! asn1_push_tag ( data , ASN1_ENUMERATED ) ) return False ;
asn1_write_uint8 ( data , v ) ;
asn1_pop_tag ( data ) ;
return ! data - > has_error ;
}