ext/certstore/loader.c in certstore_c-0.1.5 vs ext/certstore/loader.c in certstore_c-0.1.6

- old
+ new

@@ -9,10 +9,16 @@ /* See the License for the specific language governing permissions and */ /* limitations under the License. */ #include <certstore.h> +struct CertstoreLoader +{ + HCERTSTORE hStore; + PCCERT_CONTEXT pContext; +}; + static void certstore_loader_free(void* certstore); static const rb_data_type_t rb_win_certstore_loader_type = { "winevt/certstore_loader", { @@ -27,10 +33,12 @@ static void certstore_loader_free(void* ptr) { struct CertstoreLoader* loader = (struct CertstoreLoader*)ptr; + if (loader->pContext) + CertFreeCertificateContext(loader->pContext); if (loader->hStore) CertCloseStore(loader->hStore, 0); xfree(ptr); } @@ -40,10 +48,12 @@ { VALUE obj; struct CertstoreLoader* loader; obj = TypedData_Make_Struct( klass, struct CertstoreLoader, &rb_win_certstore_loader_type, loader); + loader->hStore = NULL; + loader->pContext = NULL; return obj; } static VALUE rb_win_certstore_loader_initialize(VALUE self, VALUE store_name, VALUE use_enterprise) @@ -144,11 +154,10 @@ wszString, &cchString); len = WideCharToMultiByte(CP_UTF8, 0, wszString, -1, NULL, 0, NULL, NULL); utf8str = ALLOCV_N(CHAR, vUtf8str, len); - len = WideCharToMultiByte(CP_UTF8, 0, wszString, -1, NULL, 0, NULL, NULL); WideCharToMultiByte(CP_UTF8, 0, wszString, -1, utf8str, len, NULL, NULL); // malloc ((strlen(base64 cert content) + strlen(header) + // strlen(footer) + 1(null terminator)) length). len = strlen(utf8str) + strlen(certHeader) + strlen(certFooter); certificate = ALLOCV_N(CHAR, vCertificate, len + 1); @@ -186,12 +195,14 @@ TypedData_Get_Struct( self, struct CertstoreLoader, &rb_win_certstore_loader_type, loader); while ((pContext = CertEnumCertificatesInStore(loader->hStore, pContext)) != NULL) { + loader->pContext = pContext; VALUE rb_certificate = certificate_context_to_string(pContext); rb_yield(rb_certificate); + loader->pContext = NULL; } return Qnil; } @@ -201,26 +212,22 @@ struct CertstoreLoader* loader; TypedData_Get_Struct( self, struct CertstoreLoader, &rb_win_certstore_loader_type, loader); - /* What should we dispose here? */ + if (loader->pContext) + CertFreeCertificateContext(loader->pContext); + loader->pContext = NULL; return Qnil; } static VALUE rb_win_certstore_loader_each(VALUE self) { - PCCERT_CONTEXT pContext = NULL; - struct CertstoreLoader* loader; - RETURN_ENUMERATOR(self, 0, 0); - TypedData_Get_Struct( - self, struct CertstoreLoader, &rb_win_certstore_loader_type, loader); - rb_ensure( rb_win_certstore_loader_each_pem, self, rb_win_certstore_loader_dispose, self); return Qnil; } @@ -291,11 +298,11 @@ TypedData_Get_Struct( self, struct CertstoreLoader, &rb_win_certstore_loader_type, loader); if (CertAddEncodedCertificateToStore(loader->hStore, X509_ASN_ENCODING, - RSTRING_PTR(rb_der_cert_bin_str), + (const BYTE *)RSTRING_PTR(rb_der_cert_bin_str), RSTRING_LEN(rb_der_cert_bin_str), CERT_STORE_ADD_NEW, NULL)) { return Qtrue; } else { @@ -472,10 +479,10 @@ goto error; } ALLOCV_END(vThumbprint); ALLOCV_END(vPassword); - VALUE rb_str = rb_str_new(pfxPacket.pbData, pfxPacket.cbData); + VALUE rb_str = rb_str_new((const char *)pfxPacket.pbData, pfxPacket.cbData); CryptMemFree(pfxPacket.pbData); CertCloseStore(hMemoryStore, CERT_CLOSE_STORE_CHECK_FLAG); CertFreeCertificateContext(pContext);