Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #94 from EntrustCorporation/only_sigs
Browse files Browse the repository at this point in the history
Only sigs
  • Loading branch information
Mike Ounsworth authored Jul 12, 2021
2 parents 3dad4c3 + 77894ac commit 581c298
Show file tree
Hide file tree
Showing 105 changed files with 18,942 additions and 557 deletions.
158 changes: 158 additions & 0 deletions asn1_test_data/ANY.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*-
* Copyright (c) 2004 Lev Walkin <[email protected]>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#include <asn_internal.h>
#include <ANY.h>
#include <errno.h>

static asn_OCTET_STRING_specifics_t asn_DEF_ANY_specs = {
sizeof(ANY_t),
offsetof(ANY_t, _asn_ctx),
ASN_OSUBV_ANY
};
asn_TYPE_descriptor_t asn_DEF_ANY = {
"ANY",
"ANY",
OCTET_STRING_free,
OCTET_STRING_print,
asn_generic_no_constraint,
OCTET_STRING_decode_ber,
OCTET_STRING_encode_der,
OCTET_STRING_decode_xer_hex,
ANY_encode_xer,
0, 0,
0, /* Use generic outmost tag fetcher */
0, 0, 0, 0,
0, /* No PER visible constraints */
0, 0, /* No members */
&asn_DEF_ANY_specs,
};


asn_enc_rval_t
ANY_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
int ilevel, enum xer_encoder_flags_e flags,
asn_app_consume_bytes_f *cb, void *app_key) {

if(flags & XER_F_CANONICAL) {
/*
* Canonical XER-encoding of ANY type is not supported.
*/
ASN__ENCODE_FAILED;
}

/* Dump as binary */
return OCTET_STRING_encode_xer(td, sptr, ilevel, flags, cb, app_key);
}

struct _callback_arg {
uint8_t *buffer;
size_t offset;
size_t size;
};

static int ANY__consume_bytes(const void *buffer, size_t size, void *key);

int
ANY_fromType(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) {
struct _callback_arg arg;
asn_enc_rval_t erval;

if(!st || !td) {
errno = EINVAL;
return -1;
}

if(!sptr) {
if(st->buf) FREEMEM(st->buf);
st->size = 0;
return 0;
}

arg.offset = arg.size = 0;
arg.buffer = 0;

erval = der_encode(td, sptr, ANY__consume_bytes, &arg);
if(erval.encoded == -1) {
if(arg.buffer) FREEMEM(arg.buffer);
return -1;
}
assert((size_t)erval.encoded == arg.offset);

if(st->buf) FREEMEM(st->buf);
st->buf = arg.buffer;
st->size = arg.offset;

return 0;
}

ANY_t *
ANY_new_fromType(asn_TYPE_descriptor_t *td, void *sptr) {
ANY_t tmp;
ANY_t *st;

if(!td || !sptr) {
errno = EINVAL;
return 0;
}

memset(&tmp, 0, sizeof(tmp));

if(ANY_fromType(&tmp, td, sptr)) return 0;

st = (ANY_t *)CALLOC(1, sizeof(ANY_t));
if(st) {
*st = tmp;
return st;
} else {
FREEMEM(tmp.buf);
return 0;
}
}

int
ANY_to_type(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
asn_dec_rval_t rval;
void *newst = 0;

if(!st || !td || !struct_ptr) {
errno = EINVAL;
return -1;
}

if(st->buf == 0) {
/* Nothing to convert, make it empty. */
*struct_ptr = (void *)0;
return 0;
}

rval = ber_decode(0, td, (void **)&newst, st->buf, st->size);
if(rval.code == RC_OK) {
*struct_ptr = newst;
return 0;
} else {
/* Remove possibly partially decoded data. */
ASN_STRUCT_FREE(*td, newst);
return -1;
}
}

static int ANY__consume_bytes(const void *buffer, size_t size, void *key) {
struct _callback_arg *arg = (struct _callback_arg *)key;

if((arg->offset + size) >= arg->size) {
size_t nsize = (arg->size ? arg->size << 2 : 16) + size;
void *p = REALLOC(arg->buffer, nsize);
if(!p) return -1;
arg->buffer = (uint8_t *)p;
arg->size = nsize;
}

memcpy(arg->buffer + arg->offset, buffer, size);
arg->offset += size;
assert(arg->offset < arg->size);

return 0;
}

48 changes: 48 additions & 0 deletions asn1_test_data/ANY.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*-
* Copyright (c) 2004 Lev Walkin <[email protected]>. All rights reserved.
* Redistribution and modifications are permitted subject to BSD license.
*/
#ifndef ASN_TYPE_ANY_H
#define ASN_TYPE_ANY_H

#include <OCTET_STRING.h> /* Implemented via OCTET STRING type */

#ifdef __cplusplus
extern "C" {
#endif

typedef struct ANY {
uint8_t *buf; /* BER-encoded ANY contents */
int size; /* Size of the above buffer */

asn_struct_ctx_t _asn_ctx; /* Parsing across buffer boundaries */
} ANY_t;

extern asn_TYPE_descriptor_t asn_DEF_ANY;

asn_struct_free_f ANY_free;
asn_struct_print_f ANY_print;
ber_type_decoder_f ANY_decode_ber;
der_type_encoder_f ANY_encode_der;
xer_type_encoder_f ANY_encode_xer;

/******************************
* Handy conversion routines. *
******************************/

/* Convert another ASN.1 type into the ANY. This implies DER encoding. */
int ANY_fromType(ANY_t *, asn_TYPE_descriptor_t *td, void *struct_ptr);
ANY_t *ANY_new_fromType(asn_TYPE_descriptor_t *td, void *struct_ptr);

/* Convert the contents of the ANY type into the specified type. */
int ANY_to_type(ANY_t *, asn_TYPE_descriptor_t *td, void **struct_ptr);

#define ANY_fromBuf(s, buf, size) OCTET_STRING_fromBuf((s), (buf), (size))
#define ANY_new_fromBuf(buf, size) OCTET_STRING_new_fromBuf( \
&asn_DEF_ANY, (buf), (size))

#ifdef __cplusplus
}
#endif

#endif /* ASN_TYPE_ANY_H */
Loading

0 comments on commit 581c298

Please sign in to comment.