User Tools

Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
c:x509conv [2019-03-19 22:53]
ziggi
c:x509conv [2020-02-15 00:57] (current)
Line 1: Line 1:
 =====X509 pem pack to crt/key convertor===== =====X509 pem pack to crt/key convertor=====
  
-I wrote it for internal corporate usage. ​+I wrote it in one hour for internal corporate usage. ​
  
   * win32 exe: {{x509conv.zip}}   * win32 exe: {{x509conv.zip}}
Line 14: Line 14:
 <code c x509conv.c>​ <code c x509conv.c>​
 /* /*
- ​* ​conv.c+ ​* ​x509conv.c
  *  *
- * Copyright 2019 Oleg Borodin ​ <​borodin@unix7.org>​+ * Copyright ​2004-2019 Oleg Borodin ​ <​borodin@unix7.org>​
  *  *
  * This program is free software; you can redistribute it and/or modify  * This program is free software; you can redistribute it and/or modify
Line 42: Line 42:
 #include <​openssl/​x509.h>​ #include <​openssl/​x509.h>​
 #include <​unistd.h>​ #include <​unistd.h>​
 +
 +void qexit(int code) {
 +    printf("#​ press enter key to continue\n"​);​
 +    getchar();
 +    exit(code);
 +}
  
 void readwrite_cert(char *ifilename, char *cn, size_t cn_size) { void readwrite_cert(char *ifilename, char *cn, size_t cn_size) {
Line 47: Line 53:
     if (pemfile == NULL) {     if (pemfile == NULL) {
         printf("#​ cannot open pem file %s, exit.\n",​ ifilename);         printf("#​ cannot open pem file %s, exit.\n",​ ifilename);
-        ​sleep(10);​ +        ​qexit(1);
-        exit(1);+
     }     }
  
Line 54: Line 59:
     printf("#​ open pem file %s\n", ifilename);     printf("#​ open pem file %s\n", ifilename);
  
-    if (x509 != NULL) { +    if (x509 == NULL) { 
-        ​char* subj = X509_NAME_oneline(X509_get_subject_name(x509),​ 0, 0); +        printf("# ​cannot find certificate in %s, exit.\n", ​ifilename); 
-        if (subj) { +        qexit(1); 
-            ​printf("# ​found subject: ​%s\n", ​subj); +    }
-            ​OPENSSL_free(subj); +
-        }+
  
-        X509_NAME ​*subject_name ​= X509_get_subject_name(x509);​ +    charsubj X509_NAME_oneline(X509_get_subject_name(x509), 0, 0); 
-        ​int nid_email = OBJ_txt2nid("CN");+    if (subj) { 
 +        ​printf("# found subject: %s\n", subj); 
 +        OPENSSL_free(subj);​ 
 +    }
  
-        X509_NAME_get_text_by_NID(subject_name, nid_email, cn, cn_size); +    X509_NAME *subject_name ​= X509_get_subject_name(x509); 
-        ​printf("# cn is %s\n", cn);+    int nid_email = OBJ_txt2nid("CN");
  
-        const int filename_maxsize = 256; +    X509_NAME_get_text_by_NID(subject_namenid_emailcn, cn_size)
-        char cert_filename[filename_maxsize];​ +    printf("#​ cn is %s\n", cn);
-        memset(cert_filename0sizeof(cert_filename));+
  
-        strcat(cert_filename, cn)+    const int filename_maxsize = 256; 
-        ​strcat(cert_filename, ​"​.crt"​);+    char cert_filename[filename_maxsize]
 +    ​memset(cert_filename, ​0, sizeof(cert_filename));
  
-        FILE* certfile = fopen(cert_filename, ​"​w"​); +    strcat(cert_filename, ​cn); 
-        ​if ​(certfile != NULL) { +    ​strcat(cert_filename, ".crt");
-            PEM_write_X509(certfilex509); +
-            printf("# wrote cert to %s\n", cert_filename); +
-            fclose(certfile);​ +
-        } else {+
  
-        } +    FILE* certfile = fopen(cert_filename,​ "​w"​);​ 
-        ​X509_free(x509);+    if (certfile == NULL) { 
 +        printf("#​ cannot write certificate,​ exit.\n"​);​ 
 +        ​qexit(1);
     }     }
 +    PEM_write_X509(certfile,​ x509);
 +    printf("#​ wrote cert to %s\n", cert_filename);​
 +    fclose(certfile);​
 +
 +    X509_free(x509);​
     fclose(pemfile);​     fclose(pemfile);​
 } }
Line 93: Line 102:
     if (pemfile == NULL) {     if (pemfile == NULL) {
         printf("#​ cannot open pem file %s, exit.\n",​ ifilename);         printf("#​ cannot open pem file %s, exit.\n",​ ifilename);
-        ​sleep(10);​ +        ​qexit(1);
-        exit(1);+
     }     }
  
     RSA *prikey = PEM_read_RSAPrivateKey(pemfile,​ NULL, NULL, NULL);     RSA *prikey = PEM_read_RSAPrivateKey(pemfile,​ NULL, NULL, NULL);
 +    if (prikey == NULL) {
 +        printf("#​ cannot find key in file %s, exit.\n",​ ifilename);
 +        qexit(1);
 +    }
 +
     printf("#​ read pem file %s\n", ifilename);     printf("#​ read pem file %s\n", ifilename);
  
Line 112: Line 125:
         printf("#​ wrote key to %s\n", prikey_filename);​         printf("#​ wrote key to %s\n", prikey_filename);​
     }     }
- 
-    fclose(pemfile);​ 
 } }
  
Line 121: Line 132:
     if(argc == 1) {     if(argc == 1) {
         printf("#​ no command line arguments, baby, I exit.\n"​);​         printf("#​ no command line arguments, baby, I exit.\n"​);​
-        ​sleep(10);​ +        ​qexit(1);
-        exit(1);+
     }     }
  
Line 128: Line 138:
     if (strlen(filename) < 1) {     if (strlen(filename) < 1) {
         printf("#​ zero file name, exit.\n"​);​         printf("#​ zero file name, exit.\n"​);​
-        ​exit(1);+        ​qexit(1);
     }     }
  
Line 138: Line 148:
     readwrite_prikey(filename,​ cn);     readwrite_prikey(filename,​ cn);
  
-    ​sleep(10); +    ​qexit(0);
     return 0;     return 0;
 +
 } }
 </​code>​ </​code>​