pjsip / pjproject

PJSIP project
http://www.pjsip.org
GNU General Public License v2.0
2.02k stars 770 forks source link

Support SHA-256 #2734

Closed SebaLedesma closed 3 years ago

SebaLedesma commented 3 years ago

Linphone its ussing SHA-256 as primary algorithm for validation. Sooner or later more sip servers will use it and in the long term MD5 will be deprecated.

As PJSIP already can integrate OpenSSL, it can be used to add SHA256 support.

Here it's a quick implementation: pjproject/pjsip/include/pjsip/sip_auth_parser.h from: pjsip_MD5_STR, /< "md5" string const. */ pjsip_AUTH_STR; /*< "auth" string const. / to: pjsip_MD5_STR, /< "md5" string const. */ pjsip_SHA256_STR, /*< "SHA-256" string const. / pjsip_AUTH_STR; /*< "auth" string const. /

pjproject/pjsip/src/pjsip/sip_auth_parser.c from: pjsip_MD5_STR = { "md5", 3 }, pjsip_QUOTED_MD5_STR = { "\"md5\"", 5}, pjsip_AUTH_STR = { "auth", 4}, to: pjsip_MD5_STR = { "md5", 3 }, pjsip_QUOTED_MD5_STR = { "\"md5\"", 5}, pjsip_SHA256_STR = { "SHA-256", 7}, pjsip_QUOTED_SHA256_STR = { "\"SHA-256\"", 9}, pjsip_AUTH_STR = { "auth", 4},

sip_auth_client.c: include <openssl/sha.h>

/ Check algorithm is supported. We support MD5 and AKAv1-MD5. / if (chal->algorithm.slen==0 || (pj_stricmp(&chal->algorithm, &pjsip_MD5_STR)==0 || pj_stricmp(&chal->algorithm, &pjsip_AKAv1_MD5_STR)==0))

/ Check algorithm is supported. We support MD5 and AKAv1-MD5. / if (chal->algorithm.slen==0 || (pj_stricmp(&chal->algorithm, &pjsip_MD5_STR)==0 || pj_stricmp(&chal->algorithm, &pjsip_AKAv1_MD5_STR)==0

if defined OPEN_SSL //or "openssl_h_present"

|| pj_stricmp(&chal->algorithm, &pjsip_SHA256_STR)==0

endif

 ))

... / Allocate memory. / cred->response.ptr = (char) pj_pool_alloc(pool, PJSIP_MD5STRLEN); cred->response.slen = PJSIP_MD5STRLEN; to: / Allocate memory. / if (pj_stricmp(&chal->algorithm, &pjsip_SHA256_STR)==0) { cred->response.ptr = (char) pj_pool_alloc(pool, PJSIP_SHA256STRLEN); cred->response.slen = PJSIP_SHA256STRLEN; } else { cred->response.ptr = (char*) pj_pool_alloc(pool, PJSIP_MD5STRLEN); cred->response.slen = PJSIP_MD5STRLEN; }

/*

silentindark commented 3 years ago

@shaderdyn Could you please provide a .diff (or .patch) file for test purpose?

SebaLedesma commented 3 years ago

I will try to create a diff or upload the whole file,

SebaLedesma commented 3 years ago

Here it's a diff to support SHA-256. Requires OpenSSL.

(edit: removed attachment as a newer is in the following post).

SebaLedesma commented 3 years ago

This version corrects a silly bug in the previous version. Also it adds macro control for those who dont have OpenSSL installed ( #if PJ_SSL_SOCK_IMP==PJ_SSL_SOCK_IMP_OPENSSL ... #endif)

pjproject-Support SHA-256-diff.zip

silentindark commented 3 years ago

Thank you so much, I will try to test your patch.

silentindark commented 3 years ago

@sauwming Hello, what you think about this patch?

SebaLedesma commented 3 years ago

Please note that the function digestNtoStr can also replace the original diges2toStr and so the code gets smaller. It will require to update pjsip_auth_create_digest to call it and so we can remove the original digest2toStr.

sauwming commented 3 years ago

Would you be able to create a pull request for this so we can review it?

SebaLedesma commented 3 years ago

Working on it.

SebaLedesma commented 3 years ago

@sauwming I've created a fork https://github.com/SebaLedesma/pjproject where I've commited the changes. I still have to know how to create a pull request.

SebaLedesma commented 3 years ago

Done! See https://github.com/pjsip/pjproject/pull/2753