x509_req.c revision 59191
155714Skris/* crypto/x509/x509_req.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 855714Skris * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1555714Skris * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 2255714Skris * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4055714Skris * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 5255714Skris * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include "cryptlib.h" 6155714Skris#include <openssl/bn.h> 6255714Skris#include <openssl/evp.h> 6355714Skris#include <openssl/asn1.h> 6455714Skris#include <openssl/x509.h> 6555714Skris#include <openssl/objects.h> 6655714Skris#include <openssl/buffer.h> 6755714Skris#include <openssl/pem.h> 6855714Skris 6959191SkrisX509_REQ *X509_to_X509_REQ(X509 *x, EVP_PKEY *pkey, const EVP_MD *md) 7055714Skris { 7155714Skris X509_REQ *ret; 7255714Skris X509_REQ_INFO *ri; 7355714Skris int i; 7455714Skris EVP_PKEY *pktmp; 7555714Skris 7655714Skris ret=X509_REQ_new(); 7755714Skris if (ret == NULL) 7855714Skris { 7955714Skris X509err(X509_F_X509_TO_X509_REQ,ERR_R_MALLOC_FAILURE); 8055714Skris goto err; 8155714Skris } 8255714Skris 8355714Skris ri=ret->req_info; 8455714Skris 8555714Skris ri->version->length=1; 8655714Skris ri->version->data=(unsigned char *)Malloc(1); 8755714Skris if (ri->version->data == NULL) goto err; 8855714Skris ri->version->data[0]=0; /* version == 0 */ 8955714Skris 9055714Skris if (!X509_REQ_set_subject_name(ret,X509_get_subject_name(x))) 9155714Skris goto err; 9255714Skris 9355714Skris pktmp = X509_get_pubkey(x); 9455714Skris i=X509_REQ_set_pubkey(ret,pktmp); 9555714Skris EVP_PKEY_free(pktmp); 9655714Skris if (!i) goto err; 9755714Skris 9855714Skris if (pkey != NULL) 9955714Skris { 10055714Skris if (!X509_REQ_sign(ret,pkey,md)) 10155714Skris goto err; 10255714Skris } 10355714Skris return(ret); 10455714Skriserr: 10555714Skris X509_REQ_free(ret); 10655714Skris return(NULL); 10755714Skris } 10855714Skris 10955714SkrisEVP_PKEY *X509_REQ_get_pubkey(X509_REQ *req) 11055714Skris { 11155714Skris if ((req == NULL) || (req->req_info == NULL)) 11255714Skris return(NULL); 11355714Skris return(X509_PUBKEY_get(req->req_info->pubkey)); 11455714Skris } 11555714Skris 11659191Skris/* It seems several organisations had the same idea of including a list of 11759191Skris * extensions in a certificate request. There are at least two OIDs that are 11859191Skris * used and there may be more: so the list is configurable. 11959191Skris */ 12059191Skris 12159191Skrisstatic int ext_nid_list[] = { NID_ms_ext_req, NID_ext_req, NID_undef}; 12259191Skris 12359191Skrisstatic int *ext_nids = ext_nid_list; 12459191Skris 12559191Skrisint X509_REQ_extension_nid(int req_nid) 12659191Skris{ 12759191Skris int i, nid; 12859191Skris for(i = 0; ; i++) { 12959191Skris nid = ext_nids[i]; 13059191Skris if(nid == NID_undef) return 0; 13159191Skris else if (req_nid == nid) return 1; 13259191Skris } 13359191Skris} 13459191Skris 13559191Skrisint *X509_REQ_get_extension_nids(void) 13659191Skris{ 13759191Skris return ext_nids; 13859191Skris} 13959191Skris 14059191Skrisvoid X509_REQ_set_extension_nids(int *nids) 14159191Skris{ 14259191Skris ext_nids = nids; 14359191Skris} 14459191Skris 14559191SkrisSTACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req) 14659191Skris{ 14759191Skris X509_ATTRIBUTE *attr; 14859191Skris STACK_OF(X509_ATTRIBUTE) *sk; 14959191Skris ASN1_TYPE *ext = NULL; 15059191Skris int i; 15159191Skris unsigned char *p; 15259191Skris if ((req == NULL) || (req->req_info == NULL)) 15359191Skris return(NULL); 15459191Skris sk=req->req_info->attributes; 15559191Skris if (!sk) return NULL; 15659191Skris for(i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) { 15759191Skris attr = sk_X509_ATTRIBUTE_value(sk, i); 15859191Skris if(X509_REQ_extension_nid(OBJ_obj2nid(attr->object))) { 15959191Skris if(attr->set && sk_ASN1_TYPE_num(attr->value.set)) 16059191Skris ext = sk_ASN1_TYPE_value(attr->value.set, 0); 16159191Skris else ext = attr->value.single; 16259191Skris break; 16359191Skris } 16459191Skris } 16559191Skris if(!ext || (ext->type != V_ASN1_SEQUENCE)) return NULL; 16659191Skris p = ext->value.sequence->data; 16759191Skris return d2i_ASN1_SET_OF_X509_EXTENSION(NULL, &p, 16859191Skris ext->value.sequence->length, 16959191Skris d2i_X509_EXTENSION, X509_EXTENSION_free, 17059191Skris V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL); 17159191Skris} 17259191Skris 17359191Skris/* Add a STACK_OF extensions to a certificate request: allow alternative OIDs 17459191Skris * in case we want to create a non standard one. 17559191Skris */ 17659191Skris 17759191Skrisint X509_REQ_add_extensions_nid(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts, 17859191Skris int nid) 17959191Skris{ 18059191Skris unsigned char *p = NULL, *q; 18159191Skris long len; 18259191Skris ASN1_TYPE *at = NULL; 18359191Skris X509_ATTRIBUTE *attr = NULL; 18459191Skris if(!(at = ASN1_TYPE_new()) || 18559191Skris !(at->value.sequence = ASN1_STRING_new())) goto err; 18659191Skris 18759191Skris at->type = V_ASN1_SEQUENCE; 18859191Skris /* Generate encoding of extensions */ 18959191Skris len = i2d_ASN1_SET_OF_X509_EXTENSION(exts, NULL, i2d_X509_EXTENSION, 19059191Skris V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, IS_SEQUENCE); 19159191Skris if(!(p = Malloc(len))) goto err; 19259191Skris q = p; 19359191Skris i2d_ASN1_SET_OF_X509_EXTENSION(exts, &q, i2d_X509_EXTENSION, 19459191Skris V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, IS_SEQUENCE); 19559191Skris at->value.sequence->data = p; 19659191Skris p = NULL; 19759191Skris at->value.sequence->length = len; 19859191Skris if(!(attr = X509_ATTRIBUTE_new())) goto err; 19959191Skris if(!(attr->value.set = sk_ASN1_TYPE_new_null())) goto err; 20059191Skris if(!sk_ASN1_TYPE_push(attr->value.set, at)) goto err; 20159191Skris at = NULL; 20259191Skris attr->set = 1; 20359191Skris attr->object = OBJ_nid2obj(nid); 20459191Skris if(!sk_X509_ATTRIBUTE_push(req->req_info->attributes, attr)) goto err; 20559191Skris return 1; 20659191Skris err: 20759191Skris if(p) Free(p); 20859191Skris X509_ATTRIBUTE_free(attr); 20959191Skris ASN1_TYPE_free(at); 21059191Skris return 0; 21159191Skris} 21259191Skris/* This is the normal usage: use the "official" OID */ 21359191Skrisint X509_REQ_add_extensions(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts) 21459191Skris{ 21559191Skris return X509_REQ_add_extensions_nid(req, exts, NID_ext_req); 21659191Skris} 21759191Skris 21859191Skris/* Request attribute functions */ 21959191Skris 22059191Skrisint X509_REQ_get_attr_count(const X509_REQ *req) 22159191Skris{ 22259191Skris return X509at_get_attr_count(req->req_info->attributes); 22359191Skris} 22459191Skris 22559191Skrisint X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, 22659191Skris int lastpos) 22759191Skris{ 22859191Skris return X509at_get_attr_by_NID(req->req_info->attributes, nid, lastpos); 22959191Skris} 23059191Skris 23159191Skrisint X509_REQ_get_attr_by_OBJ(const X509_REQ *req, ASN1_OBJECT *obj, 23259191Skris int lastpos) 23359191Skris{ 23459191Skris return X509at_get_attr_by_OBJ(req->req_info->attributes, obj, lastpos); 23559191Skris} 23659191Skris 23759191SkrisX509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc) 23859191Skris{ 23959191Skris return X509at_get_attr(req->req_info->attributes, loc); 24059191Skris} 24159191Skris 24259191SkrisX509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc) 24359191Skris{ 24459191Skris return X509at_delete_attr(req->req_info->attributes, loc); 24559191Skris} 24659191Skris 24759191Skrisint X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr) 24859191Skris{ 24959191Skris if(X509at_add1_attr(&req->req_info->attributes, attr)) return 1; 25059191Skris return 0; 25159191Skris} 25259191Skris 25359191Skrisint X509_REQ_add1_attr_by_OBJ(X509_REQ *req, 25459191Skris ASN1_OBJECT *obj, int type, 25559191Skris unsigned char *bytes, int len) 25659191Skris{ 25759191Skris if(X509at_add1_attr_by_OBJ(&req->req_info->attributes, obj, 25859191Skris type, bytes, len)) return 1; 25959191Skris return 0; 26059191Skris} 26159191Skris 26259191Skrisint X509_REQ_add1_attr_by_NID(X509_REQ *req, 26359191Skris int nid, int type, 26459191Skris unsigned char *bytes, int len) 26559191Skris{ 26659191Skris if(X509at_add1_attr_by_NID(&req->req_info->attributes, nid, 26759191Skris type, bytes, len)) return 1; 26859191Skris return 0; 26959191Skris} 27059191Skris 27159191Skrisint X509_REQ_add1_attr_by_txt(X509_REQ *req, 27259191Skris char *attrname, int type, 27359191Skris unsigned char *bytes, int len) 27459191Skris{ 27559191Skris if(X509at_add1_attr_by_txt(&req->req_info->attributes, attrname, 27659191Skris type, bytes, len)) return 1; 27759191Skris return 0; 27859191Skris} 279