【问题标题】:OSX Keychain Access-Generate CSR from existing Private Key for APNS (Apple Push Notification Service)OSX Keychain Access-Generate CSR from existing Private Key for APNS(Apple Push Notification Service)
【发布时间】:2012-09-13 22:40:30
【问题描述】:

当您需要为 APNS 创建新证书时,配置门户“向导”始终会提供创建新 CSR 的步骤,这意味着您还需要创建新的公钥/私钥。这些可能会开始失控,那么有没有办法从现有的私钥在钥匙串访问中创建一个 CSR(代码签名请求),而不必每次都创建一个新的?

谢谢

【问题讨论】:

    标签: iphone ios macos push-notification apple-push-notifications


    【解决方案1】:

    当您进入配置文件以启用/配置推送通知时,它首先要求的是 CSR(代码签名证书)。

    您可以使用来自 Keychain Access 的现有私钥生成此文件,而不是创建新的。

    只需打开钥匙串访问,然后滚动并找到以前的 PRIVATE KEY(可能称为您的名字),然后右键单击(两指单击)并选择 Request A Certificate From A Certificate Authority With "bla bla bla”

    我只是在用户电子邮件地址和 CA 电子邮件地址中输入相同的电子邮件地址,然后选择保存到磁盘。

    然后上传以创建您的 .cer 文件

    【讨论】:

    • 我按照上面的建议做了同样的事情,但是发生了错误:(“在钥匙串中找不到指定的项目。”可能是什么问题?
    • 这里有同样的问题,但我知道我去年的某个时候已经让它工作了......所以苹果在他们的随机更新中破坏了一些东西......?
    • @mysticboy59 有关此错误的详细信息,请参阅此问题:stackoverflow.com/questions/16845169/…
    【解决方案2】:

    通常,您可以通过右键单击 Keychain Access 中的现有私钥并​​选择使用“您的密钥名称”从证书颁发机构请求证书来执行此操作。

    不幸的是,除非您在您的钥匙串中拥有相应的公钥,否则这将失败并显示“无法在钥匙串中找到指定的项目”。这没有技术原因——证书签名请求 (CSR) 可以仅从私钥生成——但 Keychain Access 不理解这一点。

    你有两个选择。

    导出私钥并手动生成 CSR

    这是一个快速选项,它只会生成一个您可以上传到 Apple 的 CSR。

    1. 在 Keychain Access 中选择私钥,然后点击 File - Export Items...
    2. 将文件以.p12 格式保存在某处,但请记住路径。这些说明假定它位于您的主目录中并称为exported.p12。将密码留空。
    3. 打开终端并输入:

      openssl req -new -key <(openssl pkcs12 -in ~/exported.p12 -nocerts -nodes -passin pass:"") > new.certSigningRequest
      

      请参阅本文末尾的 [1],了解正在发生的事情的详细信息。

    4. 对每个提示按 Enter 键(Apple 不关心这些值)。完成后,您将拥有一个适合上传到 Apple Developer Portal 的 .certSigningRequest。当您下载相关证书时,它将与原始私钥配对。

    5. 删除exported.p12 文件,因为它包含私钥材料。

    重新创建公钥,以便 Keychain Access 满意

    此选项是一项长期修复,可让您直接从 Keychain Access 的原始密钥生成 CSR。这些说明假定您当前无法使用 Keychain Access 来执行此操作,因为您缺少相应的私钥的公共版本。您可以通过转到 Keychain Access 中的“Keys”类别并查找具有相同名称的“private key”和“public key”来检查这一点。

    1. 在 Keychain Access 中选择私钥,然后点击 File - Export Items...
    2. 将文件以.p12 格式保存在某处,但请记住路径。这些说明假定它位于您的主目录中并称为exported.p12。将密码留空。
    3. 打开终端并输入:

      openssl pkcs12 -in ~/exported.p12 -nocerts -nodes | openssl rsa -pubout > public.pem
      

      请参阅本文末尾的 [2],了解正在发生的事情的详细信息。

    4. 使用security 工具将此公钥导入钥匙串访问:

      security -v import public.pem -k ~/Library/Keychains/login.keychain
      

      您应该会看到“已导入 1 个密钥”。

    如果您想将其导入另一个钥匙串,请更改 ~/Library/Keychains/login.keychain。 (您可以通过转到钥匙串访问中的编辑 - 钥匙串列表来查看每个钥匙串的位置。

    1. 打开 Keychain Access 并找到名为“Imported Public Key”的公钥。双击它并将其名称更改为与原始私钥相同的名称。
    2. 删除exported.p12public.pem

    您现在可以右键单击原始私钥并选择使用“您的密钥名称”从证书颁发机构请求证书以生成 CSR。

    解释

    [1] 这个命令,分解:

    openssl req -new  # Generate a new certificate signing request
      -key            # Instead of generating a key, use an existing one
      <(              # Put the output of the following command in a temporary file
                      # (a Bash feature, not specific to OpenSSL)
      openssl pkcs12 -in ~/exported.p12 # Read keys from the specified PKCS12 file
      -nocerts         # Don't output the certificate contained in the file
      -nodes           # Output the private key from the file
      -passin pass:""  # The password for the container is blank
      )
    > new.certSigningRequest # Write the generated CSR to a file
    

    [2] 第二条命令,分解:

    openssl pkcs12 -in ~/exported.p12 # Read keys from the specified PKCS12 file
      -nocerts -nodes                 # Output only the private key, no certificates
    | openssl rsa -pubout             # Compute the public key from a private key
    > public.pem                      # Write the public key to a file
    

    【讨论】:

    • 很好的答案。很多细节和解释。正是我需要的。
    • 我在第 5 步中遇到问题:无法重命名在我的自定义钥匙串中导入的公钥。我必须将它移到会话钥匙串中,重命名它,然后再将其移回。
    • 如果您在重命名密钥时遇到困难,请参阅this comment
    猜你喜欢
    • 2011-01-18
    • 1970-01-01
    • 2013-01-14
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    相关资源
    最近更新 更多