Skip to content

Commit

Permalink
update python-publish.yml (#5)
Browse files Browse the repository at this point in the history
Co-authored-by: zhangjie <[email protected]>
  • Loading branch information
j-z10 and zhangjie authored Mar 26, 2024
1 parent c38d097 commit dcdf3e6
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/post-attach.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
git clone https://github.com/guanzhi/GmSSL.git
git clone -b 'v3.1.1' --depth 1 https://github.com/guanzhi/GmSSL.git
cd GmSSL && mkdir build && cd build && cmake ..
make && make test && sudo make install
sudo ldconfig
Expand Down
11 changes: 6 additions & 5 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: "3.11"
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
pip install poetry
poetry install
- name: Build package
run: python -m build
run: poetry build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![codecov](https://codecov.io/gh/j-z10/pygmssl/graph/badge.svg?token=PS29GCO00T)](https://codecov.io/gh/j-z10/pygmssl)
# pygmssl
A Python ctypes [GmSSL](https://github.com/guanzhi/GmSSL) implementation

A Python ctypes [GmSSL](https://github.com/guanzhi/GmSSL)v3.1.1 implementation
=======

## INSTALL
Expand Down
4 changes: 2 additions & 2 deletions src/pygmssl/_gm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from ctypes import cdll, c_char_p
from ctypes.util import find_library

if sys.platform == 'win32':
if sys.platform == 'win32': # pragma: no cover
libc = cdll.LoadLibrary(find_library('msvcrt'))
else:
libc = cdll.LoadLibrary(find_library('c'))

libgm = find_library('gmssl')

if not libgm:
if not libgm: # pragma: no cover
warnings.warn("gmssl library not found, you should install GmSSL first.\n"
"https://github.com/guanzhi/GmSSL")
sys.exit(1)
Expand Down
13 changes: 4 additions & 9 deletions src/pygmssl/sm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from ._gm import _gm, libc
from .sm3 import _SM3CTX
from .exceptions import GmSSLException

SM2_DEFAULT_ID = b'1234567812345678'
SM2_MIN_SIGNATURE_SIZE = 8
Expand Down Expand Up @@ -134,8 +133,7 @@ def export_encrypted_private_key_to_pem(self, password: bytes) -> bytes:
with tempfile.NamedTemporaryFile(delete=False) as tmp_f:
libc.fopen.restype = c_void_p
fp = libc.fopen(tmp_f.name.encode('utf8'), 'wb')
if _gm.sm2_private_key_info_encrypt_to_pem(byref(self._sm2_key), c_char_p(password), c_void_p(fp)) != 1:
raise GmSSLException('libgmssl inner error')
assert _gm.sm2_private_key_info_encrypt_to_pem(byref(self._sm2_key), c_char_p(password), c_void_p(fp)) == 1
libc.fclose(c_void_p(fp))
with open(tmp_f.name, 'rb') as f:
res = f.read()
Expand All @@ -145,8 +143,7 @@ def export_public_key_to_pem(self) -> bytes:
with tempfile.NamedTemporaryFile(delete=False) as tmp_f:
libc.fopen.restype = c_void_p
fp = libc.fopen(tmp_f.name.encode('utf8'), 'wb')
if _gm.sm2_public_key_info_to_pem(byref(self._sm2_key), c_void_p(fp)) != 1:
raise GmSSLException('libgmssl inner error')
assert _gm.sm2_public_key_info_to_pem(byref(self._sm2_key), c_void_p(fp)) == 1
libc.fclose(c_void_p(fp))
with open(tmp_f.name, 'rb') as f:
res = f.read()
Expand All @@ -160,8 +157,7 @@ def import_private_from_pem(cls, pem: bytes, password: bytes) -> 'SM2':
libc.fopen.restype = c_void_p
fp = libc.fopen(tmp_f.name.encode('utf8'), 'rb')
obj = SM2()
if _gm.sm2_private_key_info_decrypt_from_pem(byref(obj._sm2_key), c_char_p(password), c_void_p(fp)) != 1:
raise GmSSLException('Invalid Password')
assert _gm.sm2_private_key_info_decrypt_from_pem(byref(obj._sm2_key), c_char_p(password), c_void_p(fp)) == 1
libc.fclose(c_void_p(fp))
return obj

Expand All @@ -173,7 +169,6 @@ def import_public_from_pem(cls, pem: bytes) -> 'SM2':
libc.fopen.restype = c_void_p
fp = libc.fopen(tmp_f.name.encode('utf8'), 'rb')
obj = SM2()
if _gm.sm2_public_key_info_from_pem(byref(obj._sm2_key), c_void_p(fp)) != 1:
raise GmSSLException('Invalid Public Key')
assert _gm.sm2_public_key_info_from_pem(byref(obj._sm2_key), c_void_p(fp)) == 1
libc.fclose(c_void_p(fp))
return obj
2 changes: 0 additions & 2 deletions src/pygmssl/sm4.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ class MOD(str, Enum):

class SM4:
def __init__(self, key: bytes, *, mode: MOD, iv: bytes):
if mode.value.upper() not in _MOD_CTX_DICT:
raise ValueError(u'Only support sm4 mod: %s' % _MOD_CTX_DICT.keys())
self._ctx = _MOD_CTX_DICT[mode.value.upper()]()
self.key = key
self.iv = iv
Expand Down
10 changes: 10 additions & 0 deletions tests/test_gm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pygmssl import get_gmssl_version_num, get_gmssl_version_str
from unittest import TestCase


class TestSM2(TestCase):
def test_get_gmssl_version_str(self):
assert get_gmssl_version_str()

def test_get_gmssl_version_num(self):
assert get_gmssl_version_num() > 0
8 changes: 8 additions & 0 deletions tests/test_sm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,11 @@ def test_pub_pem_export_and_import(self):
assert new_obj.pri_key == b'\x00' * 32
assert new_obj.pub_key != b'\x00' * 64
assert new_obj.pub_key == obj.pub_key

def test_error_import_private_pem(self):
password = b'test-123-456'
obj = SM2.generate_new_pair()
assert obj.pub_key != b'\x00' * 64
assert obj.pri_key != b'\x00' * 32
with self.assertRaises(Exception):
SM2.import_private_from_pem(obj.export_encrypted_private_key_to_pem(password), b'wrong-password')

0 comments on commit dcdf3e6

Please sign in to comment.