// Generate random number in a specified range int GetRandom(int from, int to) { if ((to - from) < 1) return from; unsigned randnum; Utils_GetRandom(&randnum, sizeof(randnum)); return ((randnum % (to - from + 1)) + from); }
char* TScramAuth::getInitialRequest() { T2Utf uname(info->conn.username); unsigned char nonce[24]; Utils_GetRandom(nonce, sizeof(nonce)); cnonce = mir_base64_encode((PBYTE)nonce, sizeof(nonce)); char buf[4096]; int cbLen = mir_snprintf(buf, "n,,n=%s,r=%s", uname, cnonce); msg1 = mir_strdup(buf + 3); return mir_base64_encode((PBYTE)buf, cbLen); }
file_transfer::file_transfer(MCONTACT hCont, char* nick, char* cookie) { memset(this, 0, sizeof(*this)); pfts.cbSize = sizeof(pfts); pfts.flags = PFTS_TCHAR; pfts.hContact = hCont; hContact = hCont; sn = mir_strdup(nick); if (cookie) memcpy(icbm_cookie, cookie, 8); else Utils_GetRandom(icbm_cookie, 8); hResumeEvent = CreateEvent(NULL, FALSE, FALSE, NULL); }
int CAimProto::aim_send_message(HANDLE hServerConn, unsigned short &seqno, const char* sn, char* amsg, bool auto_response, bool blast) { aimString str(amsg); const char* msg = str.getBuf(); const unsigned short msg_len = str.getSize(); unsigned short tlv_offset = 0; char* tlv_buf = (char*)alloca(5 + msg_len + 8); char icbm_cookie[8]; Utils_GetRandom(icbm_cookie, sizeof(icbm_cookie)); aim_writegeneric(5, "\x05\x01\x00\x01\x01", tlv_offset, tlv_buf); // icbm im capabilities aim_writeshort(0x0101, tlv_offset, tlv_buf); // icbm im text tag aim_writeshort(msg_len + 4, tlv_offset, tlv_buf); // icbm im text tag length aim_writeshort(str.isUnicode() ? 2 : 0, tlv_offset, tlv_buf); // character set aim_writeshort(0, tlv_offset, tlv_buf); // language aim_writegeneric(msg_len, msg, tlv_offset, tlv_buf); // message text unsigned short offset = 0; unsigned short sn_length = (unsigned short)mir_strlen(sn); char* buf = (char*)alloca(SNAC_SIZE + 8 + 3 + sn_length + TLV_HEADER_SIZE * 3 + tlv_offset); aim_writesnac(0x04, 0x06, offset, buf, get_random()); aim_writegeneric(8, icbm_cookie, offset, buf); // icbm cookie aim_writeshort(0x01, offset, buf); // channel aim_writechar((unsigned char)sn_length, offset, buf); // screen name len aim_writegeneric(sn_length, sn, offset, buf); // screen name aim_writetlv(0x02, tlv_offset, tlv_buf, offset, buf); if (!blast) { if (auto_response) aim_writetlv(0x04, 0, 0, offset, buf); // auto-response message else { aim_writetlv(0x03, 0, 0, offset, buf); // message ack request aim_writetlv(0x06, 0, 0, offset, buf); // offline message storage } } return aim_sendflap(hServerConn, 0x02, offset, buf, seqno) ? 0 : *(int*)icbm_cookie & 0x7fffffff; }
unsigned MSN_GenRandom(void) { unsigned rndnum; Utils_GetRandom(&rndnum, sizeof(rndnum)); return rndnum & 0x7FFFFFFF; }
char* TMD5Auth::getChallenge(const TCHAR *challenge) { if (iCallCount > 0) return NULL; iCallCount++; unsigned resultLen; ptrA text((char*)mir_base64_decode( _T2A(challenge), &resultLen)); TStringPairs pairs(text); const char *realm = pairs["realm"], *nonce = pairs["nonce"]; char cnonce[40], tmpBuf[40]; DWORD digest[4], hash1[4], hash2[4]; mir_md5_state_t ctx; Utils_GetRandom(digest, sizeof(digest)); mir_snprintf(cnonce, _countof(cnonce), "%08x%08x%08x%08x", htonl(digest[0]), htonl(digest[1]), htonl(digest[2]), htonl(digest[3])); T2Utf uname(info->conn.username), passw(info->conn.password); ptrA serv(mir_utf8encode(info->conn.server)); mir_md5_init(&ctx); mir_md5_append(&ctx, (BYTE*)(char*)uname, (int)mir_strlen(uname)); mir_md5_append(&ctx, (BYTE*)":", 1); mir_md5_append(&ctx, (BYTE*)realm, (int)mir_strlen(realm)); mir_md5_append(&ctx, (BYTE*)":", 1); mir_md5_append(&ctx, (BYTE*)(char*)passw, (int)mir_strlen(passw)); mir_md5_finish(&ctx, (BYTE*)hash1); mir_md5_init(&ctx); mir_md5_append(&ctx, (BYTE*)hash1, 16); mir_md5_append(&ctx, (BYTE*)":", 1); mir_md5_append(&ctx, (BYTE*)nonce, (int)mir_strlen(nonce)); mir_md5_append(&ctx, (BYTE*)":", 1); mir_md5_append(&ctx, (BYTE*)cnonce, (int)mir_strlen(cnonce)); mir_md5_finish(&ctx, (BYTE*)hash1); mir_md5_init(&ctx); mir_md5_append(&ctx, (BYTE*)"AUTHENTICATE:xmpp/", 18); mir_md5_append(&ctx, (BYTE*)(char*)serv, (int)mir_strlen(serv)); mir_md5_finish(&ctx, (BYTE*)hash2); mir_md5_init(&ctx); mir_snprintf(tmpBuf, _countof(tmpBuf), "%08x%08x%08x%08x", htonl(hash1[0]), htonl(hash1[1]), htonl(hash1[2]), htonl(hash1[3])); mir_md5_append(&ctx, (BYTE*)tmpBuf, (int)mir_strlen(tmpBuf)); mir_md5_append(&ctx, (BYTE*)":", 1); mir_md5_append(&ctx, (BYTE*)nonce, (int)mir_strlen(nonce)); mir_snprintf(tmpBuf, _countof(tmpBuf), ":%08d:", iCallCount); mir_md5_append(&ctx, (BYTE*)tmpBuf, (int)mir_strlen(tmpBuf)); mir_md5_append(&ctx, (BYTE*)cnonce, (int)mir_strlen(cnonce)); mir_md5_append(&ctx, (BYTE*)":auth:", 6); mir_snprintf(tmpBuf, _countof(tmpBuf), "%08x%08x%08x%08x", htonl(hash2[0]), htonl(hash2[1]), htonl(hash2[2]), htonl(hash2[3])); mir_md5_append(&ctx, (BYTE*)tmpBuf, (int)mir_strlen(tmpBuf)); mir_md5_finish(&ctx, (BYTE*)digest); char *buf = (char*)alloca(8000); int cbLen = mir_snprintf(buf, 8000, "username=\"%s\",realm=\"%s\",nonce=\"%s\",cnonce=\"%s\",nc=%08d," "qop=auth,digest-uri=\"xmpp/%s\",charset=utf-8,response=%08x%08x%08x%08x", uname, realm, nonce, cnonce, iCallCount, serv, htonl(digest[0]), htonl(digest[1]), htonl(digest[2]), htonl(digest[3])); return mir_base64_encode((PBYTE)buf, cbLen); }
void CVkProto::OnReciveUploadServer(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) { CVkFileUploadParam *fup = (CVkFileUploadParam *)pReq->pUserInfo; if (!IsOnline()) { SendFileFiled(fup, VKERR_OFFLINE); return; } debugLogA("CVkProto::OnReciveUploadServer %d", reply->resultCode); if (reply->resultCode != 200) { SendFileFiled(fup, VKERR_INVALID_SERVER); return; } JSONNode jnRoot; const JSONNode &jnResponse = CheckJsonResponse(pReq, reply, jnRoot); if (!jnResponse || pReq->m_iErrorCode) { SendFileFiled(fup, pReq->m_iErrorCode); return; } CMStringA uri(jnResponse["upload_url"].as_mstring()); if (uri.IsEmpty()) { SendFileFiled(fup, VKERR_INVALID_URL); return; } FILE *pFile = _tfopen(fup->FileName, _T("rb")); if (pFile == NULL) { SendFileFiled(fup, VKERR_ERR_OPEN_FILE); return; } fseek(pFile, 0, SEEK_END); long iFileLen = ftell(pFile); //FileSize if (iFileLen < 1) { fclose(pFile); SendFileFiled(fup, VKERR_ERR_READ_FILE); return; } fseek(pFile, 0, SEEK_SET); ProtoBroadcastAck(fup->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTING, (HANDLE)fup); AsyncHttpRequest *pUploadReq = new AsyncHttpRequest(this, REQUEST_POST, uri, false, &CVkProto::OnReciveUpload); pUploadReq->m_bApiReq = false; pUploadReq->m_szParam = ""; CMStringA boundary, header; CMStringA NamePart = fup->atrName(); CMStringA FNamePart = fup->fileName(); // Boundary int iboundary; Utils_GetRandom(&iboundary, sizeof(iboundary)); boundary.AppendFormat("Miranda%dNG%d", iboundary, time(NULL)); // Header header.AppendFormat("multipart/form-data; boundary=%s", boundary); pUploadReq->AddHeader("Content-Type", header); // Content-Disposition { CMStringA DataBegin = "--"; DataBegin += boundary; DataBegin += "\r\n"; DataBegin += "Content-Disposition: form-data; name=\""; DataBegin += NamePart; DataBegin += "\"; filename=\""; DataBegin += FNamePart; DataBegin += "\";\r\n\r\n"; // } Content-Disposition CMStringA DataEnd = "\r\n--"; DataEnd += boundary; DataEnd += "--\r\n"; // Body size long dataLength = iFileLen + DataBegin.GetLength() + DataEnd.GetLength(); // Body { char* pData = (char *)mir_alloc(dataLength); memcpy(pData, (void *)DataBegin.GetBuffer(), DataBegin.GetLength()); pUploadReq->pData = pData; pData += DataBegin.GetLength(); long lBytes = (long)fread(pData, 1, iFileLen, pFile); fclose(pFile); if (lBytes != iFileLen) { SendFileFiled(fup, VKERR_ERR_READ_FILE); mir_free(pUploadReq->pData); delete pUploadReq; return; } pData += iFileLen; memcpy(pData, (void *)DataEnd.GetBuffer(), DataEnd.GetLength()); // } Body pUploadReq->dataLength = (int)dataLength; pUploadReq->pUserInfo = pReq->pUserInfo; Push(pUploadReq); }