Skip to content

Commit e433068

Browse files
committed
Changes for PR
Fix to mgf1SHA1Identifier (only had sha1 oid in params but needed sha1Identifier) Style changes and comments
1 parent 80edb96 commit e433068

File tree

2 files changed

+49
-57
lines changed

2 files changed

+49
-57
lines changed

x509/x509.go

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
// - RPKI support:
3636
// - Support for SubjectInfoAccess extension
3737
// - Support for RFC3779 extensions (in rpki.go)
38+
// - RSAES-OAEP support:
39+
// - Support for parsing RSASES-OAEP public keys from certificates
3840
// - General improvements:
3941
// - Export and use OID values throughout.
4042
// - Export OIDFromNamedCurve().
@@ -190,6 +192,9 @@ type tbsCertificate struct {
190192
Extensions []pkix.Extension `asn1:"optional,explicit,tag:3"`
191193
}
192194

195+
// RFC 4055, 4.1
196+
// The current ASN.1 parser does not support non-integer defaults so
197+
// the 'default:' tags here do nothing.
193198
type rsaesoaepAlgorithmParameters struct {
194199
HashFunc pkix.AlgorithmIdentifier `asn1:"optional,explicit,tag:0,default:sha1Identifier"`
195200
MaskgenFunc pkix.AlgorithmIdentifier `asn1:"optional,explicit,tag:1,default:mgf1SHA1Identifier"`
@@ -244,34 +249,36 @@ const (
244249
SHA512WithRSAPSS
245250
)
246251

247-
// RFC 4055
248-
// Various identifiers
249-
var (
250-
// 2.1. One-way Hash Functions
251-
OIDSHA1 = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 26}
252-
// 2.2. Mask Generation Functions
253-
OIDMFGSHA1 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 8}
254-
// 6. Basic object identifiers
255-
OIDpSpecified = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 9}
256-
)
252+
// RFC 4055, 6. Basic object identifiers
253+
var oidpSpecified = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 9}
257254

258255
// These are the default parameters for an RSAES-OAEP pubkey.
259256
// The current ASN.1 parser does not support non-integer defaults so
260257
// these currently do nothing.
261-
// It has been suggested that we switch to cryptobytes but that
262-
// only supports parsing primitives (tag < 32).
263258
var (
264259
sha1Identifier = pkix.AlgorithmIdentifier{
265-
Algorithm: OIDSHA1,
260+
Algorithm: oidSHA1,
266261
Parameters: asn1.NullRawValue,
267262
}
268263
mgf1SHA1Identifier = pkix.AlgorithmIdentifier{
269-
Algorithm: OIDMFGSHA1,
270-
Parameters: asn1.RawValue{0, 6, false, []byte{43, 14, 3, 2, 26}, []byte{6, 5, 43, 14, 3, 2, 26}},
264+
Algorithm: oidMGF1,
265+
// RFC 4055, 2.1 sha1Identifier
266+
Parameters: asn1.RawValue{
267+
Class: asn1.ClassUniversal,
268+
Tag: asn1.TagSequence,
269+
IsCompound: false,
270+
Bytes: []byte{6, 5, 43, 14, 3, 2, 26, 5, 0},
271+
FullBytes: []byte{16, 9, 6, 5, 43, 14, 3, 2, 26, 5, 0}},
271272
}
272273
pSpecifiedEmptyIdentifier = pkix.AlgorithmIdentifier{
273-
Algorithm: OIDpSpecified,
274-
Parameters: asn1.RawValue{0, 4, false, []byte{}, []byte{4, 0}},
274+
Algorithm: oidpSpecified,
275+
// RFC 4055, 4.1 nullOctetString
276+
Parameters: asn1.RawValue{
277+
Class: asn1.ClassUniversal,
278+
Tag: asn1.TagOctetString,
279+
IsCompound: false,
280+
Bytes: []byte{},
281+
FullBytes: []byte{4, 0}},
275282
}
276283
)
277284

@@ -384,6 +391,7 @@ var (
384391
oidSignatureECDSAWithSHA384 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 3}
385392
oidSignatureECDSAWithSHA512 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 4}
386393

394+
oidSHA1 = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 26}
387395
oidSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1}
388396
oidSHA384 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 2}
389397
oidSHA512 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 3}
@@ -1286,40 +1294,28 @@ type distributionPointName struct {
12861294
func parsePublicKey(algo PublicKeyAlgorithm, keyData *publicKeyInfo, nfe *NonFatalErrors) (interface{}, error) {
12871295
asn1Data := keyData.PublicKey.RightAlign()
12881296
switch algo {
1289-
case RSA:
1297+
case RSA, RSAESOAEP:
12901298
// RSA public keys must have a NULL in the parameters.
12911299
// See RFC 3279, Section 2.3.1.
1292-
if !bytes.Equal(keyData.Algorithm.Parameters.FullBytes, asn1.NullBytes) {
1300+
if algo == RSA && !bytes.Equal(keyData.Algorithm.Parameters.FullBytes, asn1.NullBytes) {
12931301
nfe.AddError(errors.New("x509: RSA key missing NULL parameters"))
12941302
}
1295-
1296-
p := new(pkcs1PublicKey)
1297-
rest, err := asn1.Unmarshal(asn1Data, p)
1298-
if err != nil {
1299-
var laxErr error
1300-
rest, laxErr = asn1.UnmarshalWithParams(asn1Data, p, "lax")
1301-
if laxErr != nil {
1302-
return nil, laxErr
1303+
if algo == RSAESOAEP {
1304+
// We only parse the parameters to ensure it is a valid encoding, we throw out the actually values
1305+
paramsData := keyData.Algorithm.Parameters.FullBytes
1306+
params := new(rsaesoaepAlgorithmParameters)
1307+
params.HashFunc = sha1Identifier
1308+
params.MaskgenFunc = mgf1SHA1Identifier
1309+
params.PSourceFunc = pSpecifiedEmptyIdentifier
1310+
rest, err := asn1.Unmarshal(paramsData, params)
1311+
if err != nil {
1312+
return nil, err
1313+
}
1314+
if len(rest) != 0 {
1315+
return nil, errors.New("x509: trailing data after RSAES-OAEP parameters")
13031316
}
1304-
nfe.AddError(err)
1305-
}
1306-
if len(rest) != 0 {
1307-
return nil, errors.New("x509: trailing data after RSA public key")
1308-
}
1309-
1310-
if p.N.Sign() <= 0 {
1311-
nfe.AddError(errors.New("x509: RSA modulus is not a positive number"))
1312-
}
1313-
if p.E <= 0 {
1314-
return nil, errors.New("x509: RSA public exponent is not a positive number")
13151317
}
13161318

1317-
pub := &rsa.PublicKey{
1318-
E: p.E,
1319-
N: p.N,
1320-
}
1321-
return pub, nil
1322-
case RSAESOAEP:
13231319
p := new(pkcs1PublicKey)
13241320
rest, err := asn1.Unmarshal(asn1Data, p)
13251321
if err != nil {
@@ -1339,16 +1335,8 @@ func parsePublicKey(algo PublicKeyAlgorithm, keyData *publicKeyInfo, nfe *NonFat
13391335
if p.E <= 0 {
13401336
return nil, errors.New("x509: RSA public exponent is not a positive number")
13411337
}
1342-
paramsData := keyData.Algorithm.Parameters.FullBytes
1343-
params := new(rsaesoaepAlgorithmParameters)
1344-
rest, err = asn1.Unmarshal(paramsData, params)
1345-
if err != nil {
1346-
return nil, err
1347-
}
1348-
if len(rest) != 0 {
1349-
return nil, errors.New("x509: trailing data after RSAES-OAEP parameters")
1350-
}
13511338

1339+
// TODO(dkarch): Update to return the parameters once crypto/x509 has come up with permanent solution
13521340
pub := &rsa.PublicKey{
13531341
E: p.E,
13541342
N: p.N,

x509/x509_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,11 +1119,15 @@ func TestRSAPSSSelfSigned(t *testing.T) {
11191119
}
11201120
}
11211121

1122-
// Valid EKCert with RSAES-OAEP Public Key
1122+
// Valid EKCert (from a TPM) with RSAES-OAEP Public Key.
1123+
// TPM1.2 uses RSA keys with OAEP padding (SHA1).
1124+
// The hardware only supports SHA1 so manufacturers have
1125+
// since switched to using rsaEncryption keys but millions
1126+
// of certificates still exist that have this type of key.
11231127
var oaepCertDER = `3082056e30820456a003020102020464f5bda5300d06092a864886f70d01010505003077310b3009060355040613024445310f300d060355040813065361786f6e793121301f060355040a1318496e66696e656f6e20546563686e6f6c6f67696573204147310c300a060355040b130341494d312630240603550403131d4946582054504d20454b20496e7465726d656469617465204341203230301e170d3135303932313231323932385a170d3235303932313231323932385a300030820137302206092a864886f70d0101073015a213301106092a864886f70d0101090404544350410382010f003082010a02820101008e7983105063a3f1c2e77d7c8b4f411db9c76f11366ccbb11757001fa51805bbbf68b6dccd9ad28a82c6a831e1591f06181c2e328c261cf9a14ff1704dc3261cc16da751760141969330a75b3fc5ae185ac76d636a97a339838bd042aa7c5999f63f946c6987b0e8be8f5908d08563f62bc6ee9510e36752bd3b2e7b55281bc92a8dcc8ba8a30d6aaa7580b672d83802449d34bfea0434edea4dbc3a9b201f3de0bf5ab2ca96a5254f4e76a58adc8d3bc385be94e93e0052e4066f238a9c1195eaacda02a6dc78393063340054e3ce99754a8770c8efcca45ad8fc999f7aaa67a8a83960141e5f4b892d3af333f09b6d13e60900e0ea8bd3b5eea30f4f2b889b0203010001a38202623082025e30550603551d110101ff044b3049a447304531163014060567810502010c0b69643a343934363538303031173015060567810502020c0c534c42393636307878312e3231123010060567810502030c0769643a30343238300c0603551d130101ff040230003081bc0603551d200101ff0481b13081ae3081ab060b6086480186f84501072f0130819b303906082b06010505070201162d687474703a2f2f7777772e766572697369676e2e636f6d2f7265706f7369746f72792f696e6465782e68746d6c305e06082b0601050507020230521e5000540043005000410020005400720075007300740065006400200050006c006100740066006f0072006d0020004d006f00640075006c006500200045006e0064006f007200730065006d0065006e00743081a10603551d2304819930819680148ffd47880e239a3a3a20de13edf101e882a9d21da17ba4793077310b3009060355040613024445310f300d060355040813065361786f6e793121301f060355040a1318496e66696e656f6e20546563686e6f6c6f67696573204147310c300a060355040b130341494d312630240603550403131d4946582054504d20454b20496e7465726d6564696174652043412032308201053081930603551d0904818b308188303a06035504343133300b300906052b0e03021a05003024302206092a864886f70d0101073015a213301106092a864886f70d010109040454435041301606056781050210310d300b0c03312e32020102020103303206056781050212312930270101ffa0030a0101a1030a0100a2030a0100a310300e1603332e310a01040a01010101ff0101ff300d06092a864886f70d010105050003820101003b25d3958cf08b2c32cefb40570f845e25fa98fd38a47be8c16e4ef663d8f057824ff550052b4a338e353b08bfe70364cda07961d3f4fb5cbf548497389a5ab59c3469d14b6f5bfdc27db3dd0cbd21ac818a64a14032cd433a2bffb939b5f54555c2b6892b5f6479e3c38bd4b30283d2e8ee57ecad779dae90bd9d6052ad6bf3efbb30f639e03b0a239f539a476b129540bc9806e2dc7011d265dca8a95301aaba872b4e176bdb670e65b750b0afdc2ca97b3f0bef55862ae135bd86b1d3a28d4eedfe5ee445a3f1d65bbad0adae49999e613eabb133166461cf47146de078a7d7f550940a1a475ecc834ee784a6a6e42fa4ad7869ef8d2b2251c830efb38496`
11241128

11251129
func TestParseCertificateWithRSAESOAEPPublicKey(t *testing.T) {
1126-
expectedKey := &rsa.PublicKey{
1130+
wantKey := &rsa.PublicKey{
11271131
E: 65537,
11281132
N: bigFromHexString("8e7983105063a3f1c2e77d7c8b4f411db9c76f11366ccbb11757001fa51805bbbf68b6dccd9ad28a82c6a831e1591f06181c2e328c261cf9a14ff1704dc3261cc16da751760141969330a75b3fc5ae185ac76d636a97a339838bd042aa7c5999f63f946c6987b0e8be8f5908d08563f62bc6ee9510e36752bd3b2e7b55281bc92a8dcc8ba8a30d6aaa7580b672d83802449d34bfea0434edea4dbc3a9b201f3de0bf5ab2ca96a5254f4e76a58adc8d3bc385be94e93e0052e4066f238a9c1195eaacda02a6dc78393063340054e3ce99754a8770c8efcca45ad8fc999f7aaa67a8a83960141e5f4b892d3af333f09b6d13e60900e0ea8bd3b5eea30f4f2b889b"),
11291133
}
@@ -1142,8 +1146,8 @@ func TestParseCertificateWithRSAESOAEPPublicKey(t *testing.T) {
11421146
if !ok {
11431147
t.Fatalf("Parsed key was not an RSA key: %s", err)
11441148
}
1145-
if expectedKey.E != parsedKey.E ||
1146-
expectedKey.N.Cmp(parsedKey.N) != 0 {
1149+
if wantKey.E != parsedKey.E ||
1150+
wantKey.N.Cmp(parsedKey.N) != 0 {
11471151
t.Fatal("Parsed key differs from expected key")
11481152
}
11491153
}

0 commit comments

Comments
 (0)