]> git.ipfire.org Git - thirdparty/apache/httpd.git/commitdiff
mod_ssl: Factor out code to read a BIO into a palloc'ed string:
authorJoe Orton <jorton@apache.org>
Sat, 2 May 2020 10:56:31 +0000 (10:56 +0000)
committerJoe Orton <jorton@apache.org>
Sat, 2 May 2020 10:56:31 +0000 (10:56 +0000)
* modules/ssl/ssl_util_ssl.c (modssl_bio_free_read): New function.
  (asn1_string_convert): Use it here.

* modules/ssl/ssl_engine_vars.c: Use it throughout.

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1877291 13f79535-47bb-0310-9956-ffa450edef68

modules/ssl/ssl_engine_vars.c
modules/ssl/ssl_util_ssl.c
modules/ssl/ssl_util_ssl.h

index cabf08170f98231bb3ca8156a1be08b0493206f0..63f65ed2c4a963d701efcab97f851b87a6b1f517 100644 (file)
@@ -539,18 +539,13 @@ static char *ssl_var_lookup_ssl_cert_dn_oneline(apr_pool_t *p, request_rec *r,
     }
     else {
         BIO* bio;
-        int n;
         unsigned long flags = XN_FLAG_RFC2253 & ~ASN1_STRFLGS_ESC_MSB;
+
         if ((bio = BIO_new(BIO_s_mem())) == NULL)
             return NULL;
         X509_NAME_print_ex(bio, xsname, 0, flags);
-        n = BIO_pending(bio);
-        if (n > 0) {
-            result = apr_palloc(p, n+1);
-            n = BIO_read(bio, result, n);
-            result[n] = NUL;
-        }
-        BIO_free(bio);
+
+        result = modssl_bio_free_read(p, bio);
     }
     return result;
 }
@@ -757,19 +752,13 @@ static char *ssl_var_lookup_ssl_cert_san(apr_pool_t *p, X509 *xs, char *var)
 
 static char *ssl_var_lookup_ssl_cert_valid(apr_pool_t *p, ASN1_TIME *tm)
 {
-    char *result;
     BIO* bio;
-    int n;
 
     if ((bio = BIO_new(BIO_s_mem())) == NULL)
         return NULL;
     ASN1_TIME_print(bio, tm);
-    n = BIO_pending(bio);
-    result = apr_pcalloc(p, n+1);
-    n = BIO_read(bio, result, n);
-    result[n] = NUL;
-    BIO_free(bio);
-    return result;
+
+    return modssl_bio_free_read(p, bio);
 }
 
 #define DIGIT2NUM(x) (((x)[0] - '0') * 10 + (x)[1] - '0')
@@ -818,19 +807,13 @@ static char *ssl_var_lookup_ssl_cert_remain(apr_pool_t *p, ASN1_TIME *tm)
 
 static char *ssl_var_lookup_ssl_cert_serial(apr_pool_t *p, X509 *xs)
 {
-    char *result;
     BIO *bio;
-    int n;
 
     if ((bio = BIO_new(BIO_s_mem())) == NULL)
         return NULL;
     i2a_ASN1_INTEGER(bio, X509_get_serialNumber(xs));
-    n = BIO_pending(bio);
-    result = apr_pcalloc(p, n+1);
-    n = BIO_read(bio, result, n);
-    result[n] = NUL;
-    BIO_free(bio);
-    return result;
+
+    return modssl_bio_free_read(p, bio);
 }
 
 static char *ssl_var_lookup_ssl_cert_chain(apr_pool_t *p, STACK_OF(X509) *sk, char *var)
@@ -885,19 +868,13 @@ static char *ssl_var_lookup_ssl_cert_rfc4523_cea(apr_pool_t *p, SSL *ssl)
 
 static char *ssl_var_lookup_ssl_cert_PEM(apr_pool_t *p, X509 *xs)
 {
-    char *result;
     BIO *bio;
-    int n;
 
     if ((bio = BIO_new(BIO_s_mem())) == NULL)
         return NULL;
     PEM_write_bio_X509(bio, xs);
-    n = BIO_pending(bio);
-    result = apr_pcalloc(p, n+1);
-    n = BIO_read(bio, result, n);
-    result[n] = NUL;
-    BIO_free(bio);
-    return result;
+
+    return modssl_bio_free_read(p, bio);
 }
 
 static char *ssl_var_lookup_ssl_cert_verify(apr_pool_t *p, 
index 6eca86908cc85e9fd5f766e965d412febaf766ca..74088f5e298e83f58204087f52a818764ca23507 100644 (file)
@@ -185,14 +185,27 @@ BOOL modssl_X509_getBC(X509 *cert, int *ca, int *pathlen)
     return TRUE;
 }
 
+char *modssl_bio_free_read(apr_pool_t *p, BIO *bio)
+{
+    int len = BIO_pending(bio);
+    char *result = NULL;
+
+    if (len > 0) {
+        result = apr_palloc(p, len+1);
+        len = BIO_read(bio, result, len);
+        result[len] = NUL;
+    }
+    BIO_free(bio);
+    return result;
+}
+
 /* Convert ASN.1 string to a pool-allocated char * string, escaping
  * control characters.  If raw is zero, convert to UTF-8, otherwise
  * unchanged from the character set. */
 static char *asn1_string_convert(apr_pool_t *p, ASN1_STRING *asn1str, int raw)
 {
-    char *result = NULL;
     BIO *bio;
-    int len, flags = ASN1_STRFLGS_ESC_CTRL;
+    int flags = ASN1_STRFLGS_ESC_CTRL;
 
     if ((bio = BIO_new(BIO_s_mem())) == NULL)
         return NULL;
@@ -200,14 +213,8 @@ static char *asn1_string_convert(apr_pool_t *p, ASN1_STRING *asn1str, int raw)
     if (!raw) flags |= ASN1_STRFLGS_UTF8_CONVERT;
     
     ASN1_STRING_print_ex(bio, asn1str, flags);
-    len = BIO_pending(bio);
-    if (len > 0) {
-        result = apr_palloc(p, len+1);
-        len = BIO_read(bio, result, len);
-        result[len] = NUL;
-    }
-    BIO_free(bio);
-    return result;
+
+    return modssl_bio_free_read(p, bio);
 }
 
 #define asn1_string_to_utf8(p, a) asn1_string_convert(p, a, 0)
index d6307d971a03ca68e18b05e072591e6d696c7b54..ec89185b1b0845c3e0dbfc4103588f019ceabbef 100644 (file)
@@ -78,6 +78,11 @@ BOOL        modssl_X509_getSAN(apr_pool_t *, X509 *, int, const char *, int, apr
 BOOL        modssl_X509_match_name(apr_pool_t *, X509 *, const char *, BOOL, server_rec *);
 char       *modssl_SSL_SESSION_id2sz(IDCONST unsigned char *, int, char *, int);
 
+/* Reads the remaining data in BIO, if not empty, and copies it into a
+ * pool-allocated string.  If empty, returns NULL.  BIO_free(bio) is
+ * called for both cases. */
+char *modssl_bio_free_read(apr_pool_t *p, BIO *bio);
+    
 #endif /* __SSL_UTIL_SSL_H__ */
 /** @} */