| 1 |
/** |
| 2 |
* WinPR: Windows Portable Runtime |
| 3 |
* Buffer Manipulation |
| 4 |
* |
| 5 |
* Copyright 2025 David Fort <[email protected]> |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
* you may not use this file except in compliance with the License. |
| 9 |
* You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, software |
| 14 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 15 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 |
* See the License for the specific language governing permissions and |
| 17 |
* limitations under the License. |
| 18 |
*/ |
| 19 |
#include <string.h> |
| 20 |
#include <winpr/cred.h> |
| 21 |
|
| 22 |
typedef struct |
| 23 |
{ |
| 24 |
LPCSTR marshalled; |
| 25 |
BYTE source[CERT_HASH_LENGTH]; |
| 26 |
} TestItem; |
| 27 |
|
| 28 |
static const TestItem testValues[] = { |
| 29 |
{ "@@BQ9eNR0KWVU-CT8sPCp8z37POZHJ", |
| 30 |
{ 0x50, 0xef, 0x35, 0x11, 0xad, 0x58, 0x15, 0xf5, 0x0b, 0x13, |
| 31 |
0xcf, 0x3e, 0x42, 0xca, 0xcf, 0xf7, 0xfe, 0x38, 0xd9, 0x91 } }, |
| 32 |
{ "@@BKay-HwJsFZzclXAWZ#nO6Eluc7P", |
| 33 |
{ 0x8a, 0x26, 0xff, 0x07, 0x9c, 0xb0, 0x45, 0x36, 0x73, 0xe5, |
| 34 |
0x05, 0x58, 0x99, 0x7f, 0x3a, 0x3a, 0x51, 0xba, 0xdc, 0xfe } |
| 35 |
|
| 36 |
} |
| 37 |
}; |
| 38 |
|
| 39 |
static int TestUnmarshal(WINPR_ATTR_UNUSED int argc, WINPR_ATTR_UNUSED char** argv) |
| 40 |
{ |
| 41 |
|
| 42 |
for (size_t i = 0; i < ARRAYSIZE(testValues); i++) |
| 43 |
{ |
| 44 |
CRED_MARSHAL_TYPE t = BinaryBlobForSystem; |
| 45 |
CERT_CREDENTIAL_INFO* certInfo = nullptr; |
| 46 |
const TestItem* const val = &testValues[i]; |
| 47 |
|
| 48 |
if (!CredUnmarshalCredentialA(val->marshalled, &t, (void**)&certInfo) || !certInfo || |
| 49 |
(t != CertCredential)) |
| 50 |
return -1; |
| 51 |
|
| 52 |
const BOOL ok = |
| 53 |
memcmp(val->source, certInfo->rgbHashOfCert, sizeof(certInfo->rgbHashOfCert)) == 0; |
| 54 |
|
| 55 |
free(certInfo); |
| 56 |
|
| 57 |
if (!ok) |
| 58 |
return -1; |
| 59 |
} |
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
static int TestMarshal(WINPR_ATTR_UNUSED int argc, WINPR_ATTR_UNUSED char** argv) |
| 64 |
{ |
| 65 |
|
| 66 |
for (size_t i = 0; i < ARRAYSIZE(testValues); i++) |
| 67 |
{ |
| 68 |
CERT_CREDENTIAL_INFO certInfo = { sizeof(certInfo), WINPR_C_ARRAY_INIT }; |
| 69 |
const TestItem* const val = &testValues[i]; |
| 70 |
|
| 71 |
memcpy(certInfo.rgbHashOfCert, val->source, sizeof(certInfo.rgbHashOfCert)); |
| 72 |
LPSTR out = nullptr; |
| 73 |
|
| 74 |
if (!CredMarshalCredentialA(CertCredential, &certInfo, &out) || !out) |
| 75 |
return -1; |
| 76 |
|
| 77 |
BOOL ok = (strcmp(val->marshalled, out) == 0); |
| 78 |
|
| 79 |
free(out); |
| 80 |
|
| 81 |
if (!ok) |
| 82 |
return -1; |
| 83 |
} |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
int TestMarshalUnmarshal(int argc, char** argv) |
| 88 |
{ |
| 89 |
int ret = TestUnmarshal(argc, argv); |
| 90 |
if (ret) |
| 91 |
return ret; |
| 92 |
|
| 93 |
ret = TestMarshal(argc, argv); |
| 94 |
return ret; |
| 95 |
} |