summaryrefslogtreecommitdiff
path: root/tests/windows/cng-windows.c
blob: 9db5699195171fc1ece0a47389bc6efab1e64f20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (C) 2015 Nikos Mavrogiannopoulos
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * GnuTLS is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuTLS is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GnuTLS.  If not, see <https://www.gnu.org/licenses/>.
 */

/* This does some basic functionality testing for the system-keys in
 * windows for CNG keys.
 * It relies on the ncrypt, and crypt32 replacements found in the dir.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifndef _WIN32

#include <stdlib.h>

void doit(void)
{
	exit(77);
}

#else

#include <windows.h>
#include <wincrypt.h>
#include <winbase.h>
#include <ncrypt.h>
#include <string.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include <gnutls/abstract.h>
#include <gnutls/system-keys.h>
#include <gnutls/crypto.h>
#include <stdio.h>
#include <assert.h>
#include <utils.h>
#include "../cert-common.h"
#include "ncrypt-int.h"

static void tls_log_func(int level, const char *str)
{
	fprintf(stderr, "<%d> %s", level, str);
}

/* sha1 hash of "hello" string */
const gnutls_datum_t sha256_hash_data = {
	(void *)"\x2c\xf2\x4d\xba\x5f\xb0\xa3\x0e\x26\xe8"
		"\x3b\x2a\xc5\xb9\xe2\x9e\x1b\x16\x1e\x5c"
		"\x1f\xa7\x42\x5e\x73\x04\x33\x62\x93\x8b"
		"\x98\x24",
	32
};

const gnutls_datum_t md5sha1_hash_data = {
	(void *)"\x5d\x41\x40\x2a\xbc\x4b\x2a\x76\xb9\x71\x9d\x91\x10\x17\xc5\x92"
		"\xaa\xf4\xc6\x1d\xdc\xc5\xe8\xa2\xda\xbe\xde\x0f\x3b\x48\x2c\xd9\xae\xa9\x43\x4d",
	36
};

const gnutls_datum_t invalid_hash_data = {
	(void *)"\xaa\xf4\xc6\x1d\xdc\xca\xe8\xa2\xda\xbe"
		"\xde\x0f\x3b\x48\x2c\xb9\xae\xa9\x43\x4d",
	20
};

const gnutls_datum_t raw_data = { (void *)"hello", 5 };

static void test_sig(void)
{
	gnutls_pubkey_t pubkey;
	gnutls_privkey_t privkey;
	gnutls_sign_algorithm_t sign_algo = GNUTLS_SIGN_RSA_SHA256;
	gnutls_datum_t signature, digest_info;
	int ret;

	assert_int_nequal(gnutls_pubkey_init(&pubkey), 0);

	assert_int_nequal(gnutls_privkey_init(&privkey), 0);

	assert_int_nequal(gnutls_privkey_import_url(privkey,
						    "system:win:id=123456", 0),
			  0);

	assert_int_nequal(gnutls_pubkey_import_x509_raw(pubkey, &cert_dat,
							GNUTLS_X509_FMT_PEM, 0),
			  0);

	assert_int_nequal(gnutls_privkey_sign_hash(privkey, GNUTLS_DIG_SHA256,
						   0, &sha256_hash_data,
						   &signature),
			  0);

	ret = gnutls_pubkey_verify_hash2(pubkey, sign_algo, 0,
					 &sha256_hash_data, &signature);
	assert(ret >= 0);

	/* should fail */
	ret = gnutls_pubkey_verify_hash2(pubkey, sign_algo, 0,
					 &invalid_hash_data, &signature);
	assert(ret == GNUTLS_E_PK_SIG_VERIFY_FAILED);

	gnutls_free(signature.data);
	signature.data = NULL;
	/* test the raw interface (MD5+SHA1)
	 */
	ret = gnutls_privkey_sign_hash(privkey, 0,
				       GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA,
				       &md5sha1_hash_data, &signature);
	assert(ret >= 0);

	ret = gnutls_pubkey_verify_hash2(pubkey, 0,
					 GNUTLS_PUBKEY_VERIFY_FLAG_TLS1_RSA,
					 &md5sha1_hash_data, &signature);
	assert(ret >= 0);

	gnutls_free(signature.data);
	signature.data = NULL;

	/* test the raw interface DigestInfo
	 */
	ret = gnutls_encode_ber_digest_info(GNUTLS_DIG_SHA256,
					    &sha256_hash_data, &digest_info);
	assert(ret >= 0);

	ret = gnutls_privkey_sign_hash(privkey, 0,
				       GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA,
				       &digest_info, &signature);
	assert(ret >= 0);

	ret = gnutls_pubkey_verify_hash2(pubkey, 0,
					 GNUTLS_PUBKEY_VERIFY_FLAG_TLS1_RSA,
					 &digest_info, &signature);
	assert(ret >= 0);

	gnutls_free(signature.data);
	gnutls_free(digest_info.data);
	gnutls_privkey_deinit(privkey);
	gnutls_pubkey_deinit(pubkey);
}

void doit(void)
{
	gnutls_global_init();
	gnutls_global_set_log_function(tls_log_func);
	if (debug)
		gnutls_global_set_log_level(6);

	test_sig();

	gnutls_global_deinit();
	return;
}

#endif