VirtualBox

Ignore:
Timestamp:
May 18, 2020 1:35:33 PM (5 years ago)
Author:
vboxsync
Message:

IPRT: Adding RTCrStoreCreateInMemEx that allows specifying a parent store to provide a kind of union store. bugref:9699

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/crypto/store-inmem.cpp

    r82968 r84329  
    8686    /** Array of certificates. */
    8787    PRTCRSTOREINMEMCERT    *papCerts;
     88
     89    /** Parent store. */
     90    RTCRSTORE               hParentStore;
     91    /** The parent store callback table. */
     92    PCRTCRSTOREPROVIDER     pParentProvider;
     93    /** The parent store provider callback argument. */
     94    void                   *pvParentProvider;
    8895} RTCRSTOREINMEM;
    8996/** Pointer to an in-memory crypto store. */
     
    220227    RTMemFree(pThis->papCerts);
    221228    pThis->papCerts = NULL;
     229
     230    if (pThis->hParentStore != NIL_RTCRSTORE)
     231    {
     232        RTCrStoreRelease(pThis->hParentStore);
     233        pThis->hParentStore = NIL_RTCRSTORE;
     234    }
     235
    222236    RTMemFree(pThis);
    223237}
     
    249263{
    250264    PRTCRSTOREINMEM pThis = (PRTCRSTOREINMEM)pvProvider;
    251     AssertReturn(pSearch->auOpaque[0] == ~(uintptr_t)pvProvider, NULL);
    252 
    253     uintptr_t i = pSearch->auOpaque[1];
    254     if (i < pThis->cCerts)
    255     {
    256         pSearch->auOpaque[1] = i + 1;
    257         PRTCRCERTCTXINT pCertCtx = &pThis->papCerts[i]->Core;
    258         ASMAtomicIncU32(&pCertCtx->cRefs);
    259         return &pCertCtx->Public;
    260     }
    261     return NULL;
     265    if (pSearch->auOpaque[0] == ~(uintptr_t)pvProvider)
     266    {
     267        uintptr_t i = pSearch->auOpaque[1];
     268        if (i < pThis->cCerts)
     269        {
     270            pSearch->auOpaque[1] = i + 1;
     271            PRTCRCERTCTXINT pCertCtx = &pThis->papCerts[i]->Core;
     272            ASMAtomicIncU32(&pCertCtx->cRefs);
     273            return &pCertCtx->Public;
     274        }
     275
     276        /* Do we have a parent store to search? */
     277        if (pThis->hParentStore == NIL_RTCRSTORE)
     278            return NULL; /* no */
     279        if (   !pThis->pParentProvider->pfnCertFindAll
     280            || !pThis->pParentProvider->pfnCertSearchNext)
     281            return NULL;
     282
     283        RTCRSTORECERTSEARCH const SavedSearch = *pSearch;
     284        int rc = pThis->pParentProvider->pfnCertFindAll(pThis->pvParentProvider, pSearch);
     285        AssertRCReturnStmt(rc, *pSearch = SavedSearch, NULL);
     286
     287        /* Restore the store.cpp specifics: */
     288        AssertCompile(RT_ELEMENTS(SavedSearch.auOpaque) == 4);
     289        pSearch->auOpaque[2] = SavedSearch.auOpaque[2];
     290        pSearch->auOpaque[3] = SavedSearch.auOpaque[3];
     291    }
     292
     293    AssertReturn(pThis->pParentProvider, NULL);
     294    AssertReturn(pThis->pParentProvider->pfnCertSearchNext, NULL);
     295    return pThis->pParentProvider->pfnCertSearchNext(pThis->pvParentProvider, pSearch);
    262296}
    263297
     
    266300static DECLCALLBACK(void) rtCrStoreInMem_CertSearchDestroy(void *pvProvider, PRTCRSTORECERTSEARCH pSearch)
    267301{
    268     NOREF(pvProvider);
    269     AssertReturnVoid(pSearch->auOpaque[0] == ~(uintptr_t)pvProvider);
    270     pSearch->auOpaque[0] = 0;
    271     pSearch->auOpaque[1] = 0;
    272     pSearch->auOpaque[2] = 0;
    273     pSearch->auOpaque[3] = 0;
     302    PRTCRSTOREINMEM pThis = (PRTCRSTOREINMEM)pvProvider;
     303    if (pSearch->auOpaque[0] == ~(uintptr_t)pvProvider)
     304    {
     305        pSearch->auOpaque[0] = 0;
     306        pSearch->auOpaque[1] = 0;
     307        pSearch->auOpaque[2] = 0;
     308        pSearch->auOpaque[3] = 0;
     309    }
     310    else
     311    {
     312        AssertReturnVoid(pThis->pParentProvider);
     313        AssertReturnVoid(pThis->pParentProvider->pfnCertSearchDestroy);
     314        pThis->pParentProvider->pfnCertSearchDestroy(pThis->pvParentProvider, pSearch);
     315    }
    274316}
    275317
     
    347389 * @returns IPRT status code.
    348390 * @param   ppStore         Where to return the store instance.
    349  */
    350 static int rtCrStoreInMemCreateInternal(PRTCRSTOREINMEM *ppStore)
     391 * @param   hParentStore    Optional parent store.  Consums reference on
     392 *                          success.
     393 */
     394static int rtCrStoreInMemCreateInternal(PRTCRSTOREINMEM *ppStore, RTCRSTORE hParentStore)
    351395{
    352396    PRTCRSTOREINMEM pStore = (PRTCRSTOREINMEM)RTMemAlloc(sizeof(*pStore));
    353397    if (pStore)
    354398    {
    355         pStore->cCerts      = 0;
    356         pStore->cCertsAlloc = 0;
    357         pStore->papCerts    = NULL;
     399        pStore->cCerts           = 0;
     400        pStore->cCertsAlloc      = 0;
     401        pStore->papCerts         = NULL;
     402        pStore->hParentStore     = hParentStore;
     403        pStore->pParentProvider  = NULL;
     404        pStore->pvParentProvider = NULL;
    358405        *ppStore = pStore;
    359         return VINF_SUCCESS;
     406        if (hParentStore == NIL_RTCRSTORE)
     407            return VINF_SUCCESS;
     408        if (~(uintptr_t)hParentStore != ~(uintptr_t)pStore)
     409        {
     410            pStore->pParentProvider = rtCrStoreGetProvider(hParentStore, &pStore->pvParentProvider);
     411            if (pStore->pParentProvider)
     412                return VINF_SUCCESS;
     413            AssertFailed();
     414        }
     415        RTMemFree(pStore);
    360416    }
    361417    *ppStore = NULL; /* shut up gcc-maybe-pita warning. */
     
    364420
    365421
    366 RTDECL(int) RTCrStoreCreateInMem(PRTCRSTORE phStore, uint32_t cSizeHint)
    367 {
     422RTDECL(int) RTCrStoreCreateInMemEx(PRTCRSTORE phStore, uint32_t cSizeHint, RTCRSTORE hParentStore)
     423{
     424    if (hParentStore != NIL_RTCRSTORE)
     425    {
     426        uint32_t cRefs = RTCrStoreRetain(hParentStore);
     427        AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
     428    }
     429
    368430    PRTCRSTOREINMEM pStore;
    369     int rc = rtCrStoreInMemCreateInternal(&pStore);
     431    int rc = rtCrStoreInMemCreateInternal(&pStore, hParentStore);
    370432    if (RT_SUCCESS(rc))
    371433    {
     
    380442        RTMemFree(pStore);
    381443    }
     444
     445    RTCrStoreRelease(hParentStore);
    382446    return rc;
    383447}
     448RT_EXPORT_SYMBOL(RTCrStoreCreateInMemEx);
     449
     450
     451RTDECL(int) RTCrStoreCreateInMem(PRTCRSTORE phStore, uint32_t cSizeHint)
     452{
     453    return RTCrStoreCreateInMemEx(phStore, cSizeHint, NIL_RTCRSTORE);
     454}
    384455RT_EXPORT_SYMBOL(RTCrStoreCreateInMem);
    385456
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette