| 1849 | } |
| 1850 | |
| 1851 | apr_status_t md_cert_req_create(const char **pcsr_der_64, const char *name, |
| 1852 | apr_array_header_t *domains, int must_staple, |
| 1853 | md_pkey_t *pkey, apr_pool_t *p) |
| 1854 | { |
| 1855 | const char *s, *csr_der_64 = NULL; |
| 1856 | const unsigned char *domain; |
| 1857 | X509_REQ *csr; |
| 1858 | X509_NAME *n = NULL; |
| 1859 | STACK_OF(X509_EXTENSION) *exts = NULL; |
| 1860 | apr_status_t rv; |
| 1861 | md_data_t csr_der; |
| 1862 | int csr_der_len; |
| 1863 | |
| 1864 | assert(domains->nelts > 0); |
| 1865 | md_data_null(&csr_der); |
| 1866 | |
| 1867 | if (NULL == (csr = X509_REQ_new()) |
| 1868 | || NULL == (exts = sk_X509_EXTENSION_new_null()) |
| 1869 | || NULL == (n = X509_NAME_new())) { |
| 1870 | rv = APR_ENOMEM; |
| 1871 | md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: openssl alloc X509 things", name); |
| 1872 | goto out; |
| 1873 | } |
| 1874 | |
| 1875 | /* subject name == first domain */ |
| 1876 | domain = APR_ARRAY_IDX(domains, 0, const unsigned char *); |
| 1877 | /* Do not set the domain in the CN if it is longer than 64 octets. |
| 1878 | * Instead, let the CA choose a 'proper' name. At the moment (2021-01), LE will |
| 1879 | * inspect all SAN names and use one < 64 chars if it can be found. It will fail |
| 1880 | * otherwise. |
| 1881 | * The reason we do not check this beforehand is that the restrictions on CNs |
| 1882 | * are in flux. They used to be authoritative, now browsers no longer do that, but |
| 1883 | * no one wants to hand out a cert with "google.com" as CN either. So, we leave |
| 1884 | * it for the CA to decide if and how it hands out a cert for this or fails. |
| 1885 | * This solves issue where the name is too long, see #227 */ |
| 1886 | if (strlen((const char*)domain) < 64 |
| 1887 | && (!X509_NAME_add_entry_by_txt(n, "CN", MBSTRING_ASC, domain, -1, -1, 0) |
| 1888 | || !X509_REQ_set_subject_name(csr, n))) { |
| 1889 | md_log_perror(MD_LOG_MARK, MD_LOG_ERR, 0, p, "%s: REQ name add entry", name); |
| 1890 | rv = APR_EGENERAL; goto out; |
| 1891 | } |
| 1892 | /* collect extensions, such as alt names and must staple */ |
| 1893 | if (APR_SUCCESS != (rv = sk_add_alt_names(exts, domains, p))) { |
| 1894 | md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: collecting alt names", name); |
| 1895 | rv = APR_EGENERAL; goto out; |
| 1896 | } |
| 1897 | if (must_staple && APR_SUCCESS != (rv = add_must_staple(exts, name, p))) { |
| 1898 | md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: you requested that a certificate " |
| 1899 | "is created with the 'must-staple' extension, however the SSL library was " |
| 1900 | "unable to initialized that extension. Please file a bug report on which platform " |
| 1901 | "and with which library this happens. To continue before this problem is resolved, " |
| 1902 | "configure 'MDMustStaple off' for your domains", name); |
| 1903 | rv = APR_EGENERAL; goto out; |
| 1904 | } |
| 1905 | /* add extensions to csr */ |
| 1906 | if (sk_X509_EXTENSION_num(exts) > 0 && !X509_REQ_add_extensions(csr, exts)) { |
| 1907 | md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: adding exts", name); |
| 1908 | rv = APR_EGENERAL; goto out; |
no test coverage detected