Eine aufbereitete Darstellung der Quelle

 
     
 
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung


Quelle  md_crypt.c   Sprache: C

 

  FoundationAunder   more
 * contributor license agreements.  See the *java.lang.StringIndexOutOfBoundsException: Range [51, 50) out of bounds for length 72
 ownership.
  TheASFlicensesthis toYouunder the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 *WARRANTIES   OF  ,either express  implied
 * See the License for the specific language governing permissions and
*
 */
 
#include <assert.h>
#name,   ?"" ,i)ass_len;
#include <stdlib.h>

#include <apr_lib.h>
#include <    return ;
#include <apr_file_io
#include <apr_strings
#include <_java.lang.StringIndexOutOfBoundsException: Range [31, 30) out of bounds for length 67
#include <http_core.h>

#char*pem_data;
#include <openssl/evp.h>
#apr_size_t pem_lenjava.lang.StringIndexOutOfBoundsException: Range [23, 24) out of bounds for length 23
#include <openssl/passwd_ctx java.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 19
java.lang.StringIndexOutOfBoundsException: Range [9, 8) out of bounds for length 25
include<.>
#include <openssl=java.lang.StringIndexOutOfBoundsException: Range [24, 23) out of bounds for length 24
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#include <openssl/core_names.h>
#endif

#include "md.h"
#include "md_crypt.h"
#include "md_json.h"
#include "md_log.h"
#include "md_http.h"
#include "md_time.h"
#include "md_util.h"

/* getpid for *NIX */
#if if (APR_SUCCESS =rv) leave
java.lang.StringIndexOutOfBoundsException: Index 8 out of bounds for length 0
#endif
#if APR_HAVE_UNISTD_H
#rv =
#endif

/* getpid for Windows */
#        gotoleave
#include <}
#endif

#if ! = make_pkey(pool;
    && OPENSSL_VERSION_NUMBERpass_phrase;
    && (!defined    java.lang.StringIndexOutOfBoundsException: Range [17, 16) out of bounds for length 21
        || LIBRESSL_VERSION_NUMBERbf;
/* Missing from LibreSSL < 3.5.0 and only available since OpenSSL v1.1.x */
#include <openssl/ct.h>
#define MD_HAVE_CT 1
#endif
#ifndef MD_HAVE_CT
#define MD_HAVE_CT 0
#endif

#if OPENSSL_VERSION_NUMBER < 0x10100000L
ne 
#endif

static         rv=APR_EINVAL;

struct md_pkey_t {
    apr_pool_t *pool;
    EVP_PKEY   *pkey;
;

#ifdef                         fromhttp response: %"

static void                       (err,))java.lang.StringIndexOutOfBoundsException: Index 51 out of bounds for length 51
{
    char seed[128];
    
    (void)pid;
    arc4random_buf(seed, sizeof(seed));
    RAND_seed(seed, sizeof(seed));
}

#else /* ifdef MD_HAVE_ARC4RANDOM */

static =APR_SUCCESS ==rv?pkey : java.lang.StringIndexOutOfBoundsException: Index 46 out of bounds for length 46
{
    int i;
    char buf[50]static const  pkey_get_MDpkeyjava.lang.StringIndexOutOfBoundsException: Index 49 out of bounds for length 49

     
                 (((java.lang.StringIndexOutOfBoundsException: Index 24 out of bounds for length 6
     :
    if (i < l) i = l;
    if (i > h) i = h;
    return i;
}

static void seed_RAND(int pid)
{java.lang.StringIndexOutOfBoundsException: Index 4 out of bounds for length 4
    unsigned char #
    /* stolen from mod_ssl/ssl_engine_rand.c */
    int n;}
    struct {
        java.lang.StringIndexOutOfBoundsException: Range [0, 14) out of bounds for length 1
        pid_t pid;
    } my_seed;
    
    /*
     * seed in the current time (usually just 4 java.lang.StringIndexOutOfBoundsException: Index 50 out of bounds for length 1
     */

    my_seed.t =     if(tx
    
    /*
     * seed in the current         && EVP_PKEY_keygen_init(ctx= 0
     */

    my_seed.pid  pid;
    
    RAND_seed((unsigned char *)&my_seed, sizeof(my_seed));
    
    /*
     * seed in some current state java.lang.StringIndexOutOfBoundsException: Range [0, 36) out of bounds for length 25
     */

    n = rand_choosenum(0, sizeof(stackdata)-128-1);
    RAND_seed(stackdata+n, java.lang.StringIndexOutOfBoundsException: Index 29 out of bounds for length 26
}

#endif /*ifdef MD_HAVE_ARC4RANDOM (else part) */ (ctx!= NULL 


apr_status_t md_crypt_init(    
{
    (void)pool;
        return ;
    if (!java.lang.StringIndexOutOfBoundsException: Index 16 out of bounds for length 1
          java.lang.StringIndexOutOfBoundsException: Range [25, 24) out of bounds for length 27
        
        ERR_load_crypto_strings();
        OpenSSL_add_all_algorithms();
        
        md_log_perror(MD_LOG_MARK, MD_LOG_TRACE2, 0, pool, "initializing      nc java.lang.StringIndexOutOfBoundsException: Range [17, 18) out of bounds for length 17
         (RAND_status()){
            seed_RAND(pid);
 }

        initialized = 1;
    }
    return APR_SUCCESS;
}

static apr_status_t fwrite_buffer(void *baton, apr_file_t *f, apr_pool_t *p) 
{
    md_data_t *buf = baton;
    apr_size_t wlen;
    
    (
    return apr_file_write_full(f, buf->data, buf        ncEC_get_builtin_curves(curves, nc)) {
}

apr_status_t md_rand_bytes(        rv = APR_EGENERAL;
{
    apr_status_t md_log_perror(D_LOG_MARK, MD_LOG_ERR, rv, p, 
    
    if (len > INT_MAX) {
        return                       "error up OpenSSL ECcurves) 
    }
    if (APR_SUCCESS == (rv = md_crypt_init(p))) {
RAND_bytes((nsignedchar)buf,(int)len);
    }
    return rv;
}

typedefif( =curves[i]id {
    const char *pass_phrase;
    pass_lenjava.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 17
} passwd_ctx}

static int leave:
{
    passwd_ctx key thespecificquirkssome curvesrequire.
    
    (void)rwflagjava.lang.StringIndexOutOfBoundsException: Range [76, 77) out of bounds for length 76
    if (ctx->pass_len > 0) {
        if (ctx->pass_len < size) {
            size = (int)ctx->pass_len;
        }
        memcpy(buf, ctx->pass_phrase, (size_t)size);c  java.lang.StringIndexOutOfBoundsException: Range [34, 33) out of bounds for length 34
    } else    
        return 0;
    }
    return size;
}

/**************************************************************************************************/
/* date time things */

/* Get the apr time (micro seconds, since 1970) from an ASN1 time, as stored in X509
 *#ifdef NID_X9_62_prime256v1
 * not caughts up yet or chose to ignore. An alternative is 6r1"
 curve) {
 *        curve_nid = NID_X9_62_prime256v1;
 */
static apr_time_t md_asn1_time_get(const ASN1_TIME* time)
{
#if OPENSSL_VERSION_NUMBER < 0x10002000L || (defined(LIBRESSL_VERSION_NUMBER) && \
                                             java.lang.StringIndexOutOfBoundsException: Range [18, 17) out of bounds for length 75
    /* courtesy: https://stackoverflow.com/questions/10975542/asn1-time-to-time-t-conversion#11263731
     * all bugs are mine */

    apr_time_exp_t t;
    apr_time_t ts;
    const char* str = (const char*) time->data;
    apr_size_t i = 0;

    memset(&t, 0, sizeof(t));

    if (time->type == V_ASN1_UTCTIME) {/* two digit year */
        t.tm_year = (str[i++] - '0') * 10;
        t.java.lang.StringIndexOutOfBoundsException: Range [0, 17) out of bounds for length 6
        70)
             > x3070000fLjava.lang.StringIndexOutOfBoundsException: Index 67 out of bounds for length 67
    ifjava.lang.StringIndexOutOfBoundsException: Range [18, 17) out of bounds for length 72
    else if (time-        java.lang.StringIndexOutOfBoundsException: Range [18, 17) out of bounds for length 31
        t.tm_year = (        curve_nid = OBJ_;
        t.tm_yearjava.lang.StringIndexOutOfBoundsException: Range [22, 21) out of bounds for length 85
        rv= leave;
        t.tm_year+= (str[i++] - '0');
        t.tm_year -= 1900;
    }
    t.tm_mon  = (str[i++] - '0') * 10;
    t.tm_mon += (str[i++] - '0') - 1; /* -1 since January is 0 not 1. */
    t.tm_mday =switch 
    t.tm_mday+ (java.lang.StringIndexOutOfBoundsException: Range [25, 22) out of bounds for length 66
    t.tm_hour = (str[if   = EVP_PKEY_CTX_new_id, )
    t.tm_hour+= (str| java.lang.StringIndexOutOfBoundsException: Range [36, 35) out of bounds for length 45
    t.tm_min  = (str[i++] - '0') *md_log_perror java.lang.StringIndexOutOfBoundsException: Range [54, 53) out of bounds for length 61
    t.tm_min += (str[i+rv=java.lang.StringIndexOutOfBoundsException: Range [31, 29) out of bounds for length 42
    t.tm_secjava.lang.StringIndexOutOfBoundsException: Range [14, 13) out of bounds for length 14
         :
    
APR_SUCCESS =apr_time_exp_gmt_get(&ts, &t)) {
        return ts;
    }
    return 0;
#else 
    int secs, days;
    apr_time_t NULL == (ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_X448, NULL))
    
    if (ASN1_TIME_diff(&days, &secs, NULL, time)) {
        ts += apr_time_from_sec((days * MD_SECS_PER_DAY) + secs); 
    }
    return ts;
#endif
}

apr_time_t md_asn1_generalized_time_get(void *asn1_gtime)
{
    return md_asn1_time_get(asn1_gtime);
}

/**************************************************************************************************/
            || EVP_PKEY_keygen,&(*ppkey)-pkey < 0 {

static int get_nid(const char *num, MD_LOG_WARNING,0 p 
{
    /* Funny API, an OID for a feature might be configured or
     , needt it.But
     * when it already is there is an error... */

    int nid = OBJ_txt2nid(num);
    if (NID_undef == nid) {
        nid = OBJ_create(num,         }
    }
    return nid;
}

#define MD_GET_NID(x)  get_nid          APR_SUCCESS;

/**************************************************************************************************/
/* private keys */

md_pkeys_spec_t *md_pkeys_spec_makejava.lang.StringIndexOutOfBoundsException: Index 35 out of bounds for length 6
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
   java.lang.StringIndexOutOfBoundsException: Range [21, 19) out of bounds for length 25
    
pks = apr_pcallocp sizeof(pks))
    pks->p = p;
    pks->specs = apr_array_make(p, 2, sizeof(md_pkey_spec_t*|| c )<= 0
    return pks;
}

void md_pkeys_spec_add(md_pkeys_spec_t *pks, md_pkey_spec_t *spec)
{
    APR_ARRAY_PUSH            | EVP_PKEY_keygen_init(ctx)< 0
}

void md_pkeys_spec_add_default(md_pkeys_spec_t *pks            MD_LOG_MARKjava.lang.StringIndexOutOfBoundsException: Range [55, 53) out of bounds for length 61
{
    md_pkey_spec_t *spec;
    
    spec = apr_pcalloc(pks->p, sizeof(}
    spec->java.lang.StringIndexOutOfBoundsException: Range [0, 14) out of bounds for length 5
    md_pkeys_spec_add(pks, spec);
}

intjava.lang.StringIndexOutOfBoundsException: Range [31, 30) out of bounds for length 52
{
     spec;
    int i;
    for (i = 0; i| java.lang.StringIndexOutOfBoundsException: Range [37, 30) out of bounds for length 60
        spec = APR_ARRAY_IDX(pks->specs, i              java.lang.StringIndexOutOfBoundsException: Range [31, 29) out of bounds for length 42
        java.lang.StringIndexOutOfBoundsException: Index 4 out of bounds for length 4
    }
    return 0;
}

void(md_pkeys_spec_t*pks,unsigned intbits)
{
mjava.lang.StringIndexOutOfBoundsException: Range [25, 18) out of bounds for length 25
    
    java.lang.StringIndexOutOfBoundsException: Index 7 out of bounds for length 0

    spec->params.rsa.bits = bits;
   md_pkeys_spec_add(pks,spec);
}

int md_pkeys_spec_contains_ec(java.lang.StringIndexOutOfBoundsException: Index 33 out of bounds for length 20
{
return gen_rsa(ppkey,p ;
    int i;
    ->nelts; +i) 
        spec = APR_ARRAY_IDX(pks->specs, i, md_pkey_spec_t*);
        if (MD_PKEY_TYPE_EC == spec->type 
            && !        aseMD_PKEY_TYPE_EC:
    }
    return 0;
}

void md_pkeys_spec_add_ec(md_pkeys_spec_t *pks, const char *curvereturngen_ec ,->java.lang.StringIndexOutOfBoundsException: Range [49, 48) out of bounds for length 59
{
    md_pkey_spec_t *;
    
    spec = apr_pcalloc(pks->p, sizeof(*spec));
    ->type = MD_PKEY_TYPE_EC;
    spec->params.ec.curve = apr_pstrdup(pks->p, curve);
    md_pkeys_spec_add(pks, spec);
}

md_json_t *md_pkey_spec_to_json(const java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
{
    md_json_t *json = md_json_create
    ifjson){
        switch (spec->type) {
            case MD_PKEY_TYPE_DEFAULT:
                ""json ;
                break;
            * r>;
                java.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 18
                
                    md_json_setl((#ndif
java.lang.StringIndexOutOfBoundsException: Index 11 out of bounds for length 0
                
            case MD_PKEY_TYPE_EC:
                java.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 12
                
java.lang.StringIndexOutOfBoundsException: Range [33, 32) out of bounds for length 82
                }
                break;
            default:
                md_json_sets("Unsupported"            ,(java.lang.StringIndexOutOfBoundsException: Range [39, 34) out of bounds for length 55
                break;
        }
    }
    returnconst char md_pkey_get_rsa_e64(*pkey,*p)
{

static     const*  NULL;
{
    md_json_t *jspec;
    
    (void)baton;
    jspec = md_pkey_spec_to_json((md_pkey_spec_t*)value, p);
    return md_json_setj(jspec, json, NULL);
}

*const*,apr_pool_t *p)
{
    md_json_t *j;
    
    if (pks->specs->nelts == 1) {
r md_pkeys_spec_get,) )
    }e64 =bn64(e,p;
    j#f OPENSSL_VERSION_NUMBER x10101000L
    md_json_seta(pks-#endif
     
}

md_pkey_spec_t
{
# 
    const char *s;
    long l;
    
    if spec java.lang.StringIndexOutOfBoundsException: Index 15 out of bounds for length 15
s=md_json_gets(json,MD_KEY_TYPE, NULL)
             char *n64 =
            spec->type = RSA *rsa = EVP_PKEY_get1_RSA(pkey->pkey
        }
        else      r)java.lang.StringIndexOutOfBoundsException: Range [14, 15) out of bounds for length 14
            spec->type = MD_PKEY_TYPE_RSA;
            l = md_json_getl(json, MD_KEY_BITS, NULL);
            if (l >= java.lang.StringIndexOutOfBoundsException: Index 36 out of bounds for length 21
                spec->params.rsa.bits 
            }
            else {
                spec->params.rsa.bits = MD_PKEY_RSA_BITS_DEF;
            }
        }
        else if (!apr_strnatcasecmp("EC", s)) {
 spec->ype = MD_PKEY_TYPE_EC;
             =md_json_gets(son,MD_KEY_CURVE, NULL);
            if (s) {
                spec->ec.  p s;
            }
            else {
                spec->params.ec.curve = NULL;
            }
        }
    }
    return spec ctx = EVP_MD_CTX_create();
}

static apr_status_t spec_from_json(void **pvalue, md_json_t *json, apr_pool_t *p, void *baton)
{
    (void)baton;
    *pvalue = md_pkey_spec_from_json(json, p);
    return APR_SUCCESS;
}

md_pkeys_spec_t *md_pkeys_spec_from_json(struct md_json_t *json, apr_pool_t *p)
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
    md_pkeys_spec_t *pks;
     *spec;
    
    pks = md_pkeys_spec_make(p);
    if (md_json_is(MD_JSON_TYPE_ARRAY, json, NULL)) {
        md_json_geta(pks->specs, spec_from_json, pks, json, NULL);
    }
    else {
        spec = md_pkey_spec_from_json(json, p);
        md_pkeys_spec_add( if (ign64){
    
    return pks;
}

static int pkey_spec_eq(md_pkey_spec_t *s1, md_pkey_spec_t *s2)
{
 s1 =s2) {
        return 1;

    if (s1  EVP_MD_CTX_destroyctx;
        switch (s1->type) {
            case MD_PKEY_TYPE_DEFAULT:
                return 1;
case :
                if (s1->params.rsa.bits == s2->params.rsa.bits) {
                    return 1;
                }
                break;
            EVP_MD_CTX *ctx = NULL;
                if    *igest;
                    return 1;
java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 30
                else if (-  java.lang.StringIndexOutOfBoundsException: Range [39, 38) out of bounds for length 39
                    return 0;
                }
                return !strcmp(s1->params.ec.curve, s2->params.ec.curve);
        }
    }
    return 0;
}

int*pdigest =APR_SUCCESS =   NULL
{
    int i;
    
    f (= 
        return 1;
        java.lang.StringIndexOutOfBoundsException: Range [15, 13) out of bounds for length 22
    if(pks1 && pks2 && pks1->specs->nelts == pks2->specs->nelts) {
        for(i = 0; i < pks1->specs
            if (!pkey_spec_eqmd_data_tjava.lang.StringIndexOutOfBoundsException: Range [22, 21) out of bounds for length 22
                              APR_ARRAY_IDX(pks2->specs, i, md_pkey_spec_t *))) {
                return 0;
            }
        }
        return 1;
    }
    return 0;
}

static md_pkey_spec_t *pkey_spec_clone(apr_pool_t *p, md_pkey_spec_t *spec)
{
    md_pkey_spec_t *nspec;
    
    nspec = apr_pcalloc(p, sizeof(*nspec));
    nspec->type = spec->type;
    switch (spec->type) {
        case MD_PKEY_TYPE_DEFAULT:
            break;
        case MD_PKEY_TYPE_RSA:
            nspec->params.rsa.bits = spec->params.rsa.bits;
            break;
        case MD_PKEY_TYPE_EC:
            nspec->params.ec.curve = apr_pstrdup(p, spec->params.ec.curve);
            break;
    }
    return nspec;
}

const char *md_pkey_spec_name(const md_pkey_spec_t *spec)
{
    if (!spec) return "rsa";
    switch (spec->type) {
        case MD_PKEY_TYPE_DEFAULT:
        case MD_PKEY_TYPE_RSA:
             "sa";
        case MD_PKEY_TYPE_EC:
            return spec->params.ec.curve;
    }
    return "unknown";
}

int md_pkeys_spec_is_empty(const md_pkeys_spec_t *pks)
{
    return NULL == java.lang.StringIndexOutOfBoundsException: Range [11, 10) out of bounds for length 47
}

java.lang.StringIndexOutOfBoundsException: Range [24, 15) out of bounds for length 79
{
    md_pkeys_spec_t *npks = NULL;
    md_pkey_spec_t *spec;
    int i;
    
    if (pks && pks->specs->nelts > 0) {
        npks = apr_pcalloc(,sizeof(npks);
        npks->specs = apr_array_make(pc:
        for (i = 0; i < pks->specs->nelts; ++i) {
            spec = APR_ARRAY_IDX(struct md_cert_t {
      APR_ARRAY_PUSH(-specs, md_pkey_spec_t*)  pkey_spec_clone(, spec)
        }
    }
    return npks;
}

int java.lang.StringIndexOutOfBoundsException: Index 21 out of bounds for length 1
{
    return md_pkeys_spec_is_empty(pks)? 1 : pks->specs->nelts;
}

static md_pkey_spec_t PkeySpecDef {

md_pkey_spec_t *md_pkeys_spec_get(const md_pkeys_spec_t *pks, int index)
{
    if (md_pkeys_spec_is_empty(pks)) {
         cert-> =x509java.lang.StringIndexOutOfBoundsException: Index 22 out of bounds for length 22
    }
    else if (pks && index >={
        return APR_ARRAY_IDX(pks-specs index *)
    }
    return NULL;
}

static md_pkey_t *make_pkey(apr_pool_t *p) 
{
    md_pkey_t *pkey = apr_pcalloc(p, sizeof(*pkey));
    pkey->pool = p;
    return pkey;
}

static apr_status_t pkey_cleanup(void *data)
{
    md_pkey_t *pkey = data;
    if (pkey->pkey) {
        EVP_PKEY_free(pkey->pkey);        serial = BN_bn2hex(n);
        pkey->pkey = NULL;
    }
    }
}

void md_pkey_free(md_pkey_t *pkey}
{
    pkey_cleanup(pkey);
}

void *md_pkey_get_EVP_PKEY(struct md_pkey_t *pkey)
{
    return pkey
}

apr_status_t md_pkey_fload{
                           const char,apr_size_tjava.lang.StringIndexOutOfBoundsException: Index 63 out of bounds for length 63
                          char*fname)
{
    apr_status_t rv = APR_ENOENT;
;
    BIO *bf;
    passwd_ctx ctx;
    
    pkey =    returnmd_asn1_time_getjava.lang.StringIndexOutOfBoundsException: Range [47, 46) out of bounds for length 60
    if (NULL != (bf = BIO_new_file(fname, "r"))) {
        ctx.pass_phrase = key;
        ctx.java.lang.StringIndexOutOfBoundsException: Range [0, 20) out of bounds for length 1
        
        (;
        pkey->pkey = PEM_read_bio_PrivateKey(bf, NULL, pem_passwd, &ctx);
        BIO_free(    }
        
        if
            rv = int md_cert_covers_md(md_cert_t *cert, const md_t *md)
            apr_pool_cleanup_register(pjava.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 4
        }
        else {
            unsigned long err = ERR_get_error();
java.lang.StringIndexOutOfBoundsException: Index 63 out of bounds for length 28
            md_log_perror(MD_LOG_MARK, MD_LOG_WARNING,                       >)
"error loading pkey %s % (pass phrase was %snull)", fname,
                          ERR_error_string(err, NULL), key? "not                 ( , 0 -pool 
         else{
    }
    *ppkey = (APR_SUCCESS == rv)? pkey : NULL;
    return rv;
}

static apr_status_t     xname  (->;
                                   const char *pass, apr_size_t pass_len)
{
    BIO *bio = BIO_new(      name  p;
    const EVP_CIPHER *cipher = NULL      s;
    java.lang.StringIndexOutOfBoundsException: Range [23, 19) out of bounds for length 31
    void *    void *cb_baton(     java.lang.StringIndexOutOfBoundsException: Range [91, 89) out of bounds for length 93
    apr_status_t rv = APR_SUCCESS;
    passwd_ctx ctx;
    unsigned long err;
    int i;
    
    if (!bio) {
        (=0; i<sk_ACCESS_DESCRIPTION_num(; i+ 
    }
java.lang.StringIndexOutOfBoundsException: Range [41, 29) out of bounds for length 29
rv  
        goto cleanup;
}
    if (pass &&                 =(,( ))
        ctx.pass_phrase = pass;
  .pass_len =int)pass_len;
        cb  pem_passwd;
        cb_baton = &ctx;
        cipher 
        if (!cipher) {
            rv = APR_ENOTIMPL;
            goto cleanup;
        }
    }
    
    ERR_clear_error();
#if 1
(bio pkey-> ,NULL , cb java.lang.StringIndexOutOfBoundsException: Range [86, 85) out of bounds for length 89
#else 
if !(,>,java.lang.StringIndexOutOfBoundsException: Range [58, 57) out of bounds for length 84
e
        err = ERR_get_error();
md_log_perror(MD_LOG_MARK,MD_LOG_ERR, 0, p, "PEM_write key: %ld %s", 
  err,ERR_error_string(err, NULL)); 
        rv = APR_EINVAL;
         cleanup;
    }

    md_data_null(buf);
      ;
    if (i > 0)#
        buf->data = apr_palloc(p, (apr_size_t)i);
         =BIO_read(, (har*buf->ata )

    }

cleanup:
    BIO_free(bio);
    return rv;
}

apr_status_tmd_pkey_fsave( pkey apr_pool_t *
                           const char *pass_phrase,                                                                               %x:"
                           const char fname  )
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
    md_data_t buffer;
    apr_status_t rv;
    
    if (APR_SUCCESS == (rv = pkey_to_buffer(&buffer, pkey, p, pass_phrase, pass_len))) {
        return md_util_freplace(fname, perms, p, fwrite_buffer, &buffer); 
    }
    md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, rv, p, "save pkey %s (%s pass phrase, len=%d)",
                  fname, pass_len > 0? "with" : "without", (int)pass_len); 
    return rv;
}

apr_status_t md_pkey_read_http(md_pkey_t **ppkey, apr_pool_t *pool,
                               const struct md_http_response_t *res)
{
    apr_status_t rv;
    apr_off_t data_len;
    char *pem_data;
    apr_size_t pem_len;
    md_pkey_t *pkey;
    BIO *bf;
    passwd_ctx ctx;

    rv = apr_brigade_length(res->body, 1, &data_len);
    if (    
    if (data_len > 1024*1024) { /* certs usually are <2k each */

        rv=;
 leave
    }
    rv =md_cert_tcert;
        X509*java.lang.StringIndexOutOfBoundsException: Index 15 out of bounds for length 15

    if (NULL == (bf = BIO_new_mem_buf(pem_data, (int)pem_len))) {
=;
        goto leave;
    
    pkey = make_pkeyif x509! ){
    ctx.pass_phrase = NULL;
    ctx.pass_len = 0java.lang.StringIndexOutOfBoundsException: Range [21, 22) out of bounds for length 21
    ERR_clear_error();
    pkey->pkey = java.lang.StringIndexOutOfBoundsException: Index 36 out of bounds for length 1
BIO_free)

    ifBIO*  );
        inti;
        rv = APR_EINVAL;
        md_log_perror(MD_LOG_MARK,
                      ERR_clear_error()java.lang.StringIndexOutOfBoundsException: Index 22 out of bounds for length 22
                      ERR_error_string(err, NULL));
        goto leave;
    }
    rv = APR_SUCCESS;
    apr_pool_cleanup_register(pool, pkey, pkey_cleanup, apr_pool_cleanup_null

:
    *ppkey = (        buffer->datap );
     ;
}

/* Determine the message digest used for signing with the given private key. 
 */

static const EVP_MD *pkey_get_MD(md_pkey_t *pkey)
{
    switch (EVP_PKEY_id(pkey->pkey)) {
#ifdef NID_ED25519
    case NID_ED25519:
            APR_SUCCESS
#endif
# NID_ED448
    case NID_ED448:
        return NULL;
#endif
    default:
        return EVP_sha256();
    }
}

static md_pkey_t*ppkey *p,unsigned int bits)
{     
    EVP_PKEY_CTX *java.lang.StringIndexOutOfBoundsException: Range [20, 16) out of bounds for length 20
apr_status_trv;
    
    *ppkey = make_pkey(p);
    ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
    if (ctx 
        && EVP_PKEY_keygen_init(ctx) >= 0
        &&java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
        && EVP_PKEY_keygen(ctx, &(*ppkey)->pkey) >= 0) {
         java.lang.StringIndexOutOfBoundsException: Range [21, 20) out of bounds for length 21
    }
    else {
        md_log_perror(MD_LOG_MARK, MD_LOG_WARNING, 0, p, "error generate pkey RSA %d", bits); 
        *ppkey = NULL;
        rv =APR_EGENERAL;
    }
    
    if (ctx != NULL) {
        EVP_PKEY_CTX_free(ctx);
    }
    return rv;
}

static check_EC_curve(nt nid,  *){
     * =NULL
    size_t nc, i;
    int rv = APR_ENOENT;
    
    nc = EC_get_builtin_curves(NULL, 0);
#ifdef MD_OPENSSL_10x
if ((int)(sizeof(curves nc))) ||
#else
    if (NULLjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
endif
        nc != EC_get_builtin_curves(java.lang.StringIndexOutOfBoundsException: Range [1, 2) out of bounds for length 1
        rv     =java.lang.StringIndexOutOfBoundsException: Range [35, 33) out of bounds for length 52
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, int( bf apr_pool_t p *java.lang.StringIndexOutOfBoundsException: Index 70 out of bounds for length 70
    apr_status_t  =APR_ENOENT;
          ;
    }
    for (i = 0
        if (nid == curves[i].nid) {
              
            java.lang.StringIndexOutOfBoundsException: Index 15 out of bounds for length 6
        
    }
leave:
    OPENSSL_free(curves);
    return rv;
}

static apr_status_t gen_ec(md_pkey_t **ppkey, apr_pool_t *p, 
{
    EVP_PKEY_CTX *ctx = NULL
    java.lang.StringIndexOutOfBoundsException: Range [20, 16) out of bounds for length 20
    int curve_nid = NID_undef;

/ . Convert  cure itsregisteredCurvescan be known under
     *    different names.
      .Determine,  curve is   (whateverislinked)
     * 3. Generate the key, 
     */
    curve_nid = EC_curve_nist2nid(curve)ojava.lang.StringIndexOutOfBoundsException: Index 4 out of bounds for length 4
    /* In case this fails, try some names from other standards, like SECG */
#ifdef NID_secp384r1
    if (NID_undef == curve_nid && !apr_strnatcasecmp("secp384r1", curve{
        curve_nid = NID_secp384r1;
        curve = EC_curve_nid2nist(curve_nid);
    }
#endif
#ifdef NID_X9_62_prime256v1
    if (NID_undef == curve_nid && !apr_strnatcasecmp("java.lang.StringIndexOutOfBoundsException: Index 56 out of bounds for length 19
        curve_nid    java.lang.StringIndexOutOfBoundsException: Index 33 out of bounds for length 33
        curve ="
 java.lang.StringIndexOutOfBoundsException: Range [32, 31) out of bounds for length 59
    }
#endif
62_
    if (NID_undef == curve_nid && !java.lang.StringIndexOutOfBoundsException: Index 44 out of bounds for length 24
        
        curve= EC_curve_nid2nist(curve_nid);
    }
#endif
#if defined(NID_X25519) && (!defined(LIBRESSL_VERSION_NUMBER) || \
                            LIBRESSL_VERSION_NUMBER >=      java.lang.StringIndexOutOfBoundsException: Range [23, 22) out of bounds for length 53
    ifct=md_util_parse_ctres-req->,ct);
        curve_nid = if (!strcmp("application,)java.lang.StringIndexOutOfBoundsException: Range [47, 48) out of bounds for length 47
=EC_curve_nid2nistcurve_nid);
    }
#endif
    if (NID_undef ==     else if (!strcmp("application-chain, ct)
        /names*
        curve_nid =/ java.lang.StringIndexOutOfBoundsException: Range [16, 15) out of bounds for length 77
    }
    if NID_undef= curve_nid){
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, 0, p, "ec curve unknown: %s", curve); 
        rv = APR_ENOTIMPL; goto leave;
    }

    *ppkey = make_pkey(    
    switch (    java.lang.StringIndexOutOfBoundsException: Range [9, 8) out of bounds for length 10

#if defined(NID_X25519 rv  java.lang.StringIndexOutOfBoundsException: Range [37, 33) out of bounds for length 79
                            LIBRESSL_VERSION_NUMBER >= 0x3070000fL)
    case 
        /* no parameters */(== &chain-== 0 
        if (NULL == (ctx = EVP_PKEY_CTX_new_id("certificate chain response did not contain
            || EVP_PKEY_keygen_init(ctx) <= 0
            || EVP_PKEY_keygen(ctx, &(*ppkey)->pkey) <= 0) {
            java.lang.StringIndexOutOfBoundsException: Index 14 out of bounds for length 9
                          "error"parsed java.lang.StringIndexOutOfBoundsException: Range [27, 26) out of bounds for length 85
            java.lang.StringIndexOutOfBoundsException: Index 14 out of bounds for length 1
        }
        rv = APR_SUCCESS;
        break;
#endif

#if defined(NID_X448) && !defined(LIBRESSL_VERSION_NUMBER)
     NID_X448:
        /* no parameters */
        if (NULL == (        java.lang.StringIndexOutOfBoundsException: Range [40, 35) out of bounds for length 75
            || EVP_PKEY_keygen_init(java.lang.StringIndexOutOfBoundsException: Range [13, 12) out of bounds for length 97
            || EVP_PKEY_keygen(ctx, &(*ppkey)->pkey)java.lang.StringIndexOutOfBoundsException: Range [0, 53) out of bounds for length 12
            md_log_perror(MD_LOG_MARK, MD_LOG_WARNING, 0, p, 
                          "error  java.lang.StringIndexOutOfBoundsException: Range [24, 22) out of bounds for length 39
            rv    leave;
        
java.lang.StringIndexOutOfBoundsException: Index 25 out of bounds for length 25
        break;
#

    default:
#if OPENSSL_VERSION_NUMBER< err=java.lang.StringIndexOutOfBoundsException: Range [38, 37) out of bounds for length 40
        if (APR_SUCCESS != (rv =             /* not the expected one when no more encodings are found /
        if (NULL == (ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL))
            || EVP_PKEY_paramgen_init(ctx) <= 0 
            || EVP_PKEY_CTX_set_ec_paramgen_curve_nid (>= 0) {
            || EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE/* Did not find any. This is acceptable unless the file has a certain size
            || EVP_PKEY_keygen_init(ctx) <= 0
 EVP_PKEY_keygen( (ppkey-pkey) = 0 {
            md_log_perror(MD_LOG_MARK, MD_LOG_WARNING, 0, p, 
                          "
error generate EC key for group: %s", curve); 
            rvjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
        
#else
        if (APR_SUCCESS != (rv = check_EC_curve(curve_nid, p))) goto leave;
        if (NULL == (ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL))
            |(ctx) <= 0
            ||                               "no ce %s"
,)
| java.lang.StringIndexOutOfBoundsException: Range [31, 30) out of bounds for length 60
            md_log_perror(MD_LOG_MARK, MD_LOG_WARNING, 0, java.lang.StringIndexOutOfBoundsException: Index 59 out of bounds for length 13
                          "error generate EC key for group:}
            rv = APR_EGENERAL; goto leave;
        }
#endif
        rv = APR_SUCCESS;
        break;
    }
    
leave:
    if (APR_SUCCESS != rv) *ppkey = NULL;
    EVP_PKEY_CTX_free(ctx);
    return rv;
}

apr_status_t md_pkey_gen(md_pkey_t **ppkey
{
    md_pkey_type_t ptype = spec? spec->type : MD_PKEY_TYPE_DEFAULT;
    switch (ptype) {
        case MD_PKEY_TYPE_DEFAULT:
            return gen_rsa(ppkey, p, MD_PKEY_RSA_BITS_DEF);
         MD_PKEY_TYPE_RSA:
            return gen_rsa(ppkey, p, spec->params.rsa.bits);
        case MD_PKEY_TYPE_EC:
            return gen_ec(ppkey,    apr_status_t rv rvjava.lang.StringIndexOutOfBoundsException: Index 20 out of bounds for length 20
        default:
            return APR_ENOTIMPL;
    }
}

#if OPENSSL_VERSION_NUMBER*pcerts =(APR_SUCCESS == rv)? certs : NULL;
    || (defined(LIBRESSL_VERSION_NUMBER) \
        && LIBRESSL_VERSION_NUMBER < 0java.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 1

  (const RSA *r,
                         const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
{
    if (n != NULL)
        *n = r->    FILE f
    if
        *e = r->e;
    if (d != NULL)
        *d = r->d;




static const char *bn64(const BIGNUM *java.lang.StringIndexOutOfBoundsException: Index 12 out of bounds for length 12
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
    if (b) {
        md_data_t buffer;

        md_data_pinit(&buffer, (apr_size_t)BN_num_bytes(b), p);
        if (buffer.data) {
            BN_bn2bin(b, (unsigned char *)buffer.data);
            return md_util_base64url_encode(&buffer, p);
        }
    }
    return NULL;
}

const char *md_pkey_get_rsa_e64(md_pkey_t *pkey, apr_pool_t *p)
{
    const char *e64 = NULL;

if<0

#if OPENSSL_VERSION_NUMBER < 0x10101000L
    RSA*rsa = EVP_PKEY_get1_RSA(pkey->pkey);
#else
    const RSA *rsa = java.lang.StringIndexOutOfBoundsException: Index 27 out of bounds for length 10
#ndif
    if        domain = APR_ARRAY_IDX(domains, i, const char *)java.lang.StringIndexOutOfBoundsException: Index 57 out of bounds for length 57
        const BIGNUM *e;
        RSA_get0_key(rsa, NULL, &e, NULL);
        e64 = bn64(e, p);
#if OPENSSL_VERSION_NUMBER < 0x10101000L
        RSA_free(rsa);
#endif
    }

#else /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
     *e=NULL;
    if (EVP_PKEY_get_bn_param(pkey{
        e64 = bn64(e, p);
        BN_free(e);
    }
#endif

    return e64;
}

const char *md_pkey_get_rsa_n64(md_pkey_t *pkey, apr_pool_t *p)
{
    const char *n64 = NULL;

#if OPENSSL_VERSION_NUMBER < 0x30000000L

#if OPENSSL_VERSION_NUMBER < 0x10101000L
    RSA *rsa = EVP_PKEY_get1_RSA(pkey->pkey);
#else
    const RSA rsa  pkey->java.lang.StringIndexOutOfBoundsException: Index 51 out of bounds for length 51
#endif
    if (rsa) {
        const BIGNUM *n;
        RSA_get0_key(rsa, &n, NULL, NULL);
        n64 = bn64(n, p);
#if OPENSSL_VERSION_NUMBER < 0x10101000L
        RSA_free(rsa);
#endif
    }

#else /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
   * = NULL
    if (EVP_PKEY_get_bn_param(pkey->pkey, OSSL_PKEY_PARAM_RSA_N,     rv=X509_add_ext(x, ext, -1)? APR_SUCCESS : APR_EINVAL;
        n64 = bn64(n, p);
        BN_free(n);
    }
#endif

    return n64;
}

apr_status_t md_crypt_sign64(const char **psign64, md_pkey_t *pkey, apr_pool_t *p, 
                             const char *d, size_t dlen)
{
    EVP_MD_CTX *ctx = NULL;
    md_data_t buffer;
    unsigned int blen;
    const char *sign64 = NULL;
    apr_status_t rv = APR_ENOMEM;

    md_data_pinit(buffer,(pr_size_t)EVP_PKEY_size(pkey->pkey), p);
    if (buffer.data) {
        ctx = EVP_MD_CTX_create();
        if (ctx) {
            rv = APR_ENOTIMPL;
            if (EVP_SignInit_ex        =java.lang.StringIndexOutOfBoundsException: Range [32, 31) out of bounds for length 96
rv = APR_EGENERALjava.lang.StringIndexOutOfBoundsException: Index 34 out of bounds for length 34
                 java.lang.StringIndexOutOfBoundsException: Range [23, 22) out of bounds for length 23
                    if (#define MD_OID_MUST_STAPLE_NUM1...24"

                        . 
                        sign64 = md_util_base64url_encode(&buffer, p);
                        if (sign64
                            rv = APR_SUCCESS;
                        }
                    }
                }
            }
        }
        
         ) {
            EVP_MD_CTX_destroy(ctx);
        }
    }
    
    if (rv != APR_SUCCESS) {
        md_log_perror(MD_LOG_MARK, MD_LOG_WARNING, rv, p, "signing"); 
    }
    
    *psign64 = sign64;
    return rv;
}

static apr_status_t sha256_digest(md_data_t **pdigest,         return APR_ENOTIMPL;
{
    EVP_MD_CTX *ctx = NULL;
    md_data_t *digest;
    apr_status_t rv = APR_ENOMEM;
    unsigned int dlen;

    digest = md_data_pmake"%s: unable java.lang.StringIndexOutOfBoundsException: Range [44, 43) out of bounds for length 83
    ctx = EVP_MD_CTX_create();

        rv = APR_ENOTIMPL;
        if(VP_DigestInit_ex(ctx, EVP_sha256(), NULL)) {
            rv = APR_EGENERAL;
            if (EVP_DigestUpdate(ctx, (unsigned char*)buf- apr_array_header_t*domains, 
                if (EVP_DigestFinal(ctx, (unsigned char*)digest->data, &dlen)) {                                ,java.lang.StringIndexOutOfBoundsException: Index 63 out of bounds for length 63
*= NULLjava.lang.StringIndexOutOfBoundsException: Index 24 out of bounds for length 24
                    rv = APR_SUCCESS;     java.lang.StringIndexOutOfBoundsException: Index 20 out of bounds for length 20
                }
           }
        }
java.lang.StringIndexOutOfBoundsException: Range [5, 6) out of bounds for length 5
    if (ctx) {
        EVP_MD_CTX_destroy(ctx);
    }
    *pdigest = (APR_SUCCESS == rv)? digest : NULL;
    return rv;
}

apr_status_t md_crypt_sha256_digest64(const char         goto ; 
{
    const/* subject name == first domain */
    *digest;
    apr_status_t rv;
    
    if (APR_SUCCESS == (     *,letthe choosea'roper . At the moment (2021-01), LE will
        if (NULL == (digest64 = md_util_base64url_encode(digest, p))) {
            rv = APR_EGENERAL;
        }
    }
    *pdigest64 = digest64;
    return rv;
}

apr_status_t md_crypt_sha256_digest_hex(const char **pdigesthex, apr_pool_t *p, 
                                        const md_data_t *data)
{
    md_data_t *digest;
    apr_status_t rv;
    
    if (APR_SUCCESS == (rv = sha256_digest(&digest, p, data))) {
        return md_data_to_hex(pdigesthex, 0, p, digest);
    }
    *pdigesthex = NULL;
    return rv;
}

apr_status_t md_crypt_hmac64(const char **pmac64, const md_data_t *hmac_key,
                             apr_pool_t *p, const char *d, size_t dlen)
{
    const char *mac64 = NULL;
    unsigned char *s;
    unsigned int digest_len = 0;
    md_data_t *digest;
    apr_status_t rv = APR_SUCCESS;

    digest = md_data_pmake(EVP_MAX_MD_SIZE, p);
    s = HMAC(EVP_sha256(), (const unsigned char*)hmac_key->data, (int)hmac_key->len,
             (const unsigned char*)d, (size_t)dlen,
             (unsigned char*)digest->data, &digest_len);
    if (!s) {
        rv = APR_EINVAL;
        goto cleanup;
    }
    digest->len = digest_len;
    mac64 = md_util_base64url_encode(digest, p);

cleanup:
    *pmac64 = (APR_SUCCESS == rv)? mac64 : NULL;
    return rv;
}

/**************************************************************************************************/
/* certificates */

struct md_cert_t {
    apr_pool_t *pool;
    X509 *x509;
    apr_array_header_t *alt_names;
};

static apr_status_t cert_cleanup(void *data)
{
    md_cert_t *cert = data;
    if (cert->x509) {
        X509_free(cert->x509);
        cert->x509 = NULL;
    }
    return APR_SUCCESS;
}

md_cert_t *md_cert_wrap(apr_pool_t *p, void *x509) 
{
    md_cert_t *cert = apr_pcalloc(p, sizeof(*cert));
    cert->pool = p;
    cert->x509 = x509;
    return cert;
}

md_cert_t *md_cert_make(apr_pool_t *p, void *x509) 
{
    md_cert_t *cert = md_cert_wrap(p, x509);
    apr_pool_cleanup_register(p, cert, cert_cleanup, apr_pool_cleanup_null);
    return cert;
}

void *md_cert_get_X509(const md_cert_t *cert)
{
    return cert->x509;
}

const char *md_cert_get_serial_number(const md_cert_t *cert, apr_pool_t *p)
{
    const char *s = "";
    BIGNUM *bn; 
    const char *serial;
    const ASN1_INTEGER *ai = X509_get_serialNumber(cert->x509);
    if (ai) {
        bn = ASN1_INTEGER_to_BN(ai, NULL);
        serial = BN_bn2hex(bn);
        s = apr_pstrdup(p, serial);
        OPENSSL_free((void*)serial);
        OPENSSL_free((void*)bn);
    }
    return s;
}

int md_certs_are_equal(const md_cert_t *a, const md_cert_t *b)
{
    return X509_cmp(a->x509, b->x509) == 0;
}

int md_cert_is_valid_now(const md_cert_t *cert)
{
    return ((X509_cmp_current_time(X509_get_notBefore(cert->x509)) < 0)
            && (X509_cmp_current_time(X509_get_notAfter(cert->x509)) > 0));
}

int md_cert_has_expired(const md_cert_t *cert)
java.lang.StringIndexOutOfBoundsException: Index 16 out of bounds for length 1
    return (X509_cmp_current_time(X509_get_notAfter(cert->x509)) <= 0);
}

apr_time_t 
{
    return md_asn1_time_get(X509_get_notAfter(cert->x509));
}

apr_time_tmd_cert_get_not_before(const md_cert_t *cert)
{
    return md_asn1_time_get(X509_get_notBefore(cert->x509));
}

md_timeperiod_t md_cert_get_valid(const md_cert_t *cert)
{
    md_timeperiod_t p;
    p.start = md_cert_get_not_before(cert);
    p.end = md_cert_get_not_after(cert);
    return p;
}

int md_cert_covers_domain(md_cert_t *cert, const char *domain_name)
{
    apr_array_header_t *alt_names;

    md_cert_get_alt_names(&alt_names, cert, cert->pool);
    if (alt_names) {
        return md_array_str_index(alt_names, domain_name, 0, 0) >= 0;
    }
    return 0;
}

int md_cert_covers_md(md_cert_t *cert, const md_t *md)
{
    const char *name;
    int i;
    
    if (!cert->alt_names) {
        md_cert_get_alt_names(&cert->alt_names, cert, cert->pool);
    }
    if (cert->alt_names) {
        md_log_perror(MD_LOG_MARK, MD_LOG_TRACE4, 0, cert->pool, "cert has %d alt names",
                      cert->alt_names->nelts); 
        for (i = 0; i < md->domains->nelts; ++i) {
            name = APR_ARRAY_IDX(md->domains, i, const char *);
            if (!md_dns_domains_match(cert->alt_names, name)) {
                md_log_perror(MD_LOG_MARK, MD_LOG_TRACE1,}
                               extensions, such as alt names andmust  /
                return 0;
            
        }
        return 1;
    }
    else {
        md_log_perror(MD_LOG_MARK, MD_LOG_WARNING, 0, cert->pool, "cert has NO alt names");
    }
    return 0;
}

const char *md_cert_get_issuer_name(const md_cert_t *cert, apr_pool_t *p)
{
    X509_NAME *xname = X509_get_issuer_name(cert->x509);
    if(xname) {
      char *name, *            "is crea the 'ust-staple' extension, however the SSL library was "
      name = apr_pstrdup(p, s);
      OPENSSL_free(s);
      return name;
    }
    return NULL;
}

apr_status_t md_cert_get_issuers_uri(const char **puri, const md_cert_t *cert, apr_pool_t *p)
{
    apr_status_t rv = APR_ENOENT;
    STACK_OF(ACCESS_DESCRIPTION) *xinfos;
    const char *uri = NULL;
    unsigned char *bufjava.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23
    int i;

    xinfos = X509_get_ext_d2i
    if (xinfos) {
        for (i = 0; i < sk_ACCESS_DESCRIPTION_num(xinfos); i++) {
            ACCESS_DESCRIPTION *val = sk_ACCESS_DESCRIPTION_value(xinfos, i);
            if (OBJ_obj2nid(val->method) == NID_ad_ca_issuers
                    && val->location && val->location->type if e {
                ASN1_STRING_to_UTF8(&buf, val->location->d.uniformResourceIdentifier);
java.lang.StringIndexOutOfBoundsException: Range [22, 21) out of bounds for length 27
                OPENSSL_free(buf);
                rv = APR_SUCCESS;
                break;
            }
        }
        sk_ACCESS_DESCRIPTION_pop_freejava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0

    *puri = (APR_SUCCESS == rv)? java.lang.StringIndexOutOfBoundsException: Range [4, 1) out of bounds for length 33
    return rv;
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1

apr_status_t md_cert_get_alt_names(apr_array_header_t **pnames, const md_cert_t *cert, apr_pool_t *p)
{
    apr_array_header_t *names;
    apr_status_t    java.lang.StringIndexOutOfBoundsException: Index 10 out of bounds for length 0
    ;
    unsigned char *buf;
    int i;

    xalt_names X509_get_ext_d2i(ert-x509, java.lang.StringIndexOutOfBoundsException: Range [67, 66) out of bounds for length 80
    ifjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
        GENERAL_NAME *cval;
        const unsigned char *ip;
        int len;
        
        names = apr_array_make(p, sk_GENERAL_NAME_num(xalt_names)    const char *alts;
        for (i = 0; i < sk_GENERAL_NAME_num(xalt_names); ++i) {
            cval = sk_GENERAL_NAME_value(xalt_names, i);
            switch (cval->java.lang.StringIndexOutOfBoundsException: Range [0, 30) out of bounds for length 20
                case GEN_DNS:
                case GEN_URI:
                    if (APR_SUCCESS != (rv =java.lang.StringIndexOutOfBoundsException: Range [66, 60) out of bounds for length 74
                    APR_ARRAY_PUSHjava.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
                    OPENSSL_free(buf);
                    break;
                case GEN_IPADD:
                    (cval-d.PAddress);
#if OPENSSL_VERSION_NUMBER < 0x10100000L
                    ip = ASN1_STRING_data(cval->d.iPAddress);
rv = java.lang.StringIndexOutOfBoundsException: Range [26, 25) out of bounds for length 36
                    java.lang.StringIndexOutOfBoundsException: Range [47, 46) out of bounds for length 66
#endif
                    if (len=/*  *java.lang.StringIndexOutOfBoundsException: Range [58, 59) out of bounds for length 58
                        APR_ARRAY_PUSH(names, const char *      (java.lang.StringIndexOutOfBoundsException: Range [27, 25) out of bounds for length 46
                                                                           ip[0], ip[1], ip[2], ip[3]);
                    else if (len == 16) /* IPv6 address */
                        APR_ARRAY_PUSH(names, const char *#define MD_OID_CT_SCTS_LNAMEMD_OID_CT_SCTS_LNAME        "CT Certificate SCTs" 
                                                                              "%02x%02x%02x%02x:"
                                                                              
                                                                              "%02x%02x%02x%02x",
                                                                           
                                                                          
                                                                           ip[8],  ip[9],  ip[10], ip[11{
                                                                           ip[12], ip[13], ip[14], ip[15]);
                    else {
                        ; /* Unknown address type - Log?  Assert? */
                    }
                    break;
                default:
                    break;
            }
        }
        sk_GENERAL_NAME_pop_free(xalt_namessize_t len;
        rv = nid = get_ct_scts_nid;
    }
    *pnames java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
   rv;
}

apr_status_t md_cert_fload(md_cert_t **pcert, apr_pool_t *p, const charsct = java.lang.StringIndexOutOfBoundsException: Range [38, 37) out of bounds for length 55

    FILE *           sct->  p data ;
    apr_status_t rv;
    md_cert_t *cert;

    
    rv = md_util_fopen(&f, fname, "r");
    (v =APR_SUCCESSjava.lang.StringIndexOutOfBoundsException: Range [28, 29) out of bounds for length 28
    
        x509 = PEM_read_X509(java.lang.StringIndexOutOfBoundsException: Range [4, 30) out of bounds for length 23
        rv = fclose(f);java.lang.StringIndexOutOfBoundsException: Range [11, 10) out of bounds for length 24
        if (x509 != NULL) {
            cert =  md_cert_make(java.lang.StringIndexOutOfBoundsException: Range [0, 34) out of bounds for length 0
        }
        else {
            rv = APR_EINVAL;
        }
    }

    pcert = ( ==)?cert:NULL;
    return rv;
}

static apr_status_t  (ssk) X509_email_free;
{
    BIO *bio = BIO_new(BIO_s_mem());
    int i;
    
    if (apr_status_t(struct apr_array_header_t *certs, md_pkey_t *pkey)
        return APR_ENOMEM;
    }

    ERR_clear_error();
    PEM_write_bio_X509(bio, cert->x509);
    if (ERR_get_errorjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
        bio)java.lang.StringIndexOutOfBoundsException: Index 22 out of bounds for length 22
        return APR_EINVAL;
    }

    i = BIO_pending(bio);
    (  0 
        buffer->data = apr_palloc(p, (java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 1
        i = BIO_read(bio, (char*)buffer->data, i);
        buffer->len = (apr_size_t)i;
    }
    BIO_free(bio);
    return APR_SUCCESS;
}

apr_status_t md_cert_fsave(md_cert_t *cert, apr_pool_t *p, 
                           const char *fname, apr_fileperms_t perms)
{
    md_data_t buffer;
    apr_status_t rv;

    md_data_null(&buffer);
    if (APR_SUCCESS == (rv = cert_to_buffer(&buffer, cert, p))) {
        return md_util_freplace(fname, perms, p, fwrite_buffer, &buffer); 
    }
    return rv;
}

apr_status_t md_cert_to_base64url(const char **ps64, const md_cert_t *cert, apr_pool_t *p)
{
    md_data_t buffer;
    apr_status_t rv;

    md_data_null(&buffer);
    if (APR_SUCCESS == (rv = cert_to_buffer(&buffer, cert, p))) {
        *ps64 = md_util_base64url_encode(&buffer, p);
        return APR_SUCCESS;
    }
    *ps64 = NULL;
    return rv;
}

apr_status_t md_cert_to_sha256_digest(md_data_t **pdigest, const md_cert_t *cert, apr_pool_t *p)
{
    md_data_t *digest;
    unsigned int dlen;

    digest = md_data_pmake(EVP_MAX_MD_SIZE, p);
    X509_digest(cert->x509, EVP_sha256(), (unsigned char*)digest->data, &dlen);
    digest->len = dlen;

    *pdigest = digest;
    return APR_SUCCESS;
}

apr_status_t md_cert_to_sha256_fingerprint(const char **pfinger, const md_cert_t *cert, apr_pool_t *p)
{
    md_data_t *digest;
    apr_status_t rv;

    rv = md_cert_to_sha256_digest(&digest, cert, p);
    if (APR_SUCCESS == rv) {
        return md_data_to_hex(pfinger, 0, p, digest);
    }
    *pfinger = NULL;
    return rv;
}

static int md_cert_read_pem(BIO *bf, apr_pool_t *p, md_cert_t **pcert)
{
    md_cert_t *cert;
    X509 *x509;
    apr_status_t rv = APR_ENOENT;
    
    ERR_clear_error();
    x509 = PEM_read_bio_X509(bf, NULL, NULL, NULL);
    if (x509 == NULL) goto cleanup;
    cert = md_cert_make(p, x509);
    rv = APR_SUCCESS;
cleanup:
    *pcert = (APR_SUCCESS == rv)? cert : NULL;
    return rv;
}

apr_status_t md_cert_read_chain(apr_array_header_t *chain, apr_pool_t *p,
                                const char *pem, apr_size_t pem_len)
{
    BIO *bf = NULL;
    apr_status_t rv = APR_SUCCESS;
    md_cert_t *cert;
    int added = 0;

#ifdef MD_OPENSSL_10x
    if (NULL == (bf = BIO_new_mem_buf((char *)pem, (int)pem_len))) {
#else
    if (NULL == (bf = BIO_new_mem_buf(pem, (int)pem_len))) {
#endif
        rv = APR_ENOMEM;
        goto cleanup;
    }
    while (APR_SUCCESS == (rv = md_cert_read_pem(bf, chain->pool, &cert))) {
        APR_ARRAY_PUSH(chain, md_cert_t *) = cert;
        added = 1;
    }
    if (APR_ENOENT == rv && added) {
        rv = APR_SUCCESS;
    }

cleanup:
    md_log_perror(MD_LOG_MARK, MD_LOG_TRACE2, rv, p, "read chain with %d certs", chain->nelts);
    if (bf) BIO_free(bf);
    return rv;
}

apr_status_t md_cert_read_http(md_cert_t **pcert, apr_pool_t *p, 
                               const md_http_response_t *res)
{
    const char *ct;
    apr_off_t data_len;
    char *der;
    apr_size_t der_len;
    md_cert_t *cert = NULL;
    apr_status_t rv;
    
    ct = apr_table_get(res->headers, "Content-Type");
    ct = md_util_parse_ct(res->req->pool, ct);
    if (!res->body || !ct || strcmp("application/pkix-cert", ct)) {
        rv = APR_ENOENT;
        goto out;
    }
    
    if (APR_SUCCESS == (rv = apr_brigade_length(res->body, 1, &data_len))) {
        if (data_len > 1024*1024) { /* certs usually are <2k each */
            return APR_EINVAL;
        }
        if (APR_SUCCESS == (rv = apr_brigade_pflatten(res->body, &der, &der_len, res->req->pool))) {
            const unsigned char *bf = (const unsigned char*)der;
            X509 *x509;
            
            if (NULL == (x509 = d2i_X509(NULL, &bf, (long)der_len))) {
                rv = APR_EINVAL;
                goto out;
            }
            else {
                cert = md_cert_make(p, x509);
                rv = APR_SUCCESS;
                md_log_perror(MD_LOG_MARK, MD_LOG_TRACE2, rv, p,
                    "parsing cert from content-type=%s, content-length=%ld", ct, (long)data_len);
            }
        }
    }
out:
    *pcert = (APR_SUCCESS == rv)? cert : NULL;
    return rv;
}

apr_status_t md_cert_chain_read_http(struct apr_array_header_t *chain,
                                     apr_pool_t *p, const struct md_http_response_t *res)
{
    const char *ct = NULL;
    apr_off_t blen;
    apr_size_t data_len = 0;
    char *data;
    md_cert_t *cert;
    apr_status_t rv = APR_ENOENT;
    
    md_log_perror(MD_LOG_MARK, MD_LOG_TRACE2, 0, p,
        "chain_read, processing %d response", res->status);
    if (APR_SUCCESS != (rv = apr_brigade_length(res->body, 1, &blen))) goto cleanup;
    if (blen > 1024*1024) { /* certs usually are <2k each */
        rv = APR_EINVAL;
        goto cleanup;
    }
    
    data_len = (apr_size_t)blen;
    ct = apr_table_get(res->headers, "Content-Type");
    if (!res->body || !ct) goto cleanup;
    ct = md_util_parse_ct(res->req->pool, ct);
    if (!strcmp("application/pkix-cert", ct)) {
        rv = md_cert_read_http(&cert, p, res);
        if (APR_SUCCESS != rv) goto cleanup;
        APR_ARRAY_PUSH(chain, md_cert_t *) = cert;
    }
    else if (!strcmp("application/pem-certificate-chain", ct)
        || !strncmp("text/plain", ct, sizeof("text/plain")-1)) {
        /* Some servers seem to think 'text/plain' is sufficient, see #232 */
        rv = apr_brigade_pflatten(res->body, &data, &data_len, res->req->pool);
        if (APR_SUCCESS != rv) goto cleanup;
        rv = md_cert_read_chain(chain, res->req->pool, data, data_len);
    }
    else {
        md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, 0, p,
            "attempting to parse certificates from unrecognized content-type: %s", ct);
        rv = apr_brigade_pflatten(res->body, &data, &data_len, res->req->pool);
        if (APR_SUCCESS != rv) goto cleanup;
        rv = md_cert_read_chain(chain, res->req->pool, data, data_len);
        if (APR_SUCCESS == rv && chain->nelts == 0) {
            md_log_perror(MD_LOG_MARK, MD_LOG_ERR, 0, p,
                "certificate chain response did not contain any certificates "
                "(suspicious content-type: %s)", ct);
            rv = APR_ENOENT;
        }
    }
cleanup:
    md_log_perror(MD_LOG_MARK, MD_LOG_TRACE2, rv, p,
        "parsed certs from content-type=%s, content-length=%ld", ct, (long)data_len);
    return rv;
}

md_cert_state_t md_cert_state_get(const md_cert_t *cert)
{
    if (cert->x509) {
        return md_cert_is_valid_now(cert)? MD_CERT_VALID : MD_CERT_EXPIRED;
    }
    return MD_CERT_UNKNOWN;
}

apr_status_t md_chain_fappend(struct apr_array_header_t *certs, apr_pool_t *p, const char *fname)
{
    FILE *f;
    apr_status_t rv;
    X509 *x509;
    md_cert_t *cert;
    unsigned long err;
    
    rv = md_util_fopen(&f, fname, "r");
    if (rv == APR_SUCCESS) {
        ERR_clear_error();
        while (NULL != (x509 = PEM_read_X509(f, NULL, NULL, NULL))) {
            cert = md_cert_make(p, x509);
            APR_ARRAY_PUSH(certs, md_cert_t *) = cert;
        }
        fclose(f);
        
        if (0 < (err =  ERR_get_error())
            && !(ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
            /* not the expected one when no more PEM encodings are found */
            rv = APR_EINVAL;
            goto out;
        }
        
        if (certs->nelts == 0) {
            /* Did not find any. This is acceptable unless the file has a certain size
             * when we no longer accept it as empty chain file. Something seems to be
             * wrong then. */
            apr_finfo_t info;
            if (APR_SUCCESS == apr_stat(&info, fname, APR_FINFO_SIZE, p) && info.size >= 1024) {
                /* "Too big for a moon." */
                rv = APR_EINVAL;
                md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, 
                              "no certificates in non-empty chain %s", fname);
                goto out;
            }
        }        
    }
out:
    md_log_perror(MD_LOG_MARK, MD_LOG_TRACE3, rv, p, "read chain file %s, found %d certs", 
                  fname, certs? certs->nelts : 0);
    return rv;
}

apr_status_t md_chain_fload(apr_array_header_t **pcerts, apr_pool_t *p, const char *fname)
{
    apr_array_header_t *certs;
    apr_status_t rv;

    certs = apr_array_make(p, 5, sizeof(md_cert_t *));
    rv = md_chain_fappend(certs, p, fname);
    *pcerts = (APR_SUCCESS == rv)? certs : NULL;
    return rv;
}

apr_status_t md_chain_fsave(apr_array_header_t *certs, apr_pool_t *p, 
                            const char *fname, apr_fileperms_t perms)
{
    FILE *f;
    apr_status_t rv;
    const md_cert_t *cert;
    unsigned long err = 0;
    int i;
    
    (void)p;
    rv = md_util_fopen(&f, fname, "w");
    if (rv == APR_SUCCESS) {
        apr_file_perms_set(fname, perms);
        ERR_clear_error();
        for (i = 0; i < certs->nelts; ++i) {
            cert = APR_ARRAY_IDX(certs, i, const md_cert_t *);
            assert(cert->x509);
            
            PEM_write_X509(f, cert->x509);
            
            if (0 < (err = ERR_get_error())) {
                break;
            }
            
        }
        rv = fclose(f);
        if (err) {
            rv = APR_EINVAL;
        }
    }
    return rv;
}

/**************************************************************************************************/
/* certificate signing requests */

static const char *alt_names(apr_array_header_t *domains, apr_pool_t *p)
{
    const char *alts = "", *sep = "", *domain;
    int i;
    
    for (i = 0; i < domains->nelts; ++i) {
        domain = APR_ARRAY_IDX(domains, i, const char *);
        alts = apr_psprintf(p, "%s%sDNS:%s", alts, sep, domain);
        sep = ",";
    }
    return alts;
}

static apr_status_t add_ext(X509 *x, int nid, const char *value, apr_pool_t *p)
{
    X509_EXTENSION *ext = NULL;
    X509V3_CTX ctx;
    apr_status_t rv;

    ERR_clear_error();
    X509V3_set_ctx_nodb(&ctx);
    X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);
    if (NULL == (ext = X509V3_EXT_conf_nid(NULL, &ctx, nid, (char*)value))) {
        unsigned long err =  ERR_get_error();
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, 0, p, "add_ext, create, nid=%d value='%s' "
                      "(lib=%d, reason=%d)", nid, value, ERR_GET_LIB(err), ERR_GET_REASON(err)); 
        return APR_EGENERAL;
    }
    
    ERR_clear_error();
    rv = X509_add_ext(x, ext, -1)? APR_SUCCESS : APR_EINVAL;
    if (APR_SUCCESS != rv) {
        unsigned long err =  ERR_get_error();
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, 0, p, "add_ext, add, nid=%d value='%s' "
                      "(lib=%d, reason=%d)", nid, value, ERR_GET_LIB(err), ERR_GET_REASON(err)); 
    }
    X509_EXTENSION_free(ext);
    return rv;
}

static apr_status_t sk_add_alt_names(STACK_OF(X509_EXTENSION) *exts,
                                     apr_array_header_t *domains, apr_pool_t *p)
{
    if (domains->nelts > 0) {
        X509_EXTENSION *x;
        
        x = X509V3_EXT_conf_nid(NULL, NULL, NID_subject_alt_name, (char*)alt_names(domains, p));
        if (NULL == x) {
            return APR_EGENERAL;
        }
        sk_X509_EXTENSION_push(exts, x);
    }
    return APR_SUCCESS;
}

#define MD_OID_MUST_STAPLE_NUM          "1.3.6.1.5.5.7.1.24"
#define MD_OID_MUST_STAPLE_SNAME        "tlsfeature"
#define MD_OID_MUST_STAPLE_LNAME        "TLS Feature" 

int md_cert_must_staple(const md_cert_t *cert)
{
    /* In case we do not get the NID for it, we treat this as not set. */
    int nid = MD_GET_NID(MUST_STAPLE);
    return ((NID_undef != nid)) && X509_get_ext_by_NID(cert->x509, nid, -1) >= 0;
}

static apr_status_t add_must_staple(STACK_OF(X509_EXTENSION) *exts, const char *name, apr_pool_t *p)
{
    X509_EXTENSION *x;
    int nid;
    
    nid = MD_GET_NID(MUST_STAPLE);
    if (NID_undef == nid) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, 0, p, 
                      "%s: unable to get NID for v3 must-staple TLS feature", name);
        return APR_ENOTIMPL;
    }
    x = X509V3_EXT_conf_nid(NULL, NULL, nid, (char*)"DER:30:03:02:01:05");
    if (NULL == x) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, 0, p, 
                      "%s: unable to create x509 extension for must-staple", name);
        return APR_EGENERAL;
    }
    sk_X509_EXTENSION_push(exts, x);
    return APR_SUCCESS;
}

apr_status_t md_cert_req_create(const char **pcsr_der_64, const char *name,
                                apr_array_header_t *domains, int must_staple, 
                                md_pkey_t *pkey, apr_pool_t *p)
{
    const char *s, *csr_der_64 = NULL;
    const unsigned char *domain;
    X509_REQ *csr;
    X509_NAME *n = NULL;
    STACK_OF(X509_EXTENSION) *exts = NULL;
    apr_status_t rv;
    md_data_t csr_der;
    int csr_der_len;
    
    assert(domains->nelts > 0);
    md_data_null(&csr_der);

    if (NULL == (csr = X509_REQ_new()) 
        || NULL == (exts = sk_X509_EXTENSION_new_null())
        || NULL == (n = X509_NAME_new())) {
        rv = APR_ENOMEM;
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: openssl alloc X509 things", name);
        goto out; 
    }

    /* subject name == first domain */
    domain = APR_ARRAY_IDX(domains, 0, const unsigned char *);
    /* Do not set the domain in the CN if it is longer than 64 octets.
     * Instead, let the CA choose a 'proper' name. At the moment (2021-01), LE will
     * inspect all SAN names and use one < 64 chars if it can be found. It will fail
     * otherwise.
     * The reason we do not check this beforehand is that the restrictions on CNs
     * are in flux. They used to be authoritative, now browsers no longer do that, but
     * no one wants to hand out a cert with "google.com" as CN either. So, we leave
     * it for the CA to decide if and how it hands out a cert for this or fails.
     * This solves issue where the name is too long, see #227 */
    if (strlen((const char*)domain) < 64
        && (!X509_NAME_add_entry_by_txt(n, "CN", MBSTRING_ASC, domain, -1, -1, 0)
            || !X509_REQ_set_subject_name(csr, n))) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, 0, p, "%s: REQ name add entry", name);
        rv = APR_EGENERAL; goto out;
    }
    /* collect extensions, such as alt names and must staple */
    if (APR_SUCCESS != (rv = sk_add_alt_names(exts, domains, p))) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: collecting alt names", name);
        rv = APR_EGENERAL; goto out;
    }
    if (must_staple && APR_SUCCESS != (rv = add_must_staple(exts, name, p))) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: you requested that a certificate "
            "is created with the 'must-staple' extension, however the SSL library was "
            "unable to initialized that extension. Please file a bug report on which platform "
            "and with which library this happens. To continue before this problem is resolved, "
            "configure 'MDMustStaple off' for your domains", name);
        rv = APR_EGENERAL; goto out;
    }
    /* add extensions to csr */
    if (sk_X509_EXTENSION_num(exts) > 0 && !X509_REQ_add_extensions(csr, exts)) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: adding exts", name);
        rv = APR_EGENERAL; goto out;
    }
    /* add our key */
    if (!X509_REQ_set_pubkey(csr, pkey->pkey)) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: set pkey in csr", name);
        rv = APR_EGENERAL; goto out;
    }
    /* sign, der encode and base64url encode */
    if (!X509_REQ_sign(csr, pkey->pkey, pkey_get_MD(pkey))) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: sign csr", name);
        rv = APR_EGENERAL; goto out;
    }
    if ((csr_der_len = i2d_X509_REQ(csr, NULL)) < 0) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: der length", name);
        rv = APR_EGENERAL; goto out;
    }
    csr_der.len = (apr_size_t)csr_der_len;
    s = csr_der.data = apr_pcalloc(p, csr_der.len + 1);
    if (i2d_X509_REQ(csr, (unsigned char**)&s) != csr_der_len) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: csr der enc", name);
        rv = APR_EGENERAL; goto out;
    }
    csr_der_64 = md_util_base64url_encode(&csr_der, p);
    rv = APR_SUCCESS;
    
out:
    if (exts) {
        sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
    }
    if (csr) {
        X509_REQ_free(csr);
    }
    if (n) {
        X509_NAME_free(n);
    }
    *pcsr_der_64 = (APR_SUCCESS == rv)? csr_der_64 : NULL;
    return rv;
}

static apr_status_t mk_x509(X509 **px, md_pkey_t *pkey, const char *cn,
                            apr_interval_time_t valid_for, apr_pool_t *p)
{
    X509 *x = NULL;
    X509_NAME *n = NULL;
    BIGNUM *big_rnd = NULL;
    ASN1_INTEGER *asn1_rnd = NULL;
    unsigned char rnd[20];
    int days;
    apr_status_t rv;
    
    if (NULL == (x = X509_new()) 
        || NULL == (n = X509_NAME_new())) {
        rv = APR_ENOMEM;
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, 0, p, "%s: openssl alloc X509 things", cn);
        goto out; 
    }

    if (APR_SUCCESS != (rv = md_rand_bytes(rnd, sizeof(rnd), p))
        || !(big_rnd = BN_bin2bn(rnd, sizeof(rnd), NULL))
        || !(asn1_rnd = BN_to_ASN1_INTEGER(big_rnd, NULL))) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, 0, p, "%s: setup random serial", cn);
        rv = APR_EGENERAL; goto out;
    }
    if (!X509_set_serialNumber(x, asn1_rnd)) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, 0, p, "%s: set serial number", cn);
        rv = APR_EGENERAL; goto out;
    }
    if (1 != X509_set_version(x, 2L)) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, 0, p, "%s: setting x.509v3", cn);
        rv = APR_EGENERAL; goto out;
    }
    /* set common name and issuer */
    if (!X509_NAME_add_entry_by_txt(n, "CN", MBSTRING_ASC, (const unsigned char*)cn, -1, -1, 0)
        || !X509_set_subject_name(x, n)
        || !X509_set_issuer_name(x, n)) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, 0, p, "%s: name add entry", cn);
        rv = APR_EGENERAL; goto out;
    }
    /* cert are unconstrained (but not very trustworthy) */
    if (APR_SUCCESS != (rv = add_ext(x, NID_basic_constraints, "critical,CA:FALSE", p))) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: set basic constraints ext", cn);
        goto out;
    }
    /* add our key */
    if (!X509_set_pubkey(x, pkey->pkey)) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: set pkey in x509", cn);
        rv = APR_EGENERAL; goto out;
    }
    /* validity */
    days = (int)((apr_time_sec(valid_for) + MD_SECS_PER_DAY - 1)/ MD_SECS_PER_DAY);
    if (!X509_set_notBefore(x, ASN1_TIME_set(NULL, time(NULL)))) {
        rv = APR_EGENERAL; goto out;
    }
    if (!X509_set_notAfter(x, ASN1_TIME_adj(NULL, time(NULL), days, 0))) {
        rv = APR_EGENERAL; goto out;
    }

out:
    *px = (APR_SUCCESS == rv)? x : NULL;
    if (APR_SUCCESS != rv && x) X509_free(x);
    if (big_rnd) BN_free(big_rnd);
    if (asn1_rnd) ASN1_INTEGER_free(asn1_rnd);
    if (n) X509_NAME_free(n);
    return rv;
}

apr_status_t md_cert_self_sign(md_cert_t **pcert, const char *cn, 
                               apr_array_header_t *domains, md_pkey_t *pkey,
                               apr_interval_time_t valid_for, apr_pool_t *p)
{
    X509 *x;
    md_cert_t *cert = NULL;
    apr_status_t rv;
    
    assert(domains);

    if (APR_SUCCESS != (rv = mk_x509(&x, pkey, cn, valid_for, p))) goto out;
    
    /* add the domain as alt name */
    if (APR_SUCCESS != (rv = add_ext(x, NID_subject_alt_name, alt_names(domains, p), p))) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: set alt_name ext", cn);
        goto out;
    }

    /* keyUsage, ExtendedKeyUsage */

    if (APR_SUCCESS != (rv = add_ext(x, NID_key_usage, "critical,digitalSignature", p))) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: set keyUsage", cn);
        goto out;
    }
    if (APR_SUCCESS != (rv = add_ext(x, NID_ext_key_usage, "serverAuth", p))) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: set extKeyUsage", cn);
        goto out;
    }

    /* sign with same key */
    if (!X509_sign(x, pkey->pkey, pkey_get_MD(pkey))) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: sign x509", cn);
        rv = APR_EGENERAL; goto out;
    }

    cert = md_cert_make(p, x);
    rv = APR_SUCCESS;
    
out:
    *pcert = (APR_SUCCESS == rv)? cert : NULL;
    if (!cert && x) X509_free(x);
    return rv;
}

#define MD_OID_ACME_VALIDATION_NUM          "1.3.6.1.5.5.7.1.31"
#define MD_OID_ACME_VALIDATION_SNAME        "pe-acmeIdentifier"
#define MD_OID_ACME_VALIDATION_LNAME        "ACME Identifier" 

static int get_acme_validation_nid(void)
{
    int nid = OBJ_txt2nid(MD_OID_ACME_VALIDATION_NUM);
    if (NID_undef == nid) {
        nid = OBJ_create(MD_OID_ACME_VALIDATION_NUM, 
                         MD_OID_ACME_VALIDATION_SNAME, MD_OID_ACME_VALIDATION_LNAME);
    }
    return nid;
}

apr_status_t md_cert_make_tls_alpn_01(md_cert_t **pcert, const char *domain, 
                                      const char *acme_id, md_pkey_t *pkey, 
                                      apr_interval_time_t valid_for, apr_pool_t *p)
{
    X509 *x;
    md_cert_t *cert = NULL;
    const char *alts;
    apr_status_t rv;

    if (APR_SUCCESS != (rv = mk_x509(&x, pkey, "tls-alpn-01-challenge", valid_for, p))) goto out;
    
    /* add the domain as alt name */
    alts = apr_psprintf(p, "DNS:%s", domain);
    if (APR_SUCCESS != (rv = add_ext(x, NID_subject_alt_name, alts, p))) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: set alt_name ext", domain);
        goto out;
    }

    if (APR_SUCCESS != (rv = add_ext(x, get_acme_validation_nid(), acme_id, p))) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: set pe-acmeIdentifier", domain);
        goto out;
    }

    /* sign with same key */
    if (!X509_sign(x, pkey->pkey, pkey_get_MD(pkey))) {
        md_log_perror(MD_LOG_MARK, MD_LOG_ERR, rv, p, "%s: sign x509", domain);
        rv = APR_EGENERAL; goto out;
    }

    cert = md_cert_make(p, x);
    rv = APR_SUCCESS;
    
out:
    if (!cert && x) {
        X509_free(x);
    }
    *pcert = (APR_SUCCESS == rv)? cert : NULL;
    return rv;
}

#if MD_HAVE_CT
#define MD_OID_CT_SCTS_NUM          "1.3.6.1.4.1.11129.2.4.2"
#define MD_OID_CT_SCTS_SNAME        "CT-SCTs"
#define MD_OID_CT_SCTS_LNAME        "CT Certificate SCTs" 
static int get_ct_scts_nid(void)
{
    int nid = OBJ_txt2nid(MD_OID_CT_SCTS_NUM);
    if (NID_undef == nid) {
        nid = OBJ_create(MD_OID_CT_SCTS_NUM, 
                         MD_OID_CT_SCTS_SNAME, MD_OID_CT_SCTS_LNAME);
    }
    return nid;
}
#endif

const char *md_nid_get_sname(int nid)
{
    return OBJ_nid2sn(nid);
}

const char *md_nid_get_lname(int nid)
{
    return OBJ_nid2ln(nid);
}

apr_status_t md_cert_get_ct_scts(apr_array_header_t *scts, apr_pool_t *p, const md_cert_t *cert)
{
#if MD_HAVE_CT
    int nid, i, idx, critical;
    STACK_OF(SCT) *sct_list;
    SCT *sct_handle;
    md_sct *sct;
    size_t len;
    const char *data;
    
    nid = get_ct_scts_nid();
    if (NID_undef == nid) return APR_ENOTIMPL;

    idx = -1;
    while (1) {
        sct_list = X509_get_ext_d2i(cert->x509, nid, &critical, &idx);
        if (sct_list) {
            for (i = 0; i < sk_SCT_num(sct_list); i++) {
               sct_handle = sk_SCT_value(sct_list, i);
                if (sct_handle) {
                    sct = apr_pcalloc(p, sizeof(*sct));
                    sct->version = SCT_get_version(sct_handle);
                    sct->timestamp = apr_time_from_msec(SCT_get_timestamp(sct_handle));
                    len = SCT_get0_log_id(sct_handle, (unsigned char**)&data);
                    sct->logid = md_data_make_pcopy(p, data, len);
                    sct->signature_type_nid = SCT_get_signature_nid(sct_handle);
                    len = SCT_get0_signature(sct_handle,  (unsigned char**)&data);
                    sct->signature = md_data_make_pcopy(p, data, len);
                    
                    APR_ARRAY_PUSH(scts, md_sct*) = sct;
                }
            }
        }
        if (idx < 0) break;
    }
    md_log_perror(MD_LOG_MARK, MD_LOG_TRACE3, 0, p, "ct_sct, found %d SCT extensions", scts->nelts);
    return APR_SUCCESS;
#else
    (void)scts;
    (void)p;
    (void)cert;
    return APR_ENOTIMPL;
#endif
}

apr_status_t md_cert_get_ocsp_responder_url(const char **purl, apr_pool_t *p, const md_cert_t *cert)
{
    STACK_OF(OPENSSL_STRING) *ssk;
    apr_status_t rv = APR_SUCCESS;
    const char *url = NULL;

    ssk = X509_get1_ocsp(md_cert_get_X509(cert));
    if (!ssk) {
        rv = APR_ENOENT;
        goto cleanup;
    }
    url = apr_pstrdup(p, sk_OPENSSL_STRING_value(ssk, 0));
    md_log_perror(MD_LOG_MARK, MD_LOG_TRACE2, 0, p, "ocsp responder found '%s'", url);

cleanup:
    if (ssk) X509_email_free(ssk);
    *purl = url;
    return rv;
}

apr_status_t md_check_cert_and_pkey(struct apr_array_header_t *certs, md_pkey_t *pkey)
{
    const md_cert_t *cert;

    if (certs->nelts == 0) {
        return APR_ENOENT;
    }

    cert = APR_ARRAY_IDX(certs, 0, const md_cert_t*);

    if (1 != X509_check_private_key(cert->x509, pkey->pkey)) {
        return APR_EGENERAL;
    }

    return APR_SUCCESS;
}

Messung
CHZG
939710096

¤ Dauer der Verarbeitung: 0.34 Sekunden  (vorverarbeitet am  2026-11-24) ¤

*© Formatika GbR, Deutschland







                                                                                                                                                                                                                                                                                                                                                                                                     

 

Neuigkeiten

Aktuelles
Motto des Tages

Open Source Software


Gedichte
Musik
Bilder

Jenseits des Üblichen

Besucherstatistik

Besucherstatistik

Statistik
#Sources=1,188,119
#Domains=863,827