test_ap_wps.py 381 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664
  1. # WPS tests
  2. # Copyright (c) 2013-2015, 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. @remote_compatible
  169. def test_ap_wps_init_through_wps_config_2(dev, apdev):
  170. """AP configuration using wps_config and wps_cred_processing=2"""
  171. ssid = "test-wps-init-config"
  172. hapd = hostapd.add_ap(apdev[0],
  173. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  174. "wps_cred_processing": "2" })
  175. if "FAIL" in hapd.request("WPS_CONFIG " + ssid.encode("hex") + " WPA2PSK CCMP " + "12345678".encode("hex")):
  176. raise Exception("WPS_CONFIG command failed")
  177. ev = hapd.wait_event(["WPS-NEW-AP-SETTINGS"], timeout=5)
  178. if ev is None:
  179. raise Exception("Timeout on WPS-NEW-AP-SETTINGS events")
  180. if "100e" not in ev:
  181. raise Exception("WPS-NEW-AP-SETTINGS did not include Credential")
  182. @remote_compatible
  183. def test_ap_wps_invalid_wps_config_passphrase(dev, apdev):
  184. """AP configuration using wps_config command with invalid passphrase"""
  185. ssid = "test-wps-init-config"
  186. hapd = hostapd.add_ap(apdev[0],
  187. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  188. if "FAIL" not in hapd.request("WPS_CONFIG " + ssid.encode("hex") + " WPA2PSK CCMP " + "1234567".encode("hex")):
  189. raise Exception("Invalid WPS_CONFIG command accepted")
  190. def test_ap_wps_conf(dev, apdev):
  191. """WPS PBC provisioning with configured AP"""
  192. ssid = "test-wps-conf"
  193. hapd = hostapd.add_ap(apdev[0],
  194. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  195. "wpa_passphrase": "12345678", "wpa": "2",
  196. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  197. logger.info("WPS provisioning step")
  198. hapd.request("WPS_PBC")
  199. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  200. dev[0].dump_monitor()
  201. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  202. dev[0].wait_connected(timeout=30)
  203. status = dev[0].get_status()
  204. if status['wpa_state'] != 'COMPLETED':
  205. raise Exception("Not fully connected")
  206. if status['bssid'] != apdev[0]['bssid']:
  207. raise Exception("Unexpected BSSID")
  208. if status['ssid'] != ssid:
  209. raise Exception("Unexpected SSID")
  210. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  211. raise Exception("Unexpected encryption configuration")
  212. if status['key_mgmt'] != 'WPA2-PSK':
  213. raise Exception("Unexpected key_mgmt")
  214. sta = hapd.get_sta(dev[0].p2p_interface_addr())
  215. if 'wpsDeviceName' not in sta or sta['wpsDeviceName'] != "Device A":
  216. raise Exception("Device name not available in STA command")
  217. def test_ap_wps_conf_5ghz(dev, apdev):
  218. """WPS PBC provisioning with configured AP on 5 GHz band"""
  219. try:
  220. hapd = None
  221. ssid = "test-wps-conf"
  222. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  223. "wpa_passphrase": "12345678", "wpa": "2",
  224. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  225. "country_code": "FI", "hw_mode": "a", "channel": "36" }
  226. hapd = hostapd.add_ap(apdev[0], params)
  227. logger.info("WPS provisioning step")
  228. hapd.request("WPS_PBC")
  229. dev[0].scan_for_bss(apdev[0]['bssid'], freq="5180")
  230. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  231. dev[0].wait_connected(timeout=30)
  232. sta = hapd.get_sta(dev[0].p2p_interface_addr())
  233. if 'wpsDeviceName' not in sta or sta['wpsDeviceName'] != "Device A":
  234. raise Exception("Device name not available in STA command")
  235. finally:
  236. dev[0].request("DISCONNECT")
  237. if hapd:
  238. hapd.request("DISABLE")
  239. subprocess.call(['iw', 'reg', 'set', '00'])
  240. dev[0].flush_scan_cache()
  241. def test_ap_wps_conf_chan14(dev, apdev):
  242. """WPS PBC provisioning with configured AP on channel 14"""
  243. try:
  244. hapd = None
  245. ssid = "test-wps-conf"
  246. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  247. "wpa_passphrase": "12345678", "wpa": "2",
  248. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  249. "country_code": "JP", "hw_mode": "b", "channel": "14" }
  250. hapd = hostapd.add_ap(apdev[0], params)
  251. logger.info("WPS provisioning step")
  252. hapd.request("WPS_PBC")
  253. dev[0].request("WPS_PBC")
  254. dev[0].wait_connected(timeout=30)
  255. sta = hapd.get_sta(dev[0].p2p_interface_addr())
  256. if 'wpsDeviceName' not in sta or sta['wpsDeviceName'] != "Device A":
  257. raise Exception("Device name not available in STA command")
  258. finally:
  259. dev[0].request("DISCONNECT")
  260. if hapd:
  261. hapd.request("DISABLE")
  262. subprocess.call(['iw', 'reg', 'set', '00'])
  263. dev[0].flush_scan_cache()
  264. @remote_compatible
  265. def test_ap_wps_twice(dev, apdev):
  266. """WPS provisioning with twice to change passphrase"""
  267. ssid = "test-wps-twice"
  268. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  269. "wpa_passphrase": "12345678", "wpa": "2",
  270. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" }
  271. hapd = hostapd.add_ap(apdev[0], params)
  272. logger.info("WPS provisioning step")
  273. hapd.request("WPS_PBC")
  274. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  275. dev[0].dump_monitor()
  276. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  277. dev[0].wait_connected(timeout=30)
  278. dev[0].request("DISCONNECT")
  279. logger.info("Restart AP with different passphrase and re-run WPS")
  280. hostapd.remove_bss(apdev[0])
  281. params['wpa_passphrase'] = 'another passphrase'
  282. hapd = hostapd.add_ap(apdev[0], params)
  283. logger.info("WPS provisioning step")
  284. hapd.request("WPS_PBC")
  285. dev[0].dump_monitor()
  286. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  287. dev[0].wait_connected(timeout=30)
  288. networks = dev[0].list_networks()
  289. if len(networks) > 1:
  290. raise Exception("Unexpected duplicated network block present")
  291. @remote_compatible
  292. def test_ap_wps_incorrect_pin(dev, apdev):
  293. """WPS PIN provisioning with incorrect PIN"""
  294. ssid = "test-wps-incorrect-pin"
  295. hapd = hostapd.add_ap(apdev[0],
  296. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  297. "wpa_passphrase": "12345678", "wpa": "2",
  298. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  299. logger.info("WPS provisioning attempt 1")
  300. hapd.request("WPS_PIN any 12345670")
  301. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  302. dev[0].dump_monitor()
  303. dev[0].request("WPS_PIN %s 55554444" % apdev[0]['bssid'])
  304. ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
  305. if ev is None:
  306. raise Exception("WPS operation timed out")
  307. if "config_error=18" not in ev:
  308. raise Exception("Incorrect config_error reported")
  309. if "msg=8" not in ev:
  310. raise Exception("PIN error detected on incorrect message")
  311. dev[0].wait_disconnected(timeout=10)
  312. dev[0].request("WPS_CANCEL")
  313. # if a scan was in progress, wait for it to complete before trying WPS again
  314. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  315. status = hapd.request("WPS_GET_STATUS")
  316. if "Last WPS result: Failed" not in status:
  317. raise Exception("WPS failure result not shown correctly")
  318. logger.info("WPS provisioning attempt 2")
  319. hapd.request("WPS_PIN any 12345670")
  320. dev[0].dump_monitor()
  321. dev[0].request("WPS_PIN %s 12344444" % apdev[0]['bssid'])
  322. ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
  323. if ev is None:
  324. raise Exception("WPS operation timed out")
  325. if "config_error=18" not in ev:
  326. raise Exception("Incorrect config_error reported")
  327. if "msg=10" not in ev:
  328. raise Exception("PIN error detected on incorrect message")
  329. dev[0].wait_disconnected(timeout=10)
  330. @remote_compatible
  331. def test_ap_wps_conf_pin(dev, apdev):
  332. """WPS PIN provisioning with configured AP"""
  333. ssid = "test-wps-conf-pin"
  334. hapd = hostapd.add_ap(apdev[0],
  335. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  336. "wpa_passphrase": "12345678", "wpa": "2",
  337. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  338. logger.info("WPS provisioning step")
  339. pin = dev[0].wps_read_pin()
  340. hapd.request("WPS_PIN any " + pin)
  341. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  342. dev[0].dump_monitor()
  343. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  344. dev[0].wait_connected(timeout=30)
  345. status = dev[0].get_status()
  346. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  347. raise Exception("Not fully connected")
  348. if status['ssid'] != ssid:
  349. raise Exception("Unexpected SSID")
  350. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  351. raise Exception("Unexpected encryption configuration")
  352. if status['key_mgmt'] != 'WPA2-PSK':
  353. raise Exception("Unexpected key_mgmt")
  354. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  355. bss = dev[1].get_bss(apdev[0]['bssid'])
  356. if "[WPS-AUTH]" in bss['flags']:
  357. raise Exception("WPS-AUTH flag not cleared")
  358. logger.info("Try to connect from another station using the same PIN")
  359. pin = dev[1].request("WPS_PIN " + apdev[0]['bssid'])
  360. ev = dev[1].wait_event(["WPS-M2D","CTRL-EVENT-CONNECTED"], timeout=30)
  361. if ev is None:
  362. raise Exception("Operation timed out")
  363. if "WPS-M2D" not in ev:
  364. raise Exception("Unexpected WPS operation started")
  365. hapd.request("WPS_PIN any " + pin)
  366. dev[1].wait_connected(timeout=30)
  367. def test_ap_wps_conf_pin_mixed_mode(dev, apdev):
  368. """WPS PIN provisioning with configured AP (WPA+WPA2)"""
  369. ssid = "test-wps-conf-pin-mixed"
  370. hapd = hostapd.add_ap(apdev[0],
  371. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  372. "wpa_passphrase": "12345678", "wpa": "3",
  373. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  374. "wpa_pairwise": "TKIP" })
  375. logger.info("WPS provisioning step")
  376. pin = dev[0].wps_read_pin()
  377. hapd.request("WPS_PIN any " + pin)
  378. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  379. dev[0].dump_monitor()
  380. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  381. dev[0].wait_connected(timeout=30)
  382. status = dev[0].get_status()
  383. dev[0].request("REMOVE_NETWORK all")
  384. dev[0].wait_disconnected()
  385. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP' or status['key_mgmt'] != 'WPA2-PSK':
  386. raise Exception("Unexpected encryption/key_mgmt configuration: pairwise=%s group=%s key_mgmt=%s" % (status['pairwise_cipher'], status['group_cipher'], status['key_mgmt']))
  387. logger.info("WPS provisioning step (auth_types=0x1b)")
  388. if "OK" not in dev[0].request("SET wps_force_auth_types 0x1b"):
  389. raise Exception("Failed to set wps_force_auth_types 0x1b")
  390. pin = dev[0].wps_read_pin()
  391. hapd.request("WPS_PIN any " + pin)
  392. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  393. dev[0].dump_monitor()
  394. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  395. dev[0].wait_connected(timeout=30)
  396. status = dev[0].get_status()
  397. dev[0].request("REMOVE_NETWORK all")
  398. dev[0].wait_disconnected()
  399. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP' or status['key_mgmt'] != 'WPA2-PSK':
  400. raise Exception("Unexpected encryption/key_mgmt configuration: pairwise=%s group=%s key_mgmt=%s" % (status['pairwise_cipher'], status['group_cipher'], status['key_mgmt']))
  401. logger.info("WPS provisioning step (auth_types=0 encr_types=0)")
  402. if "OK" not in dev[0].request("SET wps_force_auth_types 0"):
  403. raise Exception("Failed to set wps_force_auth_types 0")
  404. if "OK" not in dev[0].request("SET wps_force_encr_types 0"):
  405. raise Exception("Failed to set wps_force_encr_types 0")
  406. pin = dev[0].wps_read_pin()
  407. hapd.request("WPS_PIN any " + pin)
  408. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  409. dev[0].dump_monitor()
  410. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  411. dev[0].wait_connected(timeout=30)
  412. status = dev[0].get_status()
  413. dev[0].request("REMOVE_NETWORK all")
  414. dev[0].wait_disconnected()
  415. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP' or status['key_mgmt'] != 'WPA2-PSK':
  416. raise Exception("Unexpected encryption/key_mgmt configuration: pairwise=%s group=%s key_mgmt=%s" % (status['pairwise_cipher'], status['group_cipher'], status['key_mgmt']))
  417. dev[0].request("SET wps_force_auth_types ")
  418. dev[0].request("SET wps_force_encr_types ")
  419. @remote_compatible
  420. def test_ap_wps_conf_pin_v1(dev, apdev):
  421. """WPS PIN provisioning with configured WPS v1.0 AP"""
  422. ssid = "test-wps-conf-pin-v1"
  423. hapd = hostapd.add_ap(apdev[0],
  424. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  425. "wpa_passphrase": "12345678", "wpa": "2",
  426. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  427. logger.info("WPS provisioning step")
  428. pin = dev[0].wps_read_pin()
  429. hapd.request("SET wps_version_number 0x10")
  430. hapd.request("WPS_PIN any " + pin)
  431. found = False
  432. for i in range(0, 10):
  433. dev[0].scan(freq="2412")
  434. if "[WPS-PIN]" in dev[0].request("SCAN_RESULTS"):
  435. found = True
  436. break
  437. if not found:
  438. hapd.request("SET wps_version_number 0x20")
  439. raise Exception("WPS-PIN flag not seen in scan results")
  440. dev[0].dump_monitor()
  441. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  442. dev[0].wait_connected(timeout=30)
  443. hapd.request("SET wps_version_number 0x20")
  444. @remote_compatible
  445. def test_ap_wps_conf_pin_2sta(dev, apdev):
  446. """Two stations trying to use WPS PIN at the same time"""
  447. ssid = "test-wps-conf-pin2"
  448. hapd = hostapd.add_ap(apdev[0],
  449. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  450. "wpa_passphrase": "12345678", "wpa": "2",
  451. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  452. logger.info("WPS provisioning step")
  453. pin = "12345670"
  454. pin2 = "55554444"
  455. hapd.request("WPS_PIN " + dev[0].get_status_field("uuid") + " " + pin)
  456. hapd.request("WPS_PIN " + dev[1].get_status_field("uuid") + " " + pin)
  457. dev[0].dump_monitor()
  458. dev[1].dump_monitor()
  459. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  460. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
  461. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  462. dev[1].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  463. dev[0].wait_connected(timeout=30)
  464. dev[1].wait_connected(timeout=30)
  465. @remote_compatible
  466. def test_ap_wps_conf_pin_timeout(dev, apdev):
  467. """WPS PIN provisioning with configured AP timing out PIN"""
  468. ssid = "test-wps-conf-pin"
  469. hapd = hostapd.add_ap(apdev[0],
  470. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  471. "wpa_passphrase": "12345678", "wpa": "2",
  472. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  473. addr = dev[0].p2p_interface_addr()
  474. pin = dev[0].wps_read_pin()
  475. if "FAIL" not in hapd.request("WPS_PIN "):
  476. raise Exception("Unexpected success on invalid WPS_PIN")
  477. hapd.request("WPS_PIN any " + pin + " 1")
  478. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  479. time.sleep(1.1)
  480. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  481. ev = hapd.wait_event(["WPS-PIN-NEEDED"], timeout=20)
  482. if ev is None:
  483. raise Exception("WPS-PIN-NEEDED event timed out")
  484. ev = dev[0].wait_event(["WPS-M2D"])
  485. if ev is None:
  486. raise Exception("M2D not reported")
  487. dev[0].request("WPS_CANCEL")
  488. hapd.request("WPS_PIN any " + pin + " 20 " + addr)
  489. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  490. dev[0].wait_connected(timeout=30)
  491. def test_ap_wps_reg_connect(dev, apdev):
  492. """WPS registrar using AP PIN to connect"""
  493. ssid = "test-wps-reg-ap-pin"
  494. appin = "12345670"
  495. hostapd.add_ap(apdev[0],
  496. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  497. "wpa_passphrase": "12345678", "wpa": "2",
  498. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  499. "ap_pin": appin})
  500. logger.info("WPS provisioning step")
  501. dev[0].dump_monitor()
  502. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  503. dev[0].wps_reg(apdev[0]['bssid'], appin)
  504. status = dev[0].get_status()
  505. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  506. raise Exception("Not fully connected")
  507. if status['ssid'] != ssid:
  508. raise Exception("Unexpected SSID")
  509. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  510. raise Exception("Unexpected encryption configuration")
  511. if status['key_mgmt'] != 'WPA2-PSK':
  512. raise Exception("Unexpected key_mgmt")
  513. def test_ap_wps_reg_connect_mixed_mode(dev, apdev):
  514. """WPS registrar using AP PIN to connect (WPA+WPA2)"""
  515. ssid = "test-wps-reg-ap-pin"
  516. appin = "12345670"
  517. hostapd.add_ap(apdev[0],
  518. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  519. "wpa_passphrase": "12345678", "wpa": "3",
  520. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  521. "wpa_pairwise": "TKIP", "ap_pin": appin})
  522. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  523. dev[0].wps_reg(apdev[0]['bssid'], appin)
  524. status = dev[0].get_status()
  525. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  526. raise Exception("Not fully connected")
  527. if status['ssid'] != ssid:
  528. raise Exception("Unexpected SSID")
  529. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  530. raise Exception("Unexpected encryption configuration")
  531. if status['key_mgmt'] != 'WPA2-PSK':
  532. raise Exception("Unexpected key_mgmt")
  533. def test_ap_wps_reg_override_ap_settings(dev, apdev):
  534. """WPS registrar and ap_settings override"""
  535. ap_settings = "/tmp/ap_wps_reg_override_ap_settings"
  536. try:
  537. os.remove(ap_settings)
  538. except:
  539. pass
  540. # Override AP Settings with values that point to another AP
  541. data = build_wsc_attr(ATTR_NETWORK_INDEX, '\x01')
  542. data += build_wsc_attr(ATTR_SSID, "test")
  543. data += build_wsc_attr(ATTR_AUTH_TYPE, '\x00\x01')
  544. data += build_wsc_attr(ATTR_ENCR_TYPE, '\x00\x01')
  545. data += build_wsc_attr(ATTR_NETWORK_KEY, '')
  546. data += build_wsc_attr(ATTR_MAC_ADDR, binascii.unhexlify(apdev[1]['bssid'].replace(':', '')))
  547. with open(ap_settings, "w") as f:
  548. f.write(data)
  549. ssid = "test-wps-reg-ap-pin"
  550. appin = "12345670"
  551. hostapd.add_ap(apdev[0],
  552. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  553. "wpa_passphrase": "12345678", "wpa": "2",
  554. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  555. "ap_pin": appin, "ap_settings": ap_settings })
  556. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "test" })
  557. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  558. dev[0].scan_for_bss(apdev[1]['bssid'], freq=2412)
  559. dev[0].wps_reg(apdev[0]['bssid'], appin)
  560. ev = hapd2.wait_event(['AP-STA-CONNECTED'], timeout=10)
  561. os.remove(ap_settings)
  562. if ev is None:
  563. raise Exception("No connection with the other AP")
  564. def check_wps_reg_failure(dev, ap, appin):
  565. dev.request("WPS_REG " + ap['bssid'] + " " + appin)
  566. ev = dev.wait_event(["WPS-SUCCESS", "WPS-FAIL"], timeout=15)
  567. if ev is None:
  568. raise Exception("WPS operation timed out")
  569. if "WPS-SUCCESS" in ev:
  570. raise Exception("WPS operation succeeded unexpectedly")
  571. if "config_error=15" not in ev:
  572. raise Exception("WPS setup locked state was not reported correctly")
  573. def test_ap_wps_random_ap_pin(dev, apdev):
  574. """WPS registrar using random AP PIN"""
  575. ssid = "test-wps-reg-random-ap-pin"
  576. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  577. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  578. "wpa_passphrase": "12345678", "wpa": "2",
  579. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  580. "device_name": "Wireless AP", "manufacturer": "Company",
  581. "model_name": "WAP", "model_number": "123",
  582. "serial_number": "12345", "device_type": "6-0050F204-1",
  583. "os_version": "01020300",
  584. "config_methods": "label push_button",
  585. "uuid": ap_uuid, "upnp_iface": "lo" }
  586. hapd = hostapd.add_ap(apdev[0], params)
  587. appin = hapd.request("WPS_AP_PIN random")
  588. if "FAIL" in appin:
  589. raise Exception("Could not generate random AP PIN")
  590. if appin not in hapd.request("WPS_AP_PIN get"):
  591. raise Exception("Could not fetch current AP PIN")
  592. logger.info("WPS provisioning step")
  593. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  594. dev[0].wps_reg(apdev[0]['bssid'], appin)
  595. hapd.request("WPS_AP_PIN disable")
  596. logger.info("WPS provisioning step with AP PIN disabled")
  597. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  598. check_wps_reg_failure(dev[1], apdev[0], appin)
  599. logger.info("WPS provisioning step with AP PIN reset")
  600. appin = "12345670"
  601. hapd.request("WPS_AP_PIN set " + appin)
  602. dev[1].wps_reg(apdev[0]['bssid'], appin)
  603. dev[0].request("REMOVE_NETWORK all")
  604. dev[1].request("REMOVE_NETWORK all")
  605. dev[0].wait_disconnected(timeout=10)
  606. dev[1].wait_disconnected(timeout=10)
  607. logger.info("WPS provisioning step after AP PIN timeout")
  608. hapd.request("WPS_AP_PIN disable")
  609. appin = hapd.request("WPS_AP_PIN random 1")
  610. time.sleep(1.1)
  611. if "FAIL" not in hapd.request("WPS_AP_PIN get"):
  612. raise Exception("AP PIN unexpectedly still enabled")
  613. check_wps_reg_failure(dev[0], apdev[0], appin)
  614. logger.info("WPS provisioning step after AP PIN timeout(2)")
  615. hapd.request("WPS_AP_PIN disable")
  616. appin = "12345670"
  617. hapd.request("WPS_AP_PIN set " + appin + " 1")
  618. time.sleep(1.1)
  619. if "FAIL" not in hapd.request("WPS_AP_PIN get"):
  620. raise Exception("AP PIN unexpectedly still enabled")
  621. check_wps_reg_failure(dev[1], apdev[0], appin)
  622. with fail_test(hapd, 1, "os_get_random;wps_generate_pin"):
  623. hapd.request("WPS_AP_PIN random 1")
  624. hapd.request("WPS_AP_PIN disable")
  625. with alloc_fail(hapd, 1, "upnp_wps_set_ap_pin"):
  626. hapd.request("WPS_AP_PIN set 12345670")
  627. hapd.request("WPS_AP_PIN disable")
  628. def test_ap_wps_reg_config(dev, apdev):
  629. """WPS registrar configuring an AP using AP PIN"""
  630. ssid = "test-wps-init-ap-pin"
  631. appin = "12345670"
  632. hostapd.add_ap(apdev[0],
  633. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  634. "ap_pin": appin})
  635. logger.info("WPS configuration step")
  636. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  637. dev[0].dump_monitor()
  638. new_ssid = "wps-new-ssid"
  639. new_passphrase = "1234567890"
  640. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
  641. new_passphrase)
  642. status = dev[0].get_status()
  643. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  644. raise Exception("Not fully connected")
  645. if status['ssid'] != new_ssid:
  646. raise Exception("Unexpected SSID")
  647. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  648. raise Exception("Unexpected encryption configuration")
  649. if status['key_mgmt'] != 'WPA2-PSK':
  650. raise Exception("Unexpected key_mgmt")
  651. logger.info("Re-configure back to open")
  652. dev[0].request("REMOVE_NETWORK all")
  653. dev[0].flush_scan_cache()
  654. dev[0].dump_monitor()
  655. dev[0].wps_reg(apdev[0]['bssid'], appin, "wps-open", "OPEN", "NONE", "")
  656. status = dev[0].get_status()
  657. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  658. raise Exception("Not fully connected")
  659. if status['ssid'] != "wps-open":
  660. raise Exception("Unexpected SSID")
  661. if status['key_mgmt'] != 'NONE':
  662. raise Exception("Unexpected key_mgmt")
  663. def test_ap_wps_reg_config_ext_processing(dev, apdev):
  664. """WPS registrar configuring an AP with external config processing"""
  665. ssid = "test-wps-init-ap-pin"
  666. appin = "12345670"
  667. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  668. "wps_cred_processing": "1", "ap_pin": appin}
  669. hapd = hostapd.add_ap(apdev[0], params)
  670. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  671. new_ssid = "wps-new-ssid"
  672. new_passphrase = "1234567890"
  673. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
  674. new_passphrase, no_wait=True)
  675. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  676. if ev is None:
  677. raise Exception("WPS registrar operation timed out")
  678. ev = hapd.wait_event(["WPS-NEW-AP-SETTINGS"], timeout=15)
  679. if ev is None:
  680. raise Exception("WPS configuration timed out")
  681. if "1026" not in ev:
  682. raise Exception("AP Settings missing from event")
  683. hapd.request("SET wps_cred_processing 0")
  684. if "FAIL" in hapd.request("WPS_CONFIG " + new_ssid.encode("hex") + " WPA2PSK CCMP " + new_passphrase.encode("hex")):
  685. raise Exception("WPS_CONFIG command failed")
  686. dev[0].wait_connected(timeout=15)
  687. def test_ap_wps_reg_config_tkip(dev, apdev):
  688. """WPS registrar configuring AP to use TKIP and AP upgrading to TKIP+CCMP"""
  689. skip_with_fips(dev[0])
  690. ssid = "test-wps-init-ap"
  691. appin = "12345670"
  692. hostapd.add_ap(apdev[0],
  693. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  694. "ap_pin": appin})
  695. logger.info("WPS configuration step")
  696. dev[0].request("SET wps_version_number 0x10")
  697. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  698. dev[0].dump_monitor()
  699. new_ssid = "wps-new-ssid-with-tkip"
  700. new_passphrase = "1234567890"
  701. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPAPSK", "TKIP",
  702. new_passphrase)
  703. logger.info("Re-connect to verify WPA2 mixed mode")
  704. dev[0].request("DISCONNECT")
  705. id = 0
  706. dev[0].set_network(id, "pairwise", "CCMP")
  707. dev[0].set_network(id, "proto", "RSN")
  708. dev[0].connect_network(id)
  709. status = dev[0].get_status()
  710. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  711. raise Exception("Not fully connected: wpa_state={} bssid={}".format(status['wpa_state'], status['bssid']))
  712. if status['ssid'] != new_ssid:
  713. raise Exception("Unexpected SSID")
  714. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  715. raise Exception("Unexpected encryption configuration")
  716. if status['key_mgmt'] != 'WPA2-PSK':
  717. raise Exception("Unexpected key_mgmt")
  718. def test_ap_wps_setup_locked(dev, apdev):
  719. """WPS registrar locking up AP setup on AP PIN failures"""
  720. ssid = "test-wps-incorrect-ap-pin"
  721. appin = "12345670"
  722. hapd = hostapd.add_ap(apdev[0],
  723. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  724. "wpa_passphrase": "12345678", "wpa": "2",
  725. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  726. "ap_pin": appin})
  727. new_ssid = "wps-new-ssid-test"
  728. new_passphrase = "1234567890"
  729. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  730. ap_setup_locked=False
  731. for pin in ["55554444", "1234", "12345678", "00000000", "11111111"]:
  732. dev[0].dump_monitor()
  733. logger.info("Try incorrect AP PIN - attempt " + pin)
  734. dev[0].wps_reg(apdev[0]['bssid'], pin, new_ssid, "WPA2PSK",
  735. "CCMP", new_passphrase, no_wait=True)
  736. ev = dev[0].wait_event(["WPS-FAIL", "CTRL-EVENT-CONNECTED"])
  737. if ev is None:
  738. raise Exception("Timeout on receiving WPS operation failure event")
  739. if "CTRL-EVENT-CONNECTED" in ev:
  740. raise Exception("Unexpected connection")
  741. if "config_error=15" in ev:
  742. logger.info("AP Setup Locked")
  743. ap_setup_locked=True
  744. elif "config_error=18" not in ev:
  745. raise Exception("config_error=18 not reported")
  746. dev[0].wait_disconnected(timeout=10)
  747. time.sleep(0.1)
  748. if not ap_setup_locked:
  749. raise Exception("AP setup was not locked")
  750. dev[0].request("WPS_CANCEL")
  751. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412, force_scan=True,
  752. only_new=True)
  753. bss = dev[0].get_bss(apdev[0]['bssid'])
  754. if 'wps_ap_setup_locked' not in bss or bss['wps_ap_setup_locked'] != '1':
  755. logger.info("BSS: " + str(bss))
  756. raise Exception("AP Setup Locked not indicated in scan results")
  757. status = hapd.request("WPS_GET_STATUS")
  758. if "Last WPS result: Failed" not in status:
  759. raise Exception("WPS failure result not shown correctly")
  760. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  761. raise Exception("Peer address not shown correctly")
  762. time.sleep(0.5)
  763. dev[0].dump_monitor()
  764. logger.info("WPS provisioning step")
  765. pin = dev[0].wps_read_pin()
  766. hapd.request("WPS_PIN any " + pin)
  767. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  768. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=30)
  769. if ev is None:
  770. raise Exception("WPS success was not reported")
  771. dev[0].wait_connected(timeout=30)
  772. appin = hapd.request("WPS_AP_PIN random")
  773. if "FAIL" in appin:
  774. raise Exception("Could not generate random AP PIN")
  775. ev = hapd.wait_event(["WPS-AP-SETUP-UNLOCKED"], timeout=10)
  776. if ev is None:
  777. raise Exception("Failed to unlock AP PIN")
  778. def test_ap_wps_setup_locked_timeout(dev, apdev):
  779. """WPS re-enabling AP PIN after timeout"""
  780. ssid = "test-wps-incorrect-ap-pin"
  781. appin = "12345670"
  782. hapd = hostapd.add_ap(apdev[0],
  783. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  784. "wpa_passphrase": "12345678", "wpa": "2",
  785. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  786. "ap_pin": appin})
  787. new_ssid = "wps-new-ssid-test"
  788. new_passphrase = "1234567890"
  789. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  790. ap_setup_locked=False
  791. for pin in ["55554444", "1234", "12345678", "00000000", "11111111"]:
  792. dev[0].dump_monitor()
  793. logger.info("Try incorrect AP PIN - attempt " + pin)
  794. dev[0].wps_reg(apdev[0]['bssid'], pin, new_ssid, "WPA2PSK",
  795. "CCMP", new_passphrase, no_wait=True)
  796. ev = dev[0].wait_event(["WPS-FAIL", "CTRL-EVENT-CONNECTED"], timeout=15)
  797. if ev is None:
  798. raise Exception("Timeout on receiving WPS operation failure event")
  799. if "CTRL-EVENT-CONNECTED" in ev:
  800. raise Exception("Unexpected connection")
  801. if "config_error=15" in ev:
  802. logger.info("AP Setup Locked")
  803. ap_setup_locked=True
  804. break
  805. elif "config_error=18" not in ev:
  806. raise Exception("config_error=18 not reported")
  807. dev[0].wait_disconnected(timeout=10)
  808. time.sleep(0.1)
  809. if not ap_setup_locked:
  810. raise Exception("AP setup was not locked")
  811. ev = hapd.wait_event(["WPS-AP-SETUP-UNLOCKED"], timeout=80)
  812. if ev is None:
  813. raise Exception("AP PIN did not get unlocked on 60 second timeout")
  814. def test_ap_wps_setup_locked_2(dev, apdev):
  815. """WPS AP configured for special ap_setup_locked=2 mode"""
  816. ssid = "test-wps-ap-pin"
  817. appin = "12345670"
  818. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  819. "wpa_passphrase": "12345678", "wpa": "2",
  820. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  821. "ap_pin": appin, "ap_setup_locked": "2" }
  822. hapd = hostapd.add_ap(apdev[0], params)
  823. new_ssid = "wps-new-ssid-test"
  824. new_passphrase = "1234567890"
  825. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  826. dev[0].wps_reg(apdev[0]['bssid'], appin)
  827. dev[0].request("REMOVE_NETWORK all")
  828. dev[0].wait_disconnected()
  829. hapd.dump_monitor()
  830. dev[0].dump_monitor()
  831. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK",
  832. "CCMP", new_passphrase, no_wait=True)
  833. ev = hapd.wait_event(["WPS-FAIL"], timeout=5)
  834. if ev is None:
  835. raise Exception("hostapd did not report WPS failure")
  836. if "msg=12 config_error=15" not in ev:
  837. raise Exception("Unexpected failure reason (AP): " + ev)
  838. ev = dev[0].wait_event(["WPS-FAIL", "CTRL-EVENT-CONNECTED"])
  839. if ev is None:
  840. raise Exception("Timeout on receiving WPS operation failure event")
  841. if "CTRL-EVENT-CONNECTED" in ev:
  842. raise Exception("Unexpected connection")
  843. if "config_error=15" not in ev:
  844. raise Exception("Unexpected failure reason (STA): " + ev)
  845. dev[0].request("WPS_CANCEL")
  846. dev[0].wait_disconnected()
  847. @remote_compatible
  848. def test_ap_wps_pbc_overlap_2ap(dev, apdev):
  849. """WPS PBC session overlap with two active APs"""
  850. params = { "ssid": "wps1", "eap_server": "1", "wps_state": "2",
  851. "wpa_passphrase": "12345678", "wpa": "2",
  852. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  853. "wps_independent": "1"}
  854. hapd = hostapd.add_ap(apdev[0], params)
  855. params = { "ssid": "wps2", "eap_server": "1", "wps_state": "2",
  856. "wpa_passphrase": "123456789", "wpa": "2",
  857. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  858. "wps_independent": "1"}
  859. hapd2 = hostapd.add_ap(apdev[1], params)
  860. hapd.request("WPS_PBC")
  861. hapd2.request("WPS_PBC")
  862. logger.info("WPS provisioning step")
  863. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  864. dev[0].scan_for_bss(apdev[1]['bssid'], freq="2412")
  865. dev[0].request("WPS_PBC")
  866. ev = dev[0].wait_event(["WPS-OVERLAP-DETECTED"], timeout=15)
  867. if ev is None:
  868. raise Exception("PBC session overlap not detected")
  869. hapd.request("DISABLE")
  870. hapd2.request("DISABLE")
  871. dev[0].flush_scan_cache()
  872. @remote_compatible
  873. def test_ap_wps_pbc_overlap_2sta(dev, apdev):
  874. """WPS PBC session overlap with two active STAs"""
  875. ssid = "test-wps-pbc-overlap"
  876. hapd = hostapd.add_ap(apdev[0],
  877. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  878. "wpa_passphrase": "12345678", "wpa": "2",
  879. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  880. logger.info("WPS provisioning step")
  881. hapd.request("WPS_PBC")
  882. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  883. dev[0].dump_monitor()
  884. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
  885. dev[1].dump_monitor()
  886. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  887. dev[1].request("WPS_PBC " + apdev[0]['bssid'])
  888. ev = dev[0].wait_event(["WPS-M2D"], timeout=15)
  889. if ev is None:
  890. raise Exception("PBC session overlap not detected (dev0)")
  891. if "config_error=12" not in ev:
  892. raise Exception("PBC session overlap not correctly reported (dev0)")
  893. dev[0].request("WPS_CANCEL")
  894. dev[0].request("DISCONNECT")
  895. ev = dev[1].wait_event(["WPS-M2D"], timeout=15)
  896. if ev is None:
  897. raise Exception("PBC session overlap not detected (dev1)")
  898. if "config_error=12" not in ev:
  899. raise Exception("PBC session overlap not correctly reported (dev1)")
  900. dev[1].request("WPS_CANCEL")
  901. dev[1].request("DISCONNECT")
  902. hapd.request("WPS_CANCEL")
  903. ret = hapd.request("WPS_PBC")
  904. if "FAIL" not in ret:
  905. raise Exception("PBC mode allowed to be started while PBC overlap still active")
  906. hapd.request("DISABLE")
  907. dev[0].flush_scan_cache()
  908. dev[1].flush_scan_cache()
  909. @remote_compatible
  910. def test_ap_wps_cancel(dev, apdev):
  911. """WPS AP cancelling enabled config method"""
  912. ssid = "test-wps-ap-cancel"
  913. hapd = hostapd.add_ap(apdev[0],
  914. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  915. "wpa_passphrase": "12345678", "wpa": "2",
  916. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  917. bssid = apdev[0]['bssid']
  918. logger.info("Verify PBC enable/cancel")
  919. hapd.request("WPS_PBC")
  920. dev[0].scan(freq="2412")
  921. dev[0].scan(freq="2412")
  922. bss = dev[0].get_bss(apdev[0]['bssid'])
  923. if "[WPS-PBC]" not in bss['flags']:
  924. raise Exception("WPS-PBC flag missing")
  925. if "FAIL" in hapd.request("WPS_CANCEL"):
  926. raise Exception("WPS_CANCEL failed")
  927. dev[0].scan(freq="2412")
  928. dev[0].scan(freq="2412")
  929. bss = dev[0].get_bss(apdev[0]['bssid'])
  930. if "[WPS-PBC]" in bss['flags']:
  931. raise Exception("WPS-PBC flag not cleared")
  932. logger.info("Verify PIN enable/cancel")
  933. hapd.request("WPS_PIN any 12345670")
  934. dev[0].scan(freq="2412")
  935. dev[0].scan(freq="2412")
  936. bss = dev[0].get_bss(apdev[0]['bssid'])
  937. if "[WPS-AUTH]" not in bss['flags']:
  938. raise Exception("WPS-AUTH flag missing")
  939. if "FAIL" in hapd.request("WPS_CANCEL"):
  940. raise Exception("WPS_CANCEL failed")
  941. dev[0].scan(freq="2412")
  942. dev[0].scan(freq="2412")
  943. bss = dev[0].get_bss(apdev[0]['bssid'])
  944. if "[WPS-AUTH]" in bss['flags']:
  945. raise Exception("WPS-AUTH flag not cleared")
  946. def test_ap_wps_er_add_enrollee(dev, apdev):
  947. """WPS ER configuring AP and adding a new enrollee using PIN"""
  948. try:
  949. _test_ap_wps_er_add_enrollee(dev, apdev)
  950. finally:
  951. dev[0].request("WPS_ER_STOP")
  952. def _test_ap_wps_er_add_enrollee(dev, apdev):
  953. ssid = "wps-er-add-enrollee"
  954. ap_pin = "12345670"
  955. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  956. hostapd.add_ap(apdev[0],
  957. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  958. "device_name": "Wireless AP", "manufacturer": "Company",
  959. "model_name": "WAP", "model_number": "123",
  960. "serial_number": "12345", "device_type": "6-0050F204-1",
  961. "os_version": "01020300",
  962. 'friendly_name': "WPS AP - <>&'\" - TEST",
  963. "config_methods": "label push_button",
  964. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  965. logger.info("WPS configuration step")
  966. new_passphrase = "1234567890"
  967. dev[0].dump_monitor()
  968. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  969. dev[0].wps_reg(apdev[0]['bssid'], ap_pin, ssid, "WPA2PSK", "CCMP",
  970. new_passphrase)
  971. status = dev[0].get_status()
  972. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  973. raise Exception("Not fully connected")
  974. if status['ssid'] != ssid:
  975. raise Exception("Unexpected SSID")
  976. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  977. raise Exception("Unexpected encryption configuration")
  978. if status['key_mgmt'] != 'WPA2-PSK':
  979. raise Exception("Unexpected key_mgmt")
  980. logger.info("Start ER")
  981. dev[0].request("WPS_ER_START ifname=lo")
  982. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  983. if ev is None:
  984. raise Exception("AP discovery timed out")
  985. if ap_uuid not in ev:
  986. raise Exception("Expected AP UUID not found")
  987. if "|WPS AP - &lt;&gt;&amp;&apos;&quot; - TEST|Company|" not in ev:
  988. raise Exception("Expected friendly name not found")
  989. logger.info("Learn AP configuration through UPnP")
  990. dev[0].dump_monitor()
  991. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  992. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  993. if ev is None:
  994. raise Exception("AP learn timed out")
  995. if ap_uuid not in ev:
  996. raise Exception("Expected AP UUID not in settings")
  997. if "ssid=" + ssid not in ev:
  998. raise Exception("Expected SSID not in settings")
  999. if "key=" + new_passphrase not in ev:
  1000. raise Exception("Expected passphrase not in settings")
  1001. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1002. if ev is None:
  1003. raise Exception("WPS-FAIL after AP learn timed out")
  1004. time.sleep(0.1)
  1005. logger.info("Add Enrollee using ER")
  1006. pin = dev[1].wps_read_pin()
  1007. dev[0].dump_monitor()
  1008. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  1009. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1010. dev[1].dump_monitor()
  1011. dev[1].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  1012. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=30)
  1013. if ev is None:
  1014. raise Exception("Enrollee did not report success")
  1015. dev[1].wait_connected(timeout=15)
  1016. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  1017. if ev is None:
  1018. raise Exception("WPS ER did not report success")
  1019. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  1020. logger.info("Add a specific Enrollee using ER")
  1021. pin = dev[2].wps_read_pin()
  1022. addr2 = dev[2].p2p_interface_addr()
  1023. dev[0].dump_monitor()
  1024. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1025. dev[2].dump_monitor()
  1026. dev[2].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  1027. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=10)
  1028. if ev is None:
  1029. raise Exception("Enrollee not seen")
  1030. if addr2 not in ev:
  1031. raise Exception("Unexpected Enrollee MAC address")
  1032. dev[0].request("WPS_ER_PIN " + addr2 + " " + pin + " " + addr2)
  1033. dev[2].wait_connected(timeout=30)
  1034. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  1035. if ev is None:
  1036. raise Exception("WPS ER did not report success")
  1037. logger.info("Verify registrar selection behavior")
  1038. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  1039. dev[1].request("DISCONNECT")
  1040. dev[1].wait_disconnected(timeout=10)
  1041. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
  1042. dev[1].scan(freq="2412")
  1043. bss = dev[1].get_bss(apdev[0]['bssid'])
  1044. if "[WPS-AUTH]" not in bss['flags']:
  1045. # It is possible for scan to miss an update especially when running
  1046. # tests under load with multiple VMs, so allow another attempt.
  1047. dev[1].scan(freq="2412")
  1048. bss = dev[1].get_bss(apdev[0]['bssid'])
  1049. if "[WPS-AUTH]" not in bss['flags']:
  1050. raise Exception("WPS-AUTH flag missing")
  1051. logger.info("Stop ER")
  1052. dev[0].dump_monitor()
  1053. dev[0].request("WPS_ER_STOP")
  1054. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"])
  1055. if ev is None:
  1056. raise Exception("WPS ER unsubscription timed out")
  1057. # It takes some time for the UPnP UNSUBSCRIBE command to go through, so wait
  1058. # a bit before verifying that the scan results have changed.
  1059. time.sleep(0.2)
  1060. for i in range(0, 10):
  1061. dev[1].request("BSS_FLUSH 0")
  1062. dev[1].scan(freq="2412", only_new=True)
  1063. bss = dev[1].get_bss(apdev[0]['bssid'])
  1064. if bss and 'flags' in bss and "[WPS-AUTH]" not in bss['flags']:
  1065. break
  1066. logger.debug("WPS-AUTH flag was still in place - wait a bit longer")
  1067. time.sleep(0.1)
  1068. if "[WPS-AUTH]" in bss['flags']:
  1069. raise Exception("WPS-AUTH flag not removed")
  1070. def test_ap_wps_er_add_enrollee_uuid(dev, apdev):
  1071. """WPS ER adding a new enrollee identified by UUID"""
  1072. try:
  1073. _test_ap_wps_er_add_enrollee_uuid(dev, apdev)
  1074. finally:
  1075. dev[0].request("WPS_ER_STOP")
  1076. def _test_ap_wps_er_add_enrollee_uuid(dev, apdev):
  1077. ssid = "wps-er-add-enrollee"
  1078. ap_pin = "12345670"
  1079. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1080. hostapd.add_ap(apdev[0],
  1081. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1082. "wpa_passphrase": "12345678", "wpa": "2",
  1083. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1084. "device_name": "Wireless AP", "manufacturer": "Company",
  1085. "model_name": "WAP", "model_number": "123",
  1086. "serial_number": "12345", "device_type": "6-0050F204-1",
  1087. "os_version": "01020300",
  1088. "config_methods": "label push_button",
  1089. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  1090. logger.info("WPS configuration step")
  1091. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1092. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1093. logger.info("Start ER")
  1094. dev[0].request("WPS_ER_START ifname=lo")
  1095. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1096. if ev is None:
  1097. raise Exception("AP discovery timed out")
  1098. if ap_uuid not in ev:
  1099. raise Exception("Expected AP UUID not found")
  1100. logger.info("Learn AP configuration through UPnP")
  1101. dev[0].dump_monitor()
  1102. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1103. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  1104. if ev is None:
  1105. raise Exception("AP learn timed out")
  1106. if ap_uuid not in ev:
  1107. raise Exception("Expected AP UUID not in settings")
  1108. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1109. if ev is None:
  1110. raise Exception("WPS-FAIL after AP learn timed out")
  1111. time.sleep(0.1)
  1112. logger.info("Add a specific Enrollee using ER (PBC/UUID)")
  1113. addr1 = dev[1].p2p_interface_addr()
  1114. dev[0].dump_monitor()
  1115. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1116. dev[1].dump_monitor()
  1117. dev[1].request("WPS_PBC %s" % apdev[0]['bssid'])
  1118. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=10)
  1119. if ev is None:
  1120. raise Exception("Enrollee not seen")
  1121. if addr1 not in ev:
  1122. raise Exception("Unexpected Enrollee MAC address")
  1123. uuid = ev.split(' ')[1]
  1124. dev[0].request("WPS_ER_PBC " + uuid)
  1125. dev[1].wait_connected(timeout=30)
  1126. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  1127. if ev is None:
  1128. raise Exception("WPS ER did not report success")
  1129. logger.info("Add a specific Enrollee using ER (PIN/UUID)")
  1130. pin = dev[2].wps_read_pin()
  1131. addr2 = dev[2].p2p_interface_addr()
  1132. dev[0].dump_monitor()
  1133. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1134. dev[2].dump_monitor()
  1135. dev[2].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  1136. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=10)
  1137. if ev is None:
  1138. raise Exception("Enrollee not seen")
  1139. if addr2 not in ev:
  1140. raise Exception("Unexpected Enrollee MAC address")
  1141. uuid = ev.split(' ')[1]
  1142. dev[0].request("WPS_ER_PIN " + uuid + " " + pin)
  1143. dev[2].wait_connected(timeout=30)
  1144. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  1145. if ev is None:
  1146. raise Exception("WPS ER did not report success")
  1147. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-REMOVE"], timeout=15)
  1148. if ev is None:
  1149. raise Exception("No Enrollee STA entry timeout seen")
  1150. logger.info("Stop ER")
  1151. dev[0].dump_monitor()
  1152. dev[0].request("WPS_ER_STOP")
  1153. def test_ap_wps_er_multi_add_enrollee(dev, apdev):
  1154. """Multiple WPS ERs adding a new enrollee using PIN"""
  1155. try:
  1156. _test_ap_wps_er_multi_add_enrollee(dev, apdev)
  1157. finally:
  1158. for i in range(2):
  1159. dev[i].request("WPS_ER_STOP")
  1160. def _test_ap_wps_er_multi_add_enrollee(dev, apdev):
  1161. ssid = "wps-er-add-enrollee"
  1162. ap_pin = "12345670"
  1163. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1164. hostapd.add_ap(apdev[0],
  1165. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1166. "wpa_passphrase": "12345678", "wpa": "2",
  1167. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1168. "device_name": "Wireless AP", "manufacturer": "Company",
  1169. "model_name": "WAP", "model_number": "123",
  1170. "serial_number": "12345", "device_type": "6-0050F204-1",
  1171. "os_version": "01020300",
  1172. 'friendly_name': "WPS AP",
  1173. "config_methods": "label push_button",
  1174. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  1175. for i in range(2):
  1176. dev[i].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1177. dev[i].wps_reg(apdev[0]['bssid'], ap_pin)
  1178. for i in range(2):
  1179. dev[i].request("WPS_ER_START ifname=lo")
  1180. for i in range(2):
  1181. ev = dev[i].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1182. if ev is None:
  1183. raise Exception("AP discovery timed out")
  1184. dev[i].dump_monitor()
  1185. for i in range(2):
  1186. dev[i].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1187. for i in range(2):
  1188. ev = dev[i].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  1189. if ev is None:
  1190. raise Exception("AP learn timed out")
  1191. ev = dev[i].wait_event(["WPS-FAIL"], timeout=15)
  1192. if ev is None:
  1193. raise Exception("WPS-FAIL after AP learn timed out")
  1194. time.sleep(0.1)
  1195. pin = dev[2].wps_read_pin()
  1196. addr = dev[2].own_addr()
  1197. dev[0].dump_monitor()
  1198. dev[0].request("WPS_ER_PIN any " + pin + " " + addr)
  1199. dev[1].dump_monitor()
  1200. dev[1].request("WPS_ER_PIN any " + pin + " " + addr)
  1201. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1202. dev[2].dump_monitor()
  1203. dev[2].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  1204. ev = dev[2].wait_event(["WPS-SUCCESS"], timeout=30)
  1205. if ev is None:
  1206. raise Exception("Enrollee did not report success")
  1207. dev[2].wait_connected(timeout=15)
  1208. def test_ap_wps_er_add_enrollee_pbc(dev, apdev):
  1209. """WPS ER connected to AP and adding a new enrollee using PBC"""
  1210. try:
  1211. _test_ap_wps_er_add_enrollee_pbc(dev, apdev)
  1212. finally:
  1213. dev[0].request("WPS_ER_STOP")
  1214. def _test_ap_wps_er_add_enrollee_pbc(dev, apdev):
  1215. ssid = "wps-er-add-enrollee-pbc"
  1216. ap_pin = "12345670"
  1217. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1218. hostapd.add_ap(apdev[0],
  1219. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1220. "wpa_passphrase": "12345678", "wpa": "2",
  1221. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1222. "device_name": "Wireless AP", "manufacturer": "Company",
  1223. "model_name": "WAP", "model_number": "123",
  1224. "serial_number": "12345", "device_type": "6-0050F204-1",
  1225. "os_version": "01020300",
  1226. "config_methods": "label push_button",
  1227. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  1228. logger.info("Learn AP configuration")
  1229. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1230. dev[0].dump_monitor()
  1231. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1232. status = dev[0].get_status()
  1233. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  1234. raise Exception("Not fully connected")
  1235. logger.info("Start ER")
  1236. dev[0].request("WPS_ER_START ifname=lo")
  1237. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1238. if ev is None:
  1239. raise Exception("AP discovery timed out")
  1240. if ap_uuid not in ev:
  1241. raise Exception("Expected AP UUID not found")
  1242. enrollee = dev[1].p2p_interface_addr()
  1243. if "FAIL-UNKNOWN-UUID" not in dev[0].request("WPS_ER_PBC " + enrollee):
  1244. raise Exception("Unknown UUID not reported")
  1245. logger.info("Add Enrollee using ER and PBC")
  1246. dev[0].dump_monitor()
  1247. dev[1].dump_monitor()
  1248. dev[1].request("WPS_PBC")
  1249. for i in range(0, 2):
  1250. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=15)
  1251. if ev is None:
  1252. raise Exception("Enrollee discovery timed out")
  1253. if enrollee in ev:
  1254. break
  1255. if i == 1:
  1256. raise Exception("Expected Enrollee not found")
  1257. if "FAIL-NO-AP-SETTINGS" not in dev[0].request("WPS_ER_PBC " + enrollee):
  1258. raise Exception("Unknown UUID not reported")
  1259. logger.info("Use learned network configuration on ER")
  1260. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  1261. if "OK" not in dev[0].request("WPS_ER_PBC " + enrollee):
  1262. raise Exception("WPS_ER_PBC failed")
  1263. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=15)
  1264. if ev is None:
  1265. raise Exception("Enrollee did not report success")
  1266. dev[1].wait_connected(timeout=15)
  1267. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  1268. if ev is None:
  1269. raise Exception("WPS ER did not report success")
  1270. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  1271. def test_ap_wps_er_pbc_overlap(dev, apdev):
  1272. """WPS ER connected to AP and PBC session overlap"""
  1273. try:
  1274. _test_ap_wps_er_pbc_overlap(dev, apdev)
  1275. finally:
  1276. dev[0].request("WPS_ER_STOP")
  1277. def _test_ap_wps_er_pbc_overlap(dev, apdev):
  1278. ssid = "wps-er-add-enrollee-pbc"
  1279. ap_pin = "12345670"
  1280. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1281. hostapd.add_ap(apdev[0],
  1282. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1283. "wpa_passphrase": "12345678", "wpa": "2",
  1284. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1285. "device_name": "Wireless AP", "manufacturer": "Company",
  1286. "model_name": "WAP", "model_number": "123",
  1287. "serial_number": "12345", "device_type": "6-0050F204-1",
  1288. "os_version": "01020300",
  1289. "config_methods": "label push_button",
  1290. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  1291. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1292. dev[0].dump_monitor()
  1293. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1294. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
  1295. dev[2].scan_for_bss(apdev[0]['bssid'], freq="2412")
  1296. # avoid leaving dev 1 or 2 as the last Probe Request to the AP
  1297. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412, force_scan=True)
  1298. dev[0].dump_monitor()
  1299. dev[0].request("WPS_ER_START ifname=lo")
  1300. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1301. if ev is None:
  1302. raise Exception("AP discovery timed out")
  1303. if ap_uuid not in ev:
  1304. raise Exception("Expected AP UUID not found")
  1305. # verify BSSID selection of the AP instead of UUID
  1306. if "FAIL" in dev[0].request("WPS_ER_SET_CONFIG " + apdev[0]['bssid'] + " 0"):
  1307. raise Exception("Could not select AP based on BSSID")
  1308. dev[0].dump_monitor()
  1309. dev[1].request("WPS_PBC " + apdev[0]['bssid'])
  1310. dev[2].request("WPS_PBC " + apdev[0]['bssid'])
  1311. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
  1312. if ev is None:
  1313. raise Exception("PBC scan failed")
  1314. ev = dev[2].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
  1315. if ev is None:
  1316. raise Exception("PBC scan failed")
  1317. found1 = False
  1318. found2 = False
  1319. addr1 = dev[1].own_addr()
  1320. addr2 = dev[2].own_addr()
  1321. for i in range(3):
  1322. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=15)
  1323. if ev is None:
  1324. raise Exception("Enrollee discovery timed out")
  1325. if addr1 in ev:
  1326. found1 = True
  1327. if found2:
  1328. break
  1329. if addr2 in ev:
  1330. found2 = True
  1331. if found1:
  1332. break
  1333. if dev[0].request("WPS_ER_PBC " + ap_uuid) != "FAIL-PBC-OVERLAP\n":
  1334. raise Exception("PBC overlap not reported")
  1335. dev[1].request("WPS_CANCEL")
  1336. dev[2].request("WPS_CANCEL")
  1337. if dev[0].request("WPS_ER_PBC foo") != "FAIL\n":
  1338. raise Exception("Invalid WPS_ER_PBC accepted")
  1339. def test_ap_wps_er_v10_add_enrollee_pin(dev, apdev):
  1340. """WPS v1.0 ER connected to AP and adding a new enrollee using PIN"""
  1341. try:
  1342. _test_ap_wps_er_v10_add_enrollee_pin(dev, apdev)
  1343. finally:
  1344. dev[0].request("WPS_ER_STOP")
  1345. def _test_ap_wps_er_v10_add_enrollee_pin(dev, apdev):
  1346. ssid = "wps-er-add-enrollee-pbc"
  1347. ap_pin = "12345670"
  1348. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1349. hostapd.add_ap(apdev[0],
  1350. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1351. "wpa_passphrase": "12345678", "wpa": "2",
  1352. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1353. "device_name": "Wireless AP", "manufacturer": "Company",
  1354. "model_name": "WAP", "model_number": "123",
  1355. "serial_number": "12345", "device_type": "6-0050F204-1",
  1356. "os_version": "01020300",
  1357. "config_methods": "label push_button",
  1358. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  1359. logger.info("Learn AP configuration")
  1360. dev[0].request("SET wps_version_number 0x10")
  1361. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1362. dev[0].dump_monitor()
  1363. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1364. status = dev[0].get_status()
  1365. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  1366. raise Exception("Not fully connected")
  1367. logger.info("Start ER")
  1368. dev[0].request("WPS_ER_START ifname=lo")
  1369. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1370. if ev is None:
  1371. raise Exception("AP discovery timed out")
  1372. if ap_uuid not in ev:
  1373. raise Exception("Expected AP UUID not found")
  1374. logger.info("Use learned network configuration on ER")
  1375. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  1376. logger.info("Add Enrollee using ER and PIN")
  1377. enrollee = dev[1].p2p_interface_addr()
  1378. pin = dev[1].wps_read_pin()
  1379. dev[0].dump_monitor()
  1380. dev[0].request("WPS_ER_PIN any " + pin + " " + enrollee)
  1381. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1382. dev[1].dump_monitor()
  1383. dev[1].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  1384. dev[1].wait_connected(timeout=30)
  1385. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  1386. if ev is None:
  1387. raise Exception("WPS ER did not report success")
  1388. @remote_compatible
  1389. def test_ap_wps_er_config_ap(dev, apdev):
  1390. """WPS ER configuring AP over UPnP"""
  1391. try:
  1392. _test_ap_wps_er_config_ap(dev, apdev)
  1393. finally:
  1394. dev[0].request("WPS_ER_STOP")
  1395. def _test_ap_wps_er_config_ap(dev, apdev):
  1396. ssid = "wps-er-ap-config"
  1397. ap_pin = "12345670"
  1398. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1399. hostapd.add_ap(apdev[0],
  1400. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1401. "wpa_passphrase": "12345678", "wpa": "2",
  1402. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1403. "device_name": "Wireless AP", "manufacturer": "Company",
  1404. "model_name": "WAP", "model_number": "123",
  1405. "serial_number": "12345", "device_type": "6-0050F204-1",
  1406. "os_version": "01020300",
  1407. "config_methods": "label push_button",
  1408. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  1409. logger.info("Connect ER to the AP")
  1410. dev[0].connect(ssid, psk="12345678", scan_freq="2412")
  1411. logger.info("WPS configuration step")
  1412. dev[0].request("WPS_ER_START ifname=lo")
  1413. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1414. if ev is None:
  1415. raise Exception("AP discovery timed out")
  1416. if ap_uuid not in ev:
  1417. raise Exception("Expected AP UUID not found")
  1418. new_passphrase = "1234567890"
  1419. dev[0].request("WPS_ER_CONFIG " + apdev[0]['bssid'] + " " + ap_pin + " " +
  1420. ssid.encode("hex") + " WPA2PSK CCMP " +
  1421. new_passphrase.encode("hex"))
  1422. ev = dev[0].wait_event(["WPS-SUCCESS"])
  1423. if ev is None:
  1424. raise Exception("WPS ER configuration operation timed out")
  1425. dev[0].wait_disconnected(timeout=10)
  1426. dev[0].connect(ssid, psk="1234567890", scan_freq="2412")
  1427. logger.info("WPS ER restart")
  1428. dev[0].request("WPS_ER_START")
  1429. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1430. if ev is None:
  1431. raise Exception("AP discovery timed out on ER restart")
  1432. if ap_uuid not in ev:
  1433. raise Exception("Expected AP UUID not found on ER restart")
  1434. if "OK" not in dev[0].request("WPS_ER_STOP"):
  1435. raise Exception("WPS_ER_STOP failed")
  1436. if "OK" not in dev[0].request("WPS_ER_STOP"):
  1437. raise Exception("WPS_ER_STOP failed")
  1438. @remote_compatible
  1439. def test_ap_wps_er_cache_ap_settings(dev, apdev):
  1440. """WPS ER caching AP settings"""
  1441. try:
  1442. _test_ap_wps_er_cache_ap_settings(dev, apdev)
  1443. finally:
  1444. dev[0].request("WPS_ER_STOP")
  1445. def _test_ap_wps_er_cache_ap_settings(dev, apdev):
  1446. ssid = "wps-er-add-enrollee"
  1447. ap_pin = "12345670"
  1448. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1449. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1450. "wpa_passphrase": "12345678", "wpa": "2",
  1451. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1452. "device_name": "Wireless AP", "manufacturer": "Company",
  1453. "model_name": "WAP", "model_number": "123",
  1454. "serial_number": "12345", "device_type": "6-0050F204-1",
  1455. "os_version": "01020300",
  1456. "config_methods": "label push_button",
  1457. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo" }
  1458. hapd = hostapd.add_ap(apdev[0], params)
  1459. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1460. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1461. id = int(dev[0].list_networks()[0]['id'])
  1462. dev[0].set_network(id, "scan_freq", "2412")
  1463. dev[0].request("WPS_ER_START ifname=lo")
  1464. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1465. if ev is None:
  1466. raise Exception("AP discovery timed out")
  1467. if ap_uuid not in ev:
  1468. raise Exception("Expected AP UUID not found")
  1469. dev[0].dump_monitor()
  1470. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1471. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  1472. if ev is None:
  1473. raise Exception("AP learn timed out")
  1474. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1475. if ev is None:
  1476. raise Exception("WPS-FAIL after AP learn timed out")
  1477. time.sleep(0.1)
  1478. hapd.disable()
  1479. for i in range(2):
  1480. ev = dev[0].wait_event([ "WPS-ER-AP-REMOVE",
  1481. "CTRL-EVENT-DISCONNECTED" ],
  1482. timeout=15)
  1483. if ev is None:
  1484. raise Exception("AP removal or disconnection timed out")
  1485. hapd = hostapd.add_ap(apdev[0], params)
  1486. for i in range(2):
  1487. ev = dev[0].wait_event([ "WPS-ER-AP-ADD", "CTRL-EVENT-CONNECTED" ],
  1488. timeout=15)
  1489. if ev is None:
  1490. raise Exception("AP discovery or connection timed out")
  1491. pin = dev[1].wps_read_pin()
  1492. dev[0].dump_monitor()
  1493. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  1494. time.sleep(0.2)
  1495. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1496. dev[1].dump_monitor()
  1497. dev[1].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  1498. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=30)
  1499. if ev is None:
  1500. raise Exception("Enrollee did not report success")
  1501. dev[1].wait_connected(timeout=15)
  1502. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  1503. if ev is None:
  1504. raise Exception("WPS ER did not report success")
  1505. dev[0].dump_monitor()
  1506. dev[0].request("WPS_ER_STOP")
  1507. def test_ap_wps_er_cache_ap_settings_oom(dev, apdev):
  1508. """WPS ER caching AP settings (OOM)"""
  1509. try:
  1510. _test_ap_wps_er_cache_ap_settings_oom(dev, apdev)
  1511. finally:
  1512. dev[0].request("WPS_ER_STOP")
  1513. def _test_ap_wps_er_cache_ap_settings_oom(dev, apdev):
  1514. ssid = "wps-er-add-enrollee"
  1515. ap_pin = "12345670"
  1516. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1517. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1518. "wpa_passphrase": "12345678", "wpa": "2",
  1519. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1520. "device_name": "Wireless AP", "manufacturer": "Company",
  1521. "model_name": "WAP", "model_number": "123",
  1522. "serial_number": "12345", "device_type": "6-0050F204-1",
  1523. "os_version": "01020300",
  1524. "config_methods": "label push_button",
  1525. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo" }
  1526. hapd = hostapd.add_ap(apdev[0], params)
  1527. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1528. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1529. id = int(dev[0].list_networks()[0]['id'])
  1530. dev[0].set_network(id, "scan_freq", "2412")
  1531. dev[0].request("WPS_ER_START ifname=lo")
  1532. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1533. if ev is None:
  1534. raise Exception("AP discovery timed out")
  1535. if ap_uuid not in ev:
  1536. raise Exception("Expected AP UUID not found")
  1537. dev[0].dump_monitor()
  1538. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1539. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  1540. if ev is None:
  1541. raise Exception("AP learn timed out")
  1542. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1543. if ev is None:
  1544. raise Exception("WPS-FAIL after AP learn timed out")
  1545. time.sleep(0.1)
  1546. with alloc_fail(dev[0], 1, "=wps_er_ap_use_cached_settings"):
  1547. hapd.disable()
  1548. for i in range(2):
  1549. ev = dev[0].wait_event([ "WPS-ER-AP-REMOVE",
  1550. "CTRL-EVENT-DISCONNECTED" ],
  1551. timeout=15)
  1552. if ev is None:
  1553. raise Exception("AP removal or disconnection timed out")
  1554. hapd = hostapd.add_ap(apdev[0], params)
  1555. for i in range(2):
  1556. ev = dev[0].wait_event([ "WPS-ER-AP-ADD", "CTRL-EVENT-CONNECTED" ],
  1557. timeout=15)
  1558. if ev is None:
  1559. raise Exception("AP discovery or connection timed out")
  1560. dev[0].request("WPS_ER_STOP")
  1561. def test_ap_wps_er_cache_ap_settings_oom2(dev, apdev):
  1562. """WPS ER caching AP settings (OOM 2)"""
  1563. try:
  1564. _test_ap_wps_er_cache_ap_settings_oom2(dev, apdev)
  1565. finally:
  1566. dev[0].request("WPS_ER_STOP")
  1567. def _test_ap_wps_er_cache_ap_settings_oom2(dev, apdev):
  1568. ssid = "wps-er-add-enrollee"
  1569. ap_pin = "12345670"
  1570. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1571. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1572. "wpa_passphrase": "12345678", "wpa": "2",
  1573. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1574. "device_name": "Wireless AP", "manufacturer": "Company",
  1575. "model_name": "WAP", "model_number": "123",
  1576. "serial_number": "12345", "device_type": "6-0050F204-1",
  1577. "os_version": "01020300",
  1578. "config_methods": "label push_button",
  1579. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo" }
  1580. hapd = hostapd.add_ap(apdev[0], params)
  1581. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1582. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1583. id = int(dev[0].list_networks()[0]['id'])
  1584. dev[0].set_network(id, "scan_freq", "2412")
  1585. dev[0].request("WPS_ER_START ifname=lo")
  1586. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1587. if ev is None:
  1588. raise Exception("AP discovery timed out")
  1589. if ap_uuid not in ev:
  1590. raise Exception("Expected AP UUID not found")
  1591. dev[0].dump_monitor()
  1592. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1593. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  1594. if ev is None:
  1595. raise Exception("AP learn timed out")
  1596. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1597. if ev is None:
  1598. raise Exception("WPS-FAIL after AP learn timed out")
  1599. time.sleep(0.1)
  1600. with alloc_fail(dev[0], 1, "=wps_er_ap_cache_settings"):
  1601. hapd.disable()
  1602. for i in range(2):
  1603. ev = dev[0].wait_event([ "WPS-ER-AP-REMOVE",
  1604. "CTRL-EVENT-DISCONNECTED" ],
  1605. timeout=15)
  1606. if ev is None:
  1607. raise Exception("AP removal or disconnection timed out")
  1608. hapd = hostapd.add_ap(apdev[0], params)
  1609. for i in range(2):
  1610. ev = dev[0].wait_event([ "WPS-ER-AP-ADD", "CTRL-EVENT-CONNECTED" ],
  1611. timeout=15)
  1612. if ev is None:
  1613. raise Exception("AP discovery or connection timed out")
  1614. dev[0].request("WPS_ER_STOP")
  1615. def test_ap_wps_er_subscribe_oom(dev, apdev):
  1616. """WPS ER subscribe OOM"""
  1617. try:
  1618. _test_ap_wps_er_subscribe_oom(dev, apdev)
  1619. finally:
  1620. dev[0].request("WPS_ER_STOP")
  1621. def _test_ap_wps_er_subscribe_oom(dev, apdev):
  1622. ssid = "wps-er-add-enrollee"
  1623. ap_pin = "12345670"
  1624. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1625. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1626. "wpa_passphrase": "12345678", "wpa": "2",
  1627. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1628. "device_name": "Wireless AP", "manufacturer": "Company",
  1629. "model_name": "WAP", "model_number": "123",
  1630. "serial_number": "12345", "device_type": "6-0050F204-1",
  1631. "os_version": "01020300",
  1632. "config_methods": "label push_button",
  1633. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo" }
  1634. hapd = hostapd.add_ap(apdev[0], params)
  1635. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1636. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1637. id = int(dev[0].list_networks()[0]['id'])
  1638. dev[0].set_network(id, "scan_freq", "2412")
  1639. with alloc_fail(dev[0], 1, "http_client_addr;wps_er_subscribe"):
  1640. dev[0].request("WPS_ER_START ifname=lo")
  1641. for i in range(50):
  1642. res = dev[0].request("GET_ALLOC_FAIL")
  1643. if res.startswith("0:"):
  1644. break
  1645. time.sleep(0.1)
  1646. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=0)
  1647. if ev:
  1648. raise Exception("Unexpected AP discovery during OOM")
  1649. dev[0].request("WPS_ER_STOP")
  1650. def test_ap_wps_er_set_sel_reg_oom(dev, apdev):
  1651. """WPS ER SetSelectedRegistrar OOM"""
  1652. try:
  1653. _test_ap_wps_er_set_sel_reg_oom(dev, apdev)
  1654. finally:
  1655. dev[0].request("WPS_ER_STOP")
  1656. def _test_ap_wps_er_set_sel_reg_oom(dev, apdev):
  1657. ssid = "wps-er-add-enrollee"
  1658. ap_pin = "12345670"
  1659. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1660. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1661. "wpa_passphrase": "12345678", "wpa": "2",
  1662. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1663. "device_name": "Wireless AP", "manufacturer": "Company",
  1664. "model_name": "WAP", "model_number": "123",
  1665. "serial_number": "12345", "device_type": "6-0050F204-1",
  1666. "os_version": "01020300",
  1667. "config_methods": "label push_button",
  1668. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo" }
  1669. hapd = hostapd.add_ap(apdev[0], params)
  1670. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1671. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1672. dev[0].request("WPS_ER_START ifname=lo")
  1673. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=10)
  1674. if ev is None:
  1675. raise Exception("AP not discovered")
  1676. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1677. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  1678. if ev is None:
  1679. raise Exception("AP learn timed out")
  1680. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1681. if ev is None:
  1682. raise Exception("WPS-FAIL timed out")
  1683. time.sleep(0.1)
  1684. for func in [ "http_client_url_parse;wps_er_send_set_sel_reg",
  1685. "wps_er_soap_hdr;wps_er_send_set_sel_reg",
  1686. "http_client_addr;wps_er_send_set_sel_reg",
  1687. "wpabuf_alloc;wps_er_set_sel_reg" ]:
  1688. with alloc_fail(dev[0], 1, func):
  1689. if "OK" not in dev[0].request("WPS_ER_PBC " + ap_uuid):
  1690. raise Exception("WPS_ER_PBC failed")
  1691. ev = dev[0].wait_event(["WPS-PBC-ACTIVE"], timeout=3)
  1692. if ev is None:
  1693. raise Exception("WPS-PBC-ACTIVE not seen")
  1694. dev[0].request("WPS_ER_STOP")
  1695. @remote_compatible
  1696. def test_ap_wps_er_learn_oom(dev, apdev):
  1697. """WPS ER learn OOM"""
  1698. try:
  1699. _test_ap_wps_er_learn_oom(dev, apdev)
  1700. finally:
  1701. dev[0].request("WPS_ER_STOP")
  1702. def _test_ap_wps_er_learn_oom(dev, apdev):
  1703. ssid = "wps-er-add-enrollee"
  1704. ap_pin = "12345670"
  1705. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1706. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1707. "wpa_passphrase": "12345678", "wpa": "2",
  1708. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1709. "device_name": "Wireless AP", "manufacturer": "Company",
  1710. "model_name": "WAP", "model_number": "123",
  1711. "serial_number": "12345", "device_type": "6-0050F204-1",
  1712. "os_version": "01020300",
  1713. "config_methods": "label push_button",
  1714. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo" }
  1715. hapd = hostapd.add_ap(apdev[0], params)
  1716. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1717. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  1718. dev[0].request("WPS_ER_START ifname=lo")
  1719. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=10)
  1720. if ev is None:
  1721. raise Exception("AP not discovered")
  1722. for func in [ "wps_er_http_put_message_cb",
  1723. "xml_get_base64_item;wps_er_http_put_message_cb",
  1724. "http_client_url_parse;wps_er_ap_put_message",
  1725. "wps_er_soap_hdr;wps_er_ap_put_message",
  1726. "http_client_addr;wps_er_ap_put_message" ]:
  1727. with alloc_fail(dev[0], 1, func):
  1728. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1729. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=1)
  1730. if ev is not None:
  1731. raise Exception("AP learn succeeded during OOM")
  1732. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  1733. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=10)
  1734. if ev is None:
  1735. raise Exception("AP learn did not succeed")
  1736. if "FAIL" not in dev[0].request("WPS_ER_LEARN 00000000-9e5c-4e73-bd82-f89cbcd10d7e " + ap_pin):
  1737. raise Exception("WPS_ER_LEARN for unknown AP accepted")
  1738. dev[0].request("WPS_ER_STOP")
  1739. def test_ap_wps_fragmentation(dev, apdev):
  1740. """WPS with fragmentation in EAP-WSC and mixed mode WPA+WPA2"""
  1741. ssid = "test-wps-fragmentation"
  1742. appin = "12345670"
  1743. hapd = hostapd.add_ap(apdev[0],
  1744. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1745. "wpa_passphrase": "12345678", "wpa": "3",
  1746. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1747. "wpa_pairwise": "TKIP", "ap_pin": appin,
  1748. "fragment_size": "50" })
  1749. logger.info("WPS provisioning step (PBC)")
  1750. hapd.request("WPS_PBC")
  1751. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1752. dev[0].dump_monitor()
  1753. dev[0].request("SET wps_fragment_size 50")
  1754. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  1755. dev[0].wait_connected(timeout=30)
  1756. status = dev[0].get_status()
  1757. if status['wpa_state'] != 'COMPLETED':
  1758. raise Exception("Not fully connected")
  1759. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  1760. raise Exception("Unexpected encryption configuration")
  1761. if status['key_mgmt'] != 'WPA2-PSK':
  1762. raise Exception("Unexpected key_mgmt")
  1763. logger.info("WPS provisioning step (PIN)")
  1764. pin = dev[1].wps_read_pin()
  1765. hapd.request("WPS_PIN any " + pin)
  1766. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1767. dev[1].request("SET wps_fragment_size 50")
  1768. dev[1].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  1769. dev[1].wait_connected(timeout=30)
  1770. status = dev[1].get_status()
  1771. if status['wpa_state'] != 'COMPLETED':
  1772. raise Exception("Not fully connected")
  1773. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  1774. raise Exception("Unexpected encryption configuration")
  1775. if status['key_mgmt'] != 'WPA2-PSK':
  1776. raise Exception("Unexpected key_mgmt")
  1777. logger.info("WPS connection as registrar")
  1778. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1779. dev[2].request("SET wps_fragment_size 50")
  1780. dev[2].wps_reg(apdev[0]['bssid'], appin)
  1781. status = dev[2].get_status()
  1782. if status['wpa_state'] != 'COMPLETED':
  1783. raise Exception("Not fully connected")
  1784. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  1785. raise Exception("Unexpected encryption configuration")
  1786. if status['key_mgmt'] != 'WPA2-PSK':
  1787. raise Exception("Unexpected key_mgmt")
  1788. @remote_compatible
  1789. def test_ap_wps_new_version_sta(dev, apdev):
  1790. """WPS compatibility with new version number on the station"""
  1791. ssid = "test-wps-ver"
  1792. hapd = hostapd.add_ap(apdev[0],
  1793. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1794. "wpa_passphrase": "12345678", "wpa": "2",
  1795. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  1796. logger.info("WPS provisioning step")
  1797. hapd.request("WPS_PBC")
  1798. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  1799. dev[0].dump_monitor()
  1800. dev[0].request("SET wps_version_number 0x43")
  1801. dev[0].request("SET wps_vendor_ext_m1 000137100100020001")
  1802. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  1803. dev[0].wait_connected(timeout=30)
  1804. @remote_compatible
  1805. def test_ap_wps_new_version_ap(dev, apdev):
  1806. """WPS compatibility with new version number on the AP"""
  1807. ssid = "test-wps-ver"
  1808. hapd = hostapd.add_ap(apdev[0],
  1809. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1810. "wpa_passphrase": "12345678", "wpa": "2",
  1811. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  1812. logger.info("WPS provisioning step")
  1813. if "FAIL" in hapd.request("SET wps_version_number 0x43"):
  1814. raise Exception("Failed to enable test functionality")
  1815. hapd.request("WPS_PBC")
  1816. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  1817. dev[0].dump_monitor()
  1818. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  1819. dev[0].wait_connected(timeout=30)
  1820. hapd.request("SET wps_version_number 0x20")
  1821. @remote_compatible
  1822. def test_ap_wps_check_pin(dev, apdev):
  1823. """Verify PIN checking through control interface"""
  1824. hapd = hostapd.add_ap(apdev[0],
  1825. { "ssid": "wps", "eap_server": "1", "wps_state": "2",
  1826. "wpa_passphrase": "12345678", "wpa": "2",
  1827. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  1828. for t in [ ("12345670", "12345670"),
  1829. ("12345678", "FAIL-CHECKSUM"),
  1830. ("12345", "FAIL"),
  1831. ("123456789", "FAIL"),
  1832. ("1234-5670", "12345670"),
  1833. ("1234 5670", "12345670"),
  1834. ("1-2.3:4 5670", "12345670") ]:
  1835. res = hapd.request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
  1836. res2 = dev[0].request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
  1837. if res != res2:
  1838. raise Exception("Unexpected difference in WPS_CHECK_PIN responses")
  1839. if res != t[1]:
  1840. raise Exception("Incorrect WPS_CHECK_PIN response {} (expected {})".format(res, t[1]))
  1841. if "FAIL" not in hapd.request("WPS_CHECK_PIN 12345"):
  1842. raise Exception("Unexpected WPS_CHECK_PIN success")
  1843. if "FAIL" not in hapd.request("WPS_CHECK_PIN 123456789"):
  1844. raise Exception("Unexpected WPS_CHECK_PIN success")
  1845. for i in range(0, 10):
  1846. pin = dev[0].request("WPS_PIN get")
  1847. rpin = dev[0].request("WPS_CHECK_PIN " + pin).rstrip('\n')
  1848. if pin != rpin:
  1849. raise Exception("Random PIN validation failed for " + pin)
  1850. def test_ap_wps_wep_config(dev, apdev):
  1851. """WPS 2.0 AP rejecting WEP configuration"""
  1852. ssid = "test-wps-config"
  1853. appin = "12345670"
  1854. hapd = hostapd.add_ap(apdev[0],
  1855. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1856. "ap_pin": appin})
  1857. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1858. dev[0].wps_reg(apdev[0]['bssid'], appin, "wps-new-ssid-wep", "OPEN", "WEP",
  1859. "hello", no_wait=True)
  1860. ev = hapd.wait_event(["WPS-FAIL"], timeout=15)
  1861. if ev is None:
  1862. raise Exception("WPS-FAIL timed out")
  1863. if "reason=2" not in ev:
  1864. raise Exception("Unexpected reason code in WPS-FAIL")
  1865. status = hapd.request("WPS_GET_STATUS")
  1866. if "Last WPS result: Failed" not in status:
  1867. raise Exception("WPS failure result not shown correctly")
  1868. if "Failure Reason: WEP Prohibited" not in status:
  1869. raise Exception("Failure reason not reported correctly")
  1870. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  1871. raise Exception("Peer address not shown correctly")
  1872. def test_ap_wps_wep_enroll(dev, apdev):
  1873. """WPS 2.0 STA rejecting WEP configuration"""
  1874. ssid = "test-wps-wep"
  1875. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1876. "skip_cred_build": "1", "extra_cred": "wps-wep-cred" }
  1877. hapd = hostapd.add_ap(apdev[0], params)
  1878. hapd.request("WPS_PBC")
  1879. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1880. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  1881. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1882. if ev is None:
  1883. raise Exception("WPS-FAIL event timed out")
  1884. if "msg=12" not in ev or "reason=2 (WEP Prohibited)" not in ev:
  1885. raise Exception("Unexpected WPS-FAIL event: " + ev)
  1886. @remote_compatible
  1887. def test_ap_wps_ie_fragmentation(dev, apdev):
  1888. """WPS AP using fragmented WPS IE"""
  1889. ssid = "test-wps-ie-fragmentation"
  1890. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1891. "wpa_passphrase": "12345678", "wpa": "2",
  1892. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1893. "device_name": "1234567890abcdef1234567890abcdef",
  1894. "manufacturer": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
  1895. "model_name": "1234567890abcdef1234567890abcdef",
  1896. "model_number": "1234567890abcdef1234567890abcdef",
  1897. "serial_number": "1234567890abcdef1234567890abcdef" }
  1898. hapd = hostapd.add_ap(apdev[0], params)
  1899. hapd.request("WPS_PBC")
  1900. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  1901. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  1902. dev[0].wait_connected(timeout=30)
  1903. bss = dev[0].get_bss(apdev[0]['bssid'])
  1904. if "wps_device_name" not in bss or bss['wps_device_name'] != "1234567890abcdef1234567890abcdef":
  1905. logger.info("Device Name not received correctly")
  1906. logger.info(bss)
  1907. # This can fail if Probe Response frame is missed and Beacon frame was
  1908. # used to fill in the BSS entry. This can happen, e.g., during heavy
  1909. # load every now and then and is not really an error, so try to
  1910. # workaround by runnign another scan.
  1911. dev[0].scan(freq="2412", only_new=True)
  1912. bss = dev[0].get_bss(apdev[0]['bssid'])
  1913. if not bss or "wps_device_name" not in bss or bss['wps_device_name'] != "1234567890abcdef1234567890abcdef":
  1914. logger.info(bss)
  1915. raise Exception("Device Name not received correctly")
  1916. if len(re.findall("dd..0050f204", bss['ie'])) != 2:
  1917. raise Exception("Unexpected number of WPS IEs")
  1918. def get_psk(pskfile):
  1919. psks = {}
  1920. with open(pskfile, "r") as f:
  1921. lines = f.read().splitlines()
  1922. for l in lines:
  1923. if l == "# WPA PSKs":
  1924. continue
  1925. (addr,psk) = l.split(' ')
  1926. psks[addr] = psk
  1927. return psks
  1928. def test_ap_wps_per_station_psk(dev, apdev):
  1929. """WPS PBC provisioning with per-station PSK"""
  1930. addr0 = dev[0].own_addr()
  1931. addr1 = dev[1].own_addr()
  1932. addr2 = dev[2].own_addr()
  1933. ssid = "wps"
  1934. appin = "12345670"
  1935. pskfile = "/tmp/ap_wps_per_enrollee_psk.psk_file"
  1936. try:
  1937. os.remove(pskfile)
  1938. except:
  1939. pass
  1940. hapd = None
  1941. try:
  1942. with open(pskfile, "w") as f:
  1943. f.write("# WPA PSKs\n")
  1944. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1945. "wpa": "2", "wpa_key_mgmt": "WPA-PSK",
  1946. "rsn_pairwise": "CCMP", "ap_pin": appin,
  1947. "wpa_psk_file": pskfile }
  1948. hapd = hostapd.add_ap(apdev[0], params)
  1949. logger.info("First enrollee")
  1950. hapd.request("WPS_PBC")
  1951. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1952. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  1953. dev[0].wait_connected(timeout=30)
  1954. logger.info("Second enrollee")
  1955. hapd.request("WPS_PBC")
  1956. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1957. dev[1].request("WPS_PBC " + apdev[0]['bssid'])
  1958. dev[1].wait_connected(timeout=30)
  1959. logger.info("External registrar")
  1960. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1961. dev[2].wps_reg(apdev[0]['bssid'], appin)
  1962. logger.info("Verifying PSK results")
  1963. psks = get_psk(pskfile)
  1964. if addr0 not in psks:
  1965. raise Exception("No PSK recorded for sta0")
  1966. if addr1 not in psks:
  1967. raise Exception("No PSK recorded for sta1")
  1968. if addr2 not in psks:
  1969. raise Exception("No PSK recorded for sta2")
  1970. if psks[addr0] == psks[addr1]:
  1971. raise Exception("Same PSK recorded for sta0 and sta1")
  1972. if psks[addr0] == psks[addr2]:
  1973. raise Exception("Same PSK recorded for sta0 and sta2")
  1974. if psks[addr1] == psks[addr2]:
  1975. raise Exception("Same PSK recorded for sta1 and sta2")
  1976. dev[0].request("REMOVE_NETWORK all")
  1977. logger.info("Second external registrar")
  1978. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  1979. dev[0].wps_reg(apdev[0]['bssid'], appin)
  1980. psks2 = get_psk(pskfile)
  1981. if addr0 not in psks2:
  1982. raise Exception("No PSK recorded for sta0(reg)")
  1983. if psks[addr0] == psks2[addr0]:
  1984. raise Exception("Same PSK recorded for sta0(enrollee) and sta0(reg)")
  1985. finally:
  1986. os.remove(pskfile)
  1987. if hapd:
  1988. dev[0].request("DISCONNECT")
  1989. dev[1].request("DISCONNECT")
  1990. dev[2].request("DISCONNECT")
  1991. hapd.disable()
  1992. dev[0].flush_scan_cache()
  1993. dev[1].flush_scan_cache()
  1994. dev[2].flush_scan_cache()
  1995. def test_ap_wps_per_station_psk_failure(dev, apdev):
  1996. """WPS PBC provisioning with per-station PSK (file not writable)"""
  1997. addr0 = dev[0].p2p_dev_addr()
  1998. addr1 = dev[1].p2p_dev_addr()
  1999. addr2 = dev[2].p2p_dev_addr()
  2000. ssid = "wps"
  2001. appin = "12345670"
  2002. pskfile = "/tmp/ap_wps_per_enrollee_psk.psk_file"
  2003. try:
  2004. os.remove(pskfile)
  2005. except:
  2006. pass
  2007. hapd = None
  2008. try:
  2009. with open(pskfile, "w") as f:
  2010. f.write("# WPA PSKs\n")
  2011. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  2012. "wpa": "2", "wpa_key_mgmt": "WPA-PSK",
  2013. "rsn_pairwise": "CCMP", "ap_pin": appin,
  2014. "wpa_psk_file": pskfile }
  2015. hapd = hostapd.add_ap(apdev[0], params)
  2016. if "FAIL" in hapd.request("SET wpa_psk_file /tmp/does/not/exists/ap_wps_per_enrollee_psk_failure.psk_file"):
  2017. raise Exception("Failed to set wpa_psk_file")
  2018. logger.info("First enrollee")
  2019. hapd.request("WPS_PBC")
  2020. dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
  2021. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  2022. dev[0].wait_connected(timeout=30)
  2023. logger.info("Second enrollee")
  2024. hapd.request("WPS_PBC")
  2025. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  2026. dev[1].request("WPS_PBC " + apdev[0]['bssid'])
  2027. dev[1].wait_connected(timeout=30)
  2028. logger.info("External registrar")
  2029. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  2030. dev[2].wps_reg(apdev[0]['bssid'], appin)
  2031. logger.info("Verifying PSK results")
  2032. psks = get_psk(pskfile)
  2033. if len(psks) > 0:
  2034. raise Exception("PSK recorded unexpectedly")
  2035. finally:
  2036. if hapd:
  2037. for i in range(3):
  2038. dev[i].request("DISCONNECT")
  2039. hapd.disable()
  2040. for i in range(3):
  2041. dev[i].flush_scan_cache()
  2042. os.remove(pskfile)
  2043. def test_ap_wps_pin_request_file(dev, apdev):
  2044. """WPS PIN provisioning with configured AP"""
  2045. ssid = "wps"
  2046. pinfile = "/tmp/ap_wps_pin_request_file.log"
  2047. if os.path.exists(pinfile):
  2048. os.remove(pinfile)
  2049. hapd = hostapd.add_ap(apdev[0],
  2050. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  2051. "wps_pin_requests": pinfile,
  2052. "wpa_passphrase": "12345678", "wpa": "2",
  2053. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  2054. uuid = dev[0].get_status_field("uuid")
  2055. pin = dev[0].wps_read_pin()
  2056. try:
  2057. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  2058. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  2059. ev = hapd.wait_event(["WPS-PIN-NEEDED"], timeout=15)
  2060. if ev is None:
  2061. raise Exception("PIN needed event not shown")
  2062. if uuid not in ev:
  2063. raise Exception("UUID mismatch")
  2064. dev[0].request("WPS_CANCEL")
  2065. success = False
  2066. with open(pinfile, "r") as f:
  2067. lines = f.readlines()
  2068. for l in lines:
  2069. if uuid in l:
  2070. success = True
  2071. break
  2072. if not success:
  2073. raise Exception("PIN request entry not in the log file")
  2074. finally:
  2075. try:
  2076. os.remove(pinfile)
  2077. except:
  2078. pass
  2079. def test_ap_wps_auto_setup_with_config_file(dev, apdev):
  2080. """WPS auto-setup with configuration file"""
  2081. conffile = "/tmp/ap_wps_auto_setup_with_config_file.conf"
  2082. ifname = apdev[0]['ifname']
  2083. try:
  2084. with open(conffile, "w") as f:
  2085. f.write("driver=nl80211\n")
  2086. f.write("hw_mode=g\n")
  2087. f.write("channel=1\n")
  2088. f.write("ieee80211n=1\n")
  2089. f.write("interface=%s\n" % ifname)
  2090. f.write("ctrl_interface=/var/run/hostapd\n")
  2091. f.write("ssid=wps\n")
  2092. f.write("eap_server=1\n")
  2093. f.write("wps_state=1\n")
  2094. hapd = hostapd.add_bss(apdev[0], ifname, conffile)
  2095. hapd.request("WPS_PBC")
  2096. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  2097. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  2098. dev[0].wait_connected(timeout=30)
  2099. with open(conffile, "r") as f:
  2100. lines = f.read().splitlines()
  2101. vals = dict()
  2102. for l in lines:
  2103. try:
  2104. [name,value] = l.split('=', 1)
  2105. vals[name] = value
  2106. except ValueError, e:
  2107. if "# WPS configuration" in l:
  2108. pass
  2109. else:
  2110. raise Exception("Unexpected configuration line: " + l)
  2111. if vals['ieee80211n'] != '1' or vals['wps_state'] != '2' or "WPA-PSK" not in vals['wpa_key_mgmt']:
  2112. raise Exception("Incorrect configuration: " + str(vals))
  2113. finally:
  2114. try:
  2115. os.remove(conffile)
  2116. except:
  2117. pass
  2118. def test_ap_wps_pbc_timeout(dev, apdev, params):
  2119. """wpa_supplicant PBC walk time and WPS ER SelReg timeout [long]"""
  2120. if not params['long']:
  2121. raise HwsimSkip("Skip test case with long duration due to --long not specified")
  2122. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2123. hapd = add_ssdp_ap(apdev[0], ap_uuid)
  2124. location = ssdp_get_location(ap_uuid)
  2125. urls = upnp_get_urls(location)
  2126. eventurl = urlparse.urlparse(urls['event_sub_url'])
  2127. ctrlurl = urlparse.urlparse(urls['control_url'])
  2128. url = urlparse.urlparse(location)
  2129. conn = httplib.HTTPConnection(url.netloc)
  2130. class WPSERHTTPServer(SocketServer.StreamRequestHandler):
  2131. def handle(self):
  2132. data = self.rfile.readline().strip()
  2133. logger.debug(data)
  2134. self.wfile.write(gen_wps_event())
  2135. server = MyTCPServer(("127.0.0.1", 12345), WPSERHTTPServer)
  2136. server.timeout = 1
  2137. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2138. "NT": "upnp:event",
  2139. "timeout": "Second-1234" }
  2140. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2141. resp = conn.getresponse()
  2142. if resp.status != 200:
  2143. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2144. sid = resp.getheader("sid")
  2145. logger.debug("Subscription SID " + sid)
  2146. msg = '''<?xml version="1.0"?>
  2147. <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  2148. <s:Body>
  2149. <u:SetSelectedRegistrar xmlns:u="urn:schemas-wifialliance-org:service:WFAWLANConfig:1">
  2150. <NewMessage>EEoAARAQQQABARASAAIAABBTAAIxSBBJAA4ANyoAASABBv///////xBIABA2LbR7pTpRkYj7
  2151. VFi5hrLk
  2152. </NewMessage>
  2153. </u:SetSelectedRegistrar>
  2154. </s:Body>
  2155. </s:Envelope>'''
  2156. headers = { "Content-type": 'text/xml; charset="utf-8"' }
  2157. headers["SOAPAction"] = '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#%s"' % "SetSelectedRegistrar"
  2158. conn.request("POST", ctrlurl.path, msg, headers)
  2159. resp = conn.getresponse()
  2160. if resp.status != 200:
  2161. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2162. server.handle_request()
  2163. logger.info("Start WPS_PBC and wait for PBC walk time expiration")
  2164. if "OK" not in dev[0].request("WPS_PBC"):
  2165. raise Exception("WPS_PBC failed")
  2166. start = os.times()[4]
  2167. server.handle_request()
  2168. dev[1].request("BSS_FLUSH 0")
  2169. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True,
  2170. only_new=True)
  2171. bss = dev[1].get_bss(apdev[0]['bssid'])
  2172. logger.debug("BSS: " + str(bss))
  2173. if '[WPS-AUTH]' not in bss['flags']:
  2174. raise Exception("WPS not indicated authorized")
  2175. server.handle_request()
  2176. wps_timeout_seen = False
  2177. while True:
  2178. hapd.dump_monitor()
  2179. dev[1].dump_monitor()
  2180. if not wps_timeout_seen:
  2181. ev = dev[0].wait_event(["WPS-TIMEOUT"], timeout=0)
  2182. if ev is not None:
  2183. logger.info("PBC timeout seen")
  2184. wps_timeout_seen = True
  2185. else:
  2186. dev[0].dump_monitor()
  2187. now = os.times()[4]
  2188. if now - start > 130:
  2189. raise Exception("Selected registration information not removed")
  2190. dev[1].request("BSS_FLUSH 0")
  2191. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True,
  2192. only_new=True)
  2193. bss = dev[1].get_bss(apdev[0]['bssid'])
  2194. logger.debug("BSS: " + str(bss))
  2195. if '[WPS-AUTH]' not in bss['flags']:
  2196. break
  2197. server.handle_request()
  2198. server.server_close()
  2199. if wps_timeout_seen:
  2200. return
  2201. now = os.times()[4]
  2202. if now < start + 150:
  2203. dur = start + 150 - now
  2204. else:
  2205. dur = 1
  2206. logger.info("Continue waiting for PBC timeout (%d sec)" % dur)
  2207. ev = dev[0].wait_event(["WPS-TIMEOUT"], timeout=dur)
  2208. if ev is None:
  2209. raise Exception("WPS-TIMEOUT not reported")
  2210. def add_ssdp_ap(ap, ap_uuid):
  2211. ssid = "wps-ssdp"
  2212. ap_pin = "12345670"
  2213. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  2214. "wpa_passphrase": "12345678", "wpa": "2",
  2215. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  2216. "device_name": "Wireless AP", "manufacturer": "Company",
  2217. "model_name": "WAP", "model_number": "123",
  2218. "serial_number": "12345", "device_type": "6-0050F204-1",
  2219. "os_version": "01020300",
  2220. "config_methods": "label push_button",
  2221. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo",
  2222. "friendly_name": "WPS Access Point",
  2223. "manufacturer_url": "http://www.example.com/",
  2224. "model_description": "Wireless Access Point",
  2225. "model_url": "http://www.example.com/model/",
  2226. "upc": "123456789012" }
  2227. return hostapd.add_ap(ap, params)
  2228. def ssdp_send(msg, no_recv=False):
  2229. socket.setdefaulttimeout(1)
  2230. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  2231. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  2232. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  2233. sock.bind(("127.0.0.1", 0))
  2234. sock.sendto(msg, ("239.255.255.250", 1900))
  2235. if no_recv:
  2236. return None
  2237. return sock.recv(1000)
  2238. def ssdp_send_msearch(st, no_recv=False):
  2239. msg = '\r\n'.join([
  2240. 'M-SEARCH * HTTP/1.1',
  2241. 'HOST: 239.255.255.250:1900',
  2242. 'MX: 1',
  2243. 'MAN: "ssdp:discover"',
  2244. 'ST: ' + st,
  2245. '', ''])
  2246. return ssdp_send(msg, no_recv=no_recv)
  2247. def test_ap_wps_ssdp_msearch(dev, apdev):
  2248. """WPS AP and SSDP M-SEARCH messages"""
  2249. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2250. add_ssdp_ap(apdev[0], ap_uuid)
  2251. msg = '\r\n'.join([
  2252. 'M-SEARCH * HTTP/1.1',
  2253. 'Host: 239.255.255.250:1900',
  2254. 'Mx: 1',
  2255. 'Man: "ssdp:discover"',
  2256. 'St: urn:schemas-wifialliance-org:device:WFADevice:1',
  2257. '', ''])
  2258. ssdp_send(msg)
  2259. msg = '\r\n'.join([
  2260. 'M-SEARCH * HTTP/1.1',
  2261. 'host:\t239.255.255.250:1900\t\t\t\t \t\t',
  2262. 'mx: \t1\t\t ',
  2263. 'man: \t \t "ssdp:discover" ',
  2264. 'st: urn:schemas-wifialliance-org:device:WFADevice:1\t\t',
  2265. '', ''])
  2266. ssdp_send(msg)
  2267. ssdp_send_msearch("ssdp:all")
  2268. ssdp_send_msearch("upnp:rootdevice")
  2269. ssdp_send_msearch("uuid:" + ap_uuid)
  2270. ssdp_send_msearch("urn:schemas-wifialliance-org:service:WFAWLANConfig:1")
  2271. ssdp_send_msearch("urn:schemas-wifialliance-org:device:WFADevice:1")
  2272. msg = '\r\n'.join([
  2273. 'M-SEARCH * HTTP/1.1',
  2274. 'HOST:\t239.255.255.250:1900',
  2275. 'MAN: "ssdp:discover"',
  2276. 'MX: 130',
  2277. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2278. '', ''])
  2279. ssdp_send(msg, no_recv=True)
  2280. def test_ap_wps_ssdp_invalid_msearch(dev, apdev):
  2281. """WPS AP and invalid SSDP M-SEARCH messages"""
  2282. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2283. add_ssdp_ap(apdev[0], ap_uuid)
  2284. socket.setdefaulttimeout(1)
  2285. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  2286. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  2287. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  2288. sock.bind(("127.0.0.1", 0))
  2289. logger.debug("Missing MX")
  2290. msg = '\r\n'.join([
  2291. 'M-SEARCH * HTTP/1.1',
  2292. 'HOST: 239.255.255.250:1900',
  2293. 'MAN: "ssdp:discover"',
  2294. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2295. '', ''])
  2296. sock.sendto(msg, ("239.255.255.250", 1900))
  2297. logger.debug("Negative MX")
  2298. msg = '\r\n'.join([
  2299. 'M-SEARCH * HTTP/1.1',
  2300. 'HOST: 239.255.255.250:1900',
  2301. 'MX: -1',
  2302. 'MAN: "ssdp:discover"',
  2303. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2304. '', ''])
  2305. sock.sendto(msg, ("239.255.255.250", 1900))
  2306. logger.debug("Invalid MX")
  2307. msg = '\r\n'.join([
  2308. 'M-SEARCH * HTTP/1.1',
  2309. 'HOST: 239.255.255.250:1900',
  2310. 'MX; 1',
  2311. 'MAN: "ssdp:discover"',
  2312. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2313. '', ''])
  2314. sock.sendto(msg, ("239.255.255.250", 1900))
  2315. logger.debug("Missing MAN")
  2316. msg = '\r\n'.join([
  2317. 'M-SEARCH * HTTP/1.1',
  2318. 'HOST: 239.255.255.250:1900',
  2319. 'MX: 1',
  2320. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2321. '', ''])
  2322. sock.sendto(msg, ("239.255.255.250", 1900))
  2323. logger.debug("Invalid MAN")
  2324. msg = '\r\n'.join([
  2325. 'M-SEARCH * HTTP/1.1',
  2326. 'HOST: 239.255.255.250:1900',
  2327. 'MX: 1',
  2328. 'MAN: foo',
  2329. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2330. '', ''])
  2331. sock.sendto(msg, ("239.255.255.250", 1900))
  2332. msg = '\r\n'.join([
  2333. 'M-SEARCH * HTTP/1.1',
  2334. 'HOST: 239.255.255.250:1900',
  2335. 'MX: 1',
  2336. 'MAN; "ssdp:discover"',
  2337. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2338. '', ''])
  2339. sock.sendto(msg, ("239.255.255.250", 1900))
  2340. logger.debug("Missing HOST")
  2341. msg = '\r\n'.join([
  2342. 'M-SEARCH * HTTP/1.1',
  2343. 'MAN: "ssdp:discover"',
  2344. 'MX: 1',
  2345. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2346. '', ''])
  2347. sock.sendto(msg, ("239.255.255.250", 1900))
  2348. logger.debug("Missing ST")
  2349. msg = '\r\n'.join([
  2350. 'M-SEARCH * HTTP/1.1',
  2351. 'HOST: 239.255.255.250:1900',
  2352. 'MAN: "ssdp:discover"',
  2353. 'MX: 1',
  2354. '', ''])
  2355. sock.sendto(msg, ("239.255.255.250", 1900))
  2356. logger.debug("Mismatching ST")
  2357. msg = '\r\n'.join([
  2358. 'M-SEARCH * HTTP/1.1',
  2359. 'HOST: 239.255.255.250:1900',
  2360. 'MAN: "ssdp:discover"',
  2361. 'MX: 1',
  2362. 'ST: uuid:16d5f8a9-4ee4-4f5e-81f9-cc6e2f47f42d',
  2363. '', ''])
  2364. sock.sendto(msg, ("239.255.255.250", 1900))
  2365. msg = '\r\n'.join([
  2366. 'M-SEARCH * HTTP/1.1',
  2367. 'HOST: 239.255.255.250:1900',
  2368. 'MAN: "ssdp:discover"',
  2369. 'MX: 1',
  2370. 'ST: foo:bar',
  2371. '', ''])
  2372. sock.sendto(msg, ("239.255.255.250", 1900))
  2373. msg = '\r\n'.join([
  2374. 'M-SEARCH * HTTP/1.1',
  2375. 'HOST: 239.255.255.250:1900',
  2376. 'MAN: "ssdp:discover"',
  2377. 'MX: 1',
  2378. 'ST: foobar',
  2379. '', ''])
  2380. sock.sendto(msg, ("239.255.255.250", 1900))
  2381. logger.debug("Invalid ST")
  2382. msg = '\r\n'.join([
  2383. 'M-SEARCH * HTTP/1.1',
  2384. 'HOST: 239.255.255.250:1900',
  2385. 'MAN: "ssdp:discover"',
  2386. 'MX: 1',
  2387. 'ST; urn:schemas-wifialliance-org:device:WFADevice:1',
  2388. '', ''])
  2389. sock.sendto(msg, ("239.255.255.250", 1900))
  2390. logger.debug("Invalid M-SEARCH")
  2391. msg = '\r\n'.join([
  2392. 'M+SEARCH * HTTP/1.1',
  2393. 'HOST: 239.255.255.250:1900',
  2394. 'MAN: "ssdp:discover"',
  2395. 'MX: 1',
  2396. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2397. '', ''])
  2398. sock.sendto(msg, ("239.255.255.250", 1900))
  2399. msg = '\r\n'.join([
  2400. 'M-SEARCH-* HTTP/1.1',
  2401. 'HOST: 239.255.255.250:1900',
  2402. 'MAN: "ssdp:discover"',
  2403. 'MX: 1',
  2404. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2405. '', ''])
  2406. sock.sendto(msg, ("239.255.255.250", 1900))
  2407. logger.debug("Invalid message format")
  2408. sock.sendto("NOTIFY * HTTP/1.1", ("239.255.255.250", 1900))
  2409. msg = '\r'.join([
  2410. 'M-SEARCH * HTTP/1.1',
  2411. 'HOST: 239.255.255.250:1900',
  2412. 'MAN: "ssdp:discover"',
  2413. 'MX: 1',
  2414. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2415. '', ''])
  2416. sock.sendto(msg, ("239.255.255.250", 1900))
  2417. try:
  2418. r = sock.recv(1000)
  2419. raise Exception("Unexpected M-SEARCH response: " + r)
  2420. except socket.timeout:
  2421. pass
  2422. logger.debug("Valid M-SEARCH")
  2423. msg = '\r\n'.join([
  2424. 'M-SEARCH * HTTP/1.1',
  2425. 'HOST: 239.255.255.250:1900',
  2426. 'MAN: "ssdp:discover"',
  2427. 'MX: 1',
  2428. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2429. '', ''])
  2430. sock.sendto(msg, ("239.255.255.250", 1900))
  2431. try:
  2432. r = sock.recv(1000)
  2433. pass
  2434. except socket.timeout:
  2435. raise Exception("No SSDP response")
  2436. def test_ap_wps_ssdp_burst(dev, apdev):
  2437. """WPS AP and SSDP burst"""
  2438. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2439. add_ssdp_ap(apdev[0], ap_uuid)
  2440. msg = '\r\n'.join([
  2441. 'M-SEARCH * HTTP/1.1',
  2442. 'HOST: 239.255.255.250:1900',
  2443. 'MAN: "ssdp:discover"',
  2444. 'MX: 1',
  2445. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  2446. '', ''])
  2447. socket.setdefaulttimeout(1)
  2448. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  2449. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  2450. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  2451. sock.bind(("127.0.0.1", 0))
  2452. for i in range(0, 25):
  2453. sock.sendto(msg, ("239.255.255.250", 1900))
  2454. resp = 0
  2455. while True:
  2456. try:
  2457. r = sock.recv(1000)
  2458. if not r.startswith("HTTP/1.1 200 OK\r\n"):
  2459. raise Exception("Unexpected message: " + r)
  2460. resp += 1
  2461. except socket.timeout:
  2462. break
  2463. if resp < 20:
  2464. raise Exception("Too few SSDP responses")
  2465. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  2466. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  2467. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  2468. sock.bind(("127.0.0.1", 0))
  2469. for i in range(0, 25):
  2470. sock.sendto(msg, ("239.255.255.250", 1900))
  2471. while True:
  2472. try:
  2473. r = sock.recv(1000)
  2474. if ap_uuid in r:
  2475. break
  2476. except socket.timeout:
  2477. raise Exception("No SSDP response")
  2478. def ssdp_get_location(uuid):
  2479. res = ssdp_send_msearch("uuid:" + uuid)
  2480. location = None
  2481. for l in res.splitlines():
  2482. if l.lower().startswith("location:"):
  2483. location = l.split(':', 1)[1].strip()
  2484. break
  2485. if location is None:
  2486. raise Exception("No UPnP location found")
  2487. return location
  2488. def upnp_get_urls(location):
  2489. conn = urllib.urlopen(location, proxies={})
  2490. tree = ET.parse(conn)
  2491. root = tree.getroot()
  2492. urn = '{urn:schemas-upnp-org:device-1-0}'
  2493. service = root.find("./" + urn + "device/" + urn + "serviceList/" + urn + "service")
  2494. res = {}
  2495. res['scpd_url'] = urlparse.urljoin(location, service.find(urn + 'SCPDURL').text)
  2496. res['control_url'] = urlparse.urljoin(location, service.find(urn + 'controlURL').text)
  2497. res['event_sub_url'] = urlparse.urljoin(location, service.find(urn + 'eventSubURL').text)
  2498. return res
  2499. def upnp_soap_action(conn, path, action, include_soap_action=True,
  2500. soap_action_override=None, newmsg=None, neweventtype=None,
  2501. neweventmac=None):
  2502. soapns = 'http://schemas.xmlsoap.org/soap/envelope/'
  2503. wpsns = 'urn:schemas-wifialliance-org:service:WFAWLANConfig:1'
  2504. ET.register_namespace('soapenv', soapns)
  2505. ET.register_namespace('wfa', wpsns)
  2506. attrib = {}
  2507. attrib['{%s}encodingStyle' % soapns] = 'http://schemas.xmlsoap.org/soap/encoding/'
  2508. root = ET.Element("{%s}Envelope" % soapns, attrib=attrib)
  2509. body = ET.SubElement(root, "{%s}Body" % soapns)
  2510. act = ET.SubElement(body, "{%s}%s" % (wpsns, action))
  2511. if newmsg:
  2512. msg = ET.SubElement(act, "NewMessage")
  2513. msg.text = base64.b64encode(newmsg)
  2514. if neweventtype:
  2515. msg = ET.SubElement(act, "NewWLANEventType")
  2516. msg.text = neweventtype
  2517. if neweventmac:
  2518. msg = ET.SubElement(act, "NewWLANEventMAC")
  2519. msg.text = neweventmac
  2520. tree = ET.ElementTree(root)
  2521. soap = StringIO.StringIO()
  2522. tree.write(soap, xml_declaration=True, encoding='utf-8')
  2523. headers = { "Content-type": 'text/xml; charset="utf-8"' }
  2524. if include_soap_action:
  2525. headers["SOAPAction"] = '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#%s"' % action
  2526. elif soap_action_override:
  2527. headers["SOAPAction"] = soap_action_override
  2528. conn.request("POST", path, soap.getvalue(), headers)
  2529. return conn.getresponse()
  2530. def test_ap_wps_upnp(dev, apdev):
  2531. """WPS AP and UPnP operations"""
  2532. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2533. add_ssdp_ap(apdev[0], ap_uuid)
  2534. location = ssdp_get_location(ap_uuid)
  2535. urls = upnp_get_urls(location)
  2536. conn = urllib.urlopen(urls['scpd_url'], proxies={})
  2537. scpd = conn.read()
  2538. conn = urllib.urlopen(urlparse.urljoin(location, "unknown.html"),
  2539. proxies={})
  2540. if conn.getcode() != 404:
  2541. raise Exception("Unexpected HTTP response to GET unknown URL")
  2542. url = urlparse.urlparse(location)
  2543. conn = httplib.HTTPConnection(url.netloc)
  2544. #conn.set_debuglevel(1)
  2545. headers = { "Content-type": 'text/xml; charset="utf-8"',
  2546. "SOAPAction": '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#GetDeviceInfo"' }
  2547. conn.request("POST", "hello", "\r\n\r\n", headers)
  2548. resp = conn.getresponse()
  2549. if resp.status != 404:
  2550. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2551. conn.request("UNKNOWN", "hello", "\r\n\r\n", headers)
  2552. resp = conn.getresponse()
  2553. if resp.status != 501:
  2554. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2555. headers = { "Content-type": 'text/xml; charset="utf-8"',
  2556. "SOAPAction": '"urn:some-unknown-action#GetDeviceInfo"' }
  2557. ctrlurl = urlparse.urlparse(urls['control_url'])
  2558. conn.request("POST", ctrlurl.path, "\r\n\r\n", headers)
  2559. resp = conn.getresponse()
  2560. if resp.status != 401:
  2561. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2562. logger.debug("GetDeviceInfo without SOAPAction header")
  2563. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo",
  2564. include_soap_action=False)
  2565. if resp.status != 401:
  2566. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2567. logger.debug("GetDeviceInfo with invalid SOAPAction header")
  2568. for act in [ "foo",
  2569. "urn:schemas-wifialliance-org:service:WFAWLANConfig:1#GetDeviceInfo",
  2570. '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1"',
  2571. '"urn:schemas-wifialliance-org:service:WFAWLANConfig:123#GetDevice']:
  2572. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo",
  2573. include_soap_action=False,
  2574. soap_action_override=act)
  2575. if resp.status != 401:
  2576. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2577. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  2578. if resp.status != 200:
  2579. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2580. dev = resp.read()
  2581. if "NewDeviceInfo" not in dev:
  2582. raise Exception("Unexpected GetDeviceInfo response")
  2583. logger.debug("PutMessage without required parameters")
  2584. resp = upnp_soap_action(conn, ctrlurl.path, "PutMessage")
  2585. if resp.status != 600:
  2586. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2587. logger.debug("PutWLANResponse without required parameters")
  2588. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse")
  2589. if resp.status != 600:
  2590. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2591. logger.debug("SetSelectedRegistrar from unregistered ER")
  2592. resp = upnp_soap_action(conn, ctrlurl.path, "SetSelectedRegistrar")
  2593. if resp.status != 501:
  2594. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2595. logger.debug("Unknown action")
  2596. resp = upnp_soap_action(conn, ctrlurl.path, "Unknown")
  2597. if resp.status != 401:
  2598. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2599. def test_ap_wps_upnp_subscribe(dev, apdev):
  2600. """WPS AP and UPnP event subscription"""
  2601. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2602. hapd = add_ssdp_ap(apdev[0], ap_uuid)
  2603. location = ssdp_get_location(ap_uuid)
  2604. urls = upnp_get_urls(location)
  2605. eventurl = urlparse.urlparse(urls['event_sub_url'])
  2606. url = urlparse.urlparse(location)
  2607. conn = httplib.HTTPConnection(url.netloc)
  2608. #conn.set_debuglevel(1)
  2609. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2610. "timeout": "Second-1234" }
  2611. conn.request("SUBSCRIBE", "hello", "\r\n\r\n", headers)
  2612. resp = conn.getresponse()
  2613. if resp.status != 412:
  2614. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2615. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2616. resp = conn.getresponse()
  2617. if resp.status != 412:
  2618. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2619. headers = { "NT": "upnp:event",
  2620. "timeout": "Second-1234" }
  2621. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2622. resp = conn.getresponse()
  2623. if resp.status != 412:
  2624. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2625. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2626. "NT": "upnp:foobar",
  2627. "timeout": "Second-1234" }
  2628. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2629. resp = conn.getresponse()
  2630. if resp.status != 400:
  2631. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2632. logger.debug("Valid subscription")
  2633. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2634. "NT": "upnp:event",
  2635. "timeout": "Second-1234" }
  2636. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2637. resp = conn.getresponse()
  2638. if resp.status != 200:
  2639. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2640. sid = resp.getheader("sid")
  2641. logger.debug("Subscription SID " + sid)
  2642. logger.debug("Invalid re-subscription")
  2643. headers = { "NT": "upnp:event",
  2644. "sid": "123456734567854",
  2645. "timeout": "Second-1234" }
  2646. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2647. resp = conn.getresponse()
  2648. if resp.status != 400:
  2649. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2650. logger.debug("Invalid re-subscription")
  2651. headers = { "NT": "upnp:event",
  2652. "sid": "uuid:123456734567854",
  2653. "timeout": "Second-1234" }
  2654. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2655. resp = conn.getresponse()
  2656. if resp.status != 400:
  2657. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2658. logger.debug("Invalid re-subscription")
  2659. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2660. "NT": "upnp:event",
  2661. "sid": sid,
  2662. "timeout": "Second-1234" }
  2663. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2664. resp = conn.getresponse()
  2665. if resp.status != 400:
  2666. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2667. logger.debug("SID mismatch in re-subscription")
  2668. headers = { "NT": "upnp:event",
  2669. "sid": "uuid:4c2bca79-1ff4-4e43-85d4-952a2b8a51fb",
  2670. "timeout": "Second-1234" }
  2671. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2672. resp = conn.getresponse()
  2673. if resp.status != 412:
  2674. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2675. logger.debug("Valid re-subscription")
  2676. headers = { "NT": "upnp:event",
  2677. "sid": sid,
  2678. "timeout": "Second-1234" }
  2679. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2680. resp = conn.getresponse()
  2681. if resp.status != 200:
  2682. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2683. sid2 = resp.getheader("sid")
  2684. logger.debug("Subscription SID " + sid2)
  2685. if sid != sid2:
  2686. raise Exception("Unexpected SID change")
  2687. logger.debug("Valid re-subscription")
  2688. headers = { "NT": "upnp:event",
  2689. "sid": "uuid: \t \t" + sid.split(':')[1],
  2690. "timeout": "Second-1234" }
  2691. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2692. resp = conn.getresponse()
  2693. if resp.status != 200:
  2694. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2695. logger.debug("Invalid unsubscription")
  2696. headers = { "sid": sid }
  2697. conn.request("UNSUBSCRIBE", "/hello", "\r\n\r\n", headers)
  2698. resp = conn.getresponse()
  2699. if resp.status != 412:
  2700. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2701. headers = { "foo": "bar" }
  2702. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2703. resp = conn.getresponse()
  2704. if resp.status != 412:
  2705. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2706. logger.debug("Valid unsubscription")
  2707. headers = { "sid": sid }
  2708. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2709. resp = conn.getresponse()
  2710. if resp.status != 200:
  2711. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2712. logger.debug("Unsubscription for not existing SID")
  2713. headers = { "sid": sid }
  2714. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2715. resp = conn.getresponse()
  2716. if resp.status != 412:
  2717. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2718. logger.debug("Invalid unsubscription")
  2719. headers = { "sid": " \t \tfoo" }
  2720. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2721. resp = conn.getresponse()
  2722. if resp.status != 400:
  2723. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2724. logger.debug("Invalid unsubscription")
  2725. headers = { "sid": "uuid:\t \tfoo" }
  2726. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2727. resp = conn.getresponse()
  2728. if resp.status != 400:
  2729. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2730. logger.debug("Invalid unsubscription")
  2731. headers = { "NT": "upnp:event",
  2732. "sid": sid }
  2733. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2734. resp = conn.getresponse()
  2735. if resp.status != 400:
  2736. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2737. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2738. "sid": sid }
  2739. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2740. resp = conn.getresponse()
  2741. if resp.status != 400:
  2742. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2743. logger.debug("Valid subscription with multiple callbacks")
  2744. 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>',
  2745. "NT": "upnp:event",
  2746. "timeout": "Second-1234" }
  2747. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2748. resp = conn.getresponse()
  2749. if resp.status != 200:
  2750. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2751. sid = resp.getheader("sid")
  2752. logger.debug("Subscription SID " + sid)
  2753. # Force subscription to be deleted due to errors
  2754. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  2755. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  2756. with alloc_fail(hapd, 1, "event_build_message"):
  2757. for i in range(10):
  2758. dev[1].dump_monitor()
  2759. dev[2].dump_monitor()
  2760. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2761. dev[2].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2762. dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2763. dev[1].request("WPS_CANCEL")
  2764. dev[2].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2765. dev[2].request("WPS_CANCEL")
  2766. if i % 4 == 1:
  2767. time.sleep(1)
  2768. else:
  2769. time.sleep(0.1)
  2770. time.sleep(0.2)
  2771. headers = { "sid": sid }
  2772. conn.request("UNSUBSCRIBE", eventurl.path, "", headers)
  2773. resp = conn.getresponse()
  2774. if resp.status != 200 and resp.status != 412:
  2775. raise Exception("Unexpected HTTP response for UNSUBSCRIBE: %d" % resp.status)
  2776. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2777. "NT": "upnp:event",
  2778. "timeout": "Second-1234" }
  2779. with alloc_fail(hapd, 1, "http_client_addr;event_send_start"):
  2780. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2781. resp = conn.getresponse()
  2782. if resp.status != 200:
  2783. raise Exception("Unexpected HTTP response for SUBSCRIBE: %d" % resp.status)
  2784. sid = resp.getheader("sid")
  2785. logger.debug("Subscription SID " + sid)
  2786. headers = { "sid": sid }
  2787. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2788. resp = conn.getresponse()
  2789. if resp.status != 200:
  2790. raise Exception("Unexpected HTTP response for UNSUBSCRIBE: %d" % resp.status)
  2791. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2792. "NT": "upnp:event",
  2793. "timeout": "Second-1234" }
  2794. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2795. resp = conn.getresponse()
  2796. if resp.status != 200:
  2797. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2798. sid = resp.getheader("sid")
  2799. logger.debug("Subscription SID " + sid)
  2800. with alloc_fail(hapd, 1, "=event_add"):
  2801. for i in range(2):
  2802. dev[1].dump_monitor()
  2803. dev[2].dump_monitor()
  2804. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2805. dev[2].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2806. dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2807. dev[1].request("WPS_CANCEL")
  2808. dev[2].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2809. dev[2].request("WPS_CANCEL")
  2810. if i == 0:
  2811. time.sleep(1)
  2812. else:
  2813. time.sleep(0.1)
  2814. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2815. resp = conn.getresponse()
  2816. if resp.status != 200:
  2817. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2818. with alloc_fail(hapd, 1, "wpabuf_dup;event_add"):
  2819. dev[1].dump_monitor()
  2820. dev[2].dump_monitor()
  2821. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2822. dev[2].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2823. dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2824. dev[1].request("WPS_CANCEL")
  2825. dev[2].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2826. dev[2].request("WPS_CANCEL")
  2827. time.sleep(0.1)
  2828. with fail_test(hapd, 1, "os_get_random;uuid_make;subscription_start"):
  2829. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2830. resp = conn.getresponse()
  2831. if resp.status != 500:
  2832. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2833. with alloc_fail(hapd, 1, "=subscription_start"):
  2834. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2835. resp = conn.getresponse()
  2836. if resp.status != 500:
  2837. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2838. headers = { "callback": '',
  2839. "NT": "upnp:event",
  2840. "timeout": "Second-1234" }
  2841. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2842. resp = conn.getresponse()
  2843. if resp.status != 500:
  2844. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2845. headers = { "callback": ' <',
  2846. "NT": "upnp:event",
  2847. "timeout": "Second-1234" }
  2848. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2849. resp = conn.getresponse()
  2850. if resp.status != 500:
  2851. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2852. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2853. "NT": "upnp:event",
  2854. "timeout": "Second-1234" }
  2855. with alloc_fail(hapd, 1, "wpabuf_alloc;subscription_first_event"):
  2856. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2857. resp = conn.getresponse()
  2858. if resp.status != 500:
  2859. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2860. with alloc_fail(hapd, 1, "event_add;subscription_first_event"):
  2861. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2862. resp = conn.getresponse()
  2863. if resp.status != 500:
  2864. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2865. with alloc_fail(hapd, 1, "subscr_addr_add_url"):
  2866. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2867. resp = conn.getresponse()
  2868. if resp.status != 500:
  2869. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2870. with alloc_fail(hapd, 2, "subscr_addr_add_url"):
  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. for i in range(6):
  2876. headers = { "callback": '<http://127.0.0.1:%d/event>' % (12345 + i),
  2877. "NT": "upnp:event",
  2878. "timeout": "Second-1234" }
  2879. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2880. resp = conn.getresponse()
  2881. if resp.status != 200:
  2882. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2883. with alloc_fail(hapd, 1, "=upnp_wps_device_send_wlan_event"):
  2884. dev[1].dump_monitor()
  2885. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2886. dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2887. dev[1].request("WPS_CANCEL")
  2888. time.sleep(0.1)
  2889. with alloc_fail(hapd, 1, "wpabuf_alloc;upnp_wps_device_send_event"):
  2890. dev[1].dump_monitor()
  2891. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2892. dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2893. dev[1].request("WPS_CANCEL")
  2894. time.sleep(0.1)
  2895. with alloc_fail(hapd, 1, "base64_encode;upnp_wps_device_send_wlan_event"):
  2896. dev[1].dump_monitor()
  2897. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2898. dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2899. dev[1].request("WPS_CANCEL")
  2900. time.sleep(0.1)
  2901. hapd.disable()
  2902. with alloc_fail(hapd, 1, "get_netif_info"):
  2903. if "FAIL" not in hapd.request("ENABLE"):
  2904. raise Exception("ENABLE succeeded during OOM")
  2905. def test_ap_wps_upnp_subscribe_events(dev, apdev):
  2906. """WPS AP and UPnP event subscription and many events"""
  2907. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2908. hapd = add_ssdp_ap(apdev[0], ap_uuid)
  2909. location = ssdp_get_location(ap_uuid)
  2910. urls = upnp_get_urls(location)
  2911. eventurl = urlparse.urlparse(urls['event_sub_url'])
  2912. class WPSERHTTPServer(SocketServer.StreamRequestHandler):
  2913. def handle(self):
  2914. data = self.rfile.readline().strip()
  2915. logger.debug(data)
  2916. self.wfile.write(gen_wps_event())
  2917. server = MyTCPServer(("127.0.0.1", 12345), WPSERHTTPServer)
  2918. server.timeout = 1
  2919. url = urlparse.urlparse(location)
  2920. conn = httplib.HTTPConnection(url.netloc)
  2921. headers = { "callback": '<http://127.0.0.1:12345/event>',
  2922. "NT": "upnp:event",
  2923. "timeout": "Second-1234" }
  2924. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  2925. resp = conn.getresponse()
  2926. if resp.status != 200:
  2927. raise Exception("Unexpected HTTP response: %d" % resp.status)
  2928. sid = resp.getheader("sid")
  2929. logger.debug("Subscription SID " + sid)
  2930. # Fetch the first event message
  2931. server.handle_request()
  2932. # Force subscription event queue to reach the maximum length by generating
  2933. # new proxied events without the ER fetching any of the pending events.
  2934. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  2935. dev[2].scan_for_bss(apdev[0]['bssid'], freq=2412)
  2936. for i in range(16):
  2937. dev[1].dump_monitor()
  2938. dev[2].dump_monitor()
  2939. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2940. dev[2].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2941. dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2942. dev[1].request("WPS_CANCEL")
  2943. dev[2].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  2944. dev[2].request("WPS_CANCEL")
  2945. if i % 4 == 1:
  2946. time.sleep(1)
  2947. else:
  2948. time.sleep(0.1)
  2949. hapd.request("WPS_PIN any 12345670")
  2950. dev[1].dump_monitor()
  2951. dev[1].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  2952. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=10)
  2953. if ev is None:
  2954. raise Exception("WPS success not reported")
  2955. # Close the WPS ER HTTP server without fetching all the pending events.
  2956. # This tests hostapd code path that clears subscription and the remaining
  2957. # event queue when the interface is deinitialized.
  2958. server.handle_request()
  2959. server.server_close()
  2960. dev[1].wait_connected()
  2961. def test_ap_wps_upnp_http_proto(dev, apdev):
  2962. """WPS AP and UPnP/HTTP protocol testing"""
  2963. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  2964. add_ssdp_ap(apdev[0], ap_uuid)
  2965. location = ssdp_get_location(ap_uuid)
  2966. url = urlparse.urlparse(location)
  2967. conn = httplib.HTTPConnection(url.netloc, timeout=0.2)
  2968. #conn.set_debuglevel(1)
  2969. conn.request("HEAD", "hello")
  2970. resp = conn.getresponse()
  2971. if resp.status != 501:
  2972. raise Exception("Unexpected response to HEAD: " + str(resp.status))
  2973. conn.close()
  2974. for cmd in [ "PUT", "DELETE", "TRACE", "CONNECT", "M-SEARCH", "M-POST" ]:
  2975. try:
  2976. conn.request(cmd, "hello")
  2977. resp = conn.getresponse()
  2978. except Exception, e:
  2979. pass
  2980. conn.close()
  2981. headers = { "Content-Length": 'abc' }
  2982. conn.request("HEAD", "hello", "\r\n\r\n", headers)
  2983. try:
  2984. resp = conn.getresponse()
  2985. except Exception, e:
  2986. pass
  2987. conn.close()
  2988. headers = { "Content-Length": '-10' }
  2989. conn.request("HEAD", "hello", "\r\n\r\n", headers)
  2990. try:
  2991. resp = conn.getresponse()
  2992. except Exception, e:
  2993. pass
  2994. conn.close()
  2995. headers = { "Content-Length": '10000000000000' }
  2996. conn.request("HEAD", "hello", "\r\n\r\nhello", headers)
  2997. try:
  2998. resp = conn.getresponse()
  2999. except Exception, e:
  3000. pass
  3001. conn.close()
  3002. headers = { "Transfer-Encoding": 'abc' }
  3003. conn.request("HEAD", "hello", "\r\n\r\n", headers)
  3004. resp = conn.getresponse()
  3005. if resp.status != 501:
  3006. raise Exception("Unexpected response to HEAD: " + str(resp.status))
  3007. conn.close()
  3008. headers = { "Transfer-Encoding": 'chunked' }
  3009. conn.request("HEAD", "hello", "\r\n\r\n", headers)
  3010. resp = conn.getresponse()
  3011. if resp.status != 501:
  3012. raise Exception("Unexpected response to HEAD: " + str(resp.status))
  3013. conn.close()
  3014. # Too long a header
  3015. conn.request("HEAD", 5000 * 'A')
  3016. try:
  3017. resp = conn.getresponse()
  3018. except Exception, e:
  3019. pass
  3020. conn.close()
  3021. # Long URL but within header length limits
  3022. conn.request("HEAD", 3000 * 'A')
  3023. resp = conn.getresponse()
  3024. if resp.status != 501:
  3025. raise Exception("Unexpected response to HEAD: " + str(resp.status))
  3026. conn.close()
  3027. headers = { "Content-Length": '20' }
  3028. conn.request("POST", "hello", 10 * 'A' + "\r\n\r\n", headers)
  3029. try:
  3030. resp = conn.getresponse()
  3031. except Exception, e:
  3032. pass
  3033. conn.close()
  3034. conn.request("POST", "hello", 5000 * 'A' + "\r\n\r\n")
  3035. resp = conn.getresponse()
  3036. if resp.status != 404:
  3037. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3038. conn.close()
  3039. conn.request("POST", "hello", 60000 * 'A' + "\r\n\r\n")
  3040. try:
  3041. resp = conn.getresponse()
  3042. except Exception, e:
  3043. pass
  3044. conn.close()
  3045. def test_ap_wps_upnp_http_proto_chunked(dev, apdev):
  3046. """WPS AP and UPnP/HTTP protocol testing for chunked encoding"""
  3047. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  3048. add_ssdp_ap(apdev[0], ap_uuid)
  3049. location = ssdp_get_location(ap_uuid)
  3050. url = urlparse.urlparse(location)
  3051. conn = httplib.HTTPConnection(url.netloc)
  3052. #conn.set_debuglevel(1)
  3053. headers = { "Transfer-Encoding": 'chunked' }
  3054. conn.request("POST", "hello",
  3055. "a\r\nabcdefghij\r\n" + "2\r\nkl\r\n" + "0\r\n\r\n",
  3056. headers)
  3057. resp = conn.getresponse()
  3058. if resp.status != 404:
  3059. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3060. conn.close()
  3061. conn.putrequest("POST", "hello")
  3062. conn.putheader('Transfer-Encoding', 'chunked')
  3063. conn.endheaders()
  3064. conn.send("a\r\nabcdefghij\r\n")
  3065. time.sleep(0.1)
  3066. conn.send("2\r\nkl\r\n")
  3067. conn.send("0\r\n\r\n")
  3068. resp = conn.getresponse()
  3069. if resp.status != 404:
  3070. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3071. conn.close()
  3072. conn.putrequest("POST", "hello")
  3073. conn.putheader('Transfer-Encoding', 'chunked')
  3074. conn.endheaders()
  3075. completed = False
  3076. try:
  3077. for i in range(20000):
  3078. conn.send("1\r\nZ\r\n")
  3079. conn.send("0\r\n\r\n")
  3080. resp = conn.getresponse()
  3081. completed = True
  3082. except Exception, e:
  3083. pass
  3084. conn.close()
  3085. if completed:
  3086. raise Exception("Too long chunked request did not result in connection reset")
  3087. headers = { "Transfer-Encoding": 'chunked' }
  3088. conn.request("POST", "hello", "80000000\r\na", headers)
  3089. try:
  3090. resp = conn.getresponse()
  3091. except Exception, e:
  3092. pass
  3093. conn.close()
  3094. conn.request("POST", "hello", "10000000\r\na", headers)
  3095. try:
  3096. resp = conn.getresponse()
  3097. except Exception, e:
  3098. pass
  3099. conn.close()
  3100. @remote_compatible
  3101. def test_ap_wps_disabled(dev, apdev):
  3102. """WPS operations while WPS is disabled"""
  3103. ssid = "test-wps-disabled"
  3104. hapd = hostapd.add_ap(apdev[0], { "ssid": ssid })
  3105. if "FAIL" not in hapd.request("WPS_PBC"):
  3106. raise Exception("WPS_PBC succeeded unexpectedly")
  3107. if "FAIL" not in hapd.request("WPS_CANCEL"):
  3108. raise Exception("WPS_CANCEL succeeded unexpectedly")
  3109. def test_ap_wps_mixed_cred(dev, apdev):
  3110. """WPS 2.0 STA merging mixed mode WPA/WPA2 credentials"""
  3111. ssid = "test-wps-wep"
  3112. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3113. "skip_cred_build": "1", "extra_cred": "wps-mixed-cred" }
  3114. hapd = hostapd.add_ap(apdev[0], params)
  3115. hapd.request("WPS_PBC")
  3116. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  3117. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  3118. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=30)
  3119. if ev is None:
  3120. raise Exception("WPS-SUCCESS event timed out")
  3121. nets = dev[0].list_networks()
  3122. if len(nets) != 1:
  3123. raise Exception("Unexpected number of network blocks")
  3124. id = nets[0]['id']
  3125. proto = dev[0].get_network(id, "proto")
  3126. if proto != "WPA RSN":
  3127. raise Exception("Unexpected merged proto field value: " + proto)
  3128. pairwise = dev[0].get_network(id, "pairwise")
  3129. if pairwise != "CCMP TKIP" and pairwise != "CCMP GCMP TKIP":
  3130. raise Exception("Unexpected merged pairwise field value: " + pairwise)
  3131. @remote_compatible
  3132. def test_ap_wps_while_connected(dev, apdev):
  3133. """WPS PBC provisioning while connected to another AP"""
  3134. ssid = "test-wps-conf"
  3135. hapd = hostapd.add_ap(apdev[0],
  3136. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3137. "wpa_passphrase": "12345678", "wpa": "2",
  3138. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3139. hostapd.add_ap(apdev[1], { "ssid": "open" })
  3140. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  3141. logger.info("WPS provisioning step")
  3142. hapd.request("WPS_PBC")
  3143. dev[0].dump_monitor()
  3144. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  3145. dev[0].wait_connected(timeout=30)
  3146. status = dev[0].get_status()
  3147. if status['bssid'] != apdev[0]['bssid']:
  3148. raise Exception("Unexpected BSSID")
  3149. @remote_compatible
  3150. def test_ap_wps_while_connected_no_autoconnect(dev, apdev):
  3151. """WPS PBC provisioning while connected to another AP and STA_AUTOCONNECT disabled"""
  3152. ssid = "test-wps-conf"
  3153. hapd = hostapd.add_ap(apdev[0],
  3154. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3155. "wpa_passphrase": "12345678", "wpa": "2",
  3156. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3157. hostapd.add_ap(apdev[1], { "ssid": "open" })
  3158. try:
  3159. dev[0].request("STA_AUTOCONNECT 0")
  3160. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  3161. logger.info("WPS provisioning step")
  3162. hapd.request("WPS_PBC")
  3163. dev[0].dump_monitor()
  3164. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  3165. dev[0].wait_connected(timeout=30)
  3166. status = dev[0].get_status()
  3167. if status['bssid'] != apdev[0]['bssid']:
  3168. raise Exception("Unexpected BSSID")
  3169. finally:
  3170. dev[0].request("STA_AUTOCONNECT 1")
  3171. @remote_compatible
  3172. def test_ap_wps_from_event(dev, apdev):
  3173. """WPS PBC event on AP to enable PBC"""
  3174. ssid = "test-wps-conf"
  3175. hapd = hostapd.add_ap(apdev[0],
  3176. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3177. "wpa_passphrase": "12345678", "wpa": "2",
  3178. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3179. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  3180. dev[0].dump_monitor()
  3181. hapd.dump_monitor()
  3182. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  3183. ev = hapd.wait_event(['WPS-ENROLLEE-SEEN'], timeout=15)
  3184. if ev is None:
  3185. raise Exception("No WPS-ENROLLEE-SEEN event on AP")
  3186. vals = ev.split(' ')
  3187. if vals[1] != dev[0].p2p_interface_addr():
  3188. raise Exception("Unexpected enrollee address: " + vals[1])
  3189. if vals[5] != '4':
  3190. raise Exception("Unexpected Device Password Id: " + vals[5])
  3191. hapd.request("WPS_PBC")
  3192. dev[0].wait_connected(timeout=30)
  3193. def test_ap_wps_ap_scan_2(dev, apdev):
  3194. """AP_SCAN 2 for WPS"""
  3195. ssid = "test-wps-conf"
  3196. hapd = hostapd.add_ap(apdev[0],
  3197. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3198. "wpa_passphrase": "12345678", "wpa": "2",
  3199. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3200. hapd.request("WPS_PBC")
  3201. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  3202. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  3203. wpas.dump_monitor()
  3204. if "OK" not in wpas.request("AP_SCAN 2"):
  3205. raise Exception("Failed to set AP_SCAN 2")
  3206. wpas.flush_scan_cache()
  3207. wpas.scan_for_bss(apdev[0]['bssid'], freq="2412")
  3208. wpas.dump_monitor()
  3209. wpas.request("WPS_PBC " + apdev[0]['bssid'])
  3210. ev = wpas.wait_event(["WPS-SUCCESS"], timeout=15)
  3211. if ev is None:
  3212. raise Exception("WPS-SUCCESS event timed out")
  3213. wpas.wait_connected(timeout=30)
  3214. wpas.dump_monitor()
  3215. wpas.request("DISCONNECT")
  3216. wpas.request("BSS_FLUSH 0")
  3217. wpas.dump_monitor()
  3218. wpas.request("REASSOCIATE")
  3219. wpas.wait_connected(timeout=30)
  3220. wpas.dump_monitor()
  3221. @remote_compatible
  3222. def test_ap_wps_eapol_workaround(dev, apdev):
  3223. """EAPOL workaround code path for 802.1X header length mismatch"""
  3224. ssid = "test-wps"
  3225. hapd = hostapd.add_ap(apdev[0],
  3226. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  3227. bssid = apdev[0]['bssid']
  3228. hapd.request("SET ext_eapol_frame_io 1")
  3229. dev[0].request("SET ext_eapol_frame_io 1")
  3230. hapd.request("WPS_PBC")
  3231. dev[0].request("WPS_PBC")
  3232. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  3233. if ev is None:
  3234. raise Exception("Timeout on EAPOL-TX from hostapd")
  3235. res = dev[0].request("EAPOL_RX " + bssid + " 020000040193000501FFFF")
  3236. if "OK" not in res:
  3237. raise Exception("EAPOL_RX to wpa_supplicant failed")
  3238. def test_ap_wps_iteration(dev, apdev):
  3239. """WPS PIN and iterate through APs without selected registrar"""
  3240. ssid = "test-wps-conf"
  3241. hapd = hostapd.add_ap(apdev[0],
  3242. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3243. "wpa_passphrase": "12345678", "wpa": "2",
  3244. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3245. ssid2 = "test-wps-conf2"
  3246. hapd2 = hostapd.add_ap(apdev[1],
  3247. { "ssid": ssid2, "eap_server": "1", "wps_state": "2",
  3248. "wpa_passphrase": "12345678", "wpa": "2",
  3249. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3250. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  3251. dev[0].scan_for_bss(apdev[1]['bssid'], freq="2412")
  3252. dev[0].dump_monitor()
  3253. pin = dev[0].request("WPS_PIN any")
  3254. # Wait for iteration through all WPS APs to happen before enabling any
  3255. # Registrar.
  3256. for i in range(2):
  3257. ev = dev[0].wait_event(["Associated with"], timeout=30)
  3258. if ev is None:
  3259. raise Exception("No association seen")
  3260. ev = dev[0].wait_event(["WPS-M2D"], timeout=10)
  3261. if ev is None:
  3262. raise Exception("No M2D from AP")
  3263. dev[0].wait_disconnected()
  3264. # Verify that each AP requested PIN
  3265. ev = hapd.wait_event(["WPS-PIN-NEEDED"], timeout=1)
  3266. if ev is None:
  3267. raise Exception("No WPS-PIN-NEEDED event from AP")
  3268. ev = hapd2.wait_event(["WPS-PIN-NEEDED"], timeout=1)
  3269. if ev is None:
  3270. raise Exception("No WPS-PIN-NEEDED event from AP2")
  3271. # Provide PIN to one of the APs and verify that connection gets formed
  3272. hapd.request("WPS_PIN any " + pin)
  3273. dev[0].wait_connected(timeout=30)
  3274. def test_ap_wps_iteration_error(dev, apdev):
  3275. """WPS AP iteration on no Selected Registrar and error case with an AP"""
  3276. ssid = "test-wps-conf-pin"
  3277. hapd = hostapd.add_ap(apdev[0],
  3278. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3279. "wpa_passphrase": "12345678", "wpa": "2",
  3280. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  3281. "wps_independent": "1" })
  3282. hapd.request("SET ext_eapol_frame_io 1")
  3283. bssid = apdev[0]['bssid']
  3284. pin = dev[0].wps_read_pin()
  3285. dev[0].request("WPS_PIN any " + pin)
  3286. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  3287. if ev is None:
  3288. raise Exception("No EAPOL-TX (EAP-Request/Identity) from hostapd")
  3289. dev[0].request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  3290. ev = hapd.wait_event(["EAPOL-TX"], timeout=15)
  3291. if ev is None:
  3292. raise Exception("No EAPOL-TX (EAP-WSC/Start) from hostapd")
  3293. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=5)
  3294. if ev is None:
  3295. raise Exception("No CTRL-EVENT-EAP-STARTED")
  3296. # Do not forward any more EAPOL frames to test wpa_supplicant behavior for
  3297. # a case with an incorrectly behaving WPS AP.
  3298. # Start the real target AP and activate registrar on it.
  3299. hapd2 = hostapd.add_ap(apdev[1],
  3300. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3301. "wpa_passphrase": "12345678", "wpa": "2",
  3302. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  3303. "wps_independent": "1" })
  3304. hapd2.request("WPS_PIN any " + pin)
  3305. dev[0].wait_disconnected(timeout=15)
  3306. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=15)
  3307. if ev is None:
  3308. raise Exception("No CTRL-EVENT-EAP-STARTED for the second AP")
  3309. ev = dev[0].wait_event(["WPS-CRED-RECEIVED"], timeout=15)
  3310. if ev is None:
  3311. raise Exception("No WPS-CRED-RECEIVED for the second AP")
  3312. dev[0].wait_connected(timeout=15)
  3313. @remote_compatible
  3314. def test_ap_wps_priority(dev, apdev):
  3315. """WPS PIN provisioning with configured AP and wps_priority"""
  3316. ssid = "test-wps-conf-pin"
  3317. hapd = hostapd.add_ap(apdev[0],
  3318. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3319. "wpa_passphrase": "12345678", "wpa": "2",
  3320. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3321. logger.info("WPS provisioning step")
  3322. pin = dev[0].wps_read_pin()
  3323. hapd.request("WPS_PIN any " + pin)
  3324. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  3325. dev[0].dump_monitor()
  3326. try:
  3327. dev[0].request("SET wps_priority 6")
  3328. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  3329. dev[0].wait_connected(timeout=30)
  3330. netw = dev[0].list_networks()
  3331. prio = dev[0].get_network(netw[0]['id'], 'priority')
  3332. if prio != '6':
  3333. raise Exception("Unexpected network priority: " + prio)
  3334. finally:
  3335. dev[0].request("SET wps_priority 0")
  3336. @remote_compatible
  3337. def test_ap_wps_and_non_wps(dev, apdev):
  3338. """WPS and non-WPS AP in single hostapd process"""
  3339. params = { "ssid": "wps", "eap_server": "1", "wps_state": "1" }
  3340. hapd = hostapd.add_ap(apdev[0], params)
  3341. params = { "ssid": "no wps" }
  3342. hapd2 = hostapd.add_ap(apdev[1], params)
  3343. appin = hapd.request("WPS_AP_PIN random")
  3344. if "FAIL" in appin:
  3345. raise Exception("Could not generate random AP PIN")
  3346. if appin not in hapd.request("WPS_AP_PIN get"):
  3347. raise Exception("Could not fetch current AP PIN")
  3348. if "FAIL" in hapd.request("WPS_PBC"):
  3349. raise Exception("WPS_PBC failed")
  3350. if "FAIL" in hapd.request("WPS_CANCEL"):
  3351. raise Exception("WPS_CANCEL failed")
  3352. def test_ap_wps_init_oom(dev, apdev):
  3353. """Initial AP configuration and OOM during PSK generation"""
  3354. ssid = "test-wps"
  3355. params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
  3356. hapd = hostapd.add_ap(apdev[0], params)
  3357. with alloc_fail(hapd, 1, "base64_encode;wps_build_cred"):
  3358. pin = dev[0].wps_read_pin()
  3359. hapd.request("WPS_PIN any " + pin)
  3360. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  3361. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  3362. dev[0].wait_disconnected()
  3363. hapd.request("WPS_PIN any " + pin)
  3364. dev[0].wait_connected(timeout=30)
  3365. @remote_compatible
  3366. def test_ap_wps_er_oom(dev, apdev):
  3367. """WPS ER OOM in XML processing"""
  3368. try:
  3369. _test_ap_wps_er_oom(dev, apdev)
  3370. finally:
  3371. dev[0].request("WPS_ER_STOP")
  3372. dev[1].request("WPS_CANCEL")
  3373. dev[0].request("DISCONNECT")
  3374. def _test_ap_wps_er_oom(dev, apdev):
  3375. ssid = "wps-er-ap-config"
  3376. ap_pin = "12345670"
  3377. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  3378. hostapd.add_ap(apdev[0],
  3379. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3380. "wpa_passphrase": "12345678", "wpa": "2",
  3381. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  3382. "device_name": "Wireless AP", "manufacturer": "Company",
  3383. "model_name": "WAP", "model_number": "123",
  3384. "serial_number": "12345", "device_type": "6-0050F204-1",
  3385. "os_version": "01020300",
  3386. "config_methods": "label push_button",
  3387. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  3388. dev[0].connect(ssid, psk="12345678", scan_freq="2412")
  3389. with alloc_fail(dev[0], 1, "base64_decode;xml_get_base64_item"):
  3390. dev[0].request("WPS_ER_START ifname=lo")
  3391. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=3)
  3392. if ev is not None:
  3393. raise Exception("Unexpected AP discovery")
  3394. dev[0].request("WPS_ER_STOP")
  3395. dev[0].request("WPS_ER_START ifname=lo")
  3396. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=10)
  3397. if ev is None:
  3398. raise Exception("AP discovery timed out")
  3399. dev[1].scan_for_bss(apdev[0]['bssid'], freq=2412)
  3400. with alloc_fail(dev[0], 1, "base64_decode;xml_get_base64_item"):
  3401. dev[1].request("WPS_PBC " + apdev[0]['bssid'])
  3402. ev = dev[1].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
  3403. if ev is None:
  3404. raise Exception("PBC scan failed")
  3405. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=15)
  3406. if ev is None:
  3407. raise Exception("Enrollee discovery timed out")
  3408. @remote_compatible
  3409. def test_ap_wps_er_init_oom(dev, apdev):
  3410. """WPS ER and OOM during init"""
  3411. try:
  3412. _test_ap_wps_er_init_oom(dev, apdev)
  3413. finally:
  3414. dev[0].request("WPS_ER_STOP")
  3415. def _test_ap_wps_er_init_oom(dev, apdev):
  3416. with alloc_fail(dev[0], 1, "wps_er_init"):
  3417. if "FAIL" not in dev[0].request("WPS_ER_START ifname=lo"):
  3418. raise Exception("WPS_ER_START succeeded during OOM")
  3419. with alloc_fail(dev[0], 1, "http_server_init"):
  3420. if "FAIL" not in dev[0].request("WPS_ER_START ifname=lo"):
  3421. raise Exception("WPS_ER_START succeeded during OOM")
  3422. with alloc_fail(dev[0], 2, "http_server_init"):
  3423. if "FAIL" not in dev[0].request("WPS_ER_START ifname=lo"):
  3424. raise Exception("WPS_ER_START succeeded during OOM")
  3425. with alloc_fail(dev[0], 1, "eloop_sock_table_add_sock;?eloop_register_sock;wps_er_ssdp_init"):
  3426. if "FAIL" not in dev[0].request("WPS_ER_START ifname=lo"):
  3427. raise Exception("WPS_ER_START succeeded during OOM")
  3428. with fail_test(dev[0], 1, "os_get_random;wps_er_init"):
  3429. if "FAIL" not in dev[0].request("WPS_ER_START ifname=lo"):
  3430. raise Exception("WPS_ER_START succeeded during os_get_random failure")
  3431. @remote_compatible
  3432. def test_ap_wps_er_init_fail(dev, apdev):
  3433. """WPS ER init failure"""
  3434. if "FAIL" not in dev[0].request("WPS_ER_START ifname=does-not-exist"):
  3435. dev[0].request("WPS_ER_STOP")
  3436. raise Exception("WPS_ER_START with non-existing ifname succeeded")
  3437. def test_ap_wps_wpa_cli_action(dev, apdev, test_params):
  3438. """WPS events and wpa_cli action script"""
  3439. logdir = os.path.abspath(test_params['logdir'])
  3440. pidfile = os.path.join(logdir, 'ap_wps_wpa_cli_action.wpa_cli.pid')
  3441. logfile = os.path.join(logdir, 'ap_wps_wpa_cli_action.wpa_cli.res')
  3442. actionfile = os.path.join(logdir, 'ap_wps_wpa_cli_action.wpa_cli.action.sh')
  3443. with open(actionfile, 'w') as f:
  3444. f.write('#!/bin/sh\n')
  3445. f.write('echo $* >> %s\n' % logfile)
  3446. # Kill the process and wait some time before returning to allow all the
  3447. # pending events to be processed with some of this happening after the
  3448. # eloop SIGALRM signal has been scheduled.
  3449. f.write('if [ $2 = "WPS-SUCCESS" -a -r %s ]; then kill `cat %s`; sleep 1; fi\n' % (pidfile, pidfile))
  3450. os.chmod(actionfile, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC |
  3451. stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
  3452. ssid = "test-wps-conf"
  3453. hapd = hostapd.add_ap(apdev[0],
  3454. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  3455. "wpa_passphrase": "12345678", "wpa": "2",
  3456. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  3457. prg = os.path.join(test_params['logdir'],
  3458. 'alt-wpa_supplicant/wpa_supplicant/wpa_cli')
  3459. if not os.path.exists(prg):
  3460. prg = '../../wpa_supplicant/wpa_cli'
  3461. arg = [ prg, '-P', pidfile, '-B', '-i', dev[0].ifname, '-a', actionfile ]
  3462. subprocess.call(arg)
  3463. arg = [ 'ps', 'ax' ]
  3464. cmd = subprocess.Popen(arg, stdout=subprocess.PIPE)
  3465. out = cmd.communicate()[0]
  3466. cmd.wait()
  3467. logger.debug("Processes:\n" + out)
  3468. if "wpa_cli -P %s -B -i %s" % (pidfile, dev[0].ifname) not in out:
  3469. raise Exception("Did not see wpa_cli running")
  3470. hapd.request("WPS_PIN any 12345670")
  3471. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  3472. dev[0].dump_monitor()
  3473. dev[0].request("WPS_PIN " + apdev[0]['bssid'] + " 12345670")
  3474. dev[0].wait_connected(timeout=30)
  3475. for i in range(30):
  3476. if not os.path.exists(pidfile):
  3477. break
  3478. time.sleep(0.1)
  3479. if not os.path.exists(logfile):
  3480. raise Exception("wpa_cli action results file not found")
  3481. with open(logfile, 'r') as f:
  3482. res = f.read()
  3483. if "WPS-SUCCESS" not in res:
  3484. raise Exception("WPS-SUCCESS event not seen in action file")
  3485. arg = [ 'ps', 'ax' ]
  3486. cmd = subprocess.Popen(arg, stdout=subprocess.PIPE)
  3487. out = cmd.communicate()[0]
  3488. cmd.wait()
  3489. logger.debug("Remaining processes:\n" + out)
  3490. if "wpa_cli -P %s -B -i %s" % (pidfile, dev[0].ifname) in out:
  3491. raise Exception("wpa_cli still running")
  3492. if os.path.exists(pidfile):
  3493. raise Exception("PID file not removed")
  3494. def test_ap_wps_er_ssdp_proto(dev, apdev):
  3495. """WPS ER SSDP protocol testing"""
  3496. try:
  3497. _test_ap_wps_er_ssdp_proto(dev, apdev)
  3498. finally:
  3499. dev[0].request("WPS_ER_STOP")
  3500. def _test_ap_wps_er_ssdp_proto(dev, apdev):
  3501. socket.setdefaulttimeout(1)
  3502. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  3503. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  3504. sock.bind(("239.255.255.250", 1900))
  3505. if "FAIL" not in dev[0].request("WPS_ER_START ifname=lo foo"):
  3506. raise Exception("Invalid filter accepted")
  3507. if "OK" not in dev[0].request("WPS_ER_START ifname=lo 1.2.3.4"):
  3508. raise Exception("WPS_ER_START with filter failed")
  3509. (msg,addr) = sock.recvfrom(1000)
  3510. logger.debug("Received SSDP message from %s: %s" % (str(addr), msg))
  3511. if "M-SEARCH" not in msg:
  3512. raise Exception("Not an M-SEARCH")
  3513. sock.sendto("FOO", addr)
  3514. time.sleep(0.1)
  3515. dev[0].request("WPS_ER_STOP")
  3516. dev[0].request("WPS_ER_START ifname=lo")
  3517. (msg,addr) = sock.recvfrom(1000)
  3518. logger.debug("Received SSDP message from %s: %s" % (str(addr), msg))
  3519. if "M-SEARCH" not in msg:
  3520. raise Exception("Not an M-SEARCH")
  3521. sock.sendto("FOO", addr)
  3522. sock.sendto("HTTP/1.1 200 OK\r\nFOO\r\n\r\n", addr)
  3523. sock.sendto("HTTP/1.1 200 OK\r\nNTS:foo\r\n\r\n", addr)
  3524. sock.sendto("HTTP/1.1 200 OK\r\nNTS:ssdp:byebye\r\n\r\n", addr)
  3525. sock.sendto("HTTP/1.1 200 OK\r\ncache-control: foo=1\r\n\r\n", addr)
  3526. sock.sendto("HTTP/1.1 200 OK\r\ncache-control: max-age=1\r\n\r\n", addr)
  3527. sock.sendto("HTTP/1.1 200 OK\r\nusn:\r\n\r\n", addr)
  3528. sock.sendto("HTTP/1.1 200 OK\r\nusn:foo\r\n\r\n", addr)
  3529. sock.sendto("HTTP/1.1 200 OK\r\nusn: uuid:\r\n\r\n", addr)
  3530. sock.sendto("HTTP/1.1 200 OK\r\nusn: uuid: \r\n\r\n", addr)
  3531. sock.sendto("HTTP/1.1 200 OK\r\nusn: uuid: foo\r\n\r\n", addr)
  3532. sock.sendto("HTTP/1.1 200 OK\r\nST: urn:schemas-wifialliance-org:device:WFADevice:1\r\n\r\n", addr)
  3533. 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)
  3534. sock.sendto("HTTP/1.1 200 OK\r\nST: urn:schemas-wifialliance-org:device:WFADevice:1\r\nlocation:foo\r\n\r\n", addr)
  3535. with alloc_fail(dev[0], 1, "wps_er_ap_add"):
  3536. 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)
  3537. time.sleep(0.1)
  3538. with alloc_fail(dev[0], 2, "wps_er_ap_add"):
  3539. 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)
  3540. time.sleep(0.1)
  3541. # Add an AP with bogus URL
  3542. 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)
  3543. # Update timeout on AP without updating URL
  3544. 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)
  3545. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"], timeout=5)
  3546. if ev is None:
  3547. raise Exception("No WPS-ER-AP-REMOVE event on max-age timeout")
  3548. # Add an AP with a valid URL (but no server listing to it)
  3549. 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)
  3550. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"], timeout=5)
  3551. if ev is None:
  3552. raise Exception("No WPS-ER-AP-REMOVE event on max-age timeout")
  3553. sock.close()
  3554. wps_event_url = None
  3555. def gen_upnp_info(eventSubURL='wps_event', controlURL='wps_control',
  3556. udn='uuid:27ea801a-9e5c-4e73-bd82-f89cbcd10d7e'):
  3557. payload = '''<?xml version="1.0"?>
  3558. <root xmlns="urn:schemas-upnp-org:device-1-0">
  3559. <specVersion>
  3560. <major>1</major>
  3561. <minor>0</minor>
  3562. </specVersion>
  3563. <device>
  3564. <deviceType>urn:schemas-wifialliance-org:device:WFADevice:1</deviceType>
  3565. <friendlyName>WPS Access Point</friendlyName>
  3566. <manufacturer>Company</manufacturer>
  3567. <modelName>WAP</modelName>
  3568. <modelNumber>123</modelNumber>
  3569. <serialNumber>12345</serialNumber>
  3570. '''
  3571. if udn:
  3572. payload += '<UDN>' + udn + '</UDN>'
  3573. payload += '''<serviceList>
  3574. <service>
  3575. <serviceType>urn:schemas-wifialliance-org:service:WFAWLANConfig:1</serviceType>
  3576. <serviceId>urn:wifialliance-org:serviceId:WFAWLANConfig1</serviceId>
  3577. <SCPDURL>wps_scpd.xml</SCPDURL>
  3578. '''
  3579. if controlURL:
  3580. payload += '<controlURL>' + controlURL + '</controlURL>\n'
  3581. if eventSubURL:
  3582. payload += '<eventSubURL>' + eventSubURL + '</eventSubURL>\n'
  3583. payload += '''</service>
  3584. </serviceList>
  3585. </device>
  3586. </root>
  3587. '''
  3588. hdr = 'HTTP/1.1 200 OK\r\n' + \
  3589. 'Content-Type: text/xml; charset="utf-8"\r\n' + \
  3590. 'Server: Unspecified, UPnP/1.0, Unspecified\r\n' + \
  3591. 'Connection: close\r\n' + \
  3592. 'Content-Length: ' + str(len(payload)) + '\r\n' + \
  3593. 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
  3594. return hdr + payload
  3595. def gen_wps_control(payload_override=None):
  3596. payload = '''<?xml version="1.0"?>
  3597. <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  3598. <s:Body>
  3599. <u:GetDeviceInfoResponse xmlns:u="urn:schemas-wifialliance-org:service:WFAWLANConfig:1">
  3600. <NewDeviceInfo>EEoAARAQIgABBBBHABAn6oAanlxOc72C+Jy80Q1+ECAABgIAAAADABAaABCJZ7DPtbU3Ust9
  3601. Z3wJF07WEDIAwH45D3i1OqB7eJGwTzqeapS71h3KyXncK2xJZ+xqScrlorNEg6LijBJzG2Ca
  3602. +FZli0iliDJd397yAx/jk4nFXco3q5ylBSvSw9dhJ5u1xBKSnTilKGlUHPhLP75PUqM3fot9
  3603. 7zwtFZ4bx6x1sBA6oEe2d0aUJmLumQGCiKEIWlnxs44zego/2tAe81bDzdPBM7o5HH/FUhD+
  3604. KoGzFXp51atP+1n9Vta6AkI0Vye99JKLcC6Md9dMJltSVBgd4Xc4lRAEAAIAIxAQAAIADRAN
  3605. AAEBEAgAAgAEEEQAAQIQIQAHQ29tcGFueRAjAANXQVAQJAADMTIzEEIABTEyMzQ1EFQACAAG
  3606. AFDyBAABEBEAC1dpcmVsZXNzIEFQEDwAAQEQAgACAAAQEgACAAAQCQACAAAQLQAEgQIDABBJ
  3607. AAYANyoAASA=
  3608. </NewDeviceInfo>
  3609. </u:GetDeviceInfoResponse>
  3610. </s:Body>
  3611. </s:Envelope>
  3612. '''
  3613. if payload_override:
  3614. payload = payload_override
  3615. hdr = 'HTTP/1.1 200 OK\r\n' + \
  3616. 'Content-Type: text/xml; charset="utf-8"\r\n' + \
  3617. 'Server: Unspecified, UPnP/1.0, Unspecified\r\n' + \
  3618. 'Connection: close\r\n' + \
  3619. 'Content-Length: ' + str(len(payload)) + '\r\n' + \
  3620. 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
  3621. return hdr + payload
  3622. def gen_wps_event(sid='uuid:7eb3342a-8a5f-47fe-a585-0785bfec6d8a'):
  3623. payload = ""
  3624. hdr = 'HTTP/1.1 200 OK\r\n' + \
  3625. 'Content-Type: text/xml; charset="utf-8"\r\n' + \
  3626. 'Server: Unspecified, UPnP/1.0, Unspecified\r\n' + \
  3627. 'Connection: close\r\n' + \
  3628. 'Content-Length: ' + str(len(payload)) + '\r\n'
  3629. if sid:
  3630. hdr += 'SID: ' + sid + '\r\n'
  3631. hdr += 'Timeout: Second-1801\r\n' + \
  3632. 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
  3633. return hdr + payload
  3634. class WPSAPHTTPServer(SocketServer.StreamRequestHandler):
  3635. def handle(self):
  3636. data = self.rfile.readline().strip()
  3637. logger.info("HTTP server received: " + data)
  3638. while True:
  3639. hdr = self.rfile.readline().strip()
  3640. if len(hdr) == 0:
  3641. break
  3642. logger.info("HTTP header: " + hdr)
  3643. if "CALLBACK:" in hdr:
  3644. global wps_event_url
  3645. wps_event_url = hdr.split(' ')[1].strip('<>')
  3646. if "GET /foo.xml" in data:
  3647. self.handle_upnp_info()
  3648. elif "POST /wps_control" in data:
  3649. self.handle_wps_control()
  3650. elif "SUBSCRIBE /wps_event" in data:
  3651. self.handle_wps_event()
  3652. else:
  3653. self.handle_others(data)
  3654. def handle_upnp_info(self):
  3655. self.wfile.write(gen_upnp_info())
  3656. def handle_wps_control(self):
  3657. self.wfile.write(gen_wps_control())
  3658. def handle_wps_event(self):
  3659. self.wfile.write(gen_wps_event())
  3660. def handle_others(self, data):
  3661. logger.info("Ignore HTTP request: " + data)
  3662. class MyTCPServer(SocketServer.TCPServer):
  3663. def __init__(self, addr, handler):
  3664. self.allow_reuse_address = True
  3665. SocketServer.TCPServer.__init__(self, addr, handler)
  3666. def wps_er_start(dev, http_server, max_age=1, wait_m_search=False,
  3667. location_url=None):
  3668. socket.setdefaulttimeout(1)
  3669. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  3670. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  3671. sock.bind(("239.255.255.250", 1900))
  3672. dev.request("WPS_ER_START ifname=lo")
  3673. for i in range(100):
  3674. (msg,addr) = sock.recvfrom(1000)
  3675. logger.debug("Received SSDP message from %s: %s" % (str(addr), msg))
  3676. if "M-SEARCH" in msg:
  3677. break
  3678. if not wait_m_search:
  3679. raise Exception("Not an M-SEARCH")
  3680. if i == 99:
  3681. raise Exception("No M-SEARCH seen")
  3682. # Add an AP with a valid URL and server listing to it
  3683. server = MyTCPServer(("127.0.0.1", 12345), http_server)
  3684. if not location_url:
  3685. location_url = 'http://127.0.0.1:12345/foo.xml'
  3686. 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)
  3687. server.timeout = 1
  3688. return server,sock
  3689. def wps_er_stop(dev, sock, server, on_alloc_fail=False):
  3690. sock.close()
  3691. server.server_close()
  3692. if on_alloc_fail:
  3693. done = False
  3694. for i in range(50):
  3695. res = dev.request("GET_ALLOC_FAIL")
  3696. if res.startswith("0:"):
  3697. done = True
  3698. break
  3699. time.sleep(0.1)
  3700. if not done:
  3701. raise Exception("No allocation failure reported")
  3702. else:
  3703. ev = dev.wait_event(["WPS-ER-AP-REMOVE"], timeout=5)
  3704. if ev is None:
  3705. raise Exception("No WPS-ER-AP-REMOVE event on max-age timeout")
  3706. dev.request("WPS_ER_STOP")
  3707. def run_wps_er_proto_test(dev, handler, no_event_url=False, location_url=None):
  3708. try:
  3709. uuid = '27ea801a-9e5c-4e73-bd82-f89cbcd10d7e'
  3710. server,sock = wps_er_start(dev, handler, location_url=location_url)
  3711. global wps_event_url
  3712. wps_event_url = None
  3713. server.handle_request()
  3714. server.handle_request()
  3715. server.handle_request()
  3716. server.server_close()
  3717. if no_event_url:
  3718. if wps_event_url:
  3719. raise Exception("Received event URL unexpectedly")
  3720. return
  3721. if wps_event_url is None:
  3722. raise Exception("Did not get event URL")
  3723. logger.info("Event URL: " + wps_event_url)
  3724. finally:
  3725. dev.request("WPS_ER_STOP")
  3726. def send_wlanevent(url, uuid, data, no_response=False):
  3727. conn = httplib.HTTPConnection(url.netloc)
  3728. payload = '''<?xml version="1.0" encoding="utf-8"?>
  3729. <e:propertyset xmlns:e="urn:schemas-upnp-org:event-1-0">
  3730. <e:property><STAStatus>1</STAStatus></e:property>
  3731. <e:property><APStatus>1</APStatus></e:property>
  3732. <e:property><WLANEvent>'''
  3733. payload += base64.b64encode(data)
  3734. payload += '</WLANEvent></e:property></e:propertyset>'
  3735. headers = { "Content-type": 'text/xml; charset="utf-8"',
  3736. "Server": "Unspecified, UPnP/1.0, Unspecified",
  3737. "HOST": url.netloc,
  3738. "NT": "upnp:event",
  3739. "SID": "uuid:" + uuid,
  3740. "SEQ": "0",
  3741. "Content-Length": str(len(payload)) }
  3742. conn.request("NOTIFY", url.path, payload, headers)
  3743. if no_response:
  3744. try:
  3745. conn.getresponse()
  3746. except Exception, e:
  3747. pass
  3748. return
  3749. resp = conn.getresponse()
  3750. if resp.status != 200:
  3751. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3752. def test_ap_wps_er_http_proto(dev, apdev):
  3753. """WPS ER HTTP protocol testing"""
  3754. try:
  3755. _test_ap_wps_er_http_proto(dev, apdev)
  3756. finally:
  3757. dev[0].request("WPS_ER_STOP")
  3758. def _test_ap_wps_er_http_proto(dev, apdev):
  3759. uuid = '27ea801a-9e5c-4e73-bd82-f89cbcd10d7e'
  3760. server,sock = wps_er_start(dev[0], WPSAPHTTPServer, max_age=15)
  3761. global wps_event_url
  3762. wps_event_url = None
  3763. server.handle_request()
  3764. server.handle_request()
  3765. server.handle_request()
  3766. server.server_close()
  3767. if wps_event_url is None:
  3768. raise Exception("Did not get event URL")
  3769. logger.info("Event URL: " + wps_event_url)
  3770. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=10)
  3771. if ev is None:
  3772. raise Exception("No WPS-ER-AP-ADD event")
  3773. if uuid not in ev:
  3774. raise Exception("UUID mismatch")
  3775. sock.close()
  3776. logger.info("Valid Probe Request notification")
  3777. url = urlparse.urlparse(wps_event_url)
  3778. conn = httplib.HTTPConnection(url.netloc)
  3779. payload = '''<?xml version="1.0" encoding="utf-8"?>
  3780. <e:propertyset xmlns:e="urn:schemas-upnp-org:event-1-0">
  3781. <e:property><STAStatus>1</STAStatus></e:property>
  3782. <e:property><APStatus>1</APStatus></e:property>
  3783. <e:property><WLANEvent>ATAyOjAwOjAwOjAwOjAwOjAwEEoAARAQOgABAhAIAAIxSBBHABA2LbR7pTpRkYj7VFi5hrLk
  3784. EFQACAAAAAAAAAAAEDwAAQMQAgACAAAQCQACAAAQEgACAAAQIQABIBAjAAEgECQAASAQEQAI
  3785. RGV2aWNlIEEQSQAGADcqAAEg
  3786. </WLANEvent></e:property>
  3787. </e:propertyset>
  3788. '''
  3789. headers = { "Content-type": 'text/xml; charset="utf-8"',
  3790. "Server": "Unspecified, UPnP/1.0, Unspecified",
  3791. "HOST": url.netloc,
  3792. "NT": "upnp:event",
  3793. "SID": "uuid:" + uuid,
  3794. "SEQ": "0",
  3795. "Content-Length": str(len(payload)) }
  3796. conn.request("NOTIFY", url.path, payload, headers)
  3797. resp = conn.getresponse()
  3798. if resp.status != 200:
  3799. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3800. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=5)
  3801. if ev is None:
  3802. raise Exception("No WPS-ER-ENROLLEE-ADD event")
  3803. if "362db47b-a53a-5191-88fb-5458b986b2e4" not in ev:
  3804. raise Exception("No Enrollee UUID match")
  3805. logger.info("Incorrect event URL AP id")
  3806. conn = httplib.HTTPConnection(url.netloc)
  3807. conn.request("NOTIFY", url.path + '123', payload, headers)
  3808. resp = conn.getresponse()
  3809. if resp.status != 404:
  3810. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3811. logger.info("Missing AP id")
  3812. conn = httplib.HTTPConnection(url.netloc)
  3813. conn.request("NOTIFY", '/event/' + url.path.split('/')[2],
  3814. payload, headers)
  3815. time.sleep(0.1)
  3816. logger.info("Incorrect event URL event id")
  3817. conn = httplib.HTTPConnection(url.netloc)
  3818. conn.request("NOTIFY", '/event/123456789/123', payload, headers)
  3819. time.sleep(0.1)
  3820. logger.info("Incorrect event URL prefix")
  3821. conn = httplib.HTTPConnection(url.netloc)
  3822. conn.request("NOTIFY", '/foobar/123456789/123', payload, headers)
  3823. resp = conn.getresponse()
  3824. if resp.status != 404:
  3825. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3826. logger.info("Unsupported request")
  3827. conn = httplib.HTTPConnection(url.netloc)
  3828. conn.request("FOOBAR", '/foobar/123456789/123', payload, headers)
  3829. resp = conn.getresponse()
  3830. if resp.status != 501:
  3831. raise Exception("Unexpected HTTP response: %d" % resp.status)
  3832. logger.info("Unsupported request and OOM")
  3833. with alloc_fail(dev[0], 1, "wps_er_http_req"):
  3834. conn = httplib.HTTPConnection(url.netloc)
  3835. conn.request("FOOBAR", '/foobar/123456789/123', payload, headers)
  3836. time.sleep(0.5)
  3837. logger.info("Too short WLANEvent")
  3838. data = '\x00'
  3839. send_wlanevent(url, uuid, data)
  3840. logger.info("Invalid WLANEventMAC")
  3841. data = '\x00qwertyuiopasdfghjklzxcvbnm'
  3842. send_wlanevent(url, uuid, data)
  3843. logger.info("Unknown WLANEventType")
  3844. data = '\xff02:00:00:00:00:00'
  3845. send_wlanevent(url, uuid, data)
  3846. logger.info("Probe Request notification without any attributes")
  3847. data = '\x0102:00:00:00:00:00'
  3848. send_wlanevent(url, uuid, data)
  3849. logger.info("Probe Request notification with invalid attribute")
  3850. data = '\x0102:00:00:00:00:00\xff'
  3851. send_wlanevent(url, uuid, data)
  3852. logger.info("EAP message without any attributes")
  3853. data = '\x0202:00:00:00:00:00'
  3854. send_wlanevent(url, uuid, data)
  3855. logger.info("EAP message with invalid attribute")
  3856. data = '\x0202:00:00:00:00:00\xff'
  3857. send_wlanevent(url, uuid, data)
  3858. logger.info("EAP message from new STA and not M1")
  3859. data = '\x0202:ff:ff:ff:ff:ff' + '\x10\x22\x00\x01\x05'
  3860. send_wlanevent(url, uuid, data)
  3861. logger.info("EAP message: M1")
  3862. data = '\x0202:00:00:00:00:00'
  3863. data += '\x10\x22\x00\x01\x04'
  3864. data += '\x10\x47\x00\x10' + 16*'\x00'
  3865. data += '\x10\x20\x00\x06\x02\x00\x00\x00\x00\x00'
  3866. data += '\x10\x1a\x00\x10' + 16*'\x00'
  3867. data += '\x10\x32\x00\xc0' + 192*'\x00'
  3868. data += '\x10\x04\x00\x02\x00\x00'
  3869. data += '\x10\x10\x00\x02\x00\x00'
  3870. data += '\x10\x0d\x00\x01\x00'
  3871. data += '\x10\x08\x00\x02\x00\x00'
  3872. data += '\x10\x44\x00\x01\x00'
  3873. data += '\x10\x21\x00\x00'
  3874. data += '\x10\x23\x00\x00'
  3875. data += '\x10\x24\x00\x00'
  3876. data += '\x10\x42\x00\x00'
  3877. data += '\x10\x54\x00\x08' + 8*'\x00'
  3878. data += '\x10\x11\x00\x00'
  3879. data += '\x10\x3c\x00\x01\x00'
  3880. data += '\x10\x02\x00\x02\x00\x00'
  3881. data += '\x10\x12\x00\x02\x00\x00'
  3882. data += '\x10\x09\x00\x02\x00\x00'
  3883. data += '\x10\x2d\x00\x04\x00\x00\x00\x00'
  3884. m1 = data
  3885. send_wlanevent(url, uuid, data)
  3886. logger.info("EAP message: WSC_ACK")
  3887. data = '\x0202:00:00:00:00:00' + '\x10\x22\x00\x01\x0d'
  3888. send_wlanevent(url, uuid, data)
  3889. logger.info("EAP message: M1")
  3890. send_wlanevent(url, uuid, m1)
  3891. logger.info("EAP message: WSC_NACK")
  3892. data = '\x0202:00:00:00:00:00' + '\x10\x22\x00\x01\x0e'
  3893. send_wlanevent(url, uuid, data)
  3894. logger.info("EAP message: M1 - Too long attribute values")
  3895. data = '\x0202:00:00:00:00:00'
  3896. data += '\x10\x11\x00\x21' + 33*'\x00'
  3897. data += '\x10\x45\x00\x21' + 33*'\x00'
  3898. data += '\x10\x42\x00\x21' + 33*'\x00'
  3899. data += '\x10\x24\x00\x21' + 33*'\x00'
  3900. data += '\x10\x23\x00\x21' + 33*'\x00'
  3901. data += '\x10\x21\x00\x41' + 65*'\x00'
  3902. data += '\x10\x49\x00\x09\x00\x37\x2a\x05\x02\x00\x00\x05\x00'
  3903. send_wlanevent(url, uuid, data)
  3904. logger.info("EAP message: M1 missing UUID-E")
  3905. data = '\x0202:00:00:00:00:00'
  3906. data += '\x10\x22\x00\x01\x04'
  3907. send_wlanevent(url, uuid, data)
  3908. logger.info("EAP message: M1 missing MAC Address")
  3909. data += '\x10\x47\x00\x10' + 16*'\x00'
  3910. send_wlanevent(url, uuid, data)
  3911. logger.info("EAP message: M1 missing Enrollee Nonce")
  3912. data += '\x10\x20\x00\x06\x02\x00\x00\x00\x00\x00'
  3913. send_wlanevent(url, uuid, data)
  3914. logger.info("EAP message: M1 missing Public Key")
  3915. data += '\x10\x1a\x00\x10' + 16*'\x00'
  3916. send_wlanevent(url, uuid, data)
  3917. logger.info("EAP message: M1 missing Authentication Type flags")
  3918. data += '\x10\x32\x00\xc0' + 192*'\x00'
  3919. send_wlanevent(url, uuid, data)
  3920. logger.info("EAP message: M1 missing Encryption Type Flags")
  3921. data += '\x10\x04\x00\x02\x00\x00'
  3922. send_wlanevent(url, uuid, data)
  3923. logger.info("EAP message: M1 missing Connection Type flags")
  3924. data += '\x10\x10\x00\x02\x00\x00'
  3925. send_wlanevent(url, uuid, data)
  3926. logger.info("EAP message: M1 missing Config Methods")
  3927. data += '\x10\x0d\x00\x01\x00'
  3928. send_wlanevent(url, uuid, data)
  3929. logger.info("EAP message: M1 missing Wi-Fi Protected Setup State")
  3930. data += '\x10\x08\x00\x02\x00\x00'
  3931. send_wlanevent(url, uuid, data)
  3932. logger.info("EAP message: M1 missing Manufacturer")
  3933. data += '\x10\x44\x00\x01\x00'
  3934. send_wlanevent(url, uuid, data)
  3935. logger.info("EAP message: M1 missing Model Name")
  3936. data += '\x10\x21\x00\x00'
  3937. send_wlanevent(url, uuid, data)
  3938. logger.info("EAP message: M1 missing Model Number")
  3939. data += '\x10\x23\x00\x00'
  3940. send_wlanevent(url, uuid, data)
  3941. logger.info("EAP message: M1 missing Serial Number")
  3942. data += '\x10\x24\x00\x00'
  3943. send_wlanevent(url, uuid, data)
  3944. logger.info("EAP message: M1 missing Primary Device Type")
  3945. data += '\x10\x42\x00\x00'
  3946. send_wlanevent(url, uuid, data)
  3947. logger.info("EAP message: M1 missing Device Name")
  3948. data += '\x10\x54\x00\x08' + 8*'\x00'
  3949. send_wlanevent(url, uuid, data)
  3950. logger.info("EAP message: M1 missing RF Bands")
  3951. data += '\x10\x11\x00\x00'
  3952. send_wlanevent(url, uuid, data)
  3953. logger.info("EAP message: M1 missing Association State")
  3954. data += '\x10\x3c\x00\x01\x00'
  3955. send_wlanevent(url, uuid, data)
  3956. logger.info("EAP message: M1 missing Device Password ID")
  3957. data += '\x10\x02\x00\x02\x00\x00'
  3958. send_wlanevent(url, uuid, data)
  3959. logger.info("EAP message: M1 missing Configuration Error")
  3960. data += '\x10\x12\x00\x02\x00\x00'
  3961. send_wlanevent(url, uuid, data)
  3962. logger.info("EAP message: M1 missing OS Version")
  3963. data += '\x10\x09\x00\x02\x00\x00'
  3964. send_wlanevent(url, uuid, data)
  3965. logger.info("Check max concurrent requests")
  3966. addr = (url.hostname, url.port)
  3967. socks = {}
  3968. for i in range(20):
  3969. socks[i] = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
  3970. socket.IPPROTO_TCP)
  3971. socks[i].settimeout(10)
  3972. socks[i].connect(addr)
  3973. for i in range(20):
  3974. socks[i].send("GET / HTTP/1.1\r\n\r\n")
  3975. count = 0
  3976. for i in range(20):
  3977. try:
  3978. res = socks[i].recv(100)
  3979. if "HTTP/1" in res:
  3980. count += 1
  3981. except:
  3982. pass
  3983. socks[i].close()
  3984. logger.info("%d concurrent HTTP GET operations returned response" % count)
  3985. if count < 10:
  3986. raise Exception("Too few concurrent HTTP connections accepted")
  3987. logger.info("OOM in HTTP server")
  3988. for func in [ "http_request_init", "httpread_create",
  3989. "eloop_register_timeout;httpread_create",
  3990. "eloop_sock_table_add_sock;?eloop_register_sock;httpread_create",
  3991. "httpread_hdr_analyze" ]:
  3992. with alloc_fail(dev[0], 1, func):
  3993. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
  3994. socket.IPPROTO_TCP)
  3995. sock.connect(addr)
  3996. sock.send("GET / HTTP/1.1\r\n\r\n")
  3997. try:
  3998. sock.recv(100)
  3999. except:
  4000. pass
  4001. sock.close()
  4002. logger.info("Invalid HTTP header")
  4003. for req in [ " GET / HTTP/1.1\r\n\r\n",
  4004. "HTTP/1.1 200 OK\r\n\r\n",
  4005. "HTTP/\r\n\r\n",
  4006. "GET %%a%aa% HTTP/1.1\r\n\r\n",
  4007. "GET / HTTP/1.1\r\n FOO\r\n\r\n",
  4008. "NOTIFY / HTTP/1.1\r\n" + 4097*'a' + '\r\n\r\n',
  4009. "NOTIFY / HTTP/1.1\r\n\r\n" + 8193*'a',
  4010. "POST / HTTP/1.1\r\nTransfer-Encoding: CHUNKED\r\n\r\n foo\r\n",
  4011. "POST / HTTP/1.1\r\nTransfer-Encoding: CHUNKED\r\n\r\n1\r\nfoo\r\n",
  4012. "POST / HTTP/1.1\r\nTransfer-Encoding: CHUNKED\r\n\r\n0\r\n",
  4013. "POST / HTTP/1.1\r\nTransfer-Encoding: CHUNKED\r\n\r\n0\r\naa\ra\r\n\ra" ]:
  4014. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
  4015. socket.IPPROTO_TCP)
  4016. sock.settimeout(0.1)
  4017. sock.connect(addr)
  4018. sock.send(req)
  4019. try:
  4020. sock.recv(100)
  4021. except:
  4022. pass
  4023. sock.close()
  4024. with alloc_fail(dev[0], 2, "httpread_read_handler"):
  4025. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
  4026. socket.IPPROTO_TCP)
  4027. sock.connect(addr)
  4028. sock.send("NOTIFY / HTTP/1.1\r\n\r\n" + 4500*'a')
  4029. try:
  4030. sock.recv(100)
  4031. except:
  4032. pass
  4033. sock.close()
  4034. conn = httplib.HTTPConnection(url.netloc)
  4035. payload = '<foo'
  4036. headers = { "Content-type": 'text/xml; charset="utf-8"',
  4037. "Server": "Unspecified, UPnP/1.0, Unspecified",
  4038. "HOST": url.netloc,
  4039. "NT": "upnp:event",
  4040. "SID": "uuid:" + uuid,
  4041. "SEQ": "0",
  4042. "Content-Length": str(len(payload)) }
  4043. conn.request("NOTIFY", url.path, payload, headers)
  4044. resp = conn.getresponse()
  4045. if resp.status != 200:
  4046. raise Exception("Unexpected HTTP response: %d" % resp.status)
  4047. conn = httplib.HTTPConnection(url.netloc)
  4048. payload = '<WLANEvent foo></WLANEvent>'
  4049. headers = { "Content-type": 'text/xml; charset="utf-8"',
  4050. "Server": "Unspecified, UPnP/1.0, Unspecified",
  4051. "HOST": url.netloc,
  4052. "NT": "upnp:event",
  4053. "SID": "uuid:" + uuid,
  4054. "SEQ": "0",
  4055. "Content-Length": str(len(payload)) }
  4056. conn.request("NOTIFY", url.path, payload, headers)
  4057. resp = conn.getresponse()
  4058. if resp.status != 200:
  4059. raise Exception("Unexpected HTTP response: %d" % resp.status)
  4060. with alloc_fail(dev[0], 1, "xml_get_first_item"):
  4061. send_wlanevent(url, uuid, '')
  4062. with alloc_fail(dev[0], 1, "wpabuf_alloc_ext_data;xml_get_base64_item"):
  4063. send_wlanevent(url, uuid, 'foo')
  4064. for func in [ "wps_init",
  4065. "wps_process_manufacturer",
  4066. "wps_process_model_name",
  4067. "wps_process_model_number",
  4068. "wps_process_serial_number",
  4069. "wps_process_dev_name" ]:
  4070. with alloc_fail(dev[0], 1, func):
  4071. send_wlanevent(url, uuid, m1)
  4072. with alloc_fail(dev[0], 1, "wps_er_http_resp_ok"):
  4073. send_wlanevent(url, uuid, m1, no_response=True)
  4074. with alloc_fail(dev[0], 1, "wps_er_http_resp_not_found"):
  4075. url2 = urlparse.urlparse(wps_event_url.replace('/event/', '/notfound/'))
  4076. send_wlanevent(url2, uuid, m1, no_response=True)
  4077. logger.info("EAP message: M1")
  4078. data = '\x0202:11:22:00:00:00'
  4079. data += '\x10\x22\x00\x01\x04'
  4080. data += '\x10\x47\x00\x10' + 16*'\x00'
  4081. data += '\x10\x20\x00\x06\x02\x00\x00\x00\x00\x00'
  4082. data += '\x10\x1a\x00\x10' + 16*'\x00'
  4083. data += '\x10\x32\x00\xc0' + 192*'\x00'
  4084. data += '\x10\x04\x00\x02\x00\x00'
  4085. data += '\x10\x10\x00\x02\x00\x00'
  4086. data += '\x10\x0d\x00\x01\x00'
  4087. data += '\x10\x08\x00\x02\x00\x00'
  4088. data += '\x10\x44\x00\x01\x00'
  4089. data += '\x10\x21\x00\x00'
  4090. data += '\x10\x23\x00\x00'
  4091. data += '\x10\x24\x00\x00'
  4092. data += '\x10\x42\x00\x00'
  4093. data += '\x10\x54\x00\x08' + 8*'\x00'
  4094. data += '\x10\x11\x00\x00'
  4095. data += '\x10\x3c\x00\x01\x00'
  4096. data += '\x10\x02\x00\x02\x00\x00'
  4097. data += '\x10\x12\x00\x02\x00\x00'
  4098. data += '\x10\x09\x00\x02\x00\x00'
  4099. data += '\x10\x2d\x00\x04\x00\x00\x00\x00'
  4100. dev[0].dump_monitor()
  4101. with alloc_fail(dev[0], 1, "wps_er_add_sta_data"):
  4102. send_wlanevent(url, uuid, data)
  4103. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=0.1)
  4104. if ev is not None:
  4105. raise Exception("Unexpected enrollee add event")
  4106. send_wlanevent(url, uuid, data)
  4107. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=2)
  4108. if ev is None:
  4109. raise Exception("Enrollee add event not seen")
  4110. with alloc_fail(dev[0], 1, "base64_encode;wps_er_soap_hdr"):
  4111. send_wlanevent(url, uuid, data)
  4112. with alloc_fail(dev[0], 1, "wpabuf_alloc;wps_er_soap_hdr"):
  4113. send_wlanevent(url, uuid, data)
  4114. with alloc_fail(dev[0], 1, "http_client_url_parse;wps_er_sta_send_msg"):
  4115. send_wlanevent(url, uuid, data)
  4116. with alloc_fail(dev[0], 1, "http_client_addr;wps_er_sta_send_msg"):
  4117. send_wlanevent(url, uuid, data)
  4118. def test_ap_wps_er_http_proto_no_event_sub_url(dev, apdev):
  4119. """WPS ER HTTP protocol testing - no eventSubURL"""
  4120. class WPSAPHTTPServer_no_event_sub_url(WPSAPHTTPServer):
  4121. def handle_upnp_info(self):
  4122. self.wfile.write(gen_upnp_info(eventSubURL=None))
  4123. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_no_event_sub_url,
  4124. no_event_url=True)
  4125. def test_ap_wps_er_http_proto_event_sub_url_dns(dev, apdev):
  4126. """WPS ER HTTP protocol testing - DNS name in eventSubURL"""
  4127. class WPSAPHTTPServer_event_sub_url_dns(WPSAPHTTPServer):
  4128. def handle_upnp_info(self):
  4129. self.wfile.write(gen_upnp_info(eventSubURL='http://example.com/wps_event'))
  4130. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_event_sub_url_dns,
  4131. no_event_url=True)
  4132. def test_ap_wps_er_http_proto_subscribe_oom(dev, apdev):
  4133. """WPS ER HTTP protocol testing - subscribe OOM"""
  4134. try:
  4135. _test_ap_wps_er_http_proto_subscribe_oom(dev, apdev)
  4136. finally:
  4137. dev[0].request("WPS_ER_STOP")
  4138. def _test_ap_wps_er_http_proto_subscribe_oom(dev, apdev):
  4139. tests = [ (1, "http_client_url_parse"),
  4140. (1, "wpabuf_alloc;wps_er_subscribe"),
  4141. (1, "http_client_addr"),
  4142. (1, "eloop_sock_table_add_sock;?eloop_register_sock;http_client_addr"),
  4143. (1, "eloop_register_timeout;http_client_addr") ]
  4144. for count,func in tests:
  4145. with alloc_fail(dev[0], count, func):
  4146. server,sock = wps_er_start(dev[0], WPSAPHTTPServer)
  4147. server.handle_request()
  4148. server.handle_request()
  4149. wps_er_stop(dev[0], sock, server, on_alloc_fail=True)
  4150. def test_ap_wps_er_http_proto_no_sid(dev, apdev):
  4151. """WPS ER HTTP protocol testing - no SID"""
  4152. class WPSAPHTTPServer_no_sid(WPSAPHTTPServer):
  4153. def handle_wps_event(self):
  4154. self.wfile.write(gen_wps_event(sid=None))
  4155. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_no_sid)
  4156. def test_ap_wps_er_http_proto_invalid_sid_no_uuid(dev, apdev):
  4157. """WPS ER HTTP protocol testing - invalid SID - no UUID"""
  4158. class WPSAPHTTPServer_invalid_sid_no_uuid(WPSAPHTTPServer):
  4159. def handle_wps_event(self):
  4160. self.wfile.write(gen_wps_event(sid='FOO'))
  4161. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_invalid_sid_no_uuid)
  4162. def test_ap_wps_er_http_proto_invalid_sid_uuid(dev, apdev):
  4163. """WPS ER HTTP protocol testing - invalid SID UUID"""
  4164. class WPSAPHTTPServer_invalid_sid_uuid(WPSAPHTTPServer):
  4165. def handle_wps_event(self):
  4166. self.wfile.write(gen_wps_event(sid='uuid:FOO'))
  4167. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_invalid_sid_uuid)
  4168. def test_ap_wps_er_http_proto_subscribe_failing(dev, apdev):
  4169. """WPS ER HTTP protocol testing - SUBSCRIBE failing"""
  4170. class WPSAPHTTPServer_fail_subscribe(WPSAPHTTPServer):
  4171. def handle_wps_event(self):
  4172. payload = ""
  4173. hdr = 'HTTP/1.1 404 Not Found\r\n' + \
  4174. 'Content-Type: text/xml; charset="utf-8"\r\n' + \
  4175. 'Server: Unspecified, UPnP/1.0, Unspecified\r\n' + \
  4176. 'Connection: close\r\n' + \
  4177. 'Content-Length: ' + str(len(payload)) + '\r\n' + \
  4178. 'Timeout: Second-1801\r\n' + \
  4179. 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
  4180. self.wfile.write(hdr + payload)
  4181. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_fail_subscribe)
  4182. def test_ap_wps_er_http_proto_subscribe_invalid_response(dev, apdev):
  4183. """WPS ER HTTP protocol testing - SUBSCRIBE and invalid response"""
  4184. class WPSAPHTTPServer_subscribe_invalid_response(WPSAPHTTPServer):
  4185. def handle_wps_event(self):
  4186. payload = ""
  4187. hdr = 'HTTP/1.1 FOO\r\n' + \
  4188. 'Content-Type: text/xml; charset="utf-8"\r\n' + \
  4189. 'Server: Unspecified, UPnP/1.0, Unspecified\r\n' + \
  4190. 'Connection: close\r\n' + \
  4191. 'Content-Length: ' + str(len(payload)) + '\r\n' + \
  4192. 'Timeout: Second-1801\r\n' + \
  4193. 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
  4194. self.wfile.write(hdr + payload)
  4195. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_subscribe_invalid_response)
  4196. def test_ap_wps_er_http_proto_subscribe_invalid_response(dev, apdev):
  4197. """WPS ER HTTP protocol testing - SUBSCRIBE and invalid response"""
  4198. class WPSAPHTTPServer_invalid_m1(WPSAPHTTPServer):
  4199. def handle_wps_control(self):
  4200. payload = '''<?xml version="1.0"?>
  4201. <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  4202. <s:Body>
  4203. <u:GetDeviceInfoResponse xmlns:u="urn:schemas-wifialliance-org:service:WFAWLANConfig:1">
  4204. <NewDeviceInfo>Rk9P</NewDeviceInfo>
  4205. </u:GetDeviceInfoResponse>
  4206. </s:Body>
  4207. </s:Envelope>
  4208. '''
  4209. self.wfile.write(gen_wps_control(payload_override=payload))
  4210. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_invalid_m1, no_event_url=True)
  4211. def test_ap_wps_er_http_proto_upnp_info_no_device(dev, apdev):
  4212. """WPS ER HTTP protocol testing - No device in UPnP info"""
  4213. class WPSAPHTTPServer_no_device(WPSAPHTTPServer):
  4214. def handle_upnp_info(self):
  4215. payload = '''<?xml version="1.0"?>
  4216. <root xmlns="urn:schemas-upnp-org:device-1-0">
  4217. <specVersion>
  4218. <major>1</major>
  4219. <minor>0</minor>
  4220. </specVersion>
  4221. </root>
  4222. '''
  4223. hdr = 'HTTP/1.1 200 OK\r\n' + \
  4224. 'Content-Type: text/xml; charset="utf-8"\r\n' + \
  4225. 'Server: Unspecified, UPnP/1.0, Unspecified\r\n' + \
  4226. 'Connection: close\r\n' + \
  4227. 'Content-Length: ' + str(len(payload)) + '\r\n' + \
  4228. 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
  4229. self.wfile.write(hdr + payload)
  4230. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_no_device, no_event_url=True)
  4231. def test_ap_wps_er_http_proto_upnp_info_no_device_type(dev, apdev):
  4232. """WPS ER HTTP protocol testing - No deviceType in UPnP info"""
  4233. class WPSAPHTTPServer_no_device(WPSAPHTTPServer):
  4234. def handle_upnp_info(self):
  4235. payload = '''<?xml version="1.0"?>
  4236. <root xmlns="urn:schemas-upnp-org:device-1-0">
  4237. <specVersion>
  4238. <major>1</major>
  4239. <minor>0</minor>
  4240. </specVersion>
  4241. <device>
  4242. </device>
  4243. </root>
  4244. '''
  4245. hdr = 'HTTP/1.1 200 OK\r\n' + \
  4246. 'Content-Type: text/xml; charset="utf-8"\r\n' + \
  4247. 'Server: Unspecified, UPnP/1.0, Unspecified\r\n' + \
  4248. 'Connection: close\r\n' + \
  4249. 'Content-Length: ' + str(len(payload)) + '\r\n' + \
  4250. 'Date: Sat, 15 Aug 2015 18:55:08 GMT\r\n\r\n'
  4251. self.wfile.write(hdr + payload)
  4252. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_no_device, no_event_url=True)
  4253. def test_ap_wps_er_http_proto_upnp_info_invalid_udn_uuid(dev, apdev):
  4254. """WPS ER HTTP protocol testing - Invalid UDN UUID"""
  4255. class WPSAPHTTPServer_invalid_udn_uuid(WPSAPHTTPServer):
  4256. def handle_upnp_info(self):
  4257. self.wfile.write(gen_upnp_info(udn='uuid:foo'))
  4258. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_invalid_udn_uuid)
  4259. def test_ap_wps_er_http_proto_no_control_url(dev, apdev):
  4260. """WPS ER HTTP protocol testing - no controlURL"""
  4261. class WPSAPHTTPServer_no_control_url(WPSAPHTTPServer):
  4262. def handle_upnp_info(self):
  4263. self.wfile.write(gen_upnp_info(controlURL=None))
  4264. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_no_control_url,
  4265. no_event_url=True)
  4266. def test_ap_wps_er_http_proto_control_url_dns(dev, apdev):
  4267. """WPS ER HTTP protocol testing - DNS name in controlURL"""
  4268. class WPSAPHTTPServer_control_url_dns(WPSAPHTTPServer):
  4269. def handle_upnp_info(self):
  4270. self.wfile.write(gen_upnp_info(controlURL='http://example.com/wps_control'))
  4271. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_control_url_dns,
  4272. no_event_url=True)
  4273. def test_ap_wps_http_timeout(dev, apdev):
  4274. """WPS AP/ER and HTTP timeout"""
  4275. try:
  4276. _test_ap_wps_http_timeout(dev, apdev)
  4277. finally:
  4278. dev[0].request("WPS_ER_STOP")
  4279. def _test_ap_wps_http_timeout(dev, apdev):
  4280. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  4281. add_ssdp_ap(apdev[0], ap_uuid)
  4282. location = ssdp_get_location(ap_uuid)
  4283. url = urlparse.urlparse(location)
  4284. addr = (url.hostname, url.port)
  4285. logger.debug("Open HTTP connection to hostapd, but do not complete request")
  4286. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
  4287. socket.IPPROTO_TCP)
  4288. sock.connect(addr)
  4289. sock.send("G")
  4290. class DummyServer(SocketServer.StreamRequestHandler):
  4291. def handle(self):
  4292. logger.debug("DummyServer - start 31 sec wait")
  4293. time.sleep(31)
  4294. logger.debug("DummyServer - wait done")
  4295. logger.debug("Start WPS ER")
  4296. server,sock2 = wps_er_start(dev[0], DummyServer, max_age=40,
  4297. wait_m_search=True)
  4298. logger.debug("Start server to accept, but not complete, HTTP connection from WPS ER")
  4299. # This will wait for 31 seconds..
  4300. server.handle_request()
  4301. logger.debug("Complete HTTP connection with hostapd (that should have already closed the connection)")
  4302. try:
  4303. sock.send("ET / HTTP/1.1\r\n\r\n")
  4304. res = sock.recv(100)
  4305. sock.close()
  4306. except:
  4307. pass
  4308. def test_ap_wps_er_url_parse(dev, apdev):
  4309. """WPS ER and URL parsing special cases"""
  4310. try:
  4311. _test_ap_wps_er_url_parse(dev, apdev)
  4312. finally:
  4313. dev[0].request("WPS_ER_STOP")
  4314. def _test_ap_wps_er_url_parse(dev, apdev):
  4315. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  4316. sock.settimeout(1)
  4317. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  4318. sock.bind(("239.255.255.250", 1900))
  4319. dev[0].request("WPS_ER_START ifname=lo")
  4320. (msg,addr) = sock.recvfrom(1000)
  4321. logger.debug("Received SSDP message from %s: %s" % (str(addr), msg))
  4322. if "M-SEARCH" not in msg:
  4323. raise Exception("Not an M-SEARCH")
  4324. 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)
  4325. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"], timeout=2)
  4326. 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)
  4327. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"], timeout=2)
  4328. 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)
  4329. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"], timeout=2)
  4330. sock.close()
  4331. def test_ap_wps_er_link_update(dev, apdev):
  4332. """WPS ER and link update special cases"""
  4333. class WPSAPHTTPServer_link_update(WPSAPHTTPServer):
  4334. def handle_upnp_info(self):
  4335. self.wfile.write(gen_upnp_info(controlURL='/wps_control'))
  4336. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_link_update)
  4337. class WPSAPHTTPServer_link_update2(WPSAPHTTPServer):
  4338. def handle_others(self, data):
  4339. if "GET / " in data:
  4340. self.wfile.write(gen_upnp_info(controlURL='/wps_control'))
  4341. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_link_update2,
  4342. location_url='http://127.0.0.1:12345')
  4343. def test_ap_wps_er_http_client(dev, apdev):
  4344. """WPS ER and HTTP client special cases"""
  4345. with alloc_fail(dev[0], 1, "http_link_update"):
  4346. run_wps_er_proto_test(dev[0], WPSAPHTTPServer)
  4347. with alloc_fail(dev[0], 1, "wpabuf_alloc;http_client_url"):
  4348. run_wps_er_proto_test(dev[0], WPSAPHTTPServer, no_event_url=True)
  4349. with alloc_fail(dev[0], 1, "httpread_create;http_client_tx_ready"):
  4350. run_wps_er_proto_test(dev[0], WPSAPHTTPServer, no_event_url=True)
  4351. class WPSAPHTTPServer_req_as_resp(WPSAPHTTPServer):
  4352. def handle_upnp_info(self):
  4353. self.wfile.write("GET / HTTP/1.1\r\n\r\n")
  4354. run_wps_er_proto_test(dev[0], WPSAPHTTPServer_req_as_resp,
  4355. no_event_url=True)
  4356. def test_ap_wps_init_oom(dev, apdev):
  4357. """wps_init OOM cases"""
  4358. ssid = "test-wps"
  4359. appin = "12345670"
  4360. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  4361. "ap_pin": appin }
  4362. hapd = hostapd.add_ap(apdev[0], params)
  4363. pin = dev[0].wps_read_pin()
  4364. with alloc_fail(hapd, 1, "wps_init"):
  4365. hapd.request("WPS_PIN any " + pin)
  4366. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4367. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4368. ev = hapd.wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4369. if ev is None:
  4370. raise Exception("No EAP failure reported")
  4371. dev[0].request("WPS_CANCEL")
  4372. with alloc_fail(dev[0], 2, "wps_init"):
  4373. hapd.request("WPS_PIN any " + pin)
  4374. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4375. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4376. ev = hapd.wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4377. if ev is None:
  4378. raise Exception("No EAP failure reported")
  4379. dev[0].request("WPS_CANCEL")
  4380. with alloc_fail(dev[0], 2, "wps_init"):
  4381. hapd.request("WPS_PBC")
  4382. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4383. dev[0].request("WPS_PBC %s" % (apdev[0]['bssid']))
  4384. ev = hapd.wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4385. if ev is None:
  4386. raise Exception("No EAP failure reported")
  4387. dev[0].request("WPS_CANCEL")
  4388. dev[0].dump_monitor()
  4389. new_ssid = "wps-new-ssid"
  4390. new_passphrase = "1234567890"
  4391. with alloc_fail(dev[0], 3, "wps_init"):
  4392. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
  4393. new_passphrase, no_wait=True)
  4394. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4395. if ev is None:
  4396. raise Exception("No EAP failure reported")
  4397. dev[0].flush_scan_cache()
  4398. @remote_compatible
  4399. def test_ap_wps_invalid_assoc_req_elem(dev, apdev):
  4400. """WPS and invalid IE in Association Request frame"""
  4401. ssid = "test-wps"
  4402. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2" }
  4403. hapd = hostapd.add_ap(apdev[0], params)
  4404. pin = "12345670"
  4405. hapd.request("WPS_PIN any " + pin)
  4406. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4407. try:
  4408. dev[0].request("VENDOR_ELEM_ADD 13 dd050050f20410")
  4409. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4410. for i in range(5):
  4411. ev = hapd.wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=10)
  4412. if ev and "vendor=14122" in ev:
  4413. break
  4414. if ev is None or "vendor=14122" not in ev:
  4415. raise Exception("EAP-WSC not started")
  4416. dev[0].request("WPS_CANCEL")
  4417. finally:
  4418. dev[0].request("VENDOR_ELEM_REMOVE 13 *")
  4419. def test_ap_wps_pbc_pin_mismatch(dev, apdev):
  4420. """WPS PBC/PIN mismatch"""
  4421. ssid = "test-wps"
  4422. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2" }
  4423. hapd = hostapd.add_ap(apdev[0], params)
  4424. hapd.request("SET wps_version_number 0x10")
  4425. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4426. hapd.request("WPS_PBC")
  4427. pin = dev[0].wps_read_pin()
  4428. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4429. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  4430. if ev is None:
  4431. raise Exception("Scan did not complete")
  4432. dev[0].request("WPS_CANCEL")
  4433. hapd.request("WPS_CANCEL")
  4434. dev[0].flush_scan_cache()
  4435. @remote_compatible
  4436. def test_ap_wps_ie_invalid(dev, apdev):
  4437. """WPS PIN attempt with AP that has invalid WSC IE"""
  4438. ssid = "test-wps"
  4439. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  4440. "vendor_elements": "dd050050f20410" }
  4441. hapd = hostapd.add_ap(apdev[0], params)
  4442. params = { 'ssid': "another", "vendor_elements": "dd050050f20410" }
  4443. hostapd.add_ap(apdev[1], params)
  4444. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4445. pin = dev[0].wps_read_pin()
  4446. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4447. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  4448. if ev is None:
  4449. raise Exception("Scan did not complete")
  4450. dev[0].request("WPS_CANCEL")
  4451. @remote_compatible
  4452. def test_ap_wps_scan_prio_order(dev, apdev):
  4453. """WPS scan priority ordering"""
  4454. ssid = "test-wps"
  4455. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2" }
  4456. hapd = hostapd.add_ap(apdev[0], params)
  4457. params = { 'ssid': "another", "vendor_elements": "dd050050f20410" }
  4458. hostapd.add_ap(apdev[1], params)
  4459. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4460. dev[0].scan_for_bss(apdev[1]['bssid'], freq="2412")
  4461. pin = dev[0].wps_read_pin()
  4462. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4463. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  4464. if ev is None:
  4465. raise Exception("Scan did not complete")
  4466. dev[0].request("WPS_CANCEL")
  4467. def test_ap_wps_probe_req_ie_oom(dev, apdev):
  4468. """WPS ProbeReq IE OOM"""
  4469. ssid = "test-wps"
  4470. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2" }
  4471. hapd = hostapd.add_ap(apdev[0], params)
  4472. pin = dev[0].wps_read_pin()
  4473. hapd.request("WPS_PIN any " + pin)
  4474. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4475. with alloc_fail(dev[0], 1, "wps_build_probe_req_ie"):
  4476. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4477. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=10)
  4478. if ev is None:
  4479. raise Exception("Association not seen")
  4480. dev[0].request("WPS_CANCEL")
  4481. dev[0].wait_disconnected()
  4482. with alloc_fail(dev[0], 1, "wps_ie_encapsulate"):
  4483. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4484. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=10)
  4485. if ev is None:
  4486. raise Exception("Association not seen")
  4487. dev[0].request("WPS_CANCEL")
  4488. hapd.disable()
  4489. dev[0].request("REMOVE_NETWORK all")
  4490. dev[0].wait_disconnected()
  4491. time.sleep(0.2)
  4492. dev[0].flush_scan_cache()
  4493. def test_ap_wps_assoc_req_ie_oom(dev, apdev):
  4494. """WPS AssocReq IE OOM"""
  4495. ssid = "test-wps"
  4496. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2" }
  4497. hapd = hostapd.add_ap(apdev[0], params)
  4498. pin = dev[0].wps_read_pin()
  4499. hapd.request("WPS_PIN any " + pin)
  4500. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4501. with alloc_fail(dev[0], 1, "wps_build_assoc_req_ie"):
  4502. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4503. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=10)
  4504. if ev is None:
  4505. raise Exception("Association not seen")
  4506. dev[0].request("WPS_CANCEL")
  4507. def test_ap_wps_assoc_resp_ie_oom(dev, apdev):
  4508. """WPS AssocResp IE OOM"""
  4509. ssid = "test-wps"
  4510. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2" }
  4511. hapd = hostapd.add_ap(apdev[0], params)
  4512. pin = dev[0].wps_read_pin()
  4513. hapd.request("WPS_PIN any " + pin)
  4514. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4515. with alloc_fail(hapd, 1, "wps_build_assoc_resp_ie"):
  4516. dev[0].request("WPS_PIN %s %s" % (apdev[0]['bssid'], pin))
  4517. ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=10)
  4518. if ev is None:
  4519. raise Exception("Association not seen")
  4520. dev[0].request("WPS_CANCEL")
  4521. @remote_compatible
  4522. def test_ap_wps_bss_info_errors(dev, apdev):
  4523. """WPS BSS info errors"""
  4524. params = { "ssid": "1",
  4525. "vendor_elements": "dd0e0050f20410440001ff101100010a" }
  4526. hostapd.add_ap(apdev[0], params)
  4527. params = { 'ssid': "2", "vendor_elements": "dd050050f20410" }
  4528. hostapd.add_ap(apdev[1], params)
  4529. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4530. dev[0].scan_for_bss(apdev[1]['bssid'], freq="2412")
  4531. bss = dev[0].get_bss(apdev[0]['bssid'])
  4532. logger.info("BSS: " + str(bss))
  4533. if "wps_state" in bss:
  4534. raise Exception("Unexpected wps_state in BSS info")
  4535. if 'wps_device_name' not in bss:
  4536. raise Exception("No wps_device_name in BSS info")
  4537. if bss['wps_device_name'] != '_':
  4538. raise Exception("Unexpected wps_device_name value")
  4539. bss = dev[0].get_bss(apdev[1]['bssid'])
  4540. logger.info("BSS: " + str(bss))
  4541. with alloc_fail(dev[0], 1, "=wps_attr_text"):
  4542. bss = dev[0].get_bss(apdev[0]['bssid'])
  4543. logger.info("BSS(OOM): " + str(bss))
  4544. def wps_run_pbc_fail_ap(apdev, dev, hapd):
  4545. hapd.request("WPS_PBC")
  4546. dev.scan_for_bss(apdev['bssid'], freq="2412")
  4547. dev.request("WPS_PBC " + apdev['bssid'])
  4548. ev = dev.wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4549. if ev is None:
  4550. raise Exception("No EAP failure reported")
  4551. dev.request("WPS_CANCEL")
  4552. dev.wait_disconnected()
  4553. for i in range(5):
  4554. try:
  4555. dev.flush_scan_cache()
  4556. break
  4557. except Exception, e:
  4558. if str(e).startswith("Failed to trigger scan"):
  4559. # Try again
  4560. time.sleep(1)
  4561. else:
  4562. raise
  4563. def wps_run_pbc_fail(apdev, dev):
  4564. hapd = wps_start_ap(apdev)
  4565. wps_run_pbc_fail_ap(apdev, dev, hapd)
  4566. @remote_compatible
  4567. def test_ap_wps_pk_oom(dev, apdev):
  4568. """WPS and public key OOM"""
  4569. with alloc_fail(dev[0], 1, "wps_build_public_key"):
  4570. wps_run_pbc_fail(apdev[0], dev[0])
  4571. @remote_compatible
  4572. def test_ap_wps_pk_oom_ap(dev, apdev):
  4573. """WPS and public key OOM on AP"""
  4574. hapd = wps_start_ap(apdev[0])
  4575. with alloc_fail(hapd, 1, "wps_build_public_key"):
  4576. wps_run_pbc_fail_ap(apdev[0], dev[0], hapd)
  4577. @remote_compatible
  4578. def test_ap_wps_encr_oom_ap(dev, apdev):
  4579. """WPS and encrypted settings decryption OOM on AP"""
  4580. hapd = wps_start_ap(apdev[0])
  4581. pin = dev[0].wps_read_pin()
  4582. hapd.request("WPS_PIN any " + pin)
  4583. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4584. with alloc_fail(hapd, 1, "wps_decrypt_encr_settings"):
  4585. dev[0].request("WPS_PIN " + apdev[0]['bssid'] + " " + pin)
  4586. ev = hapd.wait_event(["WPS-FAIL"], timeout=10)
  4587. if ev is None:
  4588. raise Exception("No WPS-FAIL reported")
  4589. dev[0].request("WPS_CANCEL")
  4590. dev[0].wait_disconnected()
  4591. @remote_compatible
  4592. def test_ap_wps_encr_no_random_ap(dev, apdev):
  4593. """WPS and no random data available for encryption on AP"""
  4594. hapd = wps_start_ap(apdev[0])
  4595. with fail_test(hapd, 1, "os_get_random;wps_build_encr_settings"):
  4596. wps_run_pbc_fail_ap(apdev[0], dev[0], hapd)
  4597. @remote_compatible
  4598. def test_ap_wps_e_hash_no_random_sta(dev, apdev):
  4599. """WPS and no random data available for e-hash on STA"""
  4600. with fail_test(dev[0], 1, "os_get_random;wps_build_e_hash"):
  4601. wps_run_pbc_fail(apdev[0], dev[0])
  4602. @remote_compatible
  4603. def test_ap_wps_m1_no_random(dev, apdev):
  4604. """WPS and no random for M1 on STA"""
  4605. with fail_test(dev[0], 1, "os_get_random;wps_build_m1"):
  4606. wps_run_pbc_fail(apdev[0], dev[0])
  4607. @remote_compatible
  4608. def test_ap_wps_m1_oom(dev, apdev):
  4609. """WPS and OOM for M1 on STA"""
  4610. with alloc_fail(dev[0], 1, "wps_build_m1"):
  4611. wps_run_pbc_fail(apdev[0], dev[0])
  4612. @remote_compatible
  4613. def test_ap_wps_m3_oom(dev, apdev):
  4614. """WPS and OOM for M3 on STA"""
  4615. with alloc_fail(dev[0], 1, "wps_build_m3"):
  4616. wps_run_pbc_fail(apdev[0], dev[0])
  4617. @remote_compatible
  4618. def test_ap_wps_m5_oom(dev, apdev):
  4619. """WPS and OOM for M5 on STA"""
  4620. hapd = wps_start_ap(apdev[0])
  4621. hapd.request("WPS_PBC")
  4622. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4623. for i in range(1, 3):
  4624. with alloc_fail(dev[0], i, "wps_build_m5"):
  4625. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  4626. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4627. if ev is None:
  4628. raise Exception("No EAP failure reported")
  4629. dev[0].request("WPS_CANCEL")
  4630. dev[0].wait_disconnected()
  4631. dev[0].flush_scan_cache()
  4632. @remote_compatible
  4633. def test_ap_wps_m5_no_random(dev, apdev):
  4634. """WPS and no random for M5 on STA"""
  4635. with fail_test(dev[0], 1,
  4636. "os_get_random;wps_build_encr_settings;wps_build_m5"):
  4637. wps_run_pbc_fail(apdev[0], dev[0])
  4638. @remote_compatible
  4639. def test_ap_wps_m7_oom(dev, apdev):
  4640. """WPS and OOM for M7 on STA"""
  4641. hapd = wps_start_ap(apdev[0])
  4642. hapd.request("WPS_PBC")
  4643. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4644. for i in range(1, 3):
  4645. with alloc_fail(dev[0], i, "wps_build_m7"):
  4646. dev[0].request("WPS_PBC " + apdev[0]['bssid'])
  4647. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4648. if ev is None:
  4649. raise Exception("No EAP failure reported")
  4650. dev[0].request("WPS_CANCEL")
  4651. dev[0].wait_disconnected()
  4652. dev[0].flush_scan_cache()
  4653. @remote_compatible
  4654. def test_ap_wps_m7_no_random(dev, apdev):
  4655. """WPS and no random for M7 on STA"""
  4656. with fail_test(dev[0], 1,
  4657. "os_get_random;wps_build_encr_settings;wps_build_m7"):
  4658. wps_run_pbc_fail(apdev[0], dev[0])
  4659. @remote_compatible
  4660. def test_ap_wps_wsc_done_oom(dev, apdev):
  4661. """WPS and OOM for WSC_Done on STA"""
  4662. with alloc_fail(dev[0], 1, "wps_build_wsc_done"):
  4663. wps_run_pbc_fail(apdev[0], dev[0])
  4664. def test_ap_wps_random_psk_fail(dev, apdev):
  4665. """WPS and no random for PSK on AP"""
  4666. ssid = "test-wps"
  4667. pskfile = "/tmp/ap_wps_per_enrollee_psk.psk_file"
  4668. appin = "12345670"
  4669. try:
  4670. os.remove(pskfile)
  4671. except:
  4672. pass
  4673. try:
  4674. with open(pskfile, "w") as f:
  4675. f.write("# WPA PSKs\n")
  4676. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  4677. "wpa": "2", "wpa_key_mgmt": "WPA-PSK",
  4678. "rsn_pairwise": "CCMP", "ap_pin": appin,
  4679. "wpa_psk_file": pskfile }
  4680. hapd = hostapd.add_ap(apdev[0], params)
  4681. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  4682. with fail_test(hapd, 1, "os_get_random;wps_build_cred_network_key"):
  4683. dev[0].request("WPS_REG " + apdev[0]['bssid'] + " " + appin)
  4684. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=10)
  4685. if ev is None:
  4686. raise Exception("No EAP failure reported")
  4687. dev[0].request("WPS_CANCEL")
  4688. dev[0].wait_disconnected()
  4689. with fail_test(hapd, 1, "os_get_random;wps_build_cred"):
  4690. wps_run_pbc_fail_ap(apdev[0], dev[0], hapd)
  4691. with alloc_fail(hapd, 1, "wps_build_cred"):
  4692. wps_run_pbc_fail_ap(apdev[0], dev[0], hapd)
  4693. with alloc_fail(hapd, 2, "wps_build_cred"):
  4694. wps_run_pbc_fail_ap(apdev[0], dev[0], hapd)
  4695. finally:
  4696. os.remove(pskfile)
  4697. def wps_ext_eap_identity_req(dev, hapd, bssid):
  4698. logger.debug("EAP-Identity/Request")
  4699. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  4700. if ev is None:
  4701. raise Exception("Timeout on EAPOL-TX from hostapd")
  4702. res = dev.request("EAPOL_RX " + bssid + " " + ev.split(' ')[2])
  4703. if "OK" not in res:
  4704. raise Exception("EAPOL_RX to wpa_supplicant failed")
  4705. def wps_ext_eap_identity_resp(hapd, dev, addr):
  4706. ev = dev.wait_event(["EAPOL-TX"], timeout=10)
  4707. if ev is None:
  4708. raise Exception("Timeout on EAPOL-TX from wpa_supplicant")
  4709. res = hapd.request("EAPOL_RX " + addr + " " + ev.split(' ')[2])
  4710. if "OK" not in res:
  4711. raise Exception("EAPOL_RX to hostapd failed")
  4712. def wps_ext_eap_wsc(dst, src, src_addr, msg):
  4713. logger.debug(msg)
  4714. ev = src.wait_event(["EAPOL-TX"], timeout=10)
  4715. if ev is None:
  4716. raise Exception("Timeout on EAPOL-TX")
  4717. res = dst.request("EAPOL_RX " + src_addr + " " + ev.split(' ')[2])
  4718. if "OK" not in res:
  4719. raise Exception("EAPOL_RX failed")
  4720. def wps_start_ext(apdev, dev, pbc=False, pin=None):
  4721. addr = dev.own_addr()
  4722. bssid = apdev['bssid']
  4723. ssid = "test-wps-conf"
  4724. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  4725. "wpa_passphrase": "12345678", "wpa": "2",
  4726. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"}
  4727. hapd = hostapd.add_ap(apdev, params)
  4728. if pbc:
  4729. hapd.request("WPS_PBC")
  4730. else:
  4731. if pin is None:
  4732. pin = dev.wps_read_pin()
  4733. hapd.request("WPS_PIN any " + pin)
  4734. dev.scan_for_bss(bssid, freq="2412")
  4735. hapd.request("SET ext_eapol_frame_io 1")
  4736. dev.request("SET ext_eapol_frame_io 1")
  4737. if pbc:
  4738. dev.request("WPS_PBC " + bssid)
  4739. else:
  4740. dev.request("WPS_PIN " + bssid + " " + pin)
  4741. return addr,bssid,hapd
  4742. def wps_auth_corrupt(dst, src, addr):
  4743. ev = src.wait_event(["EAPOL-TX"], timeout=10)
  4744. if ev is None:
  4745. raise Exception("Timeout on EAPOL-TX")
  4746. src.request("SET ext_eapol_frame_io 0")
  4747. dst.request("SET ext_eapol_frame_io 0")
  4748. msg = ev.split(' ')[2]
  4749. if msg[-24:-16] != '10050008':
  4750. raise Exception("Could not find Authenticator attribute")
  4751. # Corrupt Authenticator value
  4752. msg = msg[:-1] + '%x' % ((int(msg[-1], 16) + 1) % 16)
  4753. res = dst.request("EAPOL_RX " + addr + " " + msg)
  4754. if "OK" not in res:
  4755. raise Exception("EAPOL_RX failed")
  4756. def wps_fail_finish(hapd, dev, fail_str):
  4757. ev = hapd.wait_event(["WPS-FAIL"], timeout=5)
  4758. if ev is None:
  4759. raise Exception("WPS-FAIL not indicated")
  4760. if fail_str not in ev:
  4761. raise Exception("Unexpected WPS-FAIL value: " + ev)
  4762. dev.request("WPS_CANCEL")
  4763. dev.wait_disconnected()
  4764. def wps_auth_corrupt_from_ap(dev, hapd, bssid, fail_str):
  4765. wps_auth_corrupt(dev, hapd, bssid)
  4766. wps_fail_finish(hapd, dev, fail_str)
  4767. def wps_auth_corrupt_to_ap(dev, hapd, addr, fail_str):
  4768. wps_auth_corrupt(hapd, dev, addr)
  4769. wps_fail_finish(hapd, dev, fail_str)
  4770. def test_ap_wps_authenticator_mismatch_m2(dev, apdev):
  4771. """WPS and Authenticator attribute mismatch in M2"""
  4772. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4773. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4774. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4775. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4776. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4777. logger.debug("M2")
  4778. wps_auth_corrupt_from_ap(dev[0], hapd, bssid, "msg=5")
  4779. def test_ap_wps_authenticator_mismatch_m3(dev, apdev):
  4780. """WPS and Authenticator attribute mismatch in M3"""
  4781. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4782. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4783. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4784. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4785. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4786. wps_ext_eap_wsc(dev[0], hapd, bssid, "M2")
  4787. logger.debug("M3")
  4788. wps_auth_corrupt_to_ap(dev[0], hapd, addr, "msg=7")
  4789. def test_ap_wps_authenticator_mismatch_m4(dev, apdev):
  4790. """WPS and Authenticator attribute mismatch in M4"""
  4791. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4792. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4793. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4794. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4795. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4796. wps_ext_eap_wsc(dev[0], hapd, bssid, "M2")
  4797. wps_ext_eap_wsc(hapd, dev[0], addr, "M3")
  4798. logger.debug("M4")
  4799. wps_auth_corrupt_from_ap(dev[0], hapd, bssid, "msg=8")
  4800. def test_ap_wps_authenticator_mismatch_m5(dev, apdev):
  4801. """WPS and Authenticator attribute mismatch in M5"""
  4802. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4803. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4804. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4805. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4806. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4807. wps_ext_eap_wsc(dev[0], hapd, bssid, "M2")
  4808. wps_ext_eap_wsc(hapd, dev[0], addr, "M3")
  4809. wps_ext_eap_wsc(dev[0], hapd, bssid, "M4")
  4810. logger.debug("M5")
  4811. wps_auth_corrupt_to_ap(dev[0], hapd, addr, "msg=9")
  4812. def test_ap_wps_authenticator_mismatch_m6(dev, apdev):
  4813. """WPS and Authenticator attribute mismatch in M6"""
  4814. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4815. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4816. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4817. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4818. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4819. wps_ext_eap_wsc(dev[0], hapd, bssid, "M2")
  4820. wps_ext_eap_wsc(hapd, dev[0], addr, "M3")
  4821. wps_ext_eap_wsc(dev[0], hapd, bssid, "M4")
  4822. wps_ext_eap_wsc(hapd, dev[0], addr, "M5")
  4823. logger.debug("M6")
  4824. wps_auth_corrupt_from_ap(dev[0], hapd, bssid, "msg=10")
  4825. def test_ap_wps_authenticator_mismatch_m7(dev, apdev):
  4826. """WPS and Authenticator attribute mismatch in M7"""
  4827. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4828. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4829. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4830. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4831. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4832. wps_ext_eap_wsc(dev[0], hapd, bssid, "M2")
  4833. wps_ext_eap_wsc(hapd, dev[0], addr, "M3")
  4834. wps_ext_eap_wsc(dev[0], hapd, bssid, "M4")
  4835. wps_ext_eap_wsc(hapd, dev[0], addr, "M5")
  4836. wps_ext_eap_wsc(dev[0], hapd, bssid, "M6")
  4837. logger.debug("M7")
  4838. wps_auth_corrupt_to_ap(dev[0], hapd, addr, "msg=11")
  4839. def test_ap_wps_authenticator_mismatch_m8(dev, apdev):
  4840. """WPS and Authenticator attribute mismatch in M8"""
  4841. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4842. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4843. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4844. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4845. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4846. wps_ext_eap_wsc(dev[0], hapd, bssid, "M2")
  4847. wps_ext_eap_wsc(hapd, dev[0], addr, "M3")
  4848. wps_ext_eap_wsc(dev[0], hapd, bssid, "M4")
  4849. wps_ext_eap_wsc(hapd, dev[0], addr, "M5")
  4850. wps_ext_eap_wsc(dev[0], hapd, bssid, "M6")
  4851. wps_ext_eap_wsc(hapd, dev[0], addr, "M7")
  4852. logger.debug("M8")
  4853. wps_auth_corrupt_from_ap(dev[0], hapd, bssid, "msg=12")
  4854. def test_ap_wps_authenticator_missing_m2(dev, apdev):
  4855. """WPS and Authenticator attribute missing from M2"""
  4856. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4857. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4858. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4859. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4860. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4861. logger.debug("M2")
  4862. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  4863. if ev is None:
  4864. raise Exception("Timeout on EAPOL-TX")
  4865. hapd.request("SET ext_eapol_frame_io 0")
  4866. dev[0].request("SET ext_eapol_frame_io 0")
  4867. msg = ev.split(' ')[2]
  4868. if msg[-24:-16] != '10050008':
  4869. raise Exception("Could not find Authenticator attribute")
  4870. # Remove Authenticator value
  4871. msg = msg[:-24]
  4872. mlen = "%04x" % (int(msg[4:8], 16) - 12)
  4873. msg = msg[0:4] + mlen + msg[8:12] + mlen + msg[16:]
  4874. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  4875. if "OK" not in res:
  4876. raise Exception("EAPOL_RX failed")
  4877. wps_fail_finish(hapd, dev[0], "msg=5")
  4878. def test_ap_wps_m2_dev_passwd_id_p2p(dev, apdev):
  4879. """WPS and M2 with different Device Password ID (P2P)"""
  4880. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4881. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4882. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4883. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4884. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4885. logger.debug("M2")
  4886. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  4887. if ev is None:
  4888. raise Exception("Timeout on EAPOL-TX")
  4889. hapd.request("SET ext_eapol_frame_io 0")
  4890. dev[0].request("SET ext_eapol_frame_io 0")
  4891. msg = ev.split(' ')[2]
  4892. if msg[722:730] != '10120002':
  4893. raise Exception("Could not find Device Password ID attribute")
  4894. # Replace Device Password ID value. This will fail Authenticator check, but
  4895. # allows the code path in wps_process_dev_pw_id() to be checked from debug
  4896. # log.
  4897. msg = msg[0:730] + "0005" + msg[734:]
  4898. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  4899. if "OK" not in res:
  4900. raise Exception("EAPOL_RX failed")
  4901. wps_fail_finish(hapd, dev[0], "msg=5")
  4902. def test_ap_wps_m2_dev_passwd_id_change_pin_to_pbc(dev, apdev):
  4903. """WPS and M2 with different Device Password ID (PIN to PBC)"""
  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[722:730] != '10120002':
  4917. raise Exception("Could not find Device Password ID attribute")
  4918. # Replace Device Password ID value (PIN --> PBC). This will be rejected.
  4919. msg = msg[0:730] + "0004" + msg[734:]
  4920. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  4921. if "OK" not in res:
  4922. raise Exception("EAPOL_RX failed")
  4923. wps_fail_finish(hapd, dev[0], "msg=5")
  4924. def test_ap_wps_m2_dev_passwd_id_change_pbc_to_pin(dev, apdev):
  4925. """WPS and M2 with different Device Password ID (PBC to PIN)"""
  4926. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  4927. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4928. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4929. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4930. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4931. logger.debug("M2")
  4932. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  4933. if ev is None:
  4934. raise Exception("Timeout on EAPOL-TX")
  4935. hapd.request("SET ext_eapol_frame_io 0")
  4936. dev[0].request("SET ext_eapol_frame_io 0")
  4937. msg = ev.split(' ')[2]
  4938. if msg[722:730] != '10120002':
  4939. raise Exception("Could not find Device Password ID attribute")
  4940. # Replace Device Password ID value. This will fail Authenticator check, but
  4941. # allows the code path in wps_process_dev_pw_id() to be checked from debug
  4942. # log.
  4943. msg = msg[0:730] + "0000" + msg[734:]
  4944. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  4945. if "OK" not in res:
  4946. raise Exception("EAPOL_RX failed")
  4947. wps_fail_finish(hapd, dev[0], "msg=5")
  4948. dev[0].flush_scan_cache()
  4949. def test_ap_wps_m2_missing_dev_passwd_id(dev, apdev):
  4950. """WPS and M2 without Device Password ID"""
  4951. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0])
  4952. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4953. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4954. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4955. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4956. logger.debug("M2")
  4957. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  4958. if ev is None:
  4959. raise Exception("Timeout on EAPOL-TX")
  4960. hapd.request("SET ext_eapol_frame_io 0")
  4961. dev[0].request("SET ext_eapol_frame_io 0")
  4962. msg = ev.split(' ')[2]
  4963. if msg[722:730] != '10120002':
  4964. raise Exception("Could not find Device Password ID attribute")
  4965. # Remove Device Password ID value. This will fail Authenticator check, but
  4966. # allows the code path in wps_process_dev_pw_id() to be checked from debug
  4967. # log.
  4968. mlen = "%04x" % (int(msg[4:8], 16) - 6)
  4969. msg = msg[0:4] + mlen + msg[8:12] + mlen + msg[16:722] + msg[734:]
  4970. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  4971. if "OK" not in res:
  4972. raise Exception("EAPOL_RX failed")
  4973. wps_fail_finish(hapd, dev[0], "msg=5")
  4974. def test_ap_wps_m2_missing_registrar_nonce(dev, apdev):
  4975. """WPS and M2 without Registrar Nonce"""
  4976. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  4977. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  4978. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  4979. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  4980. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  4981. logger.debug("M2")
  4982. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  4983. if ev is None:
  4984. raise Exception("Timeout on EAPOL-TX")
  4985. hapd.request("SET ext_eapol_frame_io 0")
  4986. dev[0].request("SET ext_eapol_frame_io 0")
  4987. msg = ev.split(' ')[2]
  4988. if msg[96:104] != '10390010':
  4989. raise Exception("Could not find Registrar Nonce attribute")
  4990. # Remove Registrar Nonce. This will fail Authenticator check, but
  4991. # allows the code path in wps_process_registrar_nonce() to be checked from
  4992. # the debug log.
  4993. mlen = "%04x" % (int(msg[4:8], 16) - 20)
  4994. msg = msg[0:4] + mlen + msg[8:12] + mlen + msg[16:96] + msg[136:]
  4995. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  4996. if "OK" not in res:
  4997. raise Exception("EAPOL_RX failed")
  4998. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECT"], timeout=5)
  4999. if ev is None:
  5000. raise Exception("Disconnect event not seen")
  5001. dev[0].request("WPS_CANCEL")
  5002. dev[0].flush_scan_cache()
  5003. def test_ap_wps_m2_missing_enrollee_nonce(dev, apdev):
  5004. """WPS and M2 without Enrollee Nonce"""
  5005. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5006. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5007. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5008. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5009. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5010. logger.debug("M2")
  5011. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5012. if ev is None:
  5013. raise Exception("Timeout on EAPOL-TX")
  5014. hapd.request("SET ext_eapol_frame_io 0")
  5015. dev[0].request("SET ext_eapol_frame_io 0")
  5016. msg = ev.split(' ')[2]
  5017. if msg[56:64] != '101a0010':
  5018. raise Exception("Could not find enrollee Nonce attribute")
  5019. # Remove Enrollee Nonce. This will fail Authenticator check, but
  5020. # allows the code path in wps_process_enrollee_nonce() to be checked from
  5021. # the debug log.
  5022. mlen = "%04x" % (int(msg[4:8], 16) - 20)
  5023. msg = msg[0:4] + mlen + msg[8:12] + mlen + msg[16:56] + msg[96:]
  5024. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5025. if "OK" not in res:
  5026. raise Exception("EAPOL_RX failed")
  5027. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECT"], timeout=5)
  5028. if ev is None:
  5029. raise Exception("Disconnect event not seen")
  5030. dev[0].request("WPS_CANCEL")
  5031. dev[0].flush_scan_cache()
  5032. def test_ap_wps_m2_missing_uuid_r(dev, apdev):
  5033. """WPS and M2 without UUID-R"""
  5034. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5035. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5036. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5037. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5038. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5039. logger.debug("M2")
  5040. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5041. if ev is None:
  5042. raise Exception("Timeout on EAPOL-TX")
  5043. hapd.request("SET ext_eapol_frame_io 0")
  5044. dev[0].request("SET ext_eapol_frame_io 0")
  5045. msg = ev.split(' ')[2]
  5046. if msg[136:144] != '10480010':
  5047. raise Exception("Could not find enrollee Nonce attribute")
  5048. # Remove UUID-R. This will fail Authenticator check, but allows the code
  5049. # path in wps_process_uuid_r() to be checked from the debug log.
  5050. mlen = "%04x" % (int(msg[4:8], 16) - 20)
  5051. msg = msg[0:4] + mlen + msg[8:12] + mlen + msg[16:136] + msg[176:]
  5052. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5053. if "OK" not in res:
  5054. raise Exception("EAPOL_RX failed")
  5055. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECT"], timeout=5)
  5056. if ev is None:
  5057. raise Exception("Disconnect event not seen")
  5058. dev[0].request("WPS_CANCEL")
  5059. dev[0].flush_scan_cache()
  5060. def test_ap_wps_m2_invalid(dev, apdev):
  5061. """WPS and M2 parsing failure"""
  5062. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5063. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5064. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5065. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5066. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5067. logger.debug("M2")
  5068. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5069. if ev is None:
  5070. raise Exception("Timeout on EAPOL-TX")
  5071. hapd.request("SET ext_eapol_frame_io 0")
  5072. dev[0].request("SET ext_eapol_frame_io 0")
  5073. msg = ev.split(' ')[2]
  5074. if msg[136:144] != '10480010':
  5075. raise Exception("Could not find enrollee Nonce attribute")
  5076. # Remove UUID-R. This will fail Authenticator check, but allows the code
  5077. # path in wps_process_uuid_r() to be checked from the debug log.
  5078. mlen = "%04x" % (int(msg[4:8], 16) - 1)
  5079. msg = msg[0:4] + mlen + msg[8:12] + mlen + msg[16:-2]
  5080. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5081. if "OK" not in res:
  5082. raise Exception("EAPOL_RX failed")
  5083. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECT"], timeout=5)
  5084. if ev is None:
  5085. raise Exception("Disconnect event not seen")
  5086. dev[0].request("WPS_CANCEL")
  5087. dev[0].flush_scan_cache()
  5088. def test_ap_wps_m2_missing_msg_type(dev, apdev):
  5089. """WPS and M2 without Message Type"""
  5090. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5091. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5092. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5093. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5094. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5095. logger.debug("M2")
  5096. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5097. if ev is None:
  5098. raise Exception("Timeout on EAPOL-TX")
  5099. hapd.request("SET ext_eapol_frame_io 0")
  5100. dev[0].request("SET ext_eapol_frame_io 0")
  5101. msg = ev.split(' ')[2]
  5102. if msg[46:54] != '10220001':
  5103. raise Exception("Could not find Message Type attribute")
  5104. # Remove Message Type. This will fail Authenticator check, but allows the
  5105. # code path in wps_process_wsc_msg() to be checked from the debug log.
  5106. mlen = "%04x" % (int(msg[4:8], 16) - 5)
  5107. msg = msg[0:4] + mlen + msg[8:12] + mlen + msg[16:46] + msg[56:]
  5108. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5109. if "OK" not in res:
  5110. raise Exception("EAPOL_RX failed")
  5111. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECT"], timeout=5)
  5112. if ev is None:
  5113. raise Exception("Disconnect event not seen")
  5114. dev[0].request("WPS_CANCEL")
  5115. dev[0].flush_scan_cache()
  5116. def test_ap_wps_m2_unknown_msg_type(dev, apdev):
  5117. """WPS and M2 but unknown Message Type"""
  5118. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5119. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5120. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5121. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5122. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5123. logger.debug("M2")
  5124. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5125. if ev is None:
  5126. raise Exception("Timeout on EAPOL-TX")
  5127. hapd.request("SET ext_eapol_frame_io 0")
  5128. dev[0].request("SET ext_eapol_frame_io 0")
  5129. msg = ev.split(' ')[2]
  5130. if msg[46:54] != '10220001':
  5131. raise Exception("Could not find Message Type attribute")
  5132. # Replace Message Type value. This will be rejected.
  5133. msg = msg[0:54] + "00" + msg[56:]
  5134. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5135. if "OK" not in res:
  5136. raise Exception("EAPOL_RX failed")
  5137. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECT"], timeout=5)
  5138. if ev is None:
  5139. raise Exception("Disconnect event not seen")
  5140. dev[0].request("WPS_CANCEL")
  5141. dev[0].flush_scan_cache()
  5142. def test_ap_wps_m2_unknown_opcode(dev, apdev):
  5143. """WPS and M2 but unknown opcode"""
  5144. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5145. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5146. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5147. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5148. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5149. logger.debug("M2")
  5150. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5151. if ev is None:
  5152. raise Exception("Timeout on EAPOL-TX")
  5153. hapd.request("SET ext_eapol_frame_io 0")
  5154. dev[0].request("SET ext_eapol_frame_io 0")
  5155. msg = ev.split(' ')[2]
  5156. # Replace opcode. This will be discarded in EAP-WSC processing.
  5157. msg = msg[0:32] + "00" + msg[34:]
  5158. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5159. if "OK" not in res:
  5160. raise Exception("EAPOL_RX failed")
  5161. dev[0].request("WPS_CANCEL")
  5162. dev[0].wait_disconnected()
  5163. dev[0].flush_scan_cache()
  5164. def test_ap_wps_m2_unknown_opcode2(dev, apdev):
  5165. """WPS and M2 but unknown opcode (WSC_Start)"""
  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. # Replace opcode. This will be discarded in EAP-WSC processing.
  5179. msg = msg[0:32] + "01" + msg[34:]
  5180. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5181. if "OK" not in res:
  5182. raise Exception("EAPOL_RX failed")
  5183. dev[0].request("WPS_CANCEL")
  5184. dev[0].wait_disconnected()
  5185. dev[0].flush_scan_cache()
  5186. def test_ap_wps_m2_unknown_opcode3(dev, apdev):
  5187. """WPS and M2 but unknown opcode (WSC_Done)"""
  5188. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  5189. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5190. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5191. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5192. wps_ext_eap_wsc(hapd, dev[0], addr, "M1")
  5193. logger.debug("M2")
  5194. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5195. if ev is None:
  5196. raise Exception("Timeout on EAPOL-TX")
  5197. hapd.request("SET ext_eapol_frame_io 0")
  5198. dev[0].request("SET ext_eapol_frame_io 0")
  5199. msg = ev.split(' ')[2]
  5200. # Replace opcode. This will be discarded in WPS Enrollee processing.
  5201. msg = msg[0:32] + "05" + msg[34:]
  5202. res = dev[0].request("EAPOL_RX " + bssid + " " + msg)
  5203. if "OK" not in res:
  5204. raise Exception("EAPOL_RX failed")
  5205. dev[0].request("WPS_CANCEL")
  5206. dev[0].wait_disconnected()
  5207. dev[0].flush_scan_cache()
  5208. def wps_m2_but_other(dev, apdev, title, msgtype):
  5209. addr,bssid,hapd = wps_start_ext(apdev, dev)
  5210. wps_ext_eap_identity_req(dev, hapd, bssid)
  5211. wps_ext_eap_identity_resp(hapd, dev, addr)
  5212. wps_ext_eap_wsc(dev, hapd, bssid, "EAP-WSC/Start")
  5213. wps_ext_eap_wsc(hapd, dev, addr, "M1")
  5214. logger.debug(title)
  5215. ev = hapd.wait_event(["EAPOL-TX"], timeout=10)
  5216. if ev is None:
  5217. raise Exception("Timeout on EAPOL-TX")
  5218. hapd.request("SET ext_eapol_frame_io 0")
  5219. dev.request("SET ext_eapol_frame_io 0")
  5220. msg = ev.split(' ')[2]
  5221. if msg[46:54] != '10220001':
  5222. raise Exception("Could not find Message Type attribute")
  5223. # Replace Message Type value. This will be rejected.
  5224. msg = msg[0:54] + msgtype + msg[56:]
  5225. res = dev.request("EAPOL_RX " + bssid + " " + msg)
  5226. if "OK" not in res:
  5227. raise Exception("EAPOL_RX failed")
  5228. ev = dev.wait_event(["WPS-FAIL"], timeout=5)
  5229. if ev is None:
  5230. raise Exception("WPS-FAIL event not seen")
  5231. dev.request("WPS_CANCEL")
  5232. dev.wait_disconnected()
  5233. def wps_m4_but_other(dev, apdev, title, msgtype):
  5234. addr,bssid,hapd = wps_start_ext(apdev, dev)
  5235. wps_ext_eap_identity_req(dev, hapd, bssid)
  5236. wps_ext_eap_identity_resp(hapd, dev, addr)
  5237. wps_ext_eap_wsc(dev, hapd, bssid, "EAP-WSC/Start")
  5238. wps_ext_eap_wsc(hapd, dev, addr, "M1")
  5239. wps_ext_eap_wsc(dev, hapd, bssid, "M2")
  5240. wps_ext_eap_wsc(hapd, dev, addr, "M3")
  5241. logger.debug(title)
  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.request("SET ext_eapol_frame_io 0")
  5247. msg = ev.split(' ')[2]
  5248. if msg[46:54] != '10220001':
  5249. raise Exception("Could not find Message Type attribute")
  5250. # Replace Message Type value. This will be rejected.
  5251. msg = msg[0:54] + msgtype + msg[56:]
  5252. res = dev.request("EAPOL_RX " + bssid + " " + msg)
  5253. if "OK" not in res:
  5254. raise Exception("EAPOL_RX failed")
  5255. ev = hapd.wait_event(["WPS-FAIL"], timeout=5)
  5256. if ev is None:
  5257. raise Exception("WPS-FAIL event not seen")
  5258. dev.request("WPS_CANCEL")
  5259. dev.wait_disconnected()
  5260. def test_ap_wps_m2_msg_type_m4(dev, apdev):
  5261. """WPS and M2 but Message Type M4"""
  5262. wps_m2_but_other(dev[0], apdev[0], "M2/M4", "08")
  5263. def test_ap_wps_m2_msg_type_m6(dev, apdev):
  5264. """WPS and M2 but Message Type M6"""
  5265. wps_m2_but_other(dev[0], apdev[0], "M2/M6", "0a")
  5266. def test_ap_wps_m2_msg_type_m8(dev, apdev):
  5267. """WPS and M2 but Message Type M8"""
  5268. wps_m2_but_other(dev[0], apdev[0], "M2/M8", "0c")
  5269. def test_ap_wps_m4_msg_type_m2(dev, apdev):
  5270. """WPS and M4 but Message Type M2"""
  5271. wps_m4_but_other(dev[0], apdev[0], "M4/M2", "05")
  5272. def test_ap_wps_m4_msg_type_m2d(dev, apdev):
  5273. """WPS and M4 but Message Type M2D"""
  5274. wps_m4_but_other(dev[0], apdev[0], "M4/M2D", "06")
  5275. @remote_compatible
  5276. def test_ap_wps_config_methods(dev, apdev):
  5277. """WPS configuration method parsing"""
  5278. ssid = "test-wps-conf"
  5279. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  5280. "wpa_passphrase": "12345678", "wpa": "2",
  5281. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  5282. "config_methods": "ethernet display ext_nfc_token int_nfc_token physical_display physical_push_button" }
  5283. hapd = hostapd.add_ap(apdev[0], params)
  5284. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  5285. "wpa_passphrase": "12345678", "wpa": "2",
  5286. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  5287. "config_methods": "display push_button" }
  5288. hapd2 = hostapd.add_ap(apdev[1], params)
  5289. def test_ap_wps_set_selected_registrar_proto(dev, apdev):
  5290. """WPS UPnP SetSelectedRegistrar protocol testing"""
  5291. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  5292. hapd = add_ssdp_ap(apdev[0], ap_uuid)
  5293. location = ssdp_get_location(ap_uuid)
  5294. urls = upnp_get_urls(location)
  5295. eventurl = urlparse.urlparse(urls['event_sub_url'])
  5296. ctrlurl = urlparse.urlparse(urls['control_url'])
  5297. url = urlparse.urlparse(location)
  5298. conn = httplib.HTTPConnection(url.netloc)
  5299. class WPSERHTTPServer(SocketServer.StreamRequestHandler):
  5300. def handle(self):
  5301. data = self.rfile.readline().strip()
  5302. logger.debug(data)
  5303. self.wfile.write(gen_wps_event())
  5304. server = MyTCPServer(("127.0.0.1", 12345), WPSERHTTPServer)
  5305. server.timeout = 1
  5306. headers = { "callback": '<http://127.0.0.1:12345/event>',
  5307. "NT": "upnp:event",
  5308. "timeout": "Second-1234" }
  5309. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  5310. resp = conn.getresponse()
  5311. if resp.status != 200:
  5312. raise Exception("Unexpected HTTP response: %d" % resp.status)
  5313. sid = resp.getheader("sid")
  5314. logger.debug("Subscription SID " + sid)
  5315. server.handle_request()
  5316. tests = [ (500, "10"),
  5317. (200, "104a000110" + "1041000101" + "101200020000" +
  5318. "105300023148" +
  5319. "1049002c00372a0001200124111111111111222222222222333333333333444444444444555555555555666666666666" +
  5320. "10480010362db47ba53a519188fb5458b986b2e4"),
  5321. (200, "104a000110" + "1041000100" + "101200020000" +
  5322. "105300020000"),
  5323. (200, "104a000110" + "1041000100"),
  5324. (200, "104a000110") ]
  5325. for status,test in tests:
  5326. tlvs = binascii.unhexlify(test)
  5327. newmsg = base64.b64encode(tlvs)
  5328. msg = '<?xml version="1.0"?>\n'
  5329. msg += '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  5330. msg += '<s:Body>'
  5331. msg += '<u:SetSelectedRegistrar xmlns:u="urn:schemas-wifialliance-org:service:WFAWLANConfig:1">'
  5332. msg += '<NewMessage>'
  5333. msg += newmsg
  5334. msg += "</NewMessage></u:SetSelectedRegistrar></s:Body></s:Envelope>"
  5335. headers = { "Content-type": 'text/xml; charset="utf-8"' }
  5336. headers["SOAPAction"] = '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#%s"' % "SetSelectedRegistrar"
  5337. conn.request("POST", ctrlurl.path, msg, headers)
  5338. resp = conn.getresponse()
  5339. if resp.status != status:
  5340. raise Exception("Unexpected HTTP response: %d (expected %d)" % (resp.status, status))
  5341. def test_ap_wps_adv_oom(dev, apdev):
  5342. """WPS AP and advertisement OOM"""
  5343. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  5344. hapd = add_ssdp_ap(apdev[0], ap_uuid)
  5345. with alloc_fail(hapd, 1, "=msearchreply_state_machine_start"):
  5346. ssdp_send_msearch("urn:schemas-wifialliance-org:service:WFAWLANConfig:1",
  5347. no_recv=True)
  5348. time.sleep(0.2)
  5349. with alloc_fail(hapd, 1, "eloop_register_timeout;msearchreply_state_machine_start"):
  5350. ssdp_send_msearch("urn:schemas-wifialliance-org:service:WFAWLANConfig:1",
  5351. no_recv=True)
  5352. time.sleep(0.2)
  5353. with alloc_fail(hapd, 1,
  5354. "next_advertisement;advertisement_state_machine_stop"):
  5355. hapd.disable()
  5356. with alloc_fail(hapd, 1, "ssdp_listener_start"):
  5357. if "FAIL" not in hapd.request("ENABLE"):
  5358. raise Exception("ENABLE succeeded during OOM")
  5359. def test_wps_config_methods(dev):
  5360. """WPS config method update"""
  5361. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  5362. wpas.interface_add("wlan5")
  5363. if "OK" not in wpas.request("SET config_methods display label"):
  5364. raise Exception("Failed to set config_methods")
  5365. if wpas.request("GET config_methods").strip() != "display label":
  5366. raise Exception("config_methods were not updated")
  5367. if "OK" not in wpas.request("SET config_methods "):
  5368. raise Exception("Failed to clear config_methods")
  5369. if wpas.request("GET config_methods").strip() != "":
  5370. raise Exception("config_methods were not cleared")
  5371. WPS_VENDOR_ID_WFA = 14122
  5372. WPS_VENDOR_TYPE = 1
  5373. # EAP-WSC Op-Code values
  5374. WSC_Start = 0x01
  5375. WSC_ACK = 0x02
  5376. WSC_NACK = 0x03
  5377. WSC_MSG = 0x04
  5378. WSC_Done = 0x05
  5379. WSC_FRAG_ACK = 0x06
  5380. ATTR_AP_CHANNEL = 0x1001
  5381. ATTR_ASSOC_STATE = 0x1002
  5382. ATTR_AUTH_TYPE = 0x1003
  5383. ATTR_AUTH_TYPE_FLAGS = 0x1004
  5384. ATTR_AUTHENTICATOR = 0x1005
  5385. ATTR_CONFIG_METHODS = 0x1008
  5386. ATTR_CONFIG_ERROR = 0x1009
  5387. ATTR_CONFIRM_URL4 = 0x100a
  5388. ATTR_CONFIRM_URL6 = 0x100b
  5389. ATTR_CONN_TYPE = 0x100c
  5390. ATTR_CONN_TYPE_FLAGS = 0x100d
  5391. ATTR_CRED = 0x100e
  5392. ATTR_ENCR_TYPE = 0x100f
  5393. ATTR_ENCR_TYPE_FLAGS = 0x1010
  5394. ATTR_DEV_NAME = 0x1011
  5395. ATTR_DEV_PASSWORD_ID = 0x1012
  5396. ATTR_E_HASH1 = 0x1014
  5397. ATTR_E_HASH2 = 0x1015
  5398. ATTR_E_SNONCE1 = 0x1016
  5399. ATTR_E_SNONCE2 = 0x1017
  5400. ATTR_ENCR_SETTINGS = 0x1018
  5401. ATTR_ENROLLEE_NONCE = 0x101a
  5402. ATTR_FEATURE_ID = 0x101b
  5403. ATTR_IDENTITY = 0x101c
  5404. ATTR_IDENTITY_PROOF = 0x101d
  5405. ATTR_KEY_WRAP_AUTH = 0x101e
  5406. ATTR_KEY_ID = 0x101f
  5407. ATTR_MAC_ADDR = 0x1020
  5408. ATTR_MANUFACTURER = 0x1021
  5409. ATTR_MSG_TYPE = 0x1022
  5410. ATTR_MODEL_NAME = 0x1023
  5411. ATTR_MODEL_NUMBER = 0x1024
  5412. ATTR_NETWORK_INDEX = 0x1026
  5413. ATTR_NETWORK_KEY = 0x1027
  5414. ATTR_NETWORK_KEY_INDEX = 0x1028
  5415. ATTR_NEW_DEVICE_NAME = 0x1029
  5416. ATTR_NEW_PASSWORD = 0x102a
  5417. ATTR_OOB_DEVICE_PASSWORD = 0x102c
  5418. ATTR_OS_VERSION = 0x102d
  5419. ATTR_POWER_LEVEL = 0x102f
  5420. ATTR_PSK_CURRENT = 0x1030
  5421. ATTR_PSK_MAX = 0x1031
  5422. ATTR_PUBLIC_KEY = 0x1032
  5423. ATTR_RADIO_ENABLE = 0x1033
  5424. ATTR_REBOOT = 0x1034
  5425. ATTR_REGISTRAR_CURRENT = 0x1035
  5426. ATTR_REGISTRAR_ESTABLISHED = 0x1036
  5427. ATTR_REGISTRAR_LIST = 0x1037
  5428. ATTR_REGISTRAR_MAX = 0x1038
  5429. ATTR_REGISTRAR_NONCE = 0x1039
  5430. ATTR_REQUEST_TYPE = 0x103a
  5431. ATTR_RESPONSE_TYPE = 0x103b
  5432. ATTR_RF_BANDS = 0x103c
  5433. ATTR_R_HASH1 = 0x103d
  5434. ATTR_R_HASH2 = 0x103e
  5435. ATTR_R_SNONCE1 = 0x103f
  5436. ATTR_R_SNONCE2 = 0x1040
  5437. ATTR_SELECTED_REGISTRAR = 0x1041
  5438. ATTR_SERIAL_NUMBER = 0x1042
  5439. ATTR_WPS_STATE = 0x1044
  5440. ATTR_SSID = 0x1045
  5441. ATTR_TOTAL_NETWORKS = 0x1046
  5442. ATTR_UUID_E = 0x1047
  5443. ATTR_UUID_R = 0x1048
  5444. ATTR_VENDOR_EXT = 0x1049
  5445. ATTR_VERSION = 0x104a
  5446. ATTR_X509_CERT_REQ = 0x104b
  5447. ATTR_X509_CERT = 0x104c
  5448. ATTR_EAP_IDENTITY = 0x104d
  5449. ATTR_MSG_COUNTER = 0x104e
  5450. ATTR_PUBKEY_HASH = 0x104f
  5451. ATTR_REKEY_KEY = 0x1050
  5452. ATTR_KEY_LIFETIME = 0x1051
  5453. ATTR_PERMITTED_CFG_METHODS = 0x1052
  5454. ATTR_SELECTED_REGISTRAR_CONFIG_METHODS = 0x1053
  5455. ATTR_PRIMARY_DEV_TYPE = 0x1054
  5456. ATTR_SECONDARY_DEV_TYPE_LIST = 0x1055
  5457. ATTR_PORTABLE_DEV = 0x1056
  5458. ATTR_AP_SETUP_LOCKED = 0x1057
  5459. ATTR_APPLICATION_EXT = 0x1058
  5460. ATTR_EAP_TYPE = 0x1059
  5461. ATTR_IV = 0x1060
  5462. ATTR_KEY_PROVIDED_AUTO = 0x1061
  5463. ATTR_802_1X_ENABLED = 0x1062
  5464. ATTR_APPSESSIONKEY = 0x1063
  5465. ATTR_WEPTRANSMITKEY = 0x1064
  5466. ATTR_REQUESTED_DEV_TYPE = 0x106a
  5467. # Message Type
  5468. WPS_Beacon = 0x01
  5469. WPS_ProbeRequest = 0x02
  5470. WPS_ProbeResponse = 0x03
  5471. WPS_M1 = 0x04
  5472. WPS_M2 = 0x05
  5473. WPS_M2D = 0x06
  5474. WPS_M3 = 0x07
  5475. WPS_M4 = 0x08
  5476. WPS_M5 = 0x09
  5477. WPS_M6 = 0x0a
  5478. WPS_M7 = 0x0b
  5479. WPS_M8 = 0x0c
  5480. WPS_WSC_ACK = 0x0d
  5481. WPS_WSC_NACK = 0x0e
  5482. WPS_WSC_DONE = 0x0f
  5483. def get_wsc_msg(dev):
  5484. ev = dev.wait_event(["EAPOL-TX"], timeout=10)
  5485. if ev is None:
  5486. raise Exception("Timeout on EAPOL-TX")
  5487. data = binascii.unhexlify(ev.split(' ')[2])
  5488. msg = {}
  5489. # Parse EAPOL header
  5490. if len(data) < 4:
  5491. raise Exception("No room for EAPOL header")
  5492. version,type,length = struct.unpack('>BBH', data[0:4])
  5493. msg['eapol_version'] = version
  5494. msg['eapol_type'] = type
  5495. msg['eapol_length'] = length
  5496. data = data[4:]
  5497. if length != len(data):
  5498. raise Exception("EAPOL header length mismatch (%d != %d)" % (length, len(data)))
  5499. if type != 0:
  5500. raise Exception("Unexpected EAPOL header type: %d" % type)
  5501. # Parse EAP header
  5502. if len(data) < 4:
  5503. raise Exception("No room for EAP header")
  5504. code,identifier,length = struct.unpack('>BBH', data[0:4])
  5505. msg['eap_code'] = code
  5506. msg['eap_identifier'] = identifier
  5507. msg['eap_length'] = length
  5508. data = data[4:]
  5509. if msg['eapol_length'] != msg['eap_length']:
  5510. raise Exception("EAP header length mismatch (%d != %d)" % (msg['eapol_length'], length))
  5511. # Parse EAP expanded header
  5512. if len(data) < 1:
  5513. raise Exception("No EAP type included")
  5514. msg['eap_type'], = struct.unpack('B', data[0])
  5515. data = data[1:]
  5516. if msg['eap_type'] == 254:
  5517. if len(data) < 3 + 4:
  5518. raise Exception("Truncated EAP expanded header")
  5519. msg['eap_vendor_id'], msg['eap_vendor_type'] = struct.unpack('>LL', '\0' + data[0:7])
  5520. data = data[7:]
  5521. else:
  5522. raise Exception("Unexpected EAP type")
  5523. if msg['eap_vendor_id'] != WPS_VENDOR_ID_WFA:
  5524. raise Exception("Unexpected Vendor-Id")
  5525. if msg['eap_vendor_type'] != WPS_VENDOR_TYPE:
  5526. raise Exception("Unexpected Vendor-Type")
  5527. # Parse EAP-WSC header
  5528. if len(data) < 2:
  5529. raise Exception("Truncated EAP-WSC header")
  5530. msg['wsc_opcode'], msg['wsc_flags'] = struct.unpack('BB', data[0:2])
  5531. data = data[2:]
  5532. # Parse WSC attributes
  5533. msg['raw_attrs'] = data
  5534. attrs = {}
  5535. while len(data) > 0:
  5536. if len(data) < 4:
  5537. raise Exception("Truncated attribute header")
  5538. attr,length = struct.unpack('>HH', data[0:4])
  5539. data = data[4:]
  5540. if length > len(data):
  5541. raise Exception("Truncated attribute 0x%04x" % attr)
  5542. attrs[attr] = data[0:length]
  5543. data = data[length:]
  5544. msg['wsc_attrs'] = attrs
  5545. if ATTR_MSG_TYPE in attrs:
  5546. msg['wsc_msg_type'], = struct.unpack('B', attrs[ATTR_MSG_TYPE])
  5547. return msg
  5548. def recv_wsc_msg(dev, opcode, msg_type):
  5549. msg = get_wsc_msg(dev)
  5550. if msg['wsc_opcode'] != opcode or msg['wsc_msg_type'] != msg_type:
  5551. raise Exception("Unexpected Op-Code/MsgType")
  5552. return msg, msg['wsc_attrs'], msg['raw_attrs']
  5553. def build_wsc_attr(attr, payload):
  5554. return struct.pack('>HH', attr, len(payload)) + payload
  5555. def build_attr_msg_type(msg_type):
  5556. return build_wsc_attr(ATTR_MSG_TYPE, struct.pack('B', msg_type))
  5557. def build_eap_wsc(eap_code, eap_id, payload, opcode=WSC_MSG):
  5558. length = 4 + 8 + 2 + len(payload)
  5559. # EAPOL header
  5560. msg = struct.pack('>BBH', 2, 0, length)
  5561. # EAP header
  5562. msg += struct.pack('>BBH', eap_code, eap_id, length)
  5563. # EAP expanded header for EAP-WSC
  5564. msg += struct.pack('B', 254)
  5565. msg += struct.pack('>L', WPS_VENDOR_ID_WFA)[1:4]
  5566. msg += struct.pack('>L', WPS_VENDOR_TYPE)
  5567. # EAP-WSC header
  5568. msg += struct.pack('BB', opcode, 0)
  5569. # WSC attributes
  5570. msg += payload
  5571. return msg
  5572. def build_eap_success(eap_id):
  5573. length = 4
  5574. # EAPOL header
  5575. msg = struct.pack('>BBH', 2, 0, length)
  5576. # EAP header
  5577. msg += struct.pack('>BBH', 3, eap_id, length)
  5578. return msg
  5579. def build_eap_failure(eap_id):
  5580. length = 4
  5581. # EAPOL header
  5582. msg = struct.pack('>BBH', 2, 0, length)
  5583. # EAP header
  5584. msg += struct.pack('>BBH', 4, eap_id, length)
  5585. return msg
  5586. def send_wsc_msg(dev, src, msg):
  5587. res = dev.request("EAPOL_RX " + src + " " + binascii.hexlify(msg))
  5588. if "OK" not in res:
  5589. raise Exception("EAPOL_RX failed")
  5590. group_5_prime = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF
  5591. group_5_generator = 2
  5592. def wsc_kdf(key, label, bits):
  5593. result = ''
  5594. i = 1
  5595. while len(result) * 8 < bits:
  5596. data = struct.pack('>L', i) + label + struct.pack('>L', bits)
  5597. m = hmac.new(key, data, hashlib.sha256)
  5598. result += m.digest()
  5599. i += 1
  5600. return result[0:bits / 8]
  5601. def wsc_keys(kdk):
  5602. keys = wsc_kdf(kdk, "Wi-Fi Easy and Secure Key Derivation", 640)
  5603. authkey = keys[0:32]
  5604. keywrapkey = keys[32:48]
  5605. emsk = keys[48:80]
  5606. return authkey,keywrapkey,emsk
  5607. def wsc_dev_pw_half_psk(authkey, dev_pw):
  5608. m = hmac.new(authkey, dev_pw, hashlib.sha256)
  5609. return m.digest()[0:16]
  5610. def wsc_dev_pw_psk(authkey, dev_pw):
  5611. dev_pw_1 = dev_pw[0:len(dev_pw) / 2]
  5612. dev_pw_2 = dev_pw[len(dev_pw) / 2:]
  5613. psk1 = wsc_dev_pw_half_psk(authkey, dev_pw_1)
  5614. psk2 = wsc_dev_pw_half_psk(authkey, dev_pw_2)
  5615. return psk1,psk2
  5616. def build_attr_authenticator(authkey, prev_msg, curr_msg):
  5617. m = hmac.new(authkey, prev_msg + curr_msg, hashlib.sha256)
  5618. auth = m.digest()[0:8]
  5619. return build_wsc_attr(ATTR_AUTHENTICATOR, auth)
  5620. def build_attr_encr_settings(authkey, keywrapkey, data):
  5621. m = hmac.new(authkey, data, hashlib.sha256)
  5622. kwa = m.digest()[0:8]
  5623. data += build_wsc_attr(ATTR_KEY_WRAP_AUTH, kwa)
  5624. iv = 16*'\x99'
  5625. aes = AES.new(keywrapkey, AES.MODE_CBC, iv)
  5626. pad_len = 16 - len(data) % 16
  5627. ps = pad_len * struct.pack('B', pad_len)
  5628. data += ps
  5629. wrapped = aes.encrypt(data)
  5630. return build_wsc_attr(ATTR_ENCR_SETTINGS, iv + wrapped)
  5631. def decrypt_attr_encr_settings(authkey, keywrapkey, data):
  5632. if len(data) < 32 or len(data) % 16 != 0:
  5633. raise Exception("Unexpected Encrypted Settings length: %d" % len(data))
  5634. iv = data[0:16]
  5635. encr = data[16:]
  5636. aes = AES.new(keywrapkey, AES.MODE_CBC, iv)
  5637. decrypted = aes.decrypt(encr)
  5638. pad_len, = struct.unpack('B', decrypted[-1])
  5639. if pad_len > len(decrypted):
  5640. raise Exception("Invalid padding in Encrypted Settings")
  5641. for i in range(-pad_len, -1):
  5642. if decrypted[i] != decrypted[-1]:
  5643. raise Exception("Invalid PS value in Encrypted Settings")
  5644. decrypted = decrypted[0:len(decrypted) - pad_len]
  5645. if len(decrypted) < 12:
  5646. raise Exception("Truncated Encrypted Settings plaintext")
  5647. kwa = decrypted[-12:]
  5648. attr,length = struct.unpack(">HH", kwa[0:4])
  5649. if attr != ATTR_KEY_WRAP_AUTH or length != 8:
  5650. raise Exception("Invalid KWA header")
  5651. kwa = kwa[4:]
  5652. decrypted = decrypted[0:len(decrypted) - 12]
  5653. m = hmac.new(authkey, decrypted, hashlib.sha256)
  5654. calc_kwa = m.digest()[0:8]
  5655. if kwa != calc_kwa:
  5656. raise Exception("KWA mismatch")
  5657. return decrypted
  5658. def zeropad_str(val, pad_len):
  5659. while len(val) < pad_len * 2:
  5660. val = '0' + val
  5661. return val
  5662. def wsc_dh_init():
  5663. # For now, use a hardcoded private key. In theory, this is supposed to be
  5664. # randomly selected.
  5665. own_private = 0x123456789
  5666. own_public = pow(group_5_generator, own_private, group_5_prime)
  5667. pk = binascii.unhexlify(zeropad_str(format(own_public, '02x'), 192))
  5668. return own_private, pk
  5669. def wsc_dh_kdf(peer_pk, own_private, mac_addr, e_nonce, r_nonce):
  5670. peer_public = long(binascii.hexlify(peer_pk), 16)
  5671. if peer_public < 2 or peer_public >= group_5_prime:
  5672. raise Exception("Invalid peer public key")
  5673. if pow(peer_public, (group_5_prime - 1) / 2, group_5_prime) != 1:
  5674. raise Exception("Unexpected Legendre symbol for peer public key")
  5675. shared_secret = pow(peer_public, own_private, group_5_prime)
  5676. ss = zeropad_str(format(shared_secret, "02x"), 192)
  5677. logger.debug("DH shared secret: " + ss)
  5678. dhkey = hashlib.sha256(binascii.unhexlify(ss)).digest()
  5679. logger.debug("DHKey: " + binascii.hexlify(dhkey))
  5680. m = hmac.new(dhkey, e_nonce + mac_addr + r_nonce, hashlib.sha256)
  5681. kdk = m.digest()
  5682. logger.debug("KDK: " + binascii.hexlify(kdk))
  5683. authkey,keywrapkey,emsk = wsc_keys(kdk)
  5684. logger.debug("AuthKey: " + binascii.hexlify(authkey))
  5685. logger.debug("KeyWrapKey: " + binascii.hexlify(keywrapkey))
  5686. logger.debug("EMSK: " + binascii.hexlify(emsk))
  5687. return authkey,keywrapkey
  5688. def wsc_dev_pw_hash(authkey, dev_pw, e_pk, r_pk):
  5689. psk1,psk2 = wsc_dev_pw_psk(authkey, dev_pw)
  5690. logger.debug("PSK1: " + binascii.hexlify(psk1))
  5691. logger.debug("PSK2: " + binascii.hexlify(psk2))
  5692. # Note: Secret values are supposed to be random, but hardcoded values are
  5693. # fine for testing.
  5694. s1 = 16*'\x77'
  5695. m = hmac.new(authkey, s1 + psk1 + e_pk + r_pk, hashlib.sha256)
  5696. hash1 = m.digest()
  5697. logger.debug("Hash1: " + binascii.hexlify(hash1))
  5698. s2 = 16*'\x88'
  5699. m = hmac.new(authkey, s2 + psk2 + e_pk + r_pk, hashlib.sha256)
  5700. hash2 = m.digest()
  5701. logger.debug("Hash2: " + binascii.hexlify(hash2))
  5702. return s1,s2,hash1,hash2
  5703. def build_m1(eap_id, uuid_e, mac_addr, e_nonce, e_pk,
  5704. manufacturer='', model_name='', config_methods='\x00\x00'):
  5705. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5706. attrs += build_attr_msg_type(WPS_M1)
  5707. attrs += build_wsc_attr(ATTR_UUID_E, uuid_e)
  5708. attrs += build_wsc_attr(ATTR_MAC_ADDR, mac_addr)
  5709. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  5710. attrs += build_wsc_attr(ATTR_PUBLIC_KEY, e_pk)
  5711. attrs += build_wsc_attr(ATTR_AUTH_TYPE_FLAGS, '\x00\x00')
  5712. attrs += build_wsc_attr(ATTR_ENCR_TYPE_FLAGS, '\x00\x00')
  5713. attrs += build_wsc_attr(ATTR_CONN_TYPE_FLAGS, '\x00')
  5714. attrs += build_wsc_attr(ATTR_CONFIG_METHODS, config_methods)
  5715. attrs += build_wsc_attr(ATTR_WPS_STATE, '\x00')
  5716. attrs += build_wsc_attr(ATTR_MANUFACTURER, manufacturer)
  5717. attrs += build_wsc_attr(ATTR_MODEL_NAME, model_name)
  5718. attrs += build_wsc_attr(ATTR_MODEL_NUMBER, '')
  5719. attrs += build_wsc_attr(ATTR_SERIAL_NUMBER, '')
  5720. attrs += build_wsc_attr(ATTR_PRIMARY_DEV_TYPE, 8*'\x00')
  5721. attrs += build_wsc_attr(ATTR_DEV_NAME, '')
  5722. attrs += build_wsc_attr(ATTR_RF_BANDS, '\x00')
  5723. attrs += build_wsc_attr(ATTR_ASSOC_STATE, '\x00\x00')
  5724. attrs += build_wsc_attr(ATTR_DEV_PASSWORD_ID, '\x00\x00')
  5725. attrs += build_wsc_attr(ATTR_CONFIG_ERROR, '\x00\x00')
  5726. attrs += build_wsc_attr(ATTR_OS_VERSION, '\x00\x00\x00\x00')
  5727. m1 = build_eap_wsc(2, eap_id, attrs)
  5728. return m1, attrs
  5729. def build_m2(authkey, m1, eap_id, e_nonce, r_nonce, uuid_r, r_pk,
  5730. dev_pw_id='\x00\x00', eap_code=1):
  5731. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5732. attrs += build_attr_msg_type(WPS_M2)
  5733. if e_nonce:
  5734. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  5735. if r_nonce:
  5736. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  5737. attrs += build_wsc_attr(ATTR_UUID_R, uuid_r)
  5738. if r_pk:
  5739. attrs += build_wsc_attr(ATTR_PUBLIC_KEY, r_pk)
  5740. attrs += build_wsc_attr(ATTR_AUTH_TYPE_FLAGS, '\x00\x00')
  5741. attrs += build_wsc_attr(ATTR_ENCR_TYPE_FLAGS, '\x00\x00')
  5742. attrs += build_wsc_attr(ATTR_CONN_TYPE_FLAGS, '\x00')
  5743. attrs += build_wsc_attr(ATTR_CONFIG_METHODS, '\x00\x00')
  5744. attrs += build_wsc_attr(ATTR_MANUFACTURER, '')
  5745. attrs += build_wsc_attr(ATTR_MODEL_NAME, '')
  5746. attrs += build_wsc_attr(ATTR_MODEL_NUMBER, '')
  5747. attrs += build_wsc_attr(ATTR_SERIAL_NUMBER, '')
  5748. attrs += build_wsc_attr(ATTR_PRIMARY_DEV_TYPE, 8*'\x00')
  5749. attrs += build_wsc_attr(ATTR_DEV_NAME, '')
  5750. attrs += build_wsc_attr(ATTR_RF_BANDS, '\x00')
  5751. attrs += build_wsc_attr(ATTR_ASSOC_STATE, '\x00\x00')
  5752. attrs += build_wsc_attr(ATTR_CONFIG_ERROR, '\x00\x00')
  5753. attrs += build_wsc_attr(ATTR_DEV_PASSWORD_ID, dev_pw_id)
  5754. attrs += build_wsc_attr(ATTR_OS_VERSION, '\x00\x00\x00\x00')
  5755. attrs += build_attr_authenticator(authkey, m1, attrs)
  5756. m2 = build_eap_wsc(eap_code, eap_id, attrs)
  5757. return m2, attrs
  5758. def build_m2d(m1, eap_id, e_nonce, r_nonce, uuid_r, dev_pw_id=None, eap_code=1):
  5759. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5760. attrs += build_attr_msg_type(WPS_M2D)
  5761. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  5762. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  5763. attrs += build_wsc_attr(ATTR_UUID_R, uuid_r)
  5764. attrs += build_wsc_attr(ATTR_AUTH_TYPE_FLAGS, '\x00\x00')
  5765. attrs += build_wsc_attr(ATTR_ENCR_TYPE_FLAGS, '\x00\x00')
  5766. attrs += build_wsc_attr(ATTR_CONN_TYPE_FLAGS, '\x00')
  5767. attrs += build_wsc_attr(ATTR_CONFIG_METHODS, '\x00\x00')
  5768. attrs += build_wsc_attr(ATTR_MANUFACTURER, '')
  5769. attrs += build_wsc_attr(ATTR_MODEL_NAME, '')
  5770. #attrs += build_wsc_attr(ATTR_MODEL_NUMBER, '')
  5771. attrs += build_wsc_attr(ATTR_SERIAL_NUMBER, '')
  5772. attrs += build_wsc_attr(ATTR_PRIMARY_DEV_TYPE, 8*'\x00')
  5773. attrs += build_wsc_attr(ATTR_DEV_NAME, '')
  5774. attrs += build_wsc_attr(ATTR_RF_BANDS, '\x00')
  5775. attrs += build_wsc_attr(ATTR_ASSOC_STATE, '\x00\x00')
  5776. attrs += build_wsc_attr(ATTR_CONFIG_ERROR, '\x00\x00')
  5777. attrs += build_wsc_attr(ATTR_OS_VERSION, '\x00\x00\x00\x00')
  5778. if dev_pw_id:
  5779. attrs += build_wsc_attr(ATTR_DEV_PASSWORD_ID, dev_pw_id)
  5780. m2d = build_eap_wsc(eap_code, eap_id, attrs)
  5781. return m2d, attrs
  5782. def build_ack(eap_id, e_nonce, r_nonce, msg_type=WPS_WSC_ACK, eap_code=1):
  5783. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5784. if msg_type is not None:
  5785. attrs += build_attr_msg_type(msg_type)
  5786. if e_nonce:
  5787. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  5788. if r_nonce:
  5789. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  5790. msg = build_eap_wsc(eap_code, eap_id, attrs, opcode=WSC_ACK)
  5791. return msg, attrs
  5792. def build_nack(eap_id, e_nonce, r_nonce, config_error='\x00\x00',
  5793. msg_type=WPS_WSC_NACK, eap_code=1):
  5794. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5795. if msg_type is not None:
  5796. attrs += build_attr_msg_type(msg_type)
  5797. if e_nonce:
  5798. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  5799. if r_nonce:
  5800. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  5801. if config_error:
  5802. attrs += build_wsc_attr(ATTR_CONFIG_ERROR, config_error)
  5803. msg = build_eap_wsc(eap_code, eap_id, attrs, opcode=WSC_NACK)
  5804. return msg, attrs
  5805. def test_wps_ext(dev, apdev):
  5806. """WPS against external implementation"""
  5807. pin = "12345670"
  5808. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  5809. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5810. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5811. logger.debug("Receive WSC/Start from AP")
  5812. msg = get_wsc_msg(hapd)
  5813. if msg['wsc_opcode'] != WSC_Start:
  5814. raise Exception("Unexpected Op-Code for WSC/Start")
  5815. wsc_start_id = msg['eap_identifier']
  5816. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  5817. uuid_e = 16*'\x11'
  5818. e_nonce = 16*'\x22'
  5819. own_private, e_pk = wsc_dh_init()
  5820. logger.debug("Send M1 to AP")
  5821. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  5822. e_nonce, e_pk)
  5823. send_wsc_msg(hapd, addr, m1)
  5824. logger.debug("Receive M2 from AP")
  5825. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  5826. authkey,keywrapkey = wsc_dh_kdf(m2_attrs[ATTR_PUBLIC_KEY], own_private,
  5827. mac_addr, e_nonce,
  5828. m2_attrs[ATTR_REGISTRAR_NONCE])
  5829. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk,
  5830. m2_attrs[ATTR_PUBLIC_KEY])
  5831. logger.debug("Send M3 to AP")
  5832. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5833. attrs += build_attr_msg_type(WPS_M3)
  5834. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE,
  5835. m2_attrs[ATTR_REGISTRAR_NONCE])
  5836. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  5837. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  5838. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  5839. raw_m3_attrs = attrs
  5840. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  5841. send_wsc_msg(hapd, addr, m3)
  5842. logger.debug("Receive M4 from AP")
  5843. msg, m4_attrs, raw_m4_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M4)
  5844. logger.debug("Send M5 to AP")
  5845. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5846. attrs += build_attr_msg_type(WPS_M5)
  5847. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE,
  5848. m2_attrs[ATTR_REGISTRAR_NONCE])
  5849. data = build_wsc_attr(ATTR_E_SNONCE1, e_s1)
  5850. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  5851. attrs += build_attr_authenticator(authkey, raw_m4_attrs, attrs)
  5852. raw_m5_attrs = attrs
  5853. m5 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  5854. send_wsc_msg(hapd, addr, m5)
  5855. logger.debug("Receive M6 from AP")
  5856. msg, m6_attrs, raw_m6_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M6)
  5857. logger.debug("Send M7 to AP")
  5858. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5859. attrs += build_attr_msg_type(WPS_M7)
  5860. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE,
  5861. m2_attrs[ATTR_REGISTRAR_NONCE])
  5862. data = build_wsc_attr(ATTR_E_SNONCE2, e_s2)
  5863. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  5864. attrs += build_attr_authenticator(authkey, raw_m6_attrs, attrs)
  5865. m7 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  5866. raw_m7_attrs = attrs
  5867. send_wsc_msg(hapd, addr, m7)
  5868. logger.debug("Receive M8 from AP")
  5869. msg, m8_attrs, raw_m8_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M8)
  5870. m8_cred = decrypt_attr_encr_settings(authkey, keywrapkey,
  5871. m8_attrs[ATTR_ENCR_SETTINGS])
  5872. logger.debug("M8 Credential: " + binascii.hexlify(m8_cred))
  5873. logger.debug("Prepare WSC_Done")
  5874. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5875. attrs += build_attr_msg_type(WPS_WSC_DONE)
  5876. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  5877. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE,
  5878. m2_attrs[ATTR_REGISTRAR_NONCE])
  5879. wsc_done = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_Done)
  5880. # Do not send WSC_Done yet to allow exchangw with STA complete before the
  5881. # AP disconnects.
  5882. uuid_r = 16*'\x33'
  5883. r_nonce = 16*'\x44'
  5884. eap_id = wsc_start_id
  5885. logger.debug("Send WSC/Start to STA")
  5886. wsc_start = build_eap_wsc(1, eap_id, "", opcode=WSC_Start)
  5887. send_wsc_msg(dev[0], bssid, wsc_start)
  5888. eap_id = (eap_id + 1) % 256
  5889. logger.debug("Receive M1 from STA")
  5890. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  5891. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  5892. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  5893. r_nonce)
  5894. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  5895. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  5896. logger.debug("Send M2 to STA")
  5897. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  5898. m1_attrs[ATTR_ENROLLEE_NONCE],
  5899. r_nonce, uuid_r, e_pk)
  5900. send_wsc_msg(dev[0], bssid, m2)
  5901. eap_id = (eap_id + 1) % 256
  5902. logger.debug("Receive M3 from STA")
  5903. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M3)
  5904. logger.debug("Send M4 to STA")
  5905. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5906. attrs += build_attr_msg_type(WPS_M4)
  5907. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, m1_attrs[ATTR_ENROLLEE_NONCE])
  5908. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  5909. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  5910. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  5911. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  5912. attrs += build_attr_authenticator(authkey, raw_m3_attrs, attrs)
  5913. raw_m4_attrs = attrs
  5914. m4 = build_eap_wsc(1, eap_id, attrs)
  5915. send_wsc_msg(dev[0], bssid, m4)
  5916. eap_id = (eap_id + 1) % 256
  5917. logger.debug("Receive M5 from STA")
  5918. msg, m5_attrs, raw_m5_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M5)
  5919. logger.debug("Send M6 to STA")
  5920. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5921. attrs += build_attr_msg_type(WPS_M6)
  5922. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE,
  5923. m1_attrs[ATTR_ENROLLEE_NONCE])
  5924. data = build_wsc_attr(ATTR_R_SNONCE2, r_s2)
  5925. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  5926. attrs += build_attr_authenticator(authkey, raw_m5_attrs, attrs)
  5927. raw_m6_attrs = attrs
  5928. m6 = build_eap_wsc(1, eap_id, attrs)
  5929. send_wsc_msg(dev[0], bssid, m6)
  5930. eap_id = (eap_id + 1) % 256
  5931. logger.debug("Receive M7 from STA")
  5932. msg, m7_attrs, raw_m7_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M7)
  5933. logger.debug("Send M8 to STA")
  5934. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5935. attrs += build_attr_msg_type(WPS_M8)
  5936. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE,
  5937. m1_attrs[ATTR_ENROLLEE_NONCE])
  5938. attrs += build_attr_encr_settings(authkey, keywrapkey, m8_cred)
  5939. attrs += build_attr_authenticator(authkey, raw_m7_attrs, attrs)
  5940. raw_m8_attrs = attrs
  5941. m8 = build_eap_wsc(1, eap_id, attrs)
  5942. send_wsc_msg(dev[0], bssid, m8)
  5943. eap_id = (eap_id + 1) % 256
  5944. ev = dev[0].wait_event(["WPS-CRED-RECEIVED"], timeout=5)
  5945. if ev is None:
  5946. raise Exception("wpa_supplicant did not report credential")
  5947. logger.debug("Receive WSC_Done from STA")
  5948. msg = get_wsc_msg(dev[0])
  5949. if msg['wsc_opcode'] != WSC_Done or msg['wsc_msg_type'] != WPS_WSC_DONE:
  5950. raise Exception("Unexpected Op-Code/MsgType for WSC_Done")
  5951. logger.debug("Send WSC_Done to AP")
  5952. hapd.request("SET ext_eapol_frame_io 0")
  5953. dev[0].request("SET ext_eapol_frame_io 0")
  5954. send_wsc_msg(hapd, addr, wsc_done)
  5955. ev = hapd.wait_event(["WPS-REG-SUCCESS"], timeout=5)
  5956. if ev is None:
  5957. raise Exception("hostapd did not report WPS success")
  5958. dev[0].wait_connected()
  5959. def wps_start_kwa(dev, apdev):
  5960. pin = "12345670"
  5961. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  5962. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  5963. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  5964. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  5965. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  5966. uuid_r = 16*'\x33'
  5967. r_nonce = 16*'\x44'
  5968. own_private, e_pk = wsc_dh_init()
  5969. logger.debug("Receive M1 from STA")
  5970. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  5971. eap_id = (msg['eap_identifier'] + 1) % 256
  5972. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  5973. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  5974. r_nonce)
  5975. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  5976. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  5977. logger.debug("Send M2 to STA")
  5978. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  5979. m1_attrs[ATTR_ENROLLEE_NONCE],
  5980. r_nonce, uuid_r, e_pk)
  5981. send_wsc_msg(dev[0], bssid, m2)
  5982. eap_id = (eap_id + 1) % 256
  5983. logger.debug("Receive M3 from STA")
  5984. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M3)
  5985. logger.debug("Send M4 to STA")
  5986. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  5987. attrs += build_attr_msg_type(WPS_M4)
  5988. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, m1_attrs[ATTR_ENROLLEE_NONCE])
  5989. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  5990. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  5991. return r_s1, keywrapkey, authkey, raw_m3_attrs, eap_id, bssid, attrs
  5992. def wps_stop_kwa(dev, bssid, attrs, authkey, raw_m3_attrs, eap_id):
  5993. attrs += build_attr_authenticator(authkey, raw_m3_attrs, attrs)
  5994. m4 = build_eap_wsc(1, eap_id, attrs)
  5995. send_wsc_msg(dev[0], bssid, m4)
  5996. eap_id = (eap_id + 1) % 256
  5997. logger.debug("Receive M5 from STA")
  5998. msg = get_wsc_msg(dev[0])
  5999. if msg['wsc_opcode'] != WSC_NACK:
  6000. raise Exception("Unexpected message - expected WSC_Nack")
  6001. dev[0].request("WPS_CANCEL")
  6002. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6003. dev[0].wait_disconnected()
  6004. def test_wps_ext_kwa_proto_no_kwa(dev, apdev):
  6005. """WPS and KWA error: No KWA attribute"""
  6006. r_s1,keywrapkey,authkey,raw_m3_attrs,eap_id,bssid,attrs = wps_start_kwa(dev, apdev)
  6007. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6008. # Encrypted Settings without KWA
  6009. iv = 16*'\x99'
  6010. aes = AES.new(keywrapkey, AES.MODE_CBC, iv)
  6011. pad_len = 16 - len(data) % 16
  6012. ps = pad_len * struct.pack('B', pad_len)
  6013. data += ps
  6014. wrapped = aes.encrypt(data)
  6015. attrs += build_wsc_attr(ATTR_ENCR_SETTINGS, iv + wrapped)
  6016. wps_stop_kwa(dev, bssid, attrs, authkey, raw_m3_attrs, eap_id)
  6017. def test_wps_ext_kwa_proto_data_after_kwa(dev, apdev):
  6018. """WPS and KWA error: Data after KWA"""
  6019. r_s1,keywrapkey,authkey,raw_m3_attrs,eap_id,bssid,attrs = wps_start_kwa(dev, apdev)
  6020. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6021. # Encrypted Settings and data after KWA
  6022. m = hmac.new(authkey, data, hashlib.sha256)
  6023. kwa = m.digest()[0:8]
  6024. data += build_wsc_attr(ATTR_KEY_WRAP_AUTH, kwa)
  6025. data += build_wsc_attr(ATTR_VENDOR_EXT, "1234567890")
  6026. iv = 16*'\x99'
  6027. aes = AES.new(keywrapkey, AES.MODE_CBC, iv)
  6028. pad_len = 16 - len(data) % 16
  6029. ps = pad_len * struct.pack('B', pad_len)
  6030. data += ps
  6031. wrapped = aes.encrypt(data)
  6032. attrs += build_wsc_attr(ATTR_ENCR_SETTINGS, iv + wrapped)
  6033. wps_stop_kwa(dev, bssid, attrs, authkey, raw_m3_attrs, eap_id)
  6034. def test_wps_ext_kwa_proto_kwa_mismatch(dev, apdev):
  6035. """WPS and KWA error: KWA mismatch"""
  6036. r_s1,keywrapkey,authkey,raw_m3_attrs,eap_id,bssid,attrs = wps_start_kwa(dev, apdev)
  6037. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6038. # Encrypted Settings and KWA with incorrect value
  6039. data += build_wsc_attr(ATTR_KEY_WRAP_AUTH, 8*'\x00')
  6040. iv = 16*'\x99'
  6041. aes = AES.new(keywrapkey, AES.MODE_CBC, iv)
  6042. pad_len = 16 - len(data) % 16
  6043. ps = pad_len * struct.pack('B', pad_len)
  6044. data += ps
  6045. wrapped = aes.encrypt(data)
  6046. attrs += build_wsc_attr(ATTR_ENCR_SETTINGS, iv + wrapped)
  6047. wps_stop_kwa(dev, bssid, attrs, authkey, raw_m3_attrs, eap_id)
  6048. def wps_run_cred_proto(dev, apdev, m8_cred, connect=False, no_connect=False):
  6049. pin = "12345670"
  6050. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  6051. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6052. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6053. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6054. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6055. uuid_r = 16*'\x33'
  6056. r_nonce = 16*'\x44'
  6057. own_private, e_pk = wsc_dh_init()
  6058. logger.debug("Receive M1 from STA")
  6059. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6060. eap_id = (msg['eap_identifier'] + 1) % 256
  6061. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6062. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6063. r_nonce)
  6064. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6065. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6066. logger.debug("Send M2 to STA")
  6067. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6068. m1_attrs[ATTR_ENROLLEE_NONCE],
  6069. r_nonce, uuid_r, e_pk)
  6070. send_wsc_msg(dev[0], bssid, m2)
  6071. eap_id = (eap_id + 1) % 256
  6072. logger.debug("Receive M3 from STA")
  6073. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M3)
  6074. logger.debug("Send M4 to STA")
  6075. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6076. attrs += build_attr_msg_type(WPS_M4)
  6077. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, m1_attrs[ATTR_ENROLLEE_NONCE])
  6078. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6079. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6080. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6081. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6082. attrs += build_attr_authenticator(authkey, raw_m3_attrs, attrs)
  6083. raw_m4_attrs = attrs
  6084. m4 = build_eap_wsc(1, eap_id, attrs)
  6085. send_wsc_msg(dev[0], bssid, m4)
  6086. eap_id = (eap_id + 1) % 256
  6087. logger.debug("Receive M5 from STA")
  6088. msg, m5_attrs, raw_m5_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M5)
  6089. logger.debug("Send M6 to STA")
  6090. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6091. attrs += build_attr_msg_type(WPS_M6)
  6092. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE,
  6093. m1_attrs[ATTR_ENROLLEE_NONCE])
  6094. data = build_wsc_attr(ATTR_R_SNONCE2, r_s2)
  6095. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6096. attrs += build_attr_authenticator(authkey, raw_m5_attrs, attrs)
  6097. raw_m6_attrs = attrs
  6098. m6 = build_eap_wsc(1, eap_id, attrs)
  6099. send_wsc_msg(dev[0], bssid, m6)
  6100. eap_id = (eap_id + 1) % 256
  6101. logger.debug("Receive M7 from STA")
  6102. msg, m7_attrs, raw_m7_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M7)
  6103. logger.debug("Send M8 to STA")
  6104. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6105. attrs += build_attr_msg_type(WPS_M8)
  6106. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE,
  6107. m1_attrs[ATTR_ENROLLEE_NONCE])
  6108. attrs += build_attr_encr_settings(authkey, keywrapkey, m8_cred)
  6109. attrs += build_attr_authenticator(authkey, raw_m7_attrs, attrs)
  6110. raw_m8_attrs = attrs
  6111. m8 = build_eap_wsc(1, eap_id, attrs)
  6112. send_wsc_msg(dev[0], bssid, m8)
  6113. eap_id = (eap_id + 1) % 256
  6114. if no_connect:
  6115. logger.debug("Receive WSC_Done from STA")
  6116. msg = get_wsc_msg(dev[0])
  6117. if msg['wsc_opcode'] != WSC_Done or msg['wsc_msg_type'] != WPS_WSC_DONE:
  6118. raise Exception("Unexpected Op-Code/MsgType for WSC_Done")
  6119. hapd.request("SET ext_eapol_frame_io 0")
  6120. dev[0].request("SET ext_eapol_frame_io 0")
  6121. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6122. dev[0].wait_disconnected()
  6123. dev[0].request("REMOVE_NETWORK all")
  6124. elif connect:
  6125. logger.debug("Receive WSC_Done from STA")
  6126. msg = get_wsc_msg(dev[0])
  6127. if msg['wsc_opcode'] != WSC_Done or msg['wsc_msg_type'] != WPS_WSC_DONE:
  6128. raise Exception("Unexpected Op-Code/MsgType for WSC_Done")
  6129. hapd.request("SET ext_eapol_frame_io 0")
  6130. dev[0].request("SET ext_eapol_frame_io 0")
  6131. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6132. dev[0].wait_connected()
  6133. else:
  6134. # Verify STA NACK's the credential
  6135. msg = get_wsc_msg(dev[0])
  6136. if msg['wsc_opcode'] != WSC_NACK:
  6137. raise Exception("Unexpected message - expected WSC_Nack")
  6138. dev[0].request("WPS_CANCEL")
  6139. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6140. dev[0].wait_disconnected()
  6141. def build_cred(nw_idx='\x01', ssid='test-wps-conf', auth_type='\x00\x20',
  6142. encr_type='\x00\x08', nw_key="12345678",
  6143. mac_addr='\x00\x00\x00\x00\x00\x00'):
  6144. attrs = ''
  6145. if nw_idx is not None:
  6146. attrs += build_wsc_attr(ATTR_NETWORK_INDEX, nw_idx)
  6147. if ssid is not None:
  6148. attrs += build_wsc_attr(ATTR_SSID, ssid)
  6149. if auth_type is not None:
  6150. attrs += build_wsc_attr(ATTR_AUTH_TYPE, auth_type)
  6151. if encr_type is not None:
  6152. attrs += build_wsc_attr(ATTR_ENCR_TYPE, encr_type)
  6153. if nw_key is not None:
  6154. attrs += build_wsc_attr(ATTR_NETWORK_KEY, nw_key)
  6155. if mac_addr is not None:
  6156. attrs += build_wsc_attr(ATTR_MAC_ADDR, mac_addr)
  6157. return build_wsc_attr(ATTR_CRED, attrs)
  6158. def test_wps_ext_cred_proto_success(dev, apdev):
  6159. """WPS and Credential: success"""
  6160. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6161. m8_cred = build_cred(mac_addr=mac_addr)
  6162. wps_run_cred_proto(dev, apdev, m8_cred, connect=True)
  6163. def test_wps_ext_cred_proto_mac_addr_mismatch(dev, apdev):
  6164. """WPS and Credential: MAC Address mismatch"""
  6165. m8_cred = build_cred()
  6166. wps_run_cred_proto(dev, apdev, m8_cred, connect=True)
  6167. def test_wps_ext_cred_proto_zero_padding(dev, apdev):
  6168. """WPS and Credential: zeropadded attributes"""
  6169. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6170. m8_cred = build_cred(mac_addr=mac_addr, ssid='test-wps-conf\x00',
  6171. nw_key="12345678\x00")
  6172. wps_run_cred_proto(dev, apdev, m8_cred, connect=True)
  6173. def test_wps_ext_cred_proto_ssid_missing(dev, apdev):
  6174. """WPS and Credential: SSID missing"""
  6175. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6176. m8_cred = build_cred(mac_addr=mac_addr, ssid=None)
  6177. wps_run_cred_proto(dev, apdev, m8_cred)
  6178. def test_wps_ext_cred_proto_ssid_zero_len(dev, apdev):
  6179. """WPS and Credential: Zero-length SSID"""
  6180. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6181. m8_cred = build_cred(mac_addr=mac_addr, ssid="")
  6182. wps_run_cred_proto(dev, apdev, m8_cred, no_connect=True)
  6183. def test_wps_ext_cred_proto_auth_type_missing(dev, apdev):
  6184. """WPS and Credential: Auth Type missing"""
  6185. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6186. m8_cred = build_cred(mac_addr=mac_addr, auth_type=None)
  6187. wps_run_cred_proto(dev, apdev, m8_cred)
  6188. def test_wps_ext_cred_proto_encr_type_missing(dev, apdev):
  6189. """WPS and Credential: Encr Type missing"""
  6190. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6191. m8_cred = build_cred(mac_addr=mac_addr, encr_type=None)
  6192. wps_run_cred_proto(dev, apdev, m8_cred)
  6193. def test_wps_ext_cred_proto_network_key_missing(dev, apdev):
  6194. """WPS and Credential: Network Key missing"""
  6195. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6196. m8_cred = build_cred(mac_addr=mac_addr, nw_key=None)
  6197. wps_run_cred_proto(dev, apdev, m8_cred)
  6198. def test_wps_ext_cred_proto_network_key_missing_open(dev, apdev):
  6199. """WPS and Credential: Network Key missing (open)"""
  6200. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6201. m8_cred = build_cred(mac_addr=mac_addr, auth_type='\x00\x01',
  6202. encr_type='\x00\x01', nw_key=None, ssid="foo")
  6203. wps_run_cred_proto(dev, apdev, m8_cred, no_connect=True)
  6204. def test_wps_ext_cred_proto_mac_addr_missing(dev, apdev):
  6205. """WPS and Credential: MAC Address missing"""
  6206. m8_cred = build_cred(mac_addr=None)
  6207. wps_run_cred_proto(dev, apdev, m8_cred)
  6208. def test_wps_ext_cred_proto_invalid_encr_type(dev, apdev):
  6209. """WPS and Credential: Invalid Encr Type"""
  6210. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6211. m8_cred = build_cred(mac_addr=mac_addr, encr_type='\x00\x00')
  6212. wps_run_cred_proto(dev, apdev, m8_cred)
  6213. def test_wps_ext_cred_proto_missing_cred(dev, apdev):
  6214. """WPS and Credential: Missing Credential"""
  6215. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6216. m8_cred = ''
  6217. wps_run_cred_proto(dev, apdev, m8_cred)
  6218. def test_wps_ext_proto_m2_no_public_key(dev, apdev):
  6219. """WPS and no Public Key in M2"""
  6220. pin = "12345670"
  6221. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  6222. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6223. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6224. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6225. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6226. uuid_r = 16*'\x33'
  6227. r_nonce = 16*'\x44'
  6228. own_private, e_pk = wsc_dh_init()
  6229. logger.debug("Receive M1 from STA")
  6230. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6231. eap_id = (msg['eap_identifier'] + 1) % 256
  6232. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6233. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6234. r_nonce)
  6235. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6236. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6237. logger.debug("Send M2 to STA")
  6238. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6239. m1_attrs[ATTR_ENROLLEE_NONCE],
  6240. r_nonce, uuid_r, None)
  6241. send_wsc_msg(dev[0], bssid, m2)
  6242. eap_id = (eap_id + 1) % 256
  6243. # Verify STA NACK's the credential
  6244. msg = get_wsc_msg(dev[0])
  6245. if msg['wsc_opcode'] != WSC_NACK:
  6246. raise Exception("Unexpected message - expected WSC_Nack")
  6247. dev[0].request("WPS_CANCEL")
  6248. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6249. dev[0].wait_disconnected()
  6250. def test_wps_ext_proto_m2_invalid_public_key(dev, apdev):
  6251. """WPS and invalid Public Key in M2"""
  6252. pin = "12345670"
  6253. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  6254. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6255. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6256. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6257. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6258. uuid_r = 16*'\x33'
  6259. r_nonce = 16*'\x44'
  6260. own_private, e_pk = wsc_dh_init()
  6261. logger.debug("Receive M1 from STA")
  6262. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6263. eap_id = (msg['eap_identifier'] + 1) % 256
  6264. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6265. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6266. r_nonce)
  6267. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6268. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6269. logger.debug("Send M2 to STA")
  6270. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6271. m1_attrs[ATTR_ENROLLEE_NONCE],
  6272. r_nonce, uuid_r, 192*'\xff')
  6273. send_wsc_msg(dev[0], bssid, m2)
  6274. eap_id = (eap_id + 1) % 256
  6275. # Verify STA NACK's the credential
  6276. msg = get_wsc_msg(dev[0])
  6277. if msg['wsc_opcode'] != WSC_NACK:
  6278. raise Exception("Unexpected message - expected WSC_Nack")
  6279. dev[0].request("WPS_CANCEL")
  6280. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6281. dev[0].wait_disconnected()
  6282. def test_wps_ext_proto_m2_public_key_oom(dev, apdev):
  6283. """WPS and Public Key OOM in M2"""
  6284. pin = "12345670"
  6285. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  6286. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6287. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6288. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6289. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6290. uuid_r = 16*'\x33'
  6291. r_nonce = 16*'\x44'
  6292. own_private, e_pk = wsc_dh_init()
  6293. logger.debug("Receive M1 from STA")
  6294. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6295. eap_id = (msg['eap_identifier'] + 1) % 256
  6296. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6297. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6298. r_nonce)
  6299. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6300. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6301. logger.debug("Send M2 to STA")
  6302. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6303. m1_attrs[ATTR_ENROLLEE_NONCE],
  6304. r_nonce, uuid_r, e_pk)
  6305. with alloc_fail(dev[0], 1, "wpabuf_alloc_copy;wps_process_pubkey"):
  6306. send_wsc_msg(dev[0], bssid, m2)
  6307. eap_id = (eap_id + 1) % 256
  6308. # Verify STA NACK's the credential
  6309. msg = get_wsc_msg(dev[0])
  6310. if msg['wsc_opcode'] != WSC_NACK:
  6311. raise Exception("Unexpected message - expected WSC_Nack")
  6312. dev[0].request("WPS_CANCEL")
  6313. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6314. dev[0].wait_disconnected()
  6315. def test_wps_ext_proto_nack_m3(dev, apdev):
  6316. """WPS and NACK M3"""
  6317. pin = "12345670"
  6318. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  6319. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6320. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6321. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6322. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6323. uuid_r = 16*'\x33'
  6324. r_nonce = 16*'\x44'
  6325. own_private, e_pk = wsc_dh_init()
  6326. logger.debug("Receive M1 from STA")
  6327. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6328. eap_id = (msg['eap_identifier'] + 1) % 256
  6329. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6330. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6331. r_nonce)
  6332. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6333. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6334. logger.debug("Send M2 to STA")
  6335. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6336. m1_attrs[ATTR_ENROLLEE_NONCE],
  6337. r_nonce, uuid_r, e_pk)
  6338. send_wsc_msg(dev[0], bssid, m2)
  6339. eap_id = (eap_id + 1) % 256
  6340. logger.debug("Receive M3 from STA")
  6341. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M3)
  6342. logger.debug("Send NACK to STA")
  6343. msg, attrs = build_nack(eap_id, m1_attrs[ATTR_ENROLLEE_NONCE],
  6344. r_nonce, config_error='\x01\x23')
  6345. send_wsc_msg(dev[0], bssid, msg)
  6346. ev = dev[0].wait_event(["WPS-FAIL"], timeout=5)
  6347. if ev is None:
  6348. raise Exception("Failure not reported")
  6349. if "msg=7 config_error=291" not in ev:
  6350. raise Exception("Unexpected failure reason: " + ev)
  6351. def test_wps_ext_proto_nack_m5(dev, apdev):
  6352. """WPS and NACK M5"""
  6353. pin = "12345670"
  6354. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  6355. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6356. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6357. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6358. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6359. uuid_r = 16*'\x33'
  6360. r_nonce = 16*'\x44'
  6361. own_private, e_pk = wsc_dh_init()
  6362. logger.debug("Receive M1 from STA")
  6363. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6364. eap_id = (msg['eap_identifier'] + 1) % 256
  6365. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6366. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6367. r_nonce)
  6368. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6369. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6370. logger.debug("Send M2 to STA")
  6371. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6372. m1_attrs[ATTR_ENROLLEE_NONCE],
  6373. r_nonce, uuid_r, e_pk)
  6374. send_wsc_msg(dev[0], bssid, m2)
  6375. eap_id = (eap_id + 1) % 256
  6376. logger.debug("Receive M3 from STA")
  6377. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M3)
  6378. logger.debug("Send M4 to STA")
  6379. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6380. attrs += build_attr_msg_type(WPS_M4)
  6381. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, m1_attrs[ATTR_ENROLLEE_NONCE])
  6382. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6383. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6384. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6385. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6386. attrs += build_attr_authenticator(authkey, raw_m3_attrs, attrs)
  6387. raw_m4_attrs = attrs
  6388. m4 = build_eap_wsc(1, eap_id, attrs)
  6389. send_wsc_msg(dev[0], bssid, m4)
  6390. eap_id = (eap_id + 1) % 256
  6391. logger.debug("Receive M5 from STA")
  6392. msg, m5_attrs, raw_m5_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M5)
  6393. logger.debug("Send NACK to STA")
  6394. msg, attrs = build_nack(eap_id, m1_attrs[ATTR_ENROLLEE_NONCE],
  6395. r_nonce, config_error='\x01\x24')
  6396. send_wsc_msg(dev[0], bssid, msg)
  6397. ev = dev[0].wait_event(["WPS-FAIL"], timeout=5)
  6398. if ev is None:
  6399. raise Exception("Failure not reported")
  6400. if "msg=9 config_error=292" not in ev:
  6401. raise Exception("Unexpected failure reason: " + ev)
  6402. def wps_nack_m3(dev, apdev):
  6403. pin = "00000000"
  6404. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pbc=True)
  6405. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6406. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6407. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6408. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6409. uuid_r = 16*'\x33'
  6410. r_nonce = 16*'\x44'
  6411. own_private, e_pk = wsc_dh_init()
  6412. logger.debug("Receive M1 from STA")
  6413. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6414. eap_id = (msg['eap_identifier'] + 1) % 256
  6415. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6416. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6417. r_nonce)
  6418. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6419. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6420. logger.debug("Send M2 to STA")
  6421. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6422. m1_attrs[ATTR_ENROLLEE_NONCE],
  6423. r_nonce, uuid_r, e_pk, dev_pw_id='\x00\x04')
  6424. send_wsc_msg(dev[0], bssid, m2)
  6425. eap_id = (eap_id + 1) % 256
  6426. logger.debug("Receive M3 from STA")
  6427. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M3)
  6428. return eap_id, m1_attrs[ATTR_ENROLLEE_NONCE], r_nonce, bssid
  6429. def test_wps_ext_proto_nack_m3_no_config_error(dev, apdev):
  6430. """WPS and NACK M3 missing Config Error"""
  6431. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6432. logger.debug("Send NACK to STA")
  6433. msg, attrs = build_nack(eap_id, e_nonce, r_nonce, config_error=None)
  6434. send_wsc_msg(dev[0], bssid, msg)
  6435. dev[0].request("WPS_CANCEL")
  6436. dev[0].wait_disconnected()
  6437. dev[0].flush_scan_cache()
  6438. def test_wps_ext_proto_nack_m3_no_e_nonce(dev, apdev):
  6439. """WPS and NACK M3 missing E-Nonce"""
  6440. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6441. logger.debug("Send NACK to STA")
  6442. msg, attrs = build_nack(eap_id, None, r_nonce)
  6443. send_wsc_msg(dev[0], bssid, msg)
  6444. dev[0].request("WPS_CANCEL")
  6445. dev[0].wait_disconnected()
  6446. dev[0].flush_scan_cache()
  6447. def test_wps_ext_proto_nack_m3_e_nonce_mismatch(dev, apdev):
  6448. """WPS and NACK M3 E-Nonce mismatch"""
  6449. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6450. logger.debug("Send NACK to STA")
  6451. msg, attrs = build_nack(eap_id, 16*'\x00', r_nonce)
  6452. send_wsc_msg(dev[0], bssid, msg)
  6453. dev[0].request("WPS_CANCEL")
  6454. dev[0].wait_disconnected()
  6455. dev[0].flush_scan_cache()
  6456. def test_wps_ext_proto_nack_m3_no_r_nonce(dev, apdev):
  6457. """WPS and NACK M3 missing R-Nonce"""
  6458. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6459. logger.debug("Send NACK to STA")
  6460. msg, attrs = build_nack(eap_id, e_nonce, None)
  6461. send_wsc_msg(dev[0], bssid, msg)
  6462. dev[0].request("WPS_CANCEL")
  6463. dev[0].wait_disconnected()
  6464. dev[0].flush_scan_cache()
  6465. def test_wps_ext_proto_nack_m3_r_nonce_mismatch(dev, apdev):
  6466. """WPS and NACK M3 R-Nonce mismatch"""
  6467. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6468. logger.debug("Send NACK to STA")
  6469. msg, attrs = build_nack(eap_id, e_nonce, 16*'\x00')
  6470. send_wsc_msg(dev[0], bssid, msg)
  6471. dev[0].request("WPS_CANCEL")
  6472. dev[0].wait_disconnected()
  6473. dev[0].flush_scan_cache()
  6474. def test_wps_ext_proto_nack_m3_no_msg_type(dev, apdev):
  6475. """WPS and NACK M3 no Message Type"""
  6476. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6477. logger.debug("Send NACK to STA")
  6478. msg, attrs = build_nack(eap_id, e_nonce, r_nonce, msg_type=None)
  6479. send_wsc_msg(dev[0], bssid, msg)
  6480. dev[0].request("WPS_CANCEL")
  6481. dev[0].wait_disconnected()
  6482. dev[0].flush_scan_cache()
  6483. def test_wps_ext_proto_nack_m3_invalid_msg_type(dev, apdev):
  6484. """WPS and NACK M3 invalid Message Type"""
  6485. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6486. logger.debug("Send NACK to STA")
  6487. msg, attrs = build_nack(eap_id, e_nonce, r_nonce, msg_type=123)
  6488. send_wsc_msg(dev[0], bssid, msg)
  6489. dev[0].request("WPS_CANCEL")
  6490. dev[0].wait_disconnected()
  6491. dev[0].flush_scan_cache()
  6492. def test_wps_ext_proto_nack_m3_invalid_attr(dev, apdev):
  6493. """WPS and NACK M3 invalid attribute"""
  6494. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6495. logger.debug("Send NACK to STA")
  6496. attrs = '\x10\x10\x00'
  6497. msg = build_eap_wsc(1, eap_id, attrs, opcode=WSC_NACK)
  6498. send_wsc_msg(dev[0], bssid, msg)
  6499. dev[0].request("WPS_CANCEL")
  6500. dev[0].wait_disconnected()
  6501. dev[0].flush_scan_cache()
  6502. def test_wps_ext_proto_ack_m3_no_e_nonce(dev, apdev):
  6503. """WPS and ACK M3 missing E-Nonce"""
  6504. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6505. logger.debug("Send NACK to STA")
  6506. msg, attrs = build_ack(eap_id, None, r_nonce)
  6507. send_wsc_msg(dev[0], bssid, msg)
  6508. dev[0].request("WPS_CANCEL")
  6509. dev[0].wait_disconnected()
  6510. dev[0].flush_scan_cache()
  6511. def test_wps_ext_proto_ack_m3_e_nonce_mismatch(dev, apdev):
  6512. """WPS and ACK M3 E-Nonce mismatch"""
  6513. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6514. logger.debug("Send NACK to STA")
  6515. msg, attrs = build_ack(eap_id, 16*'\x00', r_nonce)
  6516. send_wsc_msg(dev[0], bssid, msg)
  6517. dev[0].request("WPS_CANCEL")
  6518. dev[0].wait_disconnected()
  6519. dev[0].flush_scan_cache()
  6520. def test_wps_ext_proto_ack_m3_no_r_nonce(dev, apdev):
  6521. """WPS and ACK M3 missing R-Nonce"""
  6522. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6523. logger.debug("Send NACK to STA")
  6524. msg, attrs = build_ack(eap_id, e_nonce, None)
  6525. send_wsc_msg(dev[0], bssid, msg)
  6526. dev[0].request("WPS_CANCEL")
  6527. dev[0].wait_disconnected()
  6528. dev[0].flush_scan_cache()
  6529. def test_wps_ext_proto_ack_m3_r_nonce_mismatch(dev, apdev):
  6530. """WPS and ACK M3 R-Nonce mismatch"""
  6531. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6532. logger.debug("Send NACK to STA")
  6533. msg, attrs = build_ack(eap_id, e_nonce, 16*'\x00')
  6534. send_wsc_msg(dev[0], bssid, msg)
  6535. dev[0].request("WPS_CANCEL")
  6536. dev[0].wait_disconnected()
  6537. dev[0].flush_scan_cache()
  6538. def test_wps_ext_proto_ack_m3_no_msg_type(dev, apdev):
  6539. """WPS and ACK M3 no Message Type"""
  6540. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6541. logger.debug("Send NACK to STA")
  6542. msg, attrs = build_ack(eap_id, e_nonce, r_nonce, msg_type=None)
  6543. send_wsc_msg(dev[0], bssid, msg)
  6544. dev[0].request("WPS_CANCEL")
  6545. dev[0].wait_disconnected()
  6546. dev[0].flush_scan_cache()
  6547. def test_wps_ext_proto_ack_m3_invalid_msg_type(dev, apdev):
  6548. """WPS and ACK M3 invalid Message Type"""
  6549. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6550. logger.debug("Send NACK to STA")
  6551. msg, attrs = build_ack(eap_id, e_nonce, r_nonce, msg_type=123)
  6552. send_wsc_msg(dev[0], bssid, msg)
  6553. dev[0].request("WPS_CANCEL")
  6554. dev[0].wait_disconnected()
  6555. dev[0].flush_scan_cache()
  6556. def test_wps_ext_proto_ack_m3_invalid_attr(dev, apdev):
  6557. """WPS and ACK M3 invalid attribute"""
  6558. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6559. logger.debug("Send ACK to STA")
  6560. attrs = '\x10\x10\x00'
  6561. msg = build_eap_wsc(1, eap_id, attrs, opcode=WSC_ACK)
  6562. send_wsc_msg(dev[0], bssid, msg)
  6563. dev[0].request("WPS_CANCEL")
  6564. dev[0].wait_disconnected()
  6565. dev[0].flush_scan_cache()
  6566. def test_wps_ext_proto_ack_m3(dev, apdev):
  6567. """WPS and ACK M3"""
  6568. eap_id, e_nonce, r_nonce, bssid = wps_nack_m3(dev, apdev)
  6569. logger.debug("Send ACK to STA")
  6570. msg, attrs = build_ack(eap_id, e_nonce, r_nonce)
  6571. send_wsc_msg(dev[0], bssid, msg)
  6572. dev[0].request("WPS_CANCEL")
  6573. dev[0].wait_disconnected()
  6574. dev[0].flush_scan_cache()
  6575. def wps_to_m3_helper(dev, apdev):
  6576. pin = "12345670"
  6577. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  6578. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6579. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6580. wps_ext_eap_wsc(dev[0], hapd, bssid, "EAP-WSC/Start")
  6581. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  6582. uuid_r = 16*'\x33'
  6583. r_nonce = 16*'\x44'
  6584. own_private, e_pk = wsc_dh_init()
  6585. logger.debug("Receive M1 from STA")
  6586. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M1)
  6587. eap_id = (msg['eap_identifier'] + 1) % 256
  6588. authkey,keywrapkey = wsc_dh_kdf(m1_attrs[ATTR_PUBLIC_KEY], own_private,
  6589. mac_addr, m1_attrs[ATTR_ENROLLEE_NONCE],
  6590. r_nonce)
  6591. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, pin,
  6592. m1_attrs[ATTR_PUBLIC_KEY], e_pk)
  6593. logger.debug("Send M2 to STA")
  6594. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, eap_id,
  6595. m1_attrs[ATTR_ENROLLEE_NONCE],
  6596. r_nonce, uuid_r, e_pk)
  6597. send_wsc_msg(dev[0], bssid, m2)
  6598. eap_id = (eap_id + 1) % 256
  6599. logger.debug("Receive M3 from STA")
  6600. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M3)
  6601. return eap_id, m1_attrs, r_nonce, bssid, r_hash1, r_hash2, r_s1, r_s2, raw_m3_attrs, authkey, keywrapkey
  6602. def wps_to_m3(dev, apdev):
  6603. 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)
  6604. return eap_id, m1_attrs[ATTR_ENROLLEE_NONCE], r_nonce, bssid, r_hash1, r_hash2, r_s1, raw_m3_attrs, authkey, keywrapkey
  6605. def wps_to_m5(dev, apdev):
  6606. 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)
  6607. logger.debug("Send M4 to STA")
  6608. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6609. attrs += build_attr_msg_type(WPS_M4)
  6610. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, m1_attrs[ATTR_ENROLLEE_NONCE])
  6611. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6612. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6613. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6614. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6615. attrs += build_attr_authenticator(authkey, raw_m3_attrs, attrs)
  6616. raw_m4_attrs = attrs
  6617. m4 = build_eap_wsc(1, eap_id, attrs)
  6618. send_wsc_msg(dev[0], bssid, m4)
  6619. eap_id = (eap_id + 1) % 256
  6620. logger.debug("Receive M5 from STA")
  6621. msg, m5_attrs, raw_m5_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M5)
  6622. return eap_id, m1_attrs[ATTR_ENROLLEE_NONCE], r_nonce, bssid, r_hash1, r_hash2, r_s2, raw_m5_attrs, authkey, keywrapkey
  6623. def test_wps_ext_proto_m4_missing_r_hash1(dev, apdev):
  6624. """WPS and no R-Hash1 in M4"""
  6625. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s1, m3, authkey, keywrapkey = wps_to_m3(dev, apdev)
  6626. logger.debug("Send M4 to STA")
  6627. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6628. attrs += build_attr_msg_type(WPS_M4)
  6629. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6630. #attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6631. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6632. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6633. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6634. attrs += build_attr_authenticator(authkey, m3, attrs)
  6635. m4 = build_eap_wsc(1, eap_id, attrs)
  6636. send_wsc_msg(dev[0], bssid, m4)
  6637. eap_id = (eap_id + 1) % 256
  6638. logger.debug("Receive M5 (NACK) from STA")
  6639. msg = get_wsc_msg(dev[0])
  6640. if msg['wsc_opcode'] != WSC_NACK:
  6641. raise Exception("Unexpected message - expected WSC_Nack")
  6642. dev[0].request("WPS_CANCEL")
  6643. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6644. dev[0].wait_disconnected()
  6645. def test_wps_ext_proto_m4_missing_r_hash2(dev, apdev):
  6646. """WPS and no R-Hash2 in M4"""
  6647. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s1, m3, authkey, keywrapkey = wps_to_m3(dev, apdev)
  6648. logger.debug("Send M4 to STA")
  6649. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6650. attrs += build_attr_msg_type(WPS_M4)
  6651. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6652. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6653. #attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6654. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6655. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6656. attrs += build_attr_authenticator(authkey, m3, attrs)
  6657. m4 = build_eap_wsc(1, eap_id, attrs)
  6658. send_wsc_msg(dev[0], bssid, m4)
  6659. eap_id = (eap_id + 1) % 256
  6660. logger.debug("Receive M5 (NACK) from STA")
  6661. msg = get_wsc_msg(dev[0])
  6662. if msg['wsc_opcode'] != WSC_NACK:
  6663. raise Exception("Unexpected message - expected WSC_Nack")
  6664. dev[0].request("WPS_CANCEL")
  6665. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6666. dev[0].wait_disconnected()
  6667. def test_wps_ext_proto_m4_missing_r_snonce1(dev, apdev):
  6668. """WPS and no R-SNonce1 in M4"""
  6669. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s1, m3, authkey, keywrapkey = wps_to_m3(dev, apdev)
  6670. logger.debug("Send M4 to STA")
  6671. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6672. attrs += build_attr_msg_type(WPS_M4)
  6673. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6674. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6675. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6676. #data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6677. data = ''
  6678. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6679. attrs += build_attr_authenticator(authkey, m3, attrs)
  6680. m4 = build_eap_wsc(1, eap_id, attrs)
  6681. send_wsc_msg(dev[0], bssid, m4)
  6682. eap_id = (eap_id + 1) % 256
  6683. logger.debug("Receive M5 (NACK) from STA")
  6684. msg = get_wsc_msg(dev[0])
  6685. if msg['wsc_opcode'] != WSC_NACK:
  6686. raise Exception("Unexpected message - expected WSC_Nack")
  6687. dev[0].request("WPS_CANCEL")
  6688. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6689. dev[0].wait_disconnected()
  6690. def test_wps_ext_proto_m4_invalid_pad_string(dev, apdev):
  6691. """WPS and invalid pad string in M4"""
  6692. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s1, m3, authkey, keywrapkey = wps_to_m3(dev, apdev)
  6693. logger.debug("Send M4 to STA")
  6694. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6695. attrs += build_attr_msg_type(WPS_M4)
  6696. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6697. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6698. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6699. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6700. m = hmac.new(authkey, data, hashlib.sha256)
  6701. kwa = m.digest()[0:8]
  6702. data += build_wsc_attr(ATTR_KEY_WRAP_AUTH, kwa)
  6703. iv = 16*'\x99'
  6704. aes = AES.new(keywrapkey, AES.MODE_CBC, iv)
  6705. pad_len = 16 - len(data) % 16
  6706. ps = (pad_len - 1) * struct.pack('B', pad_len) + struct.pack('B', pad_len - 1)
  6707. data += ps
  6708. wrapped = aes.encrypt(data)
  6709. attrs += build_wsc_attr(ATTR_ENCR_SETTINGS, iv + wrapped)
  6710. attrs += build_attr_authenticator(authkey, m3, attrs)
  6711. m4 = build_eap_wsc(1, eap_id, attrs)
  6712. send_wsc_msg(dev[0], bssid, m4)
  6713. eap_id = (eap_id + 1) % 256
  6714. logger.debug("Receive M5 (NACK) from STA")
  6715. msg = get_wsc_msg(dev[0])
  6716. if msg['wsc_opcode'] != WSC_NACK:
  6717. raise Exception("Unexpected message - expected WSC_Nack")
  6718. dev[0].request("WPS_CANCEL")
  6719. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6720. dev[0].wait_disconnected()
  6721. def test_wps_ext_proto_m4_invalid_pad_value(dev, apdev):
  6722. """WPS and invalid pad value in M4"""
  6723. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s1, m3, authkey, keywrapkey = wps_to_m3(dev, apdev)
  6724. logger.debug("Send M4 to STA")
  6725. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6726. attrs += build_attr_msg_type(WPS_M4)
  6727. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6728. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6729. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6730. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6731. m = hmac.new(authkey, data, hashlib.sha256)
  6732. kwa = m.digest()[0:8]
  6733. data += build_wsc_attr(ATTR_KEY_WRAP_AUTH, kwa)
  6734. iv = 16*'\x99'
  6735. aes = AES.new(keywrapkey, AES.MODE_CBC, iv)
  6736. pad_len = 16 - len(data) % 16
  6737. ps = (pad_len - 1) * struct.pack('B', pad_len) + struct.pack('B', 255)
  6738. data += ps
  6739. wrapped = aes.encrypt(data)
  6740. attrs += build_wsc_attr(ATTR_ENCR_SETTINGS, iv + wrapped)
  6741. attrs += build_attr_authenticator(authkey, m3, attrs)
  6742. m4 = build_eap_wsc(1, eap_id, attrs)
  6743. send_wsc_msg(dev[0], bssid, m4)
  6744. eap_id = (eap_id + 1) % 256
  6745. logger.debug("Receive M5 (NACK) from STA")
  6746. msg = get_wsc_msg(dev[0])
  6747. if msg['wsc_opcode'] != WSC_NACK:
  6748. raise Exception("Unexpected message - expected WSC_Nack")
  6749. dev[0].request("WPS_CANCEL")
  6750. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6751. dev[0].wait_disconnected()
  6752. def test_wps_ext_proto_m4_no_encr_settings(dev, apdev):
  6753. """WPS and no Encr Settings in M4"""
  6754. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s1, m3, authkey, keywrapkey = wps_to_m3(dev, apdev)
  6755. logger.debug("Send M4 to STA")
  6756. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6757. attrs += build_attr_msg_type(WPS_M4)
  6758. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6759. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6760. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6761. attrs += build_attr_authenticator(authkey, m3, attrs)
  6762. m4 = build_eap_wsc(1, eap_id, attrs)
  6763. send_wsc_msg(dev[0], bssid, m4)
  6764. eap_id = (eap_id + 1) % 256
  6765. logger.debug("Receive M5 (NACK) from STA")
  6766. msg = get_wsc_msg(dev[0])
  6767. if msg['wsc_opcode'] != WSC_NACK:
  6768. raise Exception("Unexpected message - expected WSC_Nack")
  6769. dev[0].request("WPS_CANCEL")
  6770. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6771. dev[0].wait_disconnected()
  6772. def test_wps_ext_proto_m6_missing_r_snonce2(dev, apdev):
  6773. """WPS and no R-SNonce2 in M6"""
  6774. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s2, m5, authkey, keywrapkey = wps_to_m5(dev, apdev)
  6775. logger.debug("Send M6 to STA")
  6776. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6777. attrs += build_attr_msg_type(WPS_M6)
  6778. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6779. #data = build_wsc_attr(ATTR_R_SNONCE2, r_s2)
  6780. data = ''
  6781. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6782. attrs += build_attr_authenticator(authkey, m5, attrs)
  6783. m6 = build_eap_wsc(1, eap_id, attrs)
  6784. send_wsc_msg(dev[0], bssid, m6)
  6785. eap_id = (eap_id + 1) % 256
  6786. logger.debug("Receive M7 (NACK) from STA")
  6787. msg = get_wsc_msg(dev[0])
  6788. if msg['wsc_opcode'] != WSC_NACK:
  6789. raise Exception("Unexpected message - expected WSC_Nack")
  6790. dev[0].request("WPS_CANCEL")
  6791. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6792. dev[0].wait_disconnected()
  6793. def test_wps_ext_proto_m6_no_encr_settings(dev, apdev):
  6794. """WPS and no Encr Settings in M6"""
  6795. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s2, m5, authkey, keywrapkey = wps_to_m5(dev, apdev)
  6796. logger.debug("Send M6 to STA")
  6797. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6798. attrs += build_attr_msg_type(WPS_M6)
  6799. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6800. data = build_wsc_attr(ATTR_R_SNONCE2, r_s2)
  6801. #attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6802. attrs += build_attr_authenticator(authkey, m5, attrs)
  6803. m6 = build_eap_wsc(1, eap_id, attrs)
  6804. send_wsc_msg(dev[0], bssid, m6)
  6805. eap_id = (eap_id + 1) % 256
  6806. logger.debug("Receive M7 (NACK) from STA")
  6807. msg = get_wsc_msg(dev[0])
  6808. if msg['wsc_opcode'] != WSC_NACK:
  6809. raise Exception("Unexpected message - expected WSC_Nack")
  6810. dev[0].request("WPS_CANCEL")
  6811. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6812. dev[0].wait_disconnected()
  6813. def test_wps_ext_proto_m8_no_encr_settings(dev, apdev):
  6814. """WPS and no Encr Settings in M6"""
  6815. eap_id, e_nonce, r_nonce, bssid, r_hash1, r_hash2, r_s2, m5, authkey, keywrapkey = wps_to_m5(dev, apdev)
  6816. logger.debug("Send M6 to STA")
  6817. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6818. attrs += build_attr_msg_type(WPS_M6)
  6819. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6820. data = build_wsc_attr(ATTR_R_SNONCE2, r_s2)
  6821. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6822. attrs += build_attr_authenticator(authkey, m5, attrs)
  6823. raw_m6_attrs = attrs
  6824. m6 = build_eap_wsc(1, eap_id, attrs)
  6825. send_wsc_msg(dev[0], bssid, m6)
  6826. eap_id = (eap_id + 1) % 256
  6827. logger.debug("Receive M7 from STA")
  6828. msg, m7_attrs, raw_m7_attrs = recv_wsc_msg(dev[0], WSC_MSG, WPS_M7)
  6829. logger.debug("Send M8 to STA")
  6830. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6831. attrs += build_attr_msg_type(WPS_M8)
  6832. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6833. #attrs += build_attr_encr_settings(authkey, keywrapkey, m8_cred)
  6834. attrs += build_attr_authenticator(authkey, raw_m7_attrs, attrs)
  6835. raw_m8_attrs = attrs
  6836. m8 = build_eap_wsc(1, eap_id, attrs)
  6837. send_wsc_msg(dev[0], bssid, m8)
  6838. logger.debug("Receive WSC_Done (NACK) from STA")
  6839. msg = get_wsc_msg(dev[0])
  6840. if msg['wsc_opcode'] != WSC_NACK:
  6841. raise Exception("Unexpected message - expected WSC_Nack")
  6842. dev[0].request("WPS_CANCEL")
  6843. send_wsc_msg(dev[0], bssid, build_eap_failure(eap_id))
  6844. dev[0].wait_disconnected()
  6845. def wps_start_ext_reg(apdev, dev):
  6846. addr = dev.own_addr()
  6847. bssid = apdev['bssid']
  6848. ssid = "test-wps-conf"
  6849. appin = "12345670"
  6850. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  6851. "wpa_passphrase": "12345678", "wpa": "2",
  6852. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  6853. "ap_pin": appin }
  6854. hapd = hostapd.add_ap(apdev, params)
  6855. dev.scan_for_bss(bssid, freq="2412")
  6856. hapd.request("SET ext_eapol_frame_io 1")
  6857. dev.request("SET ext_eapol_frame_io 1")
  6858. dev.request("WPS_REG " + bssid + " " + appin)
  6859. return addr,bssid,hapd
  6860. def wps_run_ap_settings_proto(dev, apdev, ap_settings, success):
  6861. addr,bssid,hapd = wps_start_ext_reg(apdev[0], dev[0])
  6862. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6863. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6864. logger.debug("Receive M1 from AP")
  6865. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M1)
  6866. mac_addr = m1_attrs[ATTR_MAC_ADDR]
  6867. e_nonce = m1_attrs[ATTR_ENROLLEE_NONCE]
  6868. e_pk = m1_attrs[ATTR_PUBLIC_KEY]
  6869. appin = '12345670'
  6870. uuid_r = 16*'\x33'
  6871. r_nonce = 16*'\x44'
  6872. own_private, r_pk = wsc_dh_init()
  6873. authkey,keywrapkey = wsc_dh_kdf(e_pk, own_private, mac_addr, e_nonce,
  6874. r_nonce)
  6875. r_s1,r_s2,r_hash1,r_hash2 = wsc_dev_pw_hash(authkey, appin, e_pk, r_pk)
  6876. logger.debug("Send M2 to AP")
  6877. m2, raw_m2_attrs = build_m2(authkey, raw_m1_attrs, msg['eap_identifier'],
  6878. e_nonce, r_nonce, uuid_r, r_pk, eap_code=2)
  6879. send_wsc_msg(hapd, addr, m2)
  6880. logger.debug("Receive M3 from AP")
  6881. msg, m3_attrs, raw_m3_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M3)
  6882. logger.debug("Send M4 to AP")
  6883. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6884. attrs += build_attr_msg_type(WPS_M4)
  6885. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6886. attrs += build_wsc_attr(ATTR_R_HASH1, r_hash1)
  6887. attrs += build_wsc_attr(ATTR_R_HASH2, r_hash2)
  6888. data = build_wsc_attr(ATTR_R_SNONCE1, r_s1)
  6889. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6890. attrs += build_attr_authenticator(authkey, raw_m3_attrs, attrs)
  6891. raw_m4_attrs = attrs
  6892. m4 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  6893. send_wsc_msg(hapd, addr, m4)
  6894. logger.debug("Receive M5 from AP")
  6895. msg, m5_attrs, raw_m5_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M5)
  6896. logger.debug("Send M6 to STA")
  6897. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6898. attrs += build_attr_msg_type(WPS_M6)
  6899. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6900. data = build_wsc_attr(ATTR_R_SNONCE2, r_s2)
  6901. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  6902. attrs += build_attr_authenticator(authkey, raw_m5_attrs, attrs)
  6903. raw_m6_attrs = attrs
  6904. m6 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  6905. send_wsc_msg(hapd, addr, m6)
  6906. logger.debug("Receive M7 from AP")
  6907. msg, m7_attrs, raw_m7_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M7)
  6908. logger.debug("Send M8 to STA")
  6909. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  6910. attrs += build_attr_msg_type(WPS_M8)
  6911. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  6912. if ap_settings:
  6913. attrs += build_attr_encr_settings(authkey, keywrapkey, ap_settings)
  6914. attrs += build_attr_authenticator(authkey, raw_m7_attrs, attrs)
  6915. raw_m8_attrs = attrs
  6916. m8 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  6917. send_wsc_msg(hapd, addr, m8)
  6918. if success:
  6919. ev = hapd.wait_event(["WPS-NEW-AP-SETTINGS"], timeout=5)
  6920. if ev is None:
  6921. raise Exception("New AP settings not reported")
  6922. logger.debug("Receive WSC_Done from AP")
  6923. msg = get_wsc_msg(hapd)
  6924. if msg['wsc_opcode'] != WSC_Done:
  6925. raise Exception("Unexpected message - expected WSC_Done")
  6926. logger.debug("Send WSC_ACK to AP")
  6927. ack,attrs = build_ack(msg['eap_identifier'], e_nonce, r_nonce,
  6928. eap_code=2)
  6929. send_wsc_msg(hapd, addr, ack)
  6930. dev[0].wait_disconnected()
  6931. else:
  6932. ev = hapd.wait_event(["WPS-FAIL"], timeout=5)
  6933. if ev is None:
  6934. raise Exception("WPS failure not reported")
  6935. logger.debug("Receive WSC_NACK from AP")
  6936. msg = get_wsc_msg(hapd)
  6937. if msg['wsc_opcode'] != WSC_NACK:
  6938. raise Exception("Unexpected message - expected WSC_NACK")
  6939. logger.debug("Send WSC_NACK to AP")
  6940. nack,attrs = build_nack(msg['eap_identifier'], e_nonce, r_nonce,
  6941. eap_code=2)
  6942. send_wsc_msg(hapd, addr, nack)
  6943. dev[0].wait_disconnected()
  6944. def test_wps_ext_ap_settings_success(dev, apdev):
  6945. """WPS and AP Settings: success"""
  6946. ap_settings = build_wsc_attr(ATTR_NETWORK_INDEX, '\x01')
  6947. ap_settings += build_wsc_attr(ATTR_SSID, "test")
  6948. ap_settings += build_wsc_attr(ATTR_AUTH_TYPE, '\x00\x01')
  6949. ap_settings += build_wsc_attr(ATTR_ENCR_TYPE, '\x00\x01')
  6950. ap_settings += build_wsc_attr(ATTR_NETWORK_KEY, '')
  6951. ap_settings += build_wsc_attr(ATTR_MAC_ADDR, binascii.unhexlify(apdev[0]['bssid'].replace(':', '')))
  6952. wps_run_ap_settings_proto(dev, apdev, ap_settings, True)
  6953. @remote_compatible
  6954. def test_wps_ext_ap_settings_missing(dev, apdev):
  6955. """WPS and AP Settings: missing"""
  6956. wps_run_ap_settings_proto(dev, apdev, None, False)
  6957. @remote_compatible
  6958. def test_wps_ext_ap_settings_mac_addr_mismatch(dev, apdev):
  6959. """WPS and AP Settings: MAC Address mismatch"""
  6960. ap_settings = build_wsc_attr(ATTR_NETWORK_INDEX, '\x01')
  6961. ap_settings += build_wsc_attr(ATTR_SSID, "test")
  6962. ap_settings += build_wsc_attr(ATTR_AUTH_TYPE, '\x00\x01')
  6963. ap_settings += build_wsc_attr(ATTR_ENCR_TYPE, '\x00\x01')
  6964. ap_settings += build_wsc_attr(ATTR_NETWORK_KEY, '')
  6965. ap_settings += build_wsc_attr(ATTR_MAC_ADDR, '\x00\x00\x00\x00\x00\x00')
  6966. wps_run_ap_settings_proto(dev, apdev, ap_settings, True)
  6967. @remote_compatible
  6968. def test_wps_ext_ap_settings_mac_addr_missing(dev, apdev):
  6969. """WPS and AP Settings: missing MAC Address"""
  6970. ap_settings = build_wsc_attr(ATTR_NETWORK_INDEX, '\x01')
  6971. ap_settings += build_wsc_attr(ATTR_SSID, "test")
  6972. ap_settings += build_wsc_attr(ATTR_AUTH_TYPE, '\x00\x01')
  6973. ap_settings += build_wsc_attr(ATTR_ENCR_TYPE, '\x00\x01')
  6974. ap_settings += build_wsc_attr(ATTR_NETWORK_KEY, '')
  6975. wps_run_ap_settings_proto(dev, apdev, ap_settings, False)
  6976. @remote_compatible
  6977. def test_wps_ext_ap_settings_reject_encr_type(dev, apdev):
  6978. """WPS and AP Settings: reject Encr Type"""
  6979. ap_settings = build_wsc_attr(ATTR_NETWORK_INDEX, '\x01')
  6980. ap_settings += build_wsc_attr(ATTR_SSID, "test")
  6981. ap_settings += build_wsc_attr(ATTR_AUTH_TYPE, '\x00\x01')
  6982. ap_settings += build_wsc_attr(ATTR_ENCR_TYPE, '\x00\x00')
  6983. ap_settings += build_wsc_attr(ATTR_NETWORK_KEY, '')
  6984. ap_settings += build_wsc_attr(ATTR_MAC_ADDR, binascii.unhexlify(apdev[0]['bssid'].replace(':', '')))
  6985. wps_run_ap_settings_proto(dev, apdev, ap_settings, False)
  6986. @remote_compatible
  6987. def test_wps_ext_ap_settings_m2d(dev, apdev):
  6988. """WPS and AP Settings: M2D"""
  6989. addr,bssid,hapd = wps_start_ext_reg(apdev[0], dev[0])
  6990. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  6991. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  6992. logger.debug("Receive M1 from AP")
  6993. msg, m1_attrs, raw_m1_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M1)
  6994. e_nonce = m1_attrs[ATTR_ENROLLEE_NONCE]
  6995. r_nonce = 16*'\x44'
  6996. uuid_r = 16*'\x33'
  6997. logger.debug("Send M2D to AP")
  6998. m2d, raw_m2d_attrs = build_m2d(raw_m1_attrs, msg['eap_identifier'],
  6999. e_nonce, r_nonce, uuid_r,
  7000. dev_pw_id='\x00\x00', eap_code=2)
  7001. send_wsc_msg(hapd, addr, m2d)
  7002. ev = hapd.wait_event(["WPS-M2D"], timeout=5)
  7003. if ev is None:
  7004. raise Exception("M2D not reported")
  7005. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7006. def wps_wait_ap_nack(hapd, dev, e_nonce, r_nonce):
  7007. logger.debug("Receive WSC_NACK from AP")
  7008. msg = get_wsc_msg(hapd)
  7009. if msg['wsc_opcode'] != WSC_NACK:
  7010. raise Exception("Unexpected message - expected WSC_NACK")
  7011. logger.debug("Send WSC_NACK to AP")
  7012. nack,attrs = build_nack(msg['eap_identifier'], e_nonce, r_nonce,
  7013. eap_code=2)
  7014. send_wsc_msg(hapd, dev.own_addr(), nack)
  7015. dev.wait_disconnected()
  7016. @remote_compatible
  7017. def test_wps_ext_m3_missing_e_hash1(dev, apdev):
  7018. """WPS proto: M3 missing E-Hash1"""
  7019. pin = "12345670"
  7020. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7021. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7022. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7023. logger.debug("Receive WSC/Start from AP")
  7024. msg = get_wsc_msg(hapd)
  7025. if msg['wsc_opcode'] != WSC_Start:
  7026. raise Exception("Unexpected Op-Code for WSC/Start")
  7027. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7028. uuid_e = 16*'\x11'
  7029. e_nonce = 16*'\x22'
  7030. own_private, e_pk = wsc_dh_init()
  7031. logger.debug("Send M1 to AP")
  7032. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7033. e_nonce, e_pk)
  7034. send_wsc_msg(hapd, addr, m1)
  7035. logger.debug("Receive M2 from AP")
  7036. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7037. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7038. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7039. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7040. r_nonce)
  7041. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7042. logger.debug("Send M3 to AP")
  7043. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7044. attrs += build_attr_msg_type(WPS_M3)
  7045. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7046. #attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7047. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7048. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7049. raw_m3_attrs = attrs
  7050. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7051. send_wsc_msg(hapd, addr, m3)
  7052. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7053. @remote_compatible
  7054. def test_wps_ext_m3_missing_e_hash2(dev, apdev):
  7055. """WPS proto: M3 missing E-Hash2"""
  7056. pin = "12345670"
  7057. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7058. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7059. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7060. logger.debug("Receive WSC/Start from AP")
  7061. msg = get_wsc_msg(hapd)
  7062. if msg['wsc_opcode'] != WSC_Start:
  7063. raise Exception("Unexpected Op-Code for WSC/Start")
  7064. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7065. uuid_e = 16*'\x11'
  7066. e_nonce = 16*'\x22'
  7067. own_private, e_pk = wsc_dh_init()
  7068. logger.debug("Send M1 to AP")
  7069. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7070. e_nonce, e_pk)
  7071. send_wsc_msg(hapd, addr, m1)
  7072. logger.debug("Receive M2 from AP")
  7073. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7074. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7075. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7076. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7077. r_nonce)
  7078. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7079. logger.debug("Send M3 to AP")
  7080. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7081. attrs += build_attr_msg_type(WPS_M3)
  7082. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7083. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7084. #attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7085. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7086. raw_m3_attrs = attrs
  7087. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7088. send_wsc_msg(hapd, addr, m3)
  7089. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7090. @remote_compatible
  7091. def test_wps_ext_m5_missing_e_snonce1(dev, apdev):
  7092. """WPS proto: M5 missing E-SNonce1"""
  7093. pin = "12345670"
  7094. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7095. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7096. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7097. logger.debug("Receive WSC/Start from AP")
  7098. msg = get_wsc_msg(hapd)
  7099. if msg['wsc_opcode'] != WSC_Start:
  7100. raise Exception("Unexpected Op-Code for WSC/Start")
  7101. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7102. uuid_e = 16*'\x11'
  7103. e_nonce = 16*'\x22'
  7104. own_private, e_pk = wsc_dh_init()
  7105. logger.debug("Send M1 to AP")
  7106. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7107. e_nonce, e_pk)
  7108. send_wsc_msg(hapd, addr, m1)
  7109. logger.debug("Receive M2 from AP")
  7110. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7111. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7112. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7113. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7114. r_nonce)
  7115. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7116. logger.debug("Send M3 to AP")
  7117. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7118. attrs += build_attr_msg_type(WPS_M3)
  7119. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7120. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7121. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7122. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7123. raw_m3_attrs = attrs
  7124. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7125. send_wsc_msg(hapd, addr, m3)
  7126. logger.debug("Receive M4 from AP")
  7127. msg, m4_attrs, raw_m4_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M4)
  7128. logger.debug("Send M5 to AP")
  7129. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7130. attrs += build_attr_msg_type(WPS_M5)
  7131. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7132. #data = build_wsc_attr(ATTR_E_SNONCE1, e_s1)
  7133. data = ''
  7134. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7135. attrs += build_attr_authenticator(authkey, raw_m4_attrs, attrs)
  7136. raw_m5_attrs = attrs
  7137. m5 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7138. send_wsc_msg(hapd, addr, m5)
  7139. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7140. @remote_compatible
  7141. def test_wps_ext_m5_e_snonce1_mismatch(dev, apdev):
  7142. """WPS proto: M5 E-SNonce1 mismatch"""
  7143. pin = "12345670"
  7144. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7145. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7146. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7147. logger.debug("Receive WSC/Start from AP")
  7148. msg = get_wsc_msg(hapd)
  7149. if msg['wsc_opcode'] != WSC_Start:
  7150. raise Exception("Unexpected Op-Code for WSC/Start")
  7151. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7152. uuid_e = 16*'\x11'
  7153. e_nonce = 16*'\x22'
  7154. own_private, e_pk = wsc_dh_init()
  7155. logger.debug("Send M1 to AP")
  7156. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7157. e_nonce, e_pk)
  7158. send_wsc_msg(hapd, addr, m1)
  7159. logger.debug("Receive M2 from AP")
  7160. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7161. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7162. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7163. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7164. r_nonce)
  7165. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7166. logger.debug("Send M3 to AP")
  7167. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7168. attrs += build_attr_msg_type(WPS_M3)
  7169. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7170. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7171. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7172. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7173. raw_m3_attrs = attrs
  7174. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7175. send_wsc_msg(hapd, addr, m3)
  7176. logger.debug("Receive M4 from AP")
  7177. msg, m4_attrs, raw_m4_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M4)
  7178. logger.debug("Send M5 to AP")
  7179. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7180. attrs += build_attr_msg_type(WPS_M5)
  7181. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7182. data = build_wsc_attr(ATTR_E_SNONCE1, 16*'\x00')
  7183. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7184. attrs += build_attr_authenticator(authkey, raw_m4_attrs, attrs)
  7185. raw_m5_attrs = attrs
  7186. m5 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7187. send_wsc_msg(hapd, addr, m5)
  7188. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7189. def test_wps_ext_m7_missing_e_snonce2(dev, apdev):
  7190. """WPS proto: M7 missing E-SNonce2"""
  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, e_s1)
  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. logger.debug("Receive M6 from AP")
  7237. msg, m6_attrs, raw_m6_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M6)
  7238. logger.debug("Send M7 to AP")
  7239. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7240. attrs += build_attr_msg_type(WPS_M7)
  7241. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7242. #data = build_wsc_attr(ATTR_E_SNONCE2, e_s2)
  7243. data = ''
  7244. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7245. attrs += build_attr_authenticator(authkey, raw_m6_attrs, attrs)
  7246. m7 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7247. raw_m7_attrs = attrs
  7248. send_wsc_msg(hapd, addr, m7)
  7249. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7250. @remote_compatible
  7251. def test_wps_ext_m7_e_snonce2_mismatch(dev, apdev):
  7252. """WPS proto: M7 E-SNonce2 mismatch"""
  7253. pin = "12345670"
  7254. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7255. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7256. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7257. logger.debug("Receive WSC/Start from AP")
  7258. msg = get_wsc_msg(hapd)
  7259. if msg['wsc_opcode'] != WSC_Start:
  7260. raise Exception("Unexpected Op-Code for WSC/Start")
  7261. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7262. uuid_e = 16*'\x11'
  7263. e_nonce = 16*'\x22'
  7264. own_private, e_pk = wsc_dh_init()
  7265. logger.debug("Send M1 to AP")
  7266. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7267. e_nonce, e_pk)
  7268. send_wsc_msg(hapd, addr, m1)
  7269. logger.debug("Receive M2 from AP")
  7270. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7271. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7272. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7273. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7274. r_nonce)
  7275. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7276. logger.debug("Send M3 to AP")
  7277. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7278. attrs += build_attr_msg_type(WPS_M3)
  7279. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7280. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7281. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7282. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7283. raw_m3_attrs = attrs
  7284. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7285. send_wsc_msg(hapd, addr, m3)
  7286. logger.debug("Receive M4 from AP")
  7287. msg, m4_attrs, raw_m4_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M4)
  7288. logger.debug("Send M5 to AP")
  7289. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7290. attrs += build_attr_msg_type(WPS_M5)
  7291. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7292. data = build_wsc_attr(ATTR_E_SNONCE1, e_s1)
  7293. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7294. attrs += build_attr_authenticator(authkey, raw_m4_attrs, attrs)
  7295. raw_m5_attrs = attrs
  7296. m5 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7297. send_wsc_msg(hapd, addr, m5)
  7298. logger.debug("Receive M6 from AP")
  7299. msg, m6_attrs, raw_m6_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M6)
  7300. logger.debug("Send M7 to AP")
  7301. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7302. attrs += build_attr_msg_type(WPS_M7)
  7303. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7304. data = build_wsc_attr(ATTR_E_SNONCE2, 16*'\x00')
  7305. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7306. attrs += build_attr_authenticator(authkey, raw_m6_attrs, attrs)
  7307. m7 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7308. raw_m7_attrs = attrs
  7309. send_wsc_msg(hapd, addr, m7)
  7310. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7311. @remote_compatible
  7312. def test_wps_ext_m1_pubkey_oom(dev, apdev):
  7313. """WPS proto: M1 PubKey OOM"""
  7314. pin = "12345670"
  7315. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7316. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7317. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7318. logger.debug("Receive WSC/Start from AP")
  7319. msg = get_wsc_msg(hapd)
  7320. if msg['wsc_opcode'] != WSC_Start:
  7321. raise Exception("Unexpected Op-Code for WSC/Start")
  7322. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7323. uuid_e = 16*'\x11'
  7324. e_nonce = 16*'\x22'
  7325. own_private, e_pk = wsc_dh_init()
  7326. logger.debug("Send M1 to AP")
  7327. with alloc_fail(hapd, 1, "wpabuf_alloc_copy;wps_process_pubkey"):
  7328. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7329. e_nonce, e_pk)
  7330. send_wsc_msg(hapd, addr, m1)
  7331. wps_wait_eap_failure(hapd, dev[0])
  7332. def wps_wait_eap_failure(hapd, dev):
  7333. ev = hapd.wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  7334. if ev is None:
  7335. raise Exception("EAP-Failure not reported")
  7336. dev.wait_disconnected()
  7337. @remote_compatible
  7338. def test_wps_ext_m3_m1(dev, apdev):
  7339. """WPS proto: M3 replaced with M1"""
  7340. pin = "12345670"
  7341. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7342. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7343. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7344. logger.debug("Receive WSC/Start from AP")
  7345. msg = get_wsc_msg(hapd)
  7346. if msg['wsc_opcode'] != WSC_Start:
  7347. raise Exception("Unexpected Op-Code for WSC/Start")
  7348. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7349. uuid_e = 16*'\x11'
  7350. e_nonce = 16*'\x22'
  7351. own_private, e_pk = wsc_dh_init()
  7352. logger.debug("Send M1 to AP")
  7353. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7354. e_nonce, e_pk)
  7355. send_wsc_msg(hapd, addr, m1)
  7356. logger.debug("Receive M2 from AP")
  7357. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7358. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7359. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7360. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7361. r_nonce)
  7362. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7363. logger.debug("Send M3(M1) to AP")
  7364. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7365. attrs += build_attr_msg_type(WPS_M1)
  7366. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7367. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7368. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7369. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7370. raw_m3_attrs = attrs
  7371. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7372. send_wsc_msg(hapd, addr, m3)
  7373. wps_wait_eap_failure(hapd, dev[0])
  7374. @remote_compatible
  7375. def test_wps_ext_m5_m3(dev, apdev):
  7376. """WPS proto: M5 replaced with M3"""
  7377. pin = "12345670"
  7378. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7379. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7380. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7381. logger.debug("Receive WSC/Start from AP")
  7382. msg = get_wsc_msg(hapd)
  7383. if msg['wsc_opcode'] != WSC_Start:
  7384. raise Exception("Unexpected Op-Code for WSC/Start")
  7385. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7386. uuid_e = 16*'\x11'
  7387. e_nonce = 16*'\x22'
  7388. own_private, e_pk = wsc_dh_init()
  7389. logger.debug("Send M1 to AP")
  7390. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7391. e_nonce, e_pk)
  7392. send_wsc_msg(hapd, addr, m1)
  7393. logger.debug("Receive M2 from AP")
  7394. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7395. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7396. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7397. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7398. r_nonce)
  7399. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7400. logger.debug("Send M3 to AP")
  7401. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7402. attrs += build_attr_msg_type(WPS_M3)
  7403. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7404. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7405. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7406. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7407. raw_m3_attrs = attrs
  7408. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7409. send_wsc_msg(hapd, addr, m3)
  7410. logger.debug("Receive M4 from AP")
  7411. msg, m4_attrs, raw_m4_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M4)
  7412. logger.debug("Send M5(M3) to AP")
  7413. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7414. attrs += build_attr_msg_type(WPS_M3)
  7415. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7416. data = build_wsc_attr(ATTR_E_SNONCE1, e_s1)
  7417. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7418. attrs += build_attr_authenticator(authkey, raw_m4_attrs, attrs)
  7419. raw_m5_attrs = attrs
  7420. m5 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7421. send_wsc_msg(hapd, addr, m5)
  7422. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7423. @remote_compatible
  7424. def test_wps_ext_m3_m2(dev, apdev):
  7425. """WPS proto: M3 replaced with M2"""
  7426. pin = "12345670"
  7427. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7428. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7429. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7430. logger.debug("Receive WSC/Start from AP")
  7431. msg = get_wsc_msg(hapd)
  7432. if msg['wsc_opcode'] != WSC_Start:
  7433. raise Exception("Unexpected Op-Code for WSC/Start")
  7434. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7435. uuid_e = 16*'\x11'
  7436. e_nonce = 16*'\x22'
  7437. own_private, e_pk = wsc_dh_init()
  7438. logger.debug("Send M1 to AP")
  7439. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7440. e_nonce, e_pk)
  7441. send_wsc_msg(hapd, addr, m1)
  7442. logger.debug("Receive M2 from AP")
  7443. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7444. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7445. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7446. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7447. r_nonce)
  7448. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7449. logger.debug("Send M3(M2) to AP")
  7450. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7451. attrs += build_attr_msg_type(WPS_M2)
  7452. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7453. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7454. raw_m3_attrs = attrs
  7455. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7456. send_wsc_msg(hapd, addr, m3)
  7457. wps_wait_eap_failure(hapd, dev[0])
  7458. @remote_compatible
  7459. def test_wps_ext_m3_m5(dev, apdev):
  7460. """WPS proto: M3 replaced with M5"""
  7461. pin = "12345670"
  7462. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7463. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7464. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7465. logger.debug("Receive WSC/Start from AP")
  7466. msg = get_wsc_msg(hapd)
  7467. if msg['wsc_opcode'] != WSC_Start:
  7468. raise Exception("Unexpected Op-Code for WSC/Start")
  7469. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7470. uuid_e = 16*'\x11'
  7471. e_nonce = 16*'\x22'
  7472. own_private, e_pk = wsc_dh_init()
  7473. logger.debug("Send M1 to AP")
  7474. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7475. e_nonce, e_pk)
  7476. send_wsc_msg(hapd, addr, m1)
  7477. logger.debug("Receive M2 from AP")
  7478. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7479. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7480. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7481. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7482. r_nonce)
  7483. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7484. logger.debug("Send M3(M5) to AP")
  7485. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7486. attrs += build_attr_msg_type(WPS_M5)
  7487. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7488. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7489. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7490. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7491. raw_m3_attrs = attrs
  7492. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7493. send_wsc_msg(hapd, addr, m3)
  7494. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7495. @remote_compatible
  7496. def test_wps_ext_m3_m7(dev, apdev):
  7497. """WPS proto: M3 replaced with M7"""
  7498. pin = "12345670"
  7499. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7500. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7501. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7502. logger.debug("Receive WSC/Start from AP")
  7503. msg = get_wsc_msg(hapd)
  7504. if msg['wsc_opcode'] != WSC_Start:
  7505. raise Exception("Unexpected Op-Code for WSC/Start")
  7506. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7507. uuid_e = 16*'\x11'
  7508. e_nonce = 16*'\x22'
  7509. own_private, e_pk = wsc_dh_init()
  7510. logger.debug("Send M1 to AP")
  7511. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7512. e_nonce, e_pk)
  7513. send_wsc_msg(hapd, addr, m1)
  7514. logger.debug("Receive M2 from AP")
  7515. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7516. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7517. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7518. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7519. r_nonce)
  7520. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7521. logger.debug("Send M3(M7) to AP")
  7522. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7523. attrs += build_attr_msg_type(WPS_M7)
  7524. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7525. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7526. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7527. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7528. raw_m3_attrs = attrs
  7529. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7530. send_wsc_msg(hapd, addr, m3)
  7531. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  7532. @remote_compatible
  7533. def test_wps_ext_m3_done(dev, apdev):
  7534. """WPS proto: M3 replaced with WSC_Done"""
  7535. pin = "12345670"
  7536. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7537. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7538. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7539. logger.debug("Receive WSC/Start from AP")
  7540. msg = get_wsc_msg(hapd)
  7541. if msg['wsc_opcode'] != WSC_Start:
  7542. raise Exception("Unexpected Op-Code for WSC/Start")
  7543. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7544. uuid_e = 16*'\x11'
  7545. e_nonce = 16*'\x22'
  7546. own_private, e_pk = wsc_dh_init()
  7547. logger.debug("Send M1 to AP")
  7548. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7549. e_nonce, e_pk)
  7550. send_wsc_msg(hapd, addr, m1)
  7551. logger.debug("Receive M2 from AP")
  7552. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7553. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7554. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7555. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7556. r_nonce)
  7557. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7558. logger.debug("Send M3(WSC_Done) to AP")
  7559. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7560. attrs += build_attr_msg_type(WPS_WSC_DONE)
  7561. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7562. raw_m3_attrs = attrs
  7563. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_Done)
  7564. send_wsc_msg(hapd, addr, m3)
  7565. wps_wait_eap_failure(hapd, dev[0])
  7566. @remote_compatible
  7567. def test_wps_ext_m2_nack_invalid(dev, apdev):
  7568. """WPS proto: M2 followed by invalid NACK"""
  7569. pin = "12345670"
  7570. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7571. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7572. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7573. logger.debug("Receive WSC/Start from AP")
  7574. msg = get_wsc_msg(hapd)
  7575. if msg['wsc_opcode'] != WSC_Start:
  7576. raise Exception("Unexpected Op-Code for WSC/Start")
  7577. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7578. uuid_e = 16*'\x11'
  7579. e_nonce = 16*'\x22'
  7580. own_private, e_pk = wsc_dh_init()
  7581. logger.debug("Send M1 to AP")
  7582. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7583. e_nonce, e_pk)
  7584. send_wsc_msg(hapd, addr, m1)
  7585. logger.debug("Receive M2 from AP")
  7586. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7587. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7588. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7589. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7590. r_nonce)
  7591. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7592. logger.debug("Send WSC_NACK to AP")
  7593. attrs = '\x10\x00\x00'
  7594. nack = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_NACK)
  7595. send_wsc_msg(hapd, addr, nack)
  7596. wps_wait_eap_failure(hapd, dev[0])
  7597. @remote_compatible
  7598. def test_wps_ext_m2_nack_no_msg_type(dev, apdev):
  7599. """WPS proto: M2 followed by NACK without Msg Type"""
  7600. pin = "12345670"
  7601. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7602. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7603. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7604. logger.debug("Receive WSC/Start from AP")
  7605. msg = get_wsc_msg(hapd)
  7606. if msg['wsc_opcode'] != WSC_Start:
  7607. raise Exception("Unexpected Op-Code for WSC/Start")
  7608. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7609. uuid_e = 16*'\x11'
  7610. e_nonce = 16*'\x22'
  7611. own_private, e_pk = wsc_dh_init()
  7612. logger.debug("Send M1 to AP")
  7613. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7614. e_nonce, e_pk)
  7615. send_wsc_msg(hapd, addr, m1)
  7616. logger.debug("Receive M2 from AP")
  7617. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7618. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7619. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7620. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7621. r_nonce)
  7622. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7623. logger.debug("Send WSC_NACK to AP")
  7624. nack,attrs = build_nack(msg['eap_identifier'], e_nonce, r_nonce,
  7625. msg_type=None, eap_code=2)
  7626. send_wsc_msg(hapd, addr, nack)
  7627. wps_wait_eap_failure(hapd, dev[0])
  7628. @remote_compatible
  7629. def test_wps_ext_m2_nack_invalid_msg_type(dev, apdev):
  7630. """WPS proto: M2 followed by NACK with invalid Msg Type"""
  7631. pin = "12345670"
  7632. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7633. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7634. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7635. logger.debug("Receive WSC/Start from AP")
  7636. msg = get_wsc_msg(hapd)
  7637. if msg['wsc_opcode'] != WSC_Start:
  7638. raise Exception("Unexpected Op-Code for WSC/Start")
  7639. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7640. uuid_e = 16*'\x11'
  7641. e_nonce = 16*'\x22'
  7642. own_private, e_pk = wsc_dh_init()
  7643. logger.debug("Send M1 to AP")
  7644. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7645. e_nonce, e_pk)
  7646. send_wsc_msg(hapd, addr, m1)
  7647. logger.debug("Receive M2 from AP")
  7648. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7649. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7650. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7651. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7652. r_nonce)
  7653. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7654. logger.debug("Send WSC_NACK to AP")
  7655. nack,attrs = build_nack(msg['eap_identifier'], e_nonce, r_nonce,
  7656. msg_type=WPS_WSC_ACK, eap_code=2)
  7657. send_wsc_msg(hapd, addr, nack)
  7658. wps_wait_eap_failure(hapd, dev[0])
  7659. @remote_compatible
  7660. def test_wps_ext_m2_nack_e_nonce_mismatch(dev, apdev):
  7661. """WPS proto: M2 followed by NACK with e-nonce mismatch"""
  7662. pin = "12345670"
  7663. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7664. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7665. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7666. logger.debug("Receive WSC/Start from AP")
  7667. msg = get_wsc_msg(hapd)
  7668. if msg['wsc_opcode'] != WSC_Start:
  7669. raise Exception("Unexpected Op-Code for WSC/Start")
  7670. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7671. uuid_e = 16*'\x11'
  7672. e_nonce = 16*'\x22'
  7673. own_private, e_pk = wsc_dh_init()
  7674. logger.debug("Send M1 to AP")
  7675. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7676. e_nonce, e_pk)
  7677. send_wsc_msg(hapd, addr, m1)
  7678. logger.debug("Receive M2 from AP")
  7679. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7680. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7681. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7682. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7683. r_nonce)
  7684. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7685. logger.debug("Send WSC_NACK to AP")
  7686. nack,attrs = build_nack(msg['eap_identifier'], 16*'\x00', r_nonce,
  7687. eap_code=2)
  7688. send_wsc_msg(hapd, addr, nack)
  7689. wps_wait_eap_failure(hapd, dev[0])
  7690. @remote_compatible
  7691. def test_wps_ext_m2_nack_no_config_error(dev, apdev):
  7692. """WPS proto: M2 followed by NACK without Config Error"""
  7693. pin = "12345670"
  7694. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7695. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7696. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7697. logger.debug("Receive WSC/Start from AP")
  7698. msg = get_wsc_msg(hapd)
  7699. if msg['wsc_opcode'] != WSC_Start:
  7700. raise Exception("Unexpected Op-Code for WSC/Start")
  7701. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7702. uuid_e = 16*'\x11'
  7703. e_nonce = 16*'\x22'
  7704. own_private, e_pk = wsc_dh_init()
  7705. logger.debug("Send M1 to AP")
  7706. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7707. e_nonce, e_pk)
  7708. send_wsc_msg(hapd, addr, m1)
  7709. logger.debug("Receive M2 from AP")
  7710. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7711. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7712. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7713. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7714. r_nonce)
  7715. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7716. logger.debug("Send WSC_NACK to AP")
  7717. nack,attrs = build_nack(msg['eap_identifier'], e_nonce, r_nonce,
  7718. config_error=None, eap_code=2)
  7719. send_wsc_msg(hapd, addr, nack)
  7720. wps_wait_eap_failure(hapd, dev[0])
  7721. @remote_compatible
  7722. def test_wps_ext_m2_ack_invalid(dev, apdev):
  7723. """WPS proto: M2 followed by invalid ACK"""
  7724. pin = "12345670"
  7725. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7726. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7727. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7728. logger.debug("Receive WSC/Start from AP")
  7729. msg = get_wsc_msg(hapd)
  7730. if msg['wsc_opcode'] != WSC_Start:
  7731. raise Exception("Unexpected Op-Code for WSC/Start")
  7732. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7733. uuid_e = 16*'\x11'
  7734. e_nonce = 16*'\x22'
  7735. own_private, e_pk = wsc_dh_init()
  7736. logger.debug("Send M1 to AP")
  7737. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7738. e_nonce, e_pk)
  7739. send_wsc_msg(hapd, addr, m1)
  7740. logger.debug("Receive M2 from AP")
  7741. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7742. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7743. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7744. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7745. r_nonce)
  7746. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7747. logger.debug("Send WSC_ACK to AP")
  7748. attrs = '\x10\x00\x00'
  7749. ack = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_ACK)
  7750. send_wsc_msg(hapd, addr, ack)
  7751. wps_wait_eap_failure(hapd, dev[0])
  7752. @remote_compatible
  7753. def test_wps_ext_m2_ack(dev, apdev):
  7754. """WPS proto: M2 followed by ACK"""
  7755. pin = "12345670"
  7756. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7757. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7758. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7759. logger.debug("Receive WSC/Start from AP")
  7760. msg = get_wsc_msg(hapd)
  7761. if msg['wsc_opcode'] != WSC_Start:
  7762. raise Exception("Unexpected Op-Code for WSC/Start")
  7763. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7764. uuid_e = 16*'\x11'
  7765. e_nonce = 16*'\x22'
  7766. own_private, e_pk = wsc_dh_init()
  7767. logger.debug("Send M1 to AP")
  7768. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7769. e_nonce, e_pk)
  7770. send_wsc_msg(hapd, addr, m1)
  7771. logger.debug("Receive M2 from AP")
  7772. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7773. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7774. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7775. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7776. r_nonce)
  7777. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7778. logger.debug("Send WSC_ACK to AP")
  7779. ack,attrs = build_ack(msg['eap_identifier'], e_nonce, r_nonce, eap_code=2)
  7780. send_wsc_msg(hapd, addr, ack)
  7781. wps_wait_eap_failure(hapd, dev[0])
  7782. @remote_compatible
  7783. def test_wps_ext_m2_ack_no_msg_type(dev, apdev):
  7784. """WPS proto: M2 followed by ACK missing Msg Type"""
  7785. pin = "12345670"
  7786. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7787. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7788. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7789. logger.debug("Receive WSC/Start from AP")
  7790. msg = get_wsc_msg(hapd)
  7791. if msg['wsc_opcode'] != WSC_Start:
  7792. raise Exception("Unexpected Op-Code for WSC/Start")
  7793. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7794. uuid_e = 16*'\x11'
  7795. e_nonce = 16*'\x22'
  7796. own_private, e_pk = wsc_dh_init()
  7797. logger.debug("Send M1 to AP")
  7798. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7799. e_nonce, e_pk)
  7800. send_wsc_msg(hapd, addr, m1)
  7801. logger.debug("Receive M2 from AP")
  7802. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7803. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7804. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7805. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7806. r_nonce)
  7807. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7808. logger.debug("Send WSC_ACK to AP")
  7809. ack,attrs = build_ack(msg['eap_identifier'], e_nonce, r_nonce,
  7810. msg_type=None, eap_code=2)
  7811. send_wsc_msg(hapd, addr, ack)
  7812. wps_wait_eap_failure(hapd, dev[0])
  7813. @remote_compatible
  7814. def test_wps_ext_m2_ack_invalid_msg_type(dev, apdev):
  7815. """WPS proto: M2 followed by ACK with invalid Msg Type"""
  7816. pin = "12345670"
  7817. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7818. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7819. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7820. logger.debug("Receive WSC/Start from AP")
  7821. msg = get_wsc_msg(hapd)
  7822. if msg['wsc_opcode'] != WSC_Start:
  7823. raise Exception("Unexpected Op-Code for WSC/Start")
  7824. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7825. uuid_e = 16*'\x11'
  7826. e_nonce = 16*'\x22'
  7827. own_private, e_pk = wsc_dh_init()
  7828. logger.debug("Send M1 to AP")
  7829. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7830. e_nonce, e_pk)
  7831. send_wsc_msg(hapd, addr, m1)
  7832. logger.debug("Receive M2 from AP")
  7833. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7834. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7835. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7836. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7837. r_nonce)
  7838. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7839. logger.debug("Send WSC_ACK to AP")
  7840. ack,attrs = build_ack(msg['eap_identifier'], e_nonce, r_nonce,
  7841. msg_type=WPS_WSC_NACK, eap_code=2)
  7842. send_wsc_msg(hapd, addr, ack)
  7843. wps_wait_eap_failure(hapd, dev[0])
  7844. @remote_compatible
  7845. def test_wps_ext_m2_ack_e_nonce_mismatch(dev, apdev):
  7846. """WPS proto: M2 followed by ACK with e-nonce mismatch"""
  7847. pin = "12345670"
  7848. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7849. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7850. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7851. logger.debug("Receive WSC/Start from AP")
  7852. msg = get_wsc_msg(hapd)
  7853. if msg['wsc_opcode'] != WSC_Start:
  7854. raise Exception("Unexpected Op-Code for WSC/Start")
  7855. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7856. uuid_e = 16*'\x11'
  7857. e_nonce = 16*'\x22'
  7858. own_private, e_pk = wsc_dh_init()
  7859. logger.debug("Send M1 to AP")
  7860. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7861. e_nonce, e_pk)
  7862. send_wsc_msg(hapd, addr, m1)
  7863. logger.debug("Receive M2 from AP")
  7864. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7865. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7866. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7867. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7868. r_nonce)
  7869. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7870. logger.debug("Send WSC_ACK to AP")
  7871. ack,attrs = build_ack(msg['eap_identifier'], 16*'\x00', r_nonce,
  7872. eap_code=2)
  7873. send_wsc_msg(hapd, addr, ack)
  7874. wps_wait_eap_failure(hapd, dev[0])
  7875. @remote_compatible
  7876. def test_wps_ext_m1_invalid(dev, apdev):
  7877. """WPS proto: M1 failing parsing"""
  7878. pin = "12345670"
  7879. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7880. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7881. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7882. logger.debug("Receive WSC/Start from AP")
  7883. msg = get_wsc_msg(hapd)
  7884. if msg['wsc_opcode'] != WSC_Start:
  7885. raise Exception("Unexpected Op-Code for WSC/Start")
  7886. logger.debug("Send M1 to AP")
  7887. attrs = '\x10\x00\x00'
  7888. m1 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7889. send_wsc_msg(hapd, addr, m1)
  7890. wps_wait_eap_failure(hapd, dev[0])
  7891. def test_wps_ext_m1_missing_msg_type(dev, apdev):
  7892. """WPS proto: M1 missing Msg Type"""
  7893. pin = "12345670"
  7894. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7895. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7896. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7897. logger.debug("Receive WSC/Start from AP")
  7898. msg = get_wsc_msg(hapd)
  7899. if msg['wsc_opcode'] != WSC_Start:
  7900. raise Exception("Unexpected Op-Code for WSC/Start")
  7901. logger.debug("Send M1 to AP")
  7902. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7903. m1 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7904. send_wsc_msg(hapd, addr, m1)
  7905. wps_wait_ap_nack(hapd, dev[0], 16*'\x00', 16*'\x00')
  7906. def wps_ext_wsc_done(dev, apdev):
  7907. pin = "12345670"
  7908. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  7909. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  7910. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  7911. logger.debug("Receive WSC/Start from AP")
  7912. msg = get_wsc_msg(hapd)
  7913. if msg['wsc_opcode'] != WSC_Start:
  7914. raise Exception("Unexpected Op-Code for WSC/Start")
  7915. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  7916. uuid_e = 16*'\x11'
  7917. e_nonce = 16*'\x22'
  7918. own_private, e_pk = wsc_dh_init()
  7919. logger.debug("Send M1 to AP")
  7920. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  7921. e_nonce, e_pk)
  7922. send_wsc_msg(hapd, addr, m1)
  7923. logger.debug("Receive M2 from AP")
  7924. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  7925. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  7926. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  7927. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  7928. r_nonce)
  7929. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  7930. logger.debug("Send M3 to AP")
  7931. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7932. attrs += build_attr_msg_type(WPS_M3)
  7933. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7934. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  7935. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  7936. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  7937. raw_m3_attrs = attrs
  7938. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7939. send_wsc_msg(hapd, addr, m3)
  7940. logger.debug("Receive M4 from AP")
  7941. msg, m4_attrs, raw_m4_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M4)
  7942. logger.debug("Send M5 to AP")
  7943. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7944. attrs += build_attr_msg_type(WPS_M5)
  7945. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7946. data = build_wsc_attr(ATTR_E_SNONCE1, e_s1)
  7947. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7948. attrs += build_attr_authenticator(authkey, raw_m4_attrs, attrs)
  7949. raw_m5_attrs = attrs
  7950. m5 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7951. send_wsc_msg(hapd, addr, m5)
  7952. logger.debug("Receive M6 from AP")
  7953. msg, m6_attrs, raw_m6_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M6)
  7954. logger.debug("Send M7 to AP")
  7955. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7956. attrs += build_attr_msg_type(WPS_M7)
  7957. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7958. data = build_wsc_attr(ATTR_E_SNONCE2, e_s2)
  7959. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  7960. attrs += build_attr_authenticator(authkey, raw_m6_attrs, attrs)
  7961. m7 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  7962. raw_m7_attrs = attrs
  7963. send_wsc_msg(hapd, addr, m7)
  7964. logger.debug("Receive M8 from AP")
  7965. msg, m8_attrs, raw_m8_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M8)
  7966. return hapd, msg, e_nonce, r_nonce
  7967. @remote_compatible
  7968. def test_wps_ext_wsc_done_invalid(dev, apdev):
  7969. """WPS proto: invalid WSC_Done"""
  7970. hapd, msg, e_nonce, r_nonce = wps_ext_wsc_done(dev, apdev)
  7971. logger.debug("Send WSC_Done to AP")
  7972. attrs = '\x10\x00\x00'
  7973. wsc_done = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_Done)
  7974. send_wsc_msg(hapd, dev[0].own_addr(), wsc_done)
  7975. wps_wait_eap_failure(hapd, dev[0])
  7976. @remote_compatible
  7977. def test_wps_ext_wsc_done_no_msg_type(dev, apdev):
  7978. """WPS proto: invalid WSC_Done"""
  7979. hapd, msg, e_nonce, r_nonce = wps_ext_wsc_done(dev, apdev)
  7980. logger.debug("Send WSC_Done to AP")
  7981. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7982. #attrs += build_attr_msg_type(WPS_WSC_DONE)
  7983. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  7984. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7985. wsc_done = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_Done)
  7986. send_wsc_msg(hapd, dev[0].own_addr(), wsc_done)
  7987. wps_wait_eap_failure(hapd, dev[0])
  7988. @remote_compatible
  7989. def test_wps_ext_wsc_done_wrong_msg_type(dev, apdev):
  7990. """WPS proto: WSC_Done with wrong Msg Type"""
  7991. hapd, msg, e_nonce, r_nonce = wps_ext_wsc_done(dev, apdev)
  7992. logger.debug("Send WSC_Done to AP")
  7993. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  7994. attrs += build_attr_msg_type(WPS_WSC_ACK)
  7995. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  7996. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  7997. wsc_done = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_Done)
  7998. send_wsc_msg(hapd, dev[0].own_addr(), wsc_done)
  7999. wps_wait_eap_failure(hapd, dev[0])
  8000. @remote_compatible
  8001. def test_wps_ext_wsc_done_no_e_nonce(dev, apdev):
  8002. """WPS proto: WSC_Done without e_nonce"""
  8003. hapd, msg, e_nonce, r_nonce = wps_ext_wsc_done(dev, apdev)
  8004. logger.debug("Send WSC_Done to AP")
  8005. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  8006. attrs += build_attr_msg_type(WPS_WSC_DONE)
  8007. #attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  8008. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  8009. wsc_done = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_Done)
  8010. send_wsc_msg(hapd, dev[0].own_addr(), wsc_done)
  8011. wps_wait_eap_failure(hapd, dev[0])
  8012. def test_wps_ext_wsc_done_no_r_nonce(dev, apdev):
  8013. """WPS proto: WSC_Done without r_nonce"""
  8014. hapd, msg, e_nonce, r_nonce = wps_ext_wsc_done(dev, apdev)
  8015. logger.debug("Send WSC_Done to AP")
  8016. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  8017. attrs += build_attr_msg_type(WPS_WSC_DONE)
  8018. attrs += build_wsc_attr(ATTR_ENROLLEE_NONCE, e_nonce)
  8019. #attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  8020. wsc_done = build_eap_wsc(2, msg['eap_identifier'], attrs, opcode=WSC_Done)
  8021. send_wsc_msg(hapd, dev[0].own_addr(), wsc_done)
  8022. wps_wait_eap_failure(hapd, dev[0])
  8023. @remote_compatible
  8024. def test_wps_ext_m7_no_encr_settings(dev, apdev):
  8025. """WPS proto: M7 without Encr Settings"""
  8026. pin = "12345670"
  8027. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  8028. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  8029. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  8030. logger.debug("Receive WSC/Start from AP")
  8031. msg = get_wsc_msg(hapd)
  8032. if msg['wsc_opcode'] != WSC_Start:
  8033. raise Exception("Unexpected Op-Code for WSC/Start")
  8034. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  8035. uuid_e = 16*'\x11'
  8036. e_nonce = 16*'\x22'
  8037. own_private, e_pk = wsc_dh_init()
  8038. logger.debug("Send M1 to AP")
  8039. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  8040. e_nonce, e_pk)
  8041. send_wsc_msg(hapd, addr, m1)
  8042. logger.debug("Receive M2 from AP")
  8043. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  8044. r_nonce = m2_attrs[ATTR_REGISTRAR_NONCE]
  8045. r_pk = m2_attrs[ATTR_PUBLIC_KEY]
  8046. authkey,keywrapkey = wsc_dh_kdf(r_pk, own_private, mac_addr, e_nonce,
  8047. r_nonce)
  8048. e_s1,e_s2,e_hash1,e_hash2 = wsc_dev_pw_hash(authkey, pin, e_pk, r_pk)
  8049. logger.debug("Send M3 to AP")
  8050. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  8051. attrs += build_attr_msg_type(WPS_M3)
  8052. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  8053. attrs += build_wsc_attr(ATTR_E_HASH1, e_hash1)
  8054. attrs += build_wsc_attr(ATTR_E_HASH2, e_hash2)
  8055. attrs += build_attr_authenticator(authkey, raw_m2_attrs, attrs)
  8056. raw_m3_attrs = attrs
  8057. m3 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  8058. send_wsc_msg(hapd, addr, m3)
  8059. logger.debug("Receive M4 from AP")
  8060. msg, m4_attrs, raw_m4_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M4)
  8061. logger.debug("Send M5 to AP")
  8062. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  8063. attrs += build_attr_msg_type(WPS_M5)
  8064. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  8065. data = build_wsc_attr(ATTR_E_SNONCE1, e_s1)
  8066. attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  8067. attrs += build_attr_authenticator(authkey, raw_m4_attrs, attrs)
  8068. raw_m5_attrs = attrs
  8069. m5 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  8070. send_wsc_msg(hapd, addr, m5)
  8071. logger.debug("Receive M6 from AP")
  8072. msg, m6_attrs, raw_m6_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M6)
  8073. logger.debug("Send M7 to AP")
  8074. attrs = build_wsc_attr(ATTR_VERSION, '\x10')
  8075. attrs += build_attr_msg_type(WPS_M7)
  8076. attrs += build_wsc_attr(ATTR_REGISTRAR_NONCE, r_nonce)
  8077. #data = build_wsc_attr(ATTR_E_SNONCE2, e_s2)
  8078. #attrs += build_attr_encr_settings(authkey, keywrapkey, data)
  8079. attrs += build_attr_authenticator(authkey, raw_m6_attrs, attrs)
  8080. m7 = build_eap_wsc(2, msg['eap_identifier'], attrs)
  8081. raw_m7_attrs = attrs
  8082. send_wsc_msg(hapd, addr, m7)
  8083. wps_wait_ap_nack(hapd, dev[0], e_nonce, r_nonce)
  8084. @remote_compatible
  8085. def test_wps_ext_m1_workaround(dev, apdev):
  8086. """WPS proto: M1 Manufacturer/Model workaround"""
  8087. pin = "12345670"
  8088. addr,bssid,hapd = wps_start_ext(apdev[0], dev[0], pin=pin)
  8089. wps_ext_eap_identity_req(dev[0], hapd, bssid)
  8090. wps_ext_eap_identity_resp(hapd, dev[0], addr)
  8091. logger.debug("Receive WSC/Start from AP")
  8092. msg = get_wsc_msg(hapd)
  8093. if msg['wsc_opcode'] != WSC_Start:
  8094. raise Exception("Unexpected Op-Code for WSC/Start")
  8095. mac_addr = binascii.unhexlify(dev[0].own_addr().replace(':', ''))
  8096. uuid_e = 16*'\x11'
  8097. e_nonce = 16*'\x22'
  8098. own_private, e_pk = wsc_dh_init()
  8099. logger.debug("Send M1 to AP")
  8100. m1, raw_m1_attrs = build_m1(msg['eap_identifier'], uuid_e, mac_addr,
  8101. e_nonce, e_pk, manufacturer='Apple TEST',
  8102. model_name='AirPort', config_methods='\xff\xff')
  8103. send_wsc_msg(hapd, addr, m1)
  8104. logger.debug("Receive M2 from AP")
  8105. msg, m2_attrs, raw_m2_attrs = recv_wsc_msg(hapd, WSC_MSG, WPS_M2)
  8106. @remote_compatible
  8107. def test_ap_wps_disable_enable(dev, apdev):
  8108. """WPS and DISABLE/ENABLE AP"""
  8109. hapd = wps_start_ap(apdev[0])
  8110. hapd.disable()
  8111. hapd.enable()
  8112. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  8113. def test_ap_wps_upnp_web_oom(dev, apdev, params):
  8114. """hostapd WPS UPnP web OOM"""
  8115. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  8116. hapd = add_ssdp_ap(apdev[0], ap_uuid)
  8117. location = ssdp_get_location(ap_uuid)
  8118. url = urlparse.urlparse(location)
  8119. urls = upnp_get_urls(location)
  8120. eventurl = urlparse.urlparse(urls['event_sub_url'])
  8121. ctrlurl = urlparse.urlparse(urls['control_url'])
  8122. conn = httplib.HTTPConnection(url.netloc)
  8123. with alloc_fail(hapd, 1, "web_connection_parse_get"):
  8124. conn.request("GET", "/wps_device.xml")
  8125. try:
  8126. resp = conn.getresponse()
  8127. except:
  8128. pass
  8129. conn = httplib.HTTPConnection(url.netloc)
  8130. conn.request("GET", "/unknown")
  8131. resp = conn.getresponse()
  8132. if resp.status != 404:
  8133. raise Exception("Unexpected HTTP result for unknown URL: %d" + resp.status)
  8134. with alloc_fail(hapd, 1, "web_connection_parse_get"):
  8135. conn.request("GET", "/unknown")
  8136. try:
  8137. resp = conn.getresponse()
  8138. print resp.status
  8139. except:
  8140. pass
  8141. conn = httplib.HTTPConnection(url.netloc)
  8142. conn.request("GET", "/wps_device.xml")
  8143. resp = conn.getresponse()
  8144. if resp.status != 200:
  8145. raise Exception("GET /wps_device.xml failed")
  8146. conn = httplib.HTTPConnection(url.netloc)
  8147. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  8148. if resp.status != 200:
  8149. raise Exception("GetDeviceInfo failed")
  8150. with alloc_fail(hapd, 1, "web_process_get_device_info"):
  8151. conn = httplib.HTTPConnection(url.netloc)
  8152. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  8153. if resp.status != 500:
  8154. raise Exception("Internal error not reported from GetDeviceInfo OOM")
  8155. with alloc_fail(hapd, 1, "wps_build_m1;web_process_get_device_info"):
  8156. conn = httplib.HTTPConnection(url.netloc)
  8157. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  8158. if resp.status != 500:
  8159. raise Exception("Internal error not reported from GetDeviceInfo OOM")
  8160. with alloc_fail(hapd, 1, "wpabuf_alloc;web_connection_send_reply"):
  8161. conn = httplib.HTTPConnection(url.netloc)
  8162. try:
  8163. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  8164. except:
  8165. pass
  8166. conn = httplib.HTTPConnection(url.netloc)
  8167. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  8168. if resp.status != 200:
  8169. raise Exception("GetDeviceInfo failed")
  8170. # No NewWLANEventType in PutWLANResponse NewMessage
  8171. conn = httplib.HTTPConnection(url.netloc)
  8172. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse", newmsg="foo")
  8173. if resp.status != 600:
  8174. raise Exception("Unexpected HTTP response: %d" % resp.status)
  8175. # No NewWLANEventMAC in PutWLANResponse NewMessage
  8176. conn = httplib.HTTPConnection(url.netloc)
  8177. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse",
  8178. newmsg="foo", neweventtype="1")
  8179. if resp.status != 600:
  8180. raise Exception("Unexpected HTTP response: %d" % resp.status)
  8181. # Invalid NewWLANEventMAC in PutWLANResponse NewMessage
  8182. conn = httplib.HTTPConnection(url.netloc)
  8183. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse",
  8184. newmsg="foo", neweventtype="1",
  8185. neweventmac="foo")
  8186. if resp.status != 600:
  8187. raise Exception("Unexpected HTTP response: %d" % resp.status)
  8188. # Workaround for NewWLANEventMAC in PutWLANResponse NewMessage
  8189. # Ignored unexpected PutWLANResponse WLANEventType 1
  8190. conn = httplib.HTTPConnection(url.netloc)
  8191. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse",
  8192. newmsg="foo", neweventtype="1",
  8193. neweventmac="00.11.22.33.44.55")
  8194. if resp.status != 500:
  8195. raise Exception("Unexpected HTTP response: %d" % resp.status)
  8196. # PutWLANResponse NewMessage with invalid EAP message
  8197. conn = httplib.HTTPConnection(url.netloc)
  8198. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse",
  8199. newmsg="foo", neweventtype="2",
  8200. neweventmac="00:11:22:33:44:55")
  8201. if resp.status != 200:
  8202. raise Exception("Unexpected HTTP response: %d" % resp.status)
  8203. with alloc_fail(hapd, 1, "web_connection_parse_subscribe"):
  8204. conn = httplib.HTTPConnection(url.netloc)
  8205. headers = { "callback": '<http://127.0.0.1:12345/event>',
  8206. "NT": "upnp:event",
  8207. "timeout": "Second-1234" }
  8208. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  8209. try:
  8210. resp = conn.getresponse()
  8211. except:
  8212. pass
  8213. with alloc_fail(hapd, 1, "dup_binstr;web_connection_parse_subscribe"):
  8214. conn = httplib.HTTPConnection(url.netloc)
  8215. headers = { "callback": '<http://127.0.0.1:12345/event>',
  8216. "NT": "upnp:event",
  8217. "timeout": "Second-1234" }
  8218. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  8219. resp = conn.getresponse()
  8220. if resp.status != 500:
  8221. raise Exception("Unexpected HTTP response: %d" % resp.status)
  8222. with alloc_fail(hapd, 1, "wpabuf_alloc;web_connection_parse_unsubscribe"):
  8223. conn = httplib.HTTPConnection(url.netloc)
  8224. headers = { "callback": '<http://127.0.0.1:12345/event>',
  8225. "NT": "upnp:event",
  8226. "timeout": "Second-1234" }
  8227. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  8228. try:
  8229. resp = conn.getresponse()
  8230. except:
  8231. pass
  8232. with alloc_fail(hapd, 1, "web_connection_unimplemented"):
  8233. conn = httplib.HTTPConnection(url.netloc)
  8234. conn.request("HEAD", "/wps_device.xml")
  8235. try:
  8236. resp = conn.getresponse()
  8237. except:
  8238. pass
  8239. def test_ap_wps_frag_ack_oom(dev, apdev):
  8240. """WPS and fragment ack OOM"""
  8241. dev[0].request("SET wps_fragment_size 50")
  8242. hapd = wps_start_ap(apdev[0])
  8243. with alloc_fail(hapd, 1, "eap_wsc_build_frag_ack"):
  8244. wps_run_pbc_fail_ap(apdev[0], dev[0], hapd)
  8245. def wait_scan_stopped(dev):
  8246. dev.request("ABORT_SCAN")
  8247. for i in range(50):
  8248. res = dev.get_driver_status_field("scan_state")
  8249. if "SCAN_STARTED" not in res and "SCAN_REQUESTED" not in res:
  8250. break
  8251. logger.debug("Waiting for scan to complete")
  8252. time.sleep(0.1)
  8253. @remote_compatible
  8254. def test_ap_wps_eap_wsc_errors(dev, apdev):
  8255. """WPS and EAP-WSC error cases"""
  8256. ssid = "test-wps-conf-pin"
  8257. appin = "12345670"
  8258. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  8259. "wpa_passphrase": "12345678", "wpa": "2",
  8260. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  8261. "fragment_size": "300", "ap_pin": appin }
  8262. hapd = hostapd.add_ap(apdev[0], params)
  8263. bssid = apdev[0]['bssid']
  8264. pin = dev[0].wps_read_pin()
  8265. hapd.request("WPS_PIN any " + pin)
  8266. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
  8267. dev[0].dump_monitor()
  8268. dev[0].wps_reg(bssid, appin + " new_ssid=a", "new ssid", "WPA2PSK", "CCMP",
  8269. "new passphrase", no_wait=True)
  8270. ev = dev[0].wait_event(["WPS-FAIL"], timeout=10)
  8271. if ev is None:
  8272. raise Exception("WPS-FAIL not reported")
  8273. dev[0].request("WPS_CANCEL")
  8274. dev[0].wait_disconnected()
  8275. wait_scan_stopped(dev[0])
  8276. dev[0].dump_monitor()
  8277. dev[0].wps_reg(bssid, appin, "new ssid", "FOO", "CCMP",
  8278. "new passphrase", no_wait=True)
  8279. ev = dev[0].wait_event(["WPS-FAIL"], timeout=10)
  8280. if ev is None:
  8281. raise Exception("WPS-FAIL not reported")
  8282. dev[0].request("WPS_CANCEL")
  8283. dev[0].wait_disconnected()
  8284. wait_scan_stopped(dev[0])
  8285. dev[0].dump_monitor()
  8286. dev[0].wps_reg(bssid, appin, "new ssid", "WPA2PSK", "FOO",
  8287. "new passphrase", no_wait=True)
  8288. ev = dev[0].wait_event(["WPS-FAIL"], timeout=10)
  8289. if ev is None:
  8290. raise Exception("WPS-FAIL not reported")
  8291. dev[0].request("WPS_CANCEL")
  8292. dev[0].wait_disconnected()
  8293. wait_scan_stopped(dev[0])
  8294. dev[0].dump_monitor()
  8295. dev[0].wps_reg(bssid, appin + "new_key=a", "new ssid", "WPA2PSK", "CCMP",
  8296. "new passphrase", no_wait=True)
  8297. ev = dev[0].wait_event(["WPS-FAIL"], timeout=10)
  8298. if ev is None:
  8299. raise Exception("WPS-FAIL not reported")
  8300. dev[0].request("WPS_CANCEL")
  8301. dev[0].wait_disconnected()
  8302. wait_scan_stopped(dev[0])
  8303. dev[0].dump_monitor()
  8304. tests = [ "eap_wsc_init",
  8305. "eap_msg_alloc;eap_wsc_build_msg",
  8306. "wpabuf_alloc;eap_wsc_process_fragment" ]
  8307. for func in tests:
  8308. with alloc_fail(dev[0], 1, func):
  8309. dev[0].request("WPS_PIN %s %s" % (bssid, pin))
  8310. wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
  8311. dev[0].request("WPS_CANCEL")
  8312. dev[0].wait_disconnected()
  8313. wait_scan_stopped(dev[0])
  8314. dev[0].dump_monitor()
  8315. with alloc_fail(dev[0], 1, "eap_msg_alloc;eap_sm_build_expanded_nak"):
  8316. dev[0].wps_reg(bssid, appin + " new_ssid=a", "new ssid", "WPA2PSK",
  8317. "CCMP", "new passphrase", no_wait=True)
  8318. wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
  8319. dev[0].request("WPS_CANCEL")
  8320. dev[0].wait_disconnected()
  8321. wait_scan_stopped(dev[0])
  8322. dev[0].dump_monitor()
  8323. def test_ap_wps_eap_wsc(dev, apdev):
  8324. """WPS and EAP-WSC in network profile"""
  8325. params = int_eap_server_params()
  8326. params["wps_state"] = "2"
  8327. hapd = hostapd.add_ap(apdev[0], params)
  8328. bssid = apdev[0]['bssid']
  8329. logger.info("Unexpected identity")
  8330. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8331. eap="WSC", identity="WFA-SimpleConfig-Enrollee-unexpected",
  8332. wait_connect=False)
  8333. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8334. if ev is None:
  8335. raise Exception("No EAP-Failure seen")
  8336. dev[0].request("REMOVE_NETWORK all")
  8337. dev[0].wait_disconnected()
  8338. logger.info("No phase1 parameter")
  8339. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8340. eap="WSC", identity="WFA-SimpleConfig-Enrollee-1-0",
  8341. wait_connect=False)
  8342. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=5)
  8343. if ev is None:
  8344. raise Exception("Timeout on EAP method start")
  8345. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8346. if ev is None:
  8347. raise Exception("No EAP-Failure seen")
  8348. dev[0].request("REMOVE_NETWORK all")
  8349. dev[0].wait_disconnected()
  8350. logger.info("No PIN/PBC in phase1")
  8351. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8352. eap="WSC", identity="WFA-SimpleConfig-Enrollee-1-0",
  8353. phase1="foo", wait_connect=False)
  8354. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=5)
  8355. if ev is None:
  8356. raise Exception("Timeout on EAP method start")
  8357. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8358. if ev is None:
  8359. raise Exception("No EAP-Failure seen")
  8360. dev[0].request("REMOVE_NETWORK all")
  8361. dev[0].wait_disconnected()
  8362. logger.info("Invalid pkhash in phase1")
  8363. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8364. eap="WSC", identity="WFA-SimpleConfig-Enrollee-1-0",
  8365. phase1="foo pkhash=q pbc=1", wait_connect=False)
  8366. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=5)
  8367. if ev is None:
  8368. raise Exception("Timeout on EAP method start")
  8369. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8370. if ev is None:
  8371. raise Exception("No EAP-Failure seen")
  8372. dev[0].request("REMOVE_NETWORK all")
  8373. dev[0].wait_disconnected()
  8374. logger.info("Zero fragment_size")
  8375. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8376. eap="WSC", identity="WFA-SimpleConfig-Enrollee-1-0",
  8377. fragment_size="0", phase1="pin=12345670", wait_connect=False)
  8378. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=5)
  8379. if ev is None:
  8380. raise Exception("Timeout on EAP method start")
  8381. ev = dev[0].wait_event(["WPS-M2D"], timeout=5)
  8382. if ev is None:
  8383. raise Exception("No M2D seen")
  8384. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8385. if ev is None:
  8386. raise Exception("No EAP-Failure seen")
  8387. dev[0].request("REMOVE_NETWORK all")
  8388. dev[0].wait_disconnected()
  8389. logger.info("Missing new_auth")
  8390. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8391. eap="WSC", identity="WFA-SimpleConfig-Enrollee-1-0",
  8392. phase1="pin=12345670 new_ssid=aa", wait_connect=False)
  8393. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=5)
  8394. if ev is None:
  8395. raise Exception("Timeout on EAP method start")
  8396. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8397. if ev is None:
  8398. raise Exception("No EAP-Failure seen")
  8399. dev[0].request("REMOVE_NETWORK all")
  8400. dev[0].wait_disconnected()
  8401. logger.info("Missing new_encr")
  8402. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8403. eap="WSC", identity="WFA-SimpleConfig-Enrollee-1-0",
  8404. phase1="pin=12345670 new_auth=WPA2PSK new_ssid=aa", wait_connect=False)
  8405. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=5)
  8406. if ev is None:
  8407. raise Exception("Timeout on EAP method start")
  8408. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8409. if ev is None:
  8410. raise Exception("No EAP-Failure seen")
  8411. dev[0].request("REMOVE_NETWORK all")
  8412. dev[0].wait_disconnected()
  8413. logger.info("Missing new_key")
  8414. dev[0].connect("test-wpa2-eap", key_mgmt="WPA-EAP", scan_freq="2412",
  8415. eap="WSC", identity="WFA-SimpleConfig-Enrollee-1-0",
  8416. phase1="pin=12345670 new_auth=WPA2PSK new_ssid=aa new_encr=CCMP",
  8417. wait_connect=False)
  8418. ev = dev[0].wait_event(["CTRL-EVENT-EAP-PROPOSED-METHOD"], timeout=5)
  8419. if ev is None:
  8420. raise Exception("Timeout on EAP method start")
  8421. ev = dev[0].wait_event(["CTRL-EVENT-EAP-FAILURE"], timeout=5)
  8422. if ev is None:
  8423. raise Exception("No EAP-Failure seen")
  8424. dev[0].request("REMOVE_NETWORK all")
  8425. dev[0].wait_disconnected()
  8426. def test_ap_wps_and_bss_limit(dev, apdev):
  8427. """WPS and wpa_supplicant BSS entry limit"""
  8428. try:
  8429. _test_ap_wps_and_bss_limit(dev, apdev)
  8430. finally:
  8431. dev[0].request("SET bss_max_count 200")
  8432. pass
  8433. def _test_ap_wps_and_bss_limit(dev, apdev):
  8434. params = { "ssid": "test-wps", "eap_server": "1", "wps_state": "2",
  8435. "wpa_passphrase": "12345678", "wpa": "2",
  8436. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" }
  8437. hapd = hostapd.add_ap(apdev[0], params)
  8438. params = { "ssid": "test-wps-2", "eap_server": "1", "wps_state": "2",
  8439. "wpa_passphrase": "1234567890", "wpa": "2",
  8440. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" }
  8441. hapd2 = hostapd.add_ap(apdev[1], params)
  8442. id = dev[1].add_network()
  8443. dev[1].set_network(id, "mode", "2")
  8444. dev[1].set_network_quoted(id, "ssid", "wpas-ap-no-wps")
  8445. dev[1].set_network_quoted(id, "psk", "12345678")
  8446. dev[1].set_network(id, "frequency", "2462")
  8447. dev[1].set_network(id, "scan_freq", "2462")
  8448. dev[1].set_network(id, "wps_disabled", "1")
  8449. dev[1].select_network(id)
  8450. id = dev[2].add_network()
  8451. dev[2].set_network(id, "mode", "2")
  8452. dev[2].set_network_quoted(id, "ssid", "wpas-ap")
  8453. dev[2].set_network_quoted(id, "psk", "12345678")
  8454. dev[2].set_network(id, "frequency", "2437")
  8455. dev[2].set_network(id, "scan_freq", "2437")
  8456. dev[2].select_network(id)
  8457. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  8458. wpas.interface_add("wlan5")
  8459. id = wpas.add_network()
  8460. wpas.set_network(id, "mode", "2")
  8461. wpas.set_network_quoted(id, "ssid", "wpas-ap")
  8462. wpas.set_network_quoted(id, "psk", "12345678")
  8463. wpas.set_network(id, "frequency", "2437")
  8464. wpas.set_network(id, "scan_freq", "2437")
  8465. wpas.select_network(id)
  8466. dev[1].wait_connected()
  8467. dev[2].wait_connected()
  8468. wpas.wait_connected()
  8469. wpas.request("WPS_PIN any 12345670")
  8470. hapd.request("WPS_PBC")
  8471. hapd2.request("WPS_PBC")
  8472. dev[0].request("SET bss_max_count 1")
  8473. id = dev[0].add_network()
  8474. dev[0].set_network_quoted(id, "ssid", "testing")
  8475. id = dev[0].add_network()
  8476. dev[0].set_network_quoted(id, "ssid", "testing")
  8477. dev[0].set_network(id, "key_mgmt", "WPS")
  8478. dev[0].request("WPS_PBC")
  8479. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
  8480. dev[0].request("WPS_CANCEL")
  8481. id = dev[0].add_network()
  8482. dev[0].set_network_quoted(id, "ssid", "testing")
  8483. dev[0].set_network(id, "key_mgmt", "WPS")
  8484. dev[0].scan(freq="2412")
  8485. def test_ap_wps_pbc_2ap(dev, apdev):
  8486. """WPS PBC with two APs advertising same SSID"""
  8487. params = { "ssid": "wps", "eap_server": "1", "wps_state": "2",
  8488. "wpa_passphrase": "12345678", "wpa": "2",
  8489. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  8490. "wps_independent": "1"}
  8491. hapd = hostapd.add_ap(apdev[0], params)
  8492. params = { "ssid": "wps", "eap_server": "1", "wps_state": "2",
  8493. "wpa_passphrase": "123456789", "wpa": "2",
  8494. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  8495. "wps_independent": "1"}
  8496. hapd2 = hostapd.add_ap(apdev[1], params)
  8497. hapd.request("WPS_PBC")
  8498. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  8499. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  8500. wpas.dump_monitor()
  8501. wpas.scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  8502. wpas.scan_for_bss(apdev[1]['bssid'], freq="2412")
  8503. wpas.request("WPS_PBC")
  8504. wpas.wait_connected()
  8505. wpas.request("DISCONNECT")
  8506. hapd.request("DISABLE")
  8507. hapd2.request("DISABLE")
  8508. wpas.flush_scan_cache()