/*++

Copyright (c) 1996, 1997  Microsoft Corporation

Module Name:

    certprot.cpp

Abstract:

    This module contains routines associated with server side 
    I_CertProtectFunctions operations.

Author:

    Phil Hallin (philh)     19-Nov-97

--*/

#include <windows.h>
#include <wincrypt.h>

#include "cerrpc.h" // header file generated by MIDL compiler
#include "certprot.h"



///////////////////////////////////////////////////////////////////////
// LPC-exposed functions

//
// these functions return a DWORD equivalent to GetLastError().
// the client side stub code will check if the return code is not
// ERROR_SUCCESS, and if this is the case, the client stub will return
// FALSE and SetLastError() to this DWORD.
//

DWORD s_SSCertProtectFunction( 
    /* [in] */ handle_t h,
    /* [in] */ DWORD dwFuncId,
    /* [in] */ DWORD dwFlags,
    /* [in] */ LPCWSTR pwszIn,
    /* [size_is][in] */ BYTE __RPC_FAR *pbIn,
    /* [in] */ DWORD cbIn,
    /* [size_is][size_is][out] */ BYTE __RPC_FAR *__RPC_FAR *ppbOut,
    /* [out] */ DWORD __RPC_FAR *pcbOut)
{
    DWORD dwRet;

    __try {

        {
            HINSTANCE hCrypt32Dll;
            PFN_CERT_SRV_PROTECT_FUNCTION pfnCertSrvProtectFunction;

            *ppbOut = NULL;
            *pcbOut = 0;
            hCrypt32Dll = LoadLibraryW(L"crypt32.dll");
            if (hCrypt32Dll == NULL) {
                dwRet = GetLastError();
                goto Ret;
            }

            if (NULL == (pfnCertSrvProtectFunction =
                    (PFN_CERT_SRV_PROTECT_FUNCTION) GetProcAddress(
                        hCrypt32Dll, "I_CertSrvProtectFunction")))
                dwRet = ERROR_PROC_NOT_FOUND;
            else
                dwRet = pfnCertSrvProtectFunction(
                    h,
                    dwFuncId,
                    dwFlags,
                    pwszIn,
                    pbIn,
                    cbIn,
                    ppbOut,
                    pcbOut,
                    midl_user_allocate,
                    midl_user_free
                    );
            FreeLibrary(hCrypt32Dll);
            goto Ret;
        }

    } __except( EXCEPTION_EXECUTE_HANDLER ) {
        dwRet = GetExceptionCode();
        // TODO: for NT, convert exception code to winerror.
        //       for 95, just override to access violation.
    }

Ret:
    return dwRet;
}
