xref: /openssl/crypto/x509/x509cset.c (revision da1c088f)
1 /*
2  * Copyright 2001-2023 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 
X509_CRL_set_version(X509_CRL * x,long version)19 int X509_CRL_set_version(X509_CRL *x, long version)
20 {
21     if (x == NULL)
22         return 0;
23     if (x->crl.version == NULL) {
24         if ((x->crl.version = ASN1_INTEGER_new()) == NULL)
25             return 0;
26     }
27     if (!ASN1_INTEGER_set(x->crl.version, version))
28         return 0;
29     x->crl.enc.modified = 1;
30     return 1;
31 }
32 
X509_CRL_set_issuer_name(X509_CRL * x,const X509_NAME * name)33 int X509_CRL_set_issuer_name(X509_CRL *x, const X509_NAME *name)
34 {
35     if (x == NULL)
36         return 0;
37     if (!X509_NAME_set(&x->crl.issuer, name))
38         return 0;
39     x->crl.enc.modified = 1;
40     return 1;
41 }
42 
X509_CRL_set1_lastUpdate(X509_CRL * x,const ASN1_TIME * tm)43 int X509_CRL_set1_lastUpdate(X509_CRL *x, const ASN1_TIME *tm)
44 {
45     if (x == NULL || tm == NULL)
46         return 0;
47     return ossl_x509_set1_time(&x->crl.enc.modified, &x->crl.lastUpdate, tm);
48 }
49 
X509_CRL_set1_nextUpdate(X509_CRL * x,const ASN1_TIME * tm)50 int X509_CRL_set1_nextUpdate(X509_CRL *x, const ASN1_TIME *tm)
51 {
52     if (x == NULL)
53         return 0;
54     return ossl_x509_set1_time(&x->crl.enc.modified, &x->crl.nextUpdate, tm);
55 }
56 
X509_CRL_sort(X509_CRL * c)57 int X509_CRL_sort(X509_CRL *c)
58 {
59     int i;
60     X509_REVOKED *r;
61 
62     /*
63      * sort the data so it will be written in serial number order
64      */
65     sk_X509_REVOKED_sort(c->crl.revoked);
66     for (i = 0; i < sk_X509_REVOKED_num(c->crl.revoked); i++) {
67         r = sk_X509_REVOKED_value(c->crl.revoked, i);
68         r->sequence = i;
69     }
70     c->crl.enc.modified = 1;
71     return 1;
72 }
73 
X509_CRL_up_ref(X509_CRL * crl)74 int X509_CRL_up_ref(X509_CRL *crl)
75 {
76     int i;
77 
78     if (CRYPTO_UP_REF(&crl->references, &i) <= 0)
79         return 0;
80 
81     REF_PRINT_COUNT("X509_CRL", crl);
82     REF_ASSERT_ISNT(i < 2);
83     return i > 1;
84 }
85 
X509_CRL_get_version(const X509_CRL * crl)86 long X509_CRL_get_version(const X509_CRL *crl)
87 {
88     return ASN1_INTEGER_get(crl->crl.version);
89 }
90 
X509_CRL_get0_lastUpdate(const X509_CRL * crl)91 const ASN1_TIME *X509_CRL_get0_lastUpdate(const X509_CRL *crl)
92 {
93     return crl->crl.lastUpdate;
94 }
95 
X509_CRL_get0_nextUpdate(const X509_CRL * crl)96 const ASN1_TIME *X509_CRL_get0_nextUpdate(const X509_CRL *crl)
97 {
98     return crl->crl.nextUpdate;
99 }
100 
101 #ifndef OPENSSL_NO_DEPRECATED_1_1_0
X509_CRL_get_lastUpdate(X509_CRL * crl)102 ASN1_TIME *X509_CRL_get_lastUpdate(X509_CRL *crl)
103 {
104     return crl->crl.lastUpdate;
105 }
106 
X509_CRL_get_nextUpdate(X509_CRL * crl)107 ASN1_TIME *X509_CRL_get_nextUpdate(X509_CRL *crl)
108 {
109     return crl->crl.nextUpdate;
110 }
111 #endif
112 
X509_CRL_get_issuer(const X509_CRL * crl)113 X509_NAME *X509_CRL_get_issuer(const X509_CRL *crl)
114 {
115     return crl->crl.issuer;
116 }
117 
STACK_OF(X509_EXTENSION)118 const STACK_OF(X509_EXTENSION) *X509_CRL_get0_extensions(const X509_CRL *crl)
119 {
120     return crl->crl.extensions;
121 }
122 
STACK_OF(X509_REVOKED)123 STACK_OF(X509_REVOKED) *X509_CRL_get_REVOKED(X509_CRL *crl)
124 {
125     return crl->crl.revoked;
126 }
127 
X509_CRL_get0_signature(const X509_CRL * crl,const ASN1_BIT_STRING ** psig,const X509_ALGOR ** palg)128 void X509_CRL_get0_signature(const X509_CRL *crl, const ASN1_BIT_STRING **psig,
129                              const X509_ALGOR **palg)
130 {
131     if (psig != NULL)
132         *psig = &crl->signature;
133     if (palg != NULL)
134         *palg = &crl->sig_alg;
135 }
136 
X509_CRL_get_signature_nid(const X509_CRL * crl)137 int X509_CRL_get_signature_nid(const X509_CRL *crl)
138 {
139     return OBJ_obj2nid(crl->sig_alg.algorithm);
140 }
141 
X509_REVOKED_get0_revocationDate(const X509_REVOKED * x)142 const ASN1_TIME *X509_REVOKED_get0_revocationDate(const X509_REVOKED *x)
143 {
144     return x->revocationDate;
145 }
146 
X509_REVOKED_set_revocationDate(X509_REVOKED * x,ASN1_TIME * tm)147 int X509_REVOKED_set_revocationDate(X509_REVOKED *x, ASN1_TIME *tm)
148 {
149     if (x == NULL || tm == NULL)
150         return 0;
151     return ossl_x509_set1_time(NULL, &x->revocationDate, tm);
152 }
153 
X509_REVOKED_get0_serialNumber(const X509_REVOKED * x)154 const ASN1_INTEGER *X509_REVOKED_get0_serialNumber(const X509_REVOKED *x)
155 {
156     return &x->serialNumber;
157 }
158 
X509_REVOKED_set_serialNumber(X509_REVOKED * x,ASN1_INTEGER * serial)159 int X509_REVOKED_set_serialNumber(X509_REVOKED *x, ASN1_INTEGER *serial)
160 {
161     ASN1_INTEGER *in;
162 
163     if (x == NULL)
164         return 0;
165     in = &x->serialNumber;
166     if (in != serial)
167         return ASN1_STRING_copy(in, serial);
168     return 1;
169 }
170 
STACK_OF(X509_EXTENSION)171 const STACK_OF(X509_EXTENSION) *X509_REVOKED_get0_extensions(const
172                                                              X509_REVOKED *r)
173 {
174     return r->extensions;
175 }
176 
i2d_re_X509_CRL_tbs(X509_CRL * crl,unsigned char ** pp)177 int i2d_re_X509_CRL_tbs(X509_CRL *crl, unsigned char **pp)
178 {
179     crl->crl.enc.modified = 1;
180     return i2d_X509_CRL_INFO(&crl->crl, pp);
181 }
182