/*********************************************************************************************************************************** Compress data ***********************************************************************************************************************************/ static Buffer * testCompress(IoFilter *compress, Buffer *decompressed, size_t inputSize, size_t outputSize) { Buffer *compressed = bufNew(1024 * 1024); size_t inputTotal = 0; ioBufferSizeSet(outputSize); IoFilterGroup *filterGroup = ioFilterGroupNew(); ioFilterGroupAdd(filterGroup, compress); IoWrite *write = ioBufferWriteNew(compressed); ioWriteFilterGroupSet(write, filterGroup); ioWriteOpen(write); // Compress input data while (inputTotal < bufSize(decompressed)) { // Generate the input buffer based on input size. This breaks the data up into chunks as it would be in a real scenario. Buffer *input = bufNewC( bufPtr(decompressed) + inputTotal, inputSize > bufSize(decompressed) - inputTotal ? bufSize(decompressed) - inputTotal : inputSize); ioWrite(write, input); inputTotal += bufUsed(input); bufFree(input); } ioWriteClose(write); memContextFree(((GzipCompress *)ioFilterDriver(compress))->memContext); return compressed; }
void writeLog(const char *s, int length) { if (commonInfo->inWriteLog) { appendToLogBuffer(s, length); return; } commonInfo->inWriteLog++; if (!commonInfo->logKeepOpen) { openLogFile(); } flushLogBuffer(); if (ioWrite(commonInfo->logFd, s, length) < 0) { // Can't write to the log file right now, probably because the interrupts are disabled. // Save the log string for later output. appendToLogBuffer(s, length); } else { flushLogBuffer(); } if (!commonInfo->logKeepOpen) { closeLogFile(); } commonInfo->inWriteLog--; }
static bool exceptionHandler(hostContext_t* context) { uint8_t* code = (uint8_t*)context->eip; uint8_t im = code[1]; if (*code == 0xE6) { ioWrite(im,(ioData_t*)&context->al,1); //printf("0x%08X: OUT Ib (0x%02X) eAX AL (0x%08X)\n",context->eip,im,context->al); context->eip += 2; return true; } if (*code == 0xE7) { ioWrite(im,(ioData_t*)&context->eax,4); //printf("0x%08X: OUT Ib (0x%02X) eAX (0x%08X)\n",context->eip,im,context->eax); context->eip += 2; return true; } if (*code == 0xEE) { //printf("0x%08X: OUT DX (0x%04X) AL (0x%02X)\n",context->eip,context->dx,context->al); ioWrite(context->dx,(ioData_t*)&context->al,1); context->eip += 1; return true; } if (*code == 0xEF) { //printf("0x%08X: OUT DX (0x%04X) eAX (0x%08X)\n",context->eip,context->dx,context->eax); ioWrite(context->dx,(ioData_t*)&context->eax,4); context->eip += 1; return true; } if (*code == 0xED) { //printf("0x%08X: in eax, dx (0x%04X) // ecx: %i\n",context->eip,context->dx,context->ecx); ioRead(context->dx,(ioData_t*)&context->eax,4); context->eip += 1; return true; } printf("Unhandled exception at 0x%08X!\n",context->eip); return false; }
// Writes data on one handle ID of the specified group. Returns amount of bytes written. int ioWriteGroup(struct s_io_state *iostate, const int group, const unsigned char *write_buf, const int write_buf_size, const struct s_io_addr *destination_addr) { int i; int ret; for(i=0; i<iostate->max; i++) { if(iostate->handle[i].group_id == group) { ret = ioWrite(iostate, i, write_buf, write_buf_size, destination_addr); if(ret > 0) { return ret; } } } return 0; }
void flushLogBuffer() { while (commonInfo->logBufferLength > 0) { // Try to write pending output. // This will succeed as soon as the interrupts are enabled again. int length = ioWrite(commonInfo->logFd, commonInfo->logBuffer, commonInfo->logBufferLength); if (length <= 0) { break; } commonInfo->logBufferLength -= length; if (commonInfo->logBufferLength > 0) { memcpy(commonInfo->logBuffer, commonInfo->logBuffer + length, commonInfo->logBufferLength); } } }
/*********************************************************************************************************************************** Test Run ***********************************************************************************************************************************/ void testRun(void) { FUNCTION_HARNESS_VOID(); // Additional coverage not provided by testing with actual certificates // ***************************************************************************************************************************** if (testBegin("asn1ToStr(), tlsClientHostVerify(), and tlsClientHostVerifyName()")) { TEST_ERROR(asn1ToStr(NULL), CryptoError, "TLS certificate name entry is missing"); TEST_ERROR( tlsClientHostVerifyName( strNew("host"), strNewN("ab\0cd", 5)), CryptoError, "TLS certificate name contains embedded null"); TEST_ERROR(tlsClientHostVerify(strNew("host"), NULL), CryptoError, "No certificate presented by the TLS server"); TEST_RESULT_BOOL(tlsClientHostVerifyName(strNew("host"), strNew("**")), false, "invalid pattern"); TEST_RESULT_BOOL(tlsClientHostVerifyName(strNew("host"), strNew("*.")), false, "invalid pattern"); TEST_RESULT_BOOL(tlsClientHostVerifyName(strNew("a.bogus.host.com"), strNew("*.host.com")), false, "invalid host"); } // ***************************************************************************************************************************** if (testBegin("TlsClient verification")) { TlsClient *client = NULL; // Connection errors // ------------------------------------------------------------------------------------------------------------------------- TEST_ASSIGN(client, tlsClientNew(strNew("99.99.99.99.99"), 9443, 0, true, NULL, NULL), "new client"); TEST_ERROR( tlsClientOpen(client), HostConnectError, "unable to get address for '99.99.99.99.99': [-2] Name or service not known"); TEST_ASSIGN(client, tlsClientNew(strNew("localhost"), 9443, 100, true, NULL, NULL), "new client"); TEST_ERROR(tlsClientOpen(client), HostConnectError, "unable to connect to 'localhost:9443': [111] Connection refused"); // Certificate location and validation errors // ------------------------------------------------------------------------------------------------------------------------- // Add test hosts if (system( // {uncoverable_branch} "echo \"127.0.0.1 test.pgbackrest.org host.test2.pgbackrest.org test3.pgbackrest.org\" |" " sudo tee -a /etc/hosts > /dev/null") != 0) { THROW(AssertError, "unable to add test hosts to /etc/hosts"); // {uncovered+} } // Start server to test various certificate errors testTlsServerAltName(); TEST_ERROR( tlsClientOpen(tlsClientNew(strNew("localhost"), 9443, 500, true, strNew("bogus.crt"), strNew("/bogus"))), CryptoError, "unable to set user-defined CA certificate location: [33558530] No such file or directory"); TEST_ERROR( tlsClientOpen(tlsClientNew(strNew("localhost"), 9443, 500, true, NULL, strNew("/bogus"))), CryptoError, "unable to verify certificate presented by 'localhost:9443': [20] unable to get local issuer certificate"); TEST_RESULT_VOID( tlsClientOpen( tlsClientNew(strNew("test.pgbackrest.org"), 9443, 500, true, strNewFmt("%s/" TEST_CERTIFICATE_PREFIX "-ca.crt", testRepoPath()), NULL)), "success on valid ca file and match common name"); TEST_RESULT_VOID( tlsClientOpen( tlsClientNew(strNew("host.test2.pgbackrest.org"), 9443, 500, true, strNewFmt("%s/" TEST_CERTIFICATE_PREFIX "-ca.crt", testRepoPath()), NULL)), "success on valid ca file and match alt name"); TEST_ERROR( tlsClientOpen( tlsClientNew(strNew("test3.pgbackrest.org"), 9443, 500, true, strNewFmt("%s/" TEST_CERTIFICATE_PREFIX "-ca.crt", testRepoPath()), NULL)), CryptoError, "unable to find hostname 'test3.pgbackrest.org' in certificate common name or subject alternative names"); TEST_ERROR( tlsClientOpen( tlsClientNew(strNew("localhost"), 9443, 500, true, strNewFmt("%s/" TEST_CERTIFICATE_PREFIX ".crt", testRepoPath()), NULL)), CryptoError, "unable to verify certificate presented by 'localhost:9443': [20] unable to get local issuer certificate"); TEST_RESULT_VOID( tlsClientOpen(tlsClientNew(strNew("localhost"), 9443, 500, false, NULL, NULL)), "success on no verify"); } // ***************************************************************************************************************************** if (testBegin("TlsClient general usage")) { TlsClient *client = NULL; // Reset statistics tlsClientStatLocal = (TlsClientStat){0}; TEST_RESULT_STR(tlsClientStatStr(), NULL, "no stats yet"); testTlsServer(); ioBufferSizeSet(12); TEST_ASSIGN(client, tlsClientNew(strNew(TLS_TEST_HOST), 9443, 500, true, NULL, NULL), "new client"); TEST_RESULT_VOID(tlsClientOpen(client), "open client"); const Buffer *input = BUFSTRDEF("some protocol info"); TEST_RESULT_VOID(ioWrite(tlsClientIoWrite(client), input), "write input"); ioWriteFlush(tlsClientIoWrite(client)); TEST_RESULT_STR(strPtr(ioReadLine(tlsClientIoRead(client))), "something:0", "read line"); TEST_RESULT_BOOL(ioReadEof(tlsClientIoRead(client)), false, " check eof = false"); Buffer *output = bufNew(12); TEST_RESULT_INT(ioRead(tlsClientIoRead(client), output), 12, "read output"); TEST_RESULT_STR(strPtr(strNewBuf(output)), "some content", " check output"); TEST_RESULT_BOOL(ioReadEof(tlsClientIoRead(client)), false, " check eof = false"); output = bufNew(8); TEST_RESULT_INT(ioRead(tlsClientIoRead(client), output), 8, "read output"); TEST_RESULT_STR(strPtr(strNewBuf(output)), "AND MORE", " check output"); TEST_RESULT_BOOL(ioReadEof(tlsClientIoRead(client)), false, " check eof = false"); output = bufNew(12); TEST_ERROR( ioRead(tlsClientIoRead(client), output), FileReadError, "unable to read data from 'tls.test.pgbackrest.org:9443' after 500ms"); // ------------------------------------------------------------------------------------------------------------------------- input = BUFSTRDEF("more protocol info"); TEST_RESULT_VOID(tlsClientOpen(client), "open client again (it is already open)"); TEST_RESULT_VOID(ioWrite(tlsClientIoWrite(client), input), "write input"); ioWriteFlush(tlsClientIoWrite(client)); output = bufNew(12); TEST_RESULT_INT(ioRead(tlsClientIoRead(client), output), 12, "read output"); TEST_RESULT_STR(strPtr(strNewBuf(output)), "0123456789AB", " check output"); TEST_RESULT_BOOL(ioReadEof(tlsClientIoRead(client)), false, " check eof = false"); output = bufNew(12); TEST_RESULT_INT(ioRead(tlsClientIoRead(client), output), 0, "read no output after eof"); TEST_RESULT_BOOL(ioReadEof(tlsClientIoRead(client)), true, " check eof = true"); TEST_RESULT_BOOL(tlsClientStatStr() != NULL, true, "check statistics exist"); TEST_RESULT_VOID(tlsClientFree(client), "free client"); } FUNCTION_HARNESS_RESULT_VOID(); }
void utilitySavedataLog(char *buffer, const SyscallInfo *syscallInfo, u32 param) { char *s = buffer; int fd; if (syscallInfo->nid != 0x50C4CD57 && syscallInfo->nid != 0x9790B33C) { return; } if (syscallInfo->nid == 0x50C4CD57) { utilitySavedataParams = (void *) param; } if (utilitySavedataParams == NULL) { return; } int mode = _lw((int) utilitySavedataParams + 48); s = append(s, "mode="); s = appendInt(s, mode, 0); s = append(s, ", gameName="); s = append(s, utilitySavedataParams + 60); s = append(s, ", saveName="); s = append(s, utilitySavedataParams + 76); s = append(s, ", fileName="); s = append(s, utilitySavedataParams + 100); if (syscallInfo->nid == 0x9790B33C) { s = append(s, ", result="); s = appendHex(s, _lw((int) utilitySavedataParams + 28), 8); } s = append(s, "\n"); writeLog(buffer, s - buffer); s = buffer; printLogMem("Data ", _lw((int) utilitySavedataParams + 116), 16); int fileListAddr = _lw((int) utilitySavedataParams + 1528); if (fileListAddr != 0 && mode == 12) { // MODE_FILES printLogMem("FileList ", fileListAddr, 36); if (syscallInfo->nid == 0x9790B33C) { // sceUtilitySavedataShutdownStart int saveFileSecureNumEntries = _lw(fileListAddr + 12); int saveFileNumEntries = _lw(fileListAddr + 16); int systemFileNumEntries = _lw(fileListAddr + 20); int saveFileSecureEntriesAddr = _lw(fileListAddr + 24); int saveFileEntriesAddr = _lw(fileListAddr + 28); int systemEntriesAddr = _lw(fileListAddr + 32); printLogMem("SecureEntries ", saveFileSecureEntriesAddr, saveFileSecureNumEntries * 80); printLogMem("NormalEntries ", saveFileEntriesAddr, saveFileNumEntries * 80); printLogMem("SystemEntries ", systemEntriesAddr, systemFileNumEntries * 80); } } printLogMem("Params ", (int) utilitySavedataParams, _lw((int) utilitySavedataParams + 0)); if (syscallInfo->nid == 0x9790B33C) { fd = ioOpen("ms0:/SavedataStruct.bin", PSP_O_WRONLY | PSP_O_CREAT | PSP_O_APPEND, 0777); ioWrite(fd, utilitySavedataParams, _lw((int) utilitySavedataParams + 0)); ioClose(fd); fd = ioOpen("ms0:/SavedataData.bin", PSP_O_WRONLY | PSP_O_CREAT | PSP_O_APPEND, 0777); ioWrite(fd, (void *) _lw((int) utilitySavedataParams + 116), _lw((int) utilitySavedataParams + 124)); ioClose(fd); } }