0

I have seen question on signing and encrypting final mdm profile here: iOS MDM profile signing, which certificate to use?

I am using Bouncy Castle library for encryption. Currently I am stuck while encrypting the final profile using the scep identitiy certificate.

I am facing the following issue.

  1. The public key retrieved from with scep response certificate is not 16byte(128 bit) so encryption is failing with a message Key should be 128 bit.

  2. If I can change the public key to 16byte using the following code the device throws invalid profile dailog.

    public static string getKeyMessageDigest(string key)
         {
             byte[] ByteData = Encoding.UTF8.GetBytes(key);
             //MD5 creating MD5 object.
             MD5 oMd5 = MD5.Create();
             byte[] HashData = oMd5.ComputeHash(ByteData);
    
             //convert byte array to hex format
             StringBuilder oSb = new StringBuilder();
             for (int x = 0; x < HashData.Length; x++)
             {
                 //hexadecimal string value
                 oSb.Append(HashData[x].ToString("x2"));
             }
             return Convert.ToString(oSb);
         }
    

Can some one help me with some blog or sample code to encrypt the profile? Appreciate your help.

Community
  • 1
  • 1
user170940
  • 11
  • 4

3 Answers3

1

I had a similar problem. PFB the working code that I'm using to encrypt now. I'm retrieving the signing certificate from the device response, retrieving the public key from it and using the same to encrypt.

byte[] request = StreamToByte(ResponseFromDevice);
var signer = new SignedCms();
signer.Decode(request);
X509Certificate2 certificate = signer.Certificates[0];
string xmlData = "payload string to encrypt";

Byte[] cleartextsbyte = UTF8Encoding.UTF8.GetBytes(xmlData);
ContentInfo contentinfo = new ContentInfo(cleartextsbyte);
EnvelopedCms envelopedCms = new EnvelopedCms(contentinfo);
CmsRecipient recipient = new CmsRecipient(certificate);
envelopedCms.Encrypt(recipient);
string data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"><plist version=\"1.0\"><dict><key>EncryptedPayloadContent</key><data>[ENCRYPTEDDATA]</data><key>PayloadDescription</key><string>For profile enrollment</string><key>PayloadDisplayName</key><string>ProfileName</string><key>PayloadIdentifier</key><string>YourIdentifier</string><key>PayloadOrganization</key><string>YourOrg</string><key>PayloadRemovalDisallowed</key><false/><key>PayloadType</key><string>Configuration</string><key>PayloadUUID</key><string>YourUDID/string><key>PayloadVersion</key><integer>1</integer></dict></plist>";
data = data.Replace("[ENCRYPTEDDATA]", Convert.ToBase64String(envelopedCms.Encode()));
HttpContext.Current.Response.Write(data);
WebOperationContext.Current.OutgoingResponse.ContentType = "application/x-apple-aspen-config";
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK;
Viji
  • 21
  • 2
  • +1 You're a superstar! I've been struggling with this for a while now. I had the exact same solution, but had missed the `Encrypted` from `EncryptedPayloadContent`. Where on earth is that documented?! Anyway, thanks for your help! – Mark Whitaker May 29 '14 at 08:24
0

I answered in comments on your previous question:

"I would recommend to take a look on OS X Server MDM implementation.

Generally speaking to encrypt profile, as I remember you should use PKCS7 wrapping. So, you should look at this: http://www.cs.berkeley.edu/~jonah/bc/org/bouncycastle/jce/PKCS7SignedData.html

BTW. I would recommend to read up a little bit on cryptography, if you want to get general understanding. Very-very high level overview of your problem: you are trying to use RSA key directly to encrypt the data. However, it should be used to encrypt a symmetric key which in its turn is used to encrypt the data."

You can also take a look here: PKCS#7 Encryption

Your code won't work, because it's - not PKCS7 - you are trying to use MD5(public certificate key) which doesn't make any sense

I would really-really recommend to read again MDM documentation and something on cryptopraphy. It's quite easy to make it wrong (both non working or unsecure implementation).

Community
  • 1
  • 1
Victor Ronin
  • 22,758
  • 18
  • 92
  • 184
0

In bouncycastle you have to encrypt it using CMSAlgorithm.DES_EDE3_CBC. Then signed the data as you done in the previous step. Make sure you Base64 encode the encrypted payload before signing.

Dilshan
  • 3,231
  • 4
  • 39
  • 50