fedb2c58e6ca5d66ee593ae4067ceecfaa339a6f
[openssl.git] / crypto / x509 / x509cset.c
1 /*
2  * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 <openssl/asn1.h>
13 #include <openssl/objects.h>
14 #include <openssl/evp.h>
15 #include <openssl/x509.h>
16 #include "internal/x509_int.h"
17
18 int X509_CRL_set_version(X509_CRL *x, long version)
19 {
20     if (x == NULL)
21         return (0);
22     if (x->crl.version == NULL) {
23         if ((x->crl.version = ASN1_INTEGER_new()) == NULL)
24             return (0);
25     }
26     return (ASN1_INTEGER_set(x->crl.version, version));
27 }
28
29 int X509_CRL_set_issuer_name(X509_CRL *x, X509_NAME *name)
30 {
31     if (x == NULL)
32         return (0);
33     return (X509_NAME_set(&x->crl.issuer, name));
34 }
35
36 int X509_CRL_set_lastUpdate(X509_CRL *x, const ASN1_TIME *tm)
37 {
38     ASN1_TIME *in;
39
40     if (x == NULL)
41         return (0);
42     in = x->crl.lastUpdate;
43     if (in != tm) {
44         in = ASN1_STRING_dup(tm);
45         if (in != NULL) {
46             ASN1_TIME_free(x->crl.lastUpdate);
47             x->crl.lastUpdate = in;
48         }
49     }
50     return (in != NULL);
51 }
52
53 int X509_CRL_set_nextUpdate(X509_CRL *x, const ASN1_TIME *tm)
54 {
55     ASN1_TIME *in;
56
57     if (x == NULL)
58         return (0);
59     in = x->crl.nextUpdate;
60     if (in != tm) {
61         in = ASN1_STRING_dup(tm);
62         if (in != NULL) {
63             ASN1_TIME_free(x->crl.nextUpdate);
64             x->crl.nextUpdate = in;
65         }
66     }
67     return (in != NULL);
68 }
69
70 int X509_CRL_sort(X509_CRL *c)
71 {
72     int i;
73     X509_REVOKED *r;
74     /*
75      * sort the data so it will be written in serial number order
76      */
77     sk_X509_REVOKED_sort(c->crl.revoked);
78     for (i = 0; i < sk_X509_REVOKED_num(c->crl.revoked); i++) {
79         r = sk_X509_REVOKED_value(c->crl.revoked, i);
80         r->sequence = i;
81     }
82     c->crl.enc.modified = 1;
83     return 1;
84 }
85
86 int X509_CRL_up_ref(X509_CRL *crl)
87 {
88     int i;
89
90     if (CRYPTO_atomic_add(&crl->references, 1, &i, crl->lock) <= 0)
91         return 0;
92
93     REF_PRINT_COUNT("X509_CRL", crl);
94     REF_ASSERT_ISNT(i < 2);
95     return ((i > 1) ? 1 : 0);
96 }
97
98 long X509_CRL_get_version(const X509_CRL *crl)
99 {
100     return ASN1_INTEGER_get(crl->crl.version);
101 }
102
103 ASN1_TIME *X509_CRL_get_lastUpdate(const X509_CRL *crl)
104 {
105     return crl->crl.lastUpdate;
106 }
107
108 ASN1_TIME *X509_CRL_get_nextUpdate(const X509_CRL *crl)
109 {
110     return crl->crl.nextUpdate;
111 }
112
113 X509_NAME *X509_CRL_get_issuer(const X509_CRL *crl)
114 {
115     return crl->crl.issuer;
116 }
117
118 const STACK_OF(X509_EXTENSION) *X509_CRL_get0_extensions(const X509_CRL *crl)
119 {
120     return crl->crl.extensions;
121 }
122
123 STACK_OF(X509_REVOKED) *X509_CRL_get_REVOKED(X509_CRL *crl)
124 {
125     return crl->crl.revoked;
126 }
127
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
137 int X509_CRL_get_signature_nid(const X509_CRL *crl)
138 {
139     return OBJ_obj2nid(crl->sig_alg.algorithm);
140 }
141
142 const ASN1_TIME *X509_REVOKED_get0_revocationDate(const X509_REVOKED *x)
143 {
144     return x->revocationDate;
145 }
146
147 int X509_REVOKED_set_revocationDate(X509_REVOKED *x, ASN1_TIME *tm)
148 {
149     ASN1_TIME *in;
150
151     if (x == NULL)
152         return (0);
153     in = x->revocationDate;
154     if (in != tm) {
155         in = ASN1_STRING_dup(tm);
156         if (in != NULL) {
157             ASN1_TIME_free(x->revocationDate);
158             x->revocationDate = in;
159         }
160     }
161     return (in != NULL);
162 }
163
164 const ASN1_INTEGER *X509_REVOKED_get0_serialNumber(const X509_REVOKED *x)
165 {
166     return &x->serialNumber;
167 }
168
169 int X509_REVOKED_set_serialNumber(X509_REVOKED *x, ASN1_INTEGER *serial)
170 {
171     ASN1_INTEGER *in;
172
173     if (x == NULL)
174         return (0);
175     in = &x->serialNumber;
176     if (in != serial)
177         return ASN1_STRING_copy(in, serial);
178     return 1;
179 }
180
181 const STACK_OF(X509_EXTENSION) *X509_REVOKED_get0_extensions(const X509_REVOKED *r)
182 {
183     return r->extensions;
184 }
185
186 int i2d_re_X509_CRL_tbs(X509_CRL *crl, unsigned char **pp)
187 {
188     crl->crl.enc.modified = 1;
189     return i2d_X509_CRL_INFO(&crl->crl, pp);
190 }