ct_oct.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifdef OPENSSL_NO_CT
  10. # error "CT is disabled"
  11. #endif
  12. #include <limits.h>
  13. #include <string.h>
  14. #include <openssl/asn1.h>
  15. #include <openssl/buffer.h>
  16. #include <openssl/ct.h>
  17. #include <openssl/err.h>
  18. #include "ct_local.h"
  19. int o2i_SCT_signature(SCT *sct, const unsigned char **in, size_t len)
  20. {
  21. size_t siglen;
  22. size_t len_remaining = len;
  23. const unsigned char *p;
  24. if (sct->version != SCT_VERSION_V1) {
  25. ERR_raise(ERR_LIB_CT, CT_R_UNSUPPORTED_VERSION);
  26. return -1;
  27. }
  28. /*
  29. * digitally-signed struct header: (1 byte) Hash algorithm (1 byte)
  30. * Signature algorithm (2 bytes + ?) Signature
  31. *
  32. * This explicitly rejects empty signatures: they're invalid for
  33. * all supported algorithms.
  34. */
  35. if (len <= 4) {
  36. ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID_SIGNATURE);
  37. return -1;
  38. }
  39. p = *in;
  40. /* Get hash and signature algorithm */
  41. sct->hash_alg = *p++;
  42. sct->sig_alg = *p++;
  43. if (SCT_get_signature_nid(sct) == NID_undef) {
  44. ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID_SIGNATURE);
  45. return -1;
  46. }
  47. /* Retrieve signature and check it is consistent with the buffer length */
  48. n2s(p, siglen);
  49. len_remaining -= (p - *in);
  50. if (siglen > len_remaining) {
  51. ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID_SIGNATURE);
  52. return -1;
  53. }
  54. if (SCT_set1_signature(sct, p, siglen) != 1)
  55. return -1;
  56. len_remaining -= siglen;
  57. *in = p + siglen;
  58. return len - len_remaining;
  59. }
  60. SCT *o2i_SCT(SCT **psct, const unsigned char **in, size_t len)
  61. {
  62. SCT *sct = NULL;
  63. const unsigned char *p;
  64. if (len == 0 || len > MAX_SCT_SIZE) {
  65. ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID);
  66. goto err;
  67. }
  68. if ((sct = SCT_new()) == NULL)
  69. goto err;
  70. p = *in;
  71. sct->version = *p;
  72. if (sct->version == SCT_VERSION_V1) {
  73. int sig_len;
  74. size_t len2;
  75. /*-
  76. * Fixed-length header:
  77. * struct {
  78. * Version sct_version; (1 byte)
  79. * log_id id; (32 bytes)
  80. * uint64 timestamp; (8 bytes)
  81. * CtExtensions extensions; (2 bytes + ?)
  82. * }
  83. */
  84. if (len < 43) {
  85. ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID);
  86. goto err;
  87. }
  88. len -= 43;
  89. p++;
  90. sct->log_id = OPENSSL_memdup(p, CT_V1_HASHLEN);
  91. if (sct->log_id == NULL)
  92. goto err;
  93. sct->log_id_len = CT_V1_HASHLEN;
  94. p += CT_V1_HASHLEN;
  95. n2l8(p, sct->timestamp);
  96. n2s(p, len2);
  97. if (len < len2) {
  98. ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID);
  99. goto err;
  100. }
  101. if (len2 > 0) {
  102. sct->ext = OPENSSL_memdup(p, len2);
  103. if (sct->ext == NULL)
  104. goto err;
  105. }
  106. sct->ext_len = len2;
  107. p += len2;
  108. len -= len2;
  109. sig_len = o2i_SCT_signature(sct, &p, len);
  110. if (sig_len <= 0) {
  111. ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID);
  112. goto err;
  113. }
  114. len -= sig_len;
  115. *in = p + len;
  116. } else {
  117. /* If not V1 just cache encoding */
  118. sct->sct = OPENSSL_memdup(p, len);
  119. if (sct->sct == NULL)
  120. goto err;
  121. sct->sct_len = len;
  122. *in = p + len;
  123. }
  124. if (psct != NULL) {
  125. SCT_free(*psct);
  126. *psct = sct;
  127. }
  128. return sct;
  129. err:
  130. SCT_free(sct);
  131. return NULL;
  132. }
  133. int i2o_SCT_signature(const SCT *sct, unsigned char **out)
  134. {
  135. size_t len;
  136. unsigned char *p = NULL, *pstart = NULL;
  137. if (!SCT_signature_is_complete(sct)) {
  138. ERR_raise(ERR_LIB_CT, CT_R_SCT_INVALID_SIGNATURE);
  139. goto err;
  140. }
  141. if (sct->version != SCT_VERSION_V1) {
  142. ERR_raise(ERR_LIB_CT, CT_R_UNSUPPORTED_VERSION);
  143. goto err;
  144. }
  145. /*
  146. * (1 byte) Hash algorithm
  147. * (1 byte) Signature algorithm
  148. * (2 bytes + ?) Signature
  149. */
  150. len = 4 + sct->sig_len;
  151. if (out != NULL) {
  152. if (*out != NULL) {
  153. p = *out;
  154. *out += len;
  155. } else {
  156. pstart = p = OPENSSL_malloc(len);
  157. if (p == NULL)
  158. goto err;
  159. *out = p;
  160. }
  161. *p++ = sct->hash_alg;
  162. *p++ = sct->sig_alg;
  163. s2n(sct->sig_len, p);
  164. memcpy(p, sct->sig, sct->sig_len);
  165. }
  166. return len;
  167. err:
  168. OPENSSL_free(pstart);
  169. return -1;
  170. }
  171. int i2o_SCT(const SCT *sct, unsigned char **out)
  172. {
  173. size_t len;
  174. unsigned char *p = NULL, *pstart = NULL;
  175. if (!SCT_is_complete(sct)) {
  176. ERR_raise(ERR_LIB_CT, CT_R_SCT_NOT_SET);
  177. goto err;
  178. }
  179. /*
  180. * Fixed-length header: struct { (1 byte) Version sct_version; (32 bytes)
  181. * log_id id; (8 bytes) uint64 timestamp; (2 bytes + ?) CtExtensions
  182. * extensions; (1 byte) Hash algorithm (1 byte) Signature algorithm (2
  183. * bytes + ?) Signature
  184. */
  185. if (sct->version == SCT_VERSION_V1)
  186. len = 43 + sct->ext_len + 4 + sct->sig_len;
  187. else
  188. len = sct->sct_len;
  189. if (out == NULL)
  190. return len;
  191. if (*out != NULL) {
  192. p = *out;
  193. *out += len;
  194. } else {
  195. pstart = p = OPENSSL_malloc(len);
  196. if (p == NULL)
  197. goto err;
  198. *out = p;
  199. }
  200. if (sct->version == SCT_VERSION_V1) {
  201. *p++ = sct->version;
  202. memcpy(p, sct->log_id, CT_V1_HASHLEN);
  203. p += CT_V1_HASHLEN;
  204. l2n8(sct->timestamp, p);
  205. s2n(sct->ext_len, p);
  206. if (sct->ext_len > 0) {
  207. memcpy(p, sct->ext, sct->ext_len);
  208. p += sct->ext_len;
  209. }
  210. if (i2o_SCT_signature(sct, &p) <= 0)
  211. goto err;
  212. } else {
  213. memcpy(p, sct->sct, len);
  214. }
  215. return len;
  216. err:
  217. OPENSSL_free(pstart);
  218. return -1;
  219. }
  220. STACK_OF(SCT) *o2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
  221. size_t len)
  222. {
  223. STACK_OF(SCT) *sk = NULL;
  224. size_t list_len, sct_len;
  225. if (len < 2 || len > MAX_SCT_LIST_SIZE) {
  226. ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID);
  227. return NULL;
  228. }
  229. n2s(*pp, list_len);
  230. if (list_len != len - 2) {
  231. ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID);
  232. return NULL;
  233. }
  234. if (a == NULL || *a == NULL) {
  235. sk = sk_SCT_new_null();
  236. if (sk == NULL)
  237. return NULL;
  238. } else {
  239. SCT *sct;
  240. /* Use the given stack, but empty it first. */
  241. sk = *a;
  242. while ((sct = sk_SCT_pop(sk)) != NULL)
  243. SCT_free(sct);
  244. }
  245. while (list_len > 0) {
  246. SCT *sct;
  247. if (list_len < 2) {
  248. ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID);
  249. goto err;
  250. }
  251. n2s(*pp, sct_len);
  252. list_len -= 2;
  253. if (sct_len == 0 || sct_len > list_len) {
  254. ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID);
  255. goto err;
  256. }
  257. list_len -= sct_len;
  258. if ((sct = o2i_SCT(NULL, pp, sct_len)) == NULL)
  259. goto err;
  260. if (!sk_SCT_push(sk, sct)) {
  261. SCT_free(sct);
  262. goto err;
  263. }
  264. }
  265. if (a != NULL && *a == NULL)
  266. *a = sk;
  267. return sk;
  268. err:
  269. if (a == NULL || *a == NULL)
  270. SCT_LIST_free(sk);
  271. return NULL;
  272. }
  273. int i2o_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp)
  274. {
  275. int len, sct_len, i, is_pp_new = 0;
  276. size_t len2;
  277. unsigned char *p = NULL, *p2;
  278. if (pp != NULL) {
  279. if (*pp == NULL) {
  280. if ((len = i2o_SCT_LIST(a, NULL)) == -1) {
  281. ERR_raise(ERR_LIB_CT, CT_R_SCT_LIST_INVALID);
  282. return -1;
  283. }
  284. if ((*pp = OPENSSL_malloc(len)) == NULL)
  285. return -1;
  286. is_pp_new = 1;
  287. }
  288. p = *pp + 2;
  289. }
  290. len2 = 2;
  291. for (i = 0; i < sk_SCT_num(a); i++) {
  292. if (pp != NULL) {
  293. p2 = p;
  294. p += 2;
  295. if ((sct_len = i2o_SCT(sk_SCT_value(a, i), &p)) == -1)
  296. goto err;
  297. s2n(sct_len, p2);
  298. } else {
  299. if ((sct_len = i2o_SCT(sk_SCT_value(a, i), NULL)) == -1)
  300. goto err;
  301. }
  302. len2 += 2 + sct_len;
  303. }
  304. if (len2 > MAX_SCT_LIST_SIZE)
  305. goto err;
  306. if (pp != NULL) {
  307. p = *pp;
  308. s2n(len2 - 2, p);
  309. if (!is_pp_new)
  310. *pp += len2;
  311. }
  312. return len2;
  313. err:
  314. if (is_pp_new) {
  315. OPENSSL_free(*pp);
  316. *pp = NULL;
  317. }
  318. return -1;
  319. }
  320. STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
  321. long len)
  322. {
  323. ASN1_OCTET_STRING *oct = NULL;
  324. STACK_OF(SCT) *sk = NULL;
  325. const unsigned char *p;
  326. p = *pp;
  327. if (d2i_ASN1_OCTET_STRING(&oct, &p, len) == NULL)
  328. return NULL;
  329. p = oct->data;
  330. if ((sk = o2i_SCT_LIST(a, &p, oct->length)) != NULL)
  331. *pp += len;
  332. ASN1_OCTET_STRING_free(oct);
  333. return sk;
  334. }
  335. int i2d_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **out)
  336. {
  337. ASN1_OCTET_STRING oct;
  338. int len;
  339. oct.data = NULL;
  340. if ((oct.length = i2o_SCT_LIST(a, &oct.data)) == -1)
  341. return -1;
  342. len = i2d_ASN1_OCTET_STRING(&oct, out);
  343. OPENSSL_free(oct.data);
  344. return len;
  345. }