10Sstevel@tonic-gate /* a_x509a.c */
20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
30Sstevel@tonic-gate * project 1999.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate * are met:
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate *
150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate * the documentation and/or other materials provided with the
180Sstevel@tonic-gate * distribution.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate * software must display the following acknowledgment:
220Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate * endorse or promote products derived from this software without
270Sstevel@tonic-gate * prior written permission. For written permission, please contact
280Sstevel@tonic-gate * licensing@OpenSSL.org.
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate * permission of the OpenSSL Project.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate * acknowledgment:
360Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate * ====================================================================
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
550Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate *
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/evp.h>
620Sstevel@tonic-gate #include <openssl/asn1t.h>
630Sstevel@tonic-gate #include <openssl/x509.h>
640Sstevel@tonic-gate
650Sstevel@tonic-gate /* X509_CERT_AUX routines. These are used to encode additional
660Sstevel@tonic-gate * user modifiable data about a certificate. This data is
670Sstevel@tonic-gate * appended to the X509 encoding when the *_X509_AUX routines
680Sstevel@tonic-gate * are used. This means that the "traditional" X509 routines
690Sstevel@tonic-gate * will simply ignore the extra data.
700Sstevel@tonic-gate */
710Sstevel@tonic-gate
720Sstevel@tonic-gate static X509_CERT_AUX *aux_get(X509 *x);
730Sstevel@tonic-gate
740Sstevel@tonic-gate ASN1_SEQUENCE(X509_CERT_AUX) = {
750Sstevel@tonic-gate ASN1_SEQUENCE_OF_OPT(X509_CERT_AUX, trust, ASN1_OBJECT),
760Sstevel@tonic-gate ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, reject, ASN1_OBJECT, 0),
770Sstevel@tonic-gate ASN1_OPT(X509_CERT_AUX, alias, ASN1_UTF8STRING),
780Sstevel@tonic-gate ASN1_OPT(X509_CERT_AUX, keyid, ASN1_OCTET_STRING),
790Sstevel@tonic-gate ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, other, X509_ALGOR, 1)
800Sstevel@tonic-gate } ASN1_SEQUENCE_END(X509_CERT_AUX)
810Sstevel@tonic-gate
820Sstevel@tonic-gate IMPLEMENT_ASN1_FUNCTIONS(X509_CERT_AUX)
830Sstevel@tonic-gate
840Sstevel@tonic-gate static X509_CERT_AUX *aux_get(X509 *x)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate if(!x) return NULL;
870Sstevel@tonic-gate if(!x->aux && !(x->aux = X509_CERT_AUX_new())) return NULL;
880Sstevel@tonic-gate return x->aux;
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
X509_alias_set1(X509 * x,unsigned char * name,int len)910Sstevel@tonic-gate int X509_alias_set1(X509 *x, unsigned char *name, int len)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate X509_CERT_AUX *aux;
94*2139Sjp161948 if (!name)
95*2139Sjp161948 {
96*2139Sjp161948 if (!x || !x->aux || !x->aux->alias)
97*2139Sjp161948 return 1;
98*2139Sjp161948 ASN1_UTF8STRING_free(x->aux->alias);
99*2139Sjp161948 x->aux->alias = NULL;
100*2139Sjp161948 return 1;
101*2139Sjp161948 }
1020Sstevel@tonic-gate if(!(aux = aux_get(x))) return 0;
1030Sstevel@tonic-gate if(!aux->alias && !(aux->alias = ASN1_UTF8STRING_new())) return 0;
1040Sstevel@tonic-gate return ASN1_STRING_set(aux->alias, name, len);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate
X509_keyid_set1(X509 * x,unsigned char * id,int len)1070Sstevel@tonic-gate int X509_keyid_set1(X509 *x, unsigned char *id, int len)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate X509_CERT_AUX *aux;
110*2139Sjp161948 if (!id)
111*2139Sjp161948 {
112*2139Sjp161948 if (!x || !x->aux || !x->aux->keyid)
113*2139Sjp161948 return 1;
114*2139Sjp161948 ASN1_OCTET_STRING_free(x->aux->keyid);
115*2139Sjp161948 x->aux->keyid = NULL;
116*2139Sjp161948 return 1;
117*2139Sjp161948 }
1180Sstevel@tonic-gate if(!(aux = aux_get(x))) return 0;
1190Sstevel@tonic-gate if(!aux->keyid && !(aux->keyid = ASN1_OCTET_STRING_new())) return 0;
1200Sstevel@tonic-gate return ASN1_STRING_set(aux->keyid, id, len);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
X509_alias_get0(X509 * x,int * len)1230Sstevel@tonic-gate unsigned char *X509_alias_get0(X509 *x, int *len)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate if(!x->aux || !x->aux->alias) return NULL;
1260Sstevel@tonic-gate if(len) *len = x->aux->alias->length;
1270Sstevel@tonic-gate return x->aux->alias->data;
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
X509_keyid_get0(X509 * x,int * len)130*2139Sjp161948 unsigned char *X509_keyid_get0(X509 *x, int *len)
131*2139Sjp161948 {
132*2139Sjp161948 if(!x->aux || !x->aux->keyid) return NULL;
133*2139Sjp161948 if(len) *len = x->aux->keyid->length;
134*2139Sjp161948 return x->aux->keyid->data;
135*2139Sjp161948 }
136*2139Sjp161948
X509_add1_trust_object(X509 * x,ASN1_OBJECT * obj)1370Sstevel@tonic-gate int X509_add1_trust_object(X509 *x, ASN1_OBJECT *obj)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate X509_CERT_AUX *aux;
1400Sstevel@tonic-gate ASN1_OBJECT *objtmp;
1410Sstevel@tonic-gate if(!(objtmp = OBJ_dup(obj))) return 0;
1420Sstevel@tonic-gate if(!(aux = aux_get(x))) return 0;
1430Sstevel@tonic-gate if(!aux->trust
1440Sstevel@tonic-gate && !(aux->trust = sk_ASN1_OBJECT_new_null())) return 0;
1450Sstevel@tonic-gate return sk_ASN1_OBJECT_push(aux->trust, objtmp);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate
X509_add1_reject_object(X509 * x,ASN1_OBJECT * obj)1480Sstevel@tonic-gate int X509_add1_reject_object(X509 *x, ASN1_OBJECT *obj)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate X509_CERT_AUX *aux;
1510Sstevel@tonic-gate ASN1_OBJECT *objtmp;
1520Sstevel@tonic-gate if(!(objtmp = OBJ_dup(obj))) return 0;
1530Sstevel@tonic-gate if(!(aux = aux_get(x))) return 0;
1540Sstevel@tonic-gate if(!aux->reject
1550Sstevel@tonic-gate && !(aux->reject = sk_ASN1_OBJECT_new_null())) return 0;
1560Sstevel@tonic-gate return sk_ASN1_OBJECT_push(aux->reject, objtmp);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
X509_trust_clear(X509 * x)1590Sstevel@tonic-gate void X509_trust_clear(X509 *x)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate if(x->aux && x->aux->trust) {
1620Sstevel@tonic-gate sk_ASN1_OBJECT_pop_free(x->aux->trust, ASN1_OBJECT_free);
1630Sstevel@tonic-gate x->aux->trust = NULL;
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate
X509_reject_clear(X509 * x)1670Sstevel@tonic-gate void X509_reject_clear(X509 *x)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate if(x->aux && x->aux->reject) {
1700Sstevel@tonic-gate sk_ASN1_OBJECT_pop_free(x->aux->reject, ASN1_OBJECT_free);
1710Sstevel@tonic-gate x->aux->reject = NULL;
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
175*2139Sjp161948 ASN1_SEQUENCE(X509_CERT_PAIR) = {
176*2139Sjp161948 ASN1_EXP_OPT(X509_CERT_PAIR, forward, X509, 0),
177*2139Sjp161948 ASN1_EXP_OPT(X509_CERT_PAIR, reverse, X509, 1)
178*2139Sjp161948 } ASN1_SEQUENCE_END(X509_CERT_PAIR)
179*2139Sjp161948
180*2139Sjp161948 IMPLEMENT_ASN1_FUNCTIONS(X509_CERT_PAIR)
181