Skip to content

python下导出主密钥报OSError: exception: access violation writing 0x0000000000000024 #9

Description

@Jinkeee

python下导出主密钥报OSError: exception: access violation writing 0x0000000000000024‘;
image
请问该如何解决?

Activity

  1. sqs404 commented on Dec 31, 2023

    @sqs404

    同问

  2. KKKGod commented on Jan 2, 2024

    @KKKGod

    同问,sm9 sm2 的test都是这个类似问题

  3. monkeyyang commented on Mar 5, 2024

    @monkeyyang

    也是遇到了这个问题,请问楼主解决了吗?
    gmssl.sm2_private_key_info_decrypt_from_pem(byref(self), c_char_p(passwd), c_void_p(fp)) != 1:
    OSError: exception: access violation writing 0x0000000000000024

  4. 99tyw commented on Mar 26, 2024

    @99tyw

    也是遇到了这个问题,请问楼主解决了吗?
    gmssl.sm2_private_key_info_decrypt_from_pem(byref(self), c_char_p(passwd), c_void_p(fp)) != 1:
    OSError: exception: access violation writing 0x0000000000000024

  5. yujingyue9166 commented on Mar 27, 2024

    @yujingyue9166

    同问

  6. j-z10 commented on Mar 28, 2024

    @j-z10

    you guys should install GmSSLv3.1.1 instead of GmSSL@master

    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
    
    # check it's version. should be GmSSL 3.1.1
    gmssl version
  7. monkeyyang commented on Mar 28, 2024

    @monkeyyang

    感谢您的回复!
    遇到了一个新的问题,在Windows下Visual Studio命令提示符下执行:

    mkdir build
    cd build
    cmake .. -G "NMake Makefiles"
    nmake
    D:\wwwroot\GmSSL\build>nmake

    报以下错误,请问如何解决呢?

    [ 1%] Building C object CMakeFiles/gmssl.dir/src/tls.c.obj
    tls.c
    D:\wwwroot\GmSSL\src\tls.c(2323): warning C4013: “fcntl”未定义;假设外部返回 int
    D:\wwwroot\GmSSL\src\tls.c(2323): error C2065: “F_GETFL”: 未声明的标识符
    D:\wwwroot\GmSSL\src\tls.c(2328): error C2065: “O_NONBLOCK”: 未声明的标识符
    NMAKE : fatal error U1077: “"C:\Program Files\CMake\bin\cmake.exe" -E cmake_cl_compile_depends --dep-file=CMakeFiles\gmssl.dir\src\tls.c.obj.d --working-dir=D:\wwwroot\GmSSL\build --filter-prefix="注意: 包含文件: " -- C:\PROGRA1\MIB0551\2022\COMMUN1\VC\Tools\MSVC\14371.328\bin\Hostx86\x86\cl.exe @C:\Users\YANGYA~1\AppData\Local\Temp\nm7200.tmp”: 返回代 码“0x2”
    Stop.
    NMAKE : fatal error U1077: “"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\bin\HostX86\x86\nmake.exe" -s -f CMakeFiles\gmssl.dir\build.make /nologo -SL CMakeFiles\gmssl.dir\build”: 返回代 码“0x2”
    Stop.
    NMAKE : fatal error U1077: “"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\bin\HostX86\x86\nmake.exe" -s -f CMakeFiles\Makefile2 /nologo -S all”: 返回代码“0x2”
    Stop.

  8. Zhanghanhan1 commented on Mar 29, 2024

    @Zhanghanhan1

    同问 已安装gmssl3.1.1和gmssl-python

  9. j-z10 commented on Mar 30, 2024

    @j-z10

    you guys should install GmSSLv3.1.1 instead of GmSSL@master

    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
    
    # check it's version. should be GmSSL 3.1.1
    gmssl version

    看起来是windows下 pem_write 和 pem_read 导致的问题,我这个PR针对windows做了临时处理,把这一步放在python里面执行
    你们可以参考一下 j-z10/pygmssl#9

  10. FloocX commented on Mar 31, 2024

    @FloocX

    这不是一个包吧,j-z10师傅

  11. FloocX commented on Apr 1, 2024

    @FloocX

    j-z10师傅给出的代码是pygmssl的,我试了一下,是可以用的,感谢师傅

  12. ferstar commented on Oct 18, 2025

    @ferstar

    ✅ 问题已彻底解决

    你好!这个 access violation 错误已经在 PR #26 中得到彻底修复:
    #26

    🔧 问题根源

    之前的临时解决方案(降级到 GmSSL v3.1.1)只是规避了问题,真正的根源是:

    1. Windows 平台 FILE 指针跨 DLL 边界问题*:C 运行时库的 FILE* 指针无法在不同 DLL 之间传递
    2. ctypes 参数传递错误:c_char_p(passwd) 被错误地双重包装

    这导致所有涉及 PEM 文件加密操作的函数都会崩溃:

    • sm2_private_key_info_encrypt_to_pem
    • sm2_private_key_info_decrypt_from_pem
    • sm9_enc_master_key_info_encrypt_to_pem
    • sm9_sign_master_key_info_encrypt_to_pem

    ✨ PR #26 的完整修复

    修复方案:

    1. ✅ 重构了所有 Windows PEM 操作,使用 Python 原生 I/O 替代 FILE* 指针
    2. ✅ 修正了所有 ctypes 参数传递问题
    3. ✅ 在 Windows/Linux/macOS 平台通过了 124 个单元测试
    4. ✅ 内置预编译 GmSSL 库,无需手动安装依赖

    额外优势:

    • 📦 pip install 即可使用,无需编译 GmSSL
    • 🌍 跨平台支持:Linux (x86_64/ARM64)、macOS (Universal)、Windows (x86_64)
    • 🧪 测试覆盖率从 19 提升到 124 个测试

    📦 立即体验修复

    方式 1: 直接安装预编译包(推荐)

    pip install https://github.com/ferstar/GmSSL-Python/releases/download/v2.2.2/gmssl_python-2.2.2-py3-none-any.whl

    下载链接:https://github.com/ferstar/GmSSL-Python/releases/tag/v2.2.2

    方式 2: 从源码安装

    git clone https://github.com/GmSSL/GmSSL-Python.git
    cd GmSSL-Python
    git fetch origin pull/26/head:pr-26
    git checkout pr-26
    pip install -e .

    现在你可以直接使用所有 SM2/SM9 功能,无需担心 access violation 错误,也无需手动安装 GmSSL 库!

    欢迎测试反馈!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions