/
keycompare.c
296 lines (264 loc) · 11.2 KB
/
keycompare.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* ------------------------------------------------------------ *
* file: keycompare.c *
* purpose: Example code to check if a private key belongs *
* to a certificate. *
* author: 05/15/2015 Frank4DD *
* *
* gcc -o keycompare keycompare.c -lssl -lcrypto *
* *
* Updated to work with OpenSSL version 3, as well as old 1.1.1 *
* ------------------------------------------------------------ */
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <string.h>
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#include <openssl/evp.h>
#include <openssl/core_names.h>
static void set_optional_params(OSSL_PARAM *);
#else
const unsigned char pad = RSA_PKCS1_PADDING;
#endif
int RSAcmp_mod(EVP_PKEY *, EVP_PKEY *);
int RSAencrypt(EVP_PKEY *, const unsigned char *);
int RSAdecrypt(EVP_PKEY *, int);
ECDSA_SIG *ECsign(EVP_PKEY *, const unsigned char *);
int ECverify(EVP_PKEY *, const unsigned char *, ECDSA_SIG *);
unsigned char *encdata = NULL;
unsigned char *decdata = NULL;
int teststr_len, ret;
/* ---------------------------------------------------------- *
* RSAcmp_mod compares RSA public modulus n of pub and priv *
* returns 0 if key modulus match, and 1 for mismatch *
* -----------------------------------------------------------*/
int RSAcmp_mod(EVP_PKEY *priv, EVP_PKEY *pub) {
int match;
#if OPENSSL_VERSION_NUMBER < 0x30000000L
/* -------------------------------------------------------- *
* Old code for OpenSSL versions before version 3.0 *
* ---------------------------------------------------------*/
const BIGNUM *privrsa_mod;
const BIGNUM *pubrsa_mod;
RSA *privrsa = EVP_PKEY_get1_RSA(priv);
RSA_get0_key(privrsa, &privrsa_mod, NULL, NULL);
RSA *pubrsa = EVP_PKEY_get1_RSA(pub);
RSA_get0_key(pubrsa, &pubrsa_mod, NULL, NULL);
#else
/* -------------------------------------------------------- *
* New code for latest OpenSSL version 3.0 *
* ---------------------------------------------------------*/
BIGNUM *privrsa_mod = NULL;
BIGNUM *pubrsa_mod = NULL;
EVP_PKEY_get_bn_param(priv, OSSL_PKEY_PARAM_RSA_N, &privrsa_mod);
EVP_PKEY_get_bn_param(pub, OSSL_PKEY_PARAM_RSA_N, &pubrsa_mod);
#endif
char *privrsa_mod_hex = BN_bn2hex(privrsa_mod);
char *pubrsa_mod_hex = BN_bn2hex(pubrsa_mod);
printf("priv: %s\n", privrsa_mod_hex);
printf(" pub: %s\n", pubrsa_mod_hex);
if(strcmp(privrsa_mod_hex, pubrsa_mod_hex) == 0)
match = 0; // the keys modulus is matching
else
match = 1; // the keys modulus don't match
OPENSSL_free(privrsa_mod_hex);
OPENSSL_free(pubrsa_mod_hex);
#if OPENSSL_VERSION_NUMBER < 0x30000000L
RSA_free(privrsa);
RSA_free(pubrsa);
#endif
return match;
}
int main() {
const char cert_filestr[] = "./demo/cert-file.pem";
const char pkey_filestr[] = "./demo/cert-file.key";
const unsigned char cleartextstr[] = "This line will be encrypted and, if keys match, it decrypts again.";
EVP_PKEY *privkey = NULL;
EVP_PKEY *pubkey = NULL;
BIO *certbio = NULL;
BIO *pkeybio = NULL;
X509 *cert = NULL;
int i, enc_len, dec_len;
#if OPENSSL_VERSION_NUMBER < 0x30000000L
/* ---------------------------------------------------------- *
* These function calls initialize openssl for correct work. *
* ---------------------------------------------------------- */
OpenSSL_add_all_algorithms();
ERR_load_BIO_strings();
ERR_load_crypto_strings();
#endif
/* ---------------------------------------------------------- *
* Create the Input/Output BIO's. *
* ---------------------------------------------------------- */
certbio = BIO_new(BIO_s_file());
pkeybio = BIO_new(BIO_s_file());
/* ---------------------------------------------------------- *
* Load the certificate from file (PEM). *
* ---------------------------------------------------------- */
ret = BIO_read_filename(certbio, cert_filestr);
if (! (cert = PEM_read_bio_X509(certbio, NULL, 0, NULL))) {
printf("Error loading cert into memory\n");
exit(1);
}
/* ---------------------------------------------------------- *
* Load the private key from file (PEM). *
* ---------------------------------------------------------- */
ret = BIO_read_filename(pkeybio, pkey_filestr);
if (! (privkey = PEM_read_bio_PrivateKey(pkeybio, NULL, 0, NULL))) {
printf("Error loading private key into memory\n");
exit(1);
}
/* ---------------------------------------------------------- *
* Extract the certificate's public key data. *
* ---------------------------------------------------------- */
if ((pubkey = X509_get_pubkey(cert)) == NULL) {
printf("Error getting public key from certificate");
exit(1);
}
/* ---------------------------------------------------------- *
* Print the public key information and the key in PEM format *
* ---------------------------------------------------------- */
/* display the key type and size here */
if (pubkey) {
teststr_len = strlen( (const char *) cleartextstr);
switch (EVP_PKEY_id(pubkey)) {
case EVP_PKEY_RSA:
printf("Detected RSA pubkey [%d bit]. RSA modulus (n) check:\n", EVP_PKEY_bits(pubkey));
if(RSAcmp_mod(privkey, pubkey) == 0)
printf("Success: RSA modulus (n) matches between public and private key parts\n\n");
else
printf("Failure: RSA modulus (n) mismatch between public and private key parts\n\n");
printf("Cleartext string check [%d bytes]:\n\"%s\"\n\n",
(int) teststr_len, cleartextstr);
printf("Encrypting text with RSA public key [%d bit]. ", EVP_PKEY_bits(pubkey));
enc_len = RSAencrypt(pubkey, cleartextstr);
printf("Encrypted data (hex) [%d bytes]:\n", enc_len);
for(i=0;i<enc_len;i++) { printf("%02x ", encdata[i]); }
printf("\n\nDecrypting with RSA private key [%d bit]. ", EVP_PKEY_bits(privkey));
dec_len = RSAdecrypt(privkey, enc_len);
if (dec_len == -1)
printf("Failure: Keys don't match!");
else {
printf("Decrypted data [%d bytes]:\n\"%s\"\n", dec_len, decdata);
}
break;
case EVP_PKEY_EC:
printf("Detected EC Key[%d bit].\n\n", EVP_PKEY_bits(pubkey));
printf("Creating signature with EC private key [%d bit]. ", EVP_PKEY_bits(privkey));
ECDSA_SIG *sig = ECsign(privkey, cleartextstr);
printf("\n\nVerifying signature with EC public key [%d bit]. ", EVP_PKEY_bits(pubkey));
ret = ECverify(pubkey, cleartextstr, sig);
break;
default:
printf("Unknown Key [%d bit].\n\n", EVP_PKEY_bits(pubkey));
exit(1);
break;
}
}
/* ---------------------------------------------------------- *
* Free up all structures *
* ---------------------------------------------------------- */
#if OPENSSL_VERSION_NUMBER < 0x30000000L
EVP_PKEY_free(pubkey);
BIO_free_all(certbio);
#endif
exit(0);
}
/* ---------------------------------------------------------- *
* RSA encrypt the teststring with the certificate public key *
* ---------------------------------------------------------- */
int RSAencrypt(EVP_PKEY *key, const unsigned char *teststring) {
int rsa_outlen = 0;
encdata = OPENSSL_zalloc(1024); // allocate and zero 1kb space
#if OPENSSL_VERSION_NUMBER < 0x30000000L
RSA *pubrsa = NULL;
pubrsa = EVP_PKEY_get1_RSA(key);
rsa_outlen = RSA_public_encrypt(teststr_len, teststring, encdata, pubrsa, pad);
RSA_free(pubrsa);
#else
EVP_PKEY_CTX *ctx = NULL;
ctx = EVP_PKEY_CTX_new_from_pkey(NULL, key, NULL);
OSSL_PARAM params[2];
set_optional_params(params);
EVP_PKEY_encrypt_init_ex(ctx, params);
EVP_PKEY_encrypt(ctx, encdata, (size_t *) &rsa_outlen, teststring, teststr_len);
EVP_PKEY_CTX_free(ctx);
#endif
return rsa_outlen;
}
/* ---------------------------------------------------------- *
* Decrypt a teststring with the certificate private key *
* ---------------------------------------------------------- */
int RSAdecrypt(EVP_PKEY *key, int rsa_inlen) {
int rsa_outlen = 0;
decdata = OPENSSL_zalloc(1024); // allocate and zero 1kb space
#if OPENSSL_VERSION_NUMBER < 0x30000000L
RSA *privrsa = NULL;
privrsa = EVP_PKEY_get1_RSA(key);
rsa_outlen = RSA_private_decrypt(rsa_inlen, encdata, decdata, privrsa, pad);
RSA_free(privrsa);
#else
EVP_PKEY_CTX *ctx = NULL;
ctx = EVP_PKEY_CTX_new_from_pkey(NULL, key, NULL);
OSSL_PARAM params[2];
set_optional_params(params);
EVP_PKEY_decrypt_init_ex(ctx, params);
EVP_PKEY_decrypt(ctx, decdata, (size_t *) &rsa_outlen, encdata, rsa_inlen);
#endif
return rsa_outlen;
}
/* ---------------------------------------------------------- *
* Sign the teststring with the certificate private key *
* ---------------------------------------------------------- */
ECDSA_SIG *ECsign(EVP_PKEY *key, const unsigned char *teststring) {
ECDSA_SIG *sig;
#if OPENSSL_VERSION_NUMBER < 0x30000000L
EC_KEY *priveckey = NULL;
priveckey = EVP_PKEY_get1_EC_KEY(key);
sig = ECDSA_do_sign(teststring, teststr_len, priveckey);
EC_KEY_free(priveckey);
#else
EVP_MD_CTX *ctx = NULL;
ctx = EVP_MD_CTX_new();
unsigned char *sigstr;
size_t *sig_len;
EVP_DigestSign(ctx, sigstr, sig_len, teststring, teststr_len);
sig = d2i_ECDSA_SIG(NULL, (const unsigned char **) &sigstr, (long) &sig_len);
EVP_MD_CTX_free(ctx);
#endif
return sig;
}
/* ---------------------------------------------------------- *
* Validate the signature with the certificate public key *
* ---------------------------------------------------------- */
int ECverify(EVP_PKEY *key, const unsigned char *teststring, ECDSA_SIG *sig) {
teststr_len = strlen( (const char *) teststring);
#if OPENSSL_VERSION_NUMBER < 0x30000000L
EC_KEY *pubeckey = NULL;
pubeckey = EVP_PKEY_get1_EC_KEY(key);
ret = ECDSA_do_verify(teststring, teststr_len, sig, pubeckey);
EC_KEY_free(pubeckey);
#else
EVP_MD_CTX *ctx = NULL;
ctx = EVP_MD_CTX_new();
unsigned char *sigstr;
i2d_ECDSA_SIG(sig, &sigstr);
size_t sig_len = strlen( (const char *) sigstr);
/* ---------------------------------------------------------- *
* Successful verification returns 1 *
* ---------------------------------------------------------- */
ret = EVP_DigestVerify(ctx, sigstr, sig_len, teststring, teststr_len);
EVP_MD_CTX_free(ctx);
#endif
return ret;
}
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
/* ------------------------------------------------------------ *
* Helper function Set optional parameters for RSA OAEP Padding *
* ------------------------------------------------------------ */
static void set_optional_params(OSSL_PARAM *p) {
/* "pkcs1" is used by default if the padding mode is not set */
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE,
OSSL_PKEY_RSA_PAD_MODE_PKCSV15, 0);
*p = OSSL_PARAM_construct_end();
}
#endif