Beispiel #1
0
//export webRdpBitmapDecompress
func webRdpBitmapDecompress(context *C.rdpContext, bitmap *C.rdpBitmap, data *C.BYTE,
	width C.int, height C.int, bpp C.int, length C.int,
	compressed C.BOOL, codecId C.int) C.BOOL {
	log.Printf("compressed:%d bpp:%d", compressed, bpp)
	size := width * height * 4
	if bitmap.data != nil {
		C._aligned_free(unsafe.Pointer(bitmap.data))
	}
	bitmap.data = (*C.BYTE)(C._aligned_malloc(C.size_t(size), 16))
	if compressed != false {
		if bpp < 32 {
			C.freerdp_client_codecs_prepare(context.codecs, C.FREERDP_CODEC_INTERLEAVED)
			C.interleaved_decompress(context.codecs.interleaved, data, C.UINT32(length), bpp,
				&(bitmap.data), C.PIXEL_FORMAT_XRGB32, -1, 0, 0, width, height, C.getBYTEpalette(context))
		} else {
			C.freerdp_client_codecs_prepare(context.codecs, C.FREERDP_CODEC_PLANAR)
			status := C.planar_decompress(context.codecs.planar, data, C.UINT32(length),
				&(bitmap.data), C.PIXEL_FORMAT_XRGB32, -1, 0, 0, width, height, true)
			log.Printf("webRdpBitmapDecompress status::::::%d", status)
		}
	} else {
		C.freerdp_image_flip(data, bitmap.data, width, height, bpp)
	}
	bitmap.compressed = false
	bitmap.length = C.UINT32(size)
	bitmap.bpp = 32
	return true
}
Beispiel #2
0
func (nv *NVRAM) Write(contents []byte) error {
	if result := C.Tspi_SetAttribUint32(nv.handle, C.TSS_TSPATTRIB_NV_INDEX, 0, C.UINT32(nv.Index)); isError(result) {
		return Error{result}
	}
	if result := C.Tspi_NV_WriteValue(C.TSS_HNVSTORE(nv.handle), 0 /* offset */, C.UINT32(len(contents)), (*C.BYTE)(&contents[0])); isError(result) {
		return Error{result}
	}
	return nil
}
Beispiel #3
0
func (nv *NVRAM) setAttributes() error {
	if result := C.Tspi_SetAttribUint32(nv.handle, C.TSS_TSPATTRIB_NV_INDEX, 0, C.UINT32(nv.Index)); isError(result) {
		return Error{result}
	}
	if result := C.Tspi_SetAttribUint32(nv.handle, C.TSS_TSPATTRIB_NV_PERMISSIONS, 0, C.UINT32(nv.Permissions)); isError(result) {
		return Error{result}
	}
	if result := C.Tspi_SetAttribUint32(nv.handle, C.TSS_TSPATTRIB_NV_DATASIZE, 0, C.UINT32(nv.Size)); isError(result) {
		return Error{result}
	}

	return nil
}
Beispiel #4
0
func (ctx *TPMContext) Random(n uint32) (rdata []byte, err error) {
	var randbytes *C.BYTE
	result := C.Tspi_TPM_GetRandom(ctx.tpm, C.UINT32(n), &randbytes)
	if result != C.TSS_SUCCESS {
		switch result {
		case C.TSS_E_INVALID_HANDLE:
			return nil, ErrInvalidHandle
		case C.TSS_E_INTERNAL_ERROR:
			return nil, ErrInternalError
		case C.TSS_E_BAD_PARAMETER:
			return nil, ErrBadParameter
		default:
			return nil, ErrUnknown

		}
	}

	rand := C.malloc(C.size_t(n))
	if rand != nil {
		C.memcpy(rand, unsafe.Pointer(randbytes), C.size_t(n))
		rdata = C.GoBytes(rand, C.int(n))
		C.free(rand)
	}
	C.Tspi_Context_FreeMemory(ctx.ctx, randbytes)
	return

}
Beispiel #5
0
// GetEventLog returns an array of structures representing the contents of the
// TSS event log
func (tpm *TPM) GetEventLog() ([]Log, error) {
	var count C.UINT32
	var events *C.TSS_PCR_EVENT
	var event C.TSS_PCR_EVENT

	err := tspiError(C.Tspi_TPM_GetEventLog(tpm.handle, &count, &events))
	if err != nil {
		return nil, err
	}

	if count == 0 {
		return nil, nil
	}

	log := make([]Log, count)
	length := count * C.UINT32(unsafe.Sizeof(event))
	slice := (*[1 << 30]C.TSS_PCR_EVENT)(unsafe.Pointer(events))[:length:length]

	for i := 0; i < int(count); i++ {
		log[i].Pcr = int(slice[i].ulPcrIndex)
		log[i].Eventtype = int(slice[i].eventType)
		log[i].PcrValue = C.GoBytes(unsafe.Pointer(slice[i].rgbPcrValue), C.int(slice[i].ulPcrValueLength))
		log[i].Event = C.GoBytes(unsafe.Pointer(slice[i].rgbEvent), C.int(slice[i].ulEventLength))
	}
	C.Tspi_Context_FreeMemory(tpm.context, (*C.BYTE)(unsafe.Pointer(events)))
	return log, nil
}
Beispiel #6
0
func (nv *NVRAM) Read(out []byte) (int, error) {
	if result := C.Tspi_SetAttribUint32(nv.handle, C.TSS_TSPATTRIB_NV_INDEX, 0, C.UINT32(nv.Index)); isError(result) {
		return 0, Error{result}
	}
	l32 := C.UINT32(len(out))
	var buf *C.BYTE
	if result := C.Tspi_NV_ReadValue(C.TSS_HNVSTORE(nv.handle), 0 /* offset */, &l32, &buf); isError(result) {
		return 0, Error{result}
	}
	l := int(l32)
	if l > len(out) {
		l = len(out)
	}
	if l < 0 {
		l = 0
	}
	C.memcpy(unsafe.Pointer(&out[0]), unsafe.Pointer(buf), C.size_t(l))
	return l, nil
}
Beispiel #7
0
// ExtendPCR extends a pcr. If event is nil, data must be pre-hashed with
// SHA1. If event is not nil, event is used to populate the TSS event
// log. If both data and event are provided, both will be used to create the
// extend hash.
func (tpm *TPM) ExtendPCR(pcr int, data []byte, eventtype int, event []byte) error {
	var outlen C.UINT32
	var pcrval *C.BYTE
	var eventstruct C.TSS_PCR_EVENT
	var err error

	shasum := sha1.Sum(data)

	if event != nil {
		var pcrdata *C.BYTE
		var pcrdatalen C.UINT32

		eventstruct.versionInfo.bMajor = 1
		eventstruct.versionInfo.bMinor = 2
		eventstruct.versionInfo.bRevMajor = 1
		eventstruct.versionInfo.bRevMinor = 0
		eventstruct.ulPcrIndex = C.UINT32(pcr)
		eventstruct.rgbPcrValue = (*C.BYTE)(&shasum[0])
		eventstruct.eventType = C.TSS_EVENTTYPE(eventtype)
		eventstruct.ulEventLength = C.UINT32(len(event))
		eventstruct.rgbEvent = (*C.BYTE)(&event[0])

		if data == nil || len(data) == 0 {
			pcrdata = nil
			pcrdatalen = C.UINT32(0)
		} else {
			pcrdata = (*C.BYTE)(&data[0])
			pcrdatalen = C.UINT32(len(data))
		}

		err = tspiError(C.Tspi_TPM_PcrExtend(tpm.handle, C.UINT32(pcr), pcrdatalen, pcrdata, &eventstruct, &outlen, &pcrval))
	} else {
		err = tspiError(C.Tspi_TPM_PcrExtend(tpm.handle, C.UINT32(pcr), C.UINT32(len(shasum)), (*C.BYTE)(&shasum[0]), nil, &outlen, &pcrval))
	}

	C.Tspi_Context_FreeMemory(tpm.context, pcrval)

	return err
}
Beispiel #8
0
// GetEventLog returns an array of structures representing the contents of the
// TSS event log
func (tpm *TPM) GetEventLog() ([]tspiconst.Log, error) {
	var count C.UINT32
	var events *C.TSS_PCR_EVENT
	var event C.TSS_PCR_EVENT
	var log []tspiconst.Log

	f, err := os.Open("/sys/kernel/security/tpm0/binary_bios_measurements")
	if err != nil {
		return nil, err
	}

	firmware_events := bufio.NewReader(f)

	for {
		var entry tspiconst.Log
		var datalen int32
		err := binary.Read(firmware_events, binary.LittleEndian, &entry.Pcr)
		if err == io.EOF {
			break
		}
		if err != nil {
			return nil, err
		}
		err = binary.Read(firmware_events, binary.LittleEndian, &entry.Eventtype)
		if err != nil {
			return nil, err
		}
		err = binary.Read(firmware_events, binary.LittleEndian, &entry.PcrValue)
		if err != nil {
			return nil, err
		}
		err = binary.Read(firmware_events, binary.LittleEndian, &datalen)
		if err != nil {
			return nil, err
		}
		data := make([]byte, datalen)
		err = binary.Read(firmware_events, binary.LittleEndian, &data)
		if err != nil {
			return nil, err
		}
		entry.Event = data[:]
		log = append(log, entry)
	}

	err = tspiError(C.Tspi_TPM_GetEventLog(tpm.handle, &count, &events))
	if err != nil {
		return nil, err
	}

	if count == 0 {
		return log, nil
	}

	length := count * C.UINT32(unsafe.Sizeof(event))
	slice := (*[1 << 30]C.TSS_PCR_EVENT)(unsafe.Pointer(events))[:length:length]

	for i := 0; i < int(count); i++ {
		var entry tspiconst.Log
		entry.Pcr = int32(slice[i].ulPcrIndex)
		entry.Eventtype = int32(slice[i].eventType)
		copy(entry.PcrValue[:], C.GoBytes(unsafe.Pointer(slice[i].rgbPcrValue), C.int(slice[i].ulPcrValueLength)))
		entry.Event = C.GoBytes(unsafe.Pointer(slice[i].rgbEvent), C.int(slice[i].ulEventLength))
		log = append(log, entry)
	}
	C.Tspi_Context_FreeMemory(tpm.context, (*C.BYTE)(unsafe.Pointer(events)))
	return log, nil
}