atav.c
来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 1,804 行 · 第 1/3 页
C
1,804 行
/* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is the Netscape security libraries. * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1994-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. */#ifdef DEBUGstatic const char CVS_ID[] = "@(#) $RCSfile: atav.c,v $ $Revision: 1.1 $ $Date: 2000/03/31 19:16:16 $ $Name: NSS_3_1_1_RTM $";#endif /* DEBUG *//* * atav.c * * This file contains the implementation of the PKIX part-1 object * AttributeTypeAndValue. */#ifndef NSSBASE_H#include "nssbase.h"#endif /* NSSBASE_H */#ifndef ASN1_H#include "asn1.h"#endif /* ASN1_H */#ifndef PKI1_H#include "pki1.h"#endif /* PKI1_H *//* * AttributeTypeAndValue * * From draft-ietf-pkix-ipki-part1-10: * * AttributeTypeAndValue ::= SEQUENCE { * type ATTRIBUTE.&id ({SupportedAttributes}), * value ATTRIBUTE.&Type ({SupportedAttributes}{@type})} * * -- ATTRIBUTE information object class specification * -- Note: This has been greatly simplified for PKIX !! * * ATTRIBUTE ::= CLASS { * &Type, * &id OBJECT IDENTIFIER UNIQUE } * WITH SYNTAX { * WITH SYNTAX &Type ID &id } * * What this means is that the "type" of the value is determined by * the value of the oid. If we hide the structure, our accessors * can (at least in debug builds) assert value semantics beyond what * the compiler can provide. Since these things are only used in * RelativeDistinguishedNames, and since RDNs always contain a SET * of these things, we don't lose anything by hiding the structure * (and its size). */struct NSSATAVStr { NSSBER ber; const NSSOID *oid; NSSUTF8 *value; nssStringType stringForm;};/* * NSSATAV * * The public "methods" regarding this "object" are: * * NSSATAV_CreateFromBER -- constructor * NSSATAV_CreateFromUTF8 -- constructor * NSSATAV_Create -- constructor * * NSSATAV_Destroy * NSSATAV_GetDEREncoding * NSSATAV_GetUTF8Encoding * NSSATAV_GetType * NSSATAV_GetValue * NSSATAV_Compare * NSSATAV_Duplicate * * The non-public "methods" regarding this "object" are: * * nssATAV_CreateFromBER -- constructor * nssATAV_CreateFromUTF8 -- constructor * nssATAV_Create -- constructor * * nssATAV_Destroy * nssATAV_GetDEREncoding * nssATAV_GetUTF8Encoding * nssATAV_GetType * nssATAV_GetValue * nssATAV_Compare * nssATAV_Duplicate * * In debug builds, the following non-public call is also available: * * nssATAV_verifyPointer *//* * NSSATAV_CreateFromBER * * This routine creates an NSSATAV by decoding a BER- or DER-encoded * 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 created an error stack. * * The error may be one of the following values: * NSS_ERROR_INVALID_BER * NSS_ERROR_NO_MEMORY * * Return value: * NULL upon error * A pointer to an NSSATAV upon success */NSS_IMPLEMENT NSSATAV *NSSATAV_CreateFromBER( NSSArena *arenaOpt, NSSBER *berATAV){ nss_ClearErrorStack();#ifdef DEBUG if( (NSSArena *)NULL != arenaOpt ) { if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) { return (NSSATAV *)NULL; } } /* * NSSBERs can be created by the user, * so no pointer-tracking can be checked. */ if( (NSSBER *)NULL == berATAV ) { nss_SetError(NSS_ERROR_INVALID_BER); return (NSSATAV *)NULL; } if( (void *)NULL == berATAV->data ) { nss_SetError(NSS_ERROR_INVALID_BER); return (NSSATAV *)NULL; }#endif /* DEBUG */ return nssATAV_CreateFromBER(arenaOpt, berATAV);}/* * NSSATAV_CreateFromUTF8 * * This routine creates an NSSATAV by decoding a UTF8 string in the * "equals" format, e.g., "c=US." 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 created an * error stack. * * The error may be one of the following values: * NSS_ERROR_UNKNOWN_ATTRIBUTE * NSS_ERROR_INVALID_STRING * NSS_ERROR_NO_MEMORY * * Return value: * NULL upon error * A pointer to an NSSATAV upon success */NSS_IMPLEMENT NSSATAV *NSSATAV_CreateFromUTF8( NSSArena *arenaOpt, NSSUTF8 *stringATAV){ nss_ClearErrorStack();#ifdef DEBUG if( (NSSArena *)NULL != arenaOpt ) { if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) { return (NSSATAV *)NULL; } } /* * NSSUTF8s can be created by the user, * so no pointer-tracking can be checked. */ if( (NSSUTF8 *)NULL == stringATAV ) { nss_SetError(NSS_ERROR_INVALID_UTF8); return (NSSATAV *)NULL; }#endif /* DEBUG */ return nssATAV_CreateFromUTF8(arenaOpt, stringATAV);}/* * 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 created an 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){ nss_ClearErrorStack();#ifdef DEBUG 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 /* DEBUG */ return nssATAV_Create(arenaOpt, oid, data, length);}/* * 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 * create an 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){ nss_ClearErrorStack();#ifdef DEBUG if( PR_SUCCESS != nssATAV_verifyPointer(atav) ) { return PR_FAILURE; }#endif /* DEBUG */ return nssATAV_Destroy(atav);}/* * 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 * created an 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){ nss_ClearErrorStack();#ifdef DEBUG if( PR_SUCCESS != nssATAV_verifyPointer(atav) ) { return (NSSDER *)NULL; } if( (NSSArena *)NULL != arenaOpt ) { if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) { return (NSSDER *)NULL; } }#endif /* DEBUG */ return nssATAV_GetDEREncoding(atav, arenaOpt);}/* * 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 created an 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){ nss_ClearErrorStack();#ifdef DEBUG if( PR_SUCCESS != nssATAV_verifyPointer(atav) ) { return (NSSUTF8 *)NULL; } if( (NSSArena *)NULL != arenaOpt ) { if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) { return (NSSUTF8 *)NULL; } }#endif /* DEBUG */ return nssATAV_GetUTF8Encoding(atav, arenaOpt);}/* * 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 created an error stack. * * The error may be one of the following values: * NSS_ERROR_INVALID_ATAV * * Return value: * NSS_OID_UNKNOWN upon error * An element of enum NSSOIDenum upon success */NSS_IMPLEMENT const NSSOID *NSSATAV_GetType( NSSATAV *atav){ nss_ClearErrorStack();#ifdef DEBUG if( PR_SUCCESS != nssATAV_verifyPointer(atav) ) { return (NSSOID *)NULL; }#endif /* DEBUG */ return nssATAV_GetType(atav);}/* * NSSATAV_GetValue * * This routine returns a 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 created an 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){ nss_ClearErrorStack();#ifdef DEBUG if( PR_SUCCESS != nssATAV_verifyPointer(atav) ) { return (NSSUTF8 *)NULL; } if( (NSSArena *)NULL != arenaOpt ) { if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) { return (NSSUTF8 *)NULL; } }#endif /* DEBUG */ return nssATAV_GetValue(atav, 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 created an * 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){ nss_ClearErrorStack();#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 */ return nssATAV_Compare(atav1, atav2, 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 * created an 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){ nss_ClearErrorStack();#ifdef DEBUG if( PR_SUCCESS != nssATAV_verifyPointer(atav) ) { return (NSSATAV *)NULL; } if( (NSSArena *)NULL != arenaOpt ) { if( PR_SUCCESS != nssArena_verifyPointer(arenaOpt) ) { return (NSSATAV *)NULL; } }#endif /* DEBUG */ return nssATAV_Duplicate(atav, arenaOpt);}/* * The pointer-tracking code */#ifdef DEBUGextern const NSSError NSS_ERROR_INTERNAL_ERROR;static nssPointerTracker atav_pointer_tracker;static PRStatusatav_add_pointer( const NSSATAV *atav){ PRStatus rv; rv = nssPointerTracker_initialize(&atav_pointer_tracker); if( PR_SUCCESS != rv ) { return rv; } rv = nssPointerTracker_add(&atav_pointer_tracker, atav); if( PR_SUCCESS != rv ) { NSSError e = NSS_GetError(); if( NSS_ERROR_NO_MEMORY != e ) { nss_SetError(NSS_ERROR_INTERNAL_ERROR); } return rv; } return PR_SUCCESS;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?