provisioners.go 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. package authority
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/x509"
  6. "encoding/json"
  7. "encoding/pem"
  8. "fmt"
  9. "os"
  10. "github.com/pkg/errors"
  11. "gopkg.in/square/go-jose.v2/jwt"
  12. "go.step.sm/cli-utils/step"
  13. "go.step.sm/cli-utils/ui"
  14. "go.step.sm/crypto/jose"
  15. "go.step.sm/linkedca"
  16. "github.com/smallstep/certificates/authority/admin"
  17. "github.com/smallstep/certificates/authority/config"
  18. "github.com/smallstep/certificates/authority/policy"
  19. "github.com/smallstep/certificates/authority/provisioner"
  20. "github.com/smallstep/certificates/db"
  21. "github.com/smallstep/certificates/errs"
  22. )
  23. // GetEncryptedKey returns the JWE key corresponding to the given kid argument.
  24. func (a *Authority) GetEncryptedKey(kid string) (string, error) {
  25. a.adminMutex.RLock()
  26. defer a.adminMutex.RUnlock()
  27. key, ok := a.provisioners.LoadEncryptedKey(kid)
  28. if !ok {
  29. return "", errs.NotFound("encrypted key with kid %s was not found", kid)
  30. }
  31. return key, nil
  32. }
  33. // GetProvisioners returns a map listing each provisioner and the JWK Key Set
  34. // with their public keys.
  35. func (a *Authority) GetProvisioners(cursor string, limit int) (provisioner.List, string, error) {
  36. a.adminMutex.RLock()
  37. defer a.adminMutex.RUnlock()
  38. provisioners, nextCursor := a.provisioners.Find(cursor, limit)
  39. return provisioners, nextCursor, nil
  40. }
  41. // LoadProvisionerByCertificate returns an interface to the provisioner that
  42. // provisioned the certificate.
  43. func (a *Authority) LoadProvisionerByCertificate(crt *x509.Certificate) (provisioner.Interface, error) {
  44. a.adminMutex.RLock()
  45. defer a.adminMutex.RUnlock()
  46. if p, err := a.unsafeLoadProvisionerFromDatabase(crt); err == nil {
  47. return p, nil
  48. }
  49. return a.unsafeLoadProvisionerFromExtension(crt)
  50. }
  51. func (a *Authority) unsafeLoadProvisionerFromExtension(crt *x509.Certificate) (provisioner.Interface, error) {
  52. p, ok := a.provisioners.LoadByCertificate(crt)
  53. if !ok || p.GetType() == 0 {
  54. return nil, admin.NewError(admin.ErrorNotFoundType, "unable to load provisioner from certificate")
  55. }
  56. return p, nil
  57. }
  58. func (a *Authority) unsafeLoadProvisionerFromDatabase(crt *x509.Certificate) (provisioner.Interface, error) {
  59. // certificateDataGetter is an interface that can be used to retrieve the
  60. // provisioner from a db or a linked ca.
  61. type certificateDataGetter interface {
  62. GetCertificateData(string) (*db.CertificateData, error)
  63. }
  64. var err error
  65. var data *db.CertificateData
  66. if cdg, ok := a.adminDB.(certificateDataGetter); ok {
  67. data, err = cdg.GetCertificateData(crt.SerialNumber.String())
  68. } else if cdg, ok := a.db.(certificateDataGetter); ok {
  69. data, err = cdg.GetCertificateData(crt.SerialNumber.String())
  70. }
  71. if err == nil && data != nil && data.Provisioner != nil {
  72. if p, ok := a.provisioners.Load(data.Provisioner.ID); ok {
  73. return p, nil
  74. }
  75. }
  76. return nil, admin.NewError(admin.ErrorNotFoundType, "unable to load provisioner from certificate")
  77. }
  78. // LoadProvisionerByToken returns an interface to the provisioner that
  79. // provisioned the token.
  80. func (a *Authority) LoadProvisionerByToken(token *jwt.JSONWebToken, claims *jwt.Claims) (provisioner.Interface, error) {
  81. a.adminMutex.RLock()
  82. defer a.adminMutex.RUnlock()
  83. p, ok := a.provisioners.LoadByToken(token, claims)
  84. if !ok {
  85. return nil, admin.NewError(admin.ErrorNotFoundType, "unable to load provisioner from token")
  86. }
  87. return p, nil
  88. }
  89. // LoadProvisionerByID returns an interface to the provisioner with the given ID.
  90. func (a *Authority) LoadProvisionerByID(id string) (provisioner.Interface, error) {
  91. a.adminMutex.RLock()
  92. defer a.adminMutex.RUnlock()
  93. p, ok := a.provisioners.Load(id)
  94. if !ok {
  95. return nil, admin.NewError(admin.ErrorNotFoundType, "provisioner %s not found", id)
  96. }
  97. return p, nil
  98. }
  99. // LoadProvisionerByName returns an interface to the provisioner with the given Name.
  100. func (a *Authority) LoadProvisionerByName(name string) (provisioner.Interface, error) {
  101. a.adminMutex.RLock()
  102. defer a.adminMutex.RUnlock()
  103. p, ok := a.provisioners.LoadByName(name)
  104. if !ok {
  105. return nil, admin.NewError(admin.ErrorNotFoundType, "provisioner %s not found", name)
  106. }
  107. return p, nil
  108. }
  109. func (a *Authority) generateProvisionerConfig(ctx context.Context) (provisioner.Config, error) {
  110. // Merge global and configuration claims
  111. claimer, err := provisioner.NewClaimer(a.config.AuthorityConfig.Claims, config.GlobalProvisionerClaims)
  112. if err != nil {
  113. return provisioner.Config{}, err
  114. }
  115. // TODO: should we also be combining the ssh federated roots here?
  116. // If we rotate ssh roots keys, sshpop provisioner will lose ability to
  117. // validate old SSH certificates, unless they are added as federated certs.
  118. sshKeys, err := a.GetSSHRoots(ctx)
  119. if err != nil {
  120. return provisioner.Config{}, err
  121. }
  122. return provisioner.Config{
  123. Claims: claimer.Claims(),
  124. Audiences: a.config.GetAudiences(),
  125. SSHKeys: &provisioner.SSHKeys{
  126. UserKeys: sshKeys.UserKeys,
  127. HostKeys: sshKeys.HostKeys,
  128. },
  129. GetIdentityFunc: a.getIdentityFunc,
  130. AuthorizeRenewFunc: a.authorizeRenewFunc,
  131. AuthorizeSSHRenewFunc: a.authorizeSSHRenewFunc,
  132. }, nil
  133. }
  134. // StoreProvisioner stores a provisioner to the authority.
  135. func (a *Authority) StoreProvisioner(ctx context.Context, prov *linkedca.Provisioner) error {
  136. a.adminMutex.Lock()
  137. defer a.adminMutex.Unlock()
  138. certProv, err := ProvisionerToCertificates(prov)
  139. if err != nil {
  140. return admin.WrapErrorISE(err,
  141. "error converting to certificates provisioner from linkedca provisioner")
  142. }
  143. if _, ok := a.provisioners.LoadByName(prov.GetName()); ok {
  144. return admin.NewError(admin.ErrorBadRequestType,
  145. "provisioner with name %s already exists", prov.GetName())
  146. }
  147. if _, ok := a.provisioners.LoadByTokenID(certProv.GetIDForToken()); ok {
  148. return admin.NewError(admin.ErrorBadRequestType,
  149. "provisioner with token ID %s already exists", certProv.GetIDForToken())
  150. }
  151. provisionerConfig, err := a.generateProvisionerConfig(ctx)
  152. if err != nil {
  153. return admin.WrapErrorISE(err, "error generating provisioner config")
  154. }
  155. if err := a.checkProvisionerPolicy(ctx, prov.Name, prov.Policy); err != nil {
  156. return err
  157. }
  158. if err := certProv.Init(provisionerConfig); err != nil {
  159. return admin.WrapError(admin.ErrorBadRequestType, err, "error validating configuration for provisioner %s", prov.Name)
  160. }
  161. // Store to database -- this will set the ID.
  162. if err := a.adminDB.CreateProvisioner(ctx, prov); err != nil {
  163. return admin.WrapErrorISE(err, "error creating provisioner")
  164. }
  165. // We need a new conversion that has the newly set ID.
  166. certProv, err = ProvisionerToCertificates(prov)
  167. if err != nil {
  168. return admin.WrapErrorISE(err,
  169. "error converting to certificates provisioner from linkedca provisioner")
  170. }
  171. if err := certProv.Init(provisionerConfig); err != nil {
  172. return admin.WrapErrorISE(err, "error initializing provisioner %s", prov.Name)
  173. }
  174. if err := a.provisioners.Store(certProv); err != nil {
  175. if err := a.ReloadAdminResources(ctx); err != nil {
  176. return admin.WrapErrorISE(err, "error reloading admin resources on failed provisioner store")
  177. }
  178. return admin.WrapErrorISE(err, "error storing provisioner in authority cache")
  179. }
  180. return nil
  181. }
  182. // UpdateProvisioner stores an provisioner.Interface to the authority.
  183. func (a *Authority) UpdateProvisioner(ctx context.Context, nu *linkedca.Provisioner) error {
  184. a.adminMutex.Lock()
  185. defer a.adminMutex.Unlock()
  186. certProv, err := ProvisionerToCertificates(nu)
  187. if err != nil {
  188. return admin.WrapErrorISE(err,
  189. "error converting to certificates provisioner from linkedca provisioner")
  190. }
  191. provisionerConfig, err := a.generateProvisionerConfig(ctx)
  192. if err != nil {
  193. return admin.WrapErrorISE(err, "error generating provisioner config")
  194. }
  195. if err := a.checkProvisionerPolicy(ctx, nu.Name, nu.Policy); err != nil {
  196. return err
  197. }
  198. if err := certProv.Init(provisionerConfig); err != nil {
  199. return admin.WrapErrorISE(err, "error initializing provisioner %s", nu.Name)
  200. }
  201. if err := a.provisioners.Update(certProv); err != nil {
  202. return admin.WrapErrorISE(err, "error updating provisioner '%s' in authority cache", nu.Name)
  203. }
  204. if err := a.adminDB.UpdateProvisioner(ctx, nu); err != nil {
  205. if err := a.ReloadAdminResources(ctx); err != nil {
  206. return admin.WrapErrorISE(err, "error reloading admin resources on failed provisioner update")
  207. }
  208. return admin.WrapErrorISE(err, "error updating provisioner '%s'", nu.Name)
  209. }
  210. return nil
  211. }
  212. // RemoveProvisioner removes an provisioner.Interface from the authority.
  213. func (a *Authority) RemoveProvisioner(ctx context.Context, id string) error {
  214. a.adminMutex.Lock()
  215. defer a.adminMutex.Unlock()
  216. p, ok := a.provisioners.Load(id)
  217. if !ok {
  218. return admin.NewError(admin.ErrorBadRequestType,
  219. "provisioner %s not found", id)
  220. }
  221. provName, provID := p.GetName(), p.GetID()
  222. if a.IsAdminAPIEnabled() {
  223. // Validate
  224. // - Check that there will be SUPER_ADMINs that remain after we
  225. // remove this provisioner.
  226. if a.IsAdminAPIEnabled() && a.admins.SuperCount() == a.admins.SuperCountByProvisioner(provName) {
  227. return admin.NewError(admin.ErrorBadRequestType,
  228. "cannot remove provisioner %s because no super admins will remain", provName)
  229. }
  230. // Delete all admins associated with the provisioner.
  231. admins, ok := a.admins.LoadByProvisioner(provName)
  232. if ok {
  233. for _, adm := range admins {
  234. if err := a.removeAdmin(ctx, adm.Id); err != nil {
  235. return admin.WrapErrorISE(err, "error deleting admin %s, as part of provisioner %s deletion", adm.Subject, provName)
  236. }
  237. }
  238. }
  239. }
  240. // Remove provisioner from authority caches.
  241. if err := a.provisioners.Remove(provID); err != nil {
  242. return admin.WrapErrorISE(err, "error removing provisioner from authority cache")
  243. }
  244. // Remove provisioner from database.
  245. if err := a.adminDB.DeleteProvisioner(ctx, provID); err != nil {
  246. if err := a.ReloadAdminResources(ctx); err != nil {
  247. return admin.WrapErrorISE(err, "error reloading admin resources on failed provisioner remove")
  248. }
  249. return admin.WrapErrorISE(err, "error deleting provisioner %s", provName)
  250. }
  251. return nil
  252. }
  253. // CreateFirstProvisioner creates and stores the first provisioner when using
  254. // admin database provisioner storage.
  255. func CreateFirstProvisioner(ctx context.Context, adminDB admin.DB, password string) (*linkedca.Provisioner, error) {
  256. if password == "" {
  257. pass, err := ui.PromptPasswordGenerate("Please enter the password to encrypt your first provisioner, leave empty and we'll generate one")
  258. if err != nil {
  259. return nil, err
  260. }
  261. password = string(pass)
  262. }
  263. jwk, jwe, err := jose.GenerateDefaultKeyPair([]byte(password))
  264. if err != nil {
  265. return nil, admin.WrapErrorISE(err, "error generating JWK key pair")
  266. }
  267. jwkPubBytes, err := jwk.MarshalJSON()
  268. if err != nil {
  269. return nil, admin.WrapErrorISE(err, "error marshaling JWK")
  270. }
  271. jwePrivStr, err := jwe.CompactSerialize()
  272. if err != nil {
  273. return nil, admin.WrapErrorISE(err, "error serializing JWE")
  274. }
  275. p := &linkedca.Provisioner{
  276. Name: "Admin JWK",
  277. Type: linkedca.Provisioner_JWK,
  278. Details: &linkedca.ProvisionerDetails{
  279. Data: &linkedca.ProvisionerDetails_JWK{
  280. JWK: &linkedca.JWKProvisioner{
  281. PublicKey: jwkPubBytes,
  282. EncryptedPrivateKey: []byte(jwePrivStr),
  283. },
  284. },
  285. },
  286. Claims: &linkedca.Claims{
  287. X509: &linkedca.X509Claims{
  288. Enabled: true,
  289. Durations: &linkedca.Durations{
  290. Default: "5m",
  291. },
  292. },
  293. },
  294. }
  295. if err := adminDB.CreateProvisioner(ctx, p); err != nil {
  296. return nil, admin.WrapErrorISE(err, "error creating provisioner")
  297. }
  298. return p, nil
  299. }
  300. // ValidateClaims validates the Claims type.
  301. func ValidateClaims(c *linkedca.Claims) error {
  302. if c == nil {
  303. return nil
  304. }
  305. if c.X509 != nil {
  306. if c.X509.Durations != nil {
  307. if err := ValidateDurations(c.X509.Durations); err != nil {
  308. return err
  309. }
  310. }
  311. }
  312. if c.Ssh != nil {
  313. if c.Ssh.UserDurations != nil {
  314. if err := ValidateDurations(c.Ssh.UserDurations); err != nil {
  315. return err
  316. }
  317. }
  318. if c.Ssh.HostDurations != nil {
  319. if err := ValidateDurations(c.Ssh.HostDurations); err != nil {
  320. return err
  321. }
  322. }
  323. }
  324. return nil
  325. }
  326. // ValidateDurations validates the Durations type.
  327. func ValidateDurations(d *linkedca.Durations) error {
  328. var (
  329. err error
  330. min, max, def *provisioner.Duration
  331. )
  332. if d.Min != "" {
  333. min, err = provisioner.NewDuration(d.Min)
  334. if err != nil {
  335. return admin.WrapError(admin.ErrorBadRequestType, err, "min duration '%s' is invalid", d.Min)
  336. }
  337. if min.Value() < 0 {
  338. return admin.WrapError(admin.ErrorBadRequestType, err, "min duration '%s' cannot be less than 0", d.Min)
  339. }
  340. }
  341. if d.Max != "" {
  342. max, err = provisioner.NewDuration(d.Max)
  343. if err != nil {
  344. return admin.WrapError(admin.ErrorBadRequestType, err, "max duration '%s' is invalid", d.Max)
  345. }
  346. if max.Value() < 0 {
  347. return admin.WrapError(admin.ErrorBadRequestType, err, "max duration '%s' cannot be less than 0", d.Max)
  348. }
  349. }
  350. if d.Default != "" {
  351. def, err = provisioner.NewDuration(d.Default)
  352. if err != nil {
  353. return admin.WrapError(admin.ErrorBadRequestType, err, "default duration '%s' is invalid", d.Default)
  354. }
  355. if def.Value() < 0 {
  356. return admin.WrapError(admin.ErrorBadRequestType, err, "default duration '%s' cannot be less than 0", d.Default)
  357. }
  358. }
  359. if d.Min != "" && d.Max != "" && min.Value() > max.Value() {
  360. return admin.NewError(admin.ErrorBadRequestType,
  361. "min duration '%s' cannot be greater than max duration '%s'", d.Min, d.Max)
  362. }
  363. if d.Min != "" && d.Default != "" && min.Value() > def.Value() {
  364. return admin.NewError(admin.ErrorBadRequestType,
  365. "min duration '%s' cannot be greater than default duration '%s'", d.Min, d.Default)
  366. }
  367. if d.Default != "" && d.Max != "" && min.Value() > def.Value() {
  368. return admin.NewError(admin.ErrorBadRequestType,
  369. "default duration '%s' cannot be greater than max duration '%s'", d.Default, d.Max)
  370. }
  371. return nil
  372. }
  373. func provisionerListToCertificates(l []*linkedca.Provisioner) (provisioner.List, error) {
  374. var nu provisioner.List
  375. for _, p := range l {
  376. certProv, err := ProvisionerToCertificates(p)
  377. if err != nil {
  378. return nil, err
  379. }
  380. nu = append(nu, certProv)
  381. }
  382. return nu, nil
  383. }
  384. func optionsToCertificates(p *linkedca.Provisioner) *provisioner.Options {
  385. ops := &provisioner.Options{
  386. X509: &provisioner.X509Options{},
  387. SSH: &provisioner.SSHOptions{},
  388. }
  389. if p.X509Template != nil {
  390. ops.X509.Template = string(p.X509Template.Template)
  391. ops.X509.TemplateData = p.X509Template.Data
  392. }
  393. if p.SshTemplate != nil {
  394. ops.SSH.Template = string(p.SshTemplate.Template)
  395. ops.SSH.TemplateData = p.SshTemplate.Data
  396. }
  397. if pol := p.GetPolicy(); pol != nil {
  398. if x := pol.GetX509(); x != nil {
  399. if allow := x.GetAllow(); allow != nil {
  400. ops.X509.AllowedNames = &policy.X509NameOptions{
  401. DNSDomains: allow.Dns,
  402. IPRanges: allow.Ips,
  403. EmailAddresses: allow.Emails,
  404. URIDomains: allow.Uris,
  405. }
  406. }
  407. if deny := x.GetDeny(); deny != nil {
  408. ops.X509.DeniedNames = &policy.X509NameOptions{
  409. DNSDomains: deny.Dns,
  410. IPRanges: deny.Ips,
  411. EmailAddresses: deny.Emails,
  412. URIDomains: deny.Uris,
  413. }
  414. }
  415. }
  416. if ssh := pol.GetSsh(); ssh != nil {
  417. if host := ssh.GetHost(); host != nil {
  418. ops.SSH.Host = &policy.SSHHostCertificateOptions{}
  419. if allow := host.GetAllow(); allow != nil {
  420. ops.SSH.Host.AllowedNames = &policy.SSHNameOptions{
  421. DNSDomains: allow.Dns,
  422. IPRanges: allow.Ips,
  423. Principals: allow.Principals,
  424. }
  425. }
  426. if deny := host.GetDeny(); deny != nil {
  427. ops.SSH.Host.DeniedNames = &policy.SSHNameOptions{
  428. DNSDomains: deny.Dns,
  429. IPRanges: deny.Ips,
  430. Principals: deny.Principals,
  431. }
  432. }
  433. }
  434. if user := ssh.GetUser(); user != nil {
  435. ops.SSH.User = &policy.SSHUserCertificateOptions{}
  436. if allow := user.GetAllow(); allow != nil {
  437. ops.SSH.User.AllowedNames = &policy.SSHNameOptions{
  438. EmailAddresses: allow.Emails,
  439. Principals: allow.Principals,
  440. }
  441. }
  442. if deny := user.GetDeny(); deny != nil {
  443. ops.SSH.User.DeniedNames = &policy.SSHNameOptions{
  444. EmailAddresses: deny.Emails,
  445. Principals: deny.Principals,
  446. }
  447. }
  448. }
  449. }
  450. }
  451. return ops
  452. }
  453. func durationsToCertificates(d *linkedca.Durations) (min, max, def *provisioner.Duration, err error) {
  454. if len(d.Min) > 0 {
  455. min, err = provisioner.NewDuration(d.Min)
  456. if err != nil {
  457. return nil, nil, nil, admin.WrapErrorISE(err, "error parsing minimum duration '%s'", d.Min)
  458. }
  459. }
  460. if len(d.Max) > 0 {
  461. max, err = provisioner.NewDuration(d.Max)
  462. if err != nil {
  463. return nil, nil, nil, admin.WrapErrorISE(err, "error parsing maximum duration '%s'", d.Max)
  464. }
  465. }
  466. if len(d.Default) > 0 {
  467. def, err = provisioner.NewDuration(d.Default)
  468. if err != nil {
  469. return nil, nil, nil, admin.WrapErrorISE(err, "error parsing default duration '%s'", d.Default)
  470. }
  471. }
  472. return
  473. }
  474. func durationsToLinkedca(d *provisioner.Duration) string {
  475. if d == nil {
  476. return ""
  477. }
  478. return d.Duration.String()
  479. }
  480. // claimsToCertificates converts the linkedca provisioner claims type to the
  481. // certifictes claims type.
  482. func claimsToCertificates(c *linkedca.Claims) (*provisioner.Claims, error) {
  483. if c == nil {
  484. //nolint:nilnil // nil claims do not pose an issue.
  485. return nil, nil
  486. }
  487. pc := &provisioner.Claims{
  488. DisableRenewal: &c.DisableRenewal,
  489. AllowRenewalAfterExpiry: &c.AllowRenewalAfterExpiry,
  490. }
  491. var err error
  492. if xc := c.X509; xc != nil {
  493. if d := xc.Durations; d != nil {
  494. pc.MinTLSDur, pc.MaxTLSDur, pc.DefaultTLSDur, err = durationsToCertificates(d)
  495. if err != nil {
  496. return nil, err
  497. }
  498. }
  499. }
  500. if sc := c.Ssh; sc != nil {
  501. pc.EnableSSHCA = &sc.Enabled
  502. if d := sc.UserDurations; d != nil {
  503. pc.MinUserSSHDur, pc.MaxUserSSHDur, pc.DefaultUserSSHDur, err = durationsToCertificates(d)
  504. if err != nil {
  505. return nil, err
  506. }
  507. }
  508. if d := sc.HostDurations; d != nil {
  509. pc.MinHostSSHDur, pc.MaxHostSSHDur, pc.DefaultHostSSHDur, err = durationsToCertificates(d)
  510. if err != nil {
  511. return nil, err
  512. }
  513. }
  514. }
  515. return pc, nil
  516. }
  517. func claimsToLinkedca(c *provisioner.Claims) *linkedca.Claims {
  518. if c == nil {
  519. return nil
  520. }
  521. disableRenewal := config.DefaultDisableRenewal
  522. allowRenewalAfterExpiry := config.DefaultAllowRenewalAfterExpiry
  523. if c.DisableRenewal != nil {
  524. disableRenewal = *c.DisableRenewal
  525. }
  526. if c.AllowRenewalAfterExpiry != nil {
  527. allowRenewalAfterExpiry = *c.AllowRenewalAfterExpiry
  528. }
  529. lc := &linkedca.Claims{
  530. DisableRenewal: disableRenewal,
  531. AllowRenewalAfterExpiry: allowRenewalAfterExpiry,
  532. }
  533. if c.DefaultTLSDur != nil || c.MinTLSDur != nil || c.MaxTLSDur != nil {
  534. lc.X509 = &linkedca.X509Claims{
  535. Enabled: true,
  536. Durations: &linkedca.Durations{
  537. Default: durationsToLinkedca(c.DefaultTLSDur),
  538. Min: durationsToLinkedca(c.MinTLSDur),
  539. Max: durationsToLinkedca(c.MaxTLSDur),
  540. },
  541. }
  542. }
  543. if c.EnableSSHCA != nil && *c.EnableSSHCA {
  544. lc.Ssh = &linkedca.SSHClaims{
  545. Enabled: true,
  546. }
  547. if c.DefaultUserSSHDur != nil || c.MinUserSSHDur != nil || c.MaxUserSSHDur != nil {
  548. lc.Ssh.UserDurations = &linkedca.Durations{
  549. Default: durationsToLinkedca(c.DefaultUserSSHDur),
  550. Min: durationsToLinkedca(c.MinUserSSHDur),
  551. Max: durationsToLinkedca(c.MaxUserSSHDur),
  552. }
  553. }
  554. if c.DefaultHostSSHDur != nil || c.MinHostSSHDur != nil || c.MaxHostSSHDur != nil {
  555. lc.Ssh.HostDurations = &linkedca.Durations{
  556. Default: durationsToLinkedca(c.DefaultHostSSHDur),
  557. Min: durationsToLinkedca(c.MinHostSSHDur),
  558. Max: durationsToLinkedca(c.MaxHostSSHDur),
  559. }
  560. }
  561. }
  562. return lc
  563. }
  564. func provisionerOptionsToLinkedca(p *provisioner.Options) (*linkedca.Template, *linkedca.Template, error) {
  565. var err error
  566. var x509Template, sshTemplate *linkedca.Template
  567. if p == nil {
  568. return nil, nil, nil
  569. }
  570. if p.X509 != nil && p.X509.HasTemplate() {
  571. x509Template = &linkedca.Template{
  572. Template: nil,
  573. Data: nil,
  574. }
  575. if p.X509.Template != "" {
  576. x509Template.Template = []byte(p.SSH.Template)
  577. } else if p.X509.TemplateFile != "" {
  578. filename := step.Abs(p.X509.TemplateFile)
  579. if x509Template.Template, err = os.ReadFile(filename); err != nil {
  580. return nil, nil, errors.Wrap(err, "error reading x509 template")
  581. }
  582. }
  583. }
  584. if p.SSH != nil && p.SSH.HasTemplate() {
  585. sshTemplate = &linkedca.Template{
  586. Template: nil,
  587. Data: nil,
  588. }
  589. if p.SSH.Template != "" {
  590. sshTemplate.Template = []byte(p.SSH.Template)
  591. } else if p.SSH.TemplateFile != "" {
  592. filename := step.Abs(p.SSH.TemplateFile)
  593. if sshTemplate.Template, err = os.ReadFile(filename); err != nil {
  594. return nil, nil, errors.Wrap(err, "error reading ssh template")
  595. }
  596. }
  597. }
  598. return x509Template, sshTemplate, nil
  599. }
  600. func provisionerPEMToLinkedca(b []byte) [][]byte {
  601. var roots [][]byte
  602. var block *pem.Block
  603. for {
  604. if block, b = pem.Decode(b); block == nil {
  605. break
  606. }
  607. roots = append(roots, pem.EncodeToMemory(block))
  608. }
  609. return roots
  610. }
  611. // ProvisionerToCertificates converts the linkedca provisioner type to the certificates provisioner
  612. // interface.
  613. func ProvisionerToCertificates(p *linkedca.Provisioner) (provisioner.Interface, error) {
  614. claims, err := claimsToCertificates(p.Claims)
  615. if err != nil {
  616. return nil, err
  617. }
  618. details := p.Details.GetData()
  619. if details == nil {
  620. return nil, errors.New("provisioner does not have any details")
  621. }
  622. options := optionsToCertificates(p)
  623. switch d := details.(type) {
  624. case *linkedca.ProvisionerDetails_JWK:
  625. jwk := new(jose.JSONWebKey)
  626. if err := json.Unmarshal(d.JWK.PublicKey, &jwk); err != nil {
  627. return nil, errors.Wrap(err, "error unmarshaling public key")
  628. }
  629. return &provisioner.JWK{
  630. ID: p.Id,
  631. Type: p.Type.String(),
  632. Name: p.Name,
  633. Key: jwk,
  634. EncryptedKey: string(d.JWK.EncryptedPrivateKey),
  635. Claims: claims,
  636. Options: options,
  637. }, nil
  638. case *linkedca.ProvisionerDetails_X5C:
  639. var roots []byte
  640. for i, root := range d.X5C.GetRoots() {
  641. if i > 0 {
  642. roots = append(roots, '\n')
  643. }
  644. roots = append(roots, root...)
  645. }
  646. return &provisioner.X5C{
  647. ID: p.Id,
  648. Type: p.Type.String(),
  649. Name: p.Name,
  650. Roots: roots,
  651. Claims: claims,
  652. Options: options,
  653. }, nil
  654. case *linkedca.ProvisionerDetails_K8SSA:
  655. var publicKeys []byte
  656. for i, k := range d.K8SSA.GetPublicKeys() {
  657. if i > 0 {
  658. publicKeys = append(publicKeys, '\n')
  659. }
  660. publicKeys = append(publicKeys, k...)
  661. }
  662. return &provisioner.K8sSA{
  663. ID: p.Id,
  664. Type: p.Type.String(),
  665. Name: p.Name,
  666. PubKeys: publicKeys,
  667. Claims: claims,
  668. Options: options,
  669. }, nil
  670. case *linkedca.ProvisionerDetails_SSHPOP:
  671. return &provisioner.SSHPOP{
  672. ID: p.Id,
  673. Type: p.Type.String(),
  674. Name: p.Name,
  675. Claims: claims,
  676. }, nil
  677. case *linkedca.ProvisionerDetails_ACME:
  678. cfg := d.ACME
  679. return &provisioner.ACME{
  680. ID: p.Id,
  681. Type: p.Type.String(),
  682. Name: p.Name,
  683. ForceCN: cfg.ForceCn,
  684. RequireEAB: cfg.RequireEab,
  685. Challenges: challengesToCertificates(cfg.Challenges),
  686. AttestationFormats: attestationFormatsToCertificates(cfg.AttestationFormats),
  687. Claims: claims,
  688. Options: options,
  689. }, nil
  690. case *linkedca.ProvisionerDetails_OIDC:
  691. cfg := d.OIDC
  692. return &provisioner.OIDC{
  693. ID: p.Id,
  694. Type: p.Type.String(),
  695. Name: p.Name,
  696. TenantID: cfg.TenantId,
  697. ClientID: cfg.ClientId,
  698. ClientSecret: cfg.ClientSecret,
  699. ConfigurationEndpoint: cfg.ConfigurationEndpoint,
  700. Admins: cfg.Admins,
  701. Domains: cfg.Domains,
  702. Groups: cfg.Groups,
  703. ListenAddress: cfg.ListenAddress,
  704. Claims: claims,
  705. Options: options,
  706. }, nil
  707. case *linkedca.ProvisionerDetails_AWS:
  708. cfg := d.AWS
  709. instanceAge, err := parseInstanceAge(cfg.InstanceAge)
  710. if err != nil {
  711. return nil, err
  712. }
  713. return &provisioner.AWS{
  714. ID: p.Id,
  715. Type: p.Type.String(),
  716. Name: p.Name,
  717. Accounts: cfg.Accounts,
  718. DisableCustomSANs: cfg.DisableCustomSans,
  719. DisableTrustOnFirstUse: cfg.DisableTrustOnFirstUse,
  720. InstanceAge: instanceAge,
  721. Claims: claims,
  722. Options: options,
  723. }, nil
  724. case *linkedca.ProvisionerDetails_GCP:
  725. cfg := d.GCP
  726. instanceAge, err := parseInstanceAge(cfg.InstanceAge)
  727. if err != nil {
  728. return nil, err
  729. }
  730. return &provisioner.GCP{
  731. ID: p.Id,
  732. Type: p.Type.String(),
  733. Name: p.Name,
  734. ServiceAccounts: cfg.ServiceAccounts,
  735. ProjectIDs: cfg.ProjectIds,
  736. DisableCustomSANs: cfg.DisableCustomSans,
  737. DisableTrustOnFirstUse: cfg.DisableTrustOnFirstUse,
  738. InstanceAge: instanceAge,
  739. Claims: claims,
  740. Options: options,
  741. }, nil
  742. case *linkedca.ProvisionerDetails_Azure:
  743. cfg := d.Azure
  744. return &provisioner.Azure{
  745. ID: p.Id,
  746. Type: p.Type.String(),
  747. Name: p.Name,
  748. TenantID: cfg.TenantId,
  749. ResourceGroups: cfg.ResourceGroups,
  750. SubscriptionIDs: cfg.SubscriptionIds,
  751. ObjectIDs: cfg.ObjectIds,
  752. Audience: cfg.Audience,
  753. DisableCustomSANs: cfg.DisableCustomSans,
  754. DisableTrustOnFirstUse: cfg.DisableTrustOnFirstUse,
  755. Claims: claims,
  756. Options: options,
  757. }, nil
  758. case *linkedca.ProvisionerDetails_SCEP:
  759. cfg := d.SCEP
  760. return &provisioner.SCEP{
  761. ID: p.Id,
  762. Type: p.Type.String(),
  763. Name: p.Name,
  764. ForceCN: cfg.ForceCn,
  765. ChallengePassword: cfg.Challenge,
  766. Capabilities: cfg.Capabilities,
  767. IncludeRoot: cfg.IncludeRoot,
  768. MinimumPublicKeyLength: int(cfg.MinimumPublicKeyLength),
  769. EncryptionAlgorithmIdentifier: int(cfg.EncryptionAlgorithmIdentifier),
  770. Claims: claims,
  771. Options: options,
  772. }, nil
  773. case *linkedca.ProvisionerDetails_Nebula:
  774. var roots []byte
  775. for i, root := range d.Nebula.GetRoots() {
  776. if i > 0 && !bytes.HasSuffix(root, []byte{'\n'}) {
  777. roots = append(roots, '\n')
  778. }
  779. roots = append(roots, root...)
  780. }
  781. return &provisioner.Nebula{
  782. ID: p.Id,
  783. Type: p.Type.String(),
  784. Name: p.Name,
  785. Roots: roots,
  786. Claims: claims,
  787. Options: options,
  788. }, nil
  789. default:
  790. return nil, fmt.Errorf("provisioner %s not implemented", p.Type)
  791. }
  792. }
  793. // ProvisionerToLinkedca converts a provisioner.Interface to a
  794. // linkedca.Provisioner type.
  795. func ProvisionerToLinkedca(p provisioner.Interface) (*linkedca.Provisioner, error) {
  796. switch p := p.(type) {
  797. case *provisioner.JWK:
  798. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  799. if err != nil {
  800. return nil, err
  801. }
  802. publicKey, err := json.Marshal(p.Key)
  803. if err != nil {
  804. return nil, errors.Wrap(err, "error marshaling key")
  805. }
  806. return &linkedca.Provisioner{
  807. Id: p.ID,
  808. Type: linkedca.Provisioner_JWK,
  809. Name: p.GetName(),
  810. Details: &linkedca.ProvisionerDetails{
  811. Data: &linkedca.ProvisionerDetails_JWK{
  812. JWK: &linkedca.JWKProvisioner{
  813. PublicKey: publicKey,
  814. EncryptedPrivateKey: []byte(p.EncryptedKey),
  815. },
  816. },
  817. },
  818. Claims: claimsToLinkedca(p.Claims),
  819. X509Template: x509Template,
  820. SshTemplate: sshTemplate,
  821. }, nil
  822. case *provisioner.OIDC:
  823. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  824. if err != nil {
  825. return nil, err
  826. }
  827. return &linkedca.Provisioner{
  828. Id: p.ID,
  829. Type: linkedca.Provisioner_OIDC,
  830. Name: p.GetName(),
  831. Details: &linkedca.ProvisionerDetails{
  832. Data: &linkedca.ProvisionerDetails_OIDC{
  833. OIDC: &linkedca.OIDCProvisioner{
  834. ClientId: p.ClientID,
  835. ClientSecret: p.ClientSecret,
  836. ConfigurationEndpoint: p.ConfigurationEndpoint,
  837. Admins: p.Admins,
  838. Domains: p.Domains,
  839. Groups: p.Groups,
  840. ListenAddress: p.ListenAddress,
  841. TenantId: p.TenantID,
  842. },
  843. },
  844. },
  845. Claims: claimsToLinkedca(p.Claims),
  846. X509Template: x509Template,
  847. SshTemplate: sshTemplate,
  848. }, nil
  849. case *provisioner.GCP:
  850. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  851. if err != nil {
  852. return nil, err
  853. }
  854. return &linkedca.Provisioner{
  855. Id: p.ID,
  856. Type: linkedca.Provisioner_GCP,
  857. Name: p.GetName(),
  858. Details: &linkedca.ProvisionerDetails{
  859. Data: &linkedca.ProvisionerDetails_GCP{
  860. GCP: &linkedca.GCPProvisioner{
  861. ServiceAccounts: p.ServiceAccounts,
  862. ProjectIds: p.ProjectIDs,
  863. DisableCustomSans: p.DisableCustomSANs,
  864. DisableTrustOnFirstUse: p.DisableTrustOnFirstUse,
  865. InstanceAge: p.InstanceAge.String(),
  866. },
  867. },
  868. },
  869. Claims: claimsToLinkedca(p.Claims),
  870. X509Template: x509Template,
  871. SshTemplate: sshTemplate,
  872. }, nil
  873. case *provisioner.AWS:
  874. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  875. if err != nil {
  876. return nil, err
  877. }
  878. return &linkedca.Provisioner{
  879. Id: p.ID,
  880. Type: linkedca.Provisioner_AWS,
  881. Name: p.GetName(),
  882. Details: &linkedca.ProvisionerDetails{
  883. Data: &linkedca.ProvisionerDetails_AWS{
  884. AWS: &linkedca.AWSProvisioner{
  885. Accounts: p.Accounts,
  886. DisableCustomSans: p.DisableCustomSANs,
  887. DisableTrustOnFirstUse: p.DisableTrustOnFirstUse,
  888. InstanceAge: p.InstanceAge.String(),
  889. },
  890. },
  891. },
  892. Claims: claimsToLinkedca(p.Claims),
  893. X509Template: x509Template,
  894. SshTemplate: sshTemplate,
  895. }, nil
  896. case *provisioner.Azure:
  897. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  898. if err != nil {
  899. return nil, err
  900. }
  901. return &linkedca.Provisioner{
  902. Id: p.ID,
  903. Type: linkedca.Provisioner_AZURE,
  904. Name: p.GetName(),
  905. Details: &linkedca.ProvisionerDetails{
  906. Data: &linkedca.ProvisionerDetails_Azure{
  907. Azure: &linkedca.AzureProvisioner{
  908. TenantId: p.TenantID,
  909. ResourceGroups: p.ResourceGroups,
  910. SubscriptionIds: p.SubscriptionIDs,
  911. ObjectIds: p.ObjectIDs,
  912. Audience: p.Audience,
  913. DisableCustomSans: p.DisableCustomSANs,
  914. DisableTrustOnFirstUse: p.DisableTrustOnFirstUse,
  915. },
  916. },
  917. },
  918. Claims: claimsToLinkedca(p.Claims),
  919. X509Template: x509Template,
  920. SshTemplate: sshTemplate,
  921. }, nil
  922. case *provisioner.ACME:
  923. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  924. if err != nil {
  925. return nil, err
  926. }
  927. return &linkedca.Provisioner{
  928. Id: p.ID,
  929. Type: linkedca.Provisioner_ACME,
  930. Name: p.GetName(),
  931. Details: &linkedca.ProvisionerDetails{
  932. Data: &linkedca.ProvisionerDetails_ACME{
  933. ACME: &linkedca.ACMEProvisioner{
  934. ForceCn: p.ForceCN,
  935. Challenges: challengesToLinkedca(p.Challenges),
  936. AttestationFormats: attestationFormatsToLinkedca(p.AttestationFormats),
  937. },
  938. },
  939. },
  940. Claims: claimsToLinkedca(p.Claims),
  941. X509Template: x509Template,
  942. SshTemplate: sshTemplate,
  943. }, nil
  944. case *provisioner.X5C:
  945. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  946. if err != nil {
  947. return nil, err
  948. }
  949. return &linkedca.Provisioner{
  950. Id: p.ID,
  951. Type: linkedca.Provisioner_X5C,
  952. Name: p.GetName(),
  953. Details: &linkedca.ProvisionerDetails{
  954. Data: &linkedca.ProvisionerDetails_X5C{
  955. X5C: &linkedca.X5CProvisioner{
  956. Roots: provisionerPEMToLinkedca(p.Roots),
  957. },
  958. },
  959. },
  960. Claims: claimsToLinkedca(p.Claims),
  961. X509Template: x509Template,
  962. SshTemplate: sshTemplate,
  963. }, nil
  964. case *provisioner.K8sSA:
  965. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  966. if err != nil {
  967. return nil, err
  968. }
  969. return &linkedca.Provisioner{
  970. Id: p.ID,
  971. Type: linkedca.Provisioner_K8SSA,
  972. Name: p.GetName(),
  973. Details: &linkedca.ProvisionerDetails{
  974. Data: &linkedca.ProvisionerDetails_K8SSA{
  975. K8SSA: &linkedca.K8SSAProvisioner{
  976. PublicKeys: provisionerPEMToLinkedca(p.PubKeys),
  977. },
  978. },
  979. },
  980. Claims: claimsToLinkedca(p.Claims),
  981. X509Template: x509Template,
  982. SshTemplate: sshTemplate,
  983. }, nil
  984. case *provisioner.SSHPOP:
  985. return &linkedca.Provisioner{
  986. Id: p.ID,
  987. Type: linkedca.Provisioner_SSHPOP,
  988. Name: p.GetName(),
  989. Details: &linkedca.ProvisionerDetails{
  990. Data: &linkedca.ProvisionerDetails_SSHPOP{
  991. SSHPOP: &linkedca.SSHPOPProvisioner{},
  992. },
  993. },
  994. Claims: claimsToLinkedca(p.Claims),
  995. }, nil
  996. case *provisioner.SCEP:
  997. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  998. if err != nil {
  999. return nil, err
  1000. }
  1001. return &linkedca.Provisioner{
  1002. Id: p.ID,
  1003. Type: linkedca.Provisioner_SCEP,
  1004. Name: p.GetName(),
  1005. Details: &linkedca.ProvisionerDetails{
  1006. Data: &linkedca.ProvisionerDetails_SCEP{
  1007. SCEP: &linkedca.SCEPProvisioner{
  1008. ForceCn: p.ForceCN,
  1009. Challenge: p.GetChallengePassword(),
  1010. Capabilities: p.Capabilities,
  1011. MinimumPublicKeyLength: int32(p.MinimumPublicKeyLength),
  1012. IncludeRoot: p.IncludeRoot,
  1013. EncryptionAlgorithmIdentifier: int32(p.EncryptionAlgorithmIdentifier),
  1014. },
  1015. },
  1016. },
  1017. Claims: claimsToLinkedca(p.Claims),
  1018. X509Template: x509Template,
  1019. SshTemplate: sshTemplate,
  1020. }, nil
  1021. case *provisioner.Nebula:
  1022. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  1023. if err != nil {
  1024. return nil, err
  1025. }
  1026. return &linkedca.Provisioner{
  1027. Id: p.ID,
  1028. Type: linkedca.Provisioner_NEBULA,
  1029. Name: p.GetName(),
  1030. Details: &linkedca.ProvisionerDetails{
  1031. Data: &linkedca.ProvisionerDetails_Nebula{
  1032. Nebula: &linkedca.NebulaProvisioner{
  1033. Roots: provisionerPEMToLinkedca(p.Roots),
  1034. },
  1035. },
  1036. },
  1037. Claims: claimsToLinkedca(p.Claims),
  1038. X509Template: x509Template,
  1039. SshTemplate: sshTemplate,
  1040. }, nil
  1041. default:
  1042. return nil, fmt.Errorf("provisioner %s not implemented", p.GetType())
  1043. }
  1044. }
  1045. func parseInstanceAge(age string) (provisioner.Duration, error) {
  1046. var instanceAge provisioner.Duration
  1047. if age != "" {
  1048. iap, err := provisioner.NewDuration(age)
  1049. if err != nil {
  1050. return instanceAge, err
  1051. }
  1052. instanceAge = *iap
  1053. }
  1054. return instanceAge, nil
  1055. }
  1056. // challengesToCertificates converts linkedca challenges to provisioner ones
  1057. // skipping the unknown ones.
  1058. func challengesToCertificates(challenges []linkedca.ACMEProvisioner_ChallengeType) []provisioner.ACMEChallenge {
  1059. ret := make([]provisioner.ACMEChallenge, 0, len(challenges))
  1060. for _, ch := range challenges {
  1061. switch ch {
  1062. case linkedca.ACMEProvisioner_HTTP_01:
  1063. ret = append(ret, provisioner.HTTP_01)
  1064. case linkedca.ACMEProvisioner_DNS_01:
  1065. ret = append(ret, provisioner.DNS_01)
  1066. case linkedca.ACMEProvisioner_TLS_ALPN_01:
  1067. ret = append(ret, provisioner.TLS_ALPN_01)
  1068. case linkedca.ACMEProvisioner_DEVICE_ATTEST_01:
  1069. ret = append(ret, provisioner.DEVICE_ATTEST_01)
  1070. }
  1071. }
  1072. return ret
  1073. }
  1074. // challengesToLinkedca converts provisioner challenges to linkedca ones
  1075. // skipping the unknown ones.
  1076. func challengesToLinkedca(challenges []provisioner.ACMEChallenge) []linkedca.ACMEProvisioner_ChallengeType {
  1077. ret := make([]linkedca.ACMEProvisioner_ChallengeType, 0, len(challenges))
  1078. for _, ch := range challenges {
  1079. switch provisioner.ACMEChallenge(ch.String()) {
  1080. case provisioner.HTTP_01:
  1081. ret = append(ret, linkedca.ACMEProvisioner_HTTP_01)
  1082. case provisioner.DNS_01:
  1083. ret = append(ret, linkedca.ACMEProvisioner_DNS_01)
  1084. case provisioner.TLS_ALPN_01:
  1085. ret = append(ret, linkedca.ACMEProvisioner_TLS_ALPN_01)
  1086. case provisioner.DEVICE_ATTEST_01:
  1087. ret = append(ret, linkedca.ACMEProvisioner_DEVICE_ATTEST_01)
  1088. }
  1089. }
  1090. return ret
  1091. }
  1092. // attestationFormatsToCertificates converts linkedca attestation formats to
  1093. // provisioner ones skipping the unknown ones.
  1094. func attestationFormatsToCertificates(formats []linkedca.ACMEProvisioner_AttestationFormatType) []provisioner.ACMEAttestationFormat {
  1095. ret := make([]provisioner.ACMEAttestationFormat, 0, len(formats))
  1096. for _, f := range formats {
  1097. switch f {
  1098. case linkedca.ACMEProvisioner_APPLE:
  1099. ret = append(ret, provisioner.APPLE)
  1100. case linkedca.ACMEProvisioner_STEP:
  1101. ret = append(ret, provisioner.STEP)
  1102. case linkedca.ACMEProvisioner_TPM:
  1103. ret = append(ret, provisioner.TPM)
  1104. }
  1105. }
  1106. return ret
  1107. }
  1108. // attestationFormatsToLinkedca converts provisioner attestation formats to
  1109. // linkedca ones skipping the unknown ones.
  1110. func attestationFormatsToLinkedca(formats []provisioner.ACMEAttestationFormat) []linkedca.ACMEProvisioner_AttestationFormatType {
  1111. ret := make([]linkedca.ACMEProvisioner_AttestationFormatType, 0, len(formats))
  1112. for _, f := range formats {
  1113. switch provisioner.ACMEAttestationFormat(f.String()) {
  1114. case provisioner.APPLE:
  1115. ret = append(ret, linkedca.ACMEProvisioner_APPLE)
  1116. case provisioner.STEP:
  1117. ret = append(ret, linkedca.ACMEProvisioner_STEP)
  1118. case provisioner.TPM:
  1119. ret = append(ret, linkedca.ACMEProvisioner_TPM)
  1120. }
  1121. }
  1122. return ret
  1123. }