Make some more X509 functions const.
[openssl.git] / crypto / x509 / x509_set.c
1 /*
2  * Copyright 1995-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_set_version(X509 *x, long version)
19 {
20     if (x == NULL)
21         return (0);
22     if (version == 0) {
23         ASN1_INTEGER_free(x->cert_info.version);
24         x->cert_info.version = NULL;
25         return (1);
26     }
27     if (x->cert_info.version == NULL) {
28         if ((x->cert_info.version = ASN1_INTEGER_new()) == NULL)
29             return (0);
30     }
31     return (ASN1_INTEGER_set(x->cert_info.version, version));
32 }
33
34 int X509_set_serialNumber(X509 *x, ASN1_INTEGER *serial)
35 {
36     ASN1_INTEGER *in;
37
38     if (x == NULL)
39         return 0;
40     in = &x->cert_info.serialNumber;
41     if (in != serial)
42         return ASN1_STRING_copy(in, serial);
43     return 1;
44 }
45
46 int X509_set_issuer_name(X509 *x, X509_NAME *name)
47 {
48     if (x == NULL)
49         return (0);
50     return (X509_NAME_set(&x->cert_info.issuer, name));
51 }
52
53 int X509_set_subject_name(X509 *x, X509_NAME *name)
54 {
55     if (x == NULL)
56         return (0);
57     return (X509_NAME_set(&x->cert_info.subject, name));
58 }
59
60 int X509_set_notBefore(X509 *x, const ASN1_TIME *tm)
61 {
62     ASN1_TIME *in;
63
64     if (x == NULL)
65         return (0);
66     in = x->cert_info.validity.notBefore;
67     if (in != tm) {
68         in = ASN1_STRING_dup(tm);
69         if (in != NULL) {
70             ASN1_TIME_free(x->cert_info.validity.notBefore);
71             x->cert_info.validity.notBefore = in;
72         }
73     }
74     return (in != NULL);
75 }
76
77 int X509_set_notAfter(X509 *x, const ASN1_TIME *tm)
78 {
79     ASN1_TIME *in;
80
81     if (x == NULL)
82         return (0);
83     in = x->cert_info.validity.notAfter;
84     if (in != tm) {
85         in = ASN1_STRING_dup(tm);
86         if (in != NULL) {
87             ASN1_TIME_free(x->cert_info.validity.notAfter);
88             x->cert_info.validity.notAfter = in;
89         }
90     }
91     return (in != NULL);
92 }
93
94 int X509_set_pubkey(X509 *x, EVP_PKEY *pkey)
95 {
96     if (x == NULL)
97         return (0);
98     return (X509_PUBKEY_set(&(x->cert_info.key), pkey));
99 }
100
101 int X509_up_ref(X509 *x)
102 {
103     int i;
104
105     if (CRYPTO_atomic_add(&x->references, 1, &i, x->lock) <= 0)
106         return 0;
107
108     REF_PRINT_COUNT("X509", x);
109     REF_ASSERT_ISNT(i < 2);
110     return ((i > 1) ? 1 : 0);
111 }
112
113 long X509_get_version(const X509 *x)
114 {
115     return ASN1_INTEGER_get(x->cert_info.version);
116 }
117
118 ASN1_TIME * X509_get_notBefore(const X509 *x)
119 {
120     return x->cert_info.validity.notBefore;
121 }
122
123 ASN1_TIME *X509_get_notAfter(const X509 *x)
124 {
125     return x->cert_info.validity.notAfter;
126 }
127
128 int X509_get_signature_type(const X509 *x)
129 {
130     return EVP_PKEY_type(OBJ_obj2nid(x->sig_alg.algorithm));
131 }
132
133 X509_PUBKEY *X509_get_X509_PUBKEY(const X509 *x)
134 {
135     return x->cert_info.key;
136 }
137
138 STACK_OF(X509_EXTENSION) *X509_get0_extensions(const X509 *x)
139 {
140     return x->cert_info.extensions;
141 }
142
143 void X509_get0_uids(ASN1_BIT_STRING **piuid, ASN1_BIT_STRING **psuid, X509 *x)
144 {
145     if (piuid != NULL)
146         *piuid = x->cert_info.issuerUID;
147     if (psuid != NULL)
148         *psuid = x->cert_info.subjectUID;
149 }
150
151 X509_ALGOR *X509_get0_tbs_sigalg(X509 *x)
152 {
153     return &x->cert_info.signature;
154 }