#include <stdio.h>
#include <string.h>
#include "internal/cryptlib.h"
+#include "internal/asn1_int.h"
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <openssl/asn1.h>
*out = stmp.data;
return stmp.length;
}
+
+/* Return 1 if host is a valid hostname and 0 otherwise */
+int asn1_valid_host(const ASN1_STRING *host)
+{
+ int hostlen = host->length;
+ const unsigned char *hostptr = host->data;
+ int type = host->type;
+ int i;
+ char width = -1;
+ unsigned short chflags = 0, prevchflags;
+
+ if (type > 0 && type < 31)
+ width = tag2nbyte[type];
+ if (width == -1 || hostlen == 0)
+ return 0;
+ /* Treat UTF8String as width 1 as any MSB set is invalid */
+ if (width == 0)
+ width = 1;
+ for (i = 0 ; i < hostlen; i+= width) {
+ prevchflags = chflags;
+ /* Value must be <= 0x7F: check upper bytes are all zeroes */
+ if (width == 4) {
+ if (*hostptr++ != 0 || *hostptr++ != 0 || *hostptr++ != 0)
+ return 0;
+ } else if (width == 2) {
+ if (*hostptr++ != 0)
+ return 0;
+ }
+ if (*hostptr > 0x7f)
+ return 0;
+ chflags = char_type[*hostptr++];
+ if (!(chflags & (CHARTYPE_HOST_ANY | CHARTYPE_HOST_WILD))) {
+ /* Nothing else allowed at start or end of string */
+ if (i == 0 || i == hostlen - 1)
+ return 0;
+ /* Otherwise invalid if not dot or hyphen */
+ if (!(chflags & (CHARTYPE_HOST_DOT | CHARTYPE_HOST_HYPHEN)))
+ return 0;
+ /*
+ * If previous is dot or hyphen then illegal unless both
+ * are hyphens: as .- -. .. are all illegal
+ */
+ if (prevchflags & (CHARTYPE_HOST_DOT | CHARTYPE_HOST_HYPHEN)
+ && ((prevchflags & CHARTYPE_HOST_DOT)
+ || (chflags & CHARTYPE_HOST_DOT)))
+ return 0;
+ }
+ }
+ return 1;
+}
* https://www.openssl.org/source/license.html
*/
+#define CHARTYPE_HOST_ANY 4096
+#define CHARTYPE_HOST_DOT 8192
+#define CHARTYPE_HOST_HYPHEN 16384
+#define CHARTYPE_HOST_WILD 32768
+
/*
* Mask of various character properties
*/
static const unsigned short char_type[] = {
- 1026, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
- 120, 0, 1, 40, 0, 0, 0, 16, 1040, 1040, 1024, 25, 25, 16, 16, 16,
- 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 9, 9, 16, 9, 16,
- 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
- 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 0, 1025, 0, 0, 0,
- 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
- 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 0, 0, 0, 0, 2
+ 1026, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 120, 0, 1, 40,
+ 0, 0, 0, 16, 1040, 1040, 33792, 25, 25, 16400, 8208, 16,
+ 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112, 16, 9,
+ 9, 16, 9, 16, 0, 4112, 4112, 4112, 4112, 4112, 4112, 4112,
+ 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112,
+ 4112, 4112, 4112, 4112, 4112, 4112, 4112, 0, 1025, 0, 0, 0,
+ 0, 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112,
+ 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112, 4112,
+ 4112, 4112, 4112, 0, 0, 0, 0, 2
};
my $RFC2253_FIRST_ESC = 0x20; # Escaped with \ if first character
my $RFC2253_LAST_ESC = 0x40; # Escaped with \ if last character
my $RFC2254_ESC = 0x400; # Character escaped \XX
+my $HOST_ANY = 0x1000; # Valid hostname character anywhere in label
+my $HOST_DOT = 0x2000; # Dot: hostname label separator
+my $HOST_HYPHEN = 0x4000; # Hyphen: not valid at start or end.
+my $HOST_WILD = 0x8000; # Wildcard character
for($i = 0; $i < 128; $i++) {
# Set the RFC2253 escape characters (control)
if( ( ( $i >= ord("a")) && ( $i <= ord("z")) )
|| ( ( $i >= ord("A")) && ( $i <= ord("Z")) )
|| ( ( $i >= ord("0")) && ( $i <= ord("9")) ) ) {
- $arr[$i] |= $PSTRING_CHAR;
+ $arr[$i] |= $PSTRING_CHAR | $HOST_ANY;
}
}
$arr[0] |= $RFC2254_ESC;
$arr[ord("(")] |= $RFC2254_ESC;
$arr[ord(")")] |= $RFC2254_ESC;
-$arr[ord("*")] |= $RFC2254_ESC;
+$arr[ord("*")] |= $RFC2254_ESC | $HOST_WILD;
$arr[ord("\\")] |= $RFC2254_ESC;
# Remaining PrintableString characters
$arr[ord(")")] |= $PSTRING_CHAR;
$arr[ord("+")] |= $PSTRING_CHAR;
$arr[ord(",")] |= $PSTRING_CHAR;
-$arr[ord("-")] |= $PSTRING_CHAR;
-$arr[ord(".")] |= $PSTRING_CHAR;
+$arr[ord("-")] |= $PSTRING_CHAR | $HOST_HYPHEN;
+$arr[ord(".")] |= $PSTRING_CHAR | $HOST_DOT;
$arr[ord("/")] |= $PSTRING_CHAR;
$arr[ord(":")] |= $PSTRING_CHAR;
$arr[ord("=")] |= $PSTRING_CHAR;
* https://www.openssl.org/source/license.html
*/
+#define CHARTYPE_HOST_ANY $HOST_ANY
+#define CHARTYPE_HOST_DOT $HOST_DOT
+#define CHARTYPE_HOST_HYPHEN $HOST_HYPHEN
+#define CHARTYPE_HOST_WILD $HOST_WILD
+
/*
* Mask of various character properties
*/
print " ";
for($i = 0; $i < 128; $i++) {
- print("\n ") if($i && (($i % 16) == 0));
- printf(" %2d", $arr[$i]);
+ print("\n ") if($i && (($i % 12) == 0));
+ printf(" %4d", $arr[$i]);
print(",") if ($i != 127);
}
print("\n};\n");