// File: /workspace/github_c_makefile_star2_O0/fm4dd[P]learn-ssl/crldisplay.c
// Function: main
int main() {
  const char    crl_filestr[] = "./demo/webcert-crl.der";
  BIO                 *crlbio = NULL;
  BIO                 *outbio = NULL;
  X509_CRL            *mycrl  = NULL;
  X509_NAME           *issuer = NULL;
  STACK_OF(X509_REVOKED) *rev = NULL;
  X509_REVOKED     *rev_entry = NULL;
  X509_EXTENSION      *reason = NULL;
  const ASN1_BIT_STRING *asn1_sig = NULL;
  const X509_ALGOR      *sig_type = NULL;
  size_t                sig_bytes = 0;
  char         sig_type_str[1024] = "";
  const ASN1_TIME *last_update, *next_update;
  const ASN1_INTEGER *ser = NULL;
  BIGNUM *bn = NULL;
  int i, extnum, revnum;
  long version;

  /* ---------------------------------------------------------- *
   * Create the Input/Output BIO's.                             *
   * ---------------------------------------------------------- */
  crlbio = BIO_new(BIO_s_file());
  outbio = BIO_new(BIO_s_file());
  outbio = BIO_new_fp(stdout, BIO_NOCLOSE);

  /* ---------------------------------------------------------- *
   * Load the certificate revocation list from file (DER).      *
   * ---------------------------------------------------------- */
  if (BIO_read_filename(crlbio, crl_filestr) <= 0) {
    BIO_printf(outbio, "Error loading CRL file into memory: %s\n", crl_filestr);
    exit(1);
  }

  if ((mycrl = X509_CRL_new()) == NULL) {
    BIO_printf(outbio, "Error creating a new CRL object\n");
    exit(1);
  }
  BIO_printf(outbio, "Successful creation of a new empty CRL object\n");

  if((mycrl = d2i_X509_CRL_bio(crlbio, NULL)) == NULL) {
    BIO_printf(outbio, "Error loading CRL object data.\n");
    exit(1);
  }
  BIO_printf(outbio, "CRL %s converted to internal BIO object\n", crl_filestr);

  /* ---------------------------------------------------------- *
   * Print the CRL Version Number                               *
   * ---------------------------------------------------------- */
  version = X509_CRL_get_version(mycrl);
  BIO_printf(outbio, "This CRL Version Num: %lu (0x%lx)\n", version+1, version);

  /* ---------------------------------------------------------- *
   * Print the CRL Issuer Information                           *
   * ---------------------------------------------------------- */
  issuer = X509_NAME_new();
  issuer = X509_CRL_get_issuer(mycrl);
  BIO_printf(outbio, "This CRL Issuer Info: ");
  X509_NAME_print_ex(outbio, issuer, 0, XN_FLAG_ONELINE);
  BIO_printf(outbio, "\n");

  /* ---------------------------------------------------------- *
   * Print the CRL Issue Date and Time                          *
   * ---------------------------------------------------------- */
  if ((last_update = X509_CRL_get0_lastUpdate(mycrl))) {
    BIO_printf(outbio, "This CRL Release Date: ");
    ASN1_TIME_print(outbio, last_update);
    BIO_printf(outbio, "\n");
  }

  /* ---------------------------------------------------------- *
   * Print the CRL Next Release Date and Time                   *
   * ---------------------------------------------------------- */
  if ((next_update = X509_CRL_get0_nextUpdate(mycrl))) {
    BIO_printf(outbio, "Next CRL Release Date: ");
    ASN1_TIME_print(outbio, next_update);
    BIO_printf(outbio, "\n");
  }

  /* ---------------------------------------------------------- *
   * Print the CRL Signature Algorithm                          *
   * ---------------------------------------------------------- */
  X509_CRL_get0_signature(mycrl, &asn1_sig, &sig_type);
  sig_bytes = asn1_sig->length;
  OBJ_obj2txt(sig_type_str, sizeof(sig_type_str), sig_type->algorithm, 0);

  BIO_printf(outbio, " CRL Signature Format: %s\n", sig_type_str);
  BIO_printf(outbio, " CRL Signature Length: %d Bytes\n", (int) sig_bytes);

  /* ---------------------------------------------------------- *
   * Print the Number of CRL Extensions (CRL may not have any)  *
   * ---------------------------------------------------------- */
  extnum = X509_CRL_get_ext_count(mycrl);
  BIO_printf(outbio, " Number of Extensions: %d\n", extnum);

  /* ---------------------------------------------------------- *
   * Print the Number of revoked Certs (CRL may not have any)   *
   * ---------------------------------------------------------- */
  rev = X509_CRL_get_REVOKED(mycrl);

  revnum = sk_X509_REVOKED_num(rev);
  BIO_printf(outbio, "Found # revoked certs: %d\n", revnum);

  /* ---------------------------------------------------------- *
   * Print the revoked Cert Info if avail. Limit output to 10   *
   * ---------------------------------------------------------- */
  if (revnum > 0 && revnum > 10) revnum = 10;

  for(i=0; i<revnum; i++) {
    rev_entry = sk_X509_REVOKED_value(rev, i);
    if(rev_entry == NULL)
       BIO_printf(outbio, "Error getting rewvoked cert info for: %d\n", i);

    /* ---------------------------------------------------------- *
     * Get cert serial from the revocation entry                  *
     * ---------------------------------------------------------- */
    ser = X509_REVOKED_get0_serialNumber(rev_entry);

    /* ---------------------------------------------------------- *
     * Convert the serial to a hex string                         *
     * ---------------------------------------------------------- */
    char *serialstr = BN_bn2hex(ASN1_INTEGER_to_BN(ser, bn));

    BIO_printf(outbio, "Revocation #: %d S/N: %s", i, serialstr);
    BIO_printf(outbio, " Date: ");
    ASN1_TIME_print(outbio, X509_REVOKED_get0_revocationDate(rev_entry));

    /* ---------------------------------------------------------- *
     * try to get the CRL reason, if the extension exists         *
     * ---------------------------------------------------------- */
    int loc = -1;
    loc = X509_REVOKED_get_ext_by_NID(rev_entry, NID_crl_reason, -1);
    reason = X509_REVOKED_get_ext(rev_entry, loc);
    if (loc > -1) X509V3_EXT_print(outbio, reason, 0, 2);
    BIO_printf(outbio, "\n");
  }

  X509_CRL_free(mycrl);
  BIO_free_all(crlbio);
  BIO_free_all(outbio);
  exit(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4c0, %rsp            # imm = 0x4C0
movl $0x0, -0x4(%rbp)
movq 0xcd7(%rip), %rax       # 0x2010
movq %rax, -0x20(%rbp)
movq 0xcd4(%rip), %rax       # 0x2018
movq %rax, -0x18(%rbp)
movl 0xcd2(%rip), %eax       # 0x2020
movl %eax, -0x10(%rbp)
movw 0xccc(%rip), %ax        # 0x2024
movw %ax, -0xc(%rbp)
movb 0xcc4(%rip), %al        # 0x2026
movb %al, -0xa(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
leaq -0x470(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x10e0
movq $0x0, -0x488(%rbp)
movq $0x0, -0x490(%rbp)
callq 0x1080
movq %rax, %rdi
callq 0x11e0
movq %rax, -0x28(%rbp)
callq 0x1080
movq %rax, %rdi
callq 0x11e0
movq %rax, -0x30(%rbp)
movq 0x2bc9(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
xorl %esi, %esi
callq 0x1110
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
leaq -0x20(%rbp), %rcx
movl $0x6c, %esi
movl $0x3, %edx
callq 0x1050
cmpl $0x0, %eax
jg 0x1451
movq -0x30(%rbp), %rdi
leaq -0x20(%rbp), %rdx
leaq 0xbe7(%rip), %rsi       # 0x2027
movb $0x0, %al
callq 0x1210
movl $0x1, %edi
callq 0x1170
callq 0x1160
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x147c
movq -0x30(%rbp), %rdi
leaq 0xbe4(%rip), %rsi       # 0x204f
movb $0x0, %al
callq 0x1210
movl $0x1, %edi
callq 0x1170
movq -0x30(%rbp), %rdi
leaq 0xbe9(%rip), %rsi       # 0x2070
movb $0x0, %al
callq 0x1210
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1150
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x14c1
movq -0x30(%rbp), %rdi
leaq 0xbef(%rip), %rsi       # 0x209f
movb $0x0, %al
callq 0x1210
movl $0x1, %edi
callq 0x1170
movq -0x30(%rbp), %rdi
leaq -0x20(%rbp), %rdx
leaq 0xbef(%rip), %rsi       # 0x20bf
movb $0x0, %al
callq 0x1210
movq -0x38(%rbp), %rdi
callq 0x10b0
movq %rax, -0x4a8(%rbp)
movq -0x30(%rbp), %rdi
movq -0x4a8(%rbp), %rdx
addq $0x1, %rdx
movq -0x4a8(%rbp), %rcx
leaq 0xbe4(%rip), %rsi       # 0x20e8
movb $0x0, %al
callq 0x1210
callq 0x1130
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0x1140
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
leaq 0xbdf(%rip), %rsi       # 0x210b
movb $0x0, %al
callq 0x1210
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
movl $0x82031f, %ecx         # imm = 0x82031F
callq 0x1040
movq -0x30(%rbp), %rdi
leaq 0xbb7(%rip), %rsi       # 0x2109
movb $0x0, %al
callq 0x1210
movq -0x38(%rbp), %rdi
callq 0x1100
movq %rax, -0x478(%rbp)
cmpq $0x0, %rax
je 0x15a3
movq -0x30(%rbp), %rdi
leaq 0xba8(%rip), %rsi       # 0x2122
movb $0x0, %al
callq 0x1210
movq -0x30(%rbp), %rdi
movq -0x478(%rbp), %rsi
callq 0x1200
movq -0x30(%rbp), %rdi
leaq 0xb6d(%rip), %rsi       # 0x2109
movb $0x0, %al
callq 0x1210
movq -0x38(%rbp), %rdi
callq 0x1090
movq %rax, -0x480(%rbp)
cmpq $0x0, %rax
je 0x15ed
movq -0x30(%rbp), %rdi
leaq 0xb76(%rip), %rsi       # 0x213a
movb $0x0, %al
callq 0x1210
movq -0x30(%rbp), %rdi
movq -0x480(%rbp), %rsi
callq 0x1200
movq -0x30(%rbp), %rdi
leaq 0xb23(%rip), %rsi       # 0x2109
movb $0x0, %al
callq 0x1210
movq -0x38(%rbp), %rdi
leaq -0x60(%rbp), %rsi
leaq -0x68(%rbp), %rdx
callq 0x1060
movq -0x60(%rbp), %rax
movslq (%rax), %rax
movq %rax, -0x70(%rbp)
leaq -0x470(%rbp), %rdi
movq -0x68(%rbp), %rax
movq (%rax), %rdx
movl $0x400, %esi            # imm = 0x400
xorl %ecx, %ecx
callq 0x10f0
movq -0x30(%rbp), %rdi
leaq -0x470(%rbp), %rdx
leaq 0xb1d(%rip), %rsi       # 0x2152
movb $0x0, %al
callq 0x1210
movq -0x30(%rbp), %rdi
movq -0x70(%rbp), %rax
movl %eax, %edx
leaq 0xb20(%rip), %rsi       # 0x216d
movb $0x0, %al
callq 0x1210
movq -0x38(%rbp), %rdi
callq 0x1190
movl %eax, -0x498(%rbp)
movq -0x30(%rbp), %rdi
movl -0x498(%rbp), %edx
leaq 0xb1a(%rip), %rsi       # 0x218e
movb $0x0, %al
callq 0x1210
movq -0x38(%rbp), %rdi
callq 0x1180
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x1850
movq %rax, %rdi
callq 0x10d0
movl %eax, -0x49c(%rbp)
movq -0x30(%rbp), %rdi
movl -0x49c(%rbp), %edx
leaq 0xaf9(%rip), %rsi       # 0x21a9
movb $0x0, %al
callq 0x1210
cmpl $0x0, -0x49c(%rbp)
jle 0x16d3
cmpl $0xa, -0x49c(%rbp)
jle 0x16d3
movl $0xa, -0x49c(%rbp)
movl $0x0, -0x494(%rbp)
movl -0x494(%rbp), %eax
cmpl -0x49c(%rbp), %eax
jge 0x182a
movq -0x48(%rbp), %rdi
callq 0x1850
movq %rax, %rdi
movl -0x494(%rbp), %esi
callq 0x11b0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x1729
movq -0x30(%rbp), %rdi
movl -0x494(%rbp), %edx
leaq 0xaa2(%rip), %rsi       # 0x21c4
movb $0x0, %al
callq 0x1210
movq -0x50(%rbp), %rdi
callq 0x11d0
movq %rax, -0x488(%rbp)
movq -0x488(%rbp), %rdi
movq -0x490(%rbp), %rsi
callq 0x11a0
movq %rax, %rdi
callq 0x11f0
movq %rax, -0x4b0(%rbp)
movq -0x30(%rbp), %rdi
movl -0x494(%rbp), %edx
movq -0x4b0(%rbp), %rcx
leaq 0xa7b(%rip), %rsi       # 0x21ee
movb $0x0, %al
callq 0x1210
movq -0x30(%rbp), %rdi
leaq 0x9ad(%rip), %rsi       # 0x2132
movb $0x0, %al
callq 0x1210
movq -0x30(%rbp), %rax
movq %rax, -0x4c0(%rbp)
movq -0x50(%rbp), %rdi
callq 0x1070
movq -0x4c0(%rbp), %rdi
movq %rax, %rsi
callq 0x1200
movl $0xffffffff, -0x4b4(%rbp) # imm = 0xFFFFFFFF
movq -0x50(%rbp), %rdi
movl $0x8d, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x1120
movl %eax, -0x4b4(%rbp)
movq -0x50(%rbp), %rdi
movl -0x4b4(%rbp), %esi
callq 0x1030
movq %rax, -0x58(%rbp)
cmpl $-0x1, -0x4b4(%rbp)
jle 0x1804
movq -0x30(%rbp), %rdi
movq -0x58(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x2, %ecx
callq 0x11c0
movq -0x30(%rbp), %rdi
leaq 0x8fa(%rip), %rsi       # 0x2109
movb $0x0, %al
callq 0x1210
movl -0x494(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x494(%rbp)
jmp 0x16dd
movq -0x38(%rbp), %rdi
callq 0x10c0
movq -0x28(%rbp), %rdi
callq 0x10a0
movq -0x30(%rbp), %rdi
callq 0x10a0
xorl %edi, %edi
callq 0x1170
nopl (%rax)