// File: /workspace/github_c_makefile_star2_O0/fm4dd[P]learn-ssl/pkcs12test.c
// Function: main
int main() {
   X509		  *cert, *cacert;
   STACK_OF(X509) *cacertstack;
   PKCS12	  *pkcs12bundle;
   EVP_PKEY	  *cert_privkey;
   FILE 	  *cacertfile, *certfile, *keyfile, *pkcs12file;
   int		  ret, bytes = 0;
   struct stat    fstat;

#if OPENSSL_VERSION_NUMBER < 0x30000000L
/* ------------------------------------------------------------ *
 * 1.) These function calls are essential to make PEM_read and  *
 *     other openssl functions work.                            *
 * ------------------------------------------------------------ */
   OpenSSL_add_all_algorithms();
   ERR_load_crypto_strings();
#endif

/*--------------------------------------------------------------*
 * 2.) we load the certificates private key                     *
 *    ( for this test, it has no password )                     *
 *--------------------------------------------------------------*/
   if ((cert_privkey = EVP_PKEY_new()) == NULL)
      printf("Error creating EVP_PKEY structure.\n");
   if (! (keyfile = fopen(CERTKEY, "r"))) {
      printf("Error cant read certificate private key file: %s\n", CERTKEY);
      exit(1);
   }
   if (! (cert_privkey = PEM_read_PrivateKey(keyfile, NULL, NULL, NULL)))
      printf("Error loading certificate private key content.\n");
   fclose(keyfile);

/*--------------------------------------------------------------*
 * 3.) we load the corresponding certificate                    *
 *--------------------------------------------------------------*/
   if (! (certfile = fopen(CERTFILE, "r"))) {
      printf("Error cant read certificate file: %s\n", CERTFILE);
      exit(1);
   }
   if (! (cert = PEM_read_X509(certfile, NULL, NULL, NULL)))
      printf("Error loading cert into memory.\n");
   fclose(certfile);

/*--------------------------------------------------------------*
 * 4.) we load the CA certificate who signed it                 *
 *--------------------------------------------------------------*/
   if (! (cacertfile = fopen(CAFILE, "r"))) {
      printf("Error cant read ca certificate file: %s\n", CAFILE);
      exit(1);
   }
   if (! (cacert = PEM_read_X509(cacertfile,NULL,NULL,NULL)))
      printf("Error loading CA certificate into memory.\n");
   fclose(cacertfile);

/*--------------------------------------------------------------*
 * 5.) we load the CA certificate on the stack                  *
 *--------------------------------------------------------------*/
   if ((cacertstack = sk_X509_new_null()) == NULL)
      printf("Error creating STACK_OF(X509) structure.\n");
   sk_X509_push(cacertstack, cacert);

/*--------------------------------------------------------------*
 * 6.) we create the PKCS12 structure and fill it with our data *
 *--------------------------------------------------------------*/
   if ((pkcs12bundle = PKCS12_new()) == NULL)
      printf("Error creating PKCS12 structure.\n");

   /* values of zero use the openssl default values */
   pkcs12bundle = PKCS12_create(
          "test",      // certbundle access password
          "pkcs12test",// friendly certname
          cert_privkey,// the certificate private key
          cert,        // the main certificate
          cacertstack, // stack of CA cert chain
          0,           // int nid_key (default 3DES)
          0,           // int nid_cert (40bitRC2)
          0,           // int iter (default 2048)
          0,           // int mac_iter (default 1)
          0            // int keytype (default no flag)
   );
   if ( pkcs12bundle == NULL)
      printf("Error generating a valid PKCS12 certificate.\n");

/*--------------------------------------------------------------*
 * 7.) we write the PKCS12 structure out to file                *
 *--------------------------------------------------------------*/
   if (! (pkcs12file = fopen(P12FILE, "w"))) {
      printf("Error cant open pkcs12 file for writing: %s\n", P12FILE);
      exit(1);
   }
   ret = i2d_PKCS12_fp(pkcs12file, pkcs12bundle);
   if (ret == 0) {
      printf("Error writing PKCS12 certificate.\n");
      exit(1);
   }

/*--------------------------------------------------------------*
 * 8.) we are done, let's clean up                              *
 *--------------------------------------------------------------*/
   fclose(pkcs12file);
   X509_free(cert);
   X509_free(cacert);
   sk_X509_free(cacertstack);
   PKCS12_free(pkcs12bundle);

   /* get file status data */
   if (stat(P12FILE, &fstat) != 0) {
      printf("Error cannot stat p12 file: %s\n", P12FILE);
      printf("%s\n", strerror(errno));
      exit(1);
   }
   
   bytes = fstat.st_size;
   printf("Created PKCS12 file %s with %d Bytes.\n", P12FILE, bytes);

   return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movl $0x0, -0x4(%rbp)
movl $0x0, -0x58(%rbp)
callq 0x1130
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x1286
leaq 0xd85(%rip), %rdi       # 0x2004
movb $0x0, %al
callq 0x1040
leaq 0xd9b(%rip), %rdi       # 0x2028
leaq 0xda9(%rip), %rsi       # 0x203d
callq 0x10e0
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
jne 0x12c2
leaq 0xd95(%rip), %rdi       # 0x203f
leaq 0xd77(%rip), %rsi       # 0x2028
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x10f0
movq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x1080
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x12ed
leaq 0xd8b(%rip), %rdi       # 0x2071
movb $0x0, %al
callq 0x1040
movq -0x48(%rbp), %rdi
callq 0x10c0
leaq 0xda4(%rip), %rdi       # 0x20a1
leaq 0xd39(%rip), %rsi       # 0x203d
callq 0x10e0
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jne 0x1332
leaq 0xd9c(%rip), %rdi       # 0x20b6
leaq 0xd80(%rip), %rsi       # 0x20a1
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x10f0
movq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x1100
movq %rax, -0x10(%rbp)
cmpq $0x0, %rax
jne 0x135d
leaq 0xd86(%rip), %rdi       # 0x20dc
movb $0x0, %al
callq 0x1040
movq -0x40(%rbp), %rdi
callq 0x10c0
leaq 0xd90(%rip), %rdi       # 0x20fd
leaq 0xcc9(%rip), %rsi       # 0x203d
callq 0x10e0
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x13a2
leaq 0xd85(%rip), %rdi       # 0x210f
leaq 0xd6c(%rip), %rsi       # 0x20fd
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x10f0
movq -0x38(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x1100
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0x13cd
leaq 0xd72(%rip), %rdi       # 0x2138
movb $0x0, %al
callq 0x1040
movq -0x38(%rbp), %rdi
callq 0x10c0
callq 0x1140
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x13f3
leaq 0xd77(%rip), %rdi       # 0x2163
movb $0x0, %al
callq 0x1040
movq -0x20(%rbp), %rdi
callq 0x15c0
movq %rax, -0xf0(%rbp)
movq -0x18(%rbp), %rdi
callq 0x15d0
movq -0xf0(%rbp), %rdi
movq %rax, %rsi
callq 0x1090
callq 0x1120
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0x1438
leaq 0xd5c(%rip), %rdi       # 0x218d
movb $0x0, %al
callq 0x1040
movq -0x30(%rbp), %rdx
movq -0x10(%rbp), %rcx
movq -0x20(%rbp), %r8
leaq 0xd6a(%rip), %rdi       # 0x21b5
leaq 0xd5d(%rip), %rsi       # 0x21af
xorl %r9d, %r9d
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
callq 0x10d0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x1492
leaq 0xd2f(%rip), %rdi       # 0x21ba
movb $0x0, %al
callq 0x1040
leaq 0xd4f(%rip), %rdi       # 0x21e8
leaq 0xd58(%rip), %rsi       # 0x21f8
callq 0x10e0
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
jne 0x14ce
leaq 0xd44(%rip), %rdi       # 0x21fa
leaq 0xd2b(%rip), %rsi       # 0x21e8
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x10f0
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x1110
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jne 0x14fc
leaq 0xd3c(%rip), %rdi       # 0x2227
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x10f0
movq -0x50(%rbp), %rdi
callq 0x10c0
movq -0x10(%rbp), %rdi
callq 0x1060
movq -0x18(%rbp), %rdi
callq 0x1060
movq -0x20(%rbp), %rdi
callq 0x15c0
movq %rax, %rdi
callq 0x10b0
movq -0x28(%rbp), %rdi
callq 0x1070
leaq 0xcb0(%rip), %rdi       # 0x21e8
leaq -0xe8(%rbp), %rsi
callq 0x10a0
cmpl $0x0, %eax
je 0x1585
leaq 0xcfa(%rip), %rdi       # 0x224a
leaq 0xc91(%rip), %rsi       # 0x21e8
movb $0x0, %al
callq 0x1040
callq 0x1030
movl (%rax), %edi
callq 0x1050
movq %rax, %rsi
leaq 0xcf2(%rip), %rdi       # 0x2266
movb $0x0, %al
callq 0x1040
movl $0x1, %edi
callq 0x10f0
movq -0xb8(%rbp), %rax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %edx
leaq 0xcd1(%rip), %rdi       # 0x226a
leaq 0xc48(%rip), %rsi       # 0x21e8
movb $0x0, %al
callq 0x1040
xorl %eax, %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)