srtp.c 154 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727
  1. /*
  2. * srtp.c
  3. *
  4. * the secure real-time transport protocol
  5. *
  6. * David A. McGrew
  7. * Cisco Systems, Inc.
  8. */
  9. /*
  10. *
  11. * Copyright (c) 2001-2017, Cisco Systems, Inc.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * Neither the name of the Cisco Systems, Inc. nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  35. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  37. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. */
  44. // Leave this as the top level import. Ensures the existence of defines
  45. #include "config.h"
  46. #include "srtp_priv.h"
  47. #include "crypto_types.h"
  48. #include "err.h"
  49. #include "ekt.h" /* for SRTP Encrypted Key Transport */
  50. #include "alloc.h" /* for srtp_crypto_alloc() */
  51. #ifdef GCM
  52. #include "aes_gcm.h" /* for AES GCM mode */
  53. #endif
  54. #ifdef OPENSSL_KDF
  55. #include <openssl/kdf.h>
  56. #include "aes_icm_ext.h"
  57. #endif
  58. #include <limits.h>
  59. #ifdef HAVE_NETINET_IN_H
  60. #include <netinet/in.h>
  61. #elif defined(HAVE_WINSOCK2_H)
  62. #include <winsock2.h>
  63. #endif
  64. /* the debug module for srtp */
  65. srtp_debug_module_t mod_srtp = {
  66. 0, /* debugging is off by default */
  67. "srtp" /* printable name for module */
  68. };
  69. #define octets_in_rtp_header 12
  70. #define uint32s_in_rtp_header 3
  71. #define octets_in_rtcp_header 8
  72. #define uint32s_in_rtcp_header 2
  73. #define octets_in_rtp_extn_hdr 4
  74. static srtp_err_status_t srtp_validate_rtp_header(void *rtp_hdr,
  75. int *pkt_octet_len)
  76. {
  77. srtp_hdr_t *hdr = (srtp_hdr_t *)rtp_hdr;
  78. int rtp_header_len;
  79. if (*pkt_octet_len < octets_in_rtp_header)
  80. return srtp_err_status_bad_param;
  81. /* Check RTP header length */
  82. rtp_header_len = octets_in_rtp_header + 4 * hdr->cc;
  83. if (hdr->x == 1)
  84. rtp_header_len += octets_in_rtp_extn_hdr;
  85. if (*pkt_octet_len < rtp_header_len)
  86. return srtp_err_status_bad_param;
  87. /* Verifing profile length. */
  88. if (hdr->x == 1) {
  89. srtp_hdr_xtnd_t *xtn_hdr =
  90. (srtp_hdr_xtnd_t *)((uint32_t *)hdr + uint32s_in_rtp_header +
  91. hdr->cc);
  92. int profile_len = ntohs(xtn_hdr->length);
  93. rtp_header_len += profile_len * 4;
  94. /* profile length counts the number of 32-bit words */
  95. if (*pkt_octet_len < rtp_header_len)
  96. return srtp_err_status_bad_param;
  97. }
  98. return srtp_err_status_ok;
  99. }
  100. const char *srtp_get_version_string()
  101. {
  102. /*
  103. * Simply return the autotools generated string
  104. */
  105. return SRTP_VER_STRING;
  106. }
  107. unsigned int srtp_get_version()
  108. {
  109. unsigned int major = 0, minor = 0, micro = 0;
  110. unsigned int rv = 0;
  111. int parse_rv;
  112. /*
  113. * Parse the autotools generated version
  114. */
  115. parse_rv = sscanf(SRTP_VERSION, "%u.%u.%u", &major, &minor, &micro);
  116. if (parse_rv != 3) {
  117. /*
  118. * We're expected to parse all 3 version levels.
  119. * If not, then this must not be an official release.
  120. * Return all zeros on the version
  121. */
  122. return (0);
  123. }
  124. /*
  125. * We allow 8 bits for the major and minor, while
  126. * allowing 16 bits for the micro. 16 bits for the micro
  127. * may be beneficial for a continuous delivery model
  128. * in the future.
  129. */
  130. rv |= (major & 0xFF) << 24;
  131. rv |= (minor & 0xFF) << 16;
  132. rv |= micro & 0xFF;
  133. return rv;
  134. }
  135. srtp_err_status_t srtp_stream_dealloc(srtp_stream_ctx_t *stream,
  136. const srtp_stream_ctx_t *stream_template)
  137. {
  138. srtp_err_status_t status;
  139. unsigned int i = 0;
  140. srtp_session_keys_t *session_keys = NULL;
  141. srtp_session_keys_t *template_session_keys = NULL;
  142. /*
  143. * we use a conservative deallocation strategy - if any deallocation
  144. * fails, then we report that fact without trying to deallocate
  145. * anything else
  146. */
  147. if (stream->session_keys) {
  148. for (i = 0; i < stream->num_master_keys; i++) {
  149. session_keys = &stream->session_keys[i];
  150. if (stream_template &&
  151. stream->num_master_keys == stream_template->num_master_keys) {
  152. template_session_keys = &stream_template->session_keys[i];
  153. } else {
  154. template_session_keys = NULL;
  155. }
  156. /*
  157. * deallocate cipher, if it is not the same as that in template
  158. */
  159. if (template_session_keys &&
  160. session_keys->rtp_cipher == template_session_keys->rtp_cipher) {
  161. /* do nothing */
  162. } else if (session_keys->rtp_cipher) {
  163. status = srtp_cipher_dealloc(session_keys->rtp_cipher);
  164. if (status)
  165. return status;
  166. }
  167. /*
  168. * deallocate auth function, if it is not the same as that in
  169. * template
  170. */
  171. if (template_session_keys &&
  172. session_keys->rtp_auth == template_session_keys->rtp_auth) {
  173. /* do nothing */
  174. } else if (session_keys->rtp_auth) {
  175. status = srtp_auth_dealloc(session_keys->rtp_auth);
  176. if (status)
  177. return status;
  178. }
  179. if (template_session_keys &&
  180. session_keys->rtp_xtn_hdr_cipher ==
  181. template_session_keys->rtp_xtn_hdr_cipher) {
  182. /* do nothing */
  183. } else if (session_keys->rtp_xtn_hdr_cipher) {
  184. status = srtp_cipher_dealloc(session_keys->rtp_xtn_hdr_cipher);
  185. if (status)
  186. return status;
  187. }
  188. /*
  189. * deallocate rtcp cipher, if it is not the same as that in
  190. * template
  191. */
  192. if (template_session_keys &&
  193. session_keys->rtcp_cipher ==
  194. template_session_keys->rtcp_cipher) {
  195. /* do nothing */
  196. } else if (session_keys->rtcp_cipher) {
  197. status = srtp_cipher_dealloc(session_keys->rtcp_cipher);
  198. if (status)
  199. return status;
  200. }
  201. /*
  202. * deallocate rtcp auth function, if it is not the same as that in
  203. * template
  204. */
  205. if (template_session_keys &&
  206. session_keys->rtcp_auth == template_session_keys->rtcp_auth) {
  207. /* do nothing */
  208. } else if (session_keys->rtcp_auth) {
  209. status = srtp_auth_dealloc(session_keys->rtcp_auth);
  210. if (status)
  211. return status;
  212. }
  213. /*
  214. * zeroize the salt value
  215. */
  216. octet_string_set_to_zero(session_keys->salt, SRTP_AEAD_SALT_LEN);
  217. octet_string_set_to_zero(session_keys->c_salt, SRTP_AEAD_SALT_LEN);
  218. if (session_keys->mki_id) {
  219. octet_string_set_to_zero(session_keys->mki_id,
  220. session_keys->mki_size);
  221. srtp_crypto_free(session_keys->mki_id);
  222. session_keys->mki_id = NULL;
  223. }
  224. /*
  225. * deallocate key usage limit, if it is not the same as that in
  226. * template
  227. */
  228. if (template_session_keys &&
  229. session_keys->limit == template_session_keys->limit) {
  230. /* do nothing */
  231. } else if (session_keys->limit) {
  232. srtp_crypto_free(session_keys->limit);
  233. }
  234. }
  235. srtp_crypto_free(stream->session_keys);
  236. }
  237. status = srtp_rdbx_dealloc(&stream->rtp_rdbx);
  238. if (status)
  239. return status;
  240. /* DAM - need to deallocate EKT here */
  241. if (stream_template &&
  242. stream->enc_xtn_hdr == stream_template->enc_xtn_hdr) {
  243. /* do nothing */
  244. } else if (stream->enc_xtn_hdr) {
  245. srtp_crypto_free(stream->enc_xtn_hdr);
  246. }
  247. /* deallocate srtp stream context */
  248. srtp_crypto_free(stream);
  249. return srtp_err_status_ok;
  250. }
  251. srtp_err_status_t srtp_stream_alloc(srtp_stream_ctx_t **str_ptr,
  252. const srtp_policy_t *p)
  253. {
  254. srtp_stream_ctx_t *str;
  255. srtp_err_status_t stat;
  256. unsigned int i = 0;
  257. srtp_session_keys_t *session_keys = NULL;
  258. /*
  259. * This function allocates the stream context, rtp and rtcp ciphers
  260. * and auth functions, and key limit structure. If there is a
  261. * failure during allocation, we free all previously allocated
  262. * memory and return a failure code. The code could probably
  263. * be improved, but it works and should be clear.
  264. */
  265. /* allocate srtp stream and set str_ptr */
  266. str = (srtp_stream_ctx_t *)srtp_crypto_alloc(sizeof(srtp_stream_ctx_t));
  267. if (str == NULL)
  268. return srtp_err_status_alloc_fail;
  269. *str_ptr = str;
  270. /*
  271. *To keep backwards API compatible if someone is using multiple master
  272. * keys then key should be set to NULL
  273. */
  274. if (p->key != NULL) {
  275. str->num_master_keys = 1;
  276. } else {
  277. str->num_master_keys = p->num_master_keys;
  278. }
  279. str->session_keys = (srtp_session_keys_t *)srtp_crypto_alloc(
  280. sizeof(srtp_session_keys_t) * str->num_master_keys);
  281. if (str->session_keys == NULL) {
  282. srtp_stream_dealloc(str, NULL);
  283. return srtp_err_status_alloc_fail;
  284. }
  285. for (i = 0; i < str->num_master_keys; i++) {
  286. session_keys = &str->session_keys[i];
  287. /* allocate cipher */
  288. stat = srtp_crypto_kernel_alloc_cipher(
  289. p->rtp.cipher_type, &session_keys->rtp_cipher,
  290. p->rtp.cipher_key_len, p->rtp.auth_tag_len);
  291. if (stat) {
  292. srtp_stream_dealloc(str, NULL);
  293. return stat;
  294. }
  295. /* allocate auth function */
  296. stat = srtp_crypto_kernel_alloc_auth(
  297. p->rtp.auth_type, &session_keys->rtp_auth, p->rtp.auth_key_len,
  298. p->rtp.auth_tag_len);
  299. if (stat) {
  300. srtp_stream_dealloc(str, NULL);
  301. return stat;
  302. }
  303. /*
  304. * ...and now the RTCP-specific initialization - first, allocate
  305. * the cipher
  306. */
  307. stat = srtp_crypto_kernel_alloc_cipher(
  308. p->rtcp.cipher_type, &session_keys->rtcp_cipher,
  309. p->rtcp.cipher_key_len, p->rtcp.auth_tag_len);
  310. if (stat) {
  311. srtp_stream_dealloc(str, NULL);
  312. return stat;
  313. }
  314. /* allocate auth function */
  315. stat = srtp_crypto_kernel_alloc_auth(
  316. p->rtcp.auth_type, &session_keys->rtcp_auth, p->rtcp.auth_key_len,
  317. p->rtcp.auth_tag_len);
  318. if (stat) {
  319. srtp_stream_dealloc(str, NULL);
  320. return stat;
  321. }
  322. session_keys->mki_id = NULL;
  323. /* allocate key limit structure */
  324. session_keys->limit = (srtp_key_limit_ctx_t *)srtp_crypto_alloc(
  325. sizeof(srtp_key_limit_ctx_t));
  326. if (session_keys->limit == NULL) {
  327. srtp_stream_dealloc(str, NULL);
  328. return srtp_err_status_alloc_fail;
  329. }
  330. }
  331. /* allocate ekt data associated with stream */
  332. stat = srtp_ekt_alloc(&str->ekt, p->ekt);
  333. if (stat) {
  334. srtp_stream_dealloc(str, NULL);
  335. return stat;
  336. }
  337. if (p->enc_xtn_hdr && p->enc_xtn_hdr_count > 0) {
  338. srtp_cipher_type_id_t enc_xtn_hdr_cipher_type;
  339. int enc_xtn_hdr_cipher_key_len;
  340. str->enc_xtn_hdr = (int *)srtp_crypto_alloc(p->enc_xtn_hdr_count *
  341. sizeof(p->enc_xtn_hdr[0]));
  342. if (!str->enc_xtn_hdr) {
  343. srtp_stream_dealloc(str, NULL);
  344. return srtp_err_status_alloc_fail;
  345. }
  346. memcpy(str->enc_xtn_hdr, p->enc_xtn_hdr,
  347. p->enc_xtn_hdr_count * sizeof(p->enc_xtn_hdr[0]));
  348. str->enc_xtn_hdr_count = p->enc_xtn_hdr_count;
  349. /*
  350. * For GCM ciphers, the corresponding ICM cipher is used for header
  351. * extensions encryption.
  352. */
  353. switch (p->rtp.cipher_type) {
  354. case SRTP_AES_GCM_128:
  355. enc_xtn_hdr_cipher_type = SRTP_AES_ICM_128;
  356. enc_xtn_hdr_cipher_key_len = SRTP_AES_ICM_128_KEY_LEN_WSALT;
  357. break;
  358. case SRTP_AES_GCM_256:
  359. enc_xtn_hdr_cipher_type = SRTP_AES_ICM_256;
  360. enc_xtn_hdr_cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT;
  361. break;
  362. default:
  363. enc_xtn_hdr_cipher_type = p->rtp.cipher_type;
  364. enc_xtn_hdr_cipher_key_len = p->rtp.cipher_key_len;
  365. break;
  366. }
  367. for (i = 0; i < str->num_master_keys; i++) {
  368. session_keys = &str->session_keys[i];
  369. /* allocate cipher for extensions header encryption */
  370. stat = srtp_crypto_kernel_alloc_cipher(
  371. enc_xtn_hdr_cipher_type, &session_keys->rtp_xtn_hdr_cipher,
  372. enc_xtn_hdr_cipher_key_len, 0);
  373. if (stat) {
  374. srtp_stream_dealloc(str, NULL);
  375. return stat;
  376. }
  377. }
  378. } else {
  379. for (i = 0; i < str->num_master_keys; i++) {
  380. session_keys = &str->session_keys[i];
  381. session_keys->rtp_xtn_hdr_cipher = NULL;
  382. }
  383. str->enc_xtn_hdr = NULL;
  384. str->enc_xtn_hdr_count = 0;
  385. }
  386. return srtp_err_status_ok;
  387. }
  388. /*
  389. * srtp_stream_clone(stream_template, new) allocates a new stream and
  390. * initializes it using the cipher and auth of the stream_template
  391. *
  392. * the only unique data in a cloned stream is the replay database and
  393. * the SSRC
  394. */
  395. srtp_err_status_t srtp_stream_clone(const srtp_stream_ctx_t *stream_template,
  396. uint32_t ssrc,
  397. srtp_stream_ctx_t **str_ptr)
  398. {
  399. srtp_err_status_t status;
  400. srtp_stream_ctx_t *str;
  401. unsigned int i = 0;
  402. srtp_session_keys_t *session_keys = NULL;
  403. const srtp_session_keys_t *template_session_keys = NULL;
  404. debug_print(mod_srtp, "cloning stream (SSRC: 0x%08x)", ntohl(ssrc));
  405. /* allocate srtp stream and set str_ptr */
  406. str = (srtp_stream_ctx_t *)srtp_crypto_alloc(sizeof(srtp_stream_ctx_t));
  407. if (str == NULL)
  408. return srtp_err_status_alloc_fail;
  409. *str_ptr = str;
  410. str->num_master_keys = stream_template->num_master_keys;
  411. str->session_keys = (srtp_session_keys_t *)srtp_crypto_alloc(
  412. sizeof(srtp_session_keys_t) * str->num_master_keys);
  413. if (str->session_keys == NULL) {
  414. srtp_stream_dealloc(*str_ptr, stream_template);
  415. *str_ptr = NULL;
  416. return srtp_err_status_alloc_fail;
  417. }
  418. for (i = 0; i < stream_template->num_master_keys; i++) {
  419. session_keys = &str->session_keys[i];
  420. template_session_keys = &stream_template->session_keys[i];
  421. /* set cipher and auth pointers to those of the template */
  422. session_keys->rtp_cipher = template_session_keys->rtp_cipher;
  423. session_keys->rtp_auth = template_session_keys->rtp_auth;
  424. session_keys->rtp_xtn_hdr_cipher =
  425. template_session_keys->rtp_xtn_hdr_cipher;
  426. session_keys->rtcp_cipher = template_session_keys->rtcp_cipher;
  427. session_keys->rtcp_auth = template_session_keys->rtcp_auth;
  428. session_keys->mki_size = template_session_keys->mki_size;
  429. if (template_session_keys->mki_size == 0) {
  430. session_keys->mki_id = NULL;
  431. } else {
  432. session_keys->mki_id =
  433. srtp_crypto_alloc(template_session_keys->mki_size);
  434. if (session_keys->mki_id == NULL) {
  435. srtp_stream_dealloc(*str_ptr, stream_template);
  436. *str_ptr = NULL;
  437. return srtp_err_status_init_fail;
  438. }
  439. memcpy(session_keys->mki_id, template_session_keys->mki_id,
  440. session_keys->mki_size);
  441. }
  442. /* Copy the salt values */
  443. memcpy(session_keys->salt, template_session_keys->salt,
  444. SRTP_AEAD_SALT_LEN);
  445. memcpy(session_keys->c_salt, template_session_keys->c_salt,
  446. SRTP_AEAD_SALT_LEN);
  447. /* set key limit to point to that of the template */
  448. status = srtp_key_limit_clone(template_session_keys->limit,
  449. &session_keys->limit);
  450. if (status) {
  451. srtp_stream_dealloc(*str_ptr, stream_template);
  452. *str_ptr = NULL;
  453. return status;
  454. }
  455. }
  456. /* initialize replay databases */
  457. status = srtp_rdbx_init(
  458. &str->rtp_rdbx, srtp_rdbx_get_window_size(&stream_template->rtp_rdbx));
  459. if (status) {
  460. srtp_stream_dealloc(*str_ptr, stream_template);
  461. *str_ptr = NULL;
  462. return status;
  463. }
  464. srtp_rdb_init(&str->rtcp_rdb);
  465. str->allow_repeat_tx = stream_template->allow_repeat_tx;
  466. /* set ssrc to that provided */
  467. str->ssrc = ssrc;
  468. /* reset pending ROC */
  469. str->pending_roc = 0;
  470. /* set direction and security services */
  471. str->direction = stream_template->direction;
  472. str->rtp_services = stream_template->rtp_services;
  473. str->rtcp_services = stream_template->rtcp_services;
  474. /* set pointer to EKT data associated with stream */
  475. str->ekt = stream_template->ekt;
  476. /* copy information about extensions header encryption */
  477. str->enc_xtn_hdr = stream_template->enc_xtn_hdr;
  478. str->enc_xtn_hdr_count = stream_template->enc_xtn_hdr_count;
  479. /* defensive coding */
  480. str->next = NULL;
  481. return srtp_err_status_ok;
  482. }
  483. /*
  484. * key derivation functions, internal to libSRTP
  485. *
  486. * srtp_kdf_t is a key derivation context
  487. *
  488. * srtp_kdf_init(&kdf, cipher_id, k, keylen) initializes kdf to use cipher
  489. * described by cipher_id, with the master key k with length in octets keylen.
  490. *
  491. * srtp_kdf_generate(&kdf, l, kl, keylen) derives the key
  492. * corresponding to label l and puts it into kl; the length
  493. * of the key in octets is provided as keylen. this function
  494. * should be called once for each subkey that is derived.
  495. *
  496. * srtp_kdf_clear(&kdf) zeroizes and deallocates the kdf state
  497. */
  498. typedef enum {
  499. label_rtp_encryption = 0x00,
  500. label_rtp_msg_auth = 0x01,
  501. label_rtp_salt = 0x02,
  502. label_rtcp_encryption = 0x03,
  503. label_rtcp_msg_auth = 0x04,
  504. label_rtcp_salt = 0x05,
  505. label_rtp_header_encryption = 0x06,
  506. label_rtp_header_salt = 0x07
  507. } srtp_prf_label;
  508. #define MAX_SRTP_KEY_LEN 256
  509. #if defined(OPENSSL) && defined(OPENSSL_KDF)
  510. #define MAX_SRTP_AESKEY_LEN 32
  511. #define MAX_SRTP_SALT_LEN 14
  512. /*
  513. * srtp_kdf_t represents a key derivation function. The SRTP
  514. * default KDF is the only one implemented at present.
  515. */
  516. typedef struct {
  517. uint8_t master_key[MAX_SRTP_AESKEY_LEN];
  518. uint8_t master_salt[MAX_SRTP_SALT_LEN];
  519. const EVP_CIPHER *evp;
  520. } srtp_kdf_t;
  521. static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf,
  522. const uint8_t *key,
  523. int key_len,
  524. int salt_len)
  525. {
  526. memset(kdf, 0x0, sizeof(srtp_kdf_t));
  527. /* The NULL cipher has zero key length */
  528. if (key_len == 0)
  529. return srtp_err_status_ok;
  530. if ((key_len > MAX_SRTP_AESKEY_LEN) || (salt_len > MAX_SRTP_SALT_LEN)) {
  531. return srtp_err_status_bad_param;
  532. }
  533. switch (key_len) {
  534. case SRTP_AES_256_KEYSIZE:
  535. kdf->evp = EVP_aes_256_ctr();
  536. break;
  537. case SRTP_AES_192_KEYSIZE:
  538. kdf->evp = EVP_aes_192_ctr();
  539. break;
  540. case SRTP_AES_128_KEYSIZE:
  541. kdf->evp = EVP_aes_128_ctr();
  542. break;
  543. default:
  544. return srtp_err_status_bad_param;
  545. break;
  546. }
  547. memcpy(kdf->master_key, key, key_len);
  548. memcpy(kdf->master_salt, key + key_len, salt_len);
  549. return srtp_err_status_ok;
  550. }
  551. static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf,
  552. srtp_prf_label label,
  553. uint8_t *key,
  554. unsigned int length)
  555. {
  556. int ret;
  557. /* The NULL cipher will not have an EVP */
  558. if (!kdf->evp)
  559. return srtp_err_status_ok;
  560. octet_string_set_to_zero(key, length);
  561. /*
  562. * Invoke the OpenSSL SRTP KDF function
  563. * This is useful if OpenSSL is in FIPS mode and FIP
  564. * compliance is required for SRTP.
  565. */
  566. ret = kdf_srtp(kdf->evp, (char *)&kdf->master_key,
  567. (char *)&kdf->master_salt, NULL, NULL, label, (char *)key);
  568. if (ret == -1) {
  569. return (srtp_err_status_algo_fail);
  570. }
  571. return srtp_err_status_ok;
  572. }
  573. static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf)
  574. {
  575. octet_string_set_to_zero(kdf->master_key, MAX_SRTP_AESKEY_LEN);
  576. octet_string_set_to_zero(kdf->master_salt, MAX_SRTP_SALT_LEN);
  577. kdf->evp = NULL;
  578. return srtp_err_status_ok;
  579. }
  580. #else /* if OPENSSL_KDF */
  581. /*
  582. * srtp_kdf_t represents a key derivation function. The SRTP
  583. * default KDF is the only one implemented at present.
  584. */
  585. typedef struct {
  586. srtp_cipher_t *cipher; /* cipher used for key derivation */
  587. } srtp_kdf_t;
  588. static srtp_err_status_t srtp_kdf_init(srtp_kdf_t *kdf,
  589. const uint8_t *key,
  590. int key_len)
  591. {
  592. srtp_cipher_type_id_t cipher_id;
  593. srtp_err_status_t stat;
  594. switch (key_len) {
  595. case SRTP_AES_ICM_256_KEY_LEN_WSALT:
  596. cipher_id = SRTP_AES_ICM_256;
  597. break;
  598. case SRTP_AES_ICM_192_KEY_LEN_WSALT:
  599. cipher_id = SRTP_AES_ICM_192;
  600. break;
  601. case SRTP_AES_ICM_128_KEY_LEN_WSALT:
  602. cipher_id = SRTP_AES_ICM_128;
  603. break;
  604. default:
  605. return srtp_err_status_bad_param;
  606. break;
  607. }
  608. stat = srtp_crypto_kernel_alloc_cipher(cipher_id, &kdf->cipher, key_len, 0);
  609. if (stat)
  610. return stat;
  611. stat = srtp_cipher_init(kdf->cipher, key);
  612. if (stat) {
  613. srtp_cipher_dealloc(kdf->cipher);
  614. return stat;
  615. }
  616. return srtp_err_status_ok;
  617. }
  618. static srtp_err_status_t srtp_kdf_generate(srtp_kdf_t *kdf,
  619. srtp_prf_label label,
  620. uint8_t *key,
  621. unsigned int length)
  622. {
  623. srtp_err_status_t status;
  624. v128_t nonce;
  625. /* set eigth octet of nonce to <label>, set the rest of it to zero */
  626. v128_set_to_zero(&nonce);
  627. nonce.v8[7] = label;
  628. status = srtp_cipher_set_iv(kdf->cipher, (uint8_t *)&nonce,
  629. srtp_direction_encrypt);
  630. if (status)
  631. return status;
  632. /* generate keystream output */
  633. octet_string_set_to_zero(key, length);
  634. status = srtp_cipher_encrypt(kdf->cipher, key, &length);
  635. if (status)
  636. return status;
  637. return srtp_err_status_ok;
  638. }
  639. static srtp_err_status_t srtp_kdf_clear(srtp_kdf_t *kdf)
  640. {
  641. srtp_err_status_t status;
  642. status = srtp_cipher_dealloc(kdf->cipher);
  643. if (status)
  644. return status;
  645. kdf->cipher = NULL;
  646. return srtp_err_status_ok;
  647. }
  648. #endif /* else OPENSSL_KDF */
  649. /*
  650. * end of key derivation functions
  651. */
  652. /* Get the base key length corresponding to a given combined key+salt
  653. * length for the given cipher.
  654. * TODO: key and salt lengths should be separate fields in the policy. */
  655. static inline int base_key_length(const srtp_cipher_type_t *cipher,
  656. int key_length)
  657. {
  658. switch (cipher->id) {
  659. case SRTP_AES_ICM_128:
  660. case SRTP_AES_ICM_192:
  661. case SRTP_AES_ICM_256:
  662. /* The legacy modes are derived from
  663. * the configured key length on the policy */
  664. return key_length - SRTP_SALT_LEN;
  665. break;
  666. case SRTP_AES_GCM_128:
  667. return key_length - SRTP_AEAD_SALT_LEN;
  668. break;
  669. case SRTP_AES_GCM_256:
  670. return key_length - SRTP_AEAD_SALT_LEN;
  671. break;
  672. default:
  673. return key_length;
  674. break;
  675. }
  676. }
  677. unsigned int srtp_validate_policy_master_keys(const srtp_policy_t *policy)
  678. {
  679. unsigned long i = 0;
  680. if (policy->key == NULL) {
  681. if (policy->num_master_keys <= 0)
  682. return 0;
  683. if (policy->num_master_keys > SRTP_MAX_NUM_MASTER_KEYS)
  684. return 0;
  685. for (i = 0; i < policy->num_master_keys; i++) {
  686. if (policy->keys[i]->key == NULL)
  687. return 0;
  688. if (policy->keys[i]->mki_size > SRTP_MAX_MKI_LEN)
  689. return 0;
  690. }
  691. }
  692. return 1;
  693. }
  694. srtp_session_keys_t *srtp_get_session_keys_with_mki_index(
  695. srtp_stream_ctx_t *stream,
  696. unsigned int use_mki,
  697. unsigned int mki_index)
  698. {
  699. if (use_mki) {
  700. if (mki_index >= stream->num_master_keys) {
  701. return NULL;
  702. }
  703. return &stream->session_keys[mki_index];
  704. }
  705. return &stream->session_keys[0];
  706. }
  707. unsigned int srtp_inject_mki(uint8_t *mki_tag_location,
  708. srtp_session_keys_t *session_keys,
  709. unsigned int use_mki)
  710. {
  711. unsigned int mki_size = 0;
  712. if (use_mki) {
  713. mki_size = session_keys->mki_size;
  714. if (mki_size != 0) {
  715. // Write MKI into memory
  716. memcpy(mki_tag_location, session_keys->mki_id, mki_size);
  717. }
  718. }
  719. return mki_size;
  720. }
  721. srtp_err_status_t srtp_stream_init_all_master_keys(
  722. srtp_stream_ctx_t *srtp,
  723. unsigned char *key,
  724. srtp_master_key_t **keys,
  725. const unsigned int max_master_keys)
  726. {
  727. unsigned int i = 0;
  728. srtp_err_status_t status = srtp_err_status_ok;
  729. srtp_master_key_t single_master_key;
  730. if (key != NULL) {
  731. srtp->num_master_keys = 1;
  732. single_master_key.key = key;
  733. single_master_key.mki_id = NULL;
  734. single_master_key.mki_size = 0;
  735. status = srtp_stream_init_keys(srtp, &single_master_key, 0);
  736. } else {
  737. srtp->num_master_keys = max_master_keys;
  738. for (i = 0; i < srtp->num_master_keys && i < SRTP_MAX_NUM_MASTER_KEYS;
  739. i++) {
  740. status = srtp_stream_init_keys(srtp, keys[i], i);
  741. if (status) {
  742. return status;
  743. }
  744. }
  745. }
  746. return status;
  747. }
  748. srtp_err_status_t srtp_stream_init_keys(srtp_stream_ctx_t *srtp,
  749. srtp_master_key_t *master_key,
  750. const unsigned int current_mki_index)
  751. {
  752. srtp_err_status_t stat;
  753. srtp_kdf_t kdf;
  754. uint8_t tmp_key[MAX_SRTP_KEY_LEN];
  755. int kdf_keylen = 30, rtp_keylen, rtcp_keylen;
  756. int rtp_base_key_len, rtp_salt_len;
  757. int rtcp_base_key_len, rtcp_salt_len;
  758. srtp_session_keys_t *session_keys = NULL;
  759. unsigned char *key = master_key->key;
  760. /* If RTP or RTCP have a key length > AES-128, assume matching kdf. */
  761. /* TODO: kdf algorithm, master key length, and master salt length should
  762. * be part of srtp_policy_t.
  763. */
  764. session_keys = &srtp->session_keys[current_mki_index];
  765. /* initialize key limit to maximum value */
  766. #ifdef NO_64BIT_MATH
  767. {
  768. uint64_t temp;
  769. temp = make64(UINT_MAX, UINT_MAX);
  770. srtp_key_limit_set(session_keys->limit, temp);
  771. }
  772. #else
  773. srtp_key_limit_set(session_keys->limit, 0xffffffffffffLL);
  774. #endif
  775. if (master_key->mki_size != 0) {
  776. session_keys->mki_id = srtp_crypto_alloc(master_key->mki_size);
  777. if (session_keys->mki_id == NULL) {
  778. return srtp_err_status_init_fail;
  779. }
  780. memcpy(session_keys->mki_id, master_key->mki_id, master_key->mki_size);
  781. } else {
  782. session_keys->mki_id = NULL;
  783. }
  784. session_keys->mki_size = master_key->mki_size;
  785. rtp_keylen = srtp_cipher_get_key_length(session_keys->rtp_cipher);
  786. rtcp_keylen = srtp_cipher_get_key_length(session_keys->rtcp_cipher);
  787. rtp_base_key_len =
  788. base_key_length(session_keys->rtp_cipher->type, rtp_keylen);
  789. rtp_salt_len = rtp_keylen - rtp_base_key_len;
  790. if (rtp_keylen > kdf_keylen) {
  791. kdf_keylen = 46; /* AES-CTR mode is always used for KDF */
  792. }
  793. if (rtcp_keylen > kdf_keylen) {
  794. kdf_keylen = 46; /* AES-CTR mode is always used for KDF */
  795. }
  796. debug_print(mod_srtp, "srtp key len: %d", rtp_keylen);
  797. debug_print(mod_srtp, "srtcp key len: %d", rtcp_keylen);
  798. debug_print(mod_srtp, "base key len: %d", rtp_base_key_len);
  799. debug_print(mod_srtp, "kdf key len: %d", kdf_keylen);
  800. debug_print(mod_srtp, "rtp salt len: %d", rtp_salt_len);
  801. /*
  802. * Make sure the key given to us is 'zero' appended. GCM
  803. * mode uses a shorter master SALT (96 bits), but still relies on
  804. * the legacy CTR mode KDF, which uses a 112 bit master SALT.
  805. */
  806. memset(tmp_key, 0x0, MAX_SRTP_KEY_LEN);
  807. memcpy(tmp_key, key, (rtp_base_key_len + rtp_salt_len));
  808. /* initialize KDF state */
  809. #if defined(OPENSSL) && defined(OPENSSL_KDF)
  810. stat = srtp_kdf_init(&kdf, (const uint8_t *)tmp_key, rtp_base_key_len,
  811. rtp_salt_len);
  812. #else
  813. stat = srtp_kdf_init(&kdf, (const uint8_t *)tmp_key, kdf_keylen);
  814. #endif
  815. if (stat) {
  816. /* zeroize temp buffer */
  817. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  818. return srtp_err_status_init_fail;
  819. }
  820. /* generate encryption key */
  821. stat = srtp_kdf_generate(&kdf, label_rtp_encryption, tmp_key,
  822. rtp_base_key_len);
  823. if (stat) {
  824. /* zeroize temp buffer */
  825. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  826. return srtp_err_status_init_fail;
  827. }
  828. debug_print(mod_srtp, "cipher key: %s",
  829. srtp_octet_string_hex_string(tmp_key, rtp_base_key_len));
  830. /*
  831. * if the cipher in the srtp context uses a salt, then we need
  832. * to generate the salt value
  833. */
  834. if (rtp_salt_len > 0) {
  835. debug_print0(mod_srtp, "found rtp_salt_len > 0, generating salt");
  836. /* generate encryption salt, put after encryption key */
  837. stat = srtp_kdf_generate(&kdf, label_rtp_salt,
  838. tmp_key + rtp_base_key_len, rtp_salt_len);
  839. if (stat) {
  840. /* zeroize temp buffer */
  841. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  842. return srtp_err_status_init_fail;
  843. }
  844. memcpy(session_keys->salt, tmp_key + rtp_base_key_len,
  845. SRTP_AEAD_SALT_LEN);
  846. }
  847. if (rtp_salt_len > 0) {
  848. debug_print(mod_srtp, "cipher salt: %s",
  849. srtp_octet_string_hex_string(tmp_key + rtp_base_key_len,
  850. rtp_salt_len));
  851. }
  852. /* initialize cipher */
  853. stat = srtp_cipher_init(session_keys->rtp_cipher, tmp_key);
  854. if (stat) {
  855. /* zeroize temp buffer */
  856. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  857. return srtp_err_status_init_fail;
  858. }
  859. if (session_keys->rtp_xtn_hdr_cipher) {
  860. /* generate extensions header encryption key */
  861. int rtp_xtn_hdr_keylen;
  862. int rtp_xtn_hdr_base_key_len;
  863. int rtp_xtn_hdr_salt_len;
  864. srtp_kdf_t tmp_kdf;
  865. srtp_kdf_t *xtn_hdr_kdf;
  866. if (session_keys->rtp_xtn_hdr_cipher->type !=
  867. session_keys->rtp_cipher->type) {
  868. /*
  869. * With GCM ciphers, the header extensions are still encrypted using
  870. * the corresponding ICM cipher.
  871. * See https://tools.ietf.org/html/rfc7714#section-8.3
  872. */
  873. uint8_t tmp_xtn_hdr_key[MAX_SRTP_KEY_LEN];
  874. rtp_xtn_hdr_keylen =
  875. srtp_cipher_get_key_length(session_keys->rtp_xtn_hdr_cipher);
  876. rtp_xtn_hdr_base_key_len = base_key_length(
  877. session_keys->rtp_xtn_hdr_cipher->type, rtp_xtn_hdr_keylen);
  878. rtp_xtn_hdr_salt_len =
  879. rtp_xtn_hdr_keylen - rtp_xtn_hdr_base_key_len;
  880. if (rtp_xtn_hdr_salt_len > rtp_salt_len) {
  881. switch (session_keys->rtp_cipher->type->id) {
  882. case SRTP_AES_GCM_128:
  883. case SRTP_AES_GCM_256:
  884. /*
  885. * The shorter GCM salt is padded to the required ICM salt
  886. * length.
  887. */
  888. rtp_xtn_hdr_salt_len = rtp_salt_len;
  889. break;
  890. default:
  891. /* zeroize temp buffer */
  892. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  893. return srtp_err_status_bad_param;
  894. }
  895. }
  896. memset(tmp_xtn_hdr_key, 0x0, MAX_SRTP_KEY_LEN);
  897. memcpy(tmp_xtn_hdr_key, key,
  898. (rtp_xtn_hdr_base_key_len + rtp_xtn_hdr_salt_len));
  899. xtn_hdr_kdf = &tmp_kdf;
  900. /* initialize KDF state */
  901. #if defined(OPENSSL) && defined(OPENSSL_KDF)
  902. stat =
  903. srtp_kdf_init(xtn_hdr_kdf, (const uint8_t *)tmp_xtn_hdr_key,
  904. rtp_xtn_hdr_base_key_len, rtp_xtn_hdr_salt_len);
  905. #else
  906. stat = srtp_kdf_init(xtn_hdr_kdf, (const uint8_t *)tmp_xtn_hdr_key,
  907. kdf_keylen);
  908. #endif
  909. octet_string_set_to_zero(tmp_xtn_hdr_key, MAX_SRTP_KEY_LEN);
  910. if (stat) {
  911. /* zeroize temp buffer */
  912. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  913. return srtp_err_status_init_fail;
  914. }
  915. } else {
  916. /* Reuse main KDF. */
  917. rtp_xtn_hdr_keylen = rtp_keylen;
  918. rtp_xtn_hdr_base_key_len = rtp_base_key_len;
  919. rtp_xtn_hdr_salt_len = rtp_salt_len;
  920. xtn_hdr_kdf = &kdf;
  921. }
  922. stat = srtp_kdf_generate(xtn_hdr_kdf, label_rtp_header_encryption,
  923. tmp_key, rtp_xtn_hdr_base_key_len);
  924. if (stat) {
  925. /* zeroize temp buffer */
  926. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  927. return srtp_err_status_init_fail;
  928. }
  929. debug_print(
  930. mod_srtp, "extensions cipher key: %s",
  931. srtp_octet_string_hex_string(tmp_key, rtp_xtn_hdr_base_key_len));
  932. /*
  933. * if the cipher in the srtp context uses a salt, then we need
  934. * to generate the salt value
  935. */
  936. if (rtp_xtn_hdr_salt_len > 0) {
  937. debug_print0(mod_srtp,
  938. "found rtp_xtn_hdr_salt_len > 0, generating salt");
  939. /* generate encryption salt, put after encryption key */
  940. stat = srtp_kdf_generate(xtn_hdr_kdf, label_rtp_header_salt,
  941. tmp_key + rtp_xtn_hdr_base_key_len,
  942. rtp_xtn_hdr_salt_len);
  943. if (stat) {
  944. /* zeroize temp buffer */
  945. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  946. return srtp_err_status_init_fail;
  947. }
  948. }
  949. if (rtp_xtn_hdr_salt_len > 0) {
  950. debug_print(
  951. mod_srtp, "extensions cipher salt: %s",
  952. srtp_octet_string_hex_string(tmp_key + rtp_xtn_hdr_base_key_len,
  953. rtp_xtn_hdr_salt_len));
  954. }
  955. /* initialize extensions header cipher */
  956. stat = srtp_cipher_init(session_keys->rtp_xtn_hdr_cipher, tmp_key);
  957. if (stat) {
  958. /* zeroize temp buffer */
  959. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  960. return srtp_err_status_init_fail;
  961. }
  962. if (xtn_hdr_kdf != &kdf) {
  963. /* release memory for custom header extension encryption kdf */
  964. stat = srtp_kdf_clear(xtn_hdr_kdf);
  965. if (stat) {
  966. /* zeroize temp buffer */
  967. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  968. return srtp_err_status_init_fail;
  969. }
  970. }
  971. }
  972. /* generate authentication key */
  973. stat = srtp_kdf_generate(&kdf, label_rtp_msg_auth, tmp_key,
  974. srtp_auth_get_key_length(session_keys->rtp_auth));
  975. if (stat) {
  976. /* zeroize temp buffer */
  977. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  978. return srtp_err_status_init_fail;
  979. }
  980. debug_print(mod_srtp, "auth key: %s",
  981. srtp_octet_string_hex_string(
  982. tmp_key, srtp_auth_get_key_length(session_keys->rtp_auth)));
  983. /* initialize auth function */
  984. stat = srtp_auth_init(session_keys->rtp_auth, tmp_key);
  985. if (stat) {
  986. /* zeroize temp buffer */
  987. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  988. return srtp_err_status_init_fail;
  989. }
  990. /*
  991. * ...now initialize SRTCP keys
  992. */
  993. rtcp_base_key_len =
  994. base_key_length(session_keys->rtcp_cipher->type, rtcp_keylen);
  995. rtcp_salt_len = rtcp_keylen - rtcp_base_key_len;
  996. debug_print(mod_srtp, "rtcp salt len: %d", rtcp_salt_len);
  997. /* generate encryption key */
  998. stat = srtp_kdf_generate(&kdf, label_rtcp_encryption, tmp_key,
  999. rtcp_base_key_len);
  1000. if (stat) {
  1001. /* zeroize temp buffer */
  1002. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  1003. return srtp_err_status_init_fail;
  1004. }
  1005. /*
  1006. * if the cipher in the srtp context uses a salt, then we need
  1007. * to generate the salt value
  1008. */
  1009. if (rtcp_salt_len > 0) {
  1010. debug_print0(mod_srtp, "found rtcp_salt_len > 0, generating rtcp salt");
  1011. /* generate encryption salt, put after encryption key */
  1012. stat = srtp_kdf_generate(&kdf, label_rtcp_salt,
  1013. tmp_key + rtcp_base_key_len, rtcp_salt_len);
  1014. if (stat) {
  1015. /* zeroize temp buffer */
  1016. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  1017. return srtp_err_status_init_fail;
  1018. }
  1019. memcpy(session_keys->c_salt, tmp_key + rtcp_base_key_len,
  1020. SRTP_AEAD_SALT_LEN);
  1021. }
  1022. debug_print(mod_srtp, "rtcp cipher key: %s",
  1023. srtp_octet_string_hex_string(tmp_key, rtcp_base_key_len));
  1024. if (rtcp_salt_len > 0) {
  1025. debug_print(mod_srtp, "rtcp cipher salt: %s",
  1026. srtp_octet_string_hex_string(tmp_key + rtcp_base_key_len,
  1027. rtcp_salt_len));
  1028. }
  1029. /* initialize cipher */
  1030. stat = srtp_cipher_init(session_keys->rtcp_cipher, tmp_key);
  1031. if (stat) {
  1032. /* zeroize temp buffer */
  1033. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  1034. return srtp_err_status_init_fail;
  1035. }
  1036. /* generate authentication key */
  1037. stat = srtp_kdf_generate(&kdf, label_rtcp_msg_auth, tmp_key,
  1038. srtp_auth_get_key_length(session_keys->rtcp_auth));
  1039. if (stat) {
  1040. /* zeroize temp buffer */
  1041. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  1042. return srtp_err_status_init_fail;
  1043. }
  1044. debug_print(
  1045. mod_srtp, "rtcp auth key: %s",
  1046. srtp_octet_string_hex_string(
  1047. tmp_key, srtp_auth_get_key_length(session_keys->rtcp_auth)));
  1048. /* initialize auth function */
  1049. stat = srtp_auth_init(session_keys->rtcp_auth, tmp_key);
  1050. if (stat) {
  1051. /* zeroize temp buffer */
  1052. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  1053. return srtp_err_status_init_fail;
  1054. }
  1055. /* clear memory then return */
  1056. stat = srtp_kdf_clear(&kdf);
  1057. octet_string_set_to_zero(tmp_key, MAX_SRTP_KEY_LEN);
  1058. if (stat)
  1059. return srtp_err_status_init_fail;
  1060. return srtp_err_status_ok;
  1061. }
  1062. srtp_err_status_t srtp_stream_init(srtp_stream_ctx_t *srtp,
  1063. const srtp_policy_t *p)
  1064. {
  1065. srtp_err_status_t err;
  1066. debug_print(mod_srtp, "initializing stream (SSRC: 0x%08x)", p->ssrc.value);
  1067. /* initialize replay database */
  1068. /*
  1069. * window size MUST be at least 64. MAY be larger. Values more than
  1070. * 2^15 aren't meaningful due to how extended sequence numbers are
  1071. * calculated.
  1072. * Let a window size of 0 imply the default value.
  1073. */
  1074. if (p->window_size != 0 &&
  1075. (p->window_size < 64 || p->window_size >= 0x8000))
  1076. return srtp_err_status_bad_param;
  1077. if (p->window_size != 0)
  1078. err = srtp_rdbx_init(&srtp->rtp_rdbx, p->window_size);
  1079. else
  1080. err = srtp_rdbx_init(&srtp->rtp_rdbx, 128);
  1081. if (err)
  1082. return err;
  1083. /* set the SSRC value */
  1084. srtp->ssrc = htonl(p->ssrc.value);
  1085. /* reset pending ROC */
  1086. srtp->pending_roc = 0;
  1087. /* set the security service flags */
  1088. srtp->rtp_services = p->rtp.sec_serv;
  1089. srtp->rtcp_services = p->rtcp.sec_serv;
  1090. /*
  1091. * set direction to unknown - this flag gets checked in srtp_protect(),
  1092. * srtp_unprotect(), srtp_protect_rtcp(), and srtp_unprotect_rtcp(), and
  1093. * gets set appropriately if it is set to unknown.
  1094. */
  1095. srtp->direction = dir_unknown;
  1096. /* initialize SRTCP replay database */
  1097. srtp_rdb_init(&srtp->rtcp_rdb);
  1098. /* initialize allow_repeat_tx */
  1099. /* guard against uninitialized memory: allow only 0 or 1 here */
  1100. if (p->allow_repeat_tx != 0 && p->allow_repeat_tx != 1) {
  1101. srtp_rdbx_dealloc(&srtp->rtp_rdbx);
  1102. return srtp_err_status_bad_param;
  1103. }
  1104. srtp->allow_repeat_tx = p->allow_repeat_tx;
  1105. /* DAM - no RTCP key limit at present */
  1106. /* initialize keys */
  1107. err = srtp_stream_init_all_master_keys(srtp, p->key, p->keys,
  1108. p->num_master_keys);
  1109. if (err) {
  1110. srtp_rdbx_dealloc(&srtp->rtp_rdbx);
  1111. return err;
  1112. }
  1113. /*
  1114. * if EKT is in use, then initialize the EKT data associated with
  1115. * the stream
  1116. */
  1117. err = srtp_ekt_stream_init_from_policy(srtp->ekt, p->ekt);
  1118. if (err) {
  1119. srtp_rdbx_dealloc(&srtp->rtp_rdbx);
  1120. return err;
  1121. }
  1122. return srtp_err_status_ok;
  1123. }
  1124. /*
  1125. * srtp_event_reporter is an event handler function that merely
  1126. * reports the events that are reported by the callbacks
  1127. */
  1128. void srtp_event_reporter(srtp_event_data_t *data)
  1129. {
  1130. srtp_err_report(srtp_err_level_warning, "srtp: in stream 0x%x: ",
  1131. data->ssrc);
  1132. switch (data->event) {
  1133. case event_ssrc_collision:
  1134. srtp_err_report(srtp_err_level_warning, "\tSSRC collision\n");
  1135. break;
  1136. case event_key_soft_limit:
  1137. srtp_err_report(srtp_err_level_warning,
  1138. "\tkey usage soft limit reached\n");
  1139. break;
  1140. case event_key_hard_limit:
  1141. srtp_err_report(srtp_err_level_warning,
  1142. "\tkey usage hard limit reached\n");
  1143. break;
  1144. case event_packet_index_limit:
  1145. srtp_err_report(srtp_err_level_warning,
  1146. "\tpacket index limit reached\n");
  1147. break;
  1148. default:
  1149. srtp_err_report(srtp_err_level_warning,
  1150. "\tunknown event reported to handler\n");
  1151. }
  1152. }
  1153. /*
  1154. * srtp_event_handler is a global variable holding a pointer to the
  1155. * event handler function; this function is called for any unexpected
  1156. * event that needs to be handled out of the SRTP data path. see
  1157. * srtp_event_t in srtp.h for more info
  1158. *
  1159. * it is okay to set srtp_event_handler to NULL, but we set
  1160. * it to the srtp_event_reporter.
  1161. */
  1162. static srtp_event_handler_func_t *srtp_event_handler = srtp_event_reporter;
  1163. srtp_err_status_t srtp_install_event_handler(srtp_event_handler_func_t func)
  1164. {
  1165. /*
  1166. * note that we accept NULL arguments intentionally - calling this
  1167. * function with a NULL arguments removes an event handler that's
  1168. * been previously installed
  1169. */
  1170. /* set global event handling function */
  1171. srtp_event_handler = func;
  1172. return srtp_err_status_ok;
  1173. }
  1174. /*
  1175. * Check if the given extension header id is / should be encrypted.
  1176. * Returns 1 if yes, otherwise 0.
  1177. */
  1178. static int srtp_protect_extension_header(srtp_stream_ctx_t *stream, int id)
  1179. {
  1180. int *enc_xtn_hdr = stream->enc_xtn_hdr;
  1181. int count = stream->enc_xtn_hdr_count;
  1182. if (!enc_xtn_hdr || count <= 0) {
  1183. return 0;
  1184. }
  1185. while (count > 0) {
  1186. if (*enc_xtn_hdr == id) {
  1187. return 1;
  1188. }
  1189. enc_xtn_hdr++;
  1190. count--;
  1191. }
  1192. return 0;
  1193. }
  1194. /*
  1195. * extensions header encryption RFC 6904
  1196. */
  1197. static srtp_err_status_t srtp_process_header_encryption(
  1198. srtp_stream_ctx_t *stream,
  1199. srtp_hdr_xtnd_t *xtn_hdr,
  1200. srtp_session_keys_t *session_keys)
  1201. {
  1202. srtp_err_status_t status;
  1203. uint8_t keystream[257]; /* Maximum 2 bytes header + 255 bytes data. */
  1204. int keystream_pos;
  1205. uint8_t *xtn_hdr_data = ((uint8_t *)xtn_hdr) + octets_in_rtp_extn_hdr;
  1206. uint8_t *xtn_hdr_end =
  1207. xtn_hdr_data + (ntohs(xtn_hdr->length) * sizeof(uint32_t));
  1208. if (ntohs(xtn_hdr->profile_specific) == 0xbede) {
  1209. /* RFC 5285, section 4.2. One-Byte Header */
  1210. while (xtn_hdr_data < xtn_hdr_end) {
  1211. uint8_t xid = (*xtn_hdr_data & 0xf0) >> 4;
  1212. unsigned int xlen = (*xtn_hdr_data & 0x0f) + 1;
  1213. uint32_t xlen_with_header = 1 + xlen;
  1214. xtn_hdr_data++;
  1215. if (xtn_hdr_data + xlen > xtn_hdr_end)
  1216. return srtp_err_status_parse_err;
  1217. if (xid == 15) {
  1218. /* found header 15, stop further processing. */
  1219. break;
  1220. }
  1221. status = srtp_cipher_output(session_keys->rtp_xtn_hdr_cipher,
  1222. keystream, &xlen_with_header);
  1223. if (status)
  1224. return srtp_err_status_cipher_fail;
  1225. if (srtp_protect_extension_header(stream, xid)) {
  1226. keystream_pos = 1;
  1227. while (xlen > 0) {
  1228. *xtn_hdr_data ^= keystream[keystream_pos++];
  1229. xtn_hdr_data++;
  1230. xlen--;
  1231. }
  1232. } else {
  1233. xtn_hdr_data += xlen;
  1234. }
  1235. /* skip padding bytes. */
  1236. while (xtn_hdr_data < xtn_hdr_end && *xtn_hdr_data == 0) {
  1237. xtn_hdr_data++;
  1238. }
  1239. }
  1240. } else if ((ntohs(xtn_hdr->profile_specific) & 0x1fff) == 0x100) {
  1241. /* RFC 5285, section 4.3. Two-Byte Header */
  1242. while (xtn_hdr_data + 1 < xtn_hdr_end) {
  1243. uint8_t xid = *xtn_hdr_data;
  1244. unsigned int xlen = *(xtn_hdr_data + 1);
  1245. uint32_t xlen_with_header = 2 + xlen;
  1246. xtn_hdr_data += 2;
  1247. if (xtn_hdr_data + xlen > xtn_hdr_end)
  1248. return srtp_err_status_parse_err;
  1249. status = srtp_cipher_output(session_keys->rtp_xtn_hdr_cipher,
  1250. keystream, &xlen_with_header);
  1251. if (status)
  1252. return srtp_err_status_cipher_fail;
  1253. if (xlen > 0 && srtp_protect_extension_header(stream, xid)) {
  1254. keystream_pos = 2;
  1255. while (xlen > 0) {
  1256. *xtn_hdr_data ^= keystream[keystream_pos++];
  1257. xtn_hdr_data++;
  1258. xlen--;
  1259. }
  1260. } else {
  1261. xtn_hdr_data += xlen;
  1262. }
  1263. /* skip padding bytes. */
  1264. while (xtn_hdr_data < xtn_hdr_end && *xtn_hdr_data == 0) {
  1265. xtn_hdr_data++;
  1266. }
  1267. }
  1268. } else {
  1269. /* unsupported extension header format. */
  1270. return srtp_err_status_parse_err;
  1271. }
  1272. return srtp_err_status_ok;
  1273. }
  1274. /*
  1275. * AEAD uses a new IV formation method. This function implements
  1276. * section 8.1. (SRTP IV Formation for AES-GCM) of RFC7714.
  1277. * The calculation is defined as, where (+) is the xor operation:
  1278. *
  1279. *
  1280. * 0 0 0 0 0 0 0 0 0 0 1 1
  1281. * 0 1 2 3 4 5 6 7 8 9 0 1
  1282. * +--+--+--+--+--+--+--+--+--+--+--+--+
  1283. * |00|00| SSRC | ROC | SEQ |---+
  1284. * +--+--+--+--+--+--+--+--+--+--+--+--+ |
  1285. * |
  1286. * +--+--+--+--+--+--+--+--+--+--+--+--+ |
  1287. * | Encryption Salt |->(+)
  1288. * +--+--+--+--+--+--+--+--+--+--+--+--+ |
  1289. * |
  1290. * +--+--+--+--+--+--+--+--+--+--+--+--+ |
  1291. * | Initialization Vector |<--+
  1292. * +--+--+--+--+--+--+--+--+--+--+--+--+*
  1293. *
  1294. * Input: *session_keys - pointer to SRTP stream context session keys,
  1295. * used to retrieve the SALT
  1296. * *iv - Pointer to receive the calculated IV
  1297. * *seq - The ROC and SEQ value to use for the
  1298. * IV calculation.
  1299. * *hdr - The RTP header, used to get the SSRC value
  1300. *
  1301. */
  1302. static void srtp_calc_aead_iv(srtp_session_keys_t *session_keys,
  1303. v128_t *iv,
  1304. srtp_xtd_seq_num_t *seq,
  1305. srtp_hdr_t *hdr)
  1306. {
  1307. v128_t in;
  1308. v128_t salt;
  1309. #ifdef NO_64BIT_MATH
  1310. uint32_t local_roc = ((high32(*seq) << 16) | (low32(*seq) >> 16));
  1311. uint16_t local_seq = (uint16_t)(low32(*seq));
  1312. #else
  1313. uint32_t local_roc = (uint32_t)(*seq >> 16);
  1314. uint16_t local_seq = (uint16_t)*seq;
  1315. #endif
  1316. memset(&in, 0, sizeof(v128_t));
  1317. memset(&salt, 0, sizeof(v128_t));
  1318. in.v16[5] = htons(local_seq);
  1319. local_roc = htonl(local_roc);
  1320. memcpy(&in.v16[3], &local_roc, sizeof(local_roc));
  1321. /*
  1322. * Copy in the RTP SSRC value
  1323. */
  1324. memcpy(&in.v8[2], &hdr->ssrc, 4);
  1325. debug_print(mod_srtp, "Pre-salted RTP IV = %s\n", v128_hex_string(&in));
  1326. /*
  1327. * Get the SALT value from the context
  1328. */
  1329. memcpy(salt.v8, session_keys->salt, SRTP_AEAD_SALT_LEN);
  1330. debug_print(mod_srtp, "RTP SALT = %s\n", v128_hex_string(&salt));
  1331. /*
  1332. * Finally, apply tyhe SALT to the input
  1333. */
  1334. v128_xor(iv, &in, &salt);
  1335. }
  1336. srtp_session_keys_t *srtp_get_session_keys(srtp_stream_ctx_t *stream,
  1337. uint8_t *hdr,
  1338. const unsigned int *pkt_octet_len,
  1339. unsigned int *mki_size)
  1340. {
  1341. unsigned int base_mki_start_location = *pkt_octet_len;
  1342. unsigned int mki_start_location = 0;
  1343. unsigned int tag_len = 0;
  1344. unsigned int i = 0;
  1345. // Determine the authentication tag size
  1346. if (stream->session_keys[0].rtp_cipher->algorithm == SRTP_AES_GCM_128 ||
  1347. stream->session_keys[0].rtp_cipher->algorithm == SRTP_AES_GCM_256) {
  1348. tag_len = 0;
  1349. } else {
  1350. tag_len = srtp_auth_get_tag_length(stream->session_keys[0].rtp_auth);
  1351. }
  1352. if (tag_len > base_mki_start_location) {
  1353. *mki_size = 0;
  1354. return NULL;
  1355. }
  1356. base_mki_start_location -= tag_len;
  1357. for (i = 0; i < stream->num_master_keys; i++) {
  1358. if (stream->session_keys[i].mki_size != 0 &&
  1359. stream->session_keys[i].mki_size <= base_mki_start_location) {
  1360. *mki_size = stream->session_keys[i].mki_size;
  1361. mki_start_location = base_mki_start_location - *mki_size;
  1362. if (memcmp(hdr + mki_start_location, stream->session_keys[i].mki_id,
  1363. *mki_size) == 0) {
  1364. return &stream->session_keys[i];
  1365. }
  1366. }
  1367. }
  1368. *mki_size = 0;
  1369. return NULL;
  1370. }
  1371. static srtp_err_status_t srtp_estimate_index(srtp_rdbx_t *rdbx,
  1372. uint32_t roc,
  1373. srtp_xtd_seq_num_t *est,
  1374. srtp_sequence_number_t seq,
  1375. int *delta)
  1376. {
  1377. #ifdef NO_64BIT_MATH
  1378. uint32_t internal_pkt_idx_reduced;
  1379. uint32_t external_pkt_idx_reduced;
  1380. uint32_t internal_roc;
  1381. uint32_t roc_difference;
  1382. #endif
  1383. #ifdef NO_64BIT_MATH
  1384. *est = (srtp_xtd_seq_num_t)make64(roc >> 16, (roc << 16) | seq);
  1385. *delta = low32(est) - rdbx->index;
  1386. #else
  1387. *est = (srtp_xtd_seq_num_t)(((uint64_t)roc) << 16) | seq;
  1388. *delta = (int)(*est - rdbx->index);
  1389. #endif
  1390. if (*est > rdbx->index) {
  1391. #ifdef NO_64BIT_MATH
  1392. internal_roc = (uint32_t)(rdbx->index >> 16);
  1393. roc_difference = roc - internal_roc;
  1394. if (roc_difference > 1) {
  1395. *delta = 0;
  1396. return srtp_err_status_pkt_idx_adv;
  1397. }
  1398. internal_pkt_idx_reduced = (uint32_t)(rdbx->index & 0xFFFF);
  1399. external_pkt_idx_reduced = (uint32_t)((roc_difference << 16) | seq);
  1400. if (external_pkt_idx_reduced - internal_pkt_idx_reduced >
  1401. seq_num_median) {
  1402. *delta = 0;
  1403. return srtp_err_status_pkt_idx_adv;
  1404. }
  1405. #else
  1406. if (*est - rdbx->index > seq_num_median) {
  1407. *delta = 0;
  1408. return srtp_err_status_pkt_idx_adv;
  1409. }
  1410. #endif
  1411. } else if (*est < rdbx->index) {
  1412. #ifdef NO_64BIT_MATH
  1413. internal_roc = (uint32_t)(rdbx->index >> 16);
  1414. roc_difference = internal_roc - roc;
  1415. if (roc_difference > 1) {
  1416. *delta = 0;
  1417. return srtp_err_status_pkt_idx_adv;
  1418. }
  1419. internal_pkt_idx_reduced =
  1420. (uint32_t)((roc_difference << 16) | rdbx->index & 0xFFFF);
  1421. external_pkt_idx_reduced = (uint32_t)(seq);
  1422. if (internal_pkt_idx_reduced - external_pkt_idx_reduced >
  1423. seq_num_median) {
  1424. *delta = 0;
  1425. return srtp_err_status_pkt_idx_old;
  1426. }
  1427. #else
  1428. if (rdbx->index - *est > seq_num_median) {
  1429. *delta = 0;
  1430. return srtp_err_status_pkt_idx_old;
  1431. }
  1432. #endif
  1433. }
  1434. return srtp_err_status_ok;
  1435. }
  1436. static srtp_err_status_t srtp_get_est_pkt_index(srtp_hdr_t *hdr,
  1437. srtp_stream_ctx_t *stream,
  1438. srtp_xtd_seq_num_t *est,
  1439. int *delta)
  1440. {
  1441. srtp_err_status_t result = srtp_err_status_ok;
  1442. if (stream->pending_roc) {
  1443. result = srtp_estimate_index(&stream->rtp_rdbx, stream->pending_roc,
  1444. est, ntohs(hdr->seq), delta);
  1445. } else {
  1446. /* estimate packet index from seq. num. in header */
  1447. *delta =
  1448. srtp_rdbx_estimate_index(&stream->rtp_rdbx, est, ntohs(hdr->seq));
  1449. }
  1450. #ifdef NO_64BIT_MATH
  1451. debug_print2(mod_srtp, "estimated u_packet index: %08x%08x", high32(*est),
  1452. low32(*est));
  1453. #else
  1454. debug_print(mod_srtp, "estimated u_packet index: %016" PRIx64, *est);
  1455. #endif
  1456. return result;
  1457. }
  1458. /*
  1459. * This function handles outgoing SRTP packets while in AEAD mode,
  1460. * which currently supports AES-GCM encryption. All packets are
  1461. * encrypted and authenticated.
  1462. */
  1463. static srtp_err_status_t srtp_protect_aead(srtp_ctx_t *ctx,
  1464. srtp_stream_ctx_t *stream,
  1465. void *rtp_hdr,
  1466. unsigned int *pkt_octet_len,
  1467. srtp_session_keys_t *session_keys,
  1468. unsigned int use_mki)
  1469. {
  1470. srtp_hdr_t *hdr = (srtp_hdr_t *)rtp_hdr;
  1471. uint32_t *enc_start; /* pointer to start of encrypted portion */
  1472. int enc_octet_len = 0; /* number of octets in encrypted portion */
  1473. srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */
  1474. int delta; /* delta of local pkt idx and that in hdr */
  1475. srtp_err_status_t status;
  1476. uint32_t tag_len;
  1477. v128_t iv;
  1478. unsigned int aad_len;
  1479. srtp_hdr_xtnd_t *xtn_hdr = NULL;
  1480. unsigned int mki_size = 0;
  1481. uint8_t *mki_location = NULL;
  1482. debug_print0(mod_srtp, "function srtp_protect_aead");
  1483. /*
  1484. * update the key usage limit, and check it to make sure that we
  1485. * didn't just hit either the soft limit or the hard limit, and call
  1486. * the event handler if we hit either.
  1487. */
  1488. switch (srtp_key_limit_update(session_keys->limit)) {
  1489. case srtp_key_event_normal:
  1490. break;
  1491. case srtp_key_event_hard_limit:
  1492. srtp_handle_event(ctx, stream, event_key_hard_limit);
  1493. return srtp_err_status_key_expired;
  1494. case srtp_key_event_soft_limit:
  1495. default:
  1496. srtp_handle_event(ctx, stream, event_key_soft_limit);
  1497. break;
  1498. }
  1499. /* get tag length from stream */
  1500. tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth);
  1501. /*
  1502. * find starting point for encryption and length of data to be
  1503. * encrypted - the encrypted portion starts after the rtp header
  1504. * extension, if present; otherwise, it starts after the last csrc,
  1505. * if any are present
  1506. */
  1507. enc_start = (uint32_t *)hdr + uint32s_in_rtp_header + hdr->cc;
  1508. if (hdr->x == 1) {
  1509. xtn_hdr = (srtp_hdr_xtnd_t *)enc_start;
  1510. enc_start += (ntohs(xtn_hdr->length) + 1);
  1511. }
  1512. /* note: the passed size is without the auth tag */
  1513. if (!((uint8_t *)enc_start <= (uint8_t *)hdr + *pkt_octet_len))
  1514. return srtp_err_status_parse_err;
  1515. enc_octet_len =
  1516. (int)(*pkt_octet_len - ((uint8_t *)enc_start - (uint8_t *)hdr));
  1517. if (enc_octet_len < 0)
  1518. return srtp_err_status_parse_err;
  1519. /*
  1520. * estimate the packet index using the start of the replay window
  1521. * and the sequence number from the header
  1522. */
  1523. delta = srtp_rdbx_estimate_index(&stream->rtp_rdbx, &est, ntohs(hdr->seq));
  1524. status = srtp_rdbx_check(&stream->rtp_rdbx, delta);
  1525. if (status) {
  1526. if (status != srtp_err_status_replay_fail || !stream->allow_repeat_tx) {
  1527. return status; /* we've been asked to reuse an index */
  1528. }
  1529. } else {
  1530. srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
  1531. }
  1532. #ifdef NO_64BIT_MATH
  1533. debug_print2(mod_srtp, "estimated packet index: %08x%08x", high32(est),
  1534. low32(est));
  1535. #else
  1536. debug_print(mod_srtp, "estimated packet index: %016" PRIx64, est);
  1537. #endif
  1538. /*
  1539. * AEAD uses a new IV formation method
  1540. */
  1541. srtp_calc_aead_iv(session_keys, &iv, &est, hdr);
  1542. /* shift est, put into network byte order */
  1543. #ifdef NO_64BIT_MATH
  1544. est = be64_to_cpu(
  1545. make64((high32(est) << 16) | (low32(est) >> 16), low32(est) << 16));
  1546. #else
  1547. est = be64_to_cpu(est << 16);
  1548. #endif
  1549. status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv,
  1550. srtp_direction_encrypt);
  1551. if (!status && session_keys->rtp_xtn_hdr_cipher) {
  1552. iv.v32[0] = 0;
  1553. iv.v32[1] = hdr->ssrc;
  1554. iv.v64[1] = est;
  1555. status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher,
  1556. (uint8_t *)&iv, srtp_direction_encrypt);
  1557. }
  1558. if (status) {
  1559. return srtp_err_status_cipher_fail;
  1560. }
  1561. if (xtn_hdr && session_keys->rtp_xtn_hdr_cipher) {
  1562. /*
  1563. * extensions header encryption RFC 6904
  1564. */
  1565. status = srtp_process_header_encryption(stream, xtn_hdr, session_keys);
  1566. if (status) {
  1567. return status;
  1568. }
  1569. }
  1570. /*
  1571. * Set the AAD over the RTP header
  1572. */
  1573. aad_len = (uint8_t *)enc_start - (uint8_t *)hdr;
  1574. status =
  1575. srtp_cipher_set_aad(session_keys->rtp_cipher, (uint8_t *)hdr, aad_len);
  1576. if (status) {
  1577. return (srtp_err_status_cipher_fail);
  1578. }
  1579. /* Encrypt the payload */
  1580. status = srtp_cipher_encrypt(session_keys->rtp_cipher, (uint8_t *)enc_start,
  1581. (unsigned int *)&enc_octet_len);
  1582. if (status) {
  1583. return srtp_err_status_cipher_fail;
  1584. }
  1585. /*
  1586. * If we're doing GCM, we need to get the tag
  1587. * and append that to the output
  1588. */
  1589. status =
  1590. srtp_cipher_get_tag(session_keys->rtp_cipher,
  1591. (uint8_t *)enc_start + enc_octet_len, &tag_len);
  1592. if (status) {
  1593. return (srtp_err_status_cipher_fail);
  1594. }
  1595. mki_location = (uint8_t *)hdr + *pkt_octet_len + tag_len;
  1596. mki_size = srtp_inject_mki(mki_location, session_keys, use_mki);
  1597. /* increase the packet length by the length of the auth tag */
  1598. *pkt_octet_len += tag_len;
  1599. /* increase the packet length by the length of the mki_size */
  1600. *pkt_octet_len += mki_size;
  1601. return srtp_err_status_ok;
  1602. }
  1603. /*
  1604. * This function handles incoming SRTP packets while in AEAD mode,
  1605. * which currently supports AES-GCM encryption. All packets are
  1606. * encrypted and authenticated. Note, the auth tag is at the end
  1607. * of the packet stream and is automatically checked by GCM
  1608. * when decrypting the payload.
  1609. */
  1610. static srtp_err_status_t srtp_unprotect_aead(srtp_ctx_t *ctx,
  1611. srtp_stream_ctx_t *stream,
  1612. int delta,
  1613. srtp_xtd_seq_num_t est,
  1614. void *srtp_hdr,
  1615. unsigned int *pkt_octet_len,
  1616. srtp_session_keys_t *session_keys,
  1617. unsigned int mki_size)
  1618. {
  1619. srtp_hdr_t *hdr = (srtp_hdr_t *)srtp_hdr;
  1620. uint32_t *enc_start; /* pointer to start of encrypted portion */
  1621. unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */
  1622. v128_t iv;
  1623. srtp_err_status_t status;
  1624. int tag_len;
  1625. unsigned int aad_len;
  1626. srtp_hdr_xtnd_t *xtn_hdr = NULL;
  1627. debug_print0(mod_srtp, "function srtp_unprotect_aead");
  1628. #ifdef NO_64BIT_MATH
  1629. debug_print2(mod_srtp, "estimated u_packet index: %08x%08x", high32(est),
  1630. low32(est));
  1631. #else
  1632. debug_print(mod_srtp, "estimated u_packet index: %016" PRIx64, est);
  1633. #endif
  1634. /* get tag length from stream */
  1635. tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth);
  1636. /*
  1637. * AEAD uses a new IV formation method
  1638. */
  1639. srtp_calc_aead_iv(session_keys, &iv, &est, hdr);
  1640. status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv,
  1641. srtp_direction_decrypt);
  1642. if (!status && session_keys->rtp_xtn_hdr_cipher) {
  1643. iv.v32[0] = 0;
  1644. iv.v32[1] = hdr->ssrc;
  1645. #ifdef NO_64BIT_MATH
  1646. iv.v64[1] = be64_to_cpu(
  1647. make64((high32(est) << 16) | (low32(est) >> 16), low32(est) << 16));
  1648. #else
  1649. iv.v64[1] = be64_to_cpu(est << 16);
  1650. #endif
  1651. status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher,
  1652. (uint8_t *)&iv, srtp_direction_encrypt);
  1653. }
  1654. if (status) {
  1655. return srtp_err_status_cipher_fail;
  1656. }
  1657. /*
  1658. * find starting point for decryption and length of data to be
  1659. * decrypted - the encrypted portion starts after the rtp header
  1660. * extension, if present; otherwise, it starts after the last csrc,
  1661. * if any are present
  1662. */
  1663. enc_start = (uint32_t *)hdr + uint32s_in_rtp_header + hdr->cc;
  1664. if (hdr->x == 1) {
  1665. xtn_hdr = (srtp_hdr_xtnd_t *)enc_start;
  1666. enc_start += (ntohs(xtn_hdr->length) + 1);
  1667. }
  1668. if (!((uint8_t *)enc_start <=
  1669. (uint8_t *)hdr + (*pkt_octet_len - tag_len - mki_size)))
  1670. return srtp_err_status_parse_err;
  1671. /*
  1672. * We pass the tag down to the cipher when doing GCM mode
  1673. */
  1674. enc_octet_len = (unsigned int)(*pkt_octet_len - mki_size -
  1675. ((uint8_t *)enc_start - (uint8_t *)hdr));
  1676. /*
  1677. * Sanity check the encrypted payload length against
  1678. * the tag size. It must always be at least as large
  1679. * as the tag length.
  1680. */
  1681. if (enc_octet_len < (unsigned int)tag_len) {
  1682. return srtp_err_status_cipher_fail;
  1683. }
  1684. /*
  1685. * update the key usage limit, and check it to make sure that we
  1686. * didn't just hit either the soft limit or the hard limit, and call
  1687. * the event handler if we hit either.
  1688. */
  1689. switch (srtp_key_limit_update(session_keys->limit)) {
  1690. case srtp_key_event_normal:
  1691. break;
  1692. case srtp_key_event_soft_limit:
  1693. srtp_handle_event(ctx, stream, event_key_soft_limit);
  1694. break;
  1695. case srtp_key_event_hard_limit:
  1696. srtp_handle_event(ctx, stream, event_key_hard_limit);
  1697. return srtp_err_status_key_expired;
  1698. default:
  1699. break;
  1700. }
  1701. /*
  1702. * Set the AAD for AES-GCM, which is the RTP header
  1703. */
  1704. aad_len = (uint8_t *)enc_start - (uint8_t *)hdr;
  1705. status =
  1706. srtp_cipher_set_aad(session_keys->rtp_cipher, (uint8_t *)hdr, aad_len);
  1707. if (status) {
  1708. return (srtp_err_status_cipher_fail);
  1709. }
  1710. /* Decrypt the ciphertext. This also checks the auth tag based
  1711. * on the AAD we just specified above */
  1712. status = srtp_cipher_decrypt(session_keys->rtp_cipher, (uint8_t *)enc_start,
  1713. &enc_octet_len);
  1714. if (status) {
  1715. return status;
  1716. }
  1717. if (xtn_hdr && session_keys->rtp_xtn_hdr_cipher) {
  1718. /*
  1719. * extensions header encryption RFC 6904
  1720. */
  1721. status = srtp_process_header_encryption(stream, xtn_hdr, session_keys);
  1722. if (status) {
  1723. return status;
  1724. }
  1725. }
  1726. /*
  1727. * verify that stream is for received traffic - this check will
  1728. * detect SSRC collisions, since a stream that appears in both
  1729. * srtp_protect() and srtp_unprotect() will fail this test in one of
  1730. * those functions.
  1731. *
  1732. * we do this check *after* the authentication check, so that the
  1733. * latter check will catch any attempts to fool us into thinking
  1734. * that we've got a collision
  1735. */
  1736. if (stream->direction != dir_srtp_receiver) {
  1737. if (stream->direction == dir_unknown) {
  1738. stream->direction = dir_srtp_receiver;
  1739. } else {
  1740. srtp_handle_event(ctx, stream, event_ssrc_collision);
  1741. }
  1742. }
  1743. /*
  1744. * if the stream is a 'provisional' one, in which the template context
  1745. * is used, then we need to allocate a new stream at this point, since
  1746. * the authentication passed
  1747. */
  1748. if (stream == ctx->stream_template) {
  1749. srtp_stream_ctx_t *new_stream;
  1750. /*
  1751. * allocate and initialize a new stream
  1752. *
  1753. * note that we indicate failure if we can't allocate the new
  1754. * stream, and some implementations will want to not return
  1755. * failure here
  1756. */
  1757. status =
  1758. srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
  1759. if (status) {
  1760. return status;
  1761. }
  1762. /* add new stream to the head of the stream_list */
  1763. new_stream->next = ctx->stream_list;
  1764. ctx->stream_list = new_stream;
  1765. /* set stream (the pointer used in this function) */
  1766. stream = new_stream;
  1767. }
  1768. /*
  1769. * the message authentication function passed, so add the packet
  1770. * index into the replay database
  1771. */
  1772. srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
  1773. /* decrease the packet length by the length of the auth tag */
  1774. *pkt_octet_len -= tag_len;
  1775. /* decrease the packet length by the length of the mki_size */
  1776. *pkt_octet_len -= mki_size;
  1777. return srtp_err_status_ok;
  1778. }
  1779. srtp_err_status_t srtp_protect(srtp_ctx_t *ctx,
  1780. void *rtp_hdr,
  1781. int *pkt_octet_len)
  1782. {
  1783. return srtp_protect_mki(ctx, rtp_hdr, pkt_octet_len, 0, 0);
  1784. }
  1785. srtp_err_status_t srtp_protect_mki(srtp_ctx_t *ctx,
  1786. void *rtp_hdr,
  1787. int *pkt_octet_len,
  1788. unsigned int use_mki,
  1789. unsigned int mki_index)
  1790. {
  1791. srtp_hdr_t *hdr = (srtp_hdr_t *)rtp_hdr;
  1792. uint32_t *enc_start; /* pointer to start of encrypted portion */
  1793. uint32_t *auth_start; /* pointer to start of auth. portion */
  1794. int enc_octet_len = 0; /* number of octets in encrypted portion */
  1795. srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */
  1796. int delta; /* delta of local pkt idx and that in hdr */
  1797. uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
  1798. srtp_err_status_t status;
  1799. int tag_len;
  1800. srtp_stream_ctx_t *stream;
  1801. uint32_t prefix_len;
  1802. srtp_hdr_xtnd_t *xtn_hdr = NULL;
  1803. unsigned int mki_size = 0;
  1804. srtp_session_keys_t *session_keys = NULL;
  1805. uint8_t *mki_location = NULL;
  1806. int advance_packet_index = 0;
  1807. debug_print0(mod_srtp, "function srtp_protect");
  1808. /* we assume the hdr is 32-bit aligned to start */
  1809. /* Verify RTP header */
  1810. status = srtp_validate_rtp_header(rtp_hdr, pkt_octet_len);
  1811. if (status)
  1812. return status;
  1813. /* check the packet length - it must at least contain a full header */
  1814. if (*pkt_octet_len < octets_in_rtp_header)
  1815. return srtp_err_status_bad_param;
  1816. /*
  1817. * look up ssrc in srtp_stream list, and process the packet with
  1818. * the appropriate stream. if we haven't seen this stream before,
  1819. * there's a template key for this srtp_session, and the cipher
  1820. * supports key-sharing, then we assume that a new stream using
  1821. * that key has just started up
  1822. */
  1823. stream = srtp_get_stream(ctx, hdr->ssrc);
  1824. if (stream == NULL) {
  1825. if (ctx->stream_template != NULL) {
  1826. srtp_stream_ctx_t *new_stream;
  1827. /* allocate and initialize a new stream */
  1828. status =
  1829. srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
  1830. if (status)
  1831. return status;
  1832. /* add new stream to the head of the stream_list */
  1833. new_stream->next = ctx->stream_list;
  1834. ctx->stream_list = new_stream;
  1835. /* set direction to outbound */
  1836. new_stream->direction = dir_srtp_sender;
  1837. /* set stream (the pointer used in this function) */
  1838. stream = new_stream;
  1839. } else {
  1840. /* no template stream, so we return an error */
  1841. return srtp_err_status_no_ctx;
  1842. }
  1843. }
  1844. /*
  1845. * verify that stream is for sending traffic - this check will
  1846. * detect SSRC collisions, since a stream that appears in both
  1847. * srtp_protect() and srtp_unprotect() will fail this test in one of
  1848. * those functions.
  1849. */
  1850. if (stream->direction != dir_srtp_sender) {
  1851. if (stream->direction == dir_unknown) {
  1852. stream->direction = dir_srtp_sender;
  1853. } else {
  1854. srtp_handle_event(ctx, stream, event_ssrc_collision);
  1855. }
  1856. }
  1857. session_keys =
  1858. srtp_get_session_keys_with_mki_index(stream, use_mki, mki_index);
  1859. if (session_keys == NULL)
  1860. return srtp_err_status_bad_mki;
  1861. /*
  1862. * Check if this is an AEAD stream (GCM mode). If so, then dispatch
  1863. * the request to our AEAD handler.
  1864. */
  1865. if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 ||
  1866. session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) {
  1867. return srtp_protect_aead(ctx, stream, rtp_hdr,
  1868. (unsigned int *)pkt_octet_len, session_keys,
  1869. use_mki);
  1870. }
  1871. /*
  1872. * update the key usage limit, and check it to make sure that we
  1873. * didn't just hit either the soft limit or the hard limit, and call
  1874. * the event handler if we hit either.
  1875. */
  1876. switch (srtp_key_limit_update(session_keys->limit)) {
  1877. case srtp_key_event_normal:
  1878. break;
  1879. case srtp_key_event_soft_limit:
  1880. srtp_handle_event(ctx, stream, event_key_soft_limit);
  1881. break;
  1882. case srtp_key_event_hard_limit:
  1883. srtp_handle_event(ctx, stream, event_key_hard_limit);
  1884. return srtp_err_status_key_expired;
  1885. default:
  1886. break;
  1887. }
  1888. /* get tag length from stream */
  1889. tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth);
  1890. /*
  1891. * find starting point for encryption and length of data to be
  1892. * encrypted - the encrypted portion starts after the rtp header
  1893. * extension, if present; otherwise, it starts after the last csrc,
  1894. * if any are present
  1895. *
  1896. * if we're not providing confidentiality, set enc_start to NULL
  1897. */
  1898. if (stream->rtp_services & sec_serv_conf) {
  1899. enc_start = (uint32_t *)hdr + uint32s_in_rtp_header + hdr->cc;
  1900. if (hdr->x == 1) {
  1901. xtn_hdr = (srtp_hdr_xtnd_t *)enc_start;
  1902. enc_start += (ntohs(xtn_hdr->length) + 1);
  1903. }
  1904. /* note: the passed size is without the auth tag */
  1905. if (!((uint8_t *)enc_start <= (uint8_t *)hdr + *pkt_octet_len))
  1906. return srtp_err_status_parse_err;
  1907. enc_octet_len =
  1908. (int)(*pkt_octet_len - ((uint8_t *)enc_start - (uint8_t *)hdr));
  1909. if (enc_octet_len < 0)
  1910. return srtp_err_status_parse_err;
  1911. } else {
  1912. enc_start = NULL;
  1913. }
  1914. mki_location = (uint8_t *)hdr + *pkt_octet_len;
  1915. mki_size = srtp_inject_mki(mki_location, session_keys, use_mki);
  1916. /*
  1917. * if we're providing authentication, set the auth_start and auth_tag
  1918. * pointers to the proper locations; otherwise, set auth_start to NULL
  1919. * to indicate that no authentication is needed
  1920. */
  1921. if (stream->rtp_services & sec_serv_auth) {
  1922. auth_start = (uint32_t *)hdr;
  1923. auth_tag = (uint8_t *)hdr + *pkt_octet_len + mki_size;
  1924. } else {
  1925. auth_start = NULL;
  1926. auth_tag = NULL;
  1927. }
  1928. /*
  1929. * estimate the packet index using the start of the replay window
  1930. * and the sequence number from the header
  1931. */
  1932. status = srtp_get_est_pkt_index(hdr, stream, &est, &delta);
  1933. if (status && (status != srtp_err_status_pkt_idx_adv))
  1934. return status;
  1935. if (status == srtp_err_status_pkt_idx_adv)
  1936. advance_packet_index = 1;
  1937. if (advance_packet_index) {
  1938. srtp_rdbx_set_roc_seq(&stream->rtp_rdbx, (uint32_t)(est >> 16),
  1939. (uint16_t)(est & 0xFFFF));
  1940. stream->pending_roc = 0;
  1941. srtp_rdbx_add_index(&stream->rtp_rdbx, 0);
  1942. } else {
  1943. status = srtp_rdbx_check(&stream->rtp_rdbx, delta);
  1944. if (status) {
  1945. if (status != srtp_err_status_replay_fail ||
  1946. !stream->allow_repeat_tx)
  1947. return status; /* we've been asked to reuse an index */
  1948. }
  1949. srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
  1950. }
  1951. #ifdef NO_64BIT_MATH
  1952. debug_print2(mod_srtp, "estimated packet index: %08x%08x", high32(est),
  1953. low32(est));
  1954. #else
  1955. debug_print(mod_srtp, "estimated packet index: %016" PRIx64, est);
  1956. #endif
  1957. /*
  1958. * if we're using rindael counter mode, set nonce and seq
  1959. */
  1960. if (session_keys->rtp_cipher->type->id == SRTP_AES_ICM_128 ||
  1961. session_keys->rtp_cipher->type->id == SRTP_AES_ICM_192 ||
  1962. session_keys->rtp_cipher->type->id == SRTP_AES_ICM_256) {
  1963. v128_t iv;
  1964. iv.v32[0] = 0;
  1965. iv.v32[1] = hdr->ssrc;
  1966. #ifdef NO_64BIT_MATH
  1967. iv.v64[1] = be64_to_cpu(
  1968. make64((high32(est) << 16) | (low32(est) >> 16), low32(est) << 16));
  1969. #else
  1970. iv.v64[1] = be64_to_cpu(est << 16);
  1971. #endif
  1972. status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv,
  1973. srtp_direction_encrypt);
  1974. if (!status && session_keys->rtp_xtn_hdr_cipher) {
  1975. status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher,
  1976. (uint8_t *)&iv, srtp_direction_encrypt);
  1977. }
  1978. } else {
  1979. v128_t iv;
  1980. /* otherwise, set the index to est */
  1981. #ifdef NO_64BIT_MATH
  1982. iv.v32[0] = 0;
  1983. iv.v32[1] = 0;
  1984. #else
  1985. iv.v64[0] = 0;
  1986. #endif
  1987. iv.v64[1] = be64_to_cpu(est);
  1988. status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv,
  1989. srtp_direction_encrypt);
  1990. if (!status && session_keys->rtp_xtn_hdr_cipher) {
  1991. status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher,
  1992. (uint8_t *)&iv, srtp_direction_encrypt);
  1993. }
  1994. }
  1995. if (status)
  1996. return srtp_err_status_cipher_fail;
  1997. /* shift est, put into network byte order */
  1998. #ifdef NO_64BIT_MATH
  1999. est = be64_to_cpu(
  2000. make64((high32(est) << 16) | (low32(est) >> 16), low32(est) << 16));
  2001. #else
  2002. est = be64_to_cpu(est << 16);
  2003. #endif
  2004. /*
  2005. * if we're authenticating using a universal hash, put the keystream
  2006. * prefix into the authentication tag
  2007. */
  2008. if (auth_start) {
  2009. prefix_len = srtp_auth_get_prefix_length(session_keys->rtp_auth);
  2010. if (prefix_len) {
  2011. status = srtp_cipher_output(session_keys->rtp_cipher, auth_tag,
  2012. &prefix_len);
  2013. if (status)
  2014. return srtp_err_status_cipher_fail;
  2015. debug_print(mod_srtp, "keystream prefix: %s",
  2016. srtp_octet_string_hex_string(auth_tag, prefix_len));
  2017. }
  2018. }
  2019. if (xtn_hdr && session_keys->rtp_xtn_hdr_cipher) {
  2020. /*
  2021. * extensions header encryption RFC 6904
  2022. */
  2023. status = srtp_process_header_encryption(stream, xtn_hdr, session_keys);
  2024. if (status) {
  2025. return status;
  2026. }
  2027. }
  2028. /* if we're encrypting, exor keystream into the message */
  2029. if (enc_start) {
  2030. status =
  2031. srtp_cipher_encrypt(session_keys->rtp_cipher, (uint8_t *)enc_start,
  2032. (unsigned int *)&enc_octet_len);
  2033. if (status)
  2034. return srtp_err_status_cipher_fail;
  2035. }
  2036. /*
  2037. * if we're authenticating, run authentication function and put result
  2038. * into the auth_tag
  2039. */
  2040. if (auth_start) {
  2041. /* initialize auth func context */
  2042. status = srtp_auth_start(session_keys->rtp_auth);
  2043. if (status)
  2044. return status;
  2045. /* run auth func over packet */
  2046. status = srtp_auth_update(session_keys->rtp_auth, (uint8_t *)auth_start,
  2047. *pkt_octet_len);
  2048. if (status)
  2049. return status;
  2050. /* run auth func over ROC, put result into auth_tag */
  2051. debug_print(mod_srtp, "estimated packet index: %016" PRIx64, est);
  2052. status = srtp_auth_compute(session_keys->rtp_auth, (uint8_t *)&est, 4,
  2053. auth_tag);
  2054. debug_print(mod_srtp, "srtp auth tag: %s",
  2055. srtp_octet_string_hex_string(auth_tag, tag_len));
  2056. if (status)
  2057. return srtp_err_status_auth_fail;
  2058. }
  2059. if (auth_tag) {
  2060. /* increase the packet length by the length of the auth tag */
  2061. *pkt_octet_len += tag_len;
  2062. }
  2063. if (use_mki) {
  2064. /* increate the packet length by the mki size */
  2065. *pkt_octet_len += mki_size;
  2066. }
  2067. return srtp_err_status_ok;
  2068. }
  2069. srtp_err_status_t srtp_unprotect(srtp_ctx_t *ctx,
  2070. void *srtp_hdr,
  2071. int *pkt_octet_len)
  2072. {
  2073. return srtp_unprotect_mki(ctx, srtp_hdr, pkt_octet_len, 0);
  2074. }
  2075. srtp_err_status_t srtp_unprotect_mki(srtp_ctx_t *ctx,
  2076. void *srtp_hdr,
  2077. int *pkt_octet_len,
  2078. unsigned int use_mki)
  2079. {
  2080. srtp_hdr_t *hdr = (srtp_hdr_t *)srtp_hdr;
  2081. uint32_t *enc_start; /* pointer to start of encrypted portion */
  2082. uint32_t *auth_start; /* pointer to start of auth. portion */
  2083. unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */
  2084. uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
  2085. srtp_xtd_seq_num_t est; /* estimated xtd_seq_num_t of *hdr */
  2086. int delta; /* delta of local pkt idx and that in hdr */
  2087. v128_t iv;
  2088. srtp_err_status_t status;
  2089. srtp_stream_ctx_t *stream;
  2090. uint8_t tmp_tag[SRTP_MAX_TAG_LEN];
  2091. uint32_t tag_len, prefix_len;
  2092. srtp_hdr_xtnd_t *xtn_hdr = NULL;
  2093. unsigned int mki_size = 0;
  2094. srtp_session_keys_t *session_keys = NULL;
  2095. int advance_packet_index = 0;
  2096. uint32_t roc_to_set = 0;
  2097. uint16_t seq_to_set = 0;
  2098. debug_print0(mod_srtp, "function srtp_unprotect");
  2099. /* we assume the hdr is 32-bit aligned to start */
  2100. /* Verify RTP header */
  2101. status = srtp_validate_rtp_header(srtp_hdr, pkt_octet_len);
  2102. if (status)
  2103. return status;
  2104. /* check the packet length - it must at least contain a full header */
  2105. if (*pkt_octet_len < octets_in_rtp_header)
  2106. return srtp_err_status_bad_param;
  2107. /*
  2108. * look up ssrc in srtp_stream list, and process the packet with
  2109. * the appropriate stream. if we haven't seen this stream before,
  2110. * there's only one key for this srtp_session, and the cipher
  2111. * supports key-sharing, then we assume that a new stream using
  2112. * that key has just started up
  2113. */
  2114. stream = srtp_get_stream(ctx, hdr->ssrc);
  2115. if (stream == NULL) {
  2116. if (ctx->stream_template != NULL) {
  2117. stream = ctx->stream_template;
  2118. debug_print(mod_srtp, "using provisional stream (SSRC: 0x%08x)",
  2119. ntohl(hdr->ssrc));
  2120. /*
  2121. * set estimated packet index to sequence number from header,
  2122. * and set delta equal to the same value
  2123. */
  2124. #ifdef NO_64BIT_MATH
  2125. est = (srtp_xtd_seq_num_t)make64(0, ntohs(hdr->seq));
  2126. delta = low32(est);
  2127. #else
  2128. est = (srtp_xtd_seq_num_t)ntohs(hdr->seq);
  2129. delta = (int)est;
  2130. #endif
  2131. } else {
  2132. /*
  2133. * no stream corresponding to SSRC found, and we don't do
  2134. * key-sharing, so return an error
  2135. */
  2136. return srtp_err_status_no_ctx;
  2137. }
  2138. } else {
  2139. status = srtp_get_est_pkt_index(hdr, stream, &est, &delta);
  2140. if (status && (status != srtp_err_status_pkt_idx_adv))
  2141. return status;
  2142. if (status == srtp_err_status_pkt_idx_adv) {
  2143. advance_packet_index = 1;
  2144. roc_to_set = (uint32_t)(est >> 16);
  2145. seq_to_set = (uint16_t)(est & 0xFFFF);
  2146. }
  2147. /* check replay database */
  2148. if (!advance_packet_index) {
  2149. status = srtp_rdbx_check(&stream->rtp_rdbx, delta);
  2150. if (status)
  2151. return status;
  2152. }
  2153. }
  2154. #ifdef NO_64BIT_MATH
  2155. debug_print2(mod_srtp, "estimated u_packet index: %08x%08x", high32(est),
  2156. low32(est));
  2157. #else
  2158. debug_print(mod_srtp, "estimated u_packet index: %016" PRIx64, est);
  2159. #endif
  2160. /* Determine if MKI is being used and what session keys should be used */
  2161. if (use_mki) {
  2162. session_keys = srtp_get_session_keys(
  2163. stream, (uint8_t *)hdr, (const unsigned int *)pkt_octet_len,
  2164. &mki_size);
  2165. if (session_keys == NULL)
  2166. return srtp_err_status_bad_mki;
  2167. } else {
  2168. session_keys = &stream->session_keys[0];
  2169. }
  2170. /*
  2171. * Check if this is an AEAD stream (GCM mode). If so, then dispatch
  2172. * the request to our AEAD handler.
  2173. */
  2174. if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 ||
  2175. session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) {
  2176. return srtp_unprotect_aead(ctx, stream, delta, est, srtp_hdr,
  2177. (unsigned int *)pkt_octet_len, session_keys,
  2178. mki_size);
  2179. }
  2180. /* get tag length from stream */
  2181. tag_len = srtp_auth_get_tag_length(session_keys->rtp_auth);
  2182. /*
  2183. * set the cipher's IV properly, depending on whatever cipher we
  2184. * happen to be using
  2185. */
  2186. if (session_keys->rtp_cipher->type->id == SRTP_AES_ICM_128 ||
  2187. session_keys->rtp_cipher->type->id == SRTP_AES_ICM_192 ||
  2188. session_keys->rtp_cipher->type->id == SRTP_AES_ICM_256) {
  2189. /* aes counter mode */
  2190. iv.v32[0] = 0;
  2191. iv.v32[1] = hdr->ssrc; /* still in network order */
  2192. #ifdef NO_64BIT_MATH
  2193. iv.v64[1] = be64_to_cpu(
  2194. make64((high32(est) << 16) | (low32(est) >> 16), low32(est) << 16));
  2195. #else
  2196. iv.v64[1] = be64_to_cpu(est << 16);
  2197. #endif
  2198. status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv,
  2199. srtp_direction_decrypt);
  2200. if (!status && session_keys->rtp_xtn_hdr_cipher) {
  2201. status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher,
  2202. (uint8_t *)&iv, srtp_direction_decrypt);
  2203. }
  2204. } else {
  2205. /* no particular format - set the iv to the pakcet index */
  2206. #ifdef NO_64BIT_MATH
  2207. iv.v32[0] = 0;
  2208. iv.v32[1] = 0;
  2209. #else
  2210. iv.v64[0] = 0;
  2211. #endif
  2212. iv.v64[1] = be64_to_cpu(est);
  2213. status = srtp_cipher_set_iv(session_keys->rtp_cipher, (uint8_t *)&iv,
  2214. srtp_direction_decrypt);
  2215. if (!status && session_keys->rtp_xtn_hdr_cipher) {
  2216. status = srtp_cipher_set_iv(session_keys->rtp_xtn_hdr_cipher,
  2217. (uint8_t *)&iv, srtp_direction_decrypt);
  2218. }
  2219. }
  2220. if (status)
  2221. return srtp_err_status_cipher_fail;
  2222. /* shift est, put into network byte order */
  2223. #ifdef NO_64BIT_MATH
  2224. est = be64_to_cpu(
  2225. make64((high32(est) << 16) | (low32(est) >> 16), low32(est) << 16));
  2226. #else
  2227. est = be64_to_cpu(est << 16);
  2228. #endif
  2229. /*
  2230. * find starting point for decryption and length of data to be
  2231. * decrypted - the encrypted portion starts after the rtp header
  2232. * extension, if present; otherwise, it starts after the last csrc,
  2233. * if any are present
  2234. *
  2235. * if we're not providing confidentiality, set enc_start to NULL
  2236. */
  2237. if (stream->rtp_services & sec_serv_conf) {
  2238. enc_start = (uint32_t *)hdr + uint32s_in_rtp_header + hdr->cc;
  2239. if (hdr->x == 1) {
  2240. xtn_hdr = (srtp_hdr_xtnd_t *)enc_start;
  2241. enc_start += (ntohs(xtn_hdr->length) + 1);
  2242. }
  2243. if (!((uint8_t *)enc_start <=
  2244. (uint8_t *)hdr + (*pkt_octet_len - tag_len - mki_size)))
  2245. return srtp_err_status_parse_err;
  2246. enc_octet_len = (uint32_t)(*pkt_octet_len - tag_len - mki_size -
  2247. ((uint8_t *)enc_start - (uint8_t *)hdr));
  2248. } else {
  2249. enc_start = NULL;
  2250. }
  2251. /*
  2252. * if we're providing authentication, set the auth_start and auth_tag
  2253. * pointers to the proper locations; otherwise, set auth_start to NULL
  2254. * to indicate that no authentication is needed
  2255. */
  2256. if (stream->rtp_services & sec_serv_auth) {
  2257. auth_start = (uint32_t *)hdr;
  2258. auth_tag = (uint8_t *)hdr + *pkt_octet_len - tag_len;
  2259. } else {
  2260. auth_start = NULL;
  2261. auth_tag = NULL;
  2262. }
  2263. /*
  2264. * if we expect message authentication, run the authentication
  2265. * function and compare the result with the value of the auth_tag
  2266. */
  2267. if (auth_start) {
  2268. /*
  2269. * if we're using a universal hash, then we need to compute the
  2270. * keystream prefix for encrypting the universal hash output
  2271. *
  2272. * if the keystream prefix length is zero, then we know that
  2273. * the authenticator isn't using a universal hash function
  2274. */
  2275. if (session_keys->rtp_auth->prefix_len != 0) {
  2276. prefix_len = srtp_auth_get_prefix_length(session_keys->rtp_auth);
  2277. status = srtp_cipher_output(session_keys->rtp_cipher, tmp_tag,
  2278. &prefix_len);
  2279. debug_print(mod_srtp, "keystream prefix: %s",
  2280. srtp_octet_string_hex_string(tmp_tag, prefix_len));
  2281. if (status)
  2282. return srtp_err_status_cipher_fail;
  2283. }
  2284. /* initialize auth func context */
  2285. status = srtp_auth_start(session_keys->rtp_auth);
  2286. if (status)
  2287. return status;
  2288. /* now compute auth function over packet */
  2289. status = srtp_auth_update(session_keys->rtp_auth, (uint8_t *)auth_start,
  2290. *pkt_octet_len - tag_len - mki_size);
  2291. /* run auth func over ROC, then write tmp tag */
  2292. status = srtp_auth_compute(session_keys->rtp_auth, (uint8_t *)&est, 4,
  2293. tmp_tag);
  2294. debug_print(mod_srtp, "computed auth tag: %s",
  2295. srtp_octet_string_hex_string(tmp_tag, tag_len));
  2296. debug_print(mod_srtp, "packet auth tag: %s",
  2297. srtp_octet_string_hex_string(auth_tag, tag_len));
  2298. if (status)
  2299. return srtp_err_status_auth_fail;
  2300. if (srtp_octet_string_is_eq(tmp_tag, auth_tag, tag_len))
  2301. return srtp_err_status_auth_fail;
  2302. }
  2303. /*
  2304. * update the key usage limit, and check it to make sure that we
  2305. * didn't just hit either the soft limit or the hard limit, and call
  2306. * the event handler if we hit either.
  2307. */
  2308. switch (srtp_key_limit_update(session_keys->limit)) {
  2309. case srtp_key_event_normal:
  2310. break;
  2311. case srtp_key_event_soft_limit:
  2312. srtp_handle_event(ctx, stream, event_key_soft_limit);
  2313. break;
  2314. case srtp_key_event_hard_limit:
  2315. srtp_handle_event(ctx, stream, event_key_hard_limit);
  2316. return srtp_err_status_key_expired;
  2317. default:
  2318. break;
  2319. }
  2320. if (xtn_hdr && session_keys->rtp_xtn_hdr_cipher) {
  2321. /* extensions header encryption RFC 6904 */
  2322. status = srtp_process_header_encryption(stream, xtn_hdr, session_keys);
  2323. if (status) {
  2324. return status;
  2325. }
  2326. }
  2327. /* if we're decrypting, add keystream into ciphertext */
  2328. if (enc_start) {
  2329. status = srtp_cipher_decrypt(session_keys->rtp_cipher,
  2330. (uint8_t *)enc_start, &enc_octet_len);
  2331. if (status)
  2332. return srtp_err_status_cipher_fail;
  2333. }
  2334. /*
  2335. * verify that stream is for received traffic - this check will
  2336. * detect SSRC collisions, since a stream that appears in both
  2337. * srtp_protect() and srtp_unprotect() will fail this test in one of
  2338. * those functions.
  2339. *
  2340. * we do this check *after* the authentication check, so that the
  2341. * latter check will catch any attempts to fool us into thinking
  2342. * that we've got a collision
  2343. */
  2344. if (stream->direction != dir_srtp_receiver) {
  2345. if (stream->direction == dir_unknown) {
  2346. stream->direction = dir_srtp_receiver;
  2347. } else {
  2348. srtp_handle_event(ctx, stream, event_ssrc_collision);
  2349. }
  2350. }
  2351. /*
  2352. * if the stream is a 'provisional' one, in which the template context
  2353. * is used, then we need to allocate a new stream at this point, since
  2354. * the authentication passed
  2355. */
  2356. if (stream == ctx->stream_template) {
  2357. srtp_stream_ctx_t *new_stream;
  2358. /*
  2359. * allocate and initialize a new stream
  2360. *
  2361. * note that we indicate failure if we can't allocate the new
  2362. * stream, and some implementations will want to not return
  2363. * failure here
  2364. */
  2365. status =
  2366. srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
  2367. if (status)
  2368. return status;
  2369. /* add new stream to the head of the stream_list */
  2370. new_stream->next = ctx->stream_list;
  2371. ctx->stream_list = new_stream;
  2372. /* set stream (the pointer used in this function) */
  2373. stream = new_stream;
  2374. }
  2375. /*
  2376. * the message authentication function passed, so add the packet
  2377. * index into the replay database
  2378. */
  2379. if (advance_packet_index) {
  2380. srtp_rdbx_set_roc_seq(&stream->rtp_rdbx, roc_to_set, seq_to_set);
  2381. stream->pending_roc = 0;
  2382. srtp_rdbx_add_index(&stream->rtp_rdbx, 0);
  2383. } else {
  2384. srtp_rdbx_add_index(&stream->rtp_rdbx, delta);
  2385. }
  2386. /* decrease the packet length by the length of the auth tag */
  2387. *pkt_octet_len -= tag_len;
  2388. /* decrease the packet length by the mki size */
  2389. *pkt_octet_len -= mki_size;
  2390. return srtp_err_status_ok;
  2391. }
  2392. srtp_err_status_t srtp_init()
  2393. {
  2394. srtp_err_status_t status;
  2395. /* initialize crypto kernel */
  2396. status = srtp_crypto_kernel_init();
  2397. if (status)
  2398. return status;
  2399. /* load srtp debug module into the kernel */
  2400. status = srtp_crypto_kernel_load_debug_module(&mod_srtp);
  2401. if (status)
  2402. return status;
  2403. return srtp_err_status_ok;
  2404. }
  2405. srtp_err_status_t srtp_shutdown()
  2406. {
  2407. srtp_err_status_t status;
  2408. /* shut down crypto kernel */
  2409. status = srtp_crypto_kernel_shutdown();
  2410. if (status)
  2411. return status;
  2412. /* shutting down crypto kernel frees the srtp debug module as well */
  2413. return srtp_err_status_ok;
  2414. }
  2415. /*
  2416. * The following code is under consideration for removal. See
  2417. * SRTP_MAX_TRAILER_LEN
  2418. */
  2419. #if 0
  2420. /*
  2421. * srtp_get_trailer_length(&a) returns the number of octets that will
  2422. * be added to an RTP packet by the SRTP processing. This value
  2423. * is constant for a given srtp_stream_t (i.e. between initializations).
  2424. */
  2425. int
  2426. srtp_get_trailer_length(const srtp_stream_t s) {
  2427. return srtp_auth_get_tag_length(s->rtp_auth);
  2428. }
  2429. #endif
  2430. /*
  2431. * srtp_get_stream(ssrc) returns a pointer to the stream corresponding
  2432. * to ssrc, or NULL if no stream exists for that ssrc
  2433. *
  2434. * this is an internal function
  2435. */
  2436. srtp_stream_ctx_t *srtp_get_stream(srtp_t srtp, uint32_t ssrc)
  2437. {
  2438. srtp_stream_ctx_t *stream;
  2439. /* walk down list until ssrc is found */
  2440. stream = srtp->stream_list;
  2441. while (stream != NULL) {
  2442. if (stream->ssrc == ssrc)
  2443. return stream;
  2444. stream = stream->next;
  2445. }
  2446. /* we haven't found our ssrc, so return a null */
  2447. return NULL;
  2448. }
  2449. srtp_err_status_t srtp_dealloc(srtp_t session)
  2450. {
  2451. srtp_stream_ctx_t *stream;
  2452. srtp_err_status_t status;
  2453. /*
  2454. * we take a conservative deallocation strategy - if we encounter an
  2455. * error deallocating a stream, then we stop trying to deallocate
  2456. * memory and just return an error
  2457. */
  2458. /* walk list of streams, deallocating as we go */
  2459. stream = session->stream_list;
  2460. while (stream != NULL) {
  2461. srtp_stream_t next = stream->next;
  2462. status = srtp_stream_dealloc(stream, session->stream_template);
  2463. if (status)
  2464. return status;
  2465. stream = next;
  2466. }
  2467. /* deallocate stream template, if there is one */
  2468. if (session->stream_template != NULL) {
  2469. status = srtp_stream_dealloc(session->stream_template, NULL);
  2470. if (status)
  2471. return status;
  2472. }
  2473. /* deallocate session context */
  2474. srtp_crypto_free(session);
  2475. return srtp_err_status_ok;
  2476. }
  2477. srtp_err_status_t srtp_add_stream(srtp_t session, const srtp_policy_t *policy)
  2478. {
  2479. srtp_err_status_t status;
  2480. srtp_stream_t tmp;
  2481. /* sanity check arguments */
  2482. if ((session == NULL) || (policy == NULL) ||
  2483. (!srtp_validate_policy_master_keys(policy)))
  2484. return srtp_err_status_bad_param;
  2485. /* allocate stream */
  2486. status = srtp_stream_alloc(&tmp, policy);
  2487. if (status) {
  2488. return status;
  2489. }
  2490. /* initialize stream */
  2491. status = srtp_stream_init(tmp, policy);
  2492. if (status) {
  2493. srtp_stream_dealloc(tmp, NULL);
  2494. return status;
  2495. }
  2496. /*
  2497. * set the head of the stream list or the template to point to the
  2498. * stream that we've just alloced and init'ed, depending on whether
  2499. * or not it has a wildcard SSRC value or not
  2500. *
  2501. * if the template stream has already been set, then the policy is
  2502. * inconsistent, so we return a bad_param error code
  2503. */
  2504. switch (policy->ssrc.type) {
  2505. case (ssrc_any_outbound):
  2506. if (session->stream_template) {
  2507. srtp_stream_dealloc(tmp, NULL);
  2508. return srtp_err_status_bad_param;
  2509. }
  2510. session->stream_template = tmp;
  2511. session->stream_template->direction = dir_srtp_sender;
  2512. break;
  2513. case (ssrc_any_inbound):
  2514. if (session->stream_template) {
  2515. srtp_stream_dealloc(tmp, NULL);
  2516. return srtp_err_status_bad_param;
  2517. }
  2518. session->stream_template = tmp;
  2519. session->stream_template->direction = dir_srtp_receiver;
  2520. break;
  2521. case (ssrc_specific):
  2522. tmp->next = session->stream_list;
  2523. session->stream_list = tmp;
  2524. break;
  2525. case (ssrc_undefined):
  2526. default:
  2527. srtp_stream_dealloc(tmp, NULL);
  2528. return srtp_err_status_bad_param;
  2529. }
  2530. return srtp_err_status_ok;
  2531. }
  2532. srtp_err_status_t srtp_create(srtp_t *session, /* handle for session */
  2533. const srtp_policy_t *policy)
  2534. { /* SRTP policy (list) */
  2535. srtp_err_status_t stat;
  2536. srtp_ctx_t *ctx;
  2537. /* sanity check arguments */
  2538. if (session == NULL)
  2539. return srtp_err_status_bad_param;
  2540. /* allocate srtp context and set ctx_ptr */
  2541. ctx = (srtp_ctx_t *)srtp_crypto_alloc(sizeof(srtp_ctx_t));
  2542. if (ctx == NULL)
  2543. return srtp_err_status_alloc_fail;
  2544. *session = ctx;
  2545. /*
  2546. * loop over elements in the policy list, allocating and
  2547. * initializing a stream for each element
  2548. */
  2549. ctx->stream_template = NULL;
  2550. ctx->stream_list = NULL;
  2551. ctx->user_data = NULL;
  2552. while (policy != NULL) {
  2553. stat = srtp_add_stream(ctx, policy);
  2554. if (stat) {
  2555. /* clean up everything */
  2556. srtp_dealloc(*session);
  2557. *session = NULL;
  2558. return stat;
  2559. }
  2560. /* set policy to next item in list */
  2561. policy = policy->next;
  2562. }
  2563. return srtp_err_status_ok;
  2564. }
  2565. srtp_err_status_t srtp_remove_stream(srtp_t session, uint32_t ssrc)
  2566. {
  2567. srtp_stream_ctx_t *stream, *last_stream;
  2568. srtp_err_status_t status;
  2569. /* sanity check arguments */
  2570. if (session == NULL)
  2571. return srtp_err_status_bad_param;
  2572. /* find stream in list; complain if not found */
  2573. last_stream = stream = session->stream_list;
  2574. while ((stream != NULL) && (ssrc != stream->ssrc)) {
  2575. last_stream = stream;
  2576. stream = stream->next;
  2577. }
  2578. if (stream == NULL)
  2579. return srtp_err_status_no_ctx;
  2580. /* remove stream from the list */
  2581. if (last_stream == stream)
  2582. /* stream was first in list */
  2583. session->stream_list = stream->next;
  2584. else
  2585. last_stream->next = stream->next;
  2586. /* deallocate the stream */
  2587. status = srtp_stream_dealloc(stream, session->stream_template);
  2588. if (status)
  2589. return status;
  2590. return srtp_err_status_ok;
  2591. }
  2592. srtp_err_status_t srtp_update(srtp_t session, const srtp_policy_t *policy)
  2593. {
  2594. srtp_err_status_t stat;
  2595. /* sanity check arguments */
  2596. if ((session == NULL) || (policy == NULL) ||
  2597. (!srtp_validate_policy_master_keys(policy))) {
  2598. return srtp_err_status_bad_param;
  2599. }
  2600. while (policy != NULL) {
  2601. stat = srtp_update_stream(session, policy);
  2602. if (stat) {
  2603. return stat;
  2604. }
  2605. /* set policy to next item in list */
  2606. policy = policy->next;
  2607. }
  2608. return srtp_err_status_ok;
  2609. }
  2610. static srtp_err_status_t update_template_streams(srtp_t session,
  2611. const srtp_policy_t *policy)
  2612. {
  2613. srtp_err_status_t status;
  2614. srtp_stream_t new_stream_template;
  2615. srtp_stream_t new_stream_list = NULL;
  2616. if (session->stream_template == NULL) {
  2617. return srtp_err_status_bad_param;
  2618. }
  2619. /* allocate new template stream */
  2620. status = srtp_stream_alloc(&new_stream_template, policy);
  2621. if (status) {
  2622. return status;
  2623. }
  2624. /* initialize new template stream */
  2625. status = srtp_stream_init(new_stream_template, policy);
  2626. if (status) {
  2627. srtp_crypto_free(new_stream_template);
  2628. return status;
  2629. }
  2630. /* for all old templated streams */
  2631. for (;;) {
  2632. srtp_stream_t stream;
  2633. uint32_t ssrc;
  2634. srtp_xtd_seq_num_t old_index;
  2635. srtp_rdb_t old_rtcp_rdb;
  2636. stream = session->stream_list;
  2637. while ((stream != NULL) &&
  2638. (stream->session_keys[0].rtp_auth !=
  2639. session->stream_template->session_keys[0].rtp_auth)) {
  2640. stream = stream->next;
  2641. }
  2642. if (stream == NULL) {
  2643. /* no more templated streams */
  2644. break;
  2645. }
  2646. /* save old extendard seq */
  2647. ssrc = stream->ssrc;
  2648. old_index = stream->rtp_rdbx.index;
  2649. old_rtcp_rdb = stream->rtcp_rdb;
  2650. /* remove stream */
  2651. status = srtp_remove_stream(session, ssrc);
  2652. if (status) {
  2653. /* free new allocations */
  2654. while (new_stream_list != NULL) {
  2655. srtp_stream_t next = new_stream_list->next;
  2656. srtp_stream_dealloc(new_stream_list, new_stream_template);
  2657. new_stream_list = next;
  2658. }
  2659. srtp_stream_dealloc(new_stream_template, NULL);
  2660. return status;
  2661. }
  2662. /* allocate and initialize a new stream */
  2663. status = srtp_stream_clone(new_stream_template, ssrc, &stream);
  2664. if (status) {
  2665. /* free new allocations */
  2666. while (new_stream_list != NULL) {
  2667. srtp_stream_t next = new_stream_list->next;
  2668. srtp_stream_dealloc(new_stream_list, new_stream_template);
  2669. new_stream_list = next;
  2670. }
  2671. srtp_stream_dealloc(new_stream_template, NULL);
  2672. return status;
  2673. }
  2674. /* add new stream to the head of the new_stream_list */
  2675. stream->next = new_stream_list;
  2676. new_stream_list = stream;
  2677. /* restore old extended seq */
  2678. stream->rtp_rdbx.index = old_index;
  2679. stream->rtcp_rdb = old_rtcp_rdb;
  2680. }
  2681. /* dealloc old template */
  2682. srtp_stream_dealloc(session->stream_template, NULL);
  2683. /* set new template */
  2684. session->stream_template = new_stream_template;
  2685. /* add new list */
  2686. if (new_stream_list) {
  2687. srtp_stream_t tail = new_stream_list;
  2688. while (tail->next) {
  2689. tail = tail->next;
  2690. }
  2691. tail->next = session->stream_list;
  2692. session->stream_list = new_stream_list;
  2693. }
  2694. return status;
  2695. }
  2696. static srtp_err_status_t update_stream(srtp_t session,
  2697. const srtp_policy_t *policy)
  2698. {
  2699. srtp_err_status_t status;
  2700. srtp_xtd_seq_num_t old_index;
  2701. srtp_rdb_t old_rtcp_rdb;
  2702. srtp_stream_t stream;
  2703. stream = srtp_get_stream(session, htonl(policy->ssrc.value));
  2704. if (stream == NULL) {
  2705. return srtp_err_status_bad_param;
  2706. }
  2707. /* save old extendard seq */
  2708. old_index = stream->rtp_rdbx.index;
  2709. old_rtcp_rdb = stream->rtcp_rdb;
  2710. status = srtp_remove_stream(session, htonl(policy->ssrc.value));
  2711. if (status) {
  2712. return status;
  2713. }
  2714. status = srtp_add_stream(session, policy);
  2715. if (status) {
  2716. return status;
  2717. }
  2718. stream = srtp_get_stream(session, htonl(policy->ssrc.value));
  2719. if (stream == NULL) {
  2720. return srtp_err_status_fail;
  2721. }
  2722. /* restore old extended seq */
  2723. stream->rtp_rdbx.index = old_index;
  2724. stream->rtcp_rdb = old_rtcp_rdb;
  2725. return srtp_err_status_ok;
  2726. }
  2727. srtp_err_status_t srtp_update_stream(srtp_t session,
  2728. const srtp_policy_t *policy)
  2729. {
  2730. srtp_err_status_t status;
  2731. /* sanity check arguments */
  2732. if ((session == NULL) || (policy == NULL) ||
  2733. (!srtp_validate_policy_master_keys(policy)))
  2734. return srtp_err_status_bad_param;
  2735. switch (policy->ssrc.type) {
  2736. case (ssrc_any_outbound):
  2737. case (ssrc_any_inbound):
  2738. status = update_template_streams(session, policy);
  2739. break;
  2740. case (ssrc_specific):
  2741. status = update_stream(session, policy);
  2742. break;
  2743. case (ssrc_undefined):
  2744. default:
  2745. return srtp_err_status_bad_param;
  2746. }
  2747. return status;
  2748. }
  2749. /*
  2750. * The default policy - provides a convenient way for callers to use
  2751. * the default security policy
  2752. *
  2753. * The default policy is defined in RFC 3711
  2754. * (Section 5. Default and mandatory-to-implement Transforms)
  2755. *
  2756. */
  2757. /*
  2758. * NOTE: cipher_key_len is really key len (128 bits) plus salt len
  2759. * (112 bits)
  2760. */
  2761. /* There are hard-coded 16's for base_key_len in the key generation code */
  2762. void srtp_crypto_policy_set_rtp_default(srtp_crypto_policy_t *p)
  2763. {
  2764. p->cipher_type = SRTP_AES_ICM_128;
  2765. p->cipher_key_len =
  2766. SRTP_AES_ICM_128_KEY_LEN_WSALT; /* default 128 bits per RFC 3711 */
  2767. p->auth_type = SRTP_HMAC_SHA1;
  2768. p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
  2769. p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
  2770. p->sec_serv = sec_serv_conf_and_auth;
  2771. }
  2772. void srtp_crypto_policy_set_rtcp_default(srtp_crypto_policy_t *p)
  2773. {
  2774. p->cipher_type = SRTP_AES_ICM_128;
  2775. p->cipher_key_len =
  2776. SRTP_AES_ICM_128_KEY_LEN_WSALT; /* default 128 bits per RFC 3711 */
  2777. p->auth_type = SRTP_HMAC_SHA1;
  2778. p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
  2779. p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
  2780. p->sec_serv = sec_serv_conf_and_auth;
  2781. }
  2782. void srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(srtp_crypto_policy_t *p)
  2783. {
  2784. /*
  2785. * corresponds to RFC 4568
  2786. *
  2787. * note that this crypto policy is intended for SRTP, but not SRTCP
  2788. */
  2789. p->cipher_type = SRTP_AES_ICM_128;
  2790. p->cipher_key_len =
  2791. SRTP_AES_ICM_128_KEY_LEN_WSALT; /* 128 bit key, 112 bit salt */
  2792. p->auth_type = SRTP_HMAC_SHA1;
  2793. p->auth_key_len = 20; /* 160 bit key */
  2794. p->auth_tag_len = 4; /* 32 bit tag */
  2795. p->sec_serv = sec_serv_conf_and_auth;
  2796. }
  2797. void srtp_crypto_policy_set_aes_cm_128_null_auth(srtp_crypto_policy_t *p)
  2798. {
  2799. /*
  2800. * corresponds to RFC 4568
  2801. *
  2802. * note that this crypto policy is intended for SRTP, but not SRTCP
  2803. */
  2804. p->cipher_type = SRTP_AES_ICM_128;
  2805. p->cipher_key_len =
  2806. SRTP_AES_ICM_128_KEY_LEN_WSALT; /* 128 bit key, 112 bit salt */
  2807. p->auth_type = SRTP_NULL_AUTH;
  2808. p->auth_key_len = 0;
  2809. p->auth_tag_len = 0;
  2810. p->sec_serv = sec_serv_conf;
  2811. }
  2812. void srtp_crypto_policy_set_null_cipher_hmac_sha1_80(srtp_crypto_policy_t *p)
  2813. {
  2814. /*
  2815. * corresponds to RFC 4568
  2816. */
  2817. p->cipher_type = SRTP_NULL_CIPHER;
  2818. p->cipher_key_len = 0;
  2819. p->auth_type = SRTP_HMAC_SHA1;
  2820. p->auth_key_len = 20;
  2821. p->auth_tag_len = 10;
  2822. p->sec_serv = sec_serv_auth;
  2823. }
  2824. void srtp_crypto_policy_set_null_cipher_hmac_null(srtp_crypto_policy_t *p)
  2825. {
  2826. /*
  2827. * Should only be used for testing
  2828. */
  2829. p->cipher_type = SRTP_NULL_CIPHER;
  2830. p->cipher_key_len = 0;
  2831. p->auth_type = SRTP_NULL_AUTH;
  2832. p->auth_key_len = 0;
  2833. p->auth_tag_len = 0;
  2834. p->sec_serv = sec_serv_none;
  2835. }
  2836. void srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(srtp_crypto_policy_t *p)
  2837. {
  2838. /*
  2839. * corresponds to RFC 6188
  2840. */
  2841. p->cipher_type = SRTP_AES_ICM_256;
  2842. p->cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT;
  2843. p->auth_type = SRTP_HMAC_SHA1;
  2844. p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
  2845. p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
  2846. p->sec_serv = sec_serv_conf_and_auth;
  2847. }
  2848. void srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(srtp_crypto_policy_t *p)
  2849. {
  2850. /*
  2851. * corresponds to RFC 6188
  2852. *
  2853. * note that this crypto policy is intended for SRTP, but not SRTCP
  2854. */
  2855. p->cipher_type = SRTP_AES_ICM_256;
  2856. p->cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT;
  2857. p->auth_type = SRTP_HMAC_SHA1;
  2858. p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
  2859. p->auth_tag_len = 4; /* default 80 bits per RFC 3711 */
  2860. p->sec_serv = sec_serv_conf_and_auth;
  2861. }
  2862. /*
  2863. * AES-256 with no authentication.
  2864. */
  2865. void srtp_crypto_policy_set_aes_cm_256_null_auth(srtp_crypto_policy_t *p)
  2866. {
  2867. p->cipher_type = SRTP_AES_ICM_256;
  2868. p->cipher_key_len = SRTP_AES_ICM_256_KEY_LEN_WSALT;
  2869. p->auth_type = SRTP_NULL_AUTH;
  2870. p->auth_key_len = 0;
  2871. p->auth_tag_len = 0;
  2872. p->sec_serv = sec_serv_conf;
  2873. }
  2874. void srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(srtp_crypto_policy_t *p)
  2875. {
  2876. /*
  2877. * corresponds to RFC 6188
  2878. */
  2879. p->cipher_type = SRTP_AES_ICM_192;
  2880. p->cipher_key_len = SRTP_AES_ICM_192_KEY_LEN_WSALT;
  2881. p->auth_type = SRTP_HMAC_SHA1;
  2882. p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
  2883. p->auth_tag_len = 10; /* default 80 bits per RFC 3711 */
  2884. p->sec_serv = sec_serv_conf_and_auth;
  2885. }
  2886. void srtp_crypto_policy_set_aes_cm_192_hmac_sha1_32(srtp_crypto_policy_t *p)
  2887. {
  2888. /*
  2889. * corresponds to RFC 6188
  2890. *
  2891. * note that this crypto policy is intended for SRTP, but not SRTCP
  2892. */
  2893. p->cipher_type = SRTP_AES_ICM_192;
  2894. p->cipher_key_len = SRTP_AES_ICM_192_KEY_LEN_WSALT;
  2895. p->auth_type = SRTP_HMAC_SHA1;
  2896. p->auth_key_len = 20; /* default 160 bits per RFC 3711 */
  2897. p->auth_tag_len = 4; /* default 80 bits per RFC 3711 */
  2898. p->sec_serv = sec_serv_conf_and_auth;
  2899. }
  2900. /*
  2901. * AES-192 with no authentication.
  2902. */
  2903. void srtp_crypto_policy_set_aes_cm_192_null_auth(srtp_crypto_policy_t *p)
  2904. {
  2905. p->cipher_type = SRTP_AES_ICM_192;
  2906. p->cipher_key_len = SRTP_AES_ICM_192_KEY_LEN_WSALT;
  2907. p->auth_type = SRTP_NULL_AUTH;
  2908. p->auth_key_len = 0;
  2909. p->auth_tag_len = 0;
  2910. p->sec_serv = sec_serv_conf;
  2911. }
  2912. /*
  2913. * AES-128 GCM mode with 8 octet auth tag.
  2914. */
  2915. void srtp_crypto_policy_set_aes_gcm_128_8_auth(srtp_crypto_policy_t *p)
  2916. {
  2917. p->cipher_type = SRTP_AES_GCM_128;
  2918. p->cipher_key_len = SRTP_AES_GCM_128_KEY_LEN_WSALT;
  2919. p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
  2920. p->auth_key_len = 0;
  2921. p->auth_tag_len = 8; /* 8 octet tag length */
  2922. p->sec_serv = sec_serv_conf_and_auth;
  2923. }
  2924. /*
  2925. * AES-256 GCM mode with 8 octet auth tag.
  2926. */
  2927. void srtp_crypto_policy_set_aes_gcm_256_8_auth(srtp_crypto_policy_t *p)
  2928. {
  2929. p->cipher_type = SRTP_AES_GCM_256;
  2930. p->cipher_key_len = SRTP_AES_GCM_256_KEY_LEN_WSALT;
  2931. p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
  2932. p->auth_key_len = 0;
  2933. p->auth_tag_len = 8; /* 8 octet tag length */
  2934. p->sec_serv = sec_serv_conf_and_auth;
  2935. }
  2936. /*
  2937. * AES-128 GCM mode with 8 octet auth tag, no RTCP encryption.
  2938. */
  2939. void srtp_crypto_policy_set_aes_gcm_128_8_only_auth(srtp_crypto_policy_t *p)
  2940. {
  2941. p->cipher_type = SRTP_AES_GCM_128;
  2942. p->cipher_key_len = SRTP_AES_GCM_128_KEY_LEN_WSALT;
  2943. p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
  2944. p->auth_key_len = 0;
  2945. p->auth_tag_len = 8; /* 8 octet tag length */
  2946. p->sec_serv = sec_serv_auth; /* This only applies to RTCP */
  2947. }
  2948. /*
  2949. * AES-256 GCM mode with 8 octet auth tag, no RTCP encryption.
  2950. */
  2951. void srtp_crypto_policy_set_aes_gcm_256_8_only_auth(srtp_crypto_policy_t *p)
  2952. {
  2953. p->cipher_type = SRTP_AES_GCM_256;
  2954. p->cipher_key_len = SRTP_AES_GCM_256_KEY_LEN_WSALT;
  2955. p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
  2956. p->auth_key_len = 0;
  2957. p->auth_tag_len = 8; /* 8 octet tag length */
  2958. p->sec_serv = sec_serv_auth; /* This only applies to RTCP */
  2959. }
  2960. /*
  2961. * AES-128 GCM mode with 16 octet auth tag.
  2962. */
  2963. void srtp_crypto_policy_set_aes_gcm_128_16_auth(srtp_crypto_policy_t *p)
  2964. {
  2965. p->cipher_type = SRTP_AES_GCM_128;
  2966. p->cipher_key_len = SRTP_AES_GCM_128_KEY_LEN_WSALT;
  2967. p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
  2968. p->auth_key_len = 0;
  2969. p->auth_tag_len = 16; /* 16 octet tag length */
  2970. p->sec_serv = sec_serv_conf_and_auth;
  2971. }
  2972. /*
  2973. * AES-256 GCM mode with 16 octet auth tag.
  2974. */
  2975. void srtp_crypto_policy_set_aes_gcm_256_16_auth(srtp_crypto_policy_t *p)
  2976. {
  2977. p->cipher_type = SRTP_AES_GCM_256;
  2978. p->cipher_key_len = SRTP_AES_GCM_256_KEY_LEN_WSALT;
  2979. p->auth_type = SRTP_NULL_AUTH; /* GCM handles the auth for us */
  2980. p->auth_key_len = 0;
  2981. p->auth_tag_len = 16; /* 16 octet tag length */
  2982. p->sec_serv = sec_serv_conf_and_auth;
  2983. }
  2984. /*
  2985. * secure rtcp functions
  2986. */
  2987. /*
  2988. * AEAD uses a new IV formation method. This function implements
  2989. * section 9.1 (SRTCP IV Formation for AES-GCM) from RFC7714.
  2990. * The calculation is defined as, where (+) is the xor operation:
  2991. *
  2992. * 0 1 2 3 4 5 6 7 8 9 10 11
  2993. * +--+--+--+--+--+--+--+--+--+--+--+--+
  2994. * |00|00| SSRC |00|00|0+SRTCP Idx|---+
  2995. * +--+--+--+--+--+--+--+--+--+--+--+--+ |
  2996. * |
  2997. * +--+--+--+--+--+--+--+--+--+--+--+--+ |
  2998. * | Encryption Salt |->(+)
  2999. * +--+--+--+--+--+--+--+--+--+--+--+--+ |
  3000. * |
  3001. * +--+--+--+--+--+--+--+--+--+--+--+--+ |
  3002. * | Initialization Vector |<--+
  3003. * +--+--+--+--+--+--+--+--+--+--+--+--+*
  3004. *
  3005. * Input: *session_keys - pointer to SRTP stream context session keys,
  3006. * used to retrieve the SALT
  3007. * *iv - Pointer to recieve the calculated IV
  3008. * seq_num - The SEQ value to use for the IV calculation.
  3009. * *hdr - The RTP header, used to get the SSRC value
  3010. *
  3011. * Returns: srtp_err_status_ok if no error or srtp_err_status_bad_param
  3012. * if seq_num is invalid
  3013. *
  3014. */
  3015. static srtp_err_status_t srtp_calc_aead_iv_srtcp(
  3016. srtp_session_keys_t *session_keys,
  3017. v128_t *iv,
  3018. uint32_t seq_num,
  3019. srtcp_hdr_t *hdr)
  3020. {
  3021. v128_t in;
  3022. v128_t salt;
  3023. memset(&in, 0, sizeof(v128_t));
  3024. memset(&salt, 0, sizeof(v128_t));
  3025. in.v16[0] = 0;
  3026. memcpy(&in.v16[1], &hdr->ssrc, 4); /* still in network order! */
  3027. in.v16[3] = 0;
  3028. /*
  3029. * The SRTCP index (seq_num) spans bits 0 through 30 inclusive.
  3030. * The most significant bit should be zero.
  3031. */
  3032. if (seq_num & 0x80000000UL) {
  3033. return srtp_err_status_bad_param;
  3034. }
  3035. in.v32[2] = htonl(seq_num);
  3036. debug_print(mod_srtp, "Pre-salted RTCP IV = %s\n", v128_hex_string(&in));
  3037. /*
  3038. * Get the SALT value from the context
  3039. */
  3040. memcpy(salt.v8, session_keys->c_salt, 12);
  3041. debug_print(mod_srtp, "RTCP SALT = %s\n", v128_hex_string(&salt));
  3042. /*
  3043. * Finally, apply the SALT to the input
  3044. */
  3045. v128_xor(iv, &in, &salt);
  3046. return srtp_err_status_ok;
  3047. }
  3048. /*
  3049. * This code handles AEAD ciphers for outgoing RTCP. We currently support
  3050. * AES-GCM mode with 128 or 256 bit keys.
  3051. */
  3052. static srtp_err_status_t srtp_protect_rtcp_aead(
  3053. srtp_t ctx,
  3054. srtp_stream_ctx_t *stream,
  3055. void *rtcp_hdr,
  3056. unsigned int *pkt_octet_len,
  3057. srtp_session_keys_t *session_keys,
  3058. unsigned int use_mki)
  3059. {
  3060. srtcp_hdr_t *hdr = (srtcp_hdr_t *)rtcp_hdr;
  3061. uint32_t *enc_start; /* pointer to start of encrypted portion */
  3062. uint32_t *trailer_p; /* pointer to start of trailer */
  3063. uint32_t trailer; /* trailer value */
  3064. unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */
  3065. uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
  3066. srtp_err_status_t status;
  3067. uint32_t tag_len;
  3068. uint32_t seq_num;
  3069. v128_t iv;
  3070. uint32_t tseq;
  3071. unsigned int mki_size = 0;
  3072. /* get tag length from stream context */
  3073. tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth);
  3074. /*
  3075. * set encryption start and encryption length - if we're not
  3076. * providing confidentiality, set enc_start to NULL
  3077. */
  3078. enc_start = (uint32_t *)hdr + uint32s_in_rtcp_header;
  3079. enc_octet_len = *pkt_octet_len - octets_in_rtcp_header;
  3080. /* NOTE: hdr->length is not usable - it refers to only the first
  3081. * RTCP report in the compound packet!
  3082. */
  3083. trailer_p = (uint32_t *)((char *)enc_start + enc_octet_len + tag_len);
  3084. if (stream->rtcp_services & sec_serv_conf) {
  3085. trailer = htonl(SRTCP_E_BIT); /* set encrypt bit */
  3086. } else {
  3087. enc_start = NULL;
  3088. enc_octet_len = 0;
  3089. /* 0 is network-order independant */
  3090. trailer = 0x00000000; /* set encrypt bit */
  3091. }
  3092. mki_size = srtp_inject_mki((uint8_t *)hdr + *pkt_octet_len + tag_len +
  3093. sizeof(srtcp_trailer_t),
  3094. session_keys, use_mki);
  3095. /*
  3096. * set the auth_tag pointer to the proper location, which is after
  3097. * the payload, but before the trailer
  3098. * (note that srtpc *always* provides authentication, unlike srtp)
  3099. */
  3100. /* Note: This would need to change for optional mikey data */
  3101. auth_tag = (uint8_t *)hdr + *pkt_octet_len;
  3102. /*
  3103. * check sequence number for overruns, and copy it into the packet
  3104. * if its value isn't too big
  3105. */
  3106. status = srtp_rdb_increment(&stream->rtcp_rdb);
  3107. if (status) {
  3108. return status;
  3109. }
  3110. seq_num = srtp_rdb_get_value(&stream->rtcp_rdb);
  3111. trailer |= htonl(seq_num);
  3112. debug_print(mod_srtp, "srtcp index: %x", seq_num);
  3113. memcpy(trailer_p, &trailer, sizeof(trailer));
  3114. /*
  3115. * Calculate and set the IV
  3116. */
  3117. status = srtp_calc_aead_iv_srtcp(session_keys, &iv, seq_num, hdr);
  3118. if (status) {
  3119. return srtp_err_status_cipher_fail;
  3120. }
  3121. status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv,
  3122. srtp_direction_encrypt);
  3123. if (status) {
  3124. return srtp_err_status_cipher_fail;
  3125. }
  3126. /*
  3127. * Set the AAD for GCM mode
  3128. */
  3129. if (enc_start) {
  3130. /*
  3131. * If payload encryption is enabled, then the AAD consist of
  3132. * the RTCP header and the seq# at the end of the packet
  3133. */
  3134. status = srtp_cipher_set_aad(session_keys->rtcp_cipher, (uint8_t *)hdr,
  3135. octets_in_rtcp_header);
  3136. if (status) {
  3137. return (srtp_err_status_cipher_fail);
  3138. }
  3139. } else {
  3140. /*
  3141. * Since payload encryption is not enabled, we must authenticate
  3142. * the entire packet as described in RFC 7714 (Section 9.3. Data
  3143. * Types in Unencrypted SRTCP Compound Packets)
  3144. */
  3145. status = srtp_cipher_set_aad(session_keys->rtcp_cipher, (uint8_t *)hdr,
  3146. *pkt_octet_len);
  3147. if (status) {
  3148. return (srtp_err_status_cipher_fail);
  3149. }
  3150. }
  3151. /*
  3152. * Process the sequence# as AAD
  3153. */
  3154. tseq = trailer;
  3155. status = srtp_cipher_set_aad(session_keys->rtcp_cipher, (uint8_t *)&tseq,
  3156. sizeof(srtcp_trailer_t));
  3157. if (status) {
  3158. return (srtp_err_status_cipher_fail);
  3159. }
  3160. /* if we're encrypting, exor keystream into the message */
  3161. if (enc_start) {
  3162. status = srtp_cipher_encrypt(session_keys->rtcp_cipher,
  3163. (uint8_t *)enc_start, &enc_octet_len);
  3164. if (status) {
  3165. return srtp_err_status_cipher_fail;
  3166. }
  3167. /*
  3168. * Get the tag and append that to the output
  3169. */
  3170. status = srtp_cipher_get_tag(session_keys->rtcp_cipher,
  3171. (uint8_t *)auth_tag, &tag_len);
  3172. if (status) {
  3173. return (srtp_err_status_cipher_fail);
  3174. }
  3175. enc_octet_len += tag_len;
  3176. } else {
  3177. /*
  3178. * Even though we're not encrypting the payload, we need
  3179. * to run the cipher to get the auth tag.
  3180. */
  3181. unsigned int nolen = 0;
  3182. status = srtp_cipher_encrypt(session_keys->rtcp_cipher, NULL, &nolen);
  3183. if (status) {
  3184. return srtp_err_status_cipher_fail;
  3185. }
  3186. /*
  3187. * Get the tag and append that to the output
  3188. */
  3189. status = srtp_cipher_get_tag(session_keys->rtcp_cipher,
  3190. (uint8_t *)auth_tag, &tag_len);
  3191. if (status) {
  3192. return (srtp_err_status_cipher_fail);
  3193. }
  3194. enc_octet_len += tag_len;
  3195. }
  3196. /* increase the packet length by the length of the auth tag and seq_num*/
  3197. *pkt_octet_len += (tag_len + sizeof(srtcp_trailer_t));
  3198. /* increase the packet by the mki_size */
  3199. *pkt_octet_len += mki_size;
  3200. return srtp_err_status_ok;
  3201. }
  3202. /*
  3203. * This function handles incoming SRTCP packets while in AEAD mode,
  3204. * which currently supports AES-GCM encryption. Note, the auth tag is
  3205. * at the end of the packet stream and is automatically checked by GCM
  3206. * when decrypting the payload.
  3207. */
  3208. static srtp_err_status_t srtp_unprotect_rtcp_aead(
  3209. srtp_t ctx,
  3210. srtp_stream_ctx_t *stream,
  3211. void *srtcp_hdr,
  3212. unsigned int *pkt_octet_len,
  3213. srtp_session_keys_t *session_keys,
  3214. unsigned int use_mki)
  3215. {
  3216. srtcp_hdr_t *hdr = (srtcp_hdr_t *)srtcp_hdr;
  3217. uint32_t *enc_start; /* pointer to start of encrypted portion */
  3218. uint32_t *trailer_p; /* pointer to start of trailer */
  3219. uint32_t trailer; /* trailer value */
  3220. unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */
  3221. uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
  3222. srtp_err_status_t status;
  3223. int tag_len;
  3224. unsigned int tmp_len;
  3225. uint32_t seq_num;
  3226. v128_t iv;
  3227. uint32_t tseq;
  3228. unsigned int mki_size = 0;
  3229. /* get tag length from stream context */
  3230. tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth);
  3231. if (use_mki) {
  3232. mki_size = session_keys->mki_size;
  3233. }
  3234. /*
  3235. * set encryption start, encryption length, and trailer
  3236. */
  3237. /* index & E (encryption) bit follow normal data. hdr->len is the number of
  3238. * words (32-bit) in the normal packet minus 1
  3239. */
  3240. /* This should point trailer to the word past the end of the normal data. */
  3241. /* This would need to be modified for optional mikey data */
  3242. trailer_p = (uint32_t *)((char *)hdr + *pkt_octet_len -
  3243. sizeof(srtcp_trailer_t) - mki_size);
  3244. memcpy(&trailer, trailer_p, sizeof(trailer));
  3245. /*
  3246. * We pass the tag down to the cipher when doing GCM mode
  3247. */
  3248. enc_octet_len = *pkt_octet_len - (octets_in_rtcp_header +
  3249. sizeof(srtcp_trailer_t) + mki_size);
  3250. auth_tag = (uint8_t *)hdr + *pkt_octet_len - tag_len - mki_size -
  3251. sizeof(srtcp_trailer_t);
  3252. if (*((unsigned char *)trailer_p) & SRTCP_E_BYTE_BIT) {
  3253. enc_start = (uint32_t *)hdr + uint32s_in_rtcp_header;
  3254. } else {
  3255. enc_octet_len = 0;
  3256. enc_start = NULL; /* this indicates that there's no encryption */
  3257. }
  3258. /*
  3259. * check the sequence number for replays
  3260. */
  3261. /* this is easier than dealing with bitfield access */
  3262. seq_num = ntohl(trailer) & SRTCP_INDEX_MASK;
  3263. debug_print(mod_srtp, "srtcp index: %x", seq_num);
  3264. status = srtp_rdb_check(&stream->rtcp_rdb, seq_num);
  3265. if (status) {
  3266. return status;
  3267. }
  3268. /*
  3269. * Calculate and set the IV
  3270. */
  3271. status = srtp_calc_aead_iv_srtcp(session_keys, &iv, seq_num, hdr);
  3272. if (status) {
  3273. return srtp_err_status_cipher_fail;
  3274. }
  3275. status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv,
  3276. srtp_direction_decrypt);
  3277. if (status) {
  3278. return srtp_err_status_cipher_fail;
  3279. }
  3280. /*
  3281. * Set the AAD for GCM mode
  3282. */
  3283. if (enc_start) {
  3284. /*
  3285. * If payload encryption is enabled, then the AAD consist of
  3286. * the RTCP header and the seq# at the end of the packet
  3287. */
  3288. status = srtp_cipher_set_aad(session_keys->rtcp_cipher, (uint8_t *)hdr,
  3289. octets_in_rtcp_header);
  3290. if (status) {
  3291. return (srtp_err_status_cipher_fail);
  3292. }
  3293. } else {
  3294. /*
  3295. * Since payload encryption is not enabled, we must authenticate
  3296. * the entire packet as described in RFC 7714 (Section 9.3. Data
  3297. * Types in Unencrypted SRTCP Compound Packets)
  3298. */
  3299. status = srtp_cipher_set_aad(
  3300. session_keys->rtcp_cipher, (uint8_t *)hdr,
  3301. (*pkt_octet_len - tag_len - sizeof(srtcp_trailer_t) - mki_size));
  3302. if (status) {
  3303. return (srtp_err_status_cipher_fail);
  3304. }
  3305. }
  3306. /*
  3307. * Process the sequence# as AAD
  3308. */
  3309. tseq = trailer;
  3310. status = srtp_cipher_set_aad(session_keys->rtcp_cipher, (uint8_t *)&tseq,
  3311. sizeof(srtcp_trailer_t));
  3312. if (status) {
  3313. return (srtp_err_status_cipher_fail);
  3314. }
  3315. /* if we're decrypting, exor keystream into the message */
  3316. if (enc_start) {
  3317. status = srtp_cipher_decrypt(session_keys->rtcp_cipher,
  3318. (uint8_t *)enc_start, &enc_octet_len);
  3319. if (status) {
  3320. return status;
  3321. }
  3322. } else {
  3323. /*
  3324. * Still need to run the cipher to check the tag
  3325. */
  3326. tmp_len = tag_len;
  3327. status = srtp_cipher_decrypt(session_keys->rtcp_cipher,
  3328. (uint8_t *)auth_tag, &tmp_len);
  3329. if (status) {
  3330. return status;
  3331. }
  3332. }
  3333. /* decrease the packet length by the length of the auth tag and seq_num*/
  3334. *pkt_octet_len -= (tag_len + sizeof(srtcp_trailer_t) + mki_size);
  3335. /*
  3336. * verify that stream is for received traffic - this check will
  3337. * detect SSRC collisions, since a stream that appears in both
  3338. * srtp_protect() and srtp_unprotect() will fail this test in one of
  3339. * those functions.
  3340. *
  3341. * we do this check *after* the authentication check, so that the
  3342. * latter check will catch any attempts to fool us into thinking
  3343. * that we've got a collision
  3344. */
  3345. if (stream->direction != dir_srtp_receiver) {
  3346. if (stream->direction == dir_unknown) {
  3347. stream->direction = dir_srtp_receiver;
  3348. } else {
  3349. srtp_handle_event(ctx, stream, event_ssrc_collision);
  3350. }
  3351. }
  3352. /*
  3353. * if the stream is a 'provisional' one, in which the template context
  3354. * is used, then we need to allocate a new stream at this point, since
  3355. * the authentication passed
  3356. */
  3357. if (stream == ctx->stream_template) {
  3358. srtp_stream_ctx_t *new_stream;
  3359. /*
  3360. * allocate and initialize a new stream
  3361. *
  3362. * note that we indicate failure if we can't allocate the new
  3363. * stream, and some implementations will want to not return
  3364. * failure here
  3365. */
  3366. status =
  3367. srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
  3368. if (status) {
  3369. return status;
  3370. }
  3371. /* add new stream to the head of the stream_list */
  3372. new_stream->next = ctx->stream_list;
  3373. ctx->stream_list = new_stream;
  3374. /* set stream (the pointer used in this function) */
  3375. stream = new_stream;
  3376. }
  3377. /* we've passed the authentication check, so add seq_num to the rdb */
  3378. srtp_rdb_add_index(&stream->rtcp_rdb, seq_num);
  3379. return srtp_err_status_ok;
  3380. }
  3381. srtp_err_status_t srtp_protect_rtcp(srtp_t ctx,
  3382. void *rtcp_hdr,
  3383. int *pkt_octet_len)
  3384. {
  3385. return srtp_protect_rtcp_mki(ctx, rtcp_hdr, pkt_octet_len, 0, 0);
  3386. }
  3387. srtp_err_status_t srtp_protect_rtcp_mki(srtp_t ctx,
  3388. void *rtcp_hdr,
  3389. int *pkt_octet_len,
  3390. unsigned int use_mki,
  3391. unsigned int mki_index)
  3392. {
  3393. srtcp_hdr_t *hdr = (srtcp_hdr_t *)rtcp_hdr;
  3394. uint32_t *enc_start; /* pointer to start of encrypted portion */
  3395. uint32_t *auth_start; /* pointer to start of auth. portion */
  3396. uint32_t *trailer_p; /* pointer to start of trailer */
  3397. uint32_t trailer; /* trailer value */
  3398. unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */
  3399. uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
  3400. srtp_err_status_t status;
  3401. int tag_len;
  3402. srtp_stream_ctx_t *stream;
  3403. uint32_t prefix_len;
  3404. uint32_t seq_num;
  3405. unsigned int mki_size = 0;
  3406. srtp_session_keys_t *session_keys = NULL;
  3407. /* we assume the hdr is 32-bit aligned to start */
  3408. /* check the packet length - it must at least contain a full header */
  3409. if (*pkt_octet_len < octets_in_rtcp_header)
  3410. return srtp_err_status_bad_param;
  3411. /*
  3412. * look up ssrc in srtp_stream list, and process the packet with
  3413. * the appropriate stream. if we haven't seen this stream before,
  3414. * there's only one key for this srtp_session, and the cipher
  3415. * supports key-sharing, then we assume that a new stream using
  3416. * that key has just started up
  3417. */
  3418. stream = srtp_get_stream(ctx, hdr->ssrc);
  3419. if (stream == NULL) {
  3420. if (ctx->stream_template != NULL) {
  3421. srtp_stream_ctx_t *new_stream;
  3422. /* allocate and initialize a new stream */
  3423. status =
  3424. srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
  3425. if (status)
  3426. return status;
  3427. /* add new stream to the head of the stream_list */
  3428. new_stream->next = ctx->stream_list;
  3429. ctx->stream_list = new_stream;
  3430. /* set stream (the pointer used in this function) */
  3431. stream = new_stream;
  3432. } else {
  3433. /* no template stream, so we return an error */
  3434. return srtp_err_status_no_ctx;
  3435. }
  3436. }
  3437. /*
  3438. * verify that stream is for sending traffic - this check will
  3439. * detect SSRC collisions, since a stream that appears in both
  3440. * srtp_protect() and srtp_unprotect() will fail this test in one of
  3441. * those functions.
  3442. */
  3443. if (stream->direction != dir_srtp_sender) {
  3444. if (stream->direction == dir_unknown) {
  3445. stream->direction = dir_srtp_sender;
  3446. } else {
  3447. srtp_handle_event(ctx, stream, event_ssrc_collision);
  3448. }
  3449. }
  3450. session_keys =
  3451. srtp_get_session_keys_with_mki_index(stream, use_mki, mki_index);
  3452. if (session_keys == NULL)
  3453. return srtp_err_status_bad_mki;
  3454. /*
  3455. * Check if this is an AEAD stream (GCM mode). If so, then dispatch
  3456. * the request to our AEAD handler.
  3457. */
  3458. if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 ||
  3459. session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) {
  3460. return srtp_protect_rtcp_aead(ctx, stream, rtcp_hdr,
  3461. (unsigned int *)pkt_octet_len,
  3462. session_keys, use_mki);
  3463. }
  3464. /* get tag length from stream context */
  3465. tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth);
  3466. /*
  3467. * set encryption start and encryption length - if we're not
  3468. * providing confidentiality, set enc_start to NULL
  3469. */
  3470. enc_start = (uint32_t *)hdr + uint32s_in_rtcp_header;
  3471. enc_octet_len = *pkt_octet_len - octets_in_rtcp_header;
  3472. /* all of the packet, except the header, gets encrypted */
  3473. /*
  3474. * NOTE: hdr->length is not usable - it refers to only the first RTCP report
  3475. * in the compound packet!
  3476. */
  3477. trailer_p = (uint32_t *)((char *)enc_start + enc_octet_len);
  3478. if (stream->rtcp_services & sec_serv_conf) {
  3479. trailer = htonl(SRTCP_E_BIT); /* set encrypt bit */
  3480. } else {
  3481. enc_start = NULL;
  3482. enc_octet_len = 0;
  3483. /* 0 is network-order independant */
  3484. trailer = 0x00000000; /* set encrypt bit */
  3485. }
  3486. mki_size = srtp_inject_mki((uint8_t *)hdr + *pkt_octet_len +
  3487. sizeof(srtcp_trailer_t),
  3488. session_keys, use_mki);
  3489. /*
  3490. * set the auth_start and auth_tag pointers to the proper locations
  3491. * (note that srtpc *always* provides authentication, unlike srtp)
  3492. */
  3493. /* Note: This would need to change for optional mikey data */
  3494. auth_start = (uint32_t *)hdr;
  3495. auth_tag =
  3496. (uint8_t *)hdr + *pkt_octet_len + sizeof(srtcp_trailer_t) + mki_size;
  3497. /* perform EKT processing if needed */
  3498. srtp_ekt_write_data(stream->ekt, auth_tag, tag_len, pkt_octet_len,
  3499. srtp_rdbx_get_packet_index(&stream->rtp_rdbx));
  3500. /*
  3501. * check sequence number for overruns, and copy it into the packet
  3502. * if its value isn't too big
  3503. */
  3504. status = srtp_rdb_increment(&stream->rtcp_rdb);
  3505. if (status)
  3506. return status;
  3507. seq_num = srtp_rdb_get_value(&stream->rtcp_rdb);
  3508. trailer |= htonl(seq_num);
  3509. debug_print(mod_srtp, "srtcp index: %x", seq_num);
  3510. memcpy(trailer_p, &trailer, sizeof(trailer));
  3511. /*
  3512. * if we're using rindael counter mode, set nonce and seq
  3513. */
  3514. if (session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_128 ||
  3515. session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_192 ||
  3516. session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_256) {
  3517. v128_t iv;
  3518. iv.v32[0] = 0;
  3519. iv.v32[1] = hdr->ssrc; /* still in network order! */
  3520. iv.v32[2] = htonl(seq_num >> 16);
  3521. iv.v32[3] = htonl(seq_num << 16);
  3522. status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv,
  3523. srtp_direction_encrypt);
  3524. } else {
  3525. v128_t iv;
  3526. /* otherwise, just set the index to seq_num */
  3527. iv.v32[0] = 0;
  3528. iv.v32[1] = 0;
  3529. iv.v32[2] = 0;
  3530. iv.v32[3] = htonl(seq_num);
  3531. status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv,
  3532. srtp_direction_encrypt);
  3533. }
  3534. if (status)
  3535. return srtp_err_status_cipher_fail;
  3536. /*
  3537. * if we're authenticating using a universal hash, put the keystream
  3538. * prefix into the authentication tag
  3539. */
  3540. /* if auth_start is non-null, then put keystream into tag */
  3541. if (auth_start) {
  3542. /* put keystream prefix into auth_tag */
  3543. prefix_len = srtp_auth_get_prefix_length(session_keys->rtcp_auth);
  3544. status = srtp_cipher_output(session_keys->rtcp_cipher, auth_tag,
  3545. &prefix_len);
  3546. debug_print(mod_srtp, "keystream prefix: %s",
  3547. srtp_octet_string_hex_string(auth_tag, prefix_len));
  3548. if (status)
  3549. return srtp_err_status_cipher_fail;
  3550. }
  3551. /* if we're encrypting, exor keystream into the message */
  3552. if (enc_start) {
  3553. status = srtp_cipher_encrypt(session_keys->rtcp_cipher,
  3554. (uint8_t *)enc_start, &enc_octet_len);
  3555. if (status)
  3556. return srtp_err_status_cipher_fail;
  3557. }
  3558. /* initialize auth func context */
  3559. srtp_auth_start(session_keys->rtcp_auth);
  3560. /*
  3561. * run auth func over packet (including trailer), and write the
  3562. * result at auth_tag
  3563. */
  3564. status =
  3565. srtp_auth_compute(session_keys->rtcp_auth, (uint8_t *)auth_start,
  3566. (*pkt_octet_len) + sizeof(srtcp_trailer_t), auth_tag);
  3567. debug_print(mod_srtp, "srtcp auth tag: %s",
  3568. srtp_octet_string_hex_string(auth_tag, tag_len));
  3569. if (status)
  3570. return srtp_err_status_auth_fail;
  3571. /* increase the packet length by the length of the auth tag and seq_num*/
  3572. *pkt_octet_len += (tag_len + sizeof(srtcp_trailer_t));
  3573. /* increase the packet by the mki_size */
  3574. *pkt_octet_len += mki_size;
  3575. return srtp_err_status_ok;
  3576. }
  3577. srtp_err_status_t srtp_unprotect_rtcp(srtp_t ctx,
  3578. void *srtcp_hdr,
  3579. int *pkt_octet_len)
  3580. {
  3581. return srtp_unprotect_rtcp_mki(ctx, srtcp_hdr, pkt_octet_len, 0);
  3582. }
  3583. srtp_err_status_t srtp_unprotect_rtcp_mki(srtp_t ctx,
  3584. void *srtcp_hdr,
  3585. int *pkt_octet_len,
  3586. unsigned int use_mki)
  3587. {
  3588. srtcp_hdr_t *hdr = (srtcp_hdr_t *)srtcp_hdr;
  3589. uint32_t *enc_start; /* pointer to start of encrypted portion */
  3590. uint32_t *auth_start; /* pointer to start of auth. portion */
  3591. uint32_t *trailer_p; /* pointer to start of trailer */
  3592. uint32_t trailer; /* trailer value */
  3593. unsigned int enc_octet_len = 0; /* number of octets in encrypted portion */
  3594. uint8_t *auth_tag = NULL; /* location of auth_tag within packet */
  3595. uint8_t tmp_tag[SRTP_MAX_TAG_LEN];
  3596. uint8_t tag_copy[SRTP_MAX_TAG_LEN];
  3597. srtp_err_status_t status;
  3598. unsigned int auth_len;
  3599. int tag_len;
  3600. srtp_stream_ctx_t *stream;
  3601. uint32_t prefix_len;
  3602. uint32_t seq_num;
  3603. int e_bit_in_packet; /* whether the E-bit was found in the packet */
  3604. int sec_serv_confidentiality; /* whether confidentiality was requested */
  3605. unsigned int mki_size = 0;
  3606. srtp_session_keys_t *session_keys = NULL;
  3607. /* we assume the hdr is 32-bit aligned to start */
  3608. if (*pkt_octet_len < 0)
  3609. return srtp_err_status_bad_param;
  3610. /*
  3611. * check that the length value is sane; we'll check again once we
  3612. * know the tag length, but we at least want to know that it is
  3613. * a positive value
  3614. */
  3615. if ((unsigned int)(*pkt_octet_len) <
  3616. octets_in_rtcp_header + sizeof(srtcp_trailer_t))
  3617. return srtp_err_status_bad_param;
  3618. /*
  3619. * look up ssrc in srtp_stream list, and process the packet with
  3620. * the appropriate stream. if we haven't seen this stream before,
  3621. * there's only one key for this srtp_session, and the cipher
  3622. * supports key-sharing, then we assume that a new stream using
  3623. * that key has just started up
  3624. */
  3625. stream = srtp_get_stream(ctx, hdr->ssrc);
  3626. if (stream == NULL) {
  3627. if (ctx->stream_template != NULL) {
  3628. stream = ctx->stream_template;
  3629. /*
  3630. * check to see if stream_template has an EKT data structure, in
  3631. * which case we initialize the template using the EKT policy
  3632. * referenced by that data (which consists of decrypting the
  3633. * master key from the EKT field)
  3634. *
  3635. * this function initializes a *provisional* stream, and this
  3636. * stream should not be accepted until and unless the packet
  3637. * passes its authentication check
  3638. */
  3639. if (stream->ekt != NULL) {
  3640. status = srtp_stream_init_from_ekt(stream, srtcp_hdr,
  3641. *pkt_octet_len);
  3642. if (status)
  3643. return status;
  3644. }
  3645. debug_print(mod_srtp,
  3646. "srtcp using provisional stream (SSRC: 0x%08x)",
  3647. ntohl(hdr->ssrc));
  3648. } else {
  3649. /* no template stream, so we return an error */
  3650. return srtp_err_status_no_ctx;
  3651. }
  3652. }
  3653. /*
  3654. * Determine if MKI is being used and what session keys should be used
  3655. */
  3656. if (use_mki) {
  3657. session_keys = srtp_get_session_keys(
  3658. stream, (uint8_t *)hdr, (const unsigned int *)pkt_octet_len,
  3659. &mki_size);
  3660. if (session_keys == NULL)
  3661. return srtp_err_status_bad_mki;
  3662. } else {
  3663. session_keys = &stream->session_keys[0];
  3664. }
  3665. /* get tag length from stream context */
  3666. tag_len = srtp_auth_get_tag_length(session_keys->rtcp_auth);
  3667. /* check the packet length - it must contain at least a full RTCP
  3668. header, an auth tag (if applicable), and the SRTCP encrypted flag
  3669. and 31-bit index value */
  3670. if (*pkt_octet_len < (int)(octets_in_rtcp_header + tag_len + mki_size +
  3671. sizeof(srtcp_trailer_t))) {
  3672. return srtp_err_status_bad_param;
  3673. }
  3674. /*
  3675. * Check if this is an AEAD stream (GCM mode). If so, then dispatch
  3676. * the request to our AEAD handler.
  3677. */
  3678. if (session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_128 ||
  3679. session_keys->rtp_cipher->algorithm == SRTP_AES_GCM_256) {
  3680. return srtp_unprotect_rtcp_aead(ctx, stream, srtcp_hdr,
  3681. (unsigned int *)pkt_octet_len,
  3682. session_keys, mki_size);
  3683. }
  3684. sec_serv_confidentiality = stream->rtcp_services == sec_serv_conf ||
  3685. stream->rtcp_services == sec_serv_conf_and_auth;
  3686. /*
  3687. * set encryption start, encryption length, and trailer
  3688. */
  3689. enc_octet_len = *pkt_octet_len - (octets_in_rtcp_header + tag_len +
  3690. mki_size + sizeof(srtcp_trailer_t));
  3691. /*
  3692. *index & E (encryption) bit follow normal data. hdr->len is the number of
  3693. * words (32-bit) in the normal packet minus 1
  3694. */
  3695. /* This should point trailer to the word past the end of the normal data. */
  3696. /* This would need to be modified for optional mikey data */
  3697. trailer_p = (uint32_t *)((char *)hdr + *pkt_octet_len -
  3698. (tag_len + mki_size + sizeof(srtcp_trailer_t)));
  3699. memcpy(&trailer, trailer_p, sizeof(trailer));
  3700. e_bit_in_packet =
  3701. (*((unsigned char *)trailer_p) & SRTCP_E_BYTE_BIT) == SRTCP_E_BYTE_BIT;
  3702. if (e_bit_in_packet != sec_serv_confidentiality) {
  3703. return srtp_err_status_cant_check;
  3704. }
  3705. if (sec_serv_confidentiality) {
  3706. enc_start = (uint32_t *)hdr + uint32s_in_rtcp_header;
  3707. } else {
  3708. enc_octet_len = 0;
  3709. enc_start = NULL; /* this indicates that there's no encryption */
  3710. }
  3711. /*
  3712. * set the auth_start and auth_tag pointers to the proper locations
  3713. * (note that srtcp *always* uses authentication, unlike srtp)
  3714. */
  3715. auth_start = (uint32_t *)hdr;
  3716. /*
  3717. * The location of the auth tag in the packet needs to know MKI
  3718. * could be present. The data needed to calculate the Auth tag
  3719. * must not include the MKI
  3720. */
  3721. auth_len = *pkt_octet_len - tag_len - mki_size;
  3722. auth_tag = (uint8_t *)hdr + auth_len + mki_size;
  3723. /*
  3724. * if EKT is in use, then we make a copy of the tag from the packet,
  3725. * and then zeroize the location of the base tag
  3726. *
  3727. * we first re-position the auth_tag pointer so that it points to
  3728. * the base tag
  3729. */
  3730. if (stream->ekt) {
  3731. auth_tag -= srtp_ekt_octets_after_base_tag(stream->ekt);
  3732. memcpy(tag_copy, auth_tag, tag_len);
  3733. octet_string_set_to_zero(auth_tag, tag_len);
  3734. auth_tag = tag_copy;
  3735. auth_len += tag_len;
  3736. }
  3737. /*
  3738. * check the sequence number for replays
  3739. */
  3740. /* this is easier than dealing with bitfield access */
  3741. seq_num = ntohl(trailer) & SRTCP_INDEX_MASK;
  3742. debug_print(mod_srtp, "srtcp index: %x", seq_num);
  3743. status = srtp_rdb_check(&stream->rtcp_rdb, seq_num);
  3744. if (status)
  3745. return status;
  3746. /*
  3747. * if we're using aes counter mode, set nonce and seq
  3748. */
  3749. if (session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_128 ||
  3750. session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_192 ||
  3751. session_keys->rtcp_cipher->type->id == SRTP_AES_ICM_256) {
  3752. v128_t iv;
  3753. iv.v32[0] = 0;
  3754. iv.v32[1] = hdr->ssrc; /* still in network order! */
  3755. iv.v32[2] = htonl(seq_num >> 16);
  3756. iv.v32[3] = htonl(seq_num << 16);
  3757. status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv,
  3758. srtp_direction_decrypt);
  3759. } else {
  3760. v128_t iv;
  3761. /* otherwise, just set the index to seq_num */
  3762. iv.v32[0] = 0;
  3763. iv.v32[1] = 0;
  3764. iv.v32[2] = 0;
  3765. iv.v32[3] = htonl(seq_num);
  3766. status = srtp_cipher_set_iv(session_keys->rtcp_cipher, (uint8_t *)&iv,
  3767. srtp_direction_decrypt);
  3768. }
  3769. if (status)
  3770. return srtp_err_status_cipher_fail;
  3771. /* initialize auth func context */
  3772. srtp_auth_start(session_keys->rtcp_auth);
  3773. /* run auth func over packet, put result into tmp_tag */
  3774. status = srtp_auth_compute(session_keys->rtcp_auth, (uint8_t *)auth_start,
  3775. auth_len, tmp_tag);
  3776. debug_print(mod_srtp, "srtcp computed tag: %s",
  3777. srtp_octet_string_hex_string(tmp_tag, tag_len));
  3778. if (status)
  3779. return srtp_err_status_auth_fail;
  3780. /* compare the tag just computed with the one in the packet */
  3781. debug_print(mod_srtp, "srtcp tag from packet: %s",
  3782. srtp_octet_string_hex_string(auth_tag, tag_len));
  3783. if (srtp_octet_string_is_eq(tmp_tag, auth_tag, tag_len))
  3784. return srtp_err_status_auth_fail;
  3785. /*
  3786. * if we're authenticating using a universal hash, put the keystream
  3787. * prefix into the authentication tag
  3788. */
  3789. prefix_len = srtp_auth_get_prefix_length(session_keys->rtcp_auth);
  3790. if (prefix_len) {
  3791. status = srtp_cipher_output(session_keys->rtcp_cipher, auth_tag,
  3792. &prefix_len);
  3793. debug_print(mod_srtp, "keystream prefix: %s",
  3794. srtp_octet_string_hex_string(auth_tag, prefix_len));
  3795. if (status)
  3796. return srtp_err_status_cipher_fail;
  3797. }
  3798. /* if we're decrypting, exor keystream into the message */
  3799. if (enc_start) {
  3800. status = srtp_cipher_decrypt(session_keys->rtcp_cipher,
  3801. (uint8_t *)enc_start, &enc_octet_len);
  3802. if (status)
  3803. return srtp_err_status_cipher_fail;
  3804. }
  3805. /* decrease the packet length by the length of the auth tag and seq_num */
  3806. *pkt_octet_len -= (tag_len + sizeof(srtcp_trailer_t));
  3807. /* decrease the packet length by the length of the mki_size */
  3808. *pkt_octet_len -= mki_size;
  3809. /*
  3810. * if EKT is in effect, subtract the EKT data out of the packet
  3811. * length
  3812. */
  3813. *pkt_octet_len -= srtp_ekt_octets_after_base_tag(stream->ekt);
  3814. /*
  3815. * verify that stream is for received traffic - this check will
  3816. * detect SSRC collisions, since a stream that appears in both
  3817. * srtp_protect() and srtp_unprotect() will fail this test in one of
  3818. * those functions.
  3819. *
  3820. * we do this check *after* the authentication check, so that the
  3821. * latter check will catch any attempts to fool us into thinking
  3822. * that we've got a collision
  3823. */
  3824. if (stream->direction != dir_srtp_receiver) {
  3825. if (stream->direction == dir_unknown) {
  3826. stream->direction = dir_srtp_receiver;
  3827. } else {
  3828. srtp_handle_event(ctx, stream, event_ssrc_collision);
  3829. }
  3830. }
  3831. /*
  3832. * if the stream is a 'provisional' one, in which the template context
  3833. * is used, then we need to allocate a new stream at this point, since
  3834. * the authentication passed
  3835. */
  3836. if (stream == ctx->stream_template) {
  3837. srtp_stream_ctx_t *new_stream;
  3838. /*
  3839. * allocate and initialize a new stream
  3840. *
  3841. * note that we indicate failure if we can't allocate the new
  3842. * stream, and some implementations will want to not return
  3843. * failure here
  3844. */
  3845. status =
  3846. srtp_stream_clone(ctx->stream_template, hdr->ssrc, &new_stream);
  3847. if (status)
  3848. return status;
  3849. /* add new stream to the head of the stream_list */
  3850. new_stream->next = ctx->stream_list;
  3851. ctx->stream_list = new_stream;
  3852. /* set stream (the pointer used in this function) */
  3853. stream = new_stream;
  3854. }
  3855. /* we've passed the authentication check, so add seq_num to the rdb */
  3856. srtp_rdb_add_index(&stream->rtcp_rdb, seq_num);
  3857. return srtp_err_status_ok;
  3858. }
  3859. /*
  3860. * user data within srtp_t context
  3861. */
  3862. void srtp_set_user_data(srtp_t ctx, void *data)
  3863. {
  3864. ctx->user_data = data;
  3865. }
  3866. void *srtp_get_user_data(srtp_t ctx)
  3867. {
  3868. return ctx->user_data;
  3869. }
  3870. /*
  3871. * dtls keying for srtp
  3872. */
  3873. srtp_err_status_t srtp_crypto_policy_set_from_profile_for_rtp(
  3874. srtp_crypto_policy_t *policy,
  3875. srtp_profile_t profile)
  3876. {
  3877. /* set SRTP policy from the SRTP profile in the key set */
  3878. switch (profile) {
  3879. case srtp_profile_aes128_cm_sha1_80:
  3880. srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
  3881. break;
  3882. case srtp_profile_aes128_cm_sha1_32:
  3883. srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(policy);
  3884. break;
  3885. case srtp_profile_null_sha1_80:
  3886. srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy);
  3887. break;
  3888. #ifdef GCM
  3889. case srtp_profile_aead_aes_128_gcm:
  3890. srtp_crypto_policy_set_aes_gcm_128_16_auth(policy);
  3891. break;
  3892. case srtp_profile_aead_aes_256_gcm:
  3893. srtp_crypto_policy_set_aes_gcm_256_16_auth(policy);
  3894. break;
  3895. #endif
  3896. /* the following profiles are not (yet) supported */
  3897. case srtp_profile_null_sha1_32:
  3898. default:
  3899. return srtp_err_status_bad_param;
  3900. }
  3901. return srtp_err_status_ok;
  3902. }
  3903. srtp_err_status_t srtp_crypto_policy_set_from_profile_for_rtcp(
  3904. srtp_crypto_policy_t *policy,
  3905. srtp_profile_t profile)
  3906. {
  3907. /* set SRTP policy from the SRTP profile in the key set */
  3908. switch (profile) {
  3909. case srtp_profile_aes128_cm_sha1_80:
  3910. srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
  3911. break;
  3912. case srtp_profile_aes128_cm_sha1_32:
  3913. /* We do not honor the 32-bit auth tag request since
  3914. * this is not compliant with RFC 3711 */
  3915. srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(policy);
  3916. break;
  3917. case srtp_profile_null_sha1_80:
  3918. srtp_crypto_policy_set_null_cipher_hmac_sha1_80(policy);
  3919. break;
  3920. #ifdef GCM
  3921. case srtp_profile_aead_aes_128_gcm:
  3922. srtp_crypto_policy_set_aes_gcm_128_16_auth(policy);
  3923. break;
  3924. case srtp_profile_aead_aes_256_gcm:
  3925. srtp_crypto_policy_set_aes_gcm_256_16_auth(policy);
  3926. break;
  3927. #endif
  3928. /* the following profiles are not (yet) supported */
  3929. case srtp_profile_null_sha1_32:
  3930. default:
  3931. return srtp_err_status_bad_param;
  3932. }
  3933. return srtp_err_status_ok;
  3934. }
  3935. void srtp_append_salt_to_key(uint8_t *key,
  3936. unsigned int bytes_in_key,
  3937. uint8_t *salt,
  3938. unsigned int bytes_in_salt)
  3939. {
  3940. memcpy(key + bytes_in_key, salt, bytes_in_salt);
  3941. }
  3942. unsigned int srtp_profile_get_master_key_length(srtp_profile_t profile)
  3943. {
  3944. switch (profile) {
  3945. case srtp_profile_aes128_cm_sha1_80:
  3946. return SRTP_AES_128_KEY_LEN;
  3947. break;
  3948. case srtp_profile_aes128_cm_sha1_32:
  3949. return SRTP_AES_128_KEY_LEN;
  3950. break;
  3951. case srtp_profile_null_sha1_80:
  3952. return SRTP_AES_128_KEY_LEN;
  3953. break;
  3954. case srtp_profile_aead_aes_128_gcm:
  3955. return SRTP_AES_128_KEY_LEN;
  3956. break;
  3957. case srtp_profile_aead_aes_256_gcm:
  3958. return SRTP_AES_256_KEY_LEN;
  3959. break;
  3960. /* the following profiles are not (yet) supported */
  3961. case srtp_profile_null_sha1_32:
  3962. default:
  3963. return 0; /* indicate error by returning a zero */
  3964. }
  3965. }
  3966. unsigned int srtp_profile_get_master_salt_length(srtp_profile_t profile)
  3967. {
  3968. switch (profile) {
  3969. case srtp_profile_aes128_cm_sha1_80:
  3970. return SRTP_SALT_LEN;
  3971. break;
  3972. case srtp_profile_aes128_cm_sha1_32:
  3973. return SRTP_SALT_LEN;
  3974. break;
  3975. case srtp_profile_null_sha1_80:
  3976. return SRTP_SALT_LEN;
  3977. break;
  3978. case srtp_profile_aead_aes_128_gcm:
  3979. return SRTP_AEAD_SALT_LEN;
  3980. break;
  3981. case srtp_profile_aead_aes_256_gcm:
  3982. return SRTP_AEAD_SALT_LEN;
  3983. break;
  3984. /* the following profiles are not (yet) supported */
  3985. case srtp_profile_null_sha1_32:
  3986. default:
  3987. return 0; /* indicate error by returning a zero */
  3988. }
  3989. }
  3990. srtp_err_status_t stream_get_protect_trailer_length(srtp_stream_ctx_t *stream,
  3991. uint32_t is_rtp,
  3992. uint32_t use_mki,
  3993. uint32_t mki_index,
  3994. uint32_t *length)
  3995. {
  3996. srtp_session_keys_t *session_key;
  3997. *length = 0;
  3998. if (use_mki) {
  3999. if (mki_index >= stream->num_master_keys) {
  4000. return srtp_err_status_bad_mki;
  4001. }
  4002. session_key = &stream->session_keys[mki_index];
  4003. *length += session_key->mki_size;
  4004. } else {
  4005. session_key = &stream->session_keys[0];
  4006. }
  4007. if (is_rtp) {
  4008. *length += srtp_auth_get_tag_length(session_key->rtp_auth);
  4009. } else {
  4010. *length += srtp_auth_get_tag_length(session_key->rtcp_auth);
  4011. *length += sizeof(srtcp_trailer_t);
  4012. }
  4013. return srtp_err_status_ok;
  4014. }
  4015. srtp_err_status_t get_protect_trailer_length(srtp_t session,
  4016. uint32_t is_rtp,
  4017. uint32_t use_mki,
  4018. uint32_t mki_index,
  4019. uint32_t *length)
  4020. {
  4021. srtp_stream_ctx_t *stream;
  4022. if (session == NULL) {
  4023. return srtp_err_status_bad_param;
  4024. }
  4025. if (session->stream_template == NULL && session->stream_list == NULL) {
  4026. return srtp_err_status_bad_param;
  4027. }
  4028. *length = 0;
  4029. stream = session->stream_template;
  4030. if (stream != NULL) {
  4031. stream_get_protect_trailer_length(stream, is_rtp, use_mki, mki_index,
  4032. length);
  4033. }
  4034. stream = session->stream_list;
  4035. while (stream != NULL) {
  4036. uint32_t temp_length;
  4037. if (stream_get_protect_trailer_length(stream, is_rtp, use_mki,
  4038. mki_index, &temp_length) ==
  4039. srtp_err_status_ok) {
  4040. if (temp_length > *length) {
  4041. *length = temp_length;
  4042. }
  4043. }
  4044. stream = stream->next;
  4045. }
  4046. return srtp_err_status_ok;
  4047. }
  4048. srtp_err_status_t srtp_get_protect_trailer_length(srtp_t session,
  4049. uint32_t use_mki,
  4050. uint32_t mki_index,
  4051. uint32_t *length)
  4052. {
  4053. return get_protect_trailer_length(session, 1, use_mki, mki_index, length);
  4054. }
  4055. srtp_err_status_t srtp_get_protect_rtcp_trailer_length(srtp_t session,
  4056. uint32_t use_mki,
  4057. uint32_t mki_index,
  4058. uint32_t *length)
  4059. {
  4060. return get_protect_trailer_length(session, 0, use_mki, mki_index, length);
  4061. }
  4062. /*
  4063. * SRTP debug interface
  4064. */
  4065. srtp_err_status_t srtp_set_debug_module(const char *mod_name, int v)
  4066. {
  4067. return srtp_crypto_kernel_set_debug_module(mod_name, v);
  4068. }
  4069. srtp_err_status_t srtp_list_debug_modules(void)
  4070. {
  4071. return srtp_crypto_kernel_list_debug_modules();
  4072. }
  4073. /*
  4074. * srtp_log_handler is a global variable holding a pointer to the
  4075. * log handler function; this function is called for any log
  4076. * output.
  4077. */
  4078. static srtp_log_handler_func_t *srtp_log_handler = NULL;
  4079. static void *srtp_log_handler_data = NULL;
  4080. void srtp_err_handler(srtp_err_reporting_level_t level, const char *msg)
  4081. {
  4082. if (srtp_log_handler) {
  4083. srtp_log_level_t log_level = srtp_log_level_error;
  4084. switch (level) {
  4085. case srtp_err_level_error:
  4086. log_level = srtp_log_level_error;
  4087. break;
  4088. case srtp_err_level_warning:
  4089. log_level = srtp_log_level_warning;
  4090. break;
  4091. case srtp_err_level_info:
  4092. log_level = srtp_log_level_info;
  4093. break;
  4094. case srtp_err_level_debug:
  4095. log_level = srtp_log_level_debug;
  4096. break;
  4097. }
  4098. srtp_log_handler(log_level, msg, srtp_log_handler_data);
  4099. }
  4100. }
  4101. srtp_err_status_t srtp_install_log_handler(srtp_log_handler_func_t func,
  4102. void *data)
  4103. {
  4104. /*
  4105. * note that we accept NULL arguments intentionally - calling this
  4106. * function with a NULL arguments removes a log handler that's
  4107. * been previously installed
  4108. */
  4109. if (srtp_log_handler) {
  4110. srtp_install_err_report_handler(NULL);
  4111. }
  4112. srtp_log_handler = func;
  4113. srtp_log_handler_data = data;
  4114. if (srtp_log_handler) {
  4115. srtp_install_err_report_handler(srtp_err_handler);
  4116. }
  4117. return srtp_err_status_ok;
  4118. }
  4119. srtp_err_status_t srtp_set_stream_roc(srtp_t session,
  4120. uint32_t ssrc,
  4121. uint32_t roc)
  4122. {
  4123. srtp_stream_t stream;
  4124. stream = srtp_get_stream(session, htonl(ssrc));
  4125. if (stream == NULL)
  4126. return srtp_err_status_bad_param;
  4127. stream->pending_roc = roc;
  4128. return srtp_err_status_ok;
  4129. }
  4130. srtp_err_status_t srtp_get_stream_roc(srtp_t session,
  4131. uint32_t ssrc,
  4132. uint32_t *roc)
  4133. {
  4134. srtp_stream_t stream;
  4135. stream = srtp_get_stream(session, htonl(ssrc));
  4136. if (stream == NULL)
  4137. return srtp_err_status_bad_param;
  4138. *roc = srtp_rdbx_get_roc(&stream->rtp_rdbx);
  4139. return srtp_err_status_ok;
  4140. }