2016-05-18 02:52:22 +08:00
|
|
|
/*
|
2021-01-28 20:54:57 +08:00
|
|
|
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 21:00:36 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:52:22 +08:00
|
|
|
* 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 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/asn1.h>
|
2007-06-07 21:14:42 +08:00
|
|
|
#include <openssl/asn1t.h>
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/x509.h>
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/x509.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/buffer.h>
|
|
|
|
#include <openssl/pem.h>
|
1998-12-21 18:52:47 +08:00
|
|
|
|
1999-11-27 09:14:04 +08:00
|
|
|
X509_REQ *X509_to_X509_REQ(X509 *x, EVP_PKEY *pkey, const EVP_MD *md)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
X509_REQ *ret;
|
|
|
|
X509_REQ_INFO *ri;
|
|
|
|
int i;
|
|
|
|
EVP_PKEY *pktmp;
|
|
|
|
|
2021-03-05 06:22:56 +08:00
|
|
|
ret = X509_REQ_new_ex(x->libctx, x->propq);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (ret == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-09-17 01:46:16 +08:00
|
|
|
ri = &ret->req_info;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
ri->version->length = 1;
|
2015-04-29 03:28:14 +08:00
|
|
|
ri->version->data = OPENSSL_malloc(1);
|
2015-01-22 11:40:55 +08: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 08:32:36 +08:00
|
|
|
pktmp = X509_get0_pubkey(x);
|
2015-02-18 08:34:59 +08:00
|
|
|
if (pktmp == NULL)
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08: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 22:04:09 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
|
|
|
X509_REQ_free(ret);
|
2017-10-17 22:04:09 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
EVP_PKEY *X509_REQ_get_pubkey(X509_REQ *req)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-09-17 01:46:16 +08:00
|
|
|
if (req == NULL)
|
2017-10-17 22:04:09 +08:00
|
|
|
return NULL;
|
|
|
|
return X509_PUBKEY_get(req->req_info.pubkey);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2022-08-01 23:43:00 +08:00
|
|
|
EVP_PKEY *X509_REQ_get0_pubkey(const X509_REQ *req)
|
2016-04-04 05:24:51 +08:00
|
|
|
{
|
|
|
|
if (req == NULL)
|
|
|
|
return NULL;
|
2017-10-17 22:04:09 +08:00
|
|
|
return X509_PUBKEY_get0(req->req_info.pubkey);
|
2016-04-04 05:24:51 +08:00
|
|
|
}
|
|
|
|
|
2015-09-18 10:06:55 +08:00
|
|
|
X509_PUBKEY *X509_REQ_get_X509_PUBKEY(X509_REQ *req)
|
|
|
|
{
|
|
|
|
return req->req_info.pubkey;
|
|
|
|
}
|
|
|
|
|
2022-08-01 23:43:00 +08:00
|
|
|
int X509_REQ_check_private_key(const X509_REQ *req, EVP_PKEY *pkey)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2022-08-01 23:43:00 +08:00
|
|
|
return ossl_x509_check_private_key(X509_REQ_get0_pubkey(req), pkey);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2003-04-04 06:27:24 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* It seems several organisations had the same idea of including a list of
|
1999-08-10 06:38:05 +08: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 11:40:55 +08:00
|
|
|
static int ext_nid_list[] = { NID_ext_req, NID_ms_ext_req, NID_undef };
|
1999-08-10 06:38:05 +08:00
|
|
|
|
|
|
|
static int *ext_nids = ext_nid_list;
|
|
|
|
|
|
|
|
int X509_REQ_extension_nid(int req_nid)
|
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
int i, nid;
|
2022-08-29 19:59:02 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
for (i = 0;; i++) {
|
|
|
|
nid = ext_nids[i];
|
|
|
|
if (nid == NID_undef)
|
|
|
|
return 0;
|
|
|
|
else if (req_nid == nid)
|
|
|
|
return 1;
|
|
|
|
}
|
1999-08-10 06:38:05 +08:00
|
|
|
}
|
|
|
|
|
2000-02-04 07:23:24 +08:00
|
|
|
int *X509_REQ_get_extension_nids(void)
|
1999-08-10 06:38:05 +08:00
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
return ext_nids;
|
1999-08-10 06:38:05 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-08-10 06:38:05 +08:00
|
|
|
void X509_REQ_set_extension_nids(int *nids)
|
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
ext_nids = nids;
|
1999-08-10 06:38:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(X509_REQ *req)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
X509_ATTRIBUTE *attr;
|
|
|
|
ASN1_TYPE *ext = NULL;
|
|
|
|
int idx, *pnid;
|
|
|
|
const unsigned char *p;
|
|
|
|
|
2022-08-29 19:59:02 +08:00
|
|
|
if (req == NULL || !ext_nids)
|
2017-10-17 22:04:09 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
for (pnid = ext_nids; *pnid != NID_undef; pnid++) {
|
|
|
|
idx = X509_REQ_get_attr_by_NID(req, *pnid, -1);
|
2022-08-01 22:33:35 +08:00
|
|
|
if (idx < 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
continue;
|
|
|
|
attr = X509_REQ_get_attr(req, idx);
|
2015-03-15 07:48:47 +08:00
|
|
|
ext = X509_ATTRIBUTE_get0_type(attr, 0);
|
2015-01-22 11:40:55 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-01-08 15:27:17 +08:00
|
|
|
if (ext == NULL) /* no extensions is not an error */
|
|
|
|
return sk_X509_EXTENSION_new_null();
|
2022-08-01 21:06:35 +08:00
|
|
|
if (ext->type != V_ASN1_SEQUENCE) {
|
|
|
|
ERR_raise(ERR_LIB_X509, X509_R_WRONG_TYPE);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
2022-08-01 21:06:35 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
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-10 06:38:05 +08:00
|
|
|
}
|
1999-08-11 21:08:58 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* Add a STACK_OF extensions to a certificate request: allow alternative OIDs
|
1999-08-11 21:08:58 +08:00
|
|
|
* in case we want to create a non standard one.
|
|
|
|
*/
|
2021-01-06 06:07:07 +08:00
|
|
|
int X509_REQ_add_extensions_nid(X509_REQ *req,
|
|
|
|
const STACK_OF(X509_EXTENSION) *exts, int nid)
|
1999-08-11 21:08:58 +08:00
|
|
|
{
|
2015-03-15 07:48:47 +08:00
|
|
|
int extlen;
|
|
|
|
int rv = 0;
|
|
|
|
unsigned char *ext = NULL;
|
2021-01-06 06:07:07 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/* Generate encoding of extensions */
|
2021-01-06 06:07:07 +08:00
|
|
|
extlen = ASN1_item_i2d((const ASN1_VALUE *)exts, &ext,
|
2015-03-15 07:48:47 +08:00
|
|
|
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 21:08:58 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1999-08-11 21:08:58 +08:00
|
|
|
/* This is the normal usage: use the "official" OID */
|
2021-01-06 06:07:07 +08:00
|
|
|
int X509_REQ_add_extensions(X509_REQ *req, const STACK_OF(X509_EXTENSION) *exts)
|
1999-08-11 21:08:58 +08:00
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
return X509_REQ_add_extensions_nid(req, exts, NID_ext_req);
|
1999-08-11 21:08:58 +08:00
|
|
|
}
|
2000-01-19 09:02:13 +08:00
|
|
|
|
|
|
|
/* Request attribute functions */
|
|
|
|
|
|
|
|
int X509_REQ_get_attr_count(const X509_REQ *req)
|
|
|
|
{
|
2015-09-17 01:46:16 +08:00
|
|
|
return X509at_get_attr_count(req->req_info.attributes);
|
2000-01-19 09:02:13 +08:00
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, int lastpos)
|
2000-01-19 09:02:13 +08:00
|
|
|
{
|
2015-09-17 01:46:16 +08:00
|
|
|
return X509at_get_attr_by_NID(req->req_info.attributes, nid, lastpos);
|
2000-01-19 09:02:13 +08:00
|
|
|
}
|
|
|
|
|
2016-08-01 01:38:09 +08:00
|
|
|
int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, const ASN1_OBJECT *obj,
|
2015-01-22 11:40:55 +08:00
|
|
|
int lastpos)
|
2000-01-19 09:02:13 +08:00
|
|
|
{
|
2015-09-17 01:46:16 +08:00
|
|
|
return X509at_get_attr_by_OBJ(req->req_info.attributes, obj, lastpos);
|
2000-01-19 09:02:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc)
|
|
|
|
{
|
2015-09-17 01:46:16 +08:00
|
|
|
return X509at_get_attr(req->req_info.attributes, loc);
|
2000-01-19 09:02:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc)
|
|
|
|
{
|
2022-08-29 19:59:02 +08:00
|
|
|
X509_ATTRIBUTE *attr;
|
2022-08-27 21:04:38 +08:00
|
|
|
|
2022-08-29 19:59:02 +08:00
|
|
|
if (req == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
attr = X509at_delete_attr(req->req_info.attributes, loc);
|
2022-08-27 21:04:38 +08:00
|
|
|
if (attr != NULL)
|
|
|
|
req->req_info.enc.modified = 1;
|
|
|
|
return attr;
|
2000-01-19 09:02:13 +08:00
|
|
|
}
|
|
|
|
|
2000-02-26 09:55:33 +08:00
|
|
|
int X509_REQ_add1_attr(X509_REQ *req, X509_ATTRIBUTE *attr)
|
2000-01-19 09:02:13 +08:00
|
|
|
{
|
2022-08-29 19:59:02 +08:00
|
|
|
if (req == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-08-27 21:04:38 +08:00
|
|
|
if (!X509at_add1_attr(&req->req_info.attributes, attr))
|
|
|
|
return 0;
|
|
|
|
req->req_info.enc.modified = 1;
|
|
|
|
return 1;
|
2000-01-19 09:02:13 +08:00
|
|
|
}
|
|
|
|
|
2000-02-26 09:55:33 +08:00
|
|
|
int X509_REQ_add1_attr_by_OBJ(X509_REQ *req,
|
2015-01-22 11:40:55 +08:00
|
|
|
const ASN1_OBJECT *obj, int type,
|
|
|
|
const unsigned char *bytes, int len)
|
2000-01-19 09:02:13 +08:00
|
|
|
{
|
2022-08-29 19:59:02 +08:00
|
|
|
if (req == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-08-27 21:04:38 +08:00
|
|
|
if (!X509at_add1_attr_by_OBJ(&req->req_info.attributes, obj,
|
|
|
|
type, bytes, len))
|
|
|
|
return 0;
|
|
|
|
req->req_info.enc.modified = 1;
|
|
|
|
return 1;
|
2000-01-19 09:02:13 +08:00
|
|
|
}
|
|
|
|
|
2000-02-26 09:55:33 +08:00
|
|
|
int X509_REQ_add1_attr_by_NID(X509_REQ *req,
|
2015-01-22 11:40:55 +08:00
|
|
|
int nid, int type,
|
|
|
|
const unsigned char *bytes, int len)
|
2000-01-19 09:02:13 +08:00
|
|
|
{
|
2022-08-29 19:59:02 +08:00
|
|
|
if (req == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-08-27 21:04:38 +08:00
|
|
|
if (!X509at_add1_attr_by_NID(&req->req_info.attributes, nid,
|
|
|
|
type, bytes, len))
|
|
|
|
return 0;
|
|
|
|
req->req_info.enc.modified = 1;
|
|
|
|
return 1;
|
2000-01-19 09:02:13 +08:00
|
|
|
}
|
|
|
|
|
2000-02-26 09:55:33 +08:00
|
|
|
int X509_REQ_add1_attr_by_txt(X509_REQ *req,
|
2015-01-22 11:40:55 +08:00
|
|
|
const char *attrname, int type,
|
|
|
|
const unsigned char *bytes, int len)
|
2000-01-19 09:02:13 +08:00
|
|
|
{
|
2022-08-29 19:59:02 +08:00
|
|
|
if (req == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-08-27 21:04:38 +08:00
|
|
|
if (!X509at_add1_attr_by_txt(&req->req_info.attributes, attrname,
|
|
|
|
type, bytes, len))
|
|
|
|
return 0;
|
|
|
|
req->req_info.enc.modified = 1;
|
|
|
|
return 1;
|
2000-01-19 09:02:13 +08:00
|
|
|
}
|
2015-08-31 19:58:07 +08:00
|
|
|
|
2016-08-02 02:30:34 +08:00
|
|
|
long X509_REQ_get_version(const X509_REQ *req)
|
2015-08-31 19:58:07 +08:00
|
|
|
{
|
2015-09-17 01:46:16 +08:00
|
|
|
return ASN1_INTEGER_get(req->req_info.version);
|
2015-08-31 19:58:07 +08:00
|
|
|
}
|
|
|
|
|
2016-08-02 02:30:34 +08:00
|
|
|
X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req)
|
2015-08-31 19:58:07 +08:00
|
|
|
{
|
2015-09-17 01:46:16 +08:00
|
|
|
return req->req_info.subject;
|
2015-08-31 19:58:07 +08:00
|
|
|
}
|
2015-09-07 23:51:05 +08:00
|
|
|
|
2016-08-18 22:13:00 +08:00
|
|
|
void X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig,
|
|
|
|
const X509_ALGOR **palg)
|
2015-09-07 23:51:05 +08:00
|
|
|
{
|
2015-09-22 20:00:03 +08:00
|
|
|
if (psig != NULL)
|
2015-09-07 23:51:05 +08:00
|
|
|
*psig = req->signature;
|
2015-09-22 20:00:03 +08:00
|
|
|
if (palg != NULL)
|
2015-09-07 23:51:05 +08:00
|
|
|
*palg = &req->sig_alg;
|
|
|
|
}
|
|
|
|
|
2020-01-11 01:35:49 +08:00
|
|
|
void X509_REQ_set0_signature(X509_REQ *req, ASN1_BIT_STRING *psig)
|
|
|
|
{
|
|
|
|
if (req->signature)
|
2022-08-29 19:59:02 +08:00
|
|
|
ASN1_BIT_STRING_free(req->signature);
|
2020-01-11 01:35:49 +08:00
|
|
|
req->signature = psig;
|
|
|
|
}
|
|
|
|
|
|
|
|
int X509_REQ_set1_signature_algo(X509_REQ *req, X509_ALGOR *palg)
|
|
|
|
{
|
|
|
|
return X509_ALGOR_copy(&req->sig_alg, palg);
|
|
|
|
}
|
|
|
|
|
2015-09-07 23:51:05 +08:00
|
|
|
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)
|
|
|
|
{
|
2022-08-29 19:59:02 +08:00
|
|
|
if (req == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-07 23:51:05 +08:00
|
|
|
req->req_info.enc.modified = 1;
|
2022-09-29 19:00:45 +08:00
|
|
|
return i2d_X509_REQ_INFO(&req->req_info, pp);
|
2015-09-07 23:51:05 +08:00
|
|
|
}
|