order_test.go 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639
  1. package acme
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/x509"
  6. "crypto/x509/pkix"
  7. "encoding/asn1"
  8. "encoding/json"
  9. "fmt"
  10. "net"
  11. "net/url"
  12. "reflect"
  13. "testing"
  14. "time"
  15. "github.com/google/go-cmp/cmp"
  16. "github.com/pkg/errors"
  17. "github.com/smallstep/assert"
  18. "github.com/smallstep/certificates/authority"
  19. "github.com/smallstep/certificates/authority/provisioner"
  20. "github.com/smallstep/certificates/errs"
  21. "github.com/smallstep/certificates/webhook"
  22. "go.step.sm/crypto/keyutil"
  23. "go.step.sm/crypto/x509util"
  24. )
  25. func TestOrder_UpdateStatus(t *testing.T) {
  26. type test struct {
  27. o *Order
  28. err *Error
  29. db DB
  30. }
  31. tests := map[string]func(t *testing.T) test{
  32. "ok/already-invalid": func(t *testing.T) test {
  33. o := &Order{
  34. Status: StatusInvalid,
  35. }
  36. return test{
  37. o: o,
  38. }
  39. },
  40. "ok/already-valid": func(t *testing.T) test {
  41. o := &Order{
  42. Status: StatusInvalid,
  43. }
  44. return test{
  45. o: o,
  46. }
  47. },
  48. "fail/error-unexpected-status": func(t *testing.T) test {
  49. o := &Order{
  50. Status: "foo",
  51. }
  52. return test{
  53. o: o,
  54. err: NewErrorISE("unrecognized order status: %s", o.Status),
  55. }
  56. },
  57. "ok/ready-expired": func(t *testing.T) test {
  58. now := clock.Now()
  59. o := &Order{
  60. ID: "oID",
  61. AccountID: "accID",
  62. Status: StatusReady,
  63. ExpiresAt: now.Add(-5 * time.Minute),
  64. }
  65. return test{
  66. o: o,
  67. db: &MockDB{
  68. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  69. assert.Equals(t, updo.ID, o.ID)
  70. assert.Equals(t, updo.AccountID, o.AccountID)
  71. assert.Equals(t, updo.Status, StatusInvalid)
  72. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  73. return nil
  74. },
  75. },
  76. }
  77. },
  78. "fail/ready-expired-db.UpdateOrder-error": func(t *testing.T) test {
  79. now := clock.Now()
  80. o := &Order{
  81. ID: "oID",
  82. AccountID: "accID",
  83. Status: StatusReady,
  84. ExpiresAt: now.Add(-5 * time.Minute),
  85. }
  86. return test{
  87. o: o,
  88. db: &MockDB{
  89. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  90. assert.Equals(t, updo.ID, o.ID)
  91. assert.Equals(t, updo.AccountID, o.AccountID)
  92. assert.Equals(t, updo.Status, StatusInvalid)
  93. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  94. return errors.New("force")
  95. },
  96. },
  97. err: NewErrorISE("error updating order: force"),
  98. }
  99. },
  100. "ok/pending-expired": func(t *testing.T) test {
  101. now := clock.Now()
  102. o := &Order{
  103. ID: "oID",
  104. AccountID: "accID",
  105. Status: StatusPending,
  106. ExpiresAt: now.Add(-5 * time.Minute),
  107. }
  108. return test{
  109. o: o,
  110. db: &MockDB{
  111. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  112. assert.Equals(t, updo.ID, o.ID)
  113. assert.Equals(t, updo.AccountID, o.AccountID)
  114. assert.Equals(t, updo.Status, StatusInvalid)
  115. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  116. err := NewError(ErrorMalformedType, "order has expired")
  117. assert.HasPrefix(t, updo.Error.Err.Error(), err.Err.Error())
  118. assert.Equals(t, updo.Error.Type, err.Type)
  119. assert.Equals(t, updo.Error.Detail, err.Detail)
  120. assert.Equals(t, updo.Error.Status, err.Status)
  121. assert.Equals(t, updo.Error.Detail, err.Detail)
  122. return nil
  123. },
  124. },
  125. }
  126. },
  127. "ok/invalid": func(t *testing.T) test {
  128. now := clock.Now()
  129. o := &Order{
  130. ID: "oID",
  131. AccountID: "accID",
  132. Status: StatusPending,
  133. ExpiresAt: now.Add(5 * time.Minute),
  134. AuthorizationIDs: []string{"a", "b"},
  135. }
  136. az1 := &Authorization{
  137. ID: "a",
  138. Status: StatusValid,
  139. }
  140. az2 := &Authorization{
  141. ID: "b",
  142. Status: StatusInvalid,
  143. }
  144. return test{
  145. o: o,
  146. db: &MockDB{
  147. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  148. assert.Equals(t, updo.ID, o.ID)
  149. assert.Equals(t, updo.AccountID, o.AccountID)
  150. assert.Equals(t, updo.Status, StatusInvalid)
  151. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  152. return nil
  153. },
  154. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  155. switch id {
  156. case az1.ID:
  157. return az1, nil
  158. case az2.ID:
  159. return az2, nil
  160. default:
  161. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  162. return nil, errors.New("force")
  163. }
  164. },
  165. },
  166. }
  167. },
  168. "ok/still-pending": func(t *testing.T) test {
  169. now := clock.Now()
  170. o := &Order{
  171. ID: "oID",
  172. AccountID: "accID",
  173. Status: StatusPending,
  174. ExpiresAt: now.Add(5 * time.Minute),
  175. AuthorizationIDs: []string{"a", "b"},
  176. }
  177. az1 := &Authorization{
  178. ID: "a",
  179. Status: StatusValid,
  180. }
  181. az2 := &Authorization{
  182. ID: "b",
  183. Status: StatusPending,
  184. }
  185. return test{
  186. o: o,
  187. db: &MockDB{
  188. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  189. switch id {
  190. case az1.ID:
  191. return az1, nil
  192. case az2.ID:
  193. return az2, nil
  194. default:
  195. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  196. return nil, errors.New("force")
  197. }
  198. },
  199. },
  200. }
  201. },
  202. "ok/valid": func(t *testing.T) test {
  203. now := clock.Now()
  204. o := &Order{
  205. ID: "oID",
  206. AccountID: "accID",
  207. Status: StatusPending,
  208. ExpiresAt: now.Add(5 * time.Minute),
  209. AuthorizationIDs: []string{"a", "b"},
  210. }
  211. az1 := &Authorization{
  212. ID: "a",
  213. Status: StatusValid,
  214. }
  215. az2 := &Authorization{
  216. ID: "b",
  217. Status: StatusValid,
  218. }
  219. return test{
  220. o: o,
  221. db: &MockDB{
  222. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  223. assert.Equals(t, updo.ID, o.ID)
  224. assert.Equals(t, updo.AccountID, o.AccountID)
  225. assert.Equals(t, updo.Status, StatusReady)
  226. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  227. return nil
  228. },
  229. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  230. switch id {
  231. case az1.ID:
  232. return az1, nil
  233. case az2.ID:
  234. return az2, nil
  235. default:
  236. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  237. return nil, errors.New("force")
  238. }
  239. },
  240. },
  241. }
  242. },
  243. }
  244. for name, run := range tests {
  245. t.Run(name, func(t *testing.T) {
  246. tc := run(t)
  247. if err := tc.o.UpdateStatus(context.Background(), tc.db); err != nil {
  248. if assert.NotNil(t, tc.err) {
  249. var k *Error
  250. if errors.As(err, &k) {
  251. assert.Equals(t, k.Type, tc.err.Type)
  252. assert.Equals(t, k.Detail, tc.err.Detail)
  253. assert.Equals(t, k.Status, tc.err.Status)
  254. assert.Equals(t, k.Err.Error(), tc.err.Err.Error())
  255. assert.Equals(t, k.Detail, tc.err.Detail)
  256. } else {
  257. assert.FatalError(t, errors.New("unexpected error type"))
  258. }
  259. }
  260. } else {
  261. assert.Nil(t, tc.err)
  262. }
  263. })
  264. }
  265. }
  266. type mockSignAuth struct {
  267. signWithContext func(ctx context.Context, csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
  268. areSANsAllowed func(ctx context.Context, sans []string) error
  269. loadProvisionerByName func(string) (provisioner.Interface, error)
  270. ret1, ret2 interface{}
  271. err error
  272. }
  273. func (m *mockSignAuth) SignWithContext(ctx context.Context, csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  274. if m.signWithContext != nil {
  275. return m.signWithContext(ctx, csr, signOpts, extraOpts...)
  276. } else if m.err != nil {
  277. return nil, m.err
  278. }
  279. return []*x509.Certificate{m.ret1.(*x509.Certificate), m.ret2.(*x509.Certificate)}, m.err
  280. }
  281. func (m *mockSignAuth) AreSANsAllowed(ctx context.Context, sans []string) error {
  282. if m.areSANsAllowed != nil {
  283. return m.areSANsAllowed(ctx, sans)
  284. }
  285. return m.err
  286. }
  287. func (m *mockSignAuth) LoadProvisionerByName(name string) (provisioner.Interface, error) {
  288. if m.loadProvisionerByName != nil {
  289. return m.loadProvisionerByName(name)
  290. }
  291. return m.ret1.(provisioner.Interface), m.err
  292. }
  293. func (m *mockSignAuth) IsRevoked(string) (bool, error) {
  294. return false, nil
  295. }
  296. func (m *mockSignAuth) Revoke(context.Context, *authority.RevokeOptions) error {
  297. return nil
  298. }
  299. func TestOrder_Finalize(t *testing.T) {
  300. mustSigner := func(kty, crv string, size int) crypto.Signer {
  301. s, err := keyutil.GenerateSigner(kty, crv, size)
  302. if err != nil {
  303. t.Fatal(err)
  304. }
  305. return s
  306. }
  307. type test struct {
  308. o *Order
  309. err *Error
  310. db DB
  311. ca CertificateAuthority
  312. csr *x509.CertificateRequest
  313. prov Provisioner
  314. }
  315. tests := map[string]func(t *testing.T) test{
  316. "fail/invalid": func(t *testing.T) test {
  317. o := &Order{
  318. ID: "oid",
  319. Status: StatusInvalid,
  320. }
  321. return test{
  322. o: o,
  323. err: NewError(ErrorOrderNotReadyType, "order %s has been abandoned", o.ID),
  324. }
  325. },
  326. "fail/pending": func(t *testing.T) test {
  327. now := clock.Now()
  328. o := &Order{
  329. ID: "oID",
  330. AccountID: "accID",
  331. Status: StatusPending,
  332. ExpiresAt: now.Add(5 * time.Minute),
  333. AuthorizationIDs: []string{"a", "b"},
  334. }
  335. az1 := &Authorization{
  336. ID: "a",
  337. Status: StatusValid,
  338. }
  339. az2 := &Authorization{
  340. ID: "b",
  341. Status: StatusPending,
  342. ExpiresAt: now.Add(5 * time.Minute),
  343. }
  344. return test{
  345. o: o,
  346. db: &MockDB{
  347. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  348. switch id {
  349. case az1.ID:
  350. return az1, nil
  351. case az2.ID:
  352. return az2, nil
  353. default:
  354. assert.FatalError(t, errors.Errorf("unexpected authz key %s", id))
  355. return nil, errors.New("force")
  356. }
  357. },
  358. },
  359. err: NewError(ErrorOrderNotReadyType, "order %s is not ready", o.ID),
  360. }
  361. },
  362. "ok/already-valid": func(t *testing.T) test {
  363. o := &Order{
  364. ID: "oid",
  365. Status: StatusValid,
  366. }
  367. return test{
  368. o: o,
  369. }
  370. },
  371. "fail/error-unexpected-status": func(t *testing.T) test {
  372. now := clock.Now()
  373. o := &Order{
  374. ID: "oID",
  375. AccountID: "accID",
  376. Status: "foo",
  377. ExpiresAt: now.Add(5 * time.Minute),
  378. AuthorizationIDs: []string{"a", "b"},
  379. }
  380. return test{
  381. o: o,
  382. err: NewErrorISE("unrecognized order status: %s", o.Status),
  383. }
  384. },
  385. "fail/non-matching-permanent-identifier-common-name": func(t *testing.T) test {
  386. now := clock.Now()
  387. o := &Order{
  388. ID: "oID",
  389. AccountID: "accID",
  390. Status: StatusReady,
  391. ExpiresAt: now.Add(5 * time.Minute),
  392. AuthorizationIDs: []string{"a", "b"},
  393. Identifiers: []Identifier{
  394. {Type: "permanent-identifier", Value: "a-permanent-identifier"},
  395. },
  396. }
  397. signer := mustSigner("EC", "P-256", 0)
  398. fingerprint, err := keyutil.Fingerprint(signer.Public())
  399. if err != nil {
  400. t.Fatal(err)
  401. }
  402. csr := &x509.CertificateRequest{
  403. Subject: pkix.Name{
  404. CommonName: "a-different-identifier",
  405. },
  406. PublicKey: signer.Public(),
  407. ExtraExtensions: []pkix.Extension{
  408. {
  409. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  410. Value: []byte("a-permanent-identifier"),
  411. },
  412. },
  413. }
  414. return test{
  415. o: o,
  416. csr: csr,
  417. db: &MockDB{
  418. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  419. switch id {
  420. case "a":
  421. return &Authorization{
  422. ID: id,
  423. Status: StatusValid,
  424. }, nil
  425. case "b":
  426. return &Authorization{
  427. ID: id,
  428. Fingerprint: fingerprint,
  429. Status: StatusValid,
  430. }, nil
  431. default:
  432. assert.FatalError(t, errors.Errorf("unexpected authorization %s", id))
  433. return nil, errors.New("force")
  434. }
  435. },
  436. MockUpdateOrder: func(ctx context.Context, o *Order) error {
  437. return nil
  438. },
  439. },
  440. err: &Error{
  441. Type: "urn:ietf:params:acme:error:badCSR",
  442. Detail: "The CSR is unacceptable",
  443. Status: 400,
  444. Err: fmt.Errorf("CSR Subject Common Name does not match identifiers exactly: "+
  445. "CSR Subject Common Name = %s, Order Permanent Identifier = %s", csr.Subject.CommonName, "a-permanent-identifier"),
  446. },
  447. }
  448. },
  449. "fail/error-provisioner-auth": func(t *testing.T) test {
  450. now := clock.Now()
  451. o := &Order{
  452. ID: "oID",
  453. AccountID: "accID",
  454. Status: StatusReady,
  455. ExpiresAt: now.Add(5 * time.Minute),
  456. AuthorizationIDs: []string{"a", "b"},
  457. Identifiers: []Identifier{
  458. {Type: "dns", Value: "foo.internal"},
  459. {Type: "dns", Value: "bar.internal"},
  460. },
  461. }
  462. csr := &x509.CertificateRequest{
  463. Subject: pkix.Name{
  464. CommonName: "foo.internal",
  465. },
  466. DNSNames: []string{"bar.internal"},
  467. }
  468. return test{
  469. o: o,
  470. csr: csr,
  471. prov: &MockProvisioner{
  472. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  473. assert.Equals(t, token, "")
  474. return nil, errors.New("force")
  475. },
  476. },
  477. db: &MockDB{
  478. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  479. return &Authorization{ID: id, Status: StatusValid}, nil
  480. },
  481. },
  482. err: NewErrorISE("error retrieving authorization options from ACME provisioner: force"),
  483. }
  484. },
  485. "fail/error-template-options": func(t *testing.T) test {
  486. now := clock.Now()
  487. o := &Order{
  488. ID: "oID",
  489. AccountID: "accID",
  490. Status: StatusReady,
  491. ExpiresAt: now.Add(5 * time.Minute),
  492. AuthorizationIDs: []string{"a", "b"},
  493. Identifiers: []Identifier{
  494. {Type: "dns", Value: "foo.internal"},
  495. {Type: "dns", Value: "bar.internal"},
  496. },
  497. }
  498. csr := &x509.CertificateRequest{
  499. Subject: pkix.Name{
  500. CommonName: "foo.internal",
  501. },
  502. DNSNames: []string{"bar.internal"},
  503. }
  504. return test{
  505. o: o,
  506. csr: csr,
  507. prov: &MockProvisioner{
  508. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  509. assert.Equals(t, token, "")
  510. return nil, nil
  511. },
  512. MgetOptions: func() *provisioner.Options {
  513. return &provisioner.Options{
  514. X509: &provisioner.X509Options{
  515. TemplateData: json.RawMessage([]byte("fo{o")),
  516. },
  517. }
  518. },
  519. },
  520. db: &MockDB{
  521. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  522. return &Authorization{ID: id, Status: StatusValid}, nil
  523. },
  524. },
  525. err: NewErrorISE("error creating template options from ACME provisioner: error unmarshaling template data: invalid character 'o' in literal false (expecting 'a')"),
  526. }
  527. },
  528. "fail/error-ca-sign": func(t *testing.T) test {
  529. now := clock.Now()
  530. o := &Order{
  531. ID: "oID",
  532. AccountID: "accID",
  533. Status: StatusReady,
  534. ExpiresAt: now.Add(5 * time.Minute),
  535. AuthorizationIDs: []string{"a", "b"},
  536. Identifiers: []Identifier{
  537. {Type: "dns", Value: "foo.internal"},
  538. {Type: "dns", Value: "bar.internal"},
  539. },
  540. }
  541. csr := &x509.CertificateRequest{
  542. Subject: pkix.Name{
  543. CommonName: "foo.internal",
  544. },
  545. DNSNames: []string{"bar.internal"},
  546. }
  547. return test{
  548. o: o,
  549. csr: csr,
  550. prov: &MockProvisioner{
  551. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  552. assert.Equals(t, token, "")
  553. return nil, nil
  554. },
  555. MgetOptions: func() *provisioner.Options {
  556. return nil
  557. },
  558. },
  559. ca: &mockSignAuth{
  560. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  561. assert.Equals(t, _csr, csr)
  562. return nil, errors.New("force")
  563. },
  564. },
  565. db: &MockDB{
  566. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  567. return &Authorization{ID: id, Status: StatusValid}, nil
  568. },
  569. },
  570. err: NewErrorISE("error signing certificate for order oID: force"),
  571. }
  572. },
  573. "fail/webhook-error": func(t *testing.T) test {
  574. now := clock.Now()
  575. o := &Order{
  576. ID: "oID",
  577. AccountID: "accID",
  578. Status: StatusReady,
  579. ExpiresAt: now.Add(5 * time.Minute),
  580. AuthorizationIDs: []string{"a", "b"},
  581. Identifiers: []Identifier{
  582. {Type: "dns", Value: "foo.internal"},
  583. {Type: "dns", Value: "bar.internal"},
  584. },
  585. }
  586. csr := &x509.CertificateRequest{
  587. Subject: pkix.Name{
  588. CommonName: "foo.internal",
  589. },
  590. DNSNames: []string{"bar.internal"},
  591. }
  592. return test{
  593. o: o,
  594. csr: csr,
  595. prov: &MockProvisioner{
  596. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  597. assert.Equals(t, token, "")
  598. return nil, nil
  599. },
  600. MgetOptions: func() *provisioner.Options {
  601. return nil
  602. },
  603. },
  604. ca: &mockSignAuth{
  605. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  606. assert.Equals(t, _csr, csr)
  607. return nil, errs.ForbiddenErr(&webhook.Error{Code: "theCode", Message: "The message"}, "forbidden error")
  608. },
  609. },
  610. db: &MockDB{
  611. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  612. return &Authorization{ID: id, Status: StatusValid}, nil
  613. },
  614. },
  615. err: NewDetailedError(ErrorUnauthorizedType, "The message (theCode)").AddSubproblems(Subproblem{
  616. Type: "urn:smallstep:acme:error:theCode",
  617. Detail: "The message",
  618. }),
  619. }
  620. },
  621. "fail/error-db.CreateCertificate": func(t *testing.T) test {
  622. now := clock.Now()
  623. o := &Order{
  624. ID: "oID",
  625. AccountID: "accID",
  626. Status: StatusReady,
  627. ExpiresAt: now.Add(5 * time.Minute),
  628. AuthorizationIDs: []string{"a", "b"},
  629. Identifiers: []Identifier{
  630. {Type: "dns", Value: "foo.internal"},
  631. {Type: "dns", Value: "bar.internal"},
  632. },
  633. }
  634. csr := &x509.CertificateRequest{
  635. Subject: pkix.Name{
  636. CommonName: "foo.internal",
  637. },
  638. DNSNames: []string{"bar.internal"},
  639. }
  640. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  641. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  642. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  643. return test{
  644. o: o,
  645. csr: csr,
  646. prov: &MockProvisioner{
  647. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  648. assert.Equals(t, token, "")
  649. return nil, nil
  650. },
  651. MgetOptions: func() *provisioner.Options {
  652. return nil
  653. },
  654. },
  655. ca: &mockSignAuth{
  656. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  657. assert.Equals(t, _csr, csr)
  658. return []*x509.Certificate{foo, bar, baz}, nil
  659. },
  660. },
  661. db: &MockDB{
  662. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  663. return &Authorization{ID: id, Status: StatusValid}, nil
  664. },
  665. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  666. assert.Equals(t, cert.AccountID, o.AccountID)
  667. assert.Equals(t, cert.OrderID, o.ID)
  668. assert.Equals(t, cert.Leaf, foo)
  669. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  670. return errors.New("force")
  671. },
  672. },
  673. err: NewErrorISE("error creating certificate for order oID: force"),
  674. }
  675. },
  676. "fail/error-db.UpdateOrder": func(t *testing.T) test {
  677. now := clock.Now()
  678. o := &Order{
  679. ID: "oID",
  680. AccountID: "accID",
  681. Status: StatusReady,
  682. ExpiresAt: now.Add(5 * time.Minute),
  683. AuthorizationIDs: []string{"a", "b"},
  684. Identifiers: []Identifier{
  685. {Type: "dns", Value: "foo.internal"},
  686. {Type: "dns", Value: "bar.internal"},
  687. },
  688. }
  689. csr := &x509.CertificateRequest{
  690. Subject: pkix.Name{
  691. CommonName: "foo.internal",
  692. },
  693. DNSNames: []string{"bar.internal"},
  694. }
  695. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  696. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  697. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  698. return test{
  699. o: o,
  700. csr: csr,
  701. prov: &MockProvisioner{
  702. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  703. assert.Equals(t, token, "")
  704. return nil, nil
  705. },
  706. MgetOptions: func() *provisioner.Options {
  707. return nil
  708. },
  709. },
  710. ca: &mockSignAuth{
  711. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  712. assert.Equals(t, _csr, csr)
  713. return []*x509.Certificate{foo, bar, baz}, nil
  714. },
  715. },
  716. db: &MockDB{
  717. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  718. return &Authorization{ID: id, Status: StatusValid}, nil
  719. },
  720. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  721. cert.ID = "certID"
  722. assert.Equals(t, cert.AccountID, o.AccountID)
  723. assert.Equals(t, cert.OrderID, o.ID)
  724. assert.Equals(t, cert.Leaf, foo)
  725. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  726. return nil
  727. },
  728. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  729. assert.Equals(t, updo.CertificateID, "certID")
  730. assert.Equals(t, updo.Status, StatusValid)
  731. assert.Equals(t, updo.ID, o.ID)
  732. assert.Equals(t, updo.AccountID, o.AccountID)
  733. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  734. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  735. assert.Equals(t, updo.Identifiers, o.Identifiers)
  736. return errors.New("force")
  737. },
  738. },
  739. err: NewErrorISE("error updating order oID: force"),
  740. }
  741. },
  742. "fail/csr-fingerprint": func(t *testing.T) test {
  743. now := clock.Now()
  744. o := &Order{
  745. ID: "oID",
  746. AccountID: "accID",
  747. Status: StatusReady,
  748. ExpiresAt: now.Add(5 * time.Minute),
  749. AuthorizationIDs: []string{"a", "b"},
  750. Identifiers: []Identifier{
  751. {Type: "permanent-identifier", Value: "a-permanent-identifier"},
  752. },
  753. }
  754. signer := mustSigner("EC", "P-256", 0)
  755. csr := &x509.CertificateRequest{
  756. Subject: pkix.Name{
  757. CommonName: "a-permanent-identifier",
  758. },
  759. PublicKey: signer.Public(),
  760. ExtraExtensions: []pkix.Extension{
  761. {
  762. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  763. Value: []byte("a-permanent-identifier"),
  764. },
  765. },
  766. }
  767. leaf := &x509.Certificate{
  768. Subject: pkix.Name{CommonName: "a-permanent-identifier"},
  769. PublicKey: signer.Public(),
  770. ExtraExtensions: []pkix.Extension{
  771. {
  772. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  773. Value: []byte("a-permanent-identifier"),
  774. },
  775. },
  776. }
  777. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  778. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  779. return test{
  780. o: o,
  781. csr: csr,
  782. prov: &MockProvisioner{
  783. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  784. assert.Equals(t, token, "")
  785. return nil, nil
  786. },
  787. MgetOptions: func() *provisioner.Options {
  788. return nil
  789. },
  790. },
  791. ca: &mockSignAuth{
  792. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  793. assert.Equals(t, _csr, csr)
  794. return []*x509.Certificate{leaf, inter, root}, nil
  795. },
  796. },
  797. db: &MockDB{
  798. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  799. return &Authorization{
  800. ID: id,
  801. Fingerprint: "other-fingerprint",
  802. Status: StatusValid,
  803. }, nil
  804. },
  805. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  806. cert.ID = "certID"
  807. assert.Equals(t, cert.AccountID, o.AccountID)
  808. assert.Equals(t, cert.OrderID, o.ID)
  809. assert.Equals(t, cert.Leaf, leaf)
  810. assert.Equals(t, cert.Intermediates, []*x509.Certificate{inter, root})
  811. return nil
  812. },
  813. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  814. assert.Equals(t, updo.CertificateID, "certID")
  815. assert.Equals(t, updo.Status, StatusValid)
  816. assert.Equals(t, updo.ID, o.ID)
  817. assert.Equals(t, updo.AccountID, o.AccountID)
  818. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  819. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  820. assert.Equals(t, updo.Identifiers, o.Identifiers)
  821. return nil
  822. },
  823. },
  824. err: NewError(ErrorUnauthorizedType, "order oID csr does not match the attested key"),
  825. }
  826. },
  827. "ok/permanent-identifier": func(t *testing.T) test {
  828. now := clock.Now()
  829. o := &Order{
  830. ID: "oID",
  831. AccountID: "accID",
  832. Status: StatusReady,
  833. ExpiresAt: now.Add(5 * time.Minute),
  834. AuthorizationIDs: []string{"a", "b"},
  835. Identifiers: []Identifier{
  836. {Type: "permanent-identifier", Value: "a-permanent-identifier"},
  837. },
  838. }
  839. signer := mustSigner("EC", "P-256", 0)
  840. fingerprint, err := keyutil.Fingerprint(signer.Public())
  841. if err != nil {
  842. t.Fatal(err)
  843. }
  844. csr := &x509.CertificateRequest{
  845. Subject: pkix.Name{
  846. CommonName: "a-permanent-identifier",
  847. },
  848. PublicKey: signer.Public(),
  849. ExtraExtensions: []pkix.Extension{
  850. {
  851. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  852. Value: []byte("a-permanent-identifier"),
  853. },
  854. },
  855. }
  856. leaf := &x509.Certificate{
  857. Subject: pkix.Name{CommonName: "a-permanent-identifier"},
  858. PublicKey: signer.Public(),
  859. ExtraExtensions: []pkix.Extension{
  860. {
  861. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  862. Value: []byte("a-permanent-identifier"),
  863. },
  864. },
  865. }
  866. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  867. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  868. return test{
  869. o: o,
  870. csr: csr,
  871. prov: &MockProvisioner{
  872. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  873. assert.Equals(t, token, "")
  874. return nil, nil
  875. },
  876. MgetOptions: func() *provisioner.Options {
  877. return nil
  878. },
  879. },
  880. ca: &mockSignAuth{
  881. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  882. assert.Equals(t, _csr, csr)
  883. return []*x509.Certificate{leaf, inter, root}, nil
  884. },
  885. },
  886. db: &MockDB{
  887. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  888. switch id {
  889. case "a":
  890. return &Authorization{
  891. ID: id,
  892. Status: StatusValid,
  893. }, nil
  894. case "b":
  895. return &Authorization{
  896. ID: id,
  897. Fingerprint: fingerprint,
  898. Status: StatusValid,
  899. }, nil
  900. default:
  901. assert.FatalError(t, errors.Errorf("unexpected authorization %s", id))
  902. return nil, errors.New("force")
  903. }
  904. },
  905. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  906. cert.ID = "certID"
  907. assert.Equals(t, cert.AccountID, o.AccountID)
  908. assert.Equals(t, cert.OrderID, o.ID)
  909. assert.Equals(t, cert.Leaf, leaf)
  910. assert.Equals(t, cert.Intermediates, []*x509.Certificate{inter, root})
  911. return nil
  912. },
  913. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  914. assert.Equals(t, updo.CertificateID, "certID")
  915. assert.Equals(t, updo.Status, StatusValid)
  916. assert.Equals(t, updo.ID, o.ID)
  917. assert.Equals(t, updo.AccountID, o.AccountID)
  918. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  919. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  920. assert.Equals(t, updo.Identifiers, o.Identifiers)
  921. return nil
  922. },
  923. },
  924. }
  925. },
  926. "ok/permanent-identifier-only": func(t *testing.T) test {
  927. now := clock.Now()
  928. o := &Order{
  929. ID: "oID",
  930. AccountID: "accID",
  931. Status: StatusReady,
  932. ExpiresAt: now.Add(5 * time.Minute),
  933. AuthorizationIDs: []string{"a", "b"},
  934. Identifiers: []Identifier{
  935. {Type: "dns", Value: "foo.internal"},
  936. {Type: "permanent-identifier", Value: "a-permanent-identifier"},
  937. },
  938. }
  939. signer := mustSigner("EC", "P-256", 0)
  940. fingerprint, err := keyutil.Fingerprint(signer.Public())
  941. if err != nil {
  942. t.Fatal(err)
  943. }
  944. csr := &x509.CertificateRequest{
  945. Subject: pkix.Name{
  946. CommonName: "a-permanent-identifier",
  947. },
  948. DNSNames: []string{"foo.internal"},
  949. PublicKey: signer.Public(),
  950. ExtraExtensions: []pkix.Extension{
  951. {
  952. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  953. Value: []byte("a-permanent-identifier"),
  954. },
  955. },
  956. }
  957. leaf := &x509.Certificate{
  958. Subject: pkix.Name{CommonName: "a-permanent-identifier"},
  959. PublicKey: signer.Public(),
  960. ExtraExtensions: []pkix.Extension{
  961. {
  962. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3},
  963. Value: []byte("a-permanent-identifier"),
  964. },
  965. },
  966. }
  967. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  968. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  969. return test{
  970. o: o,
  971. csr: csr,
  972. prov: &MockProvisioner{
  973. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  974. assert.Equals(t, token, "")
  975. return nil, nil
  976. },
  977. MgetOptions: func() *provisioner.Options {
  978. return nil
  979. },
  980. },
  981. // TODO(hs): we should work on making the mocks more realistic. Ideally, we should get rid of
  982. // the mock entirely, relying on an instances of provisioner, authority and DB (possibly hardest), so
  983. // that behavior of the tests is what an actual CA would do. We could gradually phase them out by
  984. // using the mocking functions as a wrapper for actual test helpers generated per test case or per
  985. // function that's tested.
  986. ca: &mockSignAuth{
  987. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  988. assert.Equals(t, _csr, csr)
  989. return []*x509.Certificate{leaf, inter, root}, nil
  990. },
  991. },
  992. db: &MockDB{
  993. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  994. return &Authorization{
  995. ID: id,
  996. Fingerprint: fingerprint,
  997. Status: StatusValid,
  998. }, nil
  999. },
  1000. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  1001. cert.ID = "certID"
  1002. assert.Equals(t, cert.AccountID, o.AccountID)
  1003. assert.Equals(t, cert.OrderID, o.ID)
  1004. assert.Equals(t, cert.Leaf, leaf)
  1005. assert.Equals(t, cert.Intermediates, []*x509.Certificate{inter, root})
  1006. return nil
  1007. },
  1008. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  1009. assert.Equals(t, updo.CertificateID, "certID")
  1010. assert.Equals(t, updo.Status, StatusValid)
  1011. assert.Equals(t, updo.ID, o.ID)
  1012. assert.Equals(t, updo.AccountID, o.AccountID)
  1013. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  1014. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  1015. assert.Equals(t, updo.Identifiers, o.Identifiers)
  1016. return nil
  1017. },
  1018. },
  1019. }
  1020. },
  1021. "fail/csr-wire-id-csr-uri-missing": func(t *testing.T) test {
  1022. now := clock.Now()
  1023. o := &Order{
  1024. ID: "oID",
  1025. AccountID: "accID",
  1026. Status: StatusReady,
  1027. ExpiresAt: now.Add(5 * time.Minute),
  1028. AuthorizationIDs: []string{"a", "b"},
  1029. Identifiers: []Identifier{
  1030. {Type: "wireapp-device", Value: "{\"name\": \"device\", \"domain\": \"wire.com\", \"client-id\": \"wireapp://CzbfFjDOQrenCbDxVmgnFw!594930e9d50bb175@wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1031. },
  1032. }
  1033. signer := mustSigner("EC", "P-256", 0)
  1034. _, err := keyutil.Fingerprint(signer.Public())
  1035. if err != nil {
  1036. t.Fatal(err)
  1037. }
  1038. csr := &x509.CertificateRequest{
  1039. Subject: pkix.Name{
  1040. Names: []pkix.AttributeTypeAndValue{
  1041. {Type: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241}, Value: "device"},
  1042. },
  1043. Organization: []string{"wire.com"},
  1044. },
  1045. PublicKey: signer.Public(),
  1046. ExtraExtensions: []pkix.Extension{
  1047. {
  1048. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1049. Value: []byte("a-wireapp-user"),
  1050. },
  1051. },
  1052. }
  1053. leaf := &x509.Certificate{
  1054. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1055. PublicKey: signer.Public(),
  1056. ExtraExtensions: []pkix.Extension{
  1057. {
  1058. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1059. Value: []byte("a-wireapp-user"),
  1060. },
  1061. },
  1062. }
  1063. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1064. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1065. return test{
  1066. o: o,
  1067. csr: csr,
  1068. prov: &MockProvisioner{
  1069. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1070. assert.Equals(t, token, "")
  1071. return nil, nil
  1072. },
  1073. MgetOptions: func() *provisioner.Options {
  1074. return nil
  1075. },
  1076. },
  1077. ca: &mockSignAuth{
  1078. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1079. assert.Equals(t, _csr, csr)
  1080. return []*x509.Certificate{leaf, inter, root}, nil
  1081. },
  1082. },
  1083. db: &MockWireDB{
  1084. MockDB: MockDB{
  1085. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1086. return &Authorization{ID: id, Status: StatusValid}, nil
  1087. },
  1088. },
  1089. MockGetDpopToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1090. assert.Equals(t, orderID, o.ID)
  1091. dpopMap := map[string]interface{}{
  1092. "dpop": "a-dpop-token",
  1093. }
  1094. return dpopMap, nil
  1095. },
  1096. MockGetOidcToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1097. assert.Equals(t, orderID, o.ID)
  1098. oidcMap := map[string]interface{}{
  1099. "oidc": "a-oidc-token",
  1100. }
  1101. return oidcMap, nil
  1102. },
  1103. },
  1104. err: NewError(ErrorBadCSRType, "CSR URIs do not match identifiers exactly: CSR URIs = [], Order URIs = [wireapp://CzbfFjDOQrenCbDxVmgnFw%%21594930e9d50bb175@wire.com]"),
  1105. }
  1106. },
  1107. "fail/csr-wire-id-csr-uri-mismatch": func(t *testing.T) test {
  1108. now := clock.Now()
  1109. o := &Order{
  1110. ID: "oID",
  1111. AccountID: "accID",
  1112. Status: StatusReady,
  1113. ExpiresAt: now.Add(5 * time.Minute),
  1114. AuthorizationIDs: []string{"a", "b"},
  1115. Identifiers: []Identifier{
  1116. {Type: "wireapp-device", Value: "{\"name\": \"device\", \"domain\": \"wire.com\", \"client-id\": \"wireapp://CzbfFjDOQrenCbDxVmgnFw!594930e9d50bb175@wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1117. },
  1118. }
  1119. signer := mustSigner("EC", "P-256", 0)
  1120. _, err := keyutil.Fingerprint(signer.Public())
  1121. if err != nil {
  1122. t.Fatal(err)
  1123. }
  1124. wireURL, _ := url.Parse("someurl.com")
  1125. csr := &x509.CertificateRequest{
  1126. Subject: pkix.Name{
  1127. Names: []pkix.AttributeTypeAndValue{
  1128. {Type: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241}, Value: "device"},
  1129. },
  1130. Organization: []string{"wire.com"},
  1131. },
  1132. URIs: []*url.URL{
  1133. wireURL,
  1134. },
  1135. PublicKey: signer.Public(),
  1136. ExtraExtensions: []pkix.Extension{
  1137. {
  1138. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1139. Value: []byte("a-wireapp-user"),
  1140. },
  1141. },
  1142. }
  1143. leaf := &x509.Certificate{
  1144. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1145. PublicKey: signer.Public(),
  1146. ExtraExtensions: []pkix.Extension{
  1147. {
  1148. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1149. Value: []byte("a-wireapp-user"),
  1150. },
  1151. },
  1152. }
  1153. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1154. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1155. return test{
  1156. o: o,
  1157. csr: csr,
  1158. prov: &MockProvisioner{
  1159. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1160. assert.Equals(t, token, "")
  1161. return nil, nil
  1162. },
  1163. MgetOptions: func() *provisioner.Options {
  1164. return nil
  1165. },
  1166. },
  1167. ca: &mockSignAuth{
  1168. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1169. assert.Equals(t, _csr, csr)
  1170. return []*x509.Certificate{leaf, inter, root}, nil
  1171. },
  1172. },
  1173. db: &MockWireDB{
  1174. MockDB: MockDB{
  1175. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1176. return &Authorization{ID: id, Status: StatusValid}, nil
  1177. },
  1178. },
  1179. MockGetDpopToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1180. assert.Equals(t, orderID, o.ID)
  1181. dpopMap := map[string]interface{}{
  1182. "dpop": "a-dpop-token",
  1183. }
  1184. return dpopMap, nil
  1185. },
  1186. MockGetOidcToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1187. assert.Equals(t, orderID, o.ID)
  1188. oidcMap := map[string]interface{}{
  1189. "oidc": "a-oidc-token",
  1190. }
  1191. return oidcMap, nil
  1192. },
  1193. },
  1194. err: NewError(ErrorBadCSRType, "CSR URIs do not match identifiers exactly: CSR URIs = [someurl.com], Order URIs = [wireapp://CzbfFjDOQrenCbDxVmgnFw%%21594930e9d50bb175@wire.com]"),
  1195. }
  1196. },
  1197. "fail/other-than-wire-ids-present": func(t *testing.T) test {
  1198. now := clock.Now()
  1199. o := &Order{
  1200. ID: "oID",
  1201. AccountID: "accID",
  1202. Status: StatusReady,
  1203. ExpiresAt: now.Add(5 * time.Minute),
  1204. AuthorizationIDs: []string{"a", "b"},
  1205. Identifiers: []Identifier{
  1206. {Type: "wireapp-device", Value: "{\"name\": \"device\", \"domain\": \"wire.com\", \"client-id\": \"wireapp://CzbfFjDOQrenCbDxVmgnFw!594930e9d50bb175@wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1207. {Type: "permanent-identifier", Value: "a-permanent-identifier"},
  1208. },
  1209. }
  1210. signer := mustSigner("EC", "P-256", 0)
  1211. _, err := keyutil.Fingerprint(signer.Public())
  1212. if err != nil {
  1213. t.Fatal(err)
  1214. }
  1215. wireURL, _ := url.Parse("wireapp://CzbfFjDOQrenCbDxVmgnFw!594930e9d50bb175@wire.com")
  1216. csr := &x509.CertificateRequest{
  1217. Subject: pkix.Name{
  1218. Names: []pkix.AttributeTypeAndValue{
  1219. {Type: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241}, Value: "device"},
  1220. },
  1221. Organization: []string{"wire.com"},
  1222. },
  1223. URIs: []*url.URL{
  1224. wireURL,
  1225. },
  1226. PublicKey: signer.Public(),
  1227. ExtraExtensions: []pkix.Extension{
  1228. {
  1229. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1230. Value: []byte("a-wireapp-user"),
  1231. },
  1232. },
  1233. }
  1234. leaf := &x509.Certificate{
  1235. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1236. PublicKey: signer.Public(),
  1237. ExtraExtensions: []pkix.Extension{
  1238. {
  1239. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1240. Value: []byte("a-wireapp-user"),
  1241. },
  1242. },
  1243. }
  1244. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1245. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1246. return test{
  1247. o: o,
  1248. csr: csr,
  1249. prov: &MockProvisioner{
  1250. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1251. assert.Equals(t, token, "")
  1252. return nil, nil
  1253. },
  1254. MgetOptions: func() *provisioner.Options {
  1255. return nil
  1256. },
  1257. },
  1258. ca: &mockSignAuth{
  1259. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1260. assert.Equals(t, _csr, csr)
  1261. return []*x509.Certificate{leaf, inter, root}, nil
  1262. },
  1263. },
  1264. db: &MockWireDB{
  1265. MockDB: MockDB{
  1266. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1267. return &Authorization{ID: id, Status: StatusValid}, nil
  1268. },
  1269. },
  1270. },
  1271. err: NewError(ErrorServerInternalType, "order must have exactly one WireUser and WireDevice identifier"),
  1272. }
  1273. },
  1274. "fail/wire-id-org-missing": func(t *testing.T) test {
  1275. now := clock.Now()
  1276. o := &Order{
  1277. ID: "oID",
  1278. AccountID: "accID",
  1279. Status: StatusReady,
  1280. ExpiresAt: now.Add(5 * time.Minute),
  1281. AuthorizationIDs: []string{"a", "b"},
  1282. Identifiers: []Identifier{
  1283. {Type: "wireapp-user", Value: "{\"name\": \"Alice Smith\", \"domain\": \"wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1284. },
  1285. }
  1286. signer := mustSigner("EC", "P-256", 0)
  1287. _, err := keyutil.Fingerprint(signer.Public())
  1288. if err != nil {
  1289. t.Fatal(err)
  1290. }
  1291. wireURL, _ := url.Parse("wireapp://%40alice_wire@wire.com")
  1292. csr := &x509.CertificateRequest{
  1293. Subject: pkix.Name{
  1294. Names: []pkix.AttributeTypeAndValue{
  1295. {Type: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241}, Value: "Alice Smith"},
  1296. },
  1297. },
  1298. URIs: []*url.URL{
  1299. wireURL,
  1300. },
  1301. PublicKey: signer.Public(),
  1302. }
  1303. leaf := &x509.Certificate{
  1304. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1305. PublicKey: signer.Public(),
  1306. ExtraExtensions: []pkix.Extension{
  1307. {
  1308. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1309. Value: []byte("a-wireapp-user"),
  1310. },
  1311. },
  1312. }
  1313. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1314. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1315. return test{
  1316. o: o,
  1317. csr: csr,
  1318. prov: &MockProvisioner{
  1319. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1320. assert.Equals(t, token, "")
  1321. return nil, nil
  1322. },
  1323. MgetOptions: func() *provisioner.Options {
  1324. return nil
  1325. },
  1326. },
  1327. ca: &mockSignAuth{
  1328. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1329. assert.Equals(t, _csr, csr)
  1330. return []*x509.Certificate{leaf, inter, root}, nil
  1331. },
  1332. },
  1333. db: &MockWireDB{
  1334. MockDB: MockDB{
  1335. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1336. return &Authorization{ID: id, Status: StatusValid}, nil
  1337. },
  1338. },
  1339. },
  1340. err: NewError(ErrorServerInternalType, "expected Organization [wire.com], found []"),
  1341. }
  1342. },
  1343. "fail/wire-id-display-name-missing": func(t *testing.T) test {
  1344. now := clock.Now()
  1345. o := &Order{
  1346. ID: "oID",
  1347. AccountID: "accID",
  1348. Status: StatusReady,
  1349. ExpiresAt: now.Add(5 * time.Minute),
  1350. AuthorizationIDs: []string{"a", "b"},
  1351. Identifiers: []Identifier{
  1352. {Type: "wireapp-user", Value: "{\"name\": \"Alice Smith\", \"domain\": \"wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1353. },
  1354. }
  1355. signer := mustSigner("EC", "P-256", 0)
  1356. _, err := keyutil.Fingerprint(signer.Public())
  1357. if err != nil {
  1358. t.Fatal(err)
  1359. }
  1360. wireURL, _ := url.Parse("wireapp://%40alice_wire@wire.com")
  1361. csr := &x509.CertificateRequest{
  1362. Subject: pkix.Name{
  1363. Organization: []string{"wire.com"},
  1364. },
  1365. URIs: []*url.URL{
  1366. wireURL,
  1367. },
  1368. PublicKey: signer.Public(),
  1369. }
  1370. leaf := &x509.Certificate{
  1371. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1372. PublicKey: signer.Public(),
  1373. ExtraExtensions: []pkix.Extension{
  1374. {
  1375. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1376. Value: []byte("a-wireapp-user"),
  1377. },
  1378. },
  1379. }
  1380. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1381. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1382. return test{
  1383. o: o,
  1384. csr: csr,
  1385. prov: &MockProvisioner{
  1386. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1387. assert.Equals(t, token, "")
  1388. return nil, nil
  1389. },
  1390. MgetOptions: func() *provisioner.Options {
  1391. return nil
  1392. },
  1393. },
  1394. ca: &mockSignAuth{
  1395. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1396. assert.Equals(t, _csr, csr)
  1397. return []*x509.Certificate{leaf, inter, root}, nil
  1398. },
  1399. },
  1400. db: &MockWireDB{
  1401. MockDB: MockDB{
  1402. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1403. return &Authorization{ID: id, Status: StatusValid}, nil
  1404. },
  1405. },
  1406. },
  1407. err: NewError(ErrorServerInternalType, "CSR must contain the display name in '2.16.840.1.113730.3.1.241' OID"),
  1408. }
  1409. },
  1410. "fail/wire-id-display-name-mismatch": func(t *testing.T) test {
  1411. now := clock.Now()
  1412. o := &Order{
  1413. ID: "oID",
  1414. AccountID: "accID",
  1415. Status: StatusReady,
  1416. ExpiresAt: now.Add(5 * time.Minute),
  1417. AuthorizationIDs: []string{"a", "b"},
  1418. Identifiers: []Identifier{
  1419. {Type: "wireapp-user", Value: "{\"name\": \"Alice Smith\", \"domain\": \"wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1420. },
  1421. }
  1422. signer := mustSigner("EC", "P-256", 0)
  1423. _, err := keyutil.Fingerprint(signer.Public())
  1424. if err != nil {
  1425. t.Fatal(err)
  1426. }
  1427. wireURL, _ := url.Parse("wireapp://%40alice_wire@wire.com")
  1428. csr := &x509.CertificateRequest{
  1429. Subject: pkix.Name{
  1430. Names: []pkix.AttributeTypeAndValue{
  1431. {Type: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241}, Value: "Someone else"},
  1432. },
  1433. Organization: []string{"wire.com"},
  1434. },
  1435. URIs: []*url.URL{
  1436. wireURL,
  1437. },
  1438. PublicKey: signer.Public(),
  1439. }
  1440. leaf := &x509.Certificate{
  1441. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1442. PublicKey: signer.Public(),
  1443. ExtraExtensions: []pkix.Extension{
  1444. {
  1445. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1446. Value: []byte("a-wireapp-user"),
  1447. },
  1448. },
  1449. }
  1450. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1451. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1452. return test{
  1453. o: o,
  1454. csr: csr,
  1455. prov: &MockProvisioner{
  1456. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1457. assert.Equals(t, token, "")
  1458. return nil, nil
  1459. },
  1460. MgetOptions: func() *provisioner.Options {
  1461. return nil
  1462. },
  1463. },
  1464. ca: &mockSignAuth{
  1465. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1466. assert.Equals(t, _csr, csr)
  1467. return []*x509.Certificate{leaf, inter, root}, nil
  1468. },
  1469. },
  1470. db: &MockWireDB{
  1471. MockDB: MockDB{
  1472. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1473. return &Authorization{ID: id, Status: StatusValid}, nil
  1474. },
  1475. },
  1476. },
  1477. err: NewError(ErrorServerInternalType, "expected displayName Alice Smith, found Someone else"),
  1478. }
  1479. },
  1480. "ok/wire-id-user": func(t *testing.T) test {
  1481. now := clock.Now()
  1482. o := &Order{
  1483. ID: "oID",
  1484. AccountID: "accID",
  1485. Status: StatusReady,
  1486. ExpiresAt: now.Add(5 * time.Minute),
  1487. AuthorizationIDs: []string{"a", "b"},
  1488. Identifiers: []Identifier{
  1489. {Type: "wireapp-user", Value: "{\"name\": \"Alice Smith\", \"domain\": \"wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1490. },
  1491. }
  1492. signer := mustSigner("EC", "P-256", 0)
  1493. _, err := keyutil.Fingerprint(signer.Public())
  1494. if err != nil {
  1495. t.Fatal(err)
  1496. }
  1497. wireURL, _ := url.Parse("wireapp://%40alice_wire@wire.com")
  1498. csr := &x509.CertificateRequest{
  1499. Subject: pkix.Name{
  1500. Names: []pkix.AttributeTypeAndValue{
  1501. {Type: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241}, Value: "Alice Smith"},
  1502. },
  1503. Organization: []string{"wire.com"},
  1504. },
  1505. URIs: []*url.URL{
  1506. wireURL,
  1507. },
  1508. PublicKey: signer.Public(),
  1509. ExtraExtensions: []pkix.Extension{
  1510. {
  1511. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1512. Value: []byte("a-wireapp-user"),
  1513. },
  1514. },
  1515. }
  1516. leaf := &x509.Certificate{
  1517. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1518. PublicKey: signer.Public(),
  1519. ExtraExtensions: []pkix.Extension{
  1520. {
  1521. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1522. Value: []byte("a-wireapp-user"),
  1523. },
  1524. },
  1525. }
  1526. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1527. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1528. return test{
  1529. o: o,
  1530. csr: csr,
  1531. prov: &MockProvisioner{
  1532. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1533. assert.Equals(t, token, "")
  1534. return nil, nil
  1535. },
  1536. MgetOptions: func() *provisioner.Options {
  1537. return nil
  1538. },
  1539. },
  1540. ca: &mockSignAuth{
  1541. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1542. assert.Equals(t, _csr, csr)
  1543. return []*x509.Certificate{leaf, inter, root}, nil
  1544. },
  1545. },
  1546. db: &MockWireDB{
  1547. MockDB: MockDB{
  1548. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1549. return &Authorization{ID: id, Status: StatusValid}, nil
  1550. },
  1551. },
  1552. MockGetDpopToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1553. assert.Equals(t, orderID, o.ID)
  1554. dpopMap := map[string]interface{}{
  1555. "dpop": "a-dpop-token",
  1556. }
  1557. return dpopMap, nil
  1558. },
  1559. MockGetOidcToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1560. assert.Equals(t, orderID, o.ID)
  1561. oidcMap := map[string]interface{}{
  1562. "oidc": "a-oidc-token",
  1563. }
  1564. return oidcMap, nil
  1565. },
  1566. },
  1567. }
  1568. },
  1569. "ok/wire-id-device": func(t *testing.T) test {
  1570. now := clock.Now()
  1571. o := &Order{
  1572. ID: "oID",
  1573. AccountID: "accID",
  1574. Status: StatusReady,
  1575. ExpiresAt: now.Add(5 * time.Minute),
  1576. AuthorizationIDs: []string{"a", "b"},
  1577. Identifiers: []Identifier{
  1578. {Type: "wireapp-device", Value: "{\"name\": \"device\", \"domain\": \"wire.com\", \"client-id\": \"wireapp://CzbfFjDOQrenCbDxVmgnFw!594930e9d50bb175@wire.com\", \"handle\": \"wireapp://%40alice_wire@wire.com\"}"},
  1579. },
  1580. }
  1581. signer := mustSigner("EC", "P-256", 0)
  1582. _, err := keyutil.Fingerprint(signer.Public())
  1583. if err != nil {
  1584. t.Fatal(err)
  1585. }
  1586. wireURL, _ := url.Parse("wireapp://CzbfFjDOQrenCbDxVmgnFw!594930e9d50bb175@wire.com")
  1587. csr := &x509.CertificateRequest{
  1588. Subject: pkix.Name{
  1589. Names: []pkix.AttributeTypeAndValue{
  1590. {Type: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241}, Value: "device"},
  1591. },
  1592. Organization: []string{"wire.com"},
  1593. },
  1594. URIs: []*url.URL{
  1595. wireURL,
  1596. },
  1597. PublicKey: signer.Public(),
  1598. ExtraExtensions: []pkix.Extension{
  1599. {
  1600. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1601. Value: []byte("a-wireapp-user"),
  1602. },
  1603. },
  1604. }
  1605. leaf := &x509.Certificate{
  1606. Subject: pkix.Name{CommonName: "a-wireapp-user"},
  1607. PublicKey: signer.Public(),
  1608. ExtraExtensions: []pkix.Extension{
  1609. {
  1610. Id: asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 3, 1, 241},
  1611. Value: []byte("a-wireapp-user"),
  1612. },
  1613. },
  1614. }
  1615. inter := &x509.Certificate{Subject: pkix.Name{CommonName: "inter"}}
  1616. root := &x509.Certificate{Subject: pkix.Name{CommonName: "root"}}
  1617. return test{
  1618. o: o,
  1619. csr: csr,
  1620. prov: &MockProvisioner{
  1621. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1622. assert.Equals(t, token, "")
  1623. return nil, nil
  1624. },
  1625. MgetOptions: func() *provisioner.Options {
  1626. return nil
  1627. },
  1628. },
  1629. ca: &mockSignAuth{
  1630. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1631. assert.Equals(t, _csr, csr)
  1632. return []*x509.Certificate{leaf, inter, root}, nil
  1633. },
  1634. },
  1635. db: &MockWireDB{
  1636. MockDB: MockDB{
  1637. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1638. return &Authorization{ID: id, Status: StatusValid}, nil
  1639. },
  1640. },
  1641. MockGetDpopToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1642. assert.Equals(t, orderID, o.ID)
  1643. dpopMap := map[string]interface{}{
  1644. "dpop": "a-dpop-token",
  1645. }
  1646. return dpopMap, nil
  1647. },
  1648. MockGetOidcToken: func(ctx context.Context, orderID string) (map[string]interface{}, error) {
  1649. assert.Equals(t, orderID, o.ID)
  1650. oidcMap := map[string]interface{}{
  1651. "oidc": "a-oidc-token",
  1652. }
  1653. return oidcMap, nil
  1654. },
  1655. },
  1656. }
  1657. },
  1658. "ok/new-cert-dns": func(t *testing.T) test {
  1659. now := clock.Now()
  1660. o := &Order{
  1661. ID: "oID",
  1662. AccountID: "accID",
  1663. Status: StatusReady,
  1664. ExpiresAt: now.Add(5 * time.Minute),
  1665. AuthorizationIDs: []string{"a", "b"},
  1666. Identifiers: []Identifier{
  1667. {Type: "dns", Value: "foo.internal"},
  1668. {Type: "dns", Value: "bar.internal"},
  1669. },
  1670. }
  1671. csr := &x509.CertificateRequest{
  1672. Subject: pkix.Name{
  1673. CommonName: "foo.internal",
  1674. },
  1675. DNSNames: []string{"bar.internal"},
  1676. }
  1677. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  1678. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  1679. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  1680. return test{
  1681. o: o,
  1682. csr: csr,
  1683. prov: &MockProvisioner{
  1684. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1685. assert.Equals(t, token, "")
  1686. return nil, nil
  1687. },
  1688. MgetOptions: func() *provisioner.Options {
  1689. return nil
  1690. },
  1691. },
  1692. ca: &mockSignAuth{
  1693. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1694. assert.Equals(t, _csr, csr)
  1695. return []*x509.Certificate{foo, bar, baz}, nil
  1696. },
  1697. },
  1698. db: &MockDB{
  1699. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1700. return &Authorization{ID: id, Status: StatusValid}, nil
  1701. },
  1702. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  1703. cert.ID = "certID"
  1704. assert.Equals(t, cert.AccountID, o.AccountID)
  1705. assert.Equals(t, cert.OrderID, o.ID)
  1706. assert.Equals(t, cert.Leaf, foo)
  1707. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  1708. return nil
  1709. },
  1710. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  1711. assert.Equals(t, updo.CertificateID, "certID")
  1712. assert.Equals(t, updo.Status, StatusValid)
  1713. assert.Equals(t, updo.ID, o.ID)
  1714. assert.Equals(t, updo.AccountID, o.AccountID)
  1715. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  1716. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  1717. assert.Equals(t, updo.Identifiers, o.Identifiers)
  1718. return nil
  1719. },
  1720. },
  1721. }
  1722. },
  1723. "ok/new-cert-ip": func(t *testing.T) test {
  1724. now := clock.Now()
  1725. o := &Order{
  1726. ID: "oID",
  1727. AccountID: "accID",
  1728. Status: StatusReady,
  1729. ExpiresAt: now.Add(5 * time.Minute),
  1730. AuthorizationIDs: []string{"a", "b"},
  1731. Identifiers: []Identifier{
  1732. {Type: "ip", Value: "192.168.42.42"},
  1733. {Type: "ip", Value: "192.168.43.42"},
  1734. },
  1735. }
  1736. csr := &x509.CertificateRequest{
  1737. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")}, // in case of IPs, no Common Name
  1738. }
  1739. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  1740. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  1741. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  1742. return test{
  1743. o: o,
  1744. csr: csr,
  1745. prov: &MockProvisioner{
  1746. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1747. assert.Equals(t, token, "")
  1748. return nil, nil
  1749. },
  1750. MgetOptions: func() *provisioner.Options {
  1751. return nil
  1752. },
  1753. },
  1754. ca: &mockSignAuth{
  1755. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1756. assert.Equals(t, _csr, csr)
  1757. return []*x509.Certificate{foo, bar, baz}, nil
  1758. },
  1759. },
  1760. db: &MockDB{
  1761. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1762. return &Authorization{ID: id, Status: StatusValid}, nil
  1763. },
  1764. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  1765. cert.ID = "certID"
  1766. assert.Equals(t, cert.AccountID, o.AccountID)
  1767. assert.Equals(t, cert.OrderID, o.ID)
  1768. assert.Equals(t, cert.Leaf, foo)
  1769. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  1770. return nil
  1771. },
  1772. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  1773. assert.Equals(t, updo.CertificateID, "certID")
  1774. assert.Equals(t, updo.Status, StatusValid)
  1775. assert.Equals(t, updo.ID, o.ID)
  1776. assert.Equals(t, updo.AccountID, o.AccountID)
  1777. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  1778. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  1779. assert.Equals(t, updo.Identifiers, o.Identifiers)
  1780. return nil
  1781. },
  1782. },
  1783. }
  1784. },
  1785. "ok/new-cert-dns-and-ip": func(t *testing.T) test {
  1786. now := clock.Now()
  1787. o := &Order{
  1788. ID: "oID",
  1789. AccountID: "accID",
  1790. Status: StatusReady,
  1791. ExpiresAt: now.Add(5 * time.Minute),
  1792. AuthorizationIDs: []string{"a", "b"},
  1793. Identifiers: []Identifier{
  1794. {Type: "dns", Value: "foo.internal"},
  1795. {Type: "ip", Value: "192.168.42.42"},
  1796. },
  1797. }
  1798. csr := &x509.CertificateRequest{
  1799. Subject: pkix.Name{
  1800. CommonName: "foo.internal",
  1801. },
  1802. IPAddresses: []net.IP{net.ParseIP("192.168.42.42")},
  1803. }
  1804. foo := &x509.Certificate{Subject: pkix.Name{CommonName: "foo"}}
  1805. bar := &x509.Certificate{Subject: pkix.Name{CommonName: "bar"}}
  1806. baz := &x509.Certificate{Subject: pkix.Name{CommonName: "baz"}}
  1807. return test{
  1808. o: o,
  1809. csr: csr,
  1810. prov: &MockProvisioner{
  1811. MauthorizeSign: func(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  1812. assert.Equals(t, token, "")
  1813. return nil, nil
  1814. },
  1815. MgetOptions: func() *provisioner.Options {
  1816. return nil
  1817. },
  1818. },
  1819. ca: &mockSignAuth{
  1820. signWithContext: func(_ context.Context, _csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  1821. assert.Equals(t, _csr, csr)
  1822. return []*x509.Certificate{foo, bar, baz}, nil
  1823. },
  1824. },
  1825. db: &MockDB{
  1826. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  1827. return &Authorization{ID: id, Status: StatusValid}, nil
  1828. },
  1829. MockCreateCertificate: func(ctx context.Context, cert *Certificate) error {
  1830. cert.ID = "certID"
  1831. assert.Equals(t, cert.AccountID, o.AccountID)
  1832. assert.Equals(t, cert.OrderID, o.ID)
  1833. assert.Equals(t, cert.Leaf, foo)
  1834. assert.Equals(t, cert.Intermediates, []*x509.Certificate{bar, baz})
  1835. return nil
  1836. },
  1837. MockUpdateOrder: func(ctx context.Context, updo *Order) error {
  1838. assert.Equals(t, updo.CertificateID, "certID")
  1839. assert.Equals(t, updo.Status, StatusValid)
  1840. assert.Equals(t, updo.ID, o.ID)
  1841. assert.Equals(t, updo.AccountID, o.AccountID)
  1842. assert.Equals(t, updo.ExpiresAt, o.ExpiresAt)
  1843. assert.Equals(t, updo.AuthorizationIDs, o.AuthorizationIDs)
  1844. assert.Equals(t, updo.Identifiers, o.Identifiers)
  1845. return nil
  1846. },
  1847. },
  1848. }
  1849. },
  1850. }
  1851. for name, run := range tests {
  1852. t.Run(name, func(t *testing.T) {
  1853. tc := run(t)
  1854. if err := tc.o.Finalize(context.Background(), tc.db, tc.csr, tc.ca, tc.prov); err != nil {
  1855. if assert.NotNil(t, tc.err) {
  1856. var k *Error
  1857. if errors.As(err, &k) {
  1858. assert.Equals(t, k.Type, tc.err.Type)
  1859. assert.Equals(t, k.Detail, tc.err.Detail)
  1860. assert.Equals(t, k.Status, tc.err.Status)
  1861. assert.Equals(t, k.Err.Error(), tc.err.Err.Error())
  1862. assert.Equals(t, k.Detail, tc.err.Detail)
  1863. assert.Equals(t, k.Subproblems, tc.err.Subproblems)
  1864. } else {
  1865. assert.FatalError(t, errors.New("unexpected error type"))
  1866. }
  1867. }
  1868. } else {
  1869. assert.Nil(t, tc.err)
  1870. }
  1871. })
  1872. }
  1873. }
  1874. func Test_uniqueSortedIPs(t *testing.T) {
  1875. type args struct {
  1876. ips []net.IP
  1877. }
  1878. tests := []struct {
  1879. name string
  1880. args args
  1881. want []net.IP
  1882. }{
  1883. {
  1884. name: "ok/empty",
  1885. args: args{
  1886. ips: []net.IP{},
  1887. },
  1888. want: []net.IP{},
  1889. },
  1890. {
  1891. name: "ok/single-ipv4",
  1892. args: args{
  1893. ips: []net.IP{net.ParseIP("192.168.42.42")},
  1894. },
  1895. want: []net.IP{net.ParseIP("192.168.42.42")},
  1896. },
  1897. {
  1898. name: "ok/multiple-ipv4",
  1899. args: args{
  1900. ips: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.10"), net.ParseIP("192.168.42.1"), net.ParseIP("127.0.0.1")},
  1901. },
  1902. want: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("192.168.42.1"), net.ParseIP("192.168.42.10"), net.ParseIP("192.168.42.42")},
  1903. }, {
  1904. name: "ok/multiple-ipv4-with-varying-byte-representations",
  1905. args: args{
  1906. ips: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.10"), net.ParseIP("192.168.42.1"), []byte{0x7f, 0x0, 0x0, 0x1}},
  1907. },
  1908. want: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("192.168.42.1"), net.ParseIP("192.168.42.10"), net.ParseIP("192.168.42.42")},
  1909. },
  1910. {
  1911. name: "ok/unique-ipv4",
  1912. args: args{
  1913. ips: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.42")},
  1914. },
  1915. want: []net.IP{net.ParseIP("192.168.42.42")},
  1916. },
  1917. {
  1918. name: "ok/single-ipv6",
  1919. args: args{
  1920. ips: []net.IP{net.ParseIP("2001:db8::30")},
  1921. },
  1922. want: []net.IP{net.ParseIP("2001:db8::30")},
  1923. },
  1924. {
  1925. name: "ok/multiple-ipv6",
  1926. args: args{
  1927. ips: []net.IP{net.ParseIP("2001:db8::30"), net.ParseIP("2001:db8::20"), net.ParseIP("2001:db8::10")},
  1928. },
  1929. want: []net.IP{net.ParseIP("2001:db8::10"), net.ParseIP("2001:db8::20"), net.ParseIP("2001:db8::30")},
  1930. },
  1931. {
  1932. name: "ok/unique-ipv6",
  1933. args: args{
  1934. ips: []net.IP{net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::1")},
  1935. },
  1936. want: []net.IP{net.ParseIP("2001:db8::1")},
  1937. },
  1938. {
  1939. name: "ok/mixed-ipv4-and-ipv6",
  1940. args: args{
  1941. ips: []net.IP{net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::1"), net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.42")},
  1942. },
  1943. want: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("2001:db8::1")},
  1944. },
  1945. {
  1946. name: "ok/mixed-ipv4-and-ipv6-and-varying-byte-representations",
  1947. args: args{
  1948. ips: []net.IP{net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::1"), net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.42"), []byte{0x7f, 0x0, 0x0, 0x1}},
  1949. },
  1950. want: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("192.168.42.42"), net.ParseIP("2001:db8::1")},
  1951. },
  1952. {
  1953. name: "ok/mixed-ipv4-and-ipv6-and-more-varying-byte-representations",
  1954. args: args{
  1955. ips: []net.IP{net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::1"), net.ParseIP("192.168.42.42"), net.ParseIP("2001:db8::2"), net.ParseIP("192.168.42.42"), []byte{0x7f, 0x0, 0x0, 0x1}, []byte{0x7f, 0x0, 0x0, 0x1}, []byte{0x7f, 0x0, 0x0, 0x2}},
  1956. },
  1957. want: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("127.0.0.2"), net.ParseIP("192.168.42.42"), net.ParseIP("2001:db8::1"), net.ParseIP("2001:db8::2")},
  1958. },
  1959. }
  1960. for _, tt := range tests {
  1961. t.Run(tt.name, func(t *testing.T) {
  1962. got := uniqueSortedIPs(tt.args.ips)
  1963. if !cmp.Equal(tt.want, got) {
  1964. t.Errorf("uniqueSortedIPs() diff =\n%s", cmp.Diff(tt.want, got))
  1965. }
  1966. })
  1967. }
  1968. }
  1969. func Test_numberOfIdentifierType(t *testing.T) {
  1970. type args struct {
  1971. typ IdentifierType
  1972. ids []Identifier
  1973. }
  1974. tests := []struct {
  1975. name string
  1976. args args
  1977. want int
  1978. }{
  1979. {
  1980. name: "ok/no-identifiers",
  1981. args: args{
  1982. typ: DNS,
  1983. ids: []Identifier{},
  1984. },
  1985. want: 0,
  1986. },
  1987. {
  1988. name: "ok/no-dns",
  1989. args: args{
  1990. typ: DNS,
  1991. ids: []Identifier{
  1992. {
  1993. Type: IP,
  1994. Value: "192.168.42.42",
  1995. },
  1996. },
  1997. },
  1998. want: 0,
  1999. },
  2000. {
  2001. name: "ok/no-ips",
  2002. args: args{
  2003. typ: IP,
  2004. ids: []Identifier{
  2005. {
  2006. Type: DNS,
  2007. Value: "example.com",
  2008. },
  2009. },
  2010. },
  2011. want: 0,
  2012. },
  2013. {
  2014. name: "ok/one-dns",
  2015. args: args{
  2016. typ: DNS,
  2017. ids: []Identifier{
  2018. {
  2019. Type: DNS,
  2020. Value: "example.com",
  2021. },
  2022. {
  2023. Type: IP,
  2024. Value: "192.168.42.42",
  2025. },
  2026. },
  2027. },
  2028. want: 1,
  2029. },
  2030. {
  2031. name: "ok/one-ip",
  2032. args: args{
  2033. typ: IP,
  2034. ids: []Identifier{
  2035. {
  2036. Type: DNS,
  2037. Value: "example.com",
  2038. },
  2039. {
  2040. Type: IP,
  2041. Value: "192.168.42.42",
  2042. },
  2043. },
  2044. },
  2045. want: 1,
  2046. },
  2047. {
  2048. name: "ok/more-dns",
  2049. args: args{
  2050. typ: DNS,
  2051. ids: []Identifier{
  2052. {
  2053. Type: DNS,
  2054. Value: "example.com",
  2055. },
  2056. {
  2057. Type: DNS,
  2058. Value: "*.example.com",
  2059. },
  2060. {
  2061. Type: IP,
  2062. Value: "192.168.42.42",
  2063. },
  2064. },
  2065. },
  2066. want: 2,
  2067. },
  2068. {
  2069. name: "ok/more-ips",
  2070. args: args{
  2071. typ: IP,
  2072. ids: []Identifier{
  2073. {
  2074. Type: DNS,
  2075. Value: "example.com",
  2076. },
  2077. {
  2078. Type: IP,
  2079. Value: "192.168.42.42",
  2080. },
  2081. {
  2082. Type: IP,
  2083. Value: "192.168.42.43",
  2084. },
  2085. },
  2086. },
  2087. want: 2,
  2088. },
  2089. }
  2090. for _, tt := range tests {
  2091. t.Run(tt.name, func(t *testing.T) {
  2092. if got := numberOfIdentifierType(tt.args.typ, tt.args.ids); got != tt.want {
  2093. t.Errorf("numberOfIdentifierType() = %v, want %v", got, tt.want)
  2094. }
  2095. })
  2096. }
  2097. }
  2098. func Test_ipsAreEqual(t *testing.T) {
  2099. type args struct {
  2100. x net.IP
  2101. y net.IP
  2102. }
  2103. tests := []struct {
  2104. name string
  2105. args args
  2106. want bool
  2107. }{
  2108. {
  2109. name: "ok/ipv4",
  2110. args: args{
  2111. x: net.ParseIP("192.168.42.42"),
  2112. y: net.ParseIP("192.168.42.42"),
  2113. },
  2114. want: true,
  2115. },
  2116. {
  2117. name: "fail/ipv4",
  2118. args: args{
  2119. x: net.ParseIP("192.168.42.42"),
  2120. y: net.ParseIP("192.168.42.43"),
  2121. },
  2122. want: false,
  2123. },
  2124. {
  2125. name: "ok/ipv6",
  2126. args: args{
  2127. x: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  2128. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  2129. },
  2130. want: true,
  2131. },
  2132. {
  2133. name: "fail/ipv6",
  2134. args: args{
  2135. x: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  2136. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7335"),
  2137. },
  2138. want: false,
  2139. },
  2140. {
  2141. name: "fail/ipv4-and-ipv6",
  2142. args: args{
  2143. x: net.ParseIP("192.168.42.42"),
  2144. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  2145. },
  2146. want: false,
  2147. },
  2148. {
  2149. name: "ok/ipv4-mapped-to-ipv6",
  2150. args: args{
  2151. x: net.ParseIP("192.168.42.42"),
  2152. y: net.ParseIP("::ffff:192.168.42.42"), // parsed to the same IPv4 by Go
  2153. },
  2154. want: true, // we expect this to happen; a known issue in which ipv4 mapped ipv6 addresses are considered the same as their ipv4 counterpart
  2155. },
  2156. {
  2157. name: "fail/invalid-ipv4-and-valid-ipv6",
  2158. args: args{
  2159. x: net.ParseIP("192.168.42.1000"),
  2160. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  2161. },
  2162. want: false,
  2163. },
  2164. {
  2165. name: "fail/valid-ipv4-and-invalid-ipv6",
  2166. args: args{
  2167. x: net.ParseIP("192.168.42.42"),
  2168. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:733400"),
  2169. },
  2170. want: false,
  2171. },
  2172. {
  2173. name: "fail/invalid-ipv4-and-invalid-ipv6",
  2174. args: args{
  2175. x: net.ParseIP("192.168.42.1000"),
  2176. y: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:1000000"),
  2177. },
  2178. want: false,
  2179. },
  2180. }
  2181. for _, tt := range tests {
  2182. t.Run(tt.name, func(t *testing.T) {
  2183. if got := ipsAreEqual(tt.args.x, tt.args.y); got != tt.want {
  2184. t.Errorf("ipsAreEqual() = %v, want %v", got, tt.want)
  2185. }
  2186. })
  2187. }
  2188. }
  2189. func Test_canonicalize(t *testing.T) {
  2190. type args struct {
  2191. csr *x509.CertificateRequest
  2192. }
  2193. tests := []struct {
  2194. name string
  2195. args args
  2196. want *x509.CertificateRequest
  2197. }{
  2198. {
  2199. name: "ok/dns",
  2200. args: args{
  2201. csr: &x509.CertificateRequest{
  2202. DNSNames: []string{"www.example.com", "example.com"},
  2203. },
  2204. },
  2205. want: &x509.CertificateRequest{
  2206. DNSNames: []string{"example.com", "www.example.com"},
  2207. IPAddresses: []net.IP{},
  2208. },
  2209. },
  2210. {
  2211. name: "ok/common-name",
  2212. args: args{
  2213. csr: &x509.CertificateRequest{
  2214. Subject: pkix.Name{
  2215. CommonName: "example.com",
  2216. },
  2217. DNSNames: []string{"www.example.com"},
  2218. },
  2219. },
  2220. want: &x509.CertificateRequest{
  2221. Subject: pkix.Name{
  2222. CommonName: "example.com",
  2223. },
  2224. DNSNames: []string{"example.com", "www.example.com"},
  2225. IPAddresses: []net.IP{},
  2226. },
  2227. },
  2228. {
  2229. name: "ok/ipv4",
  2230. args: args{
  2231. csr: &x509.CertificateRequest{
  2232. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  2233. },
  2234. },
  2235. want: &x509.CertificateRequest{
  2236. DNSNames: []string{},
  2237. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")},
  2238. },
  2239. },
  2240. {
  2241. name: "ok/mixed",
  2242. args: args{
  2243. csr: &x509.CertificateRequest{
  2244. DNSNames: []string{"www.example.com", "example.com"},
  2245. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  2246. },
  2247. },
  2248. want: &x509.CertificateRequest{
  2249. DNSNames: []string{"example.com", "www.example.com"},
  2250. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")},
  2251. },
  2252. },
  2253. {
  2254. name: "ok/mixed-common-name",
  2255. args: args{
  2256. csr: &x509.CertificateRequest{
  2257. Subject: pkix.Name{
  2258. CommonName: "example.com",
  2259. },
  2260. DNSNames: []string{"www.example.com"},
  2261. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  2262. },
  2263. },
  2264. want: &x509.CertificateRequest{
  2265. Subject: pkix.Name{
  2266. CommonName: "example.com",
  2267. },
  2268. DNSNames: []string{"example.com", "www.example.com"},
  2269. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")},
  2270. },
  2271. },
  2272. {
  2273. name: "ok/ip-common-name",
  2274. args: args{
  2275. csr: &x509.CertificateRequest{
  2276. Subject: pkix.Name{
  2277. CommonName: "127.0.0.1",
  2278. },
  2279. DNSNames: []string{"example.com"},
  2280. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  2281. },
  2282. },
  2283. want: &x509.CertificateRequest{
  2284. Subject: pkix.Name{
  2285. CommonName: "127.0.0.1",
  2286. },
  2287. DNSNames: []string{"example.com"},
  2288. IPAddresses: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")},
  2289. },
  2290. },
  2291. }
  2292. for _, tt := range tests {
  2293. t.Run(tt.name, func(t *testing.T) {
  2294. got := canonicalize(tt.args.csr)
  2295. if !cmp.Equal(tt.want, got) {
  2296. t.Errorf("canonicalize() diff =\n%s", cmp.Diff(tt.want, got))
  2297. }
  2298. })
  2299. }
  2300. }
  2301. func TestOrder_sans(t *testing.T) {
  2302. type fields struct {
  2303. Identifiers []Identifier
  2304. }
  2305. tests := []struct {
  2306. name string
  2307. fields fields
  2308. csr *x509.CertificateRequest
  2309. want []x509util.SubjectAlternativeName
  2310. err *Error
  2311. }{
  2312. {
  2313. name: "ok/dns",
  2314. fields: fields{
  2315. Identifiers: []Identifier{
  2316. {Type: "dns", Value: "example.com"},
  2317. },
  2318. },
  2319. csr: &x509.CertificateRequest{
  2320. Subject: pkix.Name{
  2321. CommonName: "example.com",
  2322. },
  2323. },
  2324. want: []x509util.SubjectAlternativeName{
  2325. {Type: "dns", Value: "example.com"},
  2326. },
  2327. err: nil,
  2328. },
  2329. {
  2330. name: "fail/invalid-alternative-name-email",
  2331. fields: fields{
  2332. Identifiers: []Identifier{},
  2333. },
  2334. csr: &x509.CertificateRequest{
  2335. Subject: pkix.Name{
  2336. CommonName: "foo.internal",
  2337. },
  2338. EmailAddresses: []string{"test@example.com"},
  2339. },
  2340. want: []x509util.SubjectAlternativeName{},
  2341. err: NewError(ErrorBadCSRType, "Only DNS names and IP addresses are allowed"),
  2342. },
  2343. {
  2344. name: "fail/error-names-length-mismatch",
  2345. fields: fields{
  2346. Identifiers: []Identifier{
  2347. {Type: "dns", Value: "foo.internal"},
  2348. {Type: "dns", Value: "bar.internal"},
  2349. },
  2350. },
  2351. csr: &x509.CertificateRequest{
  2352. Subject: pkix.Name{
  2353. CommonName: "foo.internal",
  2354. },
  2355. },
  2356. want: []x509util.SubjectAlternativeName{},
  2357. err: NewError(ErrorBadCSRType, "CSR names do not match identifiers exactly: "+
  2358. "CSR names = %v, Order names = %v", []string{"foo.internal"}, []string{"bar.internal", "foo.internal"}),
  2359. },
  2360. {
  2361. name: "fail/error-names-mismatch",
  2362. fields: fields{
  2363. Identifiers: []Identifier{
  2364. {Type: "dns", Value: "foo.internal"},
  2365. {Type: "dns", Value: "bar.internal"},
  2366. },
  2367. },
  2368. csr: &x509.CertificateRequest{
  2369. Subject: pkix.Name{
  2370. CommonName: "foo.internal",
  2371. },
  2372. DNSNames: []string{"zap.internal"},
  2373. },
  2374. want: []x509util.SubjectAlternativeName{},
  2375. err: NewError(ErrorBadCSRType, "CSR names do not match identifiers exactly: "+
  2376. "CSR names = %v, Order names = %v", []string{"foo.internal", "zap.internal"}, []string{"bar.internal", "foo.internal"}),
  2377. },
  2378. {
  2379. name: "ok/ipv4",
  2380. fields: fields{
  2381. Identifiers: []Identifier{
  2382. {Type: "ip", Value: "192.168.43.42"},
  2383. {Type: "ip", Value: "192.168.42.42"},
  2384. },
  2385. },
  2386. csr: &x509.CertificateRequest{
  2387. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42")},
  2388. },
  2389. want: []x509util.SubjectAlternativeName{
  2390. {Type: "ip", Value: "192.168.42.42"},
  2391. {Type: "ip", Value: "192.168.43.42"},
  2392. },
  2393. err: nil,
  2394. },
  2395. {
  2396. name: "ok/ipv6",
  2397. fields: fields{
  2398. Identifiers: []Identifier{
  2399. {Type: "ip", Value: "2001:0db8:85a3::8a2e:0370:7335"},
  2400. {Type: "ip", Value: "2001:0db8:85a3::8a2e:0370:7334"},
  2401. },
  2402. },
  2403. csr: &x509.CertificateRequest{
  2404. IPAddresses: []net.IP{net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7335"), net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")},
  2405. },
  2406. want: []x509util.SubjectAlternativeName{
  2407. {Type: "ip", Value: "2001:db8:85a3::8a2e:370:7334"},
  2408. {Type: "ip", Value: "2001:db8:85a3::8a2e:370:7335"},
  2409. },
  2410. err: nil,
  2411. },
  2412. {
  2413. name: "fail/error-ips-length-mismatch",
  2414. fields: fields{
  2415. Identifiers: []Identifier{
  2416. {Type: "ip", Value: "192.168.42.42"},
  2417. {Type: "ip", Value: "192.168.43.42"},
  2418. },
  2419. },
  2420. csr: &x509.CertificateRequest{
  2421. IPAddresses: []net.IP{net.ParseIP("192.168.42.42")},
  2422. },
  2423. want: []x509util.SubjectAlternativeName{},
  2424. err: NewError(ErrorBadCSRType, "CSR IPs do not match identifiers exactly: "+
  2425. "CSR IPs = %v, Order IPs = %v", []net.IP{net.ParseIP("192.168.42.42")}, []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")}),
  2426. },
  2427. {
  2428. name: "fail/error-ips-mismatch",
  2429. fields: fields{
  2430. Identifiers: []Identifier{
  2431. {Type: "ip", Value: "192.168.42.42"},
  2432. {Type: "ip", Value: "192.168.43.42"},
  2433. },
  2434. },
  2435. csr: &x509.CertificateRequest{
  2436. IPAddresses: []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.42.32")},
  2437. },
  2438. want: []x509util.SubjectAlternativeName{},
  2439. err: NewError(ErrorBadCSRType, "CSR IPs do not match identifiers exactly: "+
  2440. "CSR IPs = %v, Order IPs = %v", []net.IP{net.ParseIP("192.168.42.32"), net.ParseIP("192.168.42.42")}, []net.IP{net.ParseIP("192.168.42.42"), net.ParseIP("192.168.43.42")}),
  2441. },
  2442. {
  2443. name: "ok/mixed",
  2444. fields: fields{
  2445. Identifiers: []Identifier{
  2446. {Type: "dns", Value: "foo.internal"},
  2447. {Type: "dns", Value: "bar.internal"},
  2448. {Type: "ip", Value: "192.168.43.42"},
  2449. {Type: "ip", Value: "192.168.42.42"},
  2450. {Type: "ip", Value: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"},
  2451. },
  2452. },
  2453. csr: &x509.CertificateRequest{
  2454. Subject: pkix.Name{
  2455. CommonName: "bar.internal",
  2456. },
  2457. DNSNames: []string{"foo.internal"},
  2458. IPAddresses: []net.IP{net.ParseIP("192.168.43.42"), net.ParseIP("192.168.42.42"), net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")},
  2459. },
  2460. want: []x509util.SubjectAlternativeName{
  2461. {Type: "dns", Value: "bar.internal"},
  2462. {Type: "dns", Value: "foo.internal"},
  2463. {Type: "ip", Value: "192.168.42.42"},
  2464. {Type: "ip", Value: "192.168.43.42"},
  2465. {Type: "ip", Value: "2001:db8:85a3::8a2e:370:7334"},
  2466. },
  2467. err: nil,
  2468. },
  2469. {
  2470. name: "fail/unsupported-identifier-type",
  2471. fields: fields{
  2472. Identifiers: []Identifier{
  2473. {Type: "ipv4", Value: "192.168.42.42"},
  2474. },
  2475. },
  2476. csr: &x509.CertificateRequest{
  2477. IPAddresses: []net.IP{net.ParseIP("192.168.42.42")},
  2478. },
  2479. want: []x509util.SubjectAlternativeName{},
  2480. err: NewError(ErrorServerInternalType, "unsupported identifier type in order: ipv4"),
  2481. },
  2482. }
  2483. for _, tt := range tests {
  2484. t.Run(tt.name, func(t *testing.T) {
  2485. o := &Order{
  2486. Identifiers: tt.fields.Identifiers,
  2487. }
  2488. canonicalizedCSR := canonicalize(tt.csr)
  2489. got, err := o.sans(canonicalizedCSR)
  2490. if tt.err != nil {
  2491. if err == nil {
  2492. t.Errorf("Order.sans() = %v, want error; got none", got)
  2493. return
  2494. }
  2495. var k *Error
  2496. if errors.As(err, &k) {
  2497. assert.Equals(t, k.Type, tt.err.Type)
  2498. assert.Equals(t, k.Detail, tt.err.Detail)
  2499. assert.Equals(t, k.Status, tt.err.Status)
  2500. assert.Equals(t, k.Err.Error(), tt.err.Err.Error())
  2501. assert.Equals(t, k.Detail, tt.err.Detail)
  2502. } else {
  2503. assert.FatalError(t, errors.New("unexpected error type"))
  2504. }
  2505. return
  2506. }
  2507. if !reflect.DeepEqual(got, tt.want) {
  2508. t.Errorf("Order.sans() = %v, want %v", got, tt.want)
  2509. }
  2510. })
  2511. }
  2512. }
  2513. func TestOrder_getAuthorizationFingerprint(t *testing.T) {
  2514. ctx := context.Background()
  2515. type fields struct {
  2516. AuthorizationIDs []string
  2517. }
  2518. type args struct {
  2519. ctx context.Context
  2520. db DB
  2521. }
  2522. tests := []struct {
  2523. name string
  2524. fields fields
  2525. args args
  2526. want string
  2527. wantErr bool
  2528. }{
  2529. {"ok", fields{[]string{"az1", "az2"}}, args{ctx, &MockDB{
  2530. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  2531. return &Authorization{ID: id, Status: StatusValid}, nil
  2532. },
  2533. }}, "", false},
  2534. {"ok fingerprint", fields{[]string{"az1", "az2"}}, args{ctx, &MockDB{
  2535. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  2536. if id == "az1" {
  2537. return &Authorization{ID: id, Status: StatusValid}, nil
  2538. }
  2539. return &Authorization{ID: id, Fingerprint: "fingerprint", Status: StatusValid}, nil
  2540. },
  2541. }}, "fingerprint", false},
  2542. {"fail", fields{[]string{"az1", "az2"}}, args{ctx, &MockDB{
  2543. MockGetAuthorization: func(ctx context.Context, id string) (*Authorization, error) {
  2544. return nil, errors.New("force")
  2545. },
  2546. }}, "", true},
  2547. }
  2548. for _, tt := range tests {
  2549. t.Run(tt.name, func(t *testing.T) {
  2550. o := &Order{
  2551. AuthorizationIDs: tt.fields.AuthorizationIDs,
  2552. }
  2553. got, err := o.getAuthorizationFingerprint(tt.args.ctx, tt.args.db)
  2554. if (err != nil) != tt.wantErr {
  2555. t.Errorf("Order.getAuthorizationFingerprint() error = %v, wantErr %v", err, tt.wantErr)
  2556. return
  2557. }
  2558. if got != tt.want {
  2559. t.Errorf("Order.getAuthorizationFingerprint() = %v, want %v", got, tt.want)
  2560. }
  2561. })
  2562. }
  2563. }