// File: /workspace/github_c_makefile_star2_O0/fm4dd[P]learn-ssl/certsignature.c
// Function: main
int main() {

  const char       cert_filestr[] = "./demo/cert-file.pem";
  const ASN1_BIT_STRING *asn1_sig = NULL;
  const X509_ALGOR      *sig_type = NULL;
  size_t                sig_bytes = 0;
  BIO                    *certbio = NULL;
  BIO                     *outbio = NULL;
  X509                      *cert = NULL;

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

  /* ---------------------------------------------------------- *
   * Load the certificate from file (PEM).                      *
   * ---------------------------------------------------------- */
  BIO_read_filename(certbio, cert_filestr);
  if (! (cert = PEM_read_bio_X509(certbio, NULL, 0, NULL))) {
    BIO_printf(outbio, "Error loading cert into memory: %s\n", cert_filestr);
    exit(1);
  }

  /* ---------------------------------------------------------- *
   * Extract the certificate's signature data.                  *
   * ---------------------------------------------------------- */
  X509_get0_signature(&asn1_sig, &sig_type, cert);
  //sig_type = cert->sig_alg;
  //asn1_sig = cert->signature;
  sig_bytes = asn1_sig->length;

  /* ---------------------------------------------------------- *
   * Print the signature type here                              *
   * ---------------------------------------------------------- */
  BIO_printf(outbio, "Signature Algorithm:\n");
  if (i2a_ASN1_OBJECT(outbio, sig_type->algorithm) <= 0)
    BIO_printf(outbio, "Error getting the signature algorithm.\n");
  else BIO_puts(outbio, "\n\n");

  /* ---------------------------------------------------------- *
   * Print the signature length here                            *
   * ---------------------------------------------------------- */
  BIO_printf(outbio, "Signature Length:\n%ld Bytes\n\n", sig_bytes);

  /* ---------------------------------------------------------- *
   * Print the signature data here                              *
   * ---------------------------------------------------------- */
  BIO_printf(outbio, "Signature Data:");
  if (X509_signature_dump(outbio, asn1_sig, 0) != 1)
    BIO_printf(outbio, "Error printing the signature data\n"); 

  /* ---------------------------------------------------------- *
   * Free up all structures                                     *
   * ---------------------------------------------------------- */
  X509_free(cert);
  BIO_free_all(certbio);
  BIO_free_all(outbio);
  exit(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movl $0x0, -0x4(%rbp)
movq 0xdea(%rip), %rax       # 0x2010
movq %rax, -0x20(%rbp)
movq 0xde7(%rip), %rax       # 0x2018
movq %rax, -0x18(%rbp)
movl 0xde5(%rip), %eax       # 0x2020
movl %eax, -0x10(%rbp)
movb 0xde0(%rip), %al        # 0x2024
movb %al, -0xc(%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)
callq 0x1040
movq %rax, %rdi
callq 0x10d0
movq %rax, -0x40(%rbp)
movq 0x2d41(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
xorl %esi, %esi
callq 0x1080
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x20(%rbp), %rcx
movl $0x6c, %esi
movl $0x3, %edx
callq 0x1030
movq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x10e0
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
jne 0x12f1
movq -0x48(%rbp), %rdi
leaq -0x20(%rbp), %rdx
leaq 0xd45(%rip), %rsi       # 0x2025
movb $0x0, %al
callq 0x1100
movl $0x1, %edi
callq 0x10a0
movq -0x50(%rbp), %rdx
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x10c0
movq -0x28(%rbp), %rax
movslq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rdi
leaq 0xd31(%rip), %rsi       # 0x2049
movb $0x0, %al
callq 0x1100
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
callq 0x1070
cmpl $0x0, %eax
jg 0x1348
movq -0x48(%rbp), %rdi
leaq 0xd20(%rip), %rsi       # 0x205f
movb $0x0, %al
callq 0x1100
jmp 0x1358
movq -0x48(%rbp), %rdi
leaq 0xd4f(%rip), %rsi       # 0x20a2
callq 0x10b0
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0xd20(%rip), %rsi       # 0x2087
movb $0x0, %al
callq 0x1100
movq -0x48(%rbp), %rdi
leaq 0xd2c(%rip), %rsi       # 0x20a5
movb $0x0, %al
callq 0x1100
movq -0x48(%rbp), %rdi
movq -0x28(%rbp), %rsi
xorl %edx, %edx
callq 0x13d0
cmpl $0x1, %eax
je 0x13a6
movq -0x48(%rbp), %rdi
leaq 0xd16(%rip), %rsi       # 0x20b5
movb $0x0, %al
callq 0x1100
movq -0x50(%rbp), %rdi
callq 0x1060
movq -0x40(%rbp), %rdi
callq 0x1050
movq -0x48(%rbp), %rdi
callq 0x1050
xorl %edi, %edi
callq 0x10a0
nopl (%rax,%rax)