1656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* crypto/x509/x509_att.c */ 2656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * All rights reserved. 4656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 5656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This package is an SSL implementation written 6656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * by Eric Young (eay@cryptsoft.com). 7656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * The implementation was written so as to conform with Netscapes SSL. 8656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 9656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This library is free for commercial and non-commercial use as long as 10656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the following conditions are aheared to. The following conditions 11656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * apply to all code found in this distribution, be it the RC4, RSA, 12656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * included with this distribution is covered by the same copyright terms 14656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 16656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Copyright remains Eric Young's, and as such any Copyright notices in 17656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the code are not to be removed. 18656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * If this package is used in a product, Eric Young should be given attribution 19656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * as the author of the parts of the library used. 20656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This can be in the form of a textual message at program startup or 21656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * in documentation (online or textual) provided with the package. 22656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 23656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Redistribution and use in source and binary forms, with or without 24656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * modification, are permitted provided that the following conditions 25656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * are met: 26656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 1. Redistributions of source code must retain the copyright 27656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * notice, this list of conditions and the following disclaimer. 28656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright 29656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * notice, this list of conditions and the following disclaimer in the 30656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * documentation and/or other materials provided with the distribution. 31656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 3. All advertising materials mentioning features or use of this software 32656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * must display the following acknowledgement: 33656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * "This product includes cryptographic software written by 34656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Eric Young (eay@cryptsoft.com)" 35656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * The word 'cryptographic' can be left out if the rouines from the library 36656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * being used are not cryptographic related :-). 37656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 4. If you include any Windows specific code (or a derivative thereof) from 38656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * the apps directory (application code) you must include an acknowledgement: 39656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 41656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * SUCH DAMAGE. 52656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 53656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * The licence and distribution terms for any publically available version or 54656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * derivative of this code cannot be changed. i.e. this code cannot simply be 55656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * copied and put under another distribution licence 56656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * [including the GNU Public Licence.] 57656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */ 58656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 59656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <stdio.h> 60656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/stack.h> 61656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include "cryptlib.h" 62656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/asn1.h> 63656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/objects.h> 64656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/evp.h> 65656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/x509.h> 66656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/x509v3.h> 67656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 68656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x) 69656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 70656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return sk_X509_ATTRIBUTE_num(x); 71656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 72656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 73656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid, 74656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int lastpos) 75656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 76656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ASN1_OBJECT *obj; 77656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 78656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project obj=OBJ_nid2obj(nid); 79656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (obj == NULL) return(-2); 80656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(X509at_get_attr_by_OBJ(x,obj,lastpos)); 81656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 82656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 83656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk, ASN1_OBJECT *obj, 84656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int lastpos) 85656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 86656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int n; 87656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_ATTRIBUTE *ex; 88656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 89656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (sk == NULL) return(-1); 90656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project lastpos++; 91656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (lastpos < 0) 92656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project lastpos=0; 93656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project n=sk_X509_ATTRIBUTE_num(sk); 94656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project for ( ; lastpos < n; lastpos++) 95656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 96656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ex=sk_X509_ATTRIBUTE_value(sk,lastpos); 97656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (OBJ_cmp(ex->object,obj) == 0) 98656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(lastpos); 99656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 100656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(-1); 101656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 102656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 103656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectX509_ATTRIBUTE *X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc) 104656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 105656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0) 106656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return NULL; 107656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 108656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return sk_X509_ATTRIBUTE_value(x,loc); 109656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 110656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 111656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectX509_ATTRIBUTE *X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc) 112656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 113656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_ATTRIBUTE *ret; 114656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 115656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0) 116656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(NULL); 117656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ret=sk_X509_ATTRIBUTE_delete(x,loc); 118656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(ret); 119656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 120656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 121656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectSTACK_OF(X509_ATTRIBUTE) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x, 122656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_ATTRIBUTE *attr) 123656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 124656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_ATTRIBUTE *new_attr=NULL; 125656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project STACK_OF(X509_ATTRIBUTE) *sk=NULL; 126656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 127656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (x == NULL) 128656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 129656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509err(X509_F_X509AT_ADD1_ATTR, ERR_R_PASSED_NULL_PARAMETER); 130656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project goto err2; 131656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 132656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 133656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (*x == NULL) 134656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 135656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if ((sk=sk_X509_ATTRIBUTE_new_null()) == NULL) 136656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project goto err; 137656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 138656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 139656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project sk= *x; 140656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 141656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if ((new_attr=X509_ATTRIBUTE_dup(attr)) == NULL) 142656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project goto err2; 143656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!sk_X509_ATTRIBUTE_push(sk,new_attr)) 144656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project goto err; 145656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (*x == NULL) 146656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *x=sk; 147656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(sk); 148656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecterr: 149656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509err(X509_F_X509AT_ADD1_ATTR,ERR_R_MALLOC_FAILURE); 150656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecterr2: 151656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (new_attr != NULL) X509_ATTRIBUTE_free(new_attr); 152656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (sk != NULL) sk_X509_ATTRIBUTE_free(sk); 153656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(NULL); 154656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 155656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 156656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectSTACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE) **x, 157656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project const ASN1_OBJECT *obj, int type, 158656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project const unsigned char *bytes, int len) 159656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 160656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_ATTRIBUTE *attr; 161656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project STACK_OF(X509_ATTRIBUTE) *ret; 162656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len); 163656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!attr) return 0; 164656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ret = X509at_add1_attr(x, attr); 165656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_ATTRIBUTE_free(attr); 166656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return ret; 167656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 168656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 169656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectSTACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE) **x, 170656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int nid, int type, 171656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project const unsigned char *bytes, int len) 172656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 173656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_ATTRIBUTE *attr; 174656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project STACK_OF(X509_ATTRIBUTE) *ret; 175656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len); 176656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!attr) return 0; 177656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ret = X509at_add1_attr(x, attr); 178656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_ATTRIBUTE_free(attr); 179656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return ret; 180656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 181656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 182656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectSTACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE) **x, 183656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project const char *attrname, int type, 184656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project const unsigned char *bytes, int len) 185656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 186656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_ATTRIBUTE *attr; 187656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project STACK_OF(X509_ATTRIBUTE) *ret; 188656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len); 189656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!attr) return 0; 190656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ret = X509at_add1_attr(x, attr); 191656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_ATTRIBUTE_free(attr); 192656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return ret; 193656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 194656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 195656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectvoid *X509at_get0_data_by_OBJ(STACK_OF(X509_ATTRIBUTE) *x, 196656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ASN1_OBJECT *obj, int lastpos, int type) 197656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 198656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int i; 199656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_ATTRIBUTE *at; 200656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project i = X509at_get_attr_by_OBJ(x, obj, lastpos); 201656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (i == -1) 202656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return NULL; 203656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if ((lastpos <= -2) && (X509at_get_attr_by_OBJ(x, obj, i) != -1)) 204656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return NULL; 205656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project at = X509at_get_attr(x, i); 206656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (lastpos <= -3 && (X509_ATTRIBUTE_count(at) != 1)) 207656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return NULL; 208656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return X509_ATTRIBUTE_get0_data(at, 0, type, NULL); 209656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 210656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 211656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectX509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid, 212656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int atrtype, const void *data, int len) 213656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 214656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ASN1_OBJECT *obj; 215656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_ATTRIBUTE *ret; 216656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 217656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project obj=OBJ_nid2obj(nid); 218656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (obj == NULL) 219656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 220656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_NID,X509_R_UNKNOWN_NID); 221656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(NULL); 222656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 223656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ret=X509_ATTRIBUTE_create_by_OBJ(attr,obj,atrtype,data,len); 224656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (ret == NULL) ASN1_OBJECT_free(obj); 225656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(ret); 226656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 227656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 228656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectX509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr, 229656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project const ASN1_OBJECT *obj, int atrtype, const void *data, int len) 230656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 231656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_ATTRIBUTE *ret; 232656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 233656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if ((attr == NULL) || (*attr == NULL)) 234656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 235656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if ((ret=X509_ATTRIBUTE_new()) == NULL) 236656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 237656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_OBJ,ERR_R_MALLOC_FAILURE); 238656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(NULL); 239656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 240656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 241656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 242656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ret= *attr; 243656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 244656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!X509_ATTRIBUTE_set1_object(ret,obj)) 245656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project goto err; 246656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!X509_ATTRIBUTE_set1_data(ret,atrtype,data,len)) 247656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project goto err; 248e45f106cb6b47af1f21efe76e933bdea2f5dd1caNagendra Modadugu 249656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if ((attr != NULL) && (*attr == NULL)) *attr=ret; 250656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(ret); 251656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projecterr: 252656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if ((attr == NULL) || (ret != *attr)) 253656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_ATTRIBUTE_free(ret); 254656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(NULL); 255656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 256656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 257656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectX509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr, 258656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project const char *atrname, int type, const unsigned char *bytes, int len) 259656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 260656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ASN1_OBJECT *obj; 261656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_ATTRIBUTE *nattr; 262656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 263656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project obj=OBJ_txt2obj(atrname, 0); 264656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (obj == NULL) 265656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 266656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_TXT, 267656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509_R_INVALID_FIELD_NAME); 268656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ERR_add_error_data(2, "name=", atrname); 269656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(NULL); 270656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 271656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project nattr = X509_ATTRIBUTE_create_by_OBJ(attr,obj,type,bytes,len); 272656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ASN1_OBJECT_free(obj); 273656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return nattr; 274656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 275656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 276656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj) 277656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 278656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if ((attr == NULL) || (obj == NULL)) 279656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(0); 280656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ASN1_OBJECT_free(attr->object); 281656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project attr->object=OBJ_dup(obj); 282656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(1); 283656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 284656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 285656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, const void *data, int len) 286656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 287656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ASN1_TYPE *ttmp; 288656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ASN1_STRING *stmp = NULL; 289656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int atype = 0; 290656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!attr) return 0; 291656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(attrtype & MBSTRING_FLAG) { 292656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype, 293656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project OBJ_obj2nid(attr->object)); 294656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!stmp) { 295656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_ASN1_LIB); 296656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return 0; 297656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 298656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project atype = stmp->type; 299656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } else if (len != -1){ 300656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!(stmp = ASN1_STRING_type_new(attrtype))) goto err; 301656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!ASN1_STRING_set(stmp, data, len)) goto err; 302656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project atype = attrtype; 303656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 304656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!(attr->value.set = sk_ASN1_TYPE_new_null())) goto err; 305e45f106cb6b47af1f21efe76e933bdea2f5dd1caNagendra Modadugu attr->single = 0; 306e45f106cb6b47af1f21efe76e933bdea2f5dd1caNagendra Modadugu /* This is a bit naughty because the attribute should really have 307e45f106cb6b47af1f21efe76e933bdea2f5dd1caNagendra Modadugu * at least one value but some types use and zero length SET and 308e45f106cb6b47af1f21efe76e933bdea2f5dd1caNagendra Modadugu * require this. 309e45f106cb6b47af1f21efe76e933bdea2f5dd1caNagendra Modadugu */ 310e45f106cb6b47af1f21efe76e933bdea2f5dd1caNagendra Modadugu if (attrtype == 0) 311e45f106cb6b47af1f21efe76e933bdea2f5dd1caNagendra Modadugu return 1; 312656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!(ttmp = ASN1_TYPE_new())) goto err; 313e45f106cb6b47af1f21efe76e933bdea2f5dd1caNagendra Modadugu if ((len == -1) && !(attrtype & MBSTRING_FLAG)) 314656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project { 315656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (!ASN1_TYPE_set1(ttmp, attrtype, data)) 316656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project goto err; 317656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 318656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else 319656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ASN1_TYPE_set(ttmp, atype, stmp); 320656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!sk_ASN1_TYPE_push(attr->value.set, ttmp)) goto err; 321656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return 1; 322656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project err: 323656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_MALLOC_FAILURE); 324656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return 0; 325656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 326656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 327656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectint X509_ATTRIBUTE_count(X509_ATTRIBUTE *attr) 328656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 329656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!attr->single) return sk_ASN1_TYPE_num(attr->value.set); 330656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(attr->value.single) return 1; 331656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return 0; 332656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 333656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 334656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr) 335656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 336656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (attr == NULL) return(NULL); 337656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return(attr->object); 338656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 339656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 340656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Projectvoid *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx, 341656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project int atrtype, void *data) 342656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 343656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ASN1_TYPE *ttmp; 344656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project ttmp = X509_ATTRIBUTE_get0_type(attr, idx); 345656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!ttmp) return NULL; 346656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(atrtype != ASN1_TYPE_get(ttmp)){ 347656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project X509err(X509_F_X509_ATTRIBUTE_GET0_DATA, X509_R_WRONG_TYPE); 348656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return NULL; 349656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project } 350656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project return ttmp->value.ptr; 351656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 352656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project 353656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source ProjectASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx) 354656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project{ 355656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if (attr == NULL) return(NULL); 356656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(idx >= X509_ATTRIBUTE_count(attr)) return NULL; 357656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project if(!attr->single) return sk_ASN1_TYPE_value(attr->value.set, idx); 358656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project else return attr->value.single; 359656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project} 360