// File: /workspace/github_c_makefile_star2_O1/fm4dd[P]learn-ssl/certverify-adv.c
// Function: verify_callback
static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx) {
  X509_NAME    *certissuer = NULL;
  char    buf[4096] = "";
  X509   *err_cert;
  int     err, depth;

  err_cert = X509_STORE_CTX_get_current_cert(ctx);
  err = X509_STORE_CTX_get_error(ctx);
  depth = X509_STORE_CTX_get_error_depth(ctx);

  /* ---------------------------------------------------------- *
   * Show the subject info at certification depth level         *
   * ---------------------------------------------------------- */
  X509_NAME_oneline(X509_get_subject_name(err_cert), buf, 256);
  printf("Verification check depth: %d - %.49s\n", depth, buf);

  /* ---------------------------------------------------------- *
   * Catch a too long certificate chain. The depth limit with   *
   * SSL_CTX_set_verify_depth() is on purpose set to "limit+1". *
   * ---------------------------------------------------------- */
  if (depth > X509_VERIFY_PARAM_get_depth(param)) {
      preverify_ok = 0;
      err = X509_V_ERR_CERT_CHAIN_TOO_LONG;
      X509_STORE_CTX_set_error(ctx, err);
      printf("Verification fail detail: %d - %s\n", err, X509_verify_cert_error_string(err)); 
      printf("Verification result text: %d - FAILED\n", depth);
      return err;
  }

  if (!preverify_ok) {
      BIO_printf(outbio, "Verification result text: %d - FAILED\n", depth);
      BIO_printf(outbio, "Verification fail reason: %d - %s [error code: %d]\n", depth, X509_verify_cert_error_string(err), err); 
  }

  /* ---------------------------------------------------------- *
   * At this point, err contains the last verification error.   *
   * We can use it for something special                        *
   * ---------------------------------------------------------- */
  if (!preverify_ok && (err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY)) {
      certissuer = X509_NAME_new();
      certissuer = X509_get_issuer_name(err_cert);
      BIO_printf(outbio, "\nVerification fail issuer: ---------------start----------------------\n");
      X509_NAME_print_ex(outbio, certissuer, 0, XN_FLAG_MULTILINE);
      BIO_printf(outbio, "\nVerification fail issuer: ----------------End-----------------------\n");
  }

  if (preverify_ok) printf("Verification result text: %d - PASSED\n", depth);
  return preverify_ok;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1008, %rsp           # imm = 0x1008
movq %rsi, %r12
movl %edi, %ebx
movq %rsp, %r13
movl $0x1000, %edx           # imm = 0x1000
movq %r13, %rdi
xorl %esi, %esi
callq 0x20b0
movq %r12, %rdi
callq 0x2250
movq %rax, %r14
movq %r12, %rdi
callq 0x2130
movl %eax, %r15d
movq %r12, %rdi
callq 0x2100
movl %eax, %ebp
movq %r14, %rdi
callq 0x21a0
movq %rax, %rdi
movq %r13, %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x2120
leaq 0x12f5(%rip), %rdi      # 0x3ddd
movl %ebp, %esi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2030
movq 0x3bed(%rip), %rdi      # 0x66e8
callq 0x2230
cmpl %eax, %ebp
jle 0x2b42
movl $0x16, %ebx
movq %r12, %rdi
movl $0x16, %esi
callq 0x2210
movl $0x16, %edi
callq 0x2110
leaq 0x12dc(%rip), %rdi      # 0x3e03
movl $0x16, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2030
leaq 0x12e9(%rip), %rdi      # 0x3e26
jmp 0x2be9
testl %ebx, %ebx
jne 0x2b85
movq 0x3b83(%rip), %rdi      # 0x66d0
leaq 0x12d2(%rip), %rsi      # 0x3e26
movl %ebp, %edx
xorl %eax, %eax
callq 0x22b0
movq 0x3b6c(%rip), %r12      # 0x66d0
movslq %r15d, %rdi
callq 0x2110
leaq 0x12da(%rip), %rsi      # 0x3e4d
movq %r12, %rdi
movl %ebp, %edx
movq %rax, %rcx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x22b0
xorl $0x14, %r15d
orl %ebx, %r15d
jne 0x2bde
callq 0x2170
movq %r14, %rdi
callq 0x2190
movq %rax, %r14
movq 0x3b2b(%rip), %rdi      # 0x66d0
leaq 0x12d5(%rip), %rsi      # 0x3e81
xorl %eax, %eax
callq 0x22b0
movq 0x3b16(%rip), %rdi      # 0x66d0
movl $0x2a40006, %ecx        # imm = 0x2A40006
movq %r14, %rsi
xorl %edx, %edx
callq 0x2040
movq 0x3b00(%rip), %rdi      # 0x66d0
leaq 0x12f1(%rip), %rsi      # 0x3ec8
xorl %eax, %eax
callq 0x22b0
testl %ebx, %ebx
je 0x2bf2
leaq 0x1326(%rip), %rdi      # 0x3f0f
movl %ebp, %esi
xorl %eax, %eax
callq 0x2030
movl %ebx, %eax
addq $0x1008, %rsp           # imm = 0x1008
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq