没有Openssl的HTTP公钥固定(HPKP)

时间:2019-05-15 13:33:17

标签: ssl openssl base64 ssl-certificate sha

我正计划生成以下pin-sha256:

Public-Key-Pins: 
  pin-sha256="cUPcTAZWKaASuYWhhneDttWpY3oBAkE3h2+soZS7sWs="; 
  pin-sha256="M8HztCzM3elUxkcjR2S5P4hhyBNf6lHkmjAHKhpGPWE="; 
  max-age=5184000; includeSubDomains; 
  report-uri="https://www.example.org/hpkp-report"

为了安全起见,我可以保留在IIS中,如下所示:

<system.webServer>
  ...

  <httpProtocol>
    <customHeaders>
      <add name="Public-Key-Pins" value="pin-sha256=&quot;base64+primary==&quot;; pin-sha256=&quot;base64+backup==&quot;; max-age=5184000; includeSubDomains" />
    </customHeaders>
  </httpProtocol>

  ...
</system.webServ

但是我的服务器缺少开放的ssl,由于过程问题,安装时间很长。

能否让我知道,如果没有这些我可以生成引脚的openssl,还有其他选择吗?

See this link for more details

这些是引脚生成命令,我需要在没有openssl的情况下执行此操作,

openssl rsa -in my-rsa-key-file.key -outform der -pubout | openssl dgst -sha256 -binary | openssl enc -base64

1 个答案:

答案 0 :(得分:0)

下面是上面的openssl命令链示例,该命令链转换为将base64结果输出到屏幕的openssl API调用。

主要电话是

根据您的情况,可以使用多种OPENSSL API的变体来完成相同的工作。

以下是OpenSSL C API的C ++用法。如果需要,您可以轻松剥离C ++位。

template<typename T, typename D>
std::unique_ptr<T, D> make_handle(T* handle, D deleter)
{
    return std::unique_ptr<T, D>{handle, deleter};
}

bool load_rsa_private_key_and_base64_sha256_hash_public_key()
{
    // load rsa private key in PEM format
    auto bio = make_handle(BIO_new_file("privatekey.pem", "rb"), BIO_free);
    if(!bio) return false;
    auto const key = make_handle(PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, (void*)("password")), EVP_PKEY_free);
    bio.reset();

    if(!key) return false;

    // extract private key from loaded certificate
    auto const rsa = EVP_PKEY_get0_RSA(key.get());
    if (!rsa) return false;

    // setup sha256 bio chain
    auto const bmd = make_handle(BIO_new(BIO_f_md()), BIO_free);
    if(!bmd) return false;
    if (!BIO_set_md(bmd.get(), EVP_sha256())) return false;

    auto const null_bio = make_handle(BIO_new(BIO_s_null()), BIO_free);
    auto in = BIO_push(bmd.get(), null_bio.get());

    // write RSA Public Key into DER format to the SHA256 digest BIO chain
    i2d_RSA_PUBKEY_bio(in, rsa);

    // extract the SHA256 digest
    auto mdtmp = BIO_find_type(in, BIO_TYPE_MD);
    if (!mdtmp) return false;
    std::array<char, EVP_MAX_MD_SIZE> buffer;
    auto const length = BIO_gets(mdtmp, buffer.data(), buffer.size());

    if(length <= 0)  return false;

    // convert the digest to base64 and output to the stdio
    auto const b64 = make_handle(BIO_new(BIO_f_base64()), BIO_free);
    auto const bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
    auto out = BIO_push(b64.get(), bio_out);

    BIO_write(out, buffer.data(), length);
    BIO_flush(out);

    return true;
}