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