Пример #1
0
exports.SCardTransmit = function PCSC_SCardTransmit(cardId, protocol, command, offset, length) {

	var pioSendPci = lib.pcsc_t.SCARD_IO_REQUEST();
	pioSendPci.dwProtocol = ctypes.unsigned_long(protocol);
	pioSendPci.cbPciLength = ctypes.unsigned_long(8);    	
	
	var cmd = util.sliceArray(command, 0, length);
	var pbSendBuffer = lib.pcsc_t.BYTE.array()(cmd);
	
	// lib.log("SCardTransmit: pbSendBuffer: "+pbSendBuffer);
	// lib.log("SCardTransmit: state: "+ toHexString(pdwState.value));
	// lib.log("SCardTransmit: protocol: "+ toHexString(pdwProtocol.value));
	    	
	/*
	 * cbSendLength: The length, in bytes, of the pbSendBuffer parameter.
	 * 
	 * For T=0, in the special case where no data is sent to the card and no
	 * data expected in return, this length must reflect that the bP3 member
	 * is not being sent; the length should be sizeof(CmdBytes) -
	 * sizeof(BYTE).
	 */
	var cbSendLength = lib.pcsc_t.DWORD(length);
	
	/*
	 * pioRecvPci [in, out, optional] Pointer to the protocol header
	 * structure for the instruction, followed by a buffer in which to
	 * receive any returned protocol control information (PCI) specific to
	 * the protocol in use. This parameter can be NULL if no PCI is
	 * returned.
	 */
	var pioRecvPci = null;
	
	var pbRecvBuffer = lib.pcsc_t.BYTE.array(RECEIVE_BUFFER_SIZE)();
	var pcbRecvLength = lib.pcsc_t.DWORD(RECEIVE_BUFFER_SIZE);
	
	console.debug("pcsc: SCardTransmit() - APDU command = "+util.ArrayToHexString(cmd));
	
	var status = lib.pcsc.SCardTransmit(cardId, 
			pioSendPci.address(), 
			pbSendBuffer.addressOfElement(offset), 
			cbSendLength, 
			null, 
			pbRecvBuffer.addressOfElement(0), 
			pcbRecvLength.address() );
	
	
	status = toUnsignedNumber(status);
	
	if (status == lib.pcsc_t.SCARD_S_SUCCESS) {
		var response = util.sliceArray(pbRecvBuffer, 0, pcbRecvLength.value);
		console.debug("pcsc: SCardTransmit() - APDU response = "+util.ArrayToHexString(response));
		return response;
	} else {
		throw new PCSCException(status, toErrorString(status));
	}
};
Пример #2
0
exports.SCardStatus = function PCSC_SCardStatus(cardId) {
    	
	var mszReaderName = ctypes.char.array(READERNAME_BUFFER_SIZE)();
	var pcchReaderLen = lib.pcsc_t.DWORD(READERNAME_BUFFER_SIZE);
	var pdwState = lib.pcsc_t.DWORD();
	var pdwProtocol = lib.pcsc_t.DWORD();
	var pbAtr = lib.pcsc_t.BYTE.array(ATR_BUFFER_SIZE)();
	var pcbAtrLen = lib.pcsc_t.DWORD(ATR_BUFFER_SIZE);
	
	var status = lib.pcsc.SCardStatus(cardId, mszReaderName.addressOfElement(0), pcchReaderLen.address(), pdwState.address(), pdwProtocol.address(), pbAtr.addressOfElement(0), pcbAtrLen.address());
	
	status = toUnsignedNumber(status);
	
	if (status == lib.pcsc_t.SCARD_S_SUCCESS) {
		// var reader = util.sliceArray(mszReaderName, 0, pcchReaderLen.value);
		
		var atr = util.sliceArray(pbAtr, 0, pcbAtrLen.value);
		// lib.log("SCardStatus: atr: "+atr);
		
		// TODO Documentar y revisar si se devuelve el reader también
		let result = { 
				atr: atr, 
				protocol: pdwProtocol.value, 
				state: pdwState.value
		};
		
//		lib.log("SCardStatus: atr = "+result.atr);
//		lib.log("SCardStatus: protocol = "+ toHexString(result.protocol));
//		lib.log("SCardStatus: state = "+ toHexString(result.state));
		return result;
	} else {
		throw new PCSCException(status, toErrorString(status));
	}
};
Пример #3
0
exports.SCardControl = function PCSC_SCardControl(cardId, controlCode, command) {
	   	
	var hCard = lib.pcsc_t.SCARDHANDLE(cardId);
	
	/**
	 * Control code for the operation. This value identifies the specific
	 * operation to be performed.
	 */
	var dwControlCode = lib.pcsc_t.DWORD(controlCode);
	
	/**
	 * Pointer to a buffer that contains the data required to perform the
	 * operation. This parameter can be NULL if the dwControlCode parameter
	 * specifies an operation that does not require input data.
	 */
	var pbSendBuffer = lib.pcsc_t.BYTE.array()(command);
	
	/**
	 * Size, in bytes, of the buffer pointed to by lpInBuffer.
	 */
	var cbSendLength = lib.pcsc_t.DWORD(command.length);
	
	/**
	 * Pointer to a buffer that receives the operation's output data. This
	 * parameter can be NULL if the dwControlCode parameter specifies an
	 * operation that does not produce output data.
	 */
	var pbRecvBuffer = lib.pcsc_t.BYTE.array(MAX_STACK_BUFFER_SIZE)();
	
	/**
	 * Size, in bytes, of the buffer pointed to by lpOutBuffer.
	 */
	var cbRecvLength = lib.pcsc_t.DWORD(MAX_STACK_BUFFER_SIZE);
	
	/**
	 * Pointer to a DWORD that receives the size, in bytes, of the data
	 * stored into the buffer pointed to by lpOutBuffer.
	 */
	var lpBytesReturned = lib.pcsc_t.DWORD();
	
	
	var status = lib.pcsc.SCardControl(hCard, dwControlCode, 
			pbSendBuffer.address(), 
			cbSendLength, 
			pbRecvBuffer.addressOfElement(0), 
			cbRecvLength, 
			lpBytesReturned.address() );
	
	status = toUnsignedNumber(status);
//	    	lib.log("SCardControl: hCard="+hCard);
//	    	lib.log("SCardControl: dwControlCode="+dwControlCode.value);
//	    	lib.log("SCardControl: cbRecvLength="+cbRecvLength.value);
//	    	lib.log("SCardControl: cbRecvLength="+cbRecvLength.value);
//	    	lib.log("SCardControl: lpBytesReturned="+lpBytesReturned.value);
	
	if (status == lib.pcsc_t.SCARD_S_SUCCESS) {
		var receiveBuffer = util.sliceArray(pbRecvBuffer, 0, lpBytesReturned.value);
		
		return receiveBuffer;
	} else {
		throw new PCSCException(status, toErrorString(status));
	}
};