Commit Graph

155 Commits (master)

Author SHA1 Message Date
Ori Bernstein c6a9c55de7 x509: encode empty sequence as constructed
According to the ASN.1 BER spec, we should be encoding
all sequences (including empty ones) as constructed:

	8.9.1 The encoding of a sequence value shall be constructed.
	8.10.1 The encoding of a sequence-of value shall be constructed.
	8.11.1 The encoding of a set value shall be constructed.
	8.12.1 The encoding of a set-of value shall be constructed.

However, we were only setting them as constructed when the
list was non-empty.

This changes it, and makes letsencrypt happy with the CSRs that
we generate.
2021-08-09 15:33:16 +00:00
cinap_lenrek b542dce430 libsec: take just the CN part of Distinguished Name in subjectAltName 2021-07-08 20:20:09 +00:00
cinap_lenrek 58b61ff9b9 libsec: do proper type checking, fix wrong deduplication check 2021-07-04 22:28:16 +00:00
cinap_lenrek 88060e7501 libsec: add X509reqtoRSApub() function and return subject alt names in X509to*pub() name buffer
We need a way to parse a rsa certificate request and return the public
key and subject names. The new function X509reqtoRSApub() works the
same way as X509toRSApub() but on a certificate request.

We also need to support certificates that are valid for multiple domain
names (as tlshand does not support certificate selection). For this
reason, a comma separated list is returned as the certificate subject,
making it symmetric to X509rsareq() handling.

A little helper is provided with this change (auth/x5092pub) that takes
a certificate (or a certificate request when -r flag is provided) and
outputs the RSA public key in plan 9 format appended with the subject
attribute.
2021-07-04 22:00:24 +00:00
cinap_lenrek 0339e9ac44 libsec: restore old behaviour for generating self signed cert 2021-06-21 00:38:06 +00:00
cinap_lenrek e9aed242cc libsec: emulate openssl asn1 when generating x509 csr
when trying to request certificates from letsencrypt,
their test api would reject our csr because of
"tuncated sequence" unless we force subectAltName
by passing multiple domains (as comma separated list).

apparently, we need to provide the context specific tag
"cont [ 0 ]" for the extensions even when we do have
any extensions for the csr (triggered when we need to
have subjectAltNames).

for this, we change mkcont() to take a Elist* instead,
which then can be nil when not used. also put the tag
number argument first, which makes it easier to read.
2021-06-20 23:33:13 +00:00
cinap_lenrek 57d95c7325 libsec: move zero check to curve25519_dh_finish()
As checking for all zero has to be done in a timing-safe
way to avoid a side channel, it is best todo this here
instead of letting the caller deal with it.

This adds a return type of int to curve25519_dh_finish()
where returning 0 means we got a all zero shared key.

RFC7748 states:

The check for the all-zero value results from the fact
that the X25519 function produces that value if it
operates on an input corresponding to a point with small
order, where the order divides the cofactor of the curve.
2021-06-20 14:41:26 +00:00
kemal 775a4bea43 libsec: various changes to tls
1. add the curve x25519 to tls, both client and server.
it's more faster, immune to timing attacks by design,
does not require verifying if the public key is valid,
etc etc. server-side has to check if the client supports
the curve, so a new function has been introduced to parse
the client's extensions.

2. reject weak dhe primes that can be easily cracked with
the number field sieve algorithm. this avoids attacks like
logjam.

3. stop putting unix time to the first 4 bytes of client/
server random. it can allow fingerprinting, tls 1.3 doesn't
recommend it any more and there was a draft to deprecate
this behaviour earlier.[1]

4. simply prf code, remove useless cipher enums.

[1] https://datatracker.ietf.org/doc/html/draft-mathewson-no-gmtunixtime-00
2021-06-18 19:12:44 +00:00
cinap_lenrek 383a2ca79b libsec: open internal file-descriptors with OCEXEC flag 2020-12-07 17:14:34 +01:00
Sigrid 3bdf242555 libsec/base58enc: null-terminate the result 2020-04-28 12:49:05 +02:00
cinap_lenrek 64640083de libsec: remove hash pickle functions, document ripemd160, cleanup sechash(2) manpage 2020-03-01 15:07:44 +01:00
cinap_lenrek 85216d3d95 auth/rsa2asn1: implement private key export with -a flag (thanks kvik)
kvik writes:

I needed to convert the RSA private key that was laying around in
secstore into a format understood by UNIX® tools like SSH.

With asn12rsa(8) we can go from the ASN.1/DER to Plan 9 format, but not
back - so I wrote the libsec function asn1encodeRSApriv(2) and used it in
rsa2asn1(8) by adding the -a flag which causes the full private key to be
encoded and output.
2019-08-30 07:34:35 +02:00
cinap_lenrek a3beef2363 libsec: dummy mkfile for arm64 2019-05-03 21:01:29 +02:00
cinap_lenrek 117f177ccf libsec: fix memory leak in pkcs1_decrypt() 2018-12-02 03:23:48 +01:00
cinap_lenrek 8ff72ce20d libsec: remove asn1toDSApriv() 2018-01-06 08:34:25 +01:00
cinap_lenrek 57f8b6ec75 libsec: implement SPKI fingerprinting for okCertificate()
Instead of only using a hash over the whole certificate for
white/black-listing, now we can also use a hash over the
Subject Public Key Info (SPKI) field of the certificate which
contians the public key algorithm and the public key itself.

This allows certificates to be renewed independendtly of the
public key.

X509dump() now prints the public key thumbprint in addition
to the certificate thumbprint.

tlsclient will print the certificate when run with -D flag.

okCertificate() will print the public key thumbprint in its
error string when no match has been found.
2017-12-30 03:07:47 +01:00
cinap_lenrek b42d441a23 libsec: fix mistake: strnchr -> strchr 2017-12-30 02:47:02 +01:00
cinap_lenrek 582d2e664f libsec: avoid unneccesary memory copies and redundant code in x509
getting rid of some functions that take Byte* and instead
pass uchar* and length.

keeping the signature and public key fields in CertX509
as Bits* allows ownership transfer by swapping pointers.

use common code to copy CN from subject field.
2017-12-30 02:36:47 +01:00
cinap_lenrek e3cad82680 libsec: get rid of dummy data[1] in Bytes and Ints types (thanks pr) 2017-12-29 20:04:42 +01:00
cinap_lenrek f03260bf25 libsec: make includes consistent for sha2block*.c 2017-11-30 21:50:52 +01:00
cinap_lenrek 4cdd7049a6 libsec: unroll portable sha1block function
just 6-10% slower than most assembly versions.
20% faster on zynq.
2017-11-30 21:30:03 +01:00
cinap_lenrek c09cd2882c libsec: unroll portable sha2block functions
- unroll the loops
- rotate the taps on each step, avoiding copies
- simplify boolean formulas for Ch() and Maj()

this yields arround 40% throughput increase on 32/64bit
archs for sha2_256 and sha2_512 on amd64.
2017-11-30 02:16:27 +01:00
cinap_lenrek aa3c0e55f3 libsec: optimize aesCBCencrypt()/aesCBCdecrypt()
- get rid of the temporary copies and memmoves()
- when the data pointer is aligned, do xor and copying inline

speedup for auth/aescbc encryption depends on arch:

- zynq	7%	(arm)
- t23	13%	(386)
- x230	20%	(amd64, aes-ni)
- apu2	25% (amd64, aes-ni)
2017-11-27 01:31:19 +01:00
cinap_lenrek 077e719dfb libsec: write optimized _chachablock() function for amd64 / sse2
doing 4 quarterround's in parallel using 128-bit
vector registers. for second round shuffle the columns and
then shuffle back.

code is rather obvious. only trick here is for the first
quaterround PSHUFLW/PSHUFHW is used to swap the halfwords
for the <<<16 rotation.
2017-11-20 00:10:35 +01:00
cinap_lenrek 3356e0e731 libsec: AES-NI support for amd64
Add assembler versions for aes_encrypt/aes_decrypt and the key
setup using AES-NI instruction set. This makes aes_encrypt and
aes_decrypt into function pointers which get initialized by
the first call to setupAESstate().

Note that the expanded round key words are *NOT* stored in big
endian order as with the portable implementation. For that reason
the AESstate.ekey and AESstate.dkey fields have been changed to
void* forcing an error when someone is accessing the roundkey
words. One offender was aesXCBmac, which doesnt appear to be
used and the code looks horrible so it has been deleted.

The AES-NI implementation is for amd64 only as it requires the
kernel to save/restore the FPU state across syscalls and
pagefaults.
2017-11-12 23:15:15 +01:00
cinap_lenrek c021390e21 libsec: rewrite aex_xts_encrypt()/aes_xts_decrypt()
the previous implementation was not portable at all, assuming
little endian in gf_mulx() and that one can cast unaligned
pointers to ulong in xor128(). also the error code is likely
to be ignored, so better abort() when the length is not a
multiple of the AES block size.

we also pass in full AESstate structures now instead of
the expanded key longs, so that we do not need to hardcode
the number of rounds. this allows each indiviaul keys to
be bigger than 128 bit.
2017-10-29 21:49:24 +01:00
cinap_lenrek 29411f58cf libsec: make sectorNumber argument for aes_xts routines uvlong 2017-10-17 21:36:45 +02:00
cinap_lenrek 45b7d60bf3 libsec: add AES CFB and AES OFB stream ciphers 2017-10-17 21:34:01 +02:00
cinap_lenrek 8a67560183 libsec: export asn1encodedigest(), asn1encodeRSApub(), asn1toRSApub(), pkcs1padbuf() and pkcs1unpadbuf() 2017-10-06 20:52:18 +02:00
cinap_lenrek 5f42da1535 libsec: allow \r\n terminated lines in decodePEM() 2017-10-05 20:33:46 +02:00
cinap_lenrek 346f5828e0 libsec: sha256 support for thumbprint files, use it in ssh as well
initThumbprints() now takes an application tag argument
so x509 and ssh can coexist.

the thumbprint entries can now hold both sha1 and sha256
hashes. okThumbprint() now takes a len argument for the
hash length used.

the new function okCertificate() hashes the certificate
with both and checks for any matches.

on failure, okCertificate() returns 0 and sets error string.

we also check for include loops now in thumbfiles, limiting
the number of includes to 8.
2017-04-23 19:00:08 +02:00
cinap_lenrek 249ff9a24c tlshand: simplify tlsReadN() 2017-04-04 19:01:57 +02:00
cinap_lenrek de80075fc6 tlshand: fix mpint to bytes conversion, reorganize send/recv buffer, check for overflow in msgSend()
when converting mpint to bytes, always pad it to the size of
the modulus (RSA,DHE,ECDHE). mptobytes() now takes a byte len
parameter which the caller usually calculates from the group
modulus using mpsignif(). this bug sometimes caused "bad record mac"
after the handshake.

use a shared buffer, given that msgSend()/msgRecv() don't overlap
we can use the first half for sending, and the top half for
receiving, shifting down as neccesary. the space beween sendp and
recvp is free.

explicitely check for overflow in msgSend().
2017-04-04 01:59:17 +02:00
cinap_lenrek 6ff5c10ffb tlshand: fix ECDHE and DHE for SSLv3 2017-04-03 02:48:47 +02:00
cinap_lenrek 6840a9aafd libsec: zero name buffer in X509toECpub() 2017-02-10 21:42:11 +01:00
cinap_lenrek fb2abc2a04 libsec: make X509toECpub() return CN name like X509toRSApub() 2017-02-10 21:36:19 +01:00
cinap_lenrek 24007b9120 libsec: revert asn1mpint(), rewrite rsa signature validation, cleanups
reverting asn1mpint() as all users really just expect
unsigned integers here. also openssl seems to interpret
rsa modulus as unsigned no matter what... so keeping
it as it was before.

handle nil cipher bytes in factotum_rsa_decrypt() due
to pkcs1padbuf() failing.

apply some lessions from intels berzerk paper:

instead of parsing the decrypted digest info blob, we
generate the *expected* blob's for all digest algorithms
that match the digest size and compare the results.

provide pkcs1 pad and unpad functions that consistently
enforce minimum padding size and handles block types 1
and 2.
2017-02-10 00:08:38 +01:00
cinap_lenrek 6386a0391a libsec: handle signed asn.1 bigint to mpint conversion for x509 2017-02-06 21:40:42 +01:00
cinap_lenrek bbce9c0566 rsagen: prefer 65537 as the default exponent when elen == 0, otherwise pick randomly 2017-02-06 04:25:38 +01:00
cinap_lenrek 5256f4063e libsec: fix mkbigint(), asn.1 uses two's compement signed representation
quick fix is to bias the rounding so the msb will always
be zero. should write proper conversion code to actually
deal with signed mpints... also for asn1mpint()... -- cinap
2017-02-06 03:50:03 +01:00
cinap_lenrek 02b3c609ed libsec: check if modulus is too small for message in pkcs1padbuf() 2017-02-06 02:03:16 +01:00
cinap_lenrek 1df513a2a1 libsec: need PKCS#9 "Extension Request" attribute (rsareq()) 2017-02-06 01:01:34 +01:00
cinap_lenrek 00575e07bb libsec: have rsagen() always produce postive !dk to avoid confusion 2017-02-05 05:15:17 +01:00
cinap_lenrek e9bf14eceb libsec: avoid temp variables in chacha/salsa ENCRYPT() macro
given that we only pass uchar* with constant offsets
to the s and d arguments of ENCRYPT(), we do not need
the temporary variables sp/dp and the compiler is
smart enougth to combine the const offset with the ones
from GET4() and PUT4() and emit single load and store
instructions for the byte accesses.
2017-01-15 04:09:47 +01:00
cinap_lenrek 2e23780d2f libsec: implement extended 192-bit nonce xchacha variant and hchacha function 2017-01-12 20:16:38 +01:00
cinap_lenrek ed9fdc72f5 libsec: replace des based X9.17 genrandom() with chacha random number generator 2016-12-28 02:02:00 +01:00
cinap_lenrek 6fc0e0541c libsec: remove unused get32() function 2016-11-17 19:54:13 +01:00
ftrvxmtrx 8f221cfec1 libsec: remove unused aes_setupDec 2016-11-17 02:02:32 +01:00
cinap_lenrek c9d55cadb3 libsec: add secp384r1 curve parameters for tls 2016-10-30 02:15:40 +01:00
cinap_lenrek 1492f46f87 libsec: add scrypt password based key derivation function 2016-07-10 21:41:57 +02:00