| 44 | } |
| 45 | |
| 46 | void QRCodeDialog::HandleGatewayMessage(const std::string& payload) |
| 47 | { |
| 48 | Json j = Json::parse(payload); |
| 49 | std::string op = j["op"]; |
| 50 | |
| 51 | if (op == "heartbeat") |
| 52 | { |
| 53 | Json j2; |
| 54 | j2["op"] = "heartbeat_ack"; |
| 55 | GetWebsocketClient()->SendMsg(m_gatewayId, j2.dump()); |
| 56 | } |
| 57 | else if (op == "hello") |
| 58 | { |
| 59 | // send server our public key |
| 60 | Json j2; |
| 61 | j2["op"] = "init"; |
| 62 | j2["encoded_public_key"] = m_publicKeyBase64; |
| 63 | GetWebsocketClient()->SendMsg(m_gatewayId, j2.dump()); |
| 64 | } |
| 65 | else if (op == "nonce_proof") |
| 66 | { |
| 67 | char* data = nullptr, *dataEnc = nullptr; |
| 68 | uint8_t* decryptedData = nullptr; |
| 69 | EVP_PKEY_CTX* ctx = NULL; |
| 70 | |
| 71 | // separate scope for this bitch due to the goto |
| 72 | { |
| 73 | Json j2; |
| 74 | |
| 75 | // server sent us an encrypted Proof of Identity payload, decrypt it using our private key |
| 76 | // send the server OUR proof which is the SHA256 hash of the proof of the decrypted nonce |
| 77 | std::string dataB64 = j["encrypted_nonce"]; |
| 78 | |
| 79 | // Buffer management is kind of crap here (grimacing) |
| 80 | data = new char[base64::decoded_size(dataB64.size()) + 5]; |
| 81 | auto decsz = base64::decode(data, dataB64.c_str(), dataB64.size()); |
| 82 | size_t dataSz = decsz.first; |
| 83 | int res = 0; |
| 84 | |
| 85 | ctx = EVP_PKEY_CTX_new(m_pkey, NULL); |
| 86 | |
| 87 | res = EVP_PKEY_decrypt_init(ctx); |
| 88 | if (res <= 0) goto NONCE_PROOF_ERROR_LBL; |
| 89 | |
| 90 | if (EVP_PKEY_CTX_ctrl_str(ctx, "rsa_padding_mode", "oaep") <= 0) goto NONCE_PROOF_ERROR_LBL; |
| 91 | if (EVP_PKEY_CTX_ctrl_str(ctx, "rsa_oaep_md", "sha256") <= 0) goto NONCE_PROOF_ERROR_LBL; |
| 92 | if (EVP_PKEY_CTX_ctrl_str(ctx, "rsa_mgf1_md", "sha256") <= 0) goto NONCE_PROOF_ERROR_LBL; |
| 93 | |
| 94 | decryptedData = NULL; |
| 95 | size_t decryptedSize = 0; |
| 96 | res = EVP_PKEY_decrypt(ctx, NULL, &decryptedSize, (const uint8_t*)data, dataSz); |
| 97 | if (res <= 0) goto NONCE_PROOF_ERROR_LBL; |
| 98 | |
| 99 | decryptedData = new uint8_t[decryptedSize]; |
| 100 | res = EVP_PKEY_decrypt(ctx, decryptedData, &decryptedSize, (const uint8_t*)data, dataSz); |
| 101 | if (res <= 0) goto NONCE_PROOF_ERROR_LBL; |
| 102 | |
| 103 | dataEnc = new char[base64::encoded_size(decryptedSize) + 5]; |
no test coverage detected