xref: /third_party/openssl/crypto/x509/x509_r2x.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
12e1051a39Sopenharmony_ci#include <openssl/bn.h>
13e1051a39Sopenharmony_ci#include <openssl/evp.h>
14e1051a39Sopenharmony_ci#include <openssl/asn1.h>
15e1051a39Sopenharmony_ci#include <openssl/x509.h>
16e1051a39Sopenharmony_ci#include "crypto/x509.h"
17e1051a39Sopenharmony_ci#include <openssl/objects.h>
18e1051a39Sopenharmony_ci#include <openssl/buffer.h>
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_ciX509 *X509_REQ_to_X509(X509_REQ *r, int days, EVP_PKEY *pkey)
21e1051a39Sopenharmony_ci{
22e1051a39Sopenharmony_ci    X509 *ret = NULL;
23e1051a39Sopenharmony_ci    X509_CINF *xi = NULL;
24e1051a39Sopenharmony_ci    const X509_NAME *xn;
25e1051a39Sopenharmony_ci    EVP_PKEY *pubkey = NULL;
26e1051a39Sopenharmony_ci
27e1051a39Sopenharmony_ci    if ((ret = X509_new()) == NULL) {
28e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
29e1051a39Sopenharmony_ci        return NULL;
30e1051a39Sopenharmony_ci    }
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ci    /* duplicate the request */
33e1051a39Sopenharmony_ci    xi = &ret->cert_info;
34e1051a39Sopenharmony_ci
35e1051a39Sopenharmony_ci    if (sk_X509_ATTRIBUTE_num(r->req_info.attributes) != 0) {
36e1051a39Sopenharmony_ci        if ((xi->version = ASN1_INTEGER_new()) == NULL)
37e1051a39Sopenharmony_ci            goto err;
38e1051a39Sopenharmony_ci        if (!ASN1_INTEGER_set(xi->version, 2))
39e1051a39Sopenharmony_ci            goto err;
40e1051a39Sopenharmony_ci/*-     xi->extensions=ri->attributes; <- bad, should not ever be done
41e1051a39Sopenharmony_ci        ri->attributes=NULL; */
42e1051a39Sopenharmony_ci    }
43e1051a39Sopenharmony_ci
44e1051a39Sopenharmony_ci    xn = X509_REQ_get_subject_name(r);
45e1051a39Sopenharmony_ci    if (X509_set_subject_name(ret, xn) == 0)
46e1051a39Sopenharmony_ci        goto err;
47e1051a39Sopenharmony_ci    if (X509_set_issuer_name(ret, xn) == 0)
48e1051a39Sopenharmony_ci        goto err;
49e1051a39Sopenharmony_ci
50e1051a39Sopenharmony_ci    if (X509_gmtime_adj(xi->validity.notBefore, 0) == NULL)
51e1051a39Sopenharmony_ci        goto err;
52e1051a39Sopenharmony_ci    if (X509_gmtime_adj(xi->validity.notAfter, (long)60 * 60 * 24 * days) ==
53e1051a39Sopenharmony_ci        NULL)
54e1051a39Sopenharmony_ci        goto err;
55e1051a39Sopenharmony_ci
56e1051a39Sopenharmony_ci    pubkey = X509_REQ_get0_pubkey(r);
57e1051a39Sopenharmony_ci    if (pubkey == NULL || !X509_set_pubkey(ret, pubkey))
58e1051a39Sopenharmony_ci        goto err;
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_ci    if (!X509_sign(ret, pkey, EVP_md5()))
61e1051a39Sopenharmony_ci        goto err;
62e1051a39Sopenharmony_ci    return ret;
63e1051a39Sopenharmony_ci
64e1051a39Sopenharmony_ci err:
65e1051a39Sopenharmony_ci    X509_free(ret);
66e1051a39Sopenharmony_ci    return NULL;
67e1051a39Sopenharmony_ci}
68