An open API service indexing awesome lists of open source software.

https://github.com/stargate01/pyglobalplatform

High-level API and Swig Python binding for the globalplatform library
https://github.com/stargate01/pyglobalplatform

Last synced: 6 months ago
JSON representation

High-level API and Swig Python binding for the globalplatform library

Awesome Lists containing this project

README

          

# pylobalplatform

High-level API and Swig Python binding for the GlobalPlatform (https://github.com/kaoh/globalplatform) library.

Not to be confused with https://github.com/JavaCardOS/pyGlobalPlatform , which is widely out of date.

## Requirements

Currently, only Linux is supported by the build system.

Python 3 with `setuptools`, a C toolchain, `swig`, and `pkg-config` need to be available.

The `pcsclite` and `globalplatform` libraries need to be available via pkg-config.

## Installation

`pip install .`

## Usage

The library is split into two parts: The native library adapter `globalplatform.native` which closely mirrors the interface of the upstream `globalplatform` C API, and the higher-level library `globalplatform.shell`, which follows the semantics of the upstream `gpshell` tool.

### High-level shell

The high-level shell handles memory management and does proper marshalling of all python types. It still uses `bytearray()` internally, but no longer does modifications in ByRef-passed buffers - instead the actual results are properly returned.

The class `GP211Shell` implements a subset of the `gpshell` supported commands for GP 2.1.1 , and follows the same semantics.

```python
from globalplatform import shell, native

gps = shell.GP211Shell()

gps.establish_context()
gps.card_connect("your reader")

gps.select(shell.ISD_AID)
keySet = shell.KeySet.plain() # Default keys
gps.open_sc(keySet, securityLevel = native.GP211_SCP02_SECURITY_LEVEL_C_DEC_C_MAC_R_MAC)

gps.install("applet.cap")

print(gps.get_status())

gps.card_disconnect()
gps.release_context()
```

### Native adapter

Since this is just an autogenerated SWIG wrapper, the API is the same as for the C `globalplatform` one.

All `PBYTE` and `BYTE*` (`unsigned char *`) buffer pointers are marshalled to Python as mutable `bytearray()`. Fixed-size `BYTE[N]` arrays are also marshalled as `bytearray()`, but their length constraints are enforced. `OPGP_STRING` is marshalled the same as `PBYTE`, `OPGP_CSTRING` is treated as a readonly input string.

Pointer function helpers are generated for `DWORD`, which enable handling `PDWORD` pointers.

All functions which return a `OPGP_ERROR_STATUS` result are checked, and a `OPGPError` Exception is raised if the status requires it.

Array helper functions are generated for the structs `GP211_APPLICATION_DATA`, `GP211_EXECUTABLE_MODULES_DATA`, and `OPGP_AID`.

The wrapper is compiled with threading support, such that the Python GIL is released when library functions are entered.