]> git.feebdaed.xyz Git - socialize.git/commitdiff
add gencert
authorseantywork <seantywork@gmail.com>
Tue, 25 Feb 2025 14:33:03 +0000 (14:33 +0000)
committerseantywork <seantywork@gmail.com>
Tue, 25 Feb 2025 14:33:03 +0000 (14:33 +0000)
hack/tls.sh
include/socialize/core.h
include/socialize/front/front.h
public/css/common.css [deleted file]
public/index.html
public/js/common.js [deleted file]
public/public/css/common.css [new file with mode: 0644]
public/public/js/common.js [new file with mode: 0644]
src/ctl.c
src/front/front.c
src/utils.c

index 7f1f46e6f9dd08dd523e7f930c26a25404159593..81dbea243ab580939e6679af721a44777164795d 100755 (executable)
@@ -7,7 +7,7 @@ openssl genrsa -out ca_priv.pem 4096
 
 openssl rsa -in ca_priv.pem -outform PEM -pubout -out ca_pub.pem
 
-openssl req -x509 -new -key ca_priv.pem -days 365 -out ca.crt.pem -subj "/CN=testca"
+openssl req -x509 -new -key ca_priv.pem -days 365 -out ca.crt.pem -subj "/CN=socializeca"
 
 
 echo "gen server..."
index 83d1e9e3f00a7bd8f75f5c5cf66f8328ab507226..a26bf6683136659bb48a018b67eac7f50d82a134 100644 (file)
 #define WS_COMMAND_REQ_KEY "reqkey"
 #define WS_COMMAND_ROUNDTRIP "roundtrip"
 
+#define WS_COMMAND_GENCERT "gencert"
+
 #define DEFAULT_RANDLEN 64
 
 
 # define SERVER_CERT "tls/server.crt.pem"
 #endif
 
+#define HUB_CA_NAME "socializeca"
+
 #ifndef HUB_CA_CERT
 # define HUB_CA_CERT "tls/ca.crt.pem"
 #endif
index 9b576f251b94767ff1f809968325d82544b5f7db..85834252e6d42de2097450fedff59be3104f35e3 100644 (file)
@@ -30,7 +30,7 @@ int front_access(struct mg_connection* c, struct mg_ws_message *wm, char* comman
 
 void front_communicate(struct mg_connection* c, struct mg_ws_message *wm, char* command, char* data);
 
-
+int gencert(char* newcert, char* cname);
 
 
 
diff --git a/public/css/common.css b/public/css/common.css
deleted file mode 100644 (file)
index f724ced..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-body {
-    display: flex;
-    min-height: 100vh;
-    flex-direction: column;
-}
-
-main {
-    flex: 1 0 auto;
-}
-
-body {
-    background: #fff;
-}
-
-.input-field input[type=date]:focus + label,
-.input-field input[type=text]:focus + label,
-.input-field input[type=email]:focus + label,
-.input-field input[type=password]:focus + label {
-    color: #e91e63;
-}
-
-.input-field input[type=date]:focus,
-.input-field input[type=text]:focus,
-.input-field input[type=email]:focus,
-.input-field input[type=password]:focus {
-    border-bottom: 2px solid #e91e63;
-    box-shadow: none;
-}
\ No newline at end of file
index a33dedb90e752231dcd08037553eba9dd6c5171f..f8a5f74f85f483db75884d72ff33b33221b8ee82 100644 (file)
                         <div class='col s12'>
                         </div>
                     </div>
-
-                    <div class='row'>
-                        <div class='input-field col s12'>
-                            <input class='validate' type='email' name='email' id='email'/>
-                            <label for='email'>Enter your email</label>
+                    <div id="socialize-admin">
+                        <div class='row'>
+                            <div class='input-field col s12'>
+                                <input class='validate' type='email' name='email' id='email'/>
+                                <label for='email'>Enter your email</label>
+                            </div>
                         </div>
-                    </div>
-
-                    <div class='row'>
-                        <div class='input-field col s12'>
-                            <input class='validate' type='password' name='password' id='password'/>
-                            <label for='password'>Enter your password</label>
+    
+                        <div class='row'>
+                            <div class='input-field col s12'>
+                                <input class='validate' type='password' name='password' id='password'/>
+                                <label for='password'>Enter your password</label>
+                            </div>
+                        </div>
+    
+                        <br/>
+                        <div class='row'>
+                            <input type="button" value="Sign In" onclick="signin()">
+                        
                         </div>
                     </div>
 
-                    <br/>
-                    <div class='row'>
-                        <button type='submit' name='btn_login' class='col s12 btn btn-large waves-effect indigo'>
-                            Login
-                        </button>
-                    </div>
                 </form>
 
 
diff --git a/public/js/common.js b/public/js/common.js
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/public/public/css/common.css b/public/public/css/common.css
new file mode 100644 (file)
index 0000000..f724ced
--- /dev/null
@@ -0,0 +1,28 @@
+body {
+    display: flex;
+    min-height: 100vh;
+    flex-direction: column;
+}
+
+main {
+    flex: 1 0 auto;
+}
+
+body {
+    background: #fff;
+}
+
+.input-field input[type=date]:focus + label,
+.input-field input[type=text]:focus + label,
+.input-field input[type=email]:focus + label,
+.input-field input[type=password]:focus + label {
+    color: #e91e63;
+}
+
+.input-field input[type=date]:focus,
+.input-field input[type=text]:focus,
+.input-field input[type=email]:focus,
+.input-field input[type=password]:focus {
+    border-bottom: 2px solid #e91e63;
+    box-shadow: none;
+}
\ No newline at end of file
diff --git a/public/public/js/common.js b/public/public/js/common.js
new file mode 100644 (file)
index 0000000..35b3eb1
--- /dev/null
@@ -0,0 +1,186 @@
+
+ws = {}
+
+MESSAGE_FORMAT = {
+
+    command: "",
+    data: ""
+
+}
+
+resumeToken = ""
+resumeCount = 0
+resumeThreshold = 10
+
+step = 0
+
+
+let newUI = `
+
+<div class='row'>
+    <div class='input-field col s12'>
+        <input class='validate' name='command' id='command'/>
+        <label for='command'>Enter command</label>
+    </div>
+</div>
+
+<div class='row'>
+    <div class='input-field col s12'>
+        <input class='validate' name='data' id='data'/>
+        <label for='data'>Enter data</label>
+    </div>
+</div>
+
+<br/>
+<div class='row'>
+    <input type="button" value="Send" onclick="sendFromUI()">
+
+
+`
+
+function signin(){
+
+
+    let email = document.getElementById("email").value
+
+    let password = document.getElementById("password").value
+
+    if(email == ""){
+
+        alert("email is empty")
+
+        return 
+    }
+
+    if(password == ""){
+
+        alert("password is empty")
+        
+        return 
+    }
+
+
+    ws = new WebSocket("/front")
+
+
+    let cred = email + ":" + password
+
+    ws.onopen = function(evt){
+
+        ws.send(JSON.stringify({command: "auth", data: cred}))
+    
+    }
+    
+    ws.onclose = function(evt) {
+        alert("connection to server has closed")
+    }
+    
+    ws.onmessage = function(evt) {
+
+        let msg = JSON.parse(evt.data)
+    
+        if (!msg) {
+            alert("failed to parse msg")
+
+            return
+        }
+
+
+        let data = msg.data
+
+        if(step == 0){
+
+            if(msg.status != "success") {
+
+                alert("failed to auth")
+
+                return
+            }
+   
+            resumeToken = data
+
+            
+            sendMessage("auth", data)
+
+            step = 1
+
+            return
+        }
+        
+
+        if(step == 1){
+
+            if(msg.status != "success") {
+
+                if(resumeToken != ""){
+
+                    if(resumeCount > resumeThreshold){
+
+                        alert("failed to connect")
+
+                        return
+                    }
+
+                    sendMessage("auth", resumeToken)
+
+                    resumeCount += 1
+                    return 
+                }
+
+                alert("failed to connect")
+
+                return
+            }
+
+            let adminpage = document.getElementById("socialize-admin")
+
+            adminpage.innerHTML = newUI
+
+            step = 2
+
+            return
+        }
+
+        if(msg.status == "success") {
+
+            alert("got data: \n" + msg.data)
+
+            return
+
+        } else {
+
+            alert("failed: " + msg.status)
+        }
+
+        return
+
+    }
+
+    ws.onerror = function(evt) {
+        console.log("error: " + evt.data)
+    }
+
+}
+
+function sendFromUI(){
+
+    let cmd = document.getElementById("command").value
+
+    let msg = document.getElementById("data").value
+    
+    sendMessage(cmd, msg)
+
+}
+
+function sendMessage(cmd, msg){
+
+    let req = JSON.parse(JSON.stringify(MESSAGE_FORMAT))
+
+    req.command = cmd
+    req.data = msg
+
+    ws.send(JSON.stringify(req))
+
+}
+
index 678e3626f8cd4ba1e1e51b5fb2d6dccac9c836dd..817c6e43d333e9513d99ab12e39a4ea1842d5a8c 100644 (file)
--- a/src/ctl.c
+++ b/src/ctl.c
@@ -205,15 +205,13 @@ int idpw_verify(char* idpw, char *newid, uint8_t* newtoken){
 
     }
 
-    memset(idpw, 0, idpwlen);
-
     int idlen = strlen(id);
 
     strncpy(newid, id, idlen);
 
-    int arrlen = gen_random_bytestream(newtoken, 64);
+    gen_random_bytestream(newtoken, 64);
 
-    bin2hex(newtoken, arrlen, newtoken);
+    bin2hex(newtoken, 64, newtoken);
 
     return 0;
 
index a3ac3a2ac8050ff543b792b1a4013259e2366e03..51cf7cd080b23cf1ac46f15fb22b73c15c09caa3 100644 (file)
@@ -18,6 +18,79 @@ struct mg_mgr mgr;
 int s_sig_num = 0;
 
 
+static BIO* _keygen(int bits){
+
+    RSA *r;
+    BIGNUM *bne;
+    BIO *bp_public;
+    BIO *bp_private;
+
+
+    EVP_PKEY *pkey;
+
+
+       int ret = 0;
+
+       unsigned long e = RSA_F4;
+
+
+       bne = BN_new();
+       ret = BN_set_word(bne,e);
+       if(ret != 1){
+
+        goto FREE_KEYGEN;
+       }
+
+       r = RSA_new();
+       ret = RSA_generate_key_ex(r, bits, bne, NULL);
+       if(ret != 1){
+        goto FREE_KEYGEN;
+       }
+
+    bp_private = BIO_new(BIO_s_mem());
+       ret = PEM_write_bio_RSAPrivateKey(bp_private, r, NULL, NULL, 0, NULL, NULL);
+       if(ret != 1){
+        goto FREE_KEYGEN;
+       }
+
+       bp_public = BIO_new(BIO_s_mem());
+       ret = PEM_write_bio_RSAPublicKey(bp_public, r);
+       if(ret != 1){
+        bp_public = NULL;
+        goto FREE_KEYGEN;
+       }
+
+    goto EXIT_KEYGEN;
+
+FREE_KEYGEN:
+
+    if(bp_public != NULL){
+
+        BIO_free_all(bp_public);
+    }
+
+EXIT_KEYGEN:
+
+    if(bp_private != NULL){
+
+        BIO_free_all(bp_private);
+    }
+
+    if(r != NULL){
+
+        RSA_free(r);
+    }
+       
+    if(bne != NULL){
+
+        BN_free(bne);
+    }
+
+    return bp_public;
+}
+
+
 void sntp_fn(struct mg_connection *c, int ev, void *ev_data) {
   uint64_t *expiration_time = (uint64_t *) c->data;
   if (ev == MG_EV_OPEN) {
@@ -152,10 +225,14 @@ void route(struct mg_connection *c, int ev, void *ev_data) {
 
     } else if (mg_match(hm->uri, mg_str("/front"), NULL)) {
 
-        printf("WS UPGRADE!!!!!\n");
+        printf("ws upgrade init\n");
 
         mg_ws_upgrade(c, hm, NULL);
 
+        printf("ws upgraded\n");
+
+        return;
+
     } else {
 
         struct mg_http_serve_opts opts = {.root_dir = FRONT_WEB_ROOT};
@@ -175,15 +252,11 @@ void route(struct mg_connection *c, int ev, void *ev_data) {
 
   } else if (ev == MG_EV_WS_MSG) {
 
-    struct mg_http_message *hm = (struct mg_http_message *) ev_data;
-
     struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
-    
-    if (mg_match(hm->uri, mg_str("/front"), NULL)) {
 
-        front_handler(c, wm);
 
-    } 
+    front_handler(c, wm);
+
 
   }
 }
@@ -257,6 +330,8 @@ int front_access(struct mg_connection* c, struct mg_ws_message *wm, char* comman
 
     int datalen = 0;
 
+    printf("access handler\n");
+
     if(wm->data.len > MAX_WS_BUFF){
 
         printf("failed handle ws: data too big\n");
@@ -407,6 +482,9 @@ int front_access(struct mg_connection* c, struct mg_ws_message *wm, char* comman
             fmt_logln(LOGFP, "initial auth success");
 
             printf("handle ws: initial auth success\n");
+
+            strcpy(USER.token, token);
+
             cJSON_AddItemToObject(response, "status", cJSON_CreateString("success"));
             cJSON_AddItemToObject(response, "data", cJSON_CreateString((char*)token));
             
@@ -456,17 +534,58 @@ void front_communicate(struct mg_connection* c, struct mg_ws_message *wm, char*
 
     int datalen = 0;
 
+    int result = 0;
+
     fmt_logln(LOGFP, "incoming front communication");
 
-    if (strcmp(command, WS_COMMAND_ROUNDTRIP) == 0) {
+    if (strcmp(command, WS_COMMAND_GENCERT) == 0) {
+
+        fmt_logln(LOGFP, "gencert");
 
-        fmt_logln(LOGFP, "roundtrip");
+        char newcert[MAX_BUFF] = {0};
+
+        result = gencert(newcert, data);
+
+        if(result < 0){
+
+            cJSON_AddItemToObject(response, "status", cJSON_CreateString("failed"));
+            cJSON_AddItemToObject(response, "data", cJSON_CreateString("gencert failed"));
+            
+            strcpy(ws_buff, cJSON_Print(response));
+    
+            datalen = strlen(ws_buff);
+    
+            mg_ws_send(c, ws_buff, datalen, WEBSOCKET_OP_TEXT);
+    
+
+        } else {
+
+            cJSON_AddItemToObject(response, "status", cJSON_CreateString("success"));
+            cJSON_AddItemToObject(response, "data", cJSON_CreateString(newcert));
+            
+            strcpy(ws_buff, cJSON_Print(response));
+    
+            datalen = strlen(ws_buff);
+    
+            mg_ws_send(c, ws_buff, datalen, WEBSOCKET_OP_TEXT);
+    
+
+        }
 
 
     } else {
 
         printf("failed handle ws: no such command\n");
 
+        cJSON_AddItemToObject(response, "status", cJSON_CreateString("failed"));
+        cJSON_AddItemToObject(response, "data", cJSON_CreateString("no such command"));
+        
+        strcpy(ws_buff, cJSON_Print(response));
+
+        datalen = strlen(ws_buff);
+
+        mg_ws_send(c, ws_buff, datalen, WEBSOCKET_OP_TEXT);
+
     }
 
     return;
@@ -474,3 +593,87 @@ void front_communicate(struct mg_connection* c, struct mg_ws_message *wm, char*
 }
 
 
+int gencert(char* newcert, char* cname){
+
+
+    time_t exp_ca;
+    time(&exp_ca);
+    exp_ca += 315360000;
+
+    time_t exp_s;
+    time(&exp_s);
+    exp_s += 31536000;
+
+    X509* x509_s = X509_new();
+
+    EVP_PKEY* pub_key_s = EVP_PKEY_new();
+
+    X509_NAME* ca_name = X509_NAME_new();
+    X509_NAME* s_name = X509_NAME_new();
+    X509_NAME_add_entry_by_txt(ca_name, "CN" , MBSTRING_ASC, HUB_CA_NAME, -1, -1, 0);
+    X509_NAME_add_entry_by_txt(s_name ,"CN" , MBSTRING_ASC, cname, -1, -1, 0);
+
+    char subject_alt_name[MAX_ID_LEN] = {0};
+    
+    sprintf(subject_alt_name, "DNS: %s", cname);
+
+    X509_EXTENSION *extension_san = NULL;
+    ASN1_OCTET_STRING *subject_alt_name_ASN1 = NULL;
+    subject_alt_name_ASN1 = ASN1_OCTET_STRING_new();
+    ASN1_OCTET_STRING_set(subject_alt_name_ASN1, (unsigned char*) subject_alt_name, strlen(subject_alt_name));
+    X509_EXTENSION_create_by_NID(&extension_san, NID_subject_alt_name, 0, subject_alt_name_ASN1);
+
+    BIO* pubkey = _keygen(4096);
+
+    if(pubkey == NULL){
+
+        printf("keygen failed\n");
+
+        return -10;
+    }
+    
+
+    pub_key_s = PEM_read_bio_PUBKEY(pubkey, NULL, NULL, NULL);
+
+    FILE* fp = fopen(HUB_CA_PRIV, "r");
+
+    EVP_PKEY* priv_key_ca = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
+
+    fclose(fp);
+
+
+    if(ASN1_INTEGER_set(X509_get_serialNumber(x509_s), 420) == 0){
+        printf("asn1 set serial number fail\n");
+    }
+
+
+    if(X509_time_adj_ex(X509_getm_notBefore(x509_s), 0, 0, 0) == NULL){
+        printf("set time fail\n");
+    }
+
+    if(X509_time_adj_ex(X509_getm_notAfter(x509_s), 0, 0, &exp_s) == NULL){
+        printf("set end time fail\n");
+    }
+
+    X509_set_issuer_name(x509_s, ca_name);
+    X509_set_subject_name(x509_s, s_name);
+
+    X509_add_ext(x509_s, extension_san, -1);
+
+    //set public key
+    if(X509_set_pubkey(x509_s, pub_key_s) == 0){
+        printf("set pubkey fail\n");
+    }
+
+    //sign certificate with private key
+    if(X509_sign(x509_s, priv_key_ca, EVP_sha256()) == 0){
+        printf("sign fail\n");
+        printf("Creating certificate failed...\n");
+    }
+
+
+    fp = fopen("s.pem", "w");
+    PEM_write_X509(fp, x509_s);
+    fclose(fp);
+}
+
index 88b781fc5468f1180611329cbc37eab40fb7b987..8509963b8023cf3a7caa958ce5935d8edfb16009 100644 (file)
@@ -39,7 +39,7 @@ int gen_random_bytestream(uint8_t* bytes, size_t num_bytes){
 
     if(num_bytes > MAX_PW_LEN){
 
-    return -1;
+        return -1;
 
     }
 
@@ -48,7 +48,7 @@ int gen_random_bytestream(uint8_t* bytes, size_t num_bytes){
 
     for (i = 0; i < num_bytes; i++){
 
-    bytes[i] = rand();
+        bytes[i] = rand();
 
     }
 
@@ -60,7 +60,6 @@ int bin2hex(uint8_t* hexarray, int arrlen, uint8_t* bytearray){
 
     int hexlen = 2;
     
-
     int outstrlen = hexlen * arrlen + 1;
 
     if (outstrlen > MAX_PW_LEN){
@@ -83,6 +82,7 @@ int bin2hex(uint8_t* hexarray, int arrlen, uint8_t* bytearray){
 
     memcpy(hexarray, tmparr, outstrlen);
 
+
     free(tmparr);
 
     return 0;