Guest User

Untitled

a guest
Apr 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. #include "MSNCrypto.h"
  2.  
  3. struct MSNPMSNG {
  4. unsigned long headerSize;
  5. unsigned long cryptMode;
  6. unsigned long cipherType;
  7. unsigned long hashType;
  8. unsigned long ivLength;
  9. unsigned long hashLength;
  10. unsigned long cipherLength;
  11.  
  12. unsigned char ivBytes[8];
  13. unsigned char hashBytes[20];
  14. unsigned char cipherBytes[72];
  15. };
  16.  
  17. MSNCrypto::MSNCrypto ( QObject *parent )
  18. : QObject ( parent )
  19. {
  20. }
  21.  
  22. QString
  23. MSNCrypto::mbiEncrypt ( QString& ssoKey, QString& nonce )
  24. {
  25. QString magic1 = "WS-SecureConversationSESSION KEY HASH";
  26. QString magic2 = "WS-SecureConversationSESSION KEY ENCRYPTION";
  27. QString key1, key2, key3;
  28. QByteArray hash, des3hash;
  29. QCA::Base64 encoder(QCA::Encode);
  30. QCA::Base64 decoder(QCA::Decode);
  31.  
  32. key1 = decoder.decodeString(ssoKey.toLatin1());
  33. qDebug() << "key1: " << key1;
  34. key2 = deriveKey(key1, magic1);
  35. qDebug() << "key2: " << key2;
  36. key3 = deriveKey(key1, magic2);
  37. qDebug() << "key3: " << key3;
  38.  
  39. hash = createHMACSha1(key2, nonce);
  40. qDebug() << "hash: " << hash;
  41.  
  42. qDebug() << "nonce data: " << nonce;
  43. qDebug() << "nonce len: " << nonce.size();
  44.  
  45. nonce += QByteArray(8, '\x08');
  46.  
  47. qDebug() << "nonce padded data: " << nonce;
  48. qDebug() << "nonce padded data len: " << nonce.size();
  49.  
  50. QCA::InitializationVector iv;
  51. des3hash = createTripleDes(key3, nonce, iv);
  52. qDebug() << "des3hash: " << des3hash;
  53. des3hash += QByteArray(72 - des3hash.size(), '\x00');
  54. qDebug() << "des3hash len: " << des3hash.size();
  55.  
  56.  
  57.  
  58. qDebug() << "sizeof p: " << sizeof(MSNPMSNG);
  59.  
  60. MSNPMSNG blob;
  61. blob.headerSize = 28;
  62. blob.cryptMode = 1;
  63. blob.cipherType = 0x6603;
  64. blob.hashType = 0x8004;
  65. blob.ivLength = 8;
  66. blob.hashLength = 20;
  67. blob.cipherLength = 72;
  68.  
  69.  
  70. for(qint32 i = 0; i < iv.size(); ++i) {
  71. blob.ivBytes[i] = iv[i];
  72. }
  73.  
  74. for(qint32 i = 0; i < hash.size(); ++i) {
  75. blob.hashBytes[i] = hash[i];
  76. }
  77.  
  78. for(qint32 i = 0; i < des3hash.size(); ++i) {
  79. blob.cipherBytes[i] = des3hash[i];
  80. }
  81.  
  82. QByteArray blobs(&blob, 128);
  83. }
  84.  
  85. QString
  86. MSNCrypto::deriveKey ( QString& ssoKey, QString& magic )
  87. {
  88. QByteArray hash1, hash2, hash3, hash4, temp;
  89. QCA::SecureArray mmagic(magic.toLatin1());
  90.  
  91. temp.append(mmagic.toByteArray());
  92. hash1 = createHMACSha1(ssoKey, temp);
  93. temp.clear();
  94. qDebug() << QCA::arrayToHex(hash1);
  95.  
  96. temp.append(hash1);
  97. temp.append(mmagic.toByteArray());
  98. hash2 = createHMACSha1(ssoKey, temp);
  99. temp.clear();
  100. qDebug() << QCA::arrayToHex(hash2);
  101.  
  102. temp.append(hash1);
  103. hash3 = createHMACSha1(ssoKey, temp);
  104. temp.clear();
  105. qDebug() << QCA::arrayToHex(hash3);
  106.  
  107. temp.append(hash3);
  108. temp.append(mmagic.toByteArray());
  109. hash4 = createHMACSha1(ssoKey, temp);
  110. temp.clear();
  111. qDebug() << QCA::arrayToHex(hash4);
  112.  
  113. temp.append(hash2);
  114. temp.append(hash4[0]);
  115. temp.append(hash4[1]);
  116. temp.append(hash3[2]);
  117. temp.append(hash4[3]);
  118. return temp;
  119. }
  120.  
  121. QByteArray
  122. MSNCrypto::createHMACSha1(QString key, QString secret)
  123. {
  124. if(!QCA::isSupported("hmac(sha1)")) {
  125. qFatal("[Error] HMAC Sha1 not supported!");
  126. } else {
  127. QCA::MessageAuthenticationCode hmac("hmac(sha1)", QCA::SecureArray());
  128. QCA::SymmetricKey hmacKey(key.toLatin1());
  129. hmac.setup(hmacKey);
  130. hmac.update(secret.toLatin1());
  131. QByteArray final = hmac.final().toByteArray();
  132. return final;
  133. }
  134. return "";
  135. }
  136.  
  137. QByteArray
  138. MSNCrypto::createTripleDes(QString key, QString secret, QCA::InitializationVector& iv)
  139. {
  140. if(!QCA::isSupported("tripledes-cbc")) {
  141. qFatal("triple des not support");
  142. } else {
  143. QCA::SymmetricKey desKey(key.toLatin1());
  144. QCA::InitializationVector tempIV(8);
  145. iv = tempIV;
  146. QCA::SecureArray data(secret.toLatin1());
  147. QCA::Cipher des3(QString("tripledes"), QCA::Cipher::CBC,
  148. QCA::Cipher::NoPadding, QCA::Encode, desKey, tempIV);
  149. QCA::SecureArray temp = des3.update(data);
  150. QCA::SecureArray hmmm = des3.final();
  151. if(!des3.ok()) {
  152. qFatal("Failed");
  153. }
  154. return temp.toByteArray();
  155. }
  156. return "";
  157. }
Add Comment
Please, Sign In to add comment