include openssl bio include openssl err include openssl rsa include op

 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
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <stdio.h>
#include <stdint.h>
static const char key_pem[] = {
"-----BEGIN PRIVATE KEY-----\n"
"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDRgJ0gr3uHa/UB\n"
"0CQiPu2/YQByMLYvko3SbI/seMNvkq+iawYTVl62Q1SoZ9rZldNFlEvqaePiKYaJ\n"
"PB4ZXMcEqnonD5YATq92SpxkvPngu4t2FvPPS86C6FvKbnIxfgHbR64vWQTrEOj4\n"
"s48GFFJ+5QSzl6J7/HB0WpEHdGjTaMM5kp54DvO1VX3mbOEQyoq4itm2m+eyREic\n"
"Ye3KHGykGZJJTcrTCf+qJXOWecvNpuVIny9IGyWy1CDnGNX5Qj7ckA91/WVPm7d0\n"
"qsbrKWuDKxwp0dV8p64+wWGkDJcj87HQcroU3tCsDJN4iF8KpCGQ097UyA9xdXTw\n"
"kk5riQZdAgMBAAECggEABiLVt1Dcdd1wGiL+A/DC5uGQ8UdC9sa8l6atWng5BSoP\n"
"MdsfnO5hLMZxEtOj4c6VFwseZBnA3m1n7toPaZ/Bhn31wPIgaxbi5byOsxaj3PSx\n"
"Q36tmms2e7gRhC7S8mcl58XEMMfTMI1YvXwI2t06g1Py3M26qRX+NzI14DmFHnf7\n"
"+EdiI5vfms/Whu6r3ekCkjIcPo67icP3RdaKaXzW7r9TwBPcyvriREjxCq+/sav8\n"
"ZwCUc9Zo91eh11G7tT8QrTXdxZHuVRTOxWEdgSAo5ZZ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20] = {
0xf7,0x8b,0x70,0x4e, 0x49,0x17,0x6e,0xc7, 0x8c,0x53,0x26,0x5f,
0x62,0x6c,0xd6,0x9d, 0x5b,0xd0,0x7b,0x9a
};
void report_openssl_errors() {
uint32_t errcode;
while (0 != (errcode = ERR_get_error())) {
printf("! %s\n", ERR_error_string(errcode, 0));
}
}
int main() {
BIO* biomemfile = BIO_new_mem_buf(key_pem, sizeof(key_pem));
RSA* pkey = PEM_read_bio_RSAPrivateKey(biomemfile, 0, 0, 0);
if (!pkey) { report_openssl_errors(); }
uint8_t sig[256];
int sigsize;
RSA_sign(NID_sha1,
challenge, sizeof(challenge),
sig, &sigsize,
pkey);
if (sigsize == -1) { report_openssl_errors(); }
for(int i = 0; i < sigsize; ++i) {
printf("%02X ", sig[i]);
if (i % 16 == 7)
printf(" ");
if (i % 16 == 15)
printf("\n");
}
printf("\n");
}
// --->
// 21 B3 AD 47 7A 3F 83 A6 CA 00 E7 D9 89 DB 11 C0
// 90 6F 5D 27 9C 43 BB BD 5D 4A 02 4E 1C 11 F3 6C
// 3A DC D8 25 B1 8D 16 AC 64 8D 34 7B 9F 1F 77 AC
// 29 F6 4F B3 C8 A7 42 78 D3 1E 2C 9E E8 09 9F 58
// A4 65 0F 45 9F 33 CC B5 01 38 7E A7 D7 31 B9 C7
// 46 D4 82 8C 47 68 B1 F5 86 BD 1E 01 7D 03 3B 88
// 57 0C F6 80 FC 7D 47 88 24 D9 EF F8 19 2D B3 73
// 31 B0 9D 5E 8F 9F 77 9E 33 2B E7 EE AA 51 90 05
// 29 75 A2 88 08 25 7A 9E 31 9E 5B ED 28 14 3E 54
// EC 63 AB 08 3B 61 8C 60 93 83 74 63 1F CD E7 10
// E2 B5 1D EC 61 15 40 83 A2 1E E2 80 B8 90 B3 A2
// 7D 10 BB 8E D8 1D 42 DD F2 52 E2 08 C1 7C 27 FB
// 97 C0 DF BD 28 20 C7 B9 94 D3 71 85 2D 1E 3F 75
// FF 33 EE 8F 44 D7 1A A3 A7 37 2F BD 84 4B D4 2D
// C6 72 75 C7 F4 CE 56 3C 98 6F C4 F8 1B DD 37 13
// B0 E2 BA AA 69 75 25 4D 2B A4 3C A1 D2 C3 88 69
//
// -- that's correct!