asn1.c
来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 1,727 行 · 第 1/3 页
C
1,727 行
if( (void **)NULL != pArgumentOpt ) { *pArgumentOpt = (void *)NULL; } /* Error because it's unimplemented */ nss_SetError(NSS_ERROR_INTERNAL_ERROR); return PR_FAILURE;}/* * nssASN1_Decode * * This routine will decode the specified data into the specified * destination structure, as specified by the specified template. * This routine returns a PRStatus value; in the event of an error * it will place an error on the error stack and return PR_FAILURE. * * The error may be one of the following values: * NSS_ERROR_NO_MEMORY * NSS_ERROR_INVALID_ARENA * NSS_ERROR_INVALID_POINTER * NSS_ERROR_INVALID_BER * * Return value: * PR_FAILURE upon error * PR_SUCCESS upon success */NSS_IMPLEMENT PRStatusnssASN1_Decode( NSSArena *arenaOpt, void *destination, const nssASN1Template template[], const void *berData, PRUint32 amount){ PRStatus rv; nssASN1Decoder *decoder; /* This call will do our pointer-checking for us! */ decoder = nssASN1Decoder_Create(arenaOpt, destination, template); if( (nssASN1Decoder *)NULL == decoder ) { return PR_FAILURE; } rv = nssASN1Decoder_Update(decoder, berData, amount); if( PR_SUCCESS != nssASN1Decoder_Finish(decoder) ) { rv = PR_FAILURE; } return rv;}/* * nssASN1_DecodeBER * * This routine will decode the data in the specified NSSBER * into the destination structure, as specified by the template. * This routine returns a PRStatus value; in the event of an error * it will place an error on the error stack and return PR_FAILURE. * * The error may be one of the following values: * NSS_ERROR_NO_MEMORY * NSS_ERROR_INVALID_ARENA * NSS_ERROR_INVALID_POINTER * NSS_ERROR_INVALID_NSSBER * NSS_ERROR_INVALID_BER * * Return value: * PR_FAILURE upon error * PR_SUCCESS upon success */NSS_IMPLEMENT PRStatusnssASN1_DecodeBER( NSSArena *arenaOpt, void *destination, const nssASN1Template template[], const NSSBER *data){ return nssASN1_Decode(arenaOpt, destination, template, data->data, data->size);}/* * nssASN1Encoder_Create * * This routine creates an ASN.1 Encoder, blah blah blah. This * may return NULL upon error, in which case an error will have been * placed on the error stack. * * The error may be one of the following values: * NSS_ERROR_NO_MEMORY * NSS_ERROR_INVALID_ARENA * NSS_ERROR_INVALID_POINTER * ... * * Return value: * NULL upon error * A pointer to an ASN.1 Encoder upon success */NSS_IMPLEMENT nssASN1Encoder *nssASN1Encoder_Create( const void *source, const nssASN1Template template[], NSSASN1EncodingType encoding, nssASN1EncoderWriteFunction *sink, void *argument){ SEC_ASN1EncoderContext *rv;#ifdef DEBUG if( (void *)NULL == source ) { nss_SetError(NSS_ERROR_INVALID_POINTER); return (nssASN1Encoder *)NULL; } if( (nssASN1Template *)NULL == template ) { nss_SetError(NSS_ERROR_INVALID_POINTER); return (nssASN1Encoder *)NULL; } if( (nssASN1EncoderWriteFunction *)NULL == sink ) { nss_SetError(NSS_ERROR_INVALID_POINTER); return (nssASN1Encoder *)NULL; }#endif /* DEBUG */ switch( encoding ) { case NSSASN1BER: case NSSASN1DER: break; case NSSASN1CER: case NSSASN1LWER: case NSSASN1PER: case NSSASN1UnknownEncoding: default: nss_SetError(NSS_ERROR_ENCODING_NOT_SUPPORTED); return (nssASN1Encoder *)NULL; } rv = SEC_ASN1EncoderStart((void *)source, template, (SEC_ASN1WriteProc)sink, argument); if( (SEC_ASN1EncoderContext *)NULL == rv ) { nss_SetError(PORT_GetError()); /* ugly */ return (nssASN1Encoder *)NULL; } if( NSSASN1DER == encoding ) { sec_ASN1EncoderSetDER(rv); }#ifdef DEBUG if( PR_SUCCESS != encoder_add_pointer(rv) ) { (void)SEC_ASN1EncoderFinish(rv); return (nssASN1Encoder *)NULL; }#endif /* DEBUG */ return (nssASN1Encoder *)rv;}/* * nssASN1Encoder_Update * * The error may be one of the following values: * NSS_ERROR_INVALID_ASN1ENCODER * NSS_ERROR_INVALID_POINTER * * Return value: * PR_FAILURE upon error * PR_SUCCESS upon success */NSS_IMPLEMENT PRStatusnssASN1Encoder_Update( nssASN1Encoder *encoder, const void *data, PRUint32 length){ PRStatus rv;#ifdef DEBUG if( PR_SUCCESS != nssASN1Encoder_verify(encoder) ) { return PR_FAILURE; } /* * Can data legitimately be NULL? If not, verify.. */#endif /* DEBUG */ rv = SEC_ASN1EncoderUpdate((SEC_ASN1EncoderContext *)encoder, (const char *)data, (unsigned long)length); if( PR_SUCCESS != rv ) { nss_SetError(PORT_GetError()); /* ugly */ return PR_FAILURE; } return PR_SUCCESS;}/* * nssASN1Encoder_Finish * * Destructor. * * The error may be one of the following values: * NSS_ERROR_INVALID_ASN1ENCODER * * Return value: * PR_FAILURE upon error * PR_SUCCESS upon success */NSS_IMPLEMENT PRStatusnssASN1Encoder_Finish( nssASN1Encoder *encoder){ PRStatus rv;#ifdef DEBUG if( PR_SUCCESS != nssASN1Encoder_verify(encoder) ) { return PR_FAILURE; }#endif /* DEBUG */ SEC_ASN1EncoderFinish((SEC_ASN1EncoderContext *)encoder); rv = PR_SUCCESS; /* no error return defined for that call */#ifdef DEBUG { PRStatus rv2 = encoder_remove_pointer(encoder); if( PR_SUCCESS == rv ) { rv = rv2; } }#endif /* DEBUG */ return rv;}/* * nssASN1Encoder_SetNotify * * This routine registers a callback notify routine with the encoder, * which will be called whenever.. The specified argument will be * passed as-is to the notify routine. The routine pointer may be * NULL, in which case no notify routine will be called. This routine * returns a PRStatus value; in the event of an error it will place * an error on the error stack and return PR_FAILURE. * * The error may be one of the following values: * NSS_ERROR_INVALID_ASN1DECODER * * Return value: * PR_FAILURE upon error * PR_SUCCESS upon success */NSS_IMPLEMENT PRStatusnssASN1Encoder_SetNotify( nssASN1Encoder *encoder, nssASN1NotifyFunction *callback, void *argument){#ifdef DEBUG if( PR_SUCCESS != nssASN1Encoder_verify(encoder) ) { return PR_FAILURE; }#endif /* DEBUG */ if( (nssASN1NotifyFunction *)NULL == callback ) { SEC_ASN1EncoderClearNotifyProc((SEC_ASN1EncoderContext *)encoder); } else { SEC_ASN1EncoderSetNotifyProc((SEC_ASN1EncoderContext *)encoder, (SEC_ASN1NotifyProc)callback, argument); } /* no error return defined for those routines */ return PR_SUCCESS;}/* * nssASN1Encoder_GetNotify * * If the optional pCallbackOpt argument to this routine is non-null, * then the pointer to any callback function established for this * decoder with nssASN1Encoder_SetNotify will be stored at the * location indicated by it. If the optional pArgumentOpt pointer is * non-null, the filter's closure argument will be stored there. * This routine returns a PRStatus value; in the event of an error it * will place an error on the error stack and return PR_FAILURE. * * The error may be one of the following values: * NSS_ERROR_INVALID_ASN1ENCODER * * Return value: * PR_FAILURE upon error * PR_SUCCESS upon success */NSS_IMPLEMENT PRStatusnssASN1Encoder_GetNotify( nssASN1Encoder *encoder, nssASN1NotifyFunction **pCallbackOpt, void **pArgumentOpt){#ifdef DEBUG if( PR_SUCCESS != nssASN1Encoder_verify(encoder) ) { return PR_FAILURE; }#endif /* DEBUG */ if( (nssASN1NotifyFunction **)NULL != pCallbackOpt ) { *pCallbackOpt = (nssASN1NotifyFunction *)NULL; } if( (void **)NULL != pArgumentOpt ) { *pArgumentOpt = (void *)NULL; } /* Error because it's unimplemented */ nss_SetError(NSS_ERROR_INTERNAL_ERROR); return PR_FAILURE;}/* * nssASN1Encoder_SetStreaming * * * The error may be one of the following values: * NSS_ERROR_INVALID_ASN1ENCODER * * Return value: * PR_FAILURE upon error * PR_SUCCESS upon success */NSS_IMPLEMENT PRStatusnssASN1Encoder_SetStreaming( nssASN1Encoder *encoder, PRBool streaming){ SEC_ASN1EncoderContext *cx = (SEC_ASN1EncoderContext *)encoder;#ifdef DEBUG if( PR_SUCCESS != nssASN1Encoder_verify(encoder) ) { return PR_FAILURE; }#endif /* DEBUG */ if( streaming ) { SEC_ASN1EncoderSetStreaming(cx); } else { SEC_ASN1EncoderClearStreaming(cx); } /* no error return defined for those routines */ return PR_SUCCESS;}/* * nssASN1Encoder_GetStreaming * * * The error may be one of the following values: * NSS_ERROR_INVALID_ASN1ENCODER * NSS_ERROR_INVALID_POINTER * * Return value: * PR_FAILURE upon error * PR_SUCCESS upon success */NSS_EXTERN PRStatusnssASN1Encoder_GetStreaming( nssASN1Encoder *encoder, PRBool *pStreaming){#ifdef DEBUG if( PR_SUCCESS != nssASN1Encoder_verify(encoder) ) { return PR_FAILURE; }#endif /* DEBUG */ if( (PRBool *)NULL != pStreaming ) { *pStreaming = PR_FALSE; } /* Error because it's unimplemented */ nss_SetError(NSS_ERROR_INTERNAL_ERROR); return PR_FAILURE;}/* * nssASN1Encoder_SetTakeFromBuffer * * * The error may be one of the following values: * NSS_ERROR_INVALID_ASN1ENCODER * * Return value: * PR_FAILURE upon error * PR_SUCCESS upon success */NSS_IMPLEMENT PRStatusnssASN1Encoder_SetTakeFromBuffer( nssASN1Encoder *encoder, PRBool takeFromBuffer){ SEC_ASN1EncoderContext *cx = (SEC_ASN1EncoderContext *)encoder;#ifdef DEBUG if( PR_SUCCESS != nssASN1Encoder_verify(encoder) ) { return PR_FAILURE; }#endif /* DEBUG */ if( takeFromBuffer ) { SEC_ASN1EncoderSetTakeFromBuf(cx); } else { SEC_ASN1EncoderClearTakeFromBuf(cx); } /* no error return defined for those routines */ return PR_SUCCESS;}/* * nssASN1Encoder_GetTakeFromBuffer * * * The error may be one of the following values: * NSS_ERROR_INVALID_ASN1ENCODER * NSS_ERROR_INVALID_POINTER * * Return value: * PR_FAILURE upon error * PR_SUCCESS upon success */NSS_IMPLEMENT PRStatusnssASN1Encoder_GetTakeFromBuffer( nssASN1Encoder *encoder, PRBool *pTakeFromBuffer){#ifdef DEBUG if( PR_SUCCESS != nssASN1Encoder_verify(encoder) ) { return PR_FAILURE; }#endif /* DEBUG */ if( (PRBool *)NULL != pTakeFromBuffer ) { *pTakeFromBuffer = PR_FALSE; } /* Error because it's unimplemented */ nss_SetError(NSS_ERROR_INTERNAL_ERROR); return PR_FAILURE;}/* * nssASN1_Encode * * * The error may be one of the following values: * NSS_ERROR_NO_MEMORY * NSS_ERROR_INVALID_ARENA * NSS_ERROR_INVALID_POINTER * NSS_ERROR_ENCODING_NOT_SUPPORTED * ... * * Return value: * PR_FAILURE upon error * PR_SUCCESS upon success */NSS_IMPLEMENT PRStatusnssASN1_Encode( const void *source, const nssASN1Template template[], NSSASN1EncodingType encoding, nssASN1EncoderWriteFunction *sink, void *argument){ PRStatus rv; nssASN1Encoder *encoder; encoder = nssASN1Encoder_Create(source, template, encoding, sink, argument); if( (nssASN1Encoder *)NULL == encoder ) { return PR_FAILURE; } rv = nssASN1Encoder_Update(encoder, (const void *)NULL, 0); if( PR_SUCCESS != nssASN1Encoder_Finish(encoder) ) { rv = PR_FAILURE; } return rv;}/* * nssasn1_encode_item_count * * This is a helper function for nssASN1_EncodeItem. It just counts * up the space required for an encoding. */static voidnssasn1_encode_item_count( void *arg, const char *buf, unsigned long len, int depth, nssASN1EncodingPart data_kind){ unsigned long *count; count = (unsigned long*)arg; PR_ASSERT (count != NULL); *count += len;}/* * nssasn1_encode_item_store * * This is a helper function for nssASN1_EncodeItem. It appends the * new data onto the destination item. */static voidnssasn1_encode_item_store( void *arg, const char *buf, unsigned long len, int depth, nssASN1EncodingPart data_kind){ NSSItem *dest;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?