summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ts
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2015-03-08 16:48:49 +0000
committercvs2svn <admin@example.com>2015-03-08 16:48:49 +0000
commitdecf84ba5550c1656a7fdb51b5b81969590c3f03 (patch)
tree44872802e872bdfd60730fa9cf01d9d5751251c1 /src/lib/libcrypto/ts
parent7a8f138352aa4eb7b65ac4b1a5fe7630fbee1427 (diff)
downloadopenbsd-libressl-v2.1.5.tar.gz
openbsd-libressl-v2.1.5.tar.bz2
openbsd-libressl-v2.1.5.zip
This commit was manufactured by cvs2git to create branch 'OPENBSD_5_7'.libressl-v2.1.5
Diffstat (limited to 'src/lib/libcrypto/ts')
-rw-r--r--src/lib/libcrypto/ts/ts.h839
-rw-r--r--src/lib/libcrypto/ts/ts_asn1.c605
-rw-r--r--src/lib/libcrypto/ts/ts_conf.c532
-rw-r--r--src/lib/libcrypto/ts/ts_err.c179
-rw-r--r--src/lib/libcrypto/ts/ts_lib.c150
-rw-r--r--src/lib/libcrypto/ts/ts_req_print.c104
-rw-r--r--src/lib/libcrypto/ts/ts_req_utils.c255
-rw-r--r--src/lib/libcrypto/ts/ts_rsp_print.c301
-rw-r--r--src/lib/libcrypto/ts/ts_rsp_sign.c1023
-rw-r--r--src/lib/libcrypto/ts/ts_rsp_utils.c436
-rw-r--r--src/lib/libcrypto/ts/ts_rsp_verify.c734
-rw-r--r--src/lib/libcrypto/ts/ts_verify_ctx.c166
12 files changed, 0 insertions, 5324 deletions
diff --git a/src/lib/libcrypto/ts/ts.h b/src/lib/libcrypto/ts/ts.h
deleted file mode 100644
index 4457d30dd7..0000000000
--- a/src/lib/libcrypto/ts/ts.h
+++ /dev/null
@@ -1,839 +0,0 @@
1/* $OpenBSD: ts.h,v 1.7 2014/07/10 22:45:58 jsing Exp $ */
2/* Written by Zoltan Glozik (zglozik@opentsa.org) for the OpenSSL
3 * project 2002, 2003, 2004.
4 */
5/* ====================================================================
6 * Copyright (c) 2006 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 HEADER_TS_H
60#define HEADER_TS_H
61
62#include <openssl/opensslconf.h>
63
64#ifndef OPENSSL_NO_BUFFER
65#include <openssl/buffer.h>
66#endif
67#ifndef OPENSSL_NO_EVP
68#include <openssl/evp.h>
69#endif
70#ifndef OPENSSL_NO_BIO
71#include <openssl/bio.h>
72#endif
73#include <openssl/stack.h>
74#include <openssl/asn1.h>
75#include <openssl/safestack.h>
76
77#ifndef OPENSSL_NO_RSA
78#include <openssl/rsa.h>
79#endif
80
81#ifndef OPENSSL_NO_DSA
82#include <openssl/dsa.h>
83#endif
84
85#ifndef OPENSSL_NO_DH
86#include <openssl/dh.h>
87#endif
88
89#ifdef __cplusplus
90extern "C" {
91#endif
92
93#include <openssl/x509.h>
94#include <openssl/x509v3.h>
95
96/*
97MessageImprint ::= SEQUENCE {
98 hashAlgorithm AlgorithmIdentifier,
99 hashedMessage OCTET STRING }
100*/
101
102typedef struct TS_msg_imprint_st {
103 X509_ALGOR *hash_algo;
104 ASN1_OCTET_STRING *hashed_msg;
105} TS_MSG_IMPRINT;
106
107/*
108TimeStampReq ::= SEQUENCE {
109 version INTEGER { v1(1) },
110 messageImprint MessageImprint,
111 --a hash algorithm OID and the hash value of the data to be
112 --time-stamped
113 reqPolicy TSAPolicyId OPTIONAL,
114 nonce INTEGER OPTIONAL,
115 certReq BOOLEAN DEFAULT FALSE,
116 extensions [0] IMPLICIT Extensions OPTIONAL }
117*/
118
119typedef struct TS_req_st {
120 ASN1_INTEGER *version;
121 TS_MSG_IMPRINT *msg_imprint;
122 ASN1_OBJECT *policy_id; /* OPTIONAL */
123 ASN1_INTEGER *nonce; /* OPTIONAL */
124 ASN1_BOOLEAN cert_req; /* DEFAULT FALSE */
125 STACK_OF(X509_EXTENSION) *extensions; /* [0] OPTIONAL */
126} TS_REQ;
127
128/*
129Accuracy ::= SEQUENCE {
130 seconds INTEGER OPTIONAL,
131 millis [0] INTEGER (1..999) OPTIONAL,
132 micros [1] INTEGER (1..999) OPTIONAL }
133*/
134
135typedef struct TS_accuracy_st {
136 ASN1_INTEGER *seconds;
137 ASN1_INTEGER *millis;
138 ASN1_INTEGER *micros;
139} TS_ACCURACY;
140
141/*
142TSTInfo ::= SEQUENCE {
143 version INTEGER { v1(1) },
144 policy TSAPolicyId,
145 messageImprint MessageImprint,
146 -- MUST have the same value as the similar field in
147 -- TimeStampReq
148 serialNumber INTEGER,
149 -- Time-Stamping users MUST be ready to accommodate integers
150 -- up to 160 bits.
151 genTime GeneralizedTime,
152 accuracy Accuracy OPTIONAL,
153 ordering BOOLEAN DEFAULT FALSE,
154 nonce INTEGER OPTIONAL,
155 -- MUST be present if the similar field was present
156 -- in TimeStampReq. In that case it MUST have the same value.
157 tsa [0] GeneralName OPTIONAL,
158 extensions [1] IMPLICIT Extensions OPTIONAL }
159*/
160
161typedef struct TS_tst_info_st {
162 ASN1_INTEGER *version;
163 ASN1_OBJECT *policy_id;
164 TS_MSG_IMPRINT *msg_imprint;
165 ASN1_INTEGER *serial;
166 ASN1_GENERALIZEDTIME *time;
167 TS_ACCURACY *accuracy;
168 ASN1_BOOLEAN ordering;
169 ASN1_INTEGER *nonce;
170 GENERAL_NAME *tsa;
171 STACK_OF(X509_EXTENSION) *extensions;
172} TS_TST_INFO;
173
174/*
175PKIStatusInfo ::= SEQUENCE {
176 status PKIStatus,
177 statusString PKIFreeText OPTIONAL,
178 failInfo PKIFailureInfo OPTIONAL }
179
180From RFC 1510 - section 3.1.1:
181PKIFreeText ::= SEQUENCE SIZE (1..MAX) OF UTF8String
182 -- text encoded as UTF-8 String (note: each UTF8String SHOULD
183 -- include an RFC 1766 language tag to indicate the language
184 -- of the contained text)
185*/
186
187/* Possible values for status. See ts_resp_print.c && ts_resp_verify.c. */
188
189#define TS_STATUS_GRANTED 0
190#define TS_STATUS_GRANTED_WITH_MODS 1
191#define TS_STATUS_REJECTION 2
192#define TS_STATUS_WAITING 3
193#define TS_STATUS_REVOCATION_WARNING 4
194#define TS_STATUS_REVOCATION_NOTIFICATION 5
195
196/* Possible values for failure_info. See ts_resp_print.c && ts_resp_verify.c */
197
198#define TS_INFO_BAD_ALG 0
199#define TS_INFO_BAD_REQUEST 2
200#define TS_INFO_BAD_DATA_FORMAT 5
201#define TS_INFO_TIME_NOT_AVAILABLE 14
202#define TS_INFO_UNACCEPTED_POLICY 15
203#define TS_INFO_UNACCEPTED_EXTENSION 16
204#define TS_INFO_ADD_INFO_NOT_AVAILABLE 17
205#define TS_INFO_SYSTEM_FAILURE 25
206
207typedef struct TS_status_info_st {
208 ASN1_INTEGER *status;
209 STACK_OF(ASN1_UTF8STRING) *text;
210 ASN1_BIT_STRING *failure_info;
211} TS_STATUS_INFO;
212
213DECLARE_STACK_OF(ASN1_UTF8STRING)
214DECLARE_ASN1_SET_OF(ASN1_UTF8STRING)
215
216/*
217TimeStampResp ::= SEQUENCE {
218 status PKIStatusInfo,
219 timeStampToken TimeStampToken OPTIONAL }
220*/
221
222typedef struct TS_resp_st {
223 TS_STATUS_INFO *status_info;
224 PKCS7 *token;
225 TS_TST_INFO *tst_info;
226} TS_RESP;
227
228/* The structure below would belong to the ESS component. */
229
230/*
231IssuerSerial ::= SEQUENCE {
232 issuer GeneralNames,
233 serialNumber CertificateSerialNumber
234 }
235*/
236
237typedef struct ESS_issuer_serial {
238 STACK_OF(GENERAL_NAME) *issuer;
239 ASN1_INTEGER *serial;
240} ESS_ISSUER_SERIAL;
241
242/*
243ESSCertID ::= SEQUENCE {
244 certHash Hash,
245 issuerSerial IssuerSerial OPTIONAL
246}
247*/
248
249typedef struct ESS_cert_id {
250 ASN1_OCTET_STRING *hash; /* Always SHA-1 digest. */
251 ESS_ISSUER_SERIAL *issuer_serial;
252} ESS_CERT_ID;
253
254DECLARE_STACK_OF(ESS_CERT_ID)
255DECLARE_ASN1_SET_OF(ESS_CERT_ID)
256
257/*
258SigningCertificate ::= SEQUENCE {
259 certs SEQUENCE OF ESSCertID,
260 policies SEQUENCE OF PolicyInformation OPTIONAL
261}
262*/
263
264typedef struct ESS_signing_cert {
265 STACK_OF(ESS_CERT_ID) *cert_ids;
266 STACK_OF(POLICYINFO) *policy_info;
267} ESS_SIGNING_CERT;
268
269
270TS_REQ *TS_REQ_new(void);
271void TS_REQ_free(TS_REQ *a);
272int i2d_TS_REQ(const TS_REQ *a, unsigned char **pp);
273TS_REQ *d2i_TS_REQ(TS_REQ **a, const unsigned char **pp, long length);
274
275TS_REQ *TS_REQ_dup(TS_REQ *a);
276
277TS_REQ *d2i_TS_REQ_fp(FILE *fp, TS_REQ **a);
278int i2d_TS_REQ_fp(FILE *fp, TS_REQ *a);
279TS_REQ *d2i_TS_REQ_bio(BIO *fp, TS_REQ **a);
280int i2d_TS_REQ_bio(BIO *fp, TS_REQ *a);
281
282TS_MSG_IMPRINT *TS_MSG_IMPRINT_new(void);
283void TS_MSG_IMPRINT_free(TS_MSG_IMPRINT *a);
284int i2d_TS_MSG_IMPRINT(const TS_MSG_IMPRINT *a, unsigned char **pp);
285TS_MSG_IMPRINT *d2i_TS_MSG_IMPRINT(TS_MSG_IMPRINT **a,
286 const unsigned char **pp, long length);
287
288TS_MSG_IMPRINT *TS_MSG_IMPRINT_dup(TS_MSG_IMPRINT *a);
289
290TS_MSG_IMPRINT *d2i_TS_MSG_IMPRINT_fp(FILE *fp, TS_MSG_IMPRINT **a);
291int i2d_TS_MSG_IMPRINT_fp(FILE *fp, TS_MSG_IMPRINT *a);
292TS_MSG_IMPRINT *d2i_TS_MSG_IMPRINT_bio(BIO *fp, TS_MSG_IMPRINT **a);
293int i2d_TS_MSG_IMPRINT_bio(BIO *fp, TS_MSG_IMPRINT *a);
294
295TS_RESP *TS_RESP_new(void);
296void TS_RESP_free(TS_RESP *a);
297int i2d_TS_RESP(const TS_RESP *a, unsigned char **pp);
298TS_RESP *d2i_TS_RESP(TS_RESP **a, const unsigned char **pp, long length);
299TS_TST_INFO *PKCS7_to_TS_TST_INFO(PKCS7 *token);
300TS_RESP *TS_RESP_dup(TS_RESP *a);
301
302TS_RESP *d2i_TS_RESP_fp(FILE *fp, TS_RESP **a);
303int i2d_TS_RESP_fp(FILE *fp, TS_RESP *a);
304TS_RESP *d2i_TS_RESP_bio(BIO *fp, TS_RESP **a);
305int i2d_TS_RESP_bio(BIO *fp, TS_RESP *a);
306
307TS_STATUS_INFO *TS_STATUS_INFO_new(void);
308void TS_STATUS_INFO_free(TS_STATUS_INFO *a);
309int i2d_TS_STATUS_INFO(const TS_STATUS_INFO *a, unsigned char **pp);
310TS_STATUS_INFO *d2i_TS_STATUS_INFO(TS_STATUS_INFO **a,
311 const unsigned char **pp, long length);
312TS_STATUS_INFO *TS_STATUS_INFO_dup(TS_STATUS_INFO *a);
313
314TS_TST_INFO *TS_TST_INFO_new(void);
315void TS_TST_INFO_free(TS_TST_INFO *a);
316int i2d_TS_TST_INFO(const TS_TST_INFO *a, unsigned char **pp);
317TS_TST_INFO *d2i_TS_TST_INFO(TS_TST_INFO **a, const unsigned char **pp,
318 long length);
319TS_TST_INFO *TS_TST_INFO_dup(TS_TST_INFO *a);
320
321TS_TST_INFO *d2i_TS_TST_INFO_fp(FILE *fp, TS_TST_INFO **a);
322int i2d_TS_TST_INFO_fp(FILE *fp, TS_TST_INFO *a);
323TS_TST_INFO *d2i_TS_TST_INFO_bio(BIO *fp, TS_TST_INFO **a);
324int i2d_TS_TST_INFO_bio(BIO *fp, TS_TST_INFO *a);
325
326TS_ACCURACY *TS_ACCURACY_new(void);
327void TS_ACCURACY_free(TS_ACCURACY *a);
328int i2d_TS_ACCURACY(const TS_ACCURACY *a, unsigned char **pp);
329TS_ACCURACY *d2i_TS_ACCURACY(TS_ACCURACY **a, const unsigned char **pp,
330 long length);
331TS_ACCURACY *TS_ACCURACY_dup(TS_ACCURACY *a);
332
333ESS_ISSUER_SERIAL *ESS_ISSUER_SERIAL_new(void);
334void ESS_ISSUER_SERIAL_free(ESS_ISSUER_SERIAL *a);
335int i2d_ESS_ISSUER_SERIAL(const ESS_ISSUER_SERIAL *a,
336 unsigned char **pp);
337ESS_ISSUER_SERIAL *d2i_ESS_ISSUER_SERIAL(ESS_ISSUER_SERIAL **a,
338 const unsigned char **pp, long length);
339ESS_ISSUER_SERIAL *ESS_ISSUER_SERIAL_dup(ESS_ISSUER_SERIAL *a);
340
341ESS_CERT_ID *ESS_CERT_ID_new(void);
342void ESS_CERT_ID_free(ESS_CERT_ID *a);
343int i2d_ESS_CERT_ID(const ESS_CERT_ID *a, unsigned char **pp);
344ESS_CERT_ID *d2i_ESS_CERT_ID(ESS_CERT_ID **a, const unsigned char **pp,
345 long length);
346ESS_CERT_ID *ESS_CERT_ID_dup(ESS_CERT_ID *a);
347
348ESS_SIGNING_CERT *ESS_SIGNING_CERT_new(void);
349void ESS_SIGNING_CERT_free(ESS_SIGNING_CERT *a);
350int i2d_ESS_SIGNING_CERT(const ESS_SIGNING_CERT *a,
351 unsigned char **pp);
352ESS_SIGNING_CERT *d2i_ESS_SIGNING_CERT(ESS_SIGNING_CERT **a,
353 const unsigned char **pp, long length);
354ESS_SIGNING_CERT *ESS_SIGNING_CERT_dup(ESS_SIGNING_CERT *a);
355
356void ERR_load_TS_strings(void);
357
358int TS_REQ_set_version(TS_REQ *a, long version);
359long TS_REQ_get_version(const TS_REQ *a);
360
361int TS_REQ_set_msg_imprint(TS_REQ *a, TS_MSG_IMPRINT *msg_imprint);
362TS_MSG_IMPRINT *TS_REQ_get_msg_imprint(TS_REQ *a);
363
364int TS_MSG_IMPRINT_set_algo(TS_MSG_IMPRINT *a, X509_ALGOR *alg);
365X509_ALGOR *TS_MSG_IMPRINT_get_algo(TS_MSG_IMPRINT *a);
366
367int TS_MSG_IMPRINT_set_msg(TS_MSG_IMPRINT *a, unsigned char *d, int len);
368ASN1_OCTET_STRING *TS_MSG_IMPRINT_get_msg(TS_MSG_IMPRINT *a);
369
370int TS_REQ_set_policy_id(TS_REQ *a, ASN1_OBJECT *policy);
371ASN1_OBJECT *TS_REQ_get_policy_id(TS_REQ *a);
372
373int TS_REQ_set_nonce(TS_REQ *a, const ASN1_INTEGER *nonce);
374const ASN1_INTEGER *TS_REQ_get_nonce(const TS_REQ *a);
375
376int TS_REQ_set_cert_req(TS_REQ *a, int cert_req);
377int TS_REQ_get_cert_req(const TS_REQ *a);
378
379STACK_OF(X509_EXTENSION) *TS_REQ_get_exts(TS_REQ *a);
380void TS_REQ_ext_free(TS_REQ *a);
381int TS_REQ_get_ext_count(TS_REQ *a);
382int TS_REQ_get_ext_by_NID(TS_REQ *a, int nid, int lastpos);
383int TS_REQ_get_ext_by_OBJ(TS_REQ *a, ASN1_OBJECT *obj, int lastpos);
384int TS_REQ_get_ext_by_critical(TS_REQ *a, int crit, int lastpos);
385X509_EXTENSION *TS_REQ_get_ext(TS_REQ *a, int loc);
386X509_EXTENSION *TS_REQ_delete_ext(TS_REQ *a, int loc);
387int TS_REQ_add_ext(TS_REQ *a, X509_EXTENSION *ex, int loc);
388void *TS_REQ_get_ext_d2i(TS_REQ *a, int nid, int *crit, int *idx);
389
390/* Function declarations for TS_REQ defined in ts/ts_req_print.c */
391
392int TS_REQ_print_bio(BIO *bio, TS_REQ *a);
393
394/* Function declarations for TS_RESP defined in ts/ts_resp_utils.c */
395
396int TS_RESP_set_status_info(TS_RESP *a, TS_STATUS_INFO *info);
397TS_STATUS_INFO *TS_RESP_get_status_info(TS_RESP *a);
398
399/* Caller loses ownership of PKCS7 and TS_TST_INFO objects. */
400void TS_RESP_set_tst_info(TS_RESP *a, PKCS7 *p7, TS_TST_INFO *tst_info);
401PKCS7 *TS_RESP_get_token(TS_RESP *a);
402TS_TST_INFO *TS_RESP_get_tst_info(TS_RESP *a);
403
404int TS_TST_INFO_set_version(TS_TST_INFO *a, long version);
405long TS_TST_INFO_get_version(const TS_TST_INFO *a);
406
407int TS_TST_INFO_set_policy_id(TS_TST_INFO *a, ASN1_OBJECT *policy_id);
408ASN1_OBJECT *TS_TST_INFO_get_policy_id(TS_TST_INFO *a);
409
410int TS_TST_INFO_set_msg_imprint(TS_TST_INFO *a, TS_MSG_IMPRINT *msg_imprint);
411TS_MSG_IMPRINT *TS_TST_INFO_get_msg_imprint(TS_TST_INFO *a);
412
413int TS_TST_INFO_set_serial(TS_TST_INFO *a, const ASN1_INTEGER *serial);
414const ASN1_INTEGER *TS_TST_INFO_get_serial(const TS_TST_INFO *a);
415
416int TS_TST_INFO_set_time(TS_TST_INFO *a, const ASN1_GENERALIZEDTIME *gtime);
417const ASN1_GENERALIZEDTIME *TS_TST_INFO_get_time(const TS_TST_INFO *a);
418
419int TS_TST_INFO_set_accuracy(TS_TST_INFO *a, TS_ACCURACY *accuracy);
420TS_ACCURACY *TS_TST_INFO_get_accuracy(TS_TST_INFO *a);
421
422int TS_ACCURACY_set_seconds(TS_ACCURACY *a, const ASN1_INTEGER *seconds);
423const ASN1_INTEGER *TS_ACCURACY_get_seconds(const TS_ACCURACY *a);
424
425int TS_ACCURACY_set_millis(TS_ACCURACY *a, const ASN1_INTEGER *millis);
426const ASN1_INTEGER *TS_ACCURACY_get_millis(const TS_ACCURACY *a);
427
428int TS_ACCURACY_set_micros(TS_ACCURACY *a, const ASN1_INTEGER *micros);
429const ASN1_INTEGER *TS_ACCURACY_get_micros(const TS_ACCURACY *a);
430
431int TS_TST_INFO_set_ordering(TS_TST_INFO *a, int ordering);
432int TS_TST_INFO_get_ordering(const TS_TST_INFO *a);
433
434int TS_TST_INFO_set_nonce(TS_TST_INFO *a, const ASN1_INTEGER *nonce);
435const ASN1_INTEGER *TS_TST_INFO_get_nonce(const TS_TST_INFO *a);
436
437int TS_TST_INFO_set_tsa(TS_TST_INFO *a, GENERAL_NAME *tsa);
438GENERAL_NAME *TS_TST_INFO_get_tsa(TS_TST_INFO *a);
439
440STACK_OF(X509_EXTENSION) *TS_TST_INFO_get_exts(TS_TST_INFO *a);
441void TS_TST_INFO_ext_free(TS_TST_INFO *a);
442int TS_TST_INFO_get_ext_count(TS_TST_INFO *a);
443int TS_TST_INFO_get_ext_by_NID(TS_TST_INFO *a, int nid, int lastpos);
444int TS_TST_INFO_get_ext_by_OBJ(TS_TST_INFO *a, ASN1_OBJECT *obj, int lastpos);
445int TS_TST_INFO_get_ext_by_critical(TS_TST_INFO *a, int crit, int lastpos);
446X509_EXTENSION *TS_TST_INFO_get_ext(TS_TST_INFO *a, int loc);
447X509_EXTENSION *TS_TST_INFO_delete_ext(TS_TST_INFO *a, int loc);
448int TS_TST_INFO_add_ext(TS_TST_INFO *a, X509_EXTENSION *ex, int loc);
449void *TS_TST_INFO_get_ext_d2i(TS_TST_INFO *a, int nid, int *crit, int *idx);
450
451/* Declarations related to response generation, defined in ts/ts_resp_sign.c. */
452
453/* Optional flags for response generation. */
454
455/* Don't include the TSA name in response. */
456#define TS_TSA_NAME 0x01
457
458/* Set ordering to true in response. */
459#define TS_ORDERING 0x02
460
461/*
462 * Include the signer certificate and the other specified certificates in
463 * the ESS signing certificate attribute beside the PKCS7 signed data.
464 * Only the signer certificates is included by default.
465 */
466#define TS_ESS_CERT_ID_CHAIN 0x04
467
468/* Forward declaration. */
469struct TS_resp_ctx;
470
471/* This must return a unique number less than 160 bits long. */
472typedef ASN1_INTEGER *(*TS_serial_cb)(struct TS_resp_ctx *, void *);
473
474/* This must return the seconds and microseconds since Jan 1, 1970 in
475 the sec and usec variables allocated by the caller.
476 Return non-zero for success and zero for failure. */
477typedef int (*TS_time_cb)(struct TS_resp_ctx *, void *, time_t *sec, long *usec);
478
479/* This must process the given extension.
480 * It can modify the TS_TST_INFO object of the context.
481 * Return values: !0 (processed), 0 (error, it must set the
482 * status info/failure info of the response).
483 */
484typedef int (*TS_extension_cb)(struct TS_resp_ctx *, X509_EXTENSION *, void *);
485
486typedef struct TS_resp_ctx {
487 X509 *signer_cert;
488 EVP_PKEY *signer_key;
489 STACK_OF(X509) *certs; /* Certs to include in signed data. */
490 STACK_OF(ASN1_OBJECT) *policies; /* Acceptable policies. */
491 ASN1_OBJECT *default_policy; /* It may appear in policies, too. */
492 STACK_OF(EVP_MD) *mds; /* Acceptable message digests. */
493 ASN1_INTEGER *seconds; /* accuracy, 0 means not specified. */
494 ASN1_INTEGER *millis; /* accuracy, 0 means not specified. */
495 ASN1_INTEGER *micros; /* accuracy, 0 means not specified. */
496 unsigned clock_precision_digits; /* fraction of seconds in
497 time stamp token. */
498 unsigned flags; /* Optional info, see values above. */
499
500 /* Callback functions. */
501 TS_serial_cb serial_cb;
502 void *serial_cb_data; /* User data for serial_cb. */
503
504 TS_time_cb time_cb;
505 void *time_cb_data; /* User data for time_cb. */
506
507 TS_extension_cb extension_cb;
508 void *extension_cb_data; /* User data for extension_cb. */
509
510 /* These members are used only while creating the response. */
511 TS_REQ *request;
512 TS_RESP *response;
513 TS_TST_INFO *tst_info;
514} TS_RESP_CTX;
515
516DECLARE_STACK_OF(EVP_MD)
517DECLARE_ASN1_SET_OF(EVP_MD)
518
519/* Creates a response context that can be used for generating responses. */
520TS_RESP_CTX *TS_RESP_CTX_new(void);
521void TS_RESP_CTX_free(TS_RESP_CTX *ctx);
522
523/* This parameter must be set. */
524int TS_RESP_CTX_set_signer_cert(TS_RESP_CTX *ctx, X509 *signer);
525
526/* This parameter must be set. */
527int TS_RESP_CTX_set_signer_key(TS_RESP_CTX *ctx, EVP_PKEY *key);
528
529/* This parameter must be set. */
530int TS_RESP_CTX_set_def_policy(TS_RESP_CTX *ctx, ASN1_OBJECT *def_policy);
531
532/* No additional certs are included in the response by default. */
533int TS_RESP_CTX_set_certs(TS_RESP_CTX *ctx, STACK_OF(X509) *certs);
534
535/* Adds a new acceptable policy, only the default policy
536 is accepted by default. */
537int TS_RESP_CTX_add_policy(TS_RESP_CTX *ctx, ASN1_OBJECT *policy);
538
539/* Adds a new acceptable message digest. Note that no message digests
540 are accepted by default. The md argument is shared with the caller. */
541int TS_RESP_CTX_add_md(TS_RESP_CTX *ctx, const EVP_MD *md);
542
543/* Accuracy is not included by default. */
544int TS_RESP_CTX_set_accuracy(TS_RESP_CTX *ctx,
545 int secs, int millis, int micros);
546
547/* Clock precision digits, i.e. the number of decimal digits:
548 '0' means sec, '3' msec, '6' usec, and so on. Default is 0. */
549int TS_RESP_CTX_set_clock_precision_digits(TS_RESP_CTX *ctx,
550 unsigned clock_precision_digits);
551/* At most we accept usec precision. */
552#define TS_MAX_CLOCK_PRECISION_DIGITS 6
553
554/* No flags are set by default. */
555void TS_RESP_CTX_add_flags(TS_RESP_CTX *ctx, int flags);
556
557/* Default callback always returns a constant. */
558void TS_RESP_CTX_set_serial_cb(TS_RESP_CTX *ctx, TS_serial_cb cb, void *data);
559
560/* Default callback rejects all extensions. The extension callback is called
561 * when the TS_TST_INFO object is already set up and not signed yet. */
562/* FIXME: extension handling is not tested yet. */
563void TS_RESP_CTX_set_extension_cb(TS_RESP_CTX *ctx,
564 TS_extension_cb cb, void *data);
565
566/* The following methods can be used in the callbacks. */
567int TS_RESP_CTX_set_status_info(TS_RESP_CTX *ctx,
568 int status, const char *text);
569
570/* Sets the status info only if it is still TS_STATUS_GRANTED. */
571int TS_RESP_CTX_set_status_info_cond(TS_RESP_CTX *ctx,
572 int status, const char *text);
573
574int TS_RESP_CTX_add_failure_info(TS_RESP_CTX *ctx, int failure);
575
576/* The get methods below can be used in the extension callback. */
577TS_REQ *TS_RESP_CTX_get_request(TS_RESP_CTX *ctx);
578
579TS_TST_INFO *TS_RESP_CTX_get_tst_info(TS_RESP_CTX *ctx);
580
581/*
582 * Creates the signed TS_TST_INFO and puts it in TS_RESP.
583 * In case of errors it sets the status info properly.
584 * Returns NULL only in case of memory allocation/fatal error.
585 */
586TS_RESP *TS_RESP_create_response(TS_RESP_CTX *ctx, BIO *req_bio);
587
588/*
589 * Declarations related to response verification,
590 * they are defined in ts/ts_resp_verify.c.
591 */
592
593int TS_RESP_verify_signature(PKCS7 *token, STACK_OF(X509) *certs,
594 X509_STORE *store, X509 **signer_out);
595
596/* Context structure for the generic verify method. */
597
598/* Verify the signer's certificate and the signature of the response. */
599#define TS_VFY_SIGNATURE (1u << 0)
600/* Verify the version number of the response. */
601#define TS_VFY_VERSION (1u << 1)
602/* Verify if the policy supplied by the user matches the policy of the TSA. */
603#define TS_VFY_POLICY (1u << 2)
604/* Verify the message imprint provided by the user. This flag should not be
605 specified with TS_VFY_DATA. */
606#define TS_VFY_IMPRINT (1u << 3)
607/* Verify the message imprint computed by the verify method from the user
608 provided data and the MD algorithm of the response. This flag should not be
609 specified with TS_VFY_IMPRINT. */
610#define TS_VFY_DATA (1u << 4)
611/* Verify the nonce value. */
612#define TS_VFY_NONCE (1u << 5)
613/* Verify if the TSA name field matches the signer certificate. */
614#define TS_VFY_SIGNER (1u << 6)
615/* Verify if the TSA name field equals to the user provided name. */
616#define TS_VFY_TSA_NAME (1u << 7)
617
618/* You can use the following convenience constants. */
619#define TS_VFY_ALL_IMPRINT (TS_VFY_SIGNATURE \
620 | TS_VFY_VERSION \
621 | TS_VFY_POLICY \
622 | TS_VFY_IMPRINT \
623 | TS_VFY_NONCE \
624 | TS_VFY_SIGNER \
625 | TS_VFY_TSA_NAME)
626#define TS_VFY_ALL_DATA (TS_VFY_SIGNATURE \
627 | TS_VFY_VERSION \
628 | TS_VFY_POLICY \
629 | TS_VFY_DATA \
630 | TS_VFY_NONCE \
631 | TS_VFY_SIGNER \
632 | TS_VFY_TSA_NAME)
633
634typedef struct TS_verify_ctx {
635 /* Set this to the union of TS_VFY_... flags you want to carry out. */
636 unsigned flags;
637
638 /* Must be set only with TS_VFY_SIGNATURE. certs is optional. */
639 X509_STORE *store;
640 STACK_OF(X509) *certs;
641
642 /* Must be set only with TS_VFY_POLICY. */
643 ASN1_OBJECT *policy;
644
645 /* Must be set only with TS_VFY_IMPRINT. If md_alg is NULL,
646 the algorithm from the response is used. */
647 X509_ALGOR *md_alg;
648 unsigned char *imprint;
649 unsigned imprint_len;
650
651 /* Must be set only with TS_VFY_DATA. */
652 BIO *data;
653
654 /* Must be set only with TS_VFY_TSA_NAME. */
655 ASN1_INTEGER *nonce;
656
657 /* Must be set only with TS_VFY_TSA_NAME. */
658 GENERAL_NAME *tsa_name;
659} TS_VERIFY_CTX;
660
661int TS_RESP_verify_response(TS_VERIFY_CTX *ctx, TS_RESP *response);
662int TS_RESP_verify_token(TS_VERIFY_CTX *ctx, PKCS7 *token);
663
664/*
665 * Declarations related to response verification context,
666 * they are defined in ts/ts_verify_ctx.c.
667 */
668
669/* Set all fields to zero. */
670TS_VERIFY_CTX *TS_VERIFY_CTX_new(void);
671void TS_VERIFY_CTX_init(TS_VERIFY_CTX *ctx);
672void TS_VERIFY_CTX_free(TS_VERIFY_CTX *ctx);
673void TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX *ctx);
674
675/*
676 * If ctx is NULL, it allocates and returns a new object, otherwise
677 * it returns ctx. It initialises all the members as follows:
678 * flags = TS_VFY_ALL_IMPRINT & ~(TS_VFY_TSA_NAME | TS_VFY_SIGNATURE)
679 * certs = NULL
680 * store = NULL
681 * policy = policy from the request or NULL if absent (in this case
682 * TS_VFY_POLICY is cleared from flags as well)
683 * md_alg = MD algorithm from request
684 * imprint, imprint_len = imprint from request
685 * data = NULL
686 * nonce, nonce_len = nonce from the request or NULL if absent (in this case
687 * TS_VFY_NONCE is cleared from flags as well)
688 * tsa_name = NULL
689 * Important: after calling this method TS_VFY_SIGNATURE should be added!
690 */
691TS_VERIFY_CTX *TS_REQ_to_TS_VERIFY_CTX(TS_REQ *req, TS_VERIFY_CTX *ctx);
692
693/* Function declarations for TS_RESP defined in ts/ts_resp_print.c */
694
695int TS_RESP_print_bio(BIO *bio, TS_RESP *a);
696int TS_STATUS_INFO_print_bio(BIO *bio, TS_STATUS_INFO *a);
697int TS_TST_INFO_print_bio(BIO *bio, TS_TST_INFO *a);
698
699/* Common utility functions defined in ts/ts_lib.c */
700
701int TS_ASN1_INTEGER_print_bio(BIO *bio, const ASN1_INTEGER *num);
702int TS_OBJ_print_bio(BIO *bio, const ASN1_OBJECT *obj);
703int TS_ext_print_bio(BIO *bio, const STACK_OF(X509_EXTENSION) *extensions);
704int TS_X509_ALGOR_print_bio(BIO *bio, const X509_ALGOR *alg);
705int TS_MSG_IMPRINT_print_bio(BIO *bio, TS_MSG_IMPRINT *msg);
706
707/* Function declarations for handling configuration options,
708 defined in ts/ts_conf.c */
709
710X509 *TS_CONF_load_cert(const char *file);
711STACK_OF(X509) *TS_CONF_load_certs(const char *file);
712EVP_PKEY *TS_CONF_load_key(const char *file, const char *pass);
713const char *TS_CONF_get_tsa_section(CONF *conf, const char *section);
714int TS_CONF_set_serial(CONF *conf, const char *section, TS_serial_cb cb,
715 TS_RESP_CTX *ctx);
716int TS_CONF_set_crypto_device(CONF *conf, const char *section,
717 const char *device);
718int TS_CONF_set_default_engine(const char *name);
719int TS_CONF_set_signer_cert(CONF *conf, const char *section,
720 const char *cert, TS_RESP_CTX *ctx);
721int TS_CONF_set_certs(CONF *conf, const char *section, const char *certs,
722 TS_RESP_CTX *ctx);
723int TS_CONF_set_signer_key(CONF *conf, const char *section,
724 const char *key, const char *pass, TS_RESP_CTX *ctx);
725int TS_CONF_set_def_policy(CONF *conf, const char *section,
726 const char *policy, TS_RESP_CTX *ctx);
727int TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx);
728int TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx);
729int TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx);
730int TS_CONF_set_clock_precision_digits(CONF *conf, const char *section,
731 TS_RESP_CTX *ctx);
732int TS_CONF_set_ordering(CONF *conf, const char *section, TS_RESP_CTX *ctx);
733int TS_CONF_set_tsa_name(CONF *conf, const char *section, TS_RESP_CTX *ctx);
734int TS_CONF_set_ess_cert_id_chain(CONF *conf, const char *section,
735 TS_RESP_CTX *ctx);
736
737/* -------------------------------------------------- */
738/* BEGIN ERROR CODES */
739/* The following lines are auto generated by the script mkerr.pl. Any changes
740 * made after this point may be overwritten when the script is next run.
741 */
742void ERR_load_TS_strings(void);
743
744/* Error codes for the TS functions. */
745
746/* Function codes. */
747#define TS_F_D2I_TS_RESP 147
748#define TS_F_DEF_SERIAL_CB 110
749#define TS_F_DEF_TIME_CB 111
750#define TS_F_ESS_ADD_SIGNING_CERT 112
751#define TS_F_ESS_CERT_ID_NEW_INIT 113
752#define TS_F_ESS_SIGNING_CERT_NEW_INIT 114
753#define TS_F_INT_TS_RESP_VERIFY_TOKEN 149
754#define TS_F_PKCS7_TO_TS_TST_INFO 148
755#define TS_F_TS_ACCURACY_SET_MICROS 115
756#define TS_F_TS_ACCURACY_SET_MILLIS 116
757#define TS_F_TS_ACCURACY_SET_SECONDS 117
758#define TS_F_TS_CHECK_IMPRINTS 100
759#define TS_F_TS_CHECK_NONCES 101
760#define TS_F_TS_CHECK_POLICY 102
761#define TS_F_TS_CHECK_SIGNING_CERTS 103
762#define TS_F_TS_CHECK_STATUS_INFO 104
763#define TS_F_TS_COMPUTE_IMPRINT 145
764#define TS_F_TS_CONF_SET_DEFAULT_ENGINE 146
765#define TS_F_TS_GET_STATUS_TEXT 105
766#define TS_F_TS_MSG_IMPRINT_SET_ALGO 118
767#define TS_F_TS_REQ_SET_MSG_IMPRINT 119
768#define TS_F_TS_REQ_SET_NONCE 120
769#define TS_F_TS_REQ_SET_POLICY_ID 121
770#define TS_F_TS_RESP_CREATE_RESPONSE 122
771#define TS_F_TS_RESP_CREATE_TST_INFO 123
772#define TS_F_TS_RESP_CTX_ADD_FAILURE_INFO 124
773#define TS_F_TS_RESP_CTX_ADD_MD 125
774#define TS_F_TS_RESP_CTX_ADD_POLICY 126
775#define TS_F_TS_RESP_CTX_NEW 127
776#define TS_F_TS_RESP_CTX_SET_ACCURACY 128
777#define TS_F_TS_RESP_CTX_SET_CERTS 129
778#define TS_F_TS_RESP_CTX_SET_DEF_POLICY 130
779#define TS_F_TS_RESP_CTX_SET_SIGNER_CERT 131
780#define TS_F_TS_RESP_CTX_SET_STATUS_INFO 132
781#define TS_F_TS_RESP_GET_POLICY 133
782#define TS_F_TS_RESP_SET_GENTIME_WITH_PRECISION 134
783#define TS_F_TS_RESP_SET_STATUS_INFO 135
784#define TS_F_TS_RESP_SET_TST_INFO 150
785#define TS_F_TS_RESP_SIGN 136
786#define TS_F_TS_RESP_VERIFY_SIGNATURE 106
787#define TS_F_TS_RESP_VERIFY_TOKEN 107
788#define TS_F_TS_TST_INFO_SET_ACCURACY 137
789#define TS_F_TS_TST_INFO_SET_MSG_IMPRINT 138
790#define TS_F_TS_TST_INFO_SET_NONCE 139
791#define TS_F_TS_TST_INFO_SET_POLICY_ID 140
792#define TS_F_TS_TST_INFO_SET_SERIAL 141
793#define TS_F_TS_TST_INFO_SET_TIME 142
794#define TS_F_TS_TST_INFO_SET_TSA 143
795#define TS_F_TS_VERIFY 108
796#define TS_F_TS_VERIFY_CERT 109
797#define TS_F_TS_VERIFY_CTX_NEW 144
798
799/* Reason codes. */
800#define TS_R_BAD_PKCS7_TYPE 132
801#define TS_R_BAD_TYPE 133
802#define TS_R_CERTIFICATE_VERIFY_ERROR 100
803#define TS_R_COULD_NOT_SET_ENGINE 127
804#define TS_R_COULD_NOT_SET_TIME 115
805#define TS_R_D2I_TS_RESP_INT_FAILED 128
806#define TS_R_DETACHED_CONTENT 134
807#define TS_R_ESS_ADD_SIGNING_CERT_ERROR 116
808#define TS_R_ESS_SIGNING_CERTIFICATE_ERROR 101
809#define TS_R_INVALID_NULL_POINTER 102
810#define TS_R_INVALID_SIGNER_CERTIFICATE_PURPOSE 117
811#define TS_R_MESSAGE_IMPRINT_MISMATCH 103
812#define TS_R_NONCE_MISMATCH 104
813#define TS_R_NONCE_NOT_RETURNED 105
814#define TS_R_NO_CONTENT 106
815#define TS_R_NO_TIME_STAMP_TOKEN 107
816#define TS_R_PKCS7_ADD_SIGNATURE_ERROR 118
817#define TS_R_PKCS7_ADD_SIGNED_ATTR_ERROR 119
818#define TS_R_PKCS7_TO_TS_TST_INFO_FAILED 129
819#define TS_R_POLICY_MISMATCH 108
820#define TS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE 120
821#define TS_R_RESPONSE_SETUP_ERROR 121
822#define TS_R_SIGNATURE_FAILURE 109
823#define TS_R_THERE_MUST_BE_ONE_SIGNER 110
824#define TS_R_TIME_SYSCALL_ERROR 122
825#define TS_R_TOKEN_NOT_PRESENT 130
826#define TS_R_TOKEN_PRESENT 131
827#define TS_R_TSA_NAME_MISMATCH 111
828#define TS_R_TSA_UNTRUSTED 112
829#define TS_R_TST_INFO_SETUP_ERROR 123
830#define TS_R_TS_DATASIGN 124
831#define TS_R_UNACCEPTABLE_POLICY 125
832#define TS_R_UNSUPPORTED_MD_ALGORITHM 126
833#define TS_R_UNSUPPORTED_VERSION 113
834#define TS_R_WRONG_CONTENT_TYPE 114
835
836#ifdef __cplusplus
837}
838#endif
839#endif
diff --git a/src/lib/libcrypto/ts/ts_asn1.c b/src/lib/libcrypto/ts/ts_asn1.c
deleted file mode 100644
index d85f3690c0..0000000000
--- a/src/lib/libcrypto/ts/ts_asn1.c
+++ /dev/null
@@ -1,605 +0,0 @@
1/* $OpenBSD: ts_asn1.c,v 1.8 2015/02/10 05:25:45 jsing Exp $ */
2/* Written by Nils Larsch for the OpenSSL project 2004.
3 */
4/* ====================================================================
5 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23 *
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
28 *
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
51 *
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
55 *
56 */
57
58#include <openssl/opensslconf.h>
59
60#include <openssl/ts.h>
61#include <openssl/err.h>
62#include <openssl/asn1t.h>
63
64ASN1_SEQUENCE(TS_MSG_IMPRINT) = {
65 ASN1_SIMPLE(TS_MSG_IMPRINT, hash_algo, X509_ALGOR),
66 ASN1_SIMPLE(TS_MSG_IMPRINT, hashed_msg, ASN1_OCTET_STRING)
67} ASN1_SEQUENCE_END(TS_MSG_IMPRINT)
68
69
70TS_MSG_IMPRINT *
71d2i_TS_MSG_IMPRINT(TS_MSG_IMPRINT **a, const unsigned char **in, long len)
72{
73 return (TS_MSG_IMPRINT *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
74 &TS_MSG_IMPRINT_it);
75}
76
77int
78i2d_TS_MSG_IMPRINT(const TS_MSG_IMPRINT *a, unsigned char **out)
79{
80 return ASN1_item_i2d((ASN1_VALUE *)a, out, &TS_MSG_IMPRINT_it);
81}
82
83TS_MSG_IMPRINT *
84TS_MSG_IMPRINT_new(void)
85{
86 return (TS_MSG_IMPRINT *)ASN1_item_new(&TS_MSG_IMPRINT_it);
87}
88
89void
90TS_MSG_IMPRINT_free(TS_MSG_IMPRINT *a)
91{
92 ASN1_item_free((ASN1_VALUE *)a, &TS_MSG_IMPRINT_it);
93}
94
95TS_MSG_IMPRINT *
96TS_MSG_IMPRINT_dup(TS_MSG_IMPRINT *x)
97{
98 return ASN1_item_dup(&TS_MSG_IMPRINT_it, x);
99}
100
101#ifndef OPENSSL_NO_BIO
102TS_MSG_IMPRINT *
103d2i_TS_MSG_IMPRINT_bio(BIO *bp, TS_MSG_IMPRINT **a)
104{
105 return ASN1_d2i_bio_of(TS_MSG_IMPRINT, TS_MSG_IMPRINT_new,
106 d2i_TS_MSG_IMPRINT, bp, a);
107}
108
109int
110i2d_TS_MSG_IMPRINT_bio(BIO *bp, TS_MSG_IMPRINT *a)
111{
112 return ASN1_i2d_bio_of_const(TS_MSG_IMPRINT, i2d_TS_MSG_IMPRINT, bp, a);
113}
114#endif
115
116TS_MSG_IMPRINT *
117d2i_TS_MSG_IMPRINT_fp(FILE *fp, TS_MSG_IMPRINT **a)
118{
119 return ASN1_d2i_fp_of(TS_MSG_IMPRINT, TS_MSG_IMPRINT_new,
120 d2i_TS_MSG_IMPRINT, fp, a);
121}
122
123int
124i2d_TS_MSG_IMPRINT_fp(FILE *fp, TS_MSG_IMPRINT *a)
125{
126 return ASN1_i2d_fp_of_const(TS_MSG_IMPRINT, i2d_TS_MSG_IMPRINT, fp, a);
127}
128
129ASN1_SEQUENCE(TS_REQ) = {
130 ASN1_SIMPLE(TS_REQ, version, ASN1_INTEGER),
131 ASN1_SIMPLE(TS_REQ, msg_imprint, TS_MSG_IMPRINT),
132 ASN1_OPT(TS_REQ, policy_id, ASN1_OBJECT),
133 ASN1_OPT(TS_REQ, nonce, ASN1_INTEGER),
134 ASN1_OPT(TS_REQ, cert_req, ASN1_FBOOLEAN),
135 ASN1_IMP_SEQUENCE_OF_OPT(TS_REQ, extensions, X509_EXTENSION, 0)
136} ASN1_SEQUENCE_END(TS_REQ)
137
138
139TS_REQ *
140d2i_TS_REQ(TS_REQ **a, const unsigned char **in, long len)
141{
142 return (TS_REQ *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
143 &TS_REQ_it);
144}
145
146int
147i2d_TS_REQ(const TS_REQ *a, unsigned char **out)
148{
149 return ASN1_item_i2d((ASN1_VALUE *)a, out, &TS_REQ_it);
150}
151
152TS_REQ *
153TS_REQ_new(void)
154{
155 return (TS_REQ *)ASN1_item_new(&TS_REQ_it);
156}
157
158void
159TS_REQ_free(TS_REQ *a)
160{
161 ASN1_item_free((ASN1_VALUE *)a, &TS_REQ_it);
162}
163
164TS_REQ *
165TS_REQ_dup(TS_REQ *x)
166{
167 return ASN1_item_dup(&TS_REQ_it, x);
168}
169
170#ifndef OPENSSL_NO_BIO
171TS_REQ *
172d2i_TS_REQ_bio(BIO *bp, TS_REQ **a)
173{
174 return ASN1_d2i_bio_of(TS_REQ, TS_REQ_new, d2i_TS_REQ, bp, a);
175}
176
177int
178i2d_TS_REQ_bio(BIO *bp, TS_REQ *a)
179{
180 return ASN1_i2d_bio_of_const(TS_REQ, i2d_TS_REQ, bp, a);
181}
182#endif
183
184TS_REQ *
185d2i_TS_REQ_fp(FILE *fp, TS_REQ **a)
186{
187 return ASN1_d2i_fp_of(TS_REQ, TS_REQ_new, d2i_TS_REQ, fp, a);
188}
189
190int
191i2d_TS_REQ_fp(FILE *fp, TS_REQ *a)
192{
193 return ASN1_i2d_fp_of_const(TS_REQ, i2d_TS_REQ, fp, a);
194}
195
196ASN1_SEQUENCE(TS_ACCURACY) = {
197 ASN1_OPT(TS_ACCURACY, seconds, ASN1_INTEGER),
198 ASN1_IMP_OPT(TS_ACCURACY, millis, ASN1_INTEGER, 0),
199 ASN1_IMP_OPT(TS_ACCURACY, micros, ASN1_INTEGER, 1)
200} ASN1_SEQUENCE_END(TS_ACCURACY)
201
202
203TS_ACCURACY *
204d2i_TS_ACCURACY(TS_ACCURACY **a, const unsigned char **in, long len)
205{
206 return (TS_ACCURACY *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
207 &TS_ACCURACY_it);
208}
209
210int
211i2d_TS_ACCURACY(const TS_ACCURACY *a, unsigned char **out)
212{
213 return ASN1_item_i2d((ASN1_VALUE *)a, out, &TS_ACCURACY_it);
214}
215
216TS_ACCURACY *
217TS_ACCURACY_new(void)
218{
219 return (TS_ACCURACY *)ASN1_item_new(&TS_ACCURACY_it);
220}
221
222void
223TS_ACCURACY_free(TS_ACCURACY *a)
224{
225 ASN1_item_free((ASN1_VALUE *)a, &TS_ACCURACY_it);
226}
227
228TS_ACCURACY *
229TS_ACCURACY_dup(TS_ACCURACY *x)
230{
231 return ASN1_item_dup(&TS_ACCURACY_it, x);
232}
233
234ASN1_SEQUENCE(TS_TST_INFO) = {
235 ASN1_SIMPLE(TS_TST_INFO, version, ASN1_INTEGER),
236 ASN1_SIMPLE(TS_TST_INFO, policy_id, ASN1_OBJECT),
237 ASN1_SIMPLE(TS_TST_INFO, msg_imprint, TS_MSG_IMPRINT),
238 ASN1_SIMPLE(TS_TST_INFO, serial, ASN1_INTEGER),
239 ASN1_SIMPLE(TS_TST_INFO, time, ASN1_GENERALIZEDTIME),
240 ASN1_OPT(TS_TST_INFO, accuracy, TS_ACCURACY),
241 ASN1_OPT(TS_TST_INFO, ordering, ASN1_FBOOLEAN),
242 ASN1_OPT(TS_TST_INFO, nonce, ASN1_INTEGER),
243 ASN1_EXP_OPT(TS_TST_INFO, tsa, GENERAL_NAME, 0),
244 ASN1_IMP_SEQUENCE_OF_OPT(TS_TST_INFO, extensions, X509_EXTENSION, 1)
245} ASN1_SEQUENCE_END(TS_TST_INFO)
246
247
248TS_TST_INFO *
249d2i_TS_TST_INFO(TS_TST_INFO **a, const unsigned char **in, long len)
250{
251 return (TS_TST_INFO *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
252 &TS_TST_INFO_it);
253}
254
255int
256i2d_TS_TST_INFO(const TS_TST_INFO *a, unsigned char **out)
257{
258 return ASN1_item_i2d((ASN1_VALUE *)a, out, &TS_TST_INFO_it);
259}
260
261TS_TST_INFO *
262TS_TST_INFO_new(void)
263{
264 return (TS_TST_INFO *)ASN1_item_new(&TS_TST_INFO_it);
265}
266
267void
268TS_TST_INFO_free(TS_TST_INFO *a)
269{
270 ASN1_item_free((ASN1_VALUE *)a, &TS_TST_INFO_it);
271}
272
273TS_TST_INFO *
274TS_TST_INFO_dup(TS_TST_INFO *x)
275{
276 return ASN1_item_dup(&TS_TST_INFO_it, x);
277}
278
279#ifndef OPENSSL_NO_BIO
280TS_TST_INFO *
281d2i_TS_TST_INFO_bio(BIO *bp, TS_TST_INFO **a)
282{
283 return ASN1_d2i_bio_of(TS_TST_INFO, TS_TST_INFO_new, d2i_TS_TST_INFO,
284 bp, a);
285}
286
287int
288i2d_TS_TST_INFO_bio(BIO *bp, TS_TST_INFO *a)
289{
290 return ASN1_i2d_bio_of_const(TS_TST_INFO, i2d_TS_TST_INFO, bp, a);
291}
292#endif
293
294TS_TST_INFO *
295d2i_TS_TST_INFO_fp(FILE *fp, TS_TST_INFO **a)
296{
297 return ASN1_d2i_fp_of(TS_TST_INFO, TS_TST_INFO_new, d2i_TS_TST_INFO,
298 fp, a);
299}
300
301int
302i2d_TS_TST_INFO_fp(FILE *fp, TS_TST_INFO *a)
303{
304 return ASN1_i2d_fp_of_const(TS_TST_INFO, i2d_TS_TST_INFO, fp, a);
305}
306
307ASN1_SEQUENCE(TS_STATUS_INFO) = {
308 ASN1_SIMPLE(TS_STATUS_INFO, status, ASN1_INTEGER),
309 ASN1_SEQUENCE_OF_OPT(TS_STATUS_INFO, text, ASN1_UTF8STRING),
310 ASN1_OPT(TS_STATUS_INFO, failure_info, ASN1_BIT_STRING)
311} ASN1_SEQUENCE_END(TS_STATUS_INFO)
312
313
314TS_STATUS_INFO *
315d2i_TS_STATUS_INFO(TS_STATUS_INFO **a, const unsigned char **in, long len)
316{
317 return (TS_STATUS_INFO *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
318 &TS_STATUS_INFO_it);
319}
320
321int
322i2d_TS_STATUS_INFO(const TS_STATUS_INFO *a, unsigned char **out)
323{
324 return ASN1_item_i2d((ASN1_VALUE *)a, out, &TS_STATUS_INFO_it);
325}
326
327TS_STATUS_INFO *
328TS_STATUS_INFO_new(void)
329{
330 return (TS_STATUS_INFO *)ASN1_item_new(&TS_STATUS_INFO_it);
331}
332
333void
334TS_STATUS_INFO_free(TS_STATUS_INFO *a)
335{
336 ASN1_item_free((ASN1_VALUE *)a, &TS_STATUS_INFO_it);
337}
338
339TS_STATUS_INFO *
340TS_STATUS_INFO_dup(TS_STATUS_INFO *x)
341{
342 return ASN1_item_dup(&TS_STATUS_INFO_it, x);
343}
344
345static int
346ts_resp_set_tst_info(TS_RESP *a)
347{
348 long status;
349
350 status = ASN1_INTEGER_get(a->status_info->status);
351
352 if (a->token) {
353 if (status != 0 && status != 1) {
354 TSerr(TS_F_TS_RESP_SET_TST_INFO, TS_R_TOKEN_PRESENT);
355 return 0;
356 }
357 if (a->tst_info != NULL)
358 TS_TST_INFO_free(a->tst_info);
359 a->tst_info = PKCS7_to_TS_TST_INFO(a->token);
360 if (!a->tst_info) {
361 TSerr(TS_F_TS_RESP_SET_TST_INFO,
362 TS_R_PKCS7_TO_TS_TST_INFO_FAILED);
363 return 0;
364 }
365 } else if (status == 0 || status == 1) {
366 TSerr(TS_F_TS_RESP_SET_TST_INFO, TS_R_TOKEN_NOT_PRESENT);
367 return 0;
368 }
369
370 return 1;
371}
372
373static int
374ts_resp_cb(int op, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
375{
376 TS_RESP *ts_resp = (TS_RESP *)*pval;
377
378 if (op == ASN1_OP_NEW_POST) {
379 ts_resp->tst_info = NULL;
380 } else if (op == ASN1_OP_FREE_POST) {
381 if (ts_resp->tst_info != NULL)
382 TS_TST_INFO_free(ts_resp->tst_info);
383 } else if (op == ASN1_OP_D2I_POST) {
384 if (ts_resp_set_tst_info(ts_resp) == 0)
385 return 0;
386 }
387 return 1;
388}
389
390ASN1_SEQUENCE_cb(TS_RESP, ts_resp_cb) = {
391 ASN1_SIMPLE(TS_RESP, status_info, TS_STATUS_INFO),
392 ASN1_OPT(TS_RESP, token, PKCS7),
393} ASN1_SEQUENCE_END_cb(TS_RESP, TS_RESP)
394
395
396TS_RESP *
397d2i_TS_RESP(TS_RESP **a, const unsigned char **in, long len)
398{
399 return (TS_RESP *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
400 &TS_RESP_it);
401}
402
403int
404i2d_TS_RESP(const TS_RESP *a, unsigned char **out)
405{
406 return ASN1_item_i2d((ASN1_VALUE *)a, out, &TS_RESP_it);
407}
408
409TS_RESP *
410TS_RESP_new(void)
411{
412 return (TS_RESP *)ASN1_item_new(&TS_RESP_it);
413}
414
415void
416TS_RESP_free(TS_RESP *a)
417{
418 ASN1_item_free((ASN1_VALUE *)a, &TS_RESP_it);
419}
420
421TS_RESP *
422TS_RESP_dup(TS_RESP *x)
423{
424 return ASN1_item_dup(&TS_RESP_it, x);
425}
426
427#ifndef OPENSSL_NO_BIO
428TS_RESP *
429d2i_TS_RESP_bio(BIO *bp, TS_RESP **a)
430{
431 return ASN1_d2i_bio_of(TS_RESP, TS_RESP_new, d2i_TS_RESP, bp, a);
432}
433
434int
435i2d_TS_RESP_bio(BIO *bp, TS_RESP *a)
436{
437 return ASN1_i2d_bio_of_const(TS_RESP, i2d_TS_RESP, bp, a);
438}
439#endif
440
441TS_RESP *
442d2i_TS_RESP_fp(FILE *fp, TS_RESP **a)
443{
444 return ASN1_d2i_fp_of(TS_RESP, TS_RESP_new, d2i_TS_RESP, fp, a);
445}
446
447int
448i2d_TS_RESP_fp(FILE *fp, TS_RESP *a)
449{
450 return ASN1_i2d_fp_of_const(TS_RESP, i2d_TS_RESP, fp, a);
451}
452
453ASN1_SEQUENCE(ESS_ISSUER_SERIAL) = {
454 ASN1_SEQUENCE_OF(ESS_ISSUER_SERIAL, issuer, GENERAL_NAME),
455 ASN1_SIMPLE(ESS_ISSUER_SERIAL, serial, ASN1_INTEGER)
456} ASN1_SEQUENCE_END(ESS_ISSUER_SERIAL)
457
458
459ESS_ISSUER_SERIAL *
460d2i_ESS_ISSUER_SERIAL(ESS_ISSUER_SERIAL **a, const unsigned char **in, long len)
461{
462 return (ESS_ISSUER_SERIAL *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
463 &ESS_ISSUER_SERIAL_it);
464}
465
466int
467i2d_ESS_ISSUER_SERIAL(const ESS_ISSUER_SERIAL *a, unsigned char **out)
468{
469 return ASN1_item_i2d((ASN1_VALUE *)a, out, &ESS_ISSUER_SERIAL_it);
470}
471
472ESS_ISSUER_SERIAL *
473ESS_ISSUER_SERIAL_new(void)
474{
475 return (ESS_ISSUER_SERIAL *)ASN1_item_new(&ESS_ISSUER_SERIAL_it);
476}
477
478void
479ESS_ISSUER_SERIAL_free(ESS_ISSUER_SERIAL *a)
480{
481 ASN1_item_free((ASN1_VALUE *)a, &ESS_ISSUER_SERIAL_it);
482}
483
484ESS_ISSUER_SERIAL *
485ESS_ISSUER_SERIAL_dup(ESS_ISSUER_SERIAL *x)
486{
487 return ASN1_item_dup(&ESS_ISSUER_SERIAL_it, x);
488}
489
490ASN1_SEQUENCE(ESS_CERT_ID) = {
491 ASN1_SIMPLE(ESS_CERT_ID, hash, ASN1_OCTET_STRING),
492 ASN1_OPT(ESS_CERT_ID, issuer_serial, ESS_ISSUER_SERIAL)
493} ASN1_SEQUENCE_END(ESS_CERT_ID)
494
495
496ESS_CERT_ID *
497d2i_ESS_CERT_ID(ESS_CERT_ID **a, const unsigned char **in, long len)
498{
499 return (ESS_CERT_ID *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
500 &ESS_CERT_ID_it);
501}
502
503int
504i2d_ESS_CERT_ID(const ESS_CERT_ID *a, unsigned char **out)
505{
506 return ASN1_item_i2d((ASN1_VALUE *)a, out, &ESS_CERT_ID_it);
507}
508
509ESS_CERT_ID *
510ESS_CERT_ID_new(void)
511{
512 return (ESS_CERT_ID *)ASN1_item_new(&ESS_CERT_ID_it);
513}
514
515void
516ESS_CERT_ID_free(ESS_CERT_ID *a)
517{
518 ASN1_item_free((ASN1_VALUE *)a, &ESS_CERT_ID_it);
519}
520
521ESS_CERT_ID *
522ESS_CERT_ID_dup(ESS_CERT_ID *x)
523{
524 return ASN1_item_dup(&ESS_CERT_ID_it, x);
525}
526
527ASN1_SEQUENCE(ESS_SIGNING_CERT) = {
528 ASN1_SEQUENCE_OF(ESS_SIGNING_CERT, cert_ids, ESS_CERT_ID),
529 ASN1_SEQUENCE_OF_OPT(ESS_SIGNING_CERT, policy_info, POLICYINFO)
530} ASN1_SEQUENCE_END(ESS_SIGNING_CERT)
531
532
533ESS_SIGNING_CERT *
534d2i_ESS_SIGNING_CERT(ESS_SIGNING_CERT **a, const unsigned char **in, long len)
535{
536 return (ESS_SIGNING_CERT *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
537 &ESS_SIGNING_CERT_it);
538}
539
540int
541i2d_ESS_SIGNING_CERT(const ESS_SIGNING_CERT *a, unsigned char **out)
542{
543 return ASN1_item_i2d((ASN1_VALUE *)a, out, &ESS_SIGNING_CERT_it);
544}
545
546ESS_SIGNING_CERT *
547ESS_SIGNING_CERT_new(void)
548{
549 return (ESS_SIGNING_CERT *)ASN1_item_new(&ESS_SIGNING_CERT_it);
550}
551
552void
553ESS_SIGNING_CERT_free(ESS_SIGNING_CERT *a)
554{
555 ASN1_item_free((ASN1_VALUE *)a, &ESS_SIGNING_CERT_it);
556}
557
558ESS_SIGNING_CERT *
559ESS_SIGNING_CERT_dup(ESS_SIGNING_CERT *x)
560{
561 return ASN1_item_dup(&ESS_SIGNING_CERT_it, x);
562}
563
564/* Getting encapsulated TS_TST_INFO object from PKCS7. */
565TS_TST_INFO *
566PKCS7_to_TS_TST_INFO(PKCS7 *token)
567{
568 PKCS7_SIGNED *pkcs7_signed;
569 PKCS7 *enveloped;
570 ASN1_TYPE *tst_info_wrapper;
571 ASN1_OCTET_STRING *tst_info_der;
572 const unsigned char *p;
573
574 if (!PKCS7_type_is_signed(token)) {
575 TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_BAD_PKCS7_TYPE);
576 return NULL;
577 }
578
579 /* Content must be present. */
580 if (PKCS7_get_detached(token)) {
581 TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_DETACHED_CONTENT);
582 return NULL;
583 }
584
585 /* We have a signed data with content. */
586 pkcs7_signed = token->d.sign;
587 enveloped = pkcs7_signed->contents;
588 if (OBJ_obj2nid(enveloped->type) != NID_id_smime_ct_TSTInfo) {
589 TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_BAD_PKCS7_TYPE);
590 return NULL;
591 }
592
593 /* We have a DER encoded TST_INFO as the signed data. */
594 tst_info_wrapper = enveloped->d.other;
595 if (tst_info_wrapper->type != V_ASN1_OCTET_STRING) {
596 TSerr(TS_F_PKCS7_TO_TS_TST_INFO, TS_R_BAD_TYPE);
597 return NULL;
598 }
599
600 /* We have the correct ASN1_OCTET_STRING type. */
601 tst_info_der = tst_info_wrapper->value.octet_string;
602 /* At last, decode the TST_INFO. */
603 p = tst_info_der->data;
604 return d2i_TS_TST_INFO(NULL, &p, tst_info_der->length);
605}
diff --git a/src/lib/libcrypto/ts/ts_conf.c b/src/lib/libcrypto/ts/ts_conf.c
deleted file mode 100644
index bb98a6ff4c..0000000000
--- a/src/lib/libcrypto/ts/ts_conf.c
+++ /dev/null
@@ -1,532 +0,0 @@
1/* $OpenBSD: ts_conf.c,v 1.9 2015/02/11 03:19:37 doug Exp $ */
2/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3 * project 2002.
4 */
5/* ====================================================================
6 * Copyright (c) 2006 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#include <string.h>
60
61#include <openssl/opensslconf.h>
62
63#include <openssl/crypto.h>
64#include <openssl/err.h>
65#include <openssl/pem.h>
66#include <openssl/ts.h>
67
68#ifndef OPENSSL_NO_ENGINE
69#include <openssl/engine.h>
70#endif
71
72/* Macro definitions for the configuration file. */
73
74#define BASE_SECTION "tsa"
75#define ENV_DEFAULT_TSA "default_tsa"
76#define ENV_SERIAL "serial"
77#define ENV_CRYPTO_DEVICE "crypto_device"
78#define ENV_SIGNER_CERT "signer_cert"
79#define ENV_CERTS "certs"
80#define ENV_SIGNER_KEY "signer_key"
81#define ENV_DEFAULT_POLICY "default_policy"
82#define ENV_OTHER_POLICIES "other_policies"
83#define ENV_DIGESTS "digests"
84#define ENV_ACCURACY "accuracy"
85#define ENV_ORDERING "ordering"
86#define ENV_TSA_NAME "tsa_name"
87#define ENV_ESS_CERT_ID_CHAIN "ess_cert_id_chain"
88#define ENV_VALUE_SECS "secs"
89#define ENV_VALUE_MILLISECS "millisecs"
90#define ENV_VALUE_MICROSECS "microsecs"
91#define ENV_CLOCK_PRECISION_DIGITS "clock_precision_digits"
92#define ENV_VALUE_YES "yes"
93#define ENV_VALUE_NO "no"
94
95/* Function definitions for certificate and key loading. */
96
97X509 *
98TS_CONF_load_cert(const char *file)
99{
100 BIO *cert = NULL;
101 X509 *x = NULL;
102
103 if ((cert = BIO_new_file(file, "r")) == NULL)
104 goto end;
105 x = PEM_read_bio_X509_AUX(cert, NULL, NULL, NULL);
106
107end:
108 if (x == NULL)
109 fprintf(stderr, "unable to load certificate: %s\n", file);
110 BIO_free(cert);
111 return x;
112}
113
114STACK_OF(X509) *
115TS_CONF_load_certs(const char *file)
116{
117 BIO *certs = NULL;
118 STACK_OF(X509) *othercerts = NULL;
119 STACK_OF(X509_INFO) *allcerts = NULL;
120 int i;
121
122 if (!(certs = BIO_new_file(file, "r")))
123 goto end;
124
125 if (!(othercerts = sk_X509_new_null()))
126 goto end;
127 allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL);
128 for (i = 0; i < sk_X509_INFO_num(allcerts); i++) {
129 X509_INFO *xi = sk_X509_INFO_value(allcerts, i);
130 if (xi->x509) {
131 if (sk_X509_push(othercerts, xi->x509) == 0) {
132 sk_X509_pop_free(othercerts, X509_free);
133 othercerts = NULL;
134 goto end;
135 }
136 xi->x509 = NULL;
137 }
138 }
139
140end:
141 if (othercerts == NULL)
142 fprintf(stderr, "unable to load certificates: %s\n", file);
143 sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
144 BIO_free(certs);
145 return othercerts;
146}
147
148EVP_PKEY *
149TS_CONF_load_key(const char *file, const char *pass)
150{
151 BIO *key = NULL;
152 EVP_PKEY *pkey = NULL;
153
154 if (!(key = BIO_new_file(file, "r")))
155 goto end;
156 pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, (char *) pass);
157
158end:
159 if (pkey == NULL)
160 fprintf(stderr, "unable to load private key: %s\n", file);
161 BIO_free(key);
162 return pkey;
163}
164
165/* Function definitions for handling configuration options. */
166
167static void
168TS_CONF_lookup_fail(const char *name, const char *tag)
169{
170 fprintf(stderr, "variable lookup failed for %s::%s\n", name, tag);
171}
172
173static void
174TS_CONF_invalid(const char *name, const char *tag)
175{
176 fprintf(stderr, "invalid variable value for %s::%s\n", name, tag);
177}
178
179const char *
180TS_CONF_get_tsa_section(CONF *conf, const char *section)
181{
182 if (!section) {
183 section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_TSA);
184 if (!section)
185 TS_CONF_lookup_fail(BASE_SECTION, ENV_DEFAULT_TSA);
186 }
187 return section;
188}
189
190int
191TS_CONF_set_serial(CONF *conf, const char *section, TS_serial_cb cb,
192 TS_RESP_CTX *ctx)
193{
194 int ret = 0;
195 char *serial = NCONF_get_string(conf, section, ENV_SERIAL);
196
197 if (!serial) {
198 TS_CONF_lookup_fail(section, ENV_SERIAL);
199 goto err;
200 }
201 TS_RESP_CTX_set_serial_cb(ctx, cb, serial);
202
203 ret = 1;
204
205err:
206 return ret;
207}
208
209#ifndef OPENSSL_NO_ENGINE
210
211int
212TS_CONF_set_crypto_device(CONF *conf, const char *section, const char *device)
213{
214 int ret = 0;
215
216 if (!device)
217 device = NCONF_get_string(conf, section, ENV_CRYPTO_DEVICE);
218
219 if (device && !TS_CONF_set_default_engine(device)) {
220 TS_CONF_invalid(section, ENV_CRYPTO_DEVICE);
221 goto err;
222 }
223 ret = 1;
224
225err:
226 return ret;
227}
228
229int
230TS_CONF_set_default_engine(const char *name)
231{
232 ENGINE *e = NULL;
233 int ret = 0;
234
235 /* Leave the default if builtin specified. */
236 if (strcmp(name, "builtin") == 0)
237 return 1;
238
239 if (!(e = ENGINE_by_id(name)))
240 goto err;
241 /* All the operations are going to be carried out by the engine. */
242 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
243 goto err;
244 ret = 1;
245
246err:
247 if (!ret) {
248 TSerr(TS_F_TS_CONF_SET_DEFAULT_ENGINE,
249 TS_R_COULD_NOT_SET_ENGINE);
250 ERR_asprintf_error_data("engine:%s", name);
251 }
252 if (e)
253 ENGINE_free(e);
254 return ret;
255}
256
257#endif
258
259int
260TS_CONF_set_signer_cert(CONF *conf, const char *section, const char *cert,
261 TS_RESP_CTX *ctx)
262{
263 int ret = 0;
264 X509 *cert_obj = NULL;
265
266 if (!cert)
267 cert = NCONF_get_string(conf, section, ENV_SIGNER_CERT);
268 if (!cert) {
269 TS_CONF_lookup_fail(section, ENV_SIGNER_CERT);
270 goto err;
271 }
272 if (!(cert_obj = TS_CONF_load_cert(cert)))
273 goto err;
274 if (!TS_RESP_CTX_set_signer_cert(ctx, cert_obj))
275 goto err;
276
277 ret = 1;
278
279err:
280 X509_free(cert_obj);
281 return ret;
282}
283
284int
285TS_CONF_set_certs(CONF *conf, const char *section, const char *certs,
286 TS_RESP_CTX *ctx)
287{
288 int ret = 0;
289 STACK_OF(X509) *certs_obj = NULL;
290
291 if (!certs)
292 certs = NCONF_get_string(conf, section, ENV_CERTS);
293 /* Certificate chain is optional. */
294 if (!certs)
295 goto end;
296 if (!(certs_obj = TS_CONF_load_certs(certs)))
297 goto err;
298 if (!TS_RESP_CTX_set_certs(ctx, certs_obj))
299 goto err;
300
301end:
302 ret = 1;
303err:
304 sk_X509_pop_free(certs_obj, X509_free);
305 return ret;
306}
307
308int
309TS_CONF_set_signer_key(CONF *conf, const char *section, const char *key,
310 const char *pass, TS_RESP_CTX *ctx)
311{
312 int ret = 0;
313 EVP_PKEY *key_obj = NULL;
314
315 if (!key)
316 key = NCONF_get_string(conf, section, ENV_SIGNER_KEY);
317 if (!key) {
318 TS_CONF_lookup_fail(section, ENV_SIGNER_KEY);
319 goto err;
320 }
321 if (!(key_obj = TS_CONF_load_key(key, pass)))
322 goto err;
323 if (!TS_RESP_CTX_set_signer_key(ctx, key_obj))
324 goto err;
325
326 ret = 1;
327
328err:
329 EVP_PKEY_free(key_obj);
330 return ret;
331}
332
333int
334TS_CONF_set_def_policy(CONF *conf, const char *section, const char *policy,
335 TS_RESP_CTX *ctx)
336{
337 int ret = 0;
338 ASN1_OBJECT *policy_obj = NULL;
339
340 if (!policy)
341 policy = NCONF_get_string(conf, section, ENV_DEFAULT_POLICY);
342 if (!policy) {
343 TS_CONF_lookup_fail(section, ENV_DEFAULT_POLICY);
344 goto err;
345 }
346 if (!(policy_obj = OBJ_txt2obj(policy, 0))) {
347 TS_CONF_invalid(section, ENV_DEFAULT_POLICY);
348 goto err;
349 }
350 if (!TS_RESP_CTX_set_def_policy(ctx, policy_obj))
351 goto err;
352
353 ret = 1;
354
355err:
356 ASN1_OBJECT_free(policy_obj);
357 return ret;
358}
359
360int
361TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx)
362{
363 int ret = 0;
364 int i;
365 STACK_OF(CONF_VALUE) *list = NULL;
366 char *policies = NCONF_get_string(conf, section, ENV_OTHER_POLICIES);
367
368 /* If no other policy is specified, that's fine. */
369 if (policies && !(list = X509V3_parse_list(policies))) {
370 TS_CONF_invalid(section, ENV_OTHER_POLICIES);
371 goto err;
372 }
373 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
374 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
375 const char *extval = val->value ? val->value : val->name;
376 ASN1_OBJECT *objtmp;
377 if (!(objtmp = OBJ_txt2obj(extval, 0))) {
378 TS_CONF_invalid(section, ENV_OTHER_POLICIES);
379 goto err;
380 }
381 if (!TS_RESP_CTX_add_policy(ctx, objtmp))
382 goto err;
383 ASN1_OBJECT_free(objtmp);
384 }
385
386 ret = 1;
387
388err:
389 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
390 return ret;
391}
392
393int
394TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx)
395{
396 int ret = 0;
397 int i;
398 STACK_OF(CONF_VALUE) *list = NULL;
399 char *digests = NCONF_get_string(conf, section, ENV_DIGESTS);
400
401 if (!digests) {
402 TS_CONF_lookup_fail(section, ENV_DIGESTS);
403 goto err;
404 }
405 if (!(list = X509V3_parse_list(digests))) {
406 TS_CONF_invalid(section, ENV_DIGESTS);
407 goto err;
408 }
409 if (sk_CONF_VALUE_num(list) == 0) {
410 TS_CONF_invalid(section, ENV_DIGESTS);
411 goto err;
412 }
413 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
414 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
415 const char *extval = val->value ? val->value : val->name;
416 const EVP_MD *md;
417 if (!(md = EVP_get_digestbyname(extval))) {
418 TS_CONF_invalid(section, ENV_DIGESTS);
419 goto err;
420 }
421 if (!TS_RESP_CTX_add_md(ctx, md))
422 goto err;
423 }
424
425 ret = 1;
426
427err:
428 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
429 return ret;
430}
431
432int
433TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx)
434{
435 int ret = 0;
436 int i;
437 int secs = 0, millis = 0, micros = 0;
438 STACK_OF(CONF_VALUE) *list = NULL;
439 char *accuracy = NCONF_get_string(conf, section, ENV_ACCURACY);
440
441 if (accuracy && !(list = X509V3_parse_list(accuracy))) {
442 TS_CONF_invalid(section, ENV_ACCURACY);
443 goto err;
444 }
445 for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
446 CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
447 if (strcmp(val->name, ENV_VALUE_SECS) == 0) {
448 if (val->value)
449 secs = atoi(val->value);
450 } else if (strcmp(val->name, ENV_VALUE_MILLISECS) == 0) {
451 if (val->value)
452 millis = atoi(val->value);
453 } else if (strcmp(val->name, ENV_VALUE_MICROSECS) == 0) {
454 if (val->value)
455 micros = atoi(val->value);
456 } else {
457 TS_CONF_invalid(section, ENV_ACCURACY);
458 goto err;
459 }
460 }
461 if (!TS_RESP_CTX_set_accuracy(ctx, secs, millis, micros))
462 goto err;
463
464 ret = 1;
465
466err:
467 sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
468 return ret;
469}
470
471int
472TS_CONF_set_clock_precision_digits(CONF *conf, const char *section,
473 TS_RESP_CTX *ctx)
474{
475 int ret = 0;
476 long digits = 0;
477
478 /* If not specified, set the default value to 0, i.e. sec precision */
479 if (!NCONF_get_number_e(conf, section, ENV_CLOCK_PRECISION_DIGITS,
480 &digits))
481 digits = 0;
482 if (digits < 0 || digits > TS_MAX_CLOCK_PRECISION_DIGITS) {
483 TS_CONF_invalid(section, ENV_CLOCK_PRECISION_DIGITS);
484 goto err;
485 }
486
487 if (!TS_RESP_CTX_set_clock_precision_digits(ctx, digits))
488 goto err;
489
490 return 1;
491
492err:
493 return ret;
494}
495
496static int
497TS_CONF_add_flag(CONF *conf, const char *section, const char *field, int flag,
498 TS_RESP_CTX *ctx)
499{
500 /* Default is false. */
501 const char *value = NCONF_get_string(conf, section, field);
502
503 if (value) {
504 if (strcmp(value, ENV_VALUE_YES) == 0)
505 TS_RESP_CTX_add_flags(ctx, flag);
506 else if (strcmp(value, ENV_VALUE_NO) != 0) {
507 TS_CONF_invalid(section, field);
508 return 0;
509 }
510 }
511
512 return 1;
513}
514
515int
516TS_CONF_set_ordering(CONF *conf, const char *section, TS_RESP_CTX *ctx)
517{
518 return TS_CONF_add_flag(conf, section, ENV_ORDERING, TS_ORDERING, ctx);
519}
520
521int
522TS_CONF_set_tsa_name(CONF *conf, const char *section, TS_RESP_CTX *ctx)
523{
524 return TS_CONF_add_flag(conf, section, ENV_TSA_NAME, TS_TSA_NAME, ctx);
525}
526
527int
528TS_CONF_set_ess_cert_id_chain(CONF *conf, const char *section, TS_RESP_CTX *ctx)
529{
530 return TS_CONF_add_flag(conf, section, ENV_ESS_CERT_ID_CHAIN,
531 TS_ESS_CERT_ID_CHAIN, ctx);
532}
diff --git a/src/lib/libcrypto/ts/ts_err.c b/src/lib/libcrypto/ts/ts_err.c
deleted file mode 100644
index f71be883f8..0000000000
--- a/src/lib/libcrypto/ts/ts_err.c
+++ /dev/null
@@ -1,179 +0,0 @@
1/* $OpenBSD: ts_err.c,v 1.4 2014/07/10 22:45:58 jsing Exp $ */
2/* ====================================================================
3 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56/* NOTE: this file was auto generated by the mkerr.pl script: any changes
57 * made to it will be overwritten when the script next updates this file,
58 * only reason strings will be preserved.
59 */
60
61#include <stdio.h>
62
63#include <openssl/opensslconf.h>
64
65#include <openssl/err.h>
66#include <openssl/ts.h>
67
68/* BEGIN ERROR CODES */
69#ifndef OPENSSL_NO_ERR
70
71#define ERR_FUNC(func) ERR_PACK(ERR_LIB_TS,func,0)
72#define ERR_REASON(reason) ERR_PACK(ERR_LIB_TS,0,reason)
73
74static ERR_STRING_DATA TS_str_functs[] = {
75 {ERR_FUNC(TS_F_D2I_TS_RESP), "d2i_TS_RESP"},
76 {ERR_FUNC(TS_F_DEF_SERIAL_CB), "DEF_SERIAL_CB"},
77 {ERR_FUNC(TS_F_DEF_TIME_CB), "DEF_TIME_CB"},
78 {ERR_FUNC(TS_F_ESS_ADD_SIGNING_CERT), "ESS_ADD_SIGNING_CERT"},
79 {ERR_FUNC(TS_F_ESS_CERT_ID_NEW_INIT), "ESS_CERT_ID_NEW_INIT"},
80 {ERR_FUNC(TS_F_ESS_SIGNING_CERT_NEW_INIT), "ESS_SIGNING_CERT_NEW_INIT"},
81 {ERR_FUNC(TS_F_INT_TS_RESP_VERIFY_TOKEN), "INT_TS_RESP_VERIFY_TOKEN"},
82 {ERR_FUNC(TS_F_PKCS7_TO_TS_TST_INFO), "PKCS7_to_TS_TST_INFO"},
83 {ERR_FUNC(TS_F_TS_ACCURACY_SET_MICROS), "TS_ACCURACY_set_micros"},
84 {ERR_FUNC(TS_F_TS_ACCURACY_SET_MILLIS), "TS_ACCURACY_set_millis"},
85 {ERR_FUNC(TS_F_TS_ACCURACY_SET_SECONDS), "TS_ACCURACY_set_seconds"},
86 {ERR_FUNC(TS_F_TS_CHECK_IMPRINTS), "TS_CHECK_IMPRINTS"},
87 {ERR_FUNC(TS_F_TS_CHECK_NONCES), "TS_CHECK_NONCES"},
88 {ERR_FUNC(TS_F_TS_CHECK_POLICY), "TS_CHECK_POLICY"},
89 {ERR_FUNC(TS_F_TS_CHECK_SIGNING_CERTS), "TS_CHECK_SIGNING_CERTS"},
90 {ERR_FUNC(TS_F_TS_CHECK_STATUS_INFO), "TS_CHECK_STATUS_INFO"},
91 {ERR_FUNC(TS_F_TS_COMPUTE_IMPRINT), "TS_COMPUTE_IMPRINT"},
92 {ERR_FUNC(TS_F_TS_CONF_SET_DEFAULT_ENGINE), "TS_CONF_set_default_engine"},
93 {ERR_FUNC(TS_F_TS_GET_STATUS_TEXT), "TS_GET_STATUS_TEXT"},
94 {ERR_FUNC(TS_F_TS_MSG_IMPRINT_SET_ALGO), "TS_MSG_IMPRINT_set_algo"},
95 {ERR_FUNC(TS_F_TS_REQ_SET_MSG_IMPRINT), "TS_REQ_set_msg_imprint"},
96 {ERR_FUNC(TS_F_TS_REQ_SET_NONCE), "TS_REQ_set_nonce"},
97 {ERR_FUNC(TS_F_TS_REQ_SET_POLICY_ID), "TS_REQ_set_policy_id"},
98 {ERR_FUNC(TS_F_TS_RESP_CREATE_RESPONSE), "TS_RESP_create_response"},
99 {ERR_FUNC(TS_F_TS_RESP_CREATE_TST_INFO), "TS_RESP_CREATE_TST_INFO"},
100 {ERR_FUNC(TS_F_TS_RESP_CTX_ADD_FAILURE_INFO), "TS_RESP_CTX_add_failure_info"},
101 {ERR_FUNC(TS_F_TS_RESP_CTX_ADD_MD), "TS_RESP_CTX_add_md"},
102 {ERR_FUNC(TS_F_TS_RESP_CTX_ADD_POLICY), "TS_RESP_CTX_add_policy"},
103 {ERR_FUNC(TS_F_TS_RESP_CTX_NEW), "TS_RESP_CTX_new"},
104 {ERR_FUNC(TS_F_TS_RESP_CTX_SET_ACCURACY), "TS_RESP_CTX_set_accuracy"},
105 {ERR_FUNC(TS_F_TS_RESP_CTX_SET_CERTS), "TS_RESP_CTX_set_certs"},
106 {ERR_FUNC(TS_F_TS_RESP_CTX_SET_DEF_POLICY), "TS_RESP_CTX_set_def_policy"},
107 {ERR_FUNC(TS_F_TS_RESP_CTX_SET_SIGNER_CERT), "TS_RESP_CTX_set_signer_cert"},
108 {ERR_FUNC(TS_F_TS_RESP_CTX_SET_STATUS_INFO), "TS_RESP_CTX_set_status_info"},
109 {ERR_FUNC(TS_F_TS_RESP_GET_POLICY), "TS_RESP_GET_POLICY"},
110 {ERR_FUNC(TS_F_TS_RESP_SET_GENTIME_WITH_PRECISION), "TS_RESP_SET_GENTIME_WITH_PRECISION"},
111 {ERR_FUNC(TS_F_TS_RESP_SET_STATUS_INFO), "TS_RESP_set_status_info"},
112 {ERR_FUNC(TS_F_TS_RESP_SET_TST_INFO), "TS_RESP_set_tst_info"},
113 {ERR_FUNC(TS_F_TS_RESP_SIGN), "TS_RESP_SIGN"},
114 {ERR_FUNC(TS_F_TS_RESP_VERIFY_SIGNATURE), "TS_RESP_verify_signature"},
115 {ERR_FUNC(TS_F_TS_RESP_VERIFY_TOKEN), "TS_RESP_verify_token"},
116 {ERR_FUNC(TS_F_TS_TST_INFO_SET_ACCURACY), "TS_TST_INFO_set_accuracy"},
117 {ERR_FUNC(TS_F_TS_TST_INFO_SET_MSG_IMPRINT), "TS_TST_INFO_set_msg_imprint"},
118 {ERR_FUNC(TS_F_TS_TST_INFO_SET_NONCE), "TS_TST_INFO_set_nonce"},
119 {ERR_FUNC(TS_F_TS_TST_INFO_SET_POLICY_ID), "TS_TST_INFO_set_policy_id"},
120 {ERR_FUNC(TS_F_TS_TST_INFO_SET_SERIAL), "TS_TST_INFO_set_serial"},
121 {ERR_FUNC(TS_F_TS_TST_INFO_SET_TIME), "TS_TST_INFO_set_time"},
122 {ERR_FUNC(TS_F_TS_TST_INFO_SET_TSA), "TS_TST_INFO_set_tsa"},
123 {ERR_FUNC(TS_F_TS_VERIFY), "TS_VERIFY"},
124 {ERR_FUNC(TS_F_TS_VERIFY_CERT), "TS_VERIFY_CERT"},
125 {ERR_FUNC(TS_F_TS_VERIFY_CTX_NEW), "TS_VERIFY_CTX_new"},
126 {0, NULL}
127};
128
129static ERR_STRING_DATA TS_str_reasons[]= {
130 {ERR_REASON(TS_R_BAD_PKCS7_TYPE) , "bad pkcs7 type"},
131 {ERR_REASON(TS_R_BAD_TYPE) , "bad type"},
132 {ERR_REASON(TS_R_CERTIFICATE_VERIFY_ERROR), "certificate verify error"},
133 {ERR_REASON(TS_R_COULD_NOT_SET_ENGINE) , "could not set engine"},
134 {ERR_REASON(TS_R_COULD_NOT_SET_TIME) , "could not set time"},
135 {ERR_REASON(TS_R_D2I_TS_RESP_INT_FAILED) , "d2i ts resp int failed"},
136 {ERR_REASON(TS_R_DETACHED_CONTENT) , "detached content"},
137 {ERR_REASON(TS_R_ESS_ADD_SIGNING_CERT_ERROR), "ess add signing cert error"},
138 {ERR_REASON(TS_R_ESS_SIGNING_CERTIFICATE_ERROR), "ess signing certificate error"},
139 {ERR_REASON(TS_R_INVALID_NULL_POINTER) , "invalid null pointer"},
140 {ERR_REASON(TS_R_INVALID_SIGNER_CERTIFICATE_PURPOSE), "invalid signer certificate purpose"},
141 {ERR_REASON(TS_R_MESSAGE_IMPRINT_MISMATCH), "message imprint mismatch"},
142 {ERR_REASON(TS_R_NONCE_MISMATCH) , "nonce mismatch"},
143 {ERR_REASON(TS_R_NONCE_NOT_RETURNED) , "nonce not returned"},
144 {ERR_REASON(TS_R_NO_CONTENT) , "no content"},
145 {ERR_REASON(TS_R_NO_TIME_STAMP_TOKEN) , "no time stamp token"},
146 {ERR_REASON(TS_R_PKCS7_ADD_SIGNATURE_ERROR), "pkcs7 add signature error"},
147 {ERR_REASON(TS_R_PKCS7_ADD_SIGNED_ATTR_ERROR), "pkcs7 add signed attr error"},
148 {ERR_REASON(TS_R_PKCS7_TO_TS_TST_INFO_FAILED), "pkcs7 to ts tst info failed"},
149 {ERR_REASON(TS_R_POLICY_MISMATCH) , "policy mismatch"},
150 {ERR_REASON(TS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE), "private key does not match certificate"},
151 {ERR_REASON(TS_R_RESPONSE_SETUP_ERROR) , "response setup error"},
152 {ERR_REASON(TS_R_SIGNATURE_FAILURE) , "signature failure"},
153 {ERR_REASON(TS_R_THERE_MUST_BE_ONE_SIGNER), "there must be one signer"},
154 {ERR_REASON(TS_R_TIME_SYSCALL_ERROR) , "time syscall error"},
155 {ERR_REASON(TS_R_TOKEN_NOT_PRESENT) , "token not present"},
156 {ERR_REASON(TS_R_TOKEN_PRESENT) , "token present"},
157 {ERR_REASON(TS_R_TSA_NAME_MISMATCH) , "tsa name mismatch"},
158 {ERR_REASON(TS_R_TSA_UNTRUSTED) , "tsa untrusted"},
159 {ERR_REASON(TS_R_TST_INFO_SETUP_ERROR) , "tst info setup error"},
160 {ERR_REASON(TS_R_TS_DATASIGN) , "ts datasign"},
161 {ERR_REASON(TS_R_UNACCEPTABLE_POLICY) , "unacceptable policy"},
162 {ERR_REASON(TS_R_UNSUPPORTED_MD_ALGORITHM), "unsupported md algorithm"},
163 {ERR_REASON(TS_R_UNSUPPORTED_VERSION) , "unsupported version"},
164 {ERR_REASON(TS_R_WRONG_CONTENT_TYPE) , "wrong content type"},
165 {0, NULL}
166};
167
168#endif
169
170void
171ERR_load_TS_strings(void)
172{
173#ifndef OPENSSL_NO_ERR
174 if (ERR_func_error_string(TS_str_functs[0].error) == NULL) {
175 ERR_load_strings(0, TS_str_functs);
176 ERR_load_strings(0, TS_str_reasons);
177 }
178#endif
179}
diff --git a/src/lib/libcrypto/ts/ts_lib.c b/src/lib/libcrypto/ts/ts_lib.c
deleted file mode 100644
index f08bb8701d..0000000000
--- a/src/lib/libcrypto/ts/ts_lib.c
+++ /dev/null
@@ -1,150 +0,0 @@
1/* $OpenBSD: ts_lib.c,v 1.8 2014/07/11 08:44:49 jsing Exp $ */
2/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3 * project 2002.
4 */
5/* ====================================================================
6 * Copyright (c) 2006 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#include <stdio.h>
60#include <string.h>
61
62#include <openssl/bn.h>
63#include <openssl/objects.h>
64#include <openssl/ts.h>
65#include <openssl/x509v3.h>
66
67/* Local function declarations. */
68
69/* Function definitions. */
70
71int
72TS_ASN1_INTEGER_print_bio(BIO *bio, const ASN1_INTEGER *num)
73{
74 BIGNUM num_bn;
75 int result = 0;
76 char *hex;
77
78 BN_init(&num_bn);
79 ASN1_INTEGER_to_BN(num, &num_bn);
80 if ((hex = BN_bn2hex(&num_bn))) {
81 result = BIO_write(bio, "0x", 2) > 0;
82 result = result && BIO_write(bio, hex, strlen(hex)) > 0;
83 free(hex);
84 }
85 BN_free(&num_bn);
86
87 return result;
88}
89
90int
91TS_OBJ_print_bio(BIO *bio, const ASN1_OBJECT *obj)
92{
93 char obj_txt[128];
94
95 int len = OBJ_obj2txt(obj_txt, sizeof(obj_txt), obj, 0);
96 if (len >= sizeof(obj_txt))
97 len = sizeof(obj_txt) - 1;
98 BIO_write(bio, obj_txt, len);
99 BIO_write(bio, "\n", 1);
100 return 1;
101}
102
103int
104TS_ext_print_bio(BIO *bio, const STACK_OF(X509_EXTENSION) *extensions)
105{
106 int i, critical, n;
107 X509_EXTENSION *ex;
108 ASN1_OBJECT *obj;
109
110 BIO_printf(bio, "Extensions:\n");
111 n = X509v3_get_ext_count(extensions);
112 for (i = 0; i < n; i++) {
113 ex = X509v3_get_ext(extensions, i);
114 obj = X509_EXTENSION_get_object(ex);
115 i2a_ASN1_OBJECT(bio, obj);
116 critical = X509_EXTENSION_get_critical(ex);
117 BIO_printf(bio, ": %s\n", critical ? "critical" : "");
118 if (!X509V3_EXT_print(bio, ex, 0, 4)) {
119 BIO_printf(bio, "%4s", "");
120 M_ASN1_OCTET_STRING_print(bio, ex->value);
121 }
122 BIO_write(bio, "\n", 1);
123 }
124
125 return 1;
126}
127
128int
129TS_X509_ALGOR_print_bio(BIO *bio, const X509_ALGOR *alg)
130{
131 int i = OBJ_obj2nid(alg->algorithm);
132
133 return BIO_printf(bio, "Hash Algorithm: %s\n",
134 (i == NID_undef) ? "UNKNOWN" : OBJ_nid2ln(i));
135}
136
137int
138TS_MSG_IMPRINT_print_bio(BIO *bio, TS_MSG_IMPRINT *a)
139{
140 const ASN1_OCTET_STRING *msg;
141
142 TS_X509_ALGOR_print_bio(bio, TS_MSG_IMPRINT_get_algo(a));
143
144 BIO_printf(bio, "Message data:\n");
145 msg = TS_MSG_IMPRINT_get_msg(a);
146 BIO_dump_indent(bio, (const char *)M_ASN1_STRING_data(msg),
147 M_ASN1_STRING_length(msg), 4);
148
149 return 1;
150}
diff --git a/src/lib/libcrypto/ts/ts_req_print.c b/src/lib/libcrypto/ts/ts_req_print.c
deleted file mode 100644
index 64a8133a58..0000000000
--- a/src/lib/libcrypto/ts/ts_req_print.c
+++ /dev/null
@@ -1,104 +0,0 @@
1/* $OpenBSD: ts_req_print.c,v 1.4 2014/07/11 08:44:49 jsing Exp $ */
2/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3 * project 2002.
4 */
5/* ====================================================================
6 * Copyright (c) 2006 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#include <stdio.h>
60
61#include <openssl/bn.h>
62#include <openssl/objects.h>
63#include <openssl/ts.h>
64#include <openssl/x509v3.h>
65
66/* Function definitions. */
67
68int
69TS_REQ_print_bio(BIO *bio, TS_REQ *a)
70{
71 int v;
72 ASN1_OBJECT *policy_id;
73 const ASN1_INTEGER *nonce;
74
75 if (a == NULL)
76 return 0;
77
78 v = TS_REQ_get_version(a);
79 BIO_printf(bio, "Version: %d\n", v);
80
81 TS_MSG_IMPRINT_print_bio(bio, TS_REQ_get_msg_imprint(a));
82
83 BIO_printf(bio, "Policy OID: ");
84 policy_id = TS_REQ_get_policy_id(a);
85 if (policy_id == NULL)
86 BIO_printf(bio, "unspecified\n");
87 else
88 TS_OBJ_print_bio(bio, policy_id);
89
90 BIO_printf(bio, "Nonce: ");
91 nonce = TS_REQ_get_nonce(a);
92 if (nonce == NULL)
93 BIO_printf(bio, "unspecified");
94 else
95 TS_ASN1_INTEGER_print_bio(bio, nonce);
96 BIO_write(bio, "\n", 1);
97
98 BIO_printf(bio, "Certificate required: %s\n",
99 TS_REQ_get_cert_req(a) ? "yes" : "no");
100
101 TS_ext_print_bio(bio, TS_REQ_get_exts(a));
102
103 return 1;
104}
diff --git a/src/lib/libcrypto/ts/ts_req_utils.c b/src/lib/libcrypto/ts/ts_req_utils.c
deleted file mode 100644
index ab813b2b42..0000000000
--- a/src/lib/libcrypto/ts/ts_req_utils.c
+++ /dev/null
@@ -1,255 +0,0 @@
1/* $OpenBSD: ts_req_utils.c,v 1.4 2014/07/11 08:44:49 jsing Exp $ */
2/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3 * project 2002.
4 */
5/* ====================================================================
6 * Copyright (c) 2006 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#include <stdio.h>
60
61#include <openssl/err.h>
62#include <openssl/objects.h>
63#include <openssl/ts.h>
64#include <openssl/x509v3.h>
65
66int
67TS_REQ_set_version(TS_REQ *a, long version)
68{
69 return ASN1_INTEGER_set(a->version, version);
70}
71
72long
73TS_REQ_get_version(const TS_REQ *a)
74{
75 return ASN1_INTEGER_get(a->version);
76}
77
78int
79TS_REQ_set_msg_imprint(TS_REQ *a, TS_MSG_IMPRINT *msg_imprint)
80{
81 TS_MSG_IMPRINT *new_msg_imprint;
82
83 if (a->msg_imprint == msg_imprint)
84 return 1;
85 new_msg_imprint = TS_MSG_IMPRINT_dup(msg_imprint);
86 if (new_msg_imprint == NULL) {
87 TSerr(TS_F_TS_REQ_SET_MSG_IMPRINT, ERR_R_MALLOC_FAILURE);
88 return 0;
89 }
90 TS_MSG_IMPRINT_free(a->msg_imprint);
91 a->msg_imprint = new_msg_imprint;
92 return 1;
93}
94
95TS_MSG_IMPRINT *
96TS_REQ_get_msg_imprint(TS_REQ *a)
97{
98 return a->msg_imprint;
99}
100
101int
102TS_MSG_IMPRINT_set_algo(TS_MSG_IMPRINT *a, X509_ALGOR *alg)
103{
104 X509_ALGOR *new_alg;
105
106 if (a->hash_algo == alg)
107 return 1;
108 new_alg = X509_ALGOR_dup(alg);
109 if (new_alg == NULL) {
110 TSerr(TS_F_TS_MSG_IMPRINT_SET_ALGO, ERR_R_MALLOC_FAILURE);
111 return 0;
112 }
113 X509_ALGOR_free(a->hash_algo);
114 a->hash_algo = new_alg;
115 return 1;
116}
117
118X509_ALGOR *
119TS_MSG_IMPRINT_get_algo(TS_MSG_IMPRINT *a)
120{
121 return a->hash_algo;
122}
123
124int
125TS_MSG_IMPRINT_set_msg(TS_MSG_IMPRINT *a, unsigned char *d, int len)
126{
127 return ASN1_OCTET_STRING_set(a->hashed_msg, d, len);
128}
129
130ASN1_OCTET_STRING *
131TS_MSG_IMPRINT_get_msg(TS_MSG_IMPRINT *a)
132{
133 return a->hashed_msg;
134}
135
136int
137TS_REQ_set_policy_id(TS_REQ *a, ASN1_OBJECT *policy)
138{
139 ASN1_OBJECT *new_policy;
140
141 if (a->policy_id == policy)
142 return 1;
143 new_policy = OBJ_dup(policy);
144 if (new_policy == NULL) {
145 TSerr(TS_F_TS_REQ_SET_POLICY_ID, ERR_R_MALLOC_FAILURE);
146 return 0;
147 }
148 ASN1_OBJECT_free(a->policy_id);
149 a->policy_id = new_policy;
150 return 1;
151}
152
153ASN1_OBJECT *
154TS_REQ_get_policy_id(TS_REQ *a)
155{
156 return a->policy_id;
157}
158
159int
160TS_REQ_set_nonce(TS_REQ *a, const ASN1_INTEGER *nonce)
161{
162 ASN1_INTEGER *new_nonce;
163
164 if (a->nonce == nonce)
165 return 1;
166 new_nonce = ASN1_INTEGER_dup(nonce);
167 if (new_nonce == NULL) {
168 TSerr(TS_F_TS_REQ_SET_NONCE, ERR_R_MALLOC_FAILURE);
169 return 0;
170 }
171 ASN1_INTEGER_free(a->nonce);
172 a->nonce = new_nonce;
173 return 1;
174}
175
176const ASN1_INTEGER *
177TS_REQ_get_nonce(const TS_REQ *a)
178{
179 return a->nonce;
180}
181
182int
183TS_REQ_set_cert_req(TS_REQ *a, int cert_req)
184{
185 a->cert_req = cert_req ? 0xFF : 0x00;
186 return 1;
187}
188
189int
190TS_REQ_get_cert_req(const TS_REQ *a)
191{
192 return a->cert_req ? 1 : 0;
193}
194
195STACK_OF(X509_EXTENSION) *TS_REQ_get_exts(TS_REQ *a)
196{
197 return a->extensions;
198}
199
200void
201TS_REQ_ext_free(TS_REQ *a)
202{
203 if (!a)
204 return;
205 sk_X509_EXTENSION_pop_free(a->extensions, X509_EXTENSION_free);
206 a->extensions = NULL;
207}
208
209int
210TS_REQ_get_ext_count(TS_REQ *a)
211{
212 return X509v3_get_ext_count(a->extensions);
213}
214
215int
216TS_REQ_get_ext_by_NID(TS_REQ *a, int nid, int lastpos)
217{
218 return X509v3_get_ext_by_NID(a->extensions, nid, lastpos);
219}
220
221int
222TS_REQ_get_ext_by_OBJ(TS_REQ *a, ASN1_OBJECT *obj, int lastpos)
223{
224 return X509v3_get_ext_by_OBJ(a->extensions, obj, lastpos);
225}
226
227int
228TS_REQ_get_ext_by_critical(TS_REQ *a, int crit, int lastpos)
229{
230 return X509v3_get_ext_by_critical(a->extensions, crit, lastpos);
231}
232
233X509_EXTENSION *
234TS_REQ_get_ext(TS_REQ *a, int loc)
235{
236 return X509v3_get_ext(a->extensions, loc);
237}
238
239X509_EXTENSION *
240TS_REQ_delete_ext(TS_REQ *a, int loc)
241{
242 return X509v3_delete_ext(a->extensions, loc);
243}
244
245int
246TS_REQ_add_ext(TS_REQ *a, X509_EXTENSION *ex, int loc)
247{
248 return X509v3_add_ext(&a->extensions, ex, loc) != NULL;
249}
250
251void *
252TS_REQ_get_ext_d2i(TS_REQ *a, int nid, int *crit, int *idx)
253{
254 return X509V3_get_d2i(a->extensions, nid, crit, idx);
255}
diff --git a/src/lib/libcrypto/ts/ts_rsp_print.c b/src/lib/libcrypto/ts/ts_rsp_print.c
deleted file mode 100644
index c442b71646..0000000000
--- a/src/lib/libcrypto/ts/ts_rsp_print.c
+++ /dev/null
@@ -1,301 +0,0 @@
1/* $OpenBSD: ts_rsp_print.c,v 1.5 2014/07/11 08:44:49 jsing Exp $ */
2/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3 * project 2002.
4 */
5/* ====================================================================
6 * Copyright (c) 2006 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#include <stdio.h>
60
61#include <openssl/bn.h>
62#include <openssl/objects.h>
63#include <openssl/ts.h>
64#include <openssl/x509v3.h>
65
66struct status_map_st {
67 int bit;
68 const char *text;
69};
70
71/* Local function declarations. */
72
73static int TS_status_map_print(BIO *bio, struct status_map_st *a,
74 ASN1_BIT_STRING *v);
75static int TS_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *accuracy);
76
77/* Function definitions. */
78
79int
80TS_RESP_print_bio(BIO *bio, TS_RESP *a)
81{
82 TS_TST_INFO *tst_info;
83
84 BIO_printf(bio, "Status info:\n");
85 TS_STATUS_INFO_print_bio(bio, TS_RESP_get_status_info(a));
86
87 BIO_printf(bio, "\nTST info:\n");
88 tst_info = TS_RESP_get_tst_info(a);
89 if (tst_info != NULL)
90 TS_TST_INFO_print_bio(bio, TS_RESP_get_tst_info(a));
91 else
92 BIO_printf(bio, "Not included.\n");
93
94 return 1;
95}
96
97int
98TS_STATUS_INFO_print_bio(BIO *bio, TS_STATUS_INFO *a)
99{
100 static const char *status_map[] = {
101 "Granted.",
102 "Granted with modifications.",
103 "Rejected.",
104 "Waiting.",
105 "Revocation warning.",
106 "Revoked."
107 };
108 static struct status_map_st failure_map[] = {
109 {
110 TS_INFO_BAD_ALG,
111 "unrecognized or unsupported algorithm identifier"
112 },
113 {
114 TS_INFO_BAD_REQUEST,
115 "transaction not permitted or supported"
116 },
117 {
118 TS_INFO_BAD_DATA_FORMAT,
119 "the data submitted has the wrong format"
120 },
121 {
122 TS_INFO_TIME_NOT_AVAILABLE,
123 "the TSA's time source is not available"
124 },
125 {
126 TS_INFO_UNACCEPTED_POLICY,
127 "the requested TSA policy is not supported by the TSA"
128 },
129 {
130 TS_INFO_UNACCEPTED_EXTENSION,
131 "the requested extension is not supported by the TSA"
132 },
133 {
134 TS_INFO_ADD_INFO_NOT_AVAILABLE,
135 "the additional information requested could not be understood "
136 "or is not available"
137 },
138 {
139 TS_INFO_SYSTEM_FAILURE,
140 "the request cannot be handled due to system failure"
141 },
142 { -1, NULL }
143 };
144 long status;
145 int i, lines = 0;
146
147 /* Printing status code. */
148 BIO_printf(bio, "Status: ");
149 status = ASN1_INTEGER_get(a->status);
150 if (0 <= status &&
151 status < (long)(sizeof(status_map) / sizeof(status_map[0])))
152 BIO_printf(bio, "%s\n", status_map[status]);
153 else
154 BIO_printf(bio, "out of bounds\n");
155
156 /* Printing status description. */
157 BIO_printf(bio, "Status description: ");
158 for (i = 0; i < sk_ASN1_UTF8STRING_num(a->text); ++i) {
159 if (i > 0)
160 BIO_puts(bio, "\t");
161 ASN1_STRING_print_ex(bio, sk_ASN1_UTF8STRING_value(a->text, i),
162 0);
163 BIO_puts(bio, "\n");
164 }
165 if (i == 0)
166 BIO_printf(bio, "unspecified\n");
167
168 /* Printing failure information. */
169 BIO_printf(bio, "Failure info: ");
170 if (a->failure_info != NULL)
171 lines = TS_status_map_print(bio, failure_map, a->failure_info);
172 if (lines == 0)
173 BIO_printf(bio, "unspecified");
174 BIO_printf(bio, "\n");
175
176 return 1;
177}
178
179static int
180TS_status_map_print(BIO *bio, struct status_map_st *a, ASN1_BIT_STRING *v)
181{
182 int lines = 0;
183
184 for (; a->bit >= 0; ++a) {
185 if (ASN1_BIT_STRING_get_bit(v, a->bit)) {
186 if (++lines > 1)
187 BIO_printf(bio, ", ");
188 BIO_printf(bio, "%s", a->text);
189 }
190 }
191
192 return lines;
193}
194
195int
196TS_TST_INFO_print_bio(BIO *bio, TS_TST_INFO *a)
197{
198 int v;
199 ASN1_OBJECT *policy_id;
200 const ASN1_INTEGER *serial;
201 const ASN1_GENERALIZEDTIME *gtime;
202 TS_ACCURACY *accuracy;
203 const ASN1_INTEGER *nonce;
204 GENERAL_NAME *tsa_name;
205
206 if (a == NULL)
207 return 0;
208
209 /* Print version. */
210 v = TS_TST_INFO_get_version(a);
211 BIO_printf(bio, "Version: %d\n", v);
212
213 /* Print policy id. */
214 BIO_printf(bio, "Policy OID: ");
215 policy_id = TS_TST_INFO_get_policy_id(a);
216 TS_OBJ_print_bio(bio, policy_id);
217
218 /* Print message imprint. */
219 TS_MSG_IMPRINT_print_bio(bio, TS_TST_INFO_get_msg_imprint(a));
220
221 /* Print serial number. */
222 BIO_printf(bio, "Serial number: ");
223 serial = TS_TST_INFO_get_serial(a);
224 if (serial == NULL)
225 BIO_printf(bio, "unspecified");
226 else
227 TS_ASN1_INTEGER_print_bio(bio, serial);
228 BIO_write(bio, "\n", 1);
229
230 /* Print time stamp. */
231 BIO_printf(bio, "Time stamp: ");
232 gtime = TS_TST_INFO_get_time(a);
233 ASN1_GENERALIZEDTIME_print(bio, gtime);
234 BIO_write(bio, "\n", 1);
235
236 /* Print accuracy. */
237 BIO_printf(bio, "Accuracy: ");
238 accuracy = TS_TST_INFO_get_accuracy(a);
239 if (accuracy == NULL)
240 BIO_printf(bio, "unspecified");
241 else
242 TS_ACCURACY_print_bio(bio, accuracy);
243 BIO_write(bio, "\n", 1);
244
245 /* Print ordering. */
246 BIO_printf(bio, "Ordering: %s\n",
247 TS_TST_INFO_get_ordering(a) ? "yes" : "no");
248
249 /* Print nonce. */
250 BIO_printf(bio, "Nonce: ");
251 nonce = TS_TST_INFO_get_nonce(a);
252 if (nonce == NULL)
253 BIO_printf(bio, "unspecified");
254 else
255 TS_ASN1_INTEGER_print_bio(bio, nonce);
256 BIO_write(bio, "\n", 1);
257
258 /* Print TSA name. */
259 BIO_printf(bio, "TSA: ");
260 tsa_name = TS_TST_INFO_get_tsa(a);
261 if (tsa_name == NULL)
262 BIO_printf(bio, "unspecified");
263 else {
264 STACK_OF(CONF_VALUE) *nval;
265 if ((nval = i2v_GENERAL_NAME(NULL, tsa_name, NULL)))
266 X509V3_EXT_val_prn(bio, nval, 0, 0);
267 sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
268 }
269 BIO_write(bio, "\n", 1);
270
271 /* Print extensions. */
272 TS_ext_print_bio(bio, TS_TST_INFO_get_exts(a));
273
274 return 1;
275}
276
277static int
278TS_ACCURACY_print_bio(BIO *bio, const TS_ACCURACY *accuracy)
279{
280 const ASN1_INTEGER *seconds = TS_ACCURACY_get_seconds(accuracy);
281 const ASN1_INTEGER *millis = TS_ACCURACY_get_millis(accuracy);
282 const ASN1_INTEGER *micros = TS_ACCURACY_get_micros(accuracy);
283
284 if (seconds != NULL)
285 TS_ASN1_INTEGER_print_bio(bio, seconds);
286 else
287 BIO_printf(bio, "unspecified");
288 BIO_printf(bio, " seconds, ");
289 if (millis != NULL)
290 TS_ASN1_INTEGER_print_bio(bio, millis);
291 else
292 BIO_printf(bio, "unspecified");
293 BIO_printf(bio, " millis, ");
294 if (micros != NULL)
295 TS_ASN1_INTEGER_print_bio(bio, micros);
296 else
297 BIO_printf(bio, "unspecified");
298 BIO_printf(bio, " micros");
299
300 return 1;
301}
diff --git a/src/lib/libcrypto/ts/ts_rsp_sign.c b/src/lib/libcrypto/ts/ts_rsp_sign.c
deleted file mode 100644
index 27659e8f45..0000000000
--- a/src/lib/libcrypto/ts/ts_rsp_sign.c
+++ /dev/null
@@ -1,1023 +0,0 @@
1/* $OpenBSD: ts_rsp_sign.c,v 1.18 2014/07/12 16:03:37 miod Exp $ */
2/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3 * project 2002.
4 */
5/* ====================================================================
6 * Copyright (c) 2006 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#include <sys/time.h>
60
61#include <string.h>
62
63#include <openssl/err.h>
64#include <openssl/objects.h>
65#include <openssl/pkcs7.h>
66#include <openssl/ts.h>
67
68/* Private function declarations. */
69
70static ASN1_INTEGER *def_serial_cb(struct TS_resp_ctx *, void *);
71static int def_time_cb(struct TS_resp_ctx *, void *, time_t *sec, long *usec);
72static int def_extension_cb(struct TS_resp_ctx *, X509_EXTENSION *, void *);
73
74static void TS_RESP_CTX_init(TS_RESP_CTX *ctx);
75static void TS_RESP_CTX_cleanup(TS_RESP_CTX *ctx);
76static int TS_RESP_check_request(TS_RESP_CTX *ctx);
77static ASN1_OBJECT *TS_RESP_get_policy(TS_RESP_CTX *ctx);
78static TS_TST_INFO *TS_RESP_create_tst_info(TS_RESP_CTX *ctx,
79 ASN1_OBJECT *policy);
80static int TS_RESP_process_extensions(TS_RESP_CTX *ctx);
81static int TS_RESP_sign(TS_RESP_CTX *ctx);
82
83static ESS_SIGNING_CERT *ESS_SIGNING_CERT_new_init(X509 *signcert,
84 STACK_OF(X509) *certs);
85static ESS_CERT_ID *ESS_CERT_ID_new_init(X509 *cert, int issuer_needed);
86static int TS_TST_INFO_content_new(PKCS7 *p7);
87static int ESS_add_signing_cert(PKCS7_SIGNER_INFO *si, ESS_SIGNING_CERT *sc);
88
89static ASN1_GENERALIZEDTIME *TS_RESP_set_genTime_with_precision(
90 ASN1_GENERALIZEDTIME *, time_t, long, unsigned);
91
92/* Default callbacks for response generation. */
93
94static ASN1_INTEGER *
95def_serial_cb(struct TS_resp_ctx *ctx, void *data)
96{
97 ASN1_INTEGER *serial = ASN1_INTEGER_new();
98
99 if (!serial)
100 goto err;
101 if (!ASN1_INTEGER_set(serial, 1))
102 goto err;
103 return serial;
104
105err:
106 TSerr(TS_F_DEF_SERIAL_CB, ERR_R_MALLOC_FAILURE);
107 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
108 "Error during serial number generation.");
109 return NULL;
110}
111
112/* Use the gettimeofday function call. */
113static int
114def_time_cb(struct TS_resp_ctx *ctx, void *data, time_t *sec, long *usec)
115{
116 struct timeval tv;
117
118 if (gettimeofday(&tv, NULL) != 0) {
119 TSerr(TS_F_DEF_TIME_CB, TS_R_TIME_SYSCALL_ERROR);
120 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
121 "Time is not available.");
122 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_TIME_NOT_AVAILABLE);
123 return 0;
124 }
125 /* Return time to caller. */
126 *sec = tv.tv_sec;
127 *usec = tv.tv_usec;
128
129 return 1;
130}
131
132static int
133def_extension_cb(struct TS_resp_ctx *ctx, X509_EXTENSION *ext, void *data)
134{
135 /* No extensions are processed here. */
136 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
137 "Unsupported extension.");
138 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_UNACCEPTED_EXTENSION);
139 return 0;
140}
141
142/* TS_RESP_CTX management functions. */
143
144TS_RESP_CTX *
145TS_RESP_CTX_new(void)
146{
147 TS_RESP_CTX *ctx;
148
149 if (!(ctx = calloc(1, sizeof(TS_RESP_CTX)))) {
150 TSerr(TS_F_TS_RESP_CTX_NEW, ERR_R_MALLOC_FAILURE);
151 return NULL;
152 }
153
154 /* Setting default callbacks. */
155 ctx->serial_cb = def_serial_cb;
156 ctx->time_cb = def_time_cb;
157 ctx->extension_cb = def_extension_cb;
158
159 return ctx;
160}
161
162void
163TS_RESP_CTX_free(TS_RESP_CTX *ctx)
164{
165 if (!ctx)
166 return;
167
168 X509_free(ctx->signer_cert);
169 EVP_PKEY_free(ctx->signer_key);
170 sk_X509_pop_free(ctx->certs, X509_free);
171 sk_ASN1_OBJECT_pop_free(ctx->policies, ASN1_OBJECT_free);
172 ASN1_OBJECT_free(ctx->default_policy);
173 sk_EVP_MD_free(ctx->mds); /* No EVP_MD_free method exists. */
174 ASN1_INTEGER_free(ctx->seconds);
175 ASN1_INTEGER_free(ctx->millis);
176 ASN1_INTEGER_free(ctx->micros);
177 free(ctx);
178}
179
180int
181TS_RESP_CTX_set_signer_cert(TS_RESP_CTX *ctx, X509 *signer)
182{
183 if (X509_check_purpose(signer, X509_PURPOSE_TIMESTAMP_SIGN, 0) != 1) {
184 TSerr(TS_F_TS_RESP_CTX_SET_SIGNER_CERT,
185 TS_R_INVALID_SIGNER_CERTIFICATE_PURPOSE);
186 return 0;
187 }
188 if (ctx->signer_cert)
189 X509_free(ctx->signer_cert);
190 ctx->signer_cert = signer;
191 CRYPTO_add(&ctx->signer_cert->references, +1, CRYPTO_LOCK_X509);
192 return 1;
193}
194
195int
196TS_RESP_CTX_set_signer_key(TS_RESP_CTX *ctx, EVP_PKEY *key)
197{
198 EVP_PKEY_free(ctx->signer_key);
199 ctx->signer_key = key;
200 CRYPTO_add(&ctx->signer_key->references, +1, CRYPTO_LOCK_EVP_PKEY);
201
202 return 1;
203}
204
205int
206TS_RESP_CTX_set_def_policy(TS_RESP_CTX *ctx, ASN1_OBJECT *def_policy)
207{
208 if (ctx->default_policy)
209 ASN1_OBJECT_free(ctx->default_policy);
210 if (!(ctx->default_policy = OBJ_dup(def_policy)))
211 goto err;
212 return 1;
213
214err:
215 TSerr(TS_F_TS_RESP_CTX_SET_DEF_POLICY, ERR_R_MALLOC_FAILURE);
216 return 0;
217}
218
219int
220TS_RESP_CTX_set_certs(TS_RESP_CTX *ctx, STACK_OF(X509) *certs)
221{
222 int i;
223
224 if (ctx->certs) {
225 sk_X509_pop_free(ctx->certs, X509_free);
226 ctx->certs = NULL;
227 }
228 if (!certs)
229 return 1;
230 if (!(ctx->certs = sk_X509_dup(certs))) {
231 TSerr(TS_F_TS_RESP_CTX_SET_CERTS, ERR_R_MALLOC_FAILURE);
232 return 0;
233 }
234 for (i = 0; i < sk_X509_num(ctx->certs); ++i) {
235 X509 *cert = sk_X509_value(ctx->certs, i);
236 CRYPTO_add(&cert->references, +1, CRYPTO_LOCK_X509);
237 }
238
239 return 1;
240}
241
242int
243TS_RESP_CTX_add_policy(TS_RESP_CTX *ctx, ASN1_OBJECT *policy)
244{
245 ASN1_OBJECT *copy = NULL;
246
247 /* Create new policy stack if necessary. */
248 if (!ctx->policies && !(ctx->policies = sk_ASN1_OBJECT_new_null()))
249 goto err;
250 if (!(copy = OBJ_dup(policy)))
251 goto err;
252 if (!sk_ASN1_OBJECT_push(ctx->policies, copy))
253 goto err;
254
255 return 1;
256
257err:
258 TSerr(TS_F_TS_RESP_CTX_ADD_POLICY, ERR_R_MALLOC_FAILURE);
259 ASN1_OBJECT_free(copy);
260 return 0;
261}
262
263int
264TS_RESP_CTX_add_md(TS_RESP_CTX *ctx, const EVP_MD *md)
265{
266 /* Create new md stack if necessary. */
267 if (!ctx->mds && !(ctx->mds = sk_EVP_MD_new_null()))
268 goto err;
269 /* Add the shared md, no copy needed. */
270 if (!sk_EVP_MD_push(ctx->mds, (EVP_MD *)md))
271 goto err;
272
273 return 1;
274
275err:
276 TSerr(TS_F_TS_RESP_CTX_ADD_MD, ERR_R_MALLOC_FAILURE);
277 return 0;
278}
279
280#define TS_RESP_CTX_accuracy_free(ctx) \
281 ASN1_INTEGER_free(ctx->seconds); \
282 ctx->seconds = NULL; \
283 ASN1_INTEGER_free(ctx->millis); \
284 ctx->millis = NULL; \
285 ASN1_INTEGER_free(ctx->micros); \
286 ctx->micros = NULL;
287
288int
289TS_RESP_CTX_set_accuracy(TS_RESP_CTX *ctx, int secs, int millis, int micros)
290{
291 TS_RESP_CTX_accuracy_free(ctx);
292 if (secs && (!(ctx->seconds = ASN1_INTEGER_new()) ||
293 !ASN1_INTEGER_set(ctx->seconds, secs)))
294 goto err;
295 if (millis && (!(ctx->millis = ASN1_INTEGER_new()) ||
296 !ASN1_INTEGER_set(ctx->millis, millis)))
297 goto err;
298 if (micros && (!(ctx->micros = ASN1_INTEGER_new()) ||
299 !ASN1_INTEGER_set(ctx->micros, micros)))
300 goto err;
301
302 return 1;
303
304err:
305 TS_RESP_CTX_accuracy_free(ctx);
306 TSerr(TS_F_TS_RESP_CTX_SET_ACCURACY, ERR_R_MALLOC_FAILURE);
307 return 0;
308}
309
310void
311TS_RESP_CTX_add_flags(TS_RESP_CTX *ctx, int flags)
312{
313 ctx->flags |= flags;
314}
315
316void
317TS_RESP_CTX_set_serial_cb(TS_RESP_CTX *ctx, TS_serial_cb cb, void *data)
318{
319 ctx->serial_cb = cb;
320 ctx->serial_cb_data = data;
321}
322
323void
324TS_RESP_CTX_set_extension_cb(TS_RESP_CTX *ctx, TS_extension_cb cb, void *data)
325{
326 ctx->extension_cb = cb;
327 ctx->extension_cb_data = data;
328}
329
330int
331TS_RESP_CTX_set_status_info(TS_RESP_CTX *ctx, int status, const char *text)
332{
333 TS_STATUS_INFO *si = NULL;
334 ASN1_UTF8STRING *utf8_text = NULL;
335 int ret = 0;
336
337 if (!(si = TS_STATUS_INFO_new()))
338 goto err;
339 if (!ASN1_INTEGER_set(si->status, status))
340 goto err;
341 if (text) {
342 if (!(utf8_text = ASN1_UTF8STRING_new()) ||
343 !ASN1_STRING_set(utf8_text, text, strlen(text)))
344 goto err;
345 if (!si->text && !(si->text = sk_ASN1_UTF8STRING_new_null()))
346 goto err;
347 if (!sk_ASN1_UTF8STRING_push(si->text, utf8_text))
348 goto err;
349 utf8_text = NULL; /* Ownership is lost. */
350 }
351 if (!TS_RESP_set_status_info(ctx->response, si))
352 goto err;
353 ret = 1;
354
355err:
356 if (!ret)
357 TSerr(TS_F_TS_RESP_CTX_SET_STATUS_INFO, ERR_R_MALLOC_FAILURE);
358 TS_STATUS_INFO_free(si);
359 ASN1_UTF8STRING_free(utf8_text);
360 return ret;
361}
362
363int
364TS_RESP_CTX_set_status_info_cond(TS_RESP_CTX *ctx, int status, const char *text)
365{
366 int ret = 1;
367 TS_STATUS_INFO *si = TS_RESP_get_status_info(ctx->response);
368
369 if (ASN1_INTEGER_get(si->status) == TS_STATUS_GRANTED) {
370 /* Status has not been set, set it now. */
371 ret = TS_RESP_CTX_set_status_info(ctx, status, text);
372 }
373 return ret;
374}
375
376int
377TS_RESP_CTX_add_failure_info(TS_RESP_CTX *ctx, int failure)
378{
379 TS_STATUS_INFO *si = TS_RESP_get_status_info(ctx->response);
380
381 if (!si->failure_info && !(si->failure_info = ASN1_BIT_STRING_new()))
382 goto err;
383 if (!ASN1_BIT_STRING_set_bit(si->failure_info, failure, 1))
384 goto err;
385 return 1;
386
387err:
388 TSerr(TS_F_TS_RESP_CTX_ADD_FAILURE_INFO, ERR_R_MALLOC_FAILURE);
389 return 0;
390}
391
392TS_REQ *
393TS_RESP_CTX_get_request(TS_RESP_CTX *ctx)
394{
395 return ctx->request;
396}
397
398TS_TST_INFO *
399TS_RESP_CTX_get_tst_info(TS_RESP_CTX *ctx)
400{
401 return ctx->tst_info;
402}
403
404int
405TS_RESP_CTX_set_clock_precision_digits(TS_RESP_CTX *ctx, unsigned precision)
406{
407 if (precision > TS_MAX_CLOCK_PRECISION_DIGITS)
408 return 0;
409 ctx->clock_precision_digits = precision;
410 return 1;
411}
412
413/* Main entry method of the response generation. */
414TS_RESP *
415TS_RESP_create_response(TS_RESP_CTX *ctx, BIO *req_bio)
416{
417 ASN1_OBJECT *policy;
418 TS_RESP *response;
419 int result = 0;
420
421 TS_RESP_CTX_init(ctx);
422
423 /* Creating the response object. */
424 if (!(ctx->response = TS_RESP_new())) {
425 TSerr(TS_F_TS_RESP_CREATE_RESPONSE, ERR_R_MALLOC_FAILURE);
426 goto end;
427 }
428
429 /* Parsing DER request. */
430 if (!(ctx->request = d2i_TS_REQ_bio(req_bio, NULL))) {
431 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
432 "Bad request format or "
433 "system error.");
434 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_DATA_FORMAT);
435 goto end;
436 }
437
438 /* Setting default status info. */
439 if (!TS_RESP_CTX_set_status_info(ctx, TS_STATUS_GRANTED, NULL))
440 goto end;
441
442 /* Checking the request format. */
443 if (!TS_RESP_check_request(ctx))
444 goto end;
445
446 /* Checking acceptable policies. */
447 if (!(policy = TS_RESP_get_policy(ctx)))
448 goto end;
449
450 /* Creating the TS_TST_INFO object. */
451 if (!(ctx->tst_info = TS_RESP_create_tst_info(ctx, policy)))
452 goto end;
453
454 /* Processing extensions. */
455 if (!TS_RESP_process_extensions(ctx))
456 goto end;
457
458 /* Generating the signature. */
459 if (!TS_RESP_sign(ctx))
460 goto end;
461
462 /* Everything was successful. */
463 result = 1;
464
465end:
466 if (!result) {
467 TSerr(TS_F_TS_RESP_CREATE_RESPONSE, TS_R_RESPONSE_SETUP_ERROR);
468 if (ctx->response != NULL) {
469 if (TS_RESP_CTX_set_status_info_cond(ctx,
470 TS_STATUS_REJECTION, "Error during response "
471 "generation.") == 0) {
472 TS_RESP_free(ctx->response);
473 ctx->response = NULL;
474 }
475 }
476 }
477 response = ctx->response;
478 ctx->response = NULL; /* Ownership will be returned to caller. */
479 TS_RESP_CTX_cleanup(ctx);
480 return response;
481}
482
483/* Initializes the variable part of the context. */
484static void
485TS_RESP_CTX_init(TS_RESP_CTX *ctx)
486{
487 ctx->request = NULL;
488 ctx->response = NULL;
489 ctx->tst_info = NULL;
490}
491
492/* Cleans up the variable part of the context. */
493static void
494TS_RESP_CTX_cleanup(TS_RESP_CTX *ctx)
495{
496 TS_REQ_free(ctx->request);
497 ctx->request = NULL;
498 TS_RESP_free(ctx->response);
499 ctx->response = NULL;
500 TS_TST_INFO_free(ctx->tst_info);
501 ctx->tst_info = NULL;
502}
503
504/* Checks the format and content of the request. */
505static int
506TS_RESP_check_request(TS_RESP_CTX *ctx)
507{
508 TS_REQ *request = ctx->request;
509 TS_MSG_IMPRINT *msg_imprint;
510 X509_ALGOR *md_alg;
511 int md_alg_id;
512 const ASN1_OCTET_STRING *digest;
513 EVP_MD *md = NULL;
514 int i;
515
516 /* Checking request version. */
517 if (TS_REQ_get_version(request) != 1) {
518 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
519 "Bad request version.");
520 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_REQUEST);
521 return 0;
522 }
523
524 /* Checking message digest algorithm. */
525 msg_imprint = TS_REQ_get_msg_imprint(request);
526 md_alg = TS_MSG_IMPRINT_get_algo(msg_imprint);
527 md_alg_id = OBJ_obj2nid(md_alg->algorithm);
528 for (i = 0; !md && i < sk_EVP_MD_num(ctx->mds); ++i) {
529 EVP_MD *current_md = sk_EVP_MD_value(ctx->mds, i);
530 if (md_alg_id == EVP_MD_type(current_md))
531 md = current_md;
532 }
533 if (!md) {
534 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
535 "Message digest algorithm is "
536 "not supported.");
537 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_ALG);
538 return 0;
539 }
540
541 /* No message digest takes parameter. */
542 if (md_alg->parameter &&
543 ASN1_TYPE_get(md_alg->parameter) != V_ASN1_NULL) {
544 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
545 "Superfluous message digest "
546 "parameter.");
547 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_ALG);
548 return 0;
549 }
550 /* Checking message digest size. */
551 digest = TS_MSG_IMPRINT_get_msg(msg_imprint);
552 if (digest->length != EVP_MD_size(md)) {
553 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
554 "Bad message digest.");
555 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_BAD_DATA_FORMAT);
556 return 0;
557 }
558
559 return 1;
560}
561
562/* Returns the TSA policy based on the requested and acceptable policies. */
563static ASN1_OBJECT *
564TS_RESP_get_policy(TS_RESP_CTX *ctx)
565{
566 ASN1_OBJECT *requested = TS_REQ_get_policy_id(ctx->request);
567 ASN1_OBJECT *policy = NULL;
568 int i;
569
570 if (ctx->default_policy == NULL) {
571 TSerr(TS_F_TS_RESP_GET_POLICY, TS_R_INVALID_NULL_POINTER);
572 return NULL;
573 }
574 /* Return the default policy if none is requested or the default is
575 requested. */
576 if (!requested || !OBJ_cmp(requested, ctx->default_policy))
577 policy = ctx->default_policy;
578
579 /* Check if the policy is acceptable. */
580 for (i = 0; !policy && i < sk_ASN1_OBJECT_num(ctx->policies); ++i) {
581 ASN1_OBJECT *current = sk_ASN1_OBJECT_value(ctx->policies, i);
582 if (!OBJ_cmp(requested, current))
583 policy = current;
584 }
585 if (!policy) {
586 TSerr(TS_F_TS_RESP_GET_POLICY, TS_R_UNACCEPTABLE_POLICY);
587 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
588 "Requested policy is not "
589 "supported.");
590 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_UNACCEPTED_POLICY);
591 }
592 return policy;
593}
594
595/* Creates the TS_TST_INFO object based on the settings of the context. */
596static TS_TST_INFO *
597TS_RESP_create_tst_info(TS_RESP_CTX *ctx, ASN1_OBJECT *policy)
598{
599 int result = 0;
600 TS_TST_INFO *tst_info = NULL;
601 ASN1_INTEGER *serial = NULL;
602 ASN1_GENERALIZEDTIME *asn1_time = NULL;
603 time_t sec;
604 long usec;
605 TS_ACCURACY *accuracy = NULL;
606 const ASN1_INTEGER *nonce;
607 GENERAL_NAME *tsa_name = NULL;
608
609 if (!(tst_info = TS_TST_INFO_new()))
610 goto end;
611 if (!TS_TST_INFO_set_version(tst_info, 1))
612 goto end;
613 if (!TS_TST_INFO_set_policy_id(tst_info, policy))
614 goto end;
615 if (!TS_TST_INFO_set_msg_imprint(tst_info, ctx->request->msg_imprint))
616 goto end;
617 if (!(serial = (*ctx->serial_cb)(ctx, ctx->serial_cb_data)) ||
618 !TS_TST_INFO_set_serial(tst_info, serial))
619 goto end;
620 if (!(*ctx->time_cb)(ctx, ctx->time_cb_data, &sec, &usec) ||
621 !(asn1_time = TS_RESP_set_genTime_with_precision(NULL, sec, usec,
622 ctx->clock_precision_digits)) ||
623 !TS_TST_INFO_set_time(tst_info, asn1_time))
624 goto end;
625
626 /* Setting accuracy if needed. */
627 if ((ctx->seconds || ctx->millis || ctx->micros) &&
628 !(accuracy = TS_ACCURACY_new()))
629 goto end;
630
631 if (ctx->seconds && !TS_ACCURACY_set_seconds(accuracy, ctx->seconds))
632 goto end;
633 if (ctx->millis && !TS_ACCURACY_set_millis(accuracy, ctx->millis))
634 goto end;
635 if (ctx->micros && !TS_ACCURACY_set_micros(accuracy, ctx->micros))
636 goto end;
637 if (accuracy && !TS_TST_INFO_set_accuracy(tst_info, accuracy))
638 goto end;
639
640 /* Setting ordering. */
641 if ((ctx->flags & TS_ORDERING) &&
642 !TS_TST_INFO_set_ordering(tst_info, 1))
643 goto end;
644
645 /* Setting nonce if needed. */
646 if ((nonce = TS_REQ_get_nonce(ctx->request)) != NULL &&
647 !TS_TST_INFO_set_nonce(tst_info, nonce))
648 goto end;
649
650 /* Setting TSA name to subject of signer certificate. */
651 if (ctx->flags & TS_TSA_NAME) {
652 if (!(tsa_name = GENERAL_NAME_new()))
653 goto end;
654 tsa_name->type = GEN_DIRNAME;
655 tsa_name->d.dirn =
656 X509_NAME_dup(ctx->signer_cert->cert_info->subject);
657 if (!tsa_name->d.dirn)
658 goto end;
659 if (!TS_TST_INFO_set_tsa(tst_info, tsa_name))
660 goto end;
661 }
662
663 result = 1;
664
665end:
666 if (!result) {
667 TS_TST_INFO_free(tst_info);
668 tst_info = NULL;
669 TSerr(TS_F_TS_RESP_CREATE_TST_INFO, TS_R_TST_INFO_SETUP_ERROR);
670 TS_RESP_CTX_set_status_info_cond(ctx, TS_STATUS_REJECTION,
671 "Error during TSTInfo "
672 "generation.");
673 }
674 GENERAL_NAME_free(tsa_name);
675 TS_ACCURACY_free(accuracy);
676 ASN1_GENERALIZEDTIME_free(asn1_time);
677 ASN1_INTEGER_free(serial);
678
679 return tst_info;
680}
681
682/* Processing the extensions of the request. */
683static int
684TS_RESP_process_extensions(TS_RESP_CTX *ctx)
685{
686 STACK_OF(X509_EXTENSION) *exts = TS_REQ_get_exts(ctx->request);
687 int i;
688 int ok = 1;
689
690 for (i = 0; ok && i < sk_X509_EXTENSION_num(exts); ++i) {
691 X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
692 /* XXXXX The last argument was previously
693 (void *)ctx->extension_cb, but ISO C doesn't permit
694 converting a function pointer to void *. For lack of
695 better information, I'm placing a NULL there instead.
696 The callback can pick its own address out from the ctx
697 anyway...
698 */
699 ok = (*ctx->extension_cb)(ctx, ext, NULL);
700 }
701
702 return ok;
703}
704
705/* Functions for signing the TS_TST_INFO structure of the context. */
706static int
707TS_RESP_sign(TS_RESP_CTX *ctx)
708{
709 int ret = 0;
710 PKCS7 *p7 = NULL;
711 PKCS7_SIGNER_INFO *si;
712 STACK_OF(X509) *certs; /* Certificates to include in sc. */
713 ESS_SIGNING_CERT *sc = NULL;
714 ASN1_OBJECT *oid;
715 BIO *p7bio = NULL;
716 int i;
717
718 /* Check if signcert and pkey match. */
719 if (!X509_check_private_key(ctx->signer_cert, ctx->signer_key)) {
720 TSerr(TS_F_TS_RESP_SIGN,
721 TS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
722 goto err;
723 }
724
725 /* Create a new PKCS7 signed object. */
726 if (!(p7 = PKCS7_new())) {
727 TSerr(TS_F_TS_RESP_SIGN, ERR_R_MALLOC_FAILURE);
728 goto err;
729 }
730 if (!PKCS7_set_type(p7, NID_pkcs7_signed))
731 goto err;
732
733 /* Force SignedData version to be 3 instead of the default 1. */
734 if (!ASN1_INTEGER_set(p7->d.sign->version, 3))
735 goto err;
736
737 /* Add signer certificate and optional certificate chain. */
738 if (TS_REQ_get_cert_req(ctx->request)) {
739 PKCS7_add_certificate(p7, ctx->signer_cert);
740 if (ctx->certs) {
741 for (i = 0; i < sk_X509_num(ctx->certs); ++i) {
742 X509 *cert = sk_X509_value(ctx->certs, i);
743 PKCS7_add_certificate(p7, cert);
744 }
745 }
746 }
747
748 /* Add a new signer info. */
749 if (!(si = PKCS7_add_signature(p7, ctx->signer_cert,
750 ctx->signer_key, EVP_sha1()))) {
751 TSerr(TS_F_TS_RESP_SIGN, TS_R_PKCS7_ADD_SIGNATURE_ERROR);
752 goto err;
753 }
754
755 /* Add content type signed attribute to the signer info. */
756 oid = OBJ_nid2obj(NID_id_smime_ct_TSTInfo);
757 if (!PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
758 V_ASN1_OBJECT, oid)) {
759 TSerr(TS_F_TS_RESP_SIGN, TS_R_PKCS7_ADD_SIGNED_ATTR_ERROR);
760 goto err;
761 }
762
763 /* Create the ESS SigningCertificate attribute which contains
764 the signer certificate id and optionally the certificate chain. */
765 certs = ctx->flags & TS_ESS_CERT_ID_CHAIN ? ctx->certs : NULL;
766 if (!(sc = ESS_SIGNING_CERT_new_init(ctx->signer_cert, certs)))
767 goto err;
768
769 /* Add SigningCertificate signed attribute to the signer info. */
770 if (!ESS_add_signing_cert(si, sc)) {
771 TSerr(TS_F_TS_RESP_SIGN, TS_R_ESS_ADD_SIGNING_CERT_ERROR);
772 goto err;
773 }
774
775 /* Add a new empty NID_id_smime_ct_TSTInfo encapsulated content. */
776 if (!TS_TST_INFO_content_new(p7))
777 goto err;
778
779 /* Add the DER encoded tst_info to the PKCS7 structure. */
780 if (!(p7bio = PKCS7_dataInit(p7, NULL))) {
781 TSerr(TS_F_TS_RESP_SIGN, ERR_R_MALLOC_FAILURE);
782 goto err;
783 }
784
785 /* Convert tst_info to DER. */
786 if (!i2d_TS_TST_INFO_bio(p7bio, ctx->tst_info)) {
787 TSerr(TS_F_TS_RESP_SIGN, TS_R_TS_DATASIGN);
788 goto err;
789 }
790
791 /* Create the signature and add it to the signer info. */
792 if (!PKCS7_dataFinal(p7, p7bio)) {
793 TSerr(TS_F_TS_RESP_SIGN, TS_R_TS_DATASIGN);
794 goto err;
795 }
796
797 /* Set new PKCS7 and TST_INFO objects. */
798 TS_RESP_set_tst_info(ctx->response, p7, ctx->tst_info);
799 p7 = NULL; /* Ownership is lost. */
800 ctx->tst_info = NULL; /* Ownership is lost. */
801
802 ret = 1;
803
804err:
805 if (!ret)
806 TS_RESP_CTX_set_status_info_cond(ctx, TS_STATUS_REJECTION,
807 "Error during signature "
808 "generation.");
809 BIO_free_all(p7bio);
810 ESS_SIGNING_CERT_free(sc);
811 PKCS7_free(p7);
812 return ret;
813}
814
815static ESS_SIGNING_CERT *
816ESS_SIGNING_CERT_new_init(X509 *signcert, STACK_OF(X509) *certs)
817{
818 ESS_CERT_ID *cid;
819 ESS_SIGNING_CERT *sc = NULL;
820 int i;
821
822 /* Creating the ESS_CERT_ID stack. */
823 if (!(sc = ESS_SIGNING_CERT_new()))
824 goto err;
825 if (!sc->cert_ids && !(sc->cert_ids = sk_ESS_CERT_ID_new_null()))
826 goto err;
827
828 /* Adding the signing certificate id. */
829 if (!(cid = ESS_CERT_ID_new_init(signcert, 0)) ||
830 !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
831 goto err;
832 /* Adding the certificate chain ids. */
833 for (i = 0; i < sk_X509_num(certs); ++i) {
834 X509 *cert = sk_X509_value(certs, i);
835 if (!(cid = ESS_CERT_ID_new_init(cert, 1)) ||
836 !sk_ESS_CERT_ID_push(sc->cert_ids, cid))
837 goto err;
838 }
839
840 return sc;
841
842err:
843 ESS_SIGNING_CERT_free(sc);
844 TSerr(TS_F_ESS_SIGNING_CERT_NEW_INIT, ERR_R_MALLOC_FAILURE);
845 return NULL;
846}
847
848static ESS_CERT_ID *
849ESS_CERT_ID_new_init(X509 *cert, int issuer_needed)
850{
851 ESS_CERT_ID *cid = NULL;
852 GENERAL_NAME *name = NULL;
853
854 /* Recompute SHA1 hash of certificate if necessary (side effect). */
855 X509_check_purpose(cert, -1, 0);
856
857 if (!(cid = ESS_CERT_ID_new()))
858 goto err;
859 if (!ASN1_OCTET_STRING_set(cid->hash, cert->sha1_hash,
860 sizeof(cert->sha1_hash)))
861 goto err;
862
863 /* Setting the issuer/serial if requested. */
864 if (issuer_needed) {
865 /* Creating issuer/serial structure. */
866 if (!cid->issuer_serial &&
867 !(cid->issuer_serial = ESS_ISSUER_SERIAL_new()))
868 goto err;
869 /* Creating general name from the certificate issuer. */
870 if (!(name = GENERAL_NAME_new()))
871 goto err;
872 name->type = GEN_DIRNAME;
873 if (!(name->d.dirn = X509_NAME_dup(cert->cert_info->issuer)))
874 goto err;
875 if (!sk_GENERAL_NAME_push(cid->issuer_serial->issuer, name))
876 goto err;
877 name = NULL; /* Ownership is lost. */
878 /* Setting the serial number. */
879 ASN1_INTEGER_free(cid->issuer_serial->serial);
880 if (!(cid->issuer_serial->serial =
881 ASN1_INTEGER_dup(cert->cert_info->serialNumber)))
882 goto err;
883 }
884
885 return cid;
886
887err:
888 GENERAL_NAME_free(name);
889 ESS_CERT_ID_free(cid);
890 TSerr(TS_F_ESS_CERT_ID_NEW_INIT, ERR_R_MALLOC_FAILURE);
891 return NULL;
892}
893
894static int
895TS_TST_INFO_content_new(PKCS7 *p7)
896{
897 PKCS7 *ret = NULL;
898 ASN1_OCTET_STRING *octet_string = NULL;
899
900 /* Create new encapsulated NID_id_smime_ct_TSTInfo content. */
901 if (!(ret = PKCS7_new()))
902 goto err;
903 if (!(ret->d.other = ASN1_TYPE_new()))
904 goto err;
905 ret->type = OBJ_nid2obj(NID_id_smime_ct_TSTInfo);
906 if (!(octet_string = ASN1_OCTET_STRING_new()))
907 goto err;
908 ASN1_TYPE_set(ret->d.other, V_ASN1_OCTET_STRING, octet_string);
909 octet_string = NULL;
910
911 /* Add encapsulated content to signed PKCS7 structure. */
912 if (!PKCS7_set_content(p7, ret))
913 goto err;
914
915 return 1;
916
917err:
918 ASN1_OCTET_STRING_free(octet_string);
919 PKCS7_free(ret);
920 return 0;
921}
922
923static int
924ESS_add_signing_cert(PKCS7_SIGNER_INFO *si, ESS_SIGNING_CERT *sc)
925{
926 ASN1_STRING *seq = NULL;
927 unsigned char *p, *pp = NULL;
928 int len;
929
930 len = i2d_ESS_SIGNING_CERT(sc, NULL);
931 if (!(pp = malloc(len))) {
932 TSerr(TS_F_ESS_ADD_SIGNING_CERT, ERR_R_MALLOC_FAILURE);
933 goto err;
934 }
935 p = pp;
936 i2d_ESS_SIGNING_CERT(sc, &p);
937 if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len)) {
938 TSerr(TS_F_ESS_ADD_SIGNING_CERT, ERR_R_MALLOC_FAILURE);
939 goto err;
940 }
941 free(pp);
942 pp = NULL;
943 return PKCS7_add_signed_attribute(si,
944 NID_id_smime_aa_signingCertificate, V_ASN1_SEQUENCE, seq);
945
946err:
947 ASN1_STRING_free(seq);
948 free(pp);
949
950 return 0;
951}
952
953
954static ASN1_GENERALIZEDTIME *
955TS_RESP_set_genTime_with_precision(ASN1_GENERALIZEDTIME *asn1_time,
956 time_t sec, long usec, unsigned precision)
957{
958 struct tm *tm = NULL;
959 char genTime_str[17 + TS_MAX_CLOCK_PRECISION_DIGITS];
960 char usecstr[TS_MAX_CLOCK_PRECISION_DIGITS + 2];
961 char *p;
962 int rv;
963
964 if (precision > TS_MAX_CLOCK_PRECISION_DIGITS)
965 goto err;
966
967 if (!(tm = gmtime(&sec)))
968 goto err;
969
970 /*
971 * Put "genTime_str" in GeneralizedTime format. We work around the
972 * restrictions imposed by rfc3280 (i.e. "GeneralizedTime values MUST
973 * NOT include fractional seconds") and OpenSSL related functions to
974 * meet the rfc3161 requirement: "GeneralizedTime syntax can include
975 * fraction-of-second details".
976 */
977 if (precision > 0) {
978 /* To make things a bit harder, X.690 | ISO/IEC 8825-1 provides
979 the following restrictions for a DER-encoding, which OpenSSL
980 (specifically ASN1_GENERALIZEDTIME_check() function) doesn't
981 support:
982 "The encoding MUST terminate with a "Z" (which means "Zulu"
983 time). The decimal point element, if present, MUST be the
984 point option ".". The fractional-seconds elements,
985 if present, MUST omit all trailing 0's;
986 if the elements correspond to 0, they MUST be wholly
987 omitted, and the decimal point element also MUST be
988 omitted." */
989 (void) snprintf(usecstr, sizeof(usecstr), ".%06ld", usec);
990 /* truncate and trim trailing 0 */
991 usecstr[precision + 1] = '\0';
992 p = usecstr + strlen(usecstr) - 1;
993 while (p > usecstr && *p == '0')
994 *p-- = '\0';
995 /* if we've reached the beginning, delete the . too */
996 if (p == usecstr)
997 *p = '\0';
998
999 } else {
1000 /* empty */
1001 usecstr[0] = '\0';
1002 }
1003 rv = snprintf(genTime_str, sizeof(genTime_str),
1004 "%04d%02d%02d%02d%02d%02d%sZ",
1005 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1006 tm->tm_hour, tm->tm_min, tm->tm_sec, usecstr);
1007 if (rv == -1 || rv >= sizeof(genTime_str))
1008 goto err;
1009
1010 /* Now call OpenSSL to check and set our genTime value */
1011 if (!asn1_time && !(asn1_time = M_ASN1_GENERALIZEDTIME_new()))
1012 goto err;
1013 if (!ASN1_GENERALIZEDTIME_set_string(asn1_time, genTime_str)) {
1014 ASN1_GENERALIZEDTIME_free(asn1_time);
1015 goto err;
1016 }
1017
1018 return asn1_time;
1019
1020err:
1021 TSerr(TS_F_TS_RESP_SET_GENTIME_WITH_PRECISION, TS_R_COULD_NOT_SET_TIME);
1022 return NULL;
1023}
diff --git a/src/lib/libcrypto/ts/ts_rsp_utils.c b/src/lib/libcrypto/ts/ts_rsp_utils.c
deleted file mode 100644
index 0d05814297..0000000000
--- a/src/lib/libcrypto/ts/ts_rsp_utils.c
+++ /dev/null
@@ -1,436 +0,0 @@
1/* $OpenBSD: ts_rsp_utils.c,v 1.4 2014/07/11 08:44:49 jsing Exp $ */
2/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3 * project 2002.
4 */
5/* ====================================================================
6 * Copyright (c) 2006 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#include <stdio.h>
60
61#include <openssl/err.h>
62#include <openssl/objects.h>
63#include <openssl/pkcs7.h>
64#include <openssl/ts.h>
65
66/* Function definitions. */
67
68int
69TS_RESP_set_status_info(TS_RESP *a, TS_STATUS_INFO *status_info)
70{
71 TS_STATUS_INFO *new_status_info;
72
73 if (a->status_info == status_info)
74 return 1;
75 new_status_info = TS_STATUS_INFO_dup(status_info);
76 if (new_status_info == NULL) {
77 TSerr(TS_F_TS_RESP_SET_STATUS_INFO, ERR_R_MALLOC_FAILURE);
78 return 0;
79 }
80 TS_STATUS_INFO_free(a->status_info);
81 a->status_info = new_status_info;
82
83 return 1;
84}
85
86TS_STATUS_INFO *
87TS_RESP_get_status_info(TS_RESP *a)
88{
89 return a->status_info;
90}
91
92/* Caller loses ownership of PKCS7 and TS_TST_INFO objects. */
93void
94TS_RESP_set_tst_info(TS_RESP *a, PKCS7 *p7, TS_TST_INFO *tst_info)
95{
96 /* Set new PKCS7 and TST_INFO objects. */
97 PKCS7_free(a->token);
98 a->token = p7;
99 TS_TST_INFO_free(a->tst_info);
100 a->tst_info = tst_info;
101}
102
103PKCS7 *
104TS_RESP_get_token(TS_RESP *a)
105{
106 return a->token;
107}
108
109TS_TST_INFO *
110TS_RESP_get_tst_info(TS_RESP *a)
111{
112 return a->tst_info;
113}
114
115int
116TS_TST_INFO_set_version(TS_TST_INFO *a, long version)
117{
118 return ASN1_INTEGER_set(a->version, version);
119}
120
121long
122TS_TST_INFO_get_version(const TS_TST_INFO *a)
123{
124 return ASN1_INTEGER_get(a->version);
125}
126
127int
128TS_TST_INFO_set_policy_id(TS_TST_INFO *a, ASN1_OBJECT *policy)
129{
130 ASN1_OBJECT *new_policy;
131
132 if (a->policy_id == policy)
133 return 1;
134 new_policy = OBJ_dup(policy);
135 if (new_policy == NULL) {
136 TSerr(TS_F_TS_TST_INFO_SET_POLICY_ID, ERR_R_MALLOC_FAILURE);
137 return 0;
138 }
139 ASN1_OBJECT_free(a->policy_id);
140 a->policy_id = new_policy;
141 return 1;
142}
143
144ASN1_OBJECT *
145TS_TST_INFO_get_policy_id(TS_TST_INFO *a)
146{
147 return a->policy_id;
148}
149
150int
151TS_TST_INFO_set_msg_imprint(TS_TST_INFO *a, TS_MSG_IMPRINT *msg_imprint)
152{
153 TS_MSG_IMPRINT *new_msg_imprint;
154
155 if (a->msg_imprint == msg_imprint)
156 return 1;
157 new_msg_imprint = TS_MSG_IMPRINT_dup(msg_imprint);
158 if (new_msg_imprint == NULL) {
159 TSerr(TS_F_TS_TST_INFO_SET_MSG_IMPRINT, ERR_R_MALLOC_FAILURE);
160 return 0;
161 }
162 TS_MSG_IMPRINT_free(a->msg_imprint);
163 a->msg_imprint = new_msg_imprint;
164 return 1;
165}
166
167TS_MSG_IMPRINT *
168TS_TST_INFO_get_msg_imprint(TS_TST_INFO *a)
169{
170 return a->msg_imprint;
171}
172
173int
174TS_TST_INFO_set_serial(TS_TST_INFO *a, const ASN1_INTEGER *serial)
175{
176 ASN1_INTEGER *new_serial;
177
178 if (a->serial == serial)
179 return 1;
180 new_serial = ASN1_INTEGER_dup(serial);
181 if (new_serial == NULL) {
182 TSerr(TS_F_TS_TST_INFO_SET_SERIAL, ERR_R_MALLOC_FAILURE);
183 return 0;
184 }
185 ASN1_INTEGER_free(a->serial);
186 a->serial = new_serial;
187 return 1;
188}
189
190const ASN1_INTEGER *
191TS_TST_INFO_get_serial(const TS_TST_INFO *a)
192{
193 return a->serial;
194}
195
196int
197TS_TST_INFO_set_time(TS_TST_INFO *a, const ASN1_GENERALIZEDTIME *gtime)
198{
199 ASN1_GENERALIZEDTIME *new_time;
200
201 if (a->time == gtime)
202 return 1;
203 new_time = M_ASN1_GENERALIZEDTIME_dup(gtime);
204 if (new_time == NULL) {
205 TSerr(TS_F_TS_TST_INFO_SET_TIME, ERR_R_MALLOC_FAILURE);
206 return 0;
207 }
208 ASN1_GENERALIZEDTIME_free(a->time);
209 a->time = new_time;
210 return 1;
211}
212
213const ASN1_GENERALIZEDTIME *
214TS_TST_INFO_get_time(const TS_TST_INFO *a)
215{
216 return a->time;
217}
218
219int
220TS_TST_INFO_set_accuracy(TS_TST_INFO *a, TS_ACCURACY *accuracy)
221{
222 TS_ACCURACY *new_accuracy;
223
224 if (a->accuracy == accuracy)
225 return 1;
226 new_accuracy = TS_ACCURACY_dup(accuracy);
227 if (new_accuracy == NULL) {
228 TSerr(TS_F_TS_TST_INFO_SET_ACCURACY, ERR_R_MALLOC_FAILURE);
229 return 0;
230 }
231 TS_ACCURACY_free(a->accuracy);
232 a->accuracy = new_accuracy;
233 return 1;
234}
235
236TS_ACCURACY *
237TS_TST_INFO_get_accuracy(TS_TST_INFO *a)
238{
239 return a->accuracy;
240}
241
242int
243TS_ACCURACY_set_seconds(TS_ACCURACY *a, const ASN1_INTEGER *seconds)
244{
245 ASN1_INTEGER *new_seconds;
246
247 if (a->seconds == seconds)
248 return 1;
249 new_seconds = ASN1_INTEGER_dup(seconds);
250 if (new_seconds == NULL) {
251 TSerr(TS_F_TS_ACCURACY_SET_SECONDS, ERR_R_MALLOC_FAILURE);
252 return 0;
253 }
254 ASN1_INTEGER_free(a->seconds);
255 a->seconds = new_seconds;
256 return 1;
257}
258
259const ASN1_INTEGER *
260TS_ACCURACY_get_seconds(const TS_ACCURACY *a)
261{
262 return a->seconds;
263}
264
265int
266TS_ACCURACY_set_millis(TS_ACCURACY *a, const ASN1_INTEGER *millis)
267{
268 ASN1_INTEGER *new_millis = NULL;
269
270 if (a->millis == millis)
271 return 1;
272 if (millis != NULL) {
273 new_millis = ASN1_INTEGER_dup(millis);
274 if (new_millis == NULL) {
275 TSerr(TS_F_TS_ACCURACY_SET_MILLIS,
276 ERR_R_MALLOC_FAILURE);
277 return 0;
278 }
279 }
280 ASN1_INTEGER_free(a->millis);
281 a->millis = new_millis;
282 return 1;
283}
284
285const ASN1_INTEGER *
286TS_ACCURACY_get_millis(const TS_ACCURACY *a)
287{
288 return a->millis;
289}
290
291int
292TS_ACCURACY_set_micros(TS_ACCURACY *a, const ASN1_INTEGER *micros)
293{
294 ASN1_INTEGER *new_micros = NULL;
295
296 if (a->micros == micros)
297 return 1;
298 if (micros != NULL) {
299 new_micros = ASN1_INTEGER_dup(micros);
300 if (new_micros == NULL) {
301 TSerr(TS_F_TS_ACCURACY_SET_MICROS,
302 ERR_R_MALLOC_FAILURE);
303 return 0;
304 }
305 }
306 ASN1_INTEGER_free(a->micros);
307 a->micros = new_micros;
308 return 1;
309}
310
311const ASN1_INTEGER *
312TS_ACCURACY_get_micros(const TS_ACCURACY *a)
313{
314 return a->micros;
315}
316
317int
318TS_TST_INFO_set_ordering(TS_TST_INFO *a, int ordering)
319{
320 a->ordering = ordering ? 0xFF : 0x00;
321 return 1;
322}
323
324int
325TS_TST_INFO_get_ordering(const TS_TST_INFO *a)
326{
327 return a->ordering ? 1 : 0;
328}
329
330int
331TS_TST_INFO_set_nonce(TS_TST_INFO *a, const ASN1_INTEGER *nonce)
332{
333 ASN1_INTEGER *new_nonce;
334
335 if (a->nonce == nonce)
336 return 1;
337 new_nonce = ASN1_INTEGER_dup(nonce);
338 if (new_nonce == NULL) {
339 TSerr(TS_F_TS_TST_INFO_SET_NONCE, ERR_R_MALLOC_FAILURE);
340 return 0;
341 }
342 ASN1_INTEGER_free(a->nonce);
343 a->nonce = new_nonce;
344 return 1;
345}
346
347const ASN1_INTEGER *
348TS_TST_INFO_get_nonce(const TS_TST_INFO *a)
349{
350 return a->nonce;
351}
352
353int
354TS_TST_INFO_set_tsa(TS_TST_INFO *a, GENERAL_NAME *tsa)
355{
356 GENERAL_NAME *new_tsa;
357
358 if (a->tsa == tsa)
359 return 1;
360 new_tsa = GENERAL_NAME_dup(tsa);
361 if (new_tsa == NULL) {
362 TSerr(TS_F_TS_TST_INFO_SET_TSA, ERR_R_MALLOC_FAILURE);
363 return 0;
364 }
365 GENERAL_NAME_free(a->tsa);
366 a->tsa = new_tsa;
367 return 1;
368}
369
370GENERAL_NAME *
371TS_TST_INFO_get_tsa(TS_TST_INFO *a)
372{
373 return a->tsa;
374}
375
376STACK_OF(X509_EXTENSION) *TS_TST_INFO_get_exts(TS_TST_INFO *a)
377{
378 return a->extensions;
379}
380
381void
382TS_TST_INFO_ext_free(TS_TST_INFO *a)
383{
384 if (!a)
385 return;
386 sk_X509_EXTENSION_pop_free(a->extensions, X509_EXTENSION_free);
387 a->extensions = NULL;
388}
389
390int
391TS_TST_INFO_get_ext_count(TS_TST_INFO *a)
392{
393 return X509v3_get_ext_count(a->extensions);
394}
395
396int
397TS_TST_INFO_get_ext_by_NID(TS_TST_INFO *a, int nid, int lastpos)
398{
399 return X509v3_get_ext_by_NID(a->extensions, nid, lastpos);
400}
401
402int
403TS_TST_INFO_get_ext_by_OBJ(TS_TST_INFO *a, ASN1_OBJECT *obj, int lastpos)
404{
405 return X509v3_get_ext_by_OBJ(a->extensions, obj, lastpos);
406}
407
408int
409TS_TST_INFO_get_ext_by_critical(TS_TST_INFO *a, int crit, int lastpos)
410{
411 return X509v3_get_ext_by_critical(a->extensions, crit, lastpos);
412}
413
414X509_EXTENSION *
415TS_TST_INFO_get_ext(TS_TST_INFO *a, int loc)
416{
417 return X509v3_get_ext(a->extensions, loc);
418}
419
420X509_EXTENSION *
421TS_TST_INFO_delete_ext(TS_TST_INFO *a, int loc)
422{
423 return X509v3_delete_ext(a->extensions, loc);
424}
425
426int
427TS_TST_INFO_add_ext(TS_TST_INFO *a, X509_EXTENSION *ex, int loc)
428{
429 return X509v3_add_ext(&a->extensions, ex, loc) != NULL;
430}
431
432void *
433TS_TST_INFO_get_ext_d2i(TS_TST_INFO *a, int nid, int *crit, int *idx)
434{
435 return X509V3_get_d2i(a->extensions, nid, crit, idx);
436}
diff --git a/src/lib/libcrypto/ts/ts_rsp_verify.c b/src/lib/libcrypto/ts/ts_rsp_verify.c
deleted file mode 100644
index 25fc22dfaf..0000000000
--- a/src/lib/libcrypto/ts/ts_rsp_verify.c
+++ /dev/null
@@ -1,734 +0,0 @@
1/* $OpenBSD: ts_rsp_verify.c,v 1.12 2014/07/11 08:44:49 jsing Exp $ */
2/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3 * project 2002.
4 */
5/* ====================================================================
6 * Copyright (c) 2006 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#include <stdio.h>
60#include <string.h>
61
62#include <openssl/err.h>
63#include <openssl/objects.h>
64#include <openssl/pkcs7.h>
65#include <openssl/ts.h>
66
67/* Private function declarations. */
68
69static int TS_verify_cert(X509_STORE *store, STACK_OF(X509) *untrusted,
70 X509 *signer, STACK_OF(X509) **chain);
71static int TS_check_signing_certs(PKCS7_SIGNER_INFO *si, STACK_OF(X509) *chain);
72static ESS_SIGNING_CERT *ESS_get_signing_cert(PKCS7_SIGNER_INFO *si);
73static int TS_find_cert(STACK_OF(ESS_CERT_ID) *cert_ids, X509 *cert);
74static int TS_issuer_serial_cmp(ESS_ISSUER_SERIAL *is, X509_CINF *cinfo);
75static int int_TS_RESP_verify_token(TS_VERIFY_CTX *ctx,
76 PKCS7 *token, TS_TST_INFO *tst_info);
77static int TS_check_status_info(TS_RESP *response);
78static char *TS_get_status_text(STACK_OF(ASN1_UTF8STRING) *text);
79static int TS_check_policy(ASN1_OBJECT *req_oid, TS_TST_INFO *tst_info);
80static int TS_compute_imprint(BIO *data, TS_TST_INFO *tst_info,
81 X509_ALGOR **md_alg,
82 unsigned char **imprint, unsigned *imprint_len);
83static int TS_check_imprints(X509_ALGOR *algor_a,
84 unsigned char *imprint_a, unsigned len_a,
85 TS_TST_INFO *tst_info);
86static int TS_check_nonces(const ASN1_INTEGER *a, TS_TST_INFO *tst_info);
87static int TS_check_signer_name(GENERAL_NAME *tsa_name, X509 *signer);
88static int TS_find_name(STACK_OF(GENERAL_NAME) *gen_names, GENERAL_NAME *name);
89
90/*
91 * Local mapping between response codes and descriptions.
92 * Don't forget to change TS_STATUS_BUF_SIZE when modifying
93 * the elements of this array.
94 */
95static const char *TS_status_text[] = {
96 "granted",
97 "grantedWithMods",
98 "rejection",
99 "waiting",
100 "revocationWarning",
101 "revocationNotification"
102};
103
104#define TS_STATUS_TEXT_SIZE (sizeof(TS_status_text)/sizeof(*TS_status_text))
105
106/*
107 * This must be greater or equal to the sum of the strings in TS_status_text
108 * plus the number of its elements.
109 */
110#define TS_STATUS_BUF_SIZE 256
111
112static struct {
113 int code;
114 const char *text;
115} TS_failure_info[] = {
116 { TS_INFO_BAD_ALG, "badAlg" },
117 { TS_INFO_BAD_REQUEST, "badRequest" },
118 { TS_INFO_BAD_DATA_FORMAT, "badDataFormat" },
119 { TS_INFO_TIME_NOT_AVAILABLE, "timeNotAvailable" },
120 { TS_INFO_UNACCEPTED_POLICY, "unacceptedPolicy" },
121 { TS_INFO_UNACCEPTED_EXTENSION, "unacceptedExtension" },
122 { TS_INFO_ADD_INFO_NOT_AVAILABLE, "addInfoNotAvailable" },
123 { TS_INFO_SYSTEM_FAILURE, "systemFailure" }
124};
125
126#define TS_FAILURE_INFO_SIZE (sizeof(TS_failure_info) / \
127 sizeof(*TS_failure_info))
128
129/* Functions for verifying a signed TS_TST_INFO structure. */
130
131/*
132 * This function carries out the following tasks:
133 * - Checks if there is one and only one signer.
134 * - Search for the signing certificate in 'certs' and in the response.
135 * - Check the extended key usage and key usage fields of the signer
136 * certificate (done by the path validation).
137 * - Build and validate the certificate path.
138 * - Check if the certificate path meets the requirements of the
139 * SigningCertificate ESS signed attribute.
140 * - Verify the signature value.
141 * - Returns the signer certificate in 'signer', if 'signer' is not NULL.
142 */
143int
144TS_RESP_verify_signature(PKCS7 *token, STACK_OF(X509) *certs,
145 X509_STORE *store, X509 **signer_out)
146{
147 STACK_OF(PKCS7_SIGNER_INFO) *sinfos = NULL;
148 PKCS7_SIGNER_INFO *si;
149 STACK_OF(X509) *signers = NULL;
150 X509 *signer;
151 STACK_OF(X509) *chain = NULL;
152 char buf[4096];
153 int i, j = 0, ret = 0;
154 BIO *p7bio = NULL;
155
156 /* Some sanity checks first. */
157 if (!token) {
158 TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_INVALID_NULL_POINTER);
159 goto err;
160 }
161
162 /* Check for the correct content type */
163 if (!PKCS7_type_is_signed(token)) {
164 TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_WRONG_CONTENT_TYPE);
165 goto err;
166 }
167
168 /* Check if there is one and only one signer. */
169 sinfos = PKCS7_get_signer_info(token);
170 if (!sinfos || sk_PKCS7_SIGNER_INFO_num(sinfos) != 1) {
171 TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE,
172 TS_R_THERE_MUST_BE_ONE_SIGNER);
173 goto err;
174 }
175 si = sk_PKCS7_SIGNER_INFO_value(sinfos, 0);
176
177 /* Check for no content: no data to verify signature. */
178 if (PKCS7_get_detached(token)) {
179 TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_NO_CONTENT);
180 goto err;
181 }
182
183 /* Get hold of the signer certificate, search only internal
184 certificates if it was requested. */
185 signers = PKCS7_get0_signers(token, certs, 0);
186 if (!signers || sk_X509_num(signers) != 1)
187 goto err;
188 signer = sk_X509_value(signers, 0);
189
190 /* Now verify the certificate. */
191 if (!TS_verify_cert(store, certs, signer, &chain))
192 goto err;
193
194 /* Check if the signer certificate is consistent with the
195 ESS extension. */
196 if (!TS_check_signing_certs(si, chain))
197 goto err;
198
199 /* Creating the message digest. */
200 p7bio = PKCS7_dataInit(token, NULL);
201
202 /* We now have to 'read' from p7bio to calculate digests etc. */
203 while ((i = BIO_read(p7bio, buf, sizeof(buf))) > 0)
204 ;
205
206 /* Verifying the signature. */
207 j = PKCS7_signatureVerify(p7bio, token, si, signer);
208 if (j <= 0) {
209 TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_SIGNATURE_FAILURE);
210 goto err;
211 }
212
213 /* Return the signer certificate if needed. */
214 if (signer_out) {
215 *signer_out = signer;
216 CRYPTO_add(&signer->references, 1, CRYPTO_LOCK_X509);
217 }
218
219 ret = 1;
220
221err:
222 BIO_free_all(p7bio);
223 sk_X509_pop_free(chain, X509_free);
224 sk_X509_free(signers);
225
226 return ret;
227}
228
229/*
230 * The certificate chain is returned in chain. Caller is responsible for
231 * freeing the vector.
232 */
233static int
234TS_verify_cert(X509_STORE *store, STACK_OF(X509) *untrusted, X509 *signer,
235 STACK_OF(X509) **chain)
236{
237 X509_STORE_CTX cert_ctx;
238 int i;
239 int ret = 1;
240
241 /* chain is an out argument. */
242 *chain = NULL;
243 X509_STORE_CTX_init(&cert_ctx, store, signer, untrusted);
244 X509_STORE_CTX_set_purpose(&cert_ctx, X509_PURPOSE_TIMESTAMP_SIGN);
245 i = X509_verify_cert(&cert_ctx);
246 if (i <= 0) {
247 int j = X509_STORE_CTX_get_error(&cert_ctx);
248 TSerr(TS_F_TS_VERIFY_CERT, TS_R_CERTIFICATE_VERIFY_ERROR);
249 ERR_asprintf_error_data("Verify error:%s",
250 X509_verify_cert_error_string(j));
251 ret = 0;
252 } else {
253 /* Get a copy of the certificate chain. */
254 *chain = X509_STORE_CTX_get1_chain(&cert_ctx);
255 }
256
257 X509_STORE_CTX_cleanup(&cert_ctx);
258
259 return ret;
260}
261
262static int
263TS_check_signing_certs(PKCS7_SIGNER_INFO *si, STACK_OF(X509) *chain)
264{
265 ESS_SIGNING_CERT *ss = ESS_get_signing_cert(si);
266 STACK_OF(ESS_CERT_ID) *cert_ids = NULL;
267 X509 *cert;
268 int i = 0;
269 int ret = 0;
270
271 if (!ss)
272 goto err;
273 cert_ids = ss->cert_ids;
274 /* The signer certificate must be the first in cert_ids. */
275 cert = sk_X509_value(chain, 0);
276 if (TS_find_cert(cert_ids, cert) != 0)
277 goto err;
278
279 /* Check the other certificates of the chain if there are more
280 than one certificate ids in cert_ids. */
281 if (sk_ESS_CERT_ID_num(cert_ids) > 1) {
282 /* All the certificates of the chain must be in cert_ids. */
283 for (i = 1; i < sk_X509_num(chain); ++i) {
284 cert = sk_X509_value(chain, i);
285 if (TS_find_cert(cert_ids, cert) < 0)
286 goto err;
287 }
288 }
289 ret = 1;
290
291err:
292 if (!ret)
293 TSerr(TS_F_TS_CHECK_SIGNING_CERTS,
294 TS_R_ESS_SIGNING_CERTIFICATE_ERROR);
295 ESS_SIGNING_CERT_free(ss);
296 return ret;
297}
298
299static ESS_SIGNING_CERT *
300ESS_get_signing_cert(PKCS7_SIGNER_INFO *si)
301{
302 ASN1_TYPE *attr;
303 const unsigned char *p;
304
305 attr = PKCS7_get_signed_attribute(si,
306 NID_id_smime_aa_signingCertificate);
307 if (!attr)
308 return NULL;
309 p = attr->value.sequence->data;
310 return d2i_ESS_SIGNING_CERT(NULL, &p, attr->value.sequence->length);
311}
312
313/* Returns < 0 if certificate is not found, certificate index otherwise. */
314static int
315TS_find_cert(STACK_OF(ESS_CERT_ID) *cert_ids, X509 *cert)
316{
317 int i;
318
319 if (!cert_ids || !cert)
320 return -1;
321
322 /* Recompute SHA1 hash of certificate if necessary (side effect). */
323 X509_check_purpose(cert, -1, 0);
324
325 /* Look for cert in the cert_ids vector. */
326 for (i = 0; i < sk_ESS_CERT_ID_num(cert_ids); ++i) {
327 ESS_CERT_ID *cid = sk_ESS_CERT_ID_value(cert_ids, i);
328
329 /* Check the SHA-1 hash first. */
330 if (cid->hash->length == sizeof(cert->sha1_hash) &&
331 !memcmp(cid->hash->data, cert->sha1_hash,
332 sizeof(cert->sha1_hash))) {
333 /* Check the issuer/serial as well if specified. */
334 ESS_ISSUER_SERIAL *is = cid->issuer_serial;
335 if (!is || !TS_issuer_serial_cmp(is, cert->cert_info))
336 return i;
337 }
338 }
339
340 return -1;
341}
342
343static int
344TS_issuer_serial_cmp(ESS_ISSUER_SERIAL *is, X509_CINF *cinfo)
345{
346 GENERAL_NAME *issuer;
347
348 if (!is || !cinfo || sk_GENERAL_NAME_num(is->issuer) != 1)
349 return -1;
350
351 /* Check the issuer first. It must be a directory name. */
352 issuer = sk_GENERAL_NAME_value(is->issuer, 0);
353 if (issuer->type != GEN_DIRNAME ||
354 X509_NAME_cmp(issuer->d.dirn, cinfo->issuer))
355 return -1;
356
357 /* Check the serial number, too. */
358 if (ASN1_INTEGER_cmp(is->serial, cinfo->serialNumber))
359 return -1;
360
361 return 0;
362}
363
364/*
365 * Verifies whether 'response' contains a valid response with regards
366 * to the settings of the context:
367 * - Gives an error message if the TS_TST_INFO is not present.
368 * - Calls _TS_RESP_verify_token to verify the token content.
369 */
370int
371TS_RESP_verify_response(TS_VERIFY_CTX *ctx, TS_RESP *response)
372{
373 PKCS7 *token = TS_RESP_get_token(response);
374 TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
375 int ret = 0;
376
377 /* Check if we have a successful TS_TST_INFO object in place. */
378 if (!TS_check_status_info(response))
379 goto err;
380
381 /* Check the contents of the time stamp token. */
382 if (!int_TS_RESP_verify_token(ctx, token, tst_info))
383 goto err;
384
385 ret = 1;
386
387err:
388 return ret;
389}
390
391/*
392 * Tries to extract a TS_TST_INFO structure from the PKCS7 token and
393 * calls the internal int_TS_RESP_verify_token function for verifying it.
394 */
395int
396TS_RESP_verify_token(TS_VERIFY_CTX *ctx, PKCS7 *token)
397{
398 TS_TST_INFO *tst_info = PKCS7_to_TS_TST_INFO(token);
399 int ret = 0;
400
401 if (tst_info) {
402 ret = int_TS_RESP_verify_token(ctx, token, tst_info);
403 TS_TST_INFO_free(tst_info);
404 }
405 return ret;
406}
407
408/*
409 * Verifies whether the 'token' contains a valid time stamp token
410 * with regards to the settings of the context. Only those checks are
411 * carried out that are specified in the context:
412 * - Verifies the signature of the TS_TST_INFO.
413 * - Checks the version number of the response.
414 * - Check if the requested and returned policies math.
415 * - Check if the message imprints are the same.
416 * - Check if the nonces are the same.
417 * - Check if the TSA name matches the signer.
418 * - Check if the TSA name is the expected TSA.
419 */
420static int
421int_TS_RESP_verify_token(TS_VERIFY_CTX *ctx, PKCS7 *token,
422 TS_TST_INFO *tst_info)
423{
424 X509 *signer = NULL;
425 GENERAL_NAME *tsa_name = TS_TST_INFO_get_tsa(tst_info);
426 X509_ALGOR *md_alg = NULL;
427 unsigned char *imprint = NULL;
428 unsigned imprint_len = 0;
429 int ret = 0;
430
431 /* Verify the signature. */
432 if ((ctx->flags & TS_VFY_SIGNATURE) &&
433 !TS_RESP_verify_signature(token, ctx->certs, ctx->store, &signer))
434 goto err;
435
436 /* Check version number of response. */
437 if ((ctx->flags & TS_VFY_VERSION) &&
438 TS_TST_INFO_get_version(tst_info) != 1) {
439 TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_UNSUPPORTED_VERSION);
440 goto err;
441 }
442
443 /* Check policies. */
444 if ((ctx->flags & TS_VFY_POLICY) &&
445 !TS_check_policy(ctx->policy, tst_info))
446 goto err;
447
448 /* Check message imprints. */
449 if ((ctx->flags & TS_VFY_IMPRINT) &&
450 !TS_check_imprints(ctx->md_alg, ctx->imprint, ctx->imprint_len,
451 tst_info))
452 goto err;
453
454 /* Compute and check message imprints. */
455 if ((ctx->flags & TS_VFY_DATA) &&
456 (!TS_compute_imprint(ctx->data, tst_info,
457 &md_alg, &imprint, &imprint_len) ||
458 !TS_check_imprints(md_alg, imprint, imprint_len, tst_info)))
459 goto err;
460
461 /* Check nonces. */
462 if ((ctx->flags & TS_VFY_NONCE) &&
463 !TS_check_nonces(ctx->nonce, tst_info))
464 goto err;
465
466 /* Check whether TSA name and signer certificate match. */
467 if ((ctx->flags & TS_VFY_SIGNER) &&
468 tsa_name && !TS_check_signer_name(tsa_name, signer)) {
469 TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_NAME_MISMATCH);
470 goto err;
471 }
472
473 /* Check whether the TSA is the expected one. */
474 if ((ctx->flags & TS_VFY_TSA_NAME) &&
475 !TS_check_signer_name(ctx->tsa_name, signer)) {
476 TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_UNTRUSTED);
477 goto err;
478 }
479
480 ret = 1;
481
482err:
483 X509_free(signer);
484 X509_ALGOR_free(md_alg);
485 free(imprint);
486 return ret;
487}
488
489static int
490TS_check_status_info(TS_RESP *response)
491{
492 TS_STATUS_INFO *info = TS_RESP_get_status_info(response);
493 long status = ASN1_INTEGER_get(info->status);
494 const char *status_text = NULL;
495 char *embedded_status_text = NULL;
496 char failure_text[TS_STATUS_BUF_SIZE] = "";
497
498 /* Check if everything went fine. */
499 if (status == 0 || status == 1)
500 return 1;
501
502 /* There was an error, get the description in status_text. */
503 if (0 <= status && status < (long)TS_STATUS_TEXT_SIZE)
504 status_text = TS_status_text[status];
505 else
506 status_text = "unknown code";
507
508 /* Set the embedded_status_text to the returned description. */
509 if (sk_ASN1_UTF8STRING_num(info->text) > 0 &&
510 !(embedded_status_text = TS_get_status_text(info->text)))
511 return 0;
512
513 /* Filling in failure_text with the failure information. */
514 if (info->failure_info) {
515 int i;
516 int first = 1;
517 for (i = 0; i < (int)TS_FAILURE_INFO_SIZE; ++i) {
518 if (ASN1_BIT_STRING_get_bit(info->failure_info,
519 TS_failure_info[i].code)) {
520 if (!first)
521 strlcat(failure_text, ",",
522 TS_STATUS_BUF_SIZE);
523 else
524 first = 0;
525 strlcat(failure_text, TS_failure_info[i].text,
526 TS_STATUS_BUF_SIZE);
527 }
528 }
529 }
530 if (failure_text[0] == '\0')
531 strlcpy(failure_text, "unspecified", TS_STATUS_BUF_SIZE);
532
533 /* Making up the error string. */
534 TSerr(TS_F_TS_CHECK_STATUS_INFO, TS_R_NO_TIME_STAMP_TOKEN);
535 ERR_asprintf_error_data
536 ("status code: %s, status text: %s, failure codes: %s",
537 status_text,
538 embedded_status_text ? embedded_status_text : "unspecified",
539 failure_text);
540 free(embedded_status_text);
541
542 return 0;
543}
544
545static char *
546TS_get_status_text(STACK_OF(ASN1_UTF8STRING) *text)
547{
548 int i;
549 unsigned int length = 0;
550 char *result = NULL;
551
552 /* Determine length first. */
553 for (i = 0; i < sk_ASN1_UTF8STRING_num(text); ++i) {
554 ASN1_UTF8STRING *current = sk_ASN1_UTF8STRING_value(text, i);
555 length += ASN1_STRING_length(current);
556 length += 1; /* separator character */
557 }
558 /* Allocate memory (closing '\0' included). */
559 if (!(result = malloc(length))) {
560 TSerr(TS_F_TS_GET_STATUS_TEXT, ERR_R_MALLOC_FAILURE);
561 return NULL;
562 }
563 /* Concatenate the descriptions. */
564 result[0] = '\0';
565 for (i = 0; i < sk_ASN1_UTF8STRING_num(text); ++i) {
566 ASN1_UTF8STRING *current = sk_ASN1_UTF8STRING_value(text, i);
567 if (i > 0)
568 strlcat(result, "/", length);
569 strlcat(result, (const char *)ASN1_STRING_data(current), length);
570 }
571 return result;
572}
573
574static int
575TS_check_policy(ASN1_OBJECT *req_oid, TS_TST_INFO *tst_info)
576{
577 ASN1_OBJECT *resp_oid = TS_TST_INFO_get_policy_id(tst_info);
578
579 if (OBJ_cmp(req_oid, resp_oid) != 0) {
580 TSerr(TS_F_TS_CHECK_POLICY, TS_R_POLICY_MISMATCH);
581 return 0;
582 }
583
584 return 1;
585}
586
587static int
588TS_compute_imprint(BIO *data, TS_TST_INFO *tst_info, X509_ALGOR **md_alg,
589 unsigned char **imprint, unsigned *imprint_len)
590{
591 TS_MSG_IMPRINT *msg_imprint = TS_TST_INFO_get_msg_imprint(tst_info);
592 X509_ALGOR *md_alg_resp = TS_MSG_IMPRINT_get_algo(msg_imprint);
593 const EVP_MD *md;
594 EVP_MD_CTX md_ctx;
595 unsigned char buffer[4096];
596 int length;
597
598 *md_alg = NULL;
599 *imprint = NULL;
600
601 /* Return the MD algorithm of the response. */
602 if (!(*md_alg = X509_ALGOR_dup(md_alg_resp)))
603 goto err;
604
605 /* Getting the MD object. */
606 if (!(md = EVP_get_digestbyobj((*md_alg)->algorithm))) {
607 TSerr(TS_F_TS_COMPUTE_IMPRINT, TS_R_UNSUPPORTED_MD_ALGORITHM);
608 goto err;
609 }
610
611 /* Compute message digest. */
612 length = EVP_MD_size(md);
613 if (length < 0)
614 goto err;
615 *imprint_len = length;
616 if (!(*imprint = malloc(*imprint_len))) {
617 TSerr(TS_F_TS_COMPUTE_IMPRINT, ERR_R_MALLOC_FAILURE);
618 goto err;
619 }
620
621 if (!EVP_DigestInit(&md_ctx, md))
622 goto err;
623 while ((length = BIO_read(data, buffer, sizeof(buffer))) > 0) {
624 if (!EVP_DigestUpdate(&md_ctx, buffer, length))
625 goto err;
626 }
627 if (!EVP_DigestFinal(&md_ctx, *imprint, NULL))
628 goto err;
629
630 return 1;
631
632err:
633 X509_ALGOR_free(*md_alg);
634 free(*imprint);
635 *imprint = NULL;
636 *imprint_len = 0;
637 return 0;
638}
639
640static int
641TS_check_imprints(X509_ALGOR *algor_a, unsigned char *imprint_a, unsigned len_a,
642 TS_TST_INFO *tst_info)
643{
644 TS_MSG_IMPRINT *b = TS_TST_INFO_get_msg_imprint(tst_info);
645 X509_ALGOR *algor_b = TS_MSG_IMPRINT_get_algo(b);
646 int ret = 0;
647
648 /* algor_a is optional. */
649 if (algor_a) {
650 /* Compare algorithm OIDs. */
651 if (OBJ_cmp(algor_a->algorithm, algor_b->algorithm))
652 goto err;
653
654 /* The parameter must be NULL in both. */
655 if ((algor_a->parameter &&
656 ASN1_TYPE_get(algor_a->parameter) != V_ASN1_NULL) ||
657 (algor_b->parameter &&
658 ASN1_TYPE_get(algor_b->parameter) != V_ASN1_NULL))
659 goto err;
660 }
661
662 /* Compare octet strings. */
663 ret = len_a == (unsigned) ASN1_STRING_length(b->hashed_msg) &&
664 memcmp(imprint_a, ASN1_STRING_data(b->hashed_msg), len_a) == 0;
665
666err:
667 if (!ret)
668 TSerr(TS_F_TS_CHECK_IMPRINTS, TS_R_MESSAGE_IMPRINT_MISMATCH);
669 return ret;
670}
671
672static int
673TS_check_nonces(const ASN1_INTEGER *a, TS_TST_INFO *tst_info)
674{
675 const ASN1_INTEGER *b = TS_TST_INFO_get_nonce(tst_info);
676
677 /* Error if nonce is missing. */
678 if (!b) {
679 TSerr(TS_F_TS_CHECK_NONCES, TS_R_NONCE_NOT_RETURNED);
680 return 0;
681 }
682
683 /* No error if a nonce is returned without being requested. */
684 if (ASN1_INTEGER_cmp(a, b) != 0) {
685 TSerr(TS_F_TS_CHECK_NONCES, TS_R_NONCE_MISMATCH);
686 return 0;
687 }
688
689 return 1;
690}
691
692/* Check if the specified TSA name matches either the subject
693 or one of the subject alternative names of the TSA certificate. */
694static int
695TS_check_signer_name(GENERAL_NAME *tsa_name, X509 *signer)
696{
697 STACK_OF(GENERAL_NAME) *gen_names = NULL;
698 int idx = -1;
699 int found = 0;
700
701 /* Check the subject name first. */
702 if (tsa_name->type == GEN_DIRNAME &&
703 X509_name_cmp(tsa_name->d.dirn, signer->cert_info->subject) == 0)
704 return 1;
705
706 /* Check all the alternative names. */
707 gen_names = X509_get_ext_d2i(signer, NID_subject_alt_name,
708 NULL, &idx);
709 while (gen_names != NULL &&
710 !(found = TS_find_name(gen_names, tsa_name) >= 0)) {
711 /* Get the next subject alternative name,
712 although there should be no more than one. */
713 GENERAL_NAMES_free(gen_names);
714 gen_names = X509_get_ext_d2i(signer, NID_subject_alt_name,
715 NULL, &idx);
716 }
717 if (gen_names)
718 GENERAL_NAMES_free(gen_names);
719
720 return found;
721}
722
723/* Returns 1 if name is in gen_names, 0 otherwise. */
724static int
725TS_find_name(STACK_OF(GENERAL_NAME) *gen_names, GENERAL_NAME *name)
726{
727 int i, found;
728 for (i = 0, found = 0; !found && i < sk_GENERAL_NAME_num(gen_names);
729 ++i) {
730 GENERAL_NAME *current = sk_GENERAL_NAME_value(gen_names, i);
731 found = GENERAL_NAME_cmp(current, name) == 0;
732 }
733 return found ? i - 1 : -1;
734}
diff --git a/src/lib/libcrypto/ts/ts_verify_ctx.c b/src/lib/libcrypto/ts/ts_verify_ctx.c
deleted file mode 100644
index 7dda76e7d9..0000000000
--- a/src/lib/libcrypto/ts/ts_verify_ctx.c
+++ /dev/null
@@ -1,166 +0,0 @@
1/* $OpenBSD: ts_verify_ctx.c,v 1.8 2015/02/10 09:46:30 miod Exp $ */
2/* Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL
3 * project 2003.
4 */
5/* ====================================================================
6 * Copyright (c) 2006 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#include <string.h>
60
61#include <openssl/err.h>
62#include <openssl/objects.h>
63#include <openssl/ts.h>
64
65TS_VERIFY_CTX *
66TS_VERIFY_CTX_new(void)
67{
68 TS_VERIFY_CTX *ctx = calloc(1, sizeof(TS_VERIFY_CTX));
69
70 if (!ctx)
71 TSerr(TS_F_TS_VERIFY_CTX_NEW, ERR_R_MALLOC_FAILURE);
72
73 return ctx;
74}
75
76void
77TS_VERIFY_CTX_init(TS_VERIFY_CTX *ctx)
78{
79 memset(ctx, 0, sizeof(TS_VERIFY_CTX));
80}
81
82void
83TS_VERIFY_CTX_free(TS_VERIFY_CTX *ctx)
84{
85 if (!ctx)
86 return;
87
88 TS_VERIFY_CTX_cleanup(ctx);
89 free(ctx);
90}
91
92void
93TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX *ctx)
94{
95 if (!ctx)
96 return;
97
98 X509_STORE_free(ctx->store);
99 sk_X509_pop_free(ctx->certs, X509_free);
100
101 ASN1_OBJECT_free(ctx->policy);
102
103 X509_ALGOR_free(ctx->md_alg);
104 free(ctx->imprint);
105
106 BIO_free_all(ctx->data);
107
108 ASN1_INTEGER_free(ctx->nonce);
109
110 GENERAL_NAME_free(ctx->tsa_name);
111
112 TS_VERIFY_CTX_init(ctx);
113}
114
115TS_VERIFY_CTX *
116TS_REQ_to_TS_VERIFY_CTX(TS_REQ *req, TS_VERIFY_CTX *ctx)
117{
118 TS_VERIFY_CTX *ret = ctx;
119 ASN1_OBJECT *policy;
120 TS_MSG_IMPRINT *imprint;
121 X509_ALGOR *md_alg;
122 ASN1_OCTET_STRING *msg;
123 const ASN1_INTEGER *nonce;
124
125 if (ret)
126 TS_VERIFY_CTX_cleanup(ret);
127 else if (!(ret = TS_VERIFY_CTX_new()))
128 return NULL;
129
130 /* Setting flags. */
131 ret->flags = TS_VFY_ALL_IMPRINT & ~(TS_VFY_TSA_NAME | TS_VFY_SIGNATURE);
132
133 /* Setting policy. */
134 if ((policy = TS_REQ_get_policy_id(req)) != NULL) {
135 if (!(ret->policy = OBJ_dup(policy)))
136 goto err;
137 } else
138 ret->flags &= ~TS_VFY_POLICY;
139
140 /* Setting md_alg, imprint and imprint_len. */
141 imprint = TS_REQ_get_msg_imprint(req);
142 md_alg = TS_MSG_IMPRINT_get_algo(imprint);
143 if (!(ret->md_alg = X509_ALGOR_dup(md_alg)))
144 goto err;
145 msg = TS_MSG_IMPRINT_get_msg(imprint);
146 ret->imprint_len = ASN1_STRING_length(msg);
147 if (!(ret->imprint = malloc(ret->imprint_len)))
148 goto err;
149 memcpy(ret->imprint, ASN1_STRING_data(msg), ret->imprint_len);
150
151 /* Setting nonce. */
152 if ((nonce = TS_REQ_get_nonce(req)) != NULL) {
153 if (!(ret->nonce = ASN1_INTEGER_dup(nonce)))
154 goto err;
155 } else
156 ret->flags &= ~TS_VFY_NONCE;
157
158 return ret;
159
160err:
161 if (ctx)
162 TS_VERIFY_CTX_cleanup(ctx);
163 else
164 TS_VERIFY_CTX_free(ret);
165 return NULL;
166}