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