{"record":{"id":"f87588b1f9f555ee","repo":"golang/go","slug":"crypto-rsa-missing-public-modulus-f87588","errorCode":null,"errorMessage":"crypto/rsa: missing public modulus","messagePattern":"crypto/rsa: missing public modulus","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/rsa/rsa.go","lineNumber":303,"sourceCode":"\n// rsa1024min is a GODEBUG that re-enables weak RSA keys if set to \"0\".\n// See https://go.dev/issue/68762.\nvar rsa1024min = godebug.New(\"rsa1024min\")\n\nfunc checkKeySize(size int) error {\n\tif size >= 1024 {\n\t\treturn nil\n\t}\n\tif rsa1024min.Value() == \"0\" {\n\t\trsa1024min.IncNonDefault()\n\t\treturn nil\n\t}\n\treturn fmt.Errorf(\"crypto/rsa: %d-bit keys are insecure (see https://go.dev/pkg/crypto/rsa#hdr-Minimum_key_size)\", size)\n}\n\nfunc checkPublicKeySize(k *PublicKey) error {\n\tif k.N == nil {\n\t\treturn errors.New(\"crypto/rsa: missing public modulus\")\n\t}\n\treturn checkKeySize(k.N.BitLen())\n}\n\n// GenerateKey generates a random RSA private key of the given bit size.\n//\n// If bits is less than 1024, [GenerateKey] returns an error. See the \"[Minimum\n// key size]\" section for further details.\n//\n// Since Go 1.26, a secure source of random bytes is always used, and the Reader is\n// ignored unless GODEBUG=cryptocustomrand=1 is set. This setting will be removed\n// in a future Go release. Instead, use [testing/cryptotest.SetGlobalRandom].\n//\n// [Minimum key size]: https://pkg.go.dev/crypto/rsa#hdr-Minimum_key_size\nfunc GenerateKey(random io.Reader, bits int) (*PrivateKey, error) {\n\tif err := checkKeySize(bits); err != nil {\n\t\treturn nil, err\n\t}","sourceCodeStart":285,"sourceCodeEnd":321,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/rsa/rsa.go#L285-L321","documentation":"Returned by checkPublicKeySize when k.N is nil. checkPublicKeySize runs at the entry of every public operation that uses a PublicKey (EncryptPKCS1v15, EncryptOAEP, VerifyPKCS1v15, VerifyPSS, SignJSON callers via the encrypt/verify wrappers). A nil N means the key was never populated — there is no modulus to operate on, so the operation cannot proceed.","triggerScenarios":"Use a zero-value rsa.PublicKey{}; call rsa.EncryptOAEP on a *rsa.PublicKey whose N was not assigned; parse a key with x509.ParsePKIXPublicKey but type-asserted to *rsa.PublicKey when the underlying algorithm was ECDSA (N stays nil).","commonSituations":"Default struct initialization in tests; wrong type assertion after parsing (e.g. *rsa.PublicKey vs *ecdsa.PublicKey); copying only E but not N from a parsed key.","solutions":["Ensure the *rsa.PublicKey was produced by x509.ParsePKIXPublicKey / ParsePKCS1PublicKey and that the input was actually RSA.","Add a nil check: if pub == nil || pub.N == nil { return ErrInvalidKey }.","Construct public-key variables only via parsing helpers or rsa.PrivateKey.PublicKey, never by hand."],"exampleFix":"// before\nvar pub rsa.PublicKey // zero value, N == nil\nct, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, &pub, msg, nil)\n\n// after\npubAny, err := x509.ParsePKIXPublicKey(der)\nif err != nil { return err }\npub, ok := pubAny.(*rsa.PublicKey)\nif !ok || pub.N == nil { return errors.New(\"not an RSA public key\") }\nct, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, pub, msg, nil)","handlingStrategy":"validation","validationCode":"if pub == nil || pub.N == nil {\n    return errors.New(\"RSA public key is missing modulus\")\n}\nreturn nil // then call EncryptOAEP/Verify*","typeGuard":"func isUsableRSAPublicKey(pub *rsa.PublicKey) bool {\n    return pub != nil && pub.N != nil && pub.N.Sign() > 0 && pub.E > 0\n}","tryCatchPattern":null,"preventionTips":["Parse public keys with x509.ParsePKIXPublicKey and type-assert *rsa.PublicKey.","Add nil/sign checks in a shared wrapper around Encrypt*/Verify*.","Avoid zero-value rsa.PublicKey literals."],"tags":["rsa","public-key","validation","crypto","nil-check"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}