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

Go to the source code of this file.

Classes

struct  rz_x509_validity_t
 
struct  rz_x509_name_t
 
struct  rz_x509_algorithmidentifier_t
 
struct  rz_x509_authoritykeyidentifier_t
 
struct  rz_x509_subjectpublickeyinfo_t
 
struct  rz_x509_extension_t
 
struct  rz_x509_extensions_t
 
struct  rz_x509_tbscertificate_t
 
struct  rz_x509_certificate_t
 
struct  rz_x509_crlentry
 
struct  rz_x509_certificaterevocationlist
 

Typedefs

typedef struct rz_x509_validity_t RX509Validity
 
typedef struct rz_x509_name_t RX509Name
 
typedef struct rz_x509_algorithmidentifier_t RX509AlgorithmIdentifier
 
typedef struct rz_x509_authoritykeyidentifier_t RX509AuthorityKeyIdentifier
 
typedef struct rz_x509_subjectpublickeyinfo_t RX509SubjectPublicKeyInfo
 
typedef struct rz_x509_extension_t RX509Extension
 
typedef struct rz_x509_extensions_t RX509Extensions
 
typedef struct rz_x509_tbscertificate_t RX509TBSCertificate
 
typedef struct rz_x509_certificate_t RX509Certificate
 
typedef struct rz_x509_crlentry RX509CRLEntry
 
typedef struct rz_x509_certificaterevocationlist RX509CertificateRevocationList
 

Functions

RZ_API RX509CertificateRevocationListrz_x509_parse_crl (RASN1Object *object)
 
RZ_API char * rz_x509_crl_to_string (RX509CertificateRevocationList *crl, const char *pad)
 
RZ_API void rz_x509_crl_json (PJ *pj, RX509CertificateRevocationList *crl)
 
RZ_API RX509Certificaterz_x509_parse_certificate (RASN1Object *object)
 
RZ_API RX509Certificaterz_x509_parse_certificate2 (const ut8 *buffer, ut32 length)
 
RZ_API void rz_x509_free_certificate (RX509Certificate *certificate)
 
RZ_API void rz_x509_certificate_json (PJ *pj, RX509Certificate *certificate)
 
RZ_API void rz_x509_certificate_dump (RX509Certificate *cert, const char *pad, RzStrBuf *sb)
 

Typedef Documentation

◆ RX509AlgorithmIdentifier

◆ RX509AuthorityKeyIdentifier

◆ RX509Certificate

◆ RX509CertificateRevocationList

◆ RX509CRLEntry

◆ RX509Extension

◆ RX509Extensions

◆ RX509Name

typedef struct rz_x509_name_t RX509Name

◆ RX509SubjectPublicKeyInfo

◆ RX509TBSCertificate

◆ RX509Validity

Function Documentation

◆ rz_x509_certificate_dump()

RZ_API void rz_x509_certificate_dump ( RX509Certificate cert,
const char *  pad,
RzStrBuf sb 
)

Definition at line 546 of file x509.c.

546  {
547  RASN1String *algo = NULL;
548  char *pad2;
549  if (!cert) {
550  return;
551  }
552  if (!pad) {
553  pad = "";
554  }
555  pad2 = rz_str_newf("%s ", pad);
556  if (!pad2) {
557  return;
558  }
559  rz_strbuf_appendf(sb, "%sTBSCertificate:\n", pad);
561 
562  algo = cert->algorithmIdentifier.algorithm;
563  // signature = rz_asn1_stringify_bytes (certificate->signature->binary, certificate->signature->length);
564  // eprintf ("%sAlgorithm:\n%s%s\n%sSignature: %u bytes\n%s\n",
565  // pad, pad2, algo ? algo->string : "",
566  // pad, certificate->signature->length, signature ? signature->string : "");
567  rz_strbuf_appendf(sb, "%sAlgorithm:\n%s%s\n%sSignature: %u bytes\n",
568  pad, pad2, algo ? algo->string : "", pad, cert->signature->length);
569  free(pad2);
570  // rz_asn1_free_string (signature);
571 }
static SblHeader sb
Definition: bin_mbn.c:26
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static void pad(RzStrBuf *sb, ut32 count)
Definition: protobuf.c:36
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
ut32 length
Definition: rz_asn1.h:75
const char * string
Definition: rz_asn1.h:65
RASN1String * algorithm
Definition: rz_x509.h:27
RX509TBSCertificate tbsCertificate
Definition: rz_x509.h:88
RASN1Binary * signature
Definition: rz_x509.h:90
RX509AlgorithmIdentifier algorithmIdentifier
Definition: rz_x509.h:89
static void rz_x509_tbscertificate_dump(RX509TBSCertificate *tbsc, const char *pad, RzStrBuf *sb)
Definition: x509.c:495

References rz_x509_algorithmidentifier_t::algorithm, rz_x509_certificate_t::algorithmIdentifier, free(), rz_asn1_bin_t::length, NULL, pad(), rz_str_newf(), rz_strbuf_appendf(), rz_x509_tbscertificate_dump(), sb, rz_x509_certificate_t::signature, rz_asn1_string_t::string, and rz_x509_certificate_t::tbsCertificate.

Referenced by cmd_print_fromage(), and rz_pkcs7_cms_to_string().

◆ rz_x509_certificate_json()

RZ_API void rz_x509_certificate_json ( PJ pj,
RX509Certificate certificate 
)

Definition at line 790 of file x509.c.

790  {
791  if (!certificate) {
792  return;
793  }
794  RASN1String *m = NULL;
795  pj_o(pj);
796  pj_k(pj, "TBSCertificate");
797  rz_x509_tbscertificate_json(pj, &certificate->tbsCertificate);
798  if (certificate->algorithmIdentifier.algorithm) {
799  pj_ks(pj, "Algorithm", certificate->algorithmIdentifier.algorithm->string);
800  }
801  if (certificate->signature) {
802  m = rz_asn1_stringify_integer(certificate->signature->binary, certificate->signature->length);
803  if (m) {
804  pj_ks(pj, "Signature", m->string);
805  }
807  }
808  pj_end(pj);
809 }
RZ_API void rz_asn1_free_string(RASN1String *string)
Definition: astr.c:313
RZ_API RASN1String * rz_asn1_stringify_integer(const ut8 *buffer, ut32 length)
Definition: astr.c:185
RZ_API PJ * pj_k(PJ *j, const char *k)
Definition: pj.c:104
RZ_API PJ * pj_end(PJ *j)
Definition: pj.c:87
RZ_API PJ * pj_o(PJ *j)
Definition: pj.c:75
RZ_API PJ * pj_ks(PJ *j, const char *k, const char *v)
Definition: pj.c:170
ut8 * binary
Definition: rz_asn1.h:76
RZ_API void rz_x509_tbscertificate_json(PJ *pj, RX509TBSCertificate *tbsc)
Definition: x509.c:744

References rz_x509_algorithmidentifier_t::algorithm, rz_x509_certificate_t::algorithmIdentifier, rz_asn1_bin_t::binary, rz_asn1_bin_t::length, regress::m, NULL, pj_end(), pj_k(), pj_ks(), pj_o(), rz_asn1_free_string(), rz_asn1_stringify_integer(), rz_x509_tbscertificate_json(), rz_x509_certificate_t::signature, rz_asn1_string_t::string, and rz_x509_certificate_t::tbsCertificate.

Referenced by rz_pkcs7_cms_json().

◆ rz_x509_crl_json()

RZ_API void rz_x509_crl_json ( PJ pj,
RX509CertificateRevocationList crl 
)

Definition at line 719 of file x509.c.

719  {
720  ut32 i;
721  if (crl) {
722  if (crl->signature.algorithm) {
723  pj_ks(pj, "Signature", crl->signature.algorithm->string);
724  }
725  pj_k(pj, "Issuer");
726  pj_o(pj);
727  rz_x509_name_json(pj, &crl->issuer);
728  pj_end(pj);
729  if (crl->lastUpdate) {
730  pj_ks(pj, "LastUpdate", crl->lastUpdate->string);
731  }
732  if (crl->nextUpdate) {
733  pj_ks(pj, "NextUpdate", crl->nextUpdate->string);
734  }
735  pj_k(pj, "RevokedCertificates");
736  pj_a(pj);
737  for (i = 0; i < crl->length; i++) {
739  }
740  pj_end(pj);
741  }
742 }
lzma_index ** i
Definition: index.h:629
uint32_t ut32
RZ_API PJ * pj_a(PJ *j)
Definition: pj.c:81
RX509AlgorithmIdentifier signature
Definition: rz_x509.h:101
RX509CRLEntry ** revokedCertificates
Definition: rz_x509.h:106
RZ_API void rz_x509_crlentry_json(PJ *pj, RX509CRLEntry *crle)
Definition: x509.c:703
RZ_API void rz_x509_name_json(PJ *pj, RX509Name *name)
Definition: x509.c:638

References rz_x509_algorithmidentifier_t::algorithm, i, rz_x509_certificaterevocationlist::issuer, rz_x509_certificaterevocationlist::lastUpdate, rz_x509_certificaterevocationlist::length, rz_x509_certificaterevocationlist::nextUpdate, pj_a(), pj_end(), pj_k(), pj_ks(), pj_o(), rz_x509_certificaterevocationlist::revokedCertificates, rz_x509_crlentry_json(), rz_x509_name_json(), rz_x509_certificaterevocationlist::signature, and rz_asn1_string_t::string.

Referenced by rz_pkcs7_cms_json().

◆ rz_x509_crl_to_string()

RZ_API char* rz_x509_crl_to_string ( RX509CertificateRevocationList crl,
const char *  pad 
)

Definition at line 592 of file x509.c.

592  {
593  RASN1String *algo = NULL, *last = NULL, *next = NULL;
594  ut32 i;
595  char *pad2, *pad3;
596  if (!crl) {
597  return NULL;
598  }
599  if (!pad) {
600  pad = "";
601  }
602  pad3 = rz_str_newf("%s ", pad);
603  if (!pad3) {
604  return NULL;
605  }
606  pad2 = pad3 + 2;
607  algo = crl->signature.algorithm;
608  last = crl->lastUpdate;
609  next = crl->nextUpdate;
610  RzStrBuf *sb = rz_strbuf_new("");
611  rz_strbuf_appendf(sb, "%sCRL:\n%sSignature:\n%s%s\n%sIssuer\n", pad, pad2, pad3,
612  algo ? algo->string : "", pad2);
613  rz_x509_name_dump(&crl->issuer, pad3, sb);
614 
615  rz_strbuf_appendf(sb, "%sLast Update: %s\n%sNext Update: %s\n%sRevoked Certificates:\n",
616  pad2, last ? last->string : "Missing",
617  pad2, next ? next->string : "Missing", pad2);
618 
619  for (i = 0; i < crl->length; i++) {
621  }
622 
623  free(pad3);
624  return rz_strbuf_drain(sb);
625 }
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
void rz_x509_crlentry_dump(RX509CRLEntry *crle, const char *pad, RzStrBuf *sb)
Definition: x509.c:573
RZ_API void rz_x509_name_dump(RX509Name *name, const char *pad, RzStrBuf *sb)
Definition: x509.c:434

References rz_x509_algorithmidentifier_t::algorithm, free(), i, rz_x509_certificaterevocationlist::issuer, rz_x509_certificaterevocationlist::lastUpdate, rz_x509_certificaterevocationlist::length, rz_x509_certificaterevocationlist::nextUpdate, NULL, pad(), rz_x509_certificaterevocationlist::revokedCertificates, rz_str_newf(), rz_strbuf_appendf(), rz_strbuf_drain(), rz_strbuf_new(), rz_x509_crlentry_dump(), rz_x509_name_dump(), sb, rz_x509_certificaterevocationlist::signature, and rz_asn1_string_t::string.

Referenced by rz_pkcs7_cms_to_string().

◆ rz_x509_free_certificate()

RZ_API void rz_x509_free_certificate ( RX509Certificate certificate)

Definition at line 387 of file x509.c.

387  {
388  if (certificate) {
389  rz_asn1_free_binary(certificate->signature);
392  free(certificate);
393  }
394 }
RZ_API void rz_asn1_free_binary(RASN1Binary *string)
Definition: asn1.c:463
RZ_API void rz_x509_free_algorithmidentifier(RX509AlgorithmIdentifier *ai)
Definition: x509.c:306
RZ_API void rz_x509_free_tbscertificate(RX509TBSCertificate *tbsc)
Definition: x509.c:371

References rz_x509_certificate_t::algorithmIdentifier, free(), rz_asn1_free_binary(), rz_x509_free_algorithmidentifier(), rz_x509_free_tbscertificate(), rz_x509_certificate_t::signature, and rz_x509_certificate_t::tbsCertificate.

Referenced by cmd_print_fromage(), and rz_pkcs7_free_extendedcertificatesandcertificates().

◆ rz_x509_parse_certificate()

RZ_API RX509Certificate* rz_x509_parse_certificate ( RASN1Object object)

Definition at line 217 of file x509.c.

217  {
218  if (!object) {
219  return NULL;
220  }
222  if (!cert) {
223  goto fail;
224  }
225  if (object->klass != CLASS_UNIVERSAL || object->form != FORM_CONSTRUCTED || object->list.length != 3) {
226  RZ_FREE(cert);
227  goto fail;
228  }
229  RASN1Object *tmp = object->list.objects[2];
230  if (!tmp) {
231  RZ_FREE(cert);
232  goto fail;
233  }
234  if (tmp->klass != CLASS_UNIVERSAL || tmp->form != FORM_PRIMITIVE || tmp->tag != TAG_BITSTRING) {
235  RZ_FREE(cert);
236  goto fail;
237  }
238  cert->signature = rz_asn1_create_binary(object->list.objects[2]->sector, object->list.objects[2]->length);
240 
242  RZ_FREE(cert);
243  }
244 fail:
245  rz_asn1_free_object(object);
246  return cert;
247 }
RZ_API void rz_asn1_free_object(RASN1Object *object)
Definition: asn1.c:445
#define CLASS_UNIVERSAL
Definition: rz_asn1.h:25
#define FORM_PRIMITIVE
Definition: rz_asn1.h:31
#define TAG_BITSTRING
Definition: rz_asn1.h:38
RZ_API RASN1Binary * rz_asn1_create_binary(const ut8 *buffer, ut32 length)
Definition: asn1.c:152
#define FORM_CONSTRUCTED
Definition: rz_asn1.h:32
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_FREE(x)
Definition: rz_types.h:369
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
#define fail(test)
Definition: tests.h:29
RZ_API bool rz_x509_parse_tbscertificate(RX509TBSCertificate *tbsc, RASN1Object *object)
Definition: x509.c:164
RZ_API bool rz_x509_parse_algorithmidentifier(RX509AlgorithmIdentifier *ai, RASN1Object *object)
Definition: x509.c:44

References rz_x509_certificate_t::algorithmIdentifier, CLASS_UNIVERSAL, fail, rz_asn1_object_t::form, FORM_CONSTRUCTED, FORM_PRIMITIVE, rz_asn1_object_t::klass, rz_asn1_list_t::length, rz_asn1_object_t::length, rz_asn1_object_t::list, NULL, rz_asn1_list_t::objects, rz_asn1_create_binary(), rz_asn1_free_object(), RZ_FREE, RZ_NEW0, rz_x509_parse_algorithmidentifier(), rz_x509_parse_tbscertificate(), rz_asn1_object_t::sector, rz_x509_certificate_t::signature, TAG_BITSTRING, rz_x509_certificate_t::tbsCertificate, and autogen_x86imm::tmp.

Referenced by cmd_print_fromage(), rz_pkcs7_parse_extendedcertificatesandcertificates(), and rz_x509_parse_certificate2().

◆ rz_x509_parse_certificate2()

RZ_API RX509Certificate* rz_x509_parse_certificate2 ( const ut8 buffer,
ut32  length 
)

Definition at line 249 of file x509.c.

249  {
250  RX509Certificate *certificate;
251  RASN1Object *object;
252  if (!buffer || !length) {
253  return NULL;
254  }
256  certificate = rz_x509_parse_certificate(object);
257  // object freed by rz_x509_parse_certificate
258  return certificate;
259 }
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 RASN1Object * rz_asn1_create_object(const ut8 *buffer, ut32 length, const ut8 *start_pointer)
Definition: asn1.c:120
Definition: buffer.h:15
RZ_API RX509Certificate * rz_x509_parse_certificate(RASN1Object *object)
Definition: x509.c:217

References length, NULL, rz_asn1_create_object(), and rz_x509_parse_certificate().

◆ rz_x509_parse_crl()

RZ_API RX509CertificateRevocationList* rz_x509_parse_crl ( RASN1Object object)

Definition at line 275 of file x509.c.

275  {
277  RASN1Object **elems;
278  if (!object || object->list.length < 4) {
279  return NULL;
280  }
282  if (!crl) {
283  return NULL;
284  }
285  memset(crl, 0, sizeof(RX509CertificateRevocationList));
286  elems = object->list.objects;
288  rz_x509_parse_name(&crl->issuer, elems[1]);
289  crl->lastUpdate = rz_asn1_stringify_utctime(elems[2]->sector, elems[2]->length);
290  crl->nextUpdate = rz_asn1_stringify_utctime(elems[3]->sector, elems[3]->length);
291  if (object->list.length > 4 && object->list.objects[4]) {
292  ut32 i;
293  crl->revokedCertificates = calloc(object->list.objects[4]->list.length, sizeof(RX509CRLEntry *));
294  if (!crl->revokedCertificates) {
295  free(crl);
296  return NULL;
297  }
298  crl->length = object->list.objects[4]->list.length;
299  for (i = 0; i < object->list.objects[4]->list.length; i++) {
301  }
302  }
303  return crl;
304 }
return memset(p, 0, total)
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
RZ_API RASN1String * rz_asn1_stringify_utctime(const ut8 *buffer, ut32 length)
Definition: astr.c:67
RZ_API bool rz_x509_parse_name(RX509Name *name, RASN1Object *object)
Definition: x509.c:79
RZ_API RX509CRLEntry * rz_x509_parse_crlentry(RASN1Object *object)
Definition: x509.c:261

References calloc(), free(), i, rz_x509_certificaterevocationlist::issuer, rz_x509_certificaterevocationlist::lastUpdate, rz_asn1_list_t::length, rz_x509_certificaterevocationlist::length, length, rz_asn1_object_t::list, malloc(), memset(), rz_x509_certificaterevocationlist::nextUpdate, NULL, rz_asn1_list_t::objects, rz_x509_certificaterevocationlist::revokedCertificates, rz_asn1_stringify_utctime(), rz_x509_parse_algorithmidentifier(), rz_x509_parse_crlentry(), rz_x509_parse_name(), and rz_x509_certificaterevocationlist::signature.

Referenced by rz_pkcs7_parse_certificaterevocationlists().