Просмотр исходного кода

components/openssl: add function load verify data into SSL

	1. add function to load private key into SSL
        1. add function to load certification into SSL
dongheng 9 лет назад
Родитель
Сommit
a99f6bd727
2 измененных файлов с 141 добавлено и 0 удалено
  1. 70 0
      components/openssl/library/ssl_pkey.c
  2. 71 0
      components/openssl/library/ssl_x509.c

+ 70 - 0
components/openssl/library/ssl_pkey.c

@@ -131,6 +131,26 @@ int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
     return 1;
 }
 
+/*
+ * SSL_CTX_use_certificate - set the SSL private key
+ *
+ * @param ctx - SSL point
+ * @param x   - private key point
+ *
+ * @return
+ *         1 : OK
+ *         0 : failed
+ */
+int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
+{
+    SSL_ASSERT(ctx);
+    SSL_ASSERT(pkey);
+
+    ssl->cert->pkey = pkey;
+
+    return 1;
+}
+
 /*
  * SSL_CTX_use_PrivateKey_ASN1 - load private key into the SSL context
  *
@@ -165,6 +185,40 @@ failed1:
     return 0;
 }
 
+/*
+ * SSL_use_PrivateKey_ASN1 - load private key into the SSL
+ *
+ * @param type - private key type
+ * @param ctx  - SSL context point
+ * @param d    - private key context point
+ * @param len  - private key context bytes
+ *
+ * @return
+ *         1 : OK
+ *         0 : failed
+ */
+int SSL_use_PrivateKey_ASN1(int type, SSL *ssl,
+                                const unsigned char *d, long len)
+{
+    int ret;
+    EVP_PKEY *pkey;
+
+    pkey = d2i_PrivateKey(0, &ssl->cert->pkey, &d, len);
+    if (!pkey)
+        SSL_RET(failed1, "d2i_PrivateKey\n");
+
+    ret = SSL_use_PrivateKey(ssl, pkey);
+    if (!ret)
+        SSL_RET(failed2, "SSL_CTX_use_PrivateKey\n");
+
+    return 1;
+
+failed2:
+    EVP_PKEY_free(pkey);
+failed1:
+    return 0;
+}
+
 /*
  * SSL_CTX_use_certificate_file - load the private key file into SSL context
  *
@@ -181,6 +235,22 @@ int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
     return 0;
 }
 
+/*
+ * SSL_use_PrivateKey_file - load the private key file into SSL
+ *
+ * @param ctx  - SSL point
+ * @param file - private key file name
+ * @param type - private key encoding type
+ *
+ * @return
+ *         1 : OK
+ *         0 : failed
+ */
+int SSL_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
+{
+    return 0;
+}
+
 /*
  * SSL_CTX_use_certificate_ASN1 - load the RSA ASN1 private key into SSL context
  *

+ 71 - 0
components/openssl/library/ssl_x509.c

@@ -168,6 +168,26 @@ int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
     return 1;
 }
 
+/*
+ * SSL_CTX_use_certificate - set the SSL certification
+ *
+ * @param ctx - SSL point
+ * @param x   - X509 certification point
+ *
+ * @return
+ *         1 : OK
+ *         0 : failed
+ */
+int SSL_use_certificate(SSL *ssl, X509 *x)
+{
+    SSL_ASSERT(ctx);
+    SSL_ASSERT(x);
+
+    ssl->cert->x509 = x;
+
+    return 1;
+}
+
 /*
  * SSL_get_certificate - get the SSL certification point
  *
@@ -177,6 +197,8 @@ int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
  */
 X509 *SSL_get_certificate(const SSL *ssl)
 {
+    SSL_ASSERT(ssl);
+
     return ssl->cert->x509;
 }
 
@@ -213,6 +235,39 @@ failed1:
     return 0;
 }
 
+/*
+ * SSL_use_certificate_ASN1 - load certification into the SSL
+ *
+ * @param ctx - SSL point
+ * @param len - certification context bytes
+ * @param d   - certification context point
+ *
+ * @return
+ *         1 : OK
+ *         0 : failed
+ */
+int SSL_use_certificate_ASN1(SSL *ssl, int len,
+                             const unsigned char *d)
+{
+    int ret;
+    X509 *cert;
+
+    cert = d2i_X509(&ssl->cert->x509, d, len);
+    if (!cert)
+        SSL_RET(failed1, "d2i_X509\n");
+
+    ret = SSL_use_certificate(ssl, cert);
+    if (!ret)
+        SSL_RET(failed2, "SSL_use_certificate\n");
+
+    return 1;
+
+failed2:
+    X509_free(cert);
+failed1:
+    return 0;
+}
+
 /*
  * SSL_CTX_use_certificate_file - load the certification file into SSL context
  *
@@ -228,3 +283,19 @@ int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
 {
     return 0;
 }
+
+/*
+ * SSL_use_certificate_file - load the certification file into SSL
+ *
+ * @param ctx  - SSL point
+ * @param file - certification file name
+ * @param type - certification encoding type
+ *
+ * @return
+ *         1 : OK
+ *         0 : failed
+ */
+int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
+{
+    return 0;
+}