ct_lib.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Written by Rob Stradling (rob@comodo.com) and Stephen Henson
  3. * (steve@openssl.org) 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. #ifndef OPENSSL_NO_CT
  59. # include <limits.h>
  60. # include "internal/cryptlib.h"
  61. # include "../../ssl/ssl_locl.h"
  62. # include "internal/ct_int.h"
  63. SCT *SCT_new(void)
  64. {
  65. SCT *sct = OPENSSL_zalloc(sizeof(SCT));
  66. if (sct == NULL) {
  67. CTerr(CT_F_SCT_NEW, ERR_R_MALLOC_FAILURE);
  68. return NULL;
  69. }
  70. sct->entry_type = UNSET_ENTRY;
  71. sct->version = UNSET_VERSION;
  72. return sct;
  73. }
  74. void SCT_free(SCT *sct)
  75. {
  76. if (sct) {
  77. OPENSSL_free(sct->log_id);
  78. OPENSSL_free(sct->ext);
  79. OPENSSL_free(sct->sig);
  80. OPENSSL_free(sct->sct);
  81. OPENSSL_free(sct);
  82. }
  83. }
  84. int SCT_set_version(SCT *sct, sct_version_t version)
  85. {
  86. if (version != SCT_V1) {
  87. CTerr(CT_F_SCT_SET_VERSION, CT_R_UNSUPPORTED_VERSION);
  88. return 0;
  89. }
  90. sct->version = version;
  91. return 1;
  92. }
  93. int SCT_set_log_entry_type(SCT *sct, log_entry_type_t entry_type)
  94. {
  95. if (entry_type != X509_ENTRY && entry_type != PRECERT_ENTRY) {
  96. CTerr(CT_F_SCT_SET_LOG_ENTRY_TYPE, CT_R_UNSUPPORTED_ENTRY_TYPE);
  97. return 0;
  98. }
  99. sct->entry_type = entry_type;
  100. return 1;
  101. }
  102. int SCT_set0_log_id(SCT *sct, unsigned char *log_id, size_t log_id_len)
  103. {
  104. /* Currently only SHA-256 allowed so length must be SCT_V1_HASHLEN */
  105. if (log_id_len != SCT_V1_HASHLEN) {
  106. CTerr(CT_F_SCT_SET0_LOG_ID, CT_R_INVALID_LOG_ID_LENGTH);
  107. return 0;
  108. }
  109. OPENSSL_free(sct->log_id);
  110. sct->log_id = log_id;
  111. sct->log_id_len = log_id_len;
  112. return 1;
  113. }
  114. void SCT_set_timestamp(SCT *sct, uint64_t timestamp)
  115. {
  116. sct->timestamp = timestamp;
  117. }
  118. int SCT_set_signature_nid(SCT *sct, int nid)
  119. {
  120. switch (nid) {
  121. case NID_sha256WithRSAEncryption:
  122. sct->hash_alg = TLSEXT_hash_sha256;
  123. sct->sig_alg = TLSEXT_signature_rsa;
  124. return 1;
  125. case NID_ecdsa_with_SHA256:
  126. sct->hash_alg = TLSEXT_hash_sha256;
  127. sct->sig_alg = TLSEXT_signature_ecdsa;
  128. return 1;
  129. default:
  130. CTerr(CT_F_SCT_SET_SIGNATURE_NID, CT_R_UNRECOGNIZED_SIGNATURE_NID);
  131. return 0;
  132. }
  133. }
  134. void SCT_set0_extensions(SCT *sct, unsigned char *ext, size_t ext_len)
  135. {
  136. OPENSSL_free(sct->ext);
  137. sct->ext = ext;
  138. sct->ext_len = ext_len;
  139. }
  140. void SCT_set0_signature(SCT *sct, unsigned char *sig, size_t sig_len)
  141. {
  142. OPENSSL_free(sct->sig);
  143. sct->sig = sig;
  144. sct->sig_len = sig_len;
  145. }
  146. sct_version_t SCT_get_version(const SCT *sct)
  147. {
  148. return sct->version;
  149. }
  150. log_entry_type_t SCT_get_log_entry_type(const SCT *sct)
  151. {
  152. return sct->entry_type;
  153. }
  154. size_t SCT_get0_log_id(const SCT *sct, unsigned char **log_id)
  155. {
  156. *log_id = sct->log_id;
  157. return sct->log_id_len;
  158. }
  159. uint64_t SCT_get_timestamp(const SCT *sct)
  160. {
  161. return sct->timestamp;
  162. }
  163. int SCT_get_signature_nid(const SCT *sct)
  164. {
  165. if (sct->version == SCT_V1) {
  166. if (sct->hash_alg == TLSEXT_hash_sha256) {
  167. switch (sct->sig_alg) {
  168. case TLSEXT_signature_ecdsa:
  169. return NID_ecdsa_with_SHA256;
  170. case TLSEXT_signature_rsa:
  171. return NID_sha256WithRSAEncryption;
  172. default:
  173. return NID_undef;
  174. }
  175. }
  176. }
  177. return NID_undef;
  178. }
  179. size_t SCT_get0_extensions(const SCT *sct, unsigned char **ext)
  180. {
  181. *ext = sct->ext;
  182. return sct->ext_len;
  183. }
  184. size_t SCT_get0_signature(const SCT *sct, unsigned char **sig)
  185. {
  186. *sig = sct->sig;
  187. return sct->sig_len;
  188. }
  189. #endif