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