libcollectdclient: Fix size argument passed to CryptImportKey().
[collectd.git] / src / libcollectdclient / win_hmac.c
1 /**
2  * Copyright (c) 2010-2014  Florian Forster
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  **/
22
23 #include "win_hmac.h"
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <assert.h>
28
29 /*
30  * Creating a HCRYPTKEY from a plain text password is probably the most tricky
31  * part when calculating an RFC2104 HMAC using Microsoft CryptoAPI. The
32  * examples provided at the MSDN website encourage you to use "CryptDeriveKey",
33  * which doesn't do the conditional hashing required by HMAC. (That is, it
34  * might. The documentation on any of the functions is so vague that it's well
35  * possible the elegant solution is just not documented nor demonstrated.
36  */
37 static HCRYPTKEY create_hmac_key_exact (HCRYPTPROV hProv,
38                                                                                 BYTE *pbKey, DWORD dwKeySize)
39 {
40         /* Layout of the memory expected when importing a plain text blob is
41          * documented at the "CryptImportKey" under "Remarks". The four bytes
42          * following the PUBLICKEYSTRUC structure hold the size of the key, then
43          * follows the key itself. */
44         struct plain_text_data_s
45         {
46                 PUBLICKEYSTRUC blob;
47                 DWORD key_size;
48         } *data;
49         DWORD data_size;
50         HCRYPTKEY ret_key = 0;
51         BOOL status;
52
53         data_size = sizeof (*data) + dwKeySize;
54         data = (struct plain_text_data_s *) malloc (data_size);
55         if (data == NULL)
56                 return (0);
57         memset (data, 0, data_size);
58
59         /* The key is not encrypted. */
60         data->blob.bType = PLAINTEXTKEYBLOB;
61         data->blob.bVersion = CUR_BLOB_VERSION;
62         data->blob.reserved = 0;
63         /* The "CryptImportKey" page explicitly states that "RC2" is to be used for
64          * HMAC keys. What anyone might have been thinking, I don't know. */
65         data->blob.aiKeyAlg = CALG_RC2;
66
67         /* Copy the key to the memory right behind the struct. "memcpy" should only
68          * add memory protection crap *after* the region written to, so the blob
69          * shouldn't be destroyed. We play it save, though, and set the key size
70          * last. Should problems arise, we should switch to a loop just to be sure. */
71         memcpy (data + 1, pbKey, dwKeySize);
72         data->key_size = dwKeySize;
73
74         /* Actually convert our memory region to this mysterious key structure.
75          * The "CRYPT_IPSEC_HMAC_KEY" is required to allow RC2 keys longer than
76          * 16 byte. Again, this is documented on the "CryptImportKey" page as a
77          * side note. */
78         status = CryptImportKey (hProv, (BYTE *) data,
79                 /* dwDataLen = */ sizeof (*data) + data->key_size,
80                 /* public key = */ 0,
81                 /* flags = */ CRYPT_IPSEC_HMAC_KEY,
82                 &ret_key);
83         if (!status)
84         {
85                 free (data);
86                 return (0);
87         }
88
89         free (data);
90         return (ret_key);
91 } /* HCRYPTKEY create_hmac_key_exact */
92
93 /* If the key of the HMAC is larger than the hash size, use the hash of the
94  * key instead of using the key directly. */
95 static HCRYPTKEY create_hmac_key_hashed (HCRYPTPROV hProv,
96                                                                                  BYTE *pbKey, DWORD dwKeySize,
97                                                                                  DWORD dwHashSize, HCRYPTHASH hHash)
98 {
99         BOOL status;
100         BYTE *hash_data;
101         DWORD hash_data_size;
102         HCRYPTKEY key;
103
104         assert (dwKeySize > dwHashSize);
105
106         status = CryptHashData (hHash, pbKey, dwKeySize, /* dwFlags = */ 0);
107         if (!status)
108                 return (0);
109
110         hash_data = (BYTE *) malloc (dwHashSize);
111         if (hash_data == NULL)
112                 return (0);
113         memset (hash_data, 0, dwHashSize);
114         hash_data_size = dwHashSize;
115
116         status = CryptGetHashParam (hHash, HP_HASHVAL,
117                 hash_data, &hash_data_size, /* flags = */ 0);
118         if (!status)
119         {
120                 free (hash_data);
121                 return (0);
122         }
123
124         assert (hash_data_size == dwHashSize);
125
126         key = create_hmac_key_exact (hProv, hash_data, hash_data_size);
127
128         free (hash_data);
129         return (key);
130 } /* HCRYPTKEY create_hmac_key_hashed */
131
132 /* If the key is short enough, it is (right-)padded with zeros and otherwise
133  * used as-is. */
134 static HCRYPTKEY create_hmac_key_padded (HCRYPTPROV hProv,
135                                                                                  BYTE *pbKey, DWORD dwKeySize,
136                                                                                  DWORD dwHashSize)
137 {
138         BYTE *padded_key;
139         HCRYPTKEY key;
140         DWORD i;
141
142         assert (dwKeySize <= dwHashSize);
143
144         if (dwKeySize == dwHashSize)
145                 return (create_hmac_key_exact (hProv, pbKey, dwKeySize));
146
147         padded_key = (BYTE *) malloc (dwHashSize);
148         if (padded_key == NULL)
149                 return (0);
150
151         /* Copy the key and right-pad with zeros. Don't use "memcpy" here because
152          * the fucked up version of VS will corrupt memory. */
153         for (i = 0; i < dwHashSize; i++)
154                 padded_key[i] = (i < dwKeySize) ? pbKey[i] : 0;
155
156         key = create_hmac_key_exact (hProv, padded_key, dwHashSize);
157
158         free (padded_key);
159         return (key);
160 } /* HCRYPTKEY create_hmac_key_padded */
161
162 static HCRYPTKEY create_hmac_key (HCRYPTPROV hProv,
163                                                                   ALG_ID Algid,
164                                                                   BYTE *pbKey, DWORD dwKeySize)
165 {
166         HCRYPTHASH hash = 0;
167         HCRYPTKEY key;
168         DWORD hash_size = 0;
169         DWORD param_size;
170         BOOL status;
171
172         /* Allocate a hash object to determine the hash size. */
173         status = CryptCreateHash (hProv, Algid,
174                 /* hKey = */ 0,
175                 /* dwFlags = */ 0,
176                 /* out phHash = */ &hash);
177         if (!status)
178                 return (0);
179
180         param_size = (DWORD) sizeof (hash_size);
181         status = CryptGetHashParam (hash, HP_HASHSIZE,
182                 (BYTE *) &hash_size, &param_size,
183                 /* flags = */ 0);
184         if (!status)
185         {
186                 CryptDestroyHash (hash);
187                 return (0);
188         }
189
190         /* Determine whether we need to calculate the hash of the key or if
191          * padding is sufficient. */
192         if (dwKeySize > hash_size)
193                 key = create_hmac_key_hashed (hProv, pbKey, dwKeySize, hash_size, hash);
194         else
195                 key = create_hmac_key_padded (hProv, pbKey, dwKeySize, hash_size);
196
197         CryptDestroyHash (hash);
198         return (key);
199 } /* HCRYPTKEY create_hmac_key */
200
201 BOOL CreateHMAC (HCRYPTPROV hProv,
202                                  ALG_ID Algid,
203                                  BYTE *pbKey, DWORD dwKeySize,
204                                  DWORD dwFlags,
205                                  HCRYPTHASH *phHash,
206                                  HCRYPTKEY *phKey)
207 {
208         HCRYPTKEY hmac_key;
209         HCRYPTHASH hash = 0;
210         HMAC_INFO hmac_info;
211         BOOL status;
212
213         hmac_key = create_hmac_key (hProv, Algid, pbKey, dwKeySize);
214         if (hmac_key == 0)
215                 return (FALSE);
216
217         status = CryptCreateHash (hProv, CALG_HMAC, hmac_key,
218                 /* flags = */ dwFlags,
219                 &hash);
220         if (!status)
221         {
222                 CryptDestroyKey (hmac_key);
223                 return (status);
224         }
225
226         memset (&hmac_info, 0, sizeof (hmac_info));
227         hmac_info.HashAlgid = Algid;
228         hmac_info.pbInnerString = NULL;
229         hmac_info.cbInnerString = 0;
230         hmac_info.pbOuterString = NULL;
231         hmac_info.cbOuterString = 0;
232
233         status = CryptSetHashParam (hash, HP_HMAC_INFO, (BYTE *) &hmac_info,
234                 /* flags = */ 0);
235         if (!status)
236         {
237                 CryptDestroyHash (hash);
238                 CryptDestroyKey (hmac_key);
239                 return (status);
240         }
241
242         *phHash = hash;
243         *phKey = hmac_key;
244         return (TRUE);
245 } /* BOOL CreateHMAC */
246
247 BOOL DestroyHMAC (HCRYPTHASH hHash, HCRYPTKEY hKey)
248 {
249         if (hHash != 0)
250                 CryptDestroyHash (hHash);
251
252         if (hKey != 0)
253                 CryptDestroyKey (hKey);
254
255         return (TRUE);
256 } /* BOOL DestroyHMAC */
257
258 /* vim: set ts=4 sw=4 noet : */