-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfio_ssl.c
More file actions
91 lines (71 loc) · 1.83 KB
/
fio_ssl.c
File metadata and controls
91 lines (71 loc) · 1.83 KB
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
// SPDX-License-Identifier: BSD-3-Clause
/*
* Copyright (c) 2022, Foundries.io Ltd.
* Author: Jorge Ramirez-Ortiz <jorge@foundries.io>
*/
#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <stdio.h>
#include <string.h>
#include "fio_ssl.h"
int fio_ssl_get_cert_info(struct fio_cert_info *info, uint8_t *der, size_t len)
{
X509 *x = NULL;
if (!info || !der || len == 0)
return -1;
x = d2i_X509(NULL, (const unsigned char **)&der, len);
if (!x)
return -1;
/* Get subject */
info->subject.len = i2d_X509_NAME(X509_get_subject_name(x), NULL);
if (info->subject.len < 0 ||
info->subject.len > sizeof(info->subject.data))
return -1;
i2d_X509_NAME(X509_get_subject_name(x),
(unsigned char **)&info->subject.data);
/* Get issuer */
info->issuer.len = i2d_X509_NAME(X509_get_issuer_name(x), NULL);
if (info->issuer.len < 0 ||
info->issuer.len > sizeof(info->issuer.data))
return -1;
i2d_X509_NAME(X509_get_issuer_name(x),
(unsigned char **)&info->issuer.data);
/* Get serial */
info->serial.len = i2d_ASN1_INTEGER(X509_get_serialNumber(x), NULL);
if (info->serial.len < 0 ||
info->serial.len > sizeof(info->serial.data))
return -1;
i2d_ASN1_INTEGER(X509_get_serialNumber(x),
(unsigned char **)&info->serial.data);
return 0;
}
int fio_ssl_print_cert(uint8_t *der, size_t len)
{
X509 *x = NULL;
BIO *b = NULL;
x = d2i_X509(NULL, (const unsigned char **)&der, len);
if (!x)
return -1;
b = BIO_new_fp(stdout, 0);
if (!b)
return -1;
X509_print_ex(b, x,
XN_FLAG_SEP_CPLUS_SPC | ASN1_STRFLGS_UTF8_CONVERT, 0);
BIO_free_all(b);
return 0;
}
int fio_ssl_bn_hex2dec(char **str)
{
BIGNUM *bn = BN_new();
if (!bn)
return -1;
if (!BN_hex2bn(&bn, *str)) {
BN_free(bn);
return -1;
}
free(*str);
*str = BN_bn2dec(bn);
BN_free(bn);
return 0;
}