mirror of
https://github.com/vlang/v.git
synced 2025-09-08 06:41:58 -04:00
This commit is contained in:
parent
0ea6667eca
commit
ec0b70e130
@ -44,6 +44,8 @@ fn C.EVP_PKEY_bits(pkey &C.EVP_PKEY) int
|
|||||||
fn C.EVP_PKEY_size(key &C.EVP_PKEY) int
|
fn C.EVP_PKEY_size(key &C.EVP_PKEY) int
|
||||||
fn C.EVP_PKEY_eq(a &C.EVP_PKEY, b &C.EVP_PKEY) int
|
fn C.EVP_PKEY_eq(a &C.EVP_PKEY, b &C.EVP_PKEY) int
|
||||||
|
|
||||||
|
fn C.EVP_PKEY_get1_encoded_public_key(pkey &C.EVP_PKEY, ppub &&u8) int
|
||||||
|
fn C.EVP_PKEY_get_bn_param(pkey &C.EVP_PKEY, key_name &u8, bn &&C.BIGNUM) int
|
||||||
fn C.EVP_PKEY_fromdata_init(ctx &C.EVP_PKEY_CTX) int
|
fn C.EVP_PKEY_fromdata_init(ctx &C.EVP_PKEY_CTX) int
|
||||||
fn C.EVP_PKEY_fromdata(ctx &C.EVP_PKEY_CTX, ppkey &&C.EVP_PKEY, selection int, params &C.OSSL_PARAM) int
|
fn C.EVP_PKEY_fromdata(ctx &C.EVP_PKEY_CTX, ppkey &&C.EVP_PKEY, selection int, params &C.OSSL_PARAM) int
|
||||||
|
|
||||||
@ -143,6 +145,7 @@ fn C.EC_GROUP_new_by_curve_name(nid int) &C.EC_GROUP
|
|||||||
@[typedef]
|
@[typedef]
|
||||||
struct C.BIGNUM {}
|
struct C.BIGNUM {}
|
||||||
|
|
||||||
|
fn C.BN_new() &C.BIGNUM
|
||||||
fn C.BN_num_bits(a &C.BIGNUM) int
|
fn C.BN_num_bits(a &C.BIGNUM) int
|
||||||
fn C.BN_bn2bin(a &C.BIGNUM, to &u8) int
|
fn C.BN_bn2bin(a &C.BIGNUM, to &u8) int
|
||||||
fn C.BN_bn2binpad(a &C.BIGNUM, to &u8, tolen int) int
|
fn C.BN_bn2binpad(a &C.BIGNUM, to &u8, tolen int) int
|
||||||
|
@ -289,10 +289,12 @@ fn (pv PrivateKey) sign_digest(digest []u8) ![]u8 {
|
|||||||
|
|
||||||
// bytes represent private key as bytes.
|
// bytes represent private key as bytes.
|
||||||
pub fn (pv PrivateKey) bytes() ![]u8 {
|
pub fn (pv PrivateKey) bytes() ![]u8 {
|
||||||
// This is the old one
|
bn := C.BN_new()
|
||||||
bn := voidptr(C.EC_KEY_get0_private_key(pv.key))
|
// retrieves a BIGNUM value associated with a 'priv' key name
|
||||||
if bn == 0 {
|
n := C.EVP_PKEY_get_bn_param(pv.evpkey, c'priv', &bn)
|
||||||
return error('Failed to get private key BIGNUM')
|
if n <= 0 {
|
||||||
|
C.BN_free(bn)
|
||||||
|
return error('EVP_PKEY_get_bn_param failed')
|
||||||
}
|
}
|
||||||
num_bytes := (C.BN_num_bits(bn) + 7) / 8
|
num_bytes := (C.BN_num_bits(bn) + 7) / 8
|
||||||
// Get the buffer size to store the seed.
|
// Get the buffer size to store the seed.
|
||||||
@ -305,8 +307,10 @@ pub fn (pv PrivateKey) bytes() ![]u8 {
|
|||||||
mut buf := []u8{len: int(size)}
|
mut buf := []u8{len: int(size)}
|
||||||
res := C.BN_bn2binpad(bn, buf.data, size)
|
res := C.BN_bn2binpad(bn, buf.data, size)
|
||||||
if res == 0 {
|
if res == 0 {
|
||||||
|
C.BN_free(bn)
|
||||||
return error('Failed to convert BIGNUM to bytes')
|
return error('Failed to convert BIGNUM to bytes')
|
||||||
}
|
}
|
||||||
|
C.BN_free(bn)
|
||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,45 +62,17 @@ pub fn pubkey_from_bytes(bytes []u8) !PublicKey {
|
|||||||
|
|
||||||
// bytes gets the bytes of public key.
|
// bytes gets the bytes of public key.
|
||||||
pub fn (pbk PublicKey) bytes() ![]u8 {
|
pub fn (pbk PublicKey) bytes() ![]u8 {
|
||||||
point := voidptr(C.EC_KEY_get0_public_key(pbk.key))
|
ppub := []u8{len: default_point_bufsize}
|
||||||
// defer { C.EC_POINT_free(point)}
|
n := C.EVP_PKEY_get1_encoded_public_key(pbk.evpkey, voidptr(&ppub.data))
|
||||||
if point == 0 {
|
if n <= 0 {
|
||||||
C.EC_POINT_free(point)
|
C.OPENSSL_free(voidptr(ppub.data))
|
||||||
return error('Failed to get public key BIGNUM')
|
return error('EVP_PKEY_get1_encoded_public_key failed')
|
||||||
}
|
}
|
||||||
|
out := ppub[..n].clone()
|
||||||
|
// ppub should be freed by calling `OPENSSL_free` or memleak happens.
|
||||||
|
C.OPENSSL_free(voidptr(ppub.data))
|
||||||
|
|
||||||
group := voidptr(C.EC_KEY_get0_group(pbk.key))
|
return out
|
||||||
num_bits := C.EC_GROUP_get_degree(group)
|
|
||||||
// 1 byte of conversion format || x || y of EC_POINT
|
|
||||||
num_bytes := 1 + 2 * ((num_bits + 7) / 8)
|
|
||||||
|
|
||||||
ctx := C.BN_CTX_new()
|
|
||||||
defer {
|
|
||||||
C.BN_CTX_free(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
if ctx == 0 {
|
|
||||||
C.EC_POINT_free(point)
|
|
||||||
C.BN_CTX_free(ctx)
|
|
||||||
return error('Failed to create BN_CTX')
|
|
||||||
}
|
|
||||||
mut buf := []u8{len: num_bytes}
|
|
||||||
|
|
||||||
// Get conversion format.
|
|
||||||
//
|
|
||||||
// The uncompressed form is indicated by 0x04 and the compressed form is indicated
|
|
||||||
// by either 0x02 or 0x03, hybrid 0x06
|
|
||||||
// The public key MUST be rejected if any other value is included in the first octet.
|
|
||||||
conv_form := C.EC_KEY_get_conv_form(pbk.key)
|
|
||||||
if conv_form !in [2, 3, 4, 6] {
|
|
||||||
return error('bad conversion format')
|
|
||||||
}
|
|
||||||
n := C.EC_POINT_point2oct(group, point, conv_form, buf.data, buf.len, ctx)
|
|
||||||
if n == 0 {
|
|
||||||
return error('EC_POINT_point2oct failed')
|
|
||||||
}
|
|
||||||
// returns the clone of the buffer[..n]
|
|
||||||
return buf[..n].clone()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// pubkey_from_string loads a PublicKey from valid PEM-formatted string in s.
|
// pubkey_from_string loads a PublicKey from valid PEM-formatted string in s.
|
||||||
@ -149,14 +121,16 @@ pub fn pubkey_from_string(s string) !PublicKey {
|
|||||||
}
|
}
|
||||||
chk := C.EC_KEY_check_key(eckey)
|
chk := C.EC_KEY_check_key(eckey)
|
||||||
if chk == 0 {
|
if chk == 0 {
|
||||||
|
C.BIO_free_all(bo)
|
||||||
C.EC_KEY_free(eckey)
|
C.EC_KEY_free(eckey)
|
||||||
|
C.EVP_PKEY_free(evpkey)
|
||||||
return error('EC_KEY_check_key failed')
|
return error('EC_KEY_check_key failed')
|
||||||
}
|
}
|
||||||
C.EVP_PKEY_free(evpkey)
|
|
||||||
C.BIO_free_all(bo)
|
C.BIO_free_all(bo)
|
||||||
// Its OK to return
|
// Its OK to return
|
||||||
return PublicKey{
|
return PublicKey{
|
||||||
key: eckey
|
evpkey: evpkey
|
||||||
|
key: eckey
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,16 +186,18 @@ pub fn privkey_from_string(s string) !PrivateKey {
|
|||||||
|
|
||||||
chk := C.EC_KEY_check_key(eckey)
|
chk := C.EC_KEY_check_key(eckey)
|
||||||
if chk == 0 {
|
if chk == 0 {
|
||||||
|
C.BIO_free_all(bo)
|
||||||
C.EC_KEY_free(eckey)
|
C.EC_KEY_free(eckey)
|
||||||
|
C.EVP_PKEY_free(evpkey)
|
||||||
return error('EC_KEY_check_key failed')
|
return error('EC_KEY_check_key failed')
|
||||||
}
|
}
|
||||||
ksize := ec_key_size(eckey)!
|
ksize := ec_key_size(eckey)!
|
||||||
|
|
||||||
C.EVP_PKEY_free(evpkey)
|
|
||||||
C.BIO_free_all(bo)
|
C.BIO_free_all(bo)
|
||||||
|
|
||||||
// Its OK to return
|
// Its OK to return
|
||||||
return PrivateKey{
|
return PrivateKey{
|
||||||
|
evpkey: evpkey
|
||||||
key: eckey
|
key: eckey
|
||||||
ks_flag: .fixed
|
ks_flag: .fixed
|
||||||
ks_size: ksize
|
ks_size: ksize
|
||||||
|
Loading…
x
Reference in New Issue
Block a user