]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - crypto.c
Kill more unreachable code.
[xonotic/darkplaces.git] / crypto.c
index d0ac18306443dc291099fc0d49ef6b4c95868833..8428c6784c483e44e6070b286e334734e9490ec2 100644 (file)
--- a/crypto.c
+++ b/crypto.c
@@ -1,4 +1,3 @@
-// TODO key loading, generating, saving
 #include "quakedef.h"
 #include "crypto.h"
 #include "common.h"
@@ -91,7 +90,7 @@ static size_t Crypto_UnParsePack(char *buf, size_t len, unsigned long header, co
        {
                if(pos + 4 + lumpsize[i] > len)
                        return 0;
-               Crypto_UnLittleLong(&buf[pos], lumpsize[i]);
+               Crypto_UnLittleLong(&buf[pos], (unsigned long)lumpsize[i]);
                pos += 4;
                memcpy(&buf[pos], lumps[i], lumpsize[i]);
                pos += lumpsize[i];
@@ -102,7 +101,7 @@ static size_t Crypto_UnParsePack(char *buf, size_t len, unsigned long header, co
 
 #define USE_AES
 
-#ifdef CRYPTO_STATIC
+#ifdef LINK_TO_CRYPTO
 
 #include <d0_blind_id/d0_blind_id.h>
 
@@ -147,8 +146,8 @@ static size_t Crypto_UnParsePack(char *buf, size_t len, unsigned long header, co
 #define qd0_blind_id_sign_with_private_id_sign_detached d0_blind_id_sign_with_private_id_sign_detached
 #define qd0_blind_id_setmallocfuncs d0_blind_id_setmallocfuncs
 #define qd0_blind_id_setmutexfuncs d0_blind_id_setmutexfuncs
-#define qd0_blind_id_verify_public_id d0_bind_id_verify_public_id
-#define qd0_blind_id_verify_private_id d0_bind_id_verify_private_id
+#define qd0_blind_id_verify_public_id d0_blind_id_verify_public_id
+#define qd0_blind_id_verify_private_id d0_blind_id_verify_private_id
 
 #else
 
@@ -285,7 +284,7 @@ static void Crypto_CloseLibrary (void)
 
 #endif
 
-#ifdef CRYPTO_RIJNDAEL_STATIC
+#ifdef LINK_TO_CRYPTO_RIJNDAEL
 
 #include <d0_blind_id/d0_rijndael.h>
 
@@ -413,17 +412,12 @@ static const char *GetUntilNul(const char **data, size_t *len)
                *len = 0;
                return NULL;
        }
-       else
-       {
-               n = (p - *data) + 1;
-               *len -= n;
-               *data += n;
-               if(*len == 0)
-                       *data = NULL;
-               return (const char *) data_save;
-       }
-       *data = NULL;
-       return NULL;
+       n = (p - *data) + 1;
+       *len -= n;
+       *data += n;
+       if(*len == 0)
+               *data = NULL;
+       return (const char *) data_save;
 }
 
 // d0pk reading
@@ -534,7 +528,7 @@ static crypto_t *Crypto_ServerFindInstance(lhnetaddress_t *peeraddress, qboolean
        return crypto;
 }
 
-qboolean Crypto_ServerFinishInstance(crypto_t *out, crypto_t *crypto)
+qboolean Crypto_FinishInstance(crypto_t *out, crypto_t *crypto)
 {
        // no check needed here (returned pointers are only used in prefilled fields)
        if(!crypto || !crypto->authenticated)
@@ -545,7 +539,7 @@ qboolean Crypto_ServerFinishInstance(crypto_t *out, crypto_t *crypto)
        }
        CLEAR_CDATA;
        memcpy(out, crypto, sizeof(*out));
-       memset(crypto, 0, sizeof(crypto));
+       memset(crypto, 0, sizeof(*crypto));
        return true;
 }
 
@@ -651,7 +645,7 @@ static void Crypto_StoreHostKey(lhnetaddress_t *peeraddress, const char *keystri
 
                if(idend - idstart == FP64_SIZE && keyend - keystart == FP64_SIZE)
                {
-                       for(keyid = 0; keyid < MAX_PUBKEYS; ++keyid)
+                       for(keyid = MAX_PUBKEYS - 1; keyid >= 0; --keyid)
                                if(pubkeys[keyid])
                                        if(!memcmp(pubkeys_fp64[keyid], keystart, FP64_SIZE))
                                        {
@@ -659,8 +653,7 @@ static void Crypto_StoreHostKey(lhnetaddress_t *peeraddress, const char *keystri
                                                idfp[FP64_SIZE] = 0;
                                                break;
                                        }
-                       if(keyid >= MAX_PUBKEYS)
-                               keyid = -1;
+                       // If this failed, keyid will be -1.
                }
        }
 
@@ -735,7 +728,7 @@ int Crypto_RetrieveLocalKey(int keyid, char *keyfp, size_t keyfplen, char *idfp,
                strlcpy(keyfp, pubkeys_fp64[keyid], keyfplen);
        if(idfp)
                if(pubkeys_havepriv[keyid])
-                       strlcpy(idfp, pubkeys_priv_fp64[keyid], keyfplen);
+                       strlcpy(idfp, pubkeys_priv_fp64[keyid], idfplen);
        if(issigned)
                *issigned = pubkeys_havesig[keyid];
        return 1;
@@ -1040,7 +1033,7 @@ void Crypto_Init(void)
                return;
        }
 
-       Crypto_Rijndael_OpenLibrary(); // if this fails, it's uncritical
+       (void) Crypto_Rijndael_OpenLibrary(); // if this fails, it's uncritical
 
        Crypto_InitHostKeys();
 }
@@ -1075,17 +1068,17 @@ static void Crypto_KeyGen_Finished(int code, size_t length_received, unsigned ch
                return;
        }
 
-       if(keygen_i >= MAX_PUBKEYS || !pubkeys[keygen_i])
+       if(keygen_i < 0)
        {
-               Con_Printf("overflow of keygen_i\n");
-               keygen_i = -1;
+               Con_Printf("Unexpected response from keygen server:\n");
+               Com_HexDumpToConsole(buffer, (int)length_received);
                SV_UnlockThreadMutex();
                return;
        }
-       if(keygen_i < 0)
+       if(keygen_i >= MAX_PUBKEYS || !pubkeys[keygen_i])
        {
-               Con_Printf("Unexpected response from keygen server:\n");
-               Com_HexDumpToConsole(buffer, length_received);
+               Con_Printf("overflow of keygen_i\n");
+               keygen_i = -1;
                SV_UnlockThreadMutex();
                return;
        }
@@ -1098,7 +1091,7 @@ static void Crypto_KeyGen_Finished(int code, size_t length_received, unsigned ch
                else
                {
                        Con_Printf("Invalid response from keygen server:\n");
-                       Com_HexDumpToConsole(buffer, length_received);
+                       Com_HexDumpToConsole(buffer, (int)length_received);
                }
                keygen_i = -1;
                SV_UnlockThreadMutex();
@@ -1306,7 +1299,7 @@ static void Crypto_KeyGen_f(void)
                return;
        }
        buf2l += buf2pos;
-       buf[buf2l] = 0;
+       buf2[buf2l] = 0;
        if(!Curl_Begin_ToMemory(buf2, 0, (unsigned char *) keygen_buf, sizeof(keygen_buf), Crypto_KeyGen_Finished, NULL))
        {
                Con_Printf("curl failed\n");
@@ -1487,15 +1480,15 @@ const void *Crypto_EncryptPacket(crypto_t *crypto, const void *data_src, size_t
                        if(developer_networking.integer)
                        {
                                Con_Print("To be encrypted:\n");
-                               Com_HexDumpToConsole((const unsigned char *) data_src, len_src);
+                               Com_HexDumpToConsole((const unsigned char *) data_src, (int)len_src);
                        }
-                       if(len_src + 32 > len || !HMAC_SHA256_32BYTES(h, (const unsigned char *) data_src, len_src, crypto->dhkey, DHKEY_SIZE))
+                       if(len_src + 32 > len || !HMAC_SHA256_32BYTES(h, (const unsigned char *) data_src, (int)len_src, crypto->dhkey, DHKEY_SIZE))
                        {
                                Con_Printf("Crypto_EncryptPacket failed (not enough space: %d bytes in, %d bytes out)\n", (int) len_src, (int) len);
                                return NULL;
                        }
                        *len_dst = ((len_src + 15) / 16) * 16 + 16; // add 16 for HMAC, then round to 16-size for AES
-                       ((unsigned char *) data_dst)[0] = *len_dst - len_src;
+                       ((unsigned char *) data_dst)[0] = (unsigned char)(*len_dst - len_src);
                        memcpy(((unsigned char *) data_dst)+1, h, 15);
                        aescpy(crypto->dhkey, (const unsigned char *) data_dst, ((unsigned char *) data_dst) + 16, (const unsigned char *) data_src, len_src);
                        //                    IV                                dst                                src                               len
@@ -1503,7 +1496,7 @@ const void *Crypto_EncryptPacket(crypto_t *crypto, const void *data_src, size_t
                else
                {
                        // HMAC packet = 16 bytes HMAC-SHA-256 (truncated to 128 bits), data
-                       if(len_src + 16 > len || !HMAC_SHA256_32BYTES(h, (const unsigned char *) data_src, len_src, crypto->dhkey, DHKEY_SIZE))
+                       if(len_src + 16 > len || !HMAC_SHA256_32BYTES(h, (const unsigned char *) data_src, (int)len_src, crypto->dhkey, DHKEY_SIZE))
                        {
                                Con_Printf("Crypto_EncryptPacket failed (not enough space: %d bytes in, %d bytes out)\n", (int) len_src, (int) len);
                                return NULL;
@@ -1561,7 +1554,7 @@ const void *Crypto_DecryptPacket(crypto_t *crypto, const void *data_src, size_t
                        }
                        seacpy(crypto->dhkey, (unsigned char *) data_src, (unsigned char *) data_dst, ((const unsigned char *) data_src) + 16, *len_dst);
                        //                    IV                          dst                         src                                      len
-                       if(!HMAC_SHA256_32BYTES(h, (const unsigned char *) data_dst, *len_dst, crypto->dhkey, DHKEY_SIZE))
+                       if(!HMAC_SHA256_32BYTES(h, (const unsigned char *) data_dst, (int)*len_dst, crypto->dhkey, DHKEY_SIZE))
                        {
                                Con_Printf("HMAC fail\n");
                                return NULL;
@@ -1574,7 +1567,7 @@ const void *Crypto_DecryptPacket(crypto_t *crypto, const void *data_src, size_t
                        if(developer_networking.integer)
                        {
                                Con_Print("Decrypted:\n");
-                               Com_HexDumpToConsole((const unsigned char *) data_dst, *len_dst);
+                               Com_HexDumpToConsole((const unsigned char *) data_dst, (int)*len_dst);
                        }
                        return data_dst; // no need to copy
                }
@@ -1592,10 +1585,10 @@ const void *Crypto_DecryptPacket(crypto_t *crypto, const void *data_src, size_t
                                return NULL;
                        }
                        //memcpy(data_dst, data_src + 16, *len_dst);
-                       if(!HMAC_SHA256_32BYTES(h, ((const unsigned char *) data_src) + 16, *len_dst, crypto->dhkey, DHKEY_SIZE))
+                       if(!HMAC_SHA256_32BYTES(h, ((const unsigned char *) data_src) + 16, (int)*len_dst, crypto->dhkey, DHKEY_SIZE))
                        {
                                Con_Printf("HMAC fail\n");
-                               Com_HexDumpToConsole((const unsigned char *) data_src, len_src);
+                               Com_HexDumpToConsole((const unsigned char *) data_src, (int)len_src);
                                return NULL;
                        }
 
@@ -1613,14 +1606,14 @@ const void *Crypto_DecryptPacket(crypto_t *crypto, const void *data_src, size_t
                                        if(memcmp((const unsigned char *) data_src, h, 16)) // ignore first byte, used for length
                                        {
                                                Con_Printf("HMAC mismatch\n");
-                                               Com_HexDumpToConsole((const unsigned char *) data_src, len_src);
+                                               Com_HexDumpToConsole((const unsigned char *) data_src, (int)len_src);
                                                return NULL;
                                        }
                                }
                                else
                                {
                                        Con_Printf("HMAC mismatch\n");
-                                       Com_HexDumpToConsole((const unsigned char *) data_src, len_src);
+                                       Com_HexDumpToConsole((const unsigned char *) data_src, (int)len_src);
                                        return NULL;
                                }
                        }
@@ -1764,13 +1757,13 @@ static int Crypto_ServerParsePacket_Internal(const char *data_in, size_t len_in,
                        p = GetUntilNul(&data_in, &len_in);
                        if(p && *p)
                        {
+                               // Find the highest numbered matching key for p.
                                for(i = 0; i < MAX_PUBKEYS; ++i)
                                {
                                        if(pubkeys[i])
                                                if(!strcmp(p, pubkeys_fp64[i]))
                                                        if(pubkeys_havepriv[i])
-                                                               if(serverid < 0)
-                                                                       serverid = i;
+                                                               serverid = i;
                                }
                                if(serverid < 0)
                                        return Crypto_ServerError(data_out, len_out, "Invalid server key", NULL);
@@ -1778,12 +1771,12 @@ static int Crypto_ServerParsePacket_Internal(const char *data_in, size_t len_in,
                        p = GetUntilNul(&data_in, &len_in);
                        if(p && *p)
                        {
+                               // Find the highest numbered matching key for p.
                                for(i = 0; i < MAX_PUBKEYS; ++i)
                                {
                                        if(pubkeys[i])
                                                if(!strcmp(p, pubkeys_fp64[i]))
-                                                       if(clientid < 0)
-                                                               clientid = i;
+                                                       clientid = i;
                                }
                                if(clientid < 0)
                                        return Crypto_ServerError(data_out, len_out, "Invalid client key", NULL);
@@ -2158,7 +2151,7 @@ int Crypto_ClientParsePacket(const char *data_in, size_t len_in, char *data_out,
                int clientid = -1, serverid = -1, wantserverid = -1;
                qboolean server_can_auth = true;
                char wantserver_idfp[FP64_SIZE+1];
-               int wantserver_aeslevel;
+               int wantserver_aeslevel = 0;
 
                // if we have a stored host key for the server, assume serverid to already be selected!
                // (the loop will refuse to overwrite this one then)
@@ -2236,22 +2229,20 @@ int Crypto_ClientParsePacket(const char *data_in, size_t len_in, char *data_out,
                                        break;
                                continue;
                        }
+                       // Find the highest numbered matching key for p.
                        for(i = 0; i < MAX_PUBKEYS; ++i)
                        {
                                if(pubkeys[i])
                                if(!strcmp(p, pubkeys_fp64[i]))
                                {
                                        if(pubkeys_havepriv[i])
-                                               if(clientid < 0)
-                                                       clientid = i;
+                                               clientid = i;
                                        if(server_can_auth)
-                                               if(serverid < 0)
-                                                       if(wantserverid < 0 || i == wantserverid)
-                                                               serverid = i;
+                                               if(wantserverid < 0 || i == wantserverid)
+                                                       serverid = i;
                                }
                        }
-                       if(clientid >= 0 && serverid >= 0)
-                               break;
+                       // Not breaking, as higher keys in the list always have priority.
                }
 
                // if stored host key is not found:
@@ -2260,7 +2251,6 @@ int Crypto_ClientParsePacket(const char *data_in, size_t len_in, char *data_out,
 
                if(serverid >= 0 || clientid >= 0)
                {
-                       // TODO at this point, fill clientside crypto struct!
                        MAKE_CDATA;
                        CDATA->cdata_id = ++cdata_id;
                        CDATA->s = serverid;
@@ -2325,7 +2315,7 @@ int Crypto_ClientParsePacket(const char *data_in, size_t len_in, char *data_out,
                                CDATA->next_step = 1;
                                *len_out = data_out_p - data_out;
                        }
-                       else if(clientid >= 0)
+                       else // if(clientid >= 0) // guaranteed by condition one level outside
                        {
                                // skip over server auth, perform client auth only
                                if(!CDATA->id)
@@ -2349,8 +2339,6 @@ int Crypto_ClientParsePacket(const char *data_in, size_t len_in, char *data_out,
                                data_out_p += *len_out;
                                *len_out = data_out_p - data_out;
                        }
-                       else
-                               *len_out = data_out_p - data_out;
 
                        return CRYPTO_DISCARD;
                }