iOS Code Signing: Part 3, Certificates
In Part 3 of this iOS code signing series, you'll learn how to generate certificates for iOS development and what's in these certificates.
Join the DZone community and get the full member experience.Join For Free
this is part 3 of the ios code signing tutorial series. this series covers fundamentals of the ios code signing process. you can find part 1 here .
in the previous post, we covered how to generate the certificate signing requests from keychain access as well as using command line tools. in this post, we will cover how to generate certificates for ios development and will look inside the certificate contents. as an ios developer, you must have created certificates by yourself at least once and stored them in the keychain. you probably never looked at it until your xcode tells you that your certificate expired. it's important to understand the certificate's contents in order to understand the code signing process. now, let's dive deep into certificates.
the certificate is at the core at code signing in ios apps. let's forget about apple certificates and get into real life. when you finish university or some professional course, you get a certificate. you then attach that certificate to your cv or linkedin profile to show that you are trusted by some authority (the university) that you have completed that program and you have knowledge. the employer is then impressed and offers you the job. certificates are more powerful when they are offered by popular trusted authorities. if you get the certificate from london business school, oxford, or cambridge university, then people trust you more. basically, it's the same concept in the ios or apple world.
in the ios world, you get the certificate from the trusted authority a.k.a. certificate authority (ca), which is apple certifying that
- you are a legitimate (not necessarily good) developer of apps.
- you also paid apple to get a membership with apple developer or another similar program.
- apple has verified that all the information provided is correct and issued you a certificate to build awesome ios apps.
getting the certificate from apple isn't a big thing; you just have to pay them enough to get those required certificates. however, understanding the details of certificates and why we need those certificates are definitely a big thing. apple has ten different types of certificates, as mentioned in the official documentation here . each certificate has a different purpose, but there are two main certificates: the development certificate and distribution certificate.
let's go through the process of generating the certificates. in order to generate a new certificate, we have to log in to our developer portal and select the certificates and profile section. if you click on the certificates section, you will see an option in the form of a + button to create a new certificate. click on the add new certificate which prompt you to select the type of certificate that you want to create, as shown below.
after selecting the certificate - in this case, "ios app development" - the next step will be to create the csr, which we have seen in the above session. apple will give you all the instructions to generate the csr file locally.
this will ask you to save the file certificatesigningrequest.certsigningrequest on your local machine. in the next screen, we need to upload this file to the developer portal to get the certificate. we can then download the certificate to the local machine.
at this stage, you will have the file with the extension downloaded to your local machine. you can then double click on the certificate to add it to the keychain. verify that the keychain has newly generated certificate with private key attached, as shown below.
if you see the certificate with the private key in the keychain, then you can confirm that you have successfully generated an ios developer certificate. you can also confirm that using the following command:
$ security find-identity -v -p codesigning
this command will print all the certificates that you have on the keychain.
you can generate the same for the distribution as well using a similar process, selecting "ios distribution" instead of "ios app development" in the certificate type.
now that we have successfully generated the certificate for ios development, let's look at the certificate and see what's inside to get more information.
basically, the certificate has all the data that we have provided while creating the certificate signing request. apple then adds some signer data like authority, expiry date, etc. the whole certificate has the signature attached to it. in the keychain application, if you right-click on the certificate and "get info," you will see all this information. it should have the following things:
- subject name: this contains userid, name, organization details, and the country name of the developer.
- issuer name: this contains apple details like authority, organization unit that has issued the certificate, and expiry date of the certificate.
- public key info: this contains details of the public key, like which algorithm and signature are used.
- fingerprints: finally, it contains fingerprint details like sha-256 and sha-1 etc. the certificate also shows what this certificate will be used for, like codesigning or any other activity.
you can see the certificate details from the command line, as well:
$ openssl x509 -in ios_development.cer -inform der -text -noout
this will show exactly the same information on the command line with the whole signature at the bottom.
note that these details are exposing all the information of the certificate itself, but we also need the private key attached to the certificate to code sign the apps. this is where we need the certificate in the
format to back it up with the private key.
in cryptography, pkcs#12 , a.k.a. public key cryptography standard is the standard used to store private keys with public key certificates protected with a password. this format defines multiple cryptographic objects in a single file.
in our case, in the ios development certificate, we can export the certificate from the keychain into the personal information exchange, a.k.a. p12 format. just right-click on the certificate in the keychain access and select export.
you will see the option to export your certificate in the
format. also, you can encrypt this certificate with a strong password.
you can also create the certificate in the
format using the command line. if you have generated the private key using the command line while creating csr, as mentioned above, then we can use this key.
$ openssl x509 -in xxxxx.cer -inform der -out mycert.pem -outform pem $ openssl pkcs12 -export -inkey my.key -in mycert.pem -out mycert.p12
if you provided the passphrase, then this command will prompt you to enter the passphrase, and you will see your certificate generated in the p12 format. once, we have generated the certificate in .p12 format, it's easy to backup. there is no risk of losing the private key while using it on multiple macs. the certificate in the p12 format has your certificate combined with the private key. if you are interested in knowing what's inside the p12 certificate, you can find the details using the following command:
$ openssl pkcs12 -info -in mycert.p12
this will prompt you for the password if the p12 is encrypted. the storage container for pkcs#12 is called safe bags, which stores information as bags, with each bag having its own attributes and actual content. in our case, we have two bags: one for the certificate and another for the private key. you will see bag attributes followed by the actual content of the certificate or private keys.
at this stage, we understand the ios certificates and it's internal details. however, having the certificate alone can't code sign ios apps, it requires the private key to code sign the apps. in the next part, we will see another important aspect of code signing: provisioning profiles and entitlements.
Published at DZone with permission of Shashikant Jagtap, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.