/** * Parse one X.509 certificate in DER format from a buffer and add them to a * chained list. */ int ttls_x509_crt_parse_der(ttls_x509_crt *chain, unsigned char *buf, size_t buflen) { int r; ttls_x509_crt *crt = chain, *prev = NULL; BUG_ON(!crt || !buf); while (crt->version && crt->next) { prev = crt; crt = crt->next; } /* Add new certificate on the end of the chain if needed. */ if (crt->version && !crt->next) { crt->next = kmalloc(sizeof(ttls_x509_crt), GFP_KERNEL); if (!crt->next) return TTLS_ERR_X509_ALLOC_FAILED; prev = crt; ttls_x509_crt_init(crt->next); crt = crt->next; } if ((r = x509_crt_parse_der_core(crt, buf, buflen))) { if (prev) prev->next = NULL; if (crt != chain) kfree(crt); return r; } return 0; }
/* * Parse one X.509 certificate in DER format from a buffer and add them to a * chained list */ int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf, size_t buflen ) { int ret; x509_crt *crt = chain, *prev = NULL; /* * Check for valid input */ if( crt == NULL || buf == NULL ) return( POLARSSL_ERR_X509_BAD_INPUT_DATA ); while( crt->version != 0 && crt->next != NULL ) { prev = crt; crt = crt->next; } /* * Add new certificate on the end of the chain if needed. */ if ( crt->version != 0 && crt->next == NULL) { crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) ); if( crt->next == NULL ) return( POLARSSL_ERR_X509_MALLOC_FAILED ); prev = crt; crt = crt->next; x509_crt_init( crt ); } if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 ) { if( prev ) prev->next = NULL; if( crt != chain ) polarssl_free( crt ); return( ret ); } return( 0 ); }