summaryrefslogtreecommitdiffstats
path: root/crypto/pem
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/pem')
-rw-r--r--crypto/pem/Makefile.ssl96
-rw-r--r--crypto/pem/ctx_size.c122
-rw-r--r--crypto/pem/message16
-rw-r--r--crypto/pem/pem.err38
-rw-r--r--crypto/pem/pem.h562
-rw-r--r--crypto/pem/pem.org562
-rw-r--r--crypto/pem/pem_all.c487
-rw-r--r--crypto/pem/pem_err.c116
-rw-r--r--crypto/pem/pem_info.c365
-rw-r--r--crypto/pem/pem_lib.c757
-rw-r--r--crypto/pem/pem_seal.c191
-rw-r--r--crypto/pem/pem_sign.c109
-rw-r--r--crypto/pem/pkcs7.lis22
13 files changed, 3443 insertions, 0 deletions
diff --git a/crypto/pem/Makefile.ssl b/crypto/pem/Makefile.ssl
new file mode 100644
index 0000000000..447fcb4c0d
--- /dev/null
+++ b/crypto/pem/Makefile.ssl
@@ -0,0 +1,96 @@
+#
+# SSLeay/crypto/pem/Makefile
+#
+
+DIR= pem
+TOP= ../..
+CC= cc
+INCLUDES= -I.. -I../../include
+CFLAG=-g
+INSTALLTOP=/usr/local/ssl
+MAKE= make -f Makefile.ssl
+MAKEDEPEND= makedepend -f Makefile.ssl
+MAKEFILE= Makefile.ssl
+AR= ar r
+
+CFLAGS= $(INCLUDES) $(CFLAG)
+
+ERR=pem
+ERRC=pem_err
+GENERAL=Makefile
+TEST=
+APPS=
+
+CTX_SIZE= ctx_size
+
+LIB=$(TOP)/libcrypto.a
+LIBSRC= pem_sign.c pem_seal.c pem_info.c pem_lib.c pem_all.c $(ERRC).c
+
+LIBOBJ= pem_sign.o pem_seal.o pem_info.o pem_lib.o pem_all.o $(ERRC).o
+
+SRC= $(LIBSRC)
+
+EXHEADER= pem.h
+HEADER= $(EXHEADER)
+
+ALL= $(GENERAL) $(SRC) $(HEADER)
+
+top:
+ (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
+
+all: pem.h lib
+
+pem.h: $(CTX_SIZE)
+ ./$(CTX_SIZE) <pem.org >pem.new
+ if [ -f pem.h ]; then mv -f pem.h pem.old; fi
+ mv -f pem.new pem.h
+
+$(CTX_SIZE): $(CTX_SIZE).o
+ $(CC) $(CFLAGS) -o $(CTX_SIZE) $(CTX_SIZE).o
+
+lib: $(LIBOBJ)
+ $(AR) $(LIB) $(LIBOBJ)
+ sh $(TOP)/util/ranlib.sh $(LIB)
+ @touch lib
+
+files:
+ perl $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
+
+links:
+ /bin/rm -f Makefile
+ $(TOP)/util/point.sh Makefile.ssl Makefile ;
+ $(TOP)/util/mklink.sh ../../include $(EXHEADER)
+ $(TOP)/util/mklink.sh ../../test $(TEST)
+ $(TOP)/util/mklink.sh ../../apps $(APPS)
+
+install:
+ @for i in $(EXHEADER) ; \
+ do \
+ (cp $$i $(INSTALLTOP)/include/$$i; \
+ chmod 644 $(INSTALLTOP)/include/$$i ); \
+ done;
+
+tags:
+ ctags $(SRC)
+
+tests:
+
+lint:
+ lint -DLINT $(INCLUDES) $(SRC)>fluff
+
+depend:
+ $(MAKEDEPEND) $(INCLUDES) $(CTX_SIZE).c $(LIBSRC)
+
+dclean:
+ perl -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
+ mv -f Makefile.new $(MAKEFILE)
+
+clean:
+ /bin/rm -f $(CTX_SIZE) *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
+
+errors:
+ perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).org # SPECIAL CASE .org
+ perl $(TOP)/util/err-ins.pl $(ERR).err $(ERR).h
+ perl ../err/err_genc.pl $(ERR).h $(ERRC).c
+
+# DO NOT DELETE THIS LINE -- make depend depends on it.
diff --git a/crypto/pem/ctx_size.c b/crypto/pem/ctx_size.c
new file mode 100644
index 0000000000..c9e2ce6d0d
--- /dev/null
+++ b/crypto/pem/ctx_size.c
@@ -0,0 +1,122 @@
+/* crypto/pem/ctx_size.c */
+/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+ * All rights reserved.
+ *
+ * This package is an SSL implementation written
+ * by Eric Young (eay@cryptsoft.com).
+ * The implementation was written so as to conform with Netscapes SSL.
+ *
+ * This library is free for commercial and non-commercial use as long as
+ * the following conditions are aheared to. The following conditions
+ * apply to all code found in this distribution, be it the RC4, RSA,
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
+ * included with this distribution is covered by the same copyright terms
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
+ *
+ * Copyright remains Eric Young's, and as such any Copyright notices in
+ * the code are not to be removed.
+ * If this package is used in a product, Eric Young should be given attribution
+ * as the author of the parts of the library used.
+ * This can be in the form of a textual message at program startup or
+ * in documentation (online or textual) provided with the package.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * "This product includes cryptographic software written by
+ * Eric Young (eay@cryptsoft.com)"
+ * The word 'cryptographic' can be left out if the rouines from the library
+ * being used are not cryptographic related :-).
+ * 4. If you include any Windows specific code (or a derivative thereof) from
+ * the apps directory (application code) you must include an acknowledgement:
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * The licence and distribution terms for any publically available version or
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
+ * copied and put under another distribution licence
+ * [including the GNU Public Licence.]
+ */
+
+#include <stdio.h>
+#include "cryptlib.h"
+#include "evp.h"
+
+int main(argc,argv)
+int argc;
+char *argv[];
+ {
+ int i,j;
+ char buf[256];
+
+ for (;;)
+ {
+ char *str;
+ buf[0]='\0';
+ fgets(buf,256,stdin);
+ if (buf[0] == '\0') break;
+
+ str="#define EVP_ENCODE_CTX_SIZE ";
+ if (strncmp(buf,str,strlen(str)) == 0)
+ {
+ printf("%s %d\n",str,(int)sizeof(EVP_ENCODE_CTX));
+ continue;
+ }
+ str="#define EVP_MD_SIZE ";
+ if (strncmp(buf,str,strlen(str)) == 0)
+ {
+ printf("%s %d\n",str,(int)sizeof(EVP_MD));
+ continue;
+ }
+ str="#define EVP_MD_CTX_SIZE ";
+ if (strncmp(buf,str,strlen(str)) == 0)
+ {
+ printf("%s %d\n",str,(int)sizeof(EVP_MD_CTX));
+ continue;
+ }
+ str="#define EVP_CIPHER_SIZE ";
+ if (strncmp(buf,str,strlen(str)) == 0)
+ {
+ printf("%s %d\n",str,(int)sizeof(EVP_CIPHER));
+ continue;
+ }
+ str="#define EVP_CIPHER_CTX_SIZE ";
+ if (strncmp(buf,str,strlen(str)) == 0)
+ {
+ printf("%s %d\n",str,(int)sizeof(EVP_CIPHER_CTX));
+ continue;
+ }
+
+ str="#define EVP_MAX_MD_SIZE ";
+ if (strncmp(buf,str,strlen(str)) == 0)
+ {
+ i=MD2_DIGEST_LENGTH;
+ j=MD5_DIGEST_LENGTH;
+ if (j > i) i=j;
+ j=SHA_DIGEST_LENGTH;
+ if (j > i) i=j;
+ printf("%s %d\n",str,i);
+ continue;
+ }
+ fputs(buf,stdout);
+ }
+ exit(0);
+ }
diff --git a/crypto/pem/message b/crypto/pem/message
new file mode 100644
index 0000000000..e8bf9d7592
--- /dev/null
+++ b/crypto/pem/message
@@ -0,0 +1,16 @@
+-----BEGIN PRIVACY-ENHANCED MESSAGE-----
+Proc-Type: 4,ENCRYPTED
+Proc-Type: 4,MIC-ONLY
+Proc-Type: 4,MIC-CLEAR
+Content-Domain: RFC822
+DEK-Info: DES-CBC,0123456789abcdef
+Originator-Certificate
+ xxxx
+Issuer-Certificate
+ xxxx
+MIC-Info: RSA-MD5,RSA,
+ xxxx
+
+
+-----END PRIVACY-ENHANCED MESSAGE-----
+
diff --git a/crypto/pem/pem.err b/crypto/pem/pem.err
new file mode 100644
index 0000000000..0e523abf82
--- /dev/null
+++ b/crypto/pem/pem.err
@@ -0,0 +1,38 @@
+/* Error codes for the PEM functions. */
+
+/* Function codes. */
+#define PEM_F_DEF_CALLBACK 100
+#define PEM_F_LOAD_IV 101
+#define PEM_F_PEM_ASN1_READ 102
+#define PEM_F_PEM_ASN1_READ_BIO 103
+#define PEM_F_PEM_ASN1_WRITE 104
+#define PEM_F_PEM_ASN1_WRITE_BIO 105
+#define PEM_F_PEM_DO_HEADER 106
+#define PEM_F_PEM_GET_EVP_CIPHER_INFO 107
+#define PEM_F_PEM_READ 108
+#define PEM_F_PEM_READ_BIO 109
+#define PEM_F_PEM_SEALFINAL 110
+#define PEM_F_PEM_SEALINIT 111
+#define PEM_F_PEM_SIGNFINAL 112
+#define PEM_F_PEM_WRITE 113
+#define PEM_F_PEM_WRITE_BIO 114
+#define PEM_F_PEM_X509_INFO_READ 115
+#define PEM_F_PEM_X509_INFO_READ_BIO 116
+#define PEM_F_PEM_X509_INFO_WRITE_BIO 117
+
+/* Reason codes. */
+#define PEM_R_BAD_BASE64_DECODE 100
+#define PEM_R_BAD_DECRYPT 101
+#define PEM_R_BAD_END_LINE 102
+#define PEM_R_BAD_IV_CHARS 103
+#define PEM_R_BAD_PASSWORD_READ 104
+#define PEM_R_NOT_DEK_INFO 105
+#define PEM_R_NOT_ENCRYPTED 106
+#define PEM_R_NOT_PROC_TYPE 107
+#define PEM_R_NO_START_LINE 108
+#define PEM_R_PROBLEMS_GETTING_PASSWORD 109
+#define PEM_R_PUBLIC_KEY_NO_RSA 110
+#define PEM_R_READ_KEY 111
+#define PEM_R_SHORT_HEADER 112
+#define PEM_R_UNSUPPORTED_CIPHER 113
+#define PEM_R_UNSUPPORTED_ENCRYPTION 114
diff --git a/crypto/pem/pem.h b/crypto/pem/pem.h
new file mode 100644
index 0000000000..38952509dd
--- /dev/null
+++ b/crypto/pem/pem.h
@@ -0,0 +1,562 @@
+/* crypto/pem/pem.org */
+/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+ * All rights reserved.
+ *
+ * This package is an SSL implementation written
+ * by Eric Young (eay@cryptsoft.com).
+ * The implementation was written so as to conform with Netscapes SSL.
+ *
+ * This library is free for commercial and non-commercial use as long as
+ * the following conditions are aheared to. The following conditions
+ * apply to all code found in this distribution, be it the RC4, RSA,
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
+ * included with this distribution is covered by the same copyright terms
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
+ *
+ * Copyright remains Eric Young's, and as such any Copyright notices in
+ * the code are not to be removed.
+ * If this package is used in a product, Eric Young should be given attribution
+ * as the author of the parts of the library used.
+ * This can be in the form of a textual message at program startup or
+ * in documentation (online or textual) provided with the package.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * "This product includes cryptographic software written by
+ * Eric Young (eay@cryptsoft.com)"
+ * The word 'cryptographic' can be left out if the rouines from the library
+ * being used are not cryptographic related :-).
+ * 4. If you include any Windows specific code (or a derivative thereof) from
+ * the apps directory (application code) you must include an acknowledgement:
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * The licence and distribution terms for any publically available version or
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
+ * copied and put under another distribution licence
+ * [including the GNU Public Licence.]
+ */
+
+/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
+ *
+ * Always modify pem.org since pem.h is automatically generated from
+ * it during SSLeay configuration.
+ *
+ * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
+ */
+
+#ifndef HEADER_PEM_H
+#define HEADER_PEM_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "evp.h"
+#include "x509.h"
+
+#define PEM_OBJ_UNDEF 0
+#define PEM_OBJ_X509 1
+#define PEM_OBJ_X509_REQ 2
+#define PEM_OBJ_CRL 3
+#define PEM_OBJ_SSL_SESSION 4
+#define PEM_OBJ_PRIV_KEY 10
+#define PEM_OBJ_PRIV_RSA 11
+#define PEM_OBJ_PRIV_DSA 12
+#define PEM_OBJ_PRIV_DH 13
+#define PEM_OBJ_PUB_RSA 14
+#define PEM_OBJ_PUB_DSA 15
+#define PEM_OBJ_PUB_DH 16
+#define PEM_OBJ_DHPARAMS 17
+#define PEM_OBJ_DSAPARAMS 18
+#define PEM_OBJ_PRIV_RSA_PUBLIC 19
+
+#define PEM_ERROR 30
+#define PEM_DEK_DES_CBC 40
+#define PEM_DEK_IDEA_CBC 45
+#define PEM_DEK_DES_EDE 50
+#define PEM_DEK_DES_ECB 60
+#define PEM_DEK_RSA 70
+#define PEM_DEK_RSA_MD2 80
+#define PEM_DEK_RSA_MD5 90
+
+#define PEM_MD_MD2 NID_md2
+#define PEM_MD_MD5 NID_md5
+#define PEM_MD_SHA NID_sha
+#define PEM_MD_MD2_RSA NID_md2WithRSAEncryption
+#define PEM_MD_MD5_RSA NID_md5WithRSAEncryption
+#define PEM_MD_SHA_RSA NID_sha1WithRSAEncryption
+
+#define PEM_STRING_X509_OLD "X509 CERTIFICATE"
+#define PEM_STRING_X509 "CERTIFICATE"
+#define PEM_STRING_X509_REQ_OLD "NEW CERTIFICATE REQUEST"
+#define PEM_STRING_X509_REQ "CERTIFICATE REQUEST"
+#define PEM_STRING_X509_CRL "X509 CRL"
+#define PEM_STRING_EVP_PKEY "PRIVATE KEY"
+#define PEM_STRING_RSA "RSA PRIVATE KEY"
+#define PEM_STRING_RSA_PUBLIC "RSA PUBLIC KEY"
+#define PEM_STRING_DSA "DSA PRIVATE KEY"
+#define PEM_STRING_PKCS7 "PKCS7"
+#define PEM_STRING_DHPARAMS "DH PARAMETERS"
+#define PEM_STRING_SSL_SESSION "SSL SESSION PARAMETERS"
+#define PEM_STRING_DSAPARAMS "DSA PARAMETERS"
+
+#ifndef HEADER_ENVELOPE_H
+
+#define EVP_ENCODE_CTX_SIZE 92
+#define EVP_MD_SIZE 48
+#define EVP_MD_CTX_SIZE 152
+#define EVP_CIPHER_SIZE 28
+#define EVP_CIPHER_CTX_SIZE 4212
+#define EVP_MAX_MD_SIZE 20
+
+typedef struct evp_encode_ctx_st
+ {
+ char data[EVP_ENCODE_CTX_SIZE];
+ } EVP_ENCODE_CTX;
+
+typedef struct env_md_ctx_st
+ {
+ char data[EVP_MD_CTX_SIZE];
+ } EVP_MD_CTX;
+
+typedef struct evp_cipher_st
+ {
+ char data[EVP_CIPHER_SIZE];
+ } EVP_CIPHER;
+
+typedef struct evp_cipher_ctx_st
+ {
+ char data[EVP_CIPHER_CTX_SIZE];
+ } EVP_CIPHER_CTX;
+#endif
+
+
+typedef struct PEM_Encode_Seal_st
+ {
+ EVP_ENCODE_CTX encode;
+ EVP_MD_CTX md;
+ EVP_CIPHER_CTX cipher;
+ } PEM_ENCODE_SEAL_CTX;
+
+/* enc_type is one off */
+#define PEM_TYPE_ENCRYPTED 10
+#define PEM_TYPE_MIC_ONLY 20
+#define PEM_TYPE_MIC_CLEAR 30
+#define PEM_TYPE_CLEAR 40
+
+typedef struct pem_recip_st
+ {
+ char *name;
+ X509_NAME *dn;
+
+ int cipher;
+ int key_enc;
+ char iv[8];
+ } PEM_USER;
+
+typedef struct pem_ctx_st
+ {
+ int type; /* what type of object */
+
+ struct {
+ int version;
+ int mode;
+ } proc_type;
+
+ char *domain;
+
+ struct {
+ int cipher;
+ unsigned char iv[8];
+ } DEK_info;
+
+ PEM_USER *originator;
+
+ int num_recipient;
+ PEM_USER **recipient;
+
+#ifdef HEADER_STACK_H
+ STACK *x509_chain; /* certificate chain */
+#else
+ char *x509_chain; /* certificate chain */
+#endif
+ EVP_MD *md; /* signature type */
+
+ int md_enc; /* is the md encrypted or not? */
+ int md_len; /* length of md_data */
+ char *md_data; /* message digest, could be pkey encrypted */
+
+ EVP_CIPHER *dec; /* date encryption cipher */
+ int key_len; /* key length */
+ unsigned char *key; /* key */
+ unsigned char iv[8]; /* the iv */
+
+
+ int data_enc; /* is the data encrypted */
+ int data_len;
+ unsigned char *data;
+ } PEM_CTX;
+
+#ifdef SSLEAY_MACROS
+
+#define PEM_write_SSL_SESSION(fp,x) \
+ PEM_ASN1_write((int (*)())i2d_SSL_SESSION, \
+ PEM_STRING_SSL_SESSION,fp, (char *)x, NULL,NULL,0,NULL)
+#define PEM_write_X509(fp,x) \
+ PEM_ASN1_write((int (*)())i2d_X509,PEM_STRING_X509,fp, \
+ (char *)x, NULL,NULL,0,NULL)
+#define PEM_write_X509_REQ(fp,x) PEM_ASN1_write( \
+ (int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,fp,(char *)x, \
+ NULL,NULL,0,NULL)
+#define PEM_write_X509_CRL(fp,x) \
+ PEM_ASN1_write((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL, \
+ fp,(char *)x, NULL,NULL,0,NULL)
+#define PEM_write_RSAPrivateKey(fp,x,enc,kstr,klen,cb) \
+ PEM_ASN1_write((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,fp,\
+ (char *)x,enc,kstr,klen,cb)
+#define PEM_write_RSAPublicKey(fp,x) \
+ PEM_ASN1_write((int (*)())i2d_RSAPublicKey,\
+ PEM_STRING_RSA_PUBLIC,fp,(char *)x,NULL,NULL,0,NULL)
+#define PEM_write_DSAPrivateKey(fp,x,enc,kstr,klen,cb) \
+ PEM_ASN1_write((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,fp,\
+ (char *)x,enc,kstr,klen,cb)
+#define PEM_write_PrivateKey(bp,x,enc,kstr,klen,cb) \
+ PEM_ASN1_write((int (*)())i2d_PrivateKey,\
+ (((x)->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA),\
+ bp,(char *)x,enc,kstr,klen,cb)
+#define PEM_write_PKCS7(fp,x) \
+ PEM_ASN1_write((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,fp, \
+ (char *)x, NULL,NULL,0,NULL)
+#define PEM_write_DHparams(fp,x) \
+ PEM_ASN1_write((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,fp,\
+ (char *)x,NULL,NULL,0,NULL)
+
+#define PEM_read_SSL_SESSION(fp,x,cb) (SSL_SESSION *)PEM_ASN1_read( \
+ (char *(*)())d2i_SSL_SESSION,PEM_STRING_SSL_SESSION,fp,(char **)x,cb)
+#define PEM_read_X509(fp,x,cb) (X509 *)PEM_ASN1_read( \
+ (char *(*)())d2i_X509,PEM_STRING_X509,fp,(char **)x,cb)
+#define PEM_read_X509_REQ(fp,x,cb) (X509_REQ *)PEM_ASN1_read( \
+ (char *(*)())d2i_X509_REQ,PEM_STRING_X509_REQ,fp,(char **)x,cb)
+#define PEM_read_X509_CRL(fp,x,cb) (X509_CRL *)PEM_ASN1_read( \
+ (char *(*)())d2i_X509_CRL,PEM_STRING_X509_CRL,fp,(char **)x,cb)
+#define PEM_read_RSAPrivateKey(fp,x,cb) (RSA *)PEM_ASN1_read( \
+ (char *(*)())d2i_RSAPrivateKey,PEM_STRING_RSA,fp,(char **)x,cb)
+#define PEM_read_RSAPublicKey(fp,x,cb) (RSA *)PEM_ASN1_read( \
+ (char *(*)())d2i_RSAPublicKey,PEM_STRING_RSA_PUBLIC,fp,(char **)x,cb)
+#define PEM_read_DSAPrivateKey(fp,x,cb) (DSA *)PEM_ASN1_read( \
+ (char *(*)())d2i_DSAPrivateKey,PEM_STRING_DSA,fp,(char **)x,cb)
+#define PEM_read_PrivateKey(fp,x,cb) (EVP_PKEY *)PEM_ASN1_read( \
+ (char *(*)())d2i_PrivateKey,PEM_STRING_EVP_PKEY,fp,(char **)x,cb)
+#define PEM_read_PKCS7(fp,x,cb) (PKCS7 *)PEM_ASN1_read( \
+ (char *(*)())d2i_PKCS7,PEM_STRING_PKCS7,fp,(char **)x,cb)
+#define PEM_read_DHparams(fp,x,cb) (DH *)PEM_ASN1_read( \
+ (char *(*)())d2i_DHparams,PEM_STRING_DHPARAMS,fp,(char **)x,cb)
+
+#define PEM_write_bio_SSL_SESSION(bp,x) \
+ PEM_ASN1_write_bio((int (*)())i2d_SSL_SESSION, \
+ PEM_STRING_SSL_SESSION,bp, (char *)x, NULL,NULL,0,NULL)
+#define PEM_write_bio_X509(bp,x) \
+ PEM_ASN1_write_bio((int (*)())i2d_X509,PEM_STRING_X509,bp, \
+ (char *)x, NULL,NULL,0,NULL)
+#define PEM_write_bio_X509_REQ(bp,x) PEM_ASN1_write_bio( \
+ (int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,bp,(char *)x, \
+ NULL,NULL,0,NULL)
+#define PEM_write_bio_X509_CRL(bp,x) \
+ PEM_ASN1_write_bio((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL,\
+ bp,(char *)x, NULL,NULL,0,NULL)
+#define PEM_write_bio_RSAPrivateKey(bp,x,enc,kstr,klen,cb) \
+ PEM_ASN1_write_bio((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,\
+ bp,(char *)x,enc,kstr,klen,cb)
+#define PEM_write_bio_RSAPublicKey(bp,x) \
+ PEM_ASN1_write_bio((int (*)())i2d_RSAPublicKey, \
+ PEM_STRING_RSA_PUBLIC,\
+ bp,(char *)x,NULL,NULL,0,NULL)
+#define PEM_write_bio_DSAPrivateKey(bp,x,enc,kstr,klen,cb) \
+ PEM_ASN1_write_bio((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,\
+ bp,(char *)x,enc,kstr,klen,cb)
+#define PEM_write_bio_PrivateKey(bp,x,enc,kstr,klen,cb) \
+ PEM_ASN1_write_bio((int (*)())i2d_PrivateKey,\
+ (((x)->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA),\
+ bp,(char *)x,enc,kstr,klen,cb)
+#define PEM_write_bio_PKCS7(bp,x) \
+ PEM_ASN1_write_bio((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,bp, \
+ (char *)x, NULL,NULL,0,NULL)
+#define PEM_write_bio_DHparams(bp,x) \
+ PEM_ASN1_write_bio((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,\
+ bp,(char *)x,NULL,NULL,0,NULL)
+#define PEM_write_bio_DSAparams(bp,x) \
+ PEM_ASN1_write_bio((int (*)())i2d_DSAparams, \
+ PEM_STRING_DSAPARAMS,bp,(char *)x,NULL,NULL,0,NULL)
+
+#define PEM_read_bio_SSL_SESSION(bp,x,cb) (SSL_SESSION *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_SSL_SESSION,PEM_STRING_SSL_SESSION,bp,(char **)x,cb)
+#define PEM_read_bio_X509(bp,x,cb) (X509 *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_X509,PEM_STRING_X509,bp,(char **)x,cb)
+#define PEM_read_bio_X509_REQ(bp,x,cb) (X509_REQ *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_X509_REQ,PEM_STRING_X509_REQ,bp,(char **)x,cb)
+#define PEM_read_bio_X509_CRL(bp,x,cb) (X509_CRL *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_X509_CRL,PEM_STRING_X509_CRL,bp,(char **)x,cb)
+#define PEM_read_bio_RSAPrivateKey(bp,x,cb) (RSA *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_RSAPrivateKey,PEM_STRING_RSA,bp,(char **)x,cb)
+#define PEM_read_bio_RSAPublicKey(bp,x,cb) (RSA *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_RSAPublicKey,PEM_STRING_RSA_PUBLIC,bp,(char **)x,cb)
+#define PEM_read_bio_DSAPrivateKey(bp,x,cb) (DSA *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_DSAPrivateKey,PEM_STRING_DSA,bp,(char **)x,cb)
+#define PEM_read_bio_PrivateKey(bp,x,cb) (EVP_PKEY *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_PrivateKey,PEM_STRING_EVP_PKEY,bp,(char **)x,cb)
+
+#define PEM_read_bio_PKCS7(bp,x,cb) (PKCS7 *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_PKCS7,PEM_STRING_PKCS7,bp,(char **)x,cb)
+#define PEM_read_bio_DHparams(bp,x,cb) (DH *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_DHparams,PEM_STRING_DHPARAMS,bp,(char **)x,cb)
+#define PEM_read_bio_DSAparams(bp,x,cb) (DSA *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_DSAparams,PEM_STRING_DSAPARAMS,bp,(char **)x,cb)
+
+#endif
+
+#ifndef NOPROTO
+int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher);
+int PEM_do_header (EVP_CIPHER_INFO *cipher, unsigned char *data,long *len,
+ int (*callback)());
+
+#ifdef HEADER_BIO_H
+int PEM_read_bio(BIO *bp, char **name, char **header,
+ unsigned char **data,long *len);
+int PEM_write_bio(BIO *bp,char *name,char *hdr,unsigned char *data,
+ long len);
+char * PEM_ASN1_read_bio(char *(*d2i)(),char *name,BIO *bp,char **x,
+ int (*cb)());
+int PEM_ASN1_write_bio(int (*i2d)(),char *name,BIO *bp,char *x,
+ EVP_CIPHER *enc,unsigned char *kstr,int klen,int (*callback)());
+STACK * PEM_X509_INFO_read_bio(BIO *bp, STACK *sk, int (*cb)());
+int PEM_X509_INFO_write_bio(BIO *bp,X509_INFO *xi, EVP_CIPHER *enc,
+ unsigned char *kstr, int klen, int (*cb)());
+#endif
+
+#ifndef WIN16
+int PEM_read(FILE *fp, char **name, char **header,
+ unsigned char **data,long *len);
+int PEM_write(FILE *fp,char *name,char *hdr,unsigned char *data,long len);
+char * PEM_ASN1_read(char *(*d2i)(),char *name,FILE *fp,char **x,
+ int (*cb)());
+int PEM_ASN1_write(int (*i2d)(),char *name,FILE *fp,char *x,
+ EVP_CIPHER *enc,unsigned char *kstr,int klen,int (*callback)());
+STACK * PEM_X509_INFO_read(FILE *fp, STACK *sk, int (*cb)());
+#endif
+
+int PEM_SealInit(PEM_ENCODE_SEAL_CTX *ctx, EVP_CIPHER *type,
+ EVP_MD *md_type, unsigned char **ek, int *ekl,
+ unsigned char *iv, EVP_PKEY **pubk, int npubk);
+void PEM_SealUpdate(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *out, int *outl,
+ unsigned char *in, int inl);
+int PEM_SealFinal(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *sig,int *sigl,
+ unsigned char *out, int *outl, EVP_PKEY *priv);
+
+void PEM_SignInit(EVP_MD_CTX *ctx, EVP_MD *type);
+void PEM_SignUpdate(EVP_MD_CTX *ctx,unsigned char *d,unsigned int cnt);
+int PEM_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
+ unsigned int *siglen, EVP_PKEY *pkey);
+
+void ERR_load_PEM_strings(void);
+
+void PEM_proc_type(char *buf, int type);
+void PEM_dek_info(char *buf, char *type, int len, char *str);
+
+#ifndef SSLEAY_MACROS
+
+#ifndef WIN16
+X509 *PEM_read_X509(FILE *fp,X509 **x,int (*cb)());
+X509_REQ *PEM_read_X509_REQ(FILE *fp,X509_REQ **x,int (*cb)());
+X509_CRL *PEM_read_X509_CRL(FILE *fp,X509_CRL **x,int (*cb)());
+RSA *PEM_read_RSAPrivateKey(FILE *fp,RSA **x,int (*cb)());
+RSA *PEM_read_RSAPublicKey(FILE *fp,RSA **x,int (*cb)());
+DSA *PEM_read_DSAPrivateKey(FILE *fp,DSA **x,int (*cb)());
+EVP_PKEY *PEM_read_PrivateKey(FILE *fp,EVP_PKEY **x,int (*cb)());
+PKCS7 *PEM_read_PKCS7(FILE *fp,PKCS7 **x,int (*cb)());
+DH *PEM_read_DHparams(FILE *fp,DH **x,int (*cb)());
+DSA *PEM_read_DSAparams(FILE *fp,DSA **x,int (*cb)());
+int PEM_write_X509(FILE *fp,X509 *x);
+int PEM_write_X509_REQ(FILE *fp,X509_REQ *x);
+int PEM_write_X509_CRL(FILE *fp,X509_CRL *x);
+int PEM_write_RSAPrivateKey(FILE *fp,RSA *x,EVP_CIPHER *enc,unsigned char *kstr,
+ int klen,int (*cb)());
+int PEM_write_RSAPublicKey(FILE *fp,RSA *x);
+int PEM_write_DSAPrivateKey(FILE *fp,DSA *x,EVP_CIPHER *enc,unsigned char *kstr,
+ int klen,int (*cb)());
+int PEM_write_PrivateKey(FILE *fp,EVP_PKEY *x,EVP_CIPHER *enc,
+ unsigned char *kstr,int klen,int (*cb)());
+int PEM_write_PKCS7(FILE *fp,PKCS7 *x);
+int PEM_write_DHparams(FILE *fp,DH *x);
+int PEM_write_DSAparams(FILE *fp,DSA *x);
+#endif
+
+#ifdef HEADER_BIO_H
+X509 *PEM_read_bio_X509(BIO *bp,X509 **x,int (*cb)());
+X509_REQ *PEM_read_bio_X509_REQ(BIO *bp,X509_REQ **x,int (*cb)());
+X509_CRL *PEM_read_bio_X509_CRL(BIO *bp,X509_CRL **x,int (*cb)());
+RSA *PEM_read_bio_RSAPrivateKey(BIO *bp,RSA **x,int (*cb)());
+RSA *PEM_read_bio_RSAPublicKey(BIO *bp,RSA **x,int (*cb)());
+DSA *PEM_read_bio_DSAPrivateKey(BIO *bp,DSA **x,int (*cb)());
+EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp,EVP_PKEY **x,int (*cb)());
+PKCS7 *PEM_read_bio_PKCS7(BIO *bp,PKCS7 **x,int (*cb)());
+DH *PEM_read_bio_DHparams(BIO *bp,DH **x,int (*cb)());
+DSA *PEM_read_bio_DSAparams(BIO *bp,DSA **x,int (*cb)());
+int PEM_write_bio_X509(BIO *bp,X509 *x);
+int PEM_write_bio_X509_REQ(BIO *bp,X509_REQ *x);
+int PEM_write_bio_X509_CRL(BIO *bp,X509_CRL *x);
+int PEM_write_bio_RSAPrivateKey(BIO *fp,RSA *x,EVP_CIPHER *enc,
+ unsigned char *kstr,int klen,int (*cb)());
+int PEM_write_bio_RSAPublicKey(BIO *fp,RSA *x);
+int PEM_write_bio_DSAPrivateKey(BIO *fp,DSA *x,EVP_CIPHER *enc,
+ unsigned char *kstr,int klen,int (*cb)());
+int PEM_write_bio_PrivateKey(BIO *fp,EVP_PKEY *x,EVP_CIPHER *enc,
+ unsigned char *kstr,int klen,int (*cb)());
+int PEM_write_bio_PKCS7(BIO *bp,PKCS7 *x);
+int PEM_write_bio_DHparams(BIO *bp,DH *x);
+int PEM_write_bio_DSAparams(BIO *bp,DSA *x);
+#endif
+
+#endif /* SSLEAY_MACROS */
+
+
+#else
+
+int PEM_get_EVP_CIPHER_INFO();
+int PEM_do_header();
+int PEM_read_bio();
+int PEM_write_bio();
+#ifndef WIN16
+int PEM_read();
+int PEM_write();
+STACK * PEM_X509_INFO_read();
+char * PEM_ASN1_read();
+int PEM_ASN1_write();
+#endif
+STACK * PEM_X509_INFO_read_bio();
+int PEM_X509_INFO_write_bio();
+char * PEM_ASN1_read_bio();
+int PEM_ASN1_write_bio();
+int PEM_SealInit();
+void PEM_SealUpdate();
+int PEM_SealFinal();
+int PEM_SignFinal();
+
+void ERR_load_PEM_strings();
+
+void PEM_proc_type();
+void PEM_dek_info();
+
+#ifndef SSLEAY_MACROS
+#ifndef WIN16
+X509 *PEM_read_X509();
+X509_REQ *PEM_read_X509_REQ();
+X509_CRL *PEM_read_X509_CRL();
+RSA *PEM_read_RSAPrivateKey();
+RSA *PEM_read_RSAPublicKey();
+DSA *PEM_read_DSAPrivateKey();
+EVP_PKEY *PEM_read_PrivateKey();
+PKCS7 *PEM_read_PKCS7();
+DH *PEM_read_DHparams();
+DSA *PEM_read_DSAparams();
+int PEM_write_X509();
+int PEM_write_X509_REQ();
+int PEM_write_X509_CRL();
+int PEM_write_RSAPrivateKey();
+int PEM_write_RSAPublicKey();
+int PEM_write_DSAPrivateKey();
+int PEM_write_PrivateKey();
+int PEM_write_PKCS7();
+int PEM_write_DHparams();
+int PEM_write_DSAparams();
+#endif
+
+X509 *PEM_read_bio_X509();
+X509_REQ *PEM_read_bio_X509_REQ();
+X509_CRL *PEM_read_bio_X509_CRL();
+RSA *PEM_read_bio_RSAPrivateKey();
+RSA *PEM_read_bio_RSAPublicKey();
+DSA *PEM_read_bio_DSAPrivateKey();
+EVP_PKEY *PEM_read_bio_PrivateKey();
+PKCS7 *PEM_read_bio_PKCS7();
+DH *PEM_read_bio_DHparams();
+DSA *PEM_read_bio_DSAparams();
+int PEM_write_bio_X509();
+int PEM_write_bio_X509_REQ();
+int PEM_write_bio_X509_CRL();
+int PEM_write_bio_RSAPrivateKey();
+int PEM_write_bio_RSAPublicKey();
+int PEM_write_bio_DSAPrivateKey();
+int PEM_write_bio_PrivateKey();
+int PEM_write_bio_PKCS7();
+int PEM_write_bio_DHparams();
+int PEM_write_bio_DSAparams();
+
+#endif /* SSLEAY_MACROS */
+
+#endif
+
+/* BEGIN ERROR CODES */
+/* Error codes for the PEM functions. */
+
+/* Function codes. */
+#define PEM_F_DEF_CALLBACK 100
+#define PEM_F_LOAD_IV 101
+#define PEM_F_PEM_ASN1_READ 102
+#define PEM_F_PEM_ASN1_READ_BIO 103
+#define PEM_F_PEM_ASN1_WRITE 104
+#define PEM_F_PEM_ASN1_WRITE_BIO 105
+#define PEM_F_PEM_DO_HEADER 106
+#define PEM_F_PEM_GET_EVP_CIPHER_INFO 107
+#define PEM_F_PEM_READ 108
+#define PEM_F_PEM_READ_BIO 109
+#define PEM_F_PEM_SEALFINAL 110
+#define PEM_F_PEM_SEALINIT 111
+#define PEM_F_PEM_SIGNFINAL 112
+#define PEM_F_PEM_WRITE 113
+#define PEM_F_PEM_WRITE_BIO 114
+#define PEM_F_PEM_X509_INFO_READ 115
+#define PEM_F_PEM_X509_INFO_READ_BIO 116
+#define PEM_F_PEM_X509_INFO_WRITE_BIO 117
+
+/* Reason codes. */
+#define PEM_R_BAD_BASE64_DECODE 100
+#define PEM_R_BAD_DECRYPT 101
+#define PEM_R_BAD_END_LINE 102
+#define PEM_R_BAD_IV_CHARS 103
+#define PEM_R_BAD_PASSWORD_READ 104
+#define PEM_R_NOT_DEK_INFO 105
+#define PEM_R_NOT_ENCRYPTED 106
+#define PEM_R_NOT_PROC_TYPE 107
+#define PEM_R_NO_START_LINE 108
+#define PEM_R_PROBLEMS_GETTING_PASSWORD 109
+#define PEM_R_PUBLIC_KEY_NO_RSA 110
+#define PEM_R_READ_KEY 111
+#define PEM_R_SHORT_HEADER 112
+#define PEM_R_UNSUPPORTED_CIPHER 113
+#define PEM_R_UNSUPPORTED_ENCRYPTION 114
+
+#ifdef __cplusplus
+}
+#endif
+#endif
+
diff --git a/crypto/pem/pem.org b/crypto/pem/pem.org
new file mode 100644
index 0000000000..38952509dd
--- /dev/null
+++ b/crypto/pem/pem.org
@@ -0,0 +1,562 @@
+/* crypto/pem/pem.org */
+/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+ * All rights reserved.
+ *
+ * This package is an SSL implementation written
+ * by Eric Young (eay@cryptsoft.com).
+ * The implementation was written so as to conform with Netscapes SSL.
+ *
+ * This library is free for commercial and non-commercial use as long as
+ * the following conditions are aheared to. The following conditions
+ * apply to all code found in this distribution, be it the RC4, RSA,
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
+ * included with this distribution is covered by the same copyright terms
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
+ *
+ * Copyright remains Eric Young's, and as such any Copyright notices in
+ * the code are not to be removed.
+ * If this package is used in a product, Eric Young should be given attribution
+ * as the author of the parts of the library used.
+ * This can be in the form of a textual message at program startup or
+ * in documentation (online or textual) provided with the package.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * "This product includes cryptographic software written by
+ * Eric Young (eay@cryptsoft.com)"
+ * The word 'cryptographic' can be left out if the rouines from the library
+ * being used are not cryptographic related :-).
+ * 4. If you include any Windows specific code (or a derivative thereof) from
+ * the apps directory (application code) you must include an acknowledgement:
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * The licence and distribution terms for any publically available version or
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
+ * copied and put under another distribution licence
+ * [including the GNU Public Licence.]
+ */
+
+/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
+ *
+ * Always modify pem.org since pem.h is automatically generated from
+ * it during SSLeay configuration.
+ *
+ * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
+ */
+
+#ifndef HEADER_PEM_H
+#define HEADER_PEM_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "evp.h"
+#include "x509.h"
+
+#define PEM_OBJ_UNDEF 0
+#define PEM_OBJ_X509 1
+#define PEM_OBJ_X509_REQ 2
+#define PEM_OBJ_CRL 3
+#define PEM_OBJ_SSL_SESSION 4
+#define PEM_OBJ_PRIV_KEY 10
+#define PEM_OBJ_PRIV_RSA 11
+#define PEM_OBJ_PRIV_DSA 12
+#define PEM_OBJ_PRIV_DH 13
+#define PEM_OBJ_PUB_RSA 14
+#define PEM_OBJ_PUB_DSA 15
+#define PEM_OBJ_PUB_DH 16
+#define PEM_OBJ_DHPARAMS 17
+#define PEM_OBJ_DSAPARAMS 18
+#define PEM_OBJ_PRIV_RSA_PUBLIC 19
+
+#define PEM_ERROR 30
+#define PEM_DEK_DES_CBC 40
+#define PEM_DEK_IDEA_CBC 45
+#define PEM_DEK_DES_EDE 50
+#define PEM_DEK_DES_ECB 60
+#define PEM_DEK_RSA 70
+#define PEM_DEK_RSA_MD2 80
+#define PEM_DEK_RSA_MD5 90
+
+#define PEM_MD_MD2 NID_md2
+#define PEM_MD_MD5 NID_md5
+#define PEM_MD_SHA NID_sha
+#define PEM_MD_MD2_RSA NID_md2WithRSAEncryption
+#define PEM_MD_MD5_RSA NID_md5WithRSAEncryption
+#define PEM_MD_SHA_RSA NID_sha1WithRSAEncryption
+
+#define PEM_STRING_X509_OLD "X509 CERTIFICATE"
+#define PEM_STRING_X509 "CERTIFICATE"
+#define PEM_STRING_X509_REQ_OLD "NEW CERTIFICATE REQUEST"
+#define PEM_STRING_X509_REQ "CERTIFICATE REQUEST"
+#define PEM_STRING_X509_CRL "X509 CRL"
+#define PEM_STRING_EVP_PKEY "PRIVATE KEY"
+#define PEM_STRING_RSA "RSA PRIVATE KEY"
+#define PEM_STRING_RSA_PUBLIC "RSA PUBLIC KEY"
+#define PEM_STRING_DSA "DSA PRIVATE KEY"
+#define PEM_STRING_PKCS7 "PKCS7"
+#define PEM_STRING_DHPARAMS "DH PARAMETERS"
+#define PEM_STRING_SSL_SESSION "SSL SESSION PARAMETERS"
+#define PEM_STRING_DSAPARAMS "DSA PARAMETERS"
+
+#ifndef HEADER_ENVELOPE_H
+
+#define EVP_ENCODE_CTX_SIZE 92
+#define EVP_MD_SIZE 48
+#define EVP_MD_CTX_SIZE 152
+#define EVP_CIPHER_SIZE 28
+#define EVP_CIPHER_CTX_SIZE 4212
+#define EVP_MAX_MD_SIZE 20
+
+typedef struct evp_encode_ctx_st
+ {
+ char data[EVP_ENCODE_CTX_SIZE];
+ } EVP_ENCODE_CTX;
+
+typedef struct env_md_ctx_st
+ {
+ char data[EVP_MD_CTX_SIZE];
+ } EVP_MD_CTX;
+
+typedef struct evp_cipher_st
+ {
+ char data[EVP_CIPHER_SIZE];
+ } EVP_CIPHER;
+
+typedef struct evp_cipher_ctx_st
+ {
+ char data[EVP_CIPHER_CTX_SIZE];
+ } EVP_CIPHER_CTX;
+#endif
+
+
+typedef struct PEM_Encode_Seal_st
+ {
+ EVP_ENCODE_CTX encode;
+ EVP_MD_CTX md;
+ EVP_CIPHER_CTX cipher;
+ } PEM_ENCODE_SEAL_CTX;
+
+/* enc_type is one off */
+#define PEM_TYPE_ENCRYPTED 10
+#define PEM_TYPE_MIC_ONLY 20
+#define PEM_TYPE_MIC_CLEAR 30
+#define PEM_TYPE_CLEAR 40
+
+typedef struct pem_recip_st
+ {
+ char *name;
+ X509_NAME *dn;
+
+ int cipher;
+ int key_enc;
+ char iv[8];
+ } PEM_USER;
+
+typedef struct pem_ctx_st
+ {
+ int type; /* what type of object */
+
+ struct {
+ int version;
+ int mode;
+ } proc_type;
+
+ char *domain;
+
+ struct {
+ int cipher;
+ unsigned char iv[8];
+ } DEK_info;
+
+ PEM_USER *originator;
+
+ int num_recipient;
+ PEM_USER **recipient;
+
+#ifdef HEADER_STACK_H
+ STACK *x509_chain; /* certificate chain */
+#else
+ char *x509_chain; /* certificate chain */
+#endif
+ EVP_MD *md; /* signature type */
+
+ int md_enc; /* is the md encrypted or not? */
+ int md_len; /* length of md_data */
+ char *md_data; /* message digest, could be pkey encrypted */
+
+ EVP_CIPHER *dec; /* date encryption cipher */
+ int key_len; /* key length */
+ unsigned char *key; /* key */
+ unsigned char iv[8]; /* the iv */
+
+
+ int data_enc; /* is the data encrypted */
+ int data_len;
+ unsigned char *data;
+ } PEM_CTX;
+
+#ifdef SSLEAY_MACROS
+
+#define PEM_write_SSL_SESSION(fp,x) \
+ PEM_ASN1_write((int (*)())i2d_SSL_SESSION, \
+ PEM_STRING_SSL_SESSION,fp, (char *)x, NULL,NULL,0,NULL)
+#define PEM_write_X509(fp,x) \
+ PEM_ASN1_write((int (*)())i2d_X509,PEM_STRING_X509,fp, \
+ (char *)x, NULL,NULL,0,NULL)
+#define PEM_write_X509_REQ(fp,x) PEM_ASN1_write( \
+ (int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,fp,(char *)x, \
+ NULL,NULL,0,NULL)
+#define PEM_write_X509_CRL(fp,x) \
+ PEM_ASN1_write((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL, \
+ fp,(char *)x, NULL,NULL,0,NULL)
+#define PEM_write_RSAPrivateKey(fp,x,enc,kstr,klen,cb) \
+ PEM_ASN1_write((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,fp,\
+ (char *)x,enc,kstr,klen,cb)
+#define PEM_write_RSAPublicKey(fp,x) \
+ PEM_ASN1_write((int (*)())i2d_RSAPublicKey,\
+ PEM_STRING_RSA_PUBLIC,fp,(char *)x,NULL,NULL,0,NULL)
+#define PEM_write_DSAPrivateKey(fp,x,enc,kstr,klen,cb) \
+ PEM_ASN1_write((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,fp,\
+ (char *)x,enc,kstr,klen,cb)
+#define PEM_write_PrivateKey(bp,x,enc,kstr,klen,cb) \
+ PEM_ASN1_write((int (*)())i2d_PrivateKey,\
+ (((x)->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA),\
+ bp,(char *)x,enc,kstr,klen,cb)
+#define PEM_write_PKCS7(fp,x) \
+ PEM_ASN1_write((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,fp, \
+ (char *)x, NULL,NULL,0,NULL)
+#define PEM_write_DHparams(fp,x) \
+ PEM_ASN1_write((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,fp,\
+ (char *)x,NULL,NULL,0,NULL)
+
+#define PEM_read_SSL_SESSION(fp,x,cb) (SSL_SESSION *)PEM_ASN1_read( \
+ (char *(*)())d2i_SSL_SESSION,PEM_STRING_SSL_SESSION,fp,(char **)x,cb)
+#define PEM_read_X509(fp,x,cb) (X509 *)PEM_ASN1_read( \
+ (char *(*)())d2i_X509,PEM_STRING_X509,fp,(char **)x,cb)
+#define PEM_read_X509_REQ(fp,x,cb) (X509_REQ *)PEM_ASN1_read( \
+ (char *(*)())d2i_X509_REQ,PEM_STRING_X509_REQ,fp,(char **)x,cb)
+#define PEM_read_X509_CRL(fp,x,cb) (X509_CRL *)PEM_ASN1_read( \
+ (char *(*)())d2i_X509_CRL,PEM_STRING_X509_CRL,fp,(char **)x,cb)
+#define PEM_read_RSAPrivateKey(fp,x,cb) (RSA *)PEM_ASN1_read( \
+ (char *(*)())d2i_RSAPrivateKey,PEM_STRING_RSA,fp,(char **)x,cb)
+#define PEM_read_RSAPublicKey(fp,x,cb) (RSA *)PEM_ASN1_read( \
+ (char *(*)())d2i_RSAPublicKey,PEM_STRING_RSA_PUBLIC,fp,(char **)x,cb)
+#define PEM_read_DSAPrivateKey(fp,x,cb) (DSA *)PEM_ASN1_read( \
+ (char *(*)())d2i_DSAPrivateKey,PEM_STRING_DSA,fp,(char **)x,cb)
+#define PEM_read_PrivateKey(fp,x,cb) (EVP_PKEY *)PEM_ASN1_read( \
+ (char *(*)())d2i_PrivateKey,PEM_STRING_EVP_PKEY,fp,(char **)x,cb)
+#define PEM_read_PKCS7(fp,x,cb) (PKCS7 *)PEM_ASN1_read( \
+ (char *(*)())d2i_PKCS7,PEM_STRING_PKCS7,fp,(char **)x,cb)
+#define PEM_read_DHparams(fp,x,cb) (DH *)PEM_ASN1_read( \
+ (char *(*)())d2i_DHparams,PEM_STRING_DHPARAMS,fp,(char **)x,cb)
+
+#define PEM_write_bio_SSL_SESSION(bp,x) \
+ PEM_ASN1_write_bio((int (*)())i2d_SSL_SESSION, \
+ PEM_STRING_SSL_SESSION,bp, (char *)x, NULL,NULL,0,NULL)
+#define PEM_write_bio_X509(bp,x) \
+ PEM_ASN1_write_bio((int (*)())i2d_X509,PEM_STRING_X509,bp, \
+ (char *)x, NULL,NULL,0,NULL)
+#define PEM_write_bio_X509_REQ(bp,x) PEM_ASN1_write_bio( \
+ (int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,bp,(char *)x, \
+ NULL,NULL,0,NULL)
+#define PEM_write_bio_X509_CRL(bp,x) \
+ PEM_ASN1_write_bio((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL,\
+ bp,(char *)x, NULL,NULL,0,NULL)
+#define PEM_write_bio_RSAPrivateKey(bp,x,enc,kstr,klen,cb) \
+ PEM_ASN1_write_bio((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,\
+ bp,(char *)x,enc,kstr,klen,cb)
+#define PEM_write_bio_RSAPublicKey(bp,x) \
+ PEM_ASN1_write_bio((int (*)())i2d_RSAPublicKey, \
+ PEM_STRING_RSA_PUBLIC,\
+ bp,(char *)x,NULL,NULL,0,NULL)
+#define PEM_write_bio_DSAPrivateKey(bp,x,enc,kstr,klen,cb) \
+ PEM_ASN1_write_bio((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,\
+ bp,(char *)x,enc,kstr,klen,cb)
+#define PEM_write_bio_PrivateKey(bp,x,enc,kstr,klen,cb) \
+ PEM_ASN1_write_bio((int (*)())i2d_PrivateKey,\
+ (((x)->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA),\
+ bp,(char *)x,enc,kstr,klen,cb)
+#define PEM_write_bio_PKCS7(bp,x) \
+ PEM_ASN1_write_bio((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,bp, \
+ (char *)x, NULL,NULL,0,NULL)
+#define PEM_write_bio_DHparams(bp,x) \
+ PEM_ASN1_write_bio((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,\
+ bp,(char *)x,NULL,NULL,0,NULL)
+#define PEM_write_bio_DSAparams(bp,x) \
+ PEM_ASN1_write_bio((int (*)())i2d_DSAparams, \
+ PEM_STRING_DSAPARAMS,bp,(char *)x,NULL,NULL,0,NULL)
+
+#define PEM_read_bio_SSL_SESSION(bp,x,cb) (SSL_SESSION *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_SSL_SESSION,PEM_STRING_SSL_SESSION,bp,(char **)x,cb)
+#define PEM_read_bio_X509(bp,x,cb) (X509 *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_X509,PEM_STRING_X509,bp,(char **)x,cb)
+#define PEM_read_bio_X509_REQ(bp,x,cb) (X509_REQ *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_X509_REQ,PEM_STRING_X509_REQ,bp,(char **)x,cb)
+#define PEM_read_bio_X509_CRL(bp,x,cb) (X509_CRL *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_X509_CRL,PEM_STRING_X509_CRL,bp,(char **)x,cb)
+#define PEM_read_bio_RSAPrivateKey(bp,x,cb) (RSA *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_RSAPrivateKey,PEM_STRING_RSA,bp,(char **)x,cb)
+#define PEM_read_bio_RSAPublicKey(bp,x,cb) (RSA *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_RSAPublicKey,PEM_STRING_RSA_PUBLIC,bp,(char **)x,cb)
+#define PEM_read_bio_DSAPrivateKey(bp,x,cb) (DSA *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_DSAPrivateKey,PEM_STRING_DSA,bp,(char **)x,cb)
+#define PEM_read_bio_PrivateKey(bp,x,cb) (EVP_PKEY *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_PrivateKey,PEM_STRING_EVP_PKEY,bp,(char **)x,cb)
+
+#define PEM_read_bio_PKCS7(bp,x,cb) (PKCS7 *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_PKCS7,PEM_STRING_PKCS7,bp,(char **)x,cb)
+#define PEM_read_bio_DHparams(bp,x,cb) (DH *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_DHparams,PEM_STRING_DHPARAMS,bp,(char **)x,cb)
+#define PEM_read_bio_DSAparams(bp,x,cb) (DSA *)PEM_ASN1_read_bio( \
+ (char *(*)())d2i_DSAparams,PEM_STRING_DSAPARAMS,bp,(char **)x,cb)
+
+#endif
+
+#ifndef NOPROTO
+int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher);
+int PEM_do_header (EVP_CIPHER_INFO *cipher, unsigned char *data,long *len,
+ int (*callback)());
+
+#ifdef HEADER_BIO_H
+int PEM_read_bio(BIO *bp, char **name, char **header,
+ unsigned char **data,long *len);
+int PEM_write_bio(BIO *bp,char *name,char *hdr,unsigned char *data,
+ long len);
+char * PEM_ASN1_read_bio(char *(*d2i)(),char *name,BIO *bp,char **x,
+ int (*cb)());
+int PEM_ASN1_write_bio(int (*i2d)(),char *name,BIO *bp,char *x,
+ EVP_CIPHER *enc,unsigned char *kstr,int klen,int (*callback)());
+STACK * PEM_X509_INFO_read_bio(BIO *bp, STACK *sk, int (*cb)());
+int PEM_X509_INFO_write_bio(BIO *bp,X509_INFO *xi, EVP_CIPHER *enc,
+ unsigned char *kstr, int klen, int (*cb)());
+#endif
+
+#ifndef WIN16
+int PEM_read(FILE *fp, char **name, char **header,
+ unsigned char **data,long *len);
+int PEM_write(FILE *fp,char *name,char *hdr,unsigned char *data,long len);
+char * PEM_ASN1_read(char *(*d2i)(),char *name,FILE *fp,char **x,
+ int (*cb)());
+int PEM_ASN1_write(int (*i2d)(),char *name,FILE *fp,char *x,
+ EVP_CIPHER *enc,unsigned char *kstr,int klen,int (*callback)());
+STACK * PEM_X509_INFO_read(FILE *fp, STACK *sk, int (*cb)());
+#endif
+
+int PEM_SealInit(PEM_ENCODE_SEAL_CTX *ctx, EVP_CIPHER *type,
+ EVP_MD *md_type, unsigned char **ek, int *ekl,
+ unsigned char *iv, EVP_PKEY **pubk, int npubk);
+void PEM_SealUpdate(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *out, int *outl,
+ unsigned char *in, int inl);
+int PEM_SealFinal(PEM_ENCODE_SEAL_CTX *ctx, unsigned char *sig,int *sigl,
+ unsigned char *out, int *outl, EVP_PKEY *priv);
+
+void PEM_SignInit(EVP_MD_CTX *ctx, EVP_MD *type);
+void PEM_SignUpdate(EVP_MD_CTX *ctx,unsigned char *d,unsigned int cnt);
+int PEM_SignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
+ unsigned int *siglen, EVP_PKEY *pkey);
+
+void ERR_load_PEM_strings(void);
+
+void PEM_proc_type(char *buf, int type);
+void PEM_dek_info(char *buf, char *type, int len, char *str);
+
+#ifndef SSLEAY_MACROS
+
+#ifndef WIN16
+X509 *PEM_read_X509(FILE *fp,X509 **x,int (*cb)());
+X509_REQ *PEM_read_X509_REQ(FILE *fp,X509_REQ **x,int (*cb)());
+X509_CRL *PEM_read_X509_CRL(FILE *fp,X509_CRL **x,int (*cb)());
+RSA *PEM_read_RSAPrivateKey(FILE *fp,RSA **x,int (*cb)());
+RSA *PEM_read_RSAPublicKey(FILE *fp,RSA **x,int (*cb)());
+DSA *PEM_read_DSAPrivateKey(FILE *fp,DSA **x,int (*cb)());
+EVP_PKEY *PEM_read_PrivateKey(FILE *fp,EVP_PKEY **x,int (*cb)());
+PKCS7 *PEM_read_PKCS7(FILE *fp,PKCS7 **x,int (*cb)());
+DH *PEM_read_DHparams(FILE *fp,DH **x,int (*cb)());
+DSA *PEM_read_DSAparams(FILE *fp,DSA **x,int (*cb)());
+int PEM_write_X509(FILE *fp,X509 *x);
+int PEM_write_X509_REQ(FILE *fp,X509_REQ *x);
+int PEM_write_X509_CRL(FILE *fp,X509_CRL *x);
+int PEM_write_RSAPrivateKey(FILE *fp,RSA *x,EVP_CIPHER *enc,unsigned char *kstr,
+ int klen,int (*cb)());
+int PEM_write_RSAPublicKey(FILE *fp,RSA *x);
+int PEM_write_DSAPrivateKey(FILE *fp,DSA *x,EVP_CIPHER *enc,unsigned char *kstr,
+ int klen,int (*cb)());
+int PEM_write_PrivateKey(FILE *fp,EVP_PKEY *x,EVP_CIPHER *enc,
+ unsigned char *kstr,int klen,int (*cb)());
+int PEM_write_PKCS7(FILE *fp,PKCS7 *x);
+int PEM_write_DHparams(FILE *fp,DH *x);
+int PEM_write_DSAparams(FILE *fp,DSA *x);
+#endif
+
+#ifdef HEADER_BIO_H
+X509 *PEM_read_bio_X509(BIO *bp,X509 **x,int (*cb)());
+X509_REQ *PEM_read_bio_X509_REQ(BIO *bp,X509_REQ **x,int (*cb)());
+X509_CRL *PEM_read_bio_X509_CRL(BIO *bp,X509_CRL **x,int (*cb)());
+RSA *PEM_read_bio_RSAPrivateKey(BIO *bp,RSA **x,int (*cb)());
+RSA *PEM_read_bio_RSAPublicKey(BIO *bp,RSA **x,int (*cb)());
+DSA *PEM_read_bio_DSAPrivateKey(BIO *bp,DSA **x,int (*cb)());
+EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp,EVP_PKEY **x,int (*cb)());
+PKCS7 *PEM_read_bio_PKCS7(BIO *bp,PKCS7 **x,int (*cb)());
+DH *PEM_read_bio_DHparams(BIO *bp,DH **x,int (*cb)());
+DSA *PEM_read_bio_DSAparams(BIO *bp,DSA **x,int (*cb)());
+int PEM_write_bio_X509(BIO *bp,X509 *x);
+int PEM_write_bio_X509_REQ(BIO *bp,X509_REQ *x);
+int PEM_write_bio_X509_CRL(BIO *bp,X509_CRL *x);
+int PEM_write_bio_RSAPrivateKey(BIO *fp,RSA *x,EVP_CIPHER *enc,
+ unsigned char *kstr,int klen,int (*cb)());
+int PEM_write_bio_RSAPublicKey(BIO *fp,RSA *x);
+int PEM_write_bio_DSAPrivateKey(BIO *fp,DSA *x,EVP_CIPHER *enc,
+ unsigned char *kstr,int klen,int (*cb)());
+int PEM_write_bio_PrivateKey(BIO *fp,EVP_PKEY *x,EVP_CIPHER *enc,
+ unsigned char *kstr,int klen,int (*cb)());
+int PEM_write_bio_PKCS7(BIO *bp,PKCS7 *x);
+int PEM_write_bio_DHparams(BIO *bp,DH *x);
+int PEM_write_bio_DSAparams(BIO *bp,DSA *x);
+#endif
+
+#endif /* SSLEAY_MACROS */
+
+
+#else
+
+int PEM_get_EVP_CIPHER_INFO();
+int PEM_do_header();
+int PEM_read_bio();
+int PEM_write_bio();
+#ifndef WIN16
+int PEM_read();
+int PEM_write();
+STACK * PEM_X509_INFO_read();
+char * PEM_ASN1_read();
+int PEM_ASN1_write();
+#endif
+STACK * PEM_X509_INFO_read_bio();
+int PEM_X509_INFO_write_bio();
+char * PEM_ASN1_read_bio();
+int PEM_ASN1_write_bio();
+int PEM_SealInit();
+void PEM_SealUpdate();
+int PEM_SealFinal();
+int PEM_SignFinal();
+
+void ERR_load_PEM_strings();
+
+void PEM_proc_type();
+void PEM_dek_info();
+
+#ifndef SSLEAY_MACROS
+#ifndef WIN16
+X509 *PEM_read_X509();
+X509_REQ *PEM_read_X509_REQ();
+X509_CRL *PEM_read_X509_CRL();
+RSA *PEM_read_RSAPrivateKey();
+RSA *PEM_read_RSAPublicKey();
+DSA *PEM_read_DSAPrivateKey();
+EVP_PKEY *PEM_read_PrivateKey();
+PKCS7 *PEM_read_PKCS7();
+DH *PEM_read_DHparams();
+DSA *PEM_read_DSAparams();
+int PEM_write_X509();
+int PEM_write_X509_REQ();
+int PEM_write_X509_CRL();
+int PEM_write_RSAPrivateKey();
+int PEM_write_RSAPublicKey();
+int PEM_write_DSAPrivateKey();
+int PEM_write_PrivateKey();
+int PEM_write_PKCS7();
+int PEM_write_DHparams();
+int PEM_write_DSAparams();
+#endif
+
+X509 *PEM_read_bio_X509();
+X509_REQ *PEM_read_bio_X509_REQ();
+X509_CRL *PEM_read_bio_X509_CRL();
+RSA *PEM_read_bio_RSAPrivateKey();
+RSA *PEM_read_bio_RSAPublicKey();
+DSA *PEM_read_bio_DSAPrivateKey();
+EVP_PKEY *PEM_read_bio_PrivateKey();
+PKCS7 *PEM_read_bio_PKCS7();
+DH *PEM_read_bio_DHparams();
+DSA *PEM_read_bio_DSAparams();
+int PEM_write_bio_X509();
+int PEM_write_bio_X509_REQ();
+int PEM_write_bio_X509_CRL();
+int PEM_write_bio_RSAPrivateKey();
+int PEM_write_bio_RSAPublicKey();
+int PEM_write_bio_DSAPrivateKey();
+int PEM_write_bio_PrivateKey();
+int PEM_write_bio_PKCS7();
+int PEM_write_bio_DHparams();
+int PEM_write_bio_DSAparams();
+
+#endif /* SSLEAY_MACROS */
+
+#endif
+
+/* BEGIN ERROR CODES */
+/* Error codes for the PEM functions. */
+
+/* Function codes. */
+#define PEM_F_DEF_CALLBACK 100
+#define PEM_F_LOAD_IV 101
+#define PEM_F_PEM_ASN1_READ 102
+#define PEM_F_PEM_ASN1_READ_BIO 103
+#define PEM_F_PEM_ASN1_WRITE 104
+#define PEM_F_PEM_ASN1_WRITE_BIO 105
+#define PEM_F_PEM_DO_HEADER 106
+#define PEM_F_PEM_GET_EVP_CIPHER_INFO 107
+#define PEM_F_PEM_READ 108
+#define PEM_F_PEM_READ_BIO 109
+#define PEM_F_PEM_SEALFINAL 110
+#define PEM_F_PEM_SEALINIT 111
+#define PEM_F_PEM_SIGNFINAL 112
+#define PEM_F_PEM_WRITE 113
+#define PEM_F_PEM_WRITE_BIO 114
+#define PEM_F_PEM_X509_INFO_READ 115
+#define PEM_F_PEM_X509_INFO_READ_BIO 116
+#define PEM_F_PEM_X509_INFO_WRITE_BIO 117
+
+/* Reason codes. */
+#define PEM_R_BAD_BASE64_DECODE 100
+#define PEM_R_BAD_DECRYPT 101
+#define PEM_R_BAD_END_LINE 102
+#define PEM_R_BAD_IV_CHARS 103
+#define PEM_R_BAD_PASSWORD_READ 104
+#define PEM_R_NOT_DEK_INFO 105
+#define PEM_R_NOT_ENCRYPTED 106
+#define PEM_R_NOT_PROC_TYPE 107
+#define PEM_R_NO_START_LINE 108
+#define PEM_R_PROBLEMS_GETTING_PASSWORD 109
+#define PEM_R_PUBLIC_KEY_NO_RSA 110
+#define PEM_R_READ_KEY 111
+#define PEM_R_SHORT_HEADER 112
+#define PEM_R_UNSUPPORTED_CIPHER 113
+#define PEM_R_UNSUPPORTED_ENCRYPTION 114
+
+#ifdef __cplusplus
+}
+#endif
+#endif
+
diff --git a/crypto/pem/pem_all.c b/crypto/pem/pem_all.c
new file mode 100644
index 0000000000..5485628f52
--- /dev/null
+++ b/crypto/pem/pem_all.c
@@ -0,0 +1,487 @@
+/* crypto/pem/pem_all.c */
+/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+ * All rights reserved.
+ *
+ * This package is an SSL implementation written
+ * by Eric Young (eay@cryptsoft.com).
+ * The implementation was written so as to conform with Netscapes SSL.
+ *
+ * This library is free for commercial and non-commercial use as long as
+ * the following conditions are aheared to. The following conditions
+ * apply to all code found in this distribution, be it the RC4, RSA,
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
+ * included with this distribution is covered by the same copyright terms
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
+ *
+ * Copyright remains Eric Young's, and as such any Copyright notices in
+ * the code are not to be removed.
+ * If this package is used in a product, Eric Young should be given attribution
+ * as the author of the parts of the library used.
+ * This can be in the form of a textual message at program startup or
+ * in documentation (online or textual) provided with the package.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * "This product includes cryptographic software written by
+ * Eric Young (eay@cryptsoft.com)"
+ * The word 'cryptographic' can be left out if the rouines from the library
+ * being used are not cryptographic related :-).
+ * 4. If you include any Windows specific code (or a derivative thereof) from
+ * the apps directory (application code) you must include an acknowledgement:
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * The licence and distribution terms for any publically available version or
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
+ * copied and put under another distribution licence
+ * [including the GNU Public Licence.]
+ */
+
+#include <stdio.h>
+#undef SSLEAY_MACROS
+#include "bio.h"
+#include "evp.h"
+#include "x509.h"
+#include "pkcs7.h"
+#include "pem.h"
+
+#ifndef WIN16
+/* The X509 functions */
+X509 *PEM_read_X509(fp,x,cb)
+FILE *fp;
+X509 **x;
+int (*cb)();
+ {
+ return((X509 *)PEM_ASN1_read((char *(*)())d2i_X509,
+ PEM_STRING_X509,fp,(char **)x,cb));
+ }
+#endif
+
+X509 *PEM_read_bio_X509(bp,x,cb)
+BIO *bp;
+X509 **x;
+int (*cb)();
+ {
+ return((X509 *)PEM_ASN1_read_bio((char *(*)())d2i_X509,
+ PEM_STRING_X509,bp,(char **)x,cb));
+ }
+
+#ifndef WIN16
+int PEM_write_X509(fp,x)
+FILE *fp;
+X509 *x;
+ {
+ return(PEM_ASN1_write((int (*)())i2d_X509,PEM_STRING_X509,fp,
+ (char *)x, NULL,NULL,0,NULL));
+ }
+#endif
+
+int PEM_write_bio_X509(bp,x)
+BIO *bp;
+X509 *x;
+ {
+ return(PEM_ASN1_write_bio((int (*)())i2d_X509,PEM_STRING_X509,bp,
+ (char *)x, NULL,NULL,0,NULL));
+ }
+
+#ifndef WIN16
+/* The X509_REQ functions */
+X509_REQ *PEM_read_X509_REQ(fp,x,cb)
+FILE *fp;
+X509_REQ **x;
+int (*cb)();
+ {
+ return((X509_REQ *)PEM_ASN1_read((char *(*)())d2i_X509_REQ,
+ PEM_STRING_X509_REQ,fp,(char **)x,cb));
+ }
+#endif
+
+X509_REQ *PEM_read_bio_X509_REQ(bp,x,cb)
+BIO *bp;
+X509_REQ **x;
+int (*cb)();
+ {
+ return((X509_REQ *)PEM_ASN1_read_bio((char *(*)())d2i_X509_REQ,
+ PEM_STRING_X509_REQ,bp,(char **)x,cb));
+ }
+
+#ifndef WIN16
+int PEM_write_X509_REQ(fp,x)
+FILE *fp;
+X509_REQ *x;
+ {
+ return(PEM_ASN1_write((int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,fp,
+ (char *)x, NULL,NULL,0,NULL));
+ }
+#endif
+
+int PEM_write_bio_X509_REQ(bp,x)
+BIO *bp;
+X509_REQ *x;
+ {
+ return(PEM_ASN1_write_bio((int (*)())i2d_X509_REQ,PEM_STRING_X509_REQ,
+ bp,(char *)x, NULL,NULL,0,NULL));
+ }
+
+#ifndef WIN16
+/* The X509_CRL functions */
+X509_CRL *PEM_read_X509_CRL(fp,x,cb)
+FILE *fp;
+X509_CRL **x;
+int (*cb)();
+ {
+ return((X509_CRL *)PEM_ASN1_read((char *(*)())d2i_X509_CRL,
+ PEM_STRING_X509_CRL,fp,(char **)x,cb));
+ }
+#endif
+
+X509_CRL *PEM_read_bio_X509_CRL(bp,x,cb)
+BIO *bp;
+X509_CRL **x;
+int (*cb)();
+ {
+ return((X509_CRL *)PEM_ASN1_read_bio((char *(*)())d2i_X509_CRL,
+ PEM_STRING_X509_CRL,bp,(char **)x,cb));
+ }
+
+#ifndef WIN16
+int PEM_write_X509_CRL(fp,x)
+FILE *fp;
+X509_CRL *x;
+ {
+ return(PEM_ASN1_write((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL,fp,
+ (char *)x, NULL,NULL,0,NULL));
+ }
+#endif
+
+int PEM_write_bio_X509_CRL(bp,x)
+BIO *bp;
+X509_CRL *x;
+ {
+ return(PEM_ASN1_write_bio((int (*)())i2d_X509_CRL,PEM_STRING_X509_CRL,
+ bp,(char *)x, NULL,NULL,0,NULL));
+ }
+
+#ifndef NO_RSA
+#ifndef WIN16
+/* The RSAPrivateKey functions */
+RSA *PEM_read_RSAPrivateKey(fp,x,cb)
+FILE *fp;
+RSA **x;
+int (*cb)();
+ {
+ return((RSA *)PEM_ASN1_read((char *(*)())d2i_RSAPrivateKey,
+ PEM_STRING_RSA,fp,(char **)x,cb));
+ }
+
+RSA *PEM_read_RSAPublicKey(fp,x,cb)
+FILE *fp;
+RSA **x;
+int (*cb)();
+ {
+ return((RSA *)PEM_ASN1_read((char *(*)())d2i_RSAPublicKey,
+ PEM_STRING_RSA_PUBLIC,fp,(char **)x,cb));
+ }
+#endif
+
+RSA *PEM_read_bio_RSAPrivateKey(bp,x,cb)
+BIO *bp;
+RSA **x;
+int (*cb)();
+ {
+ return((RSA *)PEM_ASN1_read_bio((char *(*)())d2i_RSAPrivateKey,
+ PEM_STRING_RSA,bp,(char **)x,cb));
+ }
+
+RSA *PEM_read_bio_RSAPublicKey(bp,x,cb)
+BIO *bp;
+RSA **x;
+int (*cb)();
+ {
+ return((RSA *)PEM_ASN1_read_bio((char *(*)())d2i_RSAPublicKey,
+ PEM_STRING_RSA_PUBLIC,bp,(char **)x,cb));
+ }
+
+#ifndef WIN16
+int PEM_write_RSAPrivateKey(fp,x,enc,kstr,klen,cb)
+FILE *fp;
+RSA *x;
+EVP_CIPHER *enc;
+unsigned char *kstr;
+int klen;
+int (*cb)();
+ {
+ return(PEM_ASN1_write((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,fp,
+ (char *)x,enc,kstr,klen,cb));
+ }
+
+int PEM_write_RSAPublicKey(fp,x)
+FILE *fp;
+RSA *x;
+ {
+ return(PEM_ASN1_write((int (*)())i2d_RSAPublicKey,
+ PEM_STRING_RSA_PUBLIC,fp,
+ (char *)x,NULL,NULL,0,NULL));
+ }
+#endif
+
+int PEM_write_bio_RSAPrivateKey(bp,x,enc,kstr,klen,cb)
+BIO *bp;
+RSA *x;
+EVP_CIPHER *enc;
+unsigned char *kstr;
+int klen;
+int (*cb)();
+ {
+ return(PEM_ASN1_write_bio((int (*)())i2d_RSAPrivateKey,PEM_STRING_RSA,
+ bp,(char *)x,enc,kstr,klen,cb));
+ }
+
+int PEM_write_bio_RSAPublicKey(bp,x)
+BIO *bp;
+RSA *x;
+ {
+ return(PEM_ASN1_write_bio((int (*)())i2d_RSAPublicKey,
+ PEM_STRING_RSA_PUBLIC,
+ bp,(char *)x,NULL,NULL,0,NULL));
+ }
+#endif /* !NO_RSA */
+
+#ifndef NO_DSA
+#ifndef WIN16
+/* The DSAPrivateKey functions */
+DSA *PEM_read_DSAPrivateKey(fp,x,cb)
+FILE *fp;
+DSA **x;
+int (*cb)();
+ {
+ return((DSA *)PEM_ASN1_read((char *(*)())d2i_DSAPrivateKey,
+ PEM_STRING_DSA,fp,(char **)x,cb));
+ }
+#endif
+
+DSA *PEM_read_bio_DSAPrivateKey(bp,x,cb)
+BIO *bp;
+DSA **x;
+int (*cb)();
+ {
+ return((DSA *)PEM_ASN1_read_bio((char *(*)())d2i_DSAPrivateKey,
+ PEM_STRING_DSA,bp,(char **)x,cb));
+ }
+
+#ifndef WIN16
+int PEM_write_DSAPrivateKey(fp,x,enc,kstr,klen,cb)
+FILE *fp;
+DSA *x;
+EVP_CIPHER *enc;
+unsigned char *kstr;
+int klen;
+int (*cb)();
+ {
+ return(PEM_ASN1_write((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,fp,
+ (char *)x,enc,kstr,klen,cb));
+ }
+#endif
+
+int PEM_write_bio_DSAPrivateKey(bp,x,enc,kstr,klen,cb)
+BIO *bp;
+DSA *x;
+EVP_CIPHER *enc;
+unsigned char *kstr;
+int klen;
+int (*cb)();
+ {
+ return(PEM_ASN1_write_bio((int (*)())i2d_DSAPrivateKey,PEM_STRING_DSA,
+ bp,(char *)x,enc,kstr,klen,cb));
+ }
+#endif
+
+#ifndef WIN16
+/* The PrivateKey functions */
+EVP_PKEY *PEM_read_PrivateKey(fp,x,cb)
+FILE *fp;
+EVP_PKEY **x;
+int (*cb)();
+ {
+ return((EVP_PKEY *)PEM_ASN1_read((char *(*)())d2i_PrivateKey,
+ PEM_STRING_EVP_PKEY,fp,(char **)x,cb));
+ }
+#endif
+
+EVP_PKEY *PEM_read_bio_PrivateKey(bp,x,cb)
+BIO *bp;
+EVP_PKEY **x;
+int (*cb)();
+ {
+ return((EVP_PKEY *)PEM_ASN1_read_bio((char *(*)())d2i_PrivateKey,
+ PEM_STRING_EVP_PKEY,bp,(char **)x,cb));
+ }
+
+#ifndef WIN16
+int PEM_write_PrivateKey(fp,x,enc,kstr,klen,cb)
+FILE *fp;
+EVP_PKEY *x;
+EVP_CIPHER *enc;
+unsigned char *kstr;
+int klen;
+int (*cb)();
+ {
+ return(PEM_ASN1_write((int (*)())i2d_PrivateKey,
+ ((x->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA),
+ fp,(char *)x,enc,kstr,klen,cb));
+ }
+#endif
+
+int PEM_write_bio_PrivateKey(bp,x,enc,kstr,klen,cb)
+BIO *bp;
+EVP_PKEY *x;
+EVP_CIPHER *enc;
+unsigned char *kstr;
+int klen;
+int (*cb)();
+ {
+ return(PEM_ASN1_write_bio((int (*)())i2d_PrivateKey,
+ ((x->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA),
+ bp,(char *)x,enc,kstr,klen,cb));
+ }
+
+#ifndef WIN16
+/* The PKCS7 functions */
+PKCS7 *PEM_read_PKCS7(fp,x,cb)
+FILE *fp;
+PKCS7 **x;
+int (*cb)();
+ {
+ return((PKCS7 *)PEM_ASN1_read((char *(*)())d2i_PKCS7,
+ PEM_STRING_PKCS7,fp,(char **)x,cb));
+ }
+#endif
+
+PKCS7 *PEM_read_bio_PKCS7(bp,x,cb)
+BIO *bp;
+PKCS7 **x;
+int (*cb)();
+ {
+ return((PKCS7 *)PEM_ASN1_read_bio((char *(*)())d2i_PKCS7,
+ PEM_STRING_PKCS7,bp,(char **)x,cb));
+ }
+
+#ifndef WIN16
+int PEM_write_PKCS7(fp,x)
+FILE *fp;
+PKCS7 *x;
+ {
+ return(PEM_ASN1_write((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,fp,
+ (char *)x, NULL,NULL,0,NULL));
+ }
+#endif
+
+int PEM_write_bio_PKCS7(bp,x)
+BIO *bp;
+PKCS7 *x;
+ {
+ return(PEM_ASN1_write_bio((int (*)())i2d_PKCS7,PEM_STRING_PKCS7,bp,
+ (char *)x, NULL,NULL,0,NULL));
+ }
+
+#ifndef NO_DH
+#ifndef WIN16
+/* The DHparams functions */
+DH *PEM_read_DHparams(fp,x,cb)
+FILE *fp;
+DH **x;
+int (*cb)();
+ {
+ return((DH *)PEM_ASN1_read((char *(*)())d2i_DHparams,
+ PEM_STRING_DHPARAMS,fp,(char **)x,cb));
+ }
+#endif
+
+DH *PEM_read_bio_DHparams(bp,x,cb)
+BIO *bp;
+DH **x;
+int (*cb)();
+ {
+ return((DH *)PEM_ASN1_read_bio((char *(*)())d2i_DHparams,
+ PEM_STRING_DHPARAMS,bp,(char **)x,cb));
+ }
+
+#ifndef WIN16
+int PEM_write_DHparams(fp,x)
+FILE *fp;
+DH *x;
+ {
+ return(PEM_ASN1_write((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,fp,
+ (char *)x, NULL,NULL,0,NULL));
+ }
+#endif
+
+int PEM_write_bio_DHparams(bp,x)
+BIO *bp;
+DH *x;
+ {
+ return(PEM_ASN1_write_bio((int (*)())i2d_DHparams,PEM_STRING_DHPARAMS,
+ bp,(char *)x, NULL,NULL,0,NULL));
+ }
+#endif
+
+#ifndef NO_DSA
+#ifndef WIN16
+/* The DSAparams functions */
+DSA *PEM_read_DSAparams(fp,x,cb)
+FILE *fp;
+DSA **x;
+int (*cb)();
+ {
+ return((DSA *)PEM_ASN1_read((char *(*)())d2i_DSAparams,
+ PEM_STRING_DSAPARAMS,fp,(char **)x,cb));
+ }
+#endif
+
+DSA *PEM_read_bio_DSAparams(bp,x,cb)
+BIO *bp;
+DSA **x;
+int (*cb)();
+ {
+ return((DSA *)PEM_ASN1_read_bio((char *(*)())d2i_DSAparams,
+ PEM_STRING_DSAPARAMS,bp,(char **)x,cb));
+ }
+
+#ifndef WIN16
+int PEM_write_DSAparams(fp,x)
+FILE *fp;
+DSA *x;
+ {
+ return(PEM_ASN1_write((int (*)())i2d_DSAparams,PEM_STRING_DSAPARAMS,fp,
+ (char *)x, NULL,NULL,0,NULL));
+ }
+#endif
+
+int PEM_write_bio_DSAparams(bp,x)
+BIO *bp;
+DSA *x;
+ {
+ return(PEM_ASN1_write_bio((int (*)())i2d_DSAparams,PEM_STRING_DSAPARAMS,
+ bp,(char *)x, NULL,NULL,0,NULL));
+ }
+#endif
+
diff --git a/crypto/pem/pem_err.c b/crypto/pem/pem_err.c
new file mode 100644
index 0000000000..b8d95204ea
--- /dev/null
+++ b/crypto/pem/pem_err.c
@@ -0,0 +1,116 @@
+/* lib/pem/pem_err.c */
+/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+ * All rights reserved.
+ *
+ * This package is an SSL implementation written
+ * by Eric Young (eay@cryptsoft.com).
+ * The implementation was written so as to conform with Netscapes SSL.
+ *
+ * This library is free for commercial and non-commercial use as long as
+ * the following conditions are aheared to. The following conditions
+ * apply to all code found in this distribution, be it the RC4, RSA,
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
+ * included with this distribution is covered by the same copyright terms
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
+ *
+ * Copyright remains Eric Young's, and as such any Copyright notices in
+ * the code are not to be removed.
+ * If this package is used in a product, Eric Young should be given attribution
+ * as the author of the parts of the library used.
+ * This can be in the form of a textual message at program startup or
+ * in documentation (online or textual) provided with the package.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * "This product includes cryptographic software written by
+ * Eric Young (eay@cryptsoft.com)"
+ * The word 'cryptographic' can be left out if the rouines from the library
+ * being used are not cryptographic related :-).
+ * 4. If you include any Windows specific code (or a derivative thereof) from
+ * the apps directory (application code) you must include an acknowledgement:
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * The licence and distribution terms for any publically available version or
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
+ * copied and put under another distribution licence
+ * [including the GNU Public Licence.]
+ */
+#include <stdio.h>
+#include "err.h"
+#include "pem.h"
+
+/* BEGIN ERROR CODES */
+static ERR_STRING_DATA PEM_str_functs[]=
+ {
+{ERR_PACK(0,PEM_F_DEF_CALLBACK,0), "DEF_CALLBACK"},
+{ERR_PACK(0,PEM_F_LOAD_IV,0), "LOAD_IV"},
+{ERR_PACK(0,PEM_F_PEM_ASN1_READ,0), "PEM_ASN1_read"},
+{ERR_PACK(0,PEM_F_PEM_ASN1_READ_BIO,0), "PEM_ASN1_read_bio"},
+{ERR_PACK(0,PEM_F_PEM_ASN1_WRITE,0), "PEM_ASN1_write"},
+{ERR_PACK(0,PEM_F_PEM_ASN1_WRITE_BIO,0), "PEM_ASN1_write_bio"},
+{ERR_PACK(0,PEM_F_PEM_DO_HEADER,0), "PEM_do_header"},
+{ERR_PACK(0,PEM_F_PEM_GET_EVP_CIPHER_INFO,0), "PEM_get_EVP_CIPHER_INFO"},
+{ERR_PACK(0,PEM_F_PEM_READ,0), "PEM_read"},
+{ERR_PACK(0,PEM_F_PEM_READ_BIO,0), "PEM_read_bio"},
+{ERR_PACK(0,PEM_F_PEM_SEALFINAL,0), "PEM_SealFinal"},
+{ERR_PACK(0,PEM_F_PEM_SEALINIT,0), "PEM_SealInit"},
+{ERR_PACK(0,PEM_F_PEM_SIGNFINAL,0), "PEM_SignFinal"},
+{ERR_PACK(0,PEM_F_PEM_WRITE,0), "PEM_write"},
+{ERR_PACK(0,PEM_F_PEM_WRITE_BIO,0), "PEM_write_bio"},
+{ERR_PACK(0,PEM_F_PEM_X509_INFO_READ,0), "PEM_X509_INFO_read"},
+{ERR_PACK(0,PEM_F_PEM_X509_INFO_READ_BIO,0), "PEM_X509_INFO_read_bio"},
+{ERR_PACK(0,PEM_F_PEM_X509_INFO_WRITE_BIO,0), "PEM_X509_INFO_write_bio"},
+{0,NULL},
+ };
+
+static ERR_STRING_DATA PEM_str_reasons[]=
+ {
+{PEM_R_BAD_BASE64_DECODE ,"bad base64 decode"},
+{PEM_R_BAD_DECRYPT ,"bad decrypt"},
+{PEM_R_BAD_END_LINE ,"bad end line"},
+{PEM_R_BAD_IV_CHARS ,"bad iv chars"},
+{PEM_R_BAD_PASSWORD_READ ,"bad password read"},
+{PEM_R_NOT_DEK_INFO ,"not dek info"},
+{PEM_R_NOT_ENCRYPTED ,"not encrypted"},
+{PEM_R_NOT_PROC_TYPE ,"not proc type"},
+{PEM_R_NO_START_LINE ,"no start line"},
+{PEM_R_PROBLEMS_GETTING_PASSWORD ,"problems getting password"},
+{PEM_R_PUBLIC_KEY_NO_RSA ,"public key no rsa"},
+{PEM_R_READ_KEY ,"read key"},
+{PEM_R_SHORT_HEADER ,"short header"},
+{PEM_R_UNSUPPORTED_CIPHER ,"unsupported cipher"},
+{PEM_R_UNSUPPORTED_ENCRYPTION ,"unsupported encryption"},
+{0,NULL},
+ };
+
+void ERR_load_PEM_strings()
+ {
+ static int init=1;
+
+ if (init)
+ {
+ init=0;
+ ERR_load_strings(ERR_LIB_PEM,PEM_str_functs);
+ ERR_load_strings(ERR_LIB_PEM,PEM_str_reasons);
+ }
+ }
diff --git a/crypto/pem/pem_info.c b/crypto/pem/pem_info.c
new file mode 100644
index 0000000000..aafd9ce7be
--- /dev/null
+++ b/crypto/pem/pem_info.c
@@ -0,0 +1,365 @@
+/* crypto/pem/pem_info.c */
+/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+ * All rights reserved.
+ *
+ * This package is an SSL implementation written
+ * by Eric Young (eay@cryptsoft.com).
+ * The implementation was written so as to conform with Netscapes SSL.
+ *
+ * This library is free for commercial and non-commercial use as long as
+ * the following conditions are aheared to. The following conditions
+ * apply to all code found in this distribution, be it the RC4, RSA,
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
+ * included with this distribution is covered by the same copyright terms
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
+ *
+ * Copyright remains Eric Young's, and as such any Copyright notices in
+ * the code are not to be removed.
+ * If this package is used in a product, Eric Young should be given attribution
+ * as the author of the parts of the library used.
+ * This can be in the form of a textual message at program startup or
+ * in documentation (online or textual) provided with the package.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * "This product includes cryptographic software written by
+ * Eric Young (eay@cryptsoft.com)"
+ * The word 'cryptographic' can be left out if the rouines from the library
+ * being used are not cryptographic related :-).
+ * 4. If you include any Windows specific code (or a derivative thereof) from
+ * the apps directory (application code) you must include an acknowledgement:
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * The licence and distribution terms for any publically available version or
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
+ * copied and put under another distribution licence
+ * [including the GNU Public Licence.]
+ */
+
+#include <stdio.h>
+#include "cryptlib.h"
+#include "buffer.h"
+#include "objects.h"
+#include "evp.h"
+#include "x509.h"
+#include "pem.h"
+
+#ifndef WIN16
+STACK *PEM_X509_INFO_read(fp,sk,cb)
+FILE *fp;
+STACK *sk;
+int (*cb)();
+ {
+ BIO *b;
+ STACK *ret;
+
+ if ((b=BIO_new(BIO_s_file())) == NULL)
+ {
+ PEMerr(PEM_F_PEM_X509_INFO_READ,ERR_R_BUF_LIB);
+ return(0);
+ }
+ BIO_set_fp(b,fp,BIO_NOCLOSE);
+ ret=PEM_X509_INFO_read_bio(b,sk,cb);
+ BIO_free(b);
+ return(ret);
+ }
+#endif
+
+STACK *PEM_X509_INFO_read_bio(bp,sk,cb)
+BIO *bp;
+STACK *sk;
+int (*cb)();
+ {
+ X509_INFO *xi=NULL;
+ char *name=NULL,*header=NULL,**pp;
+ unsigned char *data=NULL,*p;
+ long len,error=0;
+ int ok=0;
+ STACK *ret=NULL;
+ unsigned int i,raw;
+ char *(*d2i)();
+
+ if (sk == NULL)
+ {
+ if ((ret=sk_new_null()) == NULL)
+ {
+ PEMerr(PEM_F_PEM_X509_INFO_READ_BIO,ERR_R_MALLOC_FAILURE);
+ goto err;
+ }
+ }
+ else
+ ret=sk;
+
+ if ((xi=X509_INFO_new()) == NULL) goto err;
+ for (;;)
+ {
+ raw=0;
+ i=PEM_read_bio(bp,&name,&header,&data,&len);
+ if (i == 0)
+ {
+ error=ERR_GET_REASON(ERR_peek_error());
+ if (error == PEM_R_NO_START_LINE)
+ {
+ ERR_clear_error();
+ break;
+ }
+ goto err;
+ }
+start:
+ if ( (strcmp(name,PEM_STRING_X509) == 0) ||
+ (strcmp(name,PEM_STRING_X509_OLD) == 0))
+ {
+ d2i=(char *(*)())d2i_X509;
+ if (xi->x509 != NULL)
+ {
+ if (!sk_push(ret,(char *)xi)) goto err;
+ if ((xi=X509_INFO_new()) == NULL) goto err;
+ goto start;
+ }
+ pp=(char **)&(xi->x509);
+ }
+ else if (strcmp(name,PEM_STRING_X509_CRL) == 0)
+ {
+ d2i=(char *(*)())d2i_X509_CRL;
+ if (xi->crl != NULL)
+ {
+ if (!sk_push(ret,(char *)xi)) goto err;
+ if ((xi=X509_INFO_new()) == NULL) goto err;
+ goto start;
+ }
+ pp=(char **)&(xi->crl);
+ }
+ else
+#ifndef NO_RSA
+ if (strcmp(name,PEM_STRING_RSA) == 0)
+ {
+ d2i=(char *(*)())d2i_RSAPrivateKey;
+ if (xi->x_pkey != NULL)
+ {
+ if (!sk_push(ret,(char *)xi)) goto err;
+ if ((xi=X509_INFO_new()) == NULL) goto err;
+ goto start;
+ }
+
+ xi->enc_data=NULL;
+ xi->enc_len=0;
+
+ xi->x_pkey=X509_PKEY_new();
+ if ((xi->x_pkey->dec_pkey=EVP_PKEY_new()) == NULL)
+ goto err;
+ xi->x_pkey->dec_pkey->type=EVP_PKEY_RSA;
+ pp=(char **)&(xi->x_pkey->dec_pkey->pkey.rsa);
+ if ((int)strlen(header) > 10) /* assume encrypted */
+ raw=1;
+ }
+ else
+#endif
+#ifndef NO_DSA
+ if (strcmp(name,PEM_STRING_DSA) == 0)
+ {
+ d2i=(char *(*)())d2i_DSAPrivateKey;
+ if (xi->x_pkey != NULL)
+ {
+ if (!sk_push(ret,(char *)xi)) goto err;
+ if ((xi=X509_INFO_new()) == NULL) goto err;
+ goto start;
+ }
+
+ xi->enc_data=NULL;
+ xi->enc_len=0;
+
+ xi->x_pkey=X509_PKEY_new();
+ if ((xi->x_pkey->dec_pkey=EVP_PKEY_new()) == NULL)
+ goto err;
+ xi->x_pkey->dec_pkey->type=EVP_PKEY_DSA;
+ pp=(char **)&(xi->x_pkey->dec_pkey->pkey.dsa);
+ if ((int)strlen(header) > 10) /* assume encrypted */
+ raw=1;
+ }
+ else
+#endif
+ {
+ d2i=NULL;
+ pp=NULL;
+ }
+
+ if (d2i != NULL)
+ {
+ if (!raw)
+ {
+ EVP_CIPHER_INFO cipher;
+
+ if (!PEM_get_EVP_CIPHER_INFO(header,&cipher))
+ goto err;
+ if (!PEM_do_header(&cipher,data,&len,cb))
+ goto err;
+ p=data;
+ if (d2i(pp,&p,len) == NULL)
+ {
+ PEMerr(PEM_F_PEM_X509_INFO_READ_BIO,ERR_R_ASN1_LIB);
+ goto err;
+ }
+ }
+ else
+ { /* encrypted RSA data */
+ if (!PEM_get_EVP_CIPHER_INFO(header,
+ &xi->enc_cipher)) goto err;
+ xi->enc_data=(char *)data;
+ xi->enc_len=(int)len;
+ data=NULL;
+ }
+ }
+ else {
+ /* unknown */
+ }
+ if (name != NULL) Free(name);
+ if (header != NULL) Free(header);
+ if (data != NULL) Free(data);
+ name=NULL;
+ header=NULL;
+ data=NULL;
+ }
+
+ /* if the last one hasn't been pushed yet and there is anything
+ * in it then add it to the stack ...
+ */
+ if ((xi->x509 != NULL) || (xi->crl != NULL) ||
+ (xi->x_pkey != NULL) || (xi->enc_data != NULL))
+ {
+ if (!sk_push(ret,(char *)xi)) goto err;
+ xi=NULL;
+ }
+ ok=1;
+err:
+ if (xi != NULL) X509_INFO_free(xi);
+ if (!ok)
+ {
+ for (i=0; ((int)i)<sk_num(ret); i++)
+ {
+ xi=(X509_INFO *)sk_value(ret,i);
+ X509_INFO_free(xi);
+ }
+ if (ret != sk) sk_free(ret);
+ ret=NULL;
+ }
+
+ if (name != NULL) Free(name);
+ if (header != NULL) Free(header);
+ if (data != NULL) Free(data);
+ return(ret);
+ }
+
+
+/* A TJH addition */
+int PEM_X509_INFO_write_bio(bp,xi,enc,kstr,klen,cb)
+BIO *bp;
+X509_INFO *xi;
+EVP_CIPHER *enc;
+unsigned char *kstr;
+int klen;
+int (*cb)();
+ {
+ EVP_CIPHER_CTX ctx;
+ int i,ret=0;
+ unsigned char *data=NULL;
+ char *objstr=NULL;
+#define PEM_BUFSIZE 1024
+ char buf[PEM_BUFSIZE];
+ unsigned char *iv=NULL;
+
+ if (enc != NULL)
+ {
+ objstr=OBJ_nid2sn(EVP_CIPHER_nid(enc));
+ if (objstr == NULL)
+ {
+ PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,PEM_R_UNSUPPORTED_CIPHER);
+ goto err;
+ }
+ }
+
+ /* now for the fun part ... if we have a private key then
+ * we have to be able to handle a not-yet-decrypted key
+ * being written out correctly ... if it is decrypted or
+ * it is non-encrypted then we use the base code
+ */
+ if (xi->x_pkey!=NULL)
+ {
+ if ( (xi->enc_data!=NULL) && (xi->enc_len>0) )
+ {
+ /* copy from wierdo names into more normal things */
+ iv=xi->enc_cipher.iv;
+ data=(unsigned char *)xi->enc_data;
+ i=xi->enc_len;
+
+ /* we take the encryption data from the
+ * internal stuff rather than what the
+ * user has passed us ... as we have to
+ * match exactly for some strange reason
+ */
+ objstr=OBJ_nid2sn(
+ EVP_CIPHER_nid(xi->enc_cipher.cipher));
+ if (objstr == NULL)
+ {
+ PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,PEM_R_UNSUPPORTED_CIPHER);
+ goto err;
+ }
+
+ /* create the right magic header stuff */
+ buf[0]='\0';
+ PEM_proc_type(buf,PEM_TYPE_ENCRYPTED);
+ PEM_dek_info(buf,objstr,8,(char *)iv);
+
+ /* use the normal code to write things out */
+ i=PEM_write_bio(bp,PEM_STRING_RSA,buf,data,i);
+ if (i <= 0) goto err;
+ }
+ else
+ {
+ /* Add DSA/DH */
+#ifndef NO_RSA
+ /* normal optionally encrypted stuff */
+ if (PEM_write_bio_RSAPrivateKey(bp,
+ xi->x_pkey->dec_pkey->pkey.rsa,
+ enc,kstr,klen,cb)<=0)
+ goto err;
+#endif
+ }
+ }
+
+ /* if we have a certificate then write it out now */
+ if ((xi->x509 != NULL) || (PEM_write_bio_X509(bp,xi->x509) <= 0))
+ goto err;
+
+ /* we are ignoring anything else that is loaded into the X509_INFO
+ * structure for the moment ... as I don't need it so I'm not
+ * coding it here and Eric can do it when this makes it into the
+ * base library --tjh
+ */
+
+ ret=1;
+
+err:
+ memset((char *)&ctx,0,sizeof(ctx));
+ memset(buf,0,PEM_BUFSIZE);
+ return(ret);
+ }
diff --git a/crypto/pem/pem_lib.c b/crypto/pem/pem_lib.c
new file mode 100644
index 0000000000..1397d83113
--- /dev/null
+++ b/crypto/pem/pem_lib.c
@@ -0,0 +1,757 @@
+/* crypto/pem/pem_lib.c */
+/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+ * All rights reserved.
+ *
+ * This package is an SSL implementation written
+ * by Eric Young (eay@cryptsoft.com).
+ * The implementation was written so as to conform with Netscapes SSL.
+ *
+ * This library is free for commercial and non-commercial use as long as
+ * the following conditions are aheared to. The following conditions
+ * apply to all code found in this distribution, be it the RC4, RSA,
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
+ * included with this distribution is covered by the same copyright terms
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
+ *
+ * Copyright remains Eric Young's, and as such any Copyright notices in
+ * the code are not to be removed.
+ * If this package is used in a product, Eric Young should be given attribution
+ * as the author of the parts of the library used.
+ * This can be in the form of a textual message at program startup or
+ * in documentation (online or textual) provided with the package.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * "This product includes cryptographic software written by
+ * Eric Young (eay@cryptsoft.com)"
+ * The word 'cryptographic' can be left out if the rouines from the library
+ * being used are not cryptographic related :-).
+ * 4. If you include any Windows specific code (or a derivative thereof) from
+ * the apps directory (application code) you must include an acknowledgement:
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * The licence and distribution terms for any publically available version or
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
+ * copied and put under another distribution licence
+ * [including the GNU Public Licence.]
+ */
+
+#include <stdio.h>
+#include "cryptlib.h"
+#include "buffer.h"
+#include "objects.h"
+#include "evp.h"
+#include "rand.h"
+#include "x509.h"
+#include "pem.h"
+#ifndef NO_DES
+#include "des.h"
+#endif
+
+char *PEM_version="PEM part of SSLeay 0.8.1b 29-Jun-1998";
+
+#define MIN_LENGTH 4
+
+/* PEMerr(PEM_F_PEM_WRITE_BIO,ERR_R_MALLOC_FAILURE);
+ * PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
+ */
+
+#ifndef NOPROTO
+static int def_callback(char *buf, int num, int w);
+static int load_iv(unsigned char **fromp,unsigned char *to, int num);
+#else
+static int def_callback();
+static int load_iv();
+#endif
+
+static int def_callback(buf, num, w)
+char *buf;
+int num;
+int w;
+ {
+#ifdef WIN16
+ /* We should not ever call the default callback routine from
+ * windows. */
+ PEMerr(PEM_F_DEF_CALLBACK,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+ return(-1);
+#else
+ int i,j;
+ char *prompt;
+
+ prompt=EVP_get_pw_prompt();
+ if (prompt == NULL)
+ prompt="Enter PEM pass phrase:";
+
+ for (;;)
+ {
+ i=EVP_read_pw_string(buf,num,prompt,w);
+ if (i != 0)
+ {
+ PEMerr(PEM_F_DEF_CALLBACK,PEM_R_PROBLEMS_GETTING_PASSWORD);
+ memset(buf,0,(unsigned int)num);
+ return(-1);
+ }
+ j=strlen(buf);
+ if (j < MIN_LENGTH)
+ {
+ fprintf(stderr,"phrase is too short, needs to be at least %d chars\n",MIN_LENGTH);
+ }
+ else
+ break;
+ }
+ return(j);
+#endif
+ }
+
+void PEM_proc_type(buf, type)
+char *buf;
+int type;
+ {
+ char *str;
+
+ if (type == PEM_TYPE_ENCRYPTED)
+ str="ENCRYPTED";
+ else if (type == PEM_TYPE_MIC_CLEAR)
+ str="MIC-CLEAR";
+ else if (type == PEM_TYPE_MIC_ONLY)
+ str="MIC-ONLY";
+ else
+ str="BAD-TYPE";
+
+ strcat(buf,"Proc-Type: 4,");
+ strcat(buf,str);
+ strcat(buf,"\n");
+ }
+
+void PEM_dek_info(buf, type, len, str)
+char *buf;
+char *type;
+int len;
+char *str;
+ {
+ static unsigned char map[17]="0123456789ABCDEF";
+ long i;
+ int j;
+
+ strcat(buf,"DEK-Info: ");
+ strcat(buf,type);
+ strcat(buf,",");
+ j=strlen(buf);
+ for (i=0; i<len; i++)
+ {
+ buf[j+i*2] =map[(str[i]>>4)&0x0f];
+ buf[j+i*2+1]=map[(str[i] )&0x0f];
+ }
+ buf[j+i*2]='\n';
+ buf[j+i*2+1]='\0';
+ }
+
+#ifndef WIN16
+char *PEM_ASN1_read(d2i,name,fp, x, cb)
+char *(*d2i)();
+char *name;
+FILE *fp;
+char **x;
+int (*cb)();
+ {
+ BIO *b;
+ char *ret;
+
+ if ((b=BIO_new(BIO_s_file())) == NULL)
+ {
+ PEMerr(PEM_F_PEM_ASN1_READ,ERR_R_BUF_LIB);
+ return(0);
+ }
+ BIO_set_fp(b,fp,BIO_NOCLOSE);
+ ret=PEM_ASN1_read_bio(d2i,name,b,x,cb);
+ BIO_free(b);
+ return(ret);
+ }
+#endif
+
+char *PEM_ASN1_read_bio(d2i,name,bp, x, cb)
+char *(*d2i)();
+char *name;
+BIO *bp;
+char **x;
+int (*cb)();
+ {
+ EVP_CIPHER_INFO cipher;
+ char *nm=NULL,*header=NULL;
+ unsigned char *p=NULL,*data=NULL;
+ long len;
+ char *ret=NULL;
+
+ for (;;)
+ {
+ if (!PEM_read_bio(bp,&nm,&header,&data,&len)) return(NULL);
+ if ( (strcmp(nm,name) == 0) ||
+ ((strcmp(nm,PEM_STRING_RSA) == 0) &&
+ (strcmp(name,PEM_STRING_EVP_PKEY) == 0)) ||
+ ((strcmp(nm,PEM_STRING_DSA) == 0) &&
+ (strcmp(name,PEM_STRING_EVP_PKEY) == 0)) ||
+ ((strcmp(nm,PEM_STRING_X509_OLD) == 0) &&
+ (strcmp(name,PEM_STRING_X509) == 0)) ||
+ ((strcmp(nm,PEM_STRING_X509_REQ_OLD) == 0) &&
+ (strcmp(name,PEM_STRING_X509_REQ) == 0))
+ )
+ break;
+ Free(nm);
+ Free(header);
+ Free(data);
+ }
+ if (!PEM_get_EVP_CIPHER_INFO(header,&cipher)) goto err;
+ if (!PEM_do_header(&cipher,data,&len,cb)) goto err;
+ p=data;
+ if (strcmp(name,PEM_STRING_EVP_PKEY) == 0)
+ {
+ if (strcmp(nm,PEM_STRING_RSA) == 0)
+ ret=d2i(EVP_PKEY_RSA,x,&p,len);
+ else if (strcmp(nm,PEM_STRING_DSA) == 0)
+ ret=d2i(EVP_PKEY_DSA,x,&p,len);
+ }
+ else
+ ret=d2i(x,&p,len);
+ if (ret == NULL)
+ PEMerr(PEM_F_PEM_ASN1_READ_BIO,ERR_R_ASN1_LIB);
+err:
+ Free(nm);
+ Free(header);
+ Free(data);
+ return(ret);
+ }
+
+#ifndef WIN16
+int PEM_ASN1_write(i2d,name,fp, x, enc, kstr, klen, callback)
+int (*i2d)();
+char *name;
+FILE *fp;
+char *x;
+EVP_CIPHER *enc;
+unsigned char *kstr;
+int klen;
+int (*callback)();
+ {
+ BIO *b;
+ int ret;
+
+ if ((b=BIO_new(BIO_s_file())) == NULL)
+ {
+ PEMerr(PEM_F_PEM_ASN1_WRITE,ERR_R_BUF_LIB);
+ return(0);
+ }
+ BIO_set_fp(b,fp,BIO_NOCLOSE);
+ ret=PEM_ASN1_write_bio(i2d,name,b,x,enc,kstr,klen,callback);
+ BIO_free(b);
+ return(ret);
+ }
+#endif
+
+int PEM_ASN1_write_bio(i2d,name,bp, x, enc, kstr, klen, callback)
+int (*i2d)();
+char *name;
+BIO *bp;
+char *x;
+EVP_CIPHER *enc;
+unsigned char *kstr;
+int klen;
+int (*callback)();
+ {
+ EVP_CIPHER_CTX ctx;
+ int dsize=0,i,j,ret=0;
+ unsigned char *p,*data=NULL;
+ char *objstr=NULL;
+#define PEM_BUFSIZE 1024
+ char buf[PEM_BUFSIZE];
+ unsigned char key[EVP_MAX_KEY_LENGTH];
+ unsigned char iv[EVP_MAX_IV_LENGTH];
+
+ if (enc != NULL)
+ {
+ objstr=OBJ_nid2sn(EVP_CIPHER_nid(enc));
+ if (objstr == NULL)
+ {
+ PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,PEM_R_UNSUPPORTED_CIPHER);
+ goto err;
+ }
+ }
+
+ dsize=i2d(x,NULL);
+ /* dzise + 8 bytes are needed */
+ data=(unsigned char *)Malloc((unsigned int)dsize+20);
+ if (data == NULL)
+ {
+ PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,ERR_R_MALLOC_FAILURE);
+ goto err;
+ }
+ p=data;
+ i=i2d(x,&p);
+
+ if (enc != NULL)
+ {
+ if (kstr == NULL)
+ {
+ if (callback == NULL)
+ klen=def_callback(buf,PEM_BUFSIZE,1);
+ else
+ klen=(*callback)(buf,PEM_BUFSIZE,1);
+ if (klen <= 0)
+ {
+ PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,PEM_R_READ_KEY);
+ goto err;
+ }
+ kstr=(unsigned char *)buf;
+ }
+ RAND_seed(data,i);/* put in the RSA key. */
+ RAND_bytes(iv,8); /* Generate a salt */
+ /* The 'iv' is used as the iv and as a salt. It is
+ * NOT taken from the BytesToKey function */
+ EVP_BytesToKey(enc,EVP_md5(),iv,kstr,klen,1,key,NULL);
+
+ if (kstr == (unsigned char *)buf) memset(buf,0,PEM_BUFSIZE);
+
+ buf[0]='\0';
+ PEM_proc_type(buf,PEM_TYPE_ENCRYPTED);
+ PEM_dek_info(buf,objstr,8,(char *)iv);
+ /* k=strlen(buf); */
+
+ EVP_EncryptInit(&ctx,enc,key,iv);
+ EVP_EncryptUpdate(&ctx,data,&j,data,i);
+ EVP_EncryptFinal(&ctx,&(data[j]),&i);
+ i+=j;
+ ret=1;
+ }
+ else
+ {
+ ret=1;
+ buf[0]='\0';
+ }
+ i=PEM_write_bio(bp,name,buf,data,i);
+ if (i <= 0) ret=0;
+err:
+ memset(key,0,sizeof(key));
+ memset(iv,0,sizeof(iv));
+ memset((char *)&ctx,0,sizeof(ctx));
+ memset(buf,0,PEM_BUFSIZE);
+ memset(data,0,(unsigned int)dsize);
+ Free(data);
+ return(ret);
+ }
+
+int PEM_do_header(cipher, data, plen, callback)
+EVP_CIPHER_INFO *cipher;
+unsigned char *data;
+long *plen;
+int (*callback)();
+ {
+ int i,j,o,klen;
+ long len;
+ EVP_CIPHER_CTX ctx;
+ unsigned char key[EVP_MAX_KEY_LENGTH];
+ char buf[PEM_BUFSIZE];
+
+ len= *plen;
+
+ if (cipher->cipher == NULL) return(1);
+ if (callback == NULL)
+ klen=def_callback(buf,PEM_BUFSIZE,0);
+ else
+ klen=callback(buf,PEM_BUFSIZE,0);
+ if (klen <= 0)
+ {
+ PEMerr(PEM_F_PEM_DO_HEADER,PEM_R_BAD_PASSWORD_READ);
+ return(0);
+ }
+ EVP_BytesToKey(cipher->cipher,EVP_md5(),&(cipher->iv[0]),
+ (unsigned char *)buf,klen,1,key,NULL);
+
+ j=(int)len;
+ EVP_DecryptInit(&ctx,cipher->cipher,key,&(cipher->iv[0]));
+ EVP_DecryptUpdate(&ctx,data,&i,data,j);
+ o=EVP_DecryptFinal(&ctx,&(data[i]),&j);
+ EVP_CIPHER_CTX_cleanup(&ctx);
+ memset((char *)buf,0,sizeof(buf));
+ memset((char *)key,0,sizeof(key));
+ j+=i;
+ if (!o)
+ {
+ PEMerr(PEM_F_PEM_DO_HEADER,PEM_R_BAD_DECRYPT);
+ return(0);
+ }
+ *plen=j;
+ return(1);
+ }
+
+int PEM_get_EVP_CIPHER_INFO(header,cipher)
+char *header;
+EVP_CIPHER_INFO *cipher;
+ {
+ int o;
+ EVP_CIPHER *enc=NULL;
+ char *p,c;
+
+ cipher->cipher=NULL;
+ if ((header == NULL) || (*header == '\0') || (*header == '\n'))
+ return(1);
+ if (strncmp(header,"Proc-Type: ",11) != 0)
+ { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_NOT_PROC_TYPE); return(0); }
+ header+=11;
+ if (*header != '4') return(0); header++;
+ if (*header != ',') return(0); header++;
+ if (strncmp(header,"ENCRYPTED",9) != 0)
+ { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_NOT_ENCRYPTED); return(0); }
+ for (; (*header != '\n') && (*header != '\0'); header++)
+ ;
+ if (*header == '\0')
+ { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_SHORT_HEADER); return(0); }
+ header++;
+ if (strncmp(header,"DEK-Info: ",10) != 0)
+ { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_NOT_DEK_INFO); return(0); }
+ header+=10;
+
+ p=header;
+ for (;;)
+ {
+ c= *header;
+ if (!( ((c >= 'A') && (c <= 'Z')) || (c == '-') ||
+ ((c >= '0') && (c <= '9'))))
+ break;
+ header++;
+ }
+ *header='\0';
+ o=OBJ_sn2nid(p);
+ cipher->cipher=enc=EVP_get_cipherbyname(p);
+ *header=c;
+ header++;
+
+ if (enc == NULL)
+ {
+ PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_UNSUPPORTED_ENCRYPTION);
+ return(0);
+ }
+ if (!load_iv((unsigned char **)&header,&(cipher->iv[0]),8)) return(0);
+
+ return(1);
+ }
+
+static int load_iv(fromp,to,num)
+unsigned char **fromp,*to;
+int num;
+ {
+ int v,i;
+ unsigned char *from;
+
+ from= *fromp;
+ for (i=0; i<num; i++) to[i]=0;
+ num*=2;
+ for (i=0; i<num; i++)
+ {
+ if ((*from >= '0') && (*from <= '9'))
+ v= *from-'0';
+ else if ((*from >= 'A') && (*from <= 'F'))
+ v= *from-'A'+10;
+ else if ((*from >= 'a') && (*from <= 'f'))
+ v= *from-'a'+10;
+ else
+ {
+ PEMerr(PEM_F_LOAD_IV,PEM_R_BAD_IV_CHARS);
+ return(0);
+ }
+ from++;
+ to[i/2]|=v<<(long)((!(i&1))*4);
+ }
+
+ *fromp=from;
+ return(1);
+ }
+
+#ifndef WIN16
+int PEM_write(fp, name, header, data,len)
+FILE *fp;
+char *name;
+char *header;
+unsigned char *data;
+long len;
+ {
+ BIO *b;
+ int ret;
+
+ if ((b=BIO_new(BIO_s_file())) == NULL)
+ {
+ PEMerr(PEM_F_PEM_WRITE,ERR_R_BUF_LIB);
+ return(0);
+ }
+ BIO_set_fp(b,fp,BIO_NOCLOSE);
+ ret=PEM_write_bio(b, name, header, data,len);
+ BIO_free(b);
+ return(ret);
+ }
+#endif
+
+int PEM_write_bio(bp, name, header, data,len)
+BIO *bp;
+char *name;
+char *header;
+unsigned char *data;
+long len;
+ {
+ int nlen,n,i,j,outl;
+ unsigned char *buf;
+ EVP_ENCODE_CTX ctx;
+ int reason=ERR_R_BUF_LIB;
+
+ EVP_EncodeInit(&ctx);
+ nlen=strlen(name);
+
+ if ( (BIO_write(bp,"-----BEGIN ",11) != 11) ||
+ (BIO_write(bp,name,nlen) != nlen) ||
+ (BIO_write(bp,"-----\n",6) != 6))
+ goto err;
+
+ i=strlen(header);
+ if (i > 0)
+ {
+ if ( (BIO_write(bp,header,i) != i) ||
+ (BIO_write(bp,"\n",1) != 1))
+ goto err;
+ }
+
+ buf=(unsigned char *)Malloc(PEM_BUFSIZE*8);
+ if (buf == NULL)
+ {
+ reason=ERR_R_MALLOC_FAILURE;
+ goto err;
+ }
+
+ i=j=0;
+ while (len > 0)
+ {
+ n=(int)((len>(PEM_BUFSIZE*5))?(PEM_BUFSIZE*5):len);
+ EVP_EncodeUpdate(&ctx,buf,&outl,&(data[j]),n);
+ if ((outl) && (BIO_write(bp,(char *)buf,outl) != outl))
+ goto err;
+ i+=outl;
+ len-=n;
+ j+=n;
+ }
+ EVP_EncodeFinal(&ctx,buf,&outl);
+ if ((outl > 0) && (BIO_write(bp,(char *)buf,outl) != outl)) goto err;
+ Free(buf);
+ if ( (BIO_write(bp,"-----END ",9) != 9) ||
+ (BIO_write(bp,name,nlen) != nlen) ||
+ (BIO_write(bp,"-----\n",6) != 6))
+ goto err;
+ return(i+outl);
+err:
+ PEMerr(PEM_F_PEM_WRITE_BIO,reason);
+ return(0);
+ }
+
+#ifndef WIN16
+int PEM_read(fp, name, header, data,len)
+FILE *fp;
+char **name;
+char **header;
+unsigned char **data;
+long *len;
+ {
+ BIO *b;
+ int ret;
+
+ if ((b=BIO_new(BIO_s_file())) == NULL)
+ {
+ PEMerr(PEM_F_PEM_READ,ERR_R_BUF_LIB);
+ return(0);
+ }
+ BIO_set_fp(b,fp,BIO_NOCLOSE);
+ ret=PEM_read_bio(b, name, header, data,len);
+ BIO_free(b);
+ return(ret);
+ }
+#endif
+
+int PEM_read_bio(bp, name, header, data, len)
+BIO *bp;
+char **name;
+char **header;
+unsigned char **data;
+long *len;
+ {
+ EVP_ENCODE_CTX ctx;
+ int end=0,i,k,bl=0,hl=0,nohead=0;
+ char buf[256];
+ BUF_MEM *nameB;
+ BUF_MEM *headerB;
+ BUF_MEM *dataB,*tmpB;
+
+ nameB=BUF_MEM_new();
+ headerB=BUF_MEM_new();
+ dataB=BUF_MEM_new();
+ if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL))
+ {
+ PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
+ return(0);
+ }
+
+ buf[254]='\0';
+ for (;;)
+ {
+ i=BIO_gets(bp,buf,254);
+
+ if (i <= 0)
+ {
+ PEMerr(PEM_F_PEM_READ_BIO,PEM_R_NO_START_LINE);
+ goto err;
+ }
+
+ while ((i >= 0) && (buf[i] <= ' ')) i--;
+ buf[++i]='\n'; buf[++i]='\0';
+
+ if (strncmp(buf,"-----BEGIN ",11) == 0)
+ {
+ i=strlen(&(buf[11]));
+
+ if (strncmp(&(buf[11+i-6]),"-----\n",6) != 0)
+ continue;
+ if (!BUF_MEM_grow(nameB,i+9))
+ {
+ PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
+ goto err;
+ }
+ strncpy(nameB->data,&(buf[11]),(unsigned int)i-6);
+ nameB->data[i-6]='\0';
+ break;
+ }
+ }
+ hl=0;
+ if (!BUF_MEM_grow(headerB,256))
+ { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; }
+ headerB->data[0]='\0';
+ for (;;)
+ {
+ i=BIO_gets(bp,buf,254);
+ if (i <= 0) break;
+
+ while ((i >= 0) && (buf[i] <= ' ')) i--;
+ buf[++i]='\n'; buf[++i]='\0';
+
+ if (buf[0] == '\n') break;
+ if (!BUF_MEM_grow(headerB,hl+i+9))
+ { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; }
+ if (strncmp(buf,"-----END ",9) == 0)
+ {
+ nohead=1;
+ break;
+ }
+ strncpy(&(headerB->data[hl]),buf,(unsigned int)i);
+ headerB->data[hl+i]='\0';
+ hl+=i;
+ }
+
+ bl=0;
+ if (!BUF_MEM_grow(dataB,1024))
+ { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; }
+ dataB->data[0]='\0';
+ if (!nohead)
+ {
+ for (;;)
+ {
+ i=BIO_gets(bp,buf,254);
+ if (i <= 0) break;
+
+ while ((i >= 0) && (buf[i] <= ' ')) i--;
+ buf[++i]='\n'; buf[++i]='\0';
+
+ if (i != 65) end=1;
+ if (strncmp(buf,"-----END ",9) == 0)
+ break;
+ if (i > 65) break;
+ if (!BUF_MEM_grow(dataB,i+bl+9))
+ {
+ PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);
+ goto err;
+ }
+ strncpy(&(dataB->data[bl]),buf,(unsigned int)i);
+ dataB->data[bl+i]='\0';
+ bl+=i;
+ if (end)
+ {
+ buf[0]='\0';
+ i=BIO_gets(bp,buf,254);
+ if (i <= 0) break;
+
+ while ((i >= 0) && (buf[i] <= ' ')) i--;
+ buf[++i]='\n'; buf[++i]='\0';
+
+ break;
+ }
+ }
+ }
+ else
+ {
+ tmpB=headerB;
+ headerB=dataB;
+ dataB=tmpB;
+ bl=hl;
+ }
+ i=strlen(nameB->data);
+ if ( (strncmp(buf,"-----END ",9) != 0) ||
+ (strncmp(nameB->data,&(buf[9]),(unsigned int)i) != 0) ||
+ (strncmp(&(buf[9+i]),"-----\n",6) != 0))
+ {
+ PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_END_LINE);
+ goto err;
+ }
+
+ EVP_DecodeInit(&ctx);
+ i=EVP_DecodeUpdate(&ctx,
+ (unsigned char *)dataB->data,&bl,
+ (unsigned char *)dataB->data,bl);
+ if (i < 0)
+ {
+ PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_BASE64_DECODE);
+ goto err;
+ }
+ i=EVP_DecodeFinal(&ctx,(unsigned char *)&(dataB->data[bl]),&k);
+ if (i < 0)
+ {
+ PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_BASE64_DECODE);
+ goto err;
+ }
+ bl+=k;
+
+ if (bl == 0) goto err;
+ *name=nameB->data;
+ *header=headerB->data;
+ *data=(unsigned char *)dataB->data;
+ *len=bl;
+ Free(nameB);
+ Free(headerB);
+ Free(dataB);
+ return(1);
+err:
+ BUF_MEM_free(nameB);
+ BUF_MEM_free(headerB);
+ BUF_MEM_free(dataB);
+ return(0);
+ }
diff --git a/crypto/pem/pem_seal.c b/crypto/pem/pem_seal.c
new file mode 100644
index 0000000000..6acb04ad77
--- /dev/null
+++ b/crypto/pem/pem_seal.c
@@ -0,0 +1,191 @@
+/* crypto/pem/pem_seal.c */
+/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+ * All rights reserved.
+ *
+ * This package is an SSL implementation written
+ * by Eric Young (eay@cryptsoft.com).
+ * The implementation was written so as to conform with Netscapes SSL.
+ *
+ * This library is free for commercial and non-commercial use as long as
+ * the following conditions are aheared to. The following conditions
+ * apply to all code found in this distribution, be it the RC4, RSA,
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
+ * included with this distribution is covered by the same copyright terms
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
+ *
+ * Copyright remains Eric Young's, and as such any Copyright notices in
+ * the code are not to be removed.
+ * If this package is used in a product, Eric Young should be given attribution
+ * as the author of the parts of the library used.
+ * This can be in the form of a textual message at program startup or
+ * in documentation (online or textual) provided with the package.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * "This product includes cryptographic software written by
+ * Eric Young (eay@cryptsoft.com)"
+ * The word 'cryptographic' can be left out if the rouines from the library
+ * being used are not cryptographic related :-).
+ * 4. If you include any Windows specific code (or a derivative thereof) from
+ * the apps directory (application code) you must include an acknowledgement:
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * The licence and distribution terms for any publically available version or
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
+ * copied and put under another distribution licence
+ * [including the GNU Public Licence.]
+ */
+
+#include <stdio.h>
+#include "cryptlib.h"
+#include "evp.h"
+#include "rand.h"
+#include "objects.h"
+#include "x509.h"
+#include "pem.h"
+
+int PEM_SealInit(ctx,type,md_type,ek,ekl,iv,pubk,npubk)
+PEM_ENCODE_SEAL_CTX *ctx;
+EVP_CIPHER *type;
+EVP_MD *md_type;
+unsigned char **ek;
+int *ekl;
+unsigned char *iv;
+EVP_PKEY **pubk;
+int npubk;
+ {
+ unsigned char key[EVP_MAX_KEY_LENGTH];
+ int ret= -1;
+ int i,j,max=0;
+ char *s=NULL;
+
+ for (i=0; i<npubk; i++)
+ {
+ if (pubk[i]->type != EVP_PKEY_RSA)
+ {
+ PEMerr(PEM_F_PEM_SEALINIT,PEM_R_PUBLIC_KEY_NO_RSA);
+ goto err;
+ }
+ j=RSA_size(pubk[i]->pkey.rsa);
+ if (j > max) max=j;
+ }
+ s=(char *)Malloc(max*2);
+ if (s == NULL)
+ {
+ PEMerr(PEM_F_PEM_SEALINIT,ERR_R_MALLOC_FAILURE);
+ goto err;
+ }
+
+ EVP_EncodeInit(&(ctx->encode));
+ EVP_SignInit(&(ctx->md),md_type);
+
+ ret=EVP_SealInit(&(ctx->cipher),type,ek,ekl,iv,pubk,npubk);
+ if (!ret) goto err;
+
+ /* base64 encode the keys */
+ for (i=0; i<npubk; i++)
+ {
+ j=EVP_EncodeBlock((unsigned char *)s,ek[i],
+ RSA_size(pubk[i]->pkey.rsa));
+ ekl[i]=j;
+ memcpy(ek[i],s,j+1);
+ }
+
+ ret=npubk;
+err:
+ if (s != NULL) Free(s);
+ memset(key,0,EVP_MAX_KEY_LENGTH);
+ return(ret);
+ }
+
+void PEM_SealUpdate(ctx,out,outl,in,inl)
+PEM_ENCODE_SEAL_CTX *ctx;
+unsigned char *out;
+int *outl;
+unsigned char *in;
+int inl;
+ {
+ unsigned char buffer[1600];
+ int i,j;
+
+ *outl=0;
+ EVP_SignUpdate(&(ctx->md),in,inl);
+ for (;;)
+ {
+ if (inl <= 0) break;
+ if (inl > 1200)
+ i=1200;
+ else
+ i=inl;
+ EVP_EncryptUpdate(&(ctx->cipher),buffer,&j,in,i);
+ EVP_EncodeUpdate(&(ctx->encode),out,&j,buffer,j);
+ *outl+=j;
+ out+=j;
+ in+=i;
+ inl-=i;
+ }
+ }
+
+int PEM_SealFinal(ctx,sig,sigl,out,outl,priv)
+PEM_ENCODE_SEAL_CTX *ctx;
+unsigned char *sig;
+int *sigl;
+unsigned char *out;
+int *outl;
+EVP_PKEY *priv;
+ {
+ unsigned char *s=NULL;
+ int ret=0,j;
+ unsigned int i;
+
+ if (priv->type != EVP_PKEY_RSA)
+ {
+ PEMerr(PEM_F_PEM_SEALFINAL,PEM_R_PUBLIC_KEY_NO_RSA);
+ goto err;
+ }
+ i=RSA_size(priv->pkey.rsa);
+ if (i < 100) i=100;
+ s=(unsigned char *)Malloc(i*2);
+ if (s == NULL)
+ {
+ PEMerr(PEM_F_PEM_SEALFINAL,ERR_R_MALLOC_FAILURE);
+ goto err;
+ }
+
+ EVP_EncryptFinal(&(ctx->cipher),s,(int *)&i);
+ EVP_EncodeUpdate(&(ctx->encode),out,&j,s,i);
+ *outl=j;
+ out+=j;
+ EVP_EncodeFinal(&(ctx->encode),out,&j);
+ *outl+=j;
+
+ if (!EVP_SignFinal(&(ctx->md),s,&i,priv)) goto err;
+ *sigl=EVP_EncodeBlock(sig,s,i);
+
+ ret=1;
+err:
+ memset((char *)&(ctx->md),0,sizeof(ctx->md));
+ memset((char *)&(ctx->cipher),0,sizeof(ctx->cipher));
+ if (s != NULL) Free(s);
+ return(ret);
+ }
diff --git a/crypto/pem/pem_sign.c b/crypto/pem/pem_sign.c
new file mode 100644
index 0000000000..0df99a3d84
--- /dev/null
+++ b/crypto/pem/pem_sign.c
@@ -0,0 +1,109 @@
+/* crypto/pem/pem_sign.c */
+/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+ * All rights reserved.
+ *
+ * This package is an SSL implementation written
+ * by Eric Young (eay@cryptsoft.com).
+ * The implementation was written so as to conform with Netscapes SSL.
+ *
+ * This library is free for commercial and non-commercial use as long as
+ * the following conditions are aheared to. The following conditions
+ * apply to all code found in this distribution, be it the RC4, RSA,
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
+ * included with this distribution is covered by the same copyright terms
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
+ *
+ * Copyright remains Eric Young's, and as such any Copyright notices in
+ * the code are not to be removed.
+ * If this package is used in a product, Eric Young should be given attribution
+ * as the author of the parts of the library used.
+ * This can be in the form of a textual message at program startup or
+ * in documentation (online or textual) provided with the package.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * "This product includes cryptographic software written by
+ * Eric Young (eay@cryptsoft.com)"
+ * The word 'cryptographic' can be left out if the rouines from the library
+ * being used are not cryptographic related :-).
+ * 4. If you include any Windows specific code (or a derivative thereof) from
+ * the apps directory (application code) you must include an acknowledgement:
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * The licence and distribution terms for any publically available version or
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
+ * copied and put under another distribution licence
+ * [including the GNU Public Licence.]
+ */
+
+#include <stdio.h>
+#include "cryptlib.h"
+#include "rand.h"
+#include "evp.h"
+#include "objects.h"
+#include "x509.h"
+#include "pem.h"
+
+void PEM_SignInit(ctx,type)
+EVP_MD_CTX *ctx;
+EVP_MD *type;
+ {
+ EVP_DigestInit(ctx,type);
+ }
+
+void PEM_SignUpdate(ctx,data,count)
+EVP_MD_CTX *ctx;
+unsigned char *data;
+unsigned int count;
+ {
+ EVP_DigestUpdate(ctx,data,count);
+ }
+
+int PEM_SignFinal(ctx,sigret,siglen,pkey)
+EVP_MD_CTX *ctx;
+unsigned char *sigret;
+unsigned int *siglen;
+EVP_PKEY *pkey;
+ {
+ unsigned char *m;
+ int i,ret=0;
+ unsigned int m_len;
+
+ m=(unsigned char *)Malloc(EVP_PKEY_size(pkey)+2);
+ if (m == NULL)
+ {
+ PEMerr(PEM_F_PEM_SIGNFINAL,ERR_R_MALLOC_FAILURE);
+ goto err;
+ }
+
+ if (EVP_SignFinal(ctx,m,&m_len,pkey) <= 0) goto err;
+
+ i=EVP_EncodeBlock(sigret,m,m_len);
+ *siglen=i;
+ ret=1;
+err:
+ /* ctx has been zeroed by EVP_SignFinal() */
+ if (m != NULL) Free(m);
+ return(ret);
+ }
+
diff --git a/crypto/pem/pkcs7.lis b/crypto/pem/pkcs7.lis
new file mode 100644
index 0000000000..be90c5d87f
--- /dev/null
+++ b/crypto/pem/pkcs7.lis
@@ -0,0 +1,22 @@
+21 0:d=0 hl=2 l= 0 cons: univ: SEQUENCE
+ 00 2:d=0 hl=2 l= 9 prim: univ: OBJECT_IDENTIFIER :pkcs-7-signedData
+ 21 13:d=0 hl=2 l= 0 cons: cont: 00 # explicit tag
+ 21 15:d=0 hl=2 l= 0 cons: univ: SEQUENCE
+ 00 17:d=0 hl=2 l= 1 prim: univ: INTEGER # version
+ 20 20:d=0 hl=2 l= 0 cons: univ: SET
+ 21 22:d=0 hl=2 l= 0 cons: univ: SEQUENCE
+ 00 24:d=0 hl=2 l= 9 prim: univ: OBJECT_IDENTIFIER :pkcs-7-data
+ 00 35:d=0 hl=2 l= 0 prim: univ: EOC
+ 21 37:d=0 hl=2 l= 0 cons: cont: 00 # cert tag
+ 20 39:d=0 hl=4 l=545 cons: univ: SEQUENCE
+ 20 588:d=0 hl=4 l=524 cons: univ: SEQUENCE
+ 00 1116:d=0 hl=2 l= 0 prim: univ: EOC
+ 21 1118:d=0 hl=2 l= 0 cons: cont: 01 # crl tag
+ 20 1120:d=0 hl=4 l=653 cons: univ: SEQUENCE
+ 20 1777:d=0 hl=4 l=285 cons: univ: SEQUENCE
+ 00 2066:d=0 hl=2 l= 0 prim: univ: EOC
+ 21 2068:d=0 hl=2 l= 0 cons: univ: SET # signers
+ 00 2070:d=0 hl=2 l= 0 prim: univ: EOC
+ 00 2072:d=0 hl=2 l= 0 prim: univ: EOC
+ 00 2074:d=0 hl=2 l= 0 prim: univ: EOC
+00 2076:d=0 hl=2 l= 0 prim: univ: EOC