summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-11-22 10:26:01 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-11-22 10:27:06 (GMT)
commita393a4ac64abb24b0fc2cb0e3e154706013d7803 (patch)
tree2ae8c502b0a465ab202e9f38ae9fd7dd176c92ca /Swiften/SASL/SCRAMSHA1ClientAuthenticator.cpp
parent2df5e148de44c6c91c5a28d197b8b2b913ca80d4 (diff)
downloadswift-a393a4ac64abb24b0fc2cb0e3e154706013d7803.zip
swift-a393a4ac64abb24b0fc2cb0e3e154706013d7803.tar.bz2
Stringprep SCRAM-SHA-1 authcid.
Diffstat (limited to 'Swiften/SASL/SCRAMSHA1ClientAuthenticator.cpp')
-rw-r--r--Swiften/SASL/SCRAMSHA1ClientAuthenticator.cpp3
1 files changed, 2 insertions, 1 deletions
diff --git a/Swiften/SASL/SCRAMSHA1ClientAuthenticator.cpp b/Swiften/SASL/SCRAMSHA1ClientAuthenticator.cpp
index 15c8ab6..b0a9f96 100644
--- a/Swiften/SASL/SCRAMSHA1ClientAuthenticator.cpp
+++ b/Swiften/SASL/SCRAMSHA1ClientAuthenticator.cpp
@@ -1,106 +1,107 @@
#include "Swiften/SASL/SCRAMSHA1ClientAuthenticator.h"
#include <cassert>
#include <map>
#include <boost/lexical_cast.hpp>
#include "Swiften/StringCodecs/SHA1.h"
#include "Swiften/StringCodecs/Base64.h"
#include "Swiften/StringCodecs/HMACSHA1.h"
#include "Swiften/StringCodecs/PBKDF2.h"
+#include "Swiften/StringPrep/StringPrep.h"
namespace Swift {
SCRAMSHA1ClientAuthenticator::SCRAMSHA1ClientAuthenticator(const String& nonce) : ClientAuthenticator("SCRAM-SHA-1"), step(Initial), clientnonce(nonce) {
// TODO: Normalize authentication id
// TODO: Normalize getPassword()
}
ByteArray SCRAMSHA1ClientAuthenticator::getResponse() {
if (step == Initial) {
return "n,," + getInitialBareClientMessage();
}
else {
ByteArray saltedPassword = PBKDF2::encode(getPassword(), salt, iterations);
ByteArray clientKey = HMACSHA1::getResult(saltedPassword, "Client Key");
ByteArray storedKey = SHA1::getBinaryHash(clientKey);
ByteArray serverKey = HMACSHA1::getResult(saltedPassword, "Server Key");
ByteArray authMessage = getInitialBareClientMessage() + "," + initialServerMessage + "," + "c=biwsCg==," + "r=" + clientnonce + serverNonce;
ByteArray clientSignature = HMACSHA1::getResult(storedKey, authMessage);
serverSignature = HMACSHA1::getResult(serverKey, authMessage);
ByteArray clientProof = clientKey;
for (unsigned int i = 0; i < clientProof.getSize(); ++i) {
clientProof[i] ^= clientSignature[i];
}
ByteArray result = ByteArray("c=biwsCg==,r=") + clientnonce + serverNonce + ",p=" + Base64::encode(clientProof);
return result;
}
}
bool SCRAMSHA1ClientAuthenticator::setChallenge(const ByteArray& challenge) {
if (step == Initial) {
initialServerMessage = challenge;
// TODO: Check if these values are correct
std::map<char, String> keys = parseMap(String(initialServerMessage.getData(), initialServerMessage.getSize()));
salt = Base64::decode(keys['s']);
String clientServerNonce = keys['r'];
serverNonce = clientServerNonce.getSubstring(clientnonce.getUTF8Size(), clientServerNonce.npos());
iterations = boost::lexical_cast<int>(keys['i'].getUTF8String());
step = Proof;
return true;
}
else {
return challenge == Base64::encode(ByteArray("v=") + Base64::encode(serverSignature));
}
}
std::map<char, String> SCRAMSHA1ClientAuthenticator::parseMap(const String& s) {
// TODO: Do some proper checking here
std::map<char, String> result;
if (s.getUTF8Size() > 0) {
char key;
String value;
size_t i = 0;
bool expectKey = true;
while (i < s.getUTF8Size()) {
if (expectKey) {
key = s[i];
expectKey = false;
i++;
}
else if (s[i] == ',') {
result[key] = value;
value = "";
expectKey = true;
}
else {
value += s[i];
}
i++;
}
result[key] = value;
}
return result;
}
ByteArray SCRAMSHA1ClientAuthenticator::getInitialBareClientMessage() const {
- String authenticationID = getAuthenticationID();
+ String authenticationID = StringPrep::getPrepared(getAuthenticationID(), StringPrep::SASLPrep);
String escapedAuthenticationID;
for (size_t i = 0; i < authenticationID.getUTF8Size(); ++i) {
if (authenticationID[i] == ',') {
escapedAuthenticationID += "=2C";
}
else if (authenticationID[i] == '=') {
escapedAuthenticationID += "=3D";
}
else {
escapedAuthenticationID += authenticationID[i];
}
}
return ByteArray(String("n=" + escapedAuthenticationID + ",r=" + clientnonce));
}
}