// File: /workspace/github_c_makefile_star2_O1/fm4dd[P]learn-ssl/certfprint.c
// Function: main
int main() {
  const char cert_filestr[] = "./demo/cert-file.pem";
  BIO              *certbio = NULL;
  BIO               *outbio = NULL;
  X509                *cert = NULL;
  const EVP_MD *fprint_type = NULL;
  int j;
  unsigned int fprint_size;
  unsigned char fprint[EVP_MAX_MD_SIZE];

  /* ---------------------------------------------------------- *
   * 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);
  }

  /* ---------------------------------------------------------- *
   * Set the digest method and calculate the cert fingerprint   *
   * SHA-1 creates a 160bit hash, displayed as a 20 byte string *
   * ---------------------------------------------------------- */
  fprint_type = EVP_sha1();

  if (!X509_digest(cert, fprint_type, fprint, &fprint_size))
    BIO_printf(outbio,"Error creating the certificate fingerprint.\n");

  /* ---------------------------------------------------------- *
   * Print the certificate fingerprint method, length and value *
   * ---------------------------------------------------------- */
  BIO_printf(outbio,"Fingerprint Method: %s\n", 
    OBJ_nid2sn(EVP_MD_type(fprint_type)));

  BIO_printf(outbio,"Fingerprint Length: %d\n", fprint_size);

  /* Microsoft Thumbprint-style: lowercase hex bytes with space */
  BIO_printf(outbio,"Fingerprint String: ");
  for (j=0; j<fprint_size; ++j) BIO_printf(outbio, "%02x ", fprint[j]);
  BIO_printf(outbio,"\n");

  /* OpenSSL fingerprint-style: uppercase hex bytes with colon */
  //for (j=0; j<fprint_size; j++) {
  //  BIO_printf(outbio,"%02X%c", fprint[j], (j+1 == fprint_size) ?'\n':':');
  //}

  X509_free(cert);
  BIO_free_all(certbio);
  BIO_free_all(outbio);
  exit(0);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x70, %rsp
movaps 0xe00(%rip), %xmm0      # 0x2010
leaq 0x10(%rsp), %r15
movaps %xmm0, (%r15)
movabsq $0x6d65702e656c69, %rax # imm = 0x6D65702E656C69
movq %rax, 0xd(%r15)
callq 0x1040
movq %rax, %rdi
callq 0x10d0
movq %rax, %r14
movq 0x2d92(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
xorl %esi, %esi
callq 0x1070
movq %rax, %rbx
movl $0x3, %edx
movq %r14, %rdi
movl $0x6c, %esi
movq %r15, %rcx
callq 0x1030
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10e0
testq %rax, %rax
jne 0x1293
leaq 0xdab(%rip), %rsi       # 0x2025
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10f0
movl $0x1, %edi
callq 0x10a0
movq %rax, %r15
callq 0x1090
movq %rax, %r12
leaq 0x30(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movq %r15, %rdi
movq %rax, %rsi
callq 0x10b0
testl %eax, %eax
jne 0x12c8
leaq 0xd8b(%rip), %rsi       # 0x2049
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10f0
movq %r12, %rdi
callq 0x10c0
movl %eax, %edi
callq 0x1080
leaq 0xd98(%rip), %rsi       # 0x2076
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10f0
movl 0xc(%rsp), %edx
leaq 0xd98(%rip), %rsi       # 0x208e
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10f0
leaq 0xd9f(%rip), %rsi       # 0x20a6
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10f0
cmpl $0x0, 0xc(%rsp)
je 0x1341
leaq 0xd9c(%rip), %r12       # 0x20bb
xorl %r13d, %r13d
movzbl 0x30(%rsp,%r13), %edx
movq %rbx, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x10f0
incq %r13
movl 0xc(%rsp), %eax
cmpq %rax, %r13
jb 0x1322
leaq 0xd2c(%rip), %rsi       # 0x2074
movq %rbx, %rdi
xorl %eax, %eax
callq 0x10f0
movq %r15, %rdi
callq 0x1060
movq %r14, %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x1050
xorl %edi, %edi
callq 0x10a0