Const-correct PEM_write functions and i2d_SSL_SESSION

Update-Note: PEM_write functions now all take their input objects by
const pointer.

Fixed: 523893143
Change-Id: I8f3e0d152fc7000b83c13d48dab974ca695d0010
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/97207
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
diff --git a/crypto/pem/internal.h b/crypto/pem/internal.h
index 4fcfce8..117cfa0 100644
--- a/crypto/pem/internal.h
+++ b/crypto/pem/internal.h
@@ -40,45 +40,24 @@
 
 #define IMPLEMENT_PEM_write_fp(name, type, str, asn1)                        \
   static int pem_write_##name##_i2d(const void *x, unsigned char **outp) {   \
-    return i2d_##asn1((type *)x, outp);                                      \
+    return i2d_##asn1((const type *)x, outp);                                \
   }                                                                          \
-  OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x) {                   \
+  OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x) {             \
     return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, NULL, NULL, 0, \
                           NULL, NULL);                                       \
   }
 
-#define IMPLEMENT_PEM_write_fp_const(name, type, str, asn1)                 \
-  static int pem_write_##name##_i2d(const void *x, unsigned char **outp) {  \
-    return i2d_##asn1((const type *)x, outp);                               \
-  }                                                                         \
-  OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x) {            \
-    return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, (void *)x, NULL, \
-                          NULL, 0, NULL, NULL);                             \
+#define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)                       \
+  static int pem_write_##name##_i2d(const void *x, unsigned char **outp) {     \
+    return i2d_##asn1((const type *)x, outp);                                  \
+  }                                                                            \
+  OPENSSL_EXPORT int PEM_write_##name(                                         \
+      FILE *fp, const type *x, const EVP_CIPHER *enc,                          \
+      const unsigned char *pass, int pass_len, pem_password_cb *cb, void *u) { \
+    return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, pass,       \
+                          pass_len, cb, u);                                    \
   }
 
-#define IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)                   \
-  static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \
-    return i2d_##asn1((type *)x, outp);                                    \
-  }                                                                        \
-  OPENSSL_EXPORT int PEM_write_##name(                                     \
-      FILE *fp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \
-      int pass_len, pem_password_cb *cb, void *u) {                        \
-    return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, pass,   \
-                          pass_len, cb, u);                                \
-  }
-
-#define IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1)             \
-  static int pem_write_##name##_i2d(const void *x, unsigned char **outp) { \
-    return i2d_##asn1((const type *)x, outp);                              \
-  }                                                                        \
-  OPENSSL_EXPORT int PEM_write_##name(                                     \
-      FILE *fp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \
-      int pass_len, pem_password_cb *cb, void *u) {                        \
-    return PEM_ASN1_write(pem_write_##name##_i2d, str, fp, x, enc, pass,   \
-                          pass_len, cb, u);                                \
-  }
-
-
 #define IMPLEMENT_PEM_read_bio(name, type, str, asn1)                         \
   static void *pem_read_bio_##name##_d2i(void **x, const unsigned char **inp, \
                                          long len) {                          \
@@ -92,60 +71,32 @@
 
 #define IMPLEMENT_PEM_write_bio(name, type, str, asn1)                         \
   static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
-    return i2d_##asn1((type *)x, outp);                                        \
+    return i2d_##asn1((const type *)x, outp);                                  \
   }                                                                            \
-  OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x) {                  \
+  OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x) {            \
     return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, NULL,    \
                               NULL, 0, NULL, NULL);                            \
   }
 
-#define IMPLEMENT_PEM_write_bio_const(name, type, str, asn1)                   \
-  static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
-    return i2d_##asn1((const type *)x, outp);                                  \
-  }                                                                            \
-  OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x) {            \
-    return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x,  \
-                              NULL, NULL, 0, NULL, NULL);                      \
-  }
-
 #define IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1)                      \
   static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
-    return i2d_##asn1((type *)x, outp);                                        \
-  }                                                                            \
-  OPENSSL_EXPORT int PEM_write_bio_##name(                                     \
-      BIO *bp, type *x, const EVP_CIPHER *enc, const unsigned char *pass,      \
-      int pass_len, pem_password_cb *cb, void *u) {                            \
-    return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, enc,     \
-                              pass, pass_len, cb, u);                          \
-  }
-
-#define IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1)                \
-  static int pem_write_bio_##name##_i2d(const void *x, unsigned char **outp) { \
     return i2d_##asn1((const type *)x, outp);                                  \
   }                                                                            \
   OPENSSL_EXPORT int PEM_write_bio_##name(                                     \
-      BIO *bp, type *x, const EVP_CIPHER *enc, const unsigned char *pass,      \
-      int pass_len, pem_password_cb *cb, void *u) {                            \
-    return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, (void *)x,  \
-                              enc, pass, pass_len, cb, u);                     \
+      BIO *bp, const type *x, const EVP_CIPHER *enc,                           \
+      const unsigned char *pass, int pass_len, pem_password_cb *cb, void *u) { \
+    return PEM_ASN1_write_bio(pem_write_bio_##name##_i2d, str, bp, x, enc,     \
+                              pass, pass_len, cb, u);                          \
   }
 
 #define IMPLEMENT_PEM_write(name, type, str, asn1) \
   IMPLEMENT_PEM_write_bio(name, type, str, asn1)   \
   IMPLEMENT_PEM_write_fp(name, type, str, asn1)
 
-#define IMPLEMENT_PEM_write_const(name, type, str, asn1) \
-  IMPLEMENT_PEM_write_bio_const(name, type, str, asn1)   \
-  IMPLEMENT_PEM_write_fp_const(name, type, str, asn1)
-
 #define IMPLEMENT_PEM_write_cb(name, type, str, asn1) \
   IMPLEMENT_PEM_write_cb_bio(name, type, str, asn1)   \
   IMPLEMENT_PEM_write_cb_fp(name, type, str, asn1)
 
-#define IMPLEMENT_PEM_write_cb_const(name, type, str, asn1) \
-  IMPLEMENT_PEM_write_cb_bio_const(name, type, str, asn1)   \
-  IMPLEMENT_PEM_write_cb_fp_const(name, type, str, asn1)
-
 #define IMPLEMENT_PEM_read(name, type, str, asn1) \
   IMPLEMENT_PEM_read_bio(name, type, str, asn1)   \
   IMPLEMENT_PEM_read_fp(name, type, str, asn1)
@@ -154,10 +105,6 @@
   IMPLEMENT_PEM_read(name, type, str, asn1)     \
   IMPLEMENT_PEM_write(name, type, str, asn1)
 
-#define IMPLEMENT_PEM_rw_const(name, type, str, asn1) \
-  IMPLEMENT_PEM_read(name, type, str, asn1)           \
-  IMPLEMENT_PEM_write_const(name, type, str, asn1)
-
 #define IMPLEMENT_PEM_rw_cb(name, type, str, asn1) \
   IMPLEMENT_PEM_read(name, type, str, asn1)        \
   IMPLEMENT_PEM_write_cb(name, type, str, asn1)
diff --git a/crypto/pem/pem_all.cc b/crypto/pem/pem_all.cc
index 26eb973..550923c 100644
--- a/crypto/pem/pem_all.cc
+++ b/crypto/pem/pem_all.cc
@@ -71,10 +71,10 @@
   return pkey_get_rsa(pkey.get(), rsa);
 }
 
-IMPLEMENT_PEM_write_cb_const(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey)
+IMPLEMENT_PEM_write_cb(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey)
 
 
-IMPLEMENT_PEM_rw_const(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey)
+IMPLEMENT_PEM_rw(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey)
 IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY)
 #ifndef OPENSSL_NO_DSA
 static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa) {
@@ -101,7 +101,7 @@
   return pkey_get_dsa(pktmp, dsa);  // will free pktmp
 }
 
-IMPLEMENT_PEM_write_cb_const(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey)
+IMPLEMENT_PEM_write_cb(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey)
 
 IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY)
 DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, void *u) {
@@ -110,7 +110,7 @@
   return pkey_get_dsa(pktmp, dsa);  // will free pktmp
 }
 
-IMPLEMENT_PEM_rw_const(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
+IMPLEMENT_PEM_rw(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams)
 #endif
 static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey) {
   EC_KEY *dtmp;
@@ -148,6 +148,6 @@
 }
 
 
-IMPLEMENT_PEM_rw_const(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
+IMPLEMENT_PEM_rw(DHparams, DH, PEM_STRING_DHPARAMS, DHparams)
 
 IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY)
diff --git a/crypto/pem/pem_lib.cc b/crypto/pem/pem_lib.cc
index 7d34c70..f59c42b 100644
--- a/crypto/pem/pem_lib.cc
+++ b/crypto/pem/pem_lib.cc
@@ -221,7 +221,7 @@
   return 1;
 }
 
-int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, void *x,
+int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, const void *x,
                    const EVP_CIPHER *enc, const unsigned char *pass,
                    int pass_len, pem_password_cb *callback, void *u) {
   BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
@@ -235,9 +235,10 @@
   return ret;
 }
 
-int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, void *x,
-                       const EVP_CIPHER *enc, const unsigned char *pass,
-                       int pass_len, pem_password_cb *callback, void *u) {
+int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp,
+                       const void *x, const EVP_CIPHER *enc,
+                       const unsigned char *pass, int pass_len,
+                       pem_password_cb *callback, void *u) {
   ScopedEVP_CIPHER_CTX ctx;
   int dsize = 0, ret = 0;
   size_t i, j, data_size;
diff --git a/crypto/pem/pem_pkey.cc b/crypto/pem/pem_pkey.cc
index 433215e..9a45d39 100644
--- a/crypto/pem/pem_pkey.cc
+++ b/crypto/pem/pem_pkey.cc
@@ -108,7 +108,7 @@
   return ret;
 }
 
-int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
+int PEM_write_bio_PrivateKey(BIO *bp, const EVP_PKEY *x, const EVP_CIPHER *enc,
                              const unsigned char *pass, int pass_len,
                              pem_password_cb *cb, void *u) {
   return PEM_write_bio_PKCS8PrivateKey(bp, x, enc, (const char *)pass, pass_len,
@@ -127,7 +127,7 @@
   return ret;
 }
 
-int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
+int PEM_write_PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
                          const unsigned char *pass, int pass_len,
                          pem_password_cb *cb, void *u) {
   BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
diff --git a/include/openssl/pem.h b/include/openssl/pem.h
index 8229ae2..3f36013 100644
--- a/include/openssl/pem.h
+++ b/include/openssl/pem.h
@@ -78,40 +78,29 @@
                                        pem_password_cb *cb, void *u);
 
 #define DECLARE_PEM_write_fp(name, type) \
-  OPENSSL_EXPORT int PEM_write_##name(FILE *fp, type *x);
-
-#define DECLARE_PEM_write_fp_const(name, type) \
   OPENSSL_EXPORT int PEM_write_##name(FILE *fp, const type *x);
 
-#define DECLARE_PEM_write_cb_fp(name, type)                                \
-  OPENSSL_EXPORT int PEM_write_##name(                                     \
-      FILE *fp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \
-      int pass_len, pem_password_cb *cb, void *u);
+#define DECLARE_PEM_write_cb_fp(name, type)           \
+  OPENSSL_EXPORT int PEM_write_##name(                \
+      FILE *fp, const type *x, const EVP_CIPHER *enc, \
+      const unsigned char *pass, int pass_len, pem_password_cb *cb, void *u);
 
 #define DECLARE_PEM_read_bio(name, type)                      \
   OPENSSL_EXPORT type *PEM_read_bio_##name(BIO *bp, type **x, \
                                            pem_password_cb *cb, void *u);
 
 #define DECLARE_PEM_write_bio(name, type) \
-  OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, type *x);
-
-#define DECLARE_PEM_write_bio_const(name, type) \
   OPENSSL_EXPORT int PEM_write_bio_##name(BIO *bp, const type *x);
 
-#define DECLARE_PEM_write_cb_bio(name, type)                              \
-  OPENSSL_EXPORT int PEM_write_bio_##name(                                \
-      BIO *bp, type *x, const EVP_CIPHER *enc, const unsigned char *pass, \
-      int pass_len, pem_password_cb *cb, void *u);
-
+#define DECLARE_PEM_write_cb_bio(name, type)         \
+  OPENSSL_EXPORT int PEM_write_bio_##name(           \
+      BIO *bp, const type *x, const EVP_CIPHER *enc, \
+      const unsigned char *pass, int pass_len, pem_password_cb *cb, void *u);
 
 #define DECLARE_PEM_write(name, type) \
   DECLARE_PEM_write_bio(name, type)   \
   DECLARE_PEM_write_fp(name, type)
 
-#define DECLARE_PEM_write_const(name, type) \
-  DECLARE_PEM_write_bio_const(name, type)   \
-  DECLARE_PEM_write_fp_const(name, type)
-
 #define DECLARE_PEM_write_cb(name, type) \
   DECLARE_PEM_write_cb_bio(name, type)   \
   DECLARE_PEM_write_cb_fp(name, type)
@@ -124,10 +113,6 @@
   DECLARE_PEM_read(name, type)     \
   DECLARE_PEM_write(name, type)
 
-#define DECLARE_PEM_rw_const(name, type) \
-  DECLARE_PEM_read(name, type)           \
-  DECLARE_PEM_write_const(name, type)
-
 #define DECLARE_PEM_rw_cb(name, type) \
   DECLARE_PEM_read(name, type)        \
   DECLARE_PEM_write_cb(name, type)
@@ -161,7 +146,8 @@
                                        BIO *bp, void **x, pem_password_cb *cb,
                                        void *u);
 OPENSSL_EXPORT int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name,
-                                      BIO *bp, void *x, const EVP_CIPHER *enc,
+                                      BIO *bp, const void *x,
+                                      const EVP_CIPHER *enc,
                                       const unsigned char *pass, int pass_len,
                                       pem_password_cb *cb, void *u);
 
@@ -206,7 +192,7 @@
 OPENSSL_EXPORT void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp,
                                    void **x, pem_password_cb *cb, void *u);
 OPENSSL_EXPORT int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp,
-                                  void *x, const EVP_CIPHER *enc,
+                                  const void *x, const EVP_CIPHER *enc,
                                   const unsigned char *pass, int pass_len,
                                   pem_password_cb *callback, void *u);
 
@@ -237,7 +223,7 @@
 
 DECLARE_PEM_rw_cb(RSAPrivateKey, RSA)
 
-DECLARE_PEM_rw_const(RSAPublicKey, RSA)
+DECLARE_PEM_rw(RSAPublicKey, RSA)
 DECLARE_PEM_rw(RSA_PUBKEY, RSA)
 
 #ifndef OPENSSL_NO_DSA
@@ -246,7 +232,7 @@
 
 DECLARE_PEM_rw(DSA_PUBKEY, DSA)
 
-DECLARE_PEM_rw_const(DSAparams, DSA)
+DECLARE_PEM_rw(DSAparams, DSA)
 
 #endif
 
@@ -254,7 +240,7 @@
 DECLARE_PEM_rw(EC_PUBKEY, EC_KEY)
 
 
-DECLARE_PEM_rw_const(DHparams, DH)
+DECLARE_PEM_rw(DHparams, DH)
 
 
 DECLARE_PEM_rw_cb(PrivateKey, EVP_PKEY)
diff --git a/include/openssl/ssl.h b/include/openssl/ssl.h
index fca797c..b73232f 100644
--- a/include/openssl/ssl.h
+++ b/include/openssl/ssl.h
@@ -5738,7 +5738,7 @@
 // i2d_SSL_SESSION serializes `in`, as described in `i2d_SAMPLE`.
 //
 // Use `SSL_SESSION_to_bytes` instead.
-OPENSSL_EXPORT int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp);
+OPENSSL_EXPORT int i2d_SSL_SESSION(const SSL_SESSION *in, uint8_t **pp);
 
 // d2i_SSL_SESSION parses a serialized session from the `len` bytes pointed to
 // by `*inp`, as described in `d2i_SAMPLE`.
diff --git a/ssl/ssl_asn1.cc b/ssl/ssl_asn1.cc
index 6f0c374..cdf763a 100644
--- a/ssl/ssl_asn1.cc
+++ b/ssl/ssl_asn1.cc
@@ -824,7 +824,7 @@
   return 1;
 }
 
-int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
+int i2d_SSL_SESSION(const SSL_SESSION *in, uint8_t **pp) {
   ScopedCBB cbb;
   if (!CBB_init(cbb.get(), 256) ||
       !SSL_SESSION_to_bytes_if_not_resumable(in, cbb.get(), 0)) {