V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
sbdx
V2EX  ›  问与答

已经生成了 GPG 密钥,怎么转成对应的 PKCS#11 的证书??

  •  
  •   sbdx · 2021-03-25 12:05:19 +08:00 · 1904 次点击
    这是一个创建于 1143 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现状:

    1. 使用 YubiKey,生成了 GPG 密钥,并写入卡上
    2. putty 可以正常通过 pagent 读取私钥登录 SSH
    3. XShell 用的多,但是 XShell 要求的是 PIV,必须写入证书才可以
    

    需求:

    怎么将生成的 GPG 密钥,转换成 PKCS#11 格式的证书???
    

    参考:

    SSH Connections with YubiKey PKCS#11 User Authentication https://netsarang.atlassian.net/wiki/spaces/ENSUP/pages/796426271/

    第 1 条附言  ·  2021-03-25 12:40:53 +08:00
    主要是为了统一公钥,不然 YubiKey 里可以直接生成自签证书的,但是导不出私钥
    7 条回复    2021-04-01 11:34:53 +08:00
    chinvo
        1
    chinvo  
       2021-03-25 12:09:16 +08:00 via iPhone
    gpg 可以直接用于 ssh 认证
    sbdx
        2
    sbdx  
    OP
       2021-03-25 12:39:47 +08:00
    但是 XShell 里读不到 GPG 的密钥,只能加载 PIV 证书
    Kobayashi
        3
    Kobayashi  
       2021-03-25 15:55:49 +08:00 via Android
    不能转出来。你应该先 SSH 生成密钥,备份一下私钥,然后导入到 GPG 。
    dingwen07
        4
    dingwen07  
       2021-03-25 19:09:55 +08:00 via Android
    用 PGP 和 X.509 作为关键词用英文搜,有很多
    dingwen07
        5
    dingwen07  
       2021-03-25 19:15:01 +08:00 via Android
    如果你是因为无法导出私钥的话,用 OpenSSL 生成一个证书再导入不就得了
    sbdx
        6
    sbdx  
    OP
       2021-03-25 20:05:11 +08:00
    感谢,已经用 gpgsm 命令生成了自签名证书并写入 YubiKey
    ```
    根据 OpenPGP 密钥生成自签发的证书
    C:\Users\Bill\Desktop>gpgsm --gen-key >client.crt
    gpgsm (GnuPG) 2.2.27; Copyright (C) 2021 g10 Code GmbH
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.

    Please select what kind of key you want:
    (1) RSA
    (2) Existing key
    (3) Existing key from card
    Your selection? 3
    Serial number of the card: D276000124010304****************
    Available keys:
    (1) ******FD9AC53B16E8DFDF9E**************** OPENPGP.1 rsa2048 (cert,sign)
    (2) ******BC64E376FFEA2831D2**************** OPENPGP.2 rsa2048 (encr)
    (3) ******E4E052176A3B450BF3**************** OPENPGP.3 rsa2048 (sign,auth)
    Your selection? 3
    Possible actions for a RSA key:
    (1) sign, encrypt
    (2) sign
    (3) encrypt
    Your selection? 2
    Enter the X.509 subject name: CN="Alex",OU="DevOps",O="Google"
    Enter email addresses (end with an empty line):
    > [email protected]
    >
    Enter DNS names (optional; end with an empty line):
    >
    Enter URIs (optional; end with an empty line):
    >
    Create self-signed certificate? (y/N) y
    These parameters are used:
    Key-Type: card:OPENPGP.3
    Key-Length: 1024
    Key-Usage: sign
    Serial: random
    Name-DN: CN="Alex",OU="DevOps",O="Google"
    Name-Email: [email protected]

    Proceed with creation? (y/N) y
    Now creating self-signed certificate. This may take a while ...
    gpgsm: about to sign the certificate for key: &616804E4E******************************
    gpgsm: certificate created
    Ready.

    用 YubiKey Manage 导入刚才生成的 client.crt 文件
    打卡 YubiKey Manage->Application->PIV->Configure Certificates->Authentication(Slot 9a)->Import

    查看证书信息
    C:\Program Files (x86)\OpenSC Project\OpenSC\tools>pkcs15-tool --list-public-keys
    Using reader with a card: Yubico YubiKey OTP+FIDO+CCID 0
    Public RSA Key [PIV AUTH pubkey]
    Object Flags : [0x00]
    Usage : [0x2D1], encrypt, wrap, verify, verifyRecover, nonRepudiation
    Access Flags : [0x02], extract
    ModLength : 2048
    Key ref : 154 (0x9A)
    Native : yes
    ID : 01
    DirectValue : <absent>
    ```
    sbdx
        7
    sbdx  
    OP
       2021-04-01 11:34:53 +08:00
    继续补充。

    经过测试,这样只是生成了 CRT 证书,还需要把对应的 SSH 密钥写入 Yubikey 才 OK 。

    # 生成的私钥要转换成 PEM 格式的文件,尽量不要设置密码
    # 如果有密码,请在后面加上-p 参数取消即可
    ssh-keygen -f private.key -m pem

    #导入证书文件
    C:\>yubico-piv-tool -s 9a -a import-certificate -i mykey.crt
    Successfully imported a new certificate.
    #导入密钥文件,触摸策略是每次都 touch,可选 never 、cached
    C:\>yubico-piv-tool -s 9a -a import-key --touch-policy=always -i private.key
    Successfully imported a new private key.


    更详细内容可以看我的文章 https://blog.csdn.net/sbdx/article/details/115271483

    不知道发链接违规不,如果违规麻烦管理员把链接删掉。谢谢。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5381 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 07:50 · PVG 15:50 · LAX 00:50 · JFK 03:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.