// File: /workspace/github_c_makefile_star2_O2/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 0xa99(%rip), %rdi       # 0x3491
movl %ebp, %esi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2030
movq 0x2cdd(%rip), %rdi      # 0x56e8
callq 0x2230
cmpl %eax, %ebp
jle 0x2a45
pushq $0x16
popq %rbx
movq %r12, %rdi
movl %ebx, %esi
callq 0x2210
pushq $0x16
popq %rdi
callq 0x2110
leaq 0xa87(%rip), %rdi       # 0x34b7
movl %ebx, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2030
leaq 0xa97(%rip), %rdi       # 0x34da
jmp 0x2a50
testl %ebx, %ebx
je 0x2a6d
leaq 0xb73(%rip), %rdi       # 0x35c3
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
movq 0x2c5c(%rip), %rdi      # 0x56d0
leaq 0xa5f(%rip), %rsi       # 0x34da
xorl %ebx, %ebx
movl %ebp, %edx
xorl %eax, %eax
callq 0x22b0
movq 0x2c43(%rip), %r12      # 0x56d0
movslq %r15d, %r13
movq %r13, %rdi
callq 0x2110
leaq 0xa62(%rip), %rsi       # 0x3501
movq %r12, %rdi
movl %ebp, %edx
movq %rax, %rcx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x22b0
cmpl $0x14, %r13d
jne 0x2a59
callq 0x2170
movq %r14, %rdi
callq 0x2190
movq %rax, %r14
movq 0x2c02(%rip), %rdi      # 0x56d0
leaq 0xa60(%rip), %rsi       # 0x3535
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x22b0
movq 0x2beb(%rip), %rdi      # 0x56d0
movl $0x2a40006, %ecx        # imm = 0x2A40006
movq %r14, %rsi
xorl %edx, %edx
callq 0x2040
movq 0x2bd5(%rip), %rdi      # 0x56d0
leaq 0xa7a(%rip), %rsi       # 0x357c
xorl %eax, %eax
callq 0x22b0
jmp 0x2a59