blob: 6dd1fc082629a0fb1058868e313985359f79f118 [file] [log] [blame]
Adam Langley57707c72016-01-14 11:25:12 -08001/*
2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3 * 2001.
4 */
Adam Langley95c29f32014-06-20 12:00:00 -07005/* ====================================================================
6 * Copyright (c) 2001 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
Adam Langley57707c72016-01-14 11:25:12 -080013 * notice, this list of conditions and the following disclaimer.
Adam Langley95c29f32014-06-20 12:00:00 -070014 *
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#include <openssl/asn1.h>
58#include <openssl/evp.h>
59#include <openssl/obj.h>
60#include <openssl/x509.h>
61
David Benjamindc0e3542022-11-16 23:39:44 -050062#include "../asn1/internal.h"
David Benjamin43c4d172015-09-01 00:48:17 -040063#include "../internal.h"
David Benjamina5a9b542021-07-14 13:27:30 -040064#include "internal.h"
David Benjamin43c4d172015-09-01 00:48:17 -040065
David Benjamin260a10c2022-06-16 13:58:28 -040066int X509_CRL_set_version(X509_CRL *x, long version) {
67 if (x == NULL) {
68 return 0;
69 }
David Benjamin7fd831c2022-04-06 15:38:22 -040070
David Benjamin260a10c2022-06-16 13:58:28 -040071 if (version < X509_CRL_VERSION_1 || version > X509_CRL_VERSION_2) {
72 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_VERSION);
73 return 0;
74 }
David Benjamin7fd831c2022-04-06 15:38:22 -040075
David Benjamin46350482022-06-16 14:03:12 -040076 // v1(0) is default and is represented by omitting the version.
David Benjamin260a10c2022-06-16 13:58:28 -040077 if (version == X509_CRL_VERSION_1) {
78 ASN1_INTEGER_free(x->crl->version);
79 x->crl->version = NULL;
80 return 1;
81 }
David Benjamin7fd831c2022-04-06 15:38:22 -040082
David Benjamin260a10c2022-06-16 13:58:28 -040083 if (x->crl->version == NULL) {
84 x->crl->version = ASN1_INTEGER_new();
David Benjamin7fd831c2022-04-06 15:38:22 -040085 if (x->crl->version == NULL) {
David Benjamin260a10c2022-06-16 13:58:28 -040086 return 0;
David Benjamin7fd831c2022-04-06 15:38:22 -040087 }
David Benjamin260a10c2022-06-16 13:58:28 -040088 }
David Benjamincab31f62022-09-23 15:22:39 -040089 return ASN1_INTEGER_set_int64(x->crl->version, version);
Adam Langley57707c72016-01-14 11:25:12 -080090}
Adam Langley95c29f32014-06-20 12:00:00 -070091
David Benjamin260a10c2022-06-16 13:58:28 -040092int X509_CRL_set_issuer_name(X509_CRL *x, X509_NAME *name) {
David Benjaminc0b87a02022-06-16 14:02:15 -040093 if ((x == NULL) || (x->crl == NULL)) {
David Benjamin56eeb202022-06-22 13:59:05 -040094 return 0;
David Benjaminc0b87a02022-06-16 14:02:15 -040095 }
David Benjamin260a10c2022-06-16 13:58:28 -040096 return (X509_NAME_set(&x->crl->issuer, name));
Adam Langley57707c72016-01-14 11:25:12 -080097}
Adam Langley95c29f32014-06-20 12:00:00 -070098
David Benjamin260a10c2022-06-16 13:58:28 -040099int X509_CRL_set1_lastUpdate(X509_CRL *x, const ASN1_TIME *tm) {
100 ASN1_TIME *in;
Adam Langley95c29f32014-06-20 12:00:00 -0700101
David Benjaminc0b87a02022-06-16 14:02:15 -0400102 if (x == NULL) {
David Benjamin56eeb202022-06-22 13:59:05 -0400103 return 0;
David Benjaminc0b87a02022-06-16 14:02:15 -0400104 }
David Benjamin260a10c2022-06-16 13:58:28 -0400105 in = x->crl->lastUpdate;
106 if (in != tm) {
107 in = ASN1_STRING_dup(tm);
108 if (in != NULL) {
109 ASN1_TIME_free(x->crl->lastUpdate);
110 x->crl->lastUpdate = in;
Adam Langley57707c72016-01-14 11:25:12 -0800111 }
David Benjamin260a10c2022-06-16 13:58:28 -0400112 }
David Benjamin56eeb202022-06-22 13:59:05 -0400113 return in != NULL;
Adam Langley57707c72016-01-14 11:25:12 -0800114}
Adam Langley95c29f32014-06-20 12:00:00 -0700115
David Benjamin260a10c2022-06-16 13:58:28 -0400116int X509_CRL_set1_nextUpdate(X509_CRL *x, const ASN1_TIME *tm) {
117 ASN1_TIME *in;
Adam Langley95c29f32014-06-20 12:00:00 -0700118
David Benjaminc0b87a02022-06-16 14:02:15 -0400119 if (x == NULL) {
David Benjamin56eeb202022-06-22 13:59:05 -0400120 return 0;
David Benjaminc0b87a02022-06-16 14:02:15 -0400121 }
David Benjamin260a10c2022-06-16 13:58:28 -0400122 in = x->crl->nextUpdate;
123 if (in != tm) {
124 in = ASN1_STRING_dup(tm);
125 if (in != NULL) {
126 ASN1_TIME_free(x->crl->nextUpdate);
127 x->crl->nextUpdate = in;
Adam Langley57707c72016-01-14 11:25:12 -0800128 }
David Benjamin260a10c2022-06-16 13:58:28 -0400129 }
David Benjamin56eeb202022-06-22 13:59:05 -0400130 return in != NULL;
Adam Langley57707c72016-01-14 11:25:12 -0800131}
Adam Langley95c29f32014-06-20 12:00:00 -0700132
David Benjamin260a10c2022-06-16 13:58:28 -0400133int X509_CRL_sort(X509_CRL *c) {
David Benjamin46350482022-06-16 14:03:12 -0400134 // Sort the data so it will be written in serial number order.
David Benjamin260a10c2022-06-16 13:58:28 -0400135 sk_X509_REVOKED_sort(c->crl->revoked);
David Benjamindc0e3542022-11-16 23:39:44 -0500136 asn1_encoding_clear(&c->crl->enc);
David Benjamin260a10c2022-06-16 13:58:28 -0400137 return 1;
Adam Langley57707c72016-01-14 11:25:12 -0800138}
Adam Langley95c29f32014-06-20 12:00:00 -0700139
David Benjamin260a10c2022-06-16 13:58:28 -0400140int X509_CRL_up_ref(X509_CRL *crl) {
141 CRYPTO_refcount_inc(&crl->references);
142 return 1;
Adam Langley57707c72016-01-14 11:25:12 -0800143}
David Benjamin43c4d172015-09-01 00:48:17 -0400144
David Benjamin260a10c2022-06-16 13:58:28 -0400145long X509_CRL_get_version(const X509_CRL *crl) {
146 return ASN1_INTEGER_get(crl->crl->version);
David Benjamin33f8d332020-06-19 12:25:34 -0400147}
148
David Benjamin260a10c2022-06-16 13:58:28 -0400149const ASN1_TIME *X509_CRL_get0_lastUpdate(const X509_CRL *crl) {
150 return crl->crl->lastUpdate;
David Benjaminbc3286b2018-08-13 17:52:48 -0500151}
152
David Benjamin260a10c2022-06-16 13:58:28 -0400153const ASN1_TIME *X509_CRL_get0_nextUpdate(const X509_CRL *crl) {
154 return crl->crl->nextUpdate;
David Benjaminbc3286b2018-08-13 17:52:48 -0500155}
156
David Benjamin260a10c2022-06-16 13:58:28 -0400157ASN1_TIME *X509_CRL_get_lastUpdate(X509_CRL *crl) {
158 return crl->crl->lastUpdate;
David Benjamin33f8d332020-06-19 12:25:34 -0400159}
160
David Benjamin260a10c2022-06-16 13:58:28 -0400161ASN1_TIME *X509_CRL_get_nextUpdate(X509_CRL *crl) {
162 return crl->crl->nextUpdate;
David Benjamin33f8d332020-06-19 12:25:34 -0400163}
164
David Benjamin260a10c2022-06-16 13:58:28 -0400165X509_NAME *X509_CRL_get_issuer(const X509_CRL *crl) { return crl->crl->issuer; }
166
167STACK_OF(X509_REVOKED) *X509_CRL_get_REVOKED(X509_CRL *crl) {
168 return crl->crl->revoked;
David Benjamin33f8d332020-06-19 12:25:34 -0400169}
170
David Benjamin260a10c2022-06-16 13:58:28 -0400171const STACK_OF(X509_EXTENSION) *X509_CRL_get0_extensions(const X509_CRL *crl) {
172 return crl->crl->extensions;
David Benjaminee4af9e2020-08-31 16:54:47 -0400173}
174
David Benjamin0318b052018-05-07 20:38:20 -0400175void X509_CRL_get0_signature(const X509_CRL *crl, const ASN1_BIT_STRING **psig,
David Benjamin260a10c2022-06-16 13:58:28 -0400176 const X509_ALGOR **palg) {
David Benjaminc0b87a02022-06-16 14:02:15 -0400177 if (psig != NULL) {
David Benjamin260a10c2022-06-16 13:58:28 -0400178 *psig = crl->signature;
David Benjaminc0b87a02022-06-16 14:02:15 -0400179 }
180 if (palg != NULL) {
David Benjamin260a10c2022-06-16 13:58:28 -0400181 *palg = crl->sig_alg;
David Benjaminc0b87a02022-06-16 14:02:15 -0400182 }
David Benjamin0318b052018-05-07 20:38:20 -0400183}
184
David Benjamin260a10c2022-06-16 13:58:28 -0400185int X509_CRL_get_signature_nid(const X509_CRL *crl) {
186 return OBJ_obj2nid(crl->sig_alg->algorithm);
David Benjamin0318b052018-05-07 20:38:20 -0400187}
188
David Benjamin260a10c2022-06-16 13:58:28 -0400189const ASN1_TIME *X509_REVOKED_get0_revocationDate(const X509_REVOKED *revoked) {
190 return revoked->revocationDate;
David Benjamin0318b052018-05-07 20:38:20 -0400191}
192
David Benjamin260a10c2022-06-16 13:58:28 -0400193int X509_REVOKED_set_revocationDate(X509_REVOKED *revoked,
194 const ASN1_TIME *tm) {
195 ASN1_TIME *in;
Adam Langley95c29f32014-06-20 12:00:00 -0700196
David Benjaminc0b87a02022-06-16 14:02:15 -0400197 if (revoked == NULL) {
David Benjamin56eeb202022-06-22 13:59:05 -0400198 return 0;
David Benjaminc0b87a02022-06-16 14:02:15 -0400199 }
David Benjamin260a10c2022-06-16 13:58:28 -0400200 in = revoked->revocationDate;
201 if (in != tm) {
202 in = ASN1_STRING_dup(tm);
203 if (in != NULL) {
204 ASN1_TIME_free(revoked->revocationDate);
205 revoked->revocationDate = in;
Adam Langley57707c72016-01-14 11:25:12 -0800206 }
David Benjamin260a10c2022-06-16 13:58:28 -0400207 }
David Benjamin56eeb202022-06-22 13:59:05 -0400208 return in != NULL;
Adam Langley57707c72016-01-14 11:25:12 -0800209}
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin260a10c2022-06-16 13:58:28 -0400211const ASN1_INTEGER *X509_REVOKED_get0_serialNumber(
212 const X509_REVOKED *revoked) {
213 return revoked->serialNumber;
David Benjamin0318b052018-05-07 20:38:20 -0400214}
215
David Benjamin53bbb182020-11-11 15:22:29 -0500216int X509_REVOKED_set_serialNumber(X509_REVOKED *revoked,
David Benjamin260a10c2022-06-16 13:58:28 -0400217 const ASN1_INTEGER *serial) {
218 ASN1_INTEGER *in;
Adam Langley95c29f32014-06-20 12:00:00 -0700219
David Benjamina61e7472022-09-23 15:04:20 -0400220 if (serial->type != V_ASN1_INTEGER && serial->type != V_ASN1_NEG_INTEGER) {
Emily Starkfd522962022-09-07 15:54:07 -0700221 OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TYPE);
222 return 0;
223 }
224
David Benjaminc0b87a02022-06-16 14:02:15 -0400225 if (revoked == NULL) {
David Benjamin56eeb202022-06-22 13:59:05 -0400226 return 0;
David Benjaminc0b87a02022-06-16 14:02:15 -0400227 }
David Benjamin260a10c2022-06-16 13:58:28 -0400228 in = revoked->serialNumber;
229 if (in != serial) {
230 in = ASN1_INTEGER_dup(serial);
231 if (in != NULL) {
232 ASN1_INTEGER_free(revoked->serialNumber);
233 revoked->serialNumber = in;
Adam Langley57707c72016-01-14 11:25:12 -0800234 }
David Benjamin260a10c2022-06-16 13:58:28 -0400235 }
David Benjamin56eeb202022-06-22 13:59:05 -0400236 return in != NULL;
Adam Langley57707c72016-01-14 11:25:12 -0800237}
David Benjamin0318b052018-05-07 20:38:20 -0400238
David Benjamin260a10c2022-06-16 13:58:28 -0400239const STACK_OF(X509_EXTENSION) *X509_REVOKED_get0_extensions(
240 const X509_REVOKED *r) {
241 return r->extensions;
David Benjaminee4af9e2020-08-31 16:54:47 -0400242}
243
David Benjamin260a10c2022-06-16 13:58:28 -0400244int i2d_re_X509_CRL_tbs(X509_CRL *crl, unsigned char **outp) {
David Benjamindc0e3542022-11-16 23:39:44 -0500245 asn1_encoding_clear(&crl->crl->enc);
David Benjamin260a10c2022-06-16 13:58:28 -0400246 return i2d_X509_CRL_INFO(crl->crl, outp);
David Benjamin6dcce802020-11-02 18:16:14 -0500247}
248
David Benjamin260a10c2022-06-16 13:58:28 -0400249int i2d_X509_CRL_tbs(X509_CRL *crl, unsigned char **outp) {
250 return i2d_X509_CRL_INFO(crl->crl, outp);
David Benjamin0318b052018-05-07 20:38:20 -0400251}
David Benjamin4b066b02021-05-14 12:42:50 -0400252
David Benjamin260a10c2022-06-16 13:58:28 -0400253int X509_CRL_set1_signature_algo(X509_CRL *crl, const X509_ALGOR *algo) {
David Benjamindc112e72022-06-25 21:42:52 -0400254 X509_ALGOR *copy1 = X509_ALGOR_dup(algo);
255 X509_ALGOR *copy2 = X509_ALGOR_dup(algo);
David Benjamin260a10c2022-06-16 13:58:28 -0400256 if (copy1 == NULL || copy2 == NULL) {
257 X509_ALGOR_free(copy1);
258 X509_ALGOR_free(copy2);
259 return 0;
260 }
David Benjamin4b066b02021-05-14 12:42:50 -0400261
David Benjamin260a10c2022-06-16 13:58:28 -0400262 X509_ALGOR_free(crl->sig_alg);
263 crl->sig_alg = copy1;
264 X509_ALGOR_free(crl->crl->sig_alg);
265 crl->crl->sig_alg = copy2;
266 return 1;
David Benjamin4b066b02021-05-14 12:42:50 -0400267}
268
269int X509_CRL_set1_signature_value(X509_CRL *crl, const uint8_t *sig,
David Benjamin260a10c2022-06-16 13:58:28 -0400270 size_t sig_len) {
271 if (!ASN1_STRING_set(crl->signature, sig, sig_len)) {
272 return 0;
273 }
274 crl->signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
275 crl->signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
276 return 1;
David Benjamin4b066b02021-05-14 12:42:50 -0400277}