srtp.c 160 KB

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