theparanoids/crypki
GitHub: theparanoids/crypki
一个轻量级的 HSM/PKCS#11 交互服务,专注于 SSH 和 x509 证书的签发与签名。
Stars: 56 | Forks: 27
[][build-url] [][godoc] [][goreport-card] [][codecov-card] [](https://bestpractices.coreinfrastructure.org/projects/5720)
# crypki
## 目录
- [背景](#background)
- [安装](#install)
- [配置](#configuration)
- [API](#api)
- [贡献](#contribute)
- [许可证](#license)
## 背景
一个用于与 HSM 或其他 PKCS #11 设备进行交互的简单服务。它支持 SSH 和 x509 证书的签发与签名。Crypki 是 [Athenz](https://github.com/yahoo/athenz) RBAC 系统的证书签名后端。
## 安装
只要拥有 crypki 二进制文件和 .so 文件,您应该就能在任何 Linux 平台上运行 crypki 服务器。我们已在 RHEL 7、RHEL 8、Debian 9 和 Ubuntu 18.04 上进行了测试。
### 从源码构建 crypki
前置条件:
- Go >= 1.22.4
运行:
```
go install github.com/theparanoids/crypki/cmd/crypki@latest
```
## 用法
要启动 crypki 服务器,请克隆仓库并运行以下命令。
- 构建 docker 镜像
docker build -f docker-softhsm/Dockerfile -t crypki-local .
如果您想加快 docker 镜像的构建过程,可以在运行上述命令之前,使用以下命令在本地缓存依赖项。
```
go mod vendor
```
- 生成前端客户端与 crypki 后端服务器之间进行双向 TLS 所需的证书和密钥
cd docker-softhsm
./gen-crt.sh
- 启动 docker 容器
docker run -d -p :4443:4443 -v $PWD/log:/var/log/crypki -v $PWD/tls-crt:/opt/crypki/tls-crt:ro -v $PWD/shm:/dev/shm --rm --name crypki -h "localhost" crypki-local
- 验证服务器是否已启动并运行
curl -X GET https://localhost:4443/ruok --cert tls-crt/client.crt --key tls-crt/client.key --cacert tls-crt/ca.crt
**免责声明:** _上述安装指南旨在帮助您快速上手 crypki;它们应仅用于测试/开发目的。请勿将此设置用于生产环境,因为它是不安全的。_
## 配置
请查看 [示例配置文件](https://github.com/theparanoids/crypki/blob/main/config/testdata/testconf-good.json) 以了解如何配置 crypki
## API
crypki 的 API 定义在 [crypki/proto](https://github.com/theparanoids/crypki/tree/main/proto) 下。如果您熟悉或正在使用 grpc,可以直接调用 proto 文件中定义的 rpc 方法。
示例:
获取所有可用的 SSH 签名密钥
```
curl -X GET https://localhost:4443/v3/sig/ssh-user-cert/keys --cert tls-crt/client.crt --key tls-crt/client.key --cacert tls-crt/ca.crt
```
获取 SSH 用户公共签名密钥(ssh-user-cert 的 CA 公钥)
```
curl -X GET https://localhost:4443/v3/sig/ssh-user-cert/keys/ssh-user-key --cert tls-crt/client.crt --key tls-crt/client.key --cacert tls-crt/ca.crt
```
签名 SSH 用户证书
```
curl -X POST -H "Content-Type: application/json" https://localhost:4443/v3/sig/ssh-user-cert/keys/ssh-user-key --data @ssh_csr.json --cert tls-crt/client.crt --key tls-crt/client.key --cacert tls-crt/ca.crt
```
获取所有可用的 x509 签名密钥
```
curl -X GET https://localhost:4443/v3/sig/x509-cert/keys --cert tls-crt/client.crt --key tls-crt/client.key --cacert tls-crt/ca.crt
```
获取 x509 公共 CA 证书
```
curl -X GET https://localhost:4443/v3/sig/x509-cert/keys/x509-key --cert tls-crt/client.crt --key tls-crt/client.key --cacert tls-crt/ca.crt
```
签名 x509 证书
```
curl -X POST -H "Content-Type: application/json" https://localhost:4443/v3/sig/x509-cert/keys/x509-key --data @x509_csr.json --cert tls-crt/client.crt --key tls-crt/client.key --cacert tls-crt/ca.crt
```
获取 blob 签名公钥
```
curl -X GET https://localhost:4443/v3/sig/blob/keys/sign-blob-key --cert tls-crt/client.crt --key tls-crt/client.key --cacert tls-crt/ca.crt
```
签名 blob(输入是 blob 原始哈希的 base64 编码值。[示例代码](https://play.golang.org/p/AFlho2HtZoD))
```
curl -X POST -H "Content-Type: application/json" https://localhost:4443/v3/sig/blob/keys/sign-blob-key --data @sign_blob.json --cert tls-crt/client.crt --key tls-crt/client.key --cacert tls-crt/ca.crt
```
## CA 凭证
### 提取密钥标识符的 SSH CA 公钥
以下脚本从容器中导出槽位 `user_ssh_pub` 的公钥(PEM 格式),并将其转换为 SSH 格式。
```
docker cp crypki:/opt/crypki/slot_pubkeys/user_ssh_pub.pem ~/tmp/user_ssh_pub.pem
ssh-keygen -f ~/tmp/user_ssh_pub.pem -i -mPKCS8
```
### 为密钥标识符生成自签名 X509 CA 证书
通过 `gen-cacert` 二进制文件为密钥标识符 `x509-key` 生成自签名 X509 CA 证书。
```
# 进入 crypki 容器的 shell。
docker exec -ti crypki /bin/bash
# 参考 `/opt/crypki/crypki-softhsm.json` 和 `init_hsm.sh` 以查找属性 $SLOT_NUMBER、$KEY_LABEL 和 $USER_PIN。
# 在示例中,对于 `x509-key`,我们的 keyLabel 是 host_x509,keyType 是 3,signatureAlgorithm 是 11。
echo $USER_PIN > /tmp/user_pin
cat > /tmp/ca_crt_config.json <
标签:Athenz, CVE, Docker, EVTX分析, Go, HSM, PKCS#11, Python工具, RBAC, Ruby工具, SamuraiWTF, SSH证书, X.509证书, Yubikey, 加密硬件, 双向TLS, 后端服务, 安全防御评估, 密码学, 手动系统调用, 数字签名, 日志审计, 网络安全, 脚本检测, 证书签名, 请求拦截, 隐私保护