c++tpmtss.msr

TPMT_PUBLIC Serialize to other Format file?


[a similar questions link][1] [1]: https://stackoverflow.com/questions/60340870/serialize-tpm-public-key-to-der-or-pem

But,I don't Know how to do that,using botan to covert data. TPMT_PUBLIC ===> PEM


Solution

  • TPMS_RSA_PARMS *rsaParms = dynamic_cast<TPMS_RSA_PARMS*>(&*persistentPub.outPublic.parameters);
    if (rsaParms == NULL)
    {
        throw domain_error("Only RSA encryption is supported");
    }
    
    TPM2B_PUBLIC_KEY_RSA *rsaPubKey = dynamic_cast<TPM2B_PUBLIC_KEY_RSA*>(&*persistentPub.outPublic.unique);
    
        auto rsaPublicKey = Botan::RSA_PublicKey(Botan::BigInt(rsaPubKey->buffer.data(), rsaPubKey->buffer.size()), rsaParms->exponent);
        std::string strPemText = Botan::X509::PEM_encode(rsaPublicKey);
        cout << "public format Text:  " << strPemText.c_str() << endl;