957386b8e2d447f2b6261460790863e95a980878
[openssl.git] / crypto / x509 / x_x509a.c
1 /*
2  * Copyright 1999-2016 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 <openssl/evp.h>
13 #include <openssl/asn1t.h>
14 #include <openssl/x509.h>
15 #include "crypto/x509.h"
16
17 DEFINE_STACK_OF(ASN1_OBJECT)
18
19 /*
20  * X509_CERT_AUX routines. These are used to encode additional user
21  * modifiable data about a certificate. This data is appended to the X509
22  * encoding when the *_X509_AUX routines are used. This means that the
23  * "traditional" X509 routines will simply ignore the extra data.
24  */
25
26 static X509_CERT_AUX *aux_get(X509 *x);
27
28 ASN1_SEQUENCE(X509_CERT_AUX) = {
29         ASN1_SEQUENCE_OF_OPT(X509_CERT_AUX, trust, ASN1_OBJECT),
30         ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, reject, ASN1_OBJECT, 0),
31         ASN1_OPT(X509_CERT_AUX, alias, ASN1_UTF8STRING),
32         ASN1_OPT(X509_CERT_AUX, keyid, ASN1_OCTET_STRING),
33         ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, other, X509_ALGOR, 1)
34 } ASN1_SEQUENCE_END(X509_CERT_AUX)
35
36 IMPLEMENT_ASN1_FUNCTIONS(X509_CERT_AUX)
37
38 int X509_trusted(const X509 *x)
39 {
40     return x->aux ? 1 : 0;
41 }
42
43 static X509_CERT_AUX *aux_get(X509 *x)
44 {
45     if (x == NULL)
46         return NULL;
47     if (x->aux == NULL && (x->aux = X509_CERT_AUX_new()) == NULL)
48         return NULL;
49     return x->aux;
50 }
51
52 int X509_alias_set1(X509 *x, const unsigned char *name, int len)
53 {
54     X509_CERT_AUX *aux;
55     if (!name) {
56         if (!x || !x->aux || !x->aux->alias)
57             return 1;
58         ASN1_UTF8STRING_free(x->aux->alias);
59         x->aux->alias = NULL;
60         return 1;
61     }
62     if ((aux = aux_get(x)) == NULL)
63         return 0;
64     if (aux->alias == NULL && (aux->alias = ASN1_UTF8STRING_new()) == NULL)
65         return 0;
66     return ASN1_STRING_set(aux->alias, name, len);
67 }
68
69 int X509_keyid_set1(X509 *x, const unsigned char *id, int len)
70 {
71     X509_CERT_AUX *aux;
72     if (!id) {
73         if (!x || !x->aux || !x->aux->keyid)
74             return 1;
75         ASN1_OCTET_STRING_free(x->aux->keyid);
76         x->aux->keyid = NULL;
77         return 1;
78     }
79     if ((aux = aux_get(x)) == NULL)
80         return 0;
81     if (aux->keyid == NULL
82         && (aux->keyid = ASN1_OCTET_STRING_new()) == NULL)
83         return 0;
84     return ASN1_STRING_set(aux->keyid, id, len);
85 }
86
87 unsigned char *X509_alias_get0(X509 *x, int *len)
88 {
89     if (!x->aux || !x->aux->alias)
90         return NULL;
91     if (len)
92         *len = x->aux->alias->length;
93     return x->aux->alias->data;
94 }
95
96 unsigned char *X509_keyid_get0(X509 *x, int *len)
97 {
98     if (!x->aux || !x->aux->keyid)
99         return NULL;
100     if (len)
101         *len = x->aux->keyid->length;
102     return x->aux->keyid->data;
103 }
104
105 int X509_add1_trust_object(X509 *x, const ASN1_OBJECT *obj)
106 {
107     X509_CERT_AUX *aux;
108     ASN1_OBJECT *objtmp = NULL;
109     if (obj) {
110         objtmp = OBJ_dup(obj);
111         if (!objtmp)
112             return 0;
113     }
114     if ((aux = aux_get(x)) == NULL)
115         goto err;
116     if (aux->trust == NULL
117         && (aux->trust = sk_ASN1_OBJECT_new_null()) == NULL)
118         goto err;
119     if (!objtmp || sk_ASN1_OBJECT_push(aux->trust, objtmp))
120         return 1;
121  err:
122     ASN1_OBJECT_free(objtmp);
123     return 0;
124 }
125
126 int X509_add1_reject_object(X509 *x, const ASN1_OBJECT *obj)
127 {
128     X509_CERT_AUX *aux;
129     ASN1_OBJECT *objtmp;
130     if ((objtmp = OBJ_dup(obj)) == NULL)
131         return 0;
132     if ((aux = aux_get(x)) == NULL)
133         goto err;
134     if (aux->reject == NULL
135         && (aux->reject = sk_ASN1_OBJECT_new_null()) == NULL)
136         goto err;
137     return sk_ASN1_OBJECT_push(aux->reject, objtmp);
138  err:
139     ASN1_OBJECT_free(objtmp);
140     return 0;
141 }
142
143 void X509_trust_clear(X509 *x)
144 {
145     if (x->aux) {
146         sk_ASN1_OBJECT_pop_free(x->aux->trust, ASN1_OBJECT_free);
147         x->aux->trust = NULL;
148     }
149 }
150
151 void X509_reject_clear(X509 *x)
152 {
153     if (x->aux) {
154         sk_ASN1_OBJECT_pop_free(x->aux->reject, ASN1_OBJECT_free);
155         x->aux->reject = NULL;
156     }
157 }
158
159 STACK_OF(ASN1_OBJECT) *X509_get0_trust_objects(X509 *x)
160 {
161     if (x->aux != NULL)
162         return x->aux->trust;
163     return NULL;
164 }
165
166 STACK_OF(ASN1_OBJECT) *X509_get0_reject_objects(X509 *x)
167 {
168     if (x->aux != NULL)
169         return x->aux->reject;
170     return NULL;
171 }