Fix some places where X509_up_ref is used
[openssl.git] / crypto / x509 / x509cset.c
1 /*
2  * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include "internal/refcount.h"
13 #include <openssl/asn1.h>
14 #include <openssl/objects.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509.h>
17 #include "crypto/x509.h"
18
19 DEFINE_STACK_OF(X509_REVOKED)
20
21 int X509_CRL_set_version(X509_CRL *x, long version)
22 {
23     if (x == NULL)
24         return 0;
25     if (x->crl.version == NULL) {
26         if ((x->crl.version = ASN1_INTEGER_new()) == NULL)
27             return 0;
28     }
29     return ASN1_INTEGER_set(x->crl.version, version);
30 }
31
32 int X509_CRL_set_issuer_name(X509_CRL *x, const X509_NAME *name)
33 {
34     if (x == NULL)
35         return 0;
36     return X509_NAME_set(&x->crl.issuer, name);
37 }
38
39 int X509_CRL_set1_lastUpdate(X509_CRL *x, const ASN1_TIME *tm)
40 {
41     if (x == NULL)
42         return 0;
43     return x509_set1_time(&x->crl.lastUpdate, tm);
44 }
45
46 int X509_CRL_set1_nextUpdate(X509_CRL *x, const ASN1_TIME *tm)
47 {
48     if (x == NULL)
49         return 0;
50     return x509_set1_time(&x->crl.nextUpdate, tm);
51 }
52
53 int X509_CRL_sort(X509_CRL *c)
54 {
55     int i;
56     X509_REVOKED *r;
57     /*
58      * sort the data so it will be written in serial number order
59      */
60     sk_X509_REVOKED_sort(c->crl.revoked);
61     for (i = 0; i < sk_X509_REVOKED_num(c->crl.revoked); i++) {
62         r = sk_X509_REVOKED_value(c->crl.revoked, i);
63         r->sequence = i;
64     }
65     c->crl.enc.modified = 1;
66     return 1;
67 }
68
69 int X509_CRL_up_ref(X509_CRL *crl)
70 {
71     int i;
72
73     if (CRYPTO_UP_REF(&crl->references, &i, crl->lock) <= 0)
74         return 0;
75
76     REF_PRINT_COUNT("X509_CRL", crl);
77     REF_ASSERT_ISNT(i < 2);
78     return ((i > 1) ? 1 : 0);
79 }
80
81 long X509_CRL_get_version(const X509_CRL *crl)
82 {
83     return ASN1_INTEGER_get(crl->crl.version);
84 }
85
86 const ASN1_TIME *X509_CRL_get0_lastUpdate(const X509_CRL *crl)
87 {
88     return crl->crl.lastUpdate;
89 }
90
91 const ASN1_TIME *X509_CRL_get0_nextUpdate(const X509_CRL *crl)
92 {
93     return crl->crl.nextUpdate;
94 }
95
96 #ifndef OPENSSL_NO_DEPRECATED_1_1_0
97 ASN1_TIME *X509_CRL_get_lastUpdate(X509_CRL *crl)
98 {
99     return crl->crl.lastUpdate;
100 }
101
102 ASN1_TIME *X509_CRL_get_nextUpdate(X509_CRL *crl)
103 {
104     return crl->crl.nextUpdate;
105 }
106 #endif
107
108 X509_NAME *X509_CRL_get_issuer(const X509_CRL *crl)
109 {
110     return crl->crl.issuer;
111 }
112
113 const STACK_OF(X509_EXTENSION) *X509_CRL_get0_extensions(const X509_CRL *crl)
114 {
115     return crl->crl.extensions;
116 }
117
118 STACK_OF(X509_REVOKED) *X509_CRL_get_REVOKED(X509_CRL *crl)
119 {
120     return crl->crl.revoked;
121 }
122
123 void X509_CRL_get0_signature(const X509_CRL *crl, const ASN1_BIT_STRING **psig,
124                              const X509_ALGOR **palg)
125 {
126     if (psig != NULL)
127         *psig = &crl->signature;
128     if (palg != NULL)
129         *palg = &crl->sig_alg;
130 }
131
132 int X509_CRL_get_signature_nid(const X509_CRL *crl)
133 {
134     return OBJ_obj2nid(crl->sig_alg.algorithm);
135 }
136
137 const ASN1_TIME *X509_REVOKED_get0_revocationDate(const X509_REVOKED *x)
138 {
139     return x->revocationDate;
140 }
141
142 int X509_REVOKED_set_revocationDate(X509_REVOKED *x, ASN1_TIME *tm)
143 {
144     ASN1_TIME *in;
145
146     if (x == NULL)
147         return 0;
148     in = x->revocationDate;
149     if (in != tm) {
150         in = ASN1_STRING_dup(tm);
151         if (in != NULL) {
152             ASN1_TIME_free(x->revocationDate);
153             x->revocationDate = in;
154         }
155     }
156     return (in != NULL);
157 }
158
159 const ASN1_INTEGER *X509_REVOKED_get0_serialNumber(const X509_REVOKED *x)
160 {
161     return &x->serialNumber;
162 }
163
164 int X509_REVOKED_set_serialNumber(X509_REVOKED *x, ASN1_INTEGER *serial)
165 {
166     ASN1_INTEGER *in;
167
168     if (x == NULL)
169         return 0;
170     in = &x->serialNumber;
171     if (in != serial)
172         return ASN1_STRING_copy(in, serial);
173     return 1;
174 }
175
176 const STACK_OF(X509_EXTENSION) *X509_REVOKED_get0_extensions(const X509_REVOKED *r)
177 {
178     return r->extensions;
179 }
180
181 int i2d_re_X509_CRL_tbs(X509_CRL *crl, unsigned char **pp)
182 {
183     crl->crl.enc.modified = 1;
184     return i2d_X509_CRL_INFO(&crl->crl, pp);
185 }