Skip to content
This repository was archived by the owner on Jan 7, 2023. It is now read-only.

Commit ab3d2c3

Browse files
authored
Merge pull request #6 from balajijinnah/balaji/tls
Add required bindings to support openssl in libp2p-tls
2 parents 050e850 + 38a6bec commit ab3d2c3

File tree

5 files changed

+89
-3
lines changed

5 files changed

+89
-3
lines changed

cert.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,16 @@ func (c *Certificate) AddExtension(nid NID, value string) error {
331331
return nil
332332
}
333333

334+
// AddCustomExtension add custom extenstion to the certificate.
335+
func (c *Certificate) AddCustomExtension(nid NID, value []byte) error {
336+
val := (*C.char)(C.CBytes(value))
337+
defer C.free(unsafe.Pointer(val))
338+
if int(C.add_custom_ext(c.x, C.int(nid), val, C.int(len(value)))) == 0 {
339+
return errors.New("Unable to add extension")
340+
}
341+
return nil
342+
}
343+
334344
// Wraps AddExtension using a map of NID to text extension.
335345
// Will return without finishing if it encounters an error.
336346
func (c *Certificate) AddExtensions(extensions map[NID]string) error {
@@ -413,3 +423,10 @@ func (c *Certificate) SetVersion(version X509_Version) error {
413423
}
414424
return nil
415425
}
426+
427+
// GetExtensionValue returns the value of the given NID's extension.
428+
func (c *Certificate) GetExtensionValue(nid NID) []byte {
429+
dataLength := C.int(0)
430+
val := C.get_extention(c.x, C.int(nid), &dataLength)
431+
return C.GoBytes(unsafe.Pointer(val), dataLength)
432+
}

ctx.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,29 @@ func (c *Ctx) SetCipherList(list string) error {
522522
return nil
523523
}
524524

525+
// SetNextProtos sets Negotiation protocol to the ctx.
526+
func (c *Ctx) SetNextProtos(protos []string) error {
527+
if len(protos) == 0 {
528+
return nil
529+
}
530+
vector := make([]byte, 0)
531+
for _, proto := range protos {
532+
if len(proto) > 255 {
533+
return fmt.Errorf(
534+
"Proto length can't be more than 255. But got a proto %s with length %d",
535+
proto, len(proto))
536+
}
537+
vector = append(vector, byte(uint8(len(proto))))
538+
vector = append(vector, []byte(proto)...)
539+
}
540+
ret := int(C.SSL_CTX_set_alpn_protos(c.ctx, (*C.uchar)(unsafe.Pointer(&vector[0])),
541+
C.uint(len(vector))))
542+
if ret != 0 {
543+
return errors.New("Error while setting protos to ctx")
544+
}
545+
return nil
546+
}
547+
525548
type SessionCacheModes int
526549

527550
const (

extension.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
3+
#include <openssl/x509v3.h>
4+
#include <string.h>
5+
6+
const unsigned char * get_extention(X509 *x, int NID, int *data_len){
7+
int loc;
8+
ASN1_OCTET_STRING *octet_str;
9+
long xlen;
10+
int tag, xclass;
11+
12+
loc = X509_get_ext_by_NID( x, NID, -1);
13+
X509_EXTENSION *ex = X509_get_ext(x, loc);
14+
octet_str = X509_EXTENSION_get_data(ex);
15+
*data_len = octet_str->length;
16+
return octet_str->data;
17+
}
18+
19+
// Copied from https://github.com/libtor/openssl/blob/master/demos/x509/mkcert.c#L153
20+
int add_custom_ext(X509 *cert, int nid,unsigned char *value, int len)
21+
{
22+
X509_EXTENSION *ex;
23+
ASN1_OCTET_STRING *os = ASN1_OCTET_STRING_new();
24+
ASN1_OCTET_STRING_set(os,value,len);
25+
X509V3_CTX ctx;
26+
/* This sets the 'context' of the extensions. */
27+
/* No configuration database */
28+
X509V3_set_ctx_nodb(&ctx);
29+
/* Issuer and subject certs: both the target since it is self signed,
30+
* no request and no CRL
31+
*/
32+
X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
33+
// ref http://openssl.6102.n7.nabble.com/Adding-a-custom-extension-to-a-CSR-td47446.html
34+
ex = X509_EXTENSION_create_by_NID( NULL, nid, 0, os);
35+
if (!X509_add_ext(cert,ex,-1))
36+
return 0;
37+
38+
X509_EXTENSION_free(ex);
39+
return 1;
40+
}

object.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ import "C"
1919

2020
// CreateObjectIdentifier creates ObjectIdentifier and returns NID for the created
2121
// ObjectIdentifier
22-
func CreateObjectIdentifier(oid string, shortName string, longName string) int {
23-
return int(C.OBJ_create(C.CString(oid), C.CString(shortName), C.CString(longName)))
22+
func CreateObjectIdentifier(oid string, shortName string, longName string) NID {
23+
return NID(C.OBJ_create(C.CString(oid), C.CString(shortName), C.CString(longName)))
2424
}

shim.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ extern int X_SSL_CTX_set_tlsext_ticket_key_cb(SSL_CTX *sslctx,
9090
extern int X_SSL_CTX_ticket_key_cb(SSL *s, unsigned char key_name[16],
9191
unsigned char iv[EVP_MAX_IV_LENGTH],
9292
EVP_CIPHER_CTX *cctx, HMAC_CTX *hctx, int enc);
93+
extern int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
94+
unsigned int protos_len);
9395

9496
/* BIO methods */
9597
extern int X_BIO_get_flags(BIO *b);
@@ -173,4 +175,8 @@ extern int X_X509_set_version(X509 *x, long version);
173175
extern int X_PEM_write_bio_PrivateKey_traditional(BIO *bio, EVP_PKEY *key, const EVP_CIPHER *enc, unsigned char *kstr, int klen, pem_password_cb *cb, void *u);
174176

175177
/* Object methods */
176-
extern int OBJ_create(const char *oid,const char *sn,const char *ln);
178+
extern int OBJ_create(const char *oid,const char *sn,const char *ln);
179+
180+
/* Extension helper method */
181+
extern const unsigned char * get_extention(X509 *x, int NID, int *data_len);
182+
extern int add_custom_ext(X509 *cert, int nid, char *value, int len);

0 commit comments

Comments
 (0)