1656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* a_x509a.c */
2e45f106cb6b47af1f21efe76e933bdea2f5dd1caNagendra Modadugu/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * project 1999.
4656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
5656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* ====================================================================
6656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
8656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Redistribution and use in source and binary forms, with or without
9656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * modification, are permitted provided that the following conditions
10656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * are met:
11656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
12656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 1. Redistributions of source code must retain the above copyright
13656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    notice, this list of conditions and the following disclaimer.
14656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
15656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
16656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    notice, this list of conditions and the following disclaimer in
17656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    the documentation and/or other materials provided with the
18656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    distribution.
19656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
20656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 3. All advertising materials mentioning features or use of this
21656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    software must display the following acknowledgment:
22656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    "This product includes software developed by the OpenSSL Project
23656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
25656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    endorse or promote products derived from this software without
27656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    prior written permission. For written permission, please contact
28656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    licensing@OpenSSL.org.
29656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
30656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 5. Products derived from this software may not be called "OpenSSL"
31656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    nor may "OpenSSL" appear in their names without prior written
32656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    permission of the OpenSSL Project.
33656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
34656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 6. Redistributions of any form whatsoever must retain the following
35656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    acknowledgment:
36656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    "This product includes software developed by the OpenSSL Project
37656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
39656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OF THE POSSIBILITY OF SUCH DAMAGE.
51656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ====================================================================
52656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
53656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This product includes cryptographic software written by Eric Young
54656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * (eay@cryptsoft.com).  This product includes software written by Tim
55656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Hudson (tjh@cryptsoft.com).
56656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
57656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
58656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
59656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <stdio.h>
60656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include "cryptlib.h"
61656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/evp.h>
62656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/asn1t.h>
63656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/x509.h>
64656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
65656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* X509_CERT_AUX routines. These are used to encode additional
66656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * user modifiable data about a certificate. This data is
67656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * appended to the X509 encoding when the *_X509_AUX routines
68656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * are used. This means that the "traditional" X509 routines
69656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * will simply ignore the extra data.
70656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
71656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
72656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic X509_CERT_AUX *aux_get(X509 *x);
73656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
74656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectASN1_SEQUENCE(X509_CERT_AUX) = {
75656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ASN1_SEQUENCE_OF_OPT(X509_CERT_AUX, trust, ASN1_OBJECT),
76656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, reject, ASN1_OBJECT, 0),
77656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ASN1_OPT(X509_CERT_AUX, alias, ASN1_UTF8STRING),
78656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ASN1_OPT(X509_CERT_AUX, keyid, ASN1_OCTET_STRING),
79656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, other, X509_ALGOR, 1)
80656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} ASN1_SEQUENCE_END(X509_CERT_AUX)
81656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
82656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectIMPLEMENT_ASN1_FUNCTIONS(X509_CERT_AUX)
83656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
84656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectstatic X509_CERT_AUX *aux_get(X509 *x)
85656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
86656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!x) return NULL;
87656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!x->aux && !(x->aux = X509_CERT_AUX_new())) return NULL;
88656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return x->aux;
89656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project}
90656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
91656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint X509_alias_set1(X509 *x, unsigned char *name, int len)
92656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
93656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	X509_CERT_AUX *aux;
94656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (!name)
95656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
96656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (!x || !x->aux || !x->aux->alias)
97656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			return 1;
98656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		ASN1_UTF8STRING_free(x->aux->alias);
99656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		x->aux->alias = NULL;
100656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		return 1;
101656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
102656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!(aux = aux_get(x))) return 0;
103656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!aux->alias && !(aux->alias = ASN1_UTF8STRING_new())) return 0;
104656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return ASN1_STRING_set(aux->alias, name, len);
105656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project}
106656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
107656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint X509_keyid_set1(X509 *x, unsigned char *id, int len)
108656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
109656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	X509_CERT_AUX *aux;
110656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if (!id)
111656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		{
112656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if (!x || !x->aux || !x->aux->keyid)
113656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			return 1;
114656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		ASN1_OCTET_STRING_free(x->aux->keyid);
115656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		x->aux->keyid = NULL;
116656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		return 1;
117656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
118656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!(aux = aux_get(x))) return 0;
119656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!aux->keyid && !(aux->keyid = ASN1_OCTET_STRING_new())) return 0;
120656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return ASN1_STRING_set(aux->keyid, id, len);
121656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project}
122656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
123656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectunsigned char *X509_alias_get0(X509 *x, int *len)
124656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
125656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!x->aux || !x->aux->alias) return NULL;
126656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(len) *len = x->aux->alias->length;
127656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return x->aux->alias->data;
128656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project}
129656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
130656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectunsigned char *X509_keyid_get0(X509 *x, int *len)
131656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
132656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!x->aux || !x->aux->keyid) return NULL;
133656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(len) *len = x->aux->keyid->length;
134656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return x->aux->keyid->data;
135656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project}
136656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
137656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint X509_add1_trust_object(X509 *x, ASN1_OBJECT *obj)
138656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
139656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	X509_CERT_AUX *aux;
140656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ASN1_OBJECT *objtmp;
141656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!(objtmp = OBJ_dup(obj))) return 0;
142656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!(aux = aux_get(x))) return 0;
143656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!aux->trust
144656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		&& !(aux->trust = sk_ASN1_OBJECT_new_null())) return 0;
145656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return sk_ASN1_OBJECT_push(aux->trust, objtmp);
146656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project}
147656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
148656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint X509_add1_reject_object(X509 *x, ASN1_OBJECT *obj)
149656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
150656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	X509_CERT_AUX *aux;
151656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ASN1_OBJECT *objtmp;
152656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!(objtmp = OBJ_dup(obj))) return 0;
153656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!(aux = aux_get(x))) return 0;
154656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(!aux->reject
155656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		&& !(aux->reject = sk_ASN1_OBJECT_new_null())) return 0;
156656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return sk_ASN1_OBJECT_push(aux->reject, objtmp);
157656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project}
158656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
159656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectvoid X509_trust_clear(X509 *x)
160656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
161656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(x->aux && x->aux->trust) {
162656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		sk_ASN1_OBJECT_pop_free(x->aux->trust, ASN1_OBJECT_free);
163656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		x->aux->trust = NULL;
164656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
165656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project}
166656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
167656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectvoid X509_reject_clear(X509 *x)
168656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{
169656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	if(x->aux && x->aux->reject) {
170656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		sk_ASN1_OBJECT_pop_free(x->aux->reject, ASN1_OBJECT_free);
171656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		x->aux->reject = NULL;
172656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
173656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project}
174656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
175656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectASN1_SEQUENCE(X509_CERT_PAIR) = {
176656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ASN1_EXP_OPT(X509_CERT_PAIR, forward, X509, 0),
177656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	ASN1_EXP_OPT(X509_CERT_PAIR, reverse, X509, 1)
178656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} ASN1_SEQUENCE_END(X509_CERT_PAIR)
179656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
180656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectIMPLEMENT_ASN1_FUNCTIONS(X509_CERT_PAIR)
181