@@ -130,13 +130,7 @@ func RunEscrow(r utils.Runner, p pref.PrefInterface) error {
130130 return errors .Wrap (err , "failed to get mTLS common name for escrow" )
131131 }
132132
133- if mTLScommonName != "" {
134- // we will use mTLS for escrow, as well as native go http client
135- keyRotated , err = escrowWithMTLS (cryptData , r , p , mTLScommonName )
136- } else {
137- // escrow using curl if mTLS is not configured
138- keyRotated , err = escrowKey (cryptData , r , p )
139- }
133+ keyRotated , err = escrowKey (cryptData , r , p , mTLScommonName )
140134 if err != nil {
141135 return errors .Wrap (err , "escrow operation failed" )
142136 }
@@ -546,44 +540,60 @@ func runCurl(configFile string, r utils.Runner, p pref.PrefInterface) (string, e
546540 return string (out ), nil
547541}
548542
549- // escrowKey attempts to escrow a key by sending data to a server URL built from preferences.
550- // It logs the process and handles errors appropriately.
543+ // escrowKey attempts to escrow a key to the server, using either mTLS or curl based on configuration.
544+ // It builds the check-in URL, constructs the form data, and sends the escrow request using
545+ // the appropriate method based on whether a CommonNameForEscrow is configured.
551546//
552547// Parameters:
553- // - plist: CryptData containing the data to be sent.
554- // - r: utils.Runner to execute commands.
555- // - p: pref.PrefInterface to retrieve preferences.
548+ // - plist: CryptData containing the data to be sent
549+ // - r: utils.Runner interface for executing commands
550+ // - p: pref.PrefInterface for accessing preferences
551+ // - mTLScommonName: Optional common name for mTLS authentication. If empty, curl will be used.
556552//
557553// Returns:
558- // - bool: indicating if the key rotation was initiated by the server.
559- // - error: if any error occurs during the process.
560- func escrowKey (plist CryptData , r utils.Runner , p pref.PrefInterface ) (bool , error ) {
554+ // - bool: Indicates if the key was rotated as part of the escrow process
555+ // - error: Any error encountered during the process
556+ func escrowKey (plist CryptData , r utils.Runner , p pref.PrefInterface , mTLScommonName string ) (bool , error ) {
561557 log .Println ("Attempting to Escrow Key..." )
562- // serverURL, err := p.GetString("ServerURL")
563- // if err != nil {
564- // return errors.Wrap(err, "failed to get server URL")
565- // }
558+
566559 theURL , err := buildCheckinURL (p )
567560 if err != nil {
568561 return false , errors .Wrap (err , "failed to build checkin URL" )
569562 }
570563
564+ // Build form data
571565 data , err := buildData (plist , r )
572566 if err != nil {
573567 return false , errors .Wrap (err , "failed to build data" )
574568 }
575- configFile := utils .BuildCurlConfigFile (map [string ]string {"url" : theURL , "data" : data })
576- output , err := runCurl (configFile , r , p )
577- if err != nil {
578- return false , errors .Wrap (err , "failed to run curl" )
569+
570+ var responseBody string
571+
572+ // Determine whether to use mTLS or curl based on whether a common name is provided
573+ if mTLScommonName != "" {
574+ log .Printf ("Using mTLS for escrow with common name: %s" , mTLScommonName )
575+ body , err := sendRequest (theURL , data , mTLScommonName )
576+ if err != nil {
577+ return false , errors .Wrap (err , "failed to send request with mTLS" )
578+ }
579+ responseBody = string (body )
580+ } else {
581+ log .Println ("Using curl for escrow" )
582+ configFile := utils .BuildCurlConfigFile (map [string ]string {"url" : theURL , "data" : data })
583+ output , err := runCurl (configFile , r , p )
584+ if err != nil {
585+ return false , errors .Wrap (err , "failed to run curl" )
586+ }
587+ responseBody = output
579588 }
580589
581590 log .Println ("Key escrow successful." )
582591
583- keyRotated , err := serverInitiatedRotation (output , r , p )
592+ keyRotated , err := serverInitiatedRotation (responseBody , r , p )
584593 if err != nil {
585594 return false , errors .Wrap (err , "serverInitiatedRotation" )
586595 }
596+
587597 return keyRotated , nil
588598}
589599
@@ -773,48 +783,6 @@ func getRecoveryKey(keyLocation string, p pref.PrefInterface) (string, error) {
773783 return key .RecoveryKey , nil
774784}
775785
776- // escrowWithMTLS attempts to escrow a key using mTLS (mutual TLS) authentication.
777- // It builds the check-in URL, constructs the form data, creates an HTTP POST request,
778- // and sends it using an mTLS client. The function checks the response status and
779- // processes the response body to determine if the key escrow was successful.
780- //
781- // Parameters:
782- // - plist: CryptData containing the data to be sent.
783- // - r: utils.Runner interface for executing commands.
784- // - p: pref.PrefInterface for accessing preferences.
785- //
786- // Returns:
787- // - bool: Indicates if the key was rotated as part of the escrow process.
788- // - error: Any error encountered during the process.
789- func escrowWithMTLS (plist CryptData , r utils.Runner , p pref.PrefInterface , commonName string ) (bool , error ) {
790- log .Println ("Attempting to Escrow Key..." )
791-
792- theURL , err := buildCheckinURL (p )
793- if err != nil {
794- return false , errors .Wrap (err , "failed to build checkin URL" )
795- }
796-
797- // Build form data
798- data , err := buildData (plist , r )
799- if err != nil {
800- return false , errors .Wrap (err , "failed to build data" )
801- }
802-
803- body , err := sendRequest (theURL , data , commonName )
804- if err != nil {
805- return false , errors .Wrap (err , "failed to send request" )
806- }
807-
808- log .Println ("Key escrow successful." )
809-
810- keyRotated , err := serverInitiatedRotation (string (body ), r , p )
811- if err != nil {
812- return false , errors .Wrap (err , "serverInitiatedRotation" )
813- }
814-
815- return keyRotated , nil
816- }
817-
818786// sendRequest sends an HTTP POST request to the specified URL with the given data
819787// and uses mTLS (mutual TLS) for authentication with the provided common name.
820788// It returns the response body as a byte slice or an error if the request fails.
0 commit comments