srtp.c 153 KB

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