Use xoshiro256** to generate pseudo-random numbers.
[tinc] / src / invitation.c
index d615976..f2f4d76 100644 (file)
@@ -136,7 +136,7 @@ char *get_my_hostname() {
 
                if(s >= 0) {
                        send(s, request, sizeof(request) - 1, 0);
-                       int len = recv(s, line, sizeof(line) - 1, MSG_WAITALL);
+                       ssize_t len = recv(s, line, sizeof(line) - 1, MSG_WAITALL);
 
                        if(len > 0) {
                                line[len] = 0;
@@ -170,7 +170,7 @@ char *get_my_hostname() {
        // Check that the hostname is reasonable
        if(hostname) {
                for(char *p = hostname; *p; p++) {
-                       if(isalnum(*p) || *p == '-' || *p == '.' || *p == ':') {
+                       if(isalnum((uint8_t) *p) || *p == '-' || *p == '.' || *p == ':') {
                                continue;
                        }
 
@@ -216,7 +216,7 @@ again:
        }
 
        for(char *p = line; *p; p++) {
-               if(isalnum(*p) || *p == '-' || *p == '.') {
+               if(isalnum((uint8_t) *p) || *p == '-' || *p == '.') {
                        continue;
                }
 
@@ -292,6 +292,7 @@ int cmd_invite(int argc, char *argv[]) {
                return 1;
        }
 
+       free(myname);
        myname = get_my_name(true);
 
        if(!myname) {
@@ -410,7 +411,7 @@ int cmd_invite(int argc, char *argv[]) {
 
                if(!f) {
                        fprintf(stderr, "Could not write %s: %s\n", filename, strerror(errno));
-                       free(key);
+                       ecdsa_free(key);
                        return 1;
                }
 
@@ -419,7 +420,7 @@ int cmd_invite(int argc, char *argv[]) {
                if(!ecdsa_write_pem_private_key(key, f)) {
                        fprintf(stderr, "Could not write ECDSA private key\n");
                        fclose(f);
-                       free(key);
+                       ecdsa_free(key);
                        return 1;
                }
 
@@ -447,25 +448,25 @@ int cmd_invite(int argc, char *argv[]) {
        char hash[64];
        char *fingerprint = ecdsa_get_base64_public_key(key);
        sha512(fingerprint, strlen(fingerprint), hash);
-       b64encode_urlsafe(hash, hash, 18);
+       b64encode_tinc_urlsafe(hash, hash, 18);
 
-       free(key);
+       ecdsa_free(key);
 
        // Create a random cookie for this invitation.
        char cookie[25];
        randomize(cookie, 18);
 
        // Create a filename that doesn't reveal the cookie itself
-       char buf[18 + strlen(fingerprint)];
+       uint8_t buf[18 + strlen(fingerprint)];
        char cookiehash[64];
        memcpy(buf, cookie, 18);
        memcpy(buf + 18, fingerprint, sizeof(buf) - 18);
        sha512(buf, sizeof(buf), cookiehash);
-       b64encode_urlsafe(cookiehash, cookiehash, 18);
+       b64encode_tinc_urlsafe(cookiehash, cookiehash, 18);
 
        free(fingerprint);
 
-       b64encode_urlsafe(cookie, cookie, 18);
+       b64encode_tinc_urlsafe(cookie, cookie, 18);
 
        // Create a file containing the details of the invitation.
        snprintf(filename, sizeof(filename), "%s" SLASH "invitations" SLASH "%s", confbase, cookiehash);
@@ -571,7 +572,7 @@ static char *get_line(const char **data) {
                return NULL;
        }
 
-       if(len && !isprint(**data)) {
+       if(len && !isprint((uint8_t) **data)) {
                abort();
        }
 
@@ -614,7 +615,7 @@ static char *grep(const char *data, const char *var) {
        static char value[1024];
 
        const char *p = data;
-       int varlen = strlen(var);
+       size_t varlen = strlen(var);
 
        // Skip all lines not starting with var
        while(strncasecmp(p, var, varlen) || !strchr(" \t=", p[varlen])) {
@@ -673,7 +674,7 @@ static bool finalize_join(void) {
        }
 
        if(!netname) {
-               netname = grep(data, "NetName");
+               netname = xstrdup(grep(data, "NetName"));
 
                if(netname && !check_netname(netname, true)) {
                        fprintf(stderr, "Unsafe NetName found in invitation!\n");
@@ -708,7 +709,7 @@ make_names:
 
                // Generate a random netname, ask for a better one later.
                ask_netname = true;
-               snprintf(temp_netname, sizeof(temp_netname), "join_%x", rand());
+               snprintf(temp_netname, sizeof(temp_netname), "join_%x", prng(UINT32_MAX));
                netname = temp_netname;
                goto make_names;
        }
@@ -780,7 +781,7 @@ make_names:
                }
 
                // Split line into variable and value
-               int len = strcspn(l, "\t =");
+               size_t len = strcspn(l, "\t =");
                value = l + len;
                value += strspn(value, "\t ");
 
@@ -885,7 +886,7 @@ make_names:
                                continue;
                        }
 
-                       int len = strcspn(l, "\t =");
+                       size_t len = strcspn(l, "\t =");
 
                        if(len == 4 && !strncasecmp(l, "Name", 4)) {
                                value = l + len;
@@ -1086,10 +1087,10 @@ ask_netname:
 static bool invitation_send(void *handle, uint8_t type, const void *vdata, size_t len) {
        (void)handle;
        (void)type;
-       const char *data = vdata;
+       const uint8_t *data = vdata;
 
        while(len) {
-               int result = send(sock, data, len, 0);
+               ssize_t result = send(sock, data, len, 0);
 
                if(result == -1 && sockwouldblock(sockerrno)) {
                        continue;
@@ -1229,7 +1230,7 @@ int cmd_join(int argc, char *argv[]) {
                port = "655";
        }
 
-       if(!b64decode(slash, hash, 24) || !b64decode(slash + 24, cookie, 24)) {
+       if(!b64decode_tinc(slash, hash, 24) || !b64decode_tinc(slash + 24, cookie, 24)) {
                goto invalid;
        }
 
@@ -1247,6 +1248,7 @@ int cmd_join(int argc, char *argv[]) {
 
        if(!ai) {
                free(b64key);
+               ecdsa_free(key);
                return 1;
        }
 
@@ -1261,6 +1263,7 @@ next:
                if(!aip) {
                        freeaddrinfo(ai);
                        free(b64key);
+                       ecdsa_free(key);
                        return 1;
                }
        }
@@ -1285,7 +1288,7 @@ next:
        fprintf(stderr, "Connected to %s port %s...\n", address, port);
 
        // Tell him we have an invitation, and give him our throw-away key.
-       int len = snprintf(line, sizeof(line), "0 ?%s %d.%d\n", b64key, PROT_MAJOR, PROT_MINOR);
+       ssize_t len = snprintf(line, sizeof(line), "0 ?%s %d.%d\n", b64key, PROT_MAJOR, PROT_MINOR);
 
        if(len <= 0 || (size_t)len >= sizeof(line)) {
                abort();
@@ -1319,11 +1322,13 @@ next:
 
        if(sha512(fingerprint, strlen(fingerprint), hishash)) {
                fprintf(stderr, "Could not create digest\n%s\n", line + 2);
+               ecdsa_free(key);
                return 1;
        }
 
        if(memcmp(hishash, hash, 18)) {
                fprintf(stderr, "Peer has an invalid key!\n%s\n", line + 2);
+               ecdsa_free(key);
                return 1;
 
        }
@@ -1331,17 +1336,21 @@ next:
        ecdsa_t *hiskey = ecdsa_set_base64_public_key(fingerprint);
 
        if(!hiskey) {
+               ecdsa_free(key);
                return 1;
        }
 
        // Start an SPTPS session
        if(!sptps_start(&sptps, NULL, true, false, key, hiskey, "tinc invitation", 15, invitation_send, invitation_receive)) {
+               ecdsa_free(hiskey);
+               ecdsa_free(key);
                return 1;
        }
 
        // Feed rest of input buffer to SPTPS
        if(!sptps_receive_data(&sptps, buffer, blen)) {
-               return 1;
+               success = false;
+               goto exit;
        }
 
        while((len = recv(sock, line, sizeof(line), 0))) {
@@ -1361,30 +1370,33 @@ next:
 
 #endif
                        fprintf(stderr, "Error reading data from %s port %s: %s\n", address, port, sockstrerror(sockerrno));
-                       return 1;
+                       success = false;
+                       goto exit;
                }
 
                char *p = line;
 
                while(len) {
-                       int done = sptps_receive_data(&sptps, p, len);
+                       size_t done = sptps_receive_data(&sptps, p, len);
 
                        if(!done) {
-                               return 1;
+                               success = false;
+                               goto exit;
                        }
 
-                       len -= done;
+                       len -= (ssize_t) done;
                        p += done;
                }
        }
 
+exit:
        sptps_stop(&sptps);
        ecdsa_free(hiskey);
        ecdsa_free(key);
        closesocket(sock);
 
        if(!success) {
-               fprintf(stderr, "Connection closed by peer, invitation cancelled.\n");
+               fprintf(stderr, "Invitation cancelled.\n");
                return 1;
        }