This document explains how to use S/MIME email certificates in the Gmail API.
The Gmail API provides programmatic access to manage S/MIME email certificates for users in a Google Workspace domain.
An administrator must turn on hosted S/MIME for the domain for the certificates to work.
The S/MIME standard provides a specification for public key encryption and signing of MIME data. When S/MIME certificates are configured in a user account, Gmail uses them in the following ways:
Sign outgoing mail with the user certificate and private key.
Decrypt incoming mail with the user private key.
Encrypt outgoing mail with the recipient certificate and public key.
Verify incoming mail with the sender certificate and public key.
You can generate individual S/MIME certificates and upload them using the Gmail API. Each S/MIME certificate is for a specific alias for a user email account. Aliases include the primary email address and custom "Send As" addresses. The API marks a single S/MIME certificate as the default for each alias.
For more information on aliases, see Manage aliases and signatures with the Gmail API.
Authorize API access
To authorize access to the Gmail API, use one of the following methods:
Use a service account with domain-wide delegation of authority. For an explanation of these terms, see Learn about authentication and authorization. To enable this option, see Create access credentials.
Use a standard OAuth 2.0 flow that requires end-user consent to obtain an OAuth 2.0 access token. For more information, see Learn about authentication and authorization.
To use this option, the domain administrator must select the Enable S/MIME encryption for sending and receiving emails checkbox in the Google Admin console. For more information, see Turn on hosted S/MIME in your Google Admin console.
ACL scopes
The Gmail API relies on the same ACL scopes as the Gmail sendAs methods:
gmail.settings.basic: This scope is required for updating the primarySendAsS/MIME.gmail.settings.sharing: This scope is required for updating the custom from S/MIME.
Configure S/MIME keys
The
settings.sendAs.smimeInfo
resource provides several methods to manage S/MIME certificates. Each
certificate is associated with one send-as alias for a user.
To determine the send-as aliases for a user, use the
settings.sendAs.list
method on the
settings.sendAs
resource.
Upload an S/MIME key
Use the
settings.sendAs.smimeInfo.insert
method on the settings.sendAs.smimeInfo resource to upload a new S/MIME key
for an alias belonging to a user. Identify the target alias using the following
path parameters:
userId: The user's email address. Use the special valuemeto indicate the authenticated user.sendAsEmail: The alias for which you're uploading the key. This email address appears in theFrom:header for mail sent using this alias.
The S/MIME certificate and private key should be present in the
pkcs12
field in that format; no other fields should be set in the request. The pkcs12
field contains both the user S/MIME key and the signing certificate chain. The
API performs standard validations on this field before accepting it, verifying
the following:
- The subject matches the specified email address.
- Expirations are valid.
- The issuing certificate authority (CA) is in the Google trusted list.
- The certificates match Gmail's technical constraints.
If the key is encrypted, the password should be in the
encryptedKeyPassword
field. A successful settings.sendAs.smimeInfo.insert method call returns the
settings.sendAs.smimeInfo resource id used to refer to the key in the
future.
List a user's S/MIME keys
Use the
settings.sendAs.smimeInfo.list
method on the settings.sendAs.smimeInfo resource to return the list of S/MIME
keys for the given user for the given alias. Identify the target alias using the
following path parameters:
userId: The user's email address. Use the special valuemeto indicate the authenticated user.sendAsEmail: The alias for which to list keys. This email address appears in theFrom:header for mail sent using this alias.
Retrieve the S/MIME keys for an alias
Use the
settings.sendAs.smimeInfo.get
method on the settings.sendAs.smimeInfo resource to return the specific S/MIME
keys for a specific send-as alias for a user. Identify the target alias using
the following path parameters:
userId: The user's email address. Use the special valuemeto indicate the authenticated user.sendAsEmail: The alias for which you're retrieving the keys. This email address appears in theFrom:header for mail sent using this alias.
Delete an S/MIME key
Use the
settings.sendAs.smimeInfo.delete
method on the settings.sendAs.smimeInfo resource to delete the specified
S/MIME key from an alias. Identify the target alias using the following path
parameters:
userId: The user's email address. Use the special valuemeto indicate the authenticated user.sendAsEmail: The alias for which you're deleting the keys. This email address appears in theFrom:header for mail sent using this alias.id: The immutable ID for thesmimeInfo.
Set the default S/MIME key for an alias
Use the
settings.sendAs.smimeInfo.setDefault
method on the settings.sendAs.smimeInfo resource to mark the specified S/MIME
key as the default for the specified alias. Identify the target alias using the
following path parameters:
userId: The user's email address. Use the special valuemeto indicate the authenticated user.sendAsEmail: The alias for which keys to set as default. This email address appears in theFrom:header for mail sent using this alias.id: The immutable ID for thesmimeInfo.
Code samples
The following code samples show how to use the Gmail API to manage S/MIME certificates for an organization with multiple users:
Create an smimeInfo resource for an S/MIME certificate
This code sample shows how to read a certificate from a file, encode it to a
Base64URL string, and assign it to the
pkcs12
field on the settings.sendAs.smimeInfo resource:
Java
Python
Upload an S/MIME certificate
To upload a certificate, call the
settings.sendAs.smimeInfo.insert
method and supply the settings.sendAs.smimeInfo resource in the body of the
request:
Java
Python
Manage multiple users' certificates
These code samples show how to manage certificates for multiple users in an organization in one batch call:
Insert certificates from a CSV file
The following is a sample CSV file that lists user IDs and the path to each user's certificate:
$ cat certificates.csv
user1@example.com,/path/to/user1_cert.p12,cert_password_1
user2@example.com,/path/to/user2_cert.p12,cert_password_2
user3@example.com,/path/to/user3_cert.p12,cert_password_3
Java
You can use the
CreateSmimeInfo and
InsertSmimeInfo samples
to upload the certificates for the users specified in a CSV file:
Python
You can use the
create_smime_info and
insert_smime_info
samples to upload the certificates for the users specified in a CSV file:
Certificate management
This sample combines several methods from the settings.sendAs.smimeInfo
resource to show how to manage certificates for your organization. It lists the
certificates for the user. If the default certificate is expired or not set, it
uploads the certificate found in the specified file. Then it sets the
certificate whose expiration is furthest into the future as the default.
This function then processes a CSV file, similar to the previous Insert certificates from a CSV file sample.
Java
Python
Related topics
- Manage aliases and signatures with the Gmail API
- Choose Gmail API scopes
- Turn on hosted S/MIME for message encryption