v3_scts.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 "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. #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
  69. # define SCT_TIMESTAMP unsigned __int64
  70. #elif defined(__arch64__)
  71. # define SCT_TIMESTAMP unsigned long
  72. #else
  73. # define SCT_TIMESTAMP unsigned long long
  74. #endif
  75. #define n2l8(c,l) (l =((SCT_TIMESTAMP)(*((c)++)))<<56, \
  76. l|=((SCT_TIMESTAMP)(*((c)++)))<<48, \
  77. l|=((SCT_TIMESTAMP)(*((c)++)))<<40, \
  78. l|=((SCT_TIMESTAMP)(*((c)++)))<<32, \
  79. l|=((SCT_TIMESTAMP)(*((c)++)))<<24, \
  80. l|=((SCT_TIMESTAMP)(*((c)++)))<<16, \
  81. l|=((SCT_TIMESTAMP)(*((c)++)))<< 8, \
  82. l|=((SCT_TIMESTAMP)(*((c)++))))
  83. typedef struct SCT_st {
  84. /* The encoded SCT */
  85. unsigned char *sct;
  86. unsigned short sctlen;
  87. /*
  88. * Components of the SCT. "logid", "ext" and "sig" point to addresses
  89. * inside "sct".
  90. */
  91. unsigned char version;
  92. unsigned char *logid;
  93. unsigned short logidlen;
  94. SCT_TIMESTAMP timestamp;
  95. unsigned char *ext;
  96. unsigned short extlen;
  97. unsigned char hash_alg;
  98. unsigned char sig_alg;
  99. unsigned char *sig;
  100. unsigned short siglen;
  101. } SCT;
  102. DECLARE_STACK_OF(SCT)
  103. static void SCT_LIST_free(STACK_OF(SCT) *a);
  104. static STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a,
  105. const unsigned char **pp, long length);
  106. static int i2r_SCT_LIST(X509V3_EXT_METHOD *method, STACK_OF(SCT) *sct_list,
  107. BIO *out, int indent);
  108. const X509V3_EXT_METHOD v3_ct_scts[] = {
  109. {NID_ct_precert_scts, 0, NULL,
  110. 0, (X509V3_EXT_FREE)SCT_LIST_free,
  111. (X509V3_EXT_D2I)d2i_SCT_LIST, 0,
  112. 0, 0, 0, 0,
  113. (X509V3_EXT_I2R)i2r_SCT_LIST, 0,
  114. NULL},
  115. {NID_ct_cert_scts, 0, NULL,
  116. 0, (X509V3_EXT_FREE)SCT_LIST_free,
  117. (X509V3_EXT_D2I)d2i_SCT_LIST, 0,
  118. 0, 0, 0, 0,
  119. (X509V3_EXT_I2R)i2r_SCT_LIST, 0,
  120. NULL},
  121. };
  122. static void tls12_signature_print(BIO *out, const unsigned char hash_alg,
  123. const unsigned char sig_alg)
  124. {
  125. int nid = NID_undef;
  126. /* RFC6962 only permits two signature algorithms */
  127. if (hash_alg == TLSEXT_hash_sha256) {
  128. if (sig_alg == TLSEXT_signature_rsa)
  129. nid = NID_sha256WithRSAEncryption;
  130. else if (sig_alg == TLSEXT_signature_ecdsa)
  131. nid = NID_ecdsa_with_SHA256;
  132. }
  133. if (nid == NID_undef)
  134. BIO_printf(out, "%02X%02X", hash_alg, sig_alg);
  135. else
  136. BIO_printf(out, "%s", OBJ_nid2ln(nid));
  137. }
  138. static void timestamp_print(BIO *out, SCT_TIMESTAMP timestamp)
  139. {
  140. ASN1_GENERALIZEDTIME *gen;
  141. char genstr[20];
  142. gen = ASN1_GENERALIZEDTIME_new();
  143. ASN1_GENERALIZEDTIME_adj(gen, (time_t)0,
  144. (int)(timestamp / 86400000),
  145. (timestamp % 86400000) / 1000);
  146. /*
  147. * Note GeneralizedTime from ASN1_GENERALIZETIME_adj is always 15
  148. * characters long with a final Z. Update it with fractional seconds.
  149. */
  150. BIO_snprintf(genstr, sizeof(genstr), "%.14s.%03dZ",
  151. ASN1_STRING_data(gen), (unsigned int)(timestamp % 1000));
  152. ASN1_GENERALIZEDTIME_set_string(gen, genstr);
  153. ASN1_GENERALIZEDTIME_print(out, gen);
  154. ASN1_GENERALIZEDTIME_free(gen);
  155. }
  156. static void SCT_free(SCT *sct)
  157. {
  158. if (sct) {
  159. if (sct->sct)
  160. OPENSSL_free(sct->sct);
  161. OPENSSL_free(sct);
  162. }
  163. }
  164. static void SCT_LIST_free(STACK_OF(SCT) *a)
  165. {
  166. sk_SCT_pop_free(a, SCT_free);
  167. }
  168. static STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a,
  169. const unsigned char **pp, long length)
  170. {
  171. ASN1_OCTET_STRING *oct = NULL;
  172. STACK_OF(SCT) *sk = NULL;
  173. SCT *sct;
  174. unsigned char *p, *p2;
  175. unsigned short listlen, sctlen = 0, fieldlen;
  176. const unsigned char *q = *pp;
  177. if (d2i_ASN1_OCTET_STRING(&oct, &q, length) == NULL)
  178. return NULL;
  179. if (oct->length < 2)
  180. goto done;
  181. p = oct->data;
  182. n2s(p, listlen);
  183. if (listlen != oct->length - 2)
  184. goto done;
  185. if ((sk = sk_SCT_new_null()) == NULL)
  186. goto done;
  187. while (listlen > 0) {
  188. if (listlen < 2)
  189. goto err;
  190. n2s(p, sctlen);
  191. listlen -= 2;
  192. if ((sctlen < 1) || (sctlen > listlen))
  193. goto err;
  194. listlen -= sctlen;
  195. sct = OPENSSL_malloc(sizeof(SCT));
  196. if (!sct)
  197. goto err;
  198. if (!sk_SCT_push(sk, sct)) {
  199. OPENSSL_free(sct);
  200. goto err;
  201. }
  202. sct->sct = OPENSSL_malloc(sctlen);
  203. if (!sct->sct)
  204. goto err;
  205. memcpy(sct->sct, p, sctlen);
  206. sct->sctlen = sctlen;
  207. p += sctlen;
  208. p2 = sct->sct;
  209. sct->version = *p2++;
  210. if (sct->version == 0) { /* SCT v1 */
  211. /*-
  212. * Fixed-length header:
  213. * struct {
  214. * (1 byte) Version sct_version;
  215. * (32 bytes) LogID id;
  216. * (8 bytes) uint64 timestamp;
  217. * (2 bytes + ?) CtExtensions extensions;
  218. */
  219. if (sctlen < 43)
  220. goto err;
  221. sctlen -= 43;
  222. sct->logid = p2;
  223. sct->logidlen = 32;
  224. p2 += 32;
  225. n2l8(p2, sct->timestamp);
  226. n2s(p2, fieldlen);
  227. if (sctlen < fieldlen)
  228. goto err;
  229. sct->ext = p2;
  230. sct->extlen = fieldlen;
  231. p2 += fieldlen;
  232. sctlen -= fieldlen;
  233. /*-
  234. * digitally-signed struct header:
  235. * (1 byte) Hash algorithm
  236. * (1 byte) Signature algorithm
  237. * (2 bytes + ?) Signature
  238. */
  239. if (sctlen < 4)
  240. goto err;
  241. sctlen -= 4;
  242. sct->hash_alg = *p2++;
  243. sct->sig_alg = *p2++;
  244. n2s(p2, fieldlen);
  245. if (sctlen != fieldlen)
  246. goto err;
  247. sct->sig = p2;
  248. sct->siglen = fieldlen;
  249. }
  250. }
  251. done:
  252. ASN1_OCTET_STRING_free(oct);
  253. *pp = q;
  254. return sk;
  255. err:
  256. SCT_LIST_free(sk);
  257. sk = NULL;
  258. goto done;
  259. }
  260. static int i2r_SCT_LIST(X509V3_EXT_METHOD *method, STACK_OF(SCT) *sct_list,
  261. BIO *out, int indent)
  262. {
  263. SCT *sct;
  264. int i;
  265. for (i = 0; i < sk_SCT_num(sct_list);) {
  266. sct = sk_SCT_value(sct_list, i);
  267. BIO_printf(out, "%*sSigned Certificate Timestamp:", indent, "");
  268. BIO_printf(out, "\n%*sVersion : ", indent + 4, "");
  269. if (sct->version == 0) { /* SCT v1 */
  270. BIO_printf(out, "v1(0)");
  271. BIO_printf(out, "\n%*sLog ID : ", indent + 4, "");
  272. BIO_hex_string(out, indent + 16, 16, sct->logid, sct->logidlen);
  273. BIO_printf(out, "\n%*sTimestamp : ", indent + 4, "");
  274. timestamp_print(out, sct->timestamp);
  275. BIO_printf(out, "\n%*sExtensions: ", indent + 4, "");
  276. if (sct->extlen == 0)
  277. BIO_printf(out, "none");
  278. else
  279. BIO_hex_string(out, indent + 16, 16, sct->ext, sct->extlen);
  280. BIO_printf(out, "\n%*sSignature : ", indent + 4, "");
  281. tls12_signature_print(out, sct->hash_alg, sct->sig_alg);
  282. BIO_printf(out, "\n%*s ", indent + 4, "");
  283. BIO_hex_string(out, indent + 16, 16, sct->sig, sct->siglen);
  284. } else { /* Unknown version */
  285. BIO_printf(out, "unknown\n%*s", indent + 16, "");
  286. BIO_hex_string(out, indent + 16, 16, sct->sct, sct->sctlen);
  287. }
  288. if (++i < sk_SCT_num(sct_list))
  289. BIO_printf(out, "\n");
  290. }
  291. return 1;
  292. }