OpenSSL 3.0 中 M_ASN1_I2D * 相对于 OpenSSL 1.0.2 的替代方案

问题描述 投票:0回答:1

我正在将 openSSL 从 1.0.2z 迁移到 openSSL 3.0。 我无法找到 M_ASN1_I2D* 函数的替代品。 M_ASN1_I2D_len、M_ASN1_I2D_put、M_ASN1_I2D_seq_total、ASN1_I2D_vars、M_ASN1_I2D_finish

早期在文件 asn1_mac.h 中声明(在 OpenSSL 3.0 中已弃用),有人可以帮我识别该文档或其在 OpenSSL 3.0 中的替代品吗

c++ openssl asn1crypto
1个回答
0
投票

经过大量研究,我找到了替代方案 https://github.com/openssl/openssl/issues/20574

//OpenSSL 3.0 structure
typedef struct pkcs7_issuer_and_subject_st {
    X509_NAME* issuer;
    X509_NAME* subject;
} PKCS7_ISSUER_AND_SUBJECT;

ASN1_SEQUENCE(PKCS7_ISSUER_AND_SUBJECT) = {
        ASN1_SIMPLE(PKCS7_ISSUER_AND_SUBJECT, issuer, X509_NAME),
        ASN1_SIMPLE(PKCS7_ISSUER_AND_SUBJECT, subject, X509_NAME)
} ASN1_SEQUENCE_END(PKCS7_ISSUER_AND_SUBJECT)

IMPLEMENT_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SUBJECT)
PKCS7_ISSUER_AND_SUBJECT* pkcs7_IssuerAndSubjectNewInit(void) {

        //OpenSSL 1.0.2 version
    /*pkcs7_issuer_and_subject* retVal = NULL;
    ASN1_CTX c;
    M_ASN1_New_Malloc(retVal, pkcs7_issuer_and_subject);
    M_ASN1_New(retVal->issuer, X509_NAME_new);
    M_ASN1_New(retVal->subject, X509_NAME_new);
    return(retVal);
    M_ASN1_New_Error(199);*/
  
        //OpenSSL 3.0
    PKCS7_ISSUER_AND_SUBJECT* retVal = NULL;
    retVal = PKCS7_ISSUER_AND_SUBJECT_new();
    retVal->issuer = X509_NAME_new();
    retVal->subject = X509_NAME_new();
    return (retVal);
}

void pkcs7_IssuerAndSubjectFree(PKCS7_ISSUER_AND_SUBJECT* ias) {

        //OpenSSL 1.0.2 version
    /*if (ias == NULL) return;
    X509_NAME_free(ias->issuer);
    M_ASN1_INTEGER_free(ias->subject);
    OPENSSL_free(ias);*/

        //OpenSSL 3.0
    if (ias == NULL) return;
    //X509_NAME_free(ias->issuer); //If release here exception will come in PKCS7_ISSUER_AND_SUBJECT_free
    //X509_NAME_free(ias->subject);
    
    PKCS7_ISSUER_AND_SUBJECT_free(ias);
    ias = NULL;
}

void i2d_pkcs7_IssuerAndSubject(PKCS7_ISSUER_AND_SUBJECT* ias, unsigned char* pp) {
    
         //OpenSSL 1.0.2
    /*M_ASN1_I2D_vars(ias);
    M_ASN1_I2D_len(ias->issuer, i2d_X509_NAME);
    M_ASN1_I2D_len(ias->subject, i2d_X509_NAME);
    M_ASN1_I2D_seq_total();
    M_ASN1_I2D_put(ias->issuer, i2d_X509_NAME);
    M_ASN1_I2D_put(ias->subject, i2d_X509_NAME);
    M_ASN1_I2D_finish();*/

        //OpenSSL 3.0
    int len = i2d_PKCS7_ISSUER_AND_SUBJECT(ias, NULL);
    p = (unsigned char *)malloc(len * sizeof(char));
    if (p != NULL)
    {
        i2d_PKCS7_ISSUER_AND_SUBJECT(ias, &p);
    }
}

int main() {
    
    //Init
    PKCS7_ISSUER_AND_SUBJECT* ias = pkcs7_IssuerAndSubjectNewInit();

    //Get the certificate
    X509* x509;
    BIO* certBio = BIO_new(BIO_s_file());
    char* subjectname, * issuername;
    const char* path = "C:\\Users\\Cert.pem"; // certificate path
    BIO_read_filename(certBio, path); // reading certificate to bio
    x509 = PEM_read_bio_X509_AUX(certBio, NULL, 0, NULL);  //converting to x509  

    if (x509 != NULL)
    {
        X509_NAME* subjectName = X509_get_subject_name(x509);
        X509_NAME* issuerName = X509_get_issuer_name(x509);
        ias->subject = subjectName;
        ias->issuer = issuerName;

        //i2d
        i2d_pkcs7_IssuerAndSubject(ias, NULL);

        //Free
        pkcs7_IssuerAndSubjectFree(ias);
    }
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.