andre@0: /* alg1485.c - implementation of RFCs 1485, 1779 and 2253. andre@0: * andre@0: * This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "prprf.h" andre@0: #include "cert.h" andre@0: #include "certi.h" andre@0: #include "xconst.h" andre@0: #include "genname.h" andre@0: #include "secitem.h" andre@0: #include "secerr.h" andre@0: andre@0: typedef struct NameToKindStr { andre@0: const char * name; andre@0: unsigned int maxLen; /* max bytes in UTF8 encoded string value */ andre@0: SECOidTag kind; andre@0: int valueType; andre@0: } NameToKind; andre@0: andre@0: /* local type for directory string--could be printable_string or utf8 */ andre@0: #define SEC_ASN1_DS SEC_ASN1_HIGH_TAG_NUMBER andre@0: andre@0: /* Add new entries to this table, and maybe to function ParseRFC1485AVA */ andre@0: static const NameToKind name2kinds[] = { andre@0: /* IANA registered type names andre@0: * (See: http://www.iana.org/assignments/ldap-parameters) andre@0: */ andre@0: /* RFC 3280, 4630 MUST SUPPORT */ andre@0: { "CN", 640, SEC_OID_AVA_COMMON_NAME, SEC_ASN1_DS}, andre@0: { "ST", 128, SEC_OID_AVA_STATE_OR_PROVINCE, andre@0: SEC_ASN1_DS}, andre@0: { "O", 128, SEC_OID_AVA_ORGANIZATION_NAME, andre@0: SEC_ASN1_DS}, andre@0: { "OU", 128, SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME, andre@0: SEC_ASN1_DS}, andre@0: { "dnQualifier", 32767, SEC_OID_AVA_DN_QUALIFIER, SEC_ASN1_PRINTABLE_STRING}, andre@0: { "C", 2, SEC_OID_AVA_COUNTRY_NAME, SEC_ASN1_PRINTABLE_STRING}, andre@0: { "serialNumber", 64, SEC_OID_AVA_SERIAL_NUMBER,SEC_ASN1_PRINTABLE_STRING}, andre@0: andre@0: /* RFC 3280, 4630 SHOULD SUPPORT */ andre@0: { "L", 128, SEC_OID_AVA_LOCALITY, SEC_ASN1_DS}, andre@0: { "title", 64, SEC_OID_AVA_TITLE, SEC_ASN1_DS}, andre@0: { "SN", 64, SEC_OID_AVA_SURNAME, SEC_ASN1_DS}, andre@0: { "givenName", 64, SEC_OID_AVA_GIVEN_NAME, SEC_ASN1_DS}, andre@0: { "initials", 64, SEC_OID_AVA_INITIALS, SEC_ASN1_DS}, andre@0: { "generationQualifier", andre@0: 64, SEC_OID_AVA_GENERATION_QUALIFIER, andre@0: SEC_ASN1_DS}, andre@0: /* RFC 3280, 4630 MAY SUPPORT */ andre@0: { "DC", 128, SEC_OID_AVA_DC, SEC_ASN1_IA5_STRING}, andre@0: { "MAIL", 256, SEC_OID_RFC1274_MAIL, SEC_ASN1_IA5_STRING}, andre@0: { "UID", 256, SEC_OID_RFC1274_UID, SEC_ASN1_DS}, andre@0: andre@0: /* ------------------ "strict" boundary --------------------------------- andre@0: * In strict mode, cert_NameToAscii does not encode any of the attributes andre@0: * below this line. The first SECOidTag below this line must be used to andre@0: * conditionally define the "endKind" in function AppendAVA() below. andre@0: * Most new attribute names should be added below this line. andre@0: * Maybe this line should be up higher? Say, after the 3280 MUSTs and andre@0: * before the 3280 SHOULDs? andre@0: */ andre@0: andre@0: /* values from draft-ietf-ldapbis-user-schema-05 (not in RFC 3280) */ andre@0: { "postalAddress", 128, SEC_OID_AVA_POSTAL_ADDRESS, SEC_ASN1_DS}, andre@0: { "postalCode", 40, SEC_OID_AVA_POSTAL_CODE, SEC_ASN1_DS}, andre@0: { "postOfficeBox", 40, SEC_OID_AVA_POST_OFFICE_BOX,SEC_ASN1_DS}, andre@0: { "houseIdentifier",64, SEC_OID_AVA_HOUSE_IDENTIFIER,SEC_ASN1_DS}, andre@0: /* end of IANA registered type names */ andre@0: andre@0: /* legacy keywords */ andre@0: { "E", 128, SEC_OID_PKCS9_EMAIL_ADDRESS,SEC_ASN1_IA5_STRING}, andre@0: { "STREET", 128, SEC_OID_AVA_STREET_ADDRESS, SEC_ASN1_DS}, andre@0: { "pseudonym", 64, SEC_OID_AVA_PSEUDONYM, SEC_ASN1_DS}, andre@0: andre@0: /* values defined by the CAB Forum for EV */ andre@0: { "incorporationLocality", 128, SEC_OID_EV_INCORPORATION_LOCALITY, andre@0: SEC_ASN1_DS}, andre@0: { "incorporationState", 128, SEC_OID_EV_INCORPORATION_STATE, andre@0: SEC_ASN1_DS}, andre@0: { "incorporationCountry", 2, SEC_OID_EV_INCORPORATION_COUNTRY, andre@0: SEC_ASN1_PRINTABLE_STRING}, andre@0: { "businessCategory", 64, SEC_OID_BUSINESS_CATEGORY, SEC_ASN1_DS}, andre@0: andre@0: /* values defined in X.520 */ andre@0: { "name", 64, SEC_OID_AVA_NAME, SEC_ASN1_DS}, andre@0: andre@0: { 0, 256, SEC_OID_UNKNOWN, 0}, andre@0: }; andre@0: andre@0: /* Table facilitates conversion of ASCII hex to binary. */ andre@0: static const PRInt16 x2b[256] = { andre@0: /* #0x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, andre@0: /* #1x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, andre@0: /* #2x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, andre@0: /* #3x */ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, andre@0: /* #4x */ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, andre@0: /* #5x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, andre@0: /* #6x */ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, andre@0: /* #7x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, andre@0: /* #8x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, andre@0: /* #9x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, andre@0: /* #ax */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, andre@0: /* #bx */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, andre@0: /* #cx */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, andre@0: /* #dx */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, andre@0: /* #ex */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, andre@0: /* #fx */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 andre@0: }; andre@0: andre@0: #define IS_HEX(c) (x2b[(PRUint8)(c)] >= 0) andre@0: andre@0: #define C_DOUBLE_QUOTE '\042' andre@0: andre@0: #define C_BACKSLASH '\134' andre@0: andre@0: #define C_EQUAL '=' andre@0: andre@0: #define OPTIONAL_SPACE(c) \ andre@0: (((c) == ' ') || ((c) == '\r') || ((c) == '\n')) andre@0: andre@0: #define SPECIAL_CHAR(c) \ andre@0: (((c) == ',') || ((c) == '=') || ((c) == C_DOUBLE_QUOTE) || \ andre@0: ((c) == '\r') || ((c) == '\n') || ((c) == '+') || \ andre@0: ((c) == '<') || ((c) == '>') || ((c) == '#') || \ andre@0: ((c) == ';') || ((c) == C_BACKSLASH)) andre@0: andre@0: andre@0: #define IS_PRINTABLE(c) \ andre@0: ((((c) >= 'a') && ((c) <= 'z')) || \ andre@0: (((c) >= 'A') && ((c) <= 'Z')) || \ andre@0: (((c) >= '0') && ((c) <= '9')) || \ andre@0: ((c) == ' ') || \ andre@0: ((c) == '\'') || \ andre@0: ((c) == '\050') || /* ( */ \ andre@0: ((c) == '\051') || /* ) */ \ andre@0: (((c) >= '+') && ((c) <= '/')) || /* + , - . / */ \ andre@0: ((c) == ':') || \ andre@0: ((c) == '=') || \ andre@0: ((c) == '?')) andre@0: andre@0: /* RFC 2253 says we must escape ",+\"\\<>;=" EXCEPT inside a quoted string. andre@0: * Inside a quoted string, we only need to escape " and \ andre@0: * We choose to quote strings containing any of those special characters, andre@0: * so we only need to escape " and \ andre@0: */ andre@0: #define NEEDS_ESCAPE(c) \ andre@0: (c == C_DOUBLE_QUOTE || c == C_BACKSLASH) andre@0: andre@0: #define NEEDS_HEX_ESCAPE(c) \ andre@0: ((PRUint8)c < 0x20 || c == 0x7f) andre@0: andre@0: int andre@0: cert_AVAOidTagToMaxLen(SECOidTag tag) andre@0: { andre@0: const NameToKind *n2k = name2kinds; andre@0: andre@0: while (n2k->kind != tag && n2k->kind != SEC_OID_UNKNOWN) { andre@0: ++n2k; andre@0: } andre@0: return (n2k->kind != SEC_OID_UNKNOWN) ? n2k->maxLen : -1; andre@0: } andre@0: andre@0: static PRBool andre@0: IsPrintable(unsigned char *data, unsigned len) andre@0: { andre@0: unsigned char ch, *end; andre@0: andre@0: end = data + len; andre@0: while (data < end) { andre@0: ch = *data++; andre@0: if (!IS_PRINTABLE(ch)) { andre@0: return PR_FALSE; andre@0: } andre@0: } andre@0: return PR_TRUE; andre@0: } andre@0: andre@0: static void andre@0: skipSpace(const char **pbp, const char *endptr) andre@0: { andre@0: const char *bp = *pbp; andre@0: while (bp < endptr && OPTIONAL_SPACE(*bp)) { andre@0: bp++; andre@0: } andre@0: *pbp = bp; andre@0: } andre@0: andre@0: static SECStatus andre@0: scanTag(const char **pbp, const char *endptr, char *tagBuf, int tagBufSize) andre@0: { andre@0: const char *bp; andre@0: char *tagBufp; andre@0: int taglen; andre@0: andre@0: PORT_Assert(tagBufSize > 0); andre@0: andre@0: /* skip optional leading space */ andre@0: skipSpace(pbp, endptr); andre@0: if (*pbp == endptr) { andre@0: /* nothing left */ andre@0: return SECFailure; andre@0: } andre@0: andre@0: /* fill tagBuf */ andre@0: taglen = 0; andre@0: bp = *pbp; andre@0: tagBufp = tagBuf; andre@0: while (bp < endptr && !OPTIONAL_SPACE(*bp) && (*bp != C_EQUAL)) { andre@0: if (++taglen >= tagBufSize) { andre@0: *pbp = bp; andre@0: return SECFailure; andre@0: } andre@0: *tagBufp++ = *bp++; andre@0: } andre@0: /* null-terminate tagBuf -- guaranteed at least one space left */ andre@0: *tagBufp++ = 0; andre@0: *pbp = bp; andre@0: andre@0: /* skip trailing spaces till we hit something - should be an equal sign */ andre@0: skipSpace(pbp, endptr); andre@0: if (*pbp == endptr) { andre@0: /* nothing left */ andre@0: return SECFailure; andre@0: } andre@0: if (**pbp != C_EQUAL) { andre@0: /* should be an equal sign */ andre@0: return SECFailure; andre@0: } andre@0: /* skip over the equal sign */ andre@0: (*pbp)++; andre@0: andre@0: return SECSuccess; andre@0: } andre@0: andre@0: /* Returns the number of bytes in the value. 0 means failure. */ andre@0: static int andre@0: scanVal(const char **pbp, const char *endptr, char *valBuf, int valBufSize) andre@0: { andre@0: const char *bp; andre@0: char *valBufp; andre@0: int vallen = 0; andre@0: PRBool isQuoted; andre@0: andre@0: PORT_Assert(valBufSize > 0); andre@0: andre@0: /* skip optional leading space */ andre@0: skipSpace(pbp, endptr); andre@0: if(*pbp == endptr) { andre@0: /* nothing left */ andre@0: return 0; andre@0: } andre@0: andre@0: bp = *pbp; andre@0: andre@0: /* quoted? */ andre@0: if (*bp == C_DOUBLE_QUOTE) { andre@0: isQuoted = PR_TRUE; andre@0: /* skip over it */ andre@0: bp++; andre@0: } else { andre@0: isQuoted = PR_FALSE; andre@0: } andre@0: andre@0: valBufp = valBuf; andre@0: while (bp < endptr) { andre@0: char c = *bp; andre@0: if (c == C_BACKSLASH) { andre@0: /* escape character */ andre@0: bp++; andre@0: if (bp >= endptr) { andre@0: /* escape charater must appear with paired char */ andre@0: *pbp = bp; andre@0: return 0; andre@0: } andre@0: c = *bp; andre@0: if (IS_HEX(c) && (endptr - bp) >= 2 && IS_HEX(bp[1])) { andre@0: bp++; andre@0: c = (char)((x2b[(PRUint8)c] << 4) | x2b[(PRUint8)*bp]); andre@0: } andre@0: } else if (c == '#' && bp == *pbp) { andre@0: /* ignore leading #, quotation not required for it. */ andre@0: } else if (!isQuoted && SPECIAL_CHAR(c)) { andre@0: /* unescaped special and not within quoted value */ andre@0: break; andre@0: } else if (c == C_DOUBLE_QUOTE) { andre@0: /* reached unescaped double quote */ andre@0: break; andre@0: } andre@0: /* append character */ andre@0: vallen++; andre@0: if (vallen >= valBufSize) { andre@0: *pbp = bp; andre@0: return 0; andre@0: } andre@0: *valBufp++ = c; andre@0: bp++; andre@0: } andre@0: andre@0: /* strip trailing spaces from unquoted values */ andre@0: if (!isQuoted) { andre@0: while (valBufp > valBuf) { andre@0: char c = valBufp[-1]; andre@0: if (! OPTIONAL_SPACE(c)) andre@0: break; andre@0: --valBufp; andre@0: } andre@0: vallen = valBufp - valBuf; andre@0: } andre@0: andre@0: if (isQuoted) { andre@0: /* insist that we stopped on a double quote */ andre@0: if (*bp != C_DOUBLE_QUOTE) { andre@0: *pbp = bp; andre@0: return 0; andre@0: } andre@0: /* skip over the quote and skip optional space */ andre@0: bp++; andre@0: skipSpace(&bp, endptr); andre@0: } andre@0: andre@0: *pbp = bp; andre@0: andre@0: /* null-terminate valBuf -- guaranteed at least one space left */ andre@0: *valBufp = 0; andre@0: andre@0: return vallen; andre@0: } andre@0: andre@0: /* Caller must set error code upon failure */ andre@0: static SECStatus andre@0: hexToBin(PLArenaPool *pool, SECItem * destItem, const char * src, int len) andre@0: { andre@0: PRUint8 * dest; andre@0: andre@0: destItem->data = NULL; andre@0: if (len <= 0 || (len & 1)) { andre@0: goto loser; andre@0: } andre@0: len >>= 1; andre@0: if (!SECITEM_AllocItem(pool, destItem, len)) andre@0: goto loser; andre@0: dest = destItem->data; andre@0: for (; len > 0; len--, src += 2) { andre@0: PRInt16 bin = (x2b[(PRUint8)src[0]] << 4) | x2b[(PRUint8)src[1]]; andre@0: if (bin < 0) andre@0: goto loser; andre@0: *dest++ = (PRUint8)bin; andre@0: } andre@0: return SECSuccess; andre@0: loser: andre@0: if (!pool) andre@0: SECITEM_FreeItem(destItem, PR_FALSE); andre@0: return SECFailure; andre@0: } andre@0: andre@0: /* Parses one AVA, starting at *pbp. Stops at endptr. andre@0: * Advances *pbp past parsed AVA and trailing separator (if present). andre@0: * On any error, returns NULL and *pbp is undefined. andre@0: * On success, returns CERTAVA allocated from arena, and (*pbp)[-1] was andre@0: * the last character parsed. *pbp is either equal to endptr or andre@0: * points to first character after separator. andre@0: */ andre@0: static CERTAVA * andre@0: ParseRFC1485AVA(PLArenaPool *arena, const char **pbp, const char *endptr) andre@0: { andre@0: CERTAVA *a; andre@0: const NameToKind *n2k; andre@0: const char *bp; andre@0: int vt = -1; andre@0: int valLen; andre@0: SECOidTag kind = SEC_OID_UNKNOWN; andre@0: SECStatus rv = SECFailure; andre@0: SECItem derOid = { 0, NULL, 0 }; andre@0: SECItem derVal = { 0, NULL, 0}; andre@0: char sep = 0; andre@0: andre@0: char tagBuf[32]; andre@0: char valBuf[1024]; andre@0: andre@0: PORT_Assert(arena); andre@0: if (SECSuccess != scanTag(pbp, endptr, tagBuf, sizeof tagBuf) || andre@0: !(valLen = scanVal(pbp, endptr, valBuf, sizeof valBuf))) { andre@0: goto loser; andre@0: } andre@0: andre@0: bp = *pbp; andre@0: if (bp < endptr) { andre@0: sep = *bp++; /* skip over separator */ andre@0: } andre@0: *pbp = bp; andre@0: /* if we haven't finished, insist that we've stopped on a separator */ andre@0: if (sep && sep != ',' && sep != ';' && sep != '+') { andre@0: goto loser; andre@0: } andre@0: andre@0: /* is this a dotted decimal OID attribute type ? */ andre@0: if (!PL_strncasecmp("oid.", tagBuf, 4)) { andre@0: rv = SEC_StringToOID(arena, &derOid, tagBuf, strlen(tagBuf)); andre@0: } else { andre@0: for (n2k = name2kinds; n2k->name; n2k++) { andre@0: SECOidData *oidrec; andre@0: if (PORT_Strcasecmp(n2k->name, tagBuf) == 0) { andre@0: kind = n2k->kind; andre@0: vt = n2k->valueType; andre@0: oidrec = SECOID_FindOIDByTag(kind); andre@0: if (oidrec == NULL) andre@0: goto loser; andre@0: derOid = oidrec->oid; andre@0: break; andre@0: } andre@0: } andre@0: } andre@0: if (kind == SEC_OID_UNKNOWN && rv != SECSuccess) andre@0: goto loser; andre@0: andre@0: /* Is this a hex encoding of a DER attribute value ? */ andre@0: if ('#' == valBuf[0]) { andre@0: /* convert attribute value from hex to binary */ andre@0: rv = hexToBin(arena, &derVal, valBuf + 1, valLen - 1); andre@0: if (rv) andre@0: goto loser; andre@0: a = CERT_CreateAVAFromRaw(arena, &derOid, &derVal); andre@0: } else { andre@0: if (kind == SEC_OID_UNKNOWN) andre@0: goto loser; andre@0: if (kind == SEC_OID_AVA_COUNTRY_NAME && valLen != 2) andre@0: goto loser; andre@0: if (vt == SEC_ASN1_PRINTABLE_STRING && andre@0: !IsPrintable((unsigned char*) valBuf, valLen)) andre@0: goto loser; andre@0: if (vt == SEC_ASN1_DS) { andre@0: /* RFC 4630: choose PrintableString or UTF8String */ andre@0: if (IsPrintable((unsigned char*) valBuf, valLen)) andre@0: vt = SEC_ASN1_PRINTABLE_STRING; andre@0: else andre@0: vt = SEC_ASN1_UTF8_STRING; andre@0: } andre@0: andre@0: derVal.data = (unsigned char*) valBuf; andre@0: derVal.len = valLen; andre@0: a = CERT_CreateAVAFromSECItem(arena, kind, vt, &derVal); andre@0: } andre@0: return a; andre@0: andre@0: loser: andre@0: /* matched no kind -- invalid tag */ andre@0: PORT_SetError(SEC_ERROR_INVALID_AVA); andre@0: return 0; andre@0: } andre@0: andre@0: static CERTName * andre@0: ParseRFC1485Name(const char *buf, int len) andre@0: { andre@0: SECStatus rv; andre@0: CERTName *name; andre@0: const char *bp, *e; andre@0: CERTAVA *ava; andre@0: CERTRDN *rdn = NULL; andre@0: andre@0: name = CERT_CreateName(NULL); andre@0: if (name == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: e = buf + len; andre@0: bp = buf; andre@0: while (bp < e) { andre@0: ava = ParseRFC1485AVA(name->arena, &bp, e); andre@0: if (ava == 0) andre@0: goto loser; andre@0: if (!rdn) { andre@0: rdn = CERT_CreateRDN(name->arena, ava, (CERTAVA *)0); andre@0: if (rdn == 0) andre@0: goto loser; andre@0: rv = CERT_AddRDN(name, rdn); andre@0: } else { andre@0: rv = CERT_AddAVA(name->arena, rdn, ava); andre@0: } andre@0: if (rv) andre@0: goto loser; andre@0: if (bp[-1] != '+') andre@0: rdn = NULL; /* done with this RDN */ andre@0: skipSpace(&bp, e); andre@0: } andre@0: andre@0: if (name->rdns[0] == 0) { andre@0: /* empty name -- illegal */ andre@0: goto loser; andre@0: } andre@0: andre@0: /* Reverse order of RDNS to comply with RFC */ andre@0: { andre@0: CERTRDN **firstRdn; andre@0: CERTRDN **lastRdn; andre@0: CERTRDN *tmp; andre@0: andre@0: /* get first one */ andre@0: firstRdn = name->rdns; andre@0: andre@0: /* find last one */ andre@0: lastRdn = name->rdns; andre@0: while (*lastRdn) lastRdn++; andre@0: lastRdn--; andre@0: andre@0: /* reverse list */ andre@0: for ( ; firstRdn < lastRdn; firstRdn++, lastRdn--) { andre@0: tmp = *firstRdn; andre@0: *firstRdn = *lastRdn; andre@0: *lastRdn = tmp; andre@0: } andre@0: } andre@0: andre@0: /* return result */ andre@0: return name; andre@0: andre@0: loser: andre@0: CERT_DestroyName(name); andre@0: return NULL; andre@0: } andre@0: andre@0: CERTName * andre@0: CERT_AsciiToName(const char *string) andre@0: { andre@0: CERTName *name; andre@0: name = ParseRFC1485Name(string, PORT_Strlen(string)); andre@0: return name; andre@0: } andre@0: andre@0: /************************************************************************/ andre@0: andre@0: typedef struct stringBufStr { andre@0: char *buffer; andre@0: unsigned offset; andre@0: unsigned size; andre@0: } stringBuf; andre@0: andre@0: #define DEFAULT_BUFFER_SIZE 200 andre@0: andre@0: static SECStatus andre@0: AppendStr(stringBuf *bufp, char *str) andre@0: { andre@0: char *buf; andre@0: unsigned bufLen, bufSize, len; andre@0: int size = 0; andre@0: andre@0: /* Figure out how much to grow buf by (add in the '\0') */ andre@0: buf = bufp->buffer; andre@0: bufLen = bufp->offset; andre@0: len = PORT_Strlen(str); andre@0: bufSize = bufLen + len; andre@0: if (!buf) { andre@0: bufSize++; andre@0: size = PR_MAX(DEFAULT_BUFFER_SIZE,bufSize*2); andre@0: buf = (char *) PORT_Alloc(size); andre@0: bufp->size = size; andre@0: } else if (bufp->size < bufSize) { andre@0: size = bufSize*2; andre@0: buf =(char *) PORT_Realloc(buf,size); andre@0: bufp->size = size; andre@0: } andre@0: if (!buf) { andre@0: PORT_SetError(SEC_ERROR_NO_MEMORY); andre@0: return SECFailure; andre@0: } andre@0: bufp->buffer = buf; andre@0: bufp->offset = bufSize; andre@0: andre@0: /* Concatenate str onto buf */ andre@0: buf = buf + bufLen; andre@0: if (bufLen) buf--; /* stomp on old '\0' */ andre@0: PORT_Memcpy(buf, str, len+1); /* put in new null */ andre@0: return SECSuccess; andre@0: } andre@0: andre@0: typedef enum { andre@0: minimalEscape = 0, /* only hex escapes, and " and \ */ andre@0: minimalEscapeAndQuote, /* as above, plus quoting */ andre@0: fullEscape /* no quoting, full escaping */ andre@0: } EQMode; andre@0: andre@0: /* Some characters must be escaped as a hex string, e.g. c -> \nn . andre@0: * Others must be escaped by preceding with a '\', e.g. c -> \c , but andre@0: * there are certain "special characters" that may be handled by either andre@0: * escaping them, or by enclosing the entire attribute value in quotes. andre@0: * A NULL value for pEQMode implies selecting minimalEscape mode. andre@0: * Some callers will do quoting when needed, others will not. andre@0: * If a caller selects minimalEscapeAndQuote, and the string does not andre@0: * need quoting, then this function changes it to minimalEscape. andre@0: */ andre@0: static int andre@0: cert_RFC1485_GetRequiredLen(const char *src, int srclen, EQMode *pEQMode) andre@0: { andre@0: int i, reqLen=0; andre@0: EQMode mode = pEQMode ? *pEQMode : minimalEscape; andre@0: PRBool needsQuoting = PR_FALSE; andre@0: char lastC = 0; andre@0: andre@0: /* need to make an initial pass to determine if quoting is needed */ andre@0: for (i = 0; i < srclen; i++) { andre@0: char c = src[i]; andre@0: reqLen++; andre@0: if (NEEDS_HEX_ESCAPE(c)) { /* c -> \xx */ andre@0: reqLen += 2; andre@0: } else if (NEEDS_ESCAPE(c)) { /* c -> \c */ andre@0: reqLen++; andre@0: } else if (SPECIAL_CHAR(c)) { andre@0: if (mode == minimalEscapeAndQuote) /* quoting is allowed */ andre@0: needsQuoting = PR_TRUE; /* entirety will need quoting */ andre@0: else if (mode == fullEscape) andre@0: reqLen++; /* MAY escape this character */ andre@0: } else if (OPTIONAL_SPACE(c) && OPTIONAL_SPACE(lastC)) { andre@0: if (mode == minimalEscapeAndQuote) /* quoting is allowed */ andre@0: needsQuoting = PR_TRUE; /* entirety will need quoting */ andre@0: } andre@0: lastC = c; andre@0: } andre@0: /* if it begins or ends in optional space it needs quoting */ andre@0: if (!needsQuoting && srclen > 0 && mode == minimalEscapeAndQuote && andre@0: (OPTIONAL_SPACE(src[srclen-1]) || OPTIONAL_SPACE(src[0]))) { andre@0: needsQuoting = PR_TRUE; andre@0: } andre@0: andre@0: if (needsQuoting) andre@0: reqLen += 2; andre@0: if (pEQMode && mode == minimalEscapeAndQuote && !needsQuoting) andre@0: *pEQMode = minimalEscape; andre@0: return reqLen; andre@0: } andre@0: andre@0: static const char hexChars[16] = { "0123456789abcdef" }; andre@0: andre@0: static SECStatus andre@0: escapeAndQuote(char *dst, int dstlen, char *src, int srclen, EQMode *pEQMode) andre@0: { andre@0: int i, reqLen=0; andre@0: EQMode mode = pEQMode ? *pEQMode : minimalEscape; andre@0: andre@0: /* space for terminal null */ andre@0: reqLen = cert_RFC1485_GetRequiredLen(src, srclen, &mode) + 1; andre@0: if (reqLen > dstlen) { andre@0: PORT_SetError(SEC_ERROR_OUTPUT_LEN); andre@0: return SECFailure; andre@0: } andre@0: andre@0: if (mode == minimalEscapeAndQuote) andre@0: *dst++ = C_DOUBLE_QUOTE; andre@0: for (i = 0; i < srclen; i++) { andre@0: char c = src[i]; andre@0: if (NEEDS_HEX_ESCAPE(c)) { andre@0: *dst++ = C_BACKSLASH; andre@0: *dst++ = hexChars[ (c >> 4) & 0x0f ]; andre@0: *dst++ = hexChars[ c & 0x0f ]; andre@0: } else { andre@0: if (NEEDS_ESCAPE(c) || (SPECIAL_CHAR(c) && mode == fullEscape)) { andre@0: *dst++ = C_BACKSLASH; andre@0: } andre@0: *dst++ = c; andre@0: } andre@0: } andre@0: if (mode == minimalEscapeAndQuote) andre@0: *dst++ = C_DOUBLE_QUOTE; andre@0: *dst++ = 0; andre@0: if (pEQMode) andre@0: *pEQMode = mode; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: SECStatus andre@0: CERT_RFC1485_EscapeAndQuote(char *dst, int dstlen, char *src, int srclen) andre@0: { andre@0: EQMode mode = minimalEscapeAndQuote; andre@0: return escapeAndQuote(dst, dstlen, src, srclen, &mode); andre@0: } andre@0: andre@0: andre@0: /* convert an OID to dotted-decimal representation */ andre@0: /* Returns a string that must be freed with PR_smprintf_free(), */ andre@0: char * andre@0: CERT_GetOidString(const SECItem *oid) andre@0: { andre@0: PRUint8 *stop; /* points to first byte after OID string */ andre@0: PRUint8 *first; /* byte of an OID component integer */ andre@0: PRUint8 *last; /* byte of an OID component integer */ andre@0: char *rvString = NULL; andre@0: char *prefix = NULL; andre@0: andre@0: #define MAX_OID_LEN 1024 /* bytes */ andre@0: andre@0: if (oid->len > MAX_OID_LEN) { andre@0: PORT_SetError(SEC_ERROR_INPUT_LEN); andre@0: return NULL; andre@0: } andre@0: andre@0: /* first will point to the next sequence of bytes to decode */ andre@0: first = (PRUint8 *)oid->data; andre@0: /* stop points to one past the legitimate data */ andre@0: stop = &first[ oid->len ]; andre@0: andre@0: /* andre@0: * Check for our pseudo-encoded single-digit OIDs andre@0: */ andre@0: if ((*first == 0x80) && (2 == oid->len)) { andre@0: /* Funky encoding. The second byte is the number */ andre@0: rvString = PR_smprintf("%lu", (PRUint32)first[1]); andre@0: if (!rvString) { andre@0: PORT_SetError(SEC_ERROR_NO_MEMORY); andre@0: } andre@0: return rvString; andre@0: } andre@0: andre@0: for (; first < stop; first = last + 1) { andre@0: unsigned int bytesBeforeLast; andre@0: andre@0: for (last = first; last < stop; last++) { andre@0: if (0 == (*last & 0x80)) { andre@0: break; andre@0: } andre@0: } andre@0: bytesBeforeLast = (unsigned int)(last - first); andre@0: if (bytesBeforeLast <= 3U) { /* 0-28 bit number */ andre@0: PRUint32 n = 0; andre@0: PRUint32 c; andre@0: andre@0: #define CGET(i, m) \ andre@0: c = last[-i] & m; \ andre@0: n |= c << (7 * i) andre@0: andre@0: #define CASE(i, m) \ andre@0: case i: \ andre@0: CGET(i, m); \ andre@0: if (!n) goto unsupported \ andre@0: /* fall-through */ andre@0: andre@0: switch (bytesBeforeLast) { andre@0: CASE(3, 0x7f); andre@0: CASE(2, 0x7f); andre@0: CASE(1, 0x7f); andre@0: case 0: n |= last[0] & 0x7f; andre@0: break; andre@0: } andre@0: if (last[0] & 0x80) andre@0: goto unsupported; andre@0: andre@0: if (!rvString) { andre@0: /* This is the first number.. decompose it */ andre@0: PRUint32 one = PR_MIN(n/40, 2); /* never > 2 */ andre@0: PRUint32 two = n - (one * 40); andre@0: andre@0: rvString = PR_smprintf("OID.%lu.%lu", one, two); andre@0: } else { andre@0: prefix = rvString; andre@0: rvString = PR_smprintf("%s.%lu", prefix, n); andre@0: } andre@0: } else if (bytesBeforeLast <= 9U) { /* 29-64 bit number */ andre@0: PRUint64 n = 0; andre@0: PRUint64 c; andre@0: andre@0: switch (bytesBeforeLast) { andre@0: CASE(9, 0x01); andre@0: CASE(8, 0x7f); andre@0: CASE(7, 0x7f); andre@0: CASE(6, 0x7f); andre@0: CASE(5, 0x7f); andre@0: CASE(4, 0x7f); andre@0: CGET(3, 0x7f); andre@0: CGET(2, 0x7f); andre@0: CGET(1, 0x7f); andre@0: CGET(0, 0x7f); andre@0: break; andre@0: } andre@0: if (last[0] & 0x80) andre@0: goto unsupported; andre@0: andre@0: if (!rvString) { andre@0: /* This is the first number.. decompose it */ andre@0: PRUint64 one = PR_MIN(n/40, 2); /* never > 2 */ andre@0: PRUint64 two = n - (one * 40); andre@0: andre@0: rvString = PR_smprintf("OID.%llu.%llu", one, two); andre@0: } else { andre@0: prefix = rvString; andre@0: rvString = PR_smprintf("%s.%llu", prefix, n); andre@0: } andre@0: } else { andre@0: /* More than a 64-bit number, or not minimal encoding. */ andre@0: unsupported: andre@0: if (!rvString) andre@0: rvString = PR_smprintf("OID.UNSUPPORTED"); andre@0: else { andre@0: prefix = rvString; andre@0: rvString = PR_smprintf("%s.UNSUPPORTED", prefix); andre@0: } andre@0: } andre@0: andre@0: if (prefix) { andre@0: PR_smprintf_free(prefix); andre@0: prefix = NULL; andre@0: } andre@0: if (!rvString) { andre@0: PORT_SetError(SEC_ERROR_NO_MEMORY); andre@0: break; andre@0: } andre@0: } andre@0: return rvString; andre@0: } andre@0: andre@0: /* convert DER-encoded hex to a string */ andre@0: static SECItem * andre@0: get_hex_string(SECItem *data) andre@0: { andre@0: SECItem *rv; andre@0: unsigned int i, j; andre@0: static const char hex[] = { "0123456789ABCDEF" }; andre@0: andre@0: /* '#' + 2 chars per octet + terminator */ andre@0: rv = SECITEM_AllocItem(NULL, NULL, data->len*2 + 2); andre@0: if (!rv) { andre@0: return NULL; andre@0: } andre@0: rv->data[0] = '#'; andre@0: rv->len = 1 + 2 * data->len; andre@0: for (i=0; ilen; i++) { andre@0: j = data->data[i]; andre@0: rv->data[2*i+1] = hex[j >> 4]; andre@0: rv->data[2*i+2] = hex[j & 15]; andre@0: } andre@0: rv->data[rv->len] = 0; andre@0: return rv; andre@0: } andre@0: andre@0: /* For compliance with RFC 2253, RFC 3280 and RFC 4630, we choose to andre@0: * use the NAME=STRING form, rather than the OID.N.N=#hexXXXX form, andre@0: * when both of these conditions are met: andre@0: * 1) The attribute name OID (kind) has a known name string that is andre@0: * defined in one of those RFCs, or in RFCs that they cite, AND andre@0: * 2) The attribute's value encoding is RFC compliant for the kind andre@0: * (e.g., the value's encoding tag is correct for the kind, and andre@0: * the value's length is in the range allowed for the kind, and andre@0: * the value's contents are appropriate for the encoding tag). andre@0: * Otherwise, we use the OID.N.N=#hexXXXX form. andre@0: * andre@0: * If the caller prefers maximum human readability to RFC compliance, andre@0: * then andre@0: * - We print the kind in NAME= string form if we know the name andre@0: * string for the attribute type OID, regardless of whether the andre@0: * value is correctly encoded or not. else we use the OID.N.N= form. andre@0: * - We use the non-hex STRING form for the attribute value if the andre@0: * value can be represented in such a form. Otherwise, we use andre@0: * the hex string form. andre@0: * This implies that, for maximum human readability, in addition to andre@0: * the two forms allowed by the RFC, we allow two other forms of output: andre@0: * - the OID.N.N=STRING form, and andre@0: * - the NAME=#hexXXXX form andre@0: * When the caller prefers maximum human readability, we do not allow andre@0: * the value of any attribute to exceed the length allowed by the RFC. andre@0: * If the attribute value exceeds the allowed length, we truncate it to andre@0: * the allowed length and append "...". andre@0: * Also in this case, we arbitrarily impose a limit on the length of the andre@0: * entire AVA encoding, regardless of the form, of 384 bytes per AVA. andre@0: * This limit includes the trailing NULL character. If the encoded andre@0: * AVA length exceeds that limit, this function reports failure to encode andre@0: * the AVA. andre@0: * andre@0: * An ASCII representation of an AVA is said to be "invertible" if andre@0: * conversion back to DER reproduces the original DER encoding exactly. andre@0: * The RFC 2253 rules do not ensure that all ASCII AVAs derived according andre@0: * to its rules are invertible. That is because the RFCs allow some andre@0: * attribute values to be encoded in any of a number of encodings, andre@0: * and the encoding type information is lost in the non-hex STRING form. andre@0: * This is particularly true of attributes of type DirectoryString. andre@0: * The encoding type information is always preserved in the hex string andre@0: * form, because the hex includes the entire DER encoding of the value. andre@0: * andre@0: * So, when the caller perfers maximum invertibility, we apply the andre@0: * RFC compliance rules stated above, and add a third required andre@0: * condition on the use of the NAME=STRING form. andre@0: * 3) The attribute's kind is not is allowed to be encoded in any of andre@0: * several different encodings, such as DirectoryStrings. andre@0: * andre@0: * The chief difference between CERT_N2A_STRICT and CERT_N2A_INVERTIBLE andre@0: * is that the latter forces DirectoryStrings to be hex encoded. andre@0: * andre@0: * As a simplification, we assume the value is correctly encoded for andre@0: * its encoding type. That is, we do not test that all the characters andre@0: * in a string encoded type are allowed by that type. We assume it. andre@0: */ andre@0: static SECStatus andre@0: AppendAVA(stringBuf *bufp, CERTAVA *ava, CertStrictnessLevel strict) andre@0: { andre@0: #define TMPBUF_LEN 2048 andre@0: const NameToKind *pn2k = name2kinds; andre@0: SECItem *avaValue = NULL; andre@0: char *unknownTag = NULL; andre@0: char *encodedAVA = NULL; andre@0: PRBool useHex = PR_FALSE; /* use =#hexXXXX form */ andre@0: PRBool truncateName = PR_FALSE; andre@0: PRBool truncateValue = PR_FALSE; andre@0: SECOidTag endKind; andre@0: SECStatus rv; andre@0: unsigned int len; andre@0: unsigned int nameLen, valueLen; andre@0: unsigned int maxName, maxValue; andre@0: EQMode mode = minimalEscapeAndQuote; andre@0: NameToKind n2k = { NULL, 32767, SEC_OID_UNKNOWN, SEC_ASN1_DS }; andre@0: char tmpBuf[TMPBUF_LEN]; andre@0: andre@0: #define tagName n2k.name /* non-NULL means use NAME= form */ andre@0: #define maxBytes n2k.maxLen andre@0: #define tag n2k.kind andre@0: #define vt n2k.valueType andre@0: andre@0: /* READABLE mode recognizes more names from the name2kinds table andre@0: * than do STRICT or INVERTIBLE modes. This assignment chooses the andre@0: * point in the table where the attribute type name scanning stops. andre@0: */ andre@0: endKind = (strict == CERT_N2A_READABLE) ? SEC_OID_UNKNOWN andre@0: : SEC_OID_AVA_POSTAL_ADDRESS; andre@0: tag = CERT_GetAVATag(ava); andre@0: while (pn2k->kind != tag && pn2k->kind != endKind) { andre@0: ++pn2k; andre@0: } andre@0: andre@0: if (pn2k->kind != endKind ) { andre@0: n2k = *pn2k; andre@0: } else if (strict != CERT_N2A_READABLE) { andre@0: useHex = PR_TRUE; andre@0: } andre@0: /* For invertable form, force Directory Strings to use hex form. */ andre@0: if (strict == CERT_N2A_INVERTIBLE && vt == SEC_ASN1_DS) { andre@0: tagName = NULL; /* must use OID.N form */ andre@0: useHex = PR_TRUE; /* must use hex string */ andre@0: } andre@0: if (!useHex) { andre@0: avaValue = CERT_DecodeAVAValue(&ava->value); andre@0: if (!avaValue) { andre@0: useHex = PR_TRUE; andre@0: if (strict != CERT_N2A_READABLE) { andre@0: tagName = NULL; /* must use OID.N form */ andre@0: } andre@0: } andre@0: } andre@0: if (!tagName) { andre@0: /* handle unknown attribute types per RFC 2253 */ andre@0: tagName = unknownTag = CERT_GetOidString(&ava->type); andre@0: if (!tagName) { andre@0: if (avaValue) andre@0: SECITEM_FreeItem(avaValue, PR_TRUE); andre@0: return SECFailure; andre@0: } andre@0: } andre@0: if (useHex) { andre@0: avaValue = get_hex_string(&ava->value); andre@0: if (!avaValue) { andre@0: if (unknownTag) andre@0: PR_smprintf_free(unknownTag); andre@0: return SECFailure; andre@0: } andre@0: } andre@0: andre@0: nameLen = strlen(tagName); andre@0: valueLen = (useHex ? avaValue->len : andre@0: cert_RFC1485_GetRequiredLen((char *)avaValue->data, avaValue->len, andre@0: &mode)); andre@0: len = nameLen + valueLen + 2; /* Add 2 for '=' and trailing NUL */ andre@0: andre@0: maxName = nameLen; andre@0: maxValue = valueLen; andre@0: if (len <= sizeof(tmpBuf)) { andre@0: encodedAVA = tmpBuf; andre@0: } else if (strict != CERT_N2A_READABLE) { andre@0: encodedAVA = PORT_Alloc(len); andre@0: if (!encodedAVA) { andre@0: SECITEM_FreeItem(avaValue, PR_TRUE); andre@0: if (unknownTag) andre@0: PR_smprintf_free(unknownTag); andre@0: return SECFailure; andre@0: } andre@0: } else { andre@0: /* Must make output fit in tmpbuf */ andre@0: unsigned int fair = (sizeof tmpBuf)/2 - 1; /* for = and \0 */ andre@0: andre@0: if (nameLen < fair) { andre@0: /* just truncate the value */ andre@0: maxValue = (sizeof tmpBuf) - (nameLen + 6); /* for "=...\0", andre@0: and possibly '"' */ andre@0: } else if (valueLen < fair) { andre@0: /* just truncate the name */ andre@0: maxName = (sizeof tmpBuf) - (valueLen + 5); /* for "=...\0" */ andre@0: } else { andre@0: /* truncate both */ andre@0: maxName = maxValue = fair - 3; /* for "..." */ andre@0: } andre@0: if (nameLen > maxName) { andre@0: PORT_Assert(unknownTag && unknownTag == tagName); andre@0: truncateName = PR_TRUE; andre@0: nameLen = maxName; andre@0: } andre@0: encodedAVA = tmpBuf; andre@0: } andre@0: andre@0: memcpy(encodedAVA, tagName, nameLen); andre@0: if (truncateName) { andre@0: /* If tag name is too long, we know it is an OID form that was andre@0: * allocated from the heap, so we can modify it in place andre@0: */ andre@0: encodedAVA[nameLen-1] = '.'; andre@0: encodedAVA[nameLen-2] = '.'; andre@0: encodedAVA[nameLen-3] = '.'; andre@0: } andre@0: encodedAVA[nameLen++] = '='; andre@0: if (unknownTag) andre@0: PR_smprintf_free(unknownTag); andre@0: andre@0: if (strict == CERT_N2A_READABLE && maxValue > maxBytes) andre@0: maxValue = maxBytes; andre@0: if (valueLen > maxValue) { andre@0: valueLen = maxValue; andre@0: truncateValue = PR_TRUE; andre@0: } andre@0: /* escape and quote as necessary - don't quote hex strings */ andre@0: if (useHex) { andre@0: char * end = encodedAVA + nameLen + valueLen; andre@0: memcpy(encodedAVA + nameLen, (char *)avaValue->data, valueLen); andre@0: end[0] = '\0'; andre@0: if (truncateValue) { andre@0: end[-1] = '.'; andre@0: end[-2] = '.'; andre@0: end[-3] = '.'; andre@0: } andre@0: rv = SECSuccess; andre@0: } else if (!truncateValue) { andre@0: rv = escapeAndQuote(encodedAVA + nameLen, len - nameLen, andre@0: (char *)avaValue->data, avaValue->len, &mode); andre@0: } else { andre@0: /* must truncate the escaped and quoted value */ andre@0: char bigTmpBuf[TMPBUF_LEN * 3 + 3]; andre@0: PORT_Assert(valueLen < sizeof tmpBuf); andre@0: rv = escapeAndQuote(bigTmpBuf, sizeof bigTmpBuf, andre@0: (char *)avaValue->data, andre@0: PR_MIN(avaValue->len, valueLen), &mode); andre@0: andre@0: bigTmpBuf[valueLen--] = '\0'; /* hard stop here */ andre@0: /* See if we're in the middle of a multi-byte UTF8 character */ andre@0: while (((bigTmpBuf[valueLen] & 0xc0) == 0x80) && valueLen > 0) { andre@0: bigTmpBuf[valueLen--] = '\0'; andre@0: } andre@0: /* add ellipsis to signify truncation. */ andre@0: bigTmpBuf[++valueLen] = '.'; andre@0: bigTmpBuf[++valueLen] = '.'; andre@0: bigTmpBuf[++valueLen] = '.'; andre@0: if (bigTmpBuf[0] == '"') andre@0: bigTmpBuf[++valueLen] = '"'; andre@0: bigTmpBuf[++valueLen] = '\0'; andre@0: PORT_Assert(nameLen + valueLen <= (sizeof tmpBuf) - 1); andre@0: memcpy(encodedAVA + nameLen, bigTmpBuf, valueLen+1); andre@0: } andre@0: andre@0: SECITEM_FreeItem(avaValue, PR_TRUE); andre@0: if (rv == SECSuccess) andre@0: rv = AppendStr(bufp, encodedAVA); andre@0: if (encodedAVA != tmpBuf) andre@0: PORT_Free(encodedAVA); andre@0: return rv; andre@0: } andre@0: andre@0: #undef tagName andre@0: #undef maxBytes andre@0: #undef tag andre@0: #undef vt andre@0: andre@0: char * andre@0: CERT_NameToAsciiInvertible(CERTName *name, CertStrictnessLevel strict) andre@0: { andre@0: CERTRDN** rdns; andre@0: CERTRDN** lastRdn; andre@0: CERTRDN** rdn; andre@0: PRBool first = PR_TRUE; andre@0: stringBuf strBuf = { NULL, 0, 0 }; andre@0: andre@0: rdns = name->rdns; andre@0: if (rdns == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: /* find last RDN */ andre@0: lastRdn = rdns; andre@0: while (*lastRdn) lastRdn++; andre@0: lastRdn--; andre@0: andre@0: /* andre@0: * Loop over name contents in _reverse_ RDN order appending to string andre@0: */ andre@0: for (rdn = lastRdn; rdn >= rdns; rdn--) { andre@0: CERTAVA** avas = (*rdn)->avas; andre@0: CERTAVA* ava; andre@0: PRBool newRDN = PR_TRUE; andre@0: andre@0: /* andre@0: * XXX Do we need to traverse the AVAs in reverse order, too? andre@0: */ andre@0: while (avas && (ava = *avas++) != NULL) { andre@0: SECStatus rv; andre@0: /* Put in comma or plus separator */ andre@0: if (!first) { andre@0: /* Use of spaces is deprecated in RFC 2253. */ andre@0: rv = AppendStr(&strBuf, newRDN ? "," : "+"); andre@0: if (rv) goto loser; andre@0: } else { andre@0: first = PR_FALSE; andre@0: } andre@0: andre@0: /* Add in tag type plus value into strBuf */ andre@0: rv = AppendAVA(&strBuf, ava, strict); andre@0: if (rv) goto loser; andre@0: newRDN = PR_FALSE; andre@0: } andre@0: } andre@0: return strBuf.buffer; andre@0: loser: andre@0: if (strBuf.buffer) { andre@0: PORT_Free(strBuf.buffer); andre@0: } andre@0: return NULL; andre@0: } andre@0: andre@0: char * andre@0: CERT_NameToAscii(CERTName *name) andre@0: { andre@0: return CERT_NameToAsciiInvertible(name, CERT_N2A_READABLE); andre@0: } andre@0: andre@0: /* andre@0: * Return the string representation of a DER encoded distinguished name andre@0: * "dername" - The DER encoded name to convert andre@0: */ andre@0: char * andre@0: CERT_DerNameToAscii(SECItem *dername) andre@0: { andre@0: int rv; andre@0: PLArenaPool *arena = NULL; andre@0: CERTName name; andre@0: char *retstr = NULL; andre@0: andre@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); andre@0: andre@0: if ( arena == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: rv = SEC_QuickDERDecodeItem(arena, &name, CERT_NameTemplate, dername); andre@0: andre@0: if ( rv != SECSuccess ) { andre@0: goto loser; andre@0: } andre@0: andre@0: retstr = CERT_NameToAscii(&name); andre@0: andre@0: loser: andre@0: if ( arena != NULL ) { andre@0: PORT_FreeArena(arena, PR_FALSE); andre@0: } andre@0: andre@0: return(retstr); andre@0: } andre@0: andre@0: static char * andre@0: avaToString(PLArenaPool *arena, CERTAVA *ava) andre@0: { andre@0: char * buf = NULL; andre@0: SECItem* avaValue; andre@0: int valueLen; andre@0: andre@0: avaValue = CERT_DecodeAVAValue(&ava->value); andre@0: if(!avaValue) { andre@0: return buf; andre@0: } andre@0: valueLen = cert_RFC1485_GetRequiredLen((char *)avaValue->data, andre@0: avaValue->len, NULL) + 1; andre@0: if (arena) { andre@0: buf = (char *)PORT_ArenaZAlloc(arena, valueLen); andre@0: } else { andre@0: buf = (char *)PORT_ZAlloc(valueLen); andre@0: } andre@0: if (buf) { andre@0: SECStatus rv = escapeAndQuote(buf, valueLen, (char *)avaValue->data, andre@0: avaValue->len, NULL); andre@0: if (rv != SECSuccess) { andre@0: if (!arena) andre@0: PORT_Free(buf); andre@0: buf = NULL; andre@0: } andre@0: } andre@0: SECITEM_FreeItem(avaValue, PR_TRUE); andre@0: return buf; andre@0: } andre@0: andre@0: /* RDNs are sorted from most general to most specific. andre@0: * This code returns the FIRST one found, the most general one found. andre@0: */ andre@0: static char * andre@0: CERT_GetNameElement(PLArenaPool *arena, const CERTName *name, int wantedTag) andre@0: { andre@0: CERTRDN** rdns = name->rdns; andre@0: CERTRDN* rdn; andre@0: CERTAVA* ava = NULL; andre@0: andre@0: while (rdns && (rdn = *rdns++) != 0) { andre@0: CERTAVA** avas = rdn->avas; andre@0: while (avas && (ava = *avas++) != 0) { andre@0: int tag = CERT_GetAVATag(ava); andre@0: if ( tag == wantedTag ) { andre@0: avas = NULL; andre@0: rdns = NULL; /* break out of all loops */ andre@0: } andre@0: } andre@0: } andre@0: return ava ? avaToString(arena, ava) : NULL; andre@0: } andre@0: andre@0: /* RDNs are sorted from most general to most specific. andre@0: * This code returns the LAST one found, the most specific one found. andre@0: * This is particularly appropriate for Common Name. See RFC 2818. andre@0: */ andre@0: static char * andre@0: CERT_GetLastNameElement(PLArenaPool *arena, const CERTName *name, int wantedTag) andre@0: { andre@0: CERTRDN** rdns = name->rdns; andre@0: CERTRDN* rdn; andre@0: CERTAVA* lastAva = NULL; andre@0: andre@0: while (rdns && (rdn = *rdns++) != 0) { andre@0: CERTAVA** avas = rdn->avas; andre@0: CERTAVA* ava; andre@0: while (avas && (ava = *avas++) != 0) { andre@0: int tag = CERT_GetAVATag(ava); andre@0: if ( tag == wantedTag ) { andre@0: lastAva = ava; andre@0: } andre@0: } andre@0: } andre@0: return lastAva ? avaToString(arena, lastAva) : NULL; andre@0: } andre@0: andre@0: char * andre@0: CERT_GetCertificateEmailAddress(CERTCertificate *cert) andre@0: { andre@0: char *rawEmailAddr = NULL; andre@0: SECItem subAltName; andre@0: SECStatus rv; andre@0: CERTGeneralName *nameList = NULL; andre@0: CERTGeneralName *current; andre@0: PLArenaPool *arena = NULL; andre@0: int i; andre@0: andre@0: subAltName.data = NULL; andre@0: andre@0: rawEmailAddr = CERT_GetNameElement(cert->arena, &(cert->subject), andre@0: SEC_OID_PKCS9_EMAIL_ADDRESS); andre@0: if ( rawEmailAddr == NULL ) { andre@0: rawEmailAddr = CERT_GetNameElement(cert->arena, &(cert->subject), andre@0: SEC_OID_RFC1274_MAIL); andre@0: } andre@0: if ( rawEmailAddr == NULL) { andre@0: andre@0: rv = CERT_FindCertExtension(cert, SEC_OID_X509_SUBJECT_ALT_NAME, andre@0: &subAltName); andre@0: if (rv != SECSuccess) { andre@0: goto finish; andre@0: } andre@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); andre@0: if (!arena) { andre@0: goto finish; andre@0: } andre@0: nameList = current = CERT_DecodeAltNameExtension(arena, &subAltName); andre@0: if (!nameList ) { andre@0: goto finish; andre@0: } andre@0: if (nameList != NULL) { andre@0: do { andre@0: if (current->type == certDirectoryName) { andre@0: rawEmailAddr = CERT_GetNameElement(cert->arena, andre@0: &(current->name.directoryName), andre@0: SEC_OID_PKCS9_EMAIL_ADDRESS); andre@0: if ( rawEmailAddr == NULL ) { andre@0: rawEmailAddr = CERT_GetNameElement(cert->arena, andre@0: &(current->name.directoryName), SEC_OID_RFC1274_MAIL); andre@0: } andre@0: } else if (current->type == certRFC822Name) { andre@0: rawEmailAddr = (char*)PORT_ArenaZAlloc(cert->arena, andre@0: current->name.other.len + 1); andre@0: if (!rawEmailAddr) { andre@0: goto finish; andre@0: } andre@0: PORT_Memcpy(rawEmailAddr, current->name.other.data, andre@0: current->name.other.len); andre@0: rawEmailAddr[current->name.other.len] = '\0'; andre@0: } andre@0: if (rawEmailAddr) { andre@0: break; andre@0: } andre@0: current = CERT_GetNextGeneralName(current); andre@0: } while (current != nameList); andre@0: } andre@0: } andre@0: if (rawEmailAddr) { andre@0: for (i = 0; i <= (int) PORT_Strlen(rawEmailAddr); i++) { andre@0: rawEmailAddr[i] = tolower(rawEmailAddr[i]); andre@0: } andre@0: } andre@0: andre@0: finish: andre@0: andre@0: /* Don't free nameList, it's part of the arena. */ andre@0: andre@0: if (arena) { andre@0: PORT_FreeArena(arena, PR_FALSE); andre@0: } andre@0: andre@0: if ( subAltName.data ) { andre@0: SECITEM_FreeItem(&subAltName, PR_FALSE); andre@0: } andre@0: andre@0: return(rawEmailAddr); andre@0: } andre@0: andre@0: static char * andre@0: appendStringToBuf(char *dest, char *src, PRUint32 *pRemaining) andre@0: { andre@0: PRUint32 len; andre@0: if (dest && src && src[0] && *pRemaining > (len = PL_strlen(src))) { andre@0: PRUint32 i; andre@0: for (i = 0; i < len; ++i) andre@0: dest[i] = tolower(src[i]); andre@0: dest[len] = 0; andre@0: dest += len + 1; andre@0: *pRemaining -= len + 1; andre@0: } andre@0: return dest; andre@0: } andre@0: andre@0: #undef NEEDS_HEX_ESCAPE andre@0: #define NEEDS_HEX_ESCAPE(c) (c < 0x20) andre@0: andre@0: static char * andre@0: appendItemToBuf(char *dest, SECItem *src, PRUint32 *pRemaining) andre@0: { andre@0: if (dest && src && src->data && src->len && src->data[0]) { andre@0: PRUint32 len = src->len; andre@0: PRUint32 i; andre@0: PRUint32 reqLen = len + 1; andre@0: /* are there any embedded control characters ? */ andre@0: for (i = 0; i < len; i++) { andre@0: if (NEEDS_HEX_ESCAPE(src->data[i])) andre@0: reqLen += 2; andre@0: } andre@0: if (*pRemaining > reqLen) { andre@0: for (i = 0; i < len; ++i) { andre@0: PRUint8 c = src->data[i]; andre@0: if (NEEDS_HEX_ESCAPE(c)) { andre@0: *dest++ = C_BACKSLASH; andre@0: *dest++ = hexChars[ (c >> 4) & 0x0f ]; andre@0: *dest++ = hexChars[ c & 0x0f ]; andre@0: } else { andre@0: *dest++ = tolower(c); andre@0: } andre@0: } andre@0: *dest++ = '\0'; andre@0: *pRemaining -= reqLen; andre@0: } andre@0: } andre@0: return dest; andre@0: } andre@0: andre@0: /* Returns a pointer to an environment-like string, a series of andre@0: ** null-terminated strings, terminated by a zero-length string. andre@0: ** This function is intended to be internal to NSS. andre@0: */ andre@0: char * andre@0: cert_GetCertificateEmailAddresses(CERTCertificate *cert) andre@0: { andre@0: char * rawEmailAddr = NULL; andre@0: char * addrBuf = NULL; andre@0: char * pBuf = NULL; andre@0: PLArenaPool * tmpArena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); andre@0: PRUint32 maxLen = 0; andre@0: PRInt32 finalLen = 0; andre@0: SECStatus rv; andre@0: SECItem subAltName; andre@0: andre@0: if (!tmpArena) andre@0: return addrBuf; andre@0: andre@0: subAltName.data = NULL; andre@0: maxLen = cert->derCert.len; andre@0: PORT_Assert(maxLen); andre@0: if (!maxLen) andre@0: maxLen = 2000; /* a guess, should never happen */ andre@0: andre@0: pBuf = addrBuf = (char *)PORT_ArenaZAlloc(tmpArena, maxLen + 1); andre@0: if (!addrBuf) andre@0: goto loser; andre@0: andre@0: rawEmailAddr = CERT_GetNameElement(tmpArena, &cert->subject, andre@0: SEC_OID_PKCS9_EMAIL_ADDRESS); andre@0: pBuf = appendStringToBuf(pBuf, rawEmailAddr, &maxLen); andre@0: andre@0: rawEmailAddr = CERT_GetNameElement(tmpArena, &cert->subject, andre@0: SEC_OID_RFC1274_MAIL); andre@0: pBuf = appendStringToBuf(pBuf, rawEmailAddr, &maxLen); andre@0: andre@0: rv = CERT_FindCertExtension(cert, SEC_OID_X509_SUBJECT_ALT_NAME, andre@0: &subAltName); andre@0: if (rv == SECSuccess && subAltName.data) { andre@0: CERTGeneralName *nameList = NULL; andre@0: andre@0: if (!!(nameList = CERT_DecodeAltNameExtension(tmpArena, &subAltName))) { andre@0: CERTGeneralName *current = nameList; andre@0: do { andre@0: if (current->type == certDirectoryName) { andre@0: rawEmailAddr = CERT_GetNameElement(tmpArena, andre@0: ¤t->name.directoryName, andre@0: SEC_OID_PKCS9_EMAIL_ADDRESS); andre@0: pBuf = appendStringToBuf(pBuf, rawEmailAddr, &maxLen); andre@0: andre@0: rawEmailAddr = CERT_GetNameElement(tmpArena, andre@0: ¤t->name.directoryName, andre@0: SEC_OID_RFC1274_MAIL); andre@0: pBuf = appendStringToBuf(pBuf, rawEmailAddr, &maxLen); andre@0: } else if (current->type == certRFC822Name) { andre@0: pBuf = appendItemToBuf(pBuf, ¤t->name.other, &maxLen); andre@0: } andre@0: current = CERT_GetNextGeneralName(current); andre@0: } while (current != nameList); andre@0: } andre@0: SECITEM_FreeItem(&subAltName, PR_FALSE); andre@0: /* Don't free nameList, it's part of the tmpArena. */ andre@0: } andre@0: /* now copy superstring to cert's arena */ andre@0: finalLen = (pBuf - addrBuf) + 1; andre@0: pBuf = NULL; andre@0: if (finalLen > 1) { andre@0: pBuf = PORT_ArenaAlloc(cert->arena, finalLen); andre@0: if (pBuf) { andre@0: PORT_Memcpy(pBuf, addrBuf, finalLen); andre@0: } andre@0: } andre@0: loser: andre@0: if (tmpArena) andre@0: PORT_FreeArena(tmpArena, PR_FALSE); andre@0: andre@0: return pBuf; andre@0: } andre@0: andre@0: /* returns pointer to storage in cert's arena. Storage remains valid andre@0: ** as long as cert's reference count doesn't go to zero. andre@0: ** Caller should strdup or otherwise copy. andre@0: */ andre@0: const char * /* const so caller won't muck with it. */ andre@0: CERT_GetFirstEmailAddress(CERTCertificate * cert) andre@0: { andre@0: if (cert && cert->emailAddr && cert->emailAddr[0]) andre@0: return (const char *)cert->emailAddr; andre@0: return NULL; andre@0: } andre@0: andre@0: /* returns pointer to storage in cert's arena. Storage remains valid andre@0: ** as long as cert's reference count doesn't go to zero. andre@0: ** Caller should strdup or otherwise copy. andre@0: */ andre@0: const char * /* const so caller won't muck with it. */ andre@0: CERT_GetNextEmailAddress(CERTCertificate * cert, const char * prev) andre@0: { andre@0: if (cert && prev && prev[0]) { andre@0: PRUint32 len = PL_strlen(prev); andre@0: prev += len + 1; andre@0: if (prev && prev[0]) andre@0: return prev; andre@0: } andre@0: return NULL; andre@0: } andre@0: andre@0: /* This is seriously bogus, now that certs store their email addresses in andre@0: ** subject Alternative Name extensions. andre@0: ** Returns a string allocated by PORT_StrDup, which the caller must free. andre@0: */ andre@0: char * andre@0: CERT_GetCertEmailAddress(const CERTName *name) andre@0: { andre@0: char *rawEmailAddr; andre@0: char *emailAddr; andre@0: andre@0: andre@0: rawEmailAddr = CERT_GetNameElement(NULL, name, SEC_OID_PKCS9_EMAIL_ADDRESS); andre@0: if ( rawEmailAddr == NULL ) { andre@0: rawEmailAddr = CERT_GetNameElement(NULL, name, SEC_OID_RFC1274_MAIL); andre@0: } andre@0: emailAddr = CERT_FixupEmailAddr(rawEmailAddr); andre@0: if ( rawEmailAddr ) { andre@0: PORT_Free(rawEmailAddr); andre@0: } andre@0: return(emailAddr); andre@0: } andre@0: andre@0: /* The return value must be freed with PORT_Free. */ andre@0: char * andre@0: CERT_GetCommonName(const CERTName *name) andre@0: { andre@0: return(CERT_GetLastNameElement(NULL, name, SEC_OID_AVA_COMMON_NAME)); andre@0: } andre@0: andre@0: char * andre@0: CERT_GetCountryName(const CERTName *name) andre@0: { andre@0: return(CERT_GetNameElement(NULL, name, SEC_OID_AVA_COUNTRY_NAME)); andre@0: } andre@0: andre@0: char * andre@0: CERT_GetLocalityName(const CERTName *name) andre@0: { andre@0: return(CERT_GetNameElement(NULL, name, SEC_OID_AVA_LOCALITY)); andre@0: } andre@0: andre@0: char * andre@0: CERT_GetStateName(const CERTName *name) andre@0: { andre@0: return(CERT_GetNameElement(NULL, name, SEC_OID_AVA_STATE_OR_PROVINCE)); andre@0: } andre@0: andre@0: char * andre@0: CERT_GetOrgName(const CERTName *name) andre@0: { andre@0: return(CERT_GetNameElement(NULL, name, SEC_OID_AVA_ORGANIZATION_NAME)); andre@0: } andre@0: andre@0: char * andre@0: CERT_GetDomainComponentName(const CERTName *name) andre@0: { andre@0: return(CERT_GetNameElement(NULL, name, SEC_OID_AVA_DC)); andre@0: } andre@0: andre@0: char * andre@0: CERT_GetOrgUnitName(const CERTName *name) andre@0: { andre@0: return(CERT_GetNameElement(NULL, name, SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME)); andre@0: } andre@0: andre@0: char * andre@0: CERT_GetDnQualifier(const CERTName *name) andre@0: { andre@0: return(CERT_GetNameElement(NULL, name, SEC_OID_AVA_DN_QUALIFIER)); andre@0: } andre@0: andre@0: char * andre@0: CERT_GetCertUid(const CERTName *name) andre@0: { andre@0: return(CERT_GetNameElement(NULL, name, SEC_OID_RFC1274_UID)); andre@0: } andre@0: