HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiDigest::DigestInternal___SZARRAY_U1__SZARRAY_U1__I4__I4( CLR_RT_StackFrame& stack ) { TINYCLR_HEADER(); CLR_RT_HeapBlock* pThis = stack.This(); CLR_RT_HeapBlock_Array* pData = stack.Arg1().DereferenceArray(); CLR_INT32 offset = stack.Arg2().NumericByRef().s4; CLR_INT32 len = stack.Arg3().NumericByRef().s4; CLR_RT_HeapBlock* pSession = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference(); CLR_INT32 digestSize = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiDigest::FIELD__m_hashSize].NumericByRef().s4; CK_SESSION_HANDLE hSession; //CLR_INT32 maxProcessingBytes; FAULT_ON_NULL_ARG(pData); FAULT_ON_NULL(pSession); if((offset + len) > (CLR_INT32)pData->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE); hSession = (CK_SESSION_HANDLE)pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4; if(hSession == CK_SESSION_HANDLE_INVALID) TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED); // TODO: add code for processing chunks at a time if size is too big //maxProcessingBytes = pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_maxProcessingBytes].NumericByRef().s4; TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance(stack.PushValue(), digestSize, g_CLR_RT_WellKnownTypes.m_UInt8)); CRYPTOKI_CHECK_RESULT(stack, C_Digest(hSession, pData->GetElement(offset), len, stack.TopValue().DereferenceArray()->GetFirstElement(), (CK_ULONG_PTR)&digestSize)); TINYCLR_NOCLEANUP(); }
// TODO: Make common functions for transformBlock for encrypt/decrypt when async logic is in place HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Decryptor::TransformBlockInternal___I4__SZARRAY_U1__I4__I4__SZARRAY_U1__I4( CLR_RT_StackFrame& stack ) { TINYCLR_HEADER(); CLR_RT_HeapBlock* pThis = stack.This(); CLR_RT_HeapBlock_Array* pData = stack.Arg1().DereferenceArray(); CLR_INT32 dataOffset = stack.Arg2().NumericByRef().s4; CLR_INT32 dataLen = stack.Arg3().NumericByRef().s4; CLR_RT_HeapBlock_Array* pOutput = stack.Arg4().DereferenceArray(); CLR_INT32 outOffset = stack.Arg5().NumericByRef().s4; CLR_RT_HeapBlock* pSession = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference(); CK_SESSION_HANDLE hSession; CLR_UINT32 decrSize; FAULT_ON_NULL_ARG(pData); FAULT_ON_NULL_ARG(pOutput); FAULT_ON_NULL_ARG(pSession); hSession = (CK_SESSION_HANDLE)pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4; if(hSession == CK_SESSION_HANDLE_INVALID) TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED); if((dataOffset + dataLen) > (CLR_INT32)pData->m_numOfElements ) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE); if((outOffset ) > (CLR_INT32)pOutput->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE); decrSize = pOutput->m_numOfElements - outOffset; CRYPTOKI_CHECK_RESULT(stack, C_DecryptUpdate(hSession, pData->GetElement(dataOffset), dataLen, pOutput->GetElement(outOffset), (CK_ULONG_PTR)&decrSize)); stack.SetResult_I4(decrSize); TINYCLR_NOCLEANUP(); }
HRESULT Library_corlib_native_System_Text_UTF8Encoding::GetBytes___I4__STRING__I4__I4__SZARRAY_U1__I4( CLR_RT_StackFrame& stack ) { NATIVE_PROFILE_CLR_CORE(); TINYCLR_HEADER(); size_t cMaxBytes; LPCSTR str = stack.Arg1().RecoverString(); CLR_INT32 strIdx = stack.Arg2().NumericByRef().s4; CLR_INT32 strCnt = stack.Arg3().NumericByRef().s4; CLR_RT_HeapBlock_Array* pArrayBytes = stack.Arg4().DereferenceArray(); CLR_INT32 byteIdx = stack.Arg5().NumericByRef().s4; FAULT_ON_NULL(str); FAULT_ON_NULL(pArrayBytes); cMaxBytes = hal_strlen_s(str); if((strIdx + strCnt) > (CLR_INT32)cMaxBytes ) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE); if((byteIdx + strCnt) > (CLR_INT32)pArrayBytes->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE); memcpy(pArrayBytes->GetElement(byteIdx), &str[strIdx], strCnt); stack.SetResult_I4(strCnt); TINYCLR_NOCLEANUP(); }
HRESULT Library_spot_hardware_native_Microsoft_SPOT_Hardware_SPI::InternalWriteRead___VOID__SZARRAY_U2__I4__I4__SZARRAY_U2__I4__I4__I4( CLR_RT_StackFrame& stack ) { NATIVE_PROFILE_CLR_HARDWARE(); TINYCLR_HEADER(); { CLR_RT_HeapBlock* pThis = stack.This(); FAULT_ON_NULL(pThis); CLR_RT_HeapBlock_Array* writeBuffer = stack.Arg1().DereferenceArray(); FAULT_ON_NULL(writeBuffer); CLR_INT32 writeOffset = stack.Arg2().NumericByRef().s4; CLR_INT32 writeCount = stack.Arg3().NumericByRef().s4; CLR_RT_HeapBlock_Array* readBuffer = stack.Arg4().DereferenceArray(); CLR_INT32 readOffset = stack.Arg5().NumericByRef().s4; CLR_INT32 readCount = stack.Arg6().NumericByRef().s4; CLR_UINT32 startReadOffset = stack.Arg7().NumericByRef().s4; SPI_CONFIGURATION config; TINYCLR_CHECK_HRESULT(Library_spot_hardware_native_Microsoft_SPOT_Hardware_SPI__Configuration::GetInitialConfig( pThis[ FIELD__m_config ], config )); config.MD_16bits = TRUE; CPU_SPI_Initialize(); if(!::CPU_SPI_nWrite16_nRead16( config, (CLR_UINT16*)writeBuffer->GetElement(writeOffset), writeCount, readBuffer == NULL ? NULL : (CLR_UINT16*)readBuffer ->GetElement(readOffset), readBuffer == NULL ? 0 : readCount, startReadOffset )) { TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_OPERATION); } } TINYCLR_NOCLEANUP(); }
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiSign::SignUpdateInternal___VOID__SZARRAY_U1__I4__I4( CLR_RT_StackFrame& stack ) { TINYCLR_HEADER(); CLR_RT_HeapBlock* pThis = stack.This(); CLR_RT_HeapBlock_Array* pData = stack.Arg1().DereferenceArray(); CLR_INT32 offset = stack.Arg2().NumericByRef().s4; CLR_INT32 len = stack.Arg3().NumericByRef().s4; CLR_RT_HeapBlock* pSession = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference(); CLR_INT32 sigLen = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiSign::FIELD__m_signatureLength].NumericByRef().s4; CK_SESSION_HANDLE hSession; FAULT_ON_NULL_ARG(pData); FAULT_ON_NULL(pSession); if((offset + len) > (CLR_INT32)pData->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE); hSession = (CK_SESSION_HANDLE)pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4; // TODO: add code for processing chunks at a time if size is too big //maxProcessingBytes = pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_maxProcessingBytes].NumericByRef().s4; if(hSession == CK_SESSION_HANDLE_INVALID) TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED); CRYPTOKI_CHECK_RESULT(stack, C_SignUpdate(hSession, pData->GetElement(offset), len)); TINYCLR_NOCLEANUP(); }
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiSign::SignInternal___SZARRAY_U1__SZARRAY_U1__I4__I4( CLR_RT_StackFrame& stack ) { TINYCLR_HEADER(); CLR_RT_HeapBlock* pThis = stack.This(); CLR_RT_HeapBlock_Array* pData = stack.Arg1().DereferenceArray(); CLR_RT_HeapBlock_Array* pRes; CLR_INT32 offset = stack.Arg2().NumericByRef().s4; CLR_INT32 len = stack.Arg3().NumericByRef().s4; CLR_RT_HeapBlock* pSession = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference(); CK_SESSION_HANDLE hSession; CK_ULONG sigLen = 0; CLR_RT_HeapBlock hbRef; FAULT_ON_NULL(pSession); FAULT_ON_NULL_ARG(pData); if((offset + len) > (CLR_INT32)pData->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE); hSession = (CK_SESSION_HANDLE)pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4; if(hSession == CK_SESSION_HANDLE_INVALID) TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED); CRYPTOKI_CHECK_RESULT(stack, C_Sign(hSession, pData->GetElement(offset), len, NULL, (CK_ULONG_PTR)&sigLen)); TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance(hbRef, sigLen, g_CLR_RT_WellKnownTypes.m_UInt8)); pRes = hbRef.DereferenceArray(); CRYPTOKI_CHECK_RESULT(stack, C_Sign(hSession, pData->GetElement(offset), len, pRes->GetFirstElement(), (CK_ULONG_PTR)&sigLen)); if(sigLen < pRes->m_numOfElements) { TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance(stack.PushValue(), sigLen, g_CLR_RT_WellKnownTypes.m_UInt8)); memcpy(stack.TopValue().DereferenceArray()->GetFirstElement(), pRes->GetFirstElement(), sigLen); } else { stack.SetResult_Object(pRes); } TINYCLR_NOCLEANUP(); }
HRESULT Library_spot_native_Microsoft_SPOT_Hardware_Utility::InsertOrExtractValueFromArray( CLR_RT_StackFrame& stack, bool fInsert ) { NATIVE_PROFILE_CLR_CORE(); TINYCLR_HEADER(); CLR_RT_HeapBlock_Array* array; CLR_INT32 offset; CLR_INT32 size; CLR_UINT32 res; array = stack.Arg0().DereferenceArray(); FAULT_ON_NULL(array); offset = stack.Arg1().NumericByRefConst().s4; size = stack.Arg2().NumericByRefConst().s4; res = 0; switch(size) { case 1: case 2: case 4: if(offset >= 0 && (CLR_UINT32)(offset + size) <= array->m_numOfElements) { CLR_UINT8* ptr = array->GetElement( offset ); if(fInsert) { res = stack.Arg3().NumericByRef().u4; memcpy( ptr, &res, size ); } else { memcpy( &res, ptr, size ); stack.SetResult( res, DATATYPE_U4 ); } TINYCLR_SET_AND_LEAVE(S_OK); } break; } TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE); TINYCLR_NOCLEANUP(); }
HRESULT Library_spot_net_security_native_Microsoft_SPOT_Net_Security_SslNative::UpdateCertificates___STATIC__VOID__I4__MicrosoftSPOTNativeSystemSecurityCryptographyX509CertificatesX509Certificate__SZARRAY_MicrosoftSPOTNativeSystemSecurityCryptographyX509CertificatesX509Certificate( CLR_RT_StackFrame& stack ) { NATIVE_PROFILE_CLR_NETWORK(); TINYCLR_HEADER(); CLR_INT32 sslContext = stack.Arg0().NumericByRef().s4; CLR_RT_HeapBlock* hbCert = stack.Arg1().Dereference(); CLR_RT_HeapBlock_Array* arrCA = stack.Arg2().DereferenceArray(); CLR_RT_HeapBlock_Array* arrCert; CLR_UINT8* sslCert; int i; CLR_RT_HeapBlock* hbPwd; LPCSTR szPwd; FAULT_ON_NULL(hbCert); FAULT_ON_NULL(arrCA); arrCert = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_certificate ].DereferenceArray(); FAULT_ON_NULL(arrCert); sslCert = arrCert->GetFirstElement(); hbPwd = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_password ].Dereference(); FAULT_ON_NULL(hbPwd); szPwd = hbPwd->StringText(); SSL_ClearCertificateAuthority( sslContext ); if(!SSL_AddCertificateAuthority( sslContext, (const char*)sslCert, arrCert->m_numOfElements, szPwd )) TINYCLR_SET_AND_LEAVE(CLR_E_FAIL); for(i=0; i<(int)arrCA->m_numOfElements; i++) { hbCert = (CLR_RT_HeapBlock*)arrCA->GetElement( i ); FAULT_ON_NULL(arrCert); arrCert = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_certificate ].DereferenceArray(); sslCert = arrCert->GetFirstElement(); hbPwd = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_password ].Dereference(); szPwd = hbPwd->StringText(); if(!SSL_AddCertificateAuthority( sslContext, (const char*)sslCert, arrCert->m_numOfElements, szPwd )) TINYCLR_SET_AND_LEAVE(CLR_E_FAIL); } TINYCLR_NOCLEANUP(); }
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiRNG::GenerateRandom___VOID__SZARRAY_U1__I4__I4__BOOLEAN( CLR_RT_StackFrame& stack ) { TINYCLR_HEADER(); CLR_RT_HeapBlock* pThis = stack.This(); CLR_RT_HeapBlock* pSession; CLR_RT_HeapBlock_Array* pData = stack.Arg1().DereferenceArray(); CLR_INT32 offset = stack.Arg2().NumericByRef().s4; CLR_INT32 len = stack.Arg3().NumericByRef().s4; bool fNonZero = stack.Arg4().NumericByRef().s4 == 1; CK_SESSION_HANDLE hSession; CLR_UINT8* pDataElem; FAULT_ON_NULL_ARG(pData); if(len+offset > (CLR_INT32)pData->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE); pSession = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference(); FAULT_ON_NULL(pSession); hSession = pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4; pDataElem = pData->GetElement(offset); CRYPTOKI_CHECK_RESULT(stack, C_GenerateRandom(hSession, pDataElem, len)); if(fNonZero) { int i,idx = -1; CLR_UINT8 replacements[20]; for(i=0; i<len; i++) { if(*pDataElem == 0) { if(idx == -1 || idx >= ARRAYSIZE(replacements)) { CRYPTOKI_CHECK_RESULT(stack, C_GenerateRandom(hSession, replacements, ARRAYSIZE(replacements))); idx = 0; } *pDataElem = replacements[idx++]; } } } TINYCLR_NOCLEANUP(); }
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiVerify::VerifyFinalInternal___BOOLEAN__SZARRAY_U1__I4__I4( CLR_RT_StackFrame& stack ) { TINYCLR_HEADER(); CLR_RT_HeapBlock* pThis = stack.This(); CLR_RT_HeapBlock_Array* pSig = stack.Arg1().DereferenceArray(); CLR_INT32 sigOff = stack.Arg2().NumericByRef().s4; CLR_INT32 sigLen = stack.Arg3().NumericByRef().s4; CLR_RT_HeapBlock* pSession = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference(); CK_SESSION_HANDLE hSession; bool retVal = false; CK_RV result; FAULT_ON_NULL_ARG(pSig); if((sigOff + sigLen) > (CLR_INT32)pSig->m_numOfElements ) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE); hSession = pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4; if(hSession == CK_SESSION_HANDLE_INVALID) TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED); result = C_VerifyFinal(hSession, pSig->GetElement(sigOff), sigLen); switch(result) { case CKR_SIGNATURE_INVALID: retVal = false; break; case CKR_OK: retVal = true; break; case CKR_SIGNATURE_LEN_RANGE: TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER); default: CRYPTOKI_CHECK_RESULT(stack, result); break; } stack.SetResult_Boolean(retVal); TINYCLR_NOCLEANUP(); }
HRESULT Library_spot_native_Microsoft_SPOT_Hardware_Utility::ComputeCRC___STATIC__U4__SZARRAY_U1__I4__I4__U4( CLR_RT_StackFrame& stack ) { NATIVE_PROFILE_CLR_CORE(); TINYCLR_HEADER(); CLR_RT_HeapBlock* pArgs; CLR_RT_HeapBlock_Array* array; int offset; int length; CLR_UINT32 crc; int totLength; pArgs = &(stack.Arg0()); array = pArgs[ 0 ].DereferenceArray(); FAULT_ON_NULL(array); offset = pArgs[ 1 ].NumericByRef().s4; length = pArgs[ 2 ].NumericByRef().s4; crc = pArgs[ 3 ].NumericByRef().u4; totLength = array->m_numOfElements; if(offset < 0 || offset > totLength) { TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE); } if(length == -1) { length = totLength - offset; } else { if(length < 0 || (offset+length) > totLength) { TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE); } } crc = SUPPORT_ComputeCRC( array->GetElement( offset ), length, crc ); stack.SetResult( crc, DATATYPE_U4 ); TINYCLR_NOCLEANUP(); }
// May Trigger GC, but parameter value will be protected HRESULT CLR_RT_HeapBlock_Queue::Enqueue( CLR_RT_HeapBlock* value ) { NATIVE_PROFILE_CLR_CORE(); TINYCLR_HEADER(); CLR_RT_HeapBlock_Array* array = GetArray(); CLR_INT32 size = GetSize(); CLR_INT32 tail = GetTail(); CLR_INT32 capacity = array->m_numOfElements; if(size == capacity) { // Set new capacity CLR_RT_HeapBlock newArrayHB; // Protect value from GC, in case CreateInstance triggers one CLR_RT_HeapBlock valueHB; valueHB.SetObjectReference( value ); CLR_RT_ProtectFromGC gc( valueHB ); capacity *= 2; TINYCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance( newArrayHB, capacity, g_CLR_RT_WellKnownTypes.m_Object )); array = newArrayHB.DereferenceArray(); CopyTo( array, 0 ); tail = size; SetArray( array ); SetHead ( 0 ); SetTail ( tail ); } ((CLR_RT_HeapBlock*)array->GetElement( tail ))->SetObjectReference( value ); SetTail( (tail + 1) % capacity ); SetSize( size + 1 ); TINYCLR_NOCLEANUP(); }
void CLR_RT_HeapBlock_XmlNamespaceStack::PopScope( CLR_UINT32 nameSpaceCount ) { if(nameSpaceCount > 0) { CLR_RT_HeapBlock_Array* array = GetArray(); CLR_INT32 size = GetSize(); CLR_INT32 newSize = size - nameSpaceCount; CLR_UINT32 index = array->m_numOfElements - size; // We should always have at least 3 entries ("xml", "xmlns", and "" in the namespace stack) _ASSERTE(newSize >= 3); do { ((CLR_RT_HeapBlock*)array->GetElement( index++ ))->SetObjectReference( NULL ); } while(--nameSpaceCount != 0); SetSize( newSize ); } }
HRESULT Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_CryptokiVerify::VerifyUpdateInternal___VOID__SZARRAY_U1__I4__I4( CLR_RT_StackFrame& stack ) { TINYCLR_HEADER(); CLR_RT_HeapBlock* pThis = stack.This(); CLR_RT_HeapBlock_Array* pData = stack.Arg1().DereferenceArray(); CLR_INT32 offset = stack.Arg2().NumericByRef().s4; CLR_INT32 len = stack.Arg3().NumericByRef().s4; CLR_RT_HeapBlock* pSession = pThis[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_SessionContainer::FIELD__m_session].Dereference(); CK_SESSION_HANDLE hSession; FAULT_ON_NULL_ARG(pData); if((offset + len) > (CLR_INT32)pData->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE); hSession = pSession[Library_security_pkcs11_native_Microsoft_SPOT_Cryptoki_Session::FIELD__m_handle].NumericByRef().s4; if(hSession == CK_SESSION_HANDLE_INVALID) TINYCLR_SET_AND_LEAVE(CLR_E_OBJECT_DISPOSED); CRYPTOKI_CHECK_RESULT(stack, C_VerifyUpdate(hSession, pData->GetElement(offset), len)); TINYCLR_NOCLEANUP(); }
HRESULT Library_corlib_native_System_Text_UTF8Encoding::Helper__GetChars(CLR_RT_StackFrame& stack, bool fIndexed) { NATIVE_PROFILE_CLR_CORE(); TINYCLR_HEADER(); LPSTR szText; CLR_RT_HeapBlock ref; ref.SetObjectReference( NULL ); CLR_RT_ProtectFromGC gc( ref ); CLR_RT_HeapBlock_Array* pArrayBytes = stack.Arg1().DereferenceArray(); CLR_INT32 byteIdx = fIndexed ? stack.Arg2().NumericByRef().s4 : 0; CLR_INT32 byteCnt = fIndexed ? stack.Arg3().NumericByRef().s4 : pArrayBytes->m_numOfElements; CLR_RT_HeapBlock_Array* pArrayBytesCopy; CLR_RT_HeapBlock_Array* arrTmp; int cBytesCopy; FAULT_ON_NULL(pArrayBytes); _ASSERTE(pArrayBytes->m_typeOfElement == DATATYPE_U1); if((byteIdx + byteCnt) > (CLR_INT32)pArrayBytes->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE); cBytesCopy = byteCnt+1; /* Copy the array to a temporary buffer to create a zero-terminated string */ TINYCLR_CHECK_HRESULT( CLR_RT_HeapBlock_Array::CreateInstance( ref, cBytesCopy, g_CLR_RT_WellKnownTypes.m_UInt8 )); pArrayBytesCopy = ref.DereferenceArray(); szText = (LPSTR)pArrayBytesCopy->GetFirstElement(); hal_strncpy_s( szText, cBytesCopy, (LPSTR)pArrayBytes->GetElement(byteIdx), byteCnt ); TINYCLR_CHECK_HRESULT(Library_corlib_native_System_String::ConvertToCharArray( szText, stack.PushValueAndClear(), arrTmp, 0, -1 )); TINYCLR_NOCLEANUP(); }
HRESULT CLR_RT_HeapBlock_Queue::Dequeue( CLR_RT_HeapBlock*& value ) { NATIVE_PROFILE_CLR_CORE(); TINYCLR_HEADER(); CLR_RT_HeapBlock_Array* array = GetArray(); CLR_INT32 size = GetSize(); CLR_INT32 head = Head(); CLR_RT_HeapBlock* removed; if(size == 0) TINYCLR_SET_AND_LEAVE(CLR_E_INVALID_OPERATION); removed = (CLR_RT_HeapBlock*)array->GetElement( head ); value = removed->Dereference(); removed->SetObjectReference( NULL ); SetHead( (head + 1) % array->m_numOfElements ); SetSize( size - 1 ); TINYCLR_NOCLEANUP(); }
HRESULT Library_spot_net_security_native_Microsoft_SPOT_Net_Security_SslNative::ReadWriteHelper( CLR_RT_StackFrame& stack, bool isWrite ) { NATIVE_PROFILE_CLR_NETWORK(); TINYCLR_HEADER(); CLR_RT_HeapBlock* socket = stack.Arg0().Dereference(); CLR_RT_HeapBlock_Array* arrData = stack.Arg1().DereferenceArray(); CLR_INT32 offset = stack.Arg2().NumericByRef().s4; CLR_INT32 count = stack.Arg3().NumericByRef().s4; CLR_INT32 timeout_ms = stack.Arg4().NumericByRef().s4; CLR_UINT8* buffer; CLR_RT_HeapBlock hbTimeout; CLR_INT32 totReadWrite; bool fRes = true; CLR_INT64 *timeout; int result = 0; CLR_INT32 handle; if(count == 0) { stack.SetResult_I4( 0 ); TINYCLR_SET_AND_LEAVE(S_OK); } FAULT_ON_NULL(socket); handle = socket[ Library_spot_net_native_Microsoft_SPOT_Net_SocketNative::FIELD__m_Handle ].NumericByRef().s4; /* Because we could have been a rescheduled call due to a prior call that would have blocked, we need to see * if our handle has been shutdown before continuing. */ if (handle == Library_spot_net_native_Microsoft_SPOT_Net_SocketNative::DISPOSED_HANDLE) { ThrowError( stack, CLR_E_OBJECT_DISPOSED ); TINYCLR_SET_AND_LEAVE(CLR_E_PROCESS_EXCEPTION); } FAULT_ON_NULL(arrData); hbTimeout.SetInteger( timeout_ms ); TINYCLR_CHECK_HRESULT(stack.SetupTimeout( hbTimeout, timeout )); // // Push "totReadWrite" onto the eval stack. // if(stack.m_customState == 1) { stack.PushValueI4( 0 ); stack.m_customState = 2; } totReadWrite = stack.m_evalStack[ 1 ].NumericByRef().s4; buffer = arrData->GetElement( offset + totReadWrite ); count -= totReadWrite; if((offset + count + totReadWrite) > (int)arrData->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_INDEX_OUT_OF_RANGE); while(count > 0) { // first make sure we have data to read or ability to write while(fRes) { if(!isWrite) { // check SSL_DataAvailable() in case SSL has already read and buffered socket data result = SSL_DataAvailable(handle); if((result > 0) || ((result < 0) && (SOCK_getlasterror() != SOCK_EWOULDBLOCK))) { break; } } result = Library_spot_net_native_Microsoft_SPOT_Net_SocketNative::Helper__SelectSocket( handle, isWrite ? 1 : 0 ); if((result > 0) || ((result < 0) && (SOCK_getlasterror() != SOCK_EWOULDBLOCK))) { break; } // non-blocking - allow other threads to run while we wait for socket activity TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.WaitEvents( stack.m_owningThread, *timeout, CLR_RT_ExecutionEngine::c_Event_Socket, fRes )); // timeout expired if(!fRes) { result = SOCK_SOCKET_ERROR; ThrowError(stack, SOCK_ETIMEDOUT); TINYCLR_SET_AND_LEAVE( CLR_E_PROCESS_EXCEPTION ); } } // socket is in the excepted state, so let's bail out if(SOCK_SOCKET_ERROR == result) { break; } if(isWrite) { result = SSL_Write( handle, (const char*)buffer, count ); } else { result = SSL_Read( handle, (char*)buffer, count ); if(result == SSL_RESULT__WOULD_BLOCK) { continue; } } // ThrowOnError expects anything other than 0 to be a failure - so return 0 if we don't have an error if(result <= 0) { break; } buffer += result; totReadWrite += result; count -= result; // read is non-blocking if we have any data if(!isWrite && (totReadWrite > 0)) { break; } stack.m_evalStack[ 1 ].NumericByRef().s4 = totReadWrite; } stack.PopValue(); // totReadWrite stack.PopValue(); // Timeout if(result < 0) { TINYCLR_CHECK_HRESULT(ThrowOnError( stack, result )); } stack.SetResult_I4( totReadWrite ); TINYCLR_NOCLEANUP(); }
// Note that prefix needs to be atomized (with nametable) prior to calling LookupNamespace() CLR_RT_HeapBlock_String* CLR_RT_HeapBlock_XmlNamespaceStack::LookupNamespace( CLR_RT_HeapBlock_String* prefix ) { CLR_RT_HeapBlock_Array* array = GetArray(); CLR_INT32 size = GetSize(); CLR_INT32 capacity = array->m_numOfElements; for(int i = capacity - size; i < capacity; i++) { CLR_RT_HeapBlock_XmlNamespaceEntry* entry = (CLR_RT_HeapBlock_XmlNamespaceEntry*)((CLR_RT_HeapBlock*)array->GetElement( i ))->Dereference(); // since prefix is atomized, we only need to compare its reference. if(entry->GetPrefix() == prefix) { return entry->GetNamespaceURI(); } } return NULL; }
HRESULT Library_spot_net_native_Microsoft_SPOT_Net_SocketNative::SendRecvHelper( CLR_RT_StackFrame& stack, bool fSend, bool fAddress ) { NATIVE_PROFILE_CLR_NETWORK(); TINYCLR_HEADER(); CLR_RT_HeapBlock* socket = stack.Arg0().Dereference(); CLR_INT32 handle; CLR_RT_HeapBlock_Array* arrData = stack.Arg1().DereferenceArray(); CLR_UINT32 offset = stack.Arg2().NumericByRef().u4; CLR_UINT32 count = stack.Arg3().NumericByRef().u4; CLR_INT32 flags = stack.Arg4().NumericByRef().s4; CLR_INT32 timeout_ms = stack.ArgN(5).NumericByRef().s4; CLR_RT_HeapBlock hbTimeout; CLR_INT64* timeout; CLR_UINT8* buf; bool fRes = true; CLR_INT32 totReadWrite; CLR_INT32 ret = 0; FAULT_ON_NULL(socket); handle = socket[ FIELD__m_Handle ].NumericByRef().s4; FAULT_ON_NULL(arrData); if(offset + count > arrData->m_numOfElements) TINYCLR_SET_AND_LEAVE(CLR_E_INDEX_OUT_OF_RANGE); /* Because we could have been a rescheduled call due to a prior call that would have blocked, we need to see * if our handle has been shutdown before continuing. */ if (handle == DISPOSED_HANDLE) { ThrowError( stack, CLR_E_OBJECT_DISPOSED ); TINYCLR_SET_AND_LEAVE (CLR_E_PROCESS_EXCEPTION); } hbTimeout.SetInteger( timeout_ms ); TINYCLR_CHECK_HRESULT(stack.SetupTimeout( hbTimeout, timeout )); // // Push "totReadWrite" onto the eval stack. // if(stack.m_customState == 1) { stack.PushValueI4( 0 ); stack.m_customState = 2; } totReadWrite = stack.m_evalStack[ 1 ].NumericByRef().s4; buf = arrData->GetElement( offset + totReadWrite ); count -= totReadWrite; while(count > 0) { CLR_INT32 bytes = 0; // first make sure we have data to read or ability to write while(fRes) { ret = Helper__SelectSocket( handle, fSend ? 1 : 0 ); if(ret != 0) break; // non-blocking - allow other threads to run while we wait for handle activity TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.WaitEvents( stack.m_owningThread, *timeout, CLR_RT_ExecutionEngine::c_Event_Socket, fRes )); } // timeout expired if(!fRes) { ret = SOCK_SOCKET_ERROR; ThrowError( stack, SOCK_ETIMEDOUT ); TINYCLR_SET_AND_LEAVE( CLR_E_PROCESS_EXCEPTION ); } // socket is in the excepted state, so let's bail out if(SOCK_SOCKET_ERROR == ret) { break; } if(fAddress) { struct SOCK_sockaddr addr; CLR_UINT32 addrLen = sizeof(addr); CLR_RT_HeapBlock& blkAddr = stack.ArgN( 6 ); if(fSend) { TINYCLR_CHECK_HRESULT(MarshalSockAddress( &addr, addrLen, blkAddr )); bytes = SOCK_sendto( handle, (const char*)buf, count, flags, &addr, addrLen ); } else { CLR_RT_HeapBlock* pBlkAddr = blkAddr.Dereference(); TINYCLR_CHECK_HRESULT(MarshalSockAddress( &addr, addrLen, *pBlkAddr )); bytes = SOCK_recvfrom( handle, (char*)buf, count, flags, &addr, (int*)&addrLen ); if(bytes != SOCK_SOCKET_ERROR) { TINYCLR_CHECK_HRESULT(MarshalSockAddress( blkAddr, &addr, addrLen )); } } } else { if(fSend) { bytes = SOCK_send( handle, (const char*)buf, count, flags ); } else { bytes = SOCK_recv( handle, (char*)buf, count, flags ); } } // send/recv/sendto/recvfrom failed if(bytes == SOCK_SOCKET_ERROR) { CLR_INT32 err = SOCK_getlasterror(); if(err != SOCK_EWOULDBLOCK) { ret = SOCK_SOCKET_ERROR; break; } continue; } // zero recv bytes indicates the handle has been closed. else if(!fSend && (bytes == 0)) { break; } buf += bytes; totReadWrite += bytes; count -= bytes; stack.m_evalStack[ 1 ].NumericByRef().s4 = totReadWrite; // receive returns immediately after receiving bytes. if(!fSend && (totReadWrite > 0)) { break; } } stack.PopValue(); // totReadWrite stack.PopValue(); // Timeout TINYCLR_CHECK_HRESULT(ThrowOnError( stack, ret )); stack.SetResult_I4( totReadWrite ); TINYCLR_NOCLEANUP(); }
void CLR_RT_StackFrame::Pop() { NATIVE_PROFILE_CLR_CORE(); #if defined(TINYCLR_PROFILE_NEW_CALLS) { // // This passivates any outstanding handler. // CLR_PROF_HANDLER_CALLCHAIN(pm2,m_callchain); m_callchain.Leave(); } #endif #if defined(TINYCLR_PROFILE_NEW_CALLS) g_CLR_PRF_Profiler.RecordFunctionReturn( m_owningThread, m_callchain ); #endif #if defined(TINYCLR_ENABLE_SOURCELEVELDEBUGGING) if(m_owningThread->m_fHasJMCStepper || (m_flags & c_HasBreakpoint)) { g_CLR_RT_ExecutionEngine.Breakpoint_StackFrame_Pop( this, false ); } #endif const CLR_UINT32 c_flagsToCheck = CLR_RT_StackFrame::c_CallOnPop | CLR_RT_StackFrame::c_Synchronized | CLR_RT_StackFrame::c_SynchronizedGlobally | CLR_RT_StackFrame::c_NativeProfiled; if(m_flags & c_flagsToCheck) { if(m_flags & CLR_RT_StackFrame::c_CallOnPop) { m_flags |= CLR_RT_StackFrame::c_CalledOnPop; if(m_nativeMethod) { (void)m_nativeMethod( *this ); } } if(m_flags & CLR_RT_StackFrame::c_Synchronized) { m_flags &= ~CLR_RT_StackFrame::c_Synchronized; (void)HandleSynchronized( false, false ); } if(m_flags & CLR_RT_StackFrame::c_SynchronizedGlobally) { m_flags &= ~CLR_RT_StackFrame::c_SynchronizedGlobally; (void)HandleSynchronized( false, true ); } #if defined(ENABLE_NATIVE_PROFILER) if(m_flags & CLR_RT_StackFrame::c_NativeProfiled) { m_owningThread->m_fNativeProfiled = false; m_flags &= ~CLR_RT_StackFrame::c_NativeProfiled; Native_Profiler_Stop(); } #endif } CLR_RT_StackFrame* caller = Caller(); if(caller->Prev() != NULL) { #if defined(TINYCLR_ENABLE_SOURCELEVELDEBUGGING) if(caller->m_flags & CLR_RT_StackFrame::c_HasBreakpoint) { g_CLR_RT_ExecutionEngine.Breakpoint_StackFrame_Step( caller, caller->m_IP ); } #endif // // Constructors are slightly different, they push the 'this' pointer back into the caller stack. // // This is to enable the special case for strings, where the object can be recreated by the constructor... // if(caller->m_flags & CLR_RT_StackFrame::c_ExecutingConstructor) { CLR_RT_HeapBlock& src = this ->Arg0 ( ); CLR_RT_HeapBlock& dst = caller->PushValueAndAssign( src ); dst.Promote(); // // Undo the special "object -> reference" hack done by CEE_NEWOBJ. // if(dst.DataType() == DATATYPE_BYREF) { dst.ChangeDataType( DATATYPE_OBJECT ); } caller->m_flags &= ~CLR_RT_StackFrame::c_ExecutingConstructor; _ASSERTE((m_flags & CLR_RT_StackFrame::c_AppDomainTransition) == 0); } else { //Note that ExecutingConstructor is checked on 'caller', whereas the other two flags are checked on 'this' const CLR_UINT32 c_moreFlagsToCheck = CLR_RT_StackFrame::c_PseudoStackFrameForFilter | CLR_RT_StackFrame::c_AppDomainTransition; if(m_flags & c_moreFlagsToCheck) { if(m_flags & CLR_RT_StackFrame::c_PseudoStackFrameForFilter) { //Do nothing here. Pushing return values onto stack frames that don't expect them are a bad idea. } #if defined(TINYCLR_APPDOMAINS) else if((m_flags & CLR_RT_StackFrame::c_AppDomainTransition) != 0) { (void)PopAppDomainTransition(); } #endif } else //!c_moreFlagsToCheck { // // Push the return, if any. // if(m_call.m_target->retVal != DATATYPE_VOID) { if(m_owningThread->m_currentException.Dereference() == NULL) { CLR_RT_HeapBlock& src = this ->TopValue ( ); CLR_RT_HeapBlock& dst = caller->PushValueAndAssign( src ); dst.Promote(); } } } } } #if defined(TINYCLR_ENABLE_SOURCELEVELDEBUGGING) else { int idx = m_owningThread->m_scratchPad; if(idx >= 0) { CLR_RT_HeapBlock_Array* array = g_CLR_RT_ExecutionEngine.m_scratchPadArray; if(array && array->m_numOfElements > (CLR_UINT32)idx) { CLR_RT_HeapBlock* dst = (CLR_RT_HeapBlock*)array->GetElement( (CLR_UINT32)idx ); CLR_RT_HeapBlock* exception = m_owningThread->m_currentException.Dereference(); dst->SetObjectReference( NULL ); if(exception != NULL) { dst->SetObjectReference( exception ); } else if(m_call.m_target->retVal != DATATYPE_VOID) { CLR_RT_SignatureParser sig; sig.Initialize_MethodSignature( this->m_call.m_assm, this->m_call.m_target ); CLR_RT_SignatureParser::Element res; CLR_RT_TypeDescriptor desc; dst->Assign( this->TopValue() ); //Perform boxing, if needed. //Box to the return value type _SIDE_ASSERTE(SUCCEEDED(sig.Advance( res ))); _SIDE_ASSERTE(SUCCEEDED(desc.InitializeFromType( res.m_cls ))); if(c_CLR_RT_DataTypeLookup[ this->DataType() ].m_flags & CLR_RT_DataTypeLookup::c_OptimizedValueType || desc.m_handlerCls.m_target->IsEnum() ) { if(FAILED(dst->PerformBoxing( desc.m_handlerCls ))) { dst->SetObjectReference( NULL ); } } } } } } #endif // // We could be jumping outside of a nested exception handler. // m_owningThread->PopEH( this, NULL ); // // If this StackFrame owns a SubThread, kill it. // { CLR_RT_SubThread* sth = (CLR_RT_SubThread*)m_owningSubThread->Next(); if(sth->Next() && sth->m_owningStackFrame == this) { CLR_RT_SubThread::DestroyInstance( sth->m_owningThread, sth, CLR_RT_SubThread::MODE_IncludeSelf ); } } g_CLR_RT_EventCache.Append_Node( this ); }
HRESULT Library_spot_net_security_native_Microsoft_SPOT_Net_Security_SslNative::InitHelper( CLR_RT_StackFrame& stack, bool isServer ) { NATIVE_PROFILE_CLR_NETWORK(); TINYCLR_HEADER(); CLR_INT32 sslContext = -1; CLR_INT32 sslMode = stack.Arg0().NumericByRef().s4; CLR_INT32 sslVerify = stack.Arg1().NumericByRef().s4; CLR_RT_HeapBlock *hbCert = stack.Arg2().Dereference(); CLR_RT_HeapBlock_Array* arrCA = stack.Arg3().DereferenceArray(); CLR_RT_HeapBlock_Array* arrCert = NULL; CLR_UINT8* sslCert = NULL; int result; int i; bool isFirstCall = false; LPCSTR szPwd = ""; if(!g_SSL_SeedData.Initialized) { BOOL fOK = FALSE; isFirstCall = true; #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) int i; if(!HAL_CONFIG_BLOCK::ApplyConfig( g_SSL_SeedData.Config.GetDriverName(), &g_SSL_SeedData.Config, sizeof(g_SSL_SeedData.Config) )) { return CLR_E_NOT_SUPPORTED; } // validate the security key (make sure it isn't all 0x00 or all 0xFF for(i=1; i<sizeof(g_SSL_SeedData.Config.SslSeedKey) && !fOK; i++) { if( g_SSL_SeedData.Config.SslSeedKey[ i ] != 0 && g_SSL_SeedData.Config.SslSeedKey[ i ] != 0xFF && g_SSL_SeedData.Config.SslSeedKey[ i-1 ] != g_SSL_SeedData.Config.SslSeedKey[ i ]) { fOK = TRUE; } } if(!fOK) { return CLR_E_NOT_SUPPORTED; } #endif g_SSL_SeedData.m_completion.Initialize(); g_SSL_SeedData.m_completion.InitializeForUserMode( UpdateSslSeedValue, NULL ); g_SSL_SeedData.Initialized = TRUE; } if(hbCert != NULL) { arrCert = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_certificate ].DereferenceArray(); //FAULT_ON_NULL(arrCert); // If arrCert == NULL then the certificate is an X509Certificate2 which uses a certificate handle if(arrCert == NULL) { arrCert = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_handle ].DereferenceArray(); FAULT_ON_NULL(arrCert); // pass the certificate handle as the cert data parameter sslCert = arrCert->GetFirstElement(); arrCert = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_sessionHandle ].DereferenceArray(); FAULT_ON_NULL(arrCert); // pass the session handle as the ssl context parameter sslContext = *(INT32*)arrCert->GetFirstElement(); // the certificate has already been loaded so just pass an empty string szPwd = ""; } else { arrCert->Pin(); sslCert = arrCert->GetFirstElement(); CLR_RT_HeapBlock *hbPwd = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_password ].Dereference();// FAULT_ON_NULL(hbPwd); szPwd = hbPwd->StringText(); } } SSL_RegisterTimeCallback( Time_GetDateTime ); if(isServer) { result = (SSL_ServerInit( sslMode, sslVerify, (const char*)sslCert, sslCert == NULL ? 0 : arrCert->m_numOfElements, szPwd, sslContext ) ? 0 : -1); } else { result = (SSL_ClientInit( sslMode, sslVerify, (const char*)sslCert, sslCert == NULL ? 0 : arrCert->m_numOfElements, szPwd, sslContext ) ? 0 : -1); } TINYCLR_CHECK_HRESULT(ThrowOnError( stack, result )); if(isFirstCall) { GenerateNewSslSeed(); } if(arrCA != NULL) { for(i=0; i<(int)arrCA->m_numOfElements; i++) { hbCert = (CLR_RT_HeapBlock*)arrCA->GetElement( i ); FAULT_ON_NULL(hbCert); hbCert = hbCert->Dereference(); FAULT_ON_NULL(hbCert); arrCert = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_certificate ].DereferenceArray(); //FAULT_ON_NULL(arrCert); // If arrCert == NULL then the certificate is an X509Certificate2 which uses a certificate handle if(arrCert == NULL) { CLR_INT32 sessionCtx = 0; arrCert = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_handle ].DereferenceArray(); FAULT_ON_NULL(arrCert); sslCert = arrCert->GetFirstElement(); arrCert = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_sessionHandle ].DereferenceArray(); FAULT_ON_NULL(arrCert); sessionCtx = *(INT32*)arrCert->GetFirstElement(); // pass the session handle down as the password paramter and the certificate handle as the data parameter result = (SSL_AddCertificateAuthority( sslContext, (const char*)sslCert, arrCert->m_numOfElements, (LPCSTR)&sessionCtx ) ? 0 : -1); TINYCLR_CHECK_HRESULT(ThrowOnError( stack, result )); } else { arrCert->Pin(); sslCert = arrCert->GetFirstElement(); CLR_RT_HeapBlock *hbPwd = hbCert[ Library_spot_native_System_Security_Cryptography_X509Certificates_X509Certificate::FIELD__m_password ].Dereference(); FAULT_ON_NULL(hbPwd); LPCSTR szCAPwd = hbPwd->StringText(); result = (SSL_AddCertificateAuthority( sslContext, (const char*)sslCert, arrCert->m_numOfElements, szCAPwd ) ? 0 : -1); TINYCLR_CHECK_HRESULT(ThrowOnError( stack, result )); } } } stack.SetResult_I4( sslContext ); TINYCLR_CLEANUP(); if(FAILED(hr) && (sslContext != -1)) { SSL_ExitContext( sslContext ); } TINYCLR_CLEANUP_END(); }