-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrsa_to_keyblob.c
257 lines (220 loc) · 8.65 KB
/
rsa_to_keyblob.c
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* Copyright (c) 2006-2008 Secure Endpoints Inc.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*
* Copyright © 2000
* The Regents of the University of Michigan
* ALL RIGHTS RESERVED
*
* permission is granted to use, copy, create derivative works
* and redistribute this software and such derivative works
* for any purpose, so long as the name of the university of
* michigan is not used in any advertising or publicity
* pertaining to the use or distribution of this software
* without specific, written prior authorization. if the
* above copyright notice or any other identification of the
* university of michigan is included in any copy of any
* portion of this software, then the disclaimer below must
* also be included.
*
* this software is provided as is, without representation
* from the university of michigan as to its fitness for any
* purpose, and without warranty by the university of
* michigan of any kind, either express or implied, including
* without limitation the implied warranties of
* merchantability and fitness for a particular purpose. the
* regents of the university of michigan shall not be liable
* for any damages, including special, indirect, incidental, or
* consequential damages, with respect to any claim arising
* out of or in connection with the use of the software, even
* if it has been or is hereafter advised of the possibility of
* such damages.
*/
#include <tchar.h>
#include <stdio.h>
#define __WINCRYPT_H__ // PREVENT windows.h from including wincrypt.h
// since wincrypt.h and openssl namepsaces collide
// ex. X509_NAME is #define'd and typedef'd ...
#include <windows.h>
#include <openssl/x509v3.h>
#include <openssl/pem.h>
#include "debug.h"
#include <strsafe.h>
#include <assert.h>
/*
* Documentation for PRIVATEKEYBLOB
* http://msdn.microsoft.com/en-us/library/aa387401(VS.85).aspx
*/
typedef unsigned int ALG_ID;
/*#if 0*/
#pragma pack (push, 1)
typedef struct _PUBLICKEYSTRUC {
BYTE bType;
BYTE bVersion;
WORD reserved;
ALG_ID aiKeyAlg;
} PUBLICKEYSTRUC;
typedef struct _RSAPUBKEY {
DWORD magic; // Has to be "RSA1" or "RSA2"
DWORD bitlen; // # of bits in modulus
DWORD pubexp; // public exponent
// Modulus data follows
} RSAPUBKEY;
/*#endif*/
typedef struct _PRIVKEYBLOB {
PUBLICKEYSTRUC blobheader;
RSAPUBKEY rsapubkey;
BN_ULONG beginning[1];
// BYTE modulus[KEYBITS/8]; // "n"
// BYTE prime1[KEYBITS/16]; // "p"
// BYTE prime2[KEYBITS/16]; // "q"
// BYTE exponent1[KEYBITS/16]; // "dmp1"
// BYTE exponent2[KEYBITS/16]; // "dmq1"
// BYTE coefficient[KEYBITS/16]; // "iqmp"
// BYTE privateExponent[KEYBITS/8]; // "d"
} PRIVKEYBLOB;
#pragma pack (pop)
void hexdump(void *pin, char *label, int len)
{
BYTE *p = (BYTE *)pin;
int i;
log_printf("%s (%0d bytes):", label, len*BN_BYTES);
for (i=0; i<len*BN_BYTES; i++)
{
if ((i & 0x7) == 0)
log_printf("\n ");
log_printf("%#04X, ", p[i]);
}
log_printf("\n\n");
}
LPVOID GetLastErrorText()
{
DWORD dwFmtMsgFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS;
LPVOID lpMsgBuf = NULL;
FormatMessageA(dwFmtMsgFlags,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR) &lpMsgBuf,
0,
NULL);
return lpMsgBuf;
}
void HandleError(char *s)
{
DWORD dwFmtMsgFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS;
LPVOID lpMsgBuf = NULL;
char msg[1024];
DWORD dwLastError = GetLastError();
FormatMessageA(dwFmtMsgFlags,
NULL,
dwLastError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR) &lpMsgBuf,
0,
NULL);
StringCbPrintfA(msg, sizeof(msg),
"%s\nError code 0x%08x -- %s", s, dwLastError, lpMsgBuf ? lpMsgBuf : "");
MessageBoxA(0, msg, "KX509: Error!", MB_OK|MB_ICONERROR);
if (lpMsgBuf)
LocalFree(lpMsgBuf);
}
#define MAX_UNIQNAME_LEN 8
int rsa_to_keyblob(int keybits, RSA *key, BYTE **ppk, DWORD *pcbPk)
{
PRIVKEYBLOB *pk;
RSAPUBKEY *pPub;
BN_ULONG *p;
BN_ULONG *key_n, *key_p, *key_q, *key_dmp1, *key_dmq1, *key_iqmp, *key_d;
DWORD l=0;
extern int debugPrint;
assert(BN_BYTES == sizeof(BN_ULONG));
// setup PUBLICKEYSTRUC (aka BLOBHEADER) first
log_printf("BN_BYTES = %d, BN_BITS2 = %d\n", BN_BYTES, BN_BITS2);
*pcbPk = 0;
l = sizeof(PRIVKEYBLOB) - sizeof(BN_ULONG) +
keybits/8 + // modulus[KEYBITS/8]; // "n"
keybits/16 +// prime1[KEYBITS/16]; // "p"
keybits/16 +// prime2[KEYBITS/16]; // "q"
keybits/16 +// exponent1[KEYBITS/16]; // "dmp1"
keybits/16 +// exponent2[KEYBITS/16]; // "dmq1"
keybits/16 +// coefficient[KEYBITS/16]; // "iqmp"
keybits/8; // privateExponent[KEYBITS/8]; // "d"
*ppk = (BYTE *)malloc(l);
pk = (PRIVKEYBLOB *)*ppk;
if (!pk)
{
return 0;
}
memset(pk, 0, l);
*pcbPk = l;
pk->blobheader.bType= 0x07; // PRIVATEKEYBLOB
pk->blobheader.bVersion= 0x02; // CUR_BLOB_VERSION
pk->blobheader.reserved= 0x0000; // 0x0000
// pk->blobheader.aiKeyAlg= 0x00002400; // CALG_RSA_SIGN
pk->blobheader.aiKeyAlg= 0x0000A400; // CALG_RSA_KEYX
log_printf("pk->blobheader.bType=%#04X\n", pk->blobheader.bType);
log_printf("pk->blobheader.bVersion=%#04X\n", pk->blobheader.bVersion);
log_printf("pk->blobheader.reserved=%#06X\n", pk->blobheader.reserved);
log_printf("pk->blobheader.aiKeyAlg=%#010X\n", pk->blobheader.aiKeyAlg);
log_printf("\n");
// setup RSAPUBKEY next
pPub = &pk->rsapubkey;
memcpy(&pPub->magic, "RSA2", 4); // "RSA2" means PRIVKEYBLOB
pPub->bitlen=keybits;
pPub->pubexp=RSA_F4;
log_printf("pk->rsapubkey.magic=%0ld ('%s')\n", pPub->magic, &pPub->magic);
log_printf("pk->rsapubkey.bitlen=%0ld (%#010lX)\n", pPub->bitlen, pPub->bitlen);
log_printf("pk->rsapubkey.pubexp=%0ld (%#010lX)\n", pPub->pubexp, pPub->pubexp);
log_printf("\n");
// and finally, the KEY-INFO itself (n, p, q, dmp1, dmpq1, iqmp, & d)
key_n = p = &pk->beginning[0];
memcpy(p, key->n->d, BN_BYTES * key->n->top);
key_p = p += (keybits/BN_BYTES/8);
memcpy(p, key->p->d, BN_BYTES * key->p->top);
key_q = p += (keybits/BN_BYTES/16);
memcpy(p, key->q->d, BN_BYTES * key->q->top);
key_dmp1 = p += (keybits/BN_BYTES/16);
memcpy(p, key->dmp1->d, BN_BYTES * key->dmp1->top);
key_dmq1 = p += (keybits/BN_BYTES/16);
memcpy(p, key->dmq1->d, BN_BYTES * key->dmq1->top);
key_iqmp = p += (keybits/BN_BYTES/16);
memcpy(p, key->iqmp->d, BN_BYTES * key->iqmp->top);
key_d = p += (keybits/BN_BYTES/16);
memcpy(p, key->d->d, BN_BYTES * key->d->top);
if (debugPrint)
{
hexdump((BYTE *)key_n, "pk->modulus", key->n->top);
hexdump((BYTE *)key_p, "pk->prime1", key->p->top);
hexdump((BYTE *)key_q, "pk->prime2", key->q->top);
hexdump((BYTE *)key_dmp1, "pk->exponent1", key->dmp1->top);
hexdump((BYTE *)key_dmq1, "pk->exponent2", key->dmq1->top);
hexdump((BYTE *)key_iqmp, "pk->coefficient", key->iqmp->top);
hexdump((BYTE *)key_d, "pk->privateExponent", key->d->top);
}
return 1;
}