FreeRDP
Loading...
Searching...
No Matches
TestServerCertificateWrite.c
1#include <stdio.h>
2#include <string.h>
3
4#include <winpr/wtypes.h>
5#include <winpr/crt.h>
6#include <winpr/stream.h>
7#include <freerdp/crypto/certificate.h>
8
9#include "../certificate.h"
10
11/* Write the certificate into a stream that starts with capacity bytes and
12 * return the bytes written (including the dwVersion field). */
13WINPR_ATTR_MALLOC(free, 1)
14static void* test_write_cert(const rdpCertificate* cert, UINT32 version, size_t capacity,
15 size_t* plen)
16{
17 void* data = nullptr;
18 wStream* s = Stream_New(nullptr, capacity);
19 if (!s)
20 return nullptr;
21
22 const SSIZE_T len = freerdp_certificate_write_server_cert(cert, version, s);
23 if (len <= 0)
24 goto fail;
25
26 *plen = Stream_GetPosition(s);
27 if (*plen > 0)
28 data = malloc(*plen);
29 if (data)
30 memcpy(data, Stream_Buffer(s), *plen);
31
32fail:
33 Stream_Free(s, TRUE);
34 return data;
35}
36
37WINPR_ATTR_NODISCARD
38static BOOL test_version(const rdpCertificate* cert, UINT32 version)
39{
40 BOOL rc = FALSE;
41 size_t smallLen = 0;
42 size_t largeLen = 0;
43
44 void* smallBuf = test_write_cert(cert, version, 16u, &smallLen);
45 void* largeBuf = test_write_cert(cert, version, 0x10000u, &largeLen);
46
47 if (!smallBuf || !largeBuf)
48 {
49 (void)fprintf(stderr, "failed to write server certificate version 0x%08" PRIx32 "\n",
50 version);
51 }
52 else if ((smallLen != largeLen) || (memcmp(smallBuf, largeBuf, smallLen) != 0))
53 {
54 (void)fprintf(stderr,
55 "server certificate version 0x%08" PRIx32
56 " depends on the initial stream capacity\n",
57 version);
58 }
59 else
60 rc = TRUE;
61
62 free(smallBuf);
63 free(largeBuf);
64 return rc;
65}
66
67int TestServerCertificateWrite(int argc, char* argv[])
68{
69 int rc = -1;
70 WINPR_UNUSED(argc);
71 WINPR_UNUSED(argv);
72
73 rdpCertificate* cert =
74 freerdp_certificate_new_from_file(TEST_SOURCE_DIR "/rsa_pkcs1_sha256_cert.pem");
75 if (!cert)
76 {
77 (void)fprintf(stderr, "failed to load test certificate\n");
78 return -1;
79 }
80
81 if (!test_version(cert, CERT_TEMPORARILY_ISSUED | CERT_CHAIN_VERSION_1))
82 goto fail;
83 if (!test_version(cert, CERT_CHAIN_VERSION_2))
84 goto fail;
85
86 rc = 0;
87fail:
88 freerdp_certificate_free(cert);
89 return rc;
90}