-
Notifications
You must be signed in to change notification settings - Fork 1
/
dn.c
49 lines (44 loc) · 1.24 KB
/
dn.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
#include <assert.h>
#include "x509cert.h"
#include "inner.h"
static size_t
encode_rdn(const struct x509cert_rdn *rdn, unsigned char *buf)
{
struct x509cert_item item = {X509CERT_ASN1_SET};
struct x509cert_item attr = {X509CERT_ASN1_SEQUENCE};
size_t len;
attr.len = x509cert_copy(rdn->oid, NULL) + x509cert_encode(&rdn->val, NULL);
item.len = x509cert_encode(&attr, NULL);
len = x509cert_encode(&item, NULL);
if (buf) {
unsigned char *pos = buf;
pos += x509cert_encode(&item, pos);
pos += x509cert_encode(&attr, pos);
pos += x509cert_copy(rdn->oid, pos);
pos += x509cert_encode(&rdn->val, pos);
assert(pos - buf == len);
}
return len;
}
size_t
x509cert_encode_dn(const struct x509cert_dn *dn, unsigned char *buf)
{
struct x509cert_item item = {X509CERT_ASN1_SEQUENCE};
size_t len;
for (size_t i = 0; i < dn->rdn_len; ++i)
item.len += encode_rdn(&dn->rdn[i], NULL);
len = x509cert_encode(&item, NULL);
if (buf) {
unsigned char *pos = buf;
pos += x509cert_encode(&item, buf);
for (size_t i = 0; i < dn->rdn_len; ++i)
pos += encode_rdn(&dn->rdn[i], pos);
assert(pos - buf == len);
}
return len;
}
size_t
x509cert_dn_encoder(const struct x509cert_item *item, unsigned char *buf)
{
return x509cert_encode_dn(item->val, buf);
}