1 /*
2   mediastreamer2 library - modular sound and video processing and streaming
3   Copyright (C) 2006-2014 Belledonne Communications, Grenoble
4 
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9 
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14 
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19 
20 #ifndef ms_srtp_h
21 #define ms_srtp_h
22 
23 #include <ortp/rtpsession.h>
24 #include "mediastreamer2/mscommon.h"
25 
26 #ifdef __cplusplus
27 extern "C"{
28 #endif
29 /* defined in mediastream.h */
30 #ifndef MS_MEDIA_STREAM_SESSIONS_DEFINED
31 typedef struct _MSMediaStreamSessions MSMediaStreamSessions;
32 #define MS_MEDIA_STREAM_SESSIONS_DEFINED 1
33 #endif
34 
35 /*
36  * Crypto suite used configure encrypted stream*/
37 typedef enum _MSCryptoSuite{
38         MS_CRYPTO_SUITE_INVALID=0,
39         MS_AES_128_SHA1_80,
40         MS_AES_128_SHA1_32,
41         MS_AES_128_NO_AUTH,
42         MS_NO_CIPHER_SHA1_80,
43         MS_AES_256_SHA1_80,
44         MS_AES_CM_256_SHA1_80,
45         MS_AES_256_SHA1_32
46 } MSCryptoSuite;
47 
48 typedef struct _MSCryptoSuiteNameParams{
49         const char *name;
50         const char *params;
51 }MSCryptoSuiteNameParams;
52 
53 MS2_PUBLIC MSCryptoSuite ms_crypto_suite_build_from_name_params(const MSCryptoSuiteNameParams *nameparams);
54 MS2_PUBLIC int ms_crypto_suite_to_name_params(MSCryptoSuite cs, MSCryptoSuiteNameParams *nameparams);
55 
56 
57 /* defined in srtp.h*/
58 typedef struct _MSSrtpCtx MSSrtpCtx;
59 
60 typedef enum {
61 	MSSRTP_RTP_STREAM,
62 	MSSRTP_RTCP_STREAM,
63 	MSSRTP_ALL_STREAMS
64 } MSSrtpStreamType;
65 /**
66  * return humanly readable string
67  * @param[in]	type
68  * @return
69  *
70  * */
71 MS2_PUBLIC const char * ms_srtp_stream_type_to_string(const MSSrtpStreamType type);
72 /**
73  * Check if SRTP is supported
74  * @return true if SRTP is supported
75  */
76 MS2_PUBLIC bool_t ms_srtp_supported(void);
77 
78 /**
79  * Set encryption requirements.
80  * srtp session might be created/deleted depending on requirement parameter and already set keys
81  * @param[in/out]	sessions	The sessions associated to the current media stream
82  * @param[in]		yesno		If yes, any incoming/outgoing rtp packets are silently discarded.
83  * until keys are provided using functions #media_stream_set_srtp_recv_key_b64 or #media_stream_set_srtp_recv_key
84  * @return	0 on success, error code otherwise
85  */
86 
87 MS2_PUBLIC int ms_media_stream_sessions_set_encryption_mandatory(MSMediaStreamSessions *sessions, bool_t yesno);
88 
89 /**
90  * Get encryption requirements.
91  * @param[in/out]	sessions	The sessions associated to the current media stream
92  * @return	TRUE if only encrypted rtp packet shall be sent/received
93  */
94 
95 MS2_PUBLIC bool_t ms_media_stream_sessions_get_encryption_mandatory(const MSMediaStreamSessions *sessions);
96 
97 /**
98  * Set srtp receiver key for the given media stream.
99  * If no srtp session exists on the stream it is created, if it already exists srtp policy is created/modified for the receiver side of the stream.
100  *
101  * @param[in/out]	sessions	The sessions associated to the current media stream
102  * @param[in]		suite		The srtp crypto suite to use
103  * @param[in]		key		Srtp master key and master salt in a base 64 NULL terminated string
104  * @return	0 on success, error code otherwise
105  */
106 MS2_PUBLIC int ms_media_stream_sessions_set_srtp_recv_key_b64(MSMediaStreamSessions *sessions, MSCryptoSuite suite, const char* key);
107 
108 /**
109  * Set srtp receiver key for the given media stream.
110  * If no srtp session exists on the stream it is created, if it already exists srtp policy is created/modified for the receiver side of the stream.
111  *
112  * @param[in/out]	sessions	The sessions associated to the current media stream
113  * @param[in]		suite		The srtp crypto suite to use
114  * @param[in]		key		Srtp master key and master salt
115  * @param[in]		key_length	key buffer length
116  * @param[in]		stream_type	Srtp suite is applied to RTP stream, RTCP stream or both
117  * @return	0 on success, error code otherwise
118  */
119 MS2_PUBLIC int ms_media_stream_sessions_set_srtp_recv_key(MSMediaStreamSessions *sessions, MSCryptoSuite suite, const char* key, size_t key_length, MSSrtpStreamType stream_type);
120 
121 /**
122  * Set srtp sender key for the given media stream.
123  * If no srtp session exists on the stream it is created, if it already exists srtp policy is created/modified for the sender side of the stream.
124  *
125  * @param[in/out]	sessions	The sessions associated to the current media stream
126  * @param[in]		suite	The srtp crypto suite to use
127  * @param[in]		key	Srtp master key and master salt in a base 64 NULL terminated string
128  * @return	0 on success, error code otherwise
129  */
130 MS2_PUBLIC int ms_media_stream_sessions_set_srtp_send_key_b64(MSMediaStreamSessions *sessions, MSCryptoSuite suite, const char* key);
131 
132 /**
133  * Set srtp sender key for the given media stream.
134  * If no srtp session exists on the stream it is created, if it already exists srtp policy is created/modified for the sender side of the stream.
135  *
136  * @param[in/out]	stream	The mediastream to operate on
137  * @param[in]		suite		The srtp crypto suite to use
138  * @param[in]		key		Srtp master key and master salt
139  * @param[in]		key_length	key buffer length
140  * @param[in]		stream_type	Srtp suite is applied to RTP stream, RTCP stream or both
141  * @return	0 on success, error code otherwise
142  */
143 MS2_PUBLIC int ms_media_stream_sessions_set_srtp_send_key(MSMediaStreamSessions *sessions, MSCryptoSuite suite, const char* key, size_t key_length, MSSrtpStreamType stream_type);
144 
145 
146 #ifdef __cplusplus
147 }
148 #endif
149 
150 #endif /* ms_srtp_h */
151