blob: a63ee422415e8eba312a065f1fd6e24e20d79700 [file] [log] [blame]
David Benjamin4969cc92016-04-22 15:02:23 -04001/*
2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3 * 1999.
4 */
Adam Langleyd9e397b2015-01-22 14:27:53 -08005/* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
David Benjamin4969cc92016-04-22 15:02:23 -040013 * notice, this list of conditions and the following disclaimer.
Adam Langleyd9e397b2015-01-22 14:27:53 -080014 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com). */
56
57#include <stdio.h>
58
59#include <openssl/asn1t.h>
60#include <openssl/evp.h>
61#include <openssl/obj.h>
62#include <openssl/x509.h>
63
David Benjamin4969cc92016-04-22 15:02:23 -040064/*
65 * X509_CERT_AUX routines. These are used to encode additional user
66 * modifiable data about a certificate. This data is appended to the X509
67 * encoding when the *_X509_AUX routines are used. This means that the
68 * "traditional" X509 routines will simply ignore the extra data.
69 */
Adam Langleyd9e397b2015-01-22 14:27:53 -080070
71static X509_CERT_AUX *aux_get(X509 *x);
72
73ASN1_SEQUENCE(X509_CERT_AUX) = {
David Benjamin4969cc92016-04-22 15:02:23 -040074 ASN1_SEQUENCE_OF_OPT(X509_CERT_AUX, trust, ASN1_OBJECT),
75 ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, reject, ASN1_OBJECT, 0),
76 ASN1_OPT(X509_CERT_AUX, alias, ASN1_UTF8STRING),
77 ASN1_OPT(X509_CERT_AUX, keyid, ASN1_OCTET_STRING),
78 ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, other, X509_ALGOR, 1)
Adam Langleyd9e397b2015-01-22 14:27:53 -080079} ASN1_SEQUENCE_END(X509_CERT_AUX)
80
81IMPLEMENT_ASN1_FUNCTIONS(X509_CERT_AUX)
82
83static X509_CERT_AUX *aux_get(X509 *x)
84{
David Benjamin4969cc92016-04-22 15:02:23 -040085 if (!x)
86 return NULL;
87 if (!x->aux && !(x->aux = X509_CERT_AUX_new()))
88 return NULL;
89 return x->aux;
Adam Langleyd9e397b2015-01-22 14:27:53 -080090}
91
92int X509_alias_set1(X509 *x, unsigned char *name, int len)
93{
David Benjamin4969cc92016-04-22 15:02:23 -040094 X509_CERT_AUX *aux;
95 if (!name) {
96 if (!x || !x->aux || !x->aux->alias)
97 return 1;
98 ASN1_UTF8STRING_free(x->aux->alias);
99 x->aux->alias = NULL;
100 return 1;
101 }
102 if (!(aux = aux_get(x)))
103 return 0;
104 if (!aux->alias && !(aux->alias = ASN1_UTF8STRING_new()))
105 return 0;
106 return ASN1_STRING_set(aux->alias, name, len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800107}
108
109int X509_keyid_set1(X509 *x, unsigned char *id, int len)
110{
David Benjamin4969cc92016-04-22 15:02:23 -0400111 X509_CERT_AUX *aux;
112 if (!id) {
113 if (!x || !x->aux || !x->aux->keyid)
114 return 1;
115 ASN1_OCTET_STRING_free(x->aux->keyid);
116 x->aux->keyid = NULL;
117 return 1;
118 }
119 if (!(aux = aux_get(x)))
120 return 0;
121 if (!aux->keyid && !(aux->keyid = ASN1_OCTET_STRING_new()))
122 return 0;
123 return ASN1_STRING_set(aux->keyid, id, len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800124}
125
126unsigned char *X509_alias_get0(X509 *x, int *len)
127{
David Benjamin4969cc92016-04-22 15:02:23 -0400128 if (!x->aux || !x->aux->alias)
129 return NULL;
130 if (len)
131 *len = x->aux->alias->length;
132 return x->aux->alias->data;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800133}
134
135unsigned char *X509_keyid_get0(X509 *x, int *len)
136{
David Benjamin4969cc92016-04-22 15:02:23 -0400137 if (!x->aux || !x->aux->keyid)
138 return NULL;
139 if (len)
140 *len = x->aux->keyid->length;
141 return x->aux->keyid->data;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800142}
143
144int X509_add1_trust_object(X509 *x, ASN1_OBJECT *obj)
145{
David Benjamin4969cc92016-04-22 15:02:23 -0400146 ASN1_OBJECT *objtmp = OBJ_dup(obj);
147 if (objtmp == NULL)
148 goto err;
149 X509_CERT_AUX *aux = aux_get(x);
150 if (aux->trust == NULL) {
151 aux->trust = sk_ASN1_OBJECT_new_null();
152 if (aux->trust == NULL)
153 goto err;
154 }
155 if (!sk_ASN1_OBJECT_push(aux->trust, objtmp))
156 goto err;
157 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000158
David Benjamin4969cc92016-04-22 15:02:23 -0400159 err:
160 ASN1_OBJECT_free(objtmp);
161 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800162}
163
164int X509_add1_reject_object(X509 *x, ASN1_OBJECT *obj)
165{
David Benjamin4969cc92016-04-22 15:02:23 -0400166 ASN1_OBJECT *objtmp = OBJ_dup(obj);
167 if (objtmp == NULL)
168 goto err;
169 X509_CERT_AUX *aux = aux_get(x);
170 if (aux->reject == NULL) {
171 aux->reject = sk_ASN1_OBJECT_new_null();
172 if (aux->reject == NULL)
173 goto err;
174 }
175 if (!sk_ASN1_OBJECT_push(aux->reject, objtmp))
176 goto err;
177 return 1;
Kenny Rootb8494592015-09-25 02:29:14 +0000178
David Benjamin4969cc92016-04-22 15:02:23 -0400179 err:
180 ASN1_OBJECT_free(objtmp);
181 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800182}
183
184void X509_trust_clear(X509 *x)
185{
David Benjamin4969cc92016-04-22 15:02:23 -0400186 if (x->aux && x->aux->trust) {
187 sk_ASN1_OBJECT_pop_free(x->aux->trust, ASN1_OBJECT_free);
188 x->aux->trust = NULL;
189 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800190}
191
192void X509_reject_clear(X509 *x)
193{
David Benjamin4969cc92016-04-22 15:02:23 -0400194 if (x->aux && x->aux->reject) {
195 sk_ASN1_OBJECT_pop_free(x->aux->reject, ASN1_OBJECT_free);
196 x->aux->reject = NULL;
197 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800198}
199
200ASN1_SEQUENCE(X509_CERT_PAIR) = {
David Benjamin4969cc92016-04-22 15:02:23 -0400201 ASN1_EXP_OPT(X509_CERT_PAIR, forward, X509, 0),
202 ASN1_EXP_OPT(X509_CERT_PAIR, reverse, X509, 1)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800203} ASN1_SEQUENCE_END(X509_CERT_PAIR)
204
205IMPLEMENT_ASN1_FUNCTIONS(X509_CERT_PAIR)