atav.c

来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 1,804 行 · 第 1/3 页

C
1,804
字号
    }    oidder = nssOID_GetDEREncoding(rv->oid, a);    if( (NSSDER *)NULL == oidder ) {      (void)NSSArena_Destroy(a);      (void)nss_ZFreeIf(rv->value);      (void)nss_ZFreeIf(rv);      return (NSSATAV *)NULL;    }    if( (struct nss_attribute_data_str *)NULL == which ) {      /*       * We'll just have to take the user data as an octet stream.       */      if( (void *)NULL == xitem.data ) {        /*         * This means that an ATTR entry has been added to oids.txt,         * but no corresponding entry has been added to the array         * ns_attribute_data[] above.         */        nss_SetError(NSS_ERROR_INTERNAL_ERROR);        (void)NSSArena_Destroy(a);        (void)nss_ZFreeIf(rv->value);        (void)nss_ZFreeIf(rv);        return (NSSATAV *)NULL;      }      vitem = nssASN1_EncodeDER(a, (NSSDER *)NULL, &xitem,                                 nssASN1Template_OctetString);      if( (NSSItem *)NULL == vitem ) {        (void)NSSArena_Destroy(a);        (void)nss_ZFreeIf(rv->value);        (void)nss_ZFreeIf(rv);        return (NSSATAV *)NULL;      }      rv->stringForm = nssStringType_Unknown;    } else {      PRUint32 length = 0;            if( PR_SUCCESS != nssUTF8_Length(rv->value, &length) ) {        (void)NSSArena_Destroy(a);        (void)nss_ZFreeIf(rv->value);        (void)nss_ZFreeIf(rv);        return (NSSATAV *)NULL;      }      if( ((0 != which->minStringLength) &&            (length < which->minStringLength)) ||          ((0 != which->maxStringLength) &&           (length > which->maxStringLength)) ) {        nss_SetError(NSS_ERROR_INVALID_STRING);        (void)NSSArena_Destroy(a);        (void)nss_ZFreeIf(rv->value);        (void)nss_ZFreeIf(rv);        return (NSSATAV *)NULL;      }      vitem = nssUTF8_GetDEREncoding(a, which->stringType, rv->value);      if( (NSSItem *)NULL == vitem ) {        (void)NSSArena_Destroy(a);        (void)nss_ZFreeIf(rv->value);        (void)nss_ZFreeIf(rv);        return (NSSATAV *)NULL;      }      if( nssStringType_DirectoryString == which->stringType ) {        rv->stringForm = nssStringType_UTF8String;      } else {        rv->stringForm = which->stringType;      }    }    ah.oid = *oidder;    ah.value = *vitem;    status = nssASN1_EncodeDER(arenaOpt, &rv->ber, &ah,                                nss_atav_template);    if( (NSSDER *)NULL == status ) {      (void)NSSArena_Destroy(a);      (void)nss_ZFreeIf(rv->value);      (void)nss_ZFreeIf(rv);      return (NSSATAV *)NULL;    }    (void)NSSArena_Destroy(a);  }  return rv;}/* * nssATAV_Create * * This routine creates an NSSATAV from the specified NSSOID and the * specified data. If the optional arena argument is non-null, the  * memory used will be obtained from that arena; otherwise, the memory * will be obtained from the heap.If the specified data length is zero,  * the data is assumed to be terminated by first zero byte; this allows  * UTF8 strings to be easily specified.  This routine may return NULL  * upon error, in which case it will have set an error on the error  * stack. * * The error may be one of the following values: *  NSS_ERROR_INVALID_ARENA *  NSS_ERROR_INVALID_NSSOID *  NSS_ERROR_INVALID_POINTER *  NSS_ERROR_NO_MEMORY * * Return value: *  NULL upon error *  A pointer to an NSSATAV upon success */NSS_IMPLEMENT NSSATAV *nssATAV_Create(  NSSArena *arenaOpt,  const NSSOID *oid,  const void *data,  PRUint32 length){#ifdef NSSDEBUG  if( (NSSArena *)NULL != arenaOpt ) {    if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) {      return (NSSATAV *)NULL;    }  }  if( PR_SUCCESS != nssOID_verifyPointer(oid) ) {    return (NSSATAV *)NULL;  }  if( (const void *)NULL == data ) {    nss_SetError(NSS_ERROR_INVALID_POINTER);    return (NSSATAV *)NULL;  }#endif /* NSSDEBUG */  /* XXX fgmr-- oops, forgot this one */  return (NSSATAV *)NULL;}/* * nssATAV_Destroy * * This routine will destroy an ATAV object.  It should eventually be * called on all ATAVs created without an arena.  While it is not  * necessary to call it on ATAVs created within an arena, it is not an * error to do so.  This routine returns a PRStatus value; if * successful, it will return PR_SUCCESS.  If unsuccessful, it will * set an error on the error stack and return PR_FAILURE. * * The error may be one of the following values: *  NSS_ERROR_INVALID_ATAV *   * Return value: *  PR_FAILURE upon error *  PR_SUCCESS upon success */NSS_IMPLEMENT PRStatusnssATAV_Destroy(  NSSATAV *atav){#ifdef NSSDEBUG  if( PR_SUCCESS != nssATAV_verifyPointer(atav) ) {    return PR_FAILURE;  }#endif /* NSSDEBUG */  (void)nss_ZFreeIf(atav->ber.data);  (void)nss_ZFreeIf(atav->value);#ifdef DEBUG  if( PR_SUCCESS != atav_remove_pointer(atav) ) {    return PR_FAILURE;  }#endif /* DEBUG */  return PR_SUCCESS;}/* * nssATAV_GetDEREncoding * * This routine will DER-encode an ATAV object. If the optional arena * argument is non-null, the memory used will be obtained from that * arena; otherwise, the memory will be obtained from the heap.  This * routine may return null upon error, in which case it will have set * an error on the error stack. * * The error may be one of the following values: *  NSS_ERROR_INVALID_ATAV *  NSS_ERROR_NO_MEMORY * * Return value: *  NULL upon error *  The DER encoding of this NSSATAV */NSS_IMPLEMENT NSSDER *nssATAV_GetDEREncoding(  NSSATAV *atav,  NSSArena *arenaOpt){  NSSDER *rv;#ifdef NSSDEBUG  if( PR_SUCCESS != nssATAV_verifyPointer(atav) ) {    return (NSSDER *)NULL;  }#endif /* NSSDEBUG */  rv = nss_ZNEW(arenaOpt, NSSDER);  if( (NSSDER *)NULL == rv ) {    return (NSSDER *)NULL;  }  rv->data = nss_ZAlloc(arenaOpt, atav->ber.size);  if( (void *)NULL == rv->data ) {    (void)nss_ZFreeIf(rv);    return (NSSDER *)NULL;  }  rv->size = atav->ber.size;  if( PR_SUCCESS != nsslibc_memcpy(rv->data, atav->ber.data,                                    rv->size) ) {    (void)nss_ZFreeIf(rv->data);    (void)nss_ZFreeIf(rv);    return (NSSDER *)NULL;  }  return rv;}/* * nssATAV_GetUTF8Encoding * * This routine returns a UTF8 string containing a string  * representation of the ATAV in "equals" notation (e.g., "o=Acme").   * If the optional arena argument is non-null, the memory used will be * obtained from that arena; otherwise, the memory will be obtained  * from the heap.  This routine may return null upon error, in which  * case it will have set an error on the error stack. * * The error may be one of the following values: *  NSS_ERROR_INVALID_ATAV *  NSS_ERROR_NO_MEMORY * * Return value: *  NULL upon error *  A pointer to a UTF8 string containing the "equals" encoding of the  *      ATAV */NSS_IMPLEMENT NSSUTF8 *nssATAV_GetUTF8Encoding(  NSSATAV *atav,  NSSArena *arenaOpt){  NSSUTF8 *rv;  PRUint32 i;  const NSSUTF8 *alias = (NSSUTF8 *)NULL;  NSSUTF8 *oid;  NSSUTF8 *value;  PRUint32 oidlen;  PRUint32 valuelen;  PRUint32 totallen;#ifdef NSSDEBUG  if( PR_SUCCESS != nssATAV_verifyPointer(atav) ) {    return (NSSUTF8 *)NULL;  }#endif /* NSSDEBUG */  for( i = 0; i < nss_attribute_type_alias_count; i++ ) {    if( *(nss_attribute_type_aliases[i].oid) == atav->oid ) {      alias = nss_attribute_type_aliases[i].alias;      break;    }  }  if( (NSSUTF8 *)NULL == alias ) {    oid = nssOID_GetUTF8Encoding(atav->oid, (NSSArena *)NULL);    if( (NSSUTF8 *)NULL == oid ) {      return (NSSUTF8 *)NULL;    }    if( PR_SUCCESS != nssUTF8_Size(oid, &oidlen) ) {      (void)nss_ZFreeIf(oid);      return (NSSUTF8 *)NULL;    }  } else {    if( PR_SUCCESS != nssUTF8_Size(alias, &oidlen) ) {      return (NSSUTF8 *)NULL;    }    oid = (NSSUTF8 *)NULL;  }  value = nssATAV_GetValue(atav, (NSSArena *)NULL);  if( (NSSUTF8 *)NULL == value ) {    (void)nss_ZFreeIf(oid);    return (NSSUTF8 *)NULL;  }  if( PR_SUCCESS != nssUTF8_Size(value, &valuelen) ) {    (void)nss_ZFreeIf(value);    (void)nss_ZFreeIf(oid);    return (NSSUTF8 *)NULL;  }  totallen = oidlen + valuelen - 1 + 1;  rv = (NSSUTF8 *)nss_ZAlloc(arenaOpt, totallen);  if( (NSSUTF8 *)NULL == rv ) {    (void)nss_ZFreeIf(value);    (void)nss_ZFreeIf(oid);    return (NSSUTF8 *)NULL;  }  if( (NSSUTF8 *)NULL == alias ) {    if( (void *)NULL == nsslibc_memcpy(rv, oid, oidlen-1) ) {      (void)nss_ZFreeIf(rv);      (void)nss_ZFreeIf(value);      (void)nss_ZFreeIf(oid);      return (NSSUTF8 *)NULL;    }  } else {    if( (void *)NULL == nsslibc_memcpy(rv, alias, oidlen-1) ) {      (void)nss_ZFreeIf(rv);      (void)nss_ZFreeIf(value);      return (NSSUTF8 *)NULL;    }  }  rv[ oidlen-1 ] = '=';  if( (void *)NULL == nsslibc_memcpy(&rv[oidlen], value, valuelen) ) {    (void)nss_ZFreeIf(rv);    (void)nss_ZFreeIf(value);    (void)nss_ZFreeIf(oid);    return (NSSUTF8 *)NULL;  }  return rv;}/* * nssATAV_GetType * * This routine returns the NSSOID corresponding to the attribute type * in the specified ATAV.  This routine may return NSS_OID_UNKNOWN  * upon error, in which case it will have set an error on the error * stack. * * The error may be one of the following values: *  NSS_ERROR_INVALID_ATAV * * Return value: *  NSS_OID_UNKNOWN upon error *  A valid NSSOID pointer upon success */NSS_IMPLEMENT const NSSOID *nssATAV_GetType(  NSSATAV *atav){#ifdef NSSDEBUG  if( PR_SUCCESS != nssATAV_verifyPointer(atav) ) {    return (NSSOID *)NULL;  }#endif /* NSSDEBUG */  return atav->oid;}/* * nssATAV_GetValue * * This routine returns a NSSUTF8 string containing the attribute value * in the specified ATAV.  If the optional arena argument is non-null, * the memory used will be obtained from that arena; otherwise, the * memory will be obtained from the heap.  This routine may return * NULL upon error, in which case it will have set an error upon the * error stack. * * The error may be one of the following values: *  NSS_ERROR_INVALID_ATAV *  NSS_ERROR_NO_MEMORY * * Return value: *  NULL upon error *  A pointer to an NSSItem containing the attribute value. */NSS_IMPLEMENT NSSUTF8 *nssATAV_GetValue(  NSSATAV *atav,  NSSArena *arenaOpt){#ifdef NSSDEBUG  if( PR_SUCCESS != nssATAV_verifyPointer(atav) ) {    return (NSSUTF8 *)NULL;  }#endif /* NSSDEBUG */  return nssUTF8_Duplicate(atav->value, arenaOpt);}/* * nssATAV_Compare * * This routine compares two ATAVs for equality.  For two ATAVs to be * equal, the attribute types must be the same, and the attribute  * values must have equal length and contents.  The result of the  * comparison will be stored at the location pointed to by the "equalp" * variable, which must point to a valid PRBool.  This routine may  * return PR_FAILURE upon error, in which case it will have set an  * error on the error stack. * * The error may be one of the following values: *  NSS_ERROR_INVALID_ATAV *  NSS_ERROR_INVALID_ARGUMENT * * Return value: *  PR_FAILURE on error *  PR_SUCCESS upon a successful comparison (equal or not) */NSS_IMPLEMENT PRStatusnssATAV_Compare(  NSSATAV *atav1,  NSSATAV *atav2,  PRBool *equalp){  nssStringType comparison;  PRUint32 len1;  PRUint32 len2;#ifdef DEBUG  if( PR_SUCCESS != nssATAV_verifyPointer(atav1) ) {    return PR_FAILURE;  }  if( PR_SUCCESS != nssATAV_verifyPointer(atav2) ) {    return PR_FAILURE;  }  if( (PRBool *)NULL == equalp ) {    nss_SetError(NSS_ERROR_INVALID_ARGUMENT);    return PR_FAILURE;  }#endif /* DEBUG */  if( atav1->oid != atav2->oid ) {    *equalp = PR_FALSE;    return PR_SUCCESS;  }  if( atav1->stringForm != atav2->stringForm ) {    if( (nssStringType_PrintableString == atav1->stringForm) ||        (nssStringType_PrintableString == atav2->stringForm) ) {      comparison = nssStringType_PrintableString;    } else if( (nssStringType_PHGString == atav1->stringForm) ||               (nssStringType_PHGString == atav2->stringForm) ) {      comparison = nssStringType_PHGString;    } else {      comparison = atav1->stringForm;    }  } else {    comparison = atav1->stringForm;  }  switch( comparison ) {  case nssStringType_DirectoryString:    nss_SetError(NSS_ERROR_INTERNAL_ERROR);    return PR_FAILURE;  case nssStringType_TeletexString:    break;  case nssStringType_PrintableString:    return nssUTF8_PrintableMatch(atav1->value, atav2->value,                                   equalp);    /* Case-insensitive, with whitespace reduction */    break;  case nssStringType_UniversalString:    break;  case nssStringType_BMPString:    break;  case nssStringType_UTF8String:    break;  case nssStringType_PHGString:    /* Case-insensitive (XXX fgmr, actually see draft-11 pg. 21) */    return nssUTF8_CaseIgnoreMatch(atav1->value, atav2->value,                                    equalp);  case nssStringType_Unknown:    break;  }    if( PR_SUCCESS != nssUTF8_Size(atav1->value, &len1) ) {    return PR_FAILURE;  }  if( PR_SUCCESS != nssUTF8_Size(atav2->value, &len2) ) {    return PR_FAILURE;  }  if( len1 != len2 ) {    *equalp = PR_FALSE;    return PR_SUCCESS;  }  return nsslibc_compare(atav1->value, atav2->value, len1, equalp);}/* * nssATAV_Duplicate * * This routine duplicates the specified ATAV.  If the optional arena  * argument is non-null, the memory required will be obtained from * that arena; otherwise, the memory will be obtained from the heap.   * This routine may return NULL upon error, in which case it will have  * placed an error on the error stack. * * The error may be one of the following values: *  NSS_ERROR_INVALID_ATAV *  NSS_ERROR_NO_MEMORY * * Return value: *  NULL on error *  A pointer to a new ATAV */NSS_IMPLEMENT NSSATAV *nssATAV_Duplicate(  NSSATAV *atav,  NSSArena *arenaOpt){  NSSATAV *rv;#ifdef NSSDEBUG  if( PR_SUCCESS != nssATAV_verifyPointer(atav) ) {    return (NSSATAV *)NULL;  }  if( (NSSArena *)NULL != arenaOpt ) {    if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) {      return (NSSATAV *)NULL;    }  }#endif /* NSSDEBUG */  rv = nss_ZNEW(arenaOpt, NSSATAV);  if( (NSSATAV *)NULL == rv ) {    return (NSSATAV *)NULL;  }  rv->oid = atav->oid;  rv->stringForm = atav->stringForm;  rv->value = nssUTF8_Duplicate(atav->value, arenaOpt);  if( (NSSUTF8 *)NULL == rv->value ) {    (void)nss_ZFreeIf(rv);    return (NSSATAV *)NULL;  }  rv->ber.data = nss_ZAlloc(arenaOpt, atav->ber.size);  if( (void *)NULL == rv->ber.data ) {    (void)nss_ZFreeIf(rv->value);    (void)nss_ZFreeIf(rv);    return (NSSATAV *)NULL;  }  rv->ber.size = atav->ber.size;  if( PR_SUCCESS != nsslibc_memcpy(rv->ber.data, atav->ber.data,                                    atav->ber.size) ) {    (void)nss_ZFreeIf(rv->ber.data);    (void)nss_ZFreeIf(rv->value);    (void)nss_ZFreeIf(rv);    return (NSSATAV *)NULL;  }  return rv;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?