PEM encoding for OpenSSL RSA and EC public and private keys
This commit is contained in:
@@ -233,11 +233,24 @@ static bool get_encoding(private_openssl_ec_private_key_t *this,
|
|||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case KEY_PRIV_ASN1_DER:
|
case KEY_PRIV_ASN1_DER:
|
||||||
|
case KEY_PRIV_PEM:
|
||||||
{
|
{
|
||||||
|
bool success = TRUE;
|
||||||
|
|
||||||
*encoding = chunk_alloc(i2d_ECPrivateKey(this->ec, NULL));
|
*encoding = chunk_alloc(i2d_ECPrivateKey(this->ec, NULL));
|
||||||
p = encoding->ptr;
|
p = encoding->ptr;
|
||||||
i2d_ECPrivateKey(this->ec, &p);
|
i2d_ECPrivateKey(this->ec, &p);
|
||||||
return TRUE;
|
|
||||||
|
if (type == KEY_PRIV_PEM)
|
||||||
|
{
|
||||||
|
chunk_t asn1_encoding = *encoding;
|
||||||
|
|
||||||
|
success = lib->encoding->encode(lib->encoding, KEY_PRIV_PEM,
|
||||||
|
NULL, encoding, KEY_PART_ECDSA_PRIV_ASN1_DER,
|
||||||
|
asn1_encoding, KEY_PART_END);
|
||||||
|
chunk_clear(&asn1_encoding);
|
||||||
|
}
|
||||||
|
return success;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|||||||
@@ -248,11 +248,24 @@ static bool get_encoding(private_openssl_ec_public_key_t *this,
|
|||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case KEY_PUB_SPKI_ASN1_DER:
|
case KEY_PUB_SPKI_ASN1_DER:
|
||||||
|
case KEY_PUB_PEM:
|
||||||
{
|
{
|
||||||
|
bool success = TRUE;
|
||||||
|
|
||||||
*encoding = chunk_alloc(i2d_EC_PUBKEY(this->ec, NULL));
|
*encoding = chunk_alloc(i2d_EC_PUBKEY(this->ec, NULL));
|
||||||
p = encoding->ptr;
|
p = encoding->ptr;
|
||||||
i2d_EC_PUBKEY(this->ec, &p);
|
i2d_EC_PUBKEY(this->ec, &p);
|
||||||
return TRUE;
|
|
||||||
|
if (type == KEY_PUB_PEM)
|
||||||
|
{
|
||||||
|
chunk_t asn1_encoding = *encoding;
|
||||||
|
|
||||||
|
success = lib->encoding->encode(lib->encoding, KEY_PUB_PEM,
|
||||||
|
NULL, encoding, KEY_PART_ECDSA_PUB_ASN1_DER,
|
||||||
|
asn1_encoding, KEY_PART_END);
|
||||||
|
chunk_clear(&asn1_encoding);
|
||||||
|
}
|
||||||
|
return success;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|||||||
@@ -226,11 +226,24 @@ static bool get_encoding(private_openssl_rsa_private_key_t *this,
|
|||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case KEY_PRIV_ASN1_DER:
|
case KEY_PRIV_ASN1_DER:
|
||||||
|
case KEY_PRIV_PEM:
|
||||||
{
|
{
|
||||||
|
bool success = TRUE;
|
||||||
|
|
||||||
*encoding = chunk_alloc(i2d_RSAPrivateKey(this->rsa, NULL));
|
*encoding = chunk_alloc(i2d_RSAPrivateKey(this->rsa, NULL));
|
||||||
p = encoding->ptr;
|
p = encoding->ptr;
|
||||||
i2d_RSAPrivateKey(this->rsa, &p);
|
i2d_RSAPrivateKey(this->rsa, &p);
|
||||||
return TRUE;
|
|
||||||
|
if (type == KEY_PRIV_PEM)
|
||||||
|
{
|
||||||
|
chunk_t asn1_encoding = *encoding;
|
||||||
|
|
||||||
|
success = lib->encoding->encode(lib->encoding, KEY_PRIV_PEM,
|
||||||
|
NULL, encoding, KEY_PART_RSA_PRIV_ASN1_DER,
|
||||||
|
asn1_encoding, KEY_PART_END);
|
||||||
|
chunk_clear(&asn1_encoding);
|
||||||
|
}
|
||||||
|
return success;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|||||||
@@ -231,11 +231,24 @@ static bool get_encoding(private_openssl_rsa_public_key_t *this,
|
|||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case KEY_PUB_SPKI_ASN1_DER:
|
case KEY_PUB_SPKI_ASN1_DER:
|
||||||
|
case KEY_PUB_PEM:
|
||||||
{
|
{
|
||||||
|
bool success = TRUE;
|
||||||
|
|
||||||
*encoding = chunk_alloc(i2d_RSA_PUBKEY(this->rsa, NULL));
|
*encoding = chunk_alloc(i2d_RSA_PUBKEY(this->rsa, NULL));
|
||||||
p = encoding->ptr;
|
p = encoding->ptr;
|
||||||
i2d_RSA_PUBKEY(this->rsa, &p);
|
i2d_RSA_PUBKEY(this->rsa, &p);
|
||||||
return TRUE;
|
|
||||||
|
if (type == KEY_PUB_PEM)
|
||||||
|
{
|
||||||
|
chunk_t asn1_encoding = *encoding;
|
||||||
|
|
||||||
|
success = lib->encoding->encode(lib->encoding, KEY_PUB_PEM,
|
||||||
|
NULL, encoding, KEY_PART_RSA_PUB_ASN1_DER,
|
||||||
|
asn1_encoding, KEY_PART_END);
|
||||||
|
chunk_clear(&asn1_encoding);
|
||||||
|
}
|
||||||
|
return success;
|
||||||
}
|
}
|
||||||
case KEY_PUB_ASN1_DER:
|
case KEY_PUB_ASN1_DER:
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -31,21 +31,29 @@ bool pem_encoder_encode(key_encoding_type_t type, chunk_t *encoding,
|
|||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case KEY_PUB_PEM:
|
case KEY_PUB_PEM:
|
||||||
if (!key_encoding_args(args, KEY_PART_RSA_PUB_ASN1_DER,
|
if (key_encoding_args(args, KEY_PART_RSA_PUB_ASN1_DER,
|
||||||
|
&asn1, KEY_PART_END) ||
|
||||||
|
key_encoding_args(args, KEY_PART_ECDSA_PUB_ASN1_DER,
|
||||||
&asn1, KEY_PART_END))
|
&asn1, KEY_PART_END))
|
||||||
{
|
{
|
||||||
return FALSE;
|
label ="PUBLIC KEY";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
label ="PUBLIC KEY";
|
return FALSE;
|
||||||
break;
|
|
||||||
case KEY_PRIV_PEM:
|
case KEY_PRIV_PEM:
|
||||||
if (!key_encoding_args(args, KEY_PART_RSA_PRIV_ASN1_DER,
|
if (key_encoding_args(args, KEY_PART_RSA_PRIV_ASN1_DER,
|
||||||
&asn1, KEY_PART_END))
|
&asn1, KEY_PART_END))
|
||||||
{
|
{
|
||||||
return FALSE;
|
label ="RSA PRIVATE KEY";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
label ="RSA PRIVATE KEY";
|
if (key_encoding_args(args, KEY_PART_ECDSA_PRIV_ASN1_DER,
|
||||||
break;
|
&asn1, KEY_PART_END))
|
||||||
|
{
|
||||||
|
label ="EC PRIVATE KEY";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return FALSE;
|
||||||
default:
|
default:
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user