github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/c/loader.c (about)

     1  /**
     2   * Copyright 2017 Intel Corporation
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   * ------------------------------------------------------------------------------
    16   */
    17  
    18  #include "c11_support.h"
    19  
    20  #include <string.h>
    21  
    22  #include <openssl/bio.h>
    23  #include <openssl/evp.h>
    24  #include <openssl/err.h>
    25  #include <openssl/pem.h>
    26  #include <openssl/engine.h>
    27  #include <openssl/conf.h>
    28  
    29  // Extract the private and public keys from the PEM file, using the supplied
    30  // password to decrypt the file if encrypted. priv_key and pub_key must point to
    31  // an array o at least 65 and 131 character respectively.
    32  int load_pem_key(char *pemstr, size_t pemstr_len, char *password,
    33                   char *out_priv_key, char *out_pub_key) {
    34  
    35    BIO *in = NULL;
    36  
    37    BN_CTX *ctx = NULL;
    38    const EC_GROUP *group;
    39    EC_KEY *eckey = NULL;
    40    const EC_POINT *pub_key_point = NULL;
    41    const BIGNUM *priv_key = NULL, *pub_key = NULL;
    42  
    43    char *priv_key_hex = NULL;
    44    char *pub_key_hex = NULL;
    45  
    46    in = BIO_new_mem_buf(pemstr, (int)pemstr_len);
    47  
    48    // Read key from stream, decrypting with password if not NULL
    49    if (password != NULL && strcmp("", password) != 0) {
    50      // Initialize ciphers
    51      ERR_load_crypto_strings ();
    52      OpenSSL_add_all_algorithms ();
    53  
    54      eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL, password);
    55      if (eckey == NULL) {
    56        return -1; // Failed to decrypt or decode private key
    57      }
    58    } else {
    59      if ((eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL, NULL)) == NULL) {
    60        return -1; // Failed to decode private key
    61      }
    62    }
    63    BIO_free(in);
    64  
    65    // Deconstruct key into big numbers
    66    if ((ctx = BN_CTX_new()) == NULL) {
    67      return -2; // Failed to create new big number context
    68    }
    69    if ((group = EC_KEY_get0_group(eckey)) == NULL) {
    70      return -3; // Failed to load group
    71    }
    72    if ((priv_key = EC_KEY_get0_private_key(eckey)) == NULL) {
    73      return -4; // Failed to load private key
    74    }
    75    if ((pub_key_point = EC_KEY_get0_public_key(eckey)) == NULL) {
    76      return -5; // Failed to load public key point
    77    }
    78    pub_key = EC_POINT_point2bn(group, pub_key_point, EC_KEY_get_conv_form(eckey), NULL, ctx);
    79    if (pub_key == NULL) {
    80      return -6; // Failed to construct public key from point
    81    }
    82  
    83    priv_key_hex = BN_bn2hex(priv_key);
    84    pub_key_hex = BN_bn2hex(pub_key);
    85    strncpy_s(out_priv_key, 64 + 1, priv_key_hex, 64 + 1);
    86    strncpy_s(out_pub_key, 130 + 1, pub_key_hex, 130 + 1);
    87    OPENSSL_free(priv_key_hex);
    88    OPENSSL_free(pub_key_hex);
    89    return 0;
    90  }