This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
forked from spacemonkeygo/openssl
-
Notifications
You must be signed in to change notification settings - Fork 20
Add required bindings to support openssl in libp2p-tls #6
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -331,6 +331,14 @@ func (c *Certificate) AddExtension(nid NID, value string) error { | |
return nil | ||
} | ||
|
||
// AddCustomExtension add custom extenstion to the certificate. | ||
func (c *Certificate) AddCustomExtension(nid NID, value []byte) error { | ||
if int(C.add_custom_ext(c.x, C.int(nid), (*C.char)(C.CBytes(value)), C.int(len(value)))) == 0 { | ||
return errors.New("Unable to add extension") | ||
} | ||
return nil | ||
} | ||
|
||
// Wraps AddExtension using a map of NID to text extension. | ||
// Will return without finishing if it encounters an error. | ||
func (c *Certificate) AddExtensions(extensions map[NID]string) error { | ||
|
@@ -413,3 +421,17 @@ func (c *Certificate) SetVersion(version X509_Version) error { | |
} | ||
return nil | ||
} | ||
|
||
// GetExtensionValue returns the value of the given NID's extension. | ||
func (c *Certificate) GetExtensionValue(nid NID) []byte { | ||
dataLength := C.int(0) | ||
val := C.get_extention(c.x, C.int(nid), &dataLength) | ||
return charToBytes(val, int(dataLength)) | ||
} | ||
|
||
// charToBytes converts c unisgned char to golang bytes | ||
func charToBytes(src *C.uchar, sz int) []byte { | ||
dest := make([]byte, sz) | ||
copy(dest, (*(*[1024]byte)(unsafe.Pointer(src)))[:sz:sz]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is safe. This could cause us to read outside of a mapped page. I think we need to call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now, I'm using GoBytes. |
||
return dest | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
|
||
#include <openssl/x509v3.h> | ||
#include <string.h> | ||
|
||
const unsigned char * get_extention(X509 *x, int NID, int *data_len){ | ||
int loc; | ||
ASN1_OCTET_STRING *octet_str; | ||
long xlen; | ||
int tag, xclass; | ||
|
||
loc = X509_get_ext_by_NID( x, NID, -1); | ||
X509_EXTENSION *ex = X509_get_ext(x, loc); | ||
octet_str = X509_EXTENSION_get_data(ex); | ||
*data_len = octet_str->length; | ||
return octet_str->data; | ||
} | ||
|
||
// Copied from https://github.com/libtor/openssl/blob/master/demos/x509/mkcert.c#L153 | ||
int add_custom_ext(X509 *cert, int nid,unsigned char *value, int len) | ||
{ | ||
X509_EXTENSION *ex; | ||
ASN1_OCTET_STRING *os = ASN1_OCTET_STRING_new(); | ||
ASN1_OCTET_STRING_set(os,value,len); | ||
X509V3_CTX ctx; | ||
/* This sets the 'context' of the extensions. */ | ||
/* No configuration database */ | ||
X509V3_set_ctx_nodb(&ctx); | ||
/* Issuer and subject certs: both the target since it is self signed, | ||
* no request and no CRL | ||
*/ | ||
X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0); | ||
// ref http://openssl.6102.n7.nabble.com/Adding-a-custom-extension-to-a-CSR-td47446.html | ||
ex = X509_EXTENSION_create_by_NID( NULL, nid, 0, os); | ||
if (!X509_add_ext(cert,ex,-1)) | ||
return 0; | ||
|
||
X509_EXTENSION_free(ex); | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't we copying
value
inside the C code? If so, I think we need to either free the copied string after calling this, or call(*C.char)(unsafe.Pointer(&value[0]))
instead ofC.CBytes
.Note: I'm not an expert in CGO so I really have no idea what's safe and what's not safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've freed the memory