1 // Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <openssl/asn1.h>
16 #include <openssl/evp.h>
17 #include <openssl/obj.h>
18 #include <openssl/x509.h>
19 
20 #include "../asn1/internal.h"
21 #include "../internal.h"
22 #include "internal.h"
23 
X509_CRL_set_version(X509_CRL * x,long version)24 int X509_CRL_set_version(X509_CRL *x, long version) {
25   if (x == NULL) {
26     return 0;
27   }
28 
29   if (version < X509_CRL_VERSION_1 || version > X509_CRL_VERSION_2) {
30     OPENSSL_PUT_ERROR(X509, X509_R_INVALID_VERSION);
31     return 0;
32   }
33 
34   // v1(0) is default and is represented by omitting the version.
35   if (version == X509_CRL_VERSION_1) {
36     ASN1_INTEGER_free(x->crl->version);
37     x->crl->version = NULL;
38     return 1;
39   }
40 
41   if (x->crl->version == NULL) {
42     x->crl->version = ASN1_INTEGER_new();
43     if (x->crl->version == NULL) {
44       return 0;
45     }
46   }
47   return ASN1_INTEGER_set_int64(x->crl->version, version);
48 }
49 
X509_CRL_set_issuer_name(X509_CRL * x,X509_NAME * name)50 int X509_CRL_set_issuer_name(X509_CRL *x, X509_NAME *name) {
51   if ((x == NULL) || (x->crl == NULL)) {
52     return 0;
53   }
54   return (X509_NAME_set(&x->crl->issuer, name));
55 }
56 
X509_CRL_set1_lastUpdate(X509_CRL * x,const ASN1_TIME * tm)57 int X509_CRL_set1_lastUpdate(X509_CRL *x, const ASN1_TIME *tm) {
58   ASN1_TIME *in;
59 
60   if (x == NULL) {
61     return 0;
62   }
63   in = x->crl->lastUpdate;
64   if (in != tm) {
65     in = ASN1_STRING_dup(tm);
66     if (in != NULL) {
67       ASN1_TIME_free(x->crl->lastUpdate);
68       x->crl->lastUpdate = in;
69     }
70   }
71   return in != NULL;
72 }
73 
X509_CRL_set1_nextUpdate(X509_CRL * x,const ASN1_TIME * tm)74 int X509_CRL_set1_nextUpdate(X509_CRL *x, const ASN1_TIME *tm) {
75   ASN1_TIME *in;
76 
77   if (x == NULL) {
78     return 0;
79   }
80   in = x->crl->nextUpdate;
81   if (in != tm) {
82     in = ASN1_STRING_dup(tm);
83     if (in != NULL) {
84       ASN1_TIME_free(x->crl->nextUpdate);
85       x->crl->nextUpdate = in;
86     }
87   }
88   return in != NULL;
89 }
90 
X509_CRL_sort(X509_CRL * c)91 int X509_CRL_sort(X509_CRL *c) {
92   // Sort the data so it will be written in serial number order.
93   sk_X509_REVOKED_sort(c->crl->revoked);
94   asn1_encoding_clear(&c->crl->enc);
95   return 1;
96 }
97 
X509_CRL_up_ref(X509_CRL * crl)98 int X509_CRL_up_ref(X509_CRL *crl) {
99   CRYPTO_refcount_inc(&crl->references);
100   return 1;
101 }
102 
X509_CRL_get_version(const X509_CRL * crl)103 long X509_CRL_get_version(const X509_CRL *crl) {
104   return ASN1_INTEGER_get(crl->crl->version);
105 }
106 
X509_CRL_get0_lastUpdate(const X509_CRL * crl)107 const ASN1_TIME *X509_CRL_get0_lastUpdate(const X509_CRL *crl) {
108   return crl->crl->lastUpdate;
109 }
110 
X509_CRL_get0_nextUpdate(const X509_CRL * crl)111 const ASN1_TIME *X509_CRL_get0_nextUpdate(const X509_CRL *crl) {
112   return crl->crl->nextUpdate;
113 }
114 
X509_CRL_get_lastUpdate(X509_CRL * crl)115 ASN1_TIME *X509_CRL_get_lastUpdate(X509_CRL *crl) {
116   return crl->crl->lastUpdate;
117 }
118 
X509_CRL_get_nextUpdate(X509_CRL * crl)119 ASN1_TIME *X509_CRL_get_nextUpdate(X509_CRL *crl) {
120   return crl->crl->nextUpdate;
121 }
122 
X509_CRL_get_issuer(const X509_CRL * crl)123 X509_NAME *X509_CRL_get_issuer(const X509_CRL *crl) { return crl->crl->issuer; }
124 
STACK_OF(X509_REVOKED)125 STACK_OF(X509_REVOKED) *X509_CRL_get_REVOKED(X509_CRL *crl) {
126   return crl->crl->revoked;
127 }
128 
STACK_OF(X509_EXTENSION)129 const STACK_OF(X509_EXTENSION) *X509_CRL_get0_extensions(const X509_CRL *crl) {
130   return crl->crl->extensions;
131 }
132 
X509_CRL_get0_signature(const X509_CRL * crl,const ASN1_BIT_STRING ** psig,const X509_ALGOR ** palg)133 void X509_CRL_get0_signature(const X509_CRL *crl, const ASN1_BIT_STRING **psig,
134                              const X509_ALGOR **palg) {
135   if (psig != NULL) {
136     *psig = crl->signature;
137   }
138   if (palg != NULL) {
139     *palg = crl->sig_alg;
140   }
141 }
142 
X509_CRL_get_signature_nid(const X509_CRL * crl)143 int X509_CRL_get_signature_nid(const X509_CRL *crl) {
144   return OBJ_obj2nid(crl->sig_alg->algorithm);
145 }
146 
X509_REVOKED_get0_revocationDate(const X509_REVOKED * revoked)147 const ASN1_TIME *X509_REVOKED_get0_revocationDate(const X509_REVOKED *revoked) {
148   return revoked->revocationDate;
149 }
150 
X509_REVOKED_set_revocationDate(X509_REVOKED * revoked,const ASN1_TIME * tm)151 int X509_REVOKED_set_revocationDate(X509_REVOKED *revoked,
152                                     const ASN1_TIME *tm) {
153   ASN1_TIME *in;
154 
155   if (revoked == NULL) {
156     return 0;
157   }
158   in = revoked->revocationDate;
159   if (in != tm) {
160     in = ASN1_STRING_dup(tm);
161     if (in != NULL) {
162       ASN1_TIME_free(revoked->revocationDate);
163       revoked->revocationDate = in;
164     }
165   }
166   return in != NULL;
167 }
168 
X509_REVOKED_get0_serialNumber(const X509_REVOKED * revoked)169 const ASN1_INTEGER *X509_REVOKED_get0_serialNumber(
170     const X509_REVOKED *revoked) {
171   return revoked->serialNumber;
172 }
173 
X509_REVOKED_set_serialNumber(X509_REVOKED * revoked,const ASN1_INTEGER * serial)174 int X509_REVOKED_set_serialNumber(X509_REVOKED *revoked,
175                                   const ASN1_INTEGER *serial) {
176   ASN1_INTEGER *in;
177 
178   if (serial->type != V_ASN1_INTEGER && serial->type != V_ASN1_NEG_INTEGER) {
179     OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TYPE);
180     return 0;
181   }
182 
183   if (revoked == NULL) {
184     return 0;
185   }
186   in = revoked->serialNumber;
187   if (in != serial) {
188     in = ASN1_INTEGER_dup(serial);
189     if (in != NULL) {
190       ASN1_INTEGER_free(revoked->serialNumber);
191       revoked->serialNumber = in;
192     }
193   }
194   return in != NULL;
195 }
196 
STACK_OF(X509_EXTENSION)197 const STACK_OF(X509_EXTENSION) *X509_REVOKED_get0_extensions(
198     const X509_REVOKED *r) {
199   return r->extensions;
200 }
201 
i2d_re_X509_CRL_tbs(X509_CRL * crl,unsigned char ** outp)202 int i2d_re_X509_CRL_tbs(X509_CRL *crl, unsigned char **outp) {
203   asn1_encoding_clear(&crl->crl->enc);
204   return i2d_X509_CRL_INFO(crl->crl, outp);
205 }
206 
i2d_X509_CRL_tbs(X509_CRL * crl,unsigned char ** outp)207 int i2d_X509_CRL_tbs(X509_CRL *crl, unsigned char **outp) {
208   return i2d_X509_CRL_INFO(crl->crl, outp);
209 }
210 
X509_CRL_set1_signature_algo(X509_CRL * crl,const X509_ALGOR * algo)211 int X509_CRL_set1_signature_algo(X509_CRL *crl, const X509_ALGOR *algo) {
212   X509_ALGOR *copy1 = X509_ALGOR_dup(algo);
213   X509_ALGOR *copy2 = X509_ALGOR_dup(algo);
214   if (copy1 == NULL || copy2 == NULL) {
215     X509_ALGOR_free(copy1);
216     X509_ALGOR_free(copy2);
217     return 0;
218   }
219 
220   X509_ALGOR_free(crl->sig_alg);
221   crl->sig_alg = copy1;
222   X509_ALGOR_free(crl->crl->sig_alg);
223   crl->crl->sig_alg = copy2;
224   return 1;
225 }
226 
X509_CRL_set1_signature_value(X509_CRL * crl,const uint8_t * sig,size_t sig_len)227 int X509_CRL_set1_signature_value(X509_CRL *crl, const uint8_t *sig,
228                                   size_t sig_len) {
229   if (!ASN1_STRING_set(crl->signature, sig, sig_len)) {
230     return 0;
231   }
232   crl->signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
233   crl->signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
234   return 1;
235 }
236