Skip to content

Commit 6b96032

Browse files
author
Maciej Soltysiak
committed
Add option to specify certificate expiration (in days)
1 parent 2e6d756 commit 6b96032

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

cert.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "dnscrypt.h"
22

33
struct SignedCert *
4-
cert_build_cert(const uint8_t *crypt_publickey)
4+
cert_build_cert(const uint8_t *crypt_publickey, int cert_file_expire_days)
55
{
66
struct SignedCert *signed_cert = malloc(sizeof(struct SignedCert));
77
if (!signed_cert)
@@ -19,7 +19,10 @@ cert_build_cert(const uint8_t *crypt_publickey)
1919
sizeof(signed_cert->magic_query));
2020
memcpy(signed_cert->serial, "0001", 4);
2121
uint32_t ts_begin = (uint32_t)time(NULL);
22-
uint32_t ts_end = ts_begin + 365 * 24 * 3600;
22+
uint32_t ts_end = ts_begin + cert_file_expire_days * 24 * 3600;
23+
if (cert_file_expire_days <= 0) {
24+
ts_begin = ts_end;
25+
}
2326
ts_begin = htonl(ts_begin);
2427
ts_end = htonl(ts_end);
2528
memcpy(signed_cert->ts_begin, &ts_begin, 4);

cert.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#define CERT_MINOR_VERSION 0
88
#define CERT_MAGIC_HEADER "7PYqwfzt"
99

10+
#define CERT_FILE_EXPIRE_DAYS 365
11+
1012
struct SignedCert {
1113
uint8_t magic_cert[4];
1214
uint8_t version_major[2];
@@ -21,7 +23,7 @@ struct SignedCert {
2123
uint8_t end[64];
2224
};
2325

24-
struct SignedCert *cert_build_cert(const uint8_t *crypt_publickey);
26+
struct SignedCert *cert_build_cert(const uint8_t *crypt_publickey, int cert_file_expire_days);
2527
int cert_sign(struct SignedCert *signed_cert,
2628
const uint8_t *provider_secretkey);
2729
int cert_unsign(struct SignedCert *signed_cert,

main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ main(int argc, const char **argv)
188188
int gen_provider_keypair = 0;
189189
int gen_crypt_keypair = 0;
190190
int gen_cert_file = 0;
191+
int cert_file_expire_days = CERT_FILE_EXPIRE_DAYS;
191192
int verbose = 0;
192193
struct argparse argparse;
193194
struct argparse_option options[] = {
@@ -219,6 +220,7 @@ main(int argc, const char **argv)
219220
"provider secret key file"),
220221
OPT_BOOLEAN(0, "gen-cert-file", &gen_cert_file,
221222
"generate pre-signed certificate"),
223+
OPT_INTEGER(0, "cert-file-expire-days", &cert_file_expire_days),
222224
OPT_STRING(0, "provider-name", &c.provider_name, "provider name"),
223225
OPT_STRING(0, "provider-cert-file", &c.provider_cert_file,
224226
"use this to self-serve cert file"),
@@ -323,7 +325,7 @@ main(int argc, const char **argv)
323325
exit(1);
324326
}
325327
logger(LOG_NOTICE, "Generating pre-signed certificate.");
326-
struct SignedCert *signed_cert = cert_build_cert(c.crypt_publickey);
328+
struct SignedCert *signed_cert = cert_build_cert(c.crypt_publickey, cert_file_expire_days);
327329
if (!signed_cert || cert_sign(signed_cert, c.provider_secretkey) != 0) {
328330
logger(LOG_NOTICE, "Failed.");
329331
exit(1);

0 commit comments

Comments
 (0)