int ReceiveBody( IN SOCKET connSock, IN PENCRYPTION_INFO EncryptInfo, IN PNDASDIGEST_INFO DigestInfo, IN ULONG DataTransferLength, IN ULONG DataBufferLength, OUT PUCHAR DataBuffer ){ int iResult; int iTotalRecved = 0; UNREFERENCED_PARAMETER(DigestInfo); // // Parameter check // if(DataBuffer == NULL) return -1; if(DataTransferLength == 0) return 0; if(DataBufferLength < DataTransferLength) return -1; // Read Data iResult = RecvIt( connSock, DataBuffer, DataTransferLength); if(iResult == SOCKET_ERROR) { fprintf(stderr, "ReceiveBody: Can't Recv Body\n"); return iResult; } else if(iResult == 0) { fprintf(stderr, "ReceiveBody: Disconnected...\n"); return iResult; } else iTotalRecved += iResult; if(EncryptInfo && EncryptInfo->BodyEncryptAlgo != 0) { Decrypt32( DataBuffer, DataTransferLength, (unsigned char *)&EncryptInfo->CHAP_C, (unsigned char *)&EncryptInfo->Password64 ); } return iTotalRecved; }
// // Read ID or Write Key and Decrypt it. // // Return : NULL if not exist or decryped value. // CFStringRef NDASPreferencesGetIDorWriteKey( CFDictionaryRef dictEntry, CFStringRef keyString ) { bool present; CFStringRef strValue; CFDataRef cfdEncrypedData; UInt8 data[8]; present = CFDictionaryGetValueIfPresent( dictEntry, keyString, (const void**)&cfdEncrypedData ); if(!present || !cfdEncrypedData) { return NULL; } else { if(CFDataGetTypeID() == CFGetTypeID(cfdEncrypedData)) { CFDataGetBytes(cfdEncrypedData, CFRangeMake(0, 8), data); Decrypt32(data, 8, (unsigned char*)&ENCRYPT_KEY, (unsigned char*)hostIDKey); strValue = CFStringCreateWithBytes ( kCFAllocatorDefault, data, 5, CFStringGetSystemEncoding(), false ); return strValue; } else { return NULL; } } }
int ReceivePdu( IN SOCKET connSock, IN PENCRYPTION_INFO EncryptInfo, IN PNDASDIGEST_INFO DigestInfo, IN PLANSCSI_PDU_POINTERS pPdu ){ int iResult; int iTotalRecved = 0; PUCHAR pPtr = pPdu->pBufferBase; UNREFERENCED_PARAMETER(DigestInfo); // Read Header. iResult = RecvIt( connSock, pPtr, sizeof(LANSCSI_H2R_PDU_HEADER)); if(iResult == SOCKET_ERROR) { fprintf(stderr, "ReceivePdu: Can't Recv Header...\n"); return iResult; } else if(iResult == 0) { fprintf(stderr, "ReceivePdu: Disconnected...\n"); return iResult; } else iTotalRecved += iResult; // pPdu->pH2RHeader = (PLANSCSI_H2R_PDU_HEADER)pPtr; pPtr += sizeof(LANSCSI_H2R_PDU_HEADER); if(EncryptInfo && EncryptInfo->HeaderEncryptAlgo != 0) { Decrypt32( (unsigned char*)pPdu->pH2RHeader, sizeof(LANSCSI_H2R_PDU_HEADER), (unsigned char *)&EncryptInfo->CHAP_C, (unsigned char *)&EncryptInfo->Password64 ); } // Read AHS. if(ntohs(pPdu->pH2RHeader->AHSLen) > 0) { iResult = RecvIt( connSock, pPtr, ntohs(pPdu->pH2RHeader->AHSLen) ); if(iResult == SOCKET_ERROR) { fprintf(stderr, "ReceivePdu: Can't Recv AHS...\n"); return iResult; } else if(iResult == 0) { fprintf(stderr, "ReceivePdu: Disconnected...\n"); return iResult; } else iTotalRecved += iResult; pPdu->pAHS = (PCHAR)pPtr; pPtr += ntohs(pPdu->pH2RHeader->AHSLen); if ( EncryptInfo && EncryptInfo->HeaderEncryptAlgo != 0) { Decrypt32( (unsigned char*)pPdu->pAHS, ntohs(pPdu->pH2RHeader->AHSLen), (unsigned char *)&EncryptInfo->CHAP_C, (unsigned char *)&EncryptInfo->Password64); } } // Read Header Dig. pPdu->pHeaderDig = NULL; // Read Data segment. if(ntohl(pPdu->pH2RHeader->DataSegLen) > 0) { iResult = RecvIt( connSock, pPtr, ntohl(pPdu->pH2RHeader->DataSegLen) ); if(iResult == SOCKET_ERROR) { fprintf(stderr, "ReceivePdu: Can't Recv Data segment...\n"); return iResult; } else if(iResult == 0) { fprintf(stderr, "ReceivePdu: Disconnected...\n"); return iResult; } else iTotalRecved += iResult; pPdu->pDataSeg = (PCHAR)pPtr; pPtr += ntohl(pPdu->pH2RHeader->DataSegLen); if( EncryptInfo && EncryptInfo->BodyEncryptAlgo != 0) { Decrypt32( (unsigned char*)pPdu->pDataSeg, ntohl(pPdu->pH2RHeader->DataSegLen), (unsigned char *)&EncryptInfo->CHAP_C, (unsigned char *)&EncryptInfo->Password64 ); } } // Read Data Dig. pPdu->pDataDig = NULL; return iTotalRecved; }
int ReadReply( SOCKET connSock, PCHAR pBuffer, PLANSCSI_PDU pPdu ) { int iResult, iTotalRecved = 0; PCHAR pPtr = pBuffer; // Read Header. iResult = RecvIt( connSock, pPtr, sizeof(LANSCSI_H2R_PDU_HEADER) ); if(iResult == SOCKET_ERROR) { fprintf(stderr, "ReadRequest: Can't Recv Header...\n"); return iResult; } else if(iResult == 0) { fprintf(stderr, "ReadRequest: Disconnected...\n"); return iResult; } else iTotalRecved += iResult; pPdu->pH2RHeader = (PLANSCSI_H2R_PDU_HEADER)pPtr; pPtr += sizeof(LANSCSI_H2R_PDU_HEADER); if(iSessionPhase == FLAG_FULL_FEATURE_PHASE && HeaderEncryptAlgo != 0) { Decrypt32( (unsigned char*)pPdu->pH2RHeader, sizeof(LANSCSI_H2R_PDU_HEADER), (unsigned char *)&CHAP_C, (unsigned char *)&iPassword ); } // Read AHS. if(ntohs(pPdu->pH2RHeader->AHSLen) > 0) { iResult = RecvIt( connSock, pPtr, ntohs(pPdu->pH2RHeader->AHSLen) ); if(iResult == SOCKET_ERROR) { fprintf(stderr, "ReadRequest: Can't Recv AHS...\n"); return iResult; } else if(iResult == 0) { fprintf(stderr, "ReadRequest: Disconnected...\n"); return iResult; } else iTotalRecved += iResult; pPdu->pAHS = pPtr; pPtr += ntohs(pPdu->pH2RHeader->AHSLen); } // Read Header Dig. pPdu->pHeaderDig = NULL; // Read Data segment. if(ntohl(pPdu->pH2RHeader->DataSegLen) > 0) { iResult = RecvIt( connSock, pPtr, ntohl(pPdu->pH2RHeader->DataSegLen) ); if(iResult == SOCKET_ERROR) { fprintf(stderr, "ReadRequest: Can't Recv Data segment...\n"); return iResult; } else if(iResult == 0) { fprintf(stderr, "ReadRequest: Disconnected...\n"); return iResult; } else iTotalRecved += iResult; pPdu->pDataSeg = pPtr; pPtr += ntohl(pPdu->pH2RHeader->DataSegLen); if(iSessionPhase == FLAG_FULL_FEATURE_PHASE && DataEncryptAlgo != 0) { Decrypt32( (unsigned char*)pPdu->pDataSeg, ntohl(pPdu->pH2RHeader->DataSegLen), (unsigned char *)&CHAP_C, (unsigned char *)&iPassword ); } } // Read Data Dig. pPdu->pDataDig = NULL; return iTotalRecved; }