x509_req.c revision 167612
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;
8668651Skris	ri->version->data=(unsigned char *)OPENSSL_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
116160814Ssimonint X509_REQ_check_private_key(X509_REQ *x, EVP_PKEY *k)
117160814Ssimon	{
118160814Ssimon	EVP_PKEY *xk=NULL;
119160814Ssimon	int ok=0;
120160814Ssimon
121160814Ssimon	xk=X509_REQ_get_pubkey(x);
122160814Ssimon	switch (EVP_PKEY_cmp(xk, k))
123160814Ssimon		{
124160814Ssimon	case 1:
125160814Ssimon		ok=1;
126160814Ssimon		break;
127160814Ssimon	case 0:
128160814Ssimon		X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH);
129160814Ssimon		break;
130160814Ssimon	case -1:
131160814Ssimon		X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,X509_R_KEY_TYPE_MISMATCH);
132160814Ssimon		break;
133160814Ssimon	case -2:
134160814Ssimon#ifndef OPENSSL_NO_EC
135160814Ssimon		if (k->type == EVP_PKEY_EC)
136160814Ssimon			{
137160814Ssimon			X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, ERR_R_EC_LIB);
138160814Ssimon			break;
139160814Ssimon			}
140160814Ssimon#endif
141160814Ssimon#ifndef OPENSSL_NO_DH
142160814Ssimon		if (k->type == EVP_PKEY_DH)
143160814Ssimon			{
144160814Ssimon			/* No idea */
145160814Ssimon			X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,X509_R_CANT_CHECK_DH_KEY);
146160814Ssimon			break;
147160814Ssimon			}
148160814Ssimon#endif
149160814Ssimon	        X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,X509_R_UNKNOWN_KEY_TYPE);
150160814Ssimon		}
151160814Ssimon
152160814Ssimon	EVP_PKEY_free(xk);
153160814Ssimon	return(ok);
154160814Ssimon	}
155160814Ssimon
15659191Skris/* It seems several organisations had the same idea of including a list of
15759191Skris * extensions in a certificate request. There are at least two OIDs that are
15859191Skris * used and there may be more: so the list is configurable.
15959191Skris */
16059191Skris
161142425Snectarstatic int ext_nid_list[] = { NID_ext_req, NID_ms_ext_req, NID_undef};
16259191Skris
16359191Skrisstatic int *ext_nids = ext_nid_list;
16459191Skris
16559191Skrisint X509_REQ_extension_nid(int req_nid)
16659191Skris{
16759191Skris	int i, nid;
16859191Skris	for(i = 0; ; i++) {
16959191Skris		nid = ext_nids[i];
17059191Skris		if(nid == NID_undef) return 0;
17159191Skris		else if (req_nid == nid) return 1;
17259191Skris	}
17359191Skris}
17459191Skris
17559191Skrisint *X509_REQ_get_extension_nids(void)
17659191Skris{
17759191Skris	return ext_nids;
17859191Skris}
17959191Skris
18059191Skrisvoid X509_REQ_set_extension_nids(int *nids)
18159191Skris{
18259191Skris	ext_nids = nids;
18359191Skris}
18459191Skris
18559191SkrisSTACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req)
186142425Snectar	{
18759191Skris	X509_ATTRIBUTE *attr;
18859191Skris	ASN1_TYPE *ext = NULL;
189142425Snectar	int idx, *pnid;
190160814Ssimon	const unsigned char *p;
191142425Snectar
192142425Snectar	if ((req == NULL) || (req->req_info == NULL) || !ext_nids)
19359191Skris		return(NULL);
194142425Snectar	for (pnid = ext_nids; *pnid != NID_undef; pnid++)
195142425Snectar		{
196142425Snectar		idx = X509_REQ_get_attr_by_NID(req, *pnid, -1);
197142425Snectar		if (idx == -1)
198142425Snectar			continue;
199142425Snectar		attr = X509_REQ_get_attr(req, idx);
200142425Snectar		if(attr->single) ext = attr->value.single;
201142425Snectar		else if(sk_ASN1_TYPE_num(attr->value.set))
202142425Snectar			ext = sk_ASN1_TYPE_value(attr->value.set, 0);
203142425Snectar		break;
20459191Skris		}
205142425Snectar	if(!ext || (ext->type != V_ASN1_SEQUENCE))
206142425Snectar		return NULL;
20759191Skris	p = ext->value.sequence->data;
20859191Skris	return d2i_ASN1_SET_OF_X509_EXTENSION(NULL, &p,
20959191Skris			ext->value.sequence->length,
21059191Skris			d2i_X509_EXTENSION, X509_EXTENSION_free,
21159191Skris			V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL);
212160814Ssimon}
21359191Skris
21459191Skris/* Add a STACK_OF extensions to a certificate request: allow alternative OIDs
21559191Skris * in case we want to create a non standard one.
21659191Skris */
21759191Skris
21859191Skrisint X509_REQ_add_extensions_nid(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts,
21959191Skris				int nid)
22059191Skris{
22159191Skris	unsigned char *p = NULL, *q;
22259191Skris	long len;
22359191Skris	ASN1_TYPE *at = NULL;
22459191Skris	X509_ATTRIBUTE *attr = NULL;
22559191Skris	if(!(at = ASN1_TYPE_new()) ||
22659191Skris		!(at->value.sequence = ASN1_STRING_new())) goto err;
22759191Skris
22859191Skris	at->type = V_ASN1_SEQUENCE;
22959191Skris	/* Generate encoding of extensions */
23059191Skris	len = i2d_ASN1_SET_OF_X509_EXTENSION(exts, NULL, i2d_X509_EXTENSION,
23159191Skris			V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, IS_SEQUENCE);
23268651Skris	if(!(p = OPENSSL_malloc(len))) goto err;
23359191Skris	q = p;
23459191Skris	i2d_ASN1_SET_OF_X509_EXTENSION(exts, &q, i2d_X509_EXTENSION,
23559191Skris			V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, IS_SEQUENCE);
23659191Skris	at->value.sequence->data = p;
23759191Skris	p = NULL;
23859191Skris	at->value.sequence->length = len;
23959191Skris	if(!(attr = X509_ATTRIBUTE_new())) goto err;
24059191Skris	if(!(attr->value.set = sk_ASN1_TYPE_new_null())) goto err;
24159191Skris	if(!sk_ASN1_TYPE_push(attr->value.set, at)) goto err;
24259191Skris	at = NULL;
243109998Smarkm	attr->single = 0;
24459191Skris	attr->object = OBJ_nid2obj(nid);
245167612Ssimon	if (!req->req_info->attributes)
246167612Ssimon		{
247167612Ssimon		if (!(req->req_info->attributes = sk_X509_ATTRIBUTE_new_null()))
248167612Ssimon			goto err;
249167612Ssimon		}
25059191Skris	if(!sk_X509_ATTRIBUTE_push(req->req_info->attributes, attr)) goto err;
25159191Skris	return 1;
25259191Skris	err:
25368651Skris	if(p) OPENSSL_free(p);
25459191Skris	X509_ATTRIBUTE_free(attr);
25559191Skris	ASN1_TYPE_free(at);
25659191Skris	return 0;
25759191Skris}
25859191Skris/* This is the normal usage: use the "official" OID */
25959191Skrisint X509_REQ_add_extensions(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts)
26059191Skris{
26159191Skris	return X509_REQ_add_extensions_nid(req, exts, NID_ext_req);
26259191Skris}
26359191Skris
26459191Skris/* Request attribute functions */
26559191Skris
26659191Skrisint X509_REQ_get_attr_count(const X509_REQ *req)
26759191Skris{
26859191Skris	return X509at_get_attr_count(req->req_info->attributes);
26959191Skris}
27059191Skris
27159191Skrisint X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid,
27259191Skris			  int lastpos)
27359191Skris{
27459191Skris	return X509at_get_attr_by_NID(req->req_info->attributes, nid, lastpos);
27559191Skris}
27659191Skris
27759191Skrisint X509_REQ_get_attr_by_OBJ(const X509_REQ *req, ASN1_OBJECT *obj,
27859191Skris			  int lastpos)
27959191Skris{
28059191Skris	return X509at_get_attr_by_OBJ(req->req_info->attributes, obj, lastpos);
28159191Skris}
28259191Skris
28359191SkrisX509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc)
28459191Skris{
28559191Skris	return X509at_get_attr(req->req_info->attributes, loc);
28659191Skris}
28759191Skris
28859191SkrisX509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc)
28959191Skris{
29059191Skris	return X509at_delete_attr(req->req_info->attributes, loc);
29159191Skris}
29259191Skris
29359191Skrisint X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr)
29459191Skris{
29559191Skris	if(X509at_add1_attr(&req->req_info->attributes, attr)) return 1;
29659191Skris	return 0;
29759191Skris}
29859191Skris
29959191Skrisint X509_REQ_add1_attr_by_OBJ(X509_REQ *req,
300109998Smarkm			const ASN1_OBJECT *obj, int type,
301109998Smarkm			const unsigned char *bytes, int len)
30259191Skris{
30359191Skris	if(X509at_add1_attr_by_OBJ(&req->req_info->attributes, obj,
30459191Skris				type, bytes, len)) return 1;
30559191Skris	return 0;
30659191Skris}
30759191Skris
30859191Skrisint X509_REQ_add1_attr_by_NID(X509_REQ *req,
30959191Skris			int nid, int type,
310109998Smarkm			const unsigned char *bytes, int len)
31159191Skris{
31259191Skris	if(X509at_add1_attr_by_NID(&req->req_info->attributes, nid,
31359191Skris				type, bytes, len)) return 1;
31459191Skris	return 0;
31559191Skris}
31659191Skris
31759191Skrisint X509_REQ_add1_attr_by_txt(X509_REQ *req,
318109998Smarkm			const char *attrname, int type,
319109998Smarkm			const unsigned char *bytes, int len)
32059191Skris{
32159191Skris	if(X509at_add1_attr_by_txt(&req->req_info->attributes, attrname,
32259191Skris				type, bytes, len)) return 1;
32359191Skris	return 0;
32459191Skris}
325