test_ap_wps.py 396 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030
  1. # WPS tests
  2. # Copyright (c) 2013-2017, Jouni Malinen <j@w1.fi>
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. from remotehost import remote_compatible
  7. import base64
  8. import binascii
  9. from Crypto.Cipher import AES
  10. import hashlib
  11. import hmac
  12. import os
  13. import time
  14. import stat
  15. import subprocess
  16. import logging
  17. logger = logging.getLogger()
  18. import re
  19. import socket
  20. import struct
  21. import httplib
  22. import urlparse
  23. import urllib
  24. import xml.etree.ElementTree as ET
  25. import StringIO
  26. import SocketServer
  27. import hwsim_utils
  28. import hostapd
  29. from wpasupplicant import WpaSupplicant
  30. from utils import HwsimSkip, alloc_fail, fail_test, skip_with_fips
  31. from utils import wait_fail_trigger
  32. from test_ap_eap import int_eap_server_params
  33. def wps_start_ap(apdev, ssid="test-wps-conf"):
  34. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  35. "wpa_passphrase": "12345678", "wpa": "2",
  36. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" }
  37. return hostapd.add_ap(apdev, params)
  38. @remote_compatible
  39. def test_ap_wps_init(dev, apdev):
  40. """Initial AP configuration with first WPS Enrollee"""
  41. ssid = "test-wps"
  42. hapd = hostapd.add_ap(apdev[0],
  43. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  44. logger.info("WPS provisioning step")
  45. hapd.request("WPS_PBC")
  46. if "PBC Status: Active" not in hapd.request("WPS_GET_STATUS"):
  47. raise Exception("PBC status not shown correctly")
  48. id = dev[0].add_network()
  49. dev[0].set_network_quoted(id, "ssid", "home")
  50. dev[0].set_network_quoted(id, "psk", "12345678")
  51. dev[0].request("ENABLE_NETWORK %s no-connect" % id)
  52. id = dev[0].add_network()
  53. dev[0].set_network_quoted(id, "ssid", "home2")
  54. dev[0].set_network(id, "bssid", "00:11:22:33:44:55")
  55. dev[0].set_network(id, "key_mgmt", "NONE")
  56. dev[0].request("ENABLE_NETWORK %s no-connect" % id)
  57. dev[0].request("WPS_PBC")
  58. dev[0].wait_connected(timeout=30)
  59. status = dev[0].get_status()
  60. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  61. raise Exception("Not fully connected")
  62. if status['ssid'] != ssid:
  63. raise Exception("Unexpected SSID")
  64. if status['pairwise_cipher'] != 'CCMP':
  65. raise Exception("Unexpected encryption configuration")
  66. if status['key_mgmt'] != 'WPA2-PSK':
  67. raise Exception("Unexpected key_mgmt")
  68. status = hapd.request("WPS_GET_STATUS")
  69. if "PBC Status: Disabled" not in status:
  70. raise Exception("PBC status not shown correctly")
  71. if "Last WPS result: Success" not in status:
  72. raise Exception("Last WPS result not shown correctly")
  73. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  74. raise Exception("Peer address not shown correctly")
  75. conf = hapd.request("GET_CONFIG")
  76. if "wps_state=configured" not in conf:
  77. raise Exception("AP not in WPS configured state")
  78. if "wpa=3" not in conf:
  79. raise Exception("AP not in WPA+WPA2 configuration")
  80. if "rsn_pairwise_cipher=CCMP TKIP" not in conf:
  81. raise Exception("Unexpected rsn_pairwise_cipher")
  82. if "wpa_pairwise_cipher=CCMP TKIP" not in conf:
  83. raise Exception("Unexpected wpa_pairwise_cipher")
  84. if "group_cipher=TKIP" not in conf:
  85. raise Exception("Unexpected group_cipher")
  86. if len(dev[0].list_networks()) != 3:
  87. raise Exception("Unexpected number of network blocks")
  88. def test_ap_wps_init_2ap_pbc(dev, apdev):
  89. """Initial two-radio AP configuration with first WPS PBC Enrollee"""
  90. ssid = "test-wps"
  91. params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
  92. hapd = hostapd.add_ap(apdev[0], params)
  93. hostapd.add_ap(apdev[1], params)
  94. logger.info("WPS provisioning step")
  95. hapd.request("WPS_PBC")
  96. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  97. dev[0].scan_for_bss(apdev[1]['bssid'], freq="2412")
  98. bss = dev[0].get_bss(apdev[0]['bssid'])
  99. if "[WPS-PBC]" not in bss['flags']:
  100. raise Exception("WPS-PBC flag missing from AP1")
  101. bss = dev[0].get_bss(apdev[1]['bssid'])
  102. if "[WPS-PBC]" not in bss['flags']:
  103. raise Exception("WPS-PBC flag missing from AP2")
  104. dev[0].dump_monitor()
  105. dev[0].request("SET wps_cred_processing 2")
  106. dev[0].request("WPS_PBC")
  107. ev = dev[0].wait_event(["WPS-CRED-RECEIVED"], timeout=30)
  108. dev[0].request("SET wps_cred_processing 0")
  109. if ev is None:
  110. raise Exception("WPS cred event not seen")
  111. if "100e" not in ev:
  112. raise Exception("WPS attributes not included in the cred event")
  113. dev[0].wait_connected(timeout=30)
  114. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  115. dev[1].scan_for_bss(apdev[1]['bssid'], freq="2412")
  116. bss = dev[1].get_bss(apdev[0]['bssid'])
  117. if "[WPS-PBC]" in bss['flags']:
  118. raise Exception("WPS-PBC flag not cleared from AP1")
  119. bss = dev[1].get_bss(apdev[1]['bssid'])
  120. if "[WPS-PBC]" in bss['flags']:
  121. raise Exception("WPS-PBC flag not cleared from AP2")
  122. def test_ap_wps_init_2ap_pin(dev, apdev):
  123. """Initial two-radio AP configuration with first WPS PIN Enrollee"""
  124. ssid = "test-wps"
  125. params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
  126. hapd = hostapd.add_ap(apdev[0], params)
  127. hostapd.add_ap(apdev[1], params)
  128. logger.info("WPS provisioning step")
  129. pin = dev[0].wps_read_pin()
  130. hapd.request("WPS_PIN any " + pin)
  131. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  132. dev[0].scan_for_bss(apdev[1]['bssid'], freq="2412")
  133. bss = dev[0].get_bss(apdev[0]['bssid'])
  134. if "[WPS-AUTH]" not in bss['flags']:
  135. raise Exception("WPS-AUTH flag missing from AP1")
  136. bss = dev[0].get_bss(apdev[1]['bssid'])
  137. if "[WPS-AUTH]" not in bss['flags']:
  138. raise Exception("WPS-AUTH flag missing from AP2")
  139. dev[0].dump_monitor()
  140. dev[0].request("WPS_PIN any " + pin)
  141. dev[0].wait_connected(timeout=30)
  142. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  143. dev[1].scan_for_bss(apdev[1]['bssid'], freq="2412")
  144. bss = dev[1].get_bss(apdev[0]['bssid'])
  145. if "[WPS-AUTH]" in bss['flags']:
  146. raise Exception("WPS-AUTH flag not cleared from AP1")
  147. bss = dev[1].get_bss(apdev[1]['bssid'])
  148. if "[WPS-AUTH]" in bss['flags']:
  149. raise Exception("WPS-AUTH flag not cleared from AP2")
  150. @remote_compatible
  151. def test_ap_wps_init_through_wps_config(dev, apdev):
  152. """Initial AP configuration using wps_config command"""
  153. ssid = "test-wps-init-config"
  154. hapd = hostapd.add_ap(apdev[0],
  155. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  156. if "FAIL" in hapd.request("WPS_CONFIG " + ssid.encode("hex") + " WPA2PSK CCMP " + "12345678".encode("hex")):
  157. raise Exception("WPS_CONFIG command failed")
  158. ev = hapd.wait_event(["WPS-NEW-AP-SETTINGS"], timeout=5)
  159. if ev is None:
  160. raise Exception("Timeout on WPS-NEW-AP-SETTINGS events")
  161. # It takes some time for the AP to update Beacon and Probe Response frames,
  162. # so wait here before requesting the scan to be started to avoid adding
  163. # extra five second wait to the test due to fetching obsolete scan results.
  164. hapd.ping()
  165. time.sleep(0.2)
  166. dev[0].connect(ssid, psk="12345678", scan_freq="2412", proto="WPA2",
  167. pairwise="CCMP", group="CCMP")
  168. if "FAIL" not in hapd.request("WPS_CONFIG foo"):
  169. raise Exception("Invalid WPS_CONFIG accepted")
  170. @remote_compatible
  171. def test_ap_wps_init_through_wps_config_2(dev, apdev):
  172. """AP configuration using wps_config and wps_cred_processing=2"""
  173. ssid = "test-wps-init-config"
  174. hapd = hostapd.add_ap(apdev[0],
  175. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  176. "wps_cred_processing": "2" })
  177. if "FAIL" in hapd.request("WPS_CONFIG " + ssid.encode("hex") + " WPA2PSK CCMP " + "12345678".encode("hex")):
  178. raise Exception("WPS_CONFIG command failed")
  179. ev = hapd.wait_event(["WPS-NEW-AP-SETTINGS"], timeout=5)
  180. if ev is None:
  181. raise Exception("Timeout on WPS-NEW-AP-SETTINGS events")
  182. if "100e" not in ev:
  183. raise Exception("WPS-NEW-AP-SETTINGS did not include Credential")
  184. @remote_compatible
  185. def test_ap_wps_invalid_wps_config_passphrase(dev, apdev):
  186. """AP configuration using wps_config command with invalid passphrase"""
  187. ssid = "test-wps-init-config"
  188. hapd = hostapd.add_ap(apdev[0],
  189. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  190. if "FAIL" not in hapd.request("WPS_CONFIG " + ssid.encode("hex") + " WPA2PSK CCMP " + "1234567".encode("hex")):
  191. raise Exception("Invalid WPS_CONFIG command accepted")
  192. def test_ap_wps_conf(dev, apdev):
  193. """WPS PBC provisioning with configured AP"""
  194. ssid = "test-wps-conf"
  195. hapd = hostapd.add_ap(apdev[0],
  196. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  197. "wpa_passphrase": "12345678", "wpa": "2",
  198. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  199. logger.info("WPS provisioning step")
  200. hapd.request("WPS_PBC")
  201. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  202. dev[0].dump_monitor()
  203. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  204. dev[0].wait_connected(timeout=30)
  205. status = dev[0].get_status()
  206. if status['wpa_state'] != 'COMPLETED':
  207. raise Exception("Not fully connected")
  208. if status['bssid'] != apdev[0]['bssid']:
  209. raise Exception("Unexpected BSSID")
  210. if status['ssid'] != ssid:
  211. raise Exception("Unexpected SSID")
  212. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  213. raise Exception("Unexpected encryption configuration")
  214. if status['key_mgmt'] != 'WPA2-PSK':
  215. raise Exception("Unexpected key_mgmt")
  216. sta = hapd.get_sta(dev[0].p2p_interface_addr())
  217. if 'wpsDeviceName' not in sta or sta['wpsDeviceName'] != "Device A":
  218. raise Exception("Device name not available in STA command")
  219. def test_ap_wps_conf_5ghz(dev, apdev):
  220. """WPS PBC provisioning with configured AP on 5 GHz band"""
  221. try:
  222. hapd = None
  223. ssid = "test-wps-conf"
  224. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  225. "wpa_passphrase": "12345678", "wpa": "2",
  226. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  227. "country_code": "FI", "hw_mode": "a", "channel": "36" }
  228. hapd = hostapd.add_ap(apdev[0], params)
  229. logger.info("WPS provisioning step")
  230. hapd.request("WPS_PBC")
  231. dev[0].scan_for_bss(apdev[0]['bssid'], freq="5180")
  232. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  233. dev[0].wait_connected(timeout=30)
  234. sta = hapd.get_sta(dev[0].p2p_interface_addr())
  235. if 'wpsDeviceName' not in sta or sta['wpsDeviceName'] != "Device A":
  236. raise Exception("Device name not available in STA command")
  237. finally:
  238. dev[0].request("DISCONNECT")
  239. if hapd:
  240. hapd.request("DISABLE")
  241. subprocess.call(['iw', 'reg', 'set', '00'])
  242. dev[0].flush_scan_cache()
  243. def test_ap_wps_conf_chan14(dev, apdev):
  244. """WPS PBC provisioning with configured AP on channel 14"""
  245. try:
  246. hapd = None
  247. ssid = "test-wps-conf"
  248. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  249. "wpa_passphrase": "12345678", "wpa": "2",
  250. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  251. "country_code": "JP", "hw_mode": "b", "channel": "14" }
  252. hapd = hostapd.add_ap(apdev[0], params)
  253. logger.info("WPS provisioning step")
  254. hapd.request("WPS_PBC")
  255. dev[0].request("WPS_PBC")
  256. dev[0].wait_connected(timeout=30)
  257. sta = hapd.get_sta(dev[0].p2p_interface_addr())
  258. if 'wpsDeviceName' not in sta or sta['wpsDeviceName'] != "Device A":
  259. raise Exception("Device name not available in STA command")
  260. finally:
  261. dev[0].request("DISCONNECT")
  262. if hapd:
  263. hapd.request("DISABLE")
  264. subprocess.call(['iw', 'reg', 'set', '00'])
  265. dev[0].flush_scan_cache()
  266. @remote_compatible
  267. def test_ap_wps_twice(dev, apdev):
  268. """WPS provisioning with twice to change passphrase"""
  269. ssid = "test-wps-twice"
  270. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  271. "wpa_passphrase": "12345678", "wpa": "2",
  272. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" }
  273. hapd = hostapd.add_ap(apdev[0], params)
  274. logger.info("WPS provisioning step")
  275. hapd.request("WPS_PBC")
  276. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  277. dev[0].dump_monitor()
  278. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  279. dev[0].wait_connected(timeout=30)
  280. dev[0].request("DISCONNECT")
  281. logger.info("Restart AP with different passphrase and re-run WPS")
  282. hostapd.remove_bss(apdev[0])
  283. params['wpa_passphrase'] = 'another passphrase'
  284. hapd = hostapd.add_ap(apdev[0], params)
  285. logger.info("WPS provisioning step")
  286. hapd.request("WPS_PBC")
  287. dev[0].dump_monitor()
  288. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  289. dev[0].wait_connected(timeout=30)
  290. networks = dev[0].list_networks()
  291. if len(networks) > 1:
  292. raise Exception("Unexpected duplicated network block present")
  293. @remote_compatible
  294. def test_ap_wps_incorrect_pin(dev, apdev):
  295. """WPS PIN provisioning with incorrect PIN"""
  296. ssid = "test-wps-incorrect-pin"
  297. hapd = hostapd.add_ap(apdev[0],
  298. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  299. "wpa_passphrase": "12345678", "wpa": "2",
  300. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  301. logger.info("WPS provisioning attempt 1")
  302. hapd.request("WPS_PIN any 12345670")
  303. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  304. dev[0].dump_monitor()
  305. dev[0].request("WPS_PIN %s 55554444" % apdev[0]['bssid'])
  306. ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
  307. if ev is None:
  308. raise Exception("WPS operation timed out")
  309. if "config_error=18" not in ev:
  310. raise Exception("Incorrect config_error reported")
  311. if "msg=8" not in ev:
  312. raise Exception("PIN error detected on incorrect message")
  313. dev[0].wait_disconnected(timeout=10)
  314. dev[0].request("WPS_CANCEL")
  315. # if a scan was in progress, wait for it to complete before trying WPS again
  316. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  317. status = hapd.request("WPS_GET_STATUS")
  318. if "Last WPS result: Failed" not in status:
  319. raise Exception("WPS failure result not shown correctly")
  320. logger.info("WPS provisioning attempt 2")
  321. hapd.request("WPS_PIN any 12345670")
  322. dev[0].dump_monitor()
  323. dev[0].request("WPS_PIN %s 12344444" % apdev[0]['bssid'])
  324. ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
  325. if ev is None:
  326. raise Exception("WPS operation timed out")
  327. if "config_error=18" not in ev:
  328. raise Exception("Incorrect config_error reported")
  329. if "msg=10" not in ev:
  330. raise Exception("PIN error detected on incorrect message")
  331. dev[0].wait_disconnected(timeout=10)
  332. @remote_compatible
  333. def test_ap_wps_conf_pin(dev, apdev):
  334. """WPS PIN provisioning with configured AP"""
  335. ssid = "test-wps-conf-pin"
  336. hapd = hostapd.add_ap(apdev[0],
  337. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  338. "wpa_passphrase": "12345678", "wpa": "2",
  339. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  340. logger.info("WPS provisioning step")
  341. pin = dev[0].wps_read_pin()
  342. hapd.request("WPS_PIN any " + pin)
  343. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  344. dev[0].dump_monitor()
  345. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  346. dev[0].wait_connected(timeout=30)
  347. status = dev[0].get_status()
  348. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  349. raise Exception("Not fully connected")
  350. if status['ssid'] != ssid:
  351. raise Exception("Unexpected SSID")
  352. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  353. raise Exception("Unexpected encryption configuration")
  354. if status['key_mgmt'] != 'WPA2-PSK':
  355. raise Exception("Unexpected key_mgmt")
  356. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  357. bss = dev[1].get_bss(apdev[0]['bssid'])
  358. if "[WPS-AUTH]" in bss['flags']:
  359. raise Exception("WPS-AUTH flag not cleared")
  360. logger.info("Try to connect from another station using the same PIN")
  361. pin = dev[1].request("WPS_PIN " + apdev[0]['bssid'])
  362. ev = dev[1].wait_event(["WPS-M2D","CTRL-EVENT-CONNECTED"], timeout=30)
  363. if ev is None:
  364. raise Exception("Operation timed out")
  365. if "WPS-M2D" not in ev:
  366. raise Exception("Unexpected WPS operation started")
  367. hapd.request("WPS_PIN any " + pin)
  368. dev[1].wait_connected(timeout=30)
  369. def test_ap_wps_conf_pin_mixed_mode(dev, apdev):
  370. """WPS PIN provisioning with configured AP (WPA+WPA2)"""
  371. ssid = "test-wps-conf-pin-mixed"
  372. hapd = hostapd.add_ap(apdev[0],
  373. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  374. "wpa_passphrase": "12345678", "wpa": "3",
  375. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  376. "wpa_pairwise": "TKIP" })
  377. logger.info("WPS provisioning step")
  378. pin = dev[0].wps_read_pin()
  379. hapd.request("WPS_PIN any " + pin)
  380. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  381. dev[0].dump_monitor()
  382. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  383. dev[0].wait_connected(timeout=30)
  384. status = dev[0].get_status()
  385. dev[0].request("REMOVE_NETWORK all")
  386. dev[0].wait_disconnected()
  387. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP' or status['key_mgmt'] != 'WPA2-PSK':
  388. raise Exception("Unexpected encryption/key_mgmt configuration: pairwise=%s group=%s key_mgmt=%s" % (status['pairwise_cipher'], status['group_cipher'], status['key_mgmt']))
  389. logger.info("WPS provisioning step (auth_types=0x1b)")
  390. if "OK" not in dev[0].request("SET wps_force_auth_types 0x1b"):
  391. raise Exception("Failed to set wps_force_auth_types 0x1b")
  392. pin = dev[0].wps_read_pin()
  393. hapd.request("WPS_PIN any " + pin)
  394. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  395. dev[0].dump_monitor()
  396. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  397. dev[0].wait_connected(timeout=30)
  398. status = dev[0].get_status()
  399. dev[0].request("REMOVE_NETWORK all")
  400. dev[0].wait_disconnected()
  401. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP' or status['key_mgmt'] != 'WPA2-PSK':
  402. raise Exception("Unexpected encryption/key_mgmt configuration: pairwise=%s group=%s key_mgmt=%s" % (status['pairwise_cipher'], status['group_cipher'], status['key_mgmt']))
  403. logger.info("WPS provisioning step (auth_types=0 encr_types=0)")
  404. if "OK" not in dev[0].request("SET wps_force_auth_types 0"):
  405. raise Exception("Failed to set wps_force_auth_types 0")
  406. if "OK" not in dev[0].request("SET wps_force_encr_types 0"):
  407. raise Exception("Failed to set wps_force_encr_types 0")
  408. pin = dev[0].wps_read_pin()
  409. hapd.request("WPS_PIN any " + pin)
  410. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  411. dev[0].dump_monitor()
  412. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  413. dev[0].wait_connected(timeout=30)
  414. status = dev[0].get_status()
  415. dev[0].request("REMOVE_NETWORK all")
  416. dev[0].wait_disconnected()
  417. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP' or status['key_mgmt'] != 'WPA2-PSK':
  418. raise Exception("Unexpected encryption/key_mgmt configuration: pairwise=%s group=%s key_mgmt=%s" % (status['pairwise_cipher'], status['group_cipher'], status['key_mgmt']))
  419. dev[0].request("SET wps_force_auth_types ")
  420. dev[0].request("SET wps_force_encr_types ")
  421. @remote_compatible
  422. def test_ap_wps_conf_pin_v1(dev, apdev):
  423. """WPS PIN provisioning with configured WPS v1.0 AP"""
  424. ssid = "test-wps-conf-pin-v1"
  425. hapd = hostapd.add_ap(apdev[0],
  426. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  427. "wpa_passphrase": "12345678", "wpa": "2",
  428. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  429. logger.info("WPS provisioning step")
  430. pin = dev[0].wps_read_pin()
  431. hapd.request("SET wps_version_number 0x10")
  432. hapd.request("WPS_PIN any " + pin)
  433. found = False
  434. for i in range(0, 10):
  435. dev[0].scan(freq="2412")
  436. if "[WPS-PIN]" in dev[0].request("SCAN_RESULTS"):
  437. found = True
  438. break
  439. if not found:
  440. hapd.request("SET wps_version_number 0x20")
  441. raise Exception("WPS-PIN flag not seen in scan results")
  442. dev[0].dump_monitor()
  443. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  444. dev[0].wait_connected(timeout=30)
  445. hapd.request("SET wps_version_number 0x20")
  446. @remote_compatible
  447. def test_ap_wps_conf_pin_2sta(dev, apdev):
  448. """Two stations trying to use WPS PIN at the same time"""
  449. ssid = "test-wps-conf-pin2"
  450. hapd = hostapd.add_ap(apdev[0],
  451. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  452. "wpa_passphrase": "12345678", "wpa": "2",
  453. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  454. logger.info("WPS provisioning step")
  455. pin = "12345670"
  456. pin2 = "55554444"
  457. hapd.request("WPS_PIN " + dev[0].get_status_field("uuid") + " " + pin)
  458. hapd.request("WPS_PIN " + dev[1].get_status_field("uuid") + " " + pin)
  459. dev[0].dump_monitor()
  460. dev[1].dump_monitor()
  461. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  462. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
  463. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  464. dev[1].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  465. dev[0].wait_connected(timeout=30)
  466. dev[1].wait_connected(timeout=30)
  467. @remote_compatible
  468. def test_ap_wps_conf_pin_timeout(dev, apdev):
  469. """WPS PIN provisioning with configured AP timing out PIN"""
  470. ssid = "test-wps-conf-pin"
  471. hapd = hostapd.add_ap(apdev[0],
  472. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  473. "wpa_passphrase": "12345678", "wpa": "2",
  474. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  475. addr = dev[0].p2p_interface_addr()
  476. pin = dev[0].wps_read_pin()
  477. if "FAIL" not in hapd.request("WPS_PIN "):
  478. raise Exception("Unexpected success on invalid WPS_PIN")
  479. hapd.request("WPS_PIN any " + pin + " 1")
  480. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  481. time.sleep(1.1)
  482. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  483. ev = hapd.wait_event(["WPS-PIN-NEEDED"], timeout=20)
  484. if ev is None:
  485. raise Exception("WPS-PIN-NEEDED event timed out")
  486. ev = dev[0].wait_event(["WPS-M2D"])
  487. if ev is None:
  488. raise Exception("M2D not reported")
  489. dev[0].request("WPS_CANCEL")
  490. hapd.request("WPS_PIN any " + pin + " 20 " + addr)
  491. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  492. dev[0].wait_connected(timeout=30)
  493. def test_ap_wps_reg_connect(dev, apdev):
  494. """WPS registrar using AP PIN to connect"""
  495. ssid = "test-wps-reg-ap-pin"
  496. appin = "12345670"
  497. hostapd.add_ap(apdev[0],
  498. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  499. "wpa_passphrase": "12345678", "wpa": "2",
  500. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  501. "ap_pin": appin})
  502. logger.info("WPS provisioning step")
  503. dev[0].dump_monitor()
  504. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  505. dev[0].wps_reg(apdev[0]['bssid'], appin)
  506. status = dev[0].get_status()
  507. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  508. raise Exception("Not fully connected")
  509. if status['ssid'] != ssid:
  510. raise Exception("Unexpected SSID")
  511. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  512. raise Exception("Unexpected encryption configuration")
  513. if status['key_mgmt'] != 'WPA2-PSK':
  514. raise Exception("Unexpected key_mgmt")
  515. def test_ap_wps_reg_connect_zero_len_ap_pin(dev, apdev):
  516. """hostapd with zero length ap_pin parameter"""
  517. ssid = "test-wps-reg-ap-pin"
  518. appin = ""
  519. hostapd.add_ap(apdev[0],
  520. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  521. "wpa_passphrase": "12345678", "wpa": "2",
  522. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  523. "ap_pin": appin})
  524. logger.info("WPS provisioning step")
  525. dev[0].dump_monitor()
  526. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  527. dev[0].wps_reg(apdev[0]['bssid'], appin, no_wait=True)
  528. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  529. if ev is None:
  530. raise Exception("No WPS-FAIL reported")
  531. if "msg=5 config_error=15" not in ev:
  532. raise Exception("Unexpected WPS-FAIL: " + ev)
  533. def test_ap_wps_reg_connect_mixed_mode(dev, apdev):
  534. """WPS registrar using AP PIN to connect (WPA+WPA2)"""
  535. ssid = "test-wps-reg-ap-pin"
  536. appin = "12345670"
  537. hostapd.add_ap(apdev[0],
  538. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  539. "wpa_passphrase": "12345678", "wpa": "3",
  540. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  541. "wpa_pairwise": "TKIP", "ap_pin": appin})
  542. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  543. dev[0].wps_reg(apdev[0]['bssid'], appin)
  544. status = dev[0].get_status()
  545. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  546. raise Exception("Not fully connected")
  547. if status['ssid'] != ssid:
  548. raise Exception("Unexpected SSID")
  549. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  550. raise Exception("Unexpected encryption configuration")
  551. if status['key_mgmt'] != 'WPA2-PSK':
  552. raise Exception("Unexpected key_mgmt")
  553. def test_ap_wps_reg_override_ap_settings(dev, apdev):
  554. """WPS registrar and ap_settings override"""
  555. ap_settings = "/tmp/ap_wps_reg_override_ap_settings"
  556. try:
  557. os.remove(ap_settings)
  558. except:
  559. pass
  560. # Override AP Settings with values that point to another AP
  561. data = build_wsc_attr(ATTR_NETWORK_INDEX, '\x01')
  562. data += build_wsc_attr(ATTR_SSID, "test")
  563. data += build_wsc_attr(ATTR_AUTH_TYPE, '\x00\x01')
  564. data += build_wsc_attr(ATTR_ENCR_TYPE, '\x00\x01')
  565. data += build_wsc_attr(ATTR_NETWORK_KEY, '')
  566. data += build_wsc_attr(ATTR_MAC_ADDR, binascii.unhexlify(apdev[1]['bssid'].replace(':', '')))
  567. with open(ap_settings, "w") as f:
  568. f.write(data)
  569. ssid = "test-wps-reg-ap-pin"
  570. appin = "12345670"
  571. hostapd.add_ap(apdev[0],
  572. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  573. "wpa_passphrase": "12345678", "wpa": "2",
  574. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  575. "ap_pin": appin, "ap_settings": ap_settings })
  576. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "test" })
  577. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  578. dev[0].scan_for_bss(apdev[1]['bssid'], freq=2412)
  579. dev[0].wps_reg(apdev[0]['bssid'], appin)
  580. ev = hapd2.wait_event(['AP-STA-CONNECTED'], timeout=10)
  581. os.remove(ap_settings)
  582. if ev is None:
  583. raise Exception("No connection with the other AP")
  584. def check_wps_reg_failure(dev, ap, appin):
  585. dev.request("WPS_REG " + ap['bssid'] + " " + appin)
  586. ev = dev.wait_event(["WPS-SUCCESS", "WPS-FAIL"], timeout=15)
  587. if ev is None:
  588. raise Exception("WPS operation timed out")
  589. if "WPS-SUCCESS" in ev:
  590. raise Exception("WPS operation succeeded unexpectedly")
  591. if "config_error=15" not in ev:
  592. raise Exception("WPS setup locked state was not reported correctly")
  593. def test_ap_wps_random_ap_pin(dev, apdev):
  594. """WPS registrar using random AP PIN"""
  595. ssid = "test-wps-reg-random-ap-pin"
  596. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  597. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  598. "wpa_passphrase": "12345678", "wpa": "2",
  599. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  600. "device_name": "Wireless AP", "manufacturer": "Company",
  601. "model_name": "WAP", "model_number": "123",
  602. "serial_number": "12345", "device_type": "6-0050F204-1",
  603. "os_version": "01020300",
  604. "config_methods": "label push_button",
  605. "uuid": ap_uuid, "upnp_iface": "lo" }
  606. hapd = hostapd.add_ap(apdev[0], params)
  607. appin = hapd.request("WPS_AP_PIN random")
  608. if "FAIL" in appin:
  609. raise Exception("Could not generate random AP PIN")
  610. if appin not in hapd.request("WPS_AP_PIN get"):
  611. raise Exception("Could not fetch current AP PIN")
  612. logger.info("WPS provisioning step")
  613. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  614. dev[0].wps_reg(apdev[0]['bssid'], appin)
  615. hapd.request("WPS_AP_PIN disable")
  616. logger.info("WPS provisioning step with AP PIN disabled")
  617. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  618. check_wps_reg_failure(dev[1], apdev[0], appin)
  619. logger.info("WPS provisioning step with AP PIN reset")
  620. appin = "12345670"
  621. hapd.request("WPS_AP_PIN set " + appin)
  622. dev[1].wps_reg(apdev[0]['bssid'], appin)
  623. dev[0].request("REMOVE_NETWORK all")
  624. dev[1].request("REMOVE_NETWORK all")
  625. dev[0].wait_disconnected(timeout=10)
  626. dev[1].wait_disconnected(timeout=10)
  627. logger.info("WPS provisioning step after AP PIN timeout")
  628. hapd.request("WPS_AP_PIN disable")
  629. appin = hapd.request("WPS_AP_PIN random 1")
  630. time.sleep(1.1)
  631. if "FAIL" not in hapd.request("WPS_AP_PIN get"):
  632. raise Exception("AP PIN unexpectedly still enabled")
  633. check_wps_reg_failure(dev[0], apdev[0], appin)
  634. logger.info("WPS provisioning step after AP PIN timeout(2)")
  635. hapd.request("WPS_AP_PIN disable")
  636. appin = "12345670"
  637. hapd.request("WPS_AP_PIN set " + appin + " 1")
  638. time.sleep(1.1)
  639. if "FAIL" not in hapd.request("WPS_AP_PIN get"):
  640. raise Exception("AP PIN unexpectedly still enabled")
  641. check_wps_reg_failure(dev[1], apdev[0], appin)
  642. with fail_test(hapd, 1, "os_get_random;wps_generate_pin"):
  643. hapd.request("WPS_AP_PIN random 1")
  644. hapd.request("WPS_AP_PIN disable")
  645. with alloc_fail(hapd, 1, "upnp_wps_set_ap_pin"):
  646. hapd.request("WPS_AP_PIN set 12345670")
  647. hapd.request("WPS_AP_PIN disable")
  648. if "FAIL" not in hapd.request("WPS_AP_PIN set"):
  649. raise Exception("Invalid WPS_AP_PIN accepted")
  650. if "FAIL" not in hapd.request("WPS_AP_PIN foo"):
  651. raise Exception("Invalid WPS_AP_PIN accepted")
  652. def test_ap_wps_reg_config(dev, apdev):
  653. """WPS registrar configuring an AP using AP PIN"""
  654. ssid = "test-wps-init-ap-pin"
  655. appin = "12345670"
  656. hostapd.add_ap(apdev[0],
  657. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  658. "ap_pin": appin})
  659. logger.info("WPS configuration step")
  660. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  661. dev[0].dump_monitor()
  662. new_ssid = "wps-new-ssid"
  663. new_passphrase = "1234567890"
  664. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
  665. new_passphrase)
  666. status = dev[0].get_status()
  667. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  668. raise Exception("Not fully connected")
  669. if status['ssid'] != new_ssid:
  670. raise Exception("Unexpected SSID")
  671. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  672. raise Exception("Unexpected encryption configuration")
  673. if status['key_mgmt'] != 'WPA2-PSK':
  674. raise Exception("Unexpected key_mgmt")
  675. logger.info("Re-configure back to open")
  676. dev[0].request("REMOVE_NETWORK all")
  677. dev[0].flush_scan_cache()
  678. dev[0].dump_monitor()
  679. dev[0].wps_reg(apdev[0]['bssid'], appin, "wps-open", "OPEN", "NONE", "")
  680. status = dev[0].get_status()
  681. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  682. raise Exception("Not fully connected")
  683. if status['ssid'] != "wps-open":
  684. raise Exception("Unexpected SSID")
  685. if status['key_mgmt'] != 'NONE':
  686. raise Exception("Unexpected key_mgmt")
  687. def test_ap_wps_reg_config_ext_processing(dev, apdev):
  688. """WPS registrar configuring an AP with external config processing"""
  689. ssid = "test-wps-init-ap-pin"
  690. appin = "12345670"
  691. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  692. "wps_cred_processing": "1", "ap_pin": appin}
  693. hapd = hostapd.add_ap(apdev[0], params)
  694. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  695. new_ssid = "wps-new-ssid"
  696. new_passphrase = "1234567890"
  697. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
  698. new_passphrase, no_wait=True)
  699. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  700. if ev is None:
  701. raise Exception("WPS registrar operation timed out")
  702. ev = hapd.wait_event(["WPS-NEW-AP-SETTINGS"], timeout=15)
  703. if ev is None:
  704. raise Exception("WPS configuration timed out")
  705. if "1026" not in ev:
  706. raise Exception("AP Settings missing from event")
  707. hapd.request("SET wps_cred_processing 0")
  708. if "FAIL" in hapd.request("WPS_CONFIG " + new_ssid.encode("hex") + " WPA2PSK CCMP " + new_passphrase.encode("hex")):
  709. raise Exception("WPS_CONFIG command failed")
  710. dev[0].wait_connected(timeout=15)
  711. def test_ap_wps_reg_config_tkip(dev, apdev):
  712. """WPS registrar configuring AP to use TKIP and AP upgrading to TKIP+CCMP"""
  713. skip_with_fips(dev[0])
  714. ssid = "test-wps-init-ap"
  715. appin = "12345670"
  716. hostapd.add_ap(apdev[0],
  717. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  718. "ap_pin": appin})
  719. logger.info("WPS configuration step")
  720. dev[0].request("SET wps_version_number 0x10")
  721. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  722. dev[0].dump_monitor()
  723. new_ssid = "wps-new-ssid-with-tkip"
  724. new_passphrase = "1234567890"
  725. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPAPSK", "TKIP",
  726. new_passphrase)
  727. logger.info("Re-connect to verify WPA2 mixed mode")
  728. dev[0].request("DISCONNECT")
  729. id = 0
  730. dev[0].set_network(id, "pairwise", "CCMP")
  731. dev[0].set_network(id, "proto", "RSN")
  732. dev[0].connect_network(id)
  733. status = dev[0].get_status()
  734. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  735. raise Exception("Not fully connected: wpa_state={} bssid={}".format(status['wpa_state'], status['bssid']))
  736. if status['ssid'] != new_ssid:
  737. raise Exception("Unexpected SSID")
  738. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  739. raise Exception("Unexpected encryption configuration")
  740. if status['key_mgmt'] != 'WPA2-PSK':
  741. raise Exception("Unexpected key_mgmt")
  742. def test_ap_wps_setup_locked(dev, apdev):
  743. """WPS registrar locking up AP setup on AP PIN failures"""
  744. ssid = "test-wps-incorrect-ap-pin"
  745. appin = "12345670"
  746. hapd = hostapd.add_ap(apdev[0],
  747. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  748. "wpa_passphrase": "12345678", "wpa": "2",
  749. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  750. "ap_pin": appin})
  751. new_ssid = "wps-new-ssid-test"
  752. new_passphrase = "1234567890"
  753. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  754. ap_setup_locked=False
  755. for pin in ["55554444", "1234", "12345678", "00000000", "11111111"]:
  756. dev[0].dump_monitor()
  757. logger.info("Try incorrect AP PIN - attempt " + pin)
  758. dev[0].wps_reg(apdev[0]['bssid'], pin, new_ssid, "WPA2PSK",
  759. "CCMP", new_passphrase, no_wait=True)
  760. ev = dev[0].wait_event(["WPS-FAIL", "CTRL-EVENT-CONNECTED"])
  761. if ev is None:
  762. raise Exception("Timeout on receiving WPS operation failure event")
  763. if "CTRL-EVENT-CONNECTED" in ev:
  764. raise Exception("Unexpected connection")
  765. if "config_error=15" in ev:
  766. logger.info("AP Setup Locked")
  767. ap_setup_locked=True
  768. elif "config_error=18" not in ev:
  769. raise Exception("config_error=18 not reported")
  770. dev[0].wait_disconnected(timeout=10)
  771. time.sleep(0.1)
  772. if not ap_setup_locked:
  773. raise Exception("AP setup was not locked")
  774. dev[0].request("WPS_CANCEL")
  775. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412, force_scan=True,
  776. only_new=True)
  777. bss = dev[0].get_bss(apdev[0]['bssid'])
  778. if 'wps_ap_setup_locked' not in bss or bss['wps_ap_setup_locked'] != '1':
  779. logger.info("BSS: " + str(bss))
  780. raise Exception("AP Setup Locked not indicated in scan results")
  781. status = hapd.request("WPS_GET_STATUS")
  782. if "Last WPS result: Failed" not in status:
  783. raise Exception("WPS failure result not shown correctly")
  784. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  785. raise Exception("Peer address not shown correctly")
  786. time.sleep(0.5)
  787. dev[0].dump_monitor()
  788. logger.info("WPS provisioning step")
  789. pin = dev[0].wps_read_pin()
  790. hapd.request("WPS_PIN any " + pin)
  791. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  792. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=30)
  793. if ev is None:
  794. raise Exception("WPS success was not reported")
  795. dev[0].wait_connected(timeout=30)
  796. appin = hapd.request("WPS_AP_PIN random")
  797. if "FAIL" in appin:
  798. raise Exception("Could not generate random AP PIN")
  799. ev = hapd.wait_event(["WPS-AP-SETUP-UNLOCKED"], timeout=10)
  800. if ev is None:
  801. raise Exception("Failed to unlock AP PIN")
  802. def test_ap_wps_setup_locked_timeout(dev, apdev):
  803. """WPS re-enabling AP PIN after timeout"""
  804. ssid = "test-wps-incorrect-ap-pin"
  805. appin = "12345670"
  806. hapd = hostapd.add_ap(apdev[0],
  807. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  808. "wpa_passphrase": "12345678", "wpa": "2",
  809. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  810. "ap_pin": appin})
  811. new_ssid = "wps-new-ssid-test"
  812. new_passphrase = "1234567890"
  813. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  814. ap_setup_locked=False
  815. for pin in ["55554444", "1234", "12345678", "00000000", "11111111"]:
  816. dev[0].dump_monitor()
  817. logger.info("Try incorrect AP PIN - attempt " + pin)
  818. dev[0].wps_reg(apdev[0]['bssid'], pin, new_ssid, "WPA2PSK",
  819. "CCMP", new_passphrase, no_wait=True)
  820. ev = dev[0].wait_event(["WPS-FAIL", "CTRL-EVENT-CONNECTED"], timeout=15)
  821. if ev is None:
  822. raise Exception("Timeout on receiving WPS operation failure event")
  823. if "CTRL-EVENT-CONNECTED" in ev:
  824. raise Exception("Unexpected connection")
  825. if "config_error=15" in ev:
  826. logger.info("AP Setup Locked")
  827. ap_setup_locked=True
  828. break
  829. elif "config_error=18" not in ev:
  830. raise Exception("config_error=18 not reported")
  831. dev[0].wait_disconnected(timeout=10)
  832. time.sleep(0.1)
  833. if not ap_setup_locked:
  834. raise Exception("AP setup was not locked")
  835. ev = hapd.wait_event(["WPS-AP-SETUP-UNLOCKED"], timeout=80)
  836. if ev is None:
  837. raise Exception("AP PIN did not get unlocked on 60 second timeout")
  838. def test_ap_wps_setup_locked_2(dev, apdev):
  839. """WPS AP configured for special ap_setup_locked=2 mode"""
  840. ssid = "test-wps-ap-pin"
  841. appin = "12345670"
  842. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  843. "wpa_passphrase": "12345678", "wpa": "2",
  844. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  845. "ap_pin": appin, "ap_setup_locked": "2" }
  846. hapd = hostapd.add_ap(apdev[0], params)
  847. new_ssid = "wps-new-ssid-test"
  848. new_passphrase = "1234567890"
  849. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  850. dev[0].wps_reg(apdev[0]['bssid'], appin)
  851. dev[0].request("REMOVE_NETWORK all")
  852. dev[0].wait_disconnected()
  853. hapd.dump_monitor()
  854. dev[0].dump_monitor()
  855. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK",
  856. "CCMP", new_passphrase, no_wait=True)
  857. ev = hapd.wait_event(["WPS-FAIL"], timeout=5)
  858. if ev is None:
  859. raise Exception("hostapd did not report WPS failure")
  860. if "msg=12 config_error=15" not in ev:
  861. raise Exception("Unexpected failure reason (AP): " + ev)
  862. ev = dev[0].wait_event(["WPS-FAIL", "CTRL-EVENT-CONNECTED"])
  863. if ev is None:
  864. raise Exception("Timeout on receiving WPS operation failure event")
  865. if "CTRL-EVENT-CONNECTED" in ev:
  866. raise Exception("Unexpected connection")
  867. if "config_error=15" not in ev:
  868. raise Exception("Unexpected failure reason (STA): " + ev)
  869. dev[0].request("WPS_CANCEL")
  870. dev[0].wait_disconnected()
  871. @remote_compatible
  872. def test_ap_wps_pbc_overlap_2ap(dev, apdev):
  873. """WPS PBC session overlap with two active APs"""
  874. params = { "ssid": "wps1", "eap_server": "1", "wps_state": "2",
  875. "wpa_passphrase": "12345678", "wpa": "2",
  876. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  877. "wps_independent": "1"}
  878. hapd = hostapd.add_ap(apdev[0], params)
  879. params = { "ssid": "wps2", "eap_server": "1", "wps_state": "2",
  880. "wpa_passphrase": "123456789", "wpa": "2",
  881. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  882. "wps_independent": "1"}
  883. hapd2 = hostapd.add_ap(apdev[1], params)
  884. hapd.request("WPS_PBC")
  885. hapd2.request("WPS_PBC")
  886. logger.info("WPS provisioning step")
  887. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  888. dev[0].scan_for_bss(apdev[1]['bssid'], freq="2412")
  889. dev[0].request("WPS_PBC")
  890. ev = dev[0].wait_event(["WPS-OVERLAP-DETECTED"], timeout=15)
  891. if ev is None:
  892. raise Exception("PBC session overlap not detected")
  893. hapd.request("DISABLE")
  894. hapd2.request("DISABLE")
  895. dev[0].flush_scan_cache()
  896. @remote_compatible
  897. def test_ap_wps_pbc_overlap_2sta(dev, apdev):
  898. """WPS PBC session overlap with two active STAs"""
  899. ssid = "test-wps-pbc-overlap"
  900. hapd = hostapd.add_ap(apdev[0],
  901. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  902. "wpa_passphrase": "12345678", "wpa": "2",
  903. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  904. logger.info("WPS provisioning step")
  905. hapd.request("WPS_PBC")
  906. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  907. dev[0].dump_monitor()
  908. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
  909. dev[1].dump_monitor()
  910. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  911. dev[1].request("WPS_PBC " + apdev[0]['bssid'])
  912. ev = dev[0].wait_event(["WPS-M2D"], timeout=15)
  913. if ev is None:
  914. raise Exception("PBC session overlap not detected (dev0)")
  915. if "config_error=12" not in ev:
  916. raise Exception("PBC session overlap not correctly reported (dev0)")
  917. dev[0].request("WPS_CANCEL")
  918. dev[0].request("DISCONNECT")
  919. ev = dev[1].wait_event(["WPS-M2D"], timeout=15)
  920. if ev is None:
  921. raise Exception("PBC session overlap not detected (dev1)")
  922. if "config_error=12" not in ev:
  923. raise Exception("PBC session overlap not correctly reported (dev1)")
  924. dev[1].request("WPS_CANCEL")
  925. dev[1].request("DISCONNECT")
  926. hapd.request("WPS_CANCEL")
  927. ret = hapd.request("WPS_PBC")
  928. if "FAIL" not in ret:
  929. raise Exception("PBC mode allowed to be started while PBC overlap still active")
  930. hapd.request("DISABLE")
  931. dev[0].flush_scan_cache()
  932. dev[1].flush_scan_cache()
  933. @remote_compatible
  934. def test_ap_wps_cancel(dev, apdev):
  935. """WPS AP cancelling enabled config method"""
  936. ssid = "test-wps-ap-cancel"
  937. hapd = hostapd.add_ap(apdev[0],
  938. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  939. "wpa_passphrase": "12345678", "wpa": "2",
  940. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  941. bssid = apdev[0]['bssid']
  942. logger.info("Verify PBC enable/cancel")
  943. hapd.request("WPS_PBC")
  944. dev[0].scan(freq="2412")
  945. dev[0].scan(freq="2412")
  946. bss = dev[0].get_bss(apdev[0]['bssid'])
  947. if "[WPS-PBC]" not in bss['flags']:
  948. raise Exception("WPS-PBC flag missing")
  949. if "FAIL" in hapd.request("WPS_CANCEL"):
  950. raise Exception("WPS_CANCEL failed")
  951. dev[0].scan(freq="2412")
  952. dev[0].scan(freq="2412")
  953. bss = dev[0].get_bss(apdev[0]['bssid'])
  954. if "[WPS-PBC]" in bss['flags']:
  955. raise Exception("WPS-PBC flag not cleared")
  956. logger.info("Verify PIN enable/cancel")
  957. hapd.request("WPS_PIN any 12345670")
  958. dev[0].scan(freq="2412")
  959. dev[0].scan(freq="2412")
  960. bss = dev[0].get_bss(apdev[0]['bssid'])
  961. if "[WPS-AUTH]" not in bss['flags']:
  962. raise Exception("WPS-AUTH flag missing")
  963. if "FAIL" in hapd.request("WPS_CANCEL"):
  964. raise Exception("WPS_CANCEL failed")
  965. dev[0].scan(freq="2412")
  966. dev[0].scan(freq="2412")
  967. bss = dev[0].get_bss(apdev[0]['bssid'])
  968. if "[WPS-AUTH]" in bss['flags']:
  969. raise Exception("WPS-AUTH flag not cleared")
  970. def test_ap_wps_er_add_enrollee(dev, apdev):
  971. """WPS ER configuring AP and adding a new enrollee using PIN"""
  972. try:
  973. _test_ap_wps_er_add_enrollee(dev, apdev)
  974. finally:
  975. dev[0].request("WPS_ER_STOP")
  976. def _test_ap_wps_er_add_enrollee(dev, apdev):
  977. ssid = "wps-er-add-enrollee"
  978. ap_pin = "12345670"
  979. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  980. hostapd.add_ap(apdev[0],
  981. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  982. "device_name": "Wireless AP", "manufacturer": "Company",
  983. "model_name": "WAP", "model_number": "123",
  984. "serial_number": "12345", "device_type": "6-0050F204-1",
  985. "os_version": "01020300",
  986. 'friendly_name': "WPS AP - <>&'\" - TEST",
  987. "config_methods": "label push_button",
  988. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  989. logger.info("WPS configuration step")
  990. new_passphrase = "1234567890"
  991. dev[0].dump_monitor()
  992. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  993. dev[0].wps_reg(apdev[0]['bssid'], ap_pin, ssid, "WPA2PSK", "CCMP",
  994. new_passphrase)
  995. status = dev[0].get_status()
  996. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  997. raise Exception("Not fully connected")
  998. if status['ssid'] != ssid:
  999. raise Exception("Unexpected SSID")
  1000. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  1001. raise Exception("Unexpected encryption configuration")
  1002. if status['key_mgmt'] != 'WPA2-PSK':
  1003. raise Exception("Unexpected key_mgmt")
  1004. logger.info("Start ER")
  1005. dev[0].request("WPS_ER_START ifname=lo")
  1006. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1007. if ev is None:
  1008. raise Exception("AP discovery timed out")
  1009. if ap_uuid not in ev:
  1010. raise Exception("Expected AP UUID not found")
  1011. if "|WPS AP - &lt;&gt;&amp;&apos;&quot; - TEST|Company|" not in ev:
  1012. raise Exception("Expected friendly name not found")
  1013. logger.info("Learn AP configuration through UPnP")
  1014. dev[0].dump_monitor()
  1015. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1016. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  1017. if ev is None:
  1018. raise Exception("AP learn timed out")
  1019. if ap_uuid not in ev:
  1020. raise Exception("Expected AP UUID not in settings")
  1021. if "ssid=" + ssid not in ev:
  1022. raise Exception("Expected SSID not in settings")
  1023. if "key=" + new_passphrase not in ev:
  1024. raise Exception("Expected passphrase not in settings")
  1025. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1026. if ev is None:
  1027. raise Exception("WPS-FAIL after AP learn timed out")
  1028. time.sleep(0.1)
  1029. logger.info("Add Enrollee using ER")
  1030. pin = dev[1].wps_read_pin()
  1031. dev[0].dump_monitor()
  1032. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  1033. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1034. dev[1].dump_monitor()
  1035. dev[1].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  1036. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=30)
  1037. if ev is None:
  1038. raise Exception("Enrollee did not report success")
  1039. dev[1].wait_connected(timeout=15)
  1040. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  1041. if ev is None:
  1042. raise Exception("WPS ER did not report success")
  1043. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  1044. logger.info("Add a specific Enrollee using ER")
  1045. pin = dev[2].wps_read_pin()
  1046. addr2 = dev[2].p2p_interface_addr()
  1047. dev[0].dump_monitor()
  1048. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1049. dev[2].dump_monitor()
  1050. dev[2].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  1051. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=10)
  1052. if ev is None:
  1053. raise Exception("Enrollee not seen")
  1054. if addr2 not in ev:
  1055. raise Exception("Unexpected Enrollee MAC address")
  1056. dev[0].request("WPS_ER_PIN " + addr2 + " " + pin + " " + addr2)
  1057. dev[2].wait_connected(timeout=30)
  1058. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  1059. if ev is None:
  1060. raise Exception("WPS ER did not report success")
  1061. logger.info("Verify registrar selection behavior")
  1062. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  1063. dev[1].request("DISCONNECT")
  1064. dev[1].wait_disconnected(timeout=10)
  1065. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
  1066. dev[1].scan(freq="2412")
  1067. bss = dev[1].get_bss(apdev[0]['bssid'])
  1068. if "[WPS-AUTH]" not in bss['flags']:
  1069. # It is possible for scan to miss an update especially when running
  1070. # tests under load with multiple VMs, so allow another attempt.
  1071. dev[1].scan(freq="2412")
  1072. bss = dev[1].get_bss(apdev[0]['bssid'])
  1073. if "[WPS-AUTH]" not in bss['flags']:
  1074. raise Exception("WPS-AUTH flag missing")
  1075. logger.info("Stop ER")
  1076. dev[0].dump_monitor()
  1077. dev[0].request("WPS_ER_STOP")
  1078. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"])
  1079. if ev is None:
  1080. raise Exception("WPS ER unsubscription timed out")
  1081. # It takes some time for the UPnP UNSUBSCRIBE command to go through, so wait
  1082. # a bit before verifying that the scan results have changed.
  1083. time.sleep(0.2)
  1084. for i in range(0, 10):
  1085. dev[1].request("BSS_FLUSH 0")
  1086. dev[1].scan(freq="2412", only_new=True)
  1087. bss = dev[1].get_bss(apdev[0]['bssid'])
  1088. if bss and 'flags' in bss and "[WPS-AUTH]" not in bss['flags']:
  1089. break
  1090. logger.debug("WPS-AUTH flag was still in place - wait a bit longer")
  1091. time.sleep(0.1)
  1092. if "[WPS-AUTH]" in bss['flags']:
  1093. raise Exception("WPS-AUTH flag not removed")
  1094. def test_ap_wps_er_add_enrollee_uuid(dev, apdev):
  1095. """WPS ER adding a new enrollee identified by UUID"""
  1096. try:
  1097. _test_ap_wps_er_add_enrollee_uuid(dev, apdev)
  1098. finally:
  1099. dev[0].request("WPS_ER_STOP")
  1100. def _test_ap_wps_er_add_enrollee_uuid(dev, apdev):
  1101. ssid = "wps-er-add-enrollee"
  1102. ap_pin = "12345670"
  1103. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1104. hostapd.add_ap(apdev[0],
  1105. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1106. "wpa_passphrase": "12345678", "wpa": "2",
  1107. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1108. "device_name": "Wireless AP", "manufacturer": "Company",
  1109. "model_name": "WAP", "model_number": "123",
  1110. "serial_number": "12345", "device_type": "6-0050F204-1",
  1111. "os_version": "01020300",
  1112. "config_methods": "label push_button",
  1113. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  1114. logger.info("WPS configuration step")
  1115. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1116. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1117. logger.info("Start ER")
  1118. dev[0].request("WPS_ER_START ifname=lo")
  1119. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1120. if ev is None:
  1121. raise Exception("AP discovery timed out")
  1122. if ap_uuid not in ev:
  1123. raise Exception("Expected AP UUID not found")
  1124. logger.info("Learn AP configuration through UPnP")
  1125. dev[0].dump_monitor()
  1126. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1127. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  1128. if ev is None:
  1129. raise Exception("AP learn timed out")
  1130. if ap_uuid not in ev:
  1131. raise Exception("Expected AP UUID not in settings")
  1132. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1133. if ev is None:
  1134. raise Exception("WPS-FAIL after AP learn timed out")
  1135. time.sleep(0.1)
  1136. logger.info("Add a specific Enrollee using ER (PBC/UUID)")
  1137. addr1 = dev[1].p2p_interface_addr()
  1138. dev[0].dump_monitor()
  1139. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1140. dev[1].dump_monitor()
  1141. dev[1].request("WPS_PBC %s" % apdev[0]['bssid'])
  1142. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=10)
  1143. if ev is None:
  1144. raise Exception("Enrollee not seen")
  1145. if addr1 not in ev:
  1146. raise Exception("Unexpected Enrollee MAC address")
  1147. uuid = ev.split(' ')[1]
  1148. dev[0].request("WPS_ER_PBC " + uuid)
  1149. dev[1].wait_connected(timeout=30)
  1150. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  1151. if ev is None:
  1152. raise Exception("WPS ER did not report success")
  1153. logger.info("Add a specific Enrollee using ER (PIN/UUID)")
  1154. pin = dev[2].wps_read_pin()
  1155. addr2 = dev[2].p2p_interface_addr()
  1156. dev[0].dump_monitor()
  1157. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1158. dev[2].dump_monitor()
  1159. dev[2].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  1160. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=10)
  1161. if ev is None:
  1162. raise Exception("Enrollee not seen")
  1163. if addr2 not in ev:
  1164. raise Exception("Unexpected Enrollee MAC address")
  1165. uuid = ev.split(' ')[1]
  1166. dev[0].request("WPS_ER_PIN " + uuid + " " + pin)
  1167. dev[2].wait_connected(timeout=30)
  1168. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  1169. if ev is None:
  1170. raise Exception("WPS ER did not report success")
  1171. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-REMOVE"], timeout=15)
  1172. if ev is None:
  1173. raise Exception("No Enrollee STA entry timeout seen")
  1174. logger.info("Stop ER")
  1175. dev[0].dump_monitor()
  1176. dev[0].request("WPS_ER_STOP")
  1177. def test_ap_wps_er_multi_add_enrollee(dev, apdev):
  1178. """Multiple WPS ERs adding a new enrollee using PIN"""
  1179. try:
  1180. _test_ap_wps_er_multi_add_enrollee(dev, apdev)
  1181. finally:
  1182. for i in range(2):
  1183. dev[i].request("WPS_ER_STOP")
  1184. def _test_ap_wps_er_multi_add_enrollee(dev, apdev):
  1185. ssid = "wps-er-add-enrollee"
  1186. ap_pin = "12345670"
  1187. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1188. hostapd.add_ap(apdev[0],
  1189. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1190. "wpa_passphrase": "12345678", "wpa": "2",
  1191. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1192. "device_name": "Wireless AP", "manufacturer": "Company",
  1193. "model_name": "WAP", "model_number": "123",
  1194. "serial_number": "12345", "device_type": "6-0050F204-1",
  1195. "os_version": "01020300",
  1196. 'friendly_name': "WPS AP",
  1197. "config_methods": "label push_button",
  1198. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  1199. for i in range(2):
  1200. dev[i].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1201. dev[i].wps_reg(apdev[0]['bssid'], ap_pin)
  1202. for i in range(2):
  1203. dev[i].request("WPS_ER_START ifname=lo")
  1204. for i in range(2):
  1205. ev = dev[i].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1206. if ev is None:
  1207. raise Exception("AP discovery timed out")
  1208. dev[i].dump_monitor()
  1209. for i in range(2):
  1210. dev[i].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1211. for i in range(2):
  1212. ev = dev[i].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  1213. if ev is None:
  1214. raise Exception("AP learn timed out")
  1215. ev = dev[i].wait_event(["WPS-FAIL"], timeout=15)
  1216. if ev is None:
  1217. raise Exception("WPS-FAIL after AP learn timed out")
  1218. time.sleep(0.1)
  1219. pin = dev[2].wps_read_pin()
  1220. addr = dev[2].own_addr()
  1221. dev[0].dump_monitor()
  1222. dev[0].request("WPS_ER_PIN any " + pin + " " + addr)
  1223. dev[1].dump_monitor()
  1224. dev[1].request("WPS_ER_PIN any " + pin + " " + addr)
  1225. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1226. dev[2].dump_monitor()
  1227. dev[2].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  1228. ev = dev[2].wait_event(["WPS-SUCCESS"], timeout=30)
  1229. if ev is None:
  1230. raise Exception("Enrollee did not report success")
  1231. dev[2].wait_connected(timeout=15)
  1232. def test_ap_wps_er_add_enrollee_pbc(dev, apdev):
  1233. """WPS ER connected to AP and adding a new enrollee using PBC"""
  1234. try:
  1235. _test_ap_wps_er_add_enrollee_pbc(dev, apdev)
  1236. finally:
  1237. dev[0].request("WPS_ER_STOP")
  1238. def _test_ap_wps_er_add_enrollee_pbc(dev, apdev):
  1239. ssid = "wps-er-add-enrollee-pbc"
  1240. ap_pin = "12345670"
  1241. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1242. hostapd.add_ap(apdev[0],
  1243. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1244. "wpa_passphrase": "12345678", "wpa": "2",
  1245. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1246. "device_name": "Wireless AP", "manufacturer": "Company",
  1247. "model_name": "WAP", "model_number": "123",
  1248. "serial_number": "12345", "device_type": "6-0050F204-1",
  1249. "os_version": "01020300",
  1250. "config_methods": "label push_button",
  1251. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  1252. logger.info("Learn AP configuration")
  1253. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1254. dev[0].dump_monitor()
  1255. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1256. status = dev[0].get_status()
  1257. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  1258. raise Exception("Not fully connected")
  1259. logger.info("Start ER")
  1260. dev[0].request("WPS_ER_START ifname=lo")
  1261. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1262. if ev is None:
  1263. raise Exception("AP discovery timed out")
  1264. if ap_uuid not in ev:
  1265. raise Exception("Expected AP UUID not found")
  1266. enrollee = dev[1].p2p_interface_addr()
  1267. if "FAIL-UNKNOWN-UUID" not in dev[0].request("WPS_ER_PBC " + enrollee):
  1268. raise Exception("Unknown UUID not reported")
  1269. logger.info("Add Enrollee using ER and PBC")
  1270. dev[0].dump_monitor()
  1271. dev[1].dump_monitor()
  1272. dev[1].request("WPS_PBC")
  1273. for i in range(0, 2):
  1274. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=15)
  1275. if ev is None:
  1276. raise Exception("Enrollee discovery timed out")
  1277. if enrollee in ev:
  1278. break
  1279. if i == 1:
  1280. raise Exception("Expected Enrollee not found")
  1281. if "FAIL-NO-AP-SETTINGS" not in dev[0].request("WPS_ER_PBC " + enrollee):
  1282. raise Exception("Unknown UUID not reported")
  1283. logger.info("Use learned network configuration on ER")
  1284. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  1285. if "OK" not in dev[0].request("WPS_ER_PBC " + enrollee):
  1286. raise Exception("WPS_ER_PBC failed")
  1287. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=15)
  1288. if ev is None:
  1289. raise Exception("Enrollee did not report success")
  1290. dev[1].wait_connected(timeout=15)
  1291. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  1292. if ev is None:
  1293. raise Exception("WPS ER did not report success")
  1294. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  1295. def test_ap_wps_er_pbc_overlap(dev, apdev):
  1296. """WPS ER connected to AP and PBC session overlap"""
  1297. try:
  1298. _test_ap_wps_er_pbc_overlap(dev, apdev)
  1299. finally:
  1300. dev[0].request("WPS_ER_STOP")
  1301. def _test_ap_wps_er_pbc_overlap(dev, apdev):
  1302. ssid = "wps-er-add-enrollee-pbc"
  1303. ap_pin = "12345670"
  1304. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1305. hostapd.add_ap(apdev[0],
  1306. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1307. "wpa_passphrase": "12345678", "wpa": "2",
  1308. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1309. "device_name": "Wireless AP", "manufacturer": "Company",
  1310. "model_name": "WAP", "model_number": "123",
  1311. "serial_number": "12345", "device_type": "6-0050F204-1",
  1312. "os_version": "01020300",
  1313. "config_methods": "label push_button",
  1314. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  1315. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1316. dev[0].dump_monitor()
  1317. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1318. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
  1319. dev[2].scan_for_bss(apdev[0]['bssid'], freq="2412")
  1320. # avoid leaving dev 1 or 2 as the last Probe Request to the AP
  1321. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412, force_scan=True)
  1322. dev[0].dump_monitor()
  1323. dev[0].request("WPS_ER_START ifname=lo")
  1324. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1325. if ev is None:
  1326. raise Exception("AP discovery timed out")
  1327. if ap_uuid not in ev:
  1328. raise Exception("Expected AP UUID not found")
  1329. # verify BSSID selection of the AP instead of UUID
  1330. if "FAIL" in dev[0].request("WPS_ER_SET_CONFIG " + apdev[0]['bssid'] + " 0"):
  1331. raise Exception("Could not select AP based on BSSID")
  1332. dev[0].dump_monitor()
  1333. dev[1].request("WPS_PBC " + apdev[0]['bssid'])
  1334. dev[2].request("WPS_PBC " + apdev[0]['bssid'])
  1335. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
  1336. if ev is None:
  1337. raise Exception("PBC scan failed")
  1338. ev = dev[2].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
  1339. if ev is None:
  1340. raise Exception("PBC scan failed")
  1341. found1 = False
  1342. found2 = False
  1343. addr1 = dev[1].own_addr()
  1344. addr2 = dev[2].own_addr()
  1345. for i in range(3):
  1346. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=15)
  1347. if ev is None:
  1348. raise Exception("Enrollee discovery timed out")
  1349. if addr1 in ev:
  1350. found1 = True
  1351. if found2:
  1352. break
  1353. if addr2 in ev:
  1354. found2 = True
  1355. if found1:
  1356. break
  1357. if dev[0].request("WPS_ER_PBC " + ap_uuid) != "FAIL-PBC-OVERLAP\n":
  1358. raise Exception("PBC overlap not reported")
  1359. dev[1].request("WPS_CANCEL")
  1360. dev[2].request("WPS_CANCEL")
  1361. if dev[0].request("WPS_ER_PBC foo") != "FAIL\n":
  1362. raise Exception("Invalid WPS_ER_PBC accepted")
  1363. def test_ap_wps_er_v10_add_enrollee_pin(dev, apdev):
  1364. """WPS v1.0 ER connected to AP and adding a new enrollee using PIN"""
  1365. try:
  1366. _test_ap_wps_er_v10_add_enrollee_pin(dev, apdev)
  1367. finally:
  1368. dev[0].request("WPS_ER_STOP")
  1369. def _test_ap_wps_er_v10_add_enrollee_pin(dev, apdev):
  1370. ssid = "wps-er-add-enrollee-pbc"
  1371. ap_pin = "12345670"
  1372. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1373. hostapd.add_ap(apdev[0],
  1374. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1375. "wpa_passphrase": "12345678", "wpa": "2",
  1376. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1377. "device_name": "Wireless AP", "manufacturer": "Company",
  1378. "model_name": "WAP", "model_number": "123",
  1379. "serial_number": "12345", "device_type": "6-0050F204-1",
  1380. "os_version": "01020300",
  1381. "config_methods": "label push_button",
  1382. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  1383. logger.info("Learn AP configuration")
  1384. dev[0].request("SET wps_version_number 0x10")
  1385. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1386. dev[0].dump_monitor()
  1387. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1388. status = dev[0].get_status()
  1389. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  1390. raise Exception("Not fully connected")
  1391. logger.info("Start ER")
  1392. dev[0].request("WPS_ER_START ifname=lo")
  1393. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1394. if ev is None:
  1395. raise Exception("AP discovery timed out")
  1396. if ap_uuid not in ev:
  1397. raise Exception("Expected AP UUID not found")
  1398. logger.info("Use learned network configuration on ER")
  1399. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  1400. logger.info("Add Enrollee using ER and PIN")
  1401. enrollee = dev[1].p2p_interface_addr()
  1402. pin = dev[1].wps_read_pin()
  1403. dev[0].dump_monitor()
  1404. dev[0].request("WPS_ER_PIN any " + pin + " " + enrollee)
  1405. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1406. dev[1].dump_monitor()
  1407. dev[1].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  1408. dev[1].wait_connected(timeout=30)
  1409. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  1410. if ev is None:
  1411. raise Exception("WPS ER did not report success")
  1412. @remote_compatible
  1413. def test_ap_wps_er_config_ap(dev, apdev):
  1414. """WPS ER configuring AP over UPnP"""
  1415. try:
  1416. _test_ap_wps_er_config_ap(dev, apdev)
  1417. finally:
  1418. dev[0].request("WPS_ER_STOP")
  1419. def _test_ap_wps_er_config_ap(dev, apdev):
  1420. ssid = "wps-er-ap-config"
  1421. ap_pin = "12345670"
  1422. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1423. hostapd.add_ap(apdev[0],
  1424. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1425. "wpa_passphrase": "12345678", "wpa": "2",
  1426. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1427. "device_name": "Wireless AP", "manufacturer": "Company",
  1428. "model_name": "WAP", "model_number": "123",
  1429. "serial_number": "12345", "device_type": "6-0050F204-1",
  1430. "os_version": "01020300",
  1431. "config_methods": "label push_button",
  1432. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  1433. logger.info("Connect ER to the AP")
  1434. dev[0].connect(ssid, psk="12345678", scan_freq="2412")
  1435. logger.info("WPS configuration step")
  1436. dev[0].request("WPS_ER_START ifname=lo")
  1437. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1438. if ev is None:
  1439. raise Exception("AP discovery timed out")
  1440. if ap_uuid not in ev:
  1441. raise Exception("Expected AP UUID not found")
  1442. new_passphrase = "1234567890"
  1443. dev[0].request("WPS_ER_CONFIG " + apdev[0]['bssid'] + " " + ap_pin + " " +
  1444. ssid.encode("hex") + " WPA2PSK CCMP " +
  1445. new_passphrase.encode("hex"))
  1446. ev = dev[0].wait_event(["WPS-SUCCESS"])
  1447. if ev is None:
  1448. raise Exception("WPS ER configuration operation timed out")
  1449. dev[0].wait_disconnected(timeout=10)
  1450. dev[0].connect(ssid, psk="1234567890", scan_freq="2412")
  1451. logger.info("WPS ER restart")
  1452. dev[0].request("WPS_ER_START")
  1453. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1454. if ev is None:
  1455. raise Exception("AP discovery timed out on ER restart")
  1456. if ap_uuid not in ev:
  1457. raise Exception("Expected AP UUID not found on ER restart")
  1458. if "OK" not in dev[0].request("WPS_ER_STOP"):
  1459. raise Exception("WPS_ER_STOP failed")
  1460. if "OK" not in dev[0].request("WPS_ER_STOP"):
  1461. raise Exception("WPS_ER_STOP failed")
  1462. @remote_compatible
  1463. def test_ap_wps_er_cache_ap_settings(dev, apdev):
  1464. """WPS ER caching AP settings"""
  1465. try:
  1466. _test_ap_wps_er_cache_ap_settings(dev, apdev)
  1467. finally:
  1468. dev[0].request("WPS_ER_STOP")
  1469. def _test_ap_wps_er_cache_ap_settings(dev, apdev):
  1470. ssid = "wps-er-add-enrollee"
  1471. ap_pin = "12345670"
  1472. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1473. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1474. "wpa_passphrase": "12345678", "wpa": "2",
  1475. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1476. "device_name": "Wireless AP", "manufacturer": "Company",
  1477. "model_name": "WAP", "model_number": "123",
  1478. "serial_number": "12345", "device_type": "6-0050F204-1",
  1479. "os_version": "01020300",
  1480. "config_methods": "label push_button",
  1481. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo" }
  1482. hapd = hostapd.add_ap(apdev[0], params)
  1483. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1484. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1485. id = int(dev[0].list_networks()[0]['id'])
  1486. dev[0].set_network(id, "scan_freq", "2412")
  1487. dev[0].request("WPS_ER_START ifname=lo")
  1488. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1489. if ev is None:
  1490. raise Exception("AP discovery timed out")
  1491. if ap_uuid not in ev:
  1492. raise Exception("Expected AP UUID not found")
  1493. dev[0].dump_monitor()
  1494. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1495. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  1496. if ev is None:
  1497. raise Exception("AP learn timed out")
  1498. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1499. if ev is None:
  1500. raise Exception("WPS-FAIL after AP learn timed out")
  1501. time.sleep(0.1)
  1502. hapd.disable()
  1503. for i in range(2):
  1504. ev = dev[0].wait_event([ "WPS-ER-AP-REMOVE",
  1505. "CTRL-EVENT-DISCONNECTED" ],
  1506. timeout=15)
  1507. if ev is None:
  1508. raise Exception("AP removal or disconnection timed out")
  1509. hapd = hostapd.add_ap(apdev[0], params)
  1510. for i in range(2):
  1511. ev = dev[0].wait_event([ "WPS-ER-AP-ADD", "CTRL-EVENT-CONNECTED" ],
  1512. timeout=15)
  1513. if ev is None:
  1514. raise Exception("AP discovery or connection timed out")
  1515. pin = dev[1].wps_read_pin()
  1516. dev[0].dump_monitor()
  1517. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  1518. time.sleep(0.2)
  1519. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1520. dev[1].dump_monitor()
  1521. dev[1].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  1522. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=30)
  1523. if ev is None:
  1524. raise Exception("Enrollee did not report success")
  1525. dev[1].wait_connected(timeout=15)
  1526. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  1527. if ev is None:
  1528. raise Exception("WPS ER did not report success")
  1529. dev[0].dump_monitor()
  1530. dev[0].request("WPS_ER_STOP")
  1531. def test_ap_wps_er_cache_ap_settings_oom(dev, apdev):
  1532. """WPS ER caching AP settings (OOM)"""
  1533. try:
  1534. _test_ap_wps_er_cache_ap_settings_oom(dev, apdev)
  1535. finally:
  1536. dev[0].request("WPS_ER_STOP")
  1537. def _test_ap_wps_er_cache_ap_settings_oom(dev, apdev):
  1538. ssid = "wps-er-add-enrollee"
  1539. ap_pin = "12345670"
  1540. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1541. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1542. "wpa_passphrase": "12345678", "wpa": "2",
  1543. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1544. "device_name": "Wireless AP", "manufacturer": "Company",
  1545. "model_name": "WAP", "model_number": "123",
  1546. "serial_number": "12345", "device_type": "6-0050F204-1",
  1547. "os_version": "01020300",
  1548. "config_methods": "label push_button",
  1549. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo" }
  1550. hapd = hostapd.add_ap(apdev[0], params)
  1551. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1552. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1553. id = int(dev[0].list_networks()[0]['id'])
  1554. dev[0].set_network(id, "scan_freq", "2412")
  1555. dev[0].request("WPS_ER_START ifname=lo")
  1556. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1557. if ev is None:
  1558. raise Exception("AP discovery timed out")
  1559. if ap_uuid not in ev:
  1560. raise Exception("Expected AP UUID not found")
  1561. dev[0].dump_monitor()
  1562. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1563. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  1564. if ev is None:
  1565. raise Exception("AP learn timed out")
  1566. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1567. if ev is None:
  1568. raise Exception("WPS-FAIL after AP learn timed out")
  1569. time.sleep(0.1)
  1570. with alloc_fail(dev[0], 1, "=wps_er_ap_use_cached_settings"):
  1571. hapd.disable()
  1572. for i in range(2):
  1573. ev = dev[0].wait_event([ "WPS-ER-AP-REMOVE",
  1574. "CTRL-EVENT-DISCONNECTED" ],
  1575. timeout=15)
  1576. if ev is None:
  1577. raise Exception("AP removal or disconnection timed out")
  1578. hapd = hostapd.add_ap(apdev[0], params)
  1579. for i in range(2):
  1580. ev = dev[0].wait_event([ "WPS-ER-AP-ADD", "CTRL-EVENT-CONNECTED" ],
  1581. timeout=15)
  1582. if ev is None:
  1583. raise Exception("AP discovery or connection timed out")
  1584. dev[0].request("WPS_ER_STOP")
  1585. def test_ap_wps_er_cache_ap_settings_oom2(dev, apdev):
  1586. """WPS ER caching AP settings (OOM 2)"""
  1587. try:
  1588. _test_ap_wps_er_cache_ap_settings_oom2(dev, apdev)
  1589. finally:
  1590. dev[0].request("WPS_ER_STOP")
  1591. def _test_ap_wps_er_cache_ap_settings_oom2(dev, apdev):
  1592. ssid = "wps-er-add-enrollee"
  1593. ap_pin = "12345670"
  1594. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1595. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1596. "wpa_passphrase": "12345678", "wpa": "2",
  1597. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1598. "device_name": "Wireless AP", "manufacturer": "Company",
  1599. "model_name": "WAP", "model_number": "123",
  1600. "serial_number": "12345", "device_type": "6-0050F204-1",
  1601. "os_version": "01020300",
  1602. "config_methods": "label push_button",
  1603. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo" }
  1604. hapd = hostapd.add_ap(apdev[0], params)
  1605. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1606. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1607. id = int(dev[0].list_networks()[0]['id'])
  1608. dev[0].set_network(id, "scan_freq", "2412")
  1609. dev[0].request("WPS_ER_START ifname=lo")
  1610. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1611. if ev is None:
  1612. raise Exception("AP discovery timed out")
  1613. if ap_uuid not in ev:
  1614. raise Exception("Expected AP UUID not found")
  1615. dev[0].dump_monitor()
  1616. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1617. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  1618. if ev is None:
  1619. raise Exception("AP learn timed out")
  1620. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1621. if ev is None:
  1622. raise Exception("WPS-FAIL after AP learn timed out")
  1623. time.sleep(0.1)
  1624. with alloc_fail(dev[0], 1, "=wps_er_ap_cache_settings"):
  1625. hapd.disable()
  1626. for i in range(2):
  1627. ev = dev[0].wait_event([ "WPS-ER-AP-REMOVE",
  1628. "CTRL-EVENT-DISCONNECTED" ],
  1629. timeout=15)
  1630. if ev is None:
  1631. raise Exception("AP removal or disconnection timed out")
  1632. hapd = hostapd.add_ap(apdev[0], params)
  1633. for i in range(2):
  1634. ev = dev[0].wait_event([ "WPS-ER-AP-ADD", "CTRL-EVENT-CONNECTED" ],
  1635. timeout=15)
  1636. if ev is None:
  1637. raise Exception("AP discovery or connection timed out")
  1638. dev[0].request("WPS_ER_STOP")
  1639. def test_ap_wps_er_subscribe_oom(dev, apdev):
  1640. """WPS ER subscribe OOM"""
  1641. try:
  1642. _test_ap_wps_er_subscribe_oom(dev, apdev)
  1643. finally:
  1644. dev[0].request("WPS_ER_STOP")
  1645. def _test_ap_wps_er_subscribe_oom(dev, apdev):
  1646. ssid = "wps-er-add-enrollee"
  1647. ap_pin = "12345670"
  1648. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1649. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1650. "wpa_passphrase": "12345678", "wpa": "2",
  1651. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1652. "device_name": "Wireless AP", "manufacturer": "Company",
  1653. "model_name": "WAP", "model_number": "123",
  1654. "serial_number": "12345", "device_type": "6-0050F204-1",
  1655. "os_version": "01020300",
  1656. "config_methods": "label push_button",
  1657. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo" }
  1658. hapd = hostapd.add_ap(apdev[0], params)
  1659. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1660. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1661. id = int(dev[0].list_networks()[0]['id'])
  1662. dev[0].set_network(id, "scan_freq", "2412")
  1663. with alloc_fail(dev[0], 1, "http_client_addr;wps_er_subscribe"):
  1664. dev[0].request("WPS_ER_START ifname=lo")
  1665. for i in range(50):
  1666. res = dev[0].request("GET_ALLOC_FAIL")
  1667. if res.startswith("0:"):
  1668. break
  1669. time.sleep(0.1)
  1670. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=0)
  1671. if ev:
  1672. raise Exception("Unexpected AP discovery during OOM")
  1673. dev[0].request("WPS_ER_STOP")
  1674. def test_ap_wps_er_set_sel_reg_oom(dev, apdev):
  1675. """WPS ER SetSelectedRegistrar OOM"""
  1676. try:
  1677. _test_ap_wps_er_set_sel_reg_oom(dev, apdev)
  1678. finally:
  1679. dev[0].request("WPS_ER_STOP")
  1680. def _test_ap_wps_er_set_sel_reg_oom(dev, apdev):
  1681. ssid = "wps-er-add-enrollee"
  1682. ap_pin = "12345670"
  1683. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1684. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1685. "wpa_passphrase": "12345678", "wpa": "2",
  1686. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1687. "device_name": "Wireless AP", "manufacturer": "Company",
  1688. "model_name": "WAP", "model_number": "123",
  1689. "serial_number": "12345", "device_type": "6-0050F204-1",
  1690. "os_version": "01020300",
  1691. "config_methods": "label push_button",
  1692. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo" }
  1693. hapd = hostapd.add_ap(apdev[0], params)
  1694. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1695. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1696. dev[0].request("WPS_ER_START ifname=lo")
  1697. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=10)
  1698. if ev is None:
  1699. raise Exception("AP not discovered")
  1700. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1701. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  1702. if ev is None:
  1703. raise Exception("AP learn timed out")
  1704. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1705. if ev is None:
  1706. raise Exception("WPS-FAIL timed out")
  1707. time.sleep(0.1)
  1708. for func in [ "http_client_url_parse;wps_er_send_set_sel_reg",
  1709. "wps_er_soap_hdr;wps_er_send_set_sel_reg",
  1710. "http_client_addr;wps_er_send_set_sel_reg",
  1711. "wpabuf_alloc;wps_er_set_sel_reg" ]:
  1712. with alloc_fail(dev[0], 1, func):
  1713. if "OK" not in dev[0].request("WPS_ER_PBC " + ap_uuid):
  1714. raise Exception("WPS_ER_PBC failed")
  1715. ev = dev[0].wait_event(["WPS-PBC-ACTIVE"], timeout=3)
  1716. if ev is None:
  1717. raise Exception("WPS-PBC-ACTIVE not seen")
  1718. dev[0].request("WPS_ER_STOP")
  1719. @remote_compatible
  1720. def test_ap_wps_er_learn_oom(dev, apdev):
  1721. """WPS ER learn OOM"""
  1722. try:
  1723. _test_ap_wps_er_learn_oom(dev, apdev)
  1724. finally:
  1725. dev[0].request("WPS_ER_STOP")
  1726. def _test_ap_wps_er_learn_oom(dev, apdev):
  1727. ssid = "wps-er-add-enrollee"
  1728. ap_pin = "12345670"
  1729. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1730. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1731. "wpa_passphrase": "12345678", "wpa": "2",
  1732. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1733. "device_name": "Wireless AP", "manufacturer": "Company",
  1734. "model_name": "WAP", "model_number": "123",
  1735. "serial_number": "12345", "device_type": "6-0050F204-1",
  1736. "os_version": "01020300",
  1737. "config_methods": "label push_button",
  1738. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo" }
  1739. hapd = hostapd.add_ap(apdev[0], params)
  1740. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1741. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1742. dev[0].request("WPS_ER_START ifname=lo")
  1743. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=10)
  1744. if ev is None:
  1745. raise Exception("AP not discovered")
  1746. for func in [ "wps_er_http_put_message_cb",
  1747. "xml_get_base64_item;wps_er_http_put_message_cb",
  1748. "http_client_url_parse;wps_er_ap_put_message",
  1749. "wps_er_soap_hdr;wps_er_ap_put_message",
  1750. "http_client_addr;wps_er_ap_put_message" ]:
  1751. with alloc_fail(dev[0], 1, func):
  1752. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1753. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=1)
  1754. if ev is not None:
  1755. raise Exception("AP learn succeeded during OOM")
  1756. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1757. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=10)
  1758. if ev is None:
  1759. raise Exception("AP learn did not succeed")
  1760. if "FAIL" not in dev[0].request("WPS_ER_LEARN 00000000-9e5c-4e73-bd82-f89cbcd10d7e " + ap_pin):
  1761. raise Exception("WPS_ER_LEARN for unknown AP accepted")
  1762. dev[0].request("WPS_ER_STOP")
  1763. def test_ap_wps_fragmentation(dev, apdev):
  1764. """WPS with fragmentation in EAP-WSC and mixed mode WPA+WPA2"""
  1765. ssid = "test-wps-fragmentation"
  1766. appin = "12345670"
  1767. hapd = hostapd.add_ap(apdev[0],
  1768. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1769. "wpa_passphrase": "12345678", "wpa": "3",
  1770. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1771. "wpa_pairwise": "TKIP", "ap_pin": appin,
  1772. "fragment_size": "50" })
  1773. logger.info("WPS provisioning step (PBC)")
  1774. hapd.request("WPS_PBC")
  1775. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1776. dev[0].dump_monitor()
  1777. dev[0].request("SET wps_fragment_size 50")
  1778. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  1779. dev[0].wait_connected(timeout=30)
  1780. status = dev[0].get_status()
  1781. if status['wpa_state'] != 'COMPLETED':
  1782. raise Exception("Not fully connected")
  1783. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  1784. raise Exception("Unexpected encryption configuration")
  1785. if status['key_mgmt'] != 'WPA2-PSK':
  1786. raise Exception("Unexpected key_mgmt")
  1787. logger.info("WPS provisioning step (PIN)")
  1788. pin = dev[1].wps_read_pin()
  1789. hapd.request("WPS_PIN any " + pin)
  1790. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1791. dev[1].request("SET wps_fragment_size 50")
  1792. dev[1].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  1793. dev[1].wait_connected(timeout=30)
  1794. status = dev[1].get_status()
  1795. if status['wpa_state'] != 'COMPLETED':
  1796. raise Exception("Not fully connected")
  1797. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  1798. raise Exception("Unexpected encryption configuration")
  1799. if status['key_mgmt'] != 'WPA2-PSK':
  1800. raise Exception("Unexpected key_mgmt")
  1801. logger.info("WPS connection as registrar")
  1802. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1803. dev[2].request("SET wps_fragment_size 50")
  1804. dev[2].wps_reg(apdev[0]['bssid'], appin)
  1805. status = dev[2].get_status()
  1806. if status['wpa_state'] != 'COMPLETED':
  1807. raise Exception("Not fully connected")
  1808. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  1809. raise Exception("Unexpected encryption configuration")
  1810. if status['key_mgmt'] != 'WPA2-PSK':
  1811. raise Exception("Unexpected key_mgmt")
  1812. @remote_compatible
  1813. def test_ap_wps_new_version_sta(dev, apdev):
  1814. """WPS compatibility with new version number on the station"""
  1815. ssid = "test-wps-ver"
  1816. hapd = hostapd.add_ap(apdev[0],
  1817. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1818. "wpa_passphrase": "12345678", "wpa": "2",
  1819. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  1820. logger.info("WPS provisioning step")
  1821. hapd.request("WPS_PBC")
  1822. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  1823. dev[0].dump_monitor()
  1824. dev[0].request("SET wps_version_number 0x43")
  1825. dev[0].request("SET wps_vendor_ext_m1 000137100100020001")
  1826. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  1827. dev[0].wait_connected(timeout=30)
  1828. @remote_compatible
  1829. def test_ap_wps_new_version_ap(dev, apdev):
  1830. """WPS compatibility with new version number on the AP"""
  1831. ssid = "test-wps-ver"
  1832. hapd = hostapd.add_ap(apdev[0],
  1833. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1834. "wpa_passphrase": "12345678", "wpa": "2",
  1835. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  1836. logger.info("WPS provisioning step")
  1837. if "FAIL" in hapd.request("SET wps_version_number 0x43"):
  1838. raise Exception("Failed to enable test functionality")
  1839. hapd.request("WPS_PBC")
  1840. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  1841. dev[0].dump_monitor()
  1842. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  1843. dev[0].wait_connected(timeout=30)
  1844. hapd.request("SET wps_version_number 0x20")
  1845. @remote_compatible
  1846. def test_ap_wps_check_pin(dev, apdev):
  1847. """Verify PIN checking through control interface"""
  1848. hapd = hostapd.add_ap(apdev[0],
  1849. { "ssid": "wps", "eap_server": "1", "wps_state": "2",
  1850. "wpa_passphrase": "12345678", "wpa": "2",
  1851. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  1852. for t in [ ("12345670", "12345670"),
  1853. ("12345678", "FAIL-CHECKSUM"),
  1854. ("12345", "FAIL"),
  1855. ("123456789", "FAIL"),
  1856. ("1234-5670", "12345670"),
  1857. ("1234 5670", "12345670"),
  1858. ("1-2.3:4 5670", "12345670") ]:
  1859. res = hapd.request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
  1860. res2 = dev[0].request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
  1861. if res != res2:
  1862. raise Exception("Unexpected difference in WPS_CHECK_PIN responses")
  1863. if res != t[1]:
  1864. raise Exception("Incorrect WPS_CHECK_PIN response {} (expected {})".format(res, t[1]))
  1865. if "FAIL" not in hapd.request("WPS_CHECK_PIN 12345"):
  1866. raise Exception("Unexpected WPS_CHECK_PIN success")
  1867. if "FAIL" not in hapd.request("WPS_CHECK_PIN 123456789"):
  1868. raise Exception("Unexpected WPS_CHECK_PIN success")
  1869. for i in range(0, 10):
  1870. pin = dev[0].request("WPS_PIN get")
  1871. rpin = dev[0].request("WPS_CHECK_PIN " + pin).rstrip('\n')
  1872. if pin != rpin:
  1873. raise Exception("Random PIN validation failed for " + pin)
  1874. def test_ap_wps_pin_get_failure(dev, apdev):
  1875. """PIN generation failure"""
  1876. with fail_test(dev[0], 1,
  1877. "os_get_random;wpa_supplicant_ctrl_iface_wps_pin"):
  1878. if "FAIL" not in dev[0].request("WPS_PIN get"):
  1879. raise Exception("WPS_PIN did not report failure")
  1880. def test_ap_wps_wep_config(dev, apdev):
  1881. """WPS 2.0 AP rejecting WEP configuration"""
  1882. ssid = "test-wps-config"
  1883. appin = "12345670"
  1884. hapd = hostapd.add_ap(apdev[0],
  1885. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1886. "ap_pin": appin})
  1887. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1888. dev[0].wps_reg(apdev[0]['bssid'], appin, "wps-new-ssid-wep", "OPEN", "WEP",
  1889. "hello", no_wait=True)
  1890. ev = hapd.wait_event(["WPS-FAIL"], timeout=15)
  1891. if ev is None:
  1892. raise Exception("WPS-FAIL timed out")
  1893. if "reason=2" not in ev:
  1894. raise Exception("Unexpected reason code in WPS-FAIL")
  1895. status = hapd.request("WPS_GET_STATUS")
  1896. if "Last WPS result: Failed" not in status:
  1897. raise Exception("WPS failure result not shown correctly")
  1898. if "Failure Reason: WEP Prohibited" not in status:
  1899. raise Exception("Failure reason not reported correctly")
  1900. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  1901. raise Exception("Peer address not shown correctly")
  1902. def test_ap_wps_wep_enroll(dev, apdev):
  1903. """WPS 2.0 STA rejecting WEP configuration"""
  1904. ssid = "test-wps-wep"
  1905. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1906. "skip_cred_build": "1", "extra_cred": "wps-wep-cred" }
  1907. hapd = hostapd.add_ap(apdev[0], params)
  1908. hapd.request("WPS_PBC")
  1909. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1910. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  1911. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1912. if ev is None:
  1913. raise Exception("WPS-FAIL event timed out")
  1914. if "msg=12" not in ev or "reason=2 (WEP Prohibited)" not in ev:
  1915. raise Exception("Unexpected WPS-FAIL event: " + ev)
  1916. @remote_compatible
  1917. def test_ap_wps_ie_fragmentation(dev, apdev):
  1918. """WPS AP using fragmented WPS IE"""
  1919. ssid = "test-wps-ie-fragmentation"
  1920. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1921. "wpa_passphrase": "12345678", "wpa": "2",
  1922. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1923. "device_name": "1234567890abcdef1234567890abcdef",
  1924. "manufacturer": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
  1925. "model_name": "1234567890abcdef1234567890abcdef",
  1926. "model_number": "1234567890abcdef1234567890abcdef",
  1927. "serial_number": "1234567890abcdef1234567890abcdef" }
  1928. hapd = hostapd.add_ap(apdev[0], params)
  1929. hapd.request("WPS_PBC")
  1930. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  1931. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  1932. dev[0].wait_connected(timeout=30)
  1933. bss = dev[0].get_bss(apdev[0]['bssid'])
  1934. if "wps_device_name" not in bss or bss['wps_device_name'] != "1234567890abcdef1234567890abcdef":
  1935. logger.info("Device Name not received correctly")
  1936. logger.info(bss)
  1937. # This can fail if Probe Response frame is missed and Beacon frame was
  1938. # used to fill in the BSS entry. This can happen, e.g., during heavy
  1939. # load every now and then and is not really an error, so try to
  1940. # workaround by runnign another scan.
  1941. dev[0].scan(freq="2412", only_new=True)
  1942. bss = dev[0].get_bss(apdev[0]['bssid'])
  1943. if not bss or "wps_device_name" not in bss or bss['wps_device_name'] != "1234567890abcdef1234567890abcdef":
  1944. logger.info(bss)
  1945. raise Exception("Device Name not received correctly")
  1946. if len(re.findall("dd..0050f204", bss['ie'])) != 2:
  1947. raise Exception("Unexpected number of WPS IEs")
  1948. def get_psk(pskfile):
  1949. psks = {}
  1950. with open(pskfile, "r") as f:
  1951. lines = f.read().splitlines()
  1952. for l in lines:
  1953. if l == "# WPA PSKs":
  1954. continue
  1955. (addr,psk) = l.split(' ')
  1956. psks[addr] = psk
  1957. return psks
  1958. def test_ap_wps_per_station_psk(dev, apdev):
  1959. """WPS PBC provisioning with per-station PSK"""
  1960. addr0 = dev[0].own_addr()
  1961. addr1 = dev[1].own_addr()
  1962. addr2 = dev[2].own_addr()
  1963. ssid = "wps"
  1964. appin = "12345670"
  1965. pskfile = "/tmp/ap_wps_per_enrollee_psk.psk_file"
  1966. try:
  1967. os.remove(pskfile)
  1968. except:
  1969. pass
  1970. hapd = None
  1971. try:
  1972. with open(pskfile, "w") as f:
  1973. f.write("# WPA PSKs\n")
  1974. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1975. "wpa": "2", "wpa_key_mgmt": "WPA-PSK",
  1976. "rsn_pairwise": "CCMP", "ap_pin": appin,
  1977. "wpa_psk_file": pskfile }
  1978. hapd = hostapd.add_ap(apdev[0], params)
  1979. logger.info("First enrollee")
  1980. hapd.request("WPS_PBC")
  1981. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1982. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  1983. dev[0].wait_connected(timeout=30)
  1984. logger.info("Second enrollee")
  1985. hapd.request("WPS_PBC")
  1986. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1987. dev[1].request("WPS_PBC " + apdev[0]['bssid'])
  1988. dev[1].wait_connected(timeout=30)
  1989. logger.info("External registrar")
  1990. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1991. dev[2].wps_reg(apdev[0]['bssid'], appin)
  1992. logger.info("Verifying PSK results")
  1993. psks = get_psk(pskfile)
  1994. if addr0 not in psks:
  1995. raise Exception("No PSK recorded for sta0")
  1996. if addr1 not in psks:
  1997. raise Exception("No PSK recorded for sta1")
  1998. if addr2 not in psks:
  1999. raise Exception("No PSK recorded for sta2")
  2000. if psks[addr0] == psks[addr1]:
  2001. raise Exception("Same PSK recorded for sta0 and sta1")
  2002. if psks[addr0] == psks[addr2]:
  2003. raise Exception("Same PSK recorded for sta0 and sta2")
  2004. if psks[addr1] == psks[addr2]:
  2005. raise Exception("Same PSK recorded for sta1 and sta2")
  2006. dev[0].request("REMOVE_NETWORK all")
  2007. logger.info("Second external registrar")
  2008. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  2009. dev[0].wps_reg(apdev[0]['bssid'], appin)
  2010. psks2 = get_psk(pskfile)
  2011. if addr0 not in psks2:
  2012. raise Exception("No PSK recorded for sta0(reg)")
  2013. if psks[addr0] == psks2[addr0]:
  2014. raise Exception("Same PSK recorded for sta0(enrollee) and sta0(reg)")
  2015. finally:
  2016. os.remove(pskfile)
  2017. if hapd:
  2018. dev[0].request("DISCONNECT")
  2019. dev[1].request("DISCONNECT")
  2020. dev[2].request("DISCONNECT")
  2021. hapd.disable()
  2022. dev[0].flush_scan_cache()
  2023. dev[1].flush_scan_cache()
  2024. dev[2].flush_scan_cache()
  2025. def test_ap_wps_per_station_psk_failure(dev, apdev):
  2026. """WPS PBC provisioning with per-station PSK (file not writable)"""
  2027. addr0 = dev[0].p2p_dev_addr()
  2028. addr1 = dev[1].p2p_dev_addr()
  2029. addr2 = dev[2].p2p_dev_addr()
  2030. ssid = "wps"
  2031. appin = "12345670"
  2032. pskfile = "/tmp/ap_wps_per_enrollee_psk.psk_file"
  2033. try:
  2034. os.remove(pskfile)
  2035. except:
  2036. pass
  2037. hapd = None
  2038. try:
  2039. with open(pskfile, "w") as f:
  2040. f.write("# WPA PSKs\n")
  2041. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  2042. "wpa": "2", "wpa_key_mgmt": "WPA-PSK",
  2043. "rsn_pairwise": "CCMP", "ap_pin": appin,
  2044. "wpa_psk_file": pskfile }
  2045. hapd = hostapd.add_ap(apdev[0], params)
  2046. if "FAIL" in hapd.request("SET wpa_psk_file /tmp/does/not/exists/ap_wps_per_enrollee_psk_failure.psk_file"):
  2047. raise Exception("Failed to set wpa_psk_file")
  2048. logger.info("First enrollee")
  2049. hapd.request("WPS_PBC")
  2050. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  2051. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  2052. dev[0].wait_connected(timeout=30)
  2053. logger.info("Second enrollee")
  2054. hapd.request("WPS_PBC")
  2055. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  2056. dev[1].request("WPS_PBC " + apdev[0]['bssid'])
  2057. dev[1].wait_connected(timeout=30)
  2058. logger.info("External registrar")
  2059. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  2060. dev[2].wps_reg(apdev[0]['bssid'], appin)
  2061. logger.info("Verifying PSK results")
  2062. psks = get_psk(pskfile)
  2063. if len(psks) > 0:
  2064. raise Exception("PSK recorded unexpectedly")
  2065. finally:
  2066. if hapd:
  2067. for i in range(3):
  2068. dev[i].request("DISCONNECT")
  2069. hapd.disable()
  2070. for i in range(3):
  2071. dev[i].flush_scan_cache()
  2072. os.remove(pskfile)
  2073. def test_ap_wps_pin_request_file(dev, apdev):
  2074. """WPS PIN provisioning with configured AP"""
  2075. ssid = "wps"
  2076. pinfile = "/tmp/ap_wps_pin_request_file.log"
  2077. if os.path.exists(pinfile):
  2078. os.remove(pinfile)
  2079. hapd = hostapd.add_ap(apdev[0],
  2080. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  2081. "wps_pin_requests": pinfile,
  2082. "wpa_passphrase": "12345678", "wpa": "2",
  2083. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  2084. uuid = dev[0].get_status_field("uuid")
  2085. pin = dev[0].wps_read_pin()
  2086. try:
  2087. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  2088. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  2089. ev = hapd.wait_event(["WPS-PIN-NEEDED"], timeout=15)
  2090. if ev is None:
  2091. raise Exception("PIN needed event not shown")
  2092. if uuid not in ev:
  2093. raise Exception("UUID mismatch")
  2094. dev[0].request("WPS_CANCEL")
  2095. success = False
  2096. with open(pinfile, "r") as f:
  2097. lines = f.readlines()
  2098. for l in lines:
  2099. if uuid in l:
  2100. success = True
  2101. break
  2102. if not success:
  2103. raise Exception("PIN request entry not in the log file")
  2104. finally:
  2105. try:
  2106. os.remove(pinfile)
  2107. except:
  2108. pass
  2109. def test_ap_wps_auto_setup_with_config_file(dev, apdev):
  2110. """WPS auto-setup with configuration file"""
  2111. conffile = "/tmp/ap_wps_auto_setup_with_config_file.conf"
  2112. ifname = apdev[0]['ifname']
  2113. try:
  2114. with open(conffile, "w") as f:
  2115. f.write("driver=nl80211\n")
  2116. f.write("hw_mode=g\n")
  2117. f.write("channel=1\n")
  2118. f.write("ieee80211n=1\n")
  2119. f.write("interface=%s\n" % ifname)
  2120. f.write("ctrl_interface=/var/run/hostapd\n")
  2121. f.write("ssid=wps\n")
  2122. f.write("eap_server=1\n")
  2123. f.write("wps_state=1\n")
  2124. hapd = hostapd.add_bss(apdev[0], ifname, conffile)
  2125. hapd.request("WPS_PBC")
  2126. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  2127. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  2128. dev[0].wait_connected(timeout=30)
  2129. with open(conffile, "r") as f:
  2130. lines = f.read().splitlines()
  2131. vals = dict()
  2132. for l in lines:
  2133. try:
  2134. [name,value] = l.split('=', 1)
  2135. vals[name] = value
  2136. except ValueError, e:
  2137. if "# WPS configuration" in l:
  2138. pass
  2139. else:
  2140. raise Exception("Unexpected configuration line: " + l)
  2141. if vals['ieee80211n'] != '1' or vals['wps_state'] != '2' or "WPA-PSK" not in vals['wpa_key_mgmt']:
  2142. raise Exception("Incorrect configuration: " + str(vals))
  2143. finally:
  2144. try:
  2145. os.remove(conffile)
  2146. except:
  2147. pass
  2148. def test_ap_wps_pbc_timeout(dev, apdev, params):
  2149. """wpa_supplicant PBC walk time and WPS ER SelReg timeout [long]"""
  2150. if not params['long']:
  2151. raise HwsimSkip("Skip test case with long duration due to --long not specified")
  2152. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2153. hapd = add_ssdp_ap(apdev[0], ap_uuid)
  2154. location = ssdp_get_location(ap_uuid)
  2155. urls = upnp_get_urls(location)
  2156. eventurl = urlparse.urlparse(urls['event_sub_url'])
  2157. ctrlurl = urlparse.urlparse(urls['control_url'])
  2158. url = urlparse.urlparse(location)
  2159. conn = httplib.HTTPConnection(url.netloc)
  2160. class WPSERHTTPServer(SocketServer.StreamRequestHandler):
  2161. def handle(self):
  2162. data = self.rfile.readline().strip()
  2163. logger.debug(data)
  2164. self.wfile.write(gen_wps_event())
  2165. server = MyTCPServer(("127.0.0.1", 12345), WPSERHTTPServer)
  2166. server.timeout = 1
  2167. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2168. "NT": "upnp:event",
  2169. "timeout": "Second-1234" }
  2170. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2171. resp = conn.getresponse()
  2172. if resp.status != 200:
  2173. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2174. sid = resp.getheader("sid")
  2175. logger.debug("Subscription SID " + sid)
  2176. msg = '''<?xml version="1.0"?>
  2177. <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  2178. <s:Body>
  2179. <u:SetSelectedRegistrar xmlns:u="urn:schemas-wifialliance-org:service:WFAWLANConfig:1">
  2180. <NewMessage>EEoAARAQQQABARASAAIAABBTAAIxSBBJAA4ANyoAASABBv///////xBIABA2LbR7pTpRkYj7
  2181. VFi5hrLk
  2182. </NewMessage>
  2183. </u:SetSelectedRegistrar>
  2184. </s:Body>
  2185. </s:Envelope>'''
  2186. headers = { "Content-type": 'text/xml; charset="utf-8"' }
  2187. headers["SOAPAction"] = '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#%s"' % "SetSelectedRegistrar"
  2188. conn.request("POST", ctrlurl.path, msg, headers)
  2189. resp = conn.getresponse()
  2190. if resp.status != 200:
  2191. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2192. server.handle_request()
  2193. logger.info("Start WPS_PBC and wait for PBC walk time expiration")
  2194. if "OK" not in dev[0].request("WPS_PBC"):
  2195. raise Exception("WPS_PBC failed")
  2196. start = os.times()[4]
  2197. server.handle_request()
  2198. dev[1].request("BSS_FLUSH 0")
  2199. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True,
  2200. only_new=True)
  2201. bss = dev[1].get_bss(apdev[0]['bssid'])
  2202. logger.debug("BSS: " + str(bss))
  2203. if '[WPS-AUTH]' not in bss['flags']:
  2204. raise Exception("WPS not indicated authorized")
  2205. server.handle_request()
  2206. wps_timeout_seen = False
  2207. while True:
  2208. hapd.dump_monitor()
  2209. dev[1].dump_monitor()
  2210. if not wps_timeout_seen:
  2211. ev = dev[0].wait_event(["WPS-TIMEOUT"], timeout=0)
  2212. if ev is not None:
  2213. logger.info("PBC timeout seen")
  2214. wps_timeout_seen = True
  2215. else:
  2216. dev[0].dump_monitor()
  2217. now = os.times()[4]
  2218. if now - start > 130:
  2219. raise Exception("Selected registration information not removed")
  2220. dev[1].request("BSS_FLUSH 0")
  2221. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True,
  2222. only_new=True)
  2223. bss = dev[1].get_bss(apdev[0]['bssid'])
  2224. logger.debug("BSS: " + str(bss))
  2225. if '[WPS-AUTH]' not in bss['flags']:
  2226. break
  2227. server.handle_request()
  2228. server.server_close()
  2229. if wps_timeout_seen:
  2230. return
  2231. now = os.times()[4]
  2232. if now < start + 150:
  2233. dur = start + 150 - now
  2234. else:
  2235. dur = 1
  2236. logger.info("Continue waiting for PBC timeout (%d sec)" % dur)
  2237. ev = dev[0].wait_event(["WPS-TIMEOUT"], timeout=dur)
  2238. if ev is None:
  2239. raise Exception("WPS-TIMEOUT not reported")
  2240. def add_ssdp_ap(ap, ap_uuid):
  2241. ssid = "wps-ssdp"
  2242. ap_pin = "12345670"
  2243. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  2244. "wpa_passphrase": "12345678", "wpa": "2",
  2245. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  2246. "device_name": "Wireless AP", "manufacturer": "Company",
  2247. "model_name": "WAP", "model_number": "123",
  2248. "serial_number": "12345", "device_type": "6-0050F204-1",
  2249. "os_version": "01020300",
  2250. "config_methods": "label push_button",
  2251. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo",
  2252. "friendly_name": "WPS Access Point",
  2253. "manufacturer_url": "http://www.example.com/",
  2254. "model_description": "Wireless Access Point",
  2255. "model_url": "http://www.example.com/model/",
  2256. "upc": "123456789012" }
  2257. return hostapd.add_ap(ap, params)
  2258. def ssdp_send(msg, no_recv=False):
  2259. socket.setdefaulttimeout(1)
  2260. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  2261. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  2262. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  2263. sock.bind(("127.0.0.1", 0))
  2264. sock.sendto(msg, ("239.255.255.250", 1900))
  2265. if no_recv:
  2266. return None
  2267. return sock.recv(1000)
  2268. def ssdp_send_msearch(st, no_recv=False):
  2269. msg = '\r\n'.join([
  2270. 'M-SEARCH * HTTP/1.1',
  2271. 'HOST: 239.255.255.250:1900',
  2272. 'MX: 1',
  2273. 'MAN: "ssdp:discover"',
  2274. 'ST: ' + st,
  2275. '', ''])
  2276. return ssdp_send(msg, no_recv=no_recv)
  2277. def test_ap_wps_ssdp_msearch(dev, apdev):
  2278. """WPS AP and SSDP M-SEARCH messages"""
  2279. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2280. add_ssdp_ap(apdev[0], ap_uuid)
  2281. msg = '\r\n'.join([
  2282. 'M-SEARCH * HTTP/1.1',
  2283. 'Host: 239.255.255.250:1900',
  2284. 'Mx: 1',
  2285. 'Man: "ssdp:discover"',
  2286. 'St: urn:schemas-wifialliance-org:device:WFADevice:1',
  2287. '', ''])
  2288. ssdp_send(msg)
  2289. msg = '\r\n'.join([
  2290. 'M-SEARCH * HTTP/1.1',
  2291. 'host:\t239.255.255.250:1900\t\t\t\t \t\t',
  2292. 'mx: \t1\t\t ',
  2293. 'man: \t \t "ssdp:discover" ',
  2294. 'st: urn:schemas-wifialliance-org:device:WFADevice:1\t\t',
  2295. '', ''])
  2296. ssdp_send(msg)
  2297. ssdp_send_msearch("ssdp:all")
  2298. ssdp_send_msearch("upnp:rootdevice")
  2299. ssdp_send_msearch("uuid:" + ap_uuid)
  2300. ssdp_send_msearch("urn:schemas-wifialliance-org:service:WFAWLANConfig:1")
  2301. ssdp_send_msearch("urn:schemas-wifialliance-org:device:WFADevice:1")
  2302. msg = '\r\n'.join([
  2303. 'M-SEARCH * HTTP/1.1',
  2304. 'HOST:\t239.255.255.250:1900',
  2305. 'MAN: "ssdp:discover"',
  2306. 'MX: 130',
  2307. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2308. '', ''])
  2309. ssdp_send(msg, no_recv=True)
  2310. def test_ap_wps_ssdp_invalid_msearch(dev, apdev):
  2311. """WPS AP and invalid SSDP M-SEARCH messages"""
  2312. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2313. add_ssdp_ap(apdev[0], ap_uuid)
  2314. socket.setdefaulttimeout(1)
  2315. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  2316. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  2317. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  2318. sock.bind(("127.0.0.1", 0))
  2319. logger.debug("Missing MX")
  2320. msg = '\r\n'.join([
  2321. 'M-SEARCH * HTTP/1.1',
  2322. 'HOST: 239.255.255.250:1900',
  2323. 'MAN: "ssdp:discover"',
  2324. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2325. '', ''])
  2326. sock.sendto(msg, ("239.255.255.250", 1900))
  2327. logger.debug("Negative MX")
  2328. msg = '\r\n'.join([
  2329. 'M-SEARCH * HTTP/1.1',
  2330. 'HOST: 239.255.255.250:1900',
  2331. 'MX: -1',
  2332. 'MAN: "ssdp:discover"',
  2333. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2334. '', ''])
  2335. sock.sendto(msg, ("239.255.255.250", 1900))
  2336. logger.debug("Invalid MX")
  2337. msg = '\r\n'.join([
  2338. 'M-SEARCH * HTTP/1.1',
  2339. 'HOST: 239.255.255.250:1900',
  2340. 'MX; 1',
  2341. 'MAN: "ssdp:discover"',
  2342. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2343. '', ''])
  2344. sock.sendto(msg, ("239.255.255.250", 1900))
  2345. logger.debug("Missing MAN")
  2346. msg = '\r\n'.join([
  2347. 'M-SEARCH * HTTP/1.1',
  2348. 'HOST: 239.255.255.250:1900',
  2349. 'MX: 1',
  2350. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2351. '', ''])
  2352. sock.sendto(msg, ("239.255.255.250", 1900))
  2353. logger.debug("Invalid MAN")
  2354. msg = '\r\n'.join([
  2355. 'M-SEARCH * HTTP/1.1',
  2356. 'HOST: 239.255.255.250:1900',
  2357. 'MX: 1',
  2358. 'MAN: foo',
  2359. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2360. '', ''])
  2361. sock.sendto(msg, ("239.255.255.250", 1900))
  2362. msg = '\r\n'.join([
  2363. 'M-SEARCH * HTTP/1.1',
  2364. 'HOST: 239.255.255.250:1900',
  2365. 'MX: 1',
  2366. 'MAN; "ssdp:discover"',
  2367. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2368. '', ''])
  2369. sock.sendto(msg, ("239.255.255.250", 1900))
  2370. logger.debug("Missing HOST")
  2371. msg = '\r\n'.join([
  2372. 'M-SEARCH * HTTP/1.1',
  2373. 'MAN: "ssdp:discover"',
  2374. 'MX: 1',
  2375. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2376. '', ''])
  2377. sock.sendto(msg, ("239.255.255.250", 1900))
  2378. logger.debug("Missing ST")
  2379. msg = '\r\n'.join([
  2380. 'M-SEARCH * HTTP/1.1',
  2381. 'HOST: 239.255.255.250:1900',
  2382. 'MAN: "ssdp:discover"',
  2383. 'MX: 1',
  2384. '', ''])
  2385. sock.sendto(msg, ("239.255.255.250", 1900))
  2386. logger.debug("Mismatching ST")
  2387. msg = '\r\n'.join([
  2388. 'M-SEARCH * HTTP/1.1',
  2389. 'HOST: 239.255.255.250:1900',
  2390. 'MAN: "ssdp:discover"',
  2391. 'MX: 1',
  2392. 'ST: uuid:16d5f8a9-4ee4-4f5e-81f9-cc6e2f47f42d',
  2393. '', ''])
  2394. sock.sendto(msg, ("239.255.255.250", 1900))
  2395. msg = '\r\n'.join([
  2396. 'M-SEARCH * HTTP/1.1',
  2397. 'HOST: 239.255.255.250:1900',
  2398. 'MAN: "ssdp:discover"',
  2399. 'MX: 1',
  2400. 'ST: foo:bar',
  2401. '', ''])
  2402. sock.sendto(msg, ("239.255.255.250", 1900))
  2403. msg = '\r\n'.join([
  2404. 'M-SEARCH * HTTP/1.1',
  2405. 'HOST: 239.255.255.250:1900',
  2406. 'MAN: "ssdp:discover"',
  2407. 'MX: 1',
  2408. 'ST: foobar',
  2409. '', ''])
  2410. sock.sendto(msg, ("239.255.255.250", 1900))
  2411. logger.debug("Invalid ST")
  2412. msg = '\r\n'.join([
  2413. 'M-SEARCH * HTTP/1.1',
  2414. 'HOST: 239.255.255.250:1900',
  2415. 'MAN: "ssdp:discover"',
  2416. 'MX: 1',
  2417. 'ST; urn:schemas-wifialliance-org:device:WFADevice:1',
  2418. '', ''])
  2419. sock.sendto(msg, ("239.255.255.250", 1900))
  2420. logger.debug("Invalid M-SEARCH")
  2421. msg = '\r\n'.join([
  2422. 'M+SEARCH * HTTP/1.1',
  2423. 'HOST: 239.255.255.250:1900',
  2424. 'MAN: "ssdp:discover"',
  2425. 'MX: 1',
  2426. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2427. '', ''])
  2428. sock.sendto(msg, ("239.255.255.250", 1900))
  2429. msg = '\r\n'.join([
  2430. 'M-SEARCH-* HTTP/1.1',
  2431. 'HOST: 239.255.255.250:1900',
  2432. 'MAN: "ssdp:discover"',
  2433. 'MX: 1',
  2434. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2435. '', ''])
  2436. sock.sendto(msg, ("239.255.255.250", 1900))
  2437. logger.debug("Invalid message format")
  2438. sock.sendto("NOTIFY * HTTP/1.1", ("239.255.255.250", 1900))
  2439. msg = '\r'.join([
  2440. 'M-SEARCH * HTTP/1.1',
  2441. 'HOST: 239.255.255.250:1900',
  2442. 'MAN: "ssdp:discover"',
  2443. 'MX: 1',
  2444. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2445. '', ''])
  2446. sock.sendto(msg, ("239.255.255.250", 1900))
  2447. try:
  2448. r = sock.recv(1000)
  2449. raise Exception("Unexpected M-SEARCH response: " + r)
  2450. except socket.timeout:
  2451. pass
  2452. logger.debug("Valid M-SEARCH")
  2453. msg = '\r\n'.join([
  2454. 'M-SEARCH * HTTP/1.1',
  2455. 'HOST: 239.255.255.250:1900',
  2456. 'MAN: "ssdp:discover"',
  2457. 'MX: 1',
  2458. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2459. '', ''])
  2460. sock.sendto(msg, ("239.255.255.250", 1900))
  2461. try:
  2462. r = sock.recv(1000)
  2463. pass
  2464. except socket.timeout:
  2465. raise Exception("No SSDP response")
  2466. def test_ap_wps_ssdp_burst(dev, apdev):
  2467. """WPS AP and SSDP burst"""
  2468. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2469. add_ssdp_ap(apdev[0], ap_uuid)
  2470. msg = '\r\n'.join([
  2471. 'M-SEARCH * HTTP/1.1',
  2472. 'HOST: 239.255.255.250:1900',
  2473. 'MAN: "ssdp:discover"',
  2474. 'MX: 1',
  2475. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2476. '', ''])
  2477. socket.setdefaulttimeout(1)
  2478. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  2479. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  2480. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  2481. sock.bind(("127.0.0.1", 0))
  2482. for i in range(0, 25):
  2483. sock.sendto(msg, ("239.255.255.250", 1900))
  2484. resp = 0
  2485. while True:
  2486. try:
  2487. r = sock.recv(1000)
  2488. if not r.startswith("HTTP/1.1 200 OK\r\n"):
  2489. raise Exception("Unexpected message: " + r)
  2490. resp += 1
  2491. except socket.timeout:
  2492. break
  2493. if resp < 20:
  2494. raise Exception("Too few SSDP responses")
  2495. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  2496. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  2497. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  2498. sock.bind(("127.0.0.1", 0))
  2499. for i in range(0, 25):
  2500. sock.sendto(msg, ("239.255.255.250", 1900))
  2501. while True:
  2502. try:
  2503. r = sock.recv(1000)
  2504. if ap_uuid in r:
  2505. break
  2506. except socket.timeout:
  2507. raise Exception("No SSDP response")
  2508. def ssdp_get_location(uuid):
  2509. res = ssdp_send_msearch("uuid:" + uuid)
  2510. location = None
  2511. for l in res.splitlines():
  2512. if l.lower().startswith("location:"):
  2513. location = l.split(':', 1)[1].strip()
  2514. break
  2515. if location is None:
  2516. raise Exception("No UPnP location found")
  2517. return location
  2518. def upnp_get_urls(location):
  2519. conn = urllib.urlopen(location, proxies={})
  2520. tree = ET.parse(conn)
  2521. root = tree.getroot()
  2522. urn = '{urn:schemas-upnp-org:device-1-0}'
  2523. service = root.find("./" + urn + "device/" + urn + "serviceList/" + urn + "service")
  2524. res = {}
  2525. res['scpd_url'] = urlparse.urljoin(location, service.find(urn + 'SCPDURL').text)
  2526. res['control_url'] = urlparse.urljoin(location, service.find(urn + 'controlURL').text)
  2527. res['event_sub_url'] = urlparse.urljoin(location, service.find(urn + 'eventSubURL').text)
  2528. return res
  2529. def upnp_soap_action(conn, path, action, include_soap_action=True,
  2530. soap_action_override=None, newmsg=None, neweventtype=None,
  2531. neweventmac=None):
  2532. soapns = 'http://schemas.xmlsoap.org/soap/envelope/'
  2533. wpsns = 'urn:schemas-wifialliance-org:service:WFAWLANConfig:1'
  2534. ET.register_namespace('soapenv', soapns)
  2535. ET.register_namespace('wfa', wpsns)
  2536. attrib = {}
  2537. attrib['{%s}encodingStyle' % soapns] = 'http://schemas.xmlsoap.org/soap/encoding/'
  2538. root = ET.Element("{%s}Envelope" % soapns, attrib=attrib)
  2539. body = ET.SubElement(root, "{%s}Body" % soapns)
  2540. act = ET.SubElement(body, "{%s}%s" % (wpsns, action))
  2541. if newmsg:
  2542. msg = ET.SubElement(act, "NewMessage")
  2543. msg.text = base64.b64encode(newmsg)
  2544. if neweventtype:
  2545. msg = ET.SubElement(act, "NewWLANEventType")
  2546. msg.text = neweventtype
  2547. if neweventmac:
  2548. msg = ET.SubElement(act, "NewWLANEventMAC")
  2549. msg.text = neweventmac
  2550. tree = ET.ElementTree(root)
  2551. soap = StringIO.StringIO()
  2552. tree.write(soap, xml_declaration=True, encoding='utf-8')
  2553. headers = { "Content-type": 'text/xml; charset="utf-8"' }
  2554. if include_soap_action:
  2555. headers["SOAPAction"] = '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#%s"' % action
  2556. elif soap_action_override:
  2557. headers["SOAPAction"] = soap_action_override
  2558. conn.request("POST", path, soap.getvalue(), headers)
  2559. return conn.getresponse()
  2560. def test_ap_wps_upnp(dev, apdev):
  2561. """WPS AP and UPnP operations"""
  2562. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2563. add_ssdp_ap(apdev[0], ap_uuid)
  2564. location = ssdp_get_location(ap_uuid)
  2565. urls = upnp_get_urls(location)
  2566. conn = urllib.urlopen(urls['scpd_url'], proxies={})
  2567. scpd = conn.read()
  2568. conn = urllib.urlopen(urlparse.urljoin(location, "unknown.html"),
  2569. proxies={})
  2570. if conn.getcode() != 404:
  2571. raise Exception("Unexpected HTTP response to GET unknown URL")
  2572. url = urlparse.urlparse(location)
  2573. conn = httplib.HTTPConnection(url.netloc)
  2574. #conn.set_debuglevel(1)
  2575. headers = { "Content-type": 'text/xml; charset="utf-8"',
  2576. "SOAPAction": '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#GetDeviceInfo"' }
  2577. conn.request("POST", "hello", "\r\n\r\n", headers)
  2578. resp = conn.getresponse()
  2579. if resp.status != 404:
  2580. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2581. conn.request("UNKNOWN", "hello", "\r\n\r\n", headers)
  2582. resp = conn.getresponse()
  2583. if resp.status != 501:
  2584. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2585. headers = { "Content-type": 'text/xml; charset="utf-8"',
  2586. "SOAPAction": '"urn:some-unknown-action#GetDeviceInfo"' }
  2587. ctrlurl = urlparse.urlparse(urls['control_url'])
  2588. conn.request("POST", ctrlurl.path, "\r\n\r\n", headers)
  2589. resp = conn.getresponse()
  2590. if resp.status != 401:
  2591. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2592. logger.debug("GetDeviceInfo without SOAPAction header")
  2593. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo",
  2594. include_soap_action=False)
  2595. if resp.status != 401:
  2596. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2597. logger.debug("GetDeviceInfo with invalid SOAPAction header")
  2598. for act in [ "foo",
  2599. "urn:schemas-wifialliance-org:service:WFAWLANConfig:1#GetDeviceInfo",
  2600. '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1"',
  2601. '"urn:schemas-wifialliance-org:service:WFAWLANConfig:123#GetDevice']:
  2602. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo",
  2603. include_soap_action=False,
  2604. soap_action_override=act)
  2605. if resp.status != 401:
  2606. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2607. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  2608. if resp.status != 200:
  2609. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2610. dev = resp.read()
  2611. if "NewDeviceInfo" not in dev:
  2612. raise Exception("Unexpected GetDeviceInfo response")
  2613. logger.debug("PutMessage without required parameters")
  2614. resp = upnp_soap_action(conn, ctrlurl.path, "PutMessage")
  2615. if resp.status != 600:
  2616. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2617. logger.debug("PutWLANResponse without required parameters")
  2618. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse")
  2619. if resp.status != 600:
  2620. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2621. logger.debug("SetSelectedRegistrar from unregistered ER")
  2622. resp = upnp_soap_action(conn, ctrlurl.path, "SetSelectedRegistrar")
  2623. if resp.status != 501:
  2624. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2625. logger.debug("Unknown action")
  2626. resp = upnp_soap_action(conn, ctrlurl.path, "Unknown")
  2627. if resp.status != 401:
  2628. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2629. def test_ap_wps_upnp_subscribe(dev, apdev):
  2630. """WPS AP and UPnP event subscription"""
  2631. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2632. hapd = add_ssdp_ap(apdev[0], ap_uuid)
  2633. location = ssdp_get_location(ap_uuid)
  2634. urls = upnp_get_urls(location)
  2635. eventurl = urlparse.urlparse(urls['event_sub_url'])
  2636. url = urlparse.urlparse(location)
  2637. conn = httplib.HTTPConnection(url.netloc)
  2638. #conn.set_debuglevel(1)
  2639. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2640. "timeout": "Second-1234" }
  2641. conn.request("SUBSCRIBE", "hello", "\r\n\r\n", headers)
  2642. resp = conn.getresponse()
  2643. if resp.status != 412:
  2644. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2645. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2646. resp = conn.getresponse()
  2647. if resp.status != 412:
  2648. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2649. headers = { "NT": "upnp:event",
  2650. "timeout": "Second-1234" }
  2651. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2652. resp = conn.getresponse()
  2653. if resp.status != 412:
  2654. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2655. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2656. "NT": "upnp:foobar",
  2657. "timeout": "Second-1234" }
  2658. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2659. resp = conn.getresponse()
  2660. if resp.status != 400:
  2661. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2662. logger.debug("Valid subscription")
  2663. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2664. "NT": "upnp:event",
  2665. "timeout": "Second-1234" }
  2666. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2667. resp = conn.getresponse()
  2668. if resp.status != 200:
  2669. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2670. sid = resp.getheader("sid")
  2671. logger.debug("Subscription SID " + sid)
  2672. logger.debug("Invalid re-subscription")
  2673. headers = { "NT": "upnp:event",
  2674. "sid": "123456734567854",
  2675. "timeout": "Second-1234" }
  2676. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2677. resp = conn.getresponse()
  2678. if resp.status != 400:
  2679. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2680. logger.debug("Invalid re-subscription")
  2681. headers = { "NT": "upnp:event",
  2682. "sid": "uuid:123456734567854",
  2683. "timeout": "Second-1234" }
  2684. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2685. resp = conn.getresponse()
  2686. if resp.status != 400:
  2687. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2688. logger.debug("Invalid re-subscription")
  2689. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2690. "NT": "upnp:event",
  2691. "sid": sid,
  2692. "timeout": "Second-1234" }
  2693. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2694. resp = conn.getresponse()
  2695. if resp.status != 400:
  2696. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2697. logger.debug("SID mismatch in re-subscription")
  2698. headers = { "NT": "upnp:event",
  2699. "sid": "uuid:4c2bca79-1ff4-4e43-85d4-952a2b8a51fb",
  2700. "timeout": "Second-1234" }
  2701. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2702. resp = conn.getresponse()
  2703. if resp.status != 412:
  2704. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2705. logger.debug("Valid re-subscription")
  2706. headers = { "NT": "upnp:event",
  2707. "sid": sid,
  2708. "timeout": "Second-1234" }
  2709. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2710. resp = conn.getresponse()
  2711. if resp.status != 200:
  2712. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2713. sid2 = resp.getheader("sid")
  2714. logger.debug("Subscription SID " + sid2)
  2715. if sid != sid2:
  2716. raise Exception("Unexpected SID change")
  2717. logger.debug("Valid re-subscription")
  2718. headers = { "NT": "upnp:event",
  2719. "sid": "uuid: \t \t" + sid.split(':')[1],
  2720. "timeout": "Second-1234" }
  2721. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2722. resp = conn.getresponse()
  2723. if resp.status != 200:
  2724. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2725. logger.debug("Invalid unsubscription")
  2726. headers = { "sid": sid }
  2727. conn.request("UNSUBSCRIBE", "/hello", "\r\n\r\n", headers)
  2728. resp = conn.getresponse()
  2729. if resp.status != 412:
  2730. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2731. headers = { "foo": "bar" }
  2732. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2733. resp = conn.getresponse()
  2734. if resp.status != 412:
  2735. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2736. logger.debug("Valid unsubscription")
  2737. headers = { "sid": sid }
  2738. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2739. resp = conn.getresponse()
  2740. if resp.status != 200:
  2741. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2742. logger.debug("Unsubscription for not existing SID")
  2743. headers = { "sid": sid }
  2744. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2745. resp = conn.getresponse()
  2746. if resp.status != 412:
  2747. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2748. logger.debug("Invalid unsubscription")
  2749. headers = { "sid": " \t \tfoo" }
  2750. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2751. resp = conn.getresponse()
  2752. if resp.status != 400:
  2753. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2754. logger.debug("Invalid unsubscription")
  2755. headers = { "sid": "uuid:\t \tfoo" }
  2756. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2757. resp = conn.getresponse()
  2758. if resp.status != 400:
  2759. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2760. logger.debug("Invalid unsubscription")
  2761. headers = { "NT": "upnp:event",
  2762. "sid": sid }
  2763. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2764. resp = conn.getresponse()
  2765. if resp.status != 400:
  2766. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2767. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2768. "sid": sid }
  2769. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2770. resp = conn.getresponse()
  2771. if resp.status != 400:
  2772. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2773. logger.debug("Valid subscription with multiple callbacks")
  2774. headers = { "callback": '<http://127.0.0.1:12345/event> <http://127.0.0.1:12345/event>\t<http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event>',
  2775. "NT": "upnp:event",
  2776. "timeout": "Second-1234" }
  2777. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2778. resp = conn.getresponse()
  2779. if resp.status != 200:
  2780. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2781. sid = resp.getheader("sid")
  2782. logger.debug("Subscription SID " + sid)
  2783. # Force subscription to be deleted due to errors
  2784. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  2785. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  2786. with alloc_fail(hapd, 1, "event_build_message"):
  2787. for i in range(10):
  2788. dev[1].dump_monitor()
  2789. dev[2].dump_monitor()
  2790. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2791. dev[2].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2792. dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2793. dev[1].request("WPS_CANCEL")
  2794. dev[2].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2795. dev[2].request("WPS_CANCEL")
  2796. if i % 4 == 1:
  2797. time.sleep(1)
  2798. else:
  2799. time.sleep(0.1)
  2800. time.sleep(0.2)
  2801. headers = { "sid": sid }
  2802. conn.request("UNSUBSCRIBE", eventurl.path, "", headers)
  2803. resp = conn.getresponse()
  2804. if resp.status != 200 and resp.status != 412:
  2805. raise Exception("Unexpected HTTP response for UNSUBSCRIBE: %d" % resp.status)
  2806. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2807. "NT": "upnp:event",
  2808. "timeout": "Second-1234" }
  2809. with alloc_fail(hapd, 1, "http_client_addr;event_send_start"):
  2810. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2811. resp = conn.getresponse()
  2812. if resp.status != 200:
  2813. raise Exception("Unexpected HTTP response for SUBSCRIBE: %d" % resp.status)
  2814. sid = resp.getheader("sid")
  2815. logger.debug("Subscription SID " + sid)
  2816. headers = { "sid": sid }
  2817. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2818. resp = conn.getresponse()
  2819. if resp.status != 200:
  2820. raise Exception("Unexpected HTTP response for UNSUBSCRIBE: %d" % resp.status)
  2821. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2822. "NT": "upnp:event",
  2823. "timeout": "Second-1234" }
  2824. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2825. resp = conn.getresponse()
  2826. if resp.status != 200:
  2827. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2828. sid = resp.getheader("sid")
  2829. logger.debug("Subscription SID " + sid)
  2830. with alloc_fail(hapd, 1, "=event_add"):
  2831. for i in range(2):
  2832. dev[1].dump_monitor()
  2833. dev[2].dump_monitor()
  2834. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2835. dev[2].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2836. dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2837. dev[1].request("WPS_CANCEL")
  2838. dev[2].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2839. dev[2].request("WPS_CANCEL")
  2840. if i == 0:
  2841. time.sleep(1)
  2842. else:
  2843. time.sleep(0.1)
  2844. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2845. resp = conn.getresponse()
  2846. if resp.status != 200:
  2847. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2848. with alloc_fail(hapd, 1, "wpabuf_dup;event_add"):
  2849. dev[1].dump_monitor()
  2850. dev[2].dump_monitor()
  2851. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2852. dev[2].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2853. dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2854. dev[1].request("WPS_CANCEL")
  2855. dev[2].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2856. dev[2].request("WPS_CANCEL")
  2857. time.sleep(0.1)
  2858. with fail_test(hapd, 1, "os_get_random;uuid_make;subscription_start"):
  2859. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2860. resp = conn.getresponse()
  2861. if resp.status != 500:
  2862. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2863. with alloc_fail(hapd, 1, "=subscription_start"):
  2864. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2865. resp = conn.getresponse()
  2866. if resp.status != 500:
  2867. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2868. headers = { "callback": '',
  2869. "NT": "upnp:event",
  2870. "timeout": "Second-1234" }
  2871. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2872. resp = conn.getresponse()
  2873. if resp.status != 500:
  2874. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2875. headers = { "callback": ' <',
  2876. "NT": "upnp:event",
  2877. "timeout": "Second-1234" }
  2878. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2879. resp = conn.getresponse()
  2880. if resp.status != 500:
  2881. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2882. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2883. "NT": "upnp:event",
  2884. "timeout": "Second-1234" }
  2885. with alloc_fail(hapd, 1, "wpabuf_alloc;subscription_first_event"):
  2886. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2887. resp = conn.getresponse()
  2888. if resp.status != 500:
  2889. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2890. with alloc_fail(hapd, 1, "event_add;subscription_first_event"):
  2891. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2892. resp = conn.getresponse()
  2893. if resp.status != 500:
  2894. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2895. with alloc_fail(hapd, 1, "subscr_addr_add_url"):
  2896. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2897. resp = conn.getresponse()
  2898. if resp.status != 500:
  2899. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2900. with alloc_fail(hapd, 2, "subscr_addr_add_url"):
  2901. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2902. resp = conn.getresponse()
  2903. if resp.status != 500:
  2904. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2905. for i in range(6):
  2906. headers = { "callback": '<http://127.0.0.1:%d/event>' % (12345 + i),
  2907. "NT": "upnp:event",
  2908. "timeout": "Second-1234" }
  2909. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2910. resp = conn.getresponse()
  2911. if resp.status != 200:
  2912. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2913. with alloc_fail(hapd, 1, "=upnp_wps_device_send_wlan_event"):
  2914. dev[1].dump_monitor()
  2915. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2916. dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2917. dev[1].request("WPS_CANCEL")
  2918. time.sleep(0.1)
  2919. with alloc_fail(hapd, 1, "wpabuf_alloc;upnp_wps_device_send_event"):
  2920. dev[1].dump_monitor()
  2921. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2922. dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2923. dev[1].request("WPS_CANCEL")
  2924. time.sleep(0.1)
  2925. with alloc_fail(hapd, 1,
  2926. "base64_gen_encode;?base64_encode;upnp_wps_device_send_wlan_event"):
  2927. dev[1].dump_monitor()
  2928. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2929. dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2930. dev[1].request("WPS_CANCEL")
  2931. time.sleep(0.1)
  2932. hapd.disable()
  2933. with alloc_fail(hapd, 1, "get_netif_info"):
  2934. if "FAIL" not in hapd.request("ENABLE"):
  2935. raise Exception("ENABLE succeeded during OOM")
  2936. def test_ap_wps_upnp_subscribe_events(dev, apdev):
  2937. """WPS AP and UPnP event subscription and many events"""
  2938. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2939. hapd = add_ssdp_ap(apdev[0], ap_uuid)
  2940. location = ssdp_get_location(ap_uuid)
  2941. urls = upnp_get_urls(location)
  2942. eventurl = urlparse.urlparse(urls['event_sub_url'])
  2943. class WPSERHTTPServer(SocketServer.StreamRequestHandler):
  2944. def handle(self):
  2945. data = self.rfile.readline().strip()
  2946. logger.debug(data)
  2947. self.wfile.write(gen_wps_event())
  2948. server = MyTCPServer(("127.0.0.1", 12345), WPSERHTTPServer)
  2949. server.timeout = 1
  2950. url = urlparse.urlparse(location)
  2951. conn = httplib.HTTPConnection(url.netloc)
  2952. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2953. "NT": "upnp:event",
  2954. "timeout": "Second-1234" }
  2955. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2956. resp = conn.getresponse()
  2957. if resp.status != 200:
  2958. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2959. sid = resp.getheader("sid")
  2960. logger.debug("Subscription SID " + sid)
  2961. # Fetch the first event message
  2962. server.handle_request()
  2963. # Force subscription event queue to reach the maximum length by generating
  2964. # new proxied events without the ER fetching any of the pending events.
  2965. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  2966. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  2967. for i in range(16):
  2968. dev[1].dump_monitor()
  2969. dev[2].dump_monitor()
  2970. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2971. dev[2].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2972. dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2973. dev[1].request("WPS_CANCEL")
  2974. dev[2].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2975. dev[2].request("WPS_CANCEL")
  2976. if i % 4 == 1:
  2977. time.sleep(1)
  2978. else:
  2979. time.sleep(0.1)
  2980. hapd.request("WPS_PIN any 12345670")
  2981. dev[1].dump_monitor()
  2982. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2983. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=10)
  2984. if ev is None:
  2985. raise Exception("WPS success not reported")
  2986. # Close the WPS ER HTTP server without fetching all the pending events.
  2987. # This tests hostapd code path that clears subscription and the remaining
  2988. # event queue when the interface is deinitialized.
  2989. server.handle_request()
  2990. server.server_close()
  2991. dev[1].wait_connected()
  2992. def test_ap_wps_upnp_http_proto(dev, apdev):
  2993. """WPS AP and UPnP/HTTP protocol testing"""
  2994. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2995. add_ssdp_ap(apdev[0], ap_uuid)
  2996. location = ssdp_get_location(ap_uuid)
  2997. url = urlparse.urlparse(location)
  2998. conn = httplib.HTTPConnection(url.netloc, timeout=0.2)
  2999. #conn.set_debuglevel(1)
  3000. conn.request("HEAD", "hello")
  3001. resp = conn.getresponse()
  3002. if resp.status != 501:
  3003. raise Exception("Unexpected response to HEAD: " + str(resp.status))
  3004. conn.close()
  3005. for cmd in [ "PUT", "DELETE", "TRACE", "CONNECT", "M-SEARCH", "M-POST" ]:
  3006. try:
  3007. conn.request(cmd, "hello")
  3008. resp = conn.getresponse()
  3009. except Exception, e:
  3010. pass
  3011. conn.close()
  3012. headers = { "Content-Length": 'abc' }
  3013. conn.request("HEAD", "hello", "\r\n\r\n", headers)
  3014. try:
  3015. resp = conn.getresponse()
  3016. except Exception, e:
  3017. pass
  3018. conn.close()
  3019. headers = { "Content-Length": '-10' }
  3020. conn.request("HEAD", "hello", "\r\n\r\n", headers)
  3021. try:
  3022. resp = conn.getresponse()
  3023. except Exception, e:
  3024. pass
  3025. conn.close()
  3026. headers = { "Content-Length": '10000000000000' }
  3027. conn.request("HEAD", "hello", "\r\n\r\nhello", headers)
  3028. try:
  3029. resp = conn.getresponse()
  3030. except Exception, e:
  3031. pass
  3032. conn.close()
  3033. headers = { "Transfer-Encoding": 'abc' }
  3034. conn.request("HEAD", "hello", "\r\n\r\n", headers)
  3035. resp = conn.getresponse()
  3036. if resp.status != 501:
  3037. raise Exception("Unexpected response to HEAD: " + str(resp.status))
  3038. conn.close()
  3039. headers = { "Transfer-Encoding": 'chunked' }
  3040. conn.request("HEAD", "hello", "\r\n\r\n", headers)
  3041. resp = conn.getresponse()
  3042. if resp.status != 501:
  3043. raise Exception("Unexpected response to HEAD: " + str(resp.status))
  3044. conn.close()
  3045. # Too long a header
  3046. conn.request("HEAD", 5000 * 'A')
  3047. try:
  3048. resp = conn.getresponse()
  3049. except Exception, e:
  3050. pass
  3051. conn.close()
  3052. # Long URL but within header length limits
  3053. conn.request("HEAD", 3000 * 'A')
  3054. resp = conn.getresponse()
  3055. if resp.status != 501:
  3056. raise Exception("Unexpected response to HEAD: " + str(resp.status))
  3057. conn.close()
  3058. headers = { "Content-Length": '20' }
  3059. conn.request("POST", "hello", 10 * 'A' + "\r\n\r\n", headers)
  3060. try:
  3061. resp = conn.getresponse()
  3062. except Exception, e:
  3063. pass
  3064. conn.close()
  3065. conn.request("POST", "hello", 5000 * 'A' + "\r\n\r\n")
  3066. resp = conn.getresponse()
  3067. if resp.status != 404:
  3068. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3069. conn.close()
  3070. conn.request("POST", "hello", 60000 * 'A' + "\r\n\r\n")
  3071. try:
  3072. resp = conn.getresponse()
  3073. except Exception, e:
  3074. pass
  3075. conn.close()
  3076. def test_ap_wps_upnp_http_proto_chunked(dev, apdev):
  3077. """WPS AP and UPnP/HTTP protocol testing for chunked encoding"""
  3078. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  3079. add_ssdp_ap(apdev[0], ap_uuid)
  3080. location = ssdp_get_location(ap_uuid)
  3081. url = urlparse.urlparse(location)
  3082. conn = httplib.HTTPConnection(url.netloc)
  3083. #conn.set_debuglevel(1)
  3084. headers = { "Transfer-Encoding": 'chunked' }
  3085. conn.request("POST", "hello",
  3086. "a\r\nabcdefghij\r\n" + "2\r\nkl\r\n" + "0\r\n\r\n",
  3087. headers)
  3088. resp = conn.getresponse()
  3089. if resp.status != 404:
  3090. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3091. conn.close()
  3092. conn.putrequest("POST", "hello")
  3093. conn.putheader('Transfer-Encoding', 'chunked')
  3094. conn.endheaders()
  3095. conn.send("a\r\nabcdefghij\r\n")
  3096. time.sleep(0.1)
  3097. conn.send("2\r\nkl\r\n")
  3098. conn.send("0\r\n\r\n")
  3099. resp = conn.getresponse()
  3100. if resp.status != 404:
  3101. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3102. conn.close()
  3103. conn.putrequest("POST", "hello")
  3104. conn.putheader('Transfer-Encoding', 'chunked')
  3105. conn.endheaders()
  3106. completed = False
  3107. try:
  3108. for i in range(20000):
  3109. conn.send("1\r\nZ\r\n")
  3110. conn.send("0\r\n\r\n")
  3111. resp = conn.getresponse()
  3112. completed = True
  3113. except Exception, e:
  3114. pass
  3115. conn.close()
  3116. if completed:
  3117. raise Exception("Too long chunked request did not result in connection reset")
  3118. headers = { "Transfer-Encoding": 'chunked' }
  3119. conn.request("POST", "hello", "80000000\r\na", headers)
  3120. try:
  3121. resp = conn.getresponse()
  3122. except Exception, e:
  3123. pass
  3124. conn.close()
  3125. conn.request("POST", "hello", "10000000\r\na", headers)
  3126. try:
  3127. resp = conn.getresponse()
  3128. except Exception, e:
  3129. pass
  3130. conn.close()
  3131. @remote_compatible
  3132. def test_ap_wps_disabled(dev, apdev):
  3133. """WPS operations while WPS is disabled"""
  3134. ssid = "test-wps-disabled"
  3135. hapd = hostapd.add_ap(apdev[0], { "ssid": ssid })
  3136. if "FAIL" not in hapd.request("WPS_PBC"):
  3137. raise Exception("WPS_PBC succeeded unexpectedly")
  3138. if "FAIL" not in hapd.request("WPS_CANCEL"):
  3139. raise Exception("WPS_CANCEL succeeded unexpectedly")
  3140. def test_ap_wps_mixed_cred(dev, apdev):
  3141. """WPS 2.0 STA merging mixed mode WPA/WPA2 credentials"""
  3142. ssid = "test-wps-wep"
  3143. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3144. "skip_cred_build": "1", "extra_cred": "wps-mixed-cred" }
  3145. hapd = hostapd.add_ap(apdev[0], params)
  3146. hapd.request("WPS_PBC")
  3147. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  3148. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  3149. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=30)
  3150. if ev is None:
  3151. raise Exception("WPS-SUCCESS event timed out")
  3152. nets = dev[0].list_networks()
  3153. if len(nets) != 1:
  3154. raise Exception("Unexpected number of network blocks")
  3155. id = nets[0]['id']
  3156. proto = dev[0].get_network(id, "proto")
  3157. if proto != "WPA RSN":
  3158. raise Exception("Unexpected merged proto field value: " + proto)
  3159. pairwise = dev[0].get_network(id, "pairwise")
  3160. p = pairwise.split()
  3161. if "CCMP" not in p or "TKIP" not in p:
  3162. raise Exception("Unexpected merged pairwise field value: " + pairwise)
  3163. @remote_compatible
  3164. def test_ap_wps_while_connected(dev, apdev):
  3165. """WPS PBC provisioning while connected to another AP"""
  3166. ssid = "test-wps-conf"
  3167. hapd = hostapd.add_ap(apdev[0],
  3168. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3169. "wpa_passphrase": "12345678", "wpa": "2",
  3170. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3171. hostapd.add_ap(apdev[1], { "ssid": "open" })
  3172. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  3173. logger.info("WPS provisioning step")
  3174. hapd.request("WPS_PBC")
  3175. dev[0].dump_monitor()
  3176. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  3177. dev[0].wait_connected(timeout=30)
  3178. status = dev[0].get_status()
  3179. if status['bssid'] != apdev[0]['bssid']:
  3180. raise Exception("Unexpected BSSID")
  3181. @remote_compatible
  3182. def test_ap_wps_while_connected_no_autoconnect(dev, apdev):
  3183. """WPS PBC provisioning while connected to another AP and STA_AUTOCONNECT disabled"""
  3184. ssid = "test-wps-conf"
  3185. hapd = hostapd.add_ap(apdev[0],
  3186. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3187. "wpa_passphrase": "12345678", "wpa": "2",
  3188. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3189. hostapd.add_ap(apdev[1], { "ssid": "open" })
  3190. try:
  3191. dev[0].request("STA_AUTOCONNECT 0")
  3192. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  3193. logger.info("WPS provisioning step")
  3194. hapd.request("WPS_PBC")
  3195. dev[0].dump_monitor()
  3196. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  3197. dev[0].wait_connected(timeout=30)
  3198. status = dev[0].get_status()
  3199. if status['bssid'] != apdev[0]['bssid']:
  3200. raise Exception("Unexpected BSSID")
  3201. finally:
  3202. dev[0].request("STA_AUTOCONNECT 1")
  3203. @remote_compatible
  3204. def test_ap_wps_from_event(dev, apdev):
  3205. """WPS PBC event on AP to enable PBC"""
  3206. ssid = "test-wps-conf"
  3207. hapd = hostapd.add_ap(apdev[0],
  3208. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3209. "wpa_passphrase": "12345678", "wpa": "2",
  3210. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3211. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  3212. dev[0].dump_monitor()
  3213. hapd.dump_monitor()
  3214. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  3215. ev = hapd.wait_event(['WPS-ENROLLEE-SEEN'], timeout=15)
  3216. if ev is None:
  3217. raise Exception("No WPS-ENROLLEE-SEEN event on AP")
  3218. vals = ev.split(' ')
  3219. if vals[1] != dev[0].p2p_interface_addr():
  3220. raise Exception("Unexpected enrollee address: " + vals[1])
  3221. if vals[5] != '4':
  3222. raise Exception("Unexpected Device Password Id: " + vals[5])
  3223. hapd.request("WPS_PBC")
  3224. dev[0].wait_connected(timeout=30)
  3225. def test_ap_wps_ap_scan_2(dev, apdev):
  3226. """AP_SCAN 2 for WPS"""
  3227. ssid = "test-wps-conf"
  3228. hapd = hostapd.add_ap(apdev[0],
  3229. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3230. "wpa_passphrase": "12345678", "wpa": "2",
  3231. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3232. hapd.request("WPS_PBC")
  3233. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  3234. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  3235. wpas.dump_monitor()
  3236. if "OK" not in wpas.request("AP_SCAN 2"):
  3237. raise Exception("Failed to set AP_SCAN 2")
  3238. wpas.flush_scan_cache()
  3239. wpas.scan_for_bss(apdev[0]['bssid'], freq="2412")
  3240. wpas.dump_monitor()
  3241. wpas.request("WPS_PBC " + apdev[0]['bssid'])
  3242. ev = wpas.wait_event(["WPS-SUCCESS"], timeout=15)
  3243. if ev is None:
  3244. raise Exception("WPS-SUCCESS event timed out")
  3245. wpas.wait_connected(timeout=30)
  3246. wpas.dump_monitor()
  3247. wpas.request("DISCONNECT")
  3248. wpas.wait_disconnected()
  3249. id = wpas.list_networks()[0]['id']
  3250. pairwise = wpas.get_network(id, "pairwise")
  3251. if "CCMP" not in pairwise.split():
  3252. raise Exception("Unexpected pairwise parameter value: " + pairwise)
  3253. group = wpas.get_network(id, "group")
  3254. if "CCMP" not in group.split():
  3255. raise Exception("Unexpected group parameter value: " + group)
  3256. # Need to select a single cipher for ap_scan=2 testing
  3257. wpas.set_network(id, "pairwise", "CCMP")
  3258. wpas.set_network(id, "group", "CCMP")
  3259. wpas.request("BSS_FLUSH 0")
  3260. wpas.dump_monitor()
  3261. wpas.request("REASSOCIATE")
  3262. wpas.wait_connected(timeout=30)
  3263. wpas.dump_monitor()
  3264. @remote_compatible
  3265. def test_ap_wps_eapol_workaround(dev, apdev):
  3266. """EAPOL workaround code path for 802.1X header length mismatch"""
  3267. ssid = "test-wps"
  3268. hapd = hostapd.add_ap(apdev[0],
  3269. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  3270. bssid = apdev[0]['bssid']
  3271. hapd.request("SET ext_eapol_frame_io 1")
  3272. dev[0].request("SET ext_eapol_frame_io 1")
  3273. hapd.request("WPS_PBC")
  3274. dev[0].request("WPS_PBC")
  3275. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  3276. if ev is None:
  3277. raise Exception("Timeout on EAPOL-TX from hostapd")
  3278. res = dev[0].request("EAPOL_RX " + bssid + " 020000040193000501FFFF")
  3279. if "OK" not in res:
  3280. raise Exception("EAPOL_RX to wpa_supplicant failed")
  3281. def test_ap_wps_iteration(dev, apdev):
  3282. """WPS PIN and iterate through APs without selected registrar"""
  3283. ssid = "test-wps-conf"
  3284. hapd = hostapd.add_ap(apdev[0],
  3285. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3286. "wpa_passphrase": "12345678", "wpa": "2",
  3287. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3288. ssid2 = "test-wps-conf2"
  3289. hapd2 = hostapd.add_ap(apdev[1],
  3290. { "ssid": ssid2, "eap_server": "1", "wps_state": "2",
  3291. "wpa_passphrase": "12345678", "wpa": "2",
  3292. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3293. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  3294. dev[0].scan_for_bss(apdev[1]['bssid'], freq="2412")
  3295. dev[0].dump_monitor()
  3296. pin = dev[0].request("WPS_PIN any")
  3297. # Wait for iteration through all WPS APs to happen before enabling any
  3298. # Registrar.
  3299. for i in range(2):
  3300. ev = dev[0].wait_event(["Associated with"], timeout=30)
  3301. if ev is None:
  3302. raise Exception("No association seen")
  3303. ev = dev[0].wait_event(["WPS-M2D"], timeout=10)
  3304. if ev is None:
  3305. raise Exception("No M2D from AP")
  3306. dev[0].wait_disconnected()
  3307. # Verify that each AP requested PIN
  3308. ev = hapd.wait_event(["WPS-PIN-NEEDED"], timeout=1)
  3309. if ev is None:
  3310. raise Exception("No WPS-PIN-NEEDED event from AP")
  3311. ev = hapd2.wait_event(["WPS-PIN-NEEDED"], timeout=1)
  3312. if ev is None:
  3313. raise Exception("No WPS-PIN-NEEDED event from AP2")
  3314. # Provide PIN to one of the APs and verify that connection gets formed
  3315. hapd.request("WPS_PIN any " + pin)
  3316. dev[0].wait_connected(timeout=30)
  3317. def test_ap_wps_iteration_error(dev, apdev):
  3318. """WPS AP iteration on no Selected Registrar and error case with an AP"""
  3319. ssid = "test-wps-conf-pin"
  3320. hapd = hostapd.add_ap(apdev[0],
  3321. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3322. "wpa_passphrase": "12345678", "wpa": "2",
  3323. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  3324. "wps_independent": "1" })
  3325. hapd.request("SET ext_eapol_frame_io 1")
  3326. bssid = apdev[0]['bssid']
  3327. pin = dev[0].wps_read_pin()
  3328. dev[0].request("WPS_PIN any " + pin)
  3329. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  3330. if ev is None:
  3331. raise Exception("No EAPOL-TX (EAP-Request/Identity) from hostapd")
  3332. dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  3333. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  3334. if ev is None:
  3335. raise Exception("No EAPOL-TX (EAP-WSC/Start) from hostapd")
  3336. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=5)
  3337. if ev is None:
  3338. raise Exception("No CTRL-EVENT-EAP-STARTED")
  3339. # Do not forward any more EAPOL frames to test wpa_supplicant behavior for
  3340. # a case with an incorrectly behaving WPS AP.
  3341. # Start the real target AP and activate registrar on it.
  3342. hapd2 = hostapd.add_ap(apdev[1],
  3343. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3344. "wpa_passphrase": "12345678", "wpa": "2",
  3345. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  3346. "wps_independent": "1" })
  3347. hapd2.request("WPS_PIN any " + pin)
  3348. dev[0].wait_disconnected(timeout=15)
  3349. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=15)
  3350. if ev is None:
  3351. raise Exception("No CTRL-EVENT-EAP-STARTED for the second AP")
  3352. ev = dev[0].wait_event(["WPS-CRED-RECEIVED"], timeout=15)
  3353. if ev is None:
  3354. raise Exception("No WPS-CRED-RECEIVED for the second AP")
  3355. dev[0].wait_connected(timeout=15)
  3356. @remote_compatible
  3357. def test_ap_wps_priority(dev, apdev):
  3358. """WPS PIN provisioning with configured AP and wps_priority"""
  3359. ssid = "test-wps-conf-pin"
  3360. hapd = hostapd.add_ap(apdev[0],
  3361. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3362. "wpa_passphrase": "12345678", "wpa": "2",
  3363. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3364. logger.info("WPS provisioning step")
  3365. pin = dev[0].wps_read_pin()
  3366. hapd.request("WPS_PIN any " + pin)
  3367. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  3368. dev[0].dump_monitor()
  3369. try:
  3370. dev[0].request("SET wps_priority 6")
  3371. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  3372. dev[0].wait_connected(timeout=30)
  3373. netw = dev[0].list_networks()
  3374. prio = dev[0].get_network(netw[0]['id'], 'priority')
  3375. if prio != '6':
  3376. raise Exception("Unexpected network priority: " + prio)
  3377. finally:
  3378. dev[0].request("SET wps_priority 0")
  3379. @remote_compatible
  3380. def test_ap_wps_and_non_wps(dev, apdev):
  3381. """WPS and non-WPS AP in single hostapd process"""
  3382. params = { "ssid": "wps", "eap_server": "1", "wps_state": "1" }
  3383. hapd = hostapd.add_ap(apdev[0], params)
  3384. params = { "ssid": "no wps" }
  3385. hapd2 = hostapd.add_ap(apdev[1], params)
  3386. appin = hapd.request("WPS_AP_PIN random")
  3387. if "FAIL" in appin:
  3388. raise Exception("Could not generate random AP PIN")
  3389. if appin not in hapd.request("WPS_AP_PIN get"):
  3390. raise Exception("Could not fetch current AP PIN")
  3391. if "FAIL" in hapd.request("WPS_PBC"):
  3392. raise Exception("WPS_PBC failed")
  3393. if "FAIL" in hapd.request("WPS_CANCEL"):
  3394. raise Exception("WPS_CANCEL failed")
  3395. def test_ap_wps_init_oom(dev, apdev):
  3396. """Initial AP configuration and OOM during PSK generation"""
  3397. ssid = "test-wps"
  3398. params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
  3399. hapd = hostapd.add_ap(apdev[0], params)
  3400. with alloc_fail(hapd, 1, "base64_gen_encode;?base64_encode;wps_build_cred"):
  3401. pin = dev[0].wps_read_pin()
  3402. hapd.request("WPS_PIN any " + pin)
  3403. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  3404. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  3405. dev[0].wait_disconnected()
  3406. hapd.request("WPS_PIN any " + pin)
  3407. dev[0].wait_connected(timeout=30)
  3408. @remote_compatible
  3409. def test_ap_wps_er_oom(dev, apdev):
  3410. """WPS ER OOM in XML processing"""
  3411. try:
  3412. _test_ap_wps_er_oom(dev, apdev)
  3413. finally:
  3414. dev[0].request("WPS_ER_STOP")
  3415. dev[1].request("WPS_CANCEL")
  3416. dev[0].request("DISCONNECT")
  3417. def _test_ap_wps_er_oom(dev, apdev):
  3418. ssid = "wps-er-ap-config"
  3419. ap_pin = "12345670"
  3420. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  3421. hostapd.add_ap(apdev[0],
  3422. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3423. "wpa_passphrase": "12345678", "wpa": "2",
  3424. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  3425. "device_name": "Wireless AP", "manufacturer": "Company",
  3426. "model_name": "WAP", "model_number": "123",
  3427. "serial_number": "12345", "device_type": "6-0050F204-1",
  3428. "os_version": "01020300",
  3429. "config_methods": "label push_button",
  3430. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  3431. dev[0].connect(ssid, psk="12345678", scan_freq="2412")
  3432. with alloc_fail(dev[0], 1,
  3433. "base64_gen_decode;?base64_decode;xml_get_base64_item"):
  3434. dev[0].request("WPS_ER_START ifname=lo")
  3435. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=3)
  3436. if ev is not None:
  3437. raise Exception("Unexpected AP discovery")
  3438. dev[0].request("WPS_ER_STOP")
  3439. dev[0].request("WPS_ER_START ifname=lo")
  3440. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=10)
  3441. if ev is None:
  3442. raise Exception("AP discovery timed out")
  3443. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  3444. with alloc_fail(dev[0], 1,
  3445. "base64_gen_decode;?base64_decode;xml_get_base64_item"):
  3446. dev[1].request("WPS_PBC " + apdev[0]['bssid'])
  3447. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
  3448. if ev is None:
  3449. raise Exception("PBC scan failed")
  3450. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=15)
  3451. if ev is None:
  3452. raise Exception("Enrollee discovery timed out")
  3453. @remote_compatible
  3454. def test_ap_wps_er_init_oom(dev, apdev):
  3455. """WPS ER and OOM during init"""
  3456. try:
  3457. _test_ap_wps_er_init_oom(dev, apdev)
  3458. finally:
  3459. dev[0].request("WPS_ER_STOP")
  3460. def _test_ap_wps_er_init_oom(dev, apdev):
  3461. with alloc_fail(dev[0], 1, "wps_er_init"):
  3462. if "FAIL" not in dev[0].request("WPS_ER_START ifname=lo"):
  3463. raise Exception("WPS_ER_START succeeded during OOM")
  3464. with alloc_fail(dev[0], 1, "http_server_init"):
  3465. if "FAIL" not in dev[0].request("WPS_ER_START ifname=lo"):
  3466. raise Exception("WPS_ER_START succeeded during OOM")
  3467. with alloc_fail(dev[0], 2, "http_server_init"):
  3468. if "FAIL" not in dev[0].request("WPS_ER_START ifname=lo"):
  3469. raise Exception("WPS_ER_START succeeded during OOM")
  3470. with alloc_fail(dev[0], 1, "eloop_sock_table_add_sock;?eloop_register_sock;wps_er_ssdp_init"):
  3471. if "FAIL" not in dev[0].request("WPS_ER_START ifname=lo"):
  3472. raise Exception("WPS_ER_START succeeded during OOM")
  3473. with fail_test(dev[0], 1, "os_get_random;wps_er_init"):
  3474. if "FAIL" not in dev[0].request("WPS_ER_START ifname=lo"):
  3475. raise Exception("WPS_ER_START succeeded during os_get_random failure")
  3476. @remote_compatible
  3477. def test_ap_wps_er_init_fail(dev, apdev):
  3478. """WPS ER init failure"""
  3479. if "FAIL" not in dev[0].request("WPS_ER_START ifname=does-not-exist"):
  3480. dev[0].request("WPS_ER_STOP")
  3481. raise Exception("WPS_ER_START with non-existing ifname succeeded")
  3482. def test_ap_wps_wpa_cli_action(dev, apdev, test_params):
  3483. """WPS events and wpa_cli action script"""
  3484. logdir = os.path.abspath(test_params['logdir'])
  3485. pidfile = os.path.join(logdir, 'ap_wps_wpa_cli_action.wpa_cli.pid')
  3486. logfile = os.path.join(logdir, 'ap_wps_wpa_cli_action.wpa_cli.res')
  3487. actionfile = os.path.join(logdir, 'ap_wps_wpa_cli_action.wpa_cli.action.sh')
  3488. with open(actionfile, 'w') as f:
  3489. f.write('#!/bin/sh\n')
  3490. f.write('echo $* >> %s\n' % logfile)
  3491. # Kill the process and wait some time before returning to allow all the
  3492. # pending events to be processed with some of this happening after the
  3493. # eloop SIGALRM signal has been scheduled.
  3494. f.write('if [ $2 = "WPS-SUCCESS" -a -r %s ]; then kill `cat %s`; sleep 1; fi\n' % (pidfile, pidfile))
  3495. os.chmod(actionfile, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC |
  3496. stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
  3497. ssid = "test-wps-conf"
  3498. hapd = hostapd.add_ap(apdev[0],
  3499. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3500. "wpa_passphrase": "12345678", "wpa": "2",
  3501. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3502. prg = os.path.join(test_params['logdir'],
  3503. 'alt-wpa_supplicant/wpa_supplicant/wpa_cli')
  3504. if not os.path.exists(prg):
  3505. prg = '../../wpa_supplicant/wpa_cli'
  3506. arg = [ prg, '-P', pidfile, '-B', '-i', dev[0].ifname, '-a', actionfile ]
  3507. subprocess.call(arg)
  3508. arg = [ 'ps', 'ax' ]
  3509. cmd = subprocess.Popen(arg, stdout=subprocess.PIPE)
  3510. out = cmd.communicate()[0]
  3511. cmd.wait()
  3512. logger.debug("Processes:\n" + out)
  3513. if "wpa_cli -P %s -B -i %s" % (pidfile, dev[0].ifname) not in out:
  3514. raise Exception("Did not see wpa_cli running")
  3515. hapd.request("WPS_PIN any 12345670")
  3516. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  3517. dev[0].dump_monitor()
  3518. dev[0].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  3519. dev[0].wait_connected(timeout=30)
  3520. for i in range(30):
  3521. if not os.path.exists(pidfile):
  3522. break
  3523. time.sleep(0.1)
  3524. if not os.path.exists(logfile):
  3525. raise Exception("wpa_cli action results file not found")
  3526. with open(logfile, 'r') as f:
  3527. res = f.read()
  3528. if "WPS-SUCCESS" not in res:
  3529. raise Exception("WPS-SUCCESS event not seen in action file")
  3530. arg = [ 'ps', 'ax' ]
  3531. cmd = subprocess.Popen(arg, stdout=subprocess.PIPE)
  3532. out = cmd.communicate()[0]
  3533. cmd.wait()
  3534. logger.debug("Remaining processes:\n" + out)
  3535. if "wpa_cli -P %s -B -i %s" % (pidfile, dev[0].ifname) in out:
  3536. raise Exception("wpa_cli still running")
  3537. if os.path.exists(pidfile):
  3538. raise Exception("PID file not removed")
  3539. def test_ap_wps_er_ssdp_proto(dev, apdev):
  3540. """WPS ER SSDP protocol testing"""
  3541. try:
  3542. _test_ap_wps_er_ssdp_proto(dev, apdev)
  3543. finally:
  3544. dev[0].request("WPS_ER_STOP")
  3545. def _test_ap_wps_er_ssdp_proto(dev, apdev):
  3546. socket.setdefaulttimeout(1)
  3547. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  3548. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  3549. sock.bind(("239.255.255.250", 1900))
  3550. if "FAIL" not in dev[0].request("WPS_ER_START ifname=lo foo"):
  3551. raise Exception("Invalid filter accepted")
  3552. if "OK" not in dev[0].request("WPS_ER_START ifname=lo 1.2.3.4"):
  3553. raise Exception("WPS_ER_START with filter failed")
  3554. (msg,addr) = sock.recvfrom(1000)
  3555. logger.debug("Received SSDP message from %s: %s" % (str(addr), msg))
  3556. if "M-SEARCH" not in msg:
  3557. raise Exception("Not an M-SEARCH")
  3558. sock.sendto("FOO", addr)
  3559. time.sleep(0.1)
  3560. dev[0].request("WPS_ER_STOP")
  3561. dev[0].request("WPS_ER_START ifname=lo")
  3562. (msg,addr) = sock.recvfrom(1000)
  3563. logger.debug("Received SSDP message from %s: %s" % (str(addr), msg))
  3564. if "M-SEARCH" not in msg:
  3565. raise Exception("Not an M-SEARCH")
  3566. sock.sendto("FOO", addr)
  3567. sock.sendto("HTTP/1.1 200 OK\r\nFOO\r\n\r\n", addr)
  3568. sock.sendto("HTTP/1.1 200 OK\r\nNTS:foo\r\n\r\n", addr)
  3569. sock.sendto("HTTP/1.1 200 OK\r\nNTS:ssdp:byebye\r\n\r\n", addr)
  3570. sock.sendto("HTTP/1.1 200 OK\r\ncache-control: foo=1\r\n\r\n", addr)
  3571. sock.sendto("HTTP/1.1 200 OK\r\ncache-control: max-age=1\r\n\r\n", addr)
  3572. sock.sendto("HTTP/1.1 200 OK\r\nusn:\r\n\r\n", addr)
  3573. sock.sendto("HTTP/1.1 200 OK\r\nusn:foo\r\n\r\n", addr)
  3574. sock.sendto("HTTP/1.1 200 OK\r\nusn: uuid:\r\n\r\n", addr)
  3575. sock.sendto("HTTP/1.1 200 OK\r\nusn: uuid: \r\n\r\n", addr)
  3576. sock.sendto("HTTP/1.1 200 OK\r\nusn: uuid: foo\r\n\r\n", addr)
  3577. sock.sendto("HTTP/1.1 200 OK\r\nST: urn:schemas-wifialliance-org:device:WFADevice:1\r\n\r\n", addr)
  3578. sock.sendto("HTTP/1.1 200 OK\r\nST: urn:schemas-wifialliance-org:device:WFADevice:1\r\nNTS:ssdp:byebye\r\n\r\n", addr)
  3579. sock.sendto("HTTP/1.1 200 OK\r\nST: urn:schemas-wifialliance-org:device:WFADevice:1\r\nlocation:foo\r\n\r\n", addr)
  3580. with alloc_fail(dev[0], 1, "wps_er_ap_add"):
  3581. sock.sendto("HTTP/1.1 200 OK\r\nST: urn:schemas-wifialliance-org:device:WFADevice:1\r\nlocation:foo\r\ncache-control:max-age=1\r\n\r\n", addr)
  3582. time.sleep(0.1)
  3583. with alloc_fail(dev[0], 2, "wps_er_ap_add"):
  3584. sock.sendto("HTTP/1.1 200 OK\r\nST: urn:schemas-wifialliance-org:device:WFADevice:1\r\nlocation:foo\r\ncache-control:max-age=1\r\n\r\n", addr)
  3585. time.sleep(0.1)
  3586. # Add an AP with bogus URL
  3587. sock.sendto("HTTP/1.1 200 OK\r\nST: urn:schemas-wifialliance-org:device:WFADevice:1\r\nlocation:foo\r\ncache-control:max-age=1\r\n\r\n", addr)
  3588. # Update timeout on AP without updating URL
  3589. sock.sendto("HTTP/1.1 200 OK\r\nST: urn:schemas-wifialliance-org:device:WFADevice:1\r\nlocation:http://127.0.0.1:12345/foo.xml\r\ncache-control:max-age=1\r\n\r\n", addr)
  3590. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"], timeout=5)
  3591. if ev is None:
  3592. raise Exception("No WPS-ER-AP-REMOVE event on max-age timeout")
  3593. # Add an AP with a valid URL (but no server listing to it)
  3594. sock.sendto("HTTP/1.1 200 OK\r\nST: urn:schemas-wifialliance-org:device:WFADevice:1\r\nlocation:http://127.0.0.1:12345/foo.xml\r\ncache-control:max-age=1\r\n\r\n", addr)
  3595. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"], timeout=5)
  3596. if ev is None:
  3597. raise Exception("No WPS-ER-AP-REMOVE event on max-age timeout")
  3598. sock.close()
  3599. wps_event_url = None
  3600. def gen_upnp_info(eventSubURL='wps_event', controlURL='wps_control',
  3601. udn='uuid:27ea801a-9e5c-4e73-bd82-f89cbcd10d7e'):
  3602. payload = '''<?xml version="1.0"?>
  3603. <root xmlns="urn:schemas-upnp-org:device-1-0">
  3604. <specVersion>
  3605. <major>1</major>
  3606. <minor>0</minor>
  3607. </specVersion>
  3608. <device>
  3609. <deviceType>urn:schemas-wifialliance-org:device:WFADevice:1</deviceType>
  3610. <friendlyName>WPS Access Point</friendlyName>
  3611. <manufacturer>Company</manufacturer>
  3612. <modelName>WAP</modelName>
  3613. <modelNumber>123</modelNumber>
  3614. <serialNumber>12345</serialNumber>
  3615. '''
  3616. if udn:
  3617. payload += '<UDN>' + udn + '</UDN>'
  3618. payload += '''<serviceList>
  3619. <service>
  3620. <serviceType>urn:schemas-wifialliance-org:service:WFAWLANConfig:1</serviceType>
  3621. <serviceId>urn:wifialliance-org:serviceId:WFAWLANConfig1</serviceId>
  3622. <SCPDURL>wps_scpd.xml</SCPDURL>
  3623. '''
  3624. if controlURL:
  3625. payload += '<controlURL>' + controlURL + '</controlURL>\n'
  3626. if eventSubURL:
  3627. payload += '<eventSubURL>' + eventSubURL + '</eventSubURL>\n'
  3628. payload += '''</service>
  3629. </serviceList>
  3630. </device>
  3631. </root>
  3632. '''
  3633. hdr = 'HTTP/1.1 200 OK\r\n' + \
  3634. 'Content-Type: text/xml; charset="utf-8"\r\n' + \
  3635. 'Server: Unspecified, UPnP/1.0, Unspecified\r\n' + \
  3636. 'Connection: close\r\n' + \
  3637. 'Content-Length: ' + str(len(payload)) + '\r\n' + \
  3638. 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
  3639. return hdr + payload
  3640. def gen_wps_control(payload_override=None):
  3641. payload = '''<?xml version="1.0"?>
  3642. <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  3643. <s:Body>
  3644. <u:GetDeviceInfoResponse xmlns:u="urn:schemas-wifialliance-org:service:WFAWLANConfig:1">
  3645. <NewDeviceInfo>EEoAARAQIgABBBBHABAn6oAanlxOc72C+Jy80Q1+ECAABgIAAAADABAaABCJZ7DPtbU3Ust9
  3646. Z3wJF07WEDIAwH45D3i1OqB7eJGwTzqeapS71h3KyXncK2xJZ+xqScrlorNEg6LijBJzG2Ca
  3647. +FZli0iliDJd397yAx/jk4nFXco3q5ylBSvSw9dhJ5u1xBKSnTilKGlUHPhLP75PUqM3fot9
  3648. 7zwtFZ4bx6x1sBA6oEe2d0aUJmLumQGCiKEIWlnxs44zego/2tAe81bDzdPBM7o5HH/FUhD+
  3649. KoGzFXp51atP+1n9Vta6AkI0Vye99JKLcC6Md9dMJltSVBgd4Xc4lRAEAAIAIxAQAAIADRAN
  3650. AAEBEAgAAgAEEEQAAQIQIQAHQ29tcGFueRAjAANXQVAQJAADMTIzEEIABTEyMzQ1EFQACAAG
  3651. AFDyBAABEBEAC1dpcmVsZXNzIEFQEDwAAQEQAgACAAAQEgACAAAQCQACAAAQLQAEgQIDABBJ
  3652. AAYANyoAASA=
  3653. </NewDeviceInfo>
  3654. </u:GetDeviceInfoResponse>
  3655. </s:Body>
  3656. </s:Envelope>
  3657. '''
  3658. if payload_override:
  3659. payload = payload_override
  3660. hdr = 'HTTP/1.1 200 OK\r\n' + \
  3661. 'Content-Type: text/xml; charset="utf-8"\r\n' + \
  3662. 'Server: Unspecified, UPnP/1.0, Unspecified\r\n' + \
  3663. 'Connection: close\r\n' + \
  3664. 'Content-Length: ' + str(len(payload)) + '\r\n' + \
  3665. 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
  3666. return hdr + payload
  3667. def gen_wps_event(sid='uuid:7eb3342a-8a5f-47fe-a585-0785bfec6d8a'):
  3668. payload = ""
  3669. hdr = 'HTTP/1.1 200 OK\r\n' + \
  3670. 'Content-Type: text/xml; charset="utf-8"\r\n' + \
  3671. 'Server: Unspecified, UPnP/1.0, Unspecified\r\n' + \
  3672. 'Connection: close\r\n' + \
  3673. 'Content-Length: ' + str(len(payload)) + '\r\n'
  3674. if sid:
  3675. hdr += 'SID: ' + sid + '\r\n'
  3676. hdr += 'Timeout: Second-1801\r\n' + \
  3677. 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
  3678. return hdr + payload
  3679. class WPSAPHTTPServer(SocketServer.StreamRequestHandler):
  3680. def handle(self):
  3681. data = self.rfile.readline().strip()
  3682. logger.info("HTTP server received: " + data)
  3683. while True:
  3684. hdr = self.rfile.readline().strip()
  3685. if len(hdr) == 0:
  3686. break
  3687. logger.info("HTTP header: " + hdr)
  3688. if "CALLBACK:" in hdr:
  3689. global wps_event_url
  3690. wps_event_url = hdr.split(' ')[1].strip('<>')
  3691. if "GET /foo.xml" in data:
  3692. self.handle_upnp_info()
  3693. elif "POST /wps_control" in data:
  3694. self.handle_wps_control()
  3695. elif "SUBSCRIBE /wps_event" in data:
  3696. self.handle_wps_event()
  3697. else:
  3698. self.handle_others(data)
  3699. def handle_upnp_info(self):
  3700. self.wfile.write(gen_upnp_info())
  3701. def handle_wps_control(self):
  3702. self.wfile.write(gen_wps_control())
  3703. def handle_wps_event(self):
  3704. self.wfile.write(gen_wps_event())
  3705. def handle_others(self, data):
  3706. logger.info("Ignore HTTP request: " + data)
  3707. class MyTCPServer(SocketServer.TCPServer):
  3708. def __init__(self, addr, handler):
  3709. self.allow_reuse_address = True
  3710. SocketServer.TCPServer.__init__(self, addr, handler)
  3711. def wps_er_start(dev, http_server, max_age=1, wait_m_search=False,
  3712. location_url=None):
  3713. socket.setdefaulttimeout(1)
  3714. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  3715. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  3716. sock.bind(("239.255.255.250", 1900))
  3717. dev.request("WPS_ER_START ifname=lo")
  3718. for i in range(100):
  3719. (msg,addr) = sock.recvfrom(1000)
  3720. logger.debug("Received SSDP message from %s: %s" % (str(addr), msg))
  3721. if "M-SEARCH" in msg:
  3722. break
  3723. if not wait_m_search:
  3724. raise Exception("Not an M-SEARCH")
  3725. if i == 99:
  3726. raise Exception("No M-SEARCH seen")
  3727. # Add an AP with a valid URL and server listing to it
  3728. server = MyTCPServer(("127.0.0.1", 12345), http_server)
  3729. if not location_url:
  3730. location_url = 'http://127.0.0.1:12345/foo.xml'
  3731. sock.sendto("HTTP/1.1 200 OK\r\nST: urn:schemas-wifialliance-org:device:WFADevice:1\r\nlocation:%s\r\ncache-control:max-age=%d\r\n\r\n" % (location_url, max_age), addr)
  3732. server.timeout = 1
  3733. return server,sock
  3734. def wps_er_stop(dev, sock, server, on_alloc_fail=False):
  3735. sock.close()
  3736. server.server_close()
  3737. if on_alloc_fail:
  3738. done = False
  3739. for i in range(50):
  3740. res = dev.request("GET_ALLOC_FAIL")
  3741. if res.startswith("0:"):
  3742. done = True
  3743. break
  3744. time.sleep(0.1)
  3745. if not done:
  3746. raise Exception("No allocation failure reported")
  3747. else:
  3748. ev = dev.wait_event(["WPS-ER-AP-REMOVE"], timeout=5)
  3749. if ev is None:
  3750. raise Exception("No WPS-ER-AP-REMOVE event on max-age timeout")
  3751. dev.request("WPS_ER_STOP")
  3752. def run_wps_er_proto_test(dev, handler, no_event_url=False, location_url=None):
  3753. try:
  3754. uuid = '27ea801a-9e5c-4e73-bd82-f89cbcd10d7e'
  3755. server,sock = wps_er_start(dev, handler, location_url=location_url)
  3756. global wps_event_url
  3757. wps_event_url = None
  3758. server.handle_request()
  3759. server.handle_request()
  3760. server.handle_request()
  3761. server.server_close()
  3762. if no_event_url:
  3763. if wps_event_url:
  3764. raise Exception("Received event URL unexpectedly")
  3765. return
  3766. if wps_event_url is None:
  3767. raise Exception("Did not get event URL")
  3768. logger.info("Event URL: " + wps_event_url)
  3769. finally:
  3770. dev.request("WPS_ER_STOP")
  3771. def send_wlanevent(url, uuid, data, no_response=False):
  3772. conn = httplib.HTTPConnection(url.netloc)
  3773. payload = '''<?xml version="1.0" encoding="utf-8"?>
  3774. <e:propertyset xmlns:e="urn:schemas-upnp-org:event-1-0">
  3775. <e:property><STAStatus>1</STAStatus></e:property>
  3776. <e:property><APStatus>1</APStatus></e:property>
  3777. <e:property><WLANEvent>'''
  3778. payload += base64.b64encode(data)
  3779. payload += '</WLANEvent></e:property></e:propertyset>'
  3780. headers = { "Content-type": 'text/xml; charset="utf-8"',
  3781. "Server": "Unspecified, UPnP/1.0, Unspecified",
  3782. "HOST": url.netloc,
  3783. "NT": "upnp:event",
  3784. "SID": "uuid:" + uuid,
  3785. "SEQ": "0",
  3786. "Content-Length": str(len(payload)) }
  3787. conn.request("NOTIFY", url.path, payload, headers)
  3788. if no_response:
  3789. try:
  3790. conn.getresponse()
  3791. except Exception, e:
  3792. pass
  3793. return
  3794. resp = conn.getresponse()
  3795. if resp.status != 200:
  3796. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3797. def test_ap_wps_er_http_proto(dev, apdev):
  3798. """WPS ER HTTP protocol testing"""
  3799. try:
  3800. _test_ap_wps_er_http_proto(dev, apdev)
  3801. finally:
  3802. dev[0].request("WPS_ER_STOP")
  3803. def _test_ap_wps_er_http_proto(dev, apdev):
  3804. uuid = '27ea801a-9e5c-4e73-bd82-f89cbcd10d7e'
  3805. server,sock = wps_er_start(dev[0], WPSAPHTTPServer, max_age=15)
  3806. global wps_event_url
  3807. wps_event_url = None
  3808. server.handle_request()
  3809. server.handle_request()
  3810. server.handle_request()
  3811. server.server_close()
  3812. if wps_event_url is None:
  3813. raise Exception("Did not get event URL")
  3814. logger.info("Event URL: " + wps_event_url)
  3815. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=10)
  3816. if ev is None:
  3817. raise Exception("No WPS-ER-AP-ADD event")
  3818. if uuid not in ev:
  3819. raise Exception("UUID mismatch")
  3820. sock.close()
  3821. logger.info("Valid Probe Request notification")
  3822. url = urlparse.urlparse(wps_event_url)
  3823. conn = httplib.HTTPConnection(url.netloc)
  3824. payload = '''<?xml version="1.0" encoding="utf-8"?>
  3825. <e:propertyset xmlns:e="urn:schemas-upnp-org:event-1-0">
  3826. <e:property><STAStatus>1</STAStatus></e:property>
  3827. <e:property><APStatus>1</APStatus></e:property>
  3828. <e:property><WLANEvent>ATAyOjAwOjAwOjAwOjAwOjAwEEoAARAQOgABAhAIAAIxSBBHABA2LbR7pTpRkYj7VFi5hrLk
  3829. EFQACAAAAAAAAAAAEDwAAQMQAgACAAAQCQACAAAQEgACAAAQIQABIBAjAAEgECQAASAQEQAI
  3830. RGV2aWNlIEEQSQAGADcqAAEg
  3831. </WLANEvent></e:property>
  3832. </e:propertyset>
  3833. '''
  3834. headers = { "Content-type": 'text/xml; charset="utf-8"',
  3835. "Server": "Unspecified, UPnP/1.0, Unspecified",
  3836. "HOST": url.netloc,
  3837. "NT": "upnp:event",
  3838. "SID": "uuid:" + uuid,
  3839. "SEQ": "0",
  3840. "Content-Length": str(len(payload)) }
  3841. conn.request("NOTIFY", url.path, payload, headers)
  3842. resp = conn.getresponse()
  3843. if resp.status != 200:
  3844. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3845. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=5)
  3846. if ev is None:
  3847. raise Exception("No WPS-ER-ENROLLEE-ADD event")
  3848. if "362db47b-a53a-5191-88fb-5458b986b2e4" not in ev:
  3849. raise Exception("No Enrollee UUID match")
  3850. logger.info("Incorrect event URL AP id")
  3851. conn = httplib.HTTPConnection(url.netloc)
  3852. conn.request("NOTIFY", url.path + '123', payload, headers)
  3853. resp = conn.getresponse()
  3854. if resp.status != 404:
  3855. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3856. logger.info("Missing AP id")
  3857. conn = httplib.HTTPConnection(url.netloc)
  3858. conn.request("NOTIFY", '/event/' + url.path.split('/')[2],
  3859. payload, headers)
  3860. time.sleep(0.1)
  3861. logger.info("Incorrect event URL event id")
  3862. conn = httplib.HTTPConnection(url.netloc)
  3863. conn.request("NOTIFY", '/event/123456789/123', payload, headers)
  3864. time.sleep(0.1)
  3865. logger.info("Incorrect event URL prefix")
  3866. conn = httplib.HTTPConnection(url.netloc)
  3867. conn.request("NOTIFY", '/foobar/123456789/123', payload, headers)
  3868. resp = conn.getresponse()
  3869. if resp.status != 404:
  3870. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3871. logger.info("Unsupported request")
  3872. conn = httplib.HTTPConnection(url.netloc)
  3873. conn.request("FOOBAR", '/foobar/123456789/123', payload, headers)
  3874. resp = conn.getresponse()
  3875. if resp.status != 501:
  3876. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3877. logger.info("Unsupported request and OOM")
  3878. with alloc_fail(dev[0], 1, "wps_er_http_req"):
  3879. conn = httplib.HTTPConnection(url.netloc)
  3880. conn.request("FOOBAR", '/foobar/123456789/123', payload, headers)
  3881. time.sleep(0.5)
  3882. logger.info("Too short WLANEvent")
  3883. data = '\x00'
  3884. send_wlanevent(url, uuid, data)
  3885. logger.info("Invalid WLANEventMAC")
  3886. data = '\x00qwertyuiopasdfghjklzxcvbnm'
  3887. send_wlanevent(url, uuid, data)
  3888. logger.info("Unknown WLANEventType")
  3889. data = '\xff02:00:00:00:00:00'
  3890. send_wlanevent(url, uuid, data)
  3891. logger.info("Probe Request notification without any attributes")
  3892. data = '\x0102:00:00:00:00:00'
  3893. send_wlanevent(url, uuid, data)
  3894. logger.info("Probe Request notification with invalid attribute")
  3895. data = '\x0102:00:00:00:00:00\xff'
  3896. send_wlanevent(url, uuid, data)
  3897. logger.info("EAP message without any attributes")
  3898. data = '\x0202:00:00:00:00:00'
  3899. send_wlanevent(url, uuid, data)
  3900. logger.info("EAP message with invalid attribute")
  3901. data = '\x0202:00:00:00:00:00\xff'
  3902. send_wlanevent(url, uuid, data)
  3903. logger.info("EAP message from new STA and not M1")
  3904. data = '\x0202:ff:ff:ff:ff:ff' + '\x10\x22\x00\x01\x05'
  3905. send_wlanevent(url, uuid, data)
  3906. logger.info("EAP message: M1")
  3907. data = '\x0202:00:00:00:00:00'
  3908. data += '\x10\x22\x00\x01\x04'
  3909. data += '\x10\x47\x00\x10' + 16*'\x00'
  3910. data += '\x10\x20\x00\x06\x02\x00\x00\x00\x00\x00'
  3911. data += '\x10\x1a\x00\x10' + 16*'\x00'
  3912. data += '\x10\x32\x00\xc0' + 192*'\x00'
  3913. data += '\x10\x04\x00\x02\x00\x00'
  3914. data += '\x10\x10\x00\x02\x00\x00'
  3915. data += '\x10\x0d\x00\x01\x00'
  3916. data += '\x10\x08\x00\x02\x00\x00'
  3917. data += '\x10\x44\x00\x01\x00'
  3918. data += '\x10\x21\x00\x00'
  3919. data += '\x10\x23\x00\x00'
  3920. data += '\x10\x24\x00\x00'
  3921. data += '\x10\x42\x00\x00'
  3922. data += '\x10\x54\x00\x08' + 8*'\x00'
  3923. data += '\x10\x11\x00\x00'
  3924. data += '\x10\x3c\x00\x01\x00'
  3925. data += '\x10\x02\x00\x02\x00\x00'
  3926. data += '\x10\x12\x00\x02\x00\x00'
  3927. data += '\x10\x09\x00\x02\x00\x00'
  3928. data += '\x10\x2d\x00\x04\x00\x00\x00\x00'
  3929. m1 = data
  3930. send_wlanevent(url, uuid, data)
  3931. logger.info("EAP message: WSC_ACK")
  3932. data = '\x0202:00:00:00:00:00' + '\x10\x22\x00\x01\x0d'
  3933. send_wlanevent(url, uuid, data)
  3934. logger.info("EAP message: M1")
  3935. send_wlanevent(url, uuid, m1)
  3936. logger.info("EAP message: WSC_NACK")
  3937. data = '\x0202:00:00:00:00:00' + '\x10\x22\x00\x01\x0e'
  3938. send_wlanevent(url, uuid, data)
  3939. logger.info("EAP message: M1 - Too long attribute values")
  3940. data = '\x0202:00:00:00:00:00'
  3941. data += '\x10\x11\x00\x21' + 33*'\x00'
  3942. data += '\x10\x45\x00\x21' + 33*'\x00'
  3943. data += '\x10\x42\x00\x21' + 33*'\x00'
  3944. data += '\x10\x24\x00\x21' + 33*'\x00'
  3945. data += '\x10\x23\x00\x21' + 33*'\x00'
  3946. data += '\x10\x21\x00\x41' + 65*'\x00'
  3947. data += '\x10\x49\x00\x09\x00\x37\x2a\x05\x02\x00\x00\x05\x00'
  3948. send_wlanevent(url, uuid, data)
  3949. logger.info("EAP message: M1 missing UUID-E")
  3950. data = '\x0202:00:00:00:00:00'
  3951. data += '\x10\x22\x00\x01\x04'
  3952. send_wlanevent(url, uuid, data)
  3953. logger.info("EAP message: M1 missing MAC Address")
  3954. data += '\x10\x47\x00\x10' + 16*'\x00'
  3955. send_wlanevent(url, uuid, data)
  3956. logger.info("EAP message: M1 missing Enrollee Nonce")
  3957. data += '\x10\x20\x00\x06\x02\x00\x00\x00\x00\x00'
  3958. send_wlanevent(url, uuid, data)
  3959. logger.info("EAP message: M1 missing Public Key")
  3960. data += '\x10\x1a\x00\x10' + 16*'\x00'
  3961. send_wlanevent(url, uuid, data)
  3962. logger.info("EAP message: M1 missing Authentication Type flags")
  3963. data += '\x10\x32\x00\xc0' + 192*'\x00'
  3964. send_wlanevent(url, uuid, data)
  3965. logger.info("EAP message: M1 missing Encryption Type Flags")
  3966. data += '\x10\x04\x00\x02\x00\x00'
  3967. send_wlanevent(url, uuid, data)
  3968. logger.info("EAP message: M1 missing Connection Type flags")
  3969. data += '\x10\x10\x00\x02\x00\x00'
  3970. send_wlanevent(url, uuid, data)
  3971. logger.info("EAP message: M1 missing Config Methods")
  3972. data += '\x10\x0d\x00\x01\x00'
  3973. send_wlanevent(url, uuid, data)
  3974. logger.info("EAP message: M1 missing Wi-Fi Protected Setup State")
  3975. data += '\x10\x08\x00\x02\x00\x00'
  3976. send_wlanevent(url, uuid, data)
  3977. logger.info("EAP message: M1 missing Manufacturer")
  3978. data += '\x10\x44\x00\x01\x00'
  3979. send_wlanevent(url, uuid, data)
  3980. logger.info("EAP message: M1 missing Model Name")
  3981. data += '\x10\x21\x00\x00'
  3982. send_wlanevent(url, uuid, data)
  3983. logger.info("EAP message: M1 missing Model Number")
  3984. data += '\x10\x23\x00\x00'
  3985. send_wlanevent(url, uuid, data)
  3986. logger.info("EAP message: M1 missing Serial Number")
  3987. data += '\x10\x24\x00\x00'
  3988. send_wlanevent(url, uuid, data)
  3989. logger.info("EAP message: M1 missing Primary Device Type")
  3990. data += '\x10\x42\x00\x00'
  3991. send_wlanevent(url, uuid, data)
  3992. logger.info("EAP message: M1 missing Device Name")
  3993. data += '\x10\x54\x00\x08' + 8*'\x00'
  3994. send_wlanevent(url, uuid, data)
  3995. logger.info("EAP message: M1 missing RF Bands")
  3996. data += '\x10\x11\x00\x00'
  3997. send_wlanevent(url, uuid, data)
  3998. logger.info("EAP message: M1 missing Association State")
  3999. data += '\x10\x3c\x00\x01\x00'
  4000. send_wlanevent(url, uuid, data)
  4001. logger.info("EAP message: M1 missing Device Password ID")
  4002. data += '\x10\x02\x00\x02\x00\x00'
  4003. send_wlanevent(url, uuid, data)
  4004. logger.info("EAP message: M1 missing Configuration Error")
  4005. data += '\x10\x12\x00\x02\x00\x00'
  4006. send_wlanevent(url, uuid, data)
  4007. logger.info("EAP message: M1 missing OS Version")
  4008. data += '\x10\x09\x00\x02\x00\x00'
  4009. send_wlanevent(url, uuid, data)
  4010. logger.info("Check max concurrent requests")
  4011. addr = (url.hostname, url.port)
  4012. socks = {}
  4013. for i in range(20):
  4014. socks[i] = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
  4015. socket.IPPROTO_TCP)
  4016. socks[i].settimeout(10)
  4017. socks[i].connect(addr)
  4018. for i in range(20):
  4019. socks[i].send("GET / HTTP/1.1\r\n\r\n")
  4020. count = 0
  4021. for i in range(20):
  4022. try:
  4023. res = socks[i].recv(100)
  4024. if "HTTP/1" in res:
  4025. count += 1
  4026. else:
  4027. logger.info("recv[%d]: len=%d" % (i, len(res)))
  4028. except:
  4029. pass
  4030. socks[i].close()
  4031. logger.info("%d concurrent HTTP GET operations returned response" % count)
  4032. if count < 8:
  4033. raise Exception("Too few concurrent HTTP connections accepted")
  4034. logger.info("OOM in HTTP server")
  4035. for func in [ "http_request_init", "httpread_create",
  4036. "eloop_register_timeout;httpread_create",
  4037. "eloop_sock_table_add_sock;?eloop_register_sock;httpread_create",
  4038. "httpread_hdr_analyze" ]:
  4039. with alloc_fail(dev[0], 1, func):
  4040. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
  4041. socket.IPPROTO_TCP)
  4042. sock.connect(addr)
  4043. sock.send("GET / HTTP/1.1\r\n\r\n")
  4044. try:
  4045. sock.recv(100)
  4046. except:
  4047. pass
  4048. sock.close()
  4049. logger.info("Invalid HTTP header")
  4050. for req in [ " GET / HTTP/1.1\r\n\r\n",
  4051. "HTTP/1.1 200 OK\r\n\r\n",
  4052. "HTTP/\r\n\r\n",
  4053. "GET %%a%aa% HTTP/1.1\r\n\r\n",
  4054. "GET / HTTP/1.1\r\n FOO\r\n\r\n",
  4055. "NOTIFY / HTTP/1.1\r\n" + 4097*'a' + '\r\n\r\n',
  4056. "NOTIFY / HTTP/1.1\r\n\r\n" + 8193*'a',
  4057. "POST / HTTP/1.1\r\nTransfer-Encoding: CHUNKED\r\n\r\n foo\r\n",
  4058. "POST / HTTP/1.1\r\nTransfer-Encoding: CHUNKED\r\n\r\n1\r\nfoo\r\n",
  4059. "POST / HTTP/1.1\r\nTransfer-Encoding: CHUNKED\r\n\r\n0\r\n",
  4060. "POST / HTTP/1.1\r\nTransfer-Encoding: CHUNKED\r\n\r\n0\r\naa\ra\r\n\ra" ]:
  4061. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
  4062. socket.IPPROTO_TCP)
  4063. sock.settimeout(0.1)
  4064. sock.connect(addr)
  4065. sock.send(req)
  4066. try:
  4067. sock.recv(100)
  4068. except:
  4069. pass
  4070. sock.close()
  4071. with alloc_fail(dev[0], 2, "httpread_read_handler"):
  4072. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
  4073. socket.IPPROTO_TCP)
  4074. sock.connect(addr)
  4075. sock.send("NOTIFY / HTTP/1.1\r\n\r\n" + 4500*'a')
  4076. try:
  4077. sock.recv(100)
  4078. except:
  4079. pass
  4080. sock.close()
  4081. conn = httplib.HTTPConnection(url.netloc)
  4082. payload = '<foo'
  4083. headers = { "Content-type": 'text/xml; charset="utf-8"',
  4084. "Server": "Unspecified, UPnP/1.0, Unspecified",
  4085. "HOST": url.netloc,
  4086. "NT": "upnp:event",
  4087. "SID": "uuid:" + uuid,
  4088. "SEQ": "0",
  4089. "Content-Length": str(len(payload)) }
  4090. conn.request("NOTIFY", url.path, payload, headers)
  4091. resp = conn.getresponse()
  4092. if resp.status != 200:
  4093. raise Exception("Unexpected HTTP response: %d" % resp.status)
  4094. conn = httplib.HTTPConnection(url.netloc)
  4095. payload = '<WLANEvent foo></WLANEvent>'
  4096. headers = { "Content-type": 'text/xml; charset="utf-8"',
  4097. "Server": "Unspecified, UPnP/1.0, Unspecified",
  4098. "HOST": url.netloc,
  4099. "NT": "upnp:event",
  4100. "SID": "uuid:" + uuid,
  4101. "SEQ": "0",
  4102. "Content-Length": str(len(payload)) }
  4103. conn.request("NOTIFY", url.path, payload, headers)
  4104. resp = conn.getresponse()
  4105. if resp.status != 200:
  4106. raise Exception("Unexpected HTTP response: %d" % resp.status)
  4107. with alloc_fail(dev[0], 1, "xml_get_first_item"):
  4108. send_wlanevent(url, uuid, '')
  4109. with alloc_fail(dev[0], 1, "wpabuf_alloc_ext_data;xml_get_base64_item"):
  4110. send_wlanevent(url, uuid, 'foo')
  4111. for func in [ "wps_init",
  4112. "wps_process_manufacturer",
  4113. "wps_process_model_name",
  4114. "wps_process_model_number",
  4115. "wps_process_serial_number",
  4116. "wps_process_dev_name" ]:
  4117. with alloc_fail(dev[0], 1, func):
  4118. send_wlanevent(url, uuid, m1)
  4119. with alloc_fail(dev[0], 1, "wps_er_http_resp_ok"):
  4120. send_wlanevent(url, uuid, m1, no_response=True)
  4121. with alloc_fail(dev[0], 1, "wps_er_http_resp_not_found"):
  4122. url2 = urlparse.urlparse(wps_event_url.replace('/event/', '/notfound/'))
  4123. send_wlanevent(url2, uuid, m1, no_response=True)
  4124. logger.info("EAP message: M1")
  4125. data = '\x0202:11:22:00:00:00'
  4126. data += '\x10\x22\x00\x01\x04'
  4127. data += '\x10\x47\x00\x10' + 16*'\x00'
  4128. data += '\x10\x20\x00\x06\x02\x00\x00\x00\x00\x00'
  4129. data += '\x10\x1a\x00\x10' + 16*'\x00'
  4130. data += '\x10\x32\x00\xc0' + 192*'\x00'
  4131. data += '\x10\x04\x00\x02\x00\x00'
  4132. data += '\x10\x10\x00\x02\x00\x00'
  4133. data += '\x10\x0d\x00\x01\x00'
  4134. data += '\x10\x08\x00\x02\x00\x00'
  4135. data += '\x10\x44\x00\x01\x00'
  4136. data += '\x10\x21\x00\x00'
  4137. data += '\x10\x23\x00\x00'
  4138. data += '\x10\x24\x00\x00'
  4139. data += '\x10\x42\x00\x00'
  4140. data += '\x10\x54\x00\x08' + 8*'\x00'
  4141. data += '\x10\x11\x00\x00'
  4142. data += '\x10\x3c\x00\x01\x00'
  4143. data += '\x10\x02\x00\x02\x00\x00'
  4144. data += '\x10\x12\x00\x02\x00\x00'
  4145. data += '\x10\x09\x00\x02\x00\x00'
  4146. data += '\x10\x2d\x00\x04\x00\x00\x00\x00'
  4147. dev[0].dump_monitor()
  4148. with alloc_fail(dev[0], 1, "wps_er_add_sta_data"):
  4149. send_wlanevent(url, uuid, data)
  4150. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=0.1)
  4151. if ev is not None:
  4152. raise Exception("Unexpected enrollee add event")
  4153. send_wlanevent(url, uuid, data)
  4154. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=2)
  4155. if ev is None:
  4156. raise Exception("Enrollee add event not seen")
  4157. with alloc_fail(dev[0], 1,
  4158. "base64_gen_encode;?base64_encode;wps_er_soap_hdr"):
  4159. send_wlanevent(url, uuid, data)
  4160. with alloc_fail(dev[0], 1, "wpabuf_alloc;wps_er_soap_hdr"):
  4161. send_wlanevent(url, uuid, data)
  4162. with alloc_fail(dev[0], 1, "http_client_url_parse;wps_er_sta_send_msg"):
  4163. send_wlanevent(url, uuid, data)
  4164. with alloc_fail(dev[0], 1, "http_client_addr;wps_er_sta_send_msg"):
  4165. send_wlanevent(url, uuid, data)
  4166. def test_ap_wps_er_http_proto_no_event_sub_url(dev, apdev):
  4167. """WPS ER HTTP protocol testing - no eventSubURL"""
  4168. class WPSAPHTTPServer_no_event_sub_url(WPSAPHTTPServer):
  4169. def handle_upnp_info(self):
  4170. self.wfile.write(gen_upnp_info(eventSubURL=None))
  4171. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_no_event_sub_url,
  4172. no_event_url=True)
  4173. def test_ap_wps_er_http_proto_event_sub_url_dns(dev, apdev):
  4174. """WPS ER HTTP protocol testing - DNS name in eventSubURL"""
  4175. class WPSAPHTTPServer_event_sub_url_dns(WPSAPHTTPServer):
  4176. def handle_upnp_info(self):
  4177. self.wfile.write(gen_upnp_info(eventSubURL='http://example.com/wps_event'))
  4178. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_event_sub_url_dns,
  4179. no_event_url=True)
  4180. def test_ap_wps_er_http_proto_subscribe_oom(dev, apdev):
  4181. """WPS ER HTTP protocol testing - subscribe OOM"""
  4182. try:
  4183. _test_ap_wps_er_http_proto_subscribe_oom(dev, apdev)
  4184. finally:
  4185. dev[0].request("WPS_ER_STOP")
  4186. def _test_ap_wps_er_http_proto_subscribe_oom(dev, apdev):
  4187. tests = [ (1, "http_client_url_parse"),
  4188. (1, "wpabuf_alloc;wps_er_subscribe"),
  4189. (1, "http_client_addr"),
  4190. (1, "eloop_sock_table_add_sock;?eloop_register_sock;http_client_addr"),
  4191. (1, "eloop_register_timeout;http_client_addr") ]
  4192. for count,func in tests:
  4193. with alloc_fail(dev[0], count, func):
  4194. server,sock = wps_er_start(dev[0], WPSAPHTTPServer)
  4195. server.handle_request()
  4196. server.handle_request()
  4197. wps_er_stop(dev[0], sock, server, on_alloc_fail=True)
  4198. def test_ap_wps_er_http_proto_no_sid(dev, apdev):
  4199. """WPS ER HTTP protocol testing - no SID"""
  4200. class WPSAPHTTPServer_no_sid(WPSAPHTTPServer):
  4201. def handle_wps_event(self):
  4202. self.wfile.write(gen_wps_event(sid=None))
  4203. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_no_sid)
  4204. def test_ap_wps_er_http_proto_invalid_sid_no_uuid(dev, apdev):
  4205. """WPS ER HTTP protocol testing - invalid SID - no UUID"""
  4206. class WPSAPHTTPServer_invalid_sid_no_uuid(WPSAPHTTPServer):
  4207. def handle_wps_event(self):
  4208. self.wfile.write(gen_wps_event(sid='FOO'))
  4209. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_invalid_sid_no_uuid)
  4210. def test_ap_wps_er_http_proto_invalid_sid_uuid(dev, apdev):
  4211. """WPS ER HTTP protocol testing - invalid SID UUID"""
  4212. class WPSAPHTTPServer_invalid_sid_uuid(WPSAPHTTPServer):
  4213. def handle_wps_event(self):
  4214. self.wfile.write(gen_wps_event(sid='uuid:FOO'))
  4215. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_invalid_sid_uuid)
  4216. def test_ap_wps_er_http_proto_subscribe_failing(dev, apdev):
  4217. """WPS ER HTTP protocol testing - SUBSCRIBE failing"""
  4218. class WPSAPHTTPServer_fail_subscribe(WPSAPHTTPServer):
  4219. def handle_wps_event(self):
  4220. payload = ""
  4221. hdr = 'HTTP/1.1 404 Not Found\r\n' + \
  4222. 'Content-Type: text/xml; charset="utf-8"\r\n' + \
  4223. 'Server: Unspecified, UPnP/1.0, Unspecified\r\n' + \
  4224. 'Connection: close\r\n' + \
  4225. 'Content-Length: ' + str(len(payload)) + '\r\n' + \
  4226. 'Timeout: Second-1801\r\n' + \
  4227. 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
  4228. self.wfile.write(hdr + payload)
  4229. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_fail_subscribe)
  4230. def test_ap_wps_er_http_proto_subscribe_invalid_response(dev, apdev):
  4231. """WPS ER HTTP protocol testing - SUBSCRIBE and invalid response"""
  4232. class WPSAPHTTPServer_subscribe_invalid_response(WPSAPHTTPServer):
  4233. def handle_wps_event(self):
  4234. payload = ""
  4235. hdr = 'HTTP/1.1 FOO\r\n' + \
  4236. 'Content-Type: text/xml; charset="utf-8"\r\n' + \
  4237. 'Server: Unspecified, UPnP/1.0, Unspecified\r\n' + \
  4238. 'Connection: close\r\n' + \
  4239. 'Content-Length: ' + str(len(payload)) + '\r\n' + \
  4240. 'Timeout: Second-1801\r\n' + \
  4241. 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
  4242. self.wfile.write(hdr + payload)
  4243. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_subscribe_invalid_response)
  4244. def test_ap_wps_er_http_proto_subscribe_invalid_response(dev, apdev):
  4245. """WPS ER HTTP protocol testing - SUBSCRIBE and invalid response"""
  4246. class WPSAPHTTPServer_invalid_m1(WPSAPHTTPServer):
  4247. def handle_wps_control(self):
  4248. payload = '''<?xml version="1.0"?>
  4249. <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  4250. <s:Body>
  4251. <u:GetDeviceInfoResponse xmlns:u="urn:schemas-wifialliance-org:service:WFAWLANConfig:1">
  4252. <NewDeviceInfo>Rk9P</NewDeviceInfo>
  4253. </u:GetDeviceInfoResponse>
  4254. </s:Body>
  4255. </s:Envelope>
  4256. '''
  4257. self.wfile.write(gen_wps_control(payload_override=payload))
  4258. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_invalid_m1, no_event_url=True)
  4259. def test_ap_wps_er_http_proto_upnp_info_no_device(dev, apdev):
  4260. """WPS ER HTTP protocol testing - No device in UPnP info"""
  4261. class WPSAPHTTPServer_no_device(WPSAPHTTPServer):
  4262. def handle_upnp_info(self):
  4263. payload = '''<?xml version="1.0"?>
  4264. <root xmlns="urn:schemas-upnp-org:device-1-0">
  4265. <specVersion>
  4266. <major>1</major>
  4267. <minor>0</minor>
  4268. </specVersion>
  4269. </root>
  4270. '''
  4271. hdr = 'HTTP/1.1 200 OK\r\n' + \
  4272. 'Content-Type: text/xml; charset="utf-8"\r\n' + \
  4273. 'Server: Unspecified, UPnP/1.0, Unspecified\r\n' + \
  4274. 'Connection: close\r\n' + \
  4275. 'Content-Length: ' + str(len(payload)) + '\r\n' + \
  4276. 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
  4277. self.wfile.write(hdr + payload)
  4278. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_no_device, no_event_url=True)
  4279. def test_ap_wps_er_http_proto_upnp_info_no_device_type(dev, apdev):
  4280. """WPS ER HTTP protocol testing - No deviceType in UPnP info"""
  4281. class WPSAPHTTPServer_no_device(WPSAPHTTPServer):
  4282. def handle_upnp_info(self):
  4283. payload = '''<?xml version="1.0"?>
  4284. <root xmlns="urn:schemas-upnp-org:device-1-0">
  4285. <specVersion>
  4286. <major>1</major>
  4287. <minor>0</minor>
  4288. </specVersion>
  4289. <device>
  4290. </device>
  4291. </root>
  4292. '''
  4293. hdr = 'HTTP/1.1 200 OK\r\n' + \
  4294. 'Content-Type: text/xml; charset="utf-8"\r\n' + \
  4295. 'Server: Unspecified, UPnP/1.0, Unspecified\r\n' + \
  4296. 'Connection: close\r\n' + \
  4297. 'Content-Length: ' + str(len(payload)) + '\r\n' + \
  4298. 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
  4299. self.wfile.write(hdr + payload)
  4300. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_no_device, no_event_url=True)
  4301. def test_ap_wps_er_http_proto_upnp_info_invalid_udn_uuid(dev, apdev):
  4302. """WPS ER HTTP protocol testing - Invalid UDN UUID"""
  4303. class WPSAPHTTPServer_invalid_udn_uuid(WPSAPHTTPServer):
  4304. def handle_upnp_info(self):
  4305. self.wfile.write(gen_upnp_info(udn='uuid:foo'))
  4306. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_invalid_udn_uuid)
  4307. def test_ap_wps_er_http_proto_no_control_url(dev, apdev):
  4308. """WPS ER HTTP protocol testing - no controlURL"""
  4309. class WPSAPHTTPServer_no_control_url(WPSAPHTTPServer):
  4310. def handle_upnp_info(self):
  4311. self.wfile.write(gen_upnp_info(controlURL=None))
  4312. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_no_control_url,
  4313. no_event_url=True)
  4314. def test_ap_wps_er_http_proto_control_url_dns(dev, apdev):
  4315. """WPS ER HTTP protocol testing - DNS name in controlURL"""
  4316. class WPSAPHTTPServer_control_url_dns(WPSAPHTTPServer):
  4317. def handle_upnp_info(self):
  4318. self.wfile.write(gen_upnp_info(controlURL='http://example.com/wps_control'))
  4319. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_control_url_dns,
  4320. no_event_url=True)
  4321. def test_ap_wps_http_timeout(dev, apdev):
  4322. """WPS AP/ER and HTTP timeout"""
  4323. try:
  4324. _test_ap_wps_http_timeout(dev, apdev)
  4325. finally:
  4326. dev[0].request("WPS_ER_STOP")
  4327. def _test_ap_wps_http_timeout(dev, apdev):
  4328. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  4329. add_ssdp_ap(apdev[0], ap_uuid)
  4330. location = ssdp_get_location(ap_uuid)
  4331. url = urlparse.urlparse(location)
  4332. addr = (url.hostname, url.port)
  4333. logger.debug("Open HTTP connection to hostapd, but do not complete request")
  4334. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
  4335. socket.IPPROTO_TCP)
  4336. sock.connect(addr)
  4337. sock.send("G")
  4338. class DummyServer(SocketServer.StreamRequestHandler):
  4339. def handle(self):
  4340. logger.debug("DummyServer - start 31 sec wait")
  4341. time.sleep(31)
  4342. logger.debug("DummyServer - wait done")
  4343. logger.debug("Start WPS ER")
  4344. server,sock2 = wps_er_start(dev[0], DummyServer, max_age=40,
  4345. wait_m_search=True)
  4346. logger.debug("Start server to accept, but not complete, HTTP connection from WPS ER")
  4347. # This will wait for 31 seconds..
  4348. server.handle_request()
  4349. logger.debug("Complete HTTP connection with hostapd (that should have already closed the connection)")
  4350. try:
  4351. sock.send("ET / HTTP/1.1\r\n\r\n")
  4352. res = sock.recv(100)
  4353. sock.close()
  4354. except:
  4355. pass
  4356. def test_ap_wps_er_url_parse(dev, apdev):
  4357. """WPS ER and URL parsing special cases"""
  4358. try:
  4359. _test_ap_wps_er_url_parse(dev, apdev)
  4360. finally:
  4361. dev[0].request("WPS_ER_STOP")
  4362. def _test_ap_wps_er_url_parse(dev, apdev):
  4363. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  4364. sock.settimeout(1)
  4365. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  4366. sock.bind(("239.255.255.250", 1900))
  4367. dev[0].request("WPS_ER_START ifname=lo")
  4368. (msg,addr) = sock.recvfrom(1000)
  4369. logger.debug("Received SSDP message from %s: %s" % (str(addr), msg))
  4370. if "M-SEARCH" not in msg:
  4371. raise Exception("Not an M-SEARCH")
  4372. sock.sendto("HTTP/1.1 200 OK\r\nST: urn:schemas-wifialliance-org:device:WFADevice:1\r\nlocation:http://127.0.0.1\r\ncache-control:max-age=1\r\n\r\n", addr)
  4373. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"], timeout=2)
  4374. sock.sendto("HTTP/1.1 200 OK\r\nST: urn:schemas-wifialliance-org:device:WFADevice:1\r\nlocation:http://127.0.0.1/:foo\r\ncache-control:max-age=1\r\n\r\n", addr)
  4375. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"], timeout=2)
  4376. sock.sendto("HTTP/1.1 200 OK\r\nST: urn:schemas-wifialliance-org:device:WFADevice:1\r\nlocation:http://255.255.255.255:0/foo.xml\r\ncache-control:max-age=1\r\n\r\n", addr)
  4377. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"], timeout=2)
  4378. sock.close()
  4379. def test_ap_wps_er_link_update(dev, apdev):
  4380. """WPS ER and link update special cases"""
  4381. class WPSAPHTTPServer_link_update(WPSAPHTTPServer):
  4382. def handle_upnp_info(self):
  4383. self.wfile.write(gen_upnp_info(controlURL='/wps_control'))
  4384. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_link_update)
  4385. class WPSAPHTTPServer_link_update2(WPSAPHTTPServer):
  4386. def handle_others(self, data):
  4387. if "GET / " in data:
  4388. self.wfile.write(gen_upnp_info(controlURL='/wps_control'))
  4389. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_link_update2,
  4390. location_url='http://127.0.0.1:12345')
  4391. def test_ap_wps_er_http_client(dev, apdev):
  4392. """WPS ER and HTTP client special cases"""
  4393. with alloc_fail(dev[0], 1, "http_link_update"):
  4394. run_wps_er_proto_test(dev[0], WPSAPHTTPServer)
  4395. with alloc_fail(dev[0], 1, "wpabuf_alloc;http_client_url"):
  4396. run_wps_er_proto_test(dev[0], WPSAPHTTPServer, no_event_url=True)
  4397. with alloc_fail(dev[0], 1, "httpread_create;http_client_tx_ready"):
  4398. run_wps_er_proto_test(dev[0], WPSAPHTTPServer, no_event_url=True)
  4399. class WPSAPHTTPServer_req_as_resp(WPSAPHTTPServer):
  4400. def handle_upnp_info(self):
  4401. self.wfile.write("GET / HTTP/1.1\r\n\r\n")
  4402. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_req_as_resp,
  4403. no_event_url=True)
  4404. def test_ap_wps_init_oom(dev, apdev):
  4405. """wps_init OOM cases"""
  4406. ssid = "test-wps"
  4407. appin = "12345670"
  4408. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  4409. "ap_pin": appin }
  4410. hapd = hostapd.add_ap(apdev[0], params)
  4411. pin = dev[0].wps_read_pin()
  4412. with alloc_fail(hapd, 1, "wps_init"):
  4413. hapd.request("WPS_PIN any " + pin)
  4414. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4415. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4416. ev = hapd.wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4417. if ev is None:
  4418. raise Exception("No EAP failure reported")
  4419. dev[0].request("WPS_CANCEL")
  4420. with alloc_fail(dev[0], 2, "wps_init"):
  4421. hapd.request("WPS_PIN any " + pin)
  4422. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4423. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4424. ev = hapd.wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4425. if ev is None:
  4426. raise Exception("No EAP failure reported")
  4427. dev[0].request("WPS_CANCEL")
  4428. with alloc_fail(dev[0], 2, "wps_init"):
  4429. hapd.request("WPS_PBC")
  4430. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4431. dev[0].request("WPS_PBC %s" % (apdev[0]['bssid']))
  4432. ev = hapd.wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4433. if ev is None:
  4434. raise Exception("No EAP failure reported")
  4435. dev[0].request("WPS_CANCEL")
  4436. dev[0].dump_monitor()
  4437. new_ssid = "wps-new-ssid"
  4438. new_passphrase = "1234567890"
  4439. with alloc_fail(dev[0], 3, "wps_init"):
  4440. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
  4441. new_passphrase, no_wait=True)
  4442. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4443. if ev is None:
  4444. raise Exception("No EAP failure reported")
  4445. dev[0].flush_scan_cache()
  4446. @remote_compatible
  4447. def test_ap_wps_invalid_assoc_req_elem(dev, apdev):
  4448. """WPS and invalid IE in Association Request frame"""
  4449. ssid = "test-wps"
  4450. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2" }
  4451. hapd = hostapd.add_ap(apdev[0], params)
  4452. pin = "12345670"
  4453. hapd.request("WPS_PIN any " + pin)
  4454. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4455. try:
  4456. dev[0].request("VENDOR_ELEM_ADD 13 dd050050f20410")
  4457. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4458. for i in range(5):
  4459. ev = hapd.wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=10)
  4460. if ev and "vendor=14122" in ev:
  4461. break
  4462. if ev is None or "vendor=14122" not in ev:
  4463. raise Exception("EAP-WSC not started")
  4464. dev[0].request("WPS_CANCEL")
  4465. finally:
  4466. dev[0].request("VENDOR_ELEM_REMOVE 13 *")
  4467. def test_ap_wps_pbc_pin_mismatch(dev, apdev):
  4468. """WPS PBC/PIN mismatch"""
  4469. ssid = "test-wps"
  4470. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2" }
  4471. hapd = hostapd.add_ap(apdev[0], params)
  4472. hapd.request("SET wps_version_number 0x10")
  4473. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4474. hapd.request("WPS_PBC")
  4475. pin = dev[0].wps_read_pin()
  4476. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4477. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  4478. if ev is None:
  4479. raise Exception("Scan did not complete")
  4480. dev[0].request("WPS_CANCEL")
  4481. hapd.request("WPS_CANCEL")
  4482. dev[0].flush_scan_cache()
  4483. @remote_compatible
  4484. def test_ap_wps_ie_invalid(dev, apdev):
  4485. """WPS PIN attempt with AP that has invalid WSC IE"""
  4486. ssid = "test-wps"
  4487. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  4488. "vendor_elements": "dd050050f20410" }
  4489. hapd = hostapd.add_ap(apdev[0], params)
  4490. params = { 'ssid': "another", "vendor_elements": "dd050050f20410" }
  4491. hostapd.add_ap(apdev[1], params)
  4492. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4493. pin = dev[0].wps_read_pin()
  4494. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4495. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  4496. if ev is None:
  4497. raise Exception("Scan did not complete")
  4498. dev[0].request("WPS_CANCEL")
  4499. @remote_compatible
  4500. def test_ap_wps_scan_prio_order(dev, apdev):
  4501. """WPS scan priority ordering"""
  4502. ssid = "test-wps"
  4503. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2" }
  4504. hapd = hostapd.add_ap(apdev[0], params)
  4505. params = { 'ssid': "another", "vendor_elements": "dd050050f20410" }
  4506. hostapd.add_ap(apdev[1], params)
  4507. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4508. dev[0].scan_for_bss(apdev[1]['bssid'], freq="2412")
  4509. pin = dev[0].wps_read_pin()
  4510. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4511. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  4512. if ev is None:
  4513. raise Exception("Scan did not complete")
  4514. dev[0].request("WPS_CANCEL")
  4515. def test_ap_wps_probe_req_ie_oom(dev, apdev):
  4516. """WPS ProbeReq IE OOM"""
  4517. ssid = "test-wps"
  4518. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2" }
  4519. hapd = hostapd.add_ap(apdev[0], params)
  4520. pin = dev[0].wps_read_pin()
  4521. hapd.request("WPS_PIN any " + pin)
  4522. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4523. with alloc_fail(dev[0], 1, "wps_build_probe_req_ie"):
  4524. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4525. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=10)
  4526. if ev is None:
  4527. raise Exception("Association not seen")
  4528. dev[0].request("WPS_CANCEL")
  4529. dev[0].wait_disconnected()
  4530. with alloc_fail(dev[0], 1, "wps_ie_encapsulate"):
  4531. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4532. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=10)
  4533. if ev is None:
  4534. raise Exception("Association not seen")
  4535. dev[0].request("WPS_CANCEL")
  4536. hapd.disable()
  4537. dev[0].request("REMOVE_NETWORK all")
  4538. dev[0].wait_disconnected()
  4539. time.sleep(0.2)
  4540. dev[0].flush_scan_cache()
  4541. def test_ap_wps_assoc_req_ie_oom(dev, apdev):
  4542. """WPS AssocReq IE OOM"""
  4543. ssid = "test-wps"
  4544. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2" }
  4545. hapd = hostapd.add_ap(apdev[0], params)
  4546. pin = dev[0].wps_read_pin()
  4547. hapd.request("WPS_PIN any " + pin)
  4548. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4549. with alloc_fail(dev[0], 1, "wps_build_assoc_req_ie"):
  4550. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4551. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=10)
  4552. if ev is None:
  4553. raise Exception("Association not seen")
  4554. dev[0].request("WPS_CANCEL")
  4555. def test_ap_wps_assoc_resp_ie_oom(dev, apdev):
  4556. """WPS AssocResp IE OOM"""
  4557. ssid = "test-wps"
  4558. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2" }
  4559. hapd = hostapd.add_ap(apdev[0], params)
  4560. pin = dev[0].wps_read_pin()
  4561. hapd.request("WPS_PIN any " + pin)
  4562. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4563. with alloc_fail(hapd, 1, "wps_build_assoc_resp_ie"):
  4564. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4565. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=10)
  4566. if ev is None:
  4567. raise Exception("Association not seen")
  4568. dev[0].request("WPS_CANCEL")
  4569. @remote_compatible
  4570. def test_ap_wps_bss_info_errors(dev, apdev):
  4571. """WPS BSS info errors"""
  4572. params = { "ssid": "1",
  4573. "vendor_elements": "dd0e0050f20410440001ff101100010a" }
  4574. hostapd.add_ap(apdev[0], params)
  4575. params = { 'ssid': "2", "vendor_elements": "dd050050f20410" }
  4576. hostapd.add_ap(apdev[1], params)
  4577. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4578. dev[0].scan_for_bss(apdev[1]['bssid'], freq="2412")
  4579. bss = dev[0].get_bss(apdev[0]['bssid'])
  4580. logger.info("BSS: " + str(bss))
  4581. if "wps_state" in bss:
  4582. raise Exception("Unexpected wps_state in BSS info")
  4583. if 'wps_device_name' not in bss:
  4584. raise Exception("No wps_device_name in BSS info")
  4585. if bss['wps_device_name'] != '_':
  4586. raise Exception("Unexpected wps_device_name value")
  4587. bss = dev[0].get_bss(apdev[1]['bssid'])
  4588. logger.info("BSS: " + str(bss))
  4589. with alloc_fail(dev[0], 1, "=wps_attr_text"):
  4590. bss = dev[0].get_bss(apdev[0]['bssid'])
  4591. logger.info("BSS(OOM): " + str(bss))
  4592. def wps_run_pbc_fail_ap(apdev, dev, hapd):
  4593. hapd.request("WPS_PBC")
  4594. dev.scan_for_bss(apdev['bssid'], freq="2412")
  4595. dev.request("WPS_PBC " + apdev['bssid'])
  4596. ev = dev.wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4597. if ev is None:
  4598. raise Exception("No EAP failure reported")
  4599. dev.request("WPS_CANCEL")
  4600. dev.wait_disconnected()
  4601. for i in range(5):
  4602. try:
  4603. dev.flush_scan_cache()
  4604. break
  4605. except Exception, e:
  4606. if str(e).startswith("Failed to trigger scan"):
  4607. # Try again
  4608. time.sleep(1)
  4609. else:
  4610. raise
  4611. def wps_run_pbc_fail(apdev, dev):
  4612. hapd = wps_start_ap(apdev)
  4613. wps_run_pbc_fail_ap(apdev, dev, hapd)
  4614. @remote_compatible
  4615. def test_ap_wps_pk_oom(dev, apdev):
  4616. """WPS and public key OOM"""
  4617. with alloc_fail(dev[0], 1, "wps_build_public_key"):
  4618. wps_run_pbc_fail(apdev[0], dev[0])
  4619. @remote_compatible
  4620. def test_ap_wps_pk_oom_ap(dev, apdev):
  4621. """WPS and public key OOM on AP"""
  4622. hapd = wps_start_ap(apdev[0])
  4623. with alloc_fail(hapd, 1, "wps_build_public_key"):
  4624. wps_run_pbc_fail_ap(apdev[0], dev[0], hapd)
  4625. @remote_compatible
  4626. def test_ap_wps_encr_oom_ap(dev, apdev):
  4627. """WPS and encrypted settings decryption OOM on AP"""
  4628. hapd = wps_start_ap(apdev[0])
  4629. pin = dev[0].wps_read_pin()
  4630. hapd.request("WPS_PIN any " + pin)
  4631. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4632. with alloc_fail(hapd, 1, "wps_decrypt_encr_settings"):
  4633. dev[0].request("WPS_PIN " + apdev[0]['bssid'] + " " + pin)
  4634. ev = hapd.wait_event(["WPS-FAIL"], timeout=10)
  4635. if ev is None:
  4636. raise Exception("No WPS-FAIL reported")
  4637. dev[0].request("WPS_CANCEL")
  4638. dev[0].wait_disconnected()
  4639. @remote_compatible
  4640. def test_ap_wps_encr_no_random_ap(dev, apdev):
  4641. """WPS and no random data available for encryption on AP"""
  4642. hapd = wps_start_ap(apdev[0])
  4643. with fail_test(hapd, 1, "os_get_random;wps_build_encr_settings"):
  4644. wps_run_pbc_fail_ap(apdev[0], dev[0], hapd)
  4645. @remote_compatible
  4646. def test_ap_wps_e_hash_no_random_sta(dev, apdev):
  4647. """WPS and no random data available for e-hash on STA"""
  4648. with fail_test(dev[0], 1, "os_get_random;wps_build_e_hash"):
  4649. wps_run_pbc_fail(apdev[0], dev[0])
  4650. @remote_compatible
  4651. def test_ap_wps_m1_no_random(dev, apdev):
  4652. """WPS and no random for M1 on STA"""
  4653. with fail_test(dev[0], 1, "os_get_random;wps_build_m1"):
  4654. wps_run_pbc_fail(apdev[0], dev[0])
  4655. @remote_compatible
  4656. def test_ap_wps_m1_oom(dev, apdev):
  4657. """WPS and OOM for M1 on STA"""
  4658. with alloc_fail(dev[0], 1, "wps_build_m1"):
  4659. wps_run_pbc_fail(apdev[0], dev[0])
  4660. @remote_compatible
  4661. def test_ap_wps_m3_oom(dev, apdev):
  4662. """WPS and OOM for M3 on STA"""
  4663. with alloc_fail(dev[0], 1, "wps_build_m3"):
  4664. wps_run_pbc_fail(apdev[0], dev[0])
  4665. @remote_compatible
  4666. def test_ap_wps_m5_oom(dev, apdev):
  4667. """WPS and OOM for M5 on STA"""
  4668. hapd = wps_start_ap(apdev[0])
  4669. hapd.request("WPS_PBC")
  4670. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4671. for i in range(1, 3):
  4672. with alloc_fail(dev[0], i, "wps_build_m5"):
  4673. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  4674. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4675. if ev is None:
  4676. raise Exception("No EAP failure reported")
  4677. dev[0].request("WPS_CANCEL")
  4678. dev[0].wait_disconnected()
  4679. dev[0].flush_scan_cache()
  4680. @remote_compatible
  4681. def test_ap_wps_m5_no_random(dev, apdev):
  4682. """WPS and no random for M5 on STA"""
  4683. with fail_test(dev[0], 1,
  4684. "os_get_random;wps_build_encr_settings;wps_build_m5"):
  4685. wps_run_pbc_fail(apdev[0], dev[0])
  4686. @remote_compatible
  4687. def test_ap_wps_m7_oom(dev, apdev):
  4688. """WPS and OOM for M7 on STA"""
  4689. hapd = wps_start_ap(apdev[0])
  4690. hapd.request("WPS_PBC")
  4691. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4692. for i in range(1, 3):
  4693. with alloc_fail(dev[0], i, "wps_build_m7"):
  4694. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  4695. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4696. if ev is None:
  4697. raise Exception("No EAP failure reported")
  4698. dev[0].request("WPS_CANCEL")
  4699. dev[0].wait_disconnected()
  4700. dev[0].flush_scan_cache()
  4701. @remote_compatible
  4702. def test_ap_wps_m7_no_random(dev, apdev):
  4703. """WPS and no random for M7 on STA"""
  4704. with fail_test(dev[0], 1,
  4705. "os_get_random;wps_build_encr_settings;wps_build_m7"):
  4706. wps_run_pbc_fail(apdev[0], dev[0])
  4707. @remote_compatible
  4708. def test_ap_wps_wsc_done_oom(dev, apdev):
  4709. """WPS and OOM for WSC_Done on STA"""
  4710. with alloc_fail(dev[0], 1, "wps_build_wsc_done"):
  4711. wps_run_pbc_fail(apdev[0], dev[0])
  4712. def test_ap_wps_random_psk_fail(dev, apdev):
  4713. """WPS and no random for PSK on AP"""
  4714. ssid = "test-wps"
  4715. pskfile = "/tmp/ap_wps_per_enrollee_psk.psk_file"
  4716. appin = "12345670"
  4717. try:
  4718. os.remove(pskfile)
  4719. except:
  4720. pass
  4721. try:
  4722. with open(pskfile, "w") as f:
  4723. f.write("# WPA PSKs\n")
  4724. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  4725. "wpa": "2", "wpa_key_mgmt": "WPA-PSK",
  4726. "rsn_pairwise": "CCMP", "ap_pin": appin,
  4727. "wpa_psk_file": pskfile }
  4728. hapd = hostapd.add_ap(apdev[0], params)
  4729. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4730. with fail_test(hapd, 1, "os_get_random;wps_build_cred_network_key"):
  4731. dev[0].request("WPS_REG " + apdev[0]['bssid'] + " " + appin)
  4732. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4733. if ev is None:
  4734. raise Exception("No EAP failure reported")
  4735. dev[0].request("WPS_CANCEL")
  4736. dev[0].wait_disconnected()
  4737. with fail_test(hapd, 1, "os_get_random;wps_build_cred"):
  4738. wps_run_pbc_fail_ap(apdev[0], dev[0], hapd)
  4739. with alloc_fail(hapd, 1, "wps_build_cred"):
  4740. wps_run_pbc_fail_ap(apdev[0], dev[0], hapd)
  4741. with alloc_fail(hapd, 2, "wps_build_cred"):
  4742. wps_run_pbc_fail_ap(apdev[0], dev[0], hapd)
  4743. finally:
  4744. os.remove(pskfile)
  4745. def wps_ext_eap_identity_req(dev, hapd, bssid):
  4746. logger.debug("EAP-Identity/Request")
  4747. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  4748. if ev is None:
  4749. raise Exception("Timeout on EAPOL-TX from hostapd")
  4750. res = dev.request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  4751. if "OK" not in res:
  4752. raise Exception("EAPOL_RX to wpa_supplicant failed")
  4753. def wps_ext_eap_identity_resp(hapd, dev, addr):
  4754. ev = dev.wait_event(["EAPOL-TX"], timeout=10)
  4755. if ev is None:
  4756. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  4757. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  4758. if "OK" not in res:
  4759. raise Exception("EAPOL_RX to hostapd failed")
  4760. def wps_ext_eap_wsc(dst, src, src_addr, msg):
  4761. logger.debug(msg)
  4762. ev = src.wait_event(["EAPOL-TX"], timeout=10)
  4763. if ev is None:
  4764. raise Exception("Timeout on EAPOL-TX")
  4765. res = dst.request("EAPOL_RX " + src_addr + " " + ev.split(' ')[2])
  4766. if "OK" not in res:
  4767. raise Exception("EAPOL_RX failed")
  4768. def wps_start_ext(apdev, dev, pbc=False, pin=None):
  4769. addr = dev.own_addr()
  4770. bssid = apdev['bssid']
  4771. ssid = "test-wps-conf"
  4772. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  4773. "wpa_passphrase": "12345678", "wpa": "2",
  4774. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"}
  4775. hapd = hostapd.add_ap(apdev, params)
  4776. if pbc:
  4777. hapd.request("WPS_PBC")
  4778. else:
  4779. if pin is None:
  4780. pin = dev.wps_read_pin()
  4781. hapd.request("WPS_PIN any " + pin)
  4782. dev.scan_for_bss(bssid, freq="2412")
  4783. hapd.request("SET ext_eapol_frame_io 1")
  4784. dev.request("SET ext_eapol_frame_io 1")
  4785. if pbc:
  4786. dev.request("WPS_PBC " + bssid)
  4787. else:
  4788. dev.request("WPS_PIN " + bssid + " " + pin)
  4789. return addr,bssid,hapd
  4790. def wps_auth_corrupt(dst, src, addr):
  4791. ev = src.wait_event(["EAPOL-TX"], timeout=10)
  4792. if ev is None:
  4793. raise Exception("Timeout on EAPOL-TX")
  4794. src.request("SET ext_eapol_frame_io 0")
  4795. dst.request("SET ext_eapol_frame_io 0")
  4796. msg = ev.split(' ')[2]
  4797. if msg[-24:-16] != '10050008':
  4798. raise Exception("Could not find Authenticator attribute")
  4799. # Corrupt Authenticator value
  4800. msg = msg[:-1] + '%x' % ((int(msg[-1], 16) + 1) % 16)
  4801. res = dst.request("EAPOL_RX " + addr + " " + msg)
  4802. if "OK" not in res:
  4803. raise Exception("EAPOL_RX failed")
  4804. def wps_fail_finish(hapd, dev, fail_str):
  4805. ev = hapd.wait_event(["WPS-FAIL"], timeout=5)
  4806. if ev is None:
  4807. raise Exception("WPS-FAIL not indicated")
  4808. if fail_str not in ev:
  4809. raise Exception("Unexpected WPS-FAIL value: " + ev)
  4810. dev.request("WPS_CANCEL")
  4811. dev.wait_disconnected()
  4812. def wps_auth_corrupt_from_ap(dev, hapd, bssid, fail_str):
  4813. wps_auth_corrupt(dev, hapd, bssid)
  4814. wps_fail_finish(hapd, dev, fail_str)
  4815. def wps_auth_corrupt_to_ap(dev, hapd, addr, fail_str):
  4816. wps_auth_corrupt(hapd, dev, addr)
  4817. wps_fail_finish(hapd, dev, fail_str)
  4818. def test_ap_wps_authenticator_mismatch_m2(dev, apdev):
  4819. """WPS and Authenticator attribute mismatch in M2"""
  4820. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4821. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4822. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4823. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4824. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4825. logger.debug("M2")
  4826. wps_auth_corrupt_from_ap(dev[0], hapd, bssid, "msg=5")
  4827. def test_ap_wps_authenticator_mismatch_m3(dev, apdev):
  4828. """WPS and Authenticator attribute mismatch in M3"""
  4829. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4830. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4831. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4832. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4833. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4834. wps_ext_eap_wsc(dev[0], hapd, bssid, "M2")
  4835. logger.debug("M3")
  4836. wps_auth_corrupt_to_ap(dev[0], hapd, addr, "msg=7")
  4837. def test_ap_wps_authenticator_mismatch_m4(dev, apdev):
  4838. """WPS and Authenticator attribute mismatch in M4"""
  4839. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4840. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4841. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4842. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4843. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4844. wps_ext_eap_wsc(dev[0], hapd, bssid, "M2")
  4845. wps_ext_eap_wsc(hapd, dev[0], addr, "M3")
  4846. logger.debug("M4")
  4847. wps_auth_corrupt_from_ap(dev[0], hapd, bssid, "msg=8")
  4848. def test_ap_wps_authenticator_mismatch_m5(dev, apdev):
  4849. """WPS and Authenticator attribute mismatch in M5"""
  4850. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4851. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4852. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4853. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4854. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4855. wps_ext_eap_wsc(dev[0], hapd, bssid, "M2")
  4856. wps_ext_eap_wsc(hapd, dev[0], addr, "M3")
  4857. wps_ext_eap_wsc(dev[0], hapd, bssid, "M4")
  4858. logger.debug("M5")
  4859. wps_auth_corrupt_to_ap(dev[0], hapd, addr, "msg=9")
  4860. def test_ap_wps_authenticator_mismatch_m6(dev, apdev):
  4861. """WPS and Authenticator attribute mismatch in M6"""
  4862. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4863. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4864. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4865. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4866. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4867. wps_ext_eap_wsc(dev[0], hapd, bssid, "M2")
  4868. wps_ext_eap_wsc(hapd, dev[0], addr, "M3")
  4869. wps_ext_eap_wsc(dev[0], hapd, bssid, "M4")
  4870. wps_ext_eap_wsc(hapd, dev[0], addr, "M5")
  4871. logger.debug("M6")
  4872. wps_auth_corrupt_from_ap(dev[0], hapd, bssid, "msg=10")
  4873. def test_ap_wps_authenticator_mismatch_m7(dev, apdev):
  4874. """WPS and Authenticator attribute mismatch in M7"""
  4875. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4876. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4877. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4878. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4879. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4880. wps_ext_eap_wsc(dev[0], hapd, bssid, "M2")
  4881. wps_ext_eap_wsc(hapd, dev[0], addr, "M3")
  4882. wps_ext_eap_wsc(dev[0], hapd, bssid, "M4")
  4883. wps_ext_eap_wsc(hapd, dev[0], addr, "M5")
  4884. wps_ext_eap_wsc(dev[0], hapd, bssid, "M6")
  4885. logger.debug("M7")
  4886. wps_auth_corrupt_to_ap(dev[0], hapd, addr, "msg=11")
  4887. def test_ap_wps_authenticator_mismatch_m8(dev, apdev):
  4888. """WPS and Authenticator attribute mismatch in M8"""
  4889. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4890. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4891. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4892. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4893. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4894. wps_ext_eap_wsc(dev[0], hapd, bssid, "M2")
  4895. wps_ext_eap_wsc(hapd, dev[0], addr, "M3")
  4896. wps_ext_eap_wsc(dev[0], hapd, bssid, "M4")
  4897. wps_ext_eap_wsc(hapd, dev[0], addr, "M5")
  4898. wps_ext_eap_wsc(dev[0], hapd, bssid, "M6")
  4899. wps_ext_eap_wsc(hapd, dev[0], addr, "M7")
  4900. logger.debug("M8")
  4901. wps_auth_corrupt_from_ap(dev[0], hapd, bssid, "msg=12")
  4902. def test_ap_wps_authenticator_missing_m2(dev, apdev):
  4903. """WPS and Authenticator attribute missing from M2"""
  4904. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4905. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4906. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4907. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4908. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4909. logger.debug("M2")
  4910. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  4911. if ev is None:
  4912. raise Exception("Timeout on EAPOL-TX")
  4913. hapd.request("SET ext_eapol_frame_io 0")
  4914. dev[0].request("SET ext_eapol_frame_io 0")
  4915. msg = ev.split(' ')[2]
  4916. if msg[-24:-16] != '10050008':
  4917. raise Exception("Could not find Authenticator attribute")
  4918. # Remove Authenticator value
  4919. msg = msg[:-24]
  4920. mlen = "%04x" % (int(msg[4:8], 16) - 12)
  4921. msg = msg[0:4] + mlen + msg[8:12] + mlen + msg[16:]
  4922. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  4923. if "OK" not in res:
  4924. raise Exception("EAPOL_RX failed")
  4925. wps_fail_finish(hapd, dev[0], "msg=5")
  4926. def test_ap_wps_m2_dev_passwd_id_p2p(dev, apdev):
  4927. """WPS and M2 with different Device Password ID (P2P)"""
  4928. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4929. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4930. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4931. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4932. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4933. logger.debug("M2")
  4934. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  4935. if ev is None:
  4936. raise Exception("Timeout on EAPOL-TX")
  4937. hapd.request("SET ext_eapol_frame_io 0")
  4938. dev[0].request("SET ext_eapol_frame_io 0")
  4939. msg = ev.split(' ')[2]
  4940. if msg[722:730] != '10120002':
  4941. raise Exception("Could not find Device Password ID attribute")
  4942. # Replace Device Password ID value. This will fail Authenticator check, but
  4943. # allows the code path in wps_process_dev_pw_id() to be checked from debug
  4944. # log.
  4945. msg = msg[0:730] + "0005" + msg[734:]
  4946. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  4947. if "OK" not in res:
  4948. raise Exception("EAPOL_RX failed")
  4949. wps_fail_finish(hapd, dev[0], "msg=5")
  4950. def test_ap_wps_m2_dev_passwd_id_change_pin_to_pbc(dev, apdev):
  4951. """WPS and M2 with different Device Password ID (PIN to PBC)"""
  4952. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4953. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4954. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4955. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4956. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4957. logger.debug("M2")
  4958. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  4959. if ev is None:
  4960. raise Exception("Timeout on EAPOL-TX")
  4961. hapd.request("SET ext_eapol_frame_io 0")
  4962. dev[0].request("SET ext_eapol_frame_io 0")
  4963. msg = ev.split(' ')[2]
  4964. if msg[722:730] != '10120002':
  4965. raise Exception("Could not find Device Password ID attribute")
  4966. # Replace Device Password ID value (PIN --> PBC). This will be rejected.
  4967. msg = msg[0:730] + "0004" + msg[734:]
  4968. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  4969. if "OK" not in res:
  4970. raise Exception("EAPOL_RX failed")
  4971. wps_fail_finish(hapd, dev[0], "msg=5")
  4972. def test_ap_wps_m2_dev_passwd_id_change_pbc_to_pin(dev, apdev):
  4973. """WPS and M2 with different Device Password ID (PBC to PIN)"""
  4974. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  4975. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4976. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4977. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4978. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4979. logger.debug("M2")
  4980. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  4981. if ev is None:
  4982. raise Exception("Timeout on EAPOL-TX")
  4983. hapd.request("SET ext_eapol_frame_io 0")
  4984. dev[0].request("SET ext_eapol_frame_io 0")
  4985. msg = ev.split(' ')[2]
  4986. if msg[722:730] != '10120002':
  4987. raise Exception("Could not find Device Password ID attribute")
  4988. # Replace Device Password ID value. This will fail Authenticator check, but
  4989. # allows the code path in wps_process_dev_pw_id() to be checked from debug
  4990. # log.
  4991. msg = msg[0:730] + "0000" + msg[734:]
  4992. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  4993. if "OK" not in res:
  4994. raise Exception("EAPOL_RX failed")
  4995. wps_fail_finish(hapd, dev[0], "msg=5")
  4996. dev[0].flush_scan_cache()
  4997. def test_ap_wps_m2_missing_dev_passwd_id(dev, apdev):
  4998. """WPS and M2 without Device Password ID"""
  4999. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  5000. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5001. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5002. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5003. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5004. logger.debug("M2")
  5005. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5006. if ev is None:
  5007. raise Exception("Timeout on EAPOL-TX")
  5008. hapd.request("SET ext_eapol_frame_io 0")
  5009. dev[0].request("SET ext_eapol_frame_io 0")
  5010. msg = ev.split(' ')[2]
  5011. if msg[722:730] != '10120002':
  5012. raise Exception("Could not find Device Password ID attribute")
  5013. # Remove Device Password ID value. This will fail Authenticator check, but
  5014. # allows the code path in wps_process_dev_pw_id() to be checked from debug
  5015. # log.
  5016. mlen = "%04x" % (int(msg[4:8], 16) - 6)
  5017. msg = msg[0:4] + mlen + msg[8:12] + mlen + msg[16:722] + msg[734:]
  5018. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5019. if "OK" not in res:
  5020. raise Exception("EAPOL_RX failed")
  5021. wps_fail_finish(hapd, dev[0], "msg=5")
  5022. def test_ap_wps_m2_missing_registrar_nonce(dev, apdev):
  5023. """WPS and M2 without Registrar Nonce"""
  5024. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5025. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5026. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5027. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5028. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5029. logger.debug("M2")
  5030. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5031. if ev is None:
  5032. raise Exception("Timeout on EAPOL-TX")
  5033. hapd.request("SET ext_eapol_frame_io 0")
  5034. dev[0].request("SET ext_eapol_frame_io 0")
  5035. msg = ev.split(' ')[2]
  5036. if msg[96:104] != '10390010':
  5037. raise Exception("Could not find Registrar Nonce attribute")
  5038. # Remove Registrar Nonce. This will fail Authenticator check, but
  5039. # allows the code path in wps_process_registrar_nonce() to be checked from
  5040. # the debug log.
  5041. mlen = "%04x" % (int(msg[4:8], 16) - 20)
  5042. msg = msg[0:4] + mlen + msg[8:12] + mlen + msg[16:96] + msg[136:]
  5043. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5044. if "OK" not in res:
  5045. raise Exception("EAPOL_RX failed")
  5046. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECT"], timeout=5)
  5047. if ev is None:
  5048. raise Exception("Disconnect event not seen")
  5049. dev[0].request("WPS_CANCEL")
  5050. dev[0].flush_scan_cache()
  5051. def test_ap_wps_m2_missing_enrollee_nonce(dev, apdev):
  5052. """WPS and M2 without Enrollee Nonce"""
  5053. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5054. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5055. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5056. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5057. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5058. logger.debug("M2")
  5059. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5060. if ev is None:
  5061. raise Exception("Timeout on EAPOL-TX")
  5062. hapd.request("SET ext_eapol_frame_io 0")
  5063. dev[0].request("SET ext_eapol_frame_io 0")
  5064. msg = ev.split(' ')[2]
  5065. if msg[56:64] != '101a0010':
  5066. raise Exception("Could not find enrollee Nonce attribute")
  5067. # Remove Enrollee Nonce. This will fail Authenticator check, but
  5068. # allows the code path in wps_process_enrollee_nonce() to be checked from
  5069. # the debug log.
  5070. mlen = "%04x" % (int(msg[4:8], 16) - 20)
  5071. msg = msg[0:4] + mlen + msg[8:12] + mlen + msg[16:56] + msg[96:]
  5072. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5073. if "OK" not in res:
  5074. raise Exception("EAPOL_RX failed")
  5075. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECT"], timeout=5)
  5076. if ev is None:
  5077. raise Exception("Disconnect event not seen")
  5078. dev[0].request("WPS_CANCEL")
  5079. dev[0].flush_scan_cache()
  5080. def test_ap_wps_m2_missing_uuid_r(dev, apdev):
  5081. """WPS and M2 without UUID-R"""
  5082. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5083. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5084. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5085. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5086. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5087. logger.debug("M2")
  5088. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5089. if ev is None:
  5090. raise Exception("Timeout on EAPOL-TX")
  5091. hapd.request("SET ext_eapol_frame_io 0")
  5092. dev[0].request("SET ext_eapol_frame_io 0")
  5093. msg = ev.split(' ')[2]
  5094. if msg[136:144] != '10480010':
  5095. raise Exception("Could not find enrollee Nonce attribute")
  5096. # Remove UUID-R. This will fail Authenticator check, but allows the code
  5097. # path in wps_process_uuid_r() to be checked from the debug log.
  5098. mlen = "%04x" % (int(msg[4:8], 16) - 20)
  5099. msg = msg[0:4] + mlen + msg[8:12] + mlen + msg[16:136] + msg[176:]
  5100. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5101. if "OK" not in res:
  5102. raise Exception("EAPOL_RX failed")
  5103. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECT"], timeout=5)
  5104. if ev is None:
  5105. raise Exception("Disconnect event not seen")
  5106. dev[0].request("WPS_CANCEL")
  5107. dev[0].flush_scan_cache()
  5108. def test_ap_wps_m2_invalid(dev, apdev):
  5109. """WPS and M2 parsing failure"""
  5110. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5111. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5112. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5113. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5114. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5115. logger.debug("M2")
  5116. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5117. if ev is None:
  5118. raise Exception("Timeout on EAPOL-TX")
  5119. hapd.request("SET ext_eapol_frame_io 0")
  5120. dev[0].request("SET ext_eapol_frame_io 0")
  5121. msg = ev.split(' ')[2]
  5122. if msg[136:144] != '10480010':
  5123. raise Exception("Could not find enrollee Nonce attribute")
  5124. # Remove UUID-R. This will fail Authenticator check, but allows the code
  5125. # path in wps_process_uuid_r() to be checked from the debug log.
  5126. mlen = "%04x" % (int(msg[4:8], 16) - 1)
  5127. msg = msg[0:4] + mlen + msg[8:12] + mlen + msg[16:-2]
  5128. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5129. if "OK" not in res:
  5130. raise Exception("EAPOL_RX failed")
  5131. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECT"], timeout=5)
  5132. if ev is None:
  5133. raise Exception("Disconnect event not seen")
  5134. dev[0].request("WPS_CANCEL")
  5135. dev[0].flush_scan_cache()
  5136. def test_ap_wps_m2_missing_msg_type(dev, apdev):
  5137. """WPS and M2 without Message Type"""
  5138. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5139. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5140. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5141. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5142. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5143. logger.debug("M2")
  5144. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5145. if ev is None:
  5146. raise Exception("Timeout on EAPOL-TX")
  5147. hapd.request("SET ext_eapol_frame_io 0")
  5148. dev[0].request("SET ext_eapol_frame_io 0")
  5149. msg = ev.split(' ')[2]
  5150. if msg[46:54] != '10220001':
  5151. raise Exception("Could not find Message Type attribute")
  5152. # Remove Message Type. This will fail Authenticator check, but allows the
  5153. # code path in wps_process_wsc_msg() to be checked from the debug log.
  5154. mlen = "%04x" % (int(msg[4:8], 16) - 5)
  5155. msg = msg[0:4] + mlen + msg[8:12] + mlen + msg[16:46] + msg[56:]
  5156. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5157. if "OK" not in res:
  5158. raise Exception("EAPOL_RX failed")
  5159. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECT"], timeout=5)
  5160. if ev is None:
  5161. raise Exception("Disconnect event not seen")
  5162. dev[0].request("WPS_CANCEL")
  5163. dev[0].flush_scan_cache()
  5164. def test_ap_wps_m2_unknown_msg_type(dev, apdev):
  5165. """WPS and M2 but unknown Message Type"""
  5166. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5167. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5168. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5169. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5170. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5171. logger.debug("M2")
  5172. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5173. if ev is None:
  5174. raise Exception("Timeout on EAPOL-TX")
  5175. hapd.request("SET ext_eapol_frame_io 0")
  5176. dev[0].request("SET ext_eapol_frame_io 0")
  5177. msg = ev.split(' ')[2]
  5178. if msg[46:54] != '10220001':
  5179. raise Exception("Could not find Message Type attribute")
  5180. # Replace Message Type value. This will be rejected.
  5181. msg = msg[0:54] + "00" + msg[56:]
  5182. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5183. if "OK" not in res:
  5184. raise Exception("EAPOL_RX failed")
  5185. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECT"], timeout=5)
  5186. if ev is None:
  5187. raise Exception("Disconnect event not seen")
  5188. dev[0].request("WPS_CANCEL")
  5189. dev[0].flush_scan_cache()
  5190. def test_ap_wps_m2_unknown_opcode(dev, apdev):
  5191. """WPS and M2 but unknown opcode"""
  5192. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5193. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5194. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5195. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5196. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5197. logger.debug("M2")
  5198. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5199. if ev is None:
  5200. raise Exception("Timeout on EAPOL-TX")
  5201. hapd.request("SET ext_eapol_frame_io 0")
  5202. dev[0].request("SET ext_eapol_frame_io 0")
  5203. msg = ev.split(' ')[2]
  5204. # Replace opcode. This will be discarded in EAP-WSC processing.
  5205. msg = msg[0:32] + "00" + msg[34:]
  5206. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5207. if "OK" not in res:
  5208. raise Exception("EAPOL_RX failed")
  5209. dev[0].request("WPS_CANCEL")
  5210. dev[0].wait_disconnected()
  5211. dev[0].flush_scan_cache()
  5212. def test_ap_wps_m2_unknown_opcode2(dev, apdev):
  5213. """WPS and M2 but unknown opcode (WSC_Start)"""
  5214. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5215. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5216. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5217. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5218. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5219. logger.debug("M2")
  5220. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5221. if ev is None:
  5222. raise Exception("Timeout on EAPOL-TX")
  5223. hapd.request("SET ext_eapol_frame_io 0")
  5224. dev[0].request("SET ext_eapol_frame_io 0")
  5225. msg = ev.split(' ')[2]
  5226. # Replace opcode. This will be discarded in EAP-WSC processing.
  5227. msg = msg[0:32] + "01" + msg[34:]
  5228. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5229. if "OK" not in res:
  5230. raise Exception("EAPOL_RX failed")
  5231. dev[0].request("WPS_CANCEL")
  5232. dev[0].wait_disconnected()
  5233. dev[0].flush_scan_cache()
  5234. def test_ap_wps_m2_unknown_opcode3(dev, apdev):
  5235. """WPS and M2 but unknown opcode (WSC_Done)"""
  5236. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5237. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5238. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5239. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5240. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5241. logger.debug("M2")
  5242. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5243. if ev is None:
  5244. raise Exception("Timeout on EAPOL-TX")
  5245. hapd.request("SET ext_eapol_frame_io 0")
  5246. dev[0].request("SET ext_eapol_frame_io 0")
  5247. msg = ev.split(' ')[2]
  5248. # Replace opcode. This will be discarded in WPS Enrollee processing.
  5249. msg = msg[0:32] + "05" + msg[34:]
  5250. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5251. if "OK" not in res:
  5252. raise Exception("EAPOL_RX failed")
  5253. dev[0].request("WPS_CANCEL")
  5254. dev[0].wait_disconnected()
  5255. dev[0].flush_scan_cache()
  5256. def wps_m2_but_other(dev, apdev, title, msgtype):
  5257. addr,bssid,hapd = wps_start_ext(apdev, dev)
  5258. wps_ext_eap_identity_req(dev, hapd, bssid)
  5259. wps_ext_eap_identity_resp(hapd, dev, addr)
  5260. wps_ext_eap_wsc(dev, hapd, bssid, "EAP-WSC/Start")
  5261. wps_ext_eap_wsc(hapd, dev, addr, "M1")
  5262. logger.debug(title)
  5263. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5264. if ev is None:
  5265. raise Exception("Timeout on EAPOL-TX")
  5266. hapd.request("SET ext_eapol_frame_io 0")
  5267. dev.request("SET ext_eapol_frame_io 0")
  5268. msg = ev.split(' ')[2]
  5269. if msg[46:54] != '10220001':
  5270. raise Exception("Could not find Message Type attribute")
  5271. # Replace Message Type value. This will be rejected.
  5272. msg = msg[0:54] + msgtype + msg[56:]
  5273. res = dev.request("EAPOL_RX " + bssid + " " + msg)
  5274. if "OK" not in res:
  5275. raise Exception("EAPOL_RX failed")
  5276. ev = dev.wait_event(["WPS-FAIL"], timeout=5)
  5277. if ev is None:
  5278. raise Exception("WPS-FAIL event not seen")
  5279. dev.request("WPS_CANCEL")
  5280. dev.wait_disconnected()
  5281. def wps_m4_but_other(dev, apdev, title, msgtype):
  5282. addr,bssid,hapd = wps_start_ext(apdev, dev)
  5283. wps_ext_eap_identity_req(dev, hapd, bssid)
  5284. wps_ext_eap_identity_resp(hapd, dev, addr)
  5285. wps_ext_eap_wsc(dev, hapd, bssid, "EAP-WSC/Start")
  5286. wps_ext_eap_wsc(hapd, dev, addr, "M1")
  5287. wps_ext_eap_wsc(dev, hapd, bssid, "M2")
  5288. wps_ext_eap_wsc(hapd, dev, addr, "M3")
  5289. logger.debug(title)
  5290. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5291. if ev is None:
  5292. raise Exception("Timeout on EAPOL-TX")
  5293. hapd.request("SET ext_eapol_frame_io 0")
  5294. dev.request("SET ext_eapol_frame_io 0")
  5295. msg = ev.split(' ')[2]
  5296. if msg[46:54] != '10220001':
  5297. raise Exception("Could not find Message Type attribute")
  5298. # Replace Message Type value. This will be rejected.
  5299. msg = msg[0:54] + msgtype + msg[56:]
  5300. res = dev.request("EAPOL_RX " + bssid + " " + msg)
  5301. if "OK" not in res:
  5302. raise Exception("EAPOL_RX failed")
  5303. ev = hapd.wait_event(["WPS-FAIL"], timeout=5)
  5304. if ev is None:
  5305. raise Exception("WPS-FAIL event not seen")
  5306. dev.request("WPS_CANCEL")
  5307. dev.wait_disconnected()
  5308. def test_ap_wps_m2_msg_type_m4(dev, apdev):
  5309. """WPS and M2 but Message Type M4"""
  5310. wps_m2_but_other(dev[0], apdev[0], "M2/M4", "08")
  5311. def test_ap_wps_m2_msg_type_m6(dev, apdev):
  5312. """WPS and M2 but Message Type M6"""
  5313. wps_m2_but_other(dev[0], apdev[0], "M2/M6", "0a")
  5314. def test_ap_wps_m2_msg_type_m8(dev, apdev):
  5315. """WPS and M2 but Message Type M8"""
  5316. wps_m2_but_other(dev[0], apdev[0], "M2/M8", "0c")
  5317. def test_ap_wps_m4_msg_type_m2(dev, apdev):
  5318. """WPS and M4 but Message Type M2"""
  5319. wps_m4_but_other(dev[0], apdev[0], "M4/M2", "05")
  5320. def test_ap_wps_m4_msg_type_m2d(dev, apdev):
  5321. """WPS and M4 but Message Type M2D"""
  5322. wps_m4_but_other(dev[0], apdev[0], "M4/M2D", "06")
  5323. @remote_compatible
  5324. def test_ap_wps_config_methods(dev, apdev):
  5325. """WPS configuration method parsing"""
  5326. ssid = "test-wps-conf"
  5327. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  5328. "wpa_passphrase": "12345678", "wpa": "2",
  5329. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  5330. "config_methods": "ethernet display ext_nfc_token int_nfc_token physical_display physical_push_button" }
  5331. hapd = hostapd.add_ap(apdev[0], params)
  5332. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  5333. "wpa_passphrase": "12345678", "wpa": "2",
  5334. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  5335. "config_methods": "display push_button" }
  5336. hapd2 = hostapd.add_ap(apdev[1], params)
  5337. def test_ap_wps_set_selected_registrar_proto(dev, apdev):
  5338. """WPS UPnP SetSelectedRegistrar protocol testing"""
  5339. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  5340. hapd = add_ssdp_ap(apdev[0], ap_uuid)
  5341. location = ssdp_get_location(ap_uuid)
  5342. urls = upnp_get_urls(location)
  5343. eventurl = urlparse.urlparse(urls['event_sub_url'])
  5344. ctrlurl = urlparse.urlparse(urls['control_url'])
  5345. url = urlparse.urlparse(location)
  5346. conn = httplib.HTTPConnection(url.netloc)
  5347. class WPSERHTTPServer(SocketServer.StreamRequestHandler):
  5348. def handle(self):
  5349. data = self.rfile.readline().strip()
  5350. logger.debug(data)
  5351. self.wfile.write(gen_wps_event())
  5352. server = MyTCPServer(("127.0.0.1", 12345), WPSERHTTPServer)
  5353. server.timeout = 1
  5354. headers = { "callback": '<http://127.0.0.1:12345/event>',
  5355. "NT": "upnp:event",
  5356. "timeout": "Second-1234" }
  5357. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  5358. resp = conn.getresponse()
  5359. if resp.status != 200:
  5360. raise Exception("Unexpected HTTP response: %d" % resp.status)
  5361. sid = resp.getheader("sid")
  5362. logger.debug("Subscription SID " + sid)
  5363. server.handle_request()
  5364. tests = [ (500, "10"),
  5365. (200, "104a000110" + "1041000101" + "101200020000" +
  5366. "105300023148" +
  5367. "1049002c00372a0001200124111111111111222222222222333333333333444444444444555555555555666666666666" +
  5368. "10480010362db47ba53a519188fb5458b986b2e4"),
  5369. (200, "104a000110" + "1041000100" + "101200020000" +
  5370. "105300020000"),
  5371. (200, "104a000110" + "1041000100"),
  5372. (200, "104a000110") ]
  5373. for status,test in tests:
  5374. tlvs = binascii.unhexlify(test)
  5375. newmsg = base64.b64encode(tlvs)
  5376. msg = '<?xml version="1.0"?>\n'
  5377. msg += '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  5378. msg += '<s:Body>'
  5379. msg += '<u:SetSelectedRegistrar xmlns:u="urn:schemas-wifialliance-org:service:WFAWLANConfig:1">'
  5380. msg += '<NewMessage>'
  5381. msg += newmsg
  5382. msg += "</NewMessage></u:SetSelectedRegistrar></s:Body></s:Envelope>"
  5383. headers = { "Content-type": 'text/xml; charset="utf-8"' }
  5384. headers["SOAPAction"] = '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#%s"' % "SetSelectedRegistrar"
  5385. conn.request("POST", ctrlurl.path, msg, headers)
  5386. resp = conn.getresponse()
  5387. if resp.status != status:
  5388. raise Exception("Unexpected HTTP response: %d (expected %d)" % (resp.status, status))
  5389. def test_ap_wps_adv_oom(dev, apdev):
  5390. """WPS AP and advertisement OOM"""
  5391. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  5392. hapd = add_ssdp_ap(apdev[0], ap_uuid)
  5393. with alloc_fail(hapd, 1, "=msearchreply_state_machine_start"):
  5394. ssdp_send_msearch("urn:schemas-wifialliance-org:service:WFAWLANConfig:1",
  5395. no_recv=True)
  5396. time.sleep(0.2)
  5397. with alloc_fail(hapd, 1, "eloop_register_timeout;msearchreply_state_machine_start"):
  5398. ssdp_send_msearch("urn:schemas-wifialliance-org:service:WFAWLANConfig:1",
  5399. no_recv=True)
  5400. time.sleep(0.2)
  5401. with alloc_fail(hapd, 1,
  5402. "next_advertisement;advertisement_state_machine_stop"):
  5403. hapd.disable()
  5404. with alloc_fail(hapd, 1, "ssdp_listener_start"):
  5405. if "FAIL" not in hapd.request("ENABLE"):
  5406. raise Exception("ENABLE succeeded during OOM")
  5407. def test_wps_config_methods(dev):
  5408. """WPS config method update"""
  5409. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  5410. wpas.interface_add("wlan5")
  5411. if "OK" not in wpas.request("SET config_methods display label"):
  5412. raise Exception("Failed to set config_methods")
  5413. if wpas.request("GET config_methods").strip() != "display label":
  5414. raise Exception("config_methods were not updated")
  5415. if "OK" not in wpas.request("SET config_methods "):
  5416. raise Exception("Failed to clear config_methods")
  5417. if wpas.request("GET config_methods").strip() != "":
  5418. raise Exception("config_methods were not cleared")
  5419. WPS_VENDOR_ID_WFA = 14122
  5420. WPS_VENDOR_TYPE = 1
  5421. # EAP-WSC Op-Code values
  5422. WSC_Start = 0x01
  5423. WSC_ACK = 0x02
  5424. WSC_NACK = 0x03
  5425. WSC_MSG = 0x04
  5426. WSC_Done = 0x05
  5427. WSC_FRAG_ACK = 0x06
  5428. ATTR_AP_CHANNEL = 0x1001
  5429. ATTR_ASSOC_STATE = 0x1002
  5430. ATTR_AUTH_TYPE = 0x1003
  5431. ATTR_AUTH_TYPE_FLAGS = 0x1004
  5432. ATTR_AUTHENTICATOR = 0x1005
  5433. ATTR_CONFIG_METHODS = 0x1008
  5434. ATTR_CONFIG_ERROR = 0x1009
  5435. ATTR_CONFIRM_URL4 = 0x100a
  5436. ATTR_CONFIRM_URL6 = 0x100b
  5437. ATTR_CONN_TYPE = 0x100c
  5438. ATTR_CONN_TYPE_FLAGS = 0x100d
  5439. ATTR_CRED = 0x100e
  5440. ATTR_ENCR_TYPE = 0x100f
  5441. ATTR_ENCR_TYPE_FLAGS = 0x1010
  5442. ATTR_DEV_NAME = 0x1011
  5443. ATTR_DEV_PASSWORD_ID = 0x1012
  5444. ATTR_E_HASH1 = 0x1014
  5445. ATTR_E_HASH2 = 0x1015
  5446. ATTR_E_SNONCE1 = 0x1016
  5447. ATTR_E_SNONCE2 = 0x1017
  5448. ATTR_ENCR_SETTINGS = 0x1018
  5449. ATTR_ENROLLEE_NONCE = 0x101a
  5450. ATTR_FEATURE_ID = 0x101b
  5451. ATTR_IDENTITY = 0x101c
  5452. ATTR_IDENTITY_PROOF = 0x101d
  5453. ATTR_KEY_WRAP_AUTH = 0x101e
  5454. ATTR_KEY_ID = 0x101f
  5455. ATTR_MAC_ADDR = 0x1020
  5456. ATTR_MANUFACTURER = 0x1021
  5457. ATTR_MSG_TYPE = 0x1022
  5458. ATTR_MODEL_NAME = 0x1023
  5459. ATTR_MODEL_NUMBER = 0x1024
  5460. ATTR_NETWORK_INDEX = 0x1026
  5461. ATTR_NETWORK_KEY = 0x1027
  5462. ATTR_NETWORK_KEY_INDEX = 0x1028
  5463. ATTR_NEW_DEVICE_NAME = 0x1029
  5464. ATTR_NEW_PASSWORD = 0x102a
  5465. ATTR_OOB_DEVICE_PASSWORD = 0x102c
  5466. ATTR_OS_VERSION = 0x102d
  5467. ATTR_POWER_LEVEL = 0x102f
  5468. ATTR_PSK_CURRENT = 0x1030
  5469. ATTR_PSK_MAX = 0x1031
  5470. ATTR_PUBLIC_KEY = 0x1032
  5471. ATTR_RADIO_ENABLE = 0x1033
  5472. ATTR_REBOOT = 0x1034
  5473. ATTR_REGISTRAR_CURRENT = 0x1035
  5474. ATTR_REGISTRAR_ESTABLISHED = 0x1036
  5475. ATTR_REGISTRAR_LIST = 0x1037
  5476. ATTR_REGISTRAR_MAX = 0x1038
  5477. ATTR_REGISTRAR_NONCE = 0x1039
  5478. ATTR_REQUEST_TYPE = 0x103a
  5479. ATTR_RESPONSE_TYPE = 0x103b
  5480. ATTR_RF_BANDS = 0x103c
  5481. ATTR_R_HASH1 = 0x103d
  5482. ATTR_R_HASH2 = 0x103e
  5483. ATTR_R_SNONCE1 = 0x103f
  5484. ATTR_R_SNONCE2 = 0x1040
  5485. ATTR_SELECTED_REGISTRAR = 0x1041
  5486. ATTR_SERIAL_NUMBER = 0x1042
  5487. ATTR_WPS_STATE = 0x1044
  5488. ATTR_SSID = 0x1045
  5489. ATTR_TOTAL_NETWORKS = 0x1046
  5490. ATTR_UUID_E = 0x1047
  5491. ATTR_UUID_R = 0x1048
  5492. ATTR_VENDOR_EXT = 0x1049
  5493. ATTR_VERSION = 0x104a
  5494. ATTR_X509_CERT_REQ = 0x104b
  5495. ATTR_X509_CERT = 0x104c
  5496. ATTR_EAP_IDENTITY = 0x104d
  5497. ATTR_MSG_COUNTER = 0x104e
  5498. ATTR_PUBKEY_HASH = 0x104f
  5499. ATTR_REKEY_KEY = 0x1050
  5500. ATTR_KEY_LIFETIME = 0x1051
  5501. ATTR_PERMITTED_CFG_METHODS = 0x1052
  5502. ATTR_SELECTED_REGISTRAR_CONFIG_METHODS = 0x1053
  5503. ATTR_PRIMARY_DEV_TYPE = 0x1054
  5504. ATTR_SECONDARY_DEV_TYPE_LIST = 0x1055
  5505. ATTR_PORTABLE_DEV = 0x1056
  5506. ATTR_AP_SETUP_LOCKED = 0x1057
  5507. ATTR_APPLICATION_EXT = 0x1058
  5508. ATTR_EAP_TYPE = 0x1059
  5509. ATTR_IV = 0x1060
  5510. ATTR_KEY_PROVIDED_AUTO = 0x1061
  5511. ATTR_802_1X_ENABLED = 0x1062
  5512. ATTR_APPSESSIONKEY = 0x1063
  5513. ATTR_WEPTRANSMITKEY = 0x1064
  5514. ATTR_REQUESTED_DEV_TYPE = 0x106a
  5515. # Message Type
  5516. WPS_Beacon = 0x01
  5517. WPS_ProbeRequest = 0x02
  5518. WPS_ProbeResponse = 0x03
  5519. WPS_M1 = 0x04
  5520. WPS_M2 = 0x05
  5521. WPS_M2D = 0x06
  5522. WPS_M3 = 0x07
  5523. WPS_M4 = 0x08
  5524. WPS_M5 = 0x09
  5525. WPS_M6 = 0x0a
  5526. WPS_M7 = 0x0b
  5527. WPS_M8 = 0x0c
  5528. WPS_WSC_ACK = 0x0d
  5529. WPS_WSC_NACK = 0x0e
  5530. WPS_WSC_DONE = 0x0f
  5531. def get_wsc_msg(dev):
  5532. ev = dev.wait_event(["EAPOL-TX"], timeout=10)
  5533. if ev is None:
  5534. raise Exception("Timeout on EAPOL-TX")
  5535. data = binascii.unhexlify(ev.split(' ')[2])
  5536. msg = {}
  5537. # Parse EAPOL header
  5538. if len(data) < 4:
  5539. raise Exception("No room for EAPOL header")
  5540. version,type,length = struct.unpack('>BBH', data[0:4])
  5541. msg['eapol_version'] = version
  5542. msg['eapol_type'] = type
  5543. msg['eapol_length'] = length
  5544. data = data[4:]
  5545. if length != len(data):
  5546. raise Exception("EAPOL header length mismatch (%d != %d)" % (length, len(data)))
  5547. if type != 0:
  5548. raise Exception("Unexpected EAPOL header type: %d" % type)
  5549. # Parse EAP header
  5550. if len(data) < 4:
  5551. raise Exception("No room for EAP header")
  5552. code,identifier,length = struct.unpack('>BBH', data[0:4])
  5553. msg['eap_code'] = code
  5554. msg['eap_identifier'] = identifier
  5555. msg['eap_length'] = length
  5556. data = data[4:]
  5557. if msg['eapol_length'] != msg['eap_length']:
  5558. raise Exception("EAP header length mismatch (%d != %d)" % (msg['eapol_length'], length))
  5559. # Parse EAP expanded header
  5560. if len(data) < 1:
  5561. raise Exception("No EAP type included")
  5562. msg['eap_type'], = struct.unpack('B', data[0])
  5563. data = data[1:]
  5564. if msg['eap_type'] == 254:
  5565. if len(data) < 3 + 4:
  5566. raise Exception("Truncated EAP expanded header")
  5567. msg['eap_vendor_id'], msg['eap_vendor_type'] = struct.unpack('>LL', '\0' + data[0:7])
  5568. data = data[7:]
  5569. else:
  5570. raise Exception("Unexpected EAP type")
  5571. if msg['eap_vendor_id'] != WPS_VENDOR_ID_WFA:
  5572. raise Exception("Unexpected Vendor-Id")
  5573. if msg['eap_vendor_type'] != WPS_VENDOR_TYPE:
  5574. raise Exception("Unexpected Vendor-Type")
  5575. # Parse EAP-WSC header
  5576. if len(data) < 2:
  5577. raise Exception("Truncated EAP-WSC header")
  5578. msg['wsc_opcode'], msg['wsc_flags'] = struct.unpack('BB', data[0:2])
  5579. data = data[2:]
  5580. # Parse WSC attributes
  5581. msg['raw_attrs'] = data
  5582. attrs = {}
  5583. while len(data) > 0:
  5584. if len(data) < 4:
  5585. raise Exception("Truncated attribute header")
  5586. attr,length = struct.unpack('>HH', data[0:4])
  5587. data = data[4:]
  5588. if length > len(data):
  5589. raise Exception("Truncated attribute 0x%04x" % attr)
  5590. attrs[attr] = data[0:length]
  5591. data = data[length:]
  5592. msg['wsc_attrs'] = attrs
  5593. if ATTR_MSG_TYPE in attrs:
  5594. msg['wsc_msg_type'], = struct.unpack('B', attrs[ATTR_MSG_TYPE])
  5595. return msg
  5596. def recv_wsc_msg(dev, opcode, msg_type):
  5597. msg = get_wsc_msg(dev)
  5598. if msg['wsc_opcode'] != opcode or msg['wsc_msg_type'] != msg_type:
  5599. raise Exception("Unexpected Op-Code/MsgType")
  5600. return msg, msg['wsc_attrs'], msg['raw_attrs']
  5601. def build_wsc_attr(attr, payload):
  5602. return struct.pack('>HH', attr, len(payload)) + payload
  5603. def build_attr_msg_type(msg_type):
  5604. return build_wsc_attr(ATTR_MSG_TYPE, struct.pack('B', msg_type))
  5605. def build_eap_wsc(eap_code, eap_id, payload, opcode=WSC_MSG):
  5606. length = 4 + 8 + 2 + len(payload)
  5607. # EAPOL header
  5608. msg = struct.pack('>BBH', 2, 0, length)
  5609. # EAP header
  5610. msg += struct.pack('>BBH', eap_code, eap_id, length)
  5611. # EAP expanded header for EAP-WSC
  5612. msg += struct.pack('B', 254)
  5613. msg += struct.pack('>L', WPS_VENDOR_ID_WFA)[1:4]
  5614. msg += struct.pack('>L', WPS_VENDOR_TYPE)
  5615. # EAP-WSC header
  5616. msg += struct.pack('BB', opcode, 0)
  5617. # WSC attributes
  5618. msg += payload
  5619. return msg
  5620. def build_eap_success(eap_id):
  5621. length = 4
  5622. # EAPOL header
  5623. msg = struct.pack('>BBH', 2, 0, length)
  5624. # EAP header
  5625. msg += struct.pack('>BBH', 3, eap_id, length)
  5626. return msg
  5627. def build_eap_failure(eap_id):
  5628. length = 4
  5629. # EAPOL header
  5630. msg = struct.pack('>BBH', 2, 0, length)
  5631. # EAP header
  5632. msg += struct.pack('>BBH', 4, eap_id, length)
  5633. return msg
  5634. def send_wsc_msg(dev, src, msg):
  5635. res = dev.request("EAPOL_RX " + src + " " + binascii.hexlify(msg))
  5636. if "OK" not in res:
  5637. raise Exception("EAPOL_RX failed")
  5638. group_5_prime = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF
  5639. group_5_generator = 2
  5640. def wsc_kdf(key, label, bits):
  5641. result = ''
  5642. i = 1
  5643. while len(result) * 8 < bits:
  5644. data = struct.pack('>L', i) + label + struct.pack('>L', bits)
  5645. m = hmac.new(key, data, hashlib.sha256)
  5646. result += m.digest()
  5647. i += 1
  5648. return result[0:bits / 8]
  5649. def wsc_keys(kdk):
  5650. keys = wsc_kdf(kdk, "Wi-Fi Easy and Secure Key Derivation", 640)
  5651. authkey = keys[0:32]
  5652. keywrapkey = keys[32:48]
  5653. emsk = keys[48:80]
  5654. return authkey,keywrapkey,emsk
  5655. def wsc_dev_pw_half_psk(authkey, dev_pw):
  5656. m = hmac.new(authkey, dev_pw, hashlib.sha256)
  5657. return m.digest()[0:16]
  5658. def wsc_dev_pw_psk(authkey, dev_pw):
  5659. dev_pw_1 = dev_pw[0:len(dev_pw) / 2]
  5660. dev_pw_2 = dev_pw[len(dev_pw) / 2:]
  5661. psk1 = wsc_dev_pw_half_psk(authkey, dev_pw_1)
  5662. psk2 = wsc_dev_pw_half_psk(authkey, dev_pw_2)
  5663. return psk1,psk2
  5664. def build_attr_authenticator(authkey, prev_msg, curr_msg):
  5665. m = hmac.new(authkey, prev_msg + curr_msg, hashlib.sha256)
  5666. auth = m.digest()[0:8]
  5667. return build_wsc_attr(ATTR_AUTHENTICATOR, auth)
  5668. def build_attr_encr_settings(authkey, keywrapkey, data):
  5669. m = hmac.new(authkey, data, hashlib.sha256)
  5670. kwa = m.digest()[0:8]
  5671. data += build_wsc_attr(ATTR_KEY_WRAP_AUTH, kwa)
  5672. iv = 16*'\x99'
  5673. aes = AES.new(keywrapkey, AES.MODE_CBC, iv)
  5674. pad_len = 16 - len(data) % 16
  5675. ps = pad_len * struct.pack('B', pad_len)
  5676. data += ps
  5677. wrapped = aes.encrypt(data)
  5678. return build_wsc_attr(ATTR_ENCR_SETTINGS, iv + wrapped)
  5679. def decrypt_attr_encr_settings(authkey, keywrapkey, data):
  5680. if len(data) < 32 or len(data) % 16 != 0:
  5681. raise Exception("Unexpected Encrypted Settings length: %d" % len(data))
  5682. iv = data[0:16]
  5683. encr = data[16:]
  5684. aes = AES.new(keywrapkey, AES.MODE_CBC, iv)
  5685. decrypted = aes.decrypt(encr)
  5686. pad_len, = struct.unpack('B', decrypted[-1])
  5687. if pad_len > len(decrypted):
  5688. raise Exception("Invalid padding in Encrypted Settings")
  5689. for i in range(-pad_len, -1):
  5690. if decrypted[i] != decrypted[-1]:
  5691. raise Exception("Invalid PS value in Encrypted Settings")
  5692. decrypted = decrypted[0:len(decrypted) - pad_len]
  5693. if len(decrypted) < 12:
  5694. raise Exception("Truncated Encrypted Settings plaintext")
  5695. kwa = decrypted[-12:]
  5696. attr,length = struct.unpack(">HH", kwa[0:4])
  5697. if attr != ATTR_KEY_WRAP_AUTH or length != 8:
  5698. raise Exception("Invalid KWA header")
  5699. kwa = kwa[4:]
  5700. decrypted = decrypted[0:len(decrypted) - 12]
  5701. m = hmac.new(authkey, decrypted, hashlib.sha256)
  5702. calc_kwa = m.digest()[0:8]
  5703. if kwa != calc_kwa:
  5704. raise Exception("KWA mismatch")
  5705. return decrypted
  5706. def zeropad_str(val, pad_len):
  5707. while len(val) < pad_len * 2:
  5708. val = '0' + val
  5709. return val
  5710. def wsc_dh_init():
  5711. # For now, use a hardcoded private key. In theory, this is supposed to be
  5712. # randomly selected.
  5713. own_private = 0x123456789
  5714. own_public = pow(group_5_generator, own_private, group_5_prime)
  5715. pk = binascii.unhexlify(zeropad_str(format(own_public, '02x'), 192))
  5716. return own_private, pk
  5717. def wsc_dh_kdf(peer_pk, own_private, mac_addr, e_nonce, r_nonce):
  5718. peer_public = long(binascii.hexlify(peer_pk), 16)
  5719. if peer_public < 2 or peer_public >= group_5_prime:
  5720. raise Exception("Invalid peer public key")
  5721. if pow(peer_public, (group_5_prime - 1) / 2, group_5_prime) != 1:
  5722. raise Exception("Unexpected Legendre symbol for peer public key")
  5723. shared_secret = pow(peer_public, own_private, group_5_prime)
  5724. ss = zeropad_str(format(shared_secret, "02x"), 192)
  5725. logger.debug("DH shared secret: " + ss)
  5726. dhkey = hashlib.sha256(binascii.unhexlify(ss)).digest()
  5727. logger.debug("DHKey: " + binascii.hexlify(dhkey))
  5728. m = hmac.new(dhkey, e_nonce + mac_addr + r_nonce, hashlib.sha256)
  5729. kdk = m.digest()
  5730. logger.debug("KDK: " + binascii.hexlify(kdk))
  5731. authkey,keywrapkey,emsk = wsc_keys(kdk)
  5732. logger.debug("AuthKey: " + binascii.hexlify(authkey))
  5733. logger.debug("KeyWrapKey: " + binascii.hexlify(keywrapkey))
  5734. logger.debug("EMSK: " + binascii.hexlify(emsk))
  5735. return authkey,keywrapkey
  5736. def wsc_dev_pw_hash(authkey, dev_pw, e_pk, r_pk):
  5737. psk1,psk2 = wsc_dev_pw_psk(authkey, dev_pw)
  5738. logger.debug("PSK1: " + binascii.hexlify(psk1))
  5739. logger.debug("PSK2: " + binascii.hexlify(psk2))
  5740. # Note: Secret values are supposed to be random, but hardcoded values are
  5741. # fine for testing.
  5742. s1 = 16*'\x77'
  5743. m = hmac.new(authkey, s1 + psk1 + e_pk + r_pk, hashlib.sha256)
  5744. hash1 = m.digest()
  5745. logger.debug("Hash1: " + binascii.hexlify(hash1))
  5746. s2 = 16*'\x88'
  5747. m = hmac.new(authkey, s2 + psk2 + e_pk + r_pk, hashlib.sha256)
  5748. hash2 = m.digest()
  5749. logger.debug("Hash2: " + binascii.hexlify(hash2))
  5750. return s1,s2,hash1,hash2
  5751. def build_m1(eap_id, uuid_e, mac_addr, e_nonce, e_pk,
  5752. manufacturer='', model_name='', config_methods='\x00\x00'):
  5753. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5754. attrs += build_attr_msg_type(WPS_M1)
  5755. attrs += build_wsc_attr(ATTR_UUID_E, uuid_e)
  5756. attrs += build_wsc_attr(ATTR_MAC_ADDR, mac_addr)
  5757. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  5758. attrs += build_wsc_attr(ATTR_PUBLIC_KEY, e_pk)
  5759. attrs += build_wsc_attr(ATTR_AUTH_TYPE_FLAGS, '\x00\x00')
  5760. attrs += build_wsc_attr(ATTR_ENCR_TYPE_FLAGS, '\x00\x00')
  5761. attrs += build_wsc_attr(ATTR_CONN_TYPE_FLAGS, '\x00')
  5762. attrs += build_wsc_attr(ATTR_CONFIG_METHODS, config_methods)
  5763. attrs += build_wsc_attr(ATTR_WPS_STATE, '\x00')
  5764. attrs += build_wsc_attr(ATTR_MANUFACTURER, manufacturer)
  5765. attrs += build_wsc_attr(ATTR_MODEL_NAME, model_name)
  5766. attrs += build_wsc_attr(ATTR_MODEL_NUMBER, '')
  5767. attrs += build_wsc_attr(ATTR_SERIAL_NUMBER, '')
  5768. attrs += build_wsc_attr(ATTR_PRIMARY_DEV_TYPE, 8*'\x00')
  5769. attrs += build_wsc_attr(ATTR_DEV_NAME, '')
  5770. attrs += build_wsc_attr(ATTR_RF_BANDS, '\x00')
  5771. attrs += build_wsc_attr(ATTR_ASSOC_STATE, '\x00\x00')
  5772. attrs += build_wsc_attr(ATTR_DEV_PASSWORD_ID, '\x00\x00')
  5773. attrs += build_wsc_attr(ATTR_CONFIG_ERROR, '\x00\x00')
  5774. attrs += build_wsc_attr(ATTR_OS_VERSION, '\x00\x00\x00\x00')
  5775. m1 = build_eap_wsc(2, eap_id, attrs)
  5776. return m1, attrs
  5777. def build_m2(authkey, m1, eap_id, e_nonce, r_nonce, uuid_r, r_pk,
  5778. dev_pw_id='\x00\x00', eap_code=1):
  5779. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5780. attrs += build_attr_msg_type(WPS_M2)
  5781. if e_nonce:
  5782. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  5783. if r_nonce:
  5784. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  5785. attrs += build_wsc_attr(ATTR_UUID_R, uuid_r)
  5786. if r_pk:
  5787. attrs += build_wsc_attr(ATTR_PUBLIC_KEY, r_pk)
  5788. attrs += build_wsc_attr(ATTR_AUTH_TYPE_FLAGS, '\x00\x00')
  5789. attrs += build_wsc_attr(ATTR_ENCR_TYPE_FLAGS, '\x00\x00')
  5790. attrs += build_wsc_attr(ATTR_CONN_TYPE_FLAGS, '\x00')
  5791. attrs += build_wsc_attr(ATTR_CONFIG_METHODS, '\x00\x00')
  5792. attrs += build_wsc_attr(ATTR_MANUFACTURER, '')
  5793. attrs += build_wsc_attr(ATTR_MODEL_NAME, '')
  5794. attrs += build_wsc_attr(ATTR_MODEL_NUMBER, '')
  5795. attrs += build_wsc_attr(ATTR_SERIAL_NUMBER, '')
  5796. attrs += build_wsc_attr(ATTR_PRIMARY_DEV_TYPE, 8*'\x00')
  5797. attrs += build_wsc_attr(ATTR_DEV_NAME, '')
  5798. attrs += build_wsc_attr(ATTR_RF_BANDS, '\x00')
  5799. attrs += build_wsc_attr(ATTR_ASSOC_STATE, '\x00\x00')
  5800. attrs += build_wsc_attr(ATTR_CONFIG_ERROR, '\x00\x00')
  5801. attrs += build_wsc_attr(ATTR_DEV_PASSWORD_ID, dev_pw_id)
  5802. attrs += build_wsc_attr(ATTR_OS_VERSION, '\x00\x00\x00\x00')
  5803. attrs += build_attr_authenticator(authkey, m1, attrs)
  5804. m2 = build_eap_wsc(eap_code, eap_id, attrs)
  5805. return m2, attrs
  5806. def build_m2d(m1, eap_id, e_nonce, r_nonce, uuid_r, dev_pw_id=None, eap_code=1):
  5807. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5808. attrs += build_attr_msg_type(WPS_M2D)
  5809. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  5810. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  5811. attrs += build_wsc_attr(ATTR_UUID_R, uuid_r)
  5812. attrs += build_wsc_attr(ATTR_AUTH_TYPE_FLAGS, '\x00\x00')
  5813. attrs += build_wsc_attr(ATTR_ENCR_TYPE_FLAGS, '\x00\x00')
  5814. attrs += build_wsc_attr(ATTR_CONN_TYPE_FLAGS, '\x00')
  5815. attrs += build_wsc_attr(ATTR_CONFIG_METHODS, '\x00\x00')
  5816. attrs += build_wsc_attr(ATTR_MANUFACTURER, '')
  5817. attrs += build_wsc_attr(ATTR_MODEL_NAME, '')
  5818. #attrs += build_wsc_attr(ATTR_MODEL_NUMBER, '')
  5819. attrs += build_wsc_attr(ATTR_SERIAL_NUMBER, '')
  5820. attrs += build_wsc_attr(ATTR_PRIMARY_DEV_TYPE, 8*'\x00')
  5821. attrs += build_wsc_attr(ATTR_DEV_NAME, '')
  5822. attrs += build_wsc_attr(ATTR_RF_BANDS, '\x00')
  5823. attrs += build_wsc_attr(ATTR_ASSOC_STATE, '\x00\x00')
  5824. attrs += build_wsc_attr(ATTR_CONFIG_ERROR, '\x00\x00')
  5825. attrs += build_wsc_attr(ATTR_OS_VERSION, '\x00\x00\x00\x00')
  5826. if dev_pw_id:
  5827. attrs += build_wsc_attr(ATTR_DEV_PASSWORD_ID, dev_pw_id)
  5828. m2d = build_eap_wsc(eap_code, eap_id, attrs)
  5829. return m2d, attrs
  5830. def build_ack(eap_id, e_nonce, r_nonce, msg_type=WPS_WSC_ACK, eap_code=1):
  5831. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5832. if msg_type is not None:
  5833. attrs += build_attr_msg_type(msg_type)
  5834. if e_nonce:
  5835. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  5836. if r_nonce:
  5837. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  5838. msg = build_eap_wsc(eap_code, eap_id, attrs, opcode=WSC_ACK)
  5839. return msg, attrs
  5840. def build_nack(eap_id, e_nonce, r_nonce, config_error='\x00\x00',
  5841. msg_type=WPS_WSC_NACK, eap_code=1):
  5842. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5843. if msg_type is not None:
  5844. attrs += build_attr_msg_type(msg_type)
  5845. if e_nonce:
  5846. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  5847. if r_nonce:
  5848. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  5849. if config_error:
  5850. attrs += build_wsc_attr(ATTR_CONFIG_ERROR, config_error)
  5851. msg = build_eap_wsc(eap_code, eap_id, attrs, opcode=WSC_NACK)
  5852. return msg, attrs
  5853. def test_wps_ext(dev, apdev):
  5854. """WPS against external implementation"""
  5855. pin = "12345670"
  5856. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  5857. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5858. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5859. logger.debug("Receive WSC/Start from AP")
  5860. msg = get_wsc_msg(hapd)
  5861. if msg['wsc_opcode'] != WSC_Start:
  5862. raise Exception("Unexpected Op-Code for WSC/Start")
  5863. wsc_start_id = msg['eap_identifier']
  5864. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  5865. uuid_e = 16*'\x11'
  5866. e_nonce = 16*'\x22'
  5867. own_private, e_pk = wsc_dh_init()
  5868. logger.debug("Send M1 to AP")
  5869. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  5870. e_nonce, e_pk)
  5871. send_wsc_msg(hapd, addr, m1)
  5872. logger.debug("Receive M2 from AP")
  5873. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  5874. authkey,keywrapkey = wsc_dh_kdf(m2_attrs[ATTR_PUBLIC_KEY], own_private,
  5875. mac_addr, e_nonce,
  5876. m2_attrs[ATTR_REGISTRAR_NONCE])
  5877. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk,
  5878. m2_attrs[ATTR_PUBLIC_KEY])
  5879. logger.debug("Send M3 to AP")
  5880. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5881. attrs += build_attr_msg_type(WPS_M3)
  5882. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE,
  5883. m2_attrs[ATTR_REGISTRAR_NONCE])
  5884. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  5885. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  5886. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  5887. raw_m3_attrs = attrs
  5888. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  5889. send_wsc_msg(hapd, addr, m3)
  5890. logger.debug("Receive M4 from AP")
  5891. msg, m4_attrs, raw_m4_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M4)
  5892. logger.debug("Send M5 to AP")
  5893. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5894. attrs += build_attr_msg_type(WPS_M5)
  5895. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE,
  5896. m2_attrs[ATTR_REGISTRAR_NONCE])
  5897. data = build_wsc_attr(ATTR_E_SNONCE1, e_s1)
  5898. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  5899. attrs += build_attr_authenticator(authkey, raw_m4_attrs, attrs)
  5900. raw_m5_attrs = attrs
  5901. m5 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  5902. send_wsc_msg(hapd, addr, m5)
  5903. logger.debug("Receive M6 from AP")
  5904. msg, m6_attrs, raw_m6_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M6)
  5905. logger.debug("Send M7 to AP")
  5906. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5907. attrs += build_attr_msg_type(WPS_M7)
  5908. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE,
  5909. m2_attrs[ATTR_REGISTRAR_NONCE])
  5910. data = build_wsc_attr(ATTR_E_SNONCE2, e_s2)
  5911. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  5912. attrs += build_attr_authenticator(authkey, raw_m6_attrs, attrs)
  5913. m7 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  5914. raw_m7_attrs = attrs
  5915. send_wsc_msg(hapd, addr, m7)
  5916. logger.debug("Receive M8 from AP")
  5917. msg, m8_attrs, raw_m8_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M8)
  5918. m8_cred = decrypt_attr_encr_settings(authkey, keywrapkey,
  5919. m8_attrs[ATTR_ENCR_SETTINGS])
  5920. logger.debug("M8 Credential: " + binascii.hexlify(m8_cred))
  5921. logger.debug("Prepare WSC_Done")
  5922. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5923. attrs += build_attr_msg_type(WPS_WSC_DONE)
  5924. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  5925. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE,
  5926. m2_attrs[ATTR_REGISTRAR_NONCE])
  5927. wsc_done = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_Done)
  5928. # Do not send WSC_Done yet to allow exchangw with STA complete before the
  5929. # AP disconnects.
  5930. uuid_r = 16*'\x33'
  5931. r_nonce = 16*'\x44'
  5932. eap_id = wsc_start_id
  5933. logger.debug("Send WSC/Start to STA")
  5934. wsc_start = build_eap_wsc(1, eap_id, "", opcode=WSC_Start)
  5935. send_wsc_msg(dev[0], bssid, wsc_start)
  5936. eap_id = (eap_id + 1) % 256
  5937. logger.debug("Receive M1 from STA")
  5938. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  5939. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  5940. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  5941. r_nonce)
  5942. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  5943. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  5944. logger.debug("Send M2 to STA")
  5945. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  5946. m1_attrs[ATTR_ENROLLEE_NONCE],
  5947. r_nonce, uuid_r, e_pk)
  5948. send_wsc_msg(dev[0], bssid, m2)
  5949. eap_id = (eap_id + 1) % 256
  5950. logger.debug("Receive M3 from STA")
  5951. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M3)
  5952. logger.debug("Send M4 to STA")
  5953. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5954. attrs += build_attr_msg_type(WPS_M4)
  5955. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, m1_attrs[ATTR_ENROLLEE_NONCE])
  5956. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  5957. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  5958. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  5959. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  5960. attrs += build_attr_authenticator(authkey, raw_m3_attrs, attrs)
  5961. raw_m4_attrs = attrs
  5962. m4 = build_eap_wsc(1, eap_id, attrs)
  5963. send_wsc_msg(dev[0], bssid, m4)
  5964. eap_id = (eap_id + 1) % 256
  5965. logger.debug("Receive M5 from STA")
  5966. msg, m5_attrs, raw_m5_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M5)
  5967. logger.debug("Send M6 to STA")
  5968. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5969. attrs += build_attr_msg_type(WPS_M6)
  5970. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE,
  5971. m1_attrs[ATTR_ENROLLEE_NONCE])
  5972. data = build_wsc_attr(ATTR_R_SNONCE2, r_s2)
  5973. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  5974. attrs += build_attr_authenticator(authkey, raw_m5_attrs, attrs)
  5975. raw_m6_attrs = attrs
  5976. m6 = build_eap_wsc(1, eap_id, attrs)
  5977. send_wsc_msg(dev[0], bssid, m6)
  5978. eap_id = (eap_id + 1) % 256
  5979. logger.debug("Receive M7 from STA")
  5980. msg, m7_attrs, raw_m7_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M7)
  5981. logger.debug("Send M8 to STA")
  5982. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5983. attrs += build_attr_msg_type(WPS_M8)
  5984. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE,
  5985. m1_attrs[ATTR_ENROLLEE_NONCE])
  5986. attrs += build_attr_encr_settings(authkey, keywrapkey, m8_cred)
  5987. attrs += build_attr_authenticator(authkey, raw_m7_attrs, attrs)
  5988. raw_m8_attrs = attrs
  5989. m8 = build_eap_wsc(1, eap_id, attrs)
  5990. send_wsc_msg(dev[0], bssid, m8)
  5991. eap_id = (eap_id + 1) % 256
  5992. ev = dev[0].wait_event(["WPS-CRED-RECEIVED"], timeout=5)
  5993. if ev is None:
  5994. raise Exception("wpa_supplicant did not report credential")
  5995. logger.debug("Receive WSC_Done from STA")
  5996. msg = get_wsc_msg(dev[0])
  5997. if msg['wsc_opcode'] != WSC_Done or msg['wsc_msg_type'] != WPS_WSC_DONE:
  5998. raise Exception("Unexpected Op-Code/MsgType for WSC_Done")
  5999. logger.debug("Send WSC_Done to AP")
  6000. hapd.request("SET ext_eapol_frame_io 0")
  6001. dev[0].request("SET ext_eapol_frame_io 0")
  6002. send_wsc_msg(hapd, addr, wsc_done)
  6003. ev = hapd.wait_event(["WPS-REG-SUCCESS"], timeout=5)
  6004. if ev is None:
  6005. raise Exception("hostapd did not report WPS success")
  6006. dev[0].wait_connected()
  6007. def wps_start_kwa(dev, apdev):
  6008. pin = "12345670"
  6009. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  6010. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6011. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6012. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6013. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6014. uuid_r = 16*'\x33'
  6015. r_nonce = 16*'\x44'
  6016. own_private, e_pk = wsc_dh_init()
  6017. logger.debug("Receive M1 from STA")
  6018. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6019. eap_id = (msg['eap_identifier'] + 1) % 256
  6020. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6021. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6022. r_nonce)
  6023. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6024. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6025. logger.debug("Send M2 to STA")
  6026. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6027. m1_attrs[ATTR_ENROLLEE_NONCE],
  6028. r_nonce, uuid_r, e_pk)
  6029. send_wsc_msg(dev[0], bssid, m2)
  6030. eap_id = (eap_id + 1) % 256
  6031. logger.debug("Receive M3 from STA")
  6032. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M3)
  6033. logger.debug("Send M4 to STA")
  6034. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6035. attrs += build_attr_msg_type(WPS_M4)
  6036. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, m1_attrs[ATTR_ENROLLEE_NONCE])
  6037. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6038. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6039. return r_s1, keywrapkey, authkey, raw_m3_attrs, eap_id, bssid, attrs
  6040. def wps_stop_kwa(dev, bssid, attrs, authkey, raw_m3_attrs, eap_id):
  6041. attrs += build_attr_authenticator(authkey, raw_m3_attrs, attrs)
  6042. m4 = build_eap_wsc(1, eap_id, attrs)
  6043. send_wsc_msg(dev[0], bssid, m4)
  6044. eap_id = (eap_id + 1) % 256
  6045. logger.debug("Receive M5 from STA")
  6046. msg = get_wsc_msg(dev[0])
  6047. if msg['wsc_opcode'] != WSC_NACK:
  6048. raise Exception("Unexpected message - expected WSC_Nack")
  6049. dev[0].request("WPS_CANCEL")
  6050. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6051. dev[0].wait_disconnected()
  6052. def test_wps_ext_kwa_proto_no_kwa(dev, apdev):
  6053. """WPS and KWA error: No KWA attribute"""
  6054. r_s1,keywrapkey,authkey,raw_m3_attrs,eap_id,bssid,attrs = wps_start_kwa(dev, apdev)
  6055. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6056. # Encrypted Settings without KWA
  6057. iv = 16*'\x99'
  6058. aes = AES.new(keywrapkey, AES.MODE_CBC, iv)
  6059. pad_len = 16 - len(data) % 16
  6060. ps = pad_len * struct.pack('B', pad_len)
  6061. data += ps
  6062. wrapped = aes.encrypt(data)
  6063. attrs += build_wsc_attr(ATTR_ENCR_SETTINGS, iv + wrapped)
  6064. wps_stop_kwa(dev, bssid, attrs, authkey, raw_m3_attrs, eap_id)
  6065. def test_wps_ext_kwa_proto_data_after_kwa(dev, apdev):
  6066. """WPS and KWA error: Data after KWA"""
  6067. r_s1,keywrapkey,authkey,raw_m3_attrs,eap_id,bssid,attrs = wps_start_kwa(dev, apdev)
  6068. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6069. # Encrypted Settings and data after KWA
  6070. m = hmac.new(authkey, data, hashlib.sha256)
  6071. kwa = m.digest()[0:8]
  6072. data += build_wsc_attr(ATTR_KEY_WRAP_AUTH, kwa)
  6073. data += build_wsc_attr(ATTR_VENDOR_EXT, "1234567890")
  6074. iv = 16*'\x99'
  6075. aes = AES.new(keywrapkey, AES.MODE_CBC, iv)
  6076. pad_len = 16 - len(data) % 16
  6077. ps = pad_len * struct.pack('B', pad_len)
  6078. data += ps
  6079. wrapped = aes.encrypt(data)
  6080. attrs += build_wsc_attr(ATTR_ENCR_SETTINGS, iv + wrapped)
  6081. wps_stop_kwa(dev, bssid, attrs, authkey, raw_m3_attrs, eap_id)
  6082. def test_wps_ext_kwa_proto_kwa_mismatch(dev, apdev):
  6083. """WPS and KWA error: KWA mismatch"""
  6084. r_s1,keywrapkey,authkey,raw_m3_attrs,eap_id,bssid,attrs = wps_start_kwa(dev, apdev)
  6085. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6086. # Encrypted Settings and KWA with incorrect value
  6087. data += build_wsc_attr(ATTR_KEY_WRAP_AUTH, 8*'\x00')
  6088. iv = 16*'\x99'
  6089. aes = AES.new(keywrapkey, AES.MODE_CBC, iv)
  6090. pad_len = 16 - len(data) % 16
  6091. ps = pad_len * struct.pack('B', pad_len)
  6092. data += ps
  6093. wrapped = aes.encrypt(data)
  6094. attrs += build_wsc_attr(ATTR_ENCR_SETTINGS, iv + wrapped)
  6095. wps_stop_kwa(dev, bssid, attrs, authkey, raw_m3_attrs, eap_id)
  6096. def wps_run_cred_proto(dev, apdev, m8_cred, connect=False, no_connect=False):
  6097. pin = "12345670"
  6098. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  6099. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6100. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6101. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6102. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6103. uuid_r = 16*'\x33'
  6104. r_nonce = 16*'\x44'
  6105. own_private, e_pk = wsc_dh_init()
  6106. logger.debug("Receive M1 from STA")
  6107. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6108. eap_id = (msg['eap_identifier'] + 1) % 256
  6109. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6110. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6111. r_nonce)
  6112. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6113. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6114. logger.debug("Send M2 to STA")
  6115. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6116. m1_attrs[ATTR_ENROLLEE_NONCE],
  6117. r_nonce, uuid_r, e_pk)
  6118. send_wsc_msg(dev[0], bssid, m2)
  6119. eap_id = (eap_id + 1) % 256
  6120. logger.debug("Receive M3 from STA")
  6121. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M3)
  6122. logger.debug("Send M4 to STA")
  6123. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6124. attrs += build_attr_msg_type(WPS_M4)
  6125. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, m1_attrs[ATTR_ENROLLEE_NONCE])
  6126. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6127. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6128. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6129. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6130. attrs += build_attr_authenticator(authkey, raw_m3_attrs, attrs)
  6131. raw_m4_attrs = attrs
  6132. m4 = build_eap_wsc(1, eap_id, attrs)
  6133. send_wsc_msg(dev[0], bssid, m4)
  6134. eap_id = (eap_id + 1) % 256
  6135. logger.debug("Receive M5 from STA")
  6136. msg, m5_attrs, raw_m5_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M5)
  6137. logger.debug("Send M6 to STA")
  6138. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6139. attrs += build_attr_msg_type(WPS_M6)
  6140. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE,
  6141. m1_attrs[ATTR_ENROLLEE_NONCE])
  6142. data = build_wsc_attr(ATTR_R_SNONCE2, r_s2)
  6143. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6144. attrs += build_attr_authenticator(authkey, raw_m5_attrs, attrs)
  6145. raw_m6_attrs = attrs
  6146. m6 = build_eap_wsc(1, eap_id, attrs)
  6147. send_wsc_msg(dev[0], bssid, m6)
  6148. eap_id = (eap_id + 1) % 256
  6149. logger.debug("Receive M7 from STA")
  6150. msg, m7_attrs, raw_m7_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M7)
  6151. logger.debug("Send M8 to STA")
  6152. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6153. attrs += build_attr_msg_type(WPS_M8)
  6154. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE,
  6155. m1_attrs[ATTR_ENROLLEE_NONCE])
  6156. attrs += build_attr_encr_settings(authkey, keywrapkey, m8_cred)
  6157. attrs += build_attr_authenticator(authkey, raw_m7_attrs, attrs)
  6158. raw_m8_attrs = attrs
  6159. m8 = build_eap_wsc(1, eap_id, attrs)
  6160. send_wsc_msg(dev[0], bssid, m8)
  6161. eap_id = (eap_id + 1) % 256
  6162. if no_connect:
  6163. logger.debug("Receive WSC_Done from STA")
  6164. msg = get_wsc_msg(dev[0])
  6165. if msg['wsc_opcode'] != WSC_Done or msg['wsc_msg_type'] != WPS_WSC_DONE:
  6166. raise Exception("Unexpected Op-Code/MsgType for WSC_Done")
  6167. hapd.request("SET ext_eapol_frame_io 0")
  6168. dev[0].request("SET ext_eapol_frame_io 0")
  6169. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6170. dev[0].wait_disconnected()
  6171. dev[0].request("REMOVE_NETWORK all")
  6172. elif connect:
  6173. logger.debug("Receive WSC_Done from STA")
  6174. msg = get_wsc_msg(dev[0])
  6175. if msg['wsc_opcode'] != WSC_Done or msg['wsc_msg_type'] != WPS_WSC_DONE:
  6176. raise Exception("Unexpected Op-Code/MsgType for WSC_Done")
  6177. hapd.request("SET ext_eapol_frame_io 0")
  6178. dev[0].request("SET ext_eapol_frame_io 0")
  6179. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6180. dev[0].wait_connected()
  6181. else:
  6182. # Verify STA NACK's the credential
  6183. msg = get_wsc_msg(dev[0])
  6184. if msg['wsc_opcode'] != WSC_NACK:
  6185. raise Exception("Unexpected message - expected WSC_Nack")
  6186. dev[0].request("WPS_CANCEL")
  6187. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6188. dev[0].wait_disconnected()
  6189. def build_cred(nw_idx='\x01', ssid='test-wps-conf', auth_type='\x00\x20',
  6190. encr_type='\x00\x08', nw_key="12345678",
  6191. mac_addr='\x00\x00\x00\x00\x00\x00'):
  6192. attrs = ''
  6193. if nw_idx is not None:
  6194. attrs += build_wsc_attr(ATTR_NETWORK_INDEX, nw_idx)
  6195. if ssid is not None:
  6196. attrs += build_wsc_attr(ATTR_SSID, ssid)
  6197. if auth_type is not None:
  6198. attrs += build_wsc_attr(ATTR_AUTH_TYPE, auth_type)
  6199. if encr_type is not None:
  6200. attrs += build_wsc_attr(ATTR_ENCR_TYPE, encr_type)
  6201. if nw_key is not None:
  6202. attrs += build_wsc_attr(ATTR_NETWORK_KEY, nw_key)
  6203. if mac_addr is not None:
  6204. attrs += build_wsc_attr(ATTR_MAC_ADDR, mac_addr)
  6205. return build_wsc_attr(ATTR_CRED, attrs)
  6206. def test_wps_ext_cred_proto_success(dev, apdev):
  6207. """WPS and Credential: success"""
  6208. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6209. m8_cred = build_cred(mac_addr=mac_addr)
  6210. wps_run_cred_proto(dev, apdev, m8_cred, connect=True)
  6211. def test_wps_ext_cred_proto_mac_addr_mismatch(dev, apdev):
  6212. """WPS and Credential: MAC Address mismatch"""
  6213. m8_cred = build_cred()
  6214. wps_run_cred_proto(dev, apdev, m8_cred, connect=True)
  6215. def test_wps_ext_cred_proto_zero_padding(dev, apdev):
  6216. """WPS and Credential: zeropadded attributes"""
  6217. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6218. m8_cred = build_cred(mac_addr=mac_addr, ssid='test-wps-conf\x00',
  6219. nw_key="12345678\x00")
  6220. wps_run_cred_proto(dev, apdev, m8_cred, connect=True)
  6221. def test_wps_ext_cred_proto_ssid_missing(dev, apdev):
  6222. """WPS and Credential: SSID missing"""
  6223. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6224. m8_cred = build_cred(mac_addr=mac_addr, ssid=None)
  6225. wps_run_cred_proto(dev, apdev, m8_cred)
  6226. def test_wps_ext_cred_proto_ssid_zero_len(dev, apdev):
  6227. """WPS and Credential: Zero-length SSID"""
  6228. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6229. m8_cred = build_cred(mac_addr=mac_addr, ssid="")
  6230. wps_run_cred_proto(dev, apdev, m8_cred, no_connect=True)
  6231. def test_wps_ext_cred_proto_auth_type_missing(dev, apdev):
  6232. """WPS and Credential: Auth Type missing"""
  6233. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6234. m8_cred = build_cred(mac_addr=mac_addr, auth_type=None)
  6235. wps_run_cred_proto(dev, apdev, m8_cred)
  6236. def test_wps_ext_cred_proto_encr_type_missing(dev, apdev):
  6237. """WPS and Credential: Encr Type missing"""
  6238. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6239. m8_cred = build_cred(mac_addr=mac_addr, encr_type=None)
  6240. wps_run_cred_proto(dev, apdev, m8_cred)
  6241. def test_wps_ext_cred_proto_network_key_missing(dev, apdev):
  6242. """WPS and Credential: Network Key missing"""
  6243. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6244. m8_cred = build_cred(mac_addr=mac_addr, nw_key=None)
  6245. wps_run_cred_proto(dev, apdev, m8_cred)
  6246. def test_wps_ext_cred_proto_network_key_missing_open(dev, apdev):
  6247. """WPS and Credential: Network Key missing (open)"""
  6248. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6249. m8_cred = build_cred(mac_addr=mac_addr, auth_type='\x00\x01',
  6250. encr_type='\x00\x01', nw_key=None, ssid="foo")
  6251. wps_run_cred_proto(dev, apdev, m8_cred, no_connect=True)
  6252. def test_wps_ext_cred_proto_mac_addr_missing(dev, apdev):
  6253. """WPS and Credential: MAC Address missing"""
  6254. m8_cred = build_cred(mac_addr=None)
  6255. wps_run_cred_proto(dev, apdev, m8_cred)
  6256. def test_wps_ext_cred_proto_invalid_encr_type(dev, apdev):
  6257. """WPS and Credential: Invalid Encr Type"""
  6258. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6259. m8_cred = build_cred(mac_addr=mac_addr, encr_type='\x00\x00')
  6260. wps_run_cred_proto(dev, apdev, m8_cred)
  6261. def test_wps_ext_cred_proto_missing_cred(dev, apdev):
  6262. """WPS and Credential: Missing Credential"""
  6263. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6264. m8_cred = ''
  6265. wps_run_cred_proto(dev, apdev, m8_cred)
  6266. def test_wps_ext_proto_m2_no_public_key(dev, apdev):
  6267. """WPS and no Public Key in M2"""
  6268. pin = "12345670"
  6269. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  6270. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6271. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6272. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6273. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6274. uuid_r = 16*'\x33'
  6275. r_nonce = 16*'\x44'
  6276. own_private, e_pk = wsc_dh_init()
  6277. logger.debug("Receive M1 from STA")
  6278. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6279. eap_id = (msg['eap_identifier'] + 1) % 256
  6280. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6281. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6282. r_nonce)
  6283. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6284. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6285. logger.debug("Send M2 to STA")
  6286. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6287. m1_attrs[ATTR_ENROLLEE_NONCE],
  6288. r_nonce, uuid_r, None)
  6289. send_wsc_msg(dev[0], bssid, m2)
  6290. eap_id = (eap_id + 1) % 256
  6291. # Verify STA NACK's the credential
  6292. msg = get_wsc_msg(dev[0])
  6293. if msg['wsc_opcode'] != WSC_NACK:
  6294. raise Exception("Unexpected message - expected WSC_Nack")
  6295. dev[0].request("WPS_CANCEL")
  6296. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6297. dev[0].wait_disconnected()
  6298. def test_wps_ext_proto_m2_invalid_public_key(dev, apdev):
  6299. """WPS and invalid Public Key in M2"""
  6300. pin = "12345670"
  6301. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  6302. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6303. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6304. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6305. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6306. uuid_r = 16*'\x33'
  6307. r_nonce = 16*'\x44'
  6308. own_private, e_pk = wsc_dh_init()
  6309. logger.debug("Receive M1 from STA")
  6310. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6311. eap_id = (msg['eap_identifier'] + 1) % 256
  6312. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6313. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6314. r_nonce)
  6315. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6316. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6317. logger.debug("Send M2 to STA")
  6318. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6319. m1_attrs[ATTR_ENROLLEE_NONCE],
  6320. r_nonce, uuid_r, 192*'\xff')
  6321. send_wsc_msg(dev[0], bssid, m2)
  6322. eap_id = (eap_id + 1) % 256
  6323. # Verify STA NACK's the credential
  6324. msg = get_wsc_msg(dev[0])
  6325. if msg['wsc_opcode'] != WSC_NACK:
  6326. raise Exception("Unexpected message - expected WSC_Nack")
  6327. dev[0].request("WPS_CANCEL")
  6328. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6329. dev[0].wait_disconnected()
  6330. def test_wps_ext_proto_m2_public_key_oom(dev, apdev):
  6331. """WPS and Public Key OOM in M2"""
  6332. pin = "12345670"
  6333. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  6334. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6335. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6336. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6337. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6338. uuid_r = 16*'\x33'
  6339. r_nonce = 16*'\x44'
  6340. own_private, e_pk = wsc_dh_init()
  6341. logger.debug("Receive M1 from STA")
  6342. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6343. eap_id = (msg['eap_identifier'] + 1) % 256
  6344. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6345. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6346. r_nonce)
  6347. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6348. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6349. logger.debug("Send M2 to STA")
  6350. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6351. m1_attrs[ATTR_ENROLLEE_NONCE],
  6352. r_nonce, uuid_r, e_pk)
  6353. with alloc_fail(dev[0], 1, "wpabuf_alloc_copy;wps_process_pubkey"):
  6354. send_wsc_msg(dev[0], bssid, m2)
  6355. eap_id = (eap_id + 1) % 256
  6356. # Verify STA NACK's the credential
  6357. msg = get_wsc_msg(dev[0])
  6358. if msg['wsc_opcode'] != WSC_NACK:
  6359. raise Exception("Unexpected message - expected WSC_Nack")
  6360. dev[0].request("WPS_CANCEL")
  6361. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6362. dev[0].wait_disconnected()
  6363. def test_wps_ext_proto_nack_m3(dev, apdev):
  6364. """WPS and NACK M3"""
  6365. pin = "12345670"
  6366. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  6367. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6368. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6369. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6370. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6371. uuid_r = 16*'\x33'
  6372. r_nonce = 16*'\x44'
  6373. own_private, e_pk = wsc_dh_init()
  6374. logger.debug("Receive M1 from STA")
  6375. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6376. eap_id = (msg['eap_identifier'] + 1) % 256
  6377. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6378. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6379. r_nonce)
  6380. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6381. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6382. logger.debug("Send M2 to STA")
  6383. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6384. m1_attrs[ATTR_ENROLLEE_NONCE],
  6385. r_nonce, uuid_r, e_pk)
  6386. send_wsc_msg(dev[0], bssid, m2)
  6387. eap_id = (eap_id + 1) % 256
  6388. logger.debug("Receive M3 from STA")
  6389. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M3)
  6390. logger.debug("Send NACK to STA")
  6391. msg, attrs = build_nack(eap_id, m1_attrs[ATTR_ENROLLEE_NONCE],
  6392. r_nonce, config_error='\x01\x23')
  6393. send_wsc_msg(dev[0], bssid, msg)
  6394. ev = dev[0].wait_event(["WPS-FAIL"], timeout=5)
  6395. if ev is None:
  6396. raise Exception("Failure not reported")
  6397. if "msg=7 config_error=291" not in ev:
  6398. raise Exception("Unexpected failure reason: " + ev)
  6399. def test_wps_ext_proto_nack_m5(dev, apdev):
  6400. """WPS and NACK M5"""
  6401. pin = "12345670"
  6402. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  6403. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6404. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6405. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6406. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6407. uuid_r = 16*'\x33'
  6408. r_nonce = 16*'\x44'
  6409. own_private, e_pk = wsc_dh_init()
  6410. logger.debug("Receive M1 from STA")
  6411. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6412. eap_id = (msg['eap_identifier'] + 1) % 256
  6413. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6414. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6415. r_nonce)
  6416. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6417. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6418. logger.debug("Send M2 to STA")
  6419. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6420. m1_attrs[ATTR_ENROLLEE_NONCE],
  6421. r_nonce, uuid_r, e_pk)
  6422. send_wsc_msg(dev[0], bssid, m2)
  6423. eap_id = (eap_id + 1) % 256
  6424. logger.debug("Receive M3 from STA")
  6425. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M3)
  6426. logger.debug("Send M4 to STA")
  6427. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6428. attrs += build_attr_msg_type(WPS_M4)
  6429. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, m1_attrs[ATTR_ENROLLEE_NONCE])
  6430. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6431. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6432. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6433. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6434. attrs += build_attr_authenticator(authkey, raw_m3_attrs, attrs)
  6435. raw_m4_attrs = attrs
  6436. m4 = build_eap_wsc(1, eap_id, attrs)
  6437. send_wsc_msg(dev[0], bssid, m4)
  6438. eap_id = (eap_id + 1) % 256
  6439. logger.debug("Receive M5 from STA")
  6440. msg, m5_attrs, raw_m5_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M5)
  6441. logger.debug("Send NACK to STA")
  6442. msg, attrs = build_nack(eap_id, m1_attrs[ATTR_ENROLLEE_NONCE],
  6443. r_nonce, config_error='\x01\x24')
  6444. send_wsc_msg(dev[0], bssid, msg)
  6445. ev = dev[0].wait_event(["WPS-FAIL"], timeout=5)
  6446. if ev is None:
  6447. raise Exception("Failure not reported")
  6448. if "msg=9 config_error=292" not in ev:
  6449. raise Exception("Unexpected failure reason: " + ev)
  6450. def wps_nack_m3(dev, apdev):
  6451. pin = "00000000"
  6452. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  6453. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6454. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6455. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6456. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6457. uuid_r = 16*'\x33'
  6458. r_nonce = 16*'\x44'
  6459. own_private, e_pk = wsc_dh_init()
  6460. logger.debug("Receive M1 from STA")
  6461. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6462. eap_id = (msg['eap_identifier'] + 1) % 256
  6463. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6464. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6465. r_nonce)
  6466. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6467. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6468. logger.debug("Send M2 to STA")
  6469. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6470. m1_attrs[ATTR_ENROLLEE_NONCE],
  6471. r_nonce, uuid_r, e_pk, dev_pw_id='\x00\x04')
  6472. send_wsc_msg(dev[0], bssid, m2)
  6473. eap_id = (eap_id + 1) % 256
  6474. logger.debug("Receive M3 from STA")
  6475. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M3)
  6476. return eap_id, m1_attrs[ATTR_ENROLLEE_NONCE], r_nonce, bssid
  6477. def test_wps_ext_proto_nack_m3_no_config_error(dev, apdev):
  6478. """WPS and NACK M3 missing Config Error"""
  6479. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6480. logger.debug("Send NACK to STA")
  6481. msg, attrs = build_nack(eap_id, e_nonce, r_nonce, config_error=None)
  6482. send_wsc_msg(dev[0], bssid, msg)
  6483. dev[0].request("WPS_CANCEL")
  6484. dev[0].wait_disconnected()
  6485. dev[0].flush_scan_cache()
  6486. def test_wps_ext_proto_nack_m3_no_e_nonce(dev, apdev):
  6487. """WPS and NACK M3 missing E-Nonce"""
  6488. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6489. logger.debug("Send NACK to STA")
  6490. msg, attrs = build_nack(eap_id, None, r_nonce)
  6491. send_wsc_msg(dev[0], bssid, msg)
  6492. dev[0].request("WPS_CANCEL")
  6493. dev[0].wait_disconnected()
  6494. dev[0].flush_scan_cache()
  6495. def test_wps_ext_proto_nack_m3_e_nonce_mismatch(dev, apdev):
  6496. """WPS and NACK M3 E-Nonce mismatch"""
  6497. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6498. logger.debug("Send NACK to STA")
  6499. msg, attrs = build_nack(eap_id, 16*'\x00', r_nonce)
  6500. send_wsc_msg(dev[0], bssid, msg)
  6501. dev[0].request("WPS_CANCEL")
  6502. dev[0].wait_disconnected()
  6503. dev[0].flush_scan_cache()
  6504. def test_wps_ext_proto_nack_m3_no_r_nonce(dev, apdev):
  6505. """WPS and NACK M3 missing R-Nonce"""
  6506. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6507. logger.debug("Send NACK to STA")
  6508. msg, attrs = build_nack(eap_id, e_nonce, None)
  6509. send_wsc_msg(dev[0], bssid, msg)
  6510. dev[0].request("WPS_CANCEL")
  6511. dev[0].wait_disconnected()
  6512. dev[0].flush_scan_cache()
  6513. def test_wps_ext_proto_nack_m3_r_nonce_mismatch(dev, apdev):
  6514. """WPS and NACK M3 R-Nonce mismatch"""
  6515. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6516. logger.debug("Send NACK to STA")
  6517. msg, attrs = build_nack(eap_id, e_nonce, 16*'\x00')
  6518. send_wsc_msg(dev[0], bssid, msg)
  6519. dev[0].request("WPS_CANCEL")
  6520. dev[0].wait_disconnected()
  6521. dev[0].flush_scan_cache()
  6522. def test_wps_ext_proto_nack_m3_no_msg_type(dev, apdev):
  6523. """WPS and NACK M3 no Message Type"""
  6524. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6525. logger.debug("Send NACK to STA")
  6526. msg, attrs = build_nack(eap_id, e_nonce, r_nonce, msg_type=None)
  6527. send_wsc_msg(dev[0], bssid, msg)
  6528. dev[0].request("WPS_CANCEL")
  6529. dev[0].wait_disconnected()
  6530. dev[0].flush_scan_cache()
  6531. def test_wps_ext_proto_nack_m3_invalid_msg_type(dev, apdev):
  6532. """WPS and NACK M3 invalid Message Type"""
  6533. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6534. logger.debug("Send NACK to STA")
  6535. msg, attrs = build_nack(eap_id, e_nonce, r_nonce, msg_type=123)
  6536. send_wsc_msg(dev[0], bssid, msg)
  6537. dev[0].request("WPS_CANCEL")
  6538. dev[0].wait_disconnected()
  6539. dev[0].flush_scan_cache()
  6540. def test_wps_ext_proto_nack_m3_invalid_attr(dev, apdev):
  6541. """WPS and NACK M3 invalid attribute"""
  6542. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6543. logger.debug("Send NACK to STA")
  6544. attrs = '\x10\x10\x00'
  6545. msg = build_eap_wsc(1, eap_id, attrs, opcode=WSC_NACK)
  6546. send_wsc_msg(dev[0], bssid, msg)
  6547. dev[0].request("WPS_CANCEL")
  6548. dev[0].wait_disconnected()
  6549. dev[0].flush_scan_cache()
  6550. def test_wps_ext_proto_ack_m3_no_e_nonce(dev, apdev):
  6551. """WPS and ACK M3 missing E-Nonce"""
  6552. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6553. logger.debug("Send NACK to STA")
  6554. msg, attrs = build_ack(eap_id, None, r_nonce)
  6555. send_wsc_msg(dev[0], bssid, msg)
  6556. dev[0].request("WPS_CANCEL")
  6557. dev[0].wait_disconnected()
  6558. dev[0].flush_scan_cache()
  6559. def test_wps_ext_proto_ack_m3_e_nonce_mismatch(dev, apdev):
  6560. """WPS and ACK M3 E-Nonce mismatch"""
  6561. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6562. logger.debug("Send NACK to STA")
  6563. msg, attrs = build_ack(eap_id, 16*'\x00', r_nonce)
  6564. send_wsc_msg(dev[0], bssid, msg)
  6565. dev[0].request("WPS_CANCEL")
  6566. dev[0].wait_disconnected()
  6567. dev[0].flush_scan_cache()
  6568. def test_wps_ext_proto_ack_m3_no_r_nonce(dev, apdev):
  6569. """WPS and ACK M3 missing R-Nonce"""
  6570. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6571. logger.debug("Send NACK to STA")
  6572. msg, attrs = build_ack(eap_id, e_nonce, None)
  6573. send_wsc_msg(dev[0], bssid, msg)
  6574. dev[0].request("WPS_CANCEL")
  6575. dev[0].wait_disconnected()
  6576. dev[0].flush_scan_cache()
  6577. def test_wps_ext_proto_ack_m3_r_nonce_mismatch(dev, apdev):
  6578. """WPS and ACK M3 R-Nonce mismatch"""
  6579. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6580. logger.debug("Send NACK to STA")
  6581. msg, attrs = build_ack(eap_id, e_nonce, 16*'\x00')
  6582. send_wsc_msg(dev[0], bssid, msg)
  6583. dev[0].request("WPS_CANCEL")
  6584. dev[0].wait_disconnected()
  6585. dev[0].flush_scan_cache()
  6586. def test_wps_ext_proto_ack_m3_no_msg_type(dev, apdev):
  6587. """WPS and ACK M3 no Message Type"""
  6588. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6589. logger.debug("Send NACK to STA")
  6590. msg, attrs = build_ack(eap_id, e_nonce, r_nonce, msg_type=None)
  6591. send_wsc_msg(dev[0], bssid, msg)
  6592. dev[0].request("WPS_CANCEL")
  6593. dev[0].wait_disconnected()
  6594. dev[0].flush_scan_cache()
  6595. def test_wps_ext_proto_ack_m3_invalid_msg_type(dev, apdev):
  6596. """WPS and ACK M3 invalid Message Type"""
  6597. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6598. logger.debug("Send NACK to STA")
  6599. msg, attrs = build_ack(eap_id, e_nonce, r_nonce, msg_type=123)
  6600. send_wsc_msg(dev[0], bssid, msg)
  6601. dev[0].request("WPS_CANCEL")
  6602. dev[0].wait_disconnected()
  6603. dev[0].flush_scan_cache()
  6604. def test_wps_ext_proto_ack_m3_invalid_attr(dev, apdev):
  6605. """WPS and ACK M3 invalid attribute"""
  6606. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6607. logger.debug("Send ACK to STA")
  6608. attrs = '\x10\x10\x00'
  6609. msg = build_eap_wsc(1, eap_id, attrs, opcode=WSC_ACK)
  6610. send_wsc_msg(dev[0], bssid, msg)
  6611. dev[0].request("WPS_CANCEL")
  6612. dev[0].wait_disconnected()
  6613. dev[0].flush_scan_cache()
  6614. def test_wps_ext_proto_ack_m3(dev, apdev):
  6615. """WPS and ACK M3"""
  6616. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6617. logger.debug("Send ACK to STA")
  6618. msg, attrs = build_ack(eap_id, e_nonce, r_nonce)
  6619. send_wsc_msg(dev[0], bssid, msg)
  6620. dev[0].request("WPS_CANCEL")
  6621. dev[0].wait_disconnected()
  6622. dev[0].flush_scan_cache()
  6623. def wps_to_m3_helper(dev, apdev):
  6624. pin = "12345670"
  6625. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  6626. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6627. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6628. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6629. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6630. uuid_r = 16*'\x33'
  6631. r_nonce = 16*'\x44'
  6632. own_private, e_pk = wsc_dh_init()
  6633. logger.debug("Receive M1 from STA")
  6634. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6635. eap_id = (msg['eap_identifier'] + 1) % 256
  6636. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6637. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6638. r_nonce)
  6639. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6640. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6641. logger.debug("Send M2 to STA")
  6642. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6643. m1_attrs[ATTR_ENROLLEE_NONCE],
  6644. r_nonce, uuid_r, e_pk)
  6645. send_wsc_msg(dev[0], bssid, m2)
  6646. eap_id = (eap_id + 1) % 256
  6647. logger.debug("Receive M3 from STA")
  6648. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M3)
  6649. return eap_id, m1_attrs, r_nonce, bssid, r_hash1, r_hash2, r_s1, r_s2, raw_m3_attrs, authkey, keywrapkey
  6650. def wps_to_m3(dev, apdev):
  6651. eap_id, m1_attrs, r_nonce, bssid, r_hash1, r_hash2, r_s1, r_s2, raw_m3_attrs, authkey, keywrapkey = wps_to_m3_helper(dev, apdev)
  6652. return eap_id, m1_attrs[ATTR_ENROLLEE_NONCE], r_nonce, bssid, r_hash1, r_hash2, r_s1, raw_m3_attrs, authkey, keywrapkey
  6653. def wps_to_m5(dev, apdev):
  6654. eap_id, m1_attrs, r_nonce, bssid, r_hash1, r_hash2, r_s1, r_s2, raw_m3_attrs, authkey, keywrapkey = wps_to_m3_helper(dev, apdev)
  6655. logger.debug("Send M4 to STA")
  6656. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6657. attrs += build_attr_msg_type(WPS_M4)
  6658. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, m1_attrs[ATTR_ENROLLEE_NONCE])
  6659. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6660. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6661. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6662. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6663. attrs += build_attr_authenticator(authkey, raw_m3_attrs, attrs)
  6664. raw_m4_attrs = attrs
  6665. m4 = build_eap_wsc(1, eap_id, attrs)
  6666. send_wsc_msg(dev[0], bssid, m4)
  6667. eap_id = (eap_id + 1) % 256
  6668. logger.debug("Receive M5 from STA")
  6669. msg, m5_attrs, raw_m5_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M5)
  6670. return eap_id, m1_attrs[ATTR_ENROLLEE_NONCE], r_nonce, bssid, r_hash1, r_hash2, r_s2, raw_m5_attrs, authkey, keywrapkey
  6671. def test_wps_ext_proto_m4_missing_r_hash1(dev, apdev):
  6672. """WPS and no R-Hash1 in M4"""
  6673. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s1, m3, authkey, keywrapkey = wps_to_m3(dev, apdev)
  6674. logger.debug("Send M4 to STA")
  6675. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6676. attrs += build_attr_msg_type(WPS_M4)
  6677. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6678. #attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6679. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6680. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6681. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6682. attrs += build_attr_authenticator(authkey, m3, attrs)
  6683. m4 = build_eap_wsc(1, eap_id, attrs)
  6684. send_wsc_msg(dev[0], bssid, m4)
  6685. eap_id = (eap_id + 1) % 256
  6686. logger.debug("Receive M5 (NACK) from STA")
  6687. msg = get_wsc_msg(dev[0])
  6688. if msg['wsc_opcode'] != WSC_NACK:
  6689. raise Exception("Unexpected message - expected WSC_Nack")
  6690. dev[0].request("WPS_CANCEL")
  6691. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6692. dev[0].wait_disconnected()
  6693. def test_wps_ext_proto_m4_missing_r_hash2(dev, apdev):
  6694. """WPS and no R-Hash2 in M4"""
  6695. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s1, m3, authkey, keywrapkey = wps_to_m3(dev, apdev)
  6696. logger.debug("Send M4 to STA")
  6697. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6698. attrs += build_attr_msg_type(WPS_M4)
  6699. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6700. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6701. #attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6702. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6703. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6704. attrs += build_attr_authenticator(authkey, m3, attrs)
  6705. m4 = build_eap_wsc(1, eap_id, attrs)
  6706. send_wsc_msg(dev[0], bssid, m4)
  6707. eap_id = (eap_id + 1) % 256
  6708. logger.debug("Receive M5 (NACK) from STA")
  6709. msg = get_wsc_msg(dev[0])
  6710. if msg['wsc_opcode'] != WSC_NACK:
  6711. raise Exception("Unexpected message - expected WSC_Nack")
  6712. dev[0].request("WPS_CANCEL")
  6713. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6714. dev[0].wait_disconnected()
  6715. def test_wps_ext_proto_m4_missing_r_snonce1(dev, apdev):
  6716. """WPS and no R-SNonce1 in M4"""
  6717. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s1, m3, authkey, keywrapkey = wps_to_m3(dev, apdev)
  6718. logger.debug("Send M4 to STA")
  6719. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6720. attrs += build_attr_msg_type(WPS_M4)
  6721. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6722. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6723. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6724. #data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6725. data = ''
  6726. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6727. attrs += build_attr_authenticator(authkey, m3, attrs)
  6728. m4 = build_eap_wsc(1, eap_id, attrs)
  6729. send_wsc_msg(dev[0], bssid, m4)
  6730. eap_id = (eap_id + 1) % 256
  6731. logger.debug("Receive M5 (NACK) from STA")
  6732. msg = get_wsc_msg(dev[0])
  6733. if msg['wsc_opcode'] != WSC_NACK:
  6734. raise Exception("Unexpected message - expected WSC_Nack")
  6735. dev[0].request("WPS_CANCEL")
  6736. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6737. dev[0].wait_disconnected()
  6738. def test_wps_ext_proto_m4_invalid_pad_string(dev, apdev):
  6739. """WPS and invalid pad string in M4"""
  6740. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s1, m3, authkey, keywrapkey = wps_to_m3(dev, apdev)
  6741. logger.debug("Send M4 to STA")
  6742. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6743. attrs += build_attr_msg_type(WPS_M4)
  6744. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6745. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6746. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6747. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6748. m = hmac.new(authkey, data, hashlib.sha256)
  6749. kwa = m.digest()[0:8]
  6750. data += build_wsc_attr(ATTR_KEY_WRAP_AUTH, kwa)
  6751. iv = 16*'\x99'
  6752. aes = AES.new(keywrapkey, AES.MODE_CBC, iv)
  6753. pad_len = 16 - len(data) % 16
  6754. ps = (pad_len - 1) * struct.pack('B', pad_len) + struct.pack('B', pad_len - 1)
  6755. data += ps
  6756. wrapped = aes.encrypt(data)
  6757. attrs += build_wsc_attr(ATTR_ENCR_SETTINGS, iv + wrapped)
  6758. attrs += build_attr_authenticator(authkey, m3, attrs)
  6759. m4 = build_eap_wsc(1, eap_id, attrs)
  6760. send_wsc_msg(dev[0], bssid, m4)
  6761. eap_id = (eap_id + 1) % 256
  6762. logger.debug("Receive M5 (NACK) from STA")
  6763. msg = get_wsc_msg(dev[0])
  6764. if msg['wsc_opcode'] != WSC_NACK:
  6765. raise Exception("Unexpected message - expected WSC_Nack")
  6766. dev[0].request("WPS_CANCEL")
  6767. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6768. dev[0].wait_disconnected()
  6769. def test_wps_ext_proto_m4_invalid_pad_value(dev, apdev):
  6770. """WPS and invalid pad value in M4"""
  6771. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s1, m3, authkey, keywrapkey = wps_to_m3(dev, apdev)
  6772. logger.debug("Send M4 to STA")
  6773. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6774. attrs += build_attr_msg_type(WPS_M4)
  6775. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6776. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6777. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6778. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6779. m = hmac.new(authkey, data, hashlib.sha256)
  6780. kwa = m.digest()[0:8]
  6781. data += build_wsc_attr(ATTR_KEY_WRAP_AUTH, kwa)
  6782. iv = 16*'\x99'
  6783. aes = AES.new(keywrapkey, AES.MODE_CBC, iv)
  6784. pad_len = 16 - len(data) % 16
  6785. ps = (pad_len - 1) * struct.pack('B', pad_len) + struct.pack('B', 255)
  6786. data += ps
  6787. wrapped = aes.encrypt(data)
  6788. attrs += build_wsc_attr(ATTR_ENCR_SETTINGS, iv + wrapped)
  6789. attrs += build_attr_authenticator(authkey, m3, attrs)
  6790. m4 = build_eap_wsc(1, eap_id, attrs)
  6791. send_wsc_msg(dev[0], bssid, m4)
  6792. eap_id = (eap_id + 1) % 256
  6793. logger.debug("Receive M5 (NACK) from STA")
  6794. msg = get_wsc_msg(dev[0])
  6795. if msg['wsc_opcode'] != WSC_NACK:
  6796. raise Exception("Unexpected message - expected WSC_Nack")
  6797. dev[0].request("WPS_CANCEL")
  6798. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6799. dev[0].wait_disconnected()
  6800. def test_wps_ext_proto_m4_no_encr_settings(dev, apdev):
  6801. """WPS and no Encr Settings in M4"""
  6802. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s1, m3, authkey, keywrapkey = wps_to_m3(dev, apdev)
  6803. logger.debug("Send M4 to STA")
  6804. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6805. attrs += build_attr_msg_type(WPS_M4)
  6806. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6807. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6808. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6809. attrs += build_attr_authenticator(authkey, m3, attrs)
  6810. m4 = build_eap_wsc(1, eap_id, attrs)
  6811. send_wsc_msg(dev[0], bssid, m4)
  6812. eap_id = (eap_id + 1) % 256
  6813. logger.debug("Receive M5 (NACK) from STA")
  6814. msg = get_wsc_msg(dev[0])
  6815. if msg['wsc_opcode'] != WSC_NACK:
  6816. raise Exception("Unexpected message - expected WSC_Nack")
  6817. dev[0].request("WPS_CANCEL")
  6818. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6819. dev[0].wait_disconnected()
  6820. def test_wps_ext_proto_m6_missing_r_snonce2(dev, apdev):
  6821. """WPS and no R-SNonce2 in M6"""
  6822. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s2, m5, authkey, keywrapkey = wps_to_m5(dev, apdev)
  6823. logger.debug("Send M6 to STA")
  6824. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6825. attrs += build_attr_msg_type(WPS_M6)
  6826. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6827. #data = build_wsc_attr(ATTR_R_SNONCE2, r_s2)
  6828. data = ''
  6829. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6830. attrs += build_attr_authenticator(authkey, m5, attrs)
  6831. m6 = build_eap_wsc(1, eap_id, attrs)
  6832. send_wsc_msg(dev[0], bssid, m6)
  6833. eap_id = (eap_id + 1) % 256
  6834. logger.debug("Receive M7 (NACK) from STA")
  6835. msg = get_wsc_msg(dev[0])
  6836. if msg['wsc_opcode'] != WSC_NACK:
  6837. raise Exception("Unexpected message - expected WSC_Nack")
  6838. dev[0].request("WPS_CANCEL")
  6839. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6840. dev[0].wait_disconnected()
  6841. def test_wps_ext_proto_m6_no_encr_settings(dev, apdev):
  6842. """WPS and no Encr Settings in M6"""
  6843. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s2, m5, authkey, keywrapkey = wps_to_m5(dev, apdev)
  6844. logger.debug("Send M6 to STA")
  6845. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6846. attrs += build_attr_msg_type(WPS_M6)
  6847. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6848. data = build_wsc_attr(ATTR_R_SNONCE2, r_s2)
  6849. #attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6850. attrs += build_attr_authenticator(authkey, m5, attrs)
  6851. m6 = build_eap_wsc(1, eap_id, attrs)
  6852. send_wsc_msg(dev[0], bssid, m6)
  6853. eap_id = (eap_id + 1) % 256
  6854. logger.debug("Receive M7 (NACK) from STA")
  6855. msg = get_wsc_msg(dev[0])
  6856. if msg['wsc_opcode'] != WSC_NACK:
  6857. raise Exception("Unexpected message - expected WSC_Nack")
  6858. dev[0].request("WPS_CANCEL")
  6859. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6860. dev[0].wait_disconnected()
  6861. def test_wps_ext_proto_m8_no_encr_settings(dev, apdev):
  6862. """WPS and no Encr Settings in M6"""
  6863. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s2, m5, authkey, keywrapkey = wps_to_m5(dev, apdev)
  6864. logger.debug("Send M6 to STA")
  6865. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6866. attrs += build_attr_msg_type(WPS_M6)
  6867. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6868. data = build_wsc_attr(ATTR_R_SNONCE2, r_s2)
  6869. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6870. attrs += build_attr_authenticator(authkey, m5, attrs)
  6871. raw_m6_attrs = attrs
  6872. m6 = build_eap_wsc(1, eap_id, attrs)
  6873. send_wsc_msg(dev[0], bssid, m6)
  6874. eap_id = (eap_id + 1) % 256
  6875. logger.debug("Receive M7 from STA")
  6876. msg, m7_attrs, raw_m7_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M7)
  6877. logger.debug("Send M8 to STA")
  6878. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6879. attrs += build_attr_msg_type(WPS_M8)
  6880. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6881. #attrs += build_attr_encr_settings(authkey, keywrapkey, m8_cred)
  6882. attrs += build_attr_authenticator(authkey, raw_m7_attrs, attrs)
  6883. raw_m8_attrs = attrs
  6884. m8 = build_eap_wsc(1, eap_id, attrs)
  6885. send_wsc_msg(dev[0], bssid, m8)
  6886. logger.debug("Receive WSC_Done (NACK) from STA")
  6887. msg = get_wsc_msg(dev[0])
  6888. if msg['wsc_opcode'] != WSC_NACK:
  6889. raise Exception("Unexpected message - expected WSC_Nack")
  6890. dev[0].request("WPS_CANCEL")
  6891. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6892. dev[0].wait_disconnected()
  6893. def wps_start_ext_reg(apdev, dev):
  6894. addr = dev.own_addr()
  6895. bssid = apdev['bssid']
  6896. ssid = "test-wps-conf"
  6897. appin = "12345670"
  6898. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  6899. "wpa_passphrase": "12345678", "wpa": "2",
  6900. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  6901. "ap_pin": appin }
  6902. hapd = hostapd.add_ap(apdev, params)
  6903. dev.scan_for_bss(bssid, freq="2412")
  6904. hapd.request("SET ext_eapol_frame_io 1")
  6905. dev.request("SET ext_eapol_frame_io 1")
  6906. dev.request("WPS_REG " + bssid + " " + appin)
  6907. return addr,bssid,hapd
  6908. def wps_run_ap_settings_proto(dev, apdev, ap_settings, success):
  6909. addr,bssid,hapd = wps_start_ext_reg(apdev[0], dev[0])
  6910. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6911. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6912. logger.debug("Receive M1 from AP")
  6913. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M1)
  6914. mac_addr = m1_attrs[ATTR_MAC_ADDR]
  6915. e_nonce = m1_attrs[ATTR_ENROLLEE_NONCE]
  6916. e_pk = m1_attrs[ATTR_PUBLIC_KEY]
  6917. appin = '12345670'
  6918. uuid_r = 16*'\x33'
  6919. r_nonce = 16*'\x44'
  6920. own_private, r_pk = wsc_dh_init()
  6921. authkey,keywrapkey = wsc_dh_kdf(e_pk, own_private, mac_addr, e_nonce,
  6922. r_nonce)
  6923. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, appin, e_pk, r_pk)
  6924. logger.debug("Send M2 to AP")
  6925. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, msg['eap_identifier'],
  6926. e_nonce, r_nonce, uuid_r, r_pk, eap_code=2)
  6927. send_wsc_msg(hapd, addr, m2)
  6928. logger.debug("Receive M3 from AP")
  6929. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M3)
  6930. logger.debug("Send M4 to AP")
  6931. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6932. attrs += build_attr_msg_type(WPS_M4)
  6933. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6934. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6935. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6936. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6937. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6938. attrs += build_attr_authenticator(authkey, raw_m3_attrs, attrs)
  6939. raw_m4_attrs = attrs
  6940. m4 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  6941. send_wsc_msg(hapd, addr, m4)
  6942. logger.debug("Receive M5 from AP")
  6943. msg, m5_attrs, raw_m5_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M5)
  6944. logger.debug("Send M6 to STA")
  6945. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6946. attrs += build_attr_msg_type(WPS_M6)
  6947. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6948. data = build_wsc_attr(ATTR_R_SNONCE2, r_s2)
  6949. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6950. attrs += build_attr_authenticator(authkey, raw_m5_attrs, attrs)
  6951. raw_m6_attrs = attrs
  6952. m6 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  6953. send_wsc_msg(hapd, addr, m6)
  6954. logger.debug("Receive M7 from AP")
  6955. msg, m7_attrs, raw_m7_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M7)
  6956. logger.debug("Send M8 to STA")
  6957. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6958. attrs += build_attr_msg_type(WPS_M8)
  6959. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6960. if ap_settings:
  6961. attrs += build_attr_encr_settings(authkey, keywrapkey, ap_settings)
  6962. attrs += build_attr_authenticator(authkey, raw_m7_attrs, attrs)
  6963. raw_m8_attrs = attrs
  6964. m8 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  6965. send_wsc_msg(hapd, addr, m8)
  6966. if success:
  6967. ev = hapd.wait_event(["WPS-NEW-AP-SETTINGS"], timeout=5)
  6968. if ev is None:
  6969. raise Exception("New AP settings not reported")
  6970. logger.debug("Receive WSC_Done from AP")
  6971. msg = get_wsc_msg(hapd)
  6972. if msg['wsc_opcode'] != WSC_Done:
  6973. raise Exception("Unexpected message - expected WSC_Done")
  6974. logger.debug("Send WSC_ACK to AP")
  6975. ack,attrs = build_ack(msg['eap_identifier'], e_nonce, r_nonce,
  6976. eap_code=2)
  6977. send_wsc_msg(hapd, addr, ack)
  6978. dev[0].wait_disconnected()
  6979. else:
  6980. ev = hapd.wait_event(["WPS-FAIL"], timeout=5)
  6981. if ev is None:
  6982. raise Exception("WPS failure not reported")
  6983. logger.debug("Receive WSC_NACK from AP")
  6984. msg = get_wsc_msg(hapd)
  6985. if msg['wsc_opcode'] != WSC_NACK:
  6986. raise Exception("Unexpected message - expected WSC_NACK")
  6987. logger.debug("Send WSC_NACK to AP")
  6988. nack,attrs = build_nack(msg['eap_identifier'], e_nonce, r_nonce,
  6989. eap_code=2)
  6990. send_wsc_msg(hapd, addr, nack)
  6991. dev[0].wait_disconnected()
  6992. def test_wps_ext_ap_settings_success(dev, apdev):
  6993. """WPS and AP Settings: success"""
  6994. ap_settings = build_wsc_attr(ATTR_NETWORK_INDEX, '\x01')
  6995. ap_settings += build_wsc_attr(ATTR_SSID, "test")
  6996. ap_settings += build_wsc_attr(ATTR_AUTH_TYPE, '\x00\x01')
  6997. ap_settings += build_wsc_attr(ATTR_ENCR_TYPE, '\x00\x01')
  6998. ap_settings += build_wsc_attr(ATTR_NETWORK_KEY, '')
  6999. ap_settings += build_wsc_attr(ATTR_MAC_ADDR, binascii.unhexlify(apdev[0]['bssid'].replace(':', '')))
  7000. wps_run_ap_settings_proto(dev, apdev, ap_settings, True)
  7001. @remote_compatible
  7002. def test_wps_ext_ap_settings_missing(dev, apdev):
  7003. """WPS and AP Settings: missing"""
  7004. wps_run_ap_settings_proto(dev, apdev, None, False)
  7005. @remote_compatible
  7006. def test_wps_ext_ap_settings_mac_addr_mismatch(dev, apdev):
  7007. """WPS and AP Settings: MAC Address mismatch"""
  7008. ap_settings = build_wsc_attr(ATTR_NETWORK_INDEX, '\x01')
  7009. ap_settings += build_wsc_attr(ATTR_SSID, "test")
  7010. ap_settings += build_wsc_attr(ATTR_AUTH_TYPE, '\x00\x01')
  7011. ap_settings += build_wsc_attr(ATTR_ENCR_TYPE, '\x00\x01')
  7012. ap_settings += build_wsc_attr(ATTR_NETWORK_KEY, '')
  7013. ap_settings += build_wsc_attr(ATTR_MAC_ADDR, '\x00\x00\x00\x00\x00\x00')
  7014. wps_run_ap_settings_proto(dev, apdev, ap_settings, True)
  7015. @remote_compatible
  7016. def test_wps_ext_ap_settings_mac_addr_missing(dev, apdev):
  7017. """WPS and AP Settings: missing MAC Address"""
  7018. ap_settings = build_wsc_attr(ATTR_NETWORK_INDEX, '\x01')
  7019. ap_settings += build_wsc_attr(ATTR_SSID, "test")
  7020. ap_settings += build_wsc_attr(ATTR_AUTH_TYPE, '\x00\x01')
  7021. ap_settings += build_wsc_attr(ATTR_ENCR_TYPE, '\x00\x01')
  7022. ap_settings += build_wsc_attr(ATTR_NETWORK_KEY, '')
  7023. wps_run_ap_settings_proto(dev, apdev, ap_settings, False)
  7024. @remote_compatible
  7025. def test_wps_ext_ap_settings_reject_encr_type(dev, apdev):
  7026. """WPS and AP Settings: reject Encr Type"""
  7027. ap_settings = build_wsc_attr(ATTR_NETWORK_INDEX, '\x01')
  7028. ap_settings += build_wsc_attr(ATTR_SSID, "test")
  7029. ap_settings += build_wsc_attr(ATTR_AUTH_TYPE, '\x00\x01')
  7030. ap_settings += build_wsc_attr(ATTR_ENCR_TYPE, '\x00\x00')
  7031. ap_settings += build_wsc_attr(ATTR_NETWORK_KEY, '')
  7032. ap_settings += build_wsc_attr(ATTR_MAC_ADDR, binascii.unhexlify(apdev[0]['bssid'].replace(':', '')))
  7033. wps_run_ap_settings_proto(dev, apdev, ap_settings, False)
  7034. @remote_compatible
  7035. def test_wps_ext_ap_settings_m2d(dev, apdev):
  7036. """WPS and AP Settings: M2D"""
  7037. addr,bssid,hapd = wps_start_ext_reg(apdev[0], dev[0])
  7038. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7039. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7040. logger.debug("Receive M1 from AP")
  7041. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M1)
  7042. e_nonce = m1_attrs[ATTR_ENROLLEE_NONCE]
  7043. r_nonce = 16*'\x44'
  7044. uuid_r = 16*'\x33'
  7045. logger.debug("Send M2D to AP")
  7046. m2d, raw_m2d_attrs = build_m2d(raw_m1_attrs, msg['eap_identifier'],
  7047. e_nonce, r_nonce, uuid_r,
  7048. dev_pw_id='\x00\x00', eap_code=2)
  7049. send_wsc_msg(hapd, addr, m2d)
  7050. ev = hapd.wait_event(["WPS-M2D"], timeout=5)
  7051. if ev is None:
  7052. raise Exception("M2D not reported")
  7053. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7054. def wps_wait_ap_nack(hapd, dev, e_nonce, r_nonce):
  7055. logger.debug("Receive WSC_NACK from AP")
  7056. msg = get_wsc_msg(hapd)
  7057. if msg['wsc_opcode'] != WSC_NACK:
  7058. raise Exception("Unexpected message - expected WSC_NACK")
  7059. logger.debug("Send WSC_NACK to AP")
  7060. nack,attrs = build_nack(msg['eap_identifier'], e_nonce, r_nonce,
  7061. eap_code=2)
  7062. send_wsc_msg(hapd, dev.own_addr(), nack)
  7063. dev.wait_disconnected()
  7064. @remote_compatible
  7065. def test_wps_ext_m3_missing_e_hash1(dev, apdev):
  7066. """WPS proto: M3 missing E-Hash1"""
  7067. pin = "12345670"
  7068. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7069. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7070. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7071. logger.debug("Receive WSC/Start from AP")
  7072. msg = get_wsc_msg(hapd)
  7073. if msg['wsc_opcode'] != WSC_Start:
  7074. raise Exception("Unexpected Op-Code for WSC/Start")
  7075. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7076. uuid_e = 16*'\x11'
  7077. e_nonce = 16*'\x22'
  7078. own_private, e_pk = wsc_dh_init()
  7079. logger.debug("Send M1 to AP")
  7080. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7081. e_nonce, e_pk)
  7082. send_wsc_msg(hapd, addr, m1)
  7083. logger.debug("Receive M2 from AP")
  7084. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7085. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7086. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7087. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7088. r_nonce)
  7089. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7090. logger.debug("Send M3 to AP")
  7091. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7092. attrs += build_attr_msg_type(WPS_M3)
  7093. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7094. #attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7095. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7096. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7097. raw_m3_attrs = attrs
  7098. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7099. send_wsc_msg(hapd, addr, m3)
  7100. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7101. @remote_compatible
  7102. def test_wps_ext_m3_missing_e_hash2(dev, apdev):
  7103. """WPS proto: M3 missing E-Hash2"""
  7104. pin = "12345670"
  7105. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7106. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7107. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7108. logger.debug("Receive WSC/Start from AP")
  7109. msg = get_wsc_msg(hapd)
  7110. if msg['wsc_opcode'] != WSC_Start:
  7111. raise Exception("Unexpected Op-Code for WSC/Start")
  7112. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7113. uuid_e = 16*'\x11'
  7114. e_nonce = 16*'\x22'
  7115. own_private, e_pk = wsc_dh_init()
  7116. logger.debug("Send M1 to AP")
  7117. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7118. e_nonce, e_pk)
  7119. send_wsc_msg(hapd, addr, m1)
  7120. logger.debug("Receive M2 from AP")
  7121. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7122. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7123. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7124. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7125. r_nonce)
  7126. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7127. logger.debug("Send M3 to AP")
  7128. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7129. attrs += build_attr_msg_type(WPS_M3)
  7130. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7131. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7132. #attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7133. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7134. raw_m3_attrs = attrs
  7135. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7136. send_wsc_msg(hapd, addr, m3)
  7137. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7138. @remote_compatible
  7139. def test_wps_ext_m5_missing_e_snonce1(dev, apdev):
  7140. """WPS proto: M5 missing E-SNonce1"""
  7141. pin = "12345670"
  7142. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7143. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7144. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7145. logger.debug("Receive WSC/Start from AP")
  7146. msg = get_wsc_msg(hapd)
  7147. if msg['wsc_opcode'] != WSC_Start:
  7148. raise Exception("Unexpected Op-Code for WSC/Start")
  7149. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7150. uuid_e = 16*'\x11'
  7151. e_nonce = 16*'\x22'
  7152. own_private, e_pk = wsc_dh_init()
  7153. logger.debug("Send M1 to AP")
  7154. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7155. e_nonce, e_pk)
  7156. send_wsc_msg(hapd, addr, m1)
  7157. logger.debug("Receive M2 from AP")
  7158. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7159. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7160. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7161. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7162. r_nonce)
  7163. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7164. logger.debug("Send M3 to AP")
  7165. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7166. attrs += build_attr_msg_type(WPS_M3)
  7167. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7168. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7169. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7170. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7171. raw_m3_attrs = attrs
  7172. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7173. send_wsc_msg(hapd, addr, m3)
  7174. logger.debug("Receive M4 from AP")
  7175. msg, m4_attrs, raw_m4_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M4)
  7176. logger.debug("Send M5 to AP")
  7177. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7178. attrs += build_attr_msg_type(WPS_M5)
  7179. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7180. #data = build_wsc_attr(ATTR_E_SNONCE1, e_s1)
  7181. data = ''
  7182. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7183. attrs += build_attr_authenticator(authkey, raw_m4_attrs, attrs)
  7184. raw_m5_attrs = attrs
  7185. m5 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7186. send_wsc_msg(hapd, addr, m5)
  7187. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7188. @remote_compatible
  7189. def test_wps_ext_m5_e_snonce1_mismatch(dev, apdev):
  7190. """WPS proto: M5 E-SNonce1 mismatch"""
  7191. pin = "12345670"
  7192. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7193. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7194. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7195. logger.debug("Receive WSC/Start from AP")
  7196. msg = get_wsc_msg(hapd)
  7197. if msg['wsc_opcode'] != WSC_Start:
  7198. raise Exception("Unexpected Op-Code for WSC/Start")
  7199. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7200. uuid_e = 16*'\x11'
  7201. e_nonce = 16*'\x22'
  7202. own_private, e_pk = wsc_dh_init()
  7203. logger.debug("Send M1 to AP")
  7204. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7205. e_nonce, e_pk)
  7206. send_wsc_msg(hapd, addr, m1)
  7207. logger.debug("Receive M2 from AP")
  7208. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7209. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7210. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7211. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7212. r_nonce)
  7213. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7214. logger.debug("Send M3 to AP")
  7215. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7216. attrs += build_attr_msg_type(WPS_M3)
  7217. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7218. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7219. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7220. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7221. raw_m3_attrs = attrs
  7222. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7223. send_wsc_msg(hapd, addr, m3)
  7224. logger.debug("Receive M4 from AP")
  7225. msg, m4_attrs, raw_m4_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M4)
  7226. logger.debug("Send M5 to AP")
  7227. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7228. attrs += build_attr_msg_type(WPS_M5)
  7229. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7230. data = build_wsc_attr(ATTR_E_SNONCE1, 16*'\x00')
  7231. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7232. attrs += build_attr_authenticator(authkey, raw_m4_attrs, attrs)
  7233. raw_m5_attrs = attrs
  7234. m5 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7235. send_wsc_msg(hapd, addr, m5)
  7236. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7237. def test_wps_ext_m7_missing_e_snonce2(dev, apdev):
  7238. """WPS proto: M7 missing E-SNonce2"""
  7239. pin = "12345670"
  7240. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7241. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7242. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7243. logger.debug("Receive WSC/Start from AP")
  7244. msg = get_wsc_msg(hapd)
  7245. if msg['wsc_opcode'] != WSC_Start:
  7246. raise Exception("Unexpected Op-Code for WSC/Start")
  7247. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7248. uuid_e = 16*'\x11'
  7249. e_nonce = 16*'\x22'
  7250. own_private, e_pk = wsc_dh_init()
  7251. logger.debug("Send M1 to AP")
  7252. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7253. e_nonce, e_pk)
  7254. send_wsc_msg(hapd, addr, m1)
  7255. logger.debug("Receive M2 from AP")
  7256. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7257. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7258. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7259. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7260. r_nonce)
  7261. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7262. logger.debug("Send M3 to AP")
  7263. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7264. attrs += build_attr_msg_type(WPS_M3)
  7265. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7266. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7267. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7268. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7269. raw_m3_attrs = attrs
  7270. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7271. send_wsc_msg(hapd, addr, m3)
  7272. logger.debug("Receive M4 from AP")
  7273. msg, m4_attrs, raw_m4_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M4)
  7274. logger.debug("Send M5 to AP")
  7275. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7276. attrs += build_attr_msg_type(WPS_M5)
  7277. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7278. data = build_wsc_attr(ATTR_E_SNONCE1, e_s1)
  7279. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7280. attrs += build_attr_authenticator(authkey, raw_m4_attrs, attrs)
  7281. raw_m5_attrs = attrs
  7282. m5 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7283. send_wsc_msg(hapd, addr, m5)
  7284. logger.debug("Receive M6 from AP")
  7285. msg, m6_attrs, raw_m6_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M6)
  7286. logger.debug("Send M7 to AP")
  7287. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7288. attrs += build_attr_msg_type(WPS_M7)
  7289. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7290. #data = build_wsc_attr(ATTR_E_SNONCE2, e_s2)
  7291. data = ''
  7292. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7293. attrs += build_attr_authenticator(authkey, raw_m6_attrs, attrs)
  7294. m7 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7295. raw_m7_attrs = attrs
  7296. send_wsc_msg(hapd, addr, m7)
  7297. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7298. @remote_compatible
  7299. def test_wps_ext_m7_e_snonce2_mismatch(dev, apdev):
  7300. """WPS proto: M7 E-SNonce2 mismatch"""
  7301. pin = "12345670"
  7302. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7303. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7304. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7305. logger.debug("Receive WSC/Start from AP")
  7306. msg = get_wsc_msg(hapd)
  7307. if msg['wsc_opcode'] != WSC_Start:
  7308. raise Exception("Unexpected Op-Code for WSC/Start")
  7309. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7310. uuid_e = 16*'\x11'
  7311. e_nonce = 16*'\x22'
  7312. own_private, e_pk = wsc_dh_init()
  7313. logger.debug("Send M1 to AP")
  7314. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7315. e_nonce, e_pk)
  7316. send_wsc_msg(hapd, addr, m1)
  7317. logger.debug("Receive M2 from AP")
  7318. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7319. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7320. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7321. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7322. r_nonce)
  7323. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7324. logger.debug("Send M3 to AP")
  7325. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7326. attrs += build_attr_msg_type(WPS_M3)
  7327. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7328. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7329. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7330. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7331. raw_m3_attrs = attrs
  7332. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7333. send_wsc_msg(hapd, addr, m3)
  7334. logger.debug("Receive M4 from AP")
  7335. msg, m4_attrs, raw_m4_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M4)
  7336. logger.debug("Send M5 to AP")
  7337. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7338. attrs += build_attr_msg_type(WPS_M5)
  7339. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7340. data = build_wsc_attr(ATTR_E_SNONCE1, e_s1)
  7341. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7342. attrs += build_attr_authenticator(authkey, raw_m4_attrs, attrs)
  7343. raw_m5_attrs = attrs
  7344. m5 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7345. send_wsc_msg(hapd, addr, m5)
  7346. logger.debug("Receive M6 from AP")
  7347. msg, m6_attrs, raw_m6_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M6)
  7348. logger.debug("Send M7 to AP")
  7349. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7350. attrs += build_attr_msg_type(WPS_M7)
  7351. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7352. data = build_wsc_attr(ATTR_E_SNONCE2, 16*'\x00')
  7353. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7354. attrs += build_attr_authenticator(authkey, raw_m6_attrs, attrs)
  7355. m7 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7356. raw_m7_attrs = attrs
  7357. send_wsc_msg(hapd, addr, m7)
  7358. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7359. @remote_compatible
  7360. def test_wps_ext_m1_pubkey_oom(dev, apdev):
  7361. """WPS proto: M1 PubKey OOM"""
  7362. pin = "12345670"
  7363. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7364. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7365. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7366. logger.debug("Receive WSC/Start from AP")
  7367. msg = get_wsc_msg(hapd)
  7368. if msg['wsc_opcode'] != WSC_Start:
  7369. raise Exception("Unexpected Op-Code for WSC/Start")
  7370. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7371. uuid_e = 16*'\x11'
  7372. e_nonce = 16*'\x22'
  7373. own_private, e_pk = wsc_dh_init()
  7374. logger.debug("Send M1 to AP")
  7375. with alloc_fail(hapd, 1, "wpabuf_alloc_copy;wps_process_pubkey"):
  7376. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7377. e_nonce, e_pk)
  7378. send_wsc_msg(hapd, addr, m1)
  7379. wps_wait_eap_failure(hapd, dev[0])
  7380. def wps_wait_eap_failure(hapd, dev):
  7381. ev = hapd.wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  7382. if ev is None:
  7383. raise Exception("EAP-Failure not reported")
  7384. dev.wait_disconnected()
  7385. @remote_compatible
  7386. def test_wps_ext_m3_m1(dev, apdev):
  7387. """WPS proto: M3 replaced with M1"""
  7388. pin = "12345670"
  7389. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7390. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7391. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7392. logger.debug("Receive WSC/Start from AP")
  7393. msg = get_wsc_msg(hapd)
  7394. if msg['wsc_opcode'] != WSC_Start:
  7395. raise Exception("Unexpected Op-Code for WSC/Start")
  7396. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7397. uuid_e = 16*'\x11'
  7398. e_nonce = 16*'\x22'
  7399. own_private, e_pk = wsc_dh_init()
  7400. logger.debug("Send M1 to AP")
  7401. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7402. e_nonce, e_pk)
  7403. send_wsc_msg(hapd, addr, m1)
  7404. logger.debug("Receive M2 from AP")
  7405. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7406. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7407. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7408. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7409. r_nonce)
  7410. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7411. logger.debug("Send M3(M1) to AP")
  7412. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7413. attrs += build_attr_msg_type(WPS_M1)
  7414. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7415. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7416. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7417. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7418. raw_m3_attrs = attrs
  7419. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7420. send_wsc_msg(hapd, addr, m3)
  7421. wps_wait_eap_failure(hapd, dev[0])
  7422. @remote_compatible
  7423. def test_wps_ext_m5_m3(dev, apdev):
  7424. """WPS proto: M5 replaced with M3"""
  7425. pin = "12345670"
  7426. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7427. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7428. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7429. logger.debug("Receive WSC/Start from AP")
  7430. msg = get_wsc_msg(hapd)
  7431. if msg['wsc_opcode'] != WSC_Start:
  7432. raise Exception("Unexpected Op-Code for WSC/Start")
  7433. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7434. uuid_e = 16*'\x11'
  7435. e_nonce = 16*'\x22'
  7436. own_private, e_pk = wsc_dh_init()
  7437. logger.debug("Send M1 to AP")
  7438. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7439. e_nonce, e_pk)
  7440. send_wsc_msg(hapd, addr, m1)
  7441. logger.debug("Receive M2 from AP")
  7442. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7443. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7444. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7445. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7446. r_nonce)
  7447. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7448. logger.debug("Send M3 to AP")
  7449. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7450. attrs += build_attr_msg_type(WPS_M3)
  7451. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7452. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7453. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7454. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7455. raw_m3_attrs = attrs
  7456. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7457. send_wsc_msg(hapd, addr, m3)
  7458. logger.debug("Receive M4 from AP")
  7459. msg, m4_attrs, raw_m4_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M4)
  7460. logger.debug("Send M5(M3) to AP")
  7461. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7462. attrs += build_attr_msg_type(WPS_M3)
  7463. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7464. data = build_wsc_attr(ATTR_E_SNONCE1, e_s1)
  7465. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7466. attrs += build_attr_authenticator(authkey, raw_m4_attrs, attrs)
  7467. raw_m5_attrs = attrs
  7468. m5 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7469. send_wsc_msg(hapd, addr, m5)
  7470. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7471. @remote_compatible
  7472. def test_wps_ext_m3_m2(dev, apdev):
  7473. """WPS proto: M3 replaced with M2"""
  7474. pin = "12345670"
  7475. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7476. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7477. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7478. logger.debug("Receive WSC/Start from AP")
  7479. msg = get_wsc_msg(hapd)
  7480. if msg['wsc_opcode'] != WSC_Start:
  7481. raise Exception("Unexpected Op-Code for WSC/Start")
  7482. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7483. uuid_e = 16*'\x11'
  7484. e_nonce = 16*'\x22'
  7485. own_private, e_pk = wsc_dh_init()
  7486. logger.debug("Send M1 to AP")
  7487. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7488. e_nonce, e_pk)
  7489. send_wsc_msg(hapd, addr, m1)
  7490. logger.debug("Receive M2 from AP")
  7491. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7492. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7493. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7494. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7495. r_nonce)
  7496. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7497. logger.debug("Send M3(M2) to AP")
  7498. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7499. attrs += build_attr_msg_type(WPS_M2)
  7500. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7501. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7502. raw_m3_attrs = attrs
  7503. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7504. send_wsc_msg(hapd, addr, m3)
  7505. wps_wait_eap_failure(hapd, dev[0])
  7506. @remote_compatible
  7507. def test_wps_ext_m3_m5(dev, apdev):
  7508. """WPS proto: M3 replaced with M5"""
  7509. pin = "12345670"
  7510. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7511. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7512. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7513. logger.debug("Receive WSC/Start from AP")
  7514. msg = get_wsc_msg(hapd)
  7515. if msg['wsc_opcode'] != WSC_Start:
  7516. raise Exception("Unexpected Op-Code for WSC/Start")
  7517. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7518. uuid_e = 16*'\x11'
  7519. e_nonce = 16*'\x22'
  7520. own_private, e_pk = wsc_dh_init()
  7521. logger.debug("Send M1 to AP")
  7522. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7523. e_nonce, e_pk)
  7524. send_wsc_msg(hapd, addr, m1)
  7525. logger.debug("Receive M2 from AP")
  7526. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7527. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7528. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7529. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7530. r_nonce)
  7531. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7532. logger.debug("Send M3(M5) to AP")
  7533. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7534. attrs += build_attr_msg_type(WPS_M5)
  7535. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7536. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7537. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7538. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7539. raw_m3_attrs = attrs
  7540. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7541. send_wsc_msg(hapd, addr, m3)
  7542. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7543. @remote_compatible
  7544. def test_wps_ext_m3_m7(dev, apdev):
  7545. """WPS proto: M3 replaced with M7"""
  7546. pin = "12345670"
  7547. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7548. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7549. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7550. logger.debug("Receive WSC/Start from AP")
  7551. msg = get_wsc_msg(hapd)
  7552. if msg['wsc_opcode'] != WSC_Start:
  7553. raise Exception("Unexpected Op-Code for WSC/Start")
  7554. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7555. uuid_e = 16*'\x11'
  7556. e_nonce = 16*'\x22'
  7557. own_private, e_pk = wsc_dh_init()
  7558. logger.debug("Send M1 to AP")
  7559. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7560. e_nonce, e_pk)
  7561. send_wsc_msg(hapd, addr, m1)
  7562. logger.debug("Receive M2 from AP")
  7563. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7564. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7565. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7566. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7567. r_nonce)
  7568. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7569. logger.debug("Send M3(M7) to AP")
  7570. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7571. attrs += build_attr_msg_type(WPS_M7)
  7572. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7573. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7574. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7575. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7576. raw_m3_attrs = attrs
  7577. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7578. send_wsc_msg(hapd, addr, m3)
  7579. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7580. @remote_compatible
  7581. def test_wps_ext_m3_done(dev, apdev):
  7582. """WPS proto: M3 replaced with WSC_Done"""
  7583. pin = "12345670"
  7584. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7585. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7586. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7587. logger.debug("Receive WSC/Start from AP")
  7588. msg = get_wsc_msg(hapd)
  7589. if msg['wsc_opcode'] != WSC_Start:
  7590. raise Exception("Unexpected Op-Code for WSC/Start")
  7591. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7592. uuid_e = 16*'\x11'
  7593. e_nonce = 16*'\x22'
  7594. own_private, e_pk = wsc_dh_init()
  7595. logger.debug("Send M1 to AP")
  7596. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7597. e_nonce, e_pk)
  7598. send_wsc_msg(hapd, addr, m1)
  7599. logger.debug("Receive M2 from AP")
  7600. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7601. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7602. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7603. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7604. r_nonce)
  7605. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7606. logger.debug("Send M3(WSC_Done) to AP")
  7607. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7608. attrs += build_attr_msg_type(WPS_WSC_DONE)
  7609. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7610. raw_m3_attrs = attrs
  7611. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_Done)
  7612. send_wsc_msg(hapd, addr, m3)
  7613. wps_wait_eap_failure(hapd, dev[0])
  7614. @remote_compatible
  7615. def test_wps_ext_m2_nack_invalid(dev, apdev):
  7616. """WPS proto: M2 followed by invalid NACK"""
  7617. pin = "12345670"
  7618. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7619. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7620. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7621. logger.debug("Receive WSC/Start from AP")
  7622. msg = get_wsc_msg(hapd)
  7623. if msg['wsc_opcode'] != WSC_Start:
  7624. raise Exception("Unexpected Op-Code for WSC/Start")
  7625. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7626. uuid_e = 16*'\x11'
  7627. e_nonce = 16*'\x22'
  7628. own_private, e_pk = wsc_dh_init()
  7629. logger.debug("Send M1 to AP")
  7630. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7631. e_nonce, e_pk)
  7632. send_wsc_msg(hapd, addr, m1)
  7633. logger.debug("Receive M2 from AP")
  7634. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7635. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7636. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7637. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7638. r_nonce)
  7639. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7640. logger.debug("Send WSC_NACK to AP")
  7641. attrs = '\x10\x00\x00'
  7642. nack = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_NACK)
  7643. send_wsc_msg(hapd, addr, nack)
  7644. wps_wait_eap_failure(hapd, dev[0])
  7645. @remote_compatible
  7646. def test_wps_ext_m2_nack_no_msg_type(dev, apdev):
  7647. """WPS proto: M2 followed by NACK without Msg Type"""
  7648. pin = "12345670"
  7649. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7650. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7651. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7652. logger.debug("Receive WSC/Start from AP")
  7653. msg = get_wsc_msg(hapd)
  7654. if msg['wsc_opcode'] != WSC_Start:
  7655. raise Exception("Unexpected Op-Code for WSC/Start")
  7656. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7657. uuid_e = 16*'\x11'
  7658. e_nonce = 16*'\x22'
  7659. own_private, e_pk = wsc_dh_init()
  7660. logger.debug("Send M1 to AP")
  7661. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7662. e_nonce, e_pk)
  7663. send_wsc_msg(hapd, addr, m1)
  7664. logger.debug("Receive M2 from AP")
  7665. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7666. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7667. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7668. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7669. r_nonce)
  7670. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7671. logger.debug("Send WSC_NACK to AP")
  7672. nack,attrs = build_nack(msg['eap_identifier'], e_nonce, r_nonce,
  7673. msg_type=None, eap_code=2)
  7674. send_wsc_msg(hapd, addr, nack)
  7675. wps_wait_eap_failure(hapd, dev[0])
  7676. @remote_compatible
  7677. def test_wps_ext_m2_nack_invalid_msg_type(dev, apdev):
  7678. """WPS proto: M2 followed by NACK with invalid Msg Type"""
  7679. pin = "12345670"
  7680. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7681. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7682. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7683. logger.debug("Receive WSC/Start from AP")
  7684. msg = get_wsc_msg(hapd)
  7685. if msg['wsc_opcode'] != WSC_Start:
  7686. raise Exception("Unexpected Op-Code for WSC/Start")
  7687. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7688. uuid_e = 16*'\x11'
  7689. e_nonce = 16*'\x22'
  7690. own_private, e_pk = wsc_dh_init()
  7691. logger.debug("Send M1 to AP")
  7692. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7693. e_nonce, e_pk)
  7694. send_wsc_msg(hapd, addr, m1)
  7695. logger.debug("Receive M2 from AP")
  7696. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7697. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7698. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7699. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7700. r_nonce)
  7701. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7702. logger.debug("Send WSC_NACK to AP")
  7703. nack,attrs = build_nack(msg['eap_identifier'], e_nonce, r_nonce,
  7704. msg_type=WPS_WSC_ACK, eap_code=2)
  7705. send_wsc_msg(hapd, addr, nack)
  7706. wps_wait_eap_failure(hapd, dev[0])
  7707. @remote_compatible
  7708. def test_wps_ext_m2_nack_e_nonce_mismatch(dev, apdev):
  7709. """WPS proto: M2 followed by NACK with e-nonce mismatch"""
  7710. pin = "12345670"
  7711. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7712. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7713. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7714. logger.debug("Receive WSC/Start from AP")
  7715. msg = get_wsc_msg(hapd)
  7716. if msg['wsc_opcode'] != WSC_Start:
  7717. raise Exception("Unexpected Op-Code for WSC/Start")
  7718. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7719. uuid_e = 16*'\x11'
  7720. e_nonce = 16*'\x22'
  7721. own_private, e_pk = wsc_dh_init()
  7722. logger.debug("Send M1 to AP")
  7723. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7724. e_nonce, e_pk)
  7725. send_wsc_msg(hapd, addr, m1)
  7726. logger.debug("Receive M2 from AP")
  7727. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7728. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7729. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7730. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7731. r_nonce)
  7732. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7733. logger.debug("Send WSC_NACK to AP")
  7734. nack,attrs = build_nack(msg['eap_identifier'], 16*'\x00', r_nonce,
  7735. eap_code=2)
  7736. send_wsc_msg(hapd, addr, nack)
  7737. wps_wait_eap_failure(hapd, dev[0])
  7738. @remote_compatible
  7739. def test_wps_ext_m2_nack_no_config_error(dev, apdev):
  7740. """WPS proto: M2 followed by NACK without Config Error"""
  7741. pin = "12345670"
  7742. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7743. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7744. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7745. logger.debug("Receive WSC/Start from AP")
  7746. msg = get_wsc_msg(hapd)
  7747. if msg['wsc_opcode'] != WSC_Start:
  7748. raise Exception("Unexpected Op-Code for WSC/Start")
  7749. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7750. uuid_e = 16*'\x11'
  7751. e_nonce = 16*'\x22'
  7752. own_private, e_pk = wsc_dh_init()
  7753. logger.debug("Send M1 to AP")
  7754. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7755. e_nonce, e_pk)
  7756. send_wsc_msg(hapd, addr, m1)
  7757. logger.debug("Receive M2 from AP")
  7758. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7759. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7760. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7761. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7762. r_nonce)
  7763. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7764. logger.debug("Send WSC_NACK to AP")
  7765. nack,attrs = build_nack(msg['eap_identifier'], e_nonce, r_nonce,
  7766. config_error=None, eap_code=2)
  7767. send_wsc_msg(hapd, addr, nack)
  7768. wps_wait_eap_failure(hapd, dev[0])
  7769. @remote_compatible
  7770. def test_wps_ext_m2_ack_invalid(dev, apdev):
  7771. """WPS proto: M2 followed by invalid ACK"""
  7772. pin = "12345670"
  7773. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7774. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7775. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7776. logger.debug("Receive WSC/Start from AP")
  7777. msg = get_wsc_msg(hapd)
  7778. if msg['wsc_opcode'] != WSC_Start:
  7779. raise Exception("Unexpected Op-Code for WSC/Start")
  7780. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7781. uuid_e = 16*'\x11'
  7782. e_nonce = 16*'\x22'
  7783. own_private, e_pk = wsc_dh_init()
  7784. logger.debug("Send M1 to AP")
  7785. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7786. e_nonce, e_pk)
  7787. send_wsc_msg(hapd, addr, m1)
  7788. logger.debug("Receive M2 from AP")
  7789. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7790. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7791. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7792. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7793. r_nonce)
  7794. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7795. logger.debug("Send WSC_ACK to AP")
  7796. attrs = '\x10\x00\x00'
  7797. ack = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_ACK)
  7798. send_wsc_msg(hapd, addr, ack)
  7799. wps_wait_eap_failure(hapd, dev[0])
  7800. @remote_compatible
  7801. def test_wps_ext_m2_ack(dev, apdev):
  7802. """WPS proto: M2 followed by ACK"""
  7803. pin = "12345670"
  7804. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7805. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7806. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7807. logger.debug("Receive WSC/Start from AP")
  7808. msg = get_wsc_msg(hapd)
  7809. if msg['wsc_opcode'] != WSC_Start:
  7810. raise Exception("Unexpected Op-Code for WSC/Start")
  7811. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7812. uuid_e = 16*'\x11'
  7813. e_nonce = 16*'\x22'
  7814. own_private, e_pk = wsc_dh_init()
  7815. logger.debug("Send M1 to AP")
  7816. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7817. e_nonce, e_pk)
  7818. send_wsc_msg(hapd, addr, m1)
  7819. logger.debug("Receive M2 from AP")
  7820. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7821. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7822. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7823. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7824. r_nonce)
  7825. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7826. logger.debug("Send WSC_ACK to AP")
  7827. ack,attrs = build_ack(msg['eap_identifier'], e_nonce, r_nonce, eap_code=2)
  7828. send_wsc_msg(hapd, addr, ack)
  7829. wps_wait_eap_failure(hapd, dev[0])
  7830. @remote_compatible
  7831. def test_wps_ext_m2_ack_no_msg_type(dev, apdev):
  7832. """WPS proto: M2 followed by ACK missing Msg Type"""
  7833. pin = "12345670"
  7834. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7835. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7836. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7837. logger.debug("Receive WSC/Start from AP")
  7838. msg = get_wsc_msg(hapd)
  7839. if msg['wsc_opcode'] != WSC_Start:
  7840. raise Exception("Unexpected Op-Code for WSC/Start")
  7841. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7842. uuid_e = 16*'\x11'
  7843. e_nonce = 16*'\x22'
  7844. own_private, e_pk = wsc_dh_init()
  7845. logger.debug("Send M1 to AP")
  7846. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7847. e_nonce, e_pk)
  7848. send_wsc_msg(hapd, addr, m1)
  7849. logger.debug("Receive M2 from AP")
  7850. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7851. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7852. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7853. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7854. r_nonce)
  7855. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7856. logger.debug("Send WSC_ACK to AP")
  7857. ack,attrs = build_ack(msg['eap_identifier'], e_nonce, r_nonce,
  7858. msg_type=None, eap_code=2)
  7859. send_wsc_msg(hapd, addr, ack)
  7860. wps_wait_eap_failure(hapd, dev[0])
  7861. @remote_compatible
  7862. def test_wps_ext_m2_ack_invalid_msg_type(dev, apdev):
  7863. """WPS proto: M2 followed by ACK with invalid Msg Type"""
  7864. pin = "12345670"
  7865. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7866. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7867. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7868. logger.debug("Receive WSC/Start from AP")
  7869. msg = get_wsc_msg(hapd)
  7870. if msg['wsc_opcode'] != WSC_Start:
  7871. raise Exception("Unexpected Op-Code for WSC/Start")
  7872. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7873. uuid_e = 16*'\x11'
  7874. e_nonce = 16*'\x22'
  7875. own_private, e_pk = wsc_dh_init()
  7876. logger.debug("Send M1 to AP")
  7877. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7878. e_nonce, e_pk)
  7879. send_wsc_msg(hapd, addr, m1)
  7880. logger.debug("Receive M2 from AP")
  7881. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7882. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7883. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7884. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7885. r_nonce)
  7886. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7887. logger.debug("Send WSC_ACK to AP")
  7888. ack,attrs = build_ack(msg['eap_identifier'], e_nonce, r_nonce,
  7889. msg_type=WPS_WSC_NACK, eap_code=2)
  7890. send_wsc_msg(hapd, addr, ack)
  7891. wps_wait_eap_failure(hapd, dev[0])
  7892. @remote_compatible
  7893. def test_wps_ext_m2_ack_e_nonce_mismatch(dev, apdev):
  7894. """WPS proto: M2 followed by ACK with e-nonce mismatch"""
  7895. pin = "12345670"
  7896. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7897. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7898. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7899. logger.debug("Receive WSC/Start from AP")
  7900. msg = get_wsc_msg(hapd)
  7901. if msg['wsc_opcode'] != WSC_Start:
  7902. raise Exception("Unexpected Op-Code for WSC/Start")
  7903. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7904. uuid_e = 16*'\x11'
  7905. e_nonce = 16*'\x22'
  7906. own_private, e_pk = wsc_dh_init()
  7907. logger.debug("Send M1 to AP")
  7908. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7909. e_nonce, e_pk)
  7910. send_wsc_msg(hapd, addr, m1)
  7911. logger.debug("Receive M2 from AP")
  7912. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7913. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7914. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7915. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7916. r_nonce)
  7917. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7918. logger.debug("Send WSC_ACK to AP")
  7919. ack,attrs = build_ack(msg['eap_identifier'], 16*'\x00', r_nonce,
  7920. eap_code=2)
  7921. send_wsc_msg(hapd, addr, ack)
  7922. wps_wait_eap_failure(hapd, dev[0])
  7923. @remote_compatible
  7924. def test_wps_ext_m1_invalid(dev, apdev):
  7925. """WPS proto: M1 failing parsing"""
  7926. pin = "12345670"
  7927. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7928. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7929. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7930. logger.debug("Receive WSC/Start from AP")
  7931. msg = get_wsc_msg(hapd)
  7932. if msg['wsc_opcode'] != WSC_Start:
  7933. raise Exception("Unexpected Op-Code for WSC/Start")
  7934. logger.debug("Send M1 to AP")
  7935. attrs = '\x10\x00\x00'
  7936. m1 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7937. send_wsc_msg(hapd, addr, m1)
  7938. wps_wait_eap_failure(hapd, dev[0])
  7939. def test_wps_ext_m1_missing_msg_type(dev, apdev):
  7940. """WPS proto: M1 missing Msg Type"""
  7941. pin = "12345670"
  7942. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7943. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7944. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7945. logger.debug("Receive WSC/Start from AP")
  7946. msg = get_wsc_msg(hapd)
  7947. if msg['wsc_opcode'] != WSC_Start:
  7948. raise Exception("Unexpected Op-Code for WSC/Start")
  7949. logger.debug("Send M1 to AP")
  7950. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7951. m1 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7952. send_wsc_msg(hapd, addr, m1)
  7953. wps_wait_ap_nack(hapd, dev[0], 16*'\x00', 16*'\x00')
  7954. def wps_ext_wsc_done(dev, apdev):
  7955. pin = "12345670"
  7956. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7957. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7958. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7959. logger.debug("Receive WSC/Start from AP")
  7960. msg = get_wsc_msg(hapd)
  7961. if msg['wsc_opcode'] != WSC_Start:
  7962. raise Exception("Unexpected Op-Code for WSC/Start")
  7963. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7964. uuid_e = 16*'\x11'
  7965. e_nonce = 16*'\x22'
  7966. own_private, e_pk = wsc_dh_init()
  7967. logger.debug("Send M1 to AP")
  7968. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7969. e_nonce, e_pk)
  7970. send_wsc_msg(hapd, addr, m1)
  7971. logger.debug("Receive M2 from AP")
  7972. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7973. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7974. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7975. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7976. r_nonce)
  7977. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7978. logger.debug("Send M3 to AP")
  7979. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7980. attrs += build_attr_msg_type(WPS_M3)
  7981. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7982. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7983. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7984. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7985. raw_m3_attrs = attrs
  7986. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7987. send_wsc_msg(hapd, addr, m3)
  7988. logger.debug("Receive M4 from AP")
  7989. msg, m4_attrs, raw_m4_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M4)
  7990. logger.debug("Send M5 to AP")
  7991. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7992. attrs += build_attr_msg_type(WPS_M5)
  7993. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7994. data = build_wsc_attr(ATTR_E_SNONCE1, e_s1)
  7995. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7996. attrs += build_attr_authenticator(authkey, raw_m4_attrs, attrs)
  7997. raw_m5_attrs = attrs
  7998. m5 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7999. send_wsc_msg(hapd, addr, m5)
  8000. logger.debug("Receive M6 from AP")
  8001. msg, m6_attrs, raw_m6_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M6)
  8002. logger.debug("Send M7 to AP")
  8003. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  8004. attrs += build_attr_msg_type(WPS_M7)
  8005. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  8006. data = build_wsc_attr(ATTR_E_SNONCE2, e_s2)
  8007. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  8008. attrs += build_attr_authenticator(authkey, raw_m6_attrs, attrs)
  8009. m7 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  8010. raw_m7_attrs = attrs
  8011. send_wsc_msg(hapd, addr, m7)
  8012. logger.debug("Receive M8 from AP")
  8013. msg, m8_attrs, raw_m8_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M8)
  8014. return hapd, msg, e_nonce, r_nonce
  8015. @remote_compatible
  8016. def test_wps_ext_wsc_done_invalid(dev, apdev):
  8017. """WPS proto: invalid WSC_Done"""
  8018. hapd, msg, e_nonce, r_nonce = wps_ext_wsc_done(dev, apdev)
  8019. logger.debug("Send WSC_Done to AP")
  8020. attrs = '\x10\x00\x00'
  8021. wsc_done = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_Done)
  8022. send_wsc_msg(hapd, dev[0].own_addr(), wsc_done)
  8023. wps_wait_eap_failure(hapd, dev[0])
  8024. @remote_compatible
  8025. def test_wps_ext_wsc_done_no_msg_type(dev, apdev):
  8026. """WPS proto: invalid WSC_Done"""
  8027. hapd, msg, e_nonce, r_nonce = wps_ext_wsc_done(dev, apdev)
  8028. logger.debug("Send WSC_Done to AP")
  8029. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  8030. #attrs += build_attr_msg_type(WPS_WSC_DONE)
  8031. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  8032. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  8033. wsc_done = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_Done)
  8034. send_wsc_msg(hapd, dev[0].own_addr(), wsc_done)
  8035. wps_wait_eap_failure(hapd, dev[0])
  8036. @remote_compatible
  8037. def test_wps_ext_wsc_done_wrong_msg_type(dev, apdev):
  8038. """WPS proto: WSC_Done with wrong Msg Type"""
  8039. hapd, msg, e_nonce, r_nonce = wps_ext_wsc_done(dev, apdev)
  8040. logger.debug("Send WSC_Done to AP")
  8041. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  8042. attrs += build_attr_msg_type(WPS_WSC_ACK)
  8043. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  8044. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  8045. wsc_done = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_Done)
  8046. send_wsc_msg(hapd, dev[0].own_addr(), wsc_done)
  8047. wps_wait_eap_failure(hapd, dev[0])
  8048. @remote_compatible
  8049. def test_wps_ext_wsc_done_no_e_nonce(dev, apdev):
  8050. """WPS proto: WSC_Done without e_nonce"""
  8051. hapd, msg, e_nonce, r_nonce = wps_ext_wsc_done(dev, apdev)
  8052. logger.debug("Send WSC_Done to AP")
  8053. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  8054. attrs += build_attr_msg_type(WPS_WSC_DONE)
  8055. #attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  8056. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  8057. wsc_done = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_Done)
  8058. send_wsc_msg(hapd, dev[0].own_addr(), wsc_done)
  8059. wps_wait_eap_failure(hapd, dev[0])
  8060. def test_wps_ext_wsc_done_no_r_nonce(dev, apdev):
  8061. """WPS proto: WSC_Done without r_nonce"""
  8062. hapd, msg, e_nonce, r_nonce = wps_ext_wsc_done(dev, apdev)
  8063. logger.debug("Send WSC_Done to AP")
  8064. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  8065. attrs += build_attr_msg_type(WPS_WSC_DONE)
  8066. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  8067. #attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  8068. wsc_done = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_Done)
  8069. send_wsc_msg(hapd, dev[0].own_addr(), wsc_done)
  8070. wps_wait_eap_failure(hapd, dev[0])
  8071. @remote_compatible
  8072. def test_wps_ext_m7_no_encr_settings(dev, apdev):
  8073. """WPS proto: M7 without Encr Settings"""
  8074. pin = "12345670"
  8075. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  8076. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  8077. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  8078. logger.debug("Receive WSC/Start from AP")
  8079. msg = get_wsc_msg(hapd)
  8080. if msg['wsc_opcode'] != WSC_Start:
  8081. raise Exception("Unexpected Op-Code for WSC/Start")
  8082. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  8083. uuid_e = 16*'\x11'
  8084. e_nonce = 16*'\x22'
  8085. own_private, e_pk = wsc_dh_init()
  8086. logger.debug("Send M1 to AP")
  8087. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  8088. e_nonce, e_pk)
  8089. send_wsc_msg(hapd, addr, m1)
  8090. logger.debug("Receive M2 from AP")
  8091. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  8092. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  8093. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  8094. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  8095. r_nonce)
  8096. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  8097. logger.debug("Send M3 to AP")
  8098. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  8099. attrs += build_attr_msg_type(WPS_M3)
  8100. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  8101. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  8102. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  8103. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  8104. raw_m3_attrs = attrs
  8105. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  8106. send_wsc_msg(hapd, addr, m3)
  8107. logger.debug("Receive M4 from AP")
  8108. msg, m4_attrs, raw_m4_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M4)
  8109. logger.debug("Send M5 to AP")
  8110. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  8111. attrs += build_attr_msg_type(WPS_M5)
  8112. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  8113. data = build_wsc_attr(ATTR_E_SNONCE1, e_s1)
  8114. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  8115. attrs += build_attr_authenticator(authkey, raw_m4_attrs, attrs)
  8116. raw_m5_attrs = attrs
  8117. m5 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  8118. send_wsc_msg(hapd, addr, m5)
  8119. logger.debug("Receive M6 from AP")
  8120. msg, m6_attrs, raw_m6_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M6)
  8121. logger.debug("Send M7 to AP")
  8122. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  8123. attrs += build_attr_msg_type(WPS_M7)
  8124. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  8125. #data = build_wsc_attr(ATTR_E_SNONCE2, e_s2)
  8126. #attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  8127. attrs += build_attr_authenticator(authkey, raw_m6_attrs, attrs)
  8128. m7 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  8129. raw_m7_attrs = attrs
  8130. send_wsc_msg(hapd, addr, m7)
  8131. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  8132. @remote_compatible
  8133. def test_wps_ext_m1_workaround(dev, apdev):
  8134. """WPS proto: M1 Manufacturer/Model workaround"""
  8135. pin = "12345670"
  8136. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  8137. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  8138. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  8139. logger.debug("Receive WSC/Start from AP")
  8140. msg = get_wsc_msg(hapd)
  8141. if msg['wsc_opcode'] != WSC_Start:
  8142. raise Exception("Unexpected Op-Code for WSC/Start")
  8143. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  8144. uuid_e = 16*'\x11'
  8145. e_nonce = 16*'\x22'
  8146. own_private, e_pk = wsc_dh_init()
  8147. logger.debug("Send M1 to AP")
  8148. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  8149. e_nonce, e_pk, manufacturer='Apple TEST',
  8150. model_name='AirPort', config_methods='\xff\xff')
  8151. send_wsc_msg(hapd, addr, m1)
  8152. logger.debug("Receive M2 from AP")
  8153. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  8154. @remote_compatible
  8155. def test_ap_wps_disable_enable(dev, apdev):
  8156. """WPS and DISABLE/ENABLE AP"""
  8157. hapd = wps_start_ap(apdev[0])
  8158. hapd.disable()
  8159. hapd.enable()
  8160. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  8161. def test_ap_wps_upnp_web_oom(dev, apdev, params):
  8162. """hostapd WPS UPnP web OOM"""
  8163. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  8164. hapd = add_ssdp_ap(apdev[0], ap_uuid)
  8165. location = ssdp_get_location(ap_uuid)
  8166. url = urlparse.urlparse(location)
  8167. urls = upnp_get_urls(location)
  8168. eventurl = urlparse.urlparse(urls['event_sub_url'])
  8169. ctrlurl = urlparse.urlparse(urls['control_url'])
  8170. conn = httplib.HTTPConnection(url.netloc)
  8171. with alloc_fail(hapd, 1, "web_connection_parse_get"):
  8172. conn.request("GET", "/wps_device.xml")
  8173. try:
  8174. resp = conn.getresponse()
  8175. except:
  8176. pass
  8177. conn = httplib.HTTPConnection(url.netloc)
  8178. conn.request("GET", "/unknown")
  8179. resp = conn.getresponse()
  8180. if resp.status != 404:
  8181. raise Exception("Unexpected HTTP result for unknown URL: %d" + resp.status)
  8182. with alloc_fail(hapd, 1, "web_connection_parse_get"):
  8183. conn.request("GET", "/unknown")
  8184. try:
  8185. resp = conn.getresponse()
  8186. print resp.status
  8187. except:
  8188. pass
  8189. conn = httplib.HTTPConnection(url.netloc)
  8190. conn.request("GET", "/wps_device.xml")
  8191. resp = conn.getresponse()
  8192. if resp.status != 200:
  8193. raise Exception("GET /wps_device.xml failed")
  8194. conn = httplib.HTTPConnection(url.netloc)
  8195. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  8196. if resp.status != 200:
  8197. raise Exception("GetDeviceInfo failed")
  8198. with alloc_fail(hapd, 1, "web_process_get_device_info"):
  8199. conn = httplib.HTTPConnection(url.netloc)
  8200. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  8201. if resp.status != 500:
  8202. raise Exception("Internal error not reported from GetDeviceInfo OOM")
  8203. with alloc_fail(hapd, 1, "wps_build_m1;web_process_get_device_info"):
  8204. conn = httplib.HTTPConnection(url.netloc)
  8205. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  8206. if resp.status != 500:
  8207. raise Exception("Internal error not reported from GetDeviceInfo OOM")
  8208. with alloc_fail(hapd, 1, "wpabuf_alloc;web_connection_send_reply"):
  8209. conn = httplib.HTTPConnection(url.netloc)
  8210. try:
  8211. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  8212. except:
  8213. pass
  8214. conn = httplib.HTTPConnection(url.netloc)
  8215. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  8216. if resp.status != 200:
  8217. raise Exception("GetDeviceInfo failed")
  8218. # No NewWLANEventType in PutWLANResponse NewMessage
  8219. conn = httplib.HTTPConnection(url.netloc)
  8220. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse", newmsg="foo")
  8221. if resp.status != 600:
  8222. raise Exception("Unexpected HTTP response: %d" % resp.status)
  8223. # No NewWLANEventMAC in PutWLANResponse NewMessage
  8224. conn = httplib.HTTPConnection(url.netloc)
  8225. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse",
  8226. newmsg="foo", neweventtype="1")
  8227. if resp.status != 600:
  8228. raise Exception("Unexpected HTTP response: %d" % resp.status)
  8229. # Invalid NewWLANEventMAC in PutWLANResponse NewMessage
  8230. conn = httplib.HTTPConnection(url.netloc)
  8231. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse",
  8232. newmsg="foo", neweventtype="1",
  8233. neweventmac="foo")
  8234. if resp.status != 600:
  8235. raise Exception("Unexpected HTTP response: %d" % resp.status)
  8236. # Workaround for NewWLANEventMAC in PutWLANResponse NewMessage
  8237. # Ignored unexpected PutWLANResponse WLANEventType 1
  8238. conn = httplib.HTTPConnection(url.netloc)
  8239. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse",
  8240. newmsg="foo", neweventtype="1",
  8241. neweventmac="00.11.22.33.44.55")
  8242. if resp.status != 500:
  8243. raise Exception("Unexpected HTTP response: %d" % resp.status)
  8244. # PutWLANResponse NewMessage with invalid EAP message
  8245. conn = httplib.HTTPConnection(url.netloc)
  8246. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse",
  8247. newmsg="foo", neweventtype="2",
  8248. neweventmac="00:11:22:33:44:55")
  8249. if resp.status != 200:
  8250. raise Exception("Unexpected HTTP response: %d" % resp.status)
  8251. with alloc_fail(hapd, 1, "web_connection_parse_subscribe"):
  8252. conn = httplib.HTTPConnection(url.netloc)
  8253. headers = { "callback": '<http://127.0.0.1:12345/event>',
  8254. "NT": "upnp:event",
  8255. "timeout": "Second-1234" }
  8256. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  8257. try:
  8258. resp = conn.getresponse()
  8259. except:
  8260. pass
  8261. with alloc_fail(hapd, 1, "dup_binstr;web_connection_parse_subscribe"):
  8262. conn = httplib.HTTPConnection(url.netloc)
  8263. headers = { "callback": '<http://127.0.0.1:12345/event>',
  8264. "NT": "upnp:event",
  8265. "timeout": "Second-1234" }
  8266. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  8267. resp = conn.getresponse()
  8268. if resp.status != 500:
  8269. raise Exception("Unexpected HTTP response: %d" % resp.status)
  8270. with alloc_fail(hapd, 1, "wpabuf_alloc;web_connection_parse_unsubscribe"):
  8271. conn = httplib.HTTPConnection(url.netloc)
  8272. headers = { "callback": '<http://127.0.0.1:12345/event>',
  8273. "NT": "upnp:event",
  8274. "timeout": "Second-1234" }
  8275. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  8276. try:
  8277. resp = conn.getresponse()
  8278. except:
  8279. pass
  8280. with alloc_fail(hapd, 1, "web_connection_unimplemented"):
  8281. conn = httplib.HTTPConnection(url.netloc)
  8282. conn.request("HEAD", "/wps_device.xml")
  8283. try:
  8284. resp = conn.getresponse()
  8285. except:
  8286. pass
  8287. def test_ap_wps_frag_ack_oom(dev, apdev):
  8288. """WPS and fragment ack OOM"""
  8289. dev[0].request("SET wps_fragment_size 50")
  8290. hapd = wps_start_ap(apdev[0])
  8291. with alloc_fail(hapd, 1, "eap_wsc_build_frag_ack"):
  8292. wps_run_pbc_fail_ap(apdev[0], dev[0], hapd)
  8293. def wait_scan_stopped(dev):
  8294. dev.request("ABORT_SCAN")
  8295. for i in range(50):
  8296. res = dev.get_driver_status_field("scan_state")
  8297. if "SCAN_STARTED" not in res and "SCAN_REQUESTED" not in res:
  8298. break
  8299. logger.debug("Waiting for scan to complete")
  8300. time.sleep(0.1)
  8301. @remote_compatible
  8302. def test_ap_wps_eap_wsc_errors(dev, apdev):
  8303. """WPS and EAP-WSC error cases"""
  8304. ssid = "test-wps-conf-pin"
  8305. appin = "12345670"
  8306. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  8307. "wpa_passphrase": "12345678", "wpa": "2",
  8308. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  8309. "fragment_size": "300", "ap_pin": appin }
  8310. hapd = hostapd.add_ap(apdev[0], params)
  8311. bssid = apdev[0]['bssid']
  8312. pin = dev[0].wps_read_pin()
  8313. hapd.request("WPS_PIN any " + pin)
  8314. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  8315. dev[0].dump_monitor()
  8316. dev[0].wps_reg(bssid, appin + " new_ssid=a", "new ssid", "WPA2PSK", "CCMP",
  8317. "new passphrase", no_wait=True)
  8318. ev = dev[0].wait_event(["WPS-FAIL"], timeout=10)
  8319. if ev is None:
  8320. raise Exception("WPS-FAIL not reported")
  8321. dev[0].request("WPS_CANCEL")
  8322. dev[0].wait_disconnected()
  8323. wait_scan_stopped(dev[0])
  8324. dev[0].dump_monitor()
  8325. dev[0].wps_reg(bssid, appin, "new ssid", "FOO", "CCMP",
  8326. "new passphrase", no_wait=True)
  8327. ev = dev[0].wait_event(["WPS-FAIL"], timeout=10)
  8328. if ev is None:
  8329. raise Exception("WPS-FAIL not reported")
  8330. dev[0].request("WPS_CANCEL")
  8331. dev[0].wait_disconnected()
  8332. wait_scan_stopped(dev[0])
  8333. dev[0].dump_monitor()
  8334. dev[0].wps_reg(bssid, appin, "new ssid", "WPA2PSK", "FOO",
  8335. "new passphrase", no_wait=True)
  8336. ev = dev[0].wait_event(["WPS-FAIL"], timeout=10)
  8337. if ev is None:
  8338. raise Exception("WPS-FAIL not reported")
  8339. dev[0].request("WPS_CANCEL")
  8340. dev[0].wait_disconnected()
  8341. wait_scan_stopped(dev[0])
  8342. dev[0].dump_monitor()
  8343. dev[0].wps_reg(bssid, appin + "new_key=a", "new ssid", "WPA2PSK", "CCMP",
  8344. "new passphrase", no_wait=True)
  8345. ev = dev[0].wait_event(["WPS-FAIL"], timeout=10)
  8346. if ev is None:
  8347. raise Exception("WPS-FAIL not reported")
  8348. dev[0].request("WPS_CANCEL")
  8349. dev[0].wait_disconnected()
  8350. wait_scan_stopped(dev[0])
  8351. dev[0].dump_monitor()
  8352. tests = [ "eap_wsc_init",
  8353. "eap_msg_alloc;eap_wsc_build_msg",
  8354. "wpabuf_alloc;eap_wsc_process_fragment" ]
  8355. for func in tests:
  8356. with alloc_fail(dev[0], 1, func):
  8357. dev[0].request("WPS_PIN %s %s" % (bssid, pin))
  8358. wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
  8359. dev[0].request("WPS_CANCEL")
  8360. dev[0].wait_disconnected()
  8361. wait_scan_stopped(dev[0])
  8362. dev[0].dump_monitor()
  8363. with alloc_fail(dev[0], 1, "eap_msg_alloc;eap_sm_build_expanded_nak"):
  8364. dev[0].wps_reg(bssid, appin + " new_ssid=a", "new ssid", "WPA2PSK",
  8365. "CCMP", "new passphrase", no_wait=True)
  8366. wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
  8367. dev[0].request("WPS_CANCEL")
  8368. dev[0].wait_disconnected()
  8369. wait_scan_stopped(dev[0])
  8370. dev[0].dump_monitor()
  8371. def test_ap_wps_eap_wsc(dev, apdev):
  8372. """WPS and EAP-WSC in network profile"""
  8373. params = int_eap_server_params()
  8374. params["wps_state"] = "2"
  8375. hapd = hostapd.add_ap(apdev[0], params)
  8376. bssid = apdev[0]['bssid']
  8377. logger.info("Unexpected identity")
  8378. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8379. eap="WSC", identity="WFA-SimpleConfig-Enrollee-unexpected",
  8380. wait_connect=False)
  8381. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8382. if ev is None:
  8383. raise Exception("No EAP-Failure seen")
  8384. dev[0].request("REMOVE_NETWORK all")
  8385. dev[0].wait_disconnected()
  8386. logger.info("No phase1 parameter")
  8387. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8388. eap="WSC", identity="WFA-SimpleConfig-Enrollee-1-0",
  8389. wait_connect=False)
  8390. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=5)
  8391. if ev is None:
  8392. raise Exception("Timeout on EAP method start")
  8393. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8394. if ev is None:
  8395. raise Exception("No EAP-Failure seen")
  8396. dev[0].request("REMOVE_NETWORK all")
  8397. dev[0].wait_disconnected()
  8398. logger.info("No PIN/PBC in phase1")
  8399. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8400. eap="WSC", identity="WFA-SimpleConfig-Enrollee-1-0",
  8401. phase1="foo", wait_connect=False)
  8402. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=5)
  8403. if ev is None:
  8404. raise Exception("Timeout on EAP method start")
  8405. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8406. if ev is None:
  8407. raise Exception("No EAP-Failure seen")
  8408. dev[0].request("REMOVE_NETWORK all")
  8409. dev[0].wait_disconnected()
  8410. logger.info("Invalid pkhash in phase1")
  8411. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8412. eap="WSC", identity="WFA-SimpleConfig-Enrollee-1-0",
  8413. phase1="foo pkhash=q pbc=1", wait_connect=False)
  8414. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=5)
  8415. if ev is None:
  8416. raise Exception("Timeout on EAP method start")
  8417. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8418. if ev is None:
  8419. raise Exception("No EAP-Failure seen")
  8420. dev[0].request("REMOVE_NETWORK all")
  8421. dev[0].wait_disconnected()
  8422. logger.info("Zero fragment_size")
  8423. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8424. eap="WSC", identity="WFA-SimpleConfig-Enrollee-1-0",
  8425. fragment_size="0", phase1="pin=12345670", wait_connect=False)
  8426. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=5)
  8427. if ev is None:
  8428. raise Exception("Timeout on EAP method start")
  8429. ev = dev[0].wait_event(["WPS-M2D"], timeout=5)
  8430. if ev is None:
  8431. raise Exception("No M2D seen")
  8432. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8433. if ev is None:
  8434. raise Exception("No EAP-Failure seen")
  8435. dev[0].request("REMOVE_NETWORK all")
  8436. dev[0].wait_disconnected()
  8437. logger.info("Missing new_auth")
  8438. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8439. eap="WSC", identity="WFA-SimpleConfig-Enrollee-1-0",
  8440. phase1="pin=12345670 new_ssid=aa", wait_connect=False)
  8441. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=5)
  8442. if ev is None:
  8443. raise Exception("Timeout on EAP method start")
  8444. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8445. if ev is None:
  8446. raise Exception("No EAP-Failure seen")
  8447. dev[0].request("REMOVE_NETWORK all")
  8448. dev[0].wait_disconnected()
  8449. logger.info("Missing new_encr")
  8450. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8451. eap="WSC", identity="WFA-SimpleConfig-Enrollee-1-0",
  8452. phase1="pin=12345670 new_auth=WPA2PSK new_ssid=aa", wait_connect=False)
  8453. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=5)
  8454. if ev is None:
  8455. raise Exception("Timeout on EAP method start")
  8456. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8457. if ev is None:
  8458. raise Exception("No EAP-Failure seen")
  8459. dev[0].request("REMOVE_NETWORK all")
  8460. dev[0].wait_disconnected()
  8461. logger.info("Missing new_key")
  8462. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8463. eap="WSC", identity="WFA-SimpleConfig-Enrollee-1-0",
  8464. phase1="pin=12345670 new_auth=WPA2PSK new_ssid=aa new_encr=CCMP",
  8465. wait_connect=False)
  8466. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=5)
  8467. if ev is None:
  8468. raise Exception("Timeout on EAP method start")
  8469. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8470. if ev is None:
  8471. raise Exception("No EAP-Failure seen")
  8472. dev[0].request("REMOVE_NETWORK all")
  8473. dev[0].wait_disconnected()
  8474. def test_ap_wps_and_bss_limit(dev, apdev):
  8475. """WPS and wpa_supplicant BSS entry limit"""
  8476. try:
  8477. _test_ap_wps_and_bss_limit(dev, apdev)
  8478. finally:
  8479. dev[0].request("SET bss_max_count 200")
  8480. pass
  8481. def _test_ap_wps_and_bss_limit(dev, apdev):
  8482. params = { "ssid": "test-wps", "eap_server": "1", "wps_state": "2",
  8483. "wpa_passphrase": "12345678", "wpa": "2",
  8484. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" }
  8485. hapd = hostapd.add_ap(apdev[0], params)
  8486. params = { "ssid": "test-wps-2", "eap_server": "1", "wps_state": "2",
  8487. "wpa_passphrase": "1234567890", "wpa": "2",
  8488. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" }
  8489. hapd2 = hostapd.add_ap(apdev[1], params)
  8490. id = dev[1].add_network()
  8491. dev[1].set_network(id, "mode", "2")
  8492. dev[1].set_network_quoted(id, "ssid", "wpas-ap-no-wps")
  8493. dev[1].set_network_quoted(id, "psk", "12345678")
  8494. dev[1].set_network(id, "frequency", "2462")
  8495. dev[1].set_network(id, "scan_freq", "2462")
  8496. dev[1].set_network(id, "wps_disabled", "1")
  8497. dev[1].select_network(id)
  8498. id = dev[2].add_network()
  8499. dev[2].set_network(id, "mode", "2")
  8500. dev[2].set_network_quoted(id, "ssid", "wpas-ap")
  8501. dev[2].set_network_quoted(id, "psk", "12345678")
  8502. dev[2].set_network(id, "frequency", "2437")
  8503. dev[2].set_network(id, "scan_freq", "2437")
  8504. dev[2].select_network(id)
  8505. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  8506. wpas.interface_add("wlan5")
  8507. id = wpas.add_network()
  8508. wpas.set_network(id, "mode", "2")
  8509. wpas.set_network_quoted(id, "ssid", "wpas-ap")
  8510. wpas.set_network_quoted(id, "psk", "12345678")
  8511. wpas.set_network(id, "frequency", "2437")
  8512. wpas.set_network(id, "scan_freq", "2437")
  8513. wpas.select_network(id)
  8514. dev[1].wait_connected()
  8515. dev[2].wait_connected()
  8516. wpas.wait_connected()
  8517. wpas.request("WPS_PIN any 12345670")
  8518. hapd.request("WPS_PBC")
  8519. hapd2.request("WPS_PBC")
  8520. dev[0].request("SET bss_max_count 1")
  8521. id = dev[0].add_network()
  8522. dev[0].set_network_quoted(id, "ssid", "testing")
  8523. id = dev[0].add_network()
  8524. dev[0].set_network_quoted(id, "ssid", "testing")
  8525. dev[0].set_network(id, "key_mgmt", "WPS")
  8526. dev[0].request("WPS_PBC")
  8527. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
  8528. dev[0].request("WPS_CANCEL")
  8529. id = dev[0].add_network()
  8530. dev[0].set_network_quoted(id, "ssid", "testing")
  8531. dev[0].set_network(id, "key_mgmt", "WPS")
  8532. dev[0].scan(freq="2412")
  8533. def test_ap_wps_pbc_2ap(dev, apdev):
  8534. """WPS PBC with two APs advertising same SSID"""
  8535. params = { "ssid": "wps", "eap_server": "1", "wps_state": "2",
  8536. "wpa_passphrase": "12345678", "wpa": "2",
  8537. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  8538. "wps_independent": "1"}
  8539. hapd = hostapd.add_ap(apdev[0], params)
  8540. params = { "ssid": "wps", "eap_server": "1", "wps_state": "2",
  8541. "wpa_passphrase": "123456789", "wpa": "2",
  8542. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  8543. "wps_independent": "1"}
  8544. hapd2 = hostapd.add_ap(apdev[1], params)
  8545. hapd.request("WPS_PBC")
  8546. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  8547. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  8548. wpas.dump_monitor()
  8549. wpas.scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  8550. wpas.scan_for_bss(apdev[1]['bssid'], freq="2412")
  8551. wpas.request("WPS_PBC")
  8552. wpas.wait_connected()
  8553. wpas.request("DISCONNECT")
  8554. hapd.request("DISABLE")
  8555. hapd2.request("DISABLE")
  8556. wpas.flush_scan_cache()
  8557. def test_ap_wps_er_enrollee_to_conf_ap(dev, apdev):
  8558. """WPS ER enrolling a new device to a configured AP"""
  8559. try:
  8560. _test_ap_wps_er_enrollee_to_conf_ap(dev, apdev)
  8561. finally:
  8562. dev[0].request("WPS_ER_STOP")
  8563. def _test_ap_wps_er_enrollee_to_conf_ap(dev, apdev):
  8564. ssid = "wps-er-enrollee-to-conf-ap"
  8565. ap_pin = "12345670"
  8566. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  8567. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  8568. "wpa_passphrase": "12345678", "wpa": "2",
  8569. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  8570. "device_name": "Wireless AP", "manufacturer": "Company",
  8571. "model_name": "WAP", "model_number": "123",
  8572. "serial_number": "12345", "device_type": "6-0050F204-1",
  8573. "os_version": "01020300",
  8574. "config_methods": "label push_button",
  8575. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"}
  8576. hapd = hostapd.add_ap(apdev[0], params)
  8577. bssid = hapd.own_addr()
  8578. id = dev[0].connect(ssid, psk="12345678", scan_freq="2412")
  8579. dev[0].dump_monitor()
  8580. dev[0].request("WPS_ER_START ifname=lo")
  8581. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  8582. if ev is None:
  8583. raise Exception("AP discovery timed out")
  8584. if ap_uuid not in ev:
  8585. raise Exception("Expected AP UUID not found")
  8586. pin = dev[2].wps_read_pin()
  8587. addr2 = dev[2].own_addr()
  8588. dev[0].dump_monitor()
  8589. dev[2].scan_for_bss(bssid, freq=2412)
  8590. dev[2].dump_monitor()
  8591. dev[2].request("WPS_PIN %s %s" % (bssid, pin))
  8592. for i in range(3):
  8593. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=10)
  8594. if ev is None:
  8595. raise Exception("Enrollee not seen")
  8596. if addr2 in ev:
  8597. break
  8598. if addr2 not in ev:
  8599. raise Exception("Unexpected Enrollee MAC address")
  8600. dev[0].dump_monitor()
  8601. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " " + str(id))
  8602. dev[0].request("WPS_ER_PIN " + addr2 + " " + pin + " " + addr2)
  8603. dev[2].wait_connected(timeout=30)
  8604. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  8605. if ev is None:
  8606. raise Exception("WPS ER did not report success")
  8607. def test_ap_wps_er_enrollee_to_conf_ap2(dev, apdev):
  8608. """WPS ER enrolling a new device to a configured AP (2)"""
  8609. try:
  8610. _test_ap_wps_er_enrollee_to_conf_ap2(dev, apdev)
  8611. finally:
  8612. dev[0].request("WPS_ER_STOP")
  8613. def _test_ap_wps_er_enrollee_to_conf_ap2(dev, apdev):
  8614. ssid = "wps-er-enrollee-to-conf-ap"
  8615. ap_pin = "12345670"
  8616. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  8617. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  8618. "wpa_passphrase": "12345678", "wpa": "2",
  8619. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  8620. "device_name": "Wireless AP", "manufacturer": "Company",
  8621. "model_name": "WAP", "model_number": "123",
  8622. "serial_number": "12345", "device_type": "6-0050F204-1",
  8623. "os_version": "01020300",
  8624. "config_methods": "label push_button",
  8625. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"}
  8626. hapd = hostapd.add_ap(apdev[0], params)
  8627. bssid = hapd.own_addr()
  8628. id = dev[0].connect(ssid, psk="12345678", scan_freq="2412")
  8629. dev[0].dump_monitor()
  8630. dev[0].request("WPS_ER_START ifname=lo")
  8631. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  8632. if ev is None:
  8633. raise Exception("AP discovery timed out")
  8634. if ap_uuid not in ev:
  8635. raise Exception("Expected AP UUID not found")
  8636. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  8637. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  8638. if ev is None:
  8639. raise Exception("AP learn timed out")
  8640. if ap_uuid not in ev:
  8641. raise Exception("Expected AP UUID not in settings")
  8642. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  8643. if ev is None:
  8644. raise Exception("WPS-FAIL after AP learn timed out")
  8645. time.sleep(0.1)
  8646. pin = dev[1].wps_read_pin()
  8647. addr1 = dev[1].own_addr()
  8648. dev[0].dump_monitor()
  8649. dev[0].request("WPS_ER_PIN any " + pin)
  8650. time.sleep(0.1)
  8651. dev[1].scan_for_bss(bssid, freq=2412)
  8652. dev[1].request("WPS_PIN any %s" % pin)
  8653. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=30)
  8654. if ev is None:
  8655. raise Exception("Enrollee did not report success")
  8656. dev[1].wait_connected(timeout=15)
  8657. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  8658. if ev is None:
  8659. raise Exception("WPS ER did not report success")
  8660. def test_ap_wps_ignore_broadcast_ssid(dev, apdev):
  8661. """WPS AP trying to ignore broadcast SSID"""
  8662. ssid = "test-wps"
  8663. hapd = hostapd.add_ap(apdev[0],
  8664. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  8665. "ignore_broadcast_ssid": "1" })
  8666. if "FAIL" not in hapd.request("WPS_PBC"):
  8667. raise Exception("WPS unexpectedly enabled")
  8668. def test_ap_wps_wep(dev, apdev):
  8669. """WPS AP trying to enable WEP"""
  8670. ssid = "test-wps"
  8671. hapd = hostapd.add_ap(apdev[0],
  8672. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  8673. "ieee80211n": "0", "wep_key0": '"hello"' })
  8674. if "FAIL" not in hapd.request("WPS_PBC"):
  8675. raise Exception("WPS unexpectedly enabled")
  8676. def test_ap_wps_tkip(dev, apdev):
  8677. """WPS AP trying to enable TKIP"""
  8678. ssid = "test-wps"
  8679. hapd = hostapd.add_ap(apdev[0],
  8680. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  8681. "ieee80211n": "0", "wpa": '1',
  8682. "wpa_key_mgmt": "WPA-PSK",
  8683. "wpa_passphrase": "12345678" })
  8684. if "FAIL" not in hapd.request("WPS_PBC"):
  8685. raise Exception("WPS unexpectedly enabled")
  8686. def test_ap_wps_conf_dummy_cred(dev, apdev):
  8687. """WPS PIN provisioning with configured AP using dummy cred"""
  8688. ssid = "test-wps-conf"
  8689. hapd = hostapd.add_ap(apdev[0],
  8690. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  8691. "wpa_passphrase": "12345678", "wpa": "2",
  8692. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  8693. hapd.request("WPS_PIN any 12345670")
  8694. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  8695. dev[0].dump_monitor()
  8696. try:
  8697. hapd.set("wps_testing_dummy_cred", "1")
  8698. dev[0].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  8699. for i in range(1, 3):
  8700. ev = dev[0].wait_event(["WPS-CRED-RECEIVED"], timeout=15)
  8701. if ev is None:
  8702. raise Exception("WPS credential %d not received" % i)
  8703. dev[0].wait_connected(timeout=30)
  8704. finally:
  8705. hapd.set("wps_testing_dummy_cred", "0")
  8706. def test_ap_wps_rf_bands(dev, apdev):
  8707. """WPS and wps_rf_bands configuration"""
  8708. ssid = "test-wps-conf"
  8709. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  8710. "wpa_passphrase": "12345678", "wpa": "2",
  8711. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  8712. "wps_rf_bands": "ag" }
  8713. hapd = hostapd.add_ap(apdev[0], params)
  8714. bssid = hapd.own_addr()
  8715. hapd.request("WPS_PBC")
  8716. dev[0].scan_for_bss(bssid, freq="2412")
  8717. dev[0].dump_monitor()
  8718. dev[0].request("WPS_PBC " + bssid)
  8719. dev[0].wait_connected(timeout=30)
  8720. bss = dev[0].get_bss(bssid)
  8721. logger.info("BSS: " + str(bss))
  8722. if "103c000103" not in bss['ie']:
  8723. raise Exception("RF Bands attribute with expected values not found")
  8724. dev[0].request("DISCONNECT")
  8725. dev[0].wait_disconnected()
  8726. hapd.set("wps_rf_bands", "ad")
  8727. hapd.set("wps_rf_bands", "a")
  8728. hapd.set("wps_rf_bands", "g")
  8729. hapd.set("wps_rf_bands", "b")
  8730. hapd.set("wps_rf_bands", "ga")
  8731. hapd.disable()
  8732. dev[0].dump_monitor()
  8733. dev[0].flush_scan_cache()
  8734. def test_ap_wps_pbc_in_m1(dev, apdev):
  8735. """WPS and pbc_in_m1"""
  8736. ssid = "test-wps-conf"
  8737. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  8738. "wpa_passphrase": "12345678", "wpa": "2",
  8739. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  8740. "config_methods": "virtual_push_button virtual_display",
  8741. "pbc_in_m1": "1" }
  8742. hapd = hostapd.add_ap(apdev[0], params)
  8743. bssid = hapd.own_addr()
  8744. hapd.request("WPS_PBC")
  8745. dev[0].scan_for_bss(bssid, freq="2412")
  8746. dev[0].dump_monitor()
  8747. dev[0].request("WPS_PBC " + bssid)
  8748. dev[0].wait_connected(timeout=30)
  8749. dev[0].request("DISCONNECT")
  8750. dev[0].wait_disconnected()
  8751. hapd.disable()
  8752. dev[0].dump_monitor()
  8753. dev[0].flush_scan_cache()
  8754. def test_ap_wps_pin_start_failure(dev, apdev):
  8755. """WPS_PIN start failure"""
  8756. with alloc_fail(dev[0], 1, "wpas_wps_start_dev_pw"):
  8757. if "FAIL" not in dev[0].request("WPS_PIN any 12345670"):
  8758. raise Exception("WPS_PIN not rejected during OOM")
  8759. with alloc_fail(dev[0], 1, "wpas_wps_start_dev_pw"):
  8760. if "FAIL" not in dev[0].request("WPS_PIN any"):
  8761. raise Exception("WPS_PIN not rejected during OOM")
  8762. def test_ap_wps_ap_pin_failure(dev, apdev):
  8763. """WPS_AP_PIN failure"""
  8764. id = dev[0].add_network()
  8765. dev[0].set_network(id, "mode", "2")
  8766. dev[0].set_network_quoted(id, "ssid", "wpas-ap-wps")
  8767. dev[0].set_network_quoted(id, "psk", "1234567890")
  8768. dev[0].set_network(id, "frequency", "2412")
  8769. dev[0].set_network(id, "scan_freq", "2412")
  8770. dev[0].select_network(id)
  8771. dev[0].wait_connected()
  8772. with fail_test(dev[0], 1,
  8773. "os_get_random;wpa_supplicant_ctrl_iface_wps_ap_pin"):
  8774. if "FAIL" not in dev[0].request("WPS_AP_PIN random"):
  8775. raise Exception("WPS_AP_PIN random accepted")
  8776. with alloc_fail(dev[0], 1, "wpas_wps_ap_pin_set"):
  8777. if "FAIL" not in dev[0].request("WPS_AP_PIN set 12345670"):
  8778. raise Exception("WPS_AP_PIN set accepted")
  8779. dev[0].request("DISCONNECT")
  8780. dev[0].wait_disconnected()
  8781. def test_ap_wps_random_uuid(dev, apdev, params):
  8782. """WPS and random UUID on Enrollee"""
  8783. ssid = "test-wps-conf"
  8784. hapd = hostapd.add_ap(apdev[0],
  8785. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  8786. "wpa_passphrase": "12345678", "wpa": "2",
  8787. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  8788. config = os.path.join(params['logdir'], 'ap_wps_random_uuid.conf')
  8789. with open(config, "w") as f:
  8790. f.write("auto_uuid=1\n")
  8791. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  8792. uuid = []
  8793. for i in range(3):
  8794. wpas.interface_add("wlan5", config=config)
  8795. wpas.scan_for_bss(apdev[0]['bssid'], freq="2412")
  8796. wpas.dump_monitor()
  8797. wpas.request("WPS_PBC " + apdev[0]['bssid'])
  8798. ev = hapd.wait_event(["WPS-ENROLLEE-SEEN"], timeout=10)
  8799. if ev is None:
  8800. raise Exception("Enrollee not seen")
  8801. uuid.append(ev.split(' ')[2])
  8802. wpas.request("WPS_CANCEL")
  8803. wpas.dump_monitor()
  8804. wpas.interface_remove("wlan5")
  8805. hapd.dump_monitor()
  8806. logger.info("Seen UUIDs: " + str(uuid))
  8807. if uuid[0] == uuid[1] or uuid[0] == uuid[2] or uuid[1] == uuid[2]:
  8808. raise Exception("Same UUID used multiple times")
  8809. def test_ap_wps_conf_pin_gcmp_128(dev, apdev):
  8810. """WPS PIN provisioning with configured AP using GCMP-128"""
  8811. run_ap_wps_conf_pin_cipher(dev, apdev, "GCMP")
  8812. def test_ap_wps_conf_pin_gcmp_256(dev, apdev):
  8813. """WPS PIN provisioning with configured AP using GCMP-256"""
  8814. run_ap_wps_conf_pin_cipher(dev, apdev, "GCMP-256")
  8815. def test_ap_wps_conf_pin_ccmp_256(dev, apdev):
  8816. """WPS PIN provisioning with configured AP using CCMP-256"""
  8817. run_ap_wps_conf_pin_cipher(dev, apdev, "CCMP-256")
  8818. def run_ap_wps_conf_pin_cipher(dev, apdev, cipher):
  8819. if cipher not in dev[0].get_capability("pairwise"):
  8820. raise HwsimSkip("Cipher %s not supported" % cipher)
  8821. ssid = "test-wps-conf-pin"
  8822. hapd = hostapd.add_ap(apdev[0],
  8823. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  8824. "wpa_passphrase": "12345678", "wpa": "2",
  8825. "wpa_key_mgmt": "WPA-PSK",
  8826. "rsn_pairwise": cipher })
  8827. logger.info("WPS provisioning step")
  8828. pin = dev[0].wps_read_pin()
  8829. hapd.request("WPS_PIN any " + pin)
  8830. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  8831. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  8832. dev[0].wait_connected(timeout=15)