v3_scts.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* v3_scts.c */
  2. /*
  3. * Written by Rob Stradling (rob@comodo.com) for the OpenSSL project 2014.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include "internal/cryptlib.h"
  60. #include <openssl/asn1.h>
  61. #include <openssl/x509v3.h>
  62. /* Signature and hash algorithms from RFC 5246 */
  63. #define TLSEXT_hash_sha256 4
  64. #define TLSEXT_signature_rsa 1
  65. #define TLSEXT_signature_ecdsa 3
  66. #define n2s(c,s) ((s=(((unsigned int)(c[0]))<< 8)| \
  67. (((unsigned int)(c[1])) )),c+=2)
  68. #define n2l8(c,l) (l =((uint64_t)(*((c)++)))<<56, \
  69. l|=((uint64_t)(*((c)++)))<<48, \
  70. l|=((uint64_t)(*((c)++)))<<40, \
  71. l|=((uint64_t)(*((c)++)))<<32, \
  72. l|=((uint64_t)(*((c)++)))<<24, \
  73. l|=((uint64_t)(*((c)++)))<<16, \
  74. l|=((uint64_t)(*((c)++)))<< 8, \
  75. l|=((uint64_t)(*((c)++))))
  76. typedef struct SCT_st {
  77. /* The encoded SCT */
  78. unsigned char *sct;
  79. unsigned short sctlen;
  80. /*
  81. * Components of the SCT. "logid", "ext" and "sig" point to addresses
  82. * inside "sct".
  83. */
  84. unsigned char version;
  85. unsigned char *logid;
  86. unsigned short logidlen;
  87. uint64_t timestamp;
  88. unsigned char *ext;
  89. unsigned short extlen;
  90. unsigned char hash_alg;
  91. unsigned char sig_alg;
  92. unsigned char *sig;
  93. unsigned short siglen;
  94. } SCT;
  95. DECLARE_STACK_OF(SCT)
  96. static void SCT_LIST_free(STACK_OF(SCT) *a);
  97. static STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a,
  98. const unsigned char **pp, long length);
  99. static int i2r_SCT_LIST(X509V3_EXT_METHOD *method, STACK_OF(SCT) *sct_list,
  100. BIO *out, int indent);
  101. const X509V3_EXT_METHOD v3_ct_scts[] = {
  102. {NID_ct_precert_scts, 0, NULL,
  103. 0, (X509V3_EXT_FREE)SCT_LIST_free,
  104. (X509V3_EXT_D2I)d2i_SCT_LIST, 0,
  105. 0, 0, 0, 0,
  106. (X509V3_EXT_I2R)i2r_SCT_LIST, 0,
  107. NULL},
  108. {NID_ct_cert_scts, 0, NULL,
  109. 0, (X509V3_EXT_FREE)SCT_LIST_free,
  110. (X509V3_EXT_D2I)d2i_SCT_LIST, 0,
  111. 0, 0, 0, 0,
  112. (X509V3_EXT_I2R)i2r_SCT_LIST, 0,
  113. NULL},
  114. };
  115. static void tls12_signature_print(BIO *out, const unsigned char hash_alg,
  116. const unsigned char sig_alg)
  117. {
  118. int nid = NID_undef;
  119. /* RFC6962 only permits two signature algorithms */
  120. if (hash_alg == TLSEXT_hash_sha256) {
  121. if (sig_alg == TLSEXT_signature_rsa)
  122. nid = NID_sha256WithRSAEncryption;
  123. else if (sig_alg == TLSEXT_signature_ecdsa)
  124. nid = NID_ecdsa_with_SHA256;
  125. }
  126. if (nid == NID_undef)
  127. BIO_printf(out, "%02X%02X", hash_alg, sig_alg);
  128. else
  129. BIO_printf(out, "%s", OBJ_nid2ln(nid));
  130. }
  131. static void timestamp_print(BIO *out, uint64_t timestamp)
  132. {
  133. ASN1_GENERALIZEDTIME *gen;
  134. char genstr[20];
  135. gen = ASN1_GENERALIZEDTIME_new();
  136. ASN1_GENERALIZEDTIME_adj(gen, (time_t)0,
  137. (int)(timestamp / 86400000),
  138. (timestamp % 86400000) / 1000);
  139. /*
  140. * Note GeneralizedTime from ASN1_GENERALIZETIME_adj is always 15
  141. * characters long with a final Z. Update it with fractional seconds.
  142. */
  143. BIO_snprintf(genstr, sizeof(genstr), "%.14s.%03dZ",
  144. ASN1_STRING_data(gen), (unsigned int)(timestamp % 1000));
  145. ASN1_GENERALIZEDTIME_set_string(gen, genstr);
  146. ASN1_GENERALIZEDTIME_print(out, gen);
  147. ASN1_GENERALIZEDTIME_free(gen);
  148. }
  149. static void SCT_free(SCT *sct)
  150. {
  151. if (!sct)
  152. return;
  153. OPENSSL_free(sct->sct);
  154. OPENSSL_free(sct);
  155. }
  156. static void SCT_LIST_free(STACK_OF(SCT) *a)
  157. {
  158. sk_SCT_pop_free(a, SCT_free);
  159. }
  160. static STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a,
  161. const unsigned char **pp, long length)
  162. {
  163. ASN1_OCTET_STRING *oct = NULL;
  164. STACK_OF(SCT) *sk = NULL;
  165. SCT *sct;
  166. unsigned char *p, *p2;
  167. unsigned short listlen, sctlen = 0, fieldlen;
  168. if (d2i_ASN1_OCTET_STRING(&oct, pp, length) == NULL)
  169. return NULL;
  170. if (oct->length < 2)
  171. goto done;
  172. p = oct->data;
  173. n2s(p, listlen);
  174. if (listlen != oct->length - 2)
  175. goto done;
  176. if ((sk = sk_SCT_new_null()) == NULL)
  177. goto done;
  178. while (listlen > 0) {
  179. if (listlen < 2)
  180. goto err;
  181. n2s(p, sctlen);
  182. listlen -= 2;
  183. if ((sctlen < 1) || (sctlen > listlen))
  184. goto err;
  185. listlen -= sctlen;
  186. sct = OPENSSL_malloc(sizeof(*sct));
  187. if (!sct)
  188. goto err;
  189. if (!sk_SCT_push(sk, sct)) {
  190. OPENSSL_free(sct);
  191. goto err;
  192. }
  193. sct->sct = OPENSSL_malloc(sctlen);
  194. if (!sct->sct)
  195. goto err;
  196. memcpy(sct->sct, p, sctlen);
  197. sct->sctlen = sctlen;
  198. p += sctlen;
  199. p2 = sct->sct;
  200. sct->version = *p2++;
  201. if (sct->version == 0) { /* SCT v1 */
  202. /*-
  203. * Fixed-length header:
  204. * struct {
  205. * (1 byte) Version sct_version;
  206. * (32 bytes) LogID id;
  207. * (8 bytes) uint64 timestamp;
  208. * (2 bytes + ?) CtExtensions extensions;
  209. */
  210. if (sctlen < 43)
  211. goto err;
  212. sctlen -= 43;
  213. sct->logid = p2;
  214. sct->logidlen = 32;
  215. p2 += 32;
  216. n2l8(p2, sct->timestamp);
  217. n2s(p2, fieldlen);
  218. if (sctlen < fieldlen)
  219. goto err;
  220. sct->ext = p2;
  221. sct->extlen = fieldlen;
  222. p2 += fieldlen;
  223. sctlen -= fieldlen;
  224. /*-
  225. * digitally-signed struct header:
  226. * (1 byte) Hash algorithm
  227. * (1 byte) Signature algorithm
  228. * (2 bytes + ?) Signature
  229. */
  230. if (sctlen < 4)
  231. goto err;
  232. sctlen -= 4;
  233. sct->hash_alg = *p2++;
  234. sct->sig_alg = *p2++;
  235. n2s(p2, fieldlen);
  236. if (sctlen != fieldlen)
  237. goto err;
  238. sct->sig = p2;
  239. sct->siglen = fieldlen;
  240. }
  241. }
  242. done:
  243. ASN1_OCTET_STRING_free(oct);
  244. return sk;
  245. err:
  246. SCT_LIST_free(sk);
  247. sk = NULL;
  248. goto done;
  249. }
  250. static int i2r_SCT_LIST(X509V3_EXT_METHOD *method, STACK_OF(SCT) *sct_list,
  251. BIO *out, int indent)
  252. {
  253. SCT *sct;
  254. int i;
  255. for (i = 0; i < sk_SCT_num(sct_list);) {
  256. sct = sk_SCT_value(sct_list, i);
  257. BIO_printf(out, "%*sSigned Certificate Timestamp:", indent, "");
  258. BIO_printf(out, "\n%*sVersion : ", indent + 4, "");
  259. if (sct->version == 0) { /* SCT v1 */
  260. BIO_printf(out, "v1(0)");
  261. BIO_printf(out, "\n%*sLog ID : ", indent + 4, "");
  262. BIO_hex_string(out, indent + 16, 16, sct->logid, sct->logidlen);
  263. BIO_printf(out, "\n%*sTimestamp : ", indent + 4, "");
  264. timestamp_print(out, sct->timestamp);
  265. BIO_printf(out, "\n%*sExtensions: ", indent + 4, "");
  266. if (sct->extlen == 0)
  267. BIO_printf(out, "none");
  268. else
  269. BIO_hex_string(out, indent + 16, 16, sct->ext, sct->extlen);
  270. BIO_printf(out, "\n%*sSignature : ", indent + 4, "");
  271. tls12_signature_print(out, sct->hash_alg, sct->sig_alg);
  272. BIO_printf(out, "\n%*s ", indent + 4, "");
  273. BIO_hex_string(out, indent + 16, 16, sct->sig, sct->siglen);
  274. } else { /* Unknown version */
  275. BIO_printf(out, "unknown\n%*s", indent + 16, "");
  276. BIO_hex_string(out, indent + 16, 16, sct->sct, sct->sctlen);
  277. }
  278. if (++i < sk_SCT_num(sct_list))
  279. BIO_printf(out, "\n");
  280. }
  281. return 1;
  282. }