-
Notifications
You must be signed in to change notification settings - Fork 230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add proper DN construction #55
Changes from 2 commits
4420aa2
53fe0ae
92809d8
9782b3d
37421cd
f1bcfbe
214fef7
97eb860
04e0f06
14e1c13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,9 @@ package mgo | |
|
||
import ( | ||
"crypto/md5" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/asn1" | ||
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
|
@@ -825,6 +828,9 @@ type Credential struct { | |
// Mechanism defines the protocol for credential negotiation. | ||
// Defaults to "MONGODB-CR". | ||
Mechanism string | ||
|
||
// Certificate defines an x509 certificate for authentication at login. | ||
Certificate *x509.Certificate | ||
} | ||
|
||
// Login authenticates with MongoDB using the provided credential. The | ||
|
@@ -847,6 +853,14 @@ func (s *Session) Login(cred *Credential) error { | |
defer socket.Release() | ||
|
||
credCopy := *cred | ||
if cred.Certificate != nil && cred.Username != "" { | ||
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 am 99.99% sure that mongodb 3.4 doesn't need it. It will extract the user name from the client cert. 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. This probably breaks the deployment for people that specify the Username themselves. @domodwyer and @szank you know more about mgo auth then me. 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.
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. Still shouldn't the check rely on the mechanism specified? rather than the presence of a certificate? 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'd say. If the database is "$external" and the mechanism is "MONGODB-X509" then use the certificate. Overwrite the user name in the copy of the credentials. Otherwise use credentials as is. Users might want to use both forms of authentication, for example. This would be the safest way. 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. The only reason to pass a cert is to use it for authentication - if present, it's using cert-based auth, there's no reason to then set the mechanism to something else... This code should set the username/mechanism/database as appropriate when a cert is given. |
||
return fmt.Errorf("Failed to login, both certifcate and credentials are given.") | ||
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. It's more efficient to use Have a quick read of https://github.com/golang/go/wiki/CodeReviewComments though the existing mgo code doesn't exactly follow it |
||
} | ||
|
||
if cred.Certificate != nil { | ||
credCopy.Username = getRFC2253NameString(cred.Certificate) | ||
} | ||
|
||
if cred.Source == "" { | ||
if cred.Mechanism == "GSSAPI" { | ||
credCopy.Source = "$external" | ||
|
@@ -5212,3 +5226,67 @@ func hasErrMsg(d []byte) bool { | |
} | ||
return false | ||
} | ||
|
||
// getRFC2253NameString converts from an ASN.1 structured representation of the certificate | ||
// to a UTF-8 string representation(RDN) and returns it. | ||
func getRFC2253NameString(certificate *x509.Certificate) string { | ||
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. This should have unit tests covering at the very least, the examples in section 5 of the RFC - if you make a function that accepts the unmarshaled If you do the above, |
||
var RDNElementsASN1 = pkix.RDNSequence{} | ||
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. /RDNElementsASN1/rdnSequence The elements in there are not ASN1 anymore. |
||
asn1.Unmarshal(certificate.RawSubject, &RDNElementsASN1) | ||
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. Better to not swallow the error and return it to the caller as this indicates a broken/bad certificate and would be good to know. |
||
|
||
var RDNElementsString = []string{} | ||
for i := len(RDNElementsASN1) - 1; i >= 0; i-- { | ||
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. Is there a reason to iterate from the back? DN ordering? 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. There is. Something about AD field ordering. 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. the sequence is in reverse order, in the RFC it says "the output consists of the string encodings of each RelativeDistinguishedName in the RDNSequence, starting with the last element of the sequence and moving backwards toward the first." |
||
var nameAndValueList = []string{} | ||
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. This is not a critical path I think, but as a tip: var nameAndValueList = make([]string,len(RDNElementsASN1[i)) |
||
for _, attribute := range RDNElementsASN1[i] { | ||
var shortAttributeName = rdnOIDToShortName(attribute.Type) | ||
if len(shortAttributeName) > 0 { | ||
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. Invert conditional and |
||
var attributeValueString = attribute.Value.(string) | ||
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. It's not immediately clear to me, why certain DN elements need sanitizing and the ones we don't understand don't. The logic here could be much tidier if we sanitize everything. 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. according to the RFC if the element OID is not one of the "well known ones" you hex encode the DER representation of the element. Please see my comment below. Users can still use such certs, they just need to use openssl to return a proper string representation of the RDN. |
||
// escape leading space or # | ||
if strings.IndexAny(attributeValueString, " #") == 0 { | ||
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.
|
||
attributeValueString = "\\" + attributeValueString | ||
} | ||
// escape trailing space (unless the trailing space is also the first (unescaped) character) | ||
if len(attributeValueString) > 2 && attributeValueString[len(attributeValueString)-1] == ' ' { | ||
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. @szank mgo doesn't handle trailing spaces in RDN? 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. IIRC this is RFC requirement. Nothing to do with mgo. Leading and trailing spaces needs to be escaped. |
||
attributeValueString = attributeValueString[:len(attributeValueString)-1] + "\\ " | ||
} | ||
|
||
// escape , = + < > # ; | ||
var replacer = strings.NewReplacer(",", "\\,", "=", "\\=", "+", "\\+", "<", "\\<", ">", "\\>", "#", "\\#", ";", "\\;") | ||
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. Initialise |
||
attributeValueString = replacer.Replace(attributeValueString) | ||
nameAndValueList = append(nameAndValueList, fmt.Sprintf("%s=%s", shortAttributeName, attributeValueString)) | ||
} else { | ||
nameAndValueList = append(nameAndValueList, fmt.Sprintf("%s=%X", attribute.Type.String(), attribute.Value.([]byte))) | ||
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. From the RFC: Dotted decimal is OID, in human terms. We are missing the # before hex encoding tho. 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. FWIW, this code was not designed to handle cases like newline char 0x0A (\n) either. I have written a internal method that was good at converting our own cert's RDNs that weren't supposed to contain any stupid input, and had limited list of RDN field types. This is the reason for few decisions here, like the one from line 5257. I am not sure I am handling unknown OIDS correctly ( even if we add the '#' sign there. |
||
} | ||
} | ||
|
||
RDNElementsString = append(RDNElementsString, strings.Join(nameAndValueList, "+")) | ||
} | ||
|
||
return strings.Join(RDNElementsString, ",") | ||
} | ||
|
||
var oidsToShortNames = []struct { | ||
oid asn1.ObjectIdentifier | ||
shortName string | ||
}{ | ||
{asn1.ObjectIdentifier{2, 5, 4, 3}, "CN"}, | ||
{asn1.ObjectIdentifier{2, 5, 4, 6}, "C"}, | ||
{asn1.ObjectIdentifier{2, 5, 4, 7}, "L"}, | ||
{asn1.ObjectIdentifier{2, 5, 4, 8}, "ST"}, | ||
{asn1.ObjectIdentifier{2, 5, 4, 10}, "O"}, | ||
{asn1.ObjectIdentifier{2, 5, 4, 11}, "OU"}, | ||
{asn1.ObjectIdentifier{2, 5, 4, 9}, "STREET"}, | ||
{asn1.ObjectIdentifier{0, 9, 2342, 19200300, 100, 1, 25}, "DC"}, | ||
{asn1.ObjectIdentifier{0, 9, 2342, 19200300, 100, 1, 1}, "UID"}, | ||
} | ||
|
||
// rdnOIDToShortName returns an short name of the given RDN OID. If the OID does not have a short | ||
// name, the function returns an empty string | ||
func rdnOIDToShortName(oid asn1.ObjectIdentifier) string { | ||
for i := range oidsToShortNames { | ||
if oidsToShortNames[i].oid.Equal(oid) { | ||
return oidsToShortNames[i].shortName | ||
} | ||
} | ||
|
||
return "" | ||
} |
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.
The documentation here should specify the constraints - specifically:
Username
field is populated from the cert and should not be setMechanism
field should beMONGODB-X509
or not set (automatically populate in this case)Source
field should be$external
or not set (auto populate too)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 would also add link to
https://docs.mongodb.com/manual/tutorial/configure-x509-client-authentication/