Rizin
unix-like reverse engineering framework and cli tools
rz_asn1.h File Reference
#include <rz_types.h>
#include <stdint.h>
#include <rz_util/rz_strbuf.h>

Go to the source code of this file.

Classes

struct  rz_asn1_string_t
 
struct  rz_asn1_list_t
 
struct  rz_asn1_bin_t
 
struct  rz_asn1_object_t
 

Macros

#define ASN1_JSON_NULL   "null"
 
#define ASN1_JSON_EMPTY   "{}"
 
#define ASN1_OID_LEN   64
 
#define ASN1_CLASS   0xC0 /* Bits 8 and 7 */
 
#define ASN1_FORM   0x20 /* Bit 6 */
 
#define ASN1_TAG   0x1F /* Bits 5 - 1 */
 
#define ASN1_LENLONG   0x80 /* long form */
 
#define ASN1_LENSHORT   0x7F /* Bits 7 - 1 */
 
#define CLASS_UNIVERSAL   0x00 /* 0 = Universal (defined by ITU X.680) */
 
#define CLASS_APPLICATION   0x40 /* 1 = Application */
 
#define CLASS_CONTEXT   0x80 /* 2 = Context-specific */
 
#define CLASS_PRIVATE   0xC0 /* 3 = Private */
 
#define FORM_PRIMITIVE   0x00 /* 0 = primitive */
 
#define FORM_CONSTRUCTED   0x20 /* 1 = constructed */
 
#define TAG_EOC   0x00 /* 0: End-of-contents octets */
 
#define TAG_BOOLEAN   0x01 /* 1: Boolean */
 
#define TAG_INTEGER   0x02 /* 2: Integer */
 
#define TAG_BITSTRING   0x03 /* 2: Bit string */
 
#define TAG_OCTETSTRING   0x04 /* 4: Byte string */
 
#define TAG_NULL   0x05 /* 5: NULL */
 
#define TAG_OID   0x06 /* 6: Object Identifier */
 
#define TAG_OBJDESCRIPTOR   0x07 /* 7: Object Descriptor */
 
#define TAG_EXTERNAL   0x08 /* 8: External */
 
#define TAG_REAL   0x09 /* 9: Real */
 
#define TAG_ENUMERATED   0x0A /* 10: Enumerated */
 
#define TAG_EMBEDDED_PDV   0x0B /* 11: Embedded Presentation Data Value */
 
#define TAG_UTF8STRING   0x0C /* 12: UTF8 string */
 
#define TAG_SEQUENCE   0x10 /* 16: Sequence/sequence of */
 
#define TAG_SET   0x11 /* 17: Set/set of */
 
#define TAG_NUMERICSTRING   0x12 /* 18: Numeric string */
 
#define TAG_PRINTABLESTRING   0x13 /* 19: Printable string (ASCII subset) */
 
#define TAG_T61STRING   0x14 /* 20: T61/Teletex string */
 
#define TAG_VIDEOTEXSTRING   0x15 /* 21: Videotex string */
 
#define TAG_IA5STRING   0x16 /* 22: IA5/ASCII string */
 
#define TAG_UTCTIME   0x17 /* 23: UTC time */
 
#define TAG_GENERALIZEDTIME   0x18 /* 24: Generalized time */
 
#define TAG_GRAPHICSTRING   0x19 /* 25: Graphic string */
 
#define TAG_VISIBLESTRING   0x1A /* 26: Visible string (ASCII subset) */
 
#define TAG_GENERALSTRING   0x1B /* 27: General string */
 
#define TAG_UNIVERSALSTRING   0x1C /* 28: Universal string */
 
#define TAG_BMPSTRING   0x1E /* 30: Basic Multilingual Plane/Unicode string */
 

Typedefs

typedef struct rz_asn1_string_t RASN1String
 
typedef struct rz_asn1_list_t ASN1List
 
typedef struct rz_asn1_bin_t RASN1Binary
 
typedef struct rz_asn1_object_t RASN1Object
 

Functions

RZ_API RASN1Objectrz_asn1_create_object (const ut8 *buffer, ut32 length, const ut8 *start_pointer)
 
RZ_API RASN1Binaryrz_asn1_create_binary (const ut8 *buffer, ut32 length)
 
RZ_API RASN1Stringrz_asn1_create_string (const char *string, bool allocated, ut32 length)
 
RZ_API RASN1Stringrz_asn1_stringify_bits (const ut8 *buffer, ut32 length)
 
RZ_API RASN1Stringrz_asn1_stringify_utctime (const ut8 *buffer, ut32 length)
 
RZ_API RASN1Stringrz_asn1_stringify_time (const ut8 *buffer, ut32 length)
 
RZ_API RASN1Stringrz_asn1_stringify_integer (const ut8 *buffer, ut32 length)
 
RZ_API RASN1Stringrz_asn1_stringify_string (const ut8 *buffer, ut32 length)
 
RZ_API RASN1Stringrz_asn1_stringify_bytes (const ut8 *buffer, ut32 length)
 
RZ_API RASN1Stringrz_asn1_stringify_boolean (const ut8 *buffer, ut32 length)
 
RZ_API RASN1Stringrz_asn1_stringify_oid (const ut8 *buffer, ut32 length)
 
RZ_API void rz_asn1_free_object (RASN1Object *object)
 
RZ_API char * rz_asn1_to_string (RASN1Object *object, ut32 depth, RzStrBuf *sb)
 
RZ_API void rz_asn1_free_string (RASN1String *string)
 
RZ_API void rz_asn1_free_binary (RASN1Binary *string)
 
RZ_API void asn1_setformat (int fmt)
 

Macro Definition Documentation

◆ ASN1_CLASS

#define ASN1_CLASS   0xC0 /* Bits 8 and 7 */

Definition at line 18 of file rz_asn1.h.

◆ ASN1_FORM

#define ASN1_FORM   0x20 /* Bit 6 */

Definition at line 19 of file rz_asn1.h.

◆ ASN1_JSON_EMPTY

#define ASN1_JSON_EMPTY   "{}"

Definition at line 13 of file rz_asn1.h.

◆ ASN1_JSON_NULL

#define ASN1_JSON_NULL   "null"

Definition at line 12 of file rz_asn1.h.

◆ ASN1_LENLONG

#define ASN1_LENLONG   0x80 /* long form */

Definition at line 21 of file rz_asn1.h.

◆ ASN1_LENSHORT

#define ASN1_LENSHORT   0x7F /* Bits 7 - 1 */

Definition at line 22 of file rz_asn1.h.

◆ ASN1_OID_LEN

#define ASN1_OID_LEN   64

Definition at line 15 of file rz_asn1.h.

◆ ASN1_TAG

#define ASN1_TAG   0x1F /* Bits 5 - 1 */

Definition at line 20 of file rz_asn1.h.

◆ CLASS_APPLICATION

#define CLASS_APPLICATION   0x40 /* 1 = Application */

Definition at line 26 of file rz_asn1.h.

◆ CLASS_CONTEXT

#define CLASS_CONTEXT   0x80 /* 2 = Context-specific */

Definition at line 27 of file rz_asn1.h.

◆ CLASS_PRIVATE

#define CLASS_PRIVATE   0xC0 /* 3 = Private */

Definition at line 28 of file rz_asn1.h.

◆ CLASS_UNIVERSAL

#define CLASS_UNIVERSAL   0x00 /* 0 = Universal (defined by ITU X.680) */

Definition at line 25 of file rz_asn1.h.

◆ FORM_CONSTRUCTED

#define FORM_CONSTRUCTED   0x20 /* 1 = constructed */

Definition at line 32 of file rz_asn1.h.

◆ FORM_PRIMITIVE

#define FORM_PRIMITIVE   0x00 /* 0 = primitive */

Definition at line 31 of file rz_asn1.h.

◆ TAG_BITSTRING

#define TAG_BITSTRING   0x03 /* 2: Bit string */

Definition at line 38 of file rz_asn1.h.

◆ TAG_BMPSTRING

#define TAG_BMPSTRING   0x1E /* 30: Basic Multilingual Plane/Unicode string */

Definition at line 61 of file rz_asn1.h.

◆ TAG_BOOLEAN

#define TAG_BOOLEAN   0x01 /* 1: Boolean */

Definition at line 36 of file rz_asn1.h.

◆ TAG_EMBEDDED_PDV

#define TAG_EMBEDDED_PDV   0x0B /* 11: Embedded Presentation Data Value */

Definition at line 46 of file rz_asn1.h.

◆ TAG_ENUMERATED

#define TAG_ENUMERATED   0x0A /* 10: Enumerated */

Definition at line 45 of file rz_asn1.h.

◆ TAG_EOC

#define TAG_EOC   0x00 /* 0: End-of-contents octets */

Definition at line 35 of file rz_asn1.h.

◆ TAG_EXTERNAL

#define TAG_EXTERNAL   0x08 /* 8: External */

Definition at line 43 of file rz_asn1.h.

◆ TAG_GENERALIZEDTIME

#define TAG_GENERALIZEDTIME   0x18 /* 24: Generalized time */

Definition at line 56 of file rz_asn1.h.

◆ TAG_GENERALSTRING

#define TAG_GENERALSTRING   0x1B /* 27: General string */

Definition at line 59 of file rz_asn1.h.

◆ TAG_GRAPHICSTRING

#define TAG_GRAPHICSTRING   0x19 /* 25: Graphic string */

Definition at line 57 of file rz_asn1.h.

◆ TAG_IA5STRING

#define TAG_IA5STRING   0x16 /* 22: IA5/ASCII string */

Definition at line 54 of file rz_asn1.h.

◆ TAG_INTEGER

#define TAG_INTEGER   0x02 /* 2: Integer */

Definition at line 37 of file rz_asn1.h.

◆ TAG_NULL

#define TAG_NULL   0x05 /* 5: NULL */

Definition at line 40 of file rz_asn1.h.

◆ TAG_NUMERICSTRING

#define TAG_NUMERICSTRING   0x12 /* 18: Numeric string */

Definition at line 50 of file rz_asn1.h.

◆ TAG_OBJDESCRIPTOR

#define TAG_OBJDESCRIPTOR   0x07 /* 7: Object Descriptor */

Definition at line 42 of file rz_asn1.h.

◆ TAG_OCTETSTRING

#define TAG_OCTETSTRING   0x04 /* 4: Byte string */

Definition at line 39 of file rz_asn1.h.

◆ TAG_OID

#define TAG_OID   0x06 /* 6: Object Identifier */

Definition at line 41 of file rz_asn1.h.

◆ TAG_PRINTABLESTRING

#define TAG_PRINTABLESTRING   0x13 /* 19: Printable string (ASCII subset) */

Definition at line 51 of file rz_asn1.h.

◆ TAG_REAL

#define TAG_REAL   0x09 /* 9: Real */

Definition at line 44 of file rz_asn1.h.

◆ TAG_SEQUENCE

#define TAG_SEQUENCE   0x10 /* 16: Sequence/sequence of */

Definition at line 48 of file rz_asn1.h.

◆ TAG_SET

#define TAG_SET   0x11 /* 17: Set/set of */

Definition at line 49 of file rz_asn1.h.

◆ TAG_T61STRING

#define TAG_T61STRING   0x14 /* 20: T61/Teletex string */

Definition at line 52 of file rz_asn1.h.

◆ TAG_UNIVERSALSTRING

#define TAG_UNIVERSALSTRING   0x1C /* 28: Universal string */

Definition at line 60 of file rz_asn1.h.

◆ TAG_UTCTIME

#define TAG_UTCTIME   0x17 /* 23: UTC time */

Definition at line 55 of file rz_asn1.h.

◆ TAG_UTF8STRING

#define TAG_UTF8STRING   0x0C /* 12: UTF8 string */

Definition at line 47 of file rz_asn1.h.

◆ TAG_VIDEOTEXSTRING

#define TAG_VIDEOTEXSTRING   0x15 /* 21: Videotex string */

Definition at line 53 of file rz_asn1.h.

◆ TAG_VISIBLESTRING

#define TAG_VISIBLESTRING   0x1A /* 26: Visible string (ASCII subset) */

Definition at line 58 of file rz_asn1.h.

Typedef Documentation

◆ ASN1List

typedef struct rz_asn1_list_t ASN1List

◆ RASN1Binary

typedef struct rz_asn1_bin_t RASN1Binary

◆ RASN1Object

typedef struct rz_asn1_object_t RASN1Object

◆ RASN1String

typedef struct rz_asn1_string_t RASN1String

Function Documentation

◆ asn1_setformat()

RZ_API void asn1_setformat ( int  fmt)

Definition at line 12 of file asn1.c.

12  {
13  ASN1_STD_FORMAT = fmt;
14 }
static int ASN1_STD_FORMAT
Definition: asn1.c:10

References ASN1_STD_FORMAT.

Referenced by cmd_print_fromage().

◆ rz_asn1_create_binary()

RZ_API RASN1Binary* rz_asn1_create_binary ( const ut8 buffer,
ut32  length 
)

Definition at line 152 of file asn1.c.

152  {
153  if (!buffer || !length) {
154  return NULL;
155  }
156  ut8 *buf = (ut8 *)calloc(sizeof(*buf), length);
157  if (!buf) {
158  return NULL;
159  }
161  if (!bin) {
162  free(buf);
163  return NULL;
164  }
165  memcpy(buf, buffer, length);
166  bin->binary = buf;
167  bin->length = length;
168  return bin;
169 }
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
#define RZ_NEW0(x)
Definition: rz_types.h:284
Definition: malloc.c:26
Definition: buffer.h:15

References calloc(), free(), length, memcpy(), NULL, and RZ_NEW0.

Referenced by rz_pkcs7_parse_attribute(), rz_pkcs7_parse_contentinfo(), rz_pkcs7_parse_issuerandserialnumber(), rz_pkcs7_parse_signerinfo(), rz_pkcs7_parse_spcdata(), rz_pkcs7_parse_spcmessagedigest(), rz_x509_parse_certificate(), rz_x509_parse_crlentry(), rz_x509_parse_extension(), rz_x509_parse_subjectpublickeyinfo(), and rz_x509_parse_tbscertificate().

◆ rz_asn1_create_object()

RZ_API RASN1Object* rz_asn1_create_object ( const ut8 buffer,
ut32  length,
const ut8 start_pointer 
)

Definition at line 120 of file asn1.c.

120  {
121  RASN1Object *object = asn1_parse_header(buffer, length, start_pointer);
122  if (object && (object->form == FORM_CONSTRUCTED || object->tag == TAG_BITSTRING || object->tag == TAG_OCTETSTRING)) {
123  const ut8 *next = object->sector;
124  const ut8 *end = next + object->length;
125  if (end > buffer + length) {
126  free(object);
127  return NULL;
128  }
129  ut32 count = rz_asn1_count_objects(object->sector, object->length);
130  if (count > 0) {
131  object->list.length = count;
132  object->list.objects = RZ_NEWS0(RASN1Object *, count);
133  if (!object->list.objects) {
134  rz_asn1_free_object(object);
135  return NULL;
136  }
137  ut32 i;
138  for (i = 0; next >= buffer && next < end && i < count; i++) {
139  RASN1Object *inner = rz_asn1_create_object(next, end - next, start_pointer);
140  if (!inner || next == inner->sector) {
141  rz_asn1_free_object(inner);
142  break;
143  }
144  next = inner->sector + inner->length;
145  object->list.objects[i] = inner;
146  }
147  }
148  }
149  return object;
150 }
lzma_index ** i
Definition: index.h:629
RZ_API void rz_asn1_free_object(RASN1Object *object)
Definition: asn1.c:445
static ut32 rz_asn1_count_objects(const ut8 *buffer, ut32 length)
Definition: asn1.c:97
RZ_API RASN1Object * rz_asn1_create_object(const ut8 *buffer, ut32 length, const ut8 *start_pointer)
Definition: asn1.c:120
static RASN1Object * asn1_parse_header(const ut8 *buffer, ut32 length, const ut8 *start_pointer)
Definition: asn1.c:39
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
uint32_t ut32
#define TAG_BITSTRING
Definition: rz_asn1.h:38
#define TAG_OCTETSTRING
Definition: rz_asn1.h:39
#define FORM_CONSTRUCTED
Definition: rz_asn1.h:32
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282
struct rz_asn1_object_t ** objects
Definition: rz_asn1.h:71
ASN1List list
Definition: rz_asn1.h:86
const ut8 * sector
Definition: rz_asn1.h:83

References asn1_parse_header(), count, test_evm::end, rz_asn1_object_t::form, FORM_CONSTRUCTED, free(), i, rz_asn1_object_t::length, length, rz_asn1_object_t::list, NULL, rz_asn1_list_t::objects, rz_asn1_count_objects(), rz_asn1_create_object(), rz_asn1_free_object(), RZ_NEWS0, rz_asn1_object_t::sector, rz_asn1_object_t::tag, TAG_BITSTRING, and TAG_OCTETSTRING.

Referenced by cmd_print_fromage(), rz_asn1_create_object(), rz_pkcs7_parse_cms(), rz_pkcs7_parse_spcinfo(), and rz_x509_parse_certificate2().

◆ rz_asn1_create_string()

RZ_API RASN1String* rz_asn1_create_string ( const char *  string,
bool  allocated,
ut32  length 
)

Definition at line 10 of file astr.c.

10  {
11  if (!string || !length) {
12  return NULL;
13  }
15  if (s) {
16  s->allocated = allocated;
17  s->length = length;
18  s->string = string;
19  }
20  return s;
21 }
static RzSocket * s
Definition: rtr.c:28

References length, NULL, RZ_NEW0, and s.

Referenced by newstr(), rz_asn1_concatenate_strings(), rz_asn1_print_hexdump_padded(), rz_asn1_stringify_bits(), rz_asn1_stringify_bytes(), rz_asn1_stringify_integer(), rz_asn1_stringify_oid(), rz_asn1_stringify_string(), rz_asn1_stringify_time(), and rz_asn1_stringify_utctime().

◆ rz_asn1_free_binary()

◆ rz_asn1_free_object()

RZ_API void rz_asn1_free_object ( RASN1Object object)

Definition at line 445 of file asn1.c.

445  {
446  ut32 i;
447  if (!object) {
448  return;
449  }
450  // This shall not be freed. it's a pointer into the buffer.
451  object->sector = NULL;
452  if (object->list.objects) {
453  for (i = 0; i < object->list.length; i++) {
454  rz_asn1_free_object(object->list.objects[i]);
455  }
456  RZ_FREE(object->list.objects);
457  }
458  object->list.objects = NULL;
459  object->list.length = 0;
460  free(object);
461 }
#define RZ_FREE(x)
Definition: rz_types.h:369

References free(), i, rz_asn1_object_t::list, NULL, rz_asn1_list_t::objects, rz_asn1_free_object(), and RZ_FREE.

Referenced by cmd_print_fromage(), rz_asn1_create_object(), rz_asn1_free_object(), rz_pkcs7_parse_cms(), rz_pkcs7_parse_spcinfo(), and rz_x509_parse_certificate().

◆ rz_asn1_free_string()

◆ rz_asn1_stringify_bits()

RZ_API RASN1String* rz_asn1_stringify_bits ( const ut8 buffer,
ut32  length 
)

Definition at line 150 of file astr.c.

150  {
151  ut32 i, j, k;
152  ut64 size;
153  ut8 c;
154  char *str;
155  if (!buffer || !length) {
156  return NULL;
157  }
158  size = 1 + ((length - 1) * 8) - buffer[0];
159  str = (char *)malloc(size);
160  if (!str) {
161  return NULL;
162  }
163  for (i = 1, j = 0; i < length && j < size; i++) {
164  c = buffer[i];
165  for (k = 0; k < 8 && j < size; k++, j++) {
166  str[size - j - 1] = c & 0x80 ? '1' : '0';
167  c <<= 1;
168  }
169  }
170  str[size - 1] = '\0';
171  RASN1String *asn1str = rz_asn1_create_string(str, true, size);
172  if (!asn1str) {
173  free(str);
174  }
175  return asn1str;
176 }
RZ_API RASN1String * rz_asn1_create_string(const char *string, bool allocated, ut32 length)
Definition: astr.c:10
const char * k
Definition: dsignal.c:11
voidpf void uLong size
Definition: ioapi.h:138
void * malloc(size_t size)
Definition: malloc.c:123
#define c(i)
Definition: sha256.c:43
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References c, free(), i, k, length, malloc(), NULL, rz_asn1_create_string(), cmd_descs_generate::str, and ut64().

Referenced by asn1_stringify_sector().

◆ rz_asn1_stringify_boolean()

RZ_API RASN1String* rz_asn1_stringify_boolean ( const ut8 buffer,
ut32  length 
)

Definition at line 178 of file astr.c.

178  {
179  if (!buffer || length != 1 || (buffer[0] != 0 && buffer[0] != 0xFF)) {
180  return NULL;
181  }
182  return newstr(rz_str_bool(buffer[0]));
183 }
static RASN1String * newstr(const char *string)
Definition: astr.c:23
RZ_API const char * rz_str_bool(int b)
Definition: str.c:3896

References length, newstr(), NULL, and rz_str_bool().

◆ rz_asn1_stringify_bytes()

RZ_API RASN1String* rz_asn1_stringify_bytes ( const ut8 buffer,
ut32  length 
)

Definition at line 213 of file astr.c.

213  {
214  ut32 i, j, k;
215  ut64 size;
216  ut8 c;
217  char *str;
218  if (!buffer || !length) {
219  return NULL;
220  }
221  size = (4 * length);
222  size += (64 - (size % 64));
223  str = (char *)malloc(size);
224  if (!str) {
225  return NULL;
226  }
227  memset(str, 0x20, size);
228 
229  for (i = 0, j = 0, k = 48; i < length && j < size && k < size; i++, j += 3, k++) {
230  c = buffer[i];
231  str[j + 0] = _hex[c >> 4];
232  str[j + 1] = _hex[c & 15];
233  str[j + 2] = ' ';
234  str[k] = (c >= ' ' && c <= '~') ? c : '.';
235  if (i % 16 == 15) {
236  str[j + 19] = '\n';
237  j += 17;
238  k += 49;
239  }
240  }
241  str[size - 1] = '\0';
242  RASN1String *asn1str = rz_asn1_create_string(str, true, size);
243  if (!asn1str) {
244  free(str);
245  }
246  return asn1str;
247 }
static const char * _hex
Definition: astr.c:8
return memset(p, 0, total)

References _hex, c, free(), i, k, length, malloc(), memset(), NULL, rz_asn1_create_string(), cmd_descs_generate::str, and ut64().

Referenced by asn1_stringify_sector().

◆ rz_asn1_stringify_integer()

RZ_API RASN1String* rz_asn1_stringify_integer ( const ut8 buffer,
ut32  length 
)

Definition at line 185 of file astr.c.

185  {
186  ut32 i, j;
187  ut64 size;
188  ut8 c;
189  char *str;
190  if (!buffer || !length) {
191  return NULL;
192  }
193  size = 3 * length;
194  str = (char *)malloc(size);
195  if (!str) {
196  return NULL;
197  }
198  memset(str, 0, size);
199  for (i = 0, j = 0; i < length && j < size; i++, j += 3) {
200  c = buffer[i];
201  str[j + 0] = _hex[c >> 4];
202  str[j + 1] = _hex[c & 15];
203  str[j + 2] = ':';
204  }
205  str[size - 1] = '\0';
206  RASN1String *asn1str = rz_asn1_create_string(str, true, size);
207  if (!asn1str) {
208  free(str);
209  }
210  return asn1str;
211 }

References _hex, c, free(), i, length, malloc(), memset(), NULL, rz_asn1_create_string(), cmd_descs_generate::str, and ut64().

Referenced by asn1_stringify_sector(), rz_x509_certificate_json(), rz_x509_crlentry_dump(), rz_x509_crlentry_json(), rz_x509_extensions_json(), rz_x509_parse_tbscertificate(), rz_x509_signedinfo_dump(), rz_x509_signedinfo_json(), rz_x509_subjectpublickeyinfo_dump(), rz_x509_subjectpublickeyinfo_json(), rz_x509_tbscertificate_dump(), and rz_x509_tbscertificate_json().

◆ rz_asn1_stringify_oid()

RZ_API RASN1String* rz_asn1_stringify_oid ( const ut8 buffer,
ut32  length 
)

Definition at line 249 of file astr.c.

249  {
250  const ut8 *start, *end;
251  char *str, *t;
252  ut32 i, slen, bits;
253  ut64 oid;
254  if (!buffer || !length) {
255  return NULL;
256  }
257 
258  str = (char *)calloc(1, ASN1_OID_LEN);
259  if (!str) {
260  return NULL;
261  }
262 
263  end = buffer + length;
264  t = str;
265  slen = 0;
266  bits = 0;
267  oid = 0;
268 
269  for (start = buffer; start < end && slen < ASN1_OID_LEN; start++) {
270  ut8 c = *start;
271  oid <<= 7;
272  oid |= (c & 0x7F);
273  bits += 7;
274  if (!(c & 0x80)) {
275  if (!slen) {
276  ut32 m = oid / 40;
277  ut32 n = oid % 40;
278  snprintf(t, ASN1_OID_LEN, "%01u.%01u", m, n);
279  slen = strlen(str);
280  t = str + slen;
281  } else {
282  snprintf(t, ASN1_OID_LEN - slen, ".%01u", (ut32)oid);
283  slen = strlen(str);
284  t = str + slen;
285  }
286  oid = 0;
287  bits = 0;
288  }
289  }
290  // incomplete oid.
291  // bad structure.
292  if (bits > 0) {
293  free(str);
294  return NULL;
295  }
296  i = 0;
297  do {
298  if (X509OIDList[i].oid[0] == str[0]) {
299  if (!strncmp(str, X509OIDList[i].oid, ASN1_OID_LEN)) {
300  free(str);
301  return newstr(X509OIDList[i].name);
302  }
303  }
304  ++i;
305  } while (X509OIDList[i].oid && X509OIDList[i].name);
307  if (!asn1str) {
308  free(str);
309  }
310  return asn1str;
311 }
struct rz_oid_list_t X509OIDList[]
int bits(struct state *s, int need)
Definition: blast.c:72
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
snprintf
Definition: kernel.h:364
int n
Definition: mipsasm.c:19
#define ASN1_OID_LEN
Definition: rz_asn1.h:15
Definition: z80asm.h:102

References ASN1_OID_LEN, bits(), c, calloc(), test_evm::end, free(), i, length, regress::m, n, newstr(), NULL, rz_asn1_create_string(), snprintf, start, cmd_descs_generate::str, ut64(), and X509OIDList.

Referenced by asn1_stringify_sector(), rz_asn1_to_string(), rz_pkcs7_parse_attribute(), rz_pkcs7_parse_cms(), rz_pkcs7_parse_contentinfo(), rz_pkcs7_parse_spcdata(), rz_x509_parse_algorithmidentifier(), rz_x509_parse_extension(), and rz_x509_parse_name().

◆ rz_asn1_stringify_string()

RZ_API RASN1String* rz_asn1_stringify_string ( const ut8 buffer,
ut32  length 
)

Definition at line 55 of file astr.c.

55  {
56  if (!buffer || !length) {
57  return NULL;
58  }
59  char *str = rz_str_ndup((const char *)buffer, length);
60  if (!str) {
61  return NULL;
62  }
64  return rz_asn1_create_string(str, true, strlen(str));
65 }
RZ_API char * rz_str_ndup(RZ_NULLABLE const char *ptr, int len)
Create new copy of string ptr limited to size len.
Definition: str.c:1006
RZ_API void rz_str_filter(char *str)
Convert all non-printable characters in str with '.'.
Definition: str.c:2359

References length, NULL, rz_asn1_create_string(), rz_str_filter(), rz_str_ndup(), and cmd_descs_generate::str.

Referenced by asn1_stringify_sector(), rz_asn1_to_string(), and rz_x509_parse_name().

◆ rz_asn1_stringify_time()

RZ_API RASN1String* rz_asn1_stringify_time ( const ut8 buffer,
ut32  length 
)

Definition at line 108 of file astr.c.

108  {
109  if (!buffer || length != 15 || buffer[14] != 'Z') {
110  return NULL;
111  }
112  const int str_sz = 24;
113  char *str = malloc(str_sz);
114  if (!str) {
115  return NULL;
116  }
117 
118  str[0] = buffer[6];
119  str[1] = buffer[7];
120  str[2] = '/';
121  str[3] = buffer[4];
122  str[4] = buffer[5];
123  str[5] = '/';
124  str[6] = buffer[0];
125  str[7] = buffer[1];
126  str[8] = buffer[2];
127  str[9] = buffer[3];
128  str[10] = ' ';
129  str[11] = buffer[8];
130  str[12] = buffer[9];
131  str[13] = ':';
132  str[14] = buffer[10];
133  str[15] = buffer[11];
134  str[16] = ':';
135  str[17] = buffer[12];
136  str[18] = buffer[13];
137  str[19] = ' ';
138  str[20] = 'G';
139  str[21] = 'M';
140  str[22] = 'T';
141  str[23] = '\0';
142 
143  RASN1String *asn1str = rz_asn1_create_string(str, true, str_sz);
144  if (!asn1str) {
145  free(str);
146  }
147  return asn1str;
148 }

References free(), length, malloc(), NULL, rz_asn1_create_string(), and cmd_descs_generate::str.

Referenced by asn1_stringify_sector(), rz_asn1_to_string(), and rz_x509_parse_validity().

◆ rz_asn1_stringify_utctime()

RZ_API RASN1String* rz_asn1_stringify_utctime ( const ut8 buffer,
ut32  length 
)

Definition at line 67 of file astr.c.

67  {
68  if (!buffer || length != 13 || buffer[12] != 'Z') {
69  return NULL;
70  }
71  const int str_sz = 24;
72  char *str = malloc(str_sz);
73  if (!str) {
74  return NULL;
75  }
76  str[0] = buffer[4];
77  str[1] = buffer[5];
78  str[2] = '/';
79  str[3] = buffer[2];
80  str[4] = buffer[3];
81  str[5] = '/';
82  str[6] = buffer[0] < '5' ? '2' : '1';
83  str[7] = buffer[0] < '5' ? '0' : '9';
84  str[8] = buffer[0];
85  str[9] = buffer[1];
86  str[10] = ' ';
87  str[11] = buffer[6];
88  str[12] = buffer[7];
89  str[13] = ':';
90  str[14] = buffer[8];
91  str[15] = buffer[9];
92  str[16] = ':';
93  str[17] = buffer[10];
94  str[18] = buffer[11];
95  str[19] = ' ';
96  str[20] = 'G';
97  str[21] = 'M';
98  str[22] = 'T';
99  str[23] = '\0';
100 
101  RASN1String *asn1str = rz_asn1_create_string(str, true, str_sz);
102  if (!asn1str) {
103  free(str);
104  }
105  return asn1str;
106 }

References free(), length, malloc(), NULL, rz_asn1_create_string(), and cmd_descs_generate::str.

Referenced by asn1_stringify_sector(), rz_asn1_to_string(), rz_x509_parse_crl(), rz_x509_parse_crlentry(), and rz_x509_parse_validity().

◆ rz_asn1_to_string()

RZ_API char* rz_asn1_to_string ( RASN1Object object,
ut32  depth,
RzStrBuf sb 
)

Definition at line 259 of file asn1.c.

259  {
260  ut32 i;
261  bool root = false;
262  if (!object) {
263  return NULL;
264  }
265  if (!sb) {
266  sb = rz_strbuf_new("");
267  root = true;
268  }
269  // this shall not be freed. it's a pointer into the buffer.
270  RASN1String *asn1str = NULL;
271  static char temp_name[4096] = { 0 };
272  const char *name = "";
273  const char *string = "";
274 
275  switch (object->klass) {
276  case CLASS_UNIVERSAL: // universal
277  switch (object->tag) {
278  case TAG_EOC:
279  name = "EOC";
280  break;
281  case TAG_BOOLEAN:
282  name = "BOOLEAN";
283  if (object->sector) {
284  string = (object->sector[0] != 0) ? "true" : "false";
285  }
286  break;
287  case TAG_INTEGER:
288  name = "INTEGER";
289  if (object->length < 16) {
290  rz_asn1_print_hex(object, temp_name, sizeof(temp_name), depth);
291  string = temp_name;
292  } else {
293  asn1str = rz_asn1_print_hexdump_padded(object, depth);
294  }
295  break;
296  case TAG_BITSTRING:
297  name = "BIT_STRING";
298  if (!object->list.objects) {
299  if (object->length < 16) {
300  rz_asn1_print_hex(object, temp_name, sizeof(temp_name), depth);
301  string = temp_name;
302  } else {
303  asn1str = rz_asn1_print_hexdump_padded(object, depth);
304  }
305  }
306  break;
307  case TAG_OCTETSTRING:
308  name = "OCTET_STRING";
309  if (rz_str_is_printable_limited((const char *)object->sector, object->length)) {
310  asn1str = rz_asn1_stringify_string(object->sector, object->length);
311  } else if (!object->list.objects) {
312  if (object->length < 16) {
313  rz_asn1_print_hex(object, temp_name, sizeof(temp_name), depth);
314  string = temp_name;
315  } else {
316  asn1str = rz_asn1_print_hexdump_padded(object, depth);
317  }
318  }
319  break;
320  case TAG_NULL:
321  name = "NULL";
322  break;
323  case TAG_OID:
324  name = "OBJECT_IDENTIFIER";
325  asn1str = rz_asn1_stringify_oid(object->sector, object->length);
326  break;
327  case TAG_OBJDESCRIPTOR:
328  name = "OBJECT_DESCRIPTOR";
329  break;
330  case TAG_EXTERNAL:
331  name = "EXTERNAL";
332  break;
333  case TAG_REAL:
334  name = "REAL";
335  asn1str = rz_asn1_print_hexdump_padded(object, depth);
336  break;
337  case TAG_ENUMERATED:
338  name = "ENUMERATED";
339  break;
340  case TAG_EMBEDDED_PDV:
341  name = "EMBEDDED_PDV";
342  break;
343  case TAG_UTF8STRING:
344  name = "UTF8String";
345  asn1str = rz_asn1_stringify_string(object->sector, object->length);
346  break;
347  case TAG_SEQUENCE:
348  name = "SEQUENCE";
349  break;
350  case TAG_SET:
351  name = "SET";
352  break;
353  case TAG_NUMERICSTRING:
354  name = "NumericString";
355  asn1str = rz_asn1_stringify_string(object->sector, object->length);
356  break;
357  case TAG_PRINTABLESTRING:
358  name = "PrintableString"; // ASCII subset
359  asn1str = rz_asn1_stringify_string(object->sector, object->length);
360  break;
361  case TAG_T61STRING:
362  name = "TeletexString"; // aka T61String
363  asn1str = rz_asn1_stringify_string(object->sector, object->length);
364  break;
365  case TAG_VIDEOTEXSTRING:
366  name = "VideotexString";
367  asn1str = rz_asn1_stringify_string(object->sector, object->length);
368  break;
369  case TAG_IA5STRING:
370  name = "IA5String"; // ASCII
371  asn1str = rz_asn1_stringify_string(object->sector, object->length);
372  break;
373  case TAG_UTCTIME:
374  name = "UTCTime";
375  asn1str = rz_asn1_stringify_utctime(object->sector, object->length);
376  break;
377  case TAG_GENERALIZEDTIME:
378  name = "GeneralizedTime";
379  asn1str = rz_asn1_stringify_time(object->sector, object->length);
380  break;
381  case TAG_GRAPHICSTRING:
382  name = "GraphicString";
383  asn1str = rz_asn1_stringify_string(object->sector, object->length);
384  break;
385  case TAG_VISIBLESTRING:
386  name = "VisibleString"; // ASCII subset
387  asn1str = rz_asn1_stringify_string(object->sector, object->length);
388  break;
389  case TAG_GENERALSTRING:
390  name = "GeneralString";
391  break;
392  case TAG_UNIVERSALSTRING:
393  name = "UniversalString";
394  asn1str = rz_asn1_stringify_string(object->sector, object->length);
395  break;
396  case TAG_BMPSTRING:
397  name = "BMPString";
398  asn1str = rz_asn1_stringify_string(object->sector, object->length);
399  break;
400  default:
401  snprintf(temp_name, sizeof(temp_name), "Universal_%u", object->tag);
402  name = temp_name;
403  break;
404  }
405  break;
406  case CLASS_APPLICATION:
407  snprintf(temp_name, sizeof(temp_name), "Application_%u", object->tag);
408  name = temp_name;
409  break;
410  case CLASS_CONTEXT:
411  snprintf(temp_name, sizeof(temp_name), "Context [%u]", object->tag); // Context
412  name = temp_name;
413  break;
414  case CLASS_PRIVATE:
415  snprintf(temp_name, sizeof(temp_name), "Private_%u", object->tag);
416  name = temp_name;
417  break;
418  }
419  if (asn1str) {
420  string = asn1str->string;
421  }
422  if (ASN1_STD_FORMAT) {
423  rz_strbuf_appendf(sb, "%4" PFMT64d " ", object->offset);
424  rz_strbuf_appendf(sb, "%4u:%2d: %s %-20s: %s\n", object->length,
425  depth, object->form ? "cons" : "prim", name, string);
426  rz_asn1_free_string(asn1str);
427  if (object->list.objects) {
428  for (i = 0; i < object->list.length; i++) {
429  rz_asn1_to_string(object->list.objects[i], depth + 1, sb);
430  }
431  }
432  } else {
433  rz_asn1_print_padded(sb, object, depth, name, string);
434  rz_asn1_free_string(asn1str);
435  if (object->list.objects) {
436  for (i = 0; i < object->list.length; i++) {
437  RASN1Object *obj = object->list.objects[i];
438  rz_asn1_to_string(obj, depth + 1, sb);
439  }
440  }
441  }
442  return root ? rz_strbuf_drain(sb) : NULL;
443 }
RZ_API char * rz_asn1_to_string(RASN1Object *object, ut32 depth, RzStrBuf *sb)
Definition: asn1.c:259
static RASN1String * rz_asn1_print_hexdump_padded(RASN1Object *object, ut32 depth)
Definition: asn1.c:219
static void rz_asn1_print_padded(RzStrBuf *sb, RASN1Object *object, int depth, const char *k, const char *v)
Definition: asn1.c:194
RZ_API void rz_asn1_print_hex(RASN1Object *object, char *buffer, ut32 size, ut32 depth)
Definition: asn1.c:171
static SblHeader sb
Definition: bin_mbn.c:26
int root
Definition: enough.c:226
#define TAG_ENUMERATED
Definition: rz_asn1.h:45
#define TAG_EOC
Definition: rz_asn1.h:35
#define CLASS_UNIVERSAL
Definition: rz_asn1.h:25
#define TAG_GRAPHICSTRING
Definition: rz_asn1.h:57
RZ_API void rz_asn1_free_string(RASN1String *string)
Definition: astr.c:313
#define TAG_INTEGER
Definition: rz_asn1.h:37
#define TAG_EMBEDDED_PDV
Definition: rz_asn1.h:46
#define TAG_BOOLEAN
Definition: rz_asn1.h:36
RZ_API RASN1String * rz_asn1_stringify_time(const ut8 *buffer, ut32 length)
Definition: astr.c:108
#define TAG_OID
Definition: rz_asn1.h:41
#define TAG_GENERALIZEDTIME
Definition: rz_asn1.h:56
#define TAG_GENERALSTRING
Definition: rz_asn1.h:59
#define TAG_T61STRING
Definition: rz_asn1.h:52
#define TAG_SET
Definition: rz_asn1.h:49
#define CLASS_PRIVATE
Definition: rz_asn1.h:28
#define TAG_NULL
Definition: rz_asn1.h:40
#define TAG_IA5STRING
Definition: rz_asn1.h:54
#define TAG_UNIVERSALSTRING
Definition: rz_asn1.h:60
RZ_API RASN1String * rz_asn1_stringify_utctime(const ut8 *buffer, ut32 length)
Definition: astr.c:67
#define TAG_EXTERNAL
Definition: rz_asn1.h:43
#define TAG_OBJDESCRIPTOR
Definition: rz_asn1.h:42
RZ_API RASN1String * rz_asn1_stringify_oid(const ut8 *buffer, ut32 length)
Definition: astr.c:249
#define TAG_REAL
Definition: rz_asn1.h:44
#define TAG_PRINTABLESTRING
Definition: rz_asn1.h:51
#define TAG_VISIBLESTRING
Definition: rz_asn1.h:58
#define CLASS_CONTEXT
Definition: rz_asn1.h:27
RZ_API RASN1String * rz_asn1_stringify_string(const ut8 *buffer, ut32 length)
Definition: astr.c:55
#define TAG_BMPSTRING
Definition: rz_asn1.h:61
#define CLASS_APPLICATION
Definition: rz_asn1.h:26
#define TAG_NUMERICSTRING
Definition: rz_asn1.h:50
#define TAG_UTF8STRING
Definition: rz_asn1.h:47
#define TAG_SEQUENCE
Definition: rz_asn1.h:48
#define TAG_VIDEOTEXSTRING
Definition: rz_asn1.h:53
#define TAG_UTCTIME
Definition: rz_asn1.h:55
RZ_API bool rz_str_is_printable_limited(const char *str, int size)
Definition: str.c:2053
RZ_API RZ_OWN char * rz_strbuf_drain(RzStrBuf *sb)
Definition: strbuf.c:342
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
#define PFMT64d
Definition: rz_types.h:394
const char * string
Definition: rz_asn1.h:65

References ASN1_STD_FORMAT, CLASS_APPLICATION, CLASS_CONTEXT, CLASS_PRIVATE, CLASS_UNIVERSAL, rz_asn1_object_t::form, i, rz_asn1_object_t::klass, rz_asn1_object_t::length, rz_asn1_object_t::list, NULL, rz_asn1_list_t::objects, rz_asn1_object_t::offset, PFMT64d, root, rz_asn1_free_string(), rz_asn1_print_hex(), rz_asn1_print_hexdump_padded(), rz_asn1_print_padded(), rz_asn1_stringify_oid(), rz_asn1_stringify_string(), rz_asn1_stringify_time(), rz_asn1_stringify_utctime(), rz_asn1_to_string(), rz_str_is_printable_limited(), rz_strbuf_appendf(), rz_strbuf_drain(), rz_strbuf_new(), sb, rz_asn1_object_t::sector, snprintf, rz_asn1_string_t::string, rz_asn1_object_t::tag, TAG_BITSTRING, TAG_BMPSTRING, TAG_BOOLEAN, TAG_EMBEDDED_PDV, TAG_ENUMERATED, TAG_EOC, TAG_EXTERNAL, TAG_GENERALIZEDTIME, TAG_GENERALSTRING, TAG_GRAPHICSTRING, TAG_IA5STRING, TAG_INTEGER, TAG_NULL, TAG_NUMERICSTRING, TAG_OBJDESCRIPTOR, TAG_OCTETSTRING, TAG_OID, TAG_PRINTABLESTRING, TAG_REAL, TAG_SEQUENCE, TAG_SET, TAG_T61STRING, TAG_UNIVERSALSTRING, TAG_UTCTIME, TAG_UTF8STRING, TAG_VIDEOTEXSTRING, and TAG_VISIBLESTRING.

Referenced by cmd_print_fromage(), and rz_asn1_to_string().