winlin

add __openssl_compute_key to calc the shared key

@@ -129,6 +129,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -129,6 +129,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
129 #define ERROR_OpenSslSha256Final 2035 129 #define ERROR_OpenSslSha256Final 2035
130 #define ERROR_OpenSslSha256EvpDigest 2036 130 #define ERROR_OpenSslSha256EvpDigest 2036
131 #define ERROR_OpenSslSha256DigestSize 2037 131 #define ERROR_OpenSslSha256DigestSize 2037
  132 +#define ERROR_OpenSslGetPeerPublicKey 2038
  133 +#define ERROR_OpenSslComputeSharedKey 2039
132 // 134 //
133 // system control message, 135 // system control message,
134 // not an error, but special control logic. 136 // not an error, but special control logic.
@@ -202,6 +202,37 @@ namespace _srs_internal @@ -202,6 +202,37 @@ namespace _srs_internal
202 202
203 return ret; 203 return ret;
204 } 204 }
  205 + int __openssl_compute_key(DH* pdh, const char* peer_pub_key, int ppk_size, char* secret)
  206 + {
  207 + int ret = ERROR_SUCCESS;
  208 +
  209 + int32_t bits_count = 1024;
  210 +
  211 + // 2. generate the g, p, private/public key.
  212 + if ((ret = __openssl_initialize_dh(pdh, bits_count)) != ERROR_SUCCESS) {
  213 + return ret;
  214 + }
  215 +
  216 + // copy public key to bytes.
  217 + srs_assert(BN_num_bytes(pdh->pub_key) == ppk_size);
  218 +
  219 + BIGNUM* ppk = NULL;
  220 + if ((ppk = BN_bin2bn((const unsigned char*)peer_pub_key, ppk_size, 0)) == NULL) {
  221 + ret = ERROR_OpenSslGetPeerPublicKey;
  222 + return ret;
  223 + }
  224 +
  225 + // if failed, donot return, do cleanup.
  226 + if (DH_compute_key((unsigned char*)secret, ppk, pdh) < 0) {
  227 + ret = ERROR_OpenSslComputeSharedKey;
  228 + }
  229 +
  230 + if (ppk) {
  231 + BN_free(ppk);
  232 + }
  233 +
  234 + return ret;
  235 + }
205 void __openssl_free(DH* pdh) 236 void __openssl_free(DH* pdh)
206 { 237 {
207 if (pdh != NULL) { 238 if (pdh != NULL) {