Fix the effect of no-dso in crypto/init.c
[oweals/openssl.git] / crypto / ec / ecx_meth.c
index 271a4773ccfba75c0e3c3690d22581a47df8b9dd..06e3911340d8f9fd0901c093f85718c6196074b4 100644 (file)
@@ -32,18 +32,21 @@ typedef enum {
 } ecx_key_op_t;
 
 /* Setup EVP_PKEY using public, private or generation */
-static int ecx_key_op(EVP_PKEY *pkey, X509_ALGOR *palg,
+static int ecx_key_op(EVP_PKEY *pkey, const X509_ALGOR *palg,
                       const unsigned char *p, int plen, ecx_key_op_t op)
 {
-    int ptype;
-    X25519_KEY *xkey = NULL;
+    X25519_KEY *xkey;
 
     if (op != X25519_KEYGEN) {
-        /* Algorithm parameters must be absent */
-        X509_ALGOR_get0(NULL, &ptype, NULL, palg);
-        if (ptype != V_ASN1_UNDEF) {
-            ECerr(EC_F_ECX_KEY_OP, EC_R_INVALID_ENCODING);
-            return 0;
+        if (palg != NULL) {
+            int ptype;
+
+            /* Algorithm parameters must be absent */
+            X509_ALGOR_get0(NULL, &ptype, NULL, palg);
+            if (ptype != V_ASN1_UNDEF) {
+                ECerr(EC_F_ECX_KEY_OP, EC_R_INVALID_ENCODING);
+                return 0;
+            }
         }
 
         if (p == NULL || plen != X25519_KEYLEN) {
@@ -132,12 +135,12 @@ static int ecx_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
     return !CRYPTO_memcmp(akey->pubkey, bkey->pubkey, X25519_KEYLEN);
 }
 
-static int ecx_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
+static int ecx_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
 {
     const unsigned char *p;
     int plen;
     ASN1_OCTET_STRING *oct = NULL;
-    X509_ALGOR *palg;
+    const X509_ALGOR *palg;
     int rv;
 
     if (!PKCS8_pkey_get0(NULL, &p, &plen, &palg, p8))
@@ -148,7 +151,7 @@ static int ecx_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
         p = NULL;
         plen = 0;
     } else {
-        p = ASN1_STRING_data(oct);
+        p = ASN1_STRING_get0_data(oct);
         plen = ASN1_STRING_length(oct);
     }
 
@@ -266,7 +269,29 @@ static int ecx_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
 
 static int ecx_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
 {
-    return -2;
+    switch (op) {
+
+    case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
+        return ecx_key_op(pkey, NULL, arg2, arg1, X25519_PUBLIC);
+
+    case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
+        if (pkey->pkey.ptr != NULL) {
+            const X25519_KEY *xkey = pkey->pkey.ptr;
+            unsigned char **ppt = arg2;
+            *ppt = OPENSSL_memdup(xkey->pubkey, X25519_KEYLEN);
+            if (*ppt != NULL)
+                return X25519_KEYLEN;
+        }
+        return 0;
+
+    case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
+        *(int *)arg2 = NID_sha256;
+        return 2;
+
+    default:
+        return -2;
+
+    }
 }
 
 const EVP_PKEY_ASN1_METHOD ecx25519_asn1_meth = {