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>
64238405Sjkim#include <openssl/asn1t.h>
6555714Skris#include <openssl/x509.h>
6655714Skris#include <openssl/objects.h>
6755714Skris#include <openssl/buffer.h>
6855714Skris#include <openssl/pem.h>
6955714Skris
7059191SkrisX509_REQ *X509_to_X509_REQ(X509 *x, EVP_PKEY *pkey, const EVP_MD *md)
7155714Skris	{
7255714Skris	X509_REQ *ret;
7355714Skris	X509_REQ_INFO *ri;
7455714Skris	int i;
7555714Skris	EVP_PKEY *pktmp;
7655714Skris
7755714Skris	ret=X509_REQ_new();
7855714Skris	if (ret == NULL)
7955714Skris		{
8055714Skris		X509err(X509_F_X509_TO_X509_REQ,ERR_R_MALLOC_FAILURE);
8155714Skris		goto err;
8255714Skris		}
8355714Skris
8455714Skris	ri=ret->req_info;
8555714Skris
8655714Skris	ri->version->length=1;
8768651Skris	ri->version->data=(unsigned char *)OPENSSL_malloc(1);
8855714Skris	if (ri->version->data == NULL) goto err;
8955714Skris	ri->version->data[0]=0; /* version == 0 */
9055714Skris
9155714Skris	if (!X509_REQ_set_subject_name(ret,X509_get_subject_name(x)))
9255714Skris		goto err;
9355714Skris
9455714Skris	pktmp = X509_get_pubkey(x);
9555714Skris	i=X509_REQ_set_pubkey(ret,pktmp);
9655714Skris	EVP_PKEY_free(pktmp);
9755714Skris	if (!i) goto err;
9855714Skris
9955714Skris	if (pkey != NULL)
10055714Skris		{
10155714Skris		if (!X509_REQ_sign(ret,pkey,md))
10255714Skris			goto err;
10355714Skris		}
10455714Skris	return(ret);
10555714Skriserr:
10655714Skris	X509_REQ_free(ret);
10755714Skris	return(NULL);
10855714Skris	}
10955714Skris
11055714SkrisEVP_PKEY *X509_REQ_get_pubkey(X509_REQ *req)
11155714Skris	{
11255714Skris	if ((req == NULL) || (req->req_info == NULL))
11355714Skris		return(NULL);
11455714Skris	return(X509_PUBKEY_get(req->req_info->pubkey));
11555714Skris	}
11655714Skris
117160814Ssimonint X509_REQ_check_private_key(X509_REQ *x, EVP_PKEY *k)
118160814Ssimon	{
119160814Ssimon	EVP_PKEY *xk=NULL;
120160814Ssimon	int ok=0;
121160814Ssimon
122160814Ssimon	xk=X509_REQ_get_pubkey(x);
123160814Ssimon	switch (EVP_PKEY_cmp(xk, k))
124160814Ssimon		{
125160814Ssimon	case 1:
126160814Ssimon		ok=1;
127160814Ssimon		break;
128160814Ssimon	case 0:
129160814Ssimon		X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH);
130160814Ssimon		break;
131160814Ssimon	case -1:
132160814Ssimon		X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,X509_R_KEY_TYPE_MISMATCH);
133160814Ssimon		break;
134160814Ssimon	case -2:
135160814Ssimon#ifndef OPENSSL_NO_EC
136160814Ssimon		if (k->type == EVP_PKEY_EC)
137160814Ssimon			{
138160814Ssimon			X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, ERR_R_EC_LIB);
139160814Ssimon			break;
140160814Ssimon			}
141160814Ssimon#endif
142160814Ssimon#ifndef OPENSSL_NO_DH
143160814Ssimon		if (k->type == EVP_PKEY_DH)
144160814Ssimon			{
145160814Ssimon			/* No idea */
146160814Ssimon			X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,X509_R_CANT_CHECK_DH_KEY);
147160814Ssimon			break;
148160814Ssimon			}
149160814Ssimon#endif
150160814Ssimon	        X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,X509_R_UNKNOWN_KEY_TYPE);
151160814Ssimon		}
152160814Ssimon
153160814Ssimon	EVP_PKEY_free(xk);
154160814Ssimon	return(ok);
155160814Ssimon	}
156160814Ssimon
15759191Skris/* It seems several organisations had the same idea of including a list of
15859191Skris * extensions in a certificate request. There are at least two OIDs that are
15959191Skris * used and there may be more: so the list is configurable.
16059191Skris */
16159191Skris
162142425Snectarstatic int ext_nid_list[] = { NID_ext_req, NID_ms_ext_req, NID_undef};
16359191Skris
16459191Skrisstatic int *ext_nids = ext_nid_list;
16559191Skris
16659191Skrisint X509_REQ_extension_nid(int req_nid)
16759191Skris{
16859191Skris	int i, nid;
16959191Skris	for(i = 0; ; i++) {
17059191Skris		nid = ext_nids[i];
17159191Skris		if(nid == NID_undef) return 0;
17259191Skris		else if (req_nid == nid) return 1;
17359191Skris	}
17459191Skris}
17559191Skris
17659191Skrisint *X509_REQ_get_extension_nids(void)
17759191Skris{
17859191Skris	return ext_nids;
17959191Skris}
18059191Skris
18159191Skrisvoid X509_REQ_set_extension_nids(int *nids)
18259191Skris{
18359191Skris	ext_nids = nids;
18459191Skris}
18559191Skris
18659191SkrisSTACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req)
187142425Snectar	{
18859191Skris	X509_ATTRIBUTE *attr;
18959191Skris	ASN1_TYPE *ext = NULL;
190142425Snectar	int idx, *pnid;
191160814Ssimon	const unsigned char *p;
192142425Snectar
193142425Snectar	if ((req == NULL) || (req->req_info == NULL) || !ext_nids)
19459191Skris		return(NULL);
195142425Snectar	for (pnid = ext_nids; *pnid != NID_undef; pnid++)
196142425Snectar		{
197142425Snectar		idx = X509_REQ_get_attr_by_NID(req, *pnid, -1);
198142425Snectar		if (idx == -1)
199142425Snectar			continue;
200142425Snectar		attr = X509_REQ_get_attr(req, idx);
201142425Snectar		if(attr->single) ext = attr->value.single;
202142425Snectar		else if(sk_ASN1_TYPE_num(attr->value.set))
203142425Snectar			ext = sk_ASN1_TYPE_value(attr->value.set, 0);
204142425Snectar		break;
20559191Skris		}
206142425Snectar	if(!ext || (ext->type != V_ASN1_SEQUENCE))
207142425Snectar		return NULL;
20859191Skris	p = ext->value.sequence->data;
209238405Sjkim	return (STACK_OF(X509_EXTENSION) *)
210238405Sjkim		ASN1_item_d2i(NULL, &p, ext->value.sequence->length,
211238405Sjkim				ASN1_ITEM_rptr(X509_EXTENSIONS));
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	ASN1_TYPE *at = NULL;
22259191Skris	X509_ATTRIBUTE *attr = NULL;
22359191Skris	if(!(at = ASN1_TYPE_new()) ||
22459191Skris		!(at->value.sequence = ASN1_STRING_new())) goto err;
22559191Skris
22659191Skris	at->type = V_ASN1_SEQUENCE;
22759191Skris	/* Generate encoding of extensions */
228238405Sjkim	at->value.sequence->length =
229238405Sjkim			ASN1_item_i2d((ASN1_VALUE *)exts,
230238405Sjkim				&at->value.sequence->data,
231238405Sjkim				ASN1_ITEM_rptr(X509_EXTENSIONS));
23259191Skris	if(!(attr = X509_ATTRIBUTE_new())) goto err;
23359191Skris	if(!(attr->value.set = sk_ASN1_TYPE_new_null())) goto err;
23459191Skris	if(!sk_ASN1_TYPE_push(attr->value.set, at)) goto err;
23559191Skris	at = NULL;
236109998Smarkm	attr->single = 0;
23759191Skris	attr->object = OBJ_nid2obj(nid);
238167612Ssimon	if (!req->req_info->attributes)
239167612Ssimon		{
240167612Ssimon		if (!(req->req_info->attributes = sk_X509_ATTRIBUTE_new_null()))
241167612Ssimon			goto err;
242167612Ssimon		}
24359191Skris	if(!sk_X509_ATTRIBUTE_push(req->req_info->attributes, attr)) goto err;
24459191Skris	return 1;
24559191Skris	err:
24659191Skris	X509_ATTRIBUTE_free(attr);
24759191Skris	ASN1_TYPE_free(at);
24859191Skris	return 0;
24959191Skris}
25059191Skris/* This is the normal usage: use the "official" OID */
25159191Skrisint X509_REQ_add_extensions(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts)
25259191Skris{
25359191Skris	return X509_REQ_add_extensions_nid(req, exts, NID_ext_req);
25459191Skris}
25559191Skris
25659191Skris/* Request attribute functions */
25759191Skris
25859191Skrisint X509_REQ_get_attr_count(const X509_REQ *req)
25959191Skris{
26059191Skris	return X509at_get_attr_count(req->req_info->attributes);
26159191Skris}
26259191Skris
26359191Skrisint X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid,
26459191Skris			  int lastpos)
26559191Skris{
26659191Skris	return X509at_get_attr_by_NID(req->req_info->attributes, nid, lastpos);
26759191Skris}
26859191Skris
26959191Skrisint X509_REQ_get_attr_by_OBJ(const X509_REQ *req, ASN1_OBJECT *obj,
27059191Skris			  int lastpos)
27159191Skris{
27259191Skris	return X509at_get_attr_by_OBJ(req->req_info->attributes, obj, lastpos);
27359191Skris}
27459191Skris
27559191SkrisX509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc)
27659191Skris{
27759191Skris	return X509at_get_attr(req->req_info->attributes, loc);
27859191Skris}
27959191Skris
28059191SkrisX509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc)
28159191Skris{
28259191Skris	return X509at_delete_attr(req->req_info->attributes, loc);
28359191Skris}
28459191Skris
28559191Skrisint X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr)
28659191Skris{
28759191Skris	if(X509at_add1_attr(&req->req_info->attributes, attr)) return 1;
28859191Skris	return 0;
28959191Skris}
29059191Skris
29159191Skrisint X509_REQ_add1_attr_by_OBJ(X509_REQ *req,
292109998Smarkm			const ASN1_OBJECT *obj, int type,
293109998Smarkm			const unsigned char *bytes, int len)
29459191Skris{
29559191Skris	if(X509at_add1_attr_by_OBJ(&req->req_info->attributes, obj,
29659191Skris				type, bytes, len)) return 1;
29759191Skris	return 0;
29859191Skris}
29959191Skris
30059191Skrisint X509_REQ_add1_attr_by_NID(X509_REQ *req,
30159191Skris			int nid, int type,
302109998Smarkm			const unsigned char *bytes, int len)
30359191Skris{
30459191Skris	if(X509at_add1_attr_by_NID(&req->req_info->attributes, nid,
30559191Skris				type, bytes, len)) return 1;
30659191Skris	return 0;
30759191Skris}
30859191Skris
30959191Skrisint X509_REQ_add1_attr_by_txt(X509_REQ *req,
310109998Smarkm			const char *attrname, int type,
311109998Smarkm			const unsigned char *bytes, int len)
31259191Skris{
31359191Skris	if(X509at_add1_attr_by_txt(&req->req_info->attributes, attrname,
31459191Skris				type, bytes, len)) return 1;
31559191Skris	return 0;
31659191Skris}
317