cmpserver
A field-level mocking CMP (RFC 4210) CA simulator for negative-testing certificate protocols.
一个用于证书协议负向测试的 CMP(RFC 4210)CA 模拟器。它以 HTTP 服务的形式
扮演 CA,接收客户端(EE,End Entity)发来的 ir / kur / certconf,回以
ip / kup / pkiconf;核心能力是一个通用字段注入器,可在签名前对响应
报文的任意字段做篡改,从而验证对端是否正确校验/拒绝异常证书响应。
特性
- 只构建 CA → EE 方向的报文:
ip/kup/pkiconf(及error)。 - 字段级 mock 注入器:在计算 PKIProtection 之前篡改任意字段,签名覆盖篡改后内容。
- 支持 6 档操作:替换合法值、替换协议非法值、置空/零长度/NULL、删除可选字段(
OMIT)、 新增字段、Raw原样注入。 - 独立的验签旋钮
mock.protection:normal/skip/wrong_key/corrupt/empty。 - 轻量 HTTP 服务(标准库
http.server),POST /cmp收 DER、回 DER。
安装
依赖 asn1crypto、oscrypto、certbuilder,均为 PyPI 可安装的纯 Python 包。
# 从源码(开发模式)
pip install -e .
# 或直接从 PyPI 安装
pip install cmpserver
Python 版本:3.8+ 均可(纯 Python 实现,已在 3.10 / 3.14 / 3.15 实测通过)。
安装后可用一行命令验证核心模块可导入:
python -c "from cmpserver import CmpServer, MockSpec, OMIT, general_name; print('OK')"
快速开始
from oscrypto import asymmetric
from cmpserver import CmpServer, create_root_cert
# 1. 自签根 CA(onlyData=True 只返回材料,不落盘)
issuer_key, issuer_cert = create_root_cert(onlyData=True)
# 2. 起 CMP 服务
with CmpServer(port=0, host="127.0.0.1") as srv:
srv.ca_info = {"issuer_key": issuer_key, "issuer_cert": issuer_cert}
print("CMP 服务:", srv.url) # http://127.0.0.1:<random-port>
客户端(EE)把 CMP 请求(ir/kur/certconf,DER,application/pkixcmp)POST 到
{srv.url}/cmp,服务自动解析并返回对应的 ip/kup/pkiconf。
核心:字段级 mock
CmpServer.mock(MockSpec)是一个通用字段注入器,对模拟器下发的报文做字段级篡改:
mock.header[path] 签名前注入 header(dotted-path,支持 OMIT / Raw)
mock.body[path] 签名前注入 body(钻进 CertRepMessage,数字段=数组下标)
mock.protection 'normal' | 'skip' | 'wrong_key' | 'corrupt' | 'empty'
mock.protection_key wrong_key 模式下用于签名的假私钥
mock.extra_certs 默认(带签名证书) | OMIT/None(不带) | [cert,...](替换)
mock.reset() 清空
- 签名前注入:
header/body的覆盖发生在计算 PKIProtection 之前,签名覆盖篡改后内容 —— 用于测对端的字段语义校验(会不会因字段非法而拒绝)。 - 验签单独测:用
mock.protection旋钮。 - 绕过 builder 校验:注入不经过 builder 的类型/合法值 setter,可填“协议非法但结构合法”的值。
示例
from cmpserver import OMIT, Raw, general_name
from cmpserver.asn1crypto_cmp.rfc4210 import PKIFailureInfo
from datetime import datetime, timezone
srv.mock.header['pvno'] = 'cmp1999' # 协议版本非法
srv.mock.header['transaction_id'] = b'' # 置零长度
srv.mock.header['message_time'] = datetime(2000, 1, 1, tzinfo=timezone.utc) # 过期
srv.mock.header['recip_nonce'] = OMIT # 删除可选字段
srv.mock.header['sender'] = general_name(fake_cert) # 换 sender(必须包 general_name)
srv.mock.body['response.0.status.status'] = 'rejection'
srv.mock.body['response.0.status.fail_info'] = PKIFailureInfo({'bad_pop'})
srv.mock.protection = 'corrupt' # 破坏签名(测验签)
# srv.mock.protection = 'wrong_key'; srv.mock.protection_key = fake_key
srv.mock.extra_certs = OMIT # 不带 extra_certs
mock.header['sender'|'recipient']直接写进 GeneralName 字段,不能传裸x509.Name(asn1crypto 会拒绝),必须用general_name()包一下。它接受Certificate/Name/GeneralName三种入参。
架构
客户端 (EE) CMP 模拟器 (CA)
│ ir / kur / certconf (DER, application/pkixcmp) │
│ ─────────────────────────────────────────────────► │ POST /cmp
│ │ └─ msgHandler 分发
│ │ └─ issue_end_entity 签发 EE 证书
│ │ └─ builder.build() ── mock 注入 ──► 签名
│ ip / kup / pkiconf (DER) │
│ ◄───────────────────────────────────────────────── │
| 层 | 文件 | 职责 |
|---|---|---|
| 传输/路由 | cmpserver/controllers/basic_server.py |
@handler 收集路由、后台线程 HTTPServer、Request.send_der |
| 服务 | cmpserver/controllers/cmp_server.py |
POST /cmp 收 DER;持有 ca_info + mock |
| 消息分发 | cmpserver/controllers/handlers/cmpHandler.py |
按 body.name 分发并构建响应 |
| 构建 | cmpserver/cmpbuilder/__init__.py |
三个 builder + _assemble_message(签名 + mock 注入) |
| 字段注入 | cmpserver/cmpbuilder/cmp_mock.py |
MockSpec —— 签名前篡改任意字段 |
| ASN.1 | cmpserver/asn1crypto_cmp/rfc4210.py |
CMP/CRMF 结构定义,PKIMessage.load/dump 的依据 |
| 证书 | cmpserver/scripts/ca.py |
自签根 CA、签发 EE 证书 |
ir / kur / certconf 由客户端(EE)主动发出,模拟器只收取解析、不构建;
模拟器只构建 CA→EE 方向的 ip / kup / pkiconf(及 error)。
目录结构
cmpserver/
├── __init__.py # 公开 API re-export + __version__
├── cmpbuilder/ # CMP 报文构建器(包)
│ ├── __init__.py # CMPResponseBuilder(ip/kup) / CMPConfirmBuilder(pkiconf) / CMPErrorBuilder(error)
│ └── cmp_mock.py # MockSpec / OMIT / Raw / general_name
├── asn1crypto_cmp/
│ └── rfc4210.py # CMP/CRMF 的 ASN.1 结构定义(PKIMessage 等)
├── controllers/
│ ├── basic_server.py # BasicServer / Request / @handler —— 轻量 HTTP 路由框架
│ ├── cmp_server.py # CmpServer:POST /cmp,持有 ca_info + mock
│ └── handlers/
│ └── cmpHandler.py # msgHandler:按 body 类型分发并构建响应
└── scripts/
└── ca.py # create_root_cert / issue_end_entity —— 证书工厂
tests/目录为私有本地测试,依赖内部测试框架与真实对端环境,不在本仓库/包中。
边界与已知限制
- 可 mock 的报文:
ip/kup/pkiconf。error当前未接入 mock;ir/kur/certconf为收取解析、不构建。 - 只能注入“协议非法但结构合法”的值(asn1crypto 仍能编码);尚不支持位级畸形 DER。
- 签名后篡改(header_post / body_post)暂未实现。
- 已知缺口:
mock.extra_certs = []当前被当作“不带”,故“在但空的 extra_certs” 暂不可表达;header / body 内的容器字段均支持“在但空”。
License
Metadata
Release files for cmpserver 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cmpserver-0.1.0.tar.gz | 27.1 kB | Without provenance | Details for cmpserver-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cmpserver-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for cmpserver-0.1.0-py3-none-any.whl |
Total release size: 52.0 kB
Release files / cmpserver-0.1.0.tar.gz
| Download URL | cmpserver-0.1.0.tar.gz |
|---|---|
| Size | 27.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
674780b316a769ec769eaf426fc38aeb5ae390ff5345d4389c8175fafbf78af2
|
|
BLAKE2b-256 checksum How to use checksums |
f3dbfd25406cc82743f2a911a75c8d7438c820b577b4d6854ad0e6861c16d63d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.15.2
|
Release files / cmpserver-0.1.0-py3-none-any.whl
| Download URL | cmpserver-0.1.0-py3-none-any.whl |
|---|---|
| Size | 24.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4df2ff15961039573d122822ef7cdb5a6d9ea20945e424e8535d35c4fef7947b
|
|
BLAKE2b-256 checksum How to use checksums |
346e5258187fa367df42cc03e99afa8c920198e3d4ff49103945f59adcf72a52
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.15.2
|
Security for cmpserver 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.