diff --git a/cyassl/ssl.h b/cyassl/ssl.h index 5c759219d..308f776ff 100644 --- a/cyassl/ssl.h +++ b/cyassl/ssl.h @@ -784,12 +784,12 @@ CYASSL_API int CyaSSL_cmp_peer_cert_to_file(CYASSL*, const char*); CYASSL_API char* CyaSSL_X509_get_next_altname(CYASSL_X509*); #ifdef CYASSL_SEP - CYASSL_API - int CyaSSL_X509_get_device_type(CYASSL_X509*, unsigned char*, int*); - CYASSL_API - int CyaSSL_X509_get_hw_type(CYASSL_X509*, unsigned char*, int*); - CYASSL_API - int CyaSSL_X509_get_hw_serial_number(CYASSL_X509*, unsigned char*, int*); + CYASSL_API unsigned char* + CyaSSL_X509_get_device_type(CYASSL_X509*, unsigned char*, int*); + CYASSL_API unsigned char* + CyaSSL_X509_get_hw_type(CYASSL_X509*, unsigned char*, int*); + CYASSL_API unsigned char* + CyaSSL_X509_get_hw_serial_number(CYASSL_X509*, unsigned char*, int*); #endif /* connect enough to get peer cert */ diff --git a/src/ssl.c b/src/ssl.c index 089fe2297..efdb88e21 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -7013,42 +7013,75 @@ int CyaSSL_set_compression(CYASSL* ssl) #ifdef CYASSL_SEP -int CyaSSL_X509_get_device_type(CYASSL_X509* x509, byte* in, int *inOutSz) +/* copy oid into in buffer, at most *inOutSz bytes, if buffer is null will + malloc buffer, call responsible for freeing. Actual size returned in + *inOutSz. Requires inOutSz be non-null */ +byte* CyaSSL_X509_get_device_type(CYASSL_X509* x509, byte* in, int *inOutSz) { + int copySz; + CYASSL_ENTER("CyaSSL_X509_get_dev_type"); - if (x509 == NULL || inOutSz == NULL || *inOutSz < x509->deviceTypeSz) - return BAD_FUNC_ARG; + if (inOutSz == NULL) return NULL; + if (!x509->deviceTypeSz) return in; - XMEMCPY(in, x509->deviceType, x509->deviceTypeSz); - *inOutSz = x509->deviceTypeSz; + copySz = min(*inOutSz, x509->deviceTypeSz); - return SSL_SUCCESS; + if (!in) { + in = (byte*)XMALLOC(x509->deviceTypeSz, 0, DYNAMIC_TYPE_OPENSSL); + if (!in) return in; + copySz = x509->deviceTypeSz; + } + + XMEMCPY(in, x509->deviceType, copySz); + *inOutSz = copySz; + + return in; } -int CyaSSL_X509_get_hw_type(CYASSL_X509* x509, byte* in, int *inOutSz) +byte* CyaSSL_X509_get_hw_type(CYASSL_X509* x509, byte* in, int* inOutSz) { + int copySz; + CYASSL_ENTER("CyaSSL_X509_get_hw_type"); - if (x509 == NULL || inOutSz == NULL || *inOutSz < x509->hwTypeSz) - return BAD_FUNC_ARG; + if (inOutSz == NULL) return NULL; + if (!x509->hwTypeSz) return in; - XMEMCPY(in, x509->hwType, x509->hwTypeSz); - *inOutSz = x509->hwTypeSz; + copySz = min(*inOutSz, x509->hwTypeSz); - return SSL_SUCCESS; + if (!in) { + in = (byte*)XMALLOC(x509->hwTypeSz, 0, DYNAMIC_TYPE_OPENSSL); + if (!in) return in; + copySz = x509->hwTypeSz; + } + + XMEMCPY(in, x509->hwType, copySz); + *inOutSz = copySz; + + return in; } -int CyaSSL_X509_get_hw_serial_number(CYASSL_X509* x509, byte* in, int *inOutSz) +byte* CyaSSL_X509_get_hw_serial_number(CYASSL_X509* x509,byte* in,int* inOutSz) { + int copySz; + CYASSL_ENTER("CyaSSL_X509_get_hw_serial_number"); - if (x509 == NULL || inOutSz == NULL || *inOutSz < x509->hwSerialNumSz) - return BAD_FUNC_ARG; + if (inOutSz == NULL) return NULL; + if (!x509->hwTypeSz) return in; - XMEMCPY(in, x509->hwSerialNum, x509->hwSerialNumSz); - *inOutSz = x509->hwSerialNumSz; + copySz = min(*inOutSz, x509->hwSerialNumSz); - return SSL_SUCCESS; + if (!in) { + in = (byte*)XMALLOC(x509->hwSerialNumSz, 0, DYNAMIC_TYPE_OPENSSL); + if (!in) return in; + copySz = x509->hwSerialNumSz; + } + + XMEMCPY(in, x509->hwSerialNum, copySz); + *inOutSz = copySz; + + return in; } #endif /* CYASSL_SEP */