void SettingsManager::setSetting( const char *inSettingName, SimpleVector<char *> *inSettingVector ) { char **settingParts = inSettingVector->getElementArray(); char *settingString = join( settingParts, inSettingVector->size(), "\n" ); delete [] settingParts; if( mHashingOn ) { // compute hash char *stringToHash = autoSprintf( "%s%s", settingString, mStaticMembers.mHashSalt ); char *hash = computeSHA1Digest( stringToHash ); delete [] stringToHash; char *hashFileName = getSettingsFileName( inSettingName, "hash" ); FILE *file = fopen( hashFileName, "w" ); delete [] hashFileName; if( file != NULL ) { fprintf( file, "%s", hash ); fclose( file ); } delete [] hash; } FILE *file = getSettingsFile( inSettingName, "w" ); if( file != NULL ) { fprintf( file, "%s", settingString ); fclose( file ); } delete [] settingString; // else do nothing }
// sha1 digest of ascii base-10 tool id and reach concatonated with the // key above static char *computeReachSignature( toolRecord *inRecord ) { char *reachString = autoSprintf( "%d %d %s", inRecord->id, inRecord->reach, reachSignatureKey ); char *sig = computeSHA1Digest( reachString ); delete [] reachString; return sig; }
char * ChannelReceivingThread::processFlags (char *inMessageID, char *inFromAddress, char *inOldFlags, char *outIgnoreUC, char *outDropMessage, char *inLoggerName) { char *flags = stringDuplicate (inOldFlags); *outIgnoreUC = false; *outDropMessage = false; // first, check if message should be signed // addresses starting with PKH are hashes of sender's public key if (stringStartsWith (inFromAddress, "PKH")) { // make sure hash in address matches attatched public key // get public key char *pubKey = getFlagData (flags, "PUBLIC_KEY_"); if (pubKey == NULL) { AppLog::detail (inLoggerName, "Message from a PKH address does not have a PUBLIC_KEY flag." " Dropping."); *outDropMessage = true; return flags; } // get signature char *signedID = getFlagData (flags, "SIGNED_ID_"); if (pubKey == NULL) { AppLog::detail (inLoggerName, "Message from a PKH address does not have a SIGNED_ID flag." " Dropping."); delete[]pubKey; *outDropMessage = true; return flags; } char *trueKeyHash = computeSHA1Digest (pubKey); // skip "PKH" in address to get to key hash char *hashInAddress = &(inFromAddress[3]); int trueHashToAddressHashCompare = strcmp (trueKeyHash, hashInAddress); delete[]trueKeyHash; if (trueHashToAddressHashCompare != 0) { AppLog::detail (inLoggerName, "PUBLIC_KEY hash does not match hash from PKH address." " Dropping."); delete[]pubKey; *outDropMessage = true; return flags; } // we have a pubkey that matches the from address // and we have already extracted the signature above // check the signature char signatureCorrect = CryptoUtils::rsaVerify (pubKey, (unsigned char *) inMessageID, strlen (inMessageID), signedID); delete[]pubKey; delete[]signedID; if (!signatureCorrect) { AppLog::detail (inLoggerName, "Bad signature on message from a PKH address." " Dropping."); *outDropMessage = true; return flags; } } // process a FORWARD flag char *pointerToForwardFlag = strstr (flags, "FORWARD_"); if (pointerToForwardFlag != NULL) { // message has forward flag // extract the hash from the forward flag char *oldForwardFlag = stringDuplicate (pointerToForwardFlag); char *pointerToFlagSeparator = strstr (oldForwardFlag, "|"); if (pointerToFlagSeparator != NULL) { // terminate string at separator pointerToFlagSeparator[0] = '\0'; } // skip FORWARD_ to get to the hash char *pointerToHash = &(oldForwardFlag[strlen ("FORWARD_")]); // re-hash the hash to produce a new hash char *newHash = muteComputeNewForwardHash (pointerToHash); if (newHash != NULL) { // continue forwarding *outIgnoreUC = true; char *newForwardFlag = autoSprintf ("FORWARD_%s", newHash); // replace old flag with new one char *tempFlags = muteRemoveFlag (flags, oldForwardFlag); char *newFlags = muteAddFlag (tempFlags, newForwardFlag); delete[]tempFlags; delete[]flags; delete[]newForwardFlag; flags = newFlags; delete[]newHash; } else { // we're breaking the forward tree *outIgnoreUC = false; // remove the forward flag char *newFlags = muteRemoveFlag (flags, oldForwardFlag); delete[]flags; flags = newFlags; AppLog::detail (inLoggerName, "Breaking the FORWARD tree."); } delete[]oldForwardFlag; } // process a DROP_TTL flag char *pointerToDropTTLFlag = strstr (flags, "DROP_TTL_"); if (pointerToDropTTLFlag != NULL) { // message has drop TTL flag // extract the hash from the dropTTL flag char *oldDropTTLFlag = stringDuplicate (pointerToDropTTLFlag); char *pointerToFlagSeparator = strstr (oldDropTTLFlag, "|"); if (pointerToFlagSeparator != NULL) { // terminate string at separator pointerToFlagSeparator[0] = '\0'; } // skip DROP_TTL_ to get to the TTL value char *pointerToTTLValue = &(oldDropTTLFlag[strlen ("DROP_TTL_")]); int ttlValue; int numRead = sscanf (pointerToTTLValue, "%d", &ttlValue); if (numRead == 1) { ttlValue--; if (ttlValue <= 0) { *outDropMessage = true; AppLog::detail (inLoggerName, "Breaking the DROP_TTL tree, since the TTL reached 0."); } else { char *newDropTTLFlag = autoSprintf ("DROP_TTL_%d", ttlValue); // replace old flag with new one char *tempFlags = muteRemoveFlag (flags, oldDropTTLFlag); char *newFlags = muteAddFlag (tempFlags, newDropTTLFlag); delete[]tempFlags; delete[]flags; delete[]newDropTTLFlag; flags = newFlags; } // ignore the UC in drop mode *outIgnoreUC = true; } delete[]oldDropTTLFlag; } // process a DROP_CHAIN flag char *pointerToDropChainFlag = strstr (flags, "DROP_CHAIN"); if (pointerToDropChainFlag != NULL) { // message has drop chain flag // ignore UCs on all DROP tail messages *outIgnoreUC = true; if (muteShouldDropTailChainMessages ()) { *outDropMessage = true; AppLog::detail (inLoggerName, "Breaking the DROP_CHAIN."); } } return flags; }
SimpleVector<char *> *SettingsManager::getSetting( const char *inSettingName ) { char *fileName = getSettingsFileName( inSettingName ); File *settingsFile = new File( NULL, fileName ); delete [] fileName; char *fileContents = settingsFile->readFileContents(); delete settingsFile; if( fileContents == NULL ) { // return empty vector return new SimpleVector<char *>(); } if( mHashingOn ) { char *hashFileName = getSettingsFileName( inSettingName, "hash" ); File *hashFile = new File( NULL, hashFileName ); delete [] hashFileName; char *savedHash = hashFile->readFileContents(); delete hashFile; if( savedHash == NULL ) { printf( "Hash missing for setting %s\n", inSettingName ); delete [] fileContents; return new SimpleVector<char *>(); } // compute hash char *stringToHash = autoSprintf( "%s%s", fileContents, mStaticMembers.mHashSalt ); char *hash = computeSHA1Digest( stringToHash ); delete [] stringToHash; int difference = strcmp( hash, savedHash ); delete [] hash; delete [] savedHash; if( difference != 0 ) { printf( "Hash mismatch for setting %s\n", inSettingName ); delete [] fileContents; return new SimpleVector<char *>(); } } // else tokenize the file contents SimpleVector<char *> *returnVector = tokenizeString( fileContents ); delete [] fileContents; return returnVector; }