Search code examples
c#compact-frameworkwindows-cevpnras

How to set a PreSharedKey in a RasEntry on Windows CE / Compact Framework?


Since I really don't get any progress in the last hours I need to consult you for a problem which I don't get solved. We have a Win CE 5.0 application, written C#/Compact Frmaework 2.0 that uses RASDial to Dial into a VPN. Currently it uses PPTP but I have to change it to L2TP with a Pre Shard Key. But to be honest I have no experience in C++ and I really understand only half of the code or to be more clear I don't understand the RAS Api and Documentation in the MSDN. I understand how to create this L2PT RAS Entry and how to Dial it but in no way I understand where and how to set the Pre Shared Key! I found a peace of code that seems to do the same things our code does in priciple but on the Website/Board I found it the Author says this is with pre shared key but to be honest, I don't get where the key is.

(...)

// Device configuration for L2TP VPN 
if (bIsL2TP) { 
    DWORD cbKey = 0; 
    if (g_sharedKey) { 
        cbKey = (wcslen(g_sharedKey))*sizeof(WCHAR); 
    } 
    pL2TPConfigData = (PL2TP_CONFIG_DATA)new BYTE 
[sizeof(L2TP_CONFIG_DATA)+ cbKey]; 


    ZeroMemory(pL2TPConfigData, sizeof(L2TP_CONFIG_DATA)+ cbKey); 
    pL2TPConfigData->dwVersion = 1; 
    pL2TPConfigData->dwAuthType = L2TP_IPSEC_AUTH_PRESHAREDKEY; 
    pL2TPConfigData->dwFlags = 0; 
    pL2TPConfigData->cbKey = cbKey; 
    pL2TPConfigData->dwOffsetKey = sizeof(L2TP_CONFIG_DATA); 
    pL2TPConfigData->cMyCerts = 0; 
    pL2TPConfigData->cRootCerts = 0; 
    pL2TPConfigData->dwOffsetCertHashes = sizeof(L2TP_CONFIG_DATA); 
    if (g_sharedKey) { 
        memcpy((PBYTE)pL2TPConfigData+pL2TPConfigData->dwOffsetKey, 
g_sharedKey, cbKey); 
    } 
    pConfigData = (PBYTE)pL2TPConfigData; 
    cbConfigData = sizeof(L2TP_CONFIG_DATA) + cbKey; 
} 


(...) 


// Create a new phone-book entry. 
res = ::RasSetEntryProperties(NULL, g_entryName, &rasEntry, sizeof 
(rasEntry), pConfigData, cbConfigData); 
if (res != 0) { 
    wprintf(L"Cannot create or update the phone book entry (error# %u). 
Aborting.", res); 
    goto exit; 
} 

In the code the Length (cbKey) of the key is determined but can someone explain to me where the actual key is in the code? Or can someone provide me an explaination on how to set a Pre Shared Key in RASEntry for L2TP?

Thank you so much

twickl


Solution

  • The pre-shared key is copied into the L2TP_CONFIG_DATA structure with this line:

    memcpy((PBYTE)pL2TPConfigData+pL2TPConfigData->dwOffsetKey, g_sharedKey, cbKey);
    

    Basically this line says "copy the data from g_sharedKey into the pL2TPConfigData instance, starting at an offset of pL2TPConfigData->dwOffsetKey for a length of cbKey"

    The code wraps this in an if block, so if g_sharedKey is NULL, it doesn't do this copy.