Rizin
unix-like reverse engineering framework and cli tools
x509.h File Reference

Go to the source code of this file.

Functions

RZ_API bool rz_x509_parse_algorithmidentifier (RX509AlgorithmIdentifier *ai, RASN1Object *object)
 
RZ_API void rz_x509_free_algorithmidentifier (RX509AlgorithmIdentifier *ai)
 
RZ_API bool rz_x509_parse_subjectpublickeyinfo (RX509SubjectPublicKeyInfo *spki, RASN1Object *object)
 
RZ_API void rz_x509_free_subjectpublickeyinfo (RX509SubjectPublicKeyInfo *spki)
 
RZ_API bool rz_x509_parse_name (RX509Name *name, RASN1Object *object)
 
RZ_API void rz_x509_free_name (RX509Name *name)
 
RZ_API bool rz_x509_parse_extension (RX509Extension *ext, RASN1Object *object)
 
RZ_API void rz_x509_free_extension (RX509Extension *ex)
 
RZ_API bool rz_x509_parse_extensions (RX509Extensions *ext, RASN1Object *object)
 
RZ_API void rz_x509_free_extensions (RX509Extensions *ex)
 
RZ_API bool rz_x509_parse_tbscertificate (RX509TBSCertificate *tbsc, RASN1Object *object)
 
RZ_API void rz_x509_free_tbscertificate (RX509TBSCertificate *tbsc)
 
RZ_API RX509CRLEntryrz_x509_parse_crlentry (RASN1Object *object)
 
RZ_API void rz_x509_name_dump (RX509Name *name, const char *pad, RzStrBuf *sb)
 

Function Documentation

◆ rz_x509_free_algorithmidentifier()

RZ_API void rz_x509_free_algorithmidentifier ( RX509AlgorithmIdentifier ai)

Definition at line 306 of file x509.c.

306  {
307  if (ai) {
308  // no need to free ai, since this functions is used internally
311  }
312 }
RZ_API void rz_asn1_free_string(RASN1String *string)
Definition: astr.c:313
RASN1String * algorithm
Definition: rz_x509.h:27
RASN1String * parameters
Definition: rz_x509.h:28

References rz_x509_algorithmidentifier_t::algorithm, rz_x509_algorithmidentifier_t::parameters, and rz_asn1_free_string().

Referenced by rz_pkcs7_free_digestalgorithmidentifier(), rz_pkcs7_free_signerinfo(), rz_pkcs7_free_spcmessagedigest(), rz_x509_free_certificate(), rz_x509_free_crl(), rz_x509_free_subjectpublickeyinfo(), and rz_x509_free_tbscertificate().

◆ rz_x509_free_extension()

RZ_API void rz_x509_free_extension ( RX509Extension ex)

Definition at line 338 of file x509.c.

338  {
339  if (ex) {
340  rz_asn1_free_string(ex->extnID);
341  rz_asn1_free_binary(ex->extnValue);
342  // this is allocated dinamically so, i'll free
343  free(ex);
344  }
345 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API void rz_asn1_free_binary(RASN1Binary *string)
Definition: asn1.c:463
static xnu_exception_info ex

References ex, free(), rz_asn1_free_binary(), and rz_asn1_free_string().

Referenced by rz_x509_free_extensions(), and rz_x509_parse_extensions().

◆ rz_x509_free_extensions()

RZ_API void rz_x509_free_extensions ( RX509Extensions ex)

Definition at line 347 of file x509.c.

347  {
348  ut32 i;
349  if (!ex) {
350  return;
351  }
352  if (ex->extensions) {
353  for (i = 0; i < ex->length; i++) {
354  rz_x509_free_extension(ex->extensions[i]);
355  }
356  free(ex->extensions);
357  }
358  // no need to free ex, since this functions is used internally
359 }
lzma_index ** i
Definition: index.h:629
uint32_t ut32
RZ_API void rz_x509_free_extension(RX509Extension *ex)
Definition: x509.c:338

References ex, free(), i, and rz_x509_free_extension().

Referenced by rz_x509_free_tbscertificate().

◆ rz_x509_free_name()

RZ_API void rz_x509_free_name ( RX509Name name)

Definition at line 322 of file x509.c.

322  {
323  ut32 i;
324  if (!name) {
325  return;
326  }
327  if (name->names) {
328  for (i = 0; i < name->length; i++) {
329  rz_asn1_free_string(name->oids[i]);
330  rz_asn1_free_string(name->names[i]);
331  }
332  RZ_FREE(name->names);
333  RZ_FREE(name->oids);
334  }
335  // not freeing name since it's not allocated dinamically
336 }
#define RZ_FREE(x)
Definition: rz_types.h:369
Definition: z80asm.h:102

References i, rz_asn1_free_string(), and RZ_FREE.

Referenced by rz_pkcs7_free_issuerandserialnumber(), rz_x509_free_crl(), and rz_x509_free_tbscertificate().

◆ rz_x509_free_subjectpublickeyinfo()

RZ_API void rz_x509_free_subjectpublickeyinfo ( RX509SubjectPublicKeyInfo spki)

Definition at line 361 of file x509.c.

361  {
362  if (spki) {
367  // No need to free spki, since it's a static variable.
368  }
369 }
RX509AlgorithmIdentifier algorithm
Definition: rz_x509.h:54
RASN1Binary * subjectPublicKeyExponent
Definition: rz_x509.h:59
RASN1Binary * subjectPublicKey
Definition: rz_x509.h:56
RASN1Binary * subjectPublicKeyModule
Definition: rz_x509.h:60
RZ_API void rz_x509_free_algorithmidentifier(RX509AlgorithmIdentifier *ai)
Definition: x509.c:306

References rz_x509_subjectpublickeyinfo_t::algorithm, rz_asn1_free_binary(), rz_x509_free_algorithmidentifier(), rz_x509_subjectpublickeyinfo_t::subjectPublicKey, rz_x509_subjectpublickeyinfo_t::subjectPublicKeyExponent, and rz_x509_subjectpublickeyinfo_t::subjectPublicKeyModule.

Referenced by rz_x509_free_tbscertificate().

◆ rz_x509_free_tbscertificate()

RZ_API void rz_x509_free_tbscertificate ( RX509TBSCertificate tbsc)

Definition at line 371 of file x509.c.

371  {
372  if (tbsc) {
373  // version is ut32
376  rz_x509_free_name(&tbsc->issuer);
378  rz_x509_free_name(&tbsc->subject);
383  // no need to free tbsc, since this functions is used internally
384  }
385 }
RX509SubjectPublicKeyInfo subjectPublicKeyInfo
Definition: rz_x509.h:81
RASN1String * serialNumber
Definition: rz_x509.h:76
RASN1Binary * subjectUniqueID
Definition: rz_x509.h:83
RX509AlgorithmIdentifier signature
Definition: rz_x509.h:77
RX509Validity validity
Definition: rz_x509.h:79
RASN1Binary * issuerUniqueID
Definition: rz_x509.h:82
RX509Extensions extensions
Definition: rz_x509.h:84
RZ_API void rz_x509_free_subjectpublickeyinfo(RX509SubjectPublicKeyInfo *spki)
Definition: x509.c:361
RZ_API void rz_x509_free_extensions(RX509Extensions *ex)
Definition: x509.c:347
RZ_API void rz_x509_free_name(RX509Name *name)
Definition: x509.c:322
static void rz_x509_free_validity(RX509Validity *validity)
Definition: x509.c:314

References rz_x509_tbscertificate_t::extensions, rz_x509_tbscertificate_t::issuer, rz_x509_tbscertificate_t::issuerUniqueID, rz_asn1_free_binary(), rz_asn1_free_string(), rz_x509_free_algorithmidentifier(), rz_x509_free_extensions(), rz_x509_free_name(), rz_x509_free_subjectpublickeyinfo(), rz_x509_free_validity(), rz_x509_tbscertificate_t::serialNumber, rz_x509_tbscertificate_t::signature, rz_x509_tbscertificate_t::subject, rz_x509_tbscertificate_t::subjectPublicKeyInfo, rz_x509_tbscertificate_t::subjectUniqueID, and rz_x509_tbscertificate_t::validity.

Referenced by rz_x509_free_certificate().

◆ rz_x509_name_dump()

RZ_API void rz_x509_name_dump ( RX509Name name,
const char *  pad,
RzStrBuf sb 
)

Definition at line 434 of file x509.c.

434  {
435  ut32 i;
436  if (!name) {
437  return;
438  }
439  if (!pad) {
440  pad = "";
441  }
442  for (i = 0; i < name->length; i++) {
443  if (!name->oids[i] || !name->names[i]) {
444  continue;
445  }
446  rz_strbuf_appendf(sb, "%s%s: %s\n", pad, name->oids[i]->string, name->names[i]->string);
447  }
448 }
static SblHeader sb
Definition: bin_mbn.c:26
static void pad(RzStrBuf *sb, ut32 count)
Definition: protobuf.c:36
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2

References i, pad(), rz_strbuf_appendf(), and sb.

Referenced by rz_x509_crl_to_string(), rz_x509_signedinfo_dump(), and rz_x509_tbscertificate_dump().

◆ rz_x509_parse_algorithmidentifier()

RZ_API bool rz_x509_parse_algorithmidentifier ( RX509AlgorithmIdentifier ai,
RASN1Object object 
)

Definition at line 44 of file x509.c.

44  {
45  rz_return_val_if_fail(ai && object, false);
46 
47  if (object->list.length < 1 || !object->list.objects || !is_oid_object(object)) {
48  return false;
49  }
50 
51  ai->algorithm = rz_asn1_stringify_oid(object->list.objects[0]->sector, object->list.objects[0]->length);
52  ai->parameters = NULL; // TODO
53  // ai->parameters = asn1_stringify_sector (object->list.objects[1]);
54  return true;
55 }
#define NULL
Definition: cris-opc.c:27
RZ_API RASN1String * rz_asn1_stringify_oid(const ut8 *buffer, ut32 length)
Definition: astr.c:249
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
ut32 length
Definition: rz_asn1.h:70
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
static bool is_oid_object(RASN1Object *object)
Definition: x509.c:38

References rz_x509_algorithmidentifier_t::algorithm, is_oid_object(), rz_asn1_list_t::length, rz_asn1_object_t::length, rz_asn1_object_t::list, NULL, rz_asn1_list_t::objects, rz_x509_algorithmidentifier_t::parameters, rz_asn1_stringify_oid(), rz_return_val_if_fail, and rz_asn1_object_t::sector.

Referenced by rz_pkcs7_parse_digestalgorithmidentifier(), rz_pkcs7_parse_signerinfo(), rz_pkcs7_parse_spcmessagedigest(), rz_x509_parse_certificate(), rz_x509_parse_crl(), rz_x509_parse_subjectpublickeyinfo(), and rz_x509_parse_tbscertificate().

◆ rz_x509_parse_crlentry()

RZ_API RX509CRLEntry* rz_x509_parse_crlentry ( RASN1Object object)

Definition at line 261 of file x509.c.

261  {
263  if (!object || object->list.length != 2) {
264  return NULL;
265  }
266  entry = (RX509CRLEntry *)malloc(sizeof(RX509CRLEntry));
267  if (!entry) {
268  return NULL;
269  }
270  entry->userCertificate = rz_asn1_create_binary(object->list.objects[0]->sector, object->list.objects[0]->length);
271  entry->revocationDate = rz_asn1_stringify_utctime(object->list.objects[1]->sector, object->list.objects[1]->length);
272  return entry;
273 }
void * malloc(size_t size)
Definition: malloc.c:123
RZ_API RASN1Binary * rz_asn1_create_binary(const ut8 *buffer, ut32 length)
Definition: asn1.c:152
RZ_API RASN1String * rz_asn1_stringify_utctime(const ut8 *buffer, ut32 length)
Definition: astr.c:67
Definition: zipcmp.c:77

References rz_asn1_list_t::length, rz_asn1_object_t::length, rz_asn1_object_t::list, malloc(), NULL, rz_asn1_list_t::objects, rz_asn1_create_binary(), rz_asn1_stringify_utctime(), and rz_asn1_object_t::sector.

Referenced by rz_x509_parse_crl().

◆ rz_x509_parse_extension()

RZ_API bool rz_x509_parse_extension ( RX509Extension ext,
RASN1Object object 
)

Definition at line 122 of file x509.c.

122  {
123  RASN1Object *o;
124  if (!ext || !object || object->list.length < 2) {
125  return false;
126  }
127  o = object->list.objects[0];
128  if (o && o->tag == TAG_OID) {
129  ext->extnID = rz_asn1_stringify_oid(o->sector, o->length);
130  o = object->list.objects[1];
131  if (o->tag == TAG_BOOLEAN && object->list.length > 2) {
132  // This field is optional (so len must be 3)
133  ext->critical = o->sector[0] != 0;
134  o = object->list.objects[2];
135  }
136  if (o->tag == TAG_OCTETSTRING) {
137  ext->extnValue = rz_asn1_create_binary(o->sector, o->length);
138  }
139  }
140  return true;
141 }
static const char ext[]
Definition: apprentice.c:1981
#define TAG_OCTETSTRING
Definition: rz_asn1.h:39
#define TAG_BOOLEAN
Definition: rz_asn1.h:36
#define TAG_OID
Definition: rz_asn1.h:41

References ext, rz_asn1_list_t::length, rz_asn1_object_t::length, rz_asn1_object_t::list, rz_asn1_list_t::objects, rz_asn1_create_binary(), rz_asn1_stringify_oid(), rz_asn1_object_t::sector, rz_asn1_object_t::tag, TAG_BOOLEAN, TAG_OCTETSTRING, and TAG_OID.

Referenced by rz_x509_parse_extensions().

◆ rz_x509_parse_extensions()

RZ_API bool rz_x509_parse_extensions ( RX509Extensions ext,
RASN1Object object 
)

Definition at line 143 of file x509.c.

143  {
144  ut32 i;
145  if (!ext || !object || object->list.length != 1 || !object->list.objects[0]->length) {
146  return false;
147  }
148  object = object->list.objects[0];
149  ext->extensions = (RX509Extension **)calloc(object->list.length, sizeof(RX509Extension *));
150  if (!ext->extensions) {
151  return false;
152  }
153  ext->length = object->list.length;
154  for (i = 0; i < object->list.length; i++) {
155  ext->extensions[i] = RZ_NEW0(RX509Extension);
156  if (!rz_x509_parse_extension(ext->extensions[i], object->list.objects[i])) {
157  rz_x509_free_extension(ext->extensions[i]);
158  ext->extensions[i] = NULL;
159  }
160  }
161  return true;
162 }
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
#define RZ_NEW0(x)
Definition: rz_types.h:284
RZ_API bool rz_x509_parse_extension(RX509Extension *ext, RASN1Object *object)
Definition: x509.c:122

References calloc(), ext, i, rz_asn1_list_t::length, rz_asn1_object_t::length, rz_asn1_object_t::list, NULL, rz_asn1_list_t::objects, RZ_NEW0, rz_x509_free_extension(), and rz_x509_parse_extension().

Referenced by rz_x509_parse_tbscertificate().

◆ rz_x509_parse_name()

RZ_API bool rz_x509_parse_name ( RX509Name name,
RASN1Object object 
)

Definition at line 79 of file x509.c.

79  {
80  ut32 i;
81  if (!name || !object || !object->list.length) {
82  return false;
83  }
84  if (object->klass == CLASS_UNIVERSAL && object->tag == TAG_SEQUENCE) {
85  name->length = object->list.length;
86  name->names = (RASN1String **)calloc(name->length, sizeof(RASN1String *));
87  if (!name->names) {
88  name->length = 0;
89  return false;
90  }
91  name->oids = (RASN1String **)calloc(name->length, sizeof(RASN1String *));
92  if (!name->oids) {
93  name->length = 0;
94  RZ_FREE(name->names);
95  return false;
96  }
97  for (i = 0; i < object->list.length; i++) {
98  RASN1Object *o = object->list.objects[i];
99  if (o && o->klass == CLASS_UNIVERSAL &&
100  o->tag == TAG_SET &&
101  o->form == FORM_CONSTRUCTED &&
102  o->list.length == 1) {
103  o = o->list.objects[0];
104  if (o && o->list.length > 1 &&
105  o->klass == CLASS_UNIVERSAL &&
106  o->tag == TAG_SEQUENCE) {
107  if (o->list.objects[0]->klass == CLASS_UNIVERSAL &&
108  o->list.objects[0]->tag == TAG_OID) {
109  name->oids[i] = rz_asn1_stringify_oid(o->list.objects[0]->sector, o->list.objects[0]->length);
110  }
111  RASN1Object *obj1 = o->list.objects[1];
112  if (obj1 && obj1->klass == CLASS_UNIVERSAL) {
113  name->names[i] = rz_asn1_stringify_string(obj1->sector, obj1->length);
114  }
115  }
116  }
117  }
118  }
119  return true;
120 }
#define CLASS_UNIVERSAL
Definition: rz_asn1.h:25
#define TAG_SET
Definition: rz_asn1.h:49
RZ_API RASN1String * rz_asn1_stringify_string(const ut8 *buffer, ut32 length)
Definition: astr.c:55
#define FORM_CONSTRUCTED
Definition: rz_asn1.h:32
#define TAG_SEQUENCE
Definition: rz_asn1.h:48

References calloc(), CLASS_UNIVERSAL, rz_asn1_object_t::form, FORM_CONSTRUCTED, i, rz_asn1_object_t::klass, rz_asn1_list_t::length, rz_asn1_object_t::length, rz_asn1_object_t::list, rz_asn1_list_t::objects, rz_asn1_stringify_oid(), rz_asn1_stringify_string(), RZ_FREE, rz_asn1_object_t::sector, rz_asn1_object_t::tag, TAG_OID, TAG_SEQUENCE, and TAG_SET.

Referenced by rz_pkcs7_parse_issuerandserialnumber(), rz_x509_parse_crl(), and rz_x509_parse_tbscertificate().

◆ rz_x509_parse_subjectpublickeyinfo()

RZ_API bool rz_x509_parse_subjectpublickeyinfo ( RX509SubjectPublicKeyInfo spki,
RASN1Object object 
)

Definition at line 57 of file x509.c.

57  {
58  RASN1Object *o;
59  if (!spki || !object || object->list.length != 2) {
60  return false;
61  }
63  if (object->list.objects[1]) {
64  o = object->list.objects[1];
66  if (o->list.length == 1 && o->list.objects[0] && o->list.objects[0]->list.length == 2) {
67  o = o->list.objects[0];
68  if (o->list.objects[0]) {
70  }
71  if (o->list.objects[1]) {
73  }
74  }
75  }
76  return true;
77 }
RZ_API bool rz_x509_parse_algorithmidentifier(RX509AlgorithmIdentifier *ai, RASN1Object *object)
Definition: x509.c:44

References rz_x509_subjectpublickeyinfo_t::algorithm, rz_asn1_list_t::length, rz_asn1_object_t::length, rz_asn1_object_t::list, rz_asn1_list_t::objects, rz_asn1_create_binary(), rz_x509_parse_algorithmidentifier(), rz_asn1_object_t::sector, rz_x509_subjectpublickeyinfo_t::subjectPublicKey, rz_x509_subjectpublickeyinfo_t::subjectPublicKeyExponent, and rz_x509_subjectpublickeyinfo_t::subjectPublicKeyModule.

Referenced by rz_x509_parse_tbscertificate().

◆ rz_x509_parse_tbscertificate()

RZ_API bool rz_x509_parse_tbscertificate ( RX509TBSCertificate tbsc,
RASN1Object object 
)

Definition at line 164 of file x509.c.

164  {
165  RASN1Object **elems;
166  ut32 i;
167  ut32 shift = 0;
168  if (!tbsc || !object || object->list.length < 6) {
169  return false;
170  }
171  elems = object->list.objects;
172  // Following RFC
173  if (elems[0]->list.length == 1 &&
174  elems[0]->klass == CLASS_CONTEXT &&
175  elems[0]->form == FORM_CONSTRUCTED &&
176  elems[0]->list.objects[0]->tag == TAG_INTEGER &&
177  elems[0]->list.objects[0]->length == 1) {
178  // Integer inside a CLASS_CONTEXT
179  tbsc->version = (ut32)elems[0]->list.objects[0]->sector[0];
180  shift = 1;
181  } else {
182  tbsc->version = 0;
183  }
184  if (shift < object->list.length && elems[shift]->klass == CLASS_UNIVERSAL && elems[shift]->tag == TAG_INTEGER) {
185  tbsc->serialNumber = rz_asn1_stringify_integer(elems[shift]->sector, elems[shift]->length);
186  }
188  rz_x509_parse_name(&tbsc->issuer, elems[shift + 2]);
189  rz_x509_parse_validity(&tbsc->validity, elems[shift + 3]);
190  rz_x509_parse_name(&tbsc->subject, elems[shift + 4]);
192  if (tbsc->version > 0) {
193  for (i = shift + 6; i < object->list.length; i++) {
194  if (!elems[i] || elems[i]->klass != CLASS_CONTEXT) {
195  continue;
196  }
197  if (elems[i]->tag == 1) {
199  }
200  if (!elems[i]) {
201  continue;
202  }
203  if (elems[i]->tag == 2) {
205  }
206  if (!elems[i]) {
207  continue;
208  }
209  if (tbsc->version == 2 && elems[i]->tag == 3 && elems[i]->form == FORM_CONSTRUCTED) {
210  rz_x509_parse_extensions(&tbsc->extensions, elems[i]);
211  }
212  }
213  }
214  return true;
215 }
static RZ_NULLABLE RzILOpBitVector * shift(RzILOpBitVector *val, RZ_NULLABLE RzILOpBool **carry_out, arm_shifter type, RZ_OWN RzILOpBitVector *dist)
Definition: arm_il32.c:190
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
static void list(RzEgg *egg)
Definition: rz-gg.c:52
#define TAG_INTEGER
Definition: rz_asn1.h:37
RZ_API RASN1String * rz_asn1_stringify_integer(const ut8 *buffer, ut32 length)
Definition: astr.c:185
#define CLASS_CONTEXT
Definition: rz_asn1.h:27
RZ_API bool rz_x509_parse_subjectpublickeyinfo(RX509SubjectPublicKeyInfo *spki, RASN1Object *object)
Definition: x509.c:57
RZ_API bool rz_x509_parse_extensions(RX509Extensions *ext, RASN1Object *object)
Definition: x509.c:143
RZ_API bool rz_x509_parse_name(RX509Name *name, RASN1Object *object)
Definition: x509.c:79
static bool rz_x509_parse_validity(RX509Validity *validity, RASN1Object *object)
Definition: x509.c:10

References CLASS_CONTEXT, CLASS_UNIVERSAL, rz_x509_tbscertificate_t::extensions, rz_asn1_object_t::form, FORM_CONSTRUCTED, i, rz_x509_tbscertificate_t::issuer, rz_x509_tbscertificate_t::issuerUniqueID, rz_asn1_object_t::klass, rz_asn1_list_t::length, rz_asn1_object_t::length, length, rz_asn1_object_t::list, list(), rz_asn1_list_t::objects, rz_asn1_create_binary(), rz_asn1_stringify_integer(), rz_x509_parse_algorithmidentifier(), rz_x509_parse_extensions(), rz_x509_parse_name(), rz_x509_parse_subjectpublickeyinfo(), rz_x509_parse_validity(), rz_asn1_object_t::sector, rz_x509_tbscertificate_t::serialNumber, shift(), rz_x509_tbscertificate_t::signature, rz_x509_tbscertificate_t::subject, rz_x509_tbscertificate_t::subjectPublicKeyInfo, rz_x509_tbscertificate_t::subjectUniqueID, rz_asn1_object_t::tag, test-lz4-versions::tag, TAG_INTEGER, rz_x509_tbscertificate_t::validity, and rz_x509_tbscertificate_t::version.

Referenced by rz_x509_parse_certificate().