مدیریت گواهینامه های S/MIME

Gmail S/MIME API دسترسی برنامه‌ای را برای مدیریت گواهی‌های ایمیل S/MIME برای کاربران در دامنه Google Workspace فراهم می‌کند.

یک سرپرست باید S/MIME را برای دامنه فعال کند تا گواهی ها کار کنند.

استاندارد S/MIME مشخصاتی را برای رمزگذاری کلید عمومی و امضای داده های MIME ارائه می کند. پیکربندی گواهی‌های S/MIME در حساب کاربری باعث می‌شود Gmail از آن گواهی به روش‌های زیر استفاده کند:

  • Gmail از گواهینامه و کلید خصوصی کاربر برای امضای نامه های خروجی استفاده می کند.
  • جیمیل از کلید خصوصی کاربر برای رمزگشایی نامه های دریافتی استفاده می کند.
  • Gmail از گواهی گیرنده و کلید عمومی برای رمزگذاری نامه های خروجی استفاده می کند.
  • Gmail از گواهی نامه فرستنده و کلید عمومی برای تأیید نامه های دریافتی استفاده می کند.

شما گواهی‌های S/MIME جداگانه تولید می‌کنید و با استفاده از API آن‌ها را آپلود می‌کنید. هر گواهی S/MIME برای یک نام مستعار خاص برای یک حساب ایمیل کاربر است. نام مستعار شامل آدرس ایمیل اصلی و همچنین آدرس های سفارشی "ارسال به عنوان" است. یک گواهی S/MIME به عنوان پیش فرض برای هر نام مستعار مشخص می شود.

مجوز دسترسی به API

دو شکل برای مجوز دسترسی به API وجود دارد:

  1. می‌توانید از یک حساب سرویس با تفویض اختیار در سطح دامنه استفاده کنید. برای توضیح این اصطلاحات، به احراز هویت و شرایط نمای کلی مجوز مراجعه کنید. برای اطلاعات در مورد فعال کردن این گزینه، به ایجاد یک حساب سرویس با تفویض اختیار در سطح دامنه مراجعه کنید
  2. می‌توانید از یک جریان استاندارد OAuth2 استفاده کنید که برای دریافت رمز دسترسی Oauth2 به رضایت کاربر نهایی نیاز دارد. برای اطلاعات بیشتر، به نمای کلی احراز هویت و مجوز مراجعه کنید. برای استفاده از این گزینه، سرپرست دامنه باید کادر انتخاب «دسترسی کاربر نهایی S/MIME API فعال است» را در کنترل پنل دامنه فعال کند.

دامنه های ACL

این API بر همان حوزه‌های ACL متکی است که روش‌های sendAs Gmail :

gmail.settings.basic
این محدوده برای به‌روزرسانی SendAs S/MIME اولیه مورد نیاز است.
gmail.settings.sharing
این محدوده برای به‌روزرسانی سفارشی از S/MIME مورد نیاز است.

با استفاده از API

منبع users.settings.sendAs.smimeInfo روش هایی را ارائه می دهد که شما برای مدیریت گواهی های S/MIME استفاده می کنید. هر گواهی با یک نام مستعار send-as برای یک کاربر مرتبط است.

یک کلید S/MIME بارگذاری کنید

از متد smimeInfo.insert() برای آپلود یک کلید S/MIME جدید برای نام مستعار متعلق به یک کاربر استفاده کنید. شما نام مستعار هدف را با استفاده از پارامترهای زیر شناسایی می کنید:

شناسه کاربر
آدرس ایمیل کاربر. می توانید از مقدار ویژه me برای نشان دادن کاربر تأیید شده فعلی استفاده کنید.
sendAsEmail
نام مستعاری که کلید آن را آپلود می کنید. این آدرس ایمیلی است که در سرصفحه "از:" برای نامه هایی که با استفاده از این نام مستعار ارسال می شوند ظاهر می شود.

گواهی S/MIME و کلید خصوصی باید در فیلد pkcs12 در آن قالب وجود داشته باشد. هیچ فیلد دیگری نباید در درخواست تنظیم شود. انتظار می رود فیلد PKCS12 حاوی کلید S/MIME کاربر و زنجیره گواهی امضا باشد. API قبل از پذیرش این فیلد، اعتبارسنجی استاندارد را انجام می دهد و موارد زیر را تأیید می کند:

  • موضوع با آدرس ایمیل مشخص شده مطابقت دارد.
  • انقضا معتبر است.
  • مرجع صدور گواهی (CA) در لیست مورد اعتماد ما قرار دارد.
  • گواهی ها با محدودیت های فنی Gmail مطابقت دارند.

اگر کلید رمزگذاری شده است، رمز عبور باید در قسمت encryptedKeyPassword باشد. فراخوانی های موفق insert() شناسه smimeInfo را برمی گرداند که می تواند برای ارجاع به کلید در آینده استفاده شود.

کلیدهای S/MIME کاربر را فهرست کنید

از متد smimeInfo.list() برای بازگرداندن لیست کلیدهای S/MIME برای کاربر داده شده برای نام مستعار استفاده کنید. شما نام مستعار هدف را با استفاده از پارامترهای زیر شناسایی می کنید:

شناسه کاربر
آدرس ایمیل کاربر. می توانید از مقدار ویژه me برای نشان دادن کاربر تأیید شده فعلی استفاده کنید.
sendAsEmail
نام مستعار که برای فهرست کردن کلیدها. این آدرس ایمیلی است که در سرصفحه "از:" برای نامه هایی که با استفاده از این نام مستعار ارسال می شوند ظاهر می شود.

کلیدهای S/MIME را برای نام مستعار بازیابی کنید

از متد smimeInfo.get() برای برگرداندن کلیدهای S/MIME خاص برای یک نام مستعار send-as برای یک کاربر استفاده کنید. شما نام مستعار هدف را با استفاده از پارامترهای زیر شناسایی می کنید:

شناسه کاربر
آدرس ایمیل کاربر. می توانید از مقدار ویژه me برای نشان دادن کاربر تأیید شده فعلی استفاده کنید.
sendAsEmail
نام مستعار که شما در حال بازیابی کلیدها هستید. این آدرس ایمیلی است که در سرصفحه "از:" برای نامه هایی که با استفاده از این نام مستعار ارسال می شوند ظاهر می شود.

یک کلید S/MIME را حذف کنید

از متد smimeInfo.delete() برای حذف کلید S/MIME مشخص شده از نام مستعار استفاده کنید. شما نام مستعار هدف را با استفاده از پارامترهای زیر شناسایی می کنید:

شناسه کاربر
آدرس ایمیل کاربر. می توانید از مقدار ویژه me برای نشان دادن کاربر تأیید شده فعلی استفاده کنید.
sendAsEmail
نام مستعار که شما در حال بازیابی کلیدها هستید. این آدرس ایمیلی است که در سرصفحه "از:" برای نامه هایی که با استفاده از این نام مستعار ارسال می شوند ظاهر می شود.
شناسه
شناسه تغییرناپذیر SmimeInfo.

کلید S/MIME پیش فرض را برای نام مستعار تنظیم کنید

از متد smimeInfo.setDefault() برای علامت گذاری کلید S/MIME مشخص شده به عنوان پیش فرض برای نام مستعار مشخص شده استفاده کنید. شما نام مستعار هدف را با استفاده از پارامترهای زیر شناسایی می کنید:

شناسه کاربر
آدرس ایمیل کاربر. می توانید از مقدار ویژه me برای نشان دادن کاربر تأیید شده فعلی استفاده کنید.
sendAsEmail
نام مستعار که شما در حال بازیابی کلیدها هستید. این آدرس ایمیلی است که در سرصفحه "از:" برای نامه هایی که با استفاده از این نام مستعار ارسال می شوند ظاهر می شود.
شناسه
شناسه تغییرناپذیر SmimeInfo.

کد نمونه

نمونه کد زیر استفاده از API را برای مدیریت گواهی‌های S/MIME برای سازمانی با چندین کاربر نشان می‌دهد.

ایجاد یک منبع SmimeInfo برای گواهی S/MIME

نمونه کد زیر خواندن یک گواهی از فایل، کدگذاری به رشته base64url و اختصاص آن به فیلد pkcs12 منبع smimeInfo را نشان می دهد:

جاوا

gmail/snippets/src/main/java/CreateSmimeInfo.java
import com.google.api.services.gmail.model.SmimeInfo;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

/* Class to demonstrate the use of Gmail Create SmimeInfo API */
public class CreateSmimeInfo {
  /**
   * Create an SmimeInfo resource for a certificate from file.
   *
   * @param filename Name of the file containing the S/MIME certificate.
   * @param password Password for the certificate file, or null if the file is not
   *                 password-protected.
   * @return An SmimeInfo object with the specified certificate.
   */
  public static SmimeInfo createSmimeInfo(String filename, String password) {
    SmimeInfo smimeInfo = null;
    InputStream in = null;

    try {
      File file = new File(filename);
      in = new FileInputStream(file);
      byte[] fileContent = new byte[(int) file.length()];
      in.read(fileContent);

      smimeInfo = new SmimeInfo();
      smimeInfo.setPkcs12(Base64.getUrlEncoder().encodeToString(fileContent));
      if (password != null && password.length() > 0) {
        smimeInfo.setEncryptedKeyPassword(password);
      }
    } catch (Exception e) {
      System.out.printf("An error occured while reading the certificate file: %s\n", e);
    } finally {
      try {
        if (in != null) {
          in.close();
        }
      } catch (IOException ioe) {
        System.out.printf("An error occured while closing the input stream: %s\n", ioe);
      }
    }
    return smimeInfo;
  }
}

پایتون

gmail/snippet/smime snippets/create_smime_info.py
import base64


def create_smime_info(cert_filename, cert_password):
  """Create an smimeInfo resource for a certificate from file.
  Args:
    cert_filename: Name of the file containing the S/MIME certificate.
    cert_password: Password for the certificate file, or None if the file is not
        password-protected.
  Returns : Smime object, including smime information
  """

  smime_info = None
  try:
    with open(cert_filename, "rb") as cert:
      smime_info = {}
      data = cert.read().encode("UTF-8")
      smime_info["pkcs12"] = base64.urlsafe_b64encode(data).decode()
      if cert_password and len(cert_password) > 0:
        smime_info["encryptedKeyPassword"] = cert_password

  except (OSError, IOError) as error:
    print(f"An error occurred while reading the certificate file: {error}")
    smime_info = None

  return smime_info


if __name__ == "__main__":
  print(create_smime_info(cert_filename="xyz", cert_password="xyz"))

بارگذاری گواهی S/MIME

برای آپلود گواهی، با smimeInfo.insert تماس بگیرید و منبع smimeInfo را در متن درخواست ارائه دهید:

جاوا

gmail/snippets/src/main/java/InsertSmimeInfo.java
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.GmailScopes;
import com.google.api.services.gmail.model.SmimeInfo;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;

/* Class to demonstrate the use of Gmail Insert Smime Certificate API*/
public class InsertSmimeInfo {
  /**
   * Upload an S/MIME certificate for the user.
   *
   * @param userId      User's email address.
   * @param sendAsEmail The "send as" email address, or null if it should be the same as userId.
   * @param smimeInfo   The SmimeInfo object containing the user's S/MIME certificate.
   * @return An SmimeInfo object with details about the uploaded certificate, {@code null} otherwise.
   * @throws IOException - if service account credentials file not found.
   */
  public static SmimeInfo insertSmimeInfo(String userId,
                                          String sendAsEmail,
                                          SmimeInfo smimeInfo)
      throws IOException {
        /* Load pre-authorized user credentials from the environment.
           TODO(developer) - See https://developers.google.com/identity for
            guides on implementing OAuth2 for your application. */
    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
        .createScoped(GmailScopes.GMAIL_SETTINGS_SHARING);
    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
        credentials);

    // Create the gmail API client
    Gmail service = new Gmail.Builder(new NetHttpTransport(),
        GsonFactory.getDefaultInstance(),
        requestInitializer)
        .setApplicationName("Gmail samples")
        .build();

    if (sendAsEmail == null) {
      sendAsEmail = userId;
    }

    try {
      SmimeInfo results = service.users().settings().sendAs().smimeInfo()
          .insert(userId, sendAsEmail, smimeInfo)
          .execute();
      System.out.printf("Inserted certificate, id: %s\n", results.getId());
      return results;
    } catch (IOException e) {
      System.err.printf("An error occured: %s", e);
    }
    return null;
  }
}

پایتون

gmail/snippet/smime snippets/insert_smime_info.py
import create_smime_info
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def insert_smime_info():
  """Upload an S/MIME certificate for the user.
  Print the inserted certificate's id
  Returns : Result object with inserted certificate id and other meta-data

  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """
  creds, _ = google.auth.default()

  try:
    # create gmail api client
    service = build("gmail", "v1", credentials=creds)

    user_id = "gduser1@workspacesamples.dev"
    smime_info = create_smime_info.create_smime_info(
        cert_filename="xyz", cert_password="xyz"
    )
    send_as_email = None

    if not send_as_email:
      send_as_email = user_id

    # pylint: disable=maybe-no-member
    results = (
        service.users()
        .settings()
        .sendAs()
        .smimeInfo()
        .insert(userId=user_id, sendAsEmail=send_as_email, body=smime_info)
        .execute()
    )
    print(f'Inserted certificate; id: {results["id"]}')

  except HttpError as error:
    print(f"An error occurred: {error}")
    results = None

  return results


if __name__ == "__main__":
  insert_smime_info()

نمونه هایی برای مدیریت گواهی های بسیاری از کاربران

ممکن است بخواهید گواهینامه ها را برای بسیاری از کاربران سازمان به طور همزمان مدیریت کنید. مثال‌های زیر نحوه مدیریت گواهی‌های چند کاربر را در یک تماس دسته‌ای نشان می‌دهند.

درج گواهی از یک فایل CSV

فرض کنید یک فایل CSV دارید که شناسه های کاربر و مسیر گواهی هر کاربر را فهرست می کند:

$ 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

جاوا

می توانید از تماس های createSmimeInfo و insertSmimeInfo از قبل برای آپلود گواهی ها همانطور که در فایل CSV مشخص شده است استفاده کنید:

gmail/snippets/src/main/java/InsertCertFromCsv.java
import com.google.api.services.gmail.model.SmimeInfo;
import java.io.File;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

/* Class to demonstrate the use of Gmail Insert Certificate from CSV File */
public class InsertCertFromCsv {
  /**
   * Upload S/MIME certificates based on the contents of a CSV file.
   *
   * <p>Each row of the CSV file should contain a user ID, path to the certificate, and the
   * certificate password.
   *
   * @param csvFilename Name of the CSV file.
   */
  public static void insertCertFromCsv(String csvFilename) {
    try {
      File csvFile = new File(csvFilename);
      CSVParser parser =
          CSVParser.parse(csvFile, java.nio.charset.StandardCharsets.UTF_8, CSVFormat.DEFAULT);
      for (CSVRecord record : parser) {
        String userId = record.get(0);
        String certFilename = record.get(1);
        String certPassword = record.get(2);
        SmimeInfo smimeInfo = CreateSmimeInfo.createSmimeInfo(certFilename,
            certPassword);
        if (smimeInfo != null) {
          InsertSmimeInfo.insertSmimeInfo(userId,
              userId,
              smimeInfo);
        } else {
          System.err.printf("Unable to read certificate file for userId: %s\n", userId);
        }
      }
    } catch (Exception e) {
      System.err.printf("An error occured while reading the CSV file: %s", e);
    }
  }
}

پایتون

می توانید از تماس های create_smime_info و insert_smime_info از قبل برای آپلود گواهی ها همانطور که در فایل CSV مشخص شده است استفاده کنید:

gmail/snippet/smime snippets/insert_cert_from_csv.py
import csv

import create_smime_info
import insert_smime_info


def insert_cert_from_csv(csv_filename):
  """Upload S/MIME certificates based on the contents of a CSV file.
  Each row of the CSV file should contain a user ID, path to the certificate,
  and the certificate password.

  Args:
    csv_filename: Name of the CSV file.
  """

  try:
    with open(csv_filename, "rb") as cert:
      csv_reader = csv.reader(cert, delimiter=",")
      next(csv_reader, None)  # skip CSV file header
      for row in csv_reader:
        user_id = row[0]
        cert_filename = row[1]
        cert_password = row[2]
        smime_info = create_smime_info.create_smime_info(
            cert_filename=cert_filename, cert_password=cert_password
        )
        if smime_info:
          insert_smime_info.insert_smime_info()
        else:
          print(f"Unable to read certificate file for user_id: {user_id}")
        return smime_info

  except (OSError, IOError) as error:
    print(f"An error occured while reading the CSV file: {error}")


if __name__ == "__main__":
  insert_cert_from_csv(csv_filename="xyz")

مدیریت گواهی

این مثال چندین تماس از API smimeInfo را ترکیب می‌کند تا نشان دهد چگونه می‌توانید گواهی‌ها را برای سازمان خود مدیریت کنید. گواهینامه ها را برای کاربر فهرست می کند و اگر گواهی پیش فرض منقضی شده باشد یا تنظیم نشده باشد، گواهی موجود در فایل مشخص شده را آپلود می کند. سپس گواهینامه ای که انقضای آن در آینده دورتر است را به عنوان پیش فرض تنظیم می کند.

سپس از تابعی فراخوانی می شود که یک فایل CSV را مانند مثال قبلی پردازش می کند.

جاوا

gmail/snippets/src/main/java/UpdateSmimeCerts.java
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.GmailScopes;
import com.google.api.services.gmail.model.ListSmimeInfoResponse;
import com.google.api.services.gmail.model.SmimeInfo;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/* Class to demonstrate the use of Gmail Update Smime Certificate API*/
public class UpdateSmimeCerts {
  /**
   * Update S/MIME certificates for the user.
   *
   * <p>First performs a lookup of all certificates for a user. If there are no certificates, or
   * they all expire before the specified date/time, uploads the certificate in the specified file.
   * If the default certificate is expired or there was no default set, chooses the certificate with
   * the expiration furthest into the future and sets it as default.
   *
   * @param userId       User's email address.
   * @param sendAsEmail  The "send as" email address, or None if it should be the same as user_id.
   * @param certFilename Name of the file containing the S/MIME certificate.
   * @param certPassword Password for the certificate file, or None if the file is not
   *                     password-protected.
   * @param expireTime   DateTime object against which the certificate expiration is compared. If
   *                     None, uses the current time. @ returns: The ID of the default certificate.
   * @return The ID of the default certificate, {@code null} otherwise.
   * @throws IOException - if service account credentials file not found.
   */
  public static String updateSmimeCerts(String userId,
                                        String sendAsEmail,
                                        String certFilename,
                                        String certPassword,
                                        LocalDateTime expireTime)
      throws IOException {
        /* Load pre-authorized user credentials from the environment.
           TODO(developer) - See https://developers.google.com/identity for
            guides on implementing OAuth2 for your application. */
    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
        .createScoped(GmailScopes.GMAIL_SETTINGS_SHARING);
    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
        credentials);

    // Create the gmail API client
    Gmail service = new Gmail.Builder(new NetHttpTransport(),
        GsonFactory.getDefaultInstance(),
        requestInitializer)
        .setApplicationName("Gmail samples")
        .build();

    if (sendAsEmail == null) {
      sendAsEmail = userId;
    }

    ListSmimeInfoResponse listResults;
    try {
      listResults =
          service.users().settings().sendAs().smimeInfo().list(userId, sendAsEmail).execute();
    } catch (IOException e) {
      System.err.printf("An error occurred during list: %s\n", e);
      return null;
    }

    String defaultCertId = null;
    String bestCertId = null;
    LocalDateTime bestCertExpire = LocalDateTime.MIN;

    if (expireTime == null) {
      expireTime = LocalDateTime.now();
    }
    if (listResults != null && listResults.getSmimeInfo() != null) {
      for (SmimeInfo smimeInfo : listResults.getSmimeInfo()) {
        String certId = smimeInfo.getId();
        boolean isDefaultCert = smimeInfo.getIsDefault();
        if (isDefaultCert) {
          defaultCertId = certId;
        }
        LocalDateTime exp =
            LocalDateTime.ofInstant(
                Instant.ofEpochMilli(smimeInfo.getExpiration()), ZoneId.systemDefault());
        if (exp.isAfter(expireTime)) {
          if (exp.isAfter(bestCertExpire)) {
            bestCertId = certId;
            bestCertExpire = exp;
          }
        } else {
          if (isDefaultCert) {
            defaultCertId = null;
          }
        }
      }
    }
    if (defaultCertId == null) {
      String defaultId = bestCertId;
      if (defaultId == null && certFilename != null) {
        SmimeInfo smimeInfo = CreateSmimeInfo.createSmimeInfo(certFilename,
            certPassword);
        SmimeInfo insertResults = InsertSmimeInfo.insertSmimeInfo(userId,
            sendAsEmail,
            smimeInfo);
        if (insertResults != null) {
          defaultId = insertResults.getId();
        }
      }

      if (defaultId != null) {
        try {
          service.users().settings().sendAs().smimeInfo().setDefault(userId, sendAsEmail, defaultId)
              .execute();
          return defaultId;
        } catch (IOException e) {
          System.err.printf("An error occured during setDefault: %s", e);
        }
      }
    } else {
      return defaultCertId;
    }

    return null;
  }
}

پایتون

gmail/snippet/smime snippets/update_smime_cert.py
from datetime import datetime

import create_smime_info
import google.auth
import insert_smime_info
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def update_smime_cert(
    user_id, send_as_email, cert_filename, cert_password, expire_dt
):
  """Update S/MIME certificates for the user.

  First performs a lookup of all certificates for a user.  If there are no
  certificates, or they all expire before the specified date/time, uploads the
  certificate in the specified file.  If the default certificate is expired or
  there was no default set, chooses the certificate with the expiration furthest
  into the future and sets it as default.

  Args:
    user_id: User's email address.
    send_as_email: The "send as" email address, or None if it should be the same
        as user_id.
    cert_filename: Name of the file containing the S/MIME certificate.
    cert_password: Password for the certificate file, or None if the file is not
        password-protected.
    expire_dt: DateTime object against which the certificate expiration is
      compared.  If None, uses the current time.

  Returns:
    The ID of the default certificate.

  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """
  if not send_as_email:
    send_as_email = user_id

  creds, _ = google.auth.default()

  try:
    # create gmail api client
    service = build("gmail", "v1", credentials=creds)

    # pylint: disable=maybe-no-member
    results = (
        service.users()
        .settings()
        .sendAs()
        .smimeInfo()
        .list(userId=user_id, sendAsEmail=send_as_email)
        .execute()
    )

  except HttpError as error:
    print(f"An error occurred during list: {error}")
    return None

  default_cert_id = None
  best_cert_id = (None, datetime.datetime.fromtimestamp(0))

  if not expire_dt:
    expire_dt = datetime.datetime.now()
  if results and "smimeInfo" in results:
    for smime_info in results["smimeInfo"]:
      cert_id = smime_info["id"]
      is_default_cert = smime_info["isDefault"]
      if is_default_cert:
        default_cert_id = cert_id
      exp = datetime.datetime.fromtimestamp(smime_info["expiration"] / 1000)
      if exp > expire_dt:
        if exp > best_cert_id[1]:
          best_cert_id = (cert_id, exp)
      else:
        if is_default_cert:
          default_cert_id = None

  if not default_cert_id:
    default_id = best_cert_id[0]
    if not default_id and cert_filename:
      create_smime_info.create_smime_info(
          cert_filename=cert_filename, cert_password=cert_password
      )
      results = insert_smime_info.insert_smime_info()
      if results:
        default_id = results["id"]

    if default_id:
      try:
        # pylint: disable=maybe-no-member
        service.users().settings().sendAs().smimeInfo().setDefault(
            userId=user_id, sendAsEmail=send_as_email, id=default_id
        ).execute()
        return default_id
      except HttpError as error:
        print(f"An error occurred during setDefault: {error}")
  else:
    return default_cert_id

  return None


if __name__ == "__main__":
  update_smime_cert(
      user_id="xyz",
      send_as_email=None,
      cert_filename="xyz",
      cert_password="xyz",
      expire_dt=None,
  )