2016-05-17 18:52:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/asn1.h>
|
2007-06-07 13:14:42 +00:00
|
|
|
#include <openssl/asn1t.h>
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/x509.h>
|
2015-08-31 11:58:07 +00:00
|
|
|
#include "internal/x509_int.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/buffer.h>
|
|
|
|
#include <openssl/pem.h>
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-11-27 01:14:04 +00:00
|
|
|
X509_REQ *X509_to_X509_REQ(X509 *x, EVP_PKEY *pkey, const EVP_MD *md)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
X509_REQ *ret;
|
|
|
|
X509_REQ_INFO *ri;
|
|
|
|
int i;
|
|
|
|
EVP_PKEY *pktmp;
|
|
|
|
|
|
|
|
ret = X509_REQ_new();
|
|
|
|
if (ret == NULL) {
|
|
|
|
X509err(X509_F_X509_TO_X509_REQ, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-09-16 17:46:16 +00:00
|
|
|
ri = &ret->req_info;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
ri->version->length = 1;
|
2015-04-28 19:28:14 +00:00
|
|
|
ri->version->data = OPENSSL_malloc(1);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (ri->version->data == NULL)
|
|
|
|
goto err;
|
|
|
|
ri->version->data[0] = 0; /* version == 0 */
|
|
|
|
|
|
|
|
if (!X509_REQ_set_subject_name(ret, X509_get_subject_name(x)))
|
|
|
|
goto err;
|
|
|
|
|
2015-12-20 00:32:36 +00:00
|
|
|
pktmp = X509_get0_pubkey(x);
|
2015-02-18 00:34:59 +00:00
|
|
|
if (pktmp == NULL)
|
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
i = X509_REQ_set_pubkey(ret, pktmp);
|
|
|
|
if (!i)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (pkey != NULL) {
|
|
|
|
if (!X509_REQ_sign(ret, pkey, md))
|
|
|
|
goto err;
|
|
|
|
}
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
err:
|
|
|
|
X509_REQ_free(ret);
|
2017-10-17 14:04:09 +00:00
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
EVP_PKEY *X509_REQ_get_pubkey(X509_REQ *req)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-09-16 17:46:16 +00:00
|
|
|
if (req == NULL)
|
2017-10-17 14:04:09 +00:00
|
|
|
return NULL;
|
|
|
|
return X509_PUBKEY_get(req->req_info.pubkey);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-04-03 21:24:51 +00:00
|
|
|
EVP_PKEY *X509_REQ_get0_pubkey(X509_REQ *req)
|
|
|
|
{
|
|
|
|
if (req == NULL)
|
|
|
|
return NULL;
|
2017-10-17 14:04:09 +00:00
|
|
|
return X509_PUBKEY_get0(req->req_info.pubkey);
|
2016-04-03 21:24:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 02:06:55 +00:00
|
|
|
X509_PUBKEY *X509_REQ_get_X509_PUBKEY(X509_REQ *req)
|
|
|
|
{
|
|
|
|
return req->req_info.pubkey;
|
|
|
|
}
|
|
|
|
|
2003-04-03 22:27:24 +00:00
|
|
|
int X509_REQ_check_private_key(X509_REQ *x, EVP_PKEY *k)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
EVP_PKEY *xk = NULL;
|
|
|
|
int ok = 0;
|
|
|
|
|
|
|
|
xk = X509_REQ_get_pubkey(x);
|
|
|
|
switch (EVP_PKEY_cmp(xk, k)) {
|
|
|
|
case 1:
|
|
|
|
ok = 1;
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,
|
|
|
|
X509_R_KEY_VALUES_MISMATCH);
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH);
|
|
|
|
break;
|
|
|
|
case -2:
|
2003-04-03 22:27:24 +00:00
|
|
|
#ifndef OPENSSL_NO_EC
|
2016-01-19 00:21:12 +00:00
|
|
|
if (EVP_PKEY_id(k) == EVP_PKEY_EC) {
|
2015-01-22 03:40:55 +00:00
|
|
|
X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, ERR_R_EC_LIB);
|
|
|
|
break;
|
|
|
|
}
|
2003-04-03 22:27:24 +00:00
|
|
|
#endif
|
|
|
|
#ifndef OPENSSL_NO_DH
|
2016-01-19 00:21:12 +00:00
|
|
|
if (EVP_PKEY_id(k) == EVP_PKEY_DH) {
|
2015-01-22 03:40:55 +00:00
|
|
|
/* No idea */
|
|
|
|
X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY,
|
|
|
|
X509_R_CANT_CHECK_DH_KEY);
|
|
|
|
break;
|
|
|
|
}
|
2003-04-03 22:27:24 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
X509err(X509_F_X509_REQ_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE);
|
|
|
|
}
|
2003-04-03 22:27:24 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
EVP_PKEY_free(xk);
|
2017-10-17 14:04:09 +00:00
|
|
|
return ok;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2003-04-03 22:27:24 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* It seems several organisations had the same idea of including a list of
|
1999-08-09 22:38:05 +00:00
|
|
|
* extensions in a certificate request. There are at least two OIDs that are
|
|
|
|
* used and there may be more: so the list is configurable.
|
|
|
|
*/
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
static int ext_nid_list[] = { NID_ext_req, NID_ms_ext_req, NID_undef };
|
1999-08-09 22:38:05 +00:00
|
|
|
|
|
|
|
static int *ext_nids = ext_nid_list;
|
|
|
|
|
|
|
|
int X509_REQ_extension_nid(int req_nid)
|
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
int i, nid;
|
|
|
|
for (i = 0;; i++) {
|
|
|
|
nid = ext_nids[i];
|
|
|
|
if (nid == NID_undef)
|
|
|
|
return 0;
|
|
|
|
else if (req_nid == nid)
|
|
|
|
return 1;
|
|
|
|
}
|
1999-08-09 22:38:05 +00:00
|
|
|
}
|
|
|
|
|
2000-02-03 23:23:24 +00:00
|
|
|
int *X509_REQ_get_extension_nids(void)
|
1999-08-09 22:38:05 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
return ext_nids;
|
1999-08-09 22:38:05 +00:00
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
|
1999-08-09 22:38:05 +00:00
|
|
|
void X509_REQ_set_extension_nids(int *nids)
|
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
ext_nids = nids;
|
1999-08-09 22:38:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
X509_ATTRIBUTE *attr;
|
|
|
|
ASN1_TYPE *ext = NULL;
|
|
|
|
int idx, *pnid;
|
|
|
|
const unsigned char *p;
|
|
|
|
|
2015-09-16 17:46:16 +00:00
|
|
|
if ((req == NULL) || !ext_nids)
|
2017-10-17 14:04:09 +00:00
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
for (pnid = ext_nids; *pnid != NID_undef; pnid++) {
|
|
|
|
idx = X509_REQ_get_attr_by_NID(req, *pnid, -1);
|
|
|
|
if (idx == -1)
|
|
|
|
continue;
|
|
|
|
attr = X509_REQ_get_attr(req, idx);
|
2015-03-14 23:48:47 +00:00
|
|
|
ext = X509_ATTRIBUTE_get0_type(attr, 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!ext || (ext->type != V_ASN1_SEQUENCE))
|
|
|
|
return NULL;
|
|
|
|
p = ext->value.sequence->data;
|
|
|
|
return (STACK_OF(X509_EXTENSION) *)
|
|
|
|
ASN1_item_d2i(NULL, &p, ext->value.sequence->length,
|
|
|
|
ASN1_ITEM_rptr(X509_EXTENSIONS));
|
1999-08-09 22:38:05 +00:00
|
|
|
}
|
1999-08-11 13:08:58 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Add a STACK_OF extensions to a certificate request: allow alternative OIDs
|
1999-08-11 13:08:58 +00:00
|
|
|
* in case we want to create a non standard one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int X509_REQ_add_extensions_nid(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts,
|
2015-01-22 03:40:55 +00:00
|
|
|
int nid)
|
1999-08-11 13:08:58 +00:00
|
|
|
{
|
2015-03-14 23:48:47 +00:00
|
|
|
int extlen;
|
|
|
|
int rv = 0;
|
|
|
|
unsigned char *ext = NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Generate encoding of extensions */
|
2015-03-14 23:48:47 +00:00
|
|
|
extlen = ASN1_item_i2d((ASN1_VALUE *)exts, &ext,
|
|
|
|
ASN1_ITEM_rptr(X509_EXTENSIONS));
|
|
|
|
if (extlen <= 0)
|
|
|
|
return 0;
|
|
|
|
rv = X509_REQ_add1_attr_by_NID(req, nid, V_ASN1_SEQUENCE, ext, extlen);
|
|
|
|
OPENSSL_free(ext);
|
|
|
|
return rv;
|
1999-08-11 13:08:58 +00:00
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
|
1999-08-11 13:08:58 +00:00
|
|
|
/* This is the normal usage: use the "official" OID */
|
|
|
|
int X509_REQ_add_extensions(X509_REQ *req, STACK_OF(X509_EXTENSION) *exts)
|
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
return X509_REQ_add_extensions_nid(req, exts, NID_ext_req);
|
1999-08-11 13:08:58 +00:00
|
|
|
}
|
2000-01-19 01:02:13 +00:00
|
|
|
|
|
|
|
/* Request attribute functions */
|
|
|
|
|
|
|
|
int X509_REQ_get_attr_count(const X509_REQ *req)
|
|
|
|
{
|
2015-09-16 17:46:16 +00:00
|
|
|
return X509at_get_attr_count(req->req_info.attributes);
|
2000-01-19 01:02:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, int lastpos)
|
2000-01-19 01:02:13 +00:00
|
|
|
{
|
2015-09-16 17:46:16 +00:00
|
|
|
return X509at_get_attr_by_NID(req->req_info.attributes, nid, lastpos);
|
2000-01-19 01:02:13 +00:00
|
|
|
}
|
|
|
|
|
2016-07-31 17:38:09 +00:00
|
|
|
int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, const ASN1_OBJECT *obj,
|
2015-01-22 03:40:55 +00:00
|
|
|
int lastpos)
|
2000-01-19 01:02:13 +00:00
|
|
|
{
|
2015-09-16 17:46:16 +00:00
|
|
|
return X509at_get_attr_by_OBJ(req->req_info.attributes, obj, lastpos);
|
2000-01-19 01:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc)
|
|
|
|
{
|
2015-09-16 17:46:16 +00:00
|
|
|
return X509at_get_attr(req->req_info.attributes, loc);
|
2000-01-19 01:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc)
|
|
|
|
{
|
2015-09-16 17:46:16 +00:00
|
|
|
return X509at_delete_attr(req->req_info.attributes, loc);
|
2000-01-19 01:02:13 +00:00
|
|
|
}
|
|
|
|
|
2000-02-26 01:55:33 +00:00
|
|
|
int X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr)
|
2000-01-19 01:02:13 +00:00
|
|
|
{
|
2015-09-16 17:46:16 +00:00
|
|
|
if (X509at_add1_attr(&req->req_info.attributes, attr))
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
2000-01-19 01:02:13 +00:00
|
|
|
}
|
|
|
|
|
2000-02-26 01:55:33 +00:00
|
|
|
int X509_REQ_add1_attr_by_OBJ(X509_REQ *req,
|
2015-01-22 03:40:55 +00:00
|
|
|
const ASN1_OBJECT *obj, int type,
|
|
|
|
const unsigned char *bytes, int len)
|
2000-01-19 01:02:13 +00:00
|
|
|
{
|
2015-09-16 17:46:16 +00:00
|
|
|
if (X509at_add1_attr_by_OBJ(&req->req_info.attributes, obj,
|
2015-01-22 03:40:55 +00:00
|
|
|
type, bytes, len))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2000-01-19 01:02:13 +00:00
|
|
|
}
|
|
|
|
|
2000-02-26 01:55:33 +00:00
|
|
|
int X509_REQ_add1_attr_by_NID(X509_REQ *req,
|
2015-01-22 03:40:55 +00:00
|
|
|
int nid, int type,
|
|
|
|
const unsigned char *bytes, int len)
|
2000-01-19 01:02:13 +00:00
|
|
|
{
|
2015-09-16 17:46:16 +00:00
|
|
|
if (X509at_add1_attr_by_NID(&req->req_info.attributes, nid,
|
2015-01-22 03:40:55 +00:00
|
|
|
type, bytes, len))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2000-01-19 01:02:13 +00:00
|
|
|
}
|
|
|
|
|
2000-02-26 01:55:33 +00:00
|
|
|
int X509_REQ_add1_attr_by_txt(X509_REQ *req,
|
2015-01-22 03:40:55 +00:00
|
|
|
const char *attrname, int type,
|
|
|
|
const unsigned char *bytes, int len)
|
2000-01-19 01:02:13 +00:00
|
|
|
{
|
2015-09-16 17:46:16 +00:00
|
|
|
if (X509at_add1_attr_by_txt(&req->req_info.attributes, attrname,
|
2015-01-22 03:40:55 +00:00
|
|
|
type, bytes, len))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2000-01-19 01:02:13 +00:00
|
|
|
}
|
2015-08-31 11:58:07 +00:00
|
|
|
|
2016-08-01 18:30:34 +00:00
|
|
|
long X509_REQ_get_version(const X509_REQ *req)
|
2015-08-31 11:58:07 +00:00
|
|
|
{
|
2015-09-16 17:46:16 +00:00
|
|
|
return ASN1_INTEGER_get(req->req_info.version);
|
2015-08-31 11:58:07 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 18:30:34 +00:00
|
|
|
X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req)
|
2015-08-31 11:58:07 +00:00
|
|
|
{
|
2015-09-16 17:46:16 +00:00
|
|
|
return req->req_info.subject;
|
2015-08-31 11:58:07 +00:00
|
|
|
}
|
2015-09-07 15:51:05 +00:00
|
|
|
|
2016-08-18 14:13:00 +00:00
|
|
|
void X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig,
|
|
|
|
const X509_ALGOR **palg)
|
2015-09-07 15:51:05 +00:00
|
|
|
{
|
2015-09-22 12:00:03 +00:00
|
|
|
if (psig != NULL)
|
2015-09-07 15:51:05 +00:00
|
|
|
*psig = req->signature;
|
2015-09-22 12:00:03 +00:00
|
|
|
if (palg != NULL)
|
2015-09-07 15:51:05 +00:00
|
|
|
*palg = &req->sig_alg;
|
|
|
|
}
|
|
|
|
|
|
|
|
int X509_REQ_get_signature_nid(const X509_REQ *req)
|
|
|
|
{
|
|
|
|
return OBJ_obj2nid(req->sig_alg.algorithm);
|
|
|
|
}
|
|
|
|
|
|
|
|
int i2d_re_X509_REQ_tbs(X509_REQ *req, unsigned char **pp)
|
|
|
|
{
|
|
|
|
req->req_info.enc.modified = 1;
|
|
|
|
return i2d_X509_REQ_INFO(&req->req_info, pp);
|
|
|
|
}
|