Blame


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