Beispiel #1
0
void
PrivateKeyStorage::decodeEcPrivateKey
  (const ptr_lib::shared_ptr<DerNode>& algorithmParameters,
   const Blob& privateKeyDer, EcPrivateKeyLite& privateKey)
{
  // Find the curveId in EC_KEY_INFO.
  int curveId = -1;
  string oidString = algorithmParameters->toVal().toRawStr();
  for (size_t i = 0 ; i < ndn_getEcKeyInfoCount(); ++i) {
    const struct ndn_EcKeyInfo *info = ndn_getEcKeyInfo(i);
    OID curveOid(info->oidIntegerList, info->oidIntegerListLength);
    if (curveOid.toString() == oidString) {
      curveId = info->curveId;
      break;
    }
  }
  if (curveId == -1)
    throw SecurityException
      ("FilePrivateKeyStorage::decodeEcPrivateKey: Unrecognized EC algorithm parameters");

  // Get the value in the octet string.
  ptr_lib::shared_ptr<DerNode> parsedNode = DerNode::parse(privateKeyDer.buf(), 0);
  DerNode::DerOctetString* octetString = dynamic_cast<DerNode::DerOctetString*>
    (parsedNode->getChildren()[1].get());
  if (!octetString)
    throw SecurityException
      ("FilePrivateKeyStorage::decodeEcPrivateKey: Can't get the private key octet string");
  Blob octetStringValue = octetString->toVal();

  ndn_Error error;
  if ((error = privateKey.setByCurve(curveId, octetStringValue)))
    throw SecurityException
      (string("PrivateKeyStorage::decodeEcPrivateKey ") + ndn_getErrorString(error));
}
Beispiel #2
0
ec_key_st*
FilePrivateKeyStorage::decodeEcPrivateKey
  (const ptr_lib::shared_ptr<DerNode>& algorithmParameters,
   const Blob& privateKeyDer)
{
  // Find the curveId in EC_KEY_INFO.
  int curveId = -1;
  string oidString = algorithmParameters->toVal().toRawStr();
  for (size_t i = 0 ; i < sizeof(EC_KEY_INFO) / sizeof(EC_KEY_INFO[0]); ++i) {
    OID curveOid(EC_KEY_INFO[i].oidIntegerList, EC_KEY_INFO[i].oidIntegerListLength);
    if (curveOid.toString() == oidString) {
      curveId = EC_KEY_INFO[i].curveId;
      break;
    }
  }
  if (curveId == -1)
    throw SecurityException
      ("FilePrivateKeyStorage::decodeEcPrivateKey: Unrecognized EC algorithm parameters");

  // Get the value in the octet string.
  ptr_lib::shared_ptr<DerNode> parsedNode = DerNode::parse(privateKeyDer.buf(), 0);
  DerNode::DerOctetString* octetString = dynamic_cast<DerNode::DerOctetString*>
    (parsedNode->getChildren()[1].get());
  if (!octetString)
    throw SecurityException
      ("FilePrivateKeyStorage::decodeEcPrivateKey: Can't get the private key octet string");
  Blob octetStringValue = octetString->toVal();

  BIGNUM* keyBignum = BN_bin2bn(octetStringValue.buf(), octetStringValue.size(), NULL);
  if (!keyBignum) {
    // We don't expect this to happen.
    throw SecurityException
      ("FilePrivateKeyStorage::decodeEcPrivateKey: Can't create a BIGNUM for the private key value");
  }
  EC_KEY* privateKey = EC_KEY_new_by_curve_name(curveId);
  if (!privateKey) {
    // We don't expect this to happen.
    BN_free(keyBignum);
    throw SecurityException
      ("FilePrivateKeyStorage::decodeEcPrivateKey: Can't create an EC key for the curve ID");
  }
  EC_KEY_set_private_key(privateKey, keyBignum);
  BN_free(keyBignum);

  return privateKey;
}