Blame


1 be288a59 2023-02-23 thomas /*
2 be288a59 2023-02-23 thomas * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 be288a59 2023-02-23 thomas *
4 be288a59 2023-02-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 be288a59 2023-02-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 be288a59 2023-02-23 thomas * copyright notice and this permission notice appear in all copies.
7 be288a59 2023-02-23 thomas *
8 be288a59 2023-02-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 be288a59 2023-02-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 be288a59 2023-02-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 be288a59 2023-02-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 be288a59 2023-02-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 be288a59 2023-02-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 be288a59 2023-02-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 be288a59 2023-02-23 thomas */
16 be288a59 2023-02-23 thomas
17 68069cf6 2023-03-08 thomas #include "got_compat.h"
18 68069cf6 2023-03-08 thomas
19 be288a59 2023-02-23 thomas #include <sys/types.h>
20 c8ae092d 2023-02-23 thomas #include <sys/queue.h>
21 c8ae092d 2023-02-23 thomas
22 be288a59 2023-02-23 thomas #include <errno.h>
23 be288a59 2023-02-23 thomas #include <stdio.h>
24 be288a59 2023-02-23 thomas #include <stdlib.h>
25 c8ae092d 2023-02-23 thomas #include <string.h>
26 be288a59 2023-02-23 thomas #include <limits.h>
27 be288a59 2023-02-23 thomas
28 c8ae092d 2023-02-23 thomas #include "got_object.h"
29 232c0ac1 2023-04-22 thomas #include "got_error.h"
30 c8ae092d 2023-02-23 thomas
31 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
32 be288a59 2023-02-23 thomas
33 232c0ac1 2023-04-22 thomas struct got_object_id *
34 232c0ac1 2023-04-22 thomas got_object_id_dup(struct got_object_id *id1)
35 232c0ac1 2023-04-22 thomas {
36 232c0ac1 2023-04-22 thomas struct got_object_id *id2;
37 232c0ac1 2023-04-22 thomas
38 232c0ac1 2023-04-22 thomas id2 = malloc(sizeof(*id2));
39 232c0ac1 2023-04-22 thomas if (id2 == NULL)
40 232c0ac1 2023-04-22 thomas return NULL;
41 232c0ac1 2023-04-22 thomas memcpy(id2, id1, sizeof(*id2));
42 232c0ac1 2023-04-22 thomas return id2;
43 232c0ac1 2023-04-22 thomas }
44 232c0ac1 2023-04-22 thomas
45 be288a59 2023-02-23 thomas int
46 232c0ac1 2023-04-22 thomas got_object_id_cmp(const struct got_object_id *id1,
47 232c0ac1 2023-04-22 thomas const struct got_object_id *id2)
48 232c0ac1 2023-04-22 thomas {
49 232c0ac1 2023-04-22 thomas return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
50 232c0ac1 2023-04-22 thomas }
51 232c0ac1 2023-04-22 thomas
52 232c0ac1 2023-04-22 thomas const struct got_error *
53 232c0ac1 2023-04-22 thomas got_object_id_str(char **outbuf, struct got_object_id *id)
54 232c0ac1 2023-04-22 thomas {
55 232c0ac1 2023-04-22 thomas static const size_t len = GOT_OBJECT_ID_HEX_MAXLEN;
56 232c0ac1 2023-04-22 thomas
57 232c0ac1 2023-04-22 thomas *outbuf = malloc(len);
58 232c0ac1 2023-04-22 thomas if (*outbuf == NULL)
59 232c0ac1 2023-04-22 thomas return got_error_from_errno("malloc");
60 232c0ac1 2023-04-22 thomas
61 232c0ac1 2023-04-22 thomas if (got_object_id_hex(id, *outbuf, len) == NULL) {
62 232c0ac1 2023-04-22 thomas free(*outbuf);
63 232c0ac1 2023-04-22 thomas *outbuf = NULL;
64 232c0ac1 2023-04-22 thomas return got_error(GOT_ERR_BAD_OBJ_ID_STR);
65 232c0ac1 2023-04-22 thomas }
66 232c0ac1 2023-04-22 thomas
67 232c0ac1 2023-04-22 thomas return NULL;
68 232c0ac1 2023-04-22 thomas }
69 232c0ac1 2023-04-22 thomas
70 232c0ac1 2023-04-22 thomas int
71 be288a59 2023-02-23 thomas got_parse_xdigit(uint8_t *val, const char *hex)
72 be288a59 2023-02-23 thomas {
73 be288a59 2023-02-23 thomas char *ep;
74 be288a59 2023-02-23 thomas long lval;
75 be288a59 2023-02-23 thomas
76 be288a59 2023-02-23 thomas errno = 0;
77 be288a59 2023-02-23 thomas lval = strtol(hex, &ep, 16);
78 be288a59 2023-02-23 thomas if (hex[0] == '\0' || *ep != '\0')
79 be288a59 2023-02-23 thomas return 0;
80 be288a59 2023-02-23 thomas if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
81 be288a59 2023-02-23 thomas return 0;
82 be288a59 2023-02-23 thomas
83 be288a59 2023-02-23 thomas *val = (uint8_t)lval;
84 be288a59 2023-02-23 thomas return 1;
85 be288a59 2023-02-23 thomas }
86 be288a59 2023-02-23 thomas
87 c8ae092d 2023-02-23 thomas static int
88 c8ae092d 2023-02-23 thomas parse_digest(uint8_t *digest, int len, const char *line)
89 be288a59 2023-02-23 thomas {
90 be288a59 2023-02-23 thomas uint8_t b = 0;
91 be288a59 2023-02-23 thomas char hex[3] = {'\0', '\0', '\0'};
92 be288a59 2023-02-23 thomas int i, j;
93 be288a59 2023-02-23 thomas
94 c8ae092d 2023-02-23 thomas for (i = 0; i < len; i++) {
95 be288a59 2023-02-23 thomas if (line[0] == '\0' || line[1] == '\0')
96 be288a59 2023-02-23 thomas return 0;
97 be288a59 2023-02-23 thomas for (j = 0; j < 2; j++) {
98 be288a59 2023-02-23 thomas hex[j] = *line;
99 be288a59 2023-02-23 thomas line++;
100 be288a59 2023-02-23 thomas }
101 be288a59 2023-02-23 thomas if (!got_parse_xdigit(&b, hex))
102 be288a59 2023-02-23 thomas return 0;
103 be288a59 2023-02-23 thomas digest[i] = b;
104 be288a59 2023-02-23 thomas }
105 be288a59 2023-02-23 thomas
106 be288a59 2023-02-23 thomas return 1;
107 be288a59 2023-02-23 thomas }
108 be288a59 2023-02-23 thomas
109 c8ae092d 2023-02-23 thomas static char *
110 c8ae092d 2023-02-23 thomas digest_to_str(const uint8_t *digest, int len, char *buf)
111 be288a59 2023-02-23 thomas {
112 6853f3b1 2023-02-24 thomas const char hex[] = "0123456789abcdef";
113 be288a59 2023-02-23 thomas char *p = buf;
114 be288a59 2023-02-23 thomas int i;
115 be288a59 2023-02-23 thomas
116 c8ae092d 2023-02-23 thomas for (i = 0; i < len; i++) {
117 6853f3b1 2023-02-24 thomas *p++ = hex[digest[i] >> 4];
118 6853f3b1 2023-02-24 thomas *p++ = hex[digest[i] & 0xf];
119 be288a59 2023-02-23 thomas }
120 6853f3b1 2023-02-24 thomas *p = '\0';
121 be288a59 2023-02-23 thomas
122 be288a59 2023-02-23 thomas return buf;
123 be288a59 2023-02-23 thomas }
124 c8ae092d 2023-02-23 thomas
125 c8ae092d 2023-02-23 thomas char *
126 c8ae092d 2023-02-23 thomas got_sha1_digest_to_str(const uint8_t *digest, char *buf, size_t size)
127 c8ae092d 2023-02-23 thomas {
128 c8ae092d 2023-02-23 thomas if (size < SHA1_DIGEST_STRING_LENGTH)
129 c8ae092d 2023-02-23 thomas return NULL;
130 c8ae092d 2023-02-23 thomas return digest_to_str(digest, SHA1_DIGEST_LENGTH, buf);
131 c8ae092d 2023-02-23 thomas }
132 c8ae092d 2023-02-23 thomas
133 c8ae092d 2023-02-23 thomas char *
134 c8ae092d 2023-02-23 thomas got_sha256_digest_to_str(const uint8_t *digest, char *buf, size_t size)
135 c8ae092d 2023-02-23 thomas {
136 c8ae092d 2023-02-23 thomas if (size < SHA256_DIGEST_STRING_LENGTH)
137 c8ae092d 2023-02-23 thomas return NULL;
138 c8ae092d 2023-02-23 thomas return digest_to_str(digest, SHA256_DIGEST_LENGTH, buf);
139 c8ae092d 2023-02-23 thomas }
140 c8ae092d 2023-02-23 thomas
141 c8ae092d 2023-02-23 thomas int
142 c8ae092d 2023-02-23 thomas got_parse_hash_digest(uint8_t *digest, const char *line,
143 c8ae092d 2023-02-23 thomas enum got_hash_algorithm algo)
144 c8ae092d 2023-02-23 thomas {
145 c8ae092d 2023-02-23 thomas switch (algo) {
146 c8ae092d 2023-02-23 thomas case GOT_HASH_SHA1:
147 c8ae092d 2023-02-23 thomas return parse_digest(digest, SHA1_DIGEST_LENGTH, line);
148 c8ae092d 2023-02-23 thomas case GOT_HASH_SHA256:
149 c8ae092d 2023-02-23 thomas return parse_digest(digest, SHA256_DIGEST_LENGTH, line);
150 c8ae092d 2023-02-23 thomas default:
151 c8ae092d 2023-02-23 thomas return 0;
152 c8ae092d 2023-02-23 thomas }
153 c8ae092d 2023-02-23 thomas }
154 c8ae092d 2023-02-23 thomas
155 232c0ac1 2023-04-22 thomas char *
156 232c0ac1 2023-04-22 thomas got_object_id_hex(struct got_object_id *id, char *buf, size_t len)
157 232c0ac1 2023-04-22 thomas {
158 232c0ac1 2023-04-22 thomas return got_sha1_digest_to_str(id->sha1, buf, len);
159 232c0ac1 2023-04-22 thomas }
160 232c0ac1 2023-04-22 thomas
161 c8ae092d 2023-02-23 thomas int
162 c8ae092d 2023-02-23 thomas got_parse_object_id(struct got_object_id *id, const char *line,
163 c8ae092d 2023-02-23 thomas enum got_hash_algorithm algo)
164 c8ae092d 2023-02-23 thomas {
165 c8ae092d 2023-02-23 thomas memset(id, 0, sizeof(*id));
166 c8ae092d 2023-02-23 thomas
167 c8ae092d 2023-02-23 thomas /* XXX: temporary until we grow got_object_id */
168 c8ae092d 2023-02-23 thomas if (algo != GOT_HASH_SHA1)
169 c8ae092d 2023-02-23 thomas return 0;
170 c8ae092d 2023-02-23 thomas
171 c8ae092d 2023-02-23 thomas return got_parse_hash_digest(id->sha1, line, algo);
172 c8ae092d 2023-02-23 thomas }
173 b16893ba 2023-02-24 thomas
174 b16893ba 2023-02-24 thomas void
175 b16893ba 2023-02-24 thomas got_hash_init(struct got_hash *hash, enum got_hash_algorithm algo)
176 b16893ba 2023-02-24 thomas {
177 b16893ba 2023-02-24 thomas memset(hash, 0, sizeof(*hash));
178 b16893ba 2023-02-24 thomas hash->algo = algo;
179 b16893ba 2023-02-24 thomas
180 b16893ba 2023-02-24 thomas if (algo == GOT_HASH_SHA1)
181 b16893ba 2023-02-24 thomas SHA1Init(&hash->sha1_ctx);
182 b16893ba 2023-02-24 thomas else if (algo == GOT_HASH_SHA256)
183 b16893ba 2023-02-24 thomas SHA256Init(&hash->sha256_ctx);
184 b16893ba 2023-02-24 thomas }
185 b16893ba 2023-02-24 thomas
186 b16893ba 2023-02-24 thomas void
187 b16893ba 2023-02-24 thomas got_hash_update(struct got_hash *hash, const void *data, size_t len)
188 b16893ba 2023-02-24 thomas {
189 b16893ba 2023-02-24 thomas if (hash->algo == GOT_HASH_SHA1)
190 b16893ba 2023-02-24 thomas SHA1Update(&hash->sha1_ctx, data, len);
191 b16893ba 2023-02-24 thomas else if (hash->algo == GOT_HASH_SHA256)
192 b16893ba 2023-02-24 thomas SHA256Update(&hash->sha256_ctx, data, len);
193 b16893ba 2023-02-24 thomas }
194 b16893ba 2023-02-24 thomas
195 b16893ba 2023-02-24 thomas void
196 b16893ba 2023-02-24 thomas got_hash_final(struct got_hash *hash, uint8_t *out)
197 b16893ba 2023-02-24 thomas {
198 b16893ba 2023-02-24 thomas if (hash->algo == GOT_HASH_SHA1)
199 b16893ba 2023-02-24 thomas SHA1Final(out, &hash->sha1_ctx);
200 b16893ba 2023-02-24 thomas else if (hash->algo == GOT_HASH_SHA256)
201 b16893ba 2023-02-24 thomas SHA256Final(out, &hash->sha256_ctx);
202 b16893ba 2023-02-24 thomas }
203 b16893ba 2023-02-24 thomas
204 b16893ba 2023-02-24 thomas void
205 b16893ba 2023-02-24 thomas got_hash_final_object_id(struct got_hash *hash, struct got_object_id *id)
206 b16893ba 2023-02-24 thomas {
207 b16893ba 2023-02-24 thomas memset(id, 0, sizeof(*id));
208 b16893ba 2023-02-24 thomas got_hash_final(hash, id->sha1);
209 b16893ba 2023-02-24 thomas }
210 b16893ba 2023-02-24 thomas
211 b16893ba 2023-02-24 thomas int
212 b16893ba 2023-02-24 thomas got_hash_cmp(enum got_hash_algorithm algo, uint8_t *b1, uint8_t *b2)
213 b16893ba 2023-02-24 thomas {
214 b16893ba 2023-02-24 thomas if (algo == GOT_HASH_SHA1)
215 b16893ba 2023-02-24 thomas return memcmp(b1, b2, SHA1_DIGEST_LENGTH);
216 b16893ba 2023-02-24 thomas else if (algo == GOT_HASH_SHA256)
217 b16893ba 2023-02-24 thomas return memcmp(b1, b2, SHA256_DIGEST_LENGTH);
218 b16893ba 2023-02-24 thomas return -1;
219 b16893ba 2023-02-24 thomas }