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 "cert.h" andre@0: #include "secoid.h" andre@0: #include "secder.h" /* XXX remove this when remove the DERTemplates */ andre@0: #include "secasn1.h" andre@0: #include "secitem.h" andre@0: #include andre@0: #include "secerr.h" andre@0: #include "certi.h" andre@0: andre@0: static const SEC_ASN1Template cert_AVATemplate[] = { andre@0: { SEC_ASN1_SEQUENCE, andre@0: 0, NULL, sizeof(CERTAVA) }, andre@0: { SEC_ASN1_OBJECT_ID, andre@0: offsetof(CERTAVA,type), }, andre@0: { SEC_ASN1_ANY, andre@0: offsetof(CERTAVA,value), }, andre@0: { 0, } andre@0: }; andre@0: andre@0: const SEC_ASN1Template CERT_RDNTemplate[] = { andre@0: { SEC_ASN1_SET_OF, andre@0: offsetof(CERTRDN,avas), cert_AVATemplate, sizeof(CERTRDN) } andre@0: }; andre@0: andre@0: andre@0: static int andre@0: CountArray(void **array) andre@0: { andre@0: int count = 0; andre@0: if (array) { andre@0: while (*array++) { andre@0: count++; andre@0: } andre@0: } andre@0: return count; andre@0: } andre@0: andre@0: static void ** andre@0: AddToArray(PLArenaPool *arena, void **array, void *element) andre@0: { andre@0: unsigned count; andre@0: void **ap; andre@0: andre@0: /* Count up number of slots already in use in the array */ andre@0: count = 0; andre@0: ap = array; andre@0: if (ap) { andre@0: while (*ap++) { andre@0: count++; andre@0: } andre@0: } andre@0: andre@0: if (array) { andre@0: array = (void**) PORT_ArenaGrow(arena, array, andre@0: (count + 1) * sizeof(void *), andre@0: (count + 2) * sizeof(void *)); andre@0: } else { andre@0: array = (void**) PORT_ArenaAlloc(arena, (count + 2) * sizeof(void *)); andre@0: } andre@0: if (array) { andre@0: array[count] = element; andre@0: array[count+1] = 0; andre@0: } andre@0: return array; andre@0: } andre@0: andre@0: andre@0: SECOidTag andre@0: CERT_GetAVATag(CERTAVA *ava) andre@0: { andre@0: SECOidData *oid; andre@0: if (!ava->type.data) return (SECOidTag)-1; andre@0: andre@0: oid = SECOID_FindOID(&ava->type); andre@0: andre@0: if ( oid ) { andre@0: return(oid->offset); andre@0: } andre@0: return (SECOidTag)-1; andre@0: } andre@0: andre@0: static SECStatus andre@0: SetupAVAType(PLArenaPool *arena, SECOidTag type, SECItem *it, unsigned *maxLenp) andre@0: { andre@0: unsigned char *oid; andre@0: unsigned oidLen; andre@0: unsigned char *cp; andre@0: int maxLen; andre@0: SECOidData *oidrec; andre@0: andre@0: oidrec = SECOID_FindOIDByTag(type); andre@0: if (oidrec == NULL) andre@0: return SECFailure; andre@0: andre@0: oid = oidrec->oid.data; andre@0: oidLen = oidrec->oid.len; andre@0: andre@0: maxLen = cert_AVAOidTagToMaxLen(type); andre@0: if (maxLen < 0) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: andre@0: it->data = cp = (unsigned char*) PORT_ArenaAlloc(arena, oidLen); andre@0: if (cp == NULL) { andre@0: return SECFailure; andre@0: } andre@0: it->len = oidLen; andre@0: PORT_Memcpy(cp, oid, oidLen); andre@0: *maxLenp = (unsigned)maxLen; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: static SECStatus andre@0: SetupAVAValue(PLArenaPool *arena, int valueType, const SECItem *in, andre@0: SECItem *out, unsigned maxLen) andre@0: { andre@0: PRUint8 *value, *cp, *ucs4Val; andre@0: unsigned valueLen, valueLenLen, total; andre@0: unsigned ucs4Len = 0, ucs4MaxLen; andre@0: andre@0: value = in->data; andre@0: valueLen = in->len; andre@0: switch (valueType) { andre@0: case SEC_ASN1_PRINTABLE_STRING: andre@0: case SEC_ASN1_IA5_STRING: andre@0: case SEC_ASN1_T61_STRING: andre@0: case SEC_ASN1_UTF8_STRING: /* no conversion required */ andre@0: break; andre@0: case SEC_ASN1_UNIVERSAL_STRING: andre@0: ucs4MaxLen = valueLen * 6; andre@0: ucs4Val = (PRUint8 *)PORT_ArenaZAlloc(arena, ucs4MaxLen); andre@0: if(!ucs4Val || !PORT_UCS4_UTF8Conversion(PR_TRUE, value, valueLen, andre@0: ucs4Val, ucs4MaxLen, &ucs4Len)) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: value = ucs4Val; andre@0: valueLen = ucs4Len; andre@0: maxLen *= 4; andre@0: break; andre@0: default: andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: andre@0: if (valueLen > maxLen) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: andre@0: valueLenLen = DER_LengthLength(valueLen); andre@0: total = 1 + valueLenLen + valueLen; andre@0: cp = (PRUint8*)PORT_ArenaAlloc(arena, total); andre@0: if (!cp) { andre@0: return SECFailure; andre@0: } andre@0: out->data = cp; andre@0: out->len = total; andre@0: cp = (PRUint8 *)DER_StoreHeader(cp, valueType, valueLen); andre@0: PORT_Memcpy(cp, value, valueLen); andre@0: return SECSuccess; andre@0: } andre@0: andre@0: CERTAVA * andre@0: CERT_CreateAVAFromRaw(PLArenaPool *pool, const SECItem * OID, andre@0: const SECItem * value) andre@0: { andre@0: CERTAVA *ava; andre@0: int rv; andre@0: andre@0: ava = PORT_ArenaZNew(pool, CERTAVA); andre@0: if (ava) { andre@0: rv = SECITEM_CopyItem(pool, &ava->type, OID); andre@0: if (rv) andre@0: return NULL; andre@0: andre@0: rv = SECITEM_CopyItem(pool, &ava->value, value); andre@0: if (rv) andre@0: return NULL; andre@0: } andre@0: return ava; andre@0: } andre@0: andre@0: CERTAVA * andre@0: CERT_CreateAVAFromSECItem(PLArenaPool *arena, SECOidTag kind, int valueType, andre@0: SECItem *value) andre@0: { andre@0: CERTAVA *ava; andre@0: int rv; andre@0: unsigned maxLen; andre@0: andre@0: ava = (CERTAVA*) PORT_ArenaZAlloc(arena, sizeof(CERTAVA)); andre@0: if (ava) { andre@0: rv = SetupAVAType(arena, kind, &ava->type, &maxLen); andre@0: if (rv) { andre@0: /* Illegal AVA type */ andre@0: return NULL; andre@0: } andre@0: rv = SetupAVAValue(arena, valueType, value, &ava->value, maxLen); andre@0: if (rv) { andre@0: /* Illegal value type */ andre@0: return NULL; andre@0: } andre@0: } andre@0: return ava; andre@0: } andre@0: andre@0: CERTAVA * andre@0: CERT_CreateAVA(PLArenaPool *arena, SECOidTag kind, int valueType, char *value) andre@0: { andre@0: SECItem item = { siBuffer, NULL, 0 }; andre@0: andre@0: item.data = (PRUint8 *)value; andre@0: item.len = PORT_Strlen(value); andre@0: andre@0: return CERT_CreateAVAFromSECItem(arena, kind, valueType, &item); andre@0: } andre@0: andre@0: CERTAVA * andre@0: CERT_CopyAVA(PLArenaPool *arena, CERTAVA *from) andre@0: { andre@0: CERTAVA *ava; andre@0: int rv; andre@0: andre@0: ava = (CERTAVA*) PORT_ArenaZAlloc(arena, sizeof(CERTAVA)); andre@0: if (ava) { andre@0: rv = SECITEM_CopyItem(arena, &ava->type, &from->type); andre@0: if (rv) goto loser; andre@0: rv = SECITEM_CopyItem(arena, &ava->value, &from->value); andre@0: if (rv) goto loser; andre@0: } andre@0: return ava; andre@0: andre@0: loser: andre@0: return 0; andre@0: } andre@0: andre@0: /************************************************************************/ andre@0: /* XXX This template needs to go away in favor of the new SEC_ASN1 version. */ andre@0: static const SEC_ASN1Template cert_RDNTemplate[] = { andre@0: { SEC_ASN1_SET_OF, andre@0: offsetof(CERTRDN,avas), cert_AVATemplate, sizeof(CERTRDN) } andre@0: }; andre@0: andre@0: andre@0: CERTRDN * andre@0: CERT_CreateRDN(PLArenaPool *arena, CERTAVA *ava0, ...) andre@0: { andre@0: CERTAVA *ava; andre@0: CERTRDN *rdn; andre@0: va_list ap; andre@0: unsigned count; andre@0: CERTAVA **avap; andre@0: andre@0: rdn = (CERTRDN*) PORT_ArenaAlloc(arena, sizeof(CERTRDN)); andre@0: if (rdn) { andre@0: /* Count number of avas going into the rdn */ andre@0: count = 0; andre@0: if (ava0) { andre@0: count++; andre@0: va_start(ap, ava0); andre@0: while ((ava = va_arg(ap, CERTAVA*)) != 0) { andre@0: count++; andre@0: } andre@0: va_end(ap); andre@0: } andre@0: andre@0: /* Now fill in the pointers */ andre@0: rdn->avas = avap = andre@0: (CERTAVA**) PORT_ArenaAlloc( arena, (count + 1)*sizeof(CERTAVA*)); andre@0: if (!avap) { andre@0: return 0; andre@0: } andre@0: if (ava0) { andre@0: *avap++ = ava0; andre@0: va_start(ap, ava0); andre@0: while ((ava = va_arg(ap, CERTAVA*)) != 0) { andre@0: *avap++ = ava; andre@0: } andre@0: va_end(ap); andre@0: } andre@0: *avap++ = 0; andre@0: } andre@0: return rdn; andre@0: } andre@0: andre@0: SECStatus andre@0: CERT_AddAVA(PLArenaPool *arena, CERTRDN *rdn, CERTAVA *ava) andre@0: { andre@0: rdn->avas = (CERTAVA**) AddToArray(arena, (void**) rdn->avas, ava); andre@0: return rdn->avas ? SECSuccess : SECFailure; andre@0: } andre@0: andre@0: SECStatus andre@0: CERT_CopyRDN(PLArenaPool *arena, CERTRDN *to, CERTRDN *from) andre@0: { andre@0: CERTAVA **avas, *fava, *tava; andre@0: SECStatus rv = SECSuccess; andre@0: andre@0: /* Copy each ava from from */ andre@0: avas = from->avas; andre@0: if (avas) { andre@0: if (avas[0] == NULL) { andre@0: rv = CERT_AddAVA(arena, to, NULL); andre@0: return rv; andre@0: } andre@0: while ((fava = *avas++) != 0) { andre@0: tava = CERT_CopyAVA(arena, fava); andre@0: if (!tava) { andre@0: rv = SECFailure; andre@0: break; andre@0: } andre@0: rv = CERT_AddAVA(arena, to, tava); andre@0: if (rv != SECSuccess) andre@0: break; andre@0: } andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /************************************************************************/ andre@0: andre@0: const SEC_ASN1Template CERT_NameTemplate[] = { andre@0: { SEC_ASN1_SEQUENCE_OF, andre@0: offsetof(CERTName,rdns), CERT_RDNTemplate, sizeof(CERTName) } andre@0: }; andre@0: andre@0: SEC_ASN1_CHOOSER_IMPLEMENT(CERT_NameTemplate) andre@0: andre@0: CERTName * andre@0: CERT_CreateName(CERTRDN *rdn0, ...) andre@0: { andre@0: CERTRDN *rdn; andre@0: CERTName *name; andre@0: va_list ap; andre@0: unsigned count; andre@0: CERTRDN **rdnp; andre@0: PLArenaPool *arena; andre@0: andre@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); andre@0: if ( !arena ) { andre@0: return(0); andre@0: } andre@0: andre@0: name = (CERTName*) PORT_ArenaAlloc(arena, sizeof(CERTName)); andre@0: if (name) { andre@0: name->arena = arena; andre@0: andre@0: /* Count number of RDNs going into the Name */ andre@0: if (!rdn0) { andre@0: count = 0; andre@0: } else { andre@0: count = 1; andre@0: va_start(ap, rdn0); andre@0: while ((rdn = va_arg(ap, CERTRDN*)) != 0) { andre@0: count++; andre@0: } andre@0: va_end(ap); andre@0: } andre@0: andre@0: /* Allocate space (including space for terminal null ptr) */ andre@0: name->rdns = rdnp = andre@0: (CERTRDN**) PORT_ArenaAlloc(arena, (count + 1) * sizeof(CERTRDN*)); andre@0: if (!name->rdns) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* Now fill in the pointers */ andre@0: if (count > 0) { andre@0: *rdnp++ = rdn0; andre@0: va_start(ap, rdn0); andre@0: while ((rdn = va_arg(ap, CERTRDN*)) != 0) { andre@0: *rdnp++ = rdn; andre@0: } andre@0: va_end(ap); andre@0: } andre@0: andre@0: /* null terminate the list */ andre@0: *rdnp++ = 0; andre@0: } andre@0: return name; andre@0: andre@0: loser: andre@0: PORT_FreeArena(arena, PR_FALSE); andre@0: return(0); andre@0: } andre@0: andre@0: void andre@0: CERT_DestroyName(CERTName *name) andre@0: { andre@0: if (name) andre@0: { andre@0: PLArenaPool *arena = name->arena; andre@0: name->rdns = NULL; andre@0: name->arena = NULL; andre@0: if (arena) PORT_FreeArena(arena, PR_FALSE); andre@0: } andre@0: } andre@0: andre@0: SECStatus andre@0: CERT_AddRDN(CERTName *name, CERTRDN *rdn) andre@0: { andre@0: name->rdns = (CERTRDN**) AddToArray(name->arena, (void**) name->rdns, rdn); andre@0: return name->rdns ? SECSuccess : SECFailure; andre@0: } andre@0: andre@0: SECStatus andre@0: CERT_CopyName(PLArenaPool *arena, CERTName *to, const CERTName *from) andre@0: { andre@0: CERTRDN **rdns, *frdn, *trdn; andre@0: SECStatus rv = SECSuccess; andre@0: andre@0: if (!to || !from) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: andre@0: CERT_DestroyName(to); andre@0: to->arena = arena; andre@0: andre@0: /* Copy each rdn from from */ andre@0: rdns = from->rdns; andre@0: if (rdns) { andre@0: if (rdns[0] == NULL) { andre@0: rv = CERT_AddRDN(to, NULL); andre@0: return rv; andre@0: } andre@0: while ((frdn = *rdns++) != NULL) { andre@0: trdn = CERT_CreateRDN(arena, NULL); andre@0: if (!trdn) { andre@0: rv = SECFailure; andre@0: break; andre@0: } andre@0: rv = CERT_CopyRDN(arena, trdn, frdn); andre@0: if (rv != SECSuccess) andre@0: break; andre@0: rv = CERT_AddRDN(to, trdn); andre@0: if (rv != SECSuccess) andre@0: break; andre@0: } andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /************************************************************************/ andre@0: andre@0: static void andre@0: canonicalize(SECItem * foo) andre@0: { andre@0: int ch, lastch, len, src, dest; andre@0: andre@0: /* strip trailing whitespace. */ andre@0: len = foo->len; andre@0: while (len > 0 && ((ch = foo->data[len - 1]) == ' ' || andre@0: ch == '\t' || ch == '\r' || ch == '\n')) { andre@0: len--; andre@0: } andre@0: andre@0: src = 0; andre@0: /* strip leading whitespace. */ andre@0: while (src < len && ((ch = foo->data[src]) == ' ' || andre@0: ch == '\t' || ch == '\r' || ch == '\n')) { andre@0: src++; andre@0: } andre@0: dest = 0; lastch = ' '; andre@0: while (src < len) { andre@0: ch = foo->data[src++]; andre@0: if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') { andre@0: ch = ' '; andre@0: if (ch == lastch) andre@0: continue; andre@0: } else if (ch >= 'A' && ch <= 'Z') { andre@0: ch |= 0x20; /* downshift */ andre@0: } andre@0: foo->data[dest++] = lastch = ch; andre@0: } andre@0: foo->len = dest; andre@0: } andre@0: andre@0: /* SECItems a and b contain DER-encoded printable strings. */ andre@0: SECComparison andre@0: CERT_CompareDERPrintableStrings(const SECItem *a, const SECItem *b) andre@0: { andre@0: SECComparison rv = SECLessThan; andre@0: SECItem * aVal = CERT_DecodeAVAValue(a); andre@0: SECItem * bVal = CERT_DecodeAVAValue(b); andre@0: andre@0: if (aVal && aVal->len && aVal->data && andre@0: bVal && bVal->len && bVal->data) { andre@0: canonicalize(aVal); andre@0: canonicalize(bVal); andre@0: rv = SECITEM_CompareItem(aVal, bVal); andre@0: } andre@0: SECITEM_FreeItem(aVal, PR_TRUE); andre@0: SECITEM_FreeItem(bVal, PR_TRUE); andre@0: return rv; andre@0: } andre@0: andre@0: SECComparison andre@0: CERT_CompareAVA(const CERTAVA *a, const CERTAVA *b) andre@0: { andre@0: SECComparison rv; andre@0: andre@0: rv = SECITEM_CompareItem(&a->type, &b->type); andre@0: if (SECEqual != rv) andre@0: return rv; /* Attribute types don't match. */ andre@0: /* Let's be optimistic. Maybe the values will just compare equal. */ andre@0: rv = SECITEM_CompareItem(&a->value, &b->value); andre@0: if (SECEqual == rv) andre@0: return rv; /* values compared exactly. */ andre@0: if (a->value.len && a->value.data && b->value.len && b->value.data) { andre@0: /* Here, the values did not match. andre@0: ** If the values had different encodings, convert them to the same andre@0: ** encoding and compare that way. andre@0: */ andre@0: if (a->value.data[0] != b->value.data[0]) { andre@0: /* encodings differ. Convert both to UTF-8 and compare. */ andre@0: SECItem * aVal = CERT_DecodeAVAValue(&a->value); andre@0: SECItem * bVal = CERT_DecodeAVAValue(&b->value); andre@0: if (aVal && aVal->len && aVal->data && andre@0: bVal && bVal->len && bVal->data) { andre@0: rv = SECITEM_CompareItem(aVal, bVal); andre@0: } andre@0: SECITEM_FreeItem(aVal, PR_TRUE); andre@0: SECITEM_FreeItem(bVal, PR_TRUE); andre@0: } else if (a->value.data[0] == 0x13) { /* both are printable strings. */ andre@0: /* printable strings */ andre@0: rv = CERT_CompareDERPrintableStrings(&a->value, &b->value); andre@0: } andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: SECComparison andre@0: CERT_CompareRDN(const CERTRDN *a, const CERTRDN *b) andre@0: { andre@0: CERTAVA **aavas, *aava; andre@0: CERTAVA **bavas, *bava; andre@0: int ac, bc; andre@0: SECComparison rv = SECEqual; andre@0: andre@0: aavas = a->avas; andre@0: bavas = b->avas; andre@0: andre@0: /* andre@0: ** Make sure array of ava's are the same length. If not, then we are andre@0: ** not equal andre@0: */ andre@0: ac = CountArray((void**) aavas); andre@0: bc = CountArray((void**) bavas); andre@0: if (ac < bc) return SECLessThan; andre@0: if (ac > bc) return SECGreaterThan; andre@0: andre@0: while (NULL != (aava = *aavas++)) { andre@0: for (bavas = b->avas; NULL != (bava = *bavas++); ) { andre@0: rv = SECITEM_CompareItem(&aava->type, &bava->type); andre@0: if (SECEqual == rv) { andre@0: rv = CERT_CompareAVA(aava, bava); andre@0: if (SECEqual != rv) andre@0: return rv; andre@0: break; andre@0: } andre@0: } andre@0: if (!bava) /* didn't find a match */ andre@0: return SECGreaterThan; andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: SECComparison andre@0: CERT_CompareName(const CERTName *a, const CERTName *b) andre@0: { andre@0: CERTRDN **ardns, *ardn; andre@0: CERTRDN **brdns, *brdn; andre@0: int ac, bc; andre@0: SECComparison rv = SECEqual; andre@0: andre@0: ardns = a->rdns; andre@0: brdns = b->rdns; andre@0: andre@0: /* andre@0: ** Make sure array of rdn's are the same length. If not, then we are andre@0: ** not equal andre@0: */ andre@0: ac = CountArray((void**) ardns); andre@0: bc = CountArray((void**) brdns); andre@0: if (ac < bc) return SECLessThan; andre@0: if (ac > bc) return SECGreaterThan; andre@0: andre@0: for (;;) { andre@0: ardn = *ardns++; andre@0: brdn = *brdns++; andre@0: if (!ardn) { andre@0: break; andre@0: } andre@0: rv = CERT_CompareRDN(ardn, brdn); andre@0: if (rv) return rv; andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /* Moved from certhtml.c */ andre@0: SECItem * andre@0: CERT_DecodeAVAValue(const SECItem *derAVAValue) andre@0: { andre@0: SECItem *retItem; andre@0: const SEC_ASN1Template *theTemplate = NULL; andre@0: enum { conv_none, conv_ucs4, conv_ucs2, conv_iso88591 } convert = conv_none; andre@0: SECItem avaValue = {siBuffer, 0}; andre@0: PLArenaPool *newarena = NULL; andre@0: andre@0: if (!derAVAValue || !derAVAValue->len || !derAVAValue->data) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return NULL; andre@0: } andre@0: andre@0: switch(derAVAValue->data[0]) { andre@0: case SEC_ASN1_UNIVERSAL_STRING: andre@0: convert = conv_ucs4; andre@0: theTemplate = SEC_ASN1_GET(SEC_UniversalStringTemplate); andre@0: break; andre@0: case SEC_ASN1_IA5_STRING: andre@0: theTemplate = SEC_ASN1_GET(SEC_IA5StringTemplate); andre@0: break; andre@0: case SEC_ASN1_PRINTABLE_STRING: andre@0: theTemplate = SEC_ASN1_GET(SEC_PrintableStringTemplate); andre@0: break; andre@0: case SEC_ASN1_T61_STRING: andre@0: /* andre@0: * Per common practice, we're not decoding actual T.61, but instead andre@0: * treating T61-labeled strings as containing ISO-8859-1. andre@0: */ andre@0: convert = conv_iso88591; andre@0: theTemplate = SEC_ASN1_GET(SEC_T61StringTemplate); andre@0: break; andre@0: case SEC_ASN1_BMP_STRING: andre@0: convert = conv_ucs2; andre@0: theTemplate = SEC_ASN1_GET(SEC_BMPStringTemplate); andre@0: break; andre@0: case SEC_ASN1_UTF8_STRING: andre@0: /* No conversion needed ! */ andre@0: theTemplate = SEC_ASN1_GET(SEC_UTF8StringTemplate); andre@0: break; andre@0: default: andre@0: PORT_SetError(SEC_ERROR_INVALID_AVA); andre@0: return NULL; andre@0: } andre@0: andre@0: PORT_Memset(&avaValue, 0, sizeof(SECItem)); andre@0: newarena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); andre@0: if (!newarena) { andre@0: return NULL; andre@0: } andre@0: if(SEC_QuickDERDecodeItem(newarena, &avaValue, theTemplate, derAVAValue) andre@0: != SECSuccess) { andre@0: PORT_FreeArena(newarena, PR_FALSE); andre@0: return NULL; andre@0: } andre@0: andre@0: if (convert != conv_none) { andre@0: unsigned int utf8ValLen = avaValue.len * 3; andre@0: unsigned char *utf8Val = (unsigned char*) andre@0: PORT_ArenaZAlloc(newarena, utf8ValLen); andre@0: andre@0: switch (convert) { andre@0: case conv_ucs4: andre@0: if(avaValue.len % 4 != 0 || andre@0: !PORT_UCS4_UTF8Conversion(PR_FALSE, avaValue.data, avaValue.len, andre@0: utf8Val, utf8ValLen, &utf8ValLen)) { andre@0: PORT_FreeArena(newarena, PR_FALSE); andre@0: PORT_SetError(SEC_ERROR_INVALID_AVA); andre@0: return NULL; andre@0: } andre@0: break; andre@0: case conv_ucs2: andre@0: if(avaValue.len % 2 != 0 || andre@0: !PORT_UCS2_UTF8Conversion(PR_FALSE, avaValue.data, avaValue.len, andre@0: utf8Val, utf8ValLen, &utf8ValLen)) { andre@0: PORT_FreeArena(newarena, PR_FALSE); andre@0: PORT_SetError(SEC_ERROR_INVALID_AVA); andre@0: return NULL; andre@0: } andre@0: break; andre@0: case conv_iso88591: andre@0: if(!PORT_ISO88591_UTF8Conversion(avaValue.data, avaValue.len, andre@0: utf8Val, utf8ValLen, &utf8ValLen)) { andre@0: PORT_FreeArena(newarena, PR_FALSE); andre@0: PORT_SetError(SEC_ERROR_INVALID_AVA); andre@0: return NULL; andre@0: } andre@0: break; andre@0: case conv_none: andre@0: PORT_Assert(0); /* not reached */ andre@0: break; andre@0: } andre@0: andre@0: avaValue.data = utf8Val; andre@0: avaValue.len = utf8ValLen; andre@0: } andre@0: andre@0: retItem = SECITEM_DupItem(&avaValue); andre@0: PORT_FreeArena(newarena, PR_FALSE); andre@0: return retItem; andre@0: }