Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2012 Werner Dittmann |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | #include <stdint.h> |
| 20 | |
| 21 | class CryptoContext; |
| 22 | class CryptoContextCtrl; |
| 23 | |
| 24 | /** |
| 25 | * @brief SRTP and SRTCP protect and unprotect functions. |
| 26 | * |
| 27 | * The static methods take SRTP or SRTCP crypto contexts, a pointer uint8_t buffer |
| 28 | * that must contain an RTP/SRTP packet and perform the actions necessary to protect |
| 29 | * the RTP/RTCP packet or to unprotect the SRTP/SRTCP packet. |
| 30 | * |
| 31 | * The methods assume that the buffer contains all protocol relevant fields (SSRC, |
| 32 | * sequence number etc.) in network order. |
| 33 | * |
| 34 | * When encrypting the buffer must be big enough to store additional data, usually |
| 35 | * 4 - 14 bytes, depending on how the application configured the authentication parameters. |
| 36 | * |
| 37 | * @author Werner Dittmann <Werner.Dittmann@t-online.de> |
| 38 | */ |
| 39 | class SrtpHandler |
| 40 | { |
| 41 | public: |
| 42 | /** |
| 43 | * @brief Protect an RTP packet. |
| 44 | * |
| 45 | * @param pcc the SRTP CryptoContext instance |
| 46 | * |
| 47 | * @param buffer the RTP packet to protect |
| 48 | * |
| 49 | * @param length the length of the RTP packet data in bytes |
| 50 | * |
| 51 | * @param newLength the length of the resulting SRTP packet data in bytes |
| 52 | * |
| 53 | * @return @c true if protection was successful, @c false otherwise |
| 54 | */ |
| 55 | static bool protect(CryptoContext* pcc, uint8_t* buffer, size_t length, size_t* newLength); |
| 56 | |
| 57 | /** |
| 58 | * @brief Unprotect a SRTP packet. |
| 59 | * |
| 60 | * @param pcc the SRTP CryptoContext instance |
| 61 | * |
| 62 | * @param buffer the SRTP packet to unprotect |
| 63 | * |
| 64 | * @param length the length of the SRTP packet data in bytes |
| 65 | * |
| 66 | * @param newLength the length of the resulting RTP packet data in bytes |
| 67 | * |
| 68 | * @return an integer value |
| 69 | * - 1 - success |
| 70 | * - -1 - SRTP authentication failed |
| 71 | * - -2 - SRTP replay check failed |
| 72 | */ |
| 73 | static int32_t unprotect(CryptoContext* pcc, uint8_t* buffer, size_t length, size_t* newLength); |
| 74 | |
| 75 | /** |
| 76 | * @brief Protect an RTCP packet. |
| 77 | * |
| 78 | * @param pcc the SRTCP CryptoContextCtrl instance |
| 79 | * |
| 80 | * @param buffer the RTCP packet to protect |
| 81 | * |
| 82 | * @param length the length of the RTCP packet data in bytes |
| 83 | * |
| 84 | * @param newLength the length of the resulting SRTCP packet data in bytes |
| 85 | * |
| 86 | * @return @c true if protection was successful, @c false otherwise |
| 87 | */ |
| 88 | static bool protectCtrl(CryptoContextCtrl* pcc, uint8_t* buffer, size_t length, size_t* newLength); |
| 89 | |
| 90 | /** |
| 91 | * @brief Unprotect a SRTCP packet. |
| 92 | * |
| 93 | * @param pcc the SRTCP CryptoContextCtrl instance |
| 94 | * |
| 95 | * @param buffer the SRTCP packet to unprotect |
| 96 | * |
| 97 | * @param length the length of the SRTCP packet data in bytes |
| 98 | * |
| 99 | * @param newLength the length of the resulting RTCP packet data in bytes |
| 100 | * |
| 101 | * @return an integer value |
| 102 | * - 0 - illegal packet (too short, not a valid RTP header byte), dismiss it |
| 103 | * - 1 - success |
| 104 | * - -1 - SRTCP authentication failed |
| 105 | * - -2 - SRTCP replay check failed |
| 106 | */ |
| 107 | static int32_t unprotectCtrl(CryptoContextCtrl* pcc, uint8_t* buffer, size_t length, size_t* newLength); |
| 108 | |
| 109 | private: |
| 110 | static bool decodeRtp(uint8_t* buffer, int32_t length, uint32_t *ssrc, uint16_t *seq, uint8_t** payload, int32_t *payloadlen); |
| 111 | |
| 112 | }; |