Rizin
unix-like reverse engineering framework and cli tools
astr.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2017-2018 deroad <wargio@libero.it>
2 // SPDX-FileCopyrightText: 2017-2018 pancake <pancake@nopcode.org>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_util.h>
6 #include "asn1_oids.h"
7 
8 static const char *_hex = "0123456789abcdef";
9 
10 RZ_API RASN1String *rz_asn1_create_string(const char *string, bool allocated, ut32 length) {
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 }
22 
23 static RASN1String *newstr(const char *string) {
24  return rz_asn1_create_string(string, false, strlen(string) + 1);
25 }
26 
28  char *str;
29  ut32 len;
30  if (!s0 || !s1 || s0->length == 0 || s1->length == 0) {
31  return NULL;
32  }
33  len = s0->length + s1->length - 1;
34  str = (char *)malloc(len);
35  if (!str) {
36  if (freestr) {
39  }
40  return NULL;
41  }
42  memcpy(str, s0->string, s0->length);
43  memcpy(str + s0->length - 1, s1->string, s1->length);
44  if (freestr) {
47  }
49  if (!res) {
50  free(str);
51  }
52  return res;
53 }
54 
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 }
66 
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 }
107 
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 }
149 
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 }
177 
179  if (!buffer || length != 1 || (buffer[0] != 0 && buffer[0] != 0xFF)) {
180  return NULL;
181  }
182  return newstr(rz_str_bool(buffer[0]));
183 }
184 
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 }
212 
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 }
248 
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 }
312 
314  if (str) {
315  if (str->allocated) {
316  free((char *)str->string);
317  }
318  free(str);
319  }
320 }
321 
323  if (!object) {
324  return NULL;
325  }
326  const char *s = "Unknown tag";
327  // TODO: use array of strings
328  switch (object->tag) {
329  case TAG_EOC: s = "EOC"; break;
330  case TAG_BOOLEAN: s = "BOOLEAN"; break;
331  case TAG_INTEGER: s = "INTEGER"; break;
332  case TAG_BITSTRING: s = "BIT STRING"; break;
333  case TAG_OCTETSTRING: s = "OCTET STRING"; break;
334  case TAG_NULL: s = "NULL"; break;
335  case TAG_OID: s = "OBJECT IDENTIFIER"; break;
336  case TAG_OBJDESCRIPTOR: s = "ObjectDescriptor"; break;
337  case TAG_EXTERNAL: s = "EXTERNAL"; break;
338  case TAG_REAL: s = "REAL"; break;
339  case TAG_ENUMERATED: s = "ENUMERATED"; break;
340  case TAG_EMBEDDED_PDV: s = "EMBEDDED PDV"; break;
341  case TAG_UTF8STRING: s = "UTF8String"; break;
342  case TAG_SEQUENCE: s = "SEQUENCE"; break;
343  case TAG_SET: s = "SET"; break;
344  case TAG_NUMERICSTRING: s = "NumericString"; break;
345  case TAG_PRINTABLESTRING: s = "PrintableString"; break;
346  case TAG_T61STRING: s = "TeletexString"; break;
347  case TAG_VIDEOTEXSTRING: s = "VideotexString"; break;
348  case TAG_IA5STRING: s = "IA5String"; break;
349  case TAG_UTCTIME: s = "UTCTime"; break;
350  case TAG_GENERALIZEDTIME: s = "GeneralizedTime"; break;
351  case TAG_GRAPHICSTRING: s = "GraphicString"; break;
352  case TAG_VISIBLESTRING: s = "VisibleString"; break;
353  case TAG_GENERALSTRING: s = "GeneralString"; break;
354  case TAG_UNIVERSALSTRING: s = "UniversalString"; break;
355  case TAG_BMPSTRING: s = "BMPString"; break;
356  }
357  return newstr(s);
358 }
359 
361  if (!object) {
362  return NULL;
363  }
364  switch (object->tag) {
365  case TAG_EOC:
366  return NULL;
367  case TAG_BOOLEAN:
368  return newstr(rz_str_bool(object->sector[0]));
369  case TAG_REAL:
370  case TAG_INTEGER:
371  if (object->length < 16) {
372  return rz_asn1_stringify_integer(object->sector, object->length);
373  } else {
374  return rz_asn1_stringify_bytes(object->sector, object->length);
375  }
376  case TAG_BITSTRING:
377  // if (object->length < 8) {
378  return rz_asn1_stringify_bits(object->sector, object->length);
379  //} else {
380  // return asn1_stringify_bytes (object->sector, object->length);
381  //}
382  case TAG_OCTETSTRING:
383  return rz_asn1_stringify_bytes(object->sector, object->length);
384  case TAG_NULL:
385  return NULL;
386  case TAG_OID:
387  return rz_asn1_stringify_oid(object->sector, object->length);
388  // case TAG_OBJDESCRIPTOR:
389  // case TAG_EXTERNAL:
390  // case TAG_ENUMERATED:
391  // case TAG_EMBEDDED_PDV:
392  case TAG_UTF8STRING:
393  // case TAG_SEQUENCE:
394  // case TAG_SET:
395  case TAG_NUMERICSTRING:
396  case TAG_PRINTABLESTRING:
397  // case TAG_T61STRING:
398  // case TAG_VIDEOTEXSTRING:
399  case TAG_IA5STRING:
400  case TAG_VISIBLESTRING:
401  return rz_asn1_stringify_string(object->sector, object->length);
402  case TAG_UTCTIME:
403  return rz_asn1_stringify_utctime(object->sector, object->length);
404  case TAG_GENERALIZEDTIME:
405  return rz_asn1_stringify_time(object->sector, object->length);
406  // case TAG_GRAPHICSTRING:
407  // case TAG_GENERALSTRING:
408  // case TAG_UNIVERSALSTRING:
409  // case TAG_BMPSTRING:
410  }
411  return NULL;
412 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
struct rz_oid_list_t X509OIDList[]
RZ_API RASN1String * asn1_stringify_tag(RASN1Object *object)
Definition: astr.c:322
RZ_API RASN1String * rz_asn1_stringify_time(const ut8 *buffer, ut32 length)
Definition: astr.c:108
static RASN1String * newstr(const char *string)
Definition: astr.c:23
static const char * _hex
Definition: astr.c:8
RZ_API RASN1String * rz_asn1_concatenate_strings(RASN1String *s0, RASN1String *s1, bool freestr)
Definition: astr.c:27
RZ_API RASN1String * rz_asn1_stringify_integer(const ut8 *buffer, ut32 length)
Definition: astr.c:185
RZ_API RASN1String * rz_asn1_stringify_bits(const ut8 *buffer, ut32 length)
Definition: astr.c:150
RZ_API RASN1String * rz_asn1_create_string(const char *string, bool allocated, ut32 length)
Definition: astr.c:10
RZ_API RASN1String * rz_asn1_stringify_utctime(const ut8 *buffer, ut32 length)
Definition: astr.c:67
RZ_API RASN1String * rz_asn1_stringify_bytes(const ut8 *buffer, ut32 length)
Definition: astr.c:213
RZ_API RASN1String * rz_asn1_stringify_oid(const ut8 *buffer, ut32 length)
Definition: astr.c:249
RZ_API RASN1String * rz_asn1_stringify_boolean(const ut8 *buffer, ut32 length)
Definition: astr.c:178
RZ_API RASN1String * rz_asn1_stringify_string(const ut8 *buffer, ut32 length)
Definition: astr.c:55
RZ_API void rz_asn1_free_string(RASN1String *str)
Definition: astr.c:313
RZ_API RASN1String * asn1_stringify_sector(RASN1Object *object)
Definition: astr.c:360
int bits(struct state *s, int need)
Definition: blast.c:72
#define RZ_API
#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 start
Definition: sflib.h:133
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
uint32_t ut32
const char * k
Definition: dsignal.c:11
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
snprintf
Definition: kernel.h:364
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
int n
Definition: mipsasm.c:19
static RzSocket * s
Definition: rtr.c:28
#define TAG_ENUMERATED
Definition: rz_asn1.h:45
#define TAG_EOC
Definition: rz_asn1.h:35
#define TAG_GRAPHICSTRING
Definition: rz_asn1.h:57
#define TAG_INTEGER
Definition: rz_asn1.h:37
#define TAG_BITSTRING
Definition: rz_asn1.h:38
#define TAG_OCTETSTRING
Definition: rz_asn1.h:39
#define TAG_EMBEDDED_PDV
Definition: rz_asn1.h:46
#define TAG_BOOLEAN
Definition: rz_asn1.h:36
#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 ASN1_OID_LEN
Definition: rz_asn1.h:15
#define TAG_NULL
Definition: rz_asn1.h:40
#define TAG_IA5STRING
Definition: rz_asn1.h:54
#define TAG_UNIVERSALSTRING
Definition: rz_asn1.h:60
#define TAG_EXTERNAL
Definition: rz_asn1.h:43
#define TAG_OBJDESCRIPTOR
Definition: rz_asn1.h:42
#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 TAG_BMPSTRING
Definition: rz_asn1.h:61
#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 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 const char * rz_str_bool(int b)
Definition: str.c:3896
RZ_API void rz_str_filter(char *str)
Convert all non-printable characters in str with '.'.
Definition: str.c:2359
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define c(i)
Definition: sha256.c:43
#define s0(x)
Definition: sha256.c:59
#define s1(x)
Definition: sha256.c:60
Definition: buffer.h:15
Definition: z80asm.h:102
const ut8 * sector
Definition: rz_asn1.h:83
ut64(WINAPI *w32_GetEnabledXStateFeatures)()