OpenDNSSEC-signer 2.1.12
nsec3params.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 NLNet Labs. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
32#include "status.h"
33#include "log.h"
34#include "util.h"
35#include "signer/backup.h"
36#include "signer/nsec3params.h"
37#include "signer/signconf.h"
38
39#include <ctype.h>
40#include <ldns/ldns.h>
41#include <stdlib.h>
42#include <string.h>
43
44static const char* nsec3_str = "nsec3";
45
46
51ods_status
52nsec3params_create_salt(const char* salt_str, uint8_t* salt_len,
53 uint8_t** salt)
54{
55 uint8_t c;
56 uint8_t* salt_tmp;
57
58 if (!salt_str) {
59 *salt_len = 0;
60 *salt = NULL;
61 return ODS_STATUS_OK;
62 }
63 *salt_len = (uint8_t) strlen(salt_str);
64 if (*salt_len == 1 && salt_str[0] == '-') {
65 *salt_len = 0;
66 *salt = NULL;
67 return ODS_STATUS_OK;
68 } else if (*salt_len % 2 != 0) {
69 ods_log_error("[%s] invalid salt %s", nsec3_str, salt_str);
70 *salt = NULL;
71 return ODS_STATUS_ERR;
72 }
73 /* construct salt data */
74 salt_tmp = (uint8_t*) calloc(*salt_len / 2, sizeof(uint8_t));
75 if (!salt_tmp) {
76 ods_log_error("[%s] construct salt data for %s failed", nsec3_str,
77 salt_str);
78 *salt = NULL;
79 return ODS_STATUS_MALLOC_ERR;
80 }
81 for (c = 0; c < *salt_len; c += 2) {
82 if (isxdigit((int) salt_str[c]) && isxdigit((int) salt_str[c+1])) {
83 salt_tmp[c/2] = (uint8_t) ldns_hexdigit_to_int(salt_str[c]) * 16 +
84 ldns_hexdigit_to_int(salt_str[c+1]);
85 } else {
86 ods_log_error("[%s] invalid salt %s", nsec3_str, salt_str);
87 free((void*)salt_tmp);
88 *salt = NULL;
89 return ODS_STATUS_ERR;
90 }
91 }
92 *salt_len = *salt_len / 2; /* update length */
93 *salt = salt_tmp;
94 return ODS_STATUS_OK;
95}
96
97
103nsec3params_create(void* sc, uint8_t algo, uint8_t flags, uint16_t iter,
104 const char* salt)
105{
106 nsec3params_type* nsec3params = NULL;
107 uint8_t salt_len; /* calculate salt len */
108 uint8_t* salt_data; /* calculate salt data */
109
110 if (!sc) {
111 return NULL;
112 }
113 CHECKALLOC(nsec3params = (nsec3params_type*) malloc(sizeof(nsec3params_type)));
114 if (!nsec3params) {
115 ods_log_error("[%s] unable to create: allocator_alloc() failed",
116 nsec3_str);
117 return NULL;
118 }
119 nsec3params->sc = sc;
120 nsec3params->algorithm = algo;
121 nsec3params->flags = flags;
122 nsec3params->iterations = iter;
123 /* construct the salt from the string */
124 if (nsec3params_create_salt(salt, &salt_len, &salt_data) != 0) {
125 ods_log_error("[%s] unable to create: create salt failed", nsec3_str);
126 free(nsec3params);
127 return NULL;
128 }
129 nsec3params->salt_len = salt_len;
130 nsec3params->salt_data = salt_data;
131 nsec3params->rr = NULL;
132 return nsec3params;
133}
134
135
140void
141nsec3params_backup(FILE* fd, uint8_t algo, uint8_t flags,
142 uint16_t iter, const char* salt, ldns_rr* rr, const char* version)
143{
144 if (!fd) {
145 return;
146 }
147 fprintf(fd, ";;Nsec3parameters: salt %s algorithm %u optout %u "
148 "iterations %u\n", ((salt&&strlen(salt))?salt:"-"), (unsigned) algo,
149 (unsigned) flags, (unsigned) iter);
150 if (strcmp(version, ODS_SE_FILE_MAGIC_V2) == 0) {
151 if (rr) {
152 (void)util_rr_print(fd, rr);
153 }
154 fprintf(fd, ";;Nsec3done\n");
155 fprintf(fd, ";;\n");
156 }
157}
158
159
164void
166{
167 if (!nsec3params) {
168 return;
169 }
170 free(nsec3params->salt_data);
171 free(nsec3params);
172}
nsec3params_type * nsec3params_create(void *sc, uint8_t algo, uint8_t flags, uint16_t iter, const char *salt)
Definition: nsec3params.c:103
ods_status nsec3params_create_salt(const char *salt_str, uint8_t *salt_len, uint8_t **salt)
Definition: nsec3params.c:52
void nsec3params_backup(FILE *fd, uint8_t algo, uint8_t flags, uint16_t iter, const char *salt, ldns_rr *rr, const char *version)
Definition: nsec3params.c:141
void nsec3params_cleanup(nsec3params_type *nsec3params)
Definition: nsec3params.c:165
signconf_type * sc
Definition: nsec3params.h:48
uint8_t * salt_data
Definition: nsec3params.h:53
uint16_t iterations
Definition: nsec3params.h:51