开发者

how to encrypt and sign iphone mobile configuration file using Ruby

开发者 https://www.devze.com 2023-03-29 02:36 出处:网络
I have a unsigned mobileconfig file in xml format (template) generated by iphone configuration utility. I would like to encrypt and sign it using openssl and be able to install it on iphone using Ruby

I have a unsigned mobileconfig file in xml format (template) generated by iphone configuration utility. I would like to encrypt and sign it using openssl and be able to install it on iphone using Ruby on rails. I do not want to create a SCEP server to do this as I want to keep modifying this template xml file dynamically and serve it using some URL.

Thanks for your help in advance.

I have already checked following question but it is not clear from it how to encrypt the file that can be installed correctly on iphone - as I keep getting "config file could not be installed due to unknown error" when I tried to mimic encrypted file format from iphone c开发者_Go百科onfiguration utility by only encrypting the part and appending/prepending other parts of the configuration file appropriately.

Signing iPhone Configuration XML Profile with Ruby on Rails

This manual by apple is useful but its more geared for creating a SCEP server and not for manipulating template mobileconfig file -

http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/iPhoneOTAConfiguration/profile-service/profile-service.html


Incase if still anybody is having issues with signing and encrypting the profile with Ruby, following answer would be useful.

I have used OpenSSL module available in Ruby and Plist gem.

Consider a passcode restriction profile.

passcode_payload ={
              'PayloadUUID' => 'RANDOM_STRING_UUID',
              'PayloadOrganization' => 'PayloadOrganization',
              'PayloadVersion' => 1,
              'PayloadIdentifier' => 'com.test.PayloadIdentifier',
              'PayloadType' => 'Configuration',
              'PayloadDisplayName' => 'PayloadDisplayName',
              'PayloadRemovalDisallowed' => false
            }
  passcode_payload_content = {
              'PayloadDescription' => 'PayloadDescription',
              'PayloadDisplayName' => 'PayloadDisplayName',
              'PayloadIdentifier' => 'PayloadIdentifier',
              'PayloadOrganization' => 'PayloadOrganization',
              'PayloadType' => 'com.apple.mobiledevice.passwordpolicy',
              'PayloadUUID' => "RANDOM_STRING_UUID",
              'PayloadVersion' => 1,
              'allowSimple' => true,
              'forcePIN' => true
              'maxPINAgeInDays' => 20,
              'minComplexChars' => 1,
              'minLength' => 4,
              'requireAlphanumeric' => true
            }

**

Encryption

**

Usually for a normal profile the passcode_payload_content goes into the passcode_payload['PayloadContent'] as array of dictionaries.

passcode_payload['PayloadContent'] = [passcode_payload_content]

But for an encrypted profile, PayloadContent should be removed and EncryptedPayloadContent should be used as per the configuration profile key reference document.

from the doc,

To encrypt a profile do the following:

  • Remove the PayloadContent array and serialize it as a proper plist.
  • Note that the top-level object in this plist is an array, not a dictionary.
  • CMS-encrypt the serialized plist as enveloped data. Serialize the encrypted data in DER format.
  • Set the serialized data as the value of as a Data plist item in the profile, using the key EncryptedPayloadContent
  • Since top level object in the plist should be an array

    passcode_payload_content_array = [passcode_payload_content]
    

    Serializing to proper plist

    to_be_encrypted_plist = passcode_payload_content_array.to_plist
    

    Encrypting the certificate payload content,

    device_certificate = OpenSSL::X509::Certificate.new File.read('deviceIdentityCertificate.pem')
    encrypted_payload = OpenSSL::PKCS7.encrypt([device_certificate],to_be_encrypted_plist, OpenSSL::Cipher::Cipher::new("des-ede3-cbc"),OpenSSL::PKCS7::BINARY)
    

    Add encrypted payload content to the original payload in der format

    passcode_payload['EncryptedPayloadContent'] = StringIO.new(encrypted_payload.to_der)
    

    **

    Signing

    **

    signed_passcode_profile = OpenSSL::PKCS7.sign(SSL_CERTIFICATE, SSL_KEY, passcode_payload.to_plist, [], OpenSSL::PKCS7::BINARY)
    

    At last, you can use

    send_data signed_passcode_profile.to_der, :type => "application/x-apple-aspen-config" 
    

    to send the payload.


    The link you mentioned talks only about signing the iPhone configuration profile. Does atleast signing a profile works for you?
    You can verify that signature generated by your code is correct using openssl command line utility. Write the generated signature to a file and use following command to verify signature and extract original data from pkcs#7 signature.

    openssl smime -verify -inform DER -in signed_config.p7s -signer your_signing_certificate -out data.txt

    0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    关注公众号