10Sstevel@tonic-gate /* crypto/x509/x509_req.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/bn.h>
620Sstevel@tonic-gate #include <openssl/evp.h>
630Sstevel@tonic-gate #include <openssl/asn1.h>
640Sstevel@tonic-gate #include <openssl/x509.h>
650Sstevel@tonic-gate #include <openssl/objects.h>
660Sstevel@tonic-gate #include <openssl/buffer.h>
670Sstevel@tonic-gate #include <openssl/pem.h>
680Sstevel@tonic-gate
X509_to_X509_REQ(X509 * x,EVP_PKEY * pkey,const EVP_MD * md)690Sstevel@tonic-gate X509_REQ *X509_to_X509_REQ(X509 *x, EVP_PKEY *pkey, const EVP_MD *md)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate X509_REQ *ret;
720Sstevel@tonic-gate X509_REQ_INFO *ri;
730Sstevel@tonic-gate int i;
740Sstevel@tonic-gate EVP_PKEY *pktmp;
750Sstevel@tonic-gate
760Sstevel@tonic-gate ret=X509_REQ_new();
770Sstevel@tonic-gate if (ret == NULL)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate X509err(X509_F_X509_TO_X509_REQ,ERR_R_MALLOC_FAILURE);
800Sstevel@tonic-gate goto err;
810Sstevel@tonic-gate }
820Sstevel@tonic-gate
830Sstevel@tonic-gate ri=ret->req_info;
840Sstevel@tonic-gate
850Sstevel@tonic-gate ri->version->length=1;
860Sstevel@tonic-gate ri->version->data=(unsigned char *)OPENSSL_malloc(1);
870Sstevel@tonic-gate if (ri->version->data == NULL) goto err;
880Sstevel@tonic-gate ri->version->data[0]=0; /* version == 0 */
890Sstevel@tonic-gate
900Sstevel@tonic-gate if (!X509_REQ_set_subject_name(ret,X509_get_subject_name(x)))
910Sstevel@tonic-gate goto err;
920Sstevel@tonic-gate
930Sstevel@tonic-gate pktmp = X509_get_pubkey(x);
940Sstevel@tonic-gate i=X509_REQ_set_pubkey(ret,pktmp);
950Sstevel@tonic-gate EVP_PKEY_free(pktmp);
960Sstevel@tonic-gate if (!i) goto err;
970Sstevel@tonic-gate
980Sstevel@tonic-gate if (pkey != NULL)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate if (!X509_REQ_sign(ret,pkey,md))
1010Sstevel@tonic-gate goto err;
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate return(ret);
1040Sstevel@tonic-gate err:
1050Sstevel@tonic-gate X509_REQ_free(ret);
1060Sstevel@tonic-gate return(NULL);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
X509_REQ_get_pubkey(X509_REQ * req)1090Sstevel@tonic-gate EVP_PKEY *X509_REQ_get_pubkey(X509_REQ *req)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate if ((req == NULL) || (req->req_info == NULL))
1120Sstevel@tonic-gate return(NULL);
1130Sstevel@tonic-gate return(X509_PUBKEY_get(req->req_info->pubkey));
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
X509_REQ_check_private_key(X509_REQ * x,EVP_PKEY * k)116*2139Sjp161948 int X509_REQ_check_private_key(X509_REQ *x, EVP_PKEY *k)
117*2139Sjp161948 {
118*2139Sjp161948 EVP_PKEY *xk=NULL;
119*2139Sjp161948 int ok=0;
120*2139Sjp161948
121*2139Sjp161948 xk=X509_REQ_get_pubkey(x);
122*2139Sjp161948 switch (EVP_PKEY_cmp(xk, k))
123*2139Sjp161948 {
124*2139Sjp161948 case 1:
125*2139Sjp161948 ok=1;
126*2139Sjp161948 break;
127*2139Sjp161948 case 0:
128*2139Sjp161948 X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH);
129*2139Sjp161948 break;
130*2139Sjp161948 case -1:
131*2139Sjp161948 X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,X509_R_KEY_TYPE_MISMATCH);
132*2139Sjp161948 break;
133*2139Sjp161948 case -2:
134*2139Sjp161948 #ifndef OPENSSL_NO_EC
135*2139Sjp161948 if (k->type == EVP_PKEY_EC)
136*2139Sjp161948 {
137*2139Sjp161948 X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, ERR_R_EC_LIB);
138*2139Sjp161948 break;
139*2139Sjp161948 }
140*2139Sjp161948 #endif
141*2139Sjp161948 #ifndef OPENSSL_NO_DH
142*2139Sjp161948 if (k->type == EVP_PKEY_DH)
143*2139Sjp161948 {
144*2139Sjp161948 /* No idea */
145*2139Sjp161948 X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,X509_R_CANT_CHECK_DH_KEY);
146*2139Sjp161948 break;
147*2139Sjp161948 }
148*2139Sjp161948 #endif
149*2139Sjp161948 X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,X509_R_UNKNOWN_KEY_TYPE);
150*2139Sjp161948 }
151*2139Sjp161948
152*2139Sjp161948 EVP_PKEY_free(xk);
153*2139Sjp161948 return(ok);
154*2139Sjp161948 }
155*2139Sjp161948
1560Sstevel@tonic-gate /* It seems several organisations had the same idea of including a list of
1570Sstevel@tonic-gate * extensions in a certificate request. There are at least two OIDs that are
1580Sstevel@tonic-gate * used and there may be more: so the list is configurable.
1590Sstevel@tonic-gate */
1600Sstevel@tonic-gate
161*2139Sjp161948 static int ext_nid_list[] = { NID_ext_req, NID_ms_ext_req, NID_undef};
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate static int *ext_nids = ext_nid_list;
1640Sstevel@tonic-gate
X509_REQ_extension_nid(int req_nid)1650Sstevel@tonic-gate int X509_REQ_extension_nid(int req_nid)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate int i, nid;
1680Sstevel@tonic-gate for(i = 0; ; i++) {
1690Sstevel@tonic-gate nid = ext_nids[i];
1700Sstevel@tonic-gate if(nid == NID_undef) return 0;
1710Sstevel@tonic-gate else if (req_nid == nid) return 1;
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
X509_REQ_get_extension_nids(void)1750Sstevel@tonic-gate int *X509_REQ_get_extension_nids(void)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate return ext_nids;
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate
X509_REQ_set_extension_nids(int * nids)1800Sstevel@tonic-gate void X509_REQ_set_extension_nids(int *nids)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate ext_nids = nids;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
STACK_OF(X509_EXTENSION)1850Sstevel@tonic-gate STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req)
186*2139Sjp161948 {
1870Sstevel@tonic-gate X509_ATTRIBUTE *attr;
1880Sstevel@tonic-gate ASN1_TYPE *ext = NULL;
189*2139Sjp161948 int idx, *pnid;
190*2139Sjp161948 const unsigned char *p;
191*2139Sjp161948
192*2139Sjp161948 if ((req == NULL) || (req->req_info == NULL) || !ext_nids)
1930Sstevel@tonic-gate return(NULL);
194*2139Sjp161948 for (pnid = ext_nids; *pnid != NID_undef; pnid++)
195*2139Sjp161948 {
196*2139Sjp161948 idx = X509_REQ_get_attr_by_NID(req, *pnid, -1);
197*2139Sjp161948 if (idx == -1)
198*2139Sjp161948 continue;
199*2139Sjp161948 attr = X509_REQ_get_attr(req, idx);
200*2139Sjp161948 if(attr->single) ext = attr->value.single;
201*2139Sjp161948 else if(sk_ASN1_TYPE_num(attr->value.set))
202*2139Sjp161948 ext = sk_ASN1_TYPE_value(attr->value.set, 0);
203*2139Sjp161948 break;
2040Sstevel@tonic-gate }
205*2139Sjp161948 if(!ext || (ext->type != V_ASN1_SEQUENCE))
206*2139Sjp161948 return NULL;
2070Sstevel@tonic-gate p = ext->value.sequence->data;
2080Sstevel@tonic-gate return d2i_ASN1_SET_OF_X509_EXTENSION(NULL, &p,
2090Sstevel@tonic-gate ext->value.sequence->length,
2100Sstevel@tonic-gate d2i_X509_EXTENSION, X509_EXTENSION_free,
2110Sstevel@tonic-gate V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate /* Add a STACK_OF extensions to a certificate request: allow alternative OIDs
2150Sstevel@tonic-gate * in case we want to create a non standard one.
2160Sstevel@tonic-gate */
2170Sstevel@tonic-gate
X509_REQ_add_extensions_nid(X509_REQ * req,STACK_OF (X509_EXTENSION)* exts,int nid)2180Sstevel@tonic-gate int X509_REQ_add_extensions_nid(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts,
2190Sstevel@tonic-gate int nid)
2200Sstevel@tonic-gate {
2210Sstevel@tonic-gate unsigned char *p = NULL, *q;
2220Sstevel@tonic-gate long len;
2230Sstevel@tonic-gate ASN1_TYPE *at = NULL;
2240Sstevel@tonic-gate X509_ATTRIBUTE *attr = NULL;
2250Sstevel@tonic-gate if(!(at = ASN1_TYPE_new()) ||
2260Sstevel@tonic-gate !(at->value.sequence = ASN1_STRING_new())) goto err;
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate at->type = V_ASN1_SEQUENCE;
2290Sstevel@tonic-gate /* Generate encoding of extensions */
2300Sstevel@tonic-gate len = i2d_ASN1_SET_OF_X509_EXTENSION(exts, NULL, i2d_X509_EXTENSION,
2310Sstevel@tonic-gate V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, IS_SEQUENCE);
2320Sstevel@tonic-gate if(!(p = OPENSSL_malloc(len))) goto err;
2330Sstevel@tonic-gate q = p;
2340Sstevel@tonic-gate i2d_ASN1_SET_OF_X509_EXTENSION(exts, &q, i2d_X509_EXTENSION,
2350Sstevel@tonic-gate V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, IS_SEQUENCE);
2360Sstevel@tonic-gate at->value.sequence->data = p;
2370Sstevel@tonic-gate p = NULL;
2380Sstevel@tonic-gate at->value.sequence->length = len;
2390Sstevel@tonic-gate if(!(attr = X509_ATTRIBUTE_new())) goto err;
2400Sstevel@tonic-gate if(!(attr->value.set = sk_ASN1_TYPE_new_null())) goto err;
2410Sstevel@tonic-gate if(!sk_ASN1_TYPE_push(attr->value.set, at)) goto err;
2420Sstevel@tonic-gate at = NULL;
2430Sstevel@tonic-gate attr->single = 0;
2440Sstevel@tonic-gate attr->object = OBJ_nid2obj(nid);
2450Sstevel@tonic-gate if(!sk_X509_ATTRIBUTE_push(req->req_info->attributes, attr)) goto err;
2460Sstevel@tonic-gate return 1;
2470Sstevel@tonic-gate err:
2480Sstevel@tonic-gate if(p) OPENSSL_free(p);
2490Sstevel@tonic-gate X509_ATTRIBUTE_free(attr);
2500Sstevel@tonic-gate ASN1_TYPE_free(at);
2510Sstevel@tonic-gate return 0;
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate /* This is the normal usage: use the "official" OID */
X509_REQ_add_extensions(X509_REQ * req,STACK_OF (X509_EXTENSION)* exts)2540Sstevel@tonic-gate int X509_REQ_add_extensions(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate return X509_REQ_add_extensions_nid(req, exts, NID_ext_req);
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate /* Request attribute functions */
2600Sstevel@tonic-gate
X509_REQ_get_attr_count(const X509_REQ * req)2610Sstevel@tonic-gate int X509_REQ_get_attr_count(const X509_REQ *req)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate return X509at_get_attr_count(req->req_info->attributes);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate
X509_REQ_get_attr_by_NID(const X509_REQ * req,int nid,int lastpos)2660Sstevel@tonic-gate int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid,
2670Sstevel@tonic-gate int lastpos)
2680Sstevel@tonic-gate {
2690Sstevel@tonic-gate return X509at_get_attr_by_NID(req->req_info->attributes, nid, lastpos);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate
X509_REQ_get_attr_by_OBJ(const X509_REQ * req,ASN1_OBJECT * obj,int lastpos)2720Sstevel@tonic-gate int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, ASN1_OBJECT *obj,
2730Sstevel@tonic-gate int lastpos)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate return X509at_get_attr_by_OBJ(req->req_info->attributes, obj, lastpos);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate
X509_REQ_get_attr(const X509_REQ * req,int loc)2780Sstevel@tonic-gate X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate return X509at_get_attr(req->req_info->attributes, loc);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
X509_REQ_delete_attr(X509_REQ * req,int loc)2830Sstevel@tonic-gate X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate return X509at_delete_attr(req->req_info->attributes, loc);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate
X509_REQ_add1_attr(X509_REQ * req,X509_ATTRIBUTE * attr)2880Sstevel@tonic-gate int X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate if(X509at_add1_attr(&req->req_info->attributes, attr)) return 1;
2910Sstevel@tonic-gate return 0;
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate
X509_REQ_add1_attr_by_OBJ(X509_REQ * req,const ASN1_OBJECT * obj,int type,const unsigned char * bytes,int len)2940Sstevel@tonic-gate int X509_REQ_add1_attr_by_OBJ(X509_REQ *req,
2950Sstevel@tonic-gate const ASN1_OBJECT *obj, int type,
2960Sstevel@tonic-gate const unsigned char *bytes, int len)
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate if(X509at_add1_attr_by_OBJ(&req->req_info->attributes, obj,
2990Sstevel@tonic-gate type, bytes, len)) return 1;
3000Sstevel@tonic-gate return 0;
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate
X509_REQ_add1_attr_by_NID(X509_REQ * req,int nid,int type,const unsigned char * bytes,int len)3030Sstevel@tonic-gate int X509_REQ_add1_attr_by_NID(X509_REQ *req,
3040Sstevel@tonic-gate int nid, int type,
3050Sstevel@tonic-gate const unsigned char *bytes, int len)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate if(X509at_add1_attr_by_NID(&req->req_info->attributes, nid,
3080Sstevel@tonic-gate type, bytes, len)) return 1;
3090Sstevel@tonic-gate return 0;
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate
X509_REQ_add1_attr_by_txt(X509_REQ * req,const char * attrname,int type,const unsigned char * bytes,int len)3120Sstevel@tonic-gate int X509_REQ_add1_attr_by_txt(X509_REQ *req,
3130Sstevel@tonic-gate const char *attrname, int type,
3140Sstevel@tonic-gate const unsigned char *bytes, int len)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate if(X509at_add1_attr_by_txt(&req->req_info->attributes, attrname,
3170Sstevel@tonic-gate type, bytes, len)) return 1;
3180Sstevel@tonic-gate return 0;
3190Sstevel@tonic-gate }
320