代码之家  ›  专栏  ›  技术社区  ›  Naomi

OpenSSL堆栈API-推送到堆栈后释放对象

  •  0
  • Naomi  · 技术社区  · 6 年前

    我想知道我是否需要自由 X509 STACK_OF(X509) sk_X509_free() 呼叫为我免费的一切,包括内容。我在OpenSSL中没有找到这方面的文档。

    std::vector<std::string> caPems;    
    
    // Fill the vector from input
    // ...
    
    
    BIO *bufio = NULL;
    X509 *x509 = NULL, *x509_ca = NULL;
    bool success = false;
    STACK_OF(X509)* x509_ca_stack;
    
    x509_ca_stack = sk_X509_new_null();
    if (x509_ca_stack) {
        success = true;
        for (const std::string& caPem : caPems) {
            BIO_new_mem_buf(caPem.c_str(), caPem.size());
            PEM_read_bio_X509(bufio, &x509_ca, NULL, NULL);
            BIO_free_all(bufio);
            if (x509_ca != nullptr) {
                sk_X509_push(x509_ca_stack, x509_ca);
                x509_ca = NULL; // should I free after push???
            } else
                success = false;
        }
        if (success)
            foo(x509_ca_stack);
        sk_X509_free(x509_ca_stack); // or is this free enough for the entire stack?
    } else {
        printf("ERROR: failed loading cert\n");
    }
    

    0 回复  |  直到 6 年前