123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291 |
- // Copyright 2009 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- // Package x509 parses X.509-encoded keys and certificates.
- //
- // On UNIX systems the environment variables SSL_CERT_FILE and SSL_CERT_DIR
- // can be used to override the system default locations for the SSL certificate
- // file and SSL certificate files directory, respectively.
- //
- // This is a fork of the Go library crypto/x509 package, primarily adapted for
- // use with Certificate Transparency. Main areas of difference are:
- //
- // - Life as a fork:
- // - Rename OS-specific cgo code so it doesn't clash with main Go library.
- // - Use local library imports (asn1, pkix) throughout.
- // - Add version-specific wrappers for Go version-incompatible code (in
- // ptr_*_windows.go).
- // - Laxer certificate parsing:
- // - Add options to disable various validation checks (times, EKUs etc).
- // - Use NonFatalErrors type for some errors and continue parsing; this
- // can be checked with IsFatal(err).
- // - Support for short bitlength ECDSA curves (in curves.go).
- // - Certificate Transparency specific function:
- // - Parsing and marshaling of SCTList extension.
- // - RemoveSCTList() function for rebuilding CT leaf entry.
- // - Pre-certificate processing (RemoveCTPoison(), BuildPrecertTBS(),
- // ParseTBSCertificate(), IsPrecertificate()).
- // - Revocation list processing:
- // - Detailed CRL parsing (in revoked.go)
- // - Detailed error recording mechanism (in error.go, errors.go)
- // - Factor out parseDistributionPoints() for reuse.
- // - Factor out and generalize GeneralNames parsing (in names.go)
- // - Fix CRL commenting.
- // - RPKI support:
- // - Support for SubjectInfoAccess extension
- // - Support for RFC3779 extensions (in rpki.go)
- // - RSAES-OAEP support:
- // - Support for parsing RSASES-OAEP public keys from certificates
- // - Ed25519 support:
- // - Support for parsing and marshaling Ed25519 keys
- // - General improvements:
- // - Export and use OID values throughout.
- // - Export OIDFromNamedCurve().
- // - Export SignatureAlgorithmFromAI().
- // - Add OID value to UnhandledCriticalExtension error.
- // - Minor typo/lint fixes.
- package x509
- import (
- "bytes"
- "crypto"
- "crypto/dsa"
- "crypto/ecdsa"
- "crypto/elliptic"
- "crypto/rsa"
- _ "crypto/sha1"
- _ "crypto/sha256"
- _ "crypto/sha512"
- "encoding/pem"
- "errors"
- "fmt"
- "io"
- "math/big"
- "net"
- "net/url"
- "strconv"
- "strings"
- "time"
- "unicode/utf8"
- "golang.org/x/crypto/cryptobyte"
- cryptobyte_asn1 "golang.org/x/crypto/cryptobyte/asn1"
- "golang.org/x/crypto/ed25519"
- "github.com/google/certificate-transparency-go/asn1"
- "github.com/google/certificate-transparency-go/tls"
- "github.com/google/certificate-transparency-go/x509/pkix"
- )
- // pkixPublicKey reflects a PKIX public key structure. See SubjectPublicKeyInfo
- // in RFC 3280.
- type pkixPublicKey struct {
- Algo pkix.AlgorithmIdentifier
- BitString asn1.BitString
- }
- // ParsePKIXPublicKey parses a public key in PKIX, ASN.1 DER form.
- //
- // It returns a *rsa.PublicKey, *dsa.PublicKey, *ecdsa.PublicKey, or
- // ed25519.PublicKey. More types might be supported in the future.
- //
- // This kind of key is commonly encoded in PEM blocks of type "PUBLIC KEY".
- func ParsePKIXPublicKey(derBytes []byte) (pub interface{}, err error) {
- var pki publicKeyInfo
- if rest, err := asn1.Unmarshal(derBytes, &pki); err != nil {
- return nil, err
- } else if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after ASN.1 of public-key")
- }
- algo := getPublicKeyAlgorithmFromOID(pki.Algorithm.Algorithm)
- if algo == UnknownPublicKeyAlgorithm {
- return nil, errors.New("x509: unknown public key algorithm")
- }
- var nfe NonFatalErrors
- pub, err = parsePublicKey(algo, &pki, &nfe)
- if err != nil {
- return pub, err
- }
- // Treat non-fatal errors as fatal for this entrypoint.
- if len(nfe.Errors) > 0 {
- return nil, nfe.Errors[0]
- }
- return pub, nil
- }
- func marshalPublicKey(pub interface{}) (publicKeyBytes []byte, publicKeyAlgorithm pkix.AlgorithmIdentifier, err error) {
- switch pub := pub.(type) {
- case *rsa.PublicKey:
- publicKeyBytes, err = asn1.Marshal(pkcs1PublicKey{
- N: pub.N,
- E: pub.E,
- })
- if err != nil {
- return nil, pkix.AlgorithmIdentifier{}, err
- }
- publicKeyAlgorithm.Algorithm = OIDPublicKeyRSA
- // This is a NULL parameters value which is required by
- // RFC 3279, Section 2.3.1.
- publicKeyAlgorithm.Parameters = asn1.NullRawValue
- case *ecdsa.PublicKey:
- publicKeyBytes = elliptic.Marshal(pub.Curve, pub.X, pub.Y)
- oid, ok := OIDFromNamedCurve(pub.Curve)
- if !ok {
- return nil, pkix.AlgorithmIdentifier{}, errors.New("x509: unsupported elliptic curve")
- }
- publicKeyAlgorithm.Algorithm = OIDPublicKeyECDSA
- var paramBytes []byte
- paramBytes, err = asn1.Marshal(oid)
- if err != nil {
- return
- }
- publicKeyAlgorithm.Parameters.FullBytes = paramBytes
- case ed25519.PublicKey:
- publicKeyBytes = pub
- publicKeyAlgorithm.Algorithm = OIDPublicKeyEd25519
- default:
- return nil, pkix.AlgorithmIdentifier{}, fmt.Errorf("x509: unsupported public key type: %T", pub)
- }
- return publicKeyBytes, publicKeyAlgorithm, nil
- }
- // MarshalPKIXPublicKey converts a public key to PKIX, ASN.1 DER form.
- //
- // The following key types are currently supported: *rsa.PublicKey, *ecdsa.PublicKey
- // and ed25519.PublicKey. Unsupported key types result in an error.
- //
- // This kind of key is commonly encoded in PEM blocks of type "PUBLIC KEY".
- func MarshalPKIXPublicKey(pub interface{}) ([]byte, error) {
- var publicKeyBytes []byte
- var publicKeyAlgorithm pkix.AlgorithmIdentifier
- var err error
- if publicKeyBytes, publicKeyAlgorithm, err = marshalPublicKey(pub); err != nil {
- return nil, err
- }
- pkix := pkixPublicKey{
- Algo: publicKeyAlgorithm,
- BitString: asn1.BitString{
- Bytes: publicKeyBytes,
- BitLength: 8 * len(publicKeyBytes),
- },
- }
- ret, _ := asn1.Marshal(pkix)
- return ret, nil
- }
- // These structures reflect the ASN.1 structure of X.509 certificates.:
- type certificate struct {
- Raw asn1.RawContent
- TBSCertificate tbsCertificate
- SignatureAlgorithm pkix.AlgorithmIdentifier
- SignatureValue asn1.BitString
- }
- type tbsCertificate struct {
- Raw asn1.RawContent
- Version int `asn1:"optional,explicit,default:0,tag:0"`
- SerialNumber *big.Int
- SignatureAlgorithm pkix.AlgorithmIdentifier
- Issuer asn1.RawValue
- Validity validity
- Subject asn1.RawValue
- PublicKey publicKeyInfo
- UniqueId asn1.BitString `asn1:"optional,tag:1"`
- SubjectUniqueId asn1.BitString `asn1:"optional,tag:2"`
- Extensions []pkix.Extension `asn1:"optional,explicit,tag:3"`
- }
- // RFC 4055, 4.1
- // The current ASN.1 parser does not support non-integer defaults so
- // the 'default:' tags here do nothing.
- type rsaesoaepAlgorithmParameters struct {
- HashFunc pkix.AlgorithmIdentifier `asn1:"optional,explicit,tag:0,default:sha1Identifier"`
- MaskgenFunc pkix.AlgorithmIdentifier `asn1:"optional,explicit,tag:1,default:mgf1SHA1Identifier"`
- PSourceFunc pkix.AlgorithmIdentifier `asn1:"optional,explicit,tag:2,default:pSpecifiedEmptyIdentifier"`
- }
- type dsaAlgorithmParameters struct {
- P, Q, G *big.Int
- }
- type dsaSignature struct {
- R, S *big.Int
- }
- type ecdsaSignature dsaSignature
- type validity struct {
- NotBefore, NotAfter time.Time
- }
- type publicKeyInfo struct {
- Raw asn1.RawContent
- Algorithm pkix.AlgorithmIdentifier
- PublicKey asn1.BitString
- }
- // RFC 5280, 4.2.1.1
- type authKeyId struct {
- Id []byte `asn1:"optional,tag:0"`
- }
- // SignatureAlgorithm indicates the algorithm used to sign a certificate.
- type SignatureAlgorithm int
- // SignatureAlgorithm values:
- const (
- UnknownSignatureAlgorithm SignatureAlgorithm = iota
- MD2WithRSA
- MD5WithRSA
- SHA1WithRSA
- SHA256WithRSA
- SHA384WithRSA
- SHA512WithRSA
- DSAWithSHA1
- DSAWithSHA256
- ECDSAWithSHA1
- ECDSAWithSHA256
- ECDSAWithSHA384
- ECDSAWithSHA512
- SHA256WithRSAPSS
- SHA384WithRSAPSS
- SHA512WithRSAPSS
- PureEd25519
- )
- // RFC 4055, 6. Basic object identifiers
- var oidpSpecified = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 9}
- // These are the default parameters for an RSAES-OAEP pubkey.
- // The current ASN.1 parser does not support non-integer defaults so
- // these currently do nothing.
- var (
- sha1Identifier = pkix.AlgorithmIdentifier{
- Algorithm: oidSHA1,
- Parameters: asn1.NullRawValue,
- }
- mgf1SHA1Identifier = pkix.AlgorithmIdentifier{
- Algorithm: oidMGF1,
- // RFC 4055, 2.1 sha1Identifier
- Parameters: asn1.RawValue{
- Class: asn1.ClassUniversal,
- Tag: asn1.TagSequence,
- IsCompound: false,
- Bytes: []byte{6, 5, 43, 14, 3, 2, 26, 5, 0},
- FullBytes: []byte{16, 9, 6, 5, 43, 14, 3, 2, 26, 5, 0}},
- }
- pSpecifiedEmptyIdentifier = pkix.AlgorithmIdentifier{
- Algorithm: oidpSpecified,
- // RFC 4055, 4.1 nullOctetString
- Parameters: asn1.RawValue{
- Class: asn1.ClassUniversal,
- Tag: asn1.TagOctetString,
- IsCompound: false,
- Bytes: []byte{},
- FullBytes: []byte{4, 0}},
- }
- )
- func (algo SignatureAlgorithm) isRSAPSS() bool {
- switch algo {
- case SHA256WithRSAPSS, SHA384WithRSAPSS, SHA512WithRSAPSS:
- return true
- default:
- return false
- }
- }
- func (algo SignatureAlgorithm) String() string {
- for _, details := range signatureAlgorithmDetails {
- if details.algo == algo {
- return details.name
- }
- }
- return strconv.Itoa(int(algo))
- }
- // PublicKeyAlgorithm indicates the algorithm used for a certificate's public key.
- type PublicKeyAlgorithm int
- // PublicKeyAlgorithm values:
- const (
- UnknownPublicKeyAlgorithm PublicKeyAlgorithm = iota
- RSA
- DSA
- ECDSA
- Ed25519
- RSAESOAEP
- )
- var publicKeyAlgoName = [...]string{
- RSA: "RSA",
- DSA: "DSA",
- ECDSA: "ECDSA",
- Ed25519: "Ed25519",
- RSAESOAEP: "RSAESOAEP",
- }
- func (algo PublicKeyAlgorithm) String() string {
- if 0 < algo && int(algo) < len(publicKeyAlgoName) {
- return publicKeyAlgoName[algo]
- }
- return strconv.Itoa(int(algo))
- }
- // OIDs for signature algorithms
- //
- // pkcs-1 OBJECT IDENTIFIER ::= {
- // iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) 1 }
- //
- //
- // RFC 3279 2.2.1 RSA Signature Algorithms
- //
- // md2WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 2 }
- //
- // md5WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 4 }
- //
- // sha-1WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 5 }
- //
- // dsaWithSha1 OBJECT IDENTIFIER ::= {
- // iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 3 }
- //
- // RFC 3279 2.2.3 ECDSA Signature Algorithm
- //
- // ecdsa-with-SHA1 OBJECT IDENTIFIER ::= {
- // iso(1) member-body(2) us(840) ansi-x962(10045)
- // signatures(4) ecdsa-with-SHA1(1)}
- //
- //
- // RFC 4055 5 PKCS #1 Version 1.5
- //
- // sha256WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 11 }
- //
- // sha384WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 12 }
- //
- // sha512WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 13 }
- //
- //
- // RFC 5758 3.1 DSA Signature Algorithms
- //
- // dsaWithSha256 OBJECT IDENTIFIER ::= {
- // joint-iso-ccitt(2) country(16) us(840) organization(1) gov(101)
- // csor(3) algorithms(4) id-dsa-with-sha2(3) 2}
- //
- // RFC 5758 3.2 ECDSA Signature Algorithm
- //
- // ecdsa-with-SHA256 OBJECT IDENTIFIER ::= { iso(1) member-body(2)
- // us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 2 }
- //
- // ecdsa-with-SHA384 OBJECT IDENTIFIER ::= { iso(1) member-body(2)
- // us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 3 }
- //
- // ecdsa-with-SHA512 OBJECT IDENTIFIER ::= { iso(1) member-body(2)
- // us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 4 }
- //
- //
- // RFC 8410 3 Curve25519 and Curve448 Algorithm Identifiers
- //
- // id-Ed25519 OBJECT IDENTIFIER ::= { 1 3 101 112 }
- var (
- oidSignatureMD2WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 2}
- oidSignatureMD5WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 4}
- oidSignatureSHA1WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 5}
- oidSignatureSHA256WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 11}
- oidSignatureSHA384WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 12}
- oidSignatureSHA512WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 13}
- oidSignatureRSAPSS = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 10}
- oidSignatureDSAWithSHA1 = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 3}
- oidSignatureDSAWithSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 3, 2}
- oidSignatureECDSAWithSHA1 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 1}
- oidSignatureECDSAWithSHA256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2}
- oidSignatureECDSAWithSHA384 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 3}
- oidSignatureECDSAWithSHA512 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 4}
- oidSignatureEd25519 = asn1.ObjectIdentifier{1, 3, 101, 112}
- oidSHA1 = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 26}
- oidSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1}
- oidSHA384 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 2}
- oidSHA512 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 3}
- oidMGF1 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 8}
- // oidISOSignatureSHA1WithRSA means the same as oidSignatureSHA1WithRSA
- // but it's specified by ISO. Microsoft's makecert.exe has been known
- // to produce certificates with this OID.
- oidISOSignatureSHA1WithRSA = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 29}
- )
- var signatureAlgorithmDetails = []struct {
- algo SignatureAlgorithm
- name string
- oid asn1.ObjectIdentifier
- pubKeyAlgo PublicKeyAlgorithm
- hash crypto.Hash
- }{
- {MD2WithRSA, "MD2-RSA", oidSignatureMD2WithRSA, RSA, crypto.Hash(0) /* no value for MD2 */},
- {MD5WithRSA, "MD5-RSA", oidSignatureMD5WithRSA, RSA, crypto.MD5},
- {SHA1WithRSA, "SHA1-RSA", oidSignatureSHA1WithRSA, RSA, crypto.SHA1},
- {SHA1WithRSA, "SHA1-RSA", oidISOSignatureSHA1WithRSA, RSA, crypto.SHA1},
- {SHA256WithRSA, "SHA256-RSA", oidSignatureSHA256WithRSA, RSA, crypto.SHA256},
- {SHA384WithRSA, "SHA384-RSA", oidSignatureSHA384WithRSA, RSA, crypto.SHA384},
- {SHA512WithRSA, "SHA512-RSA", oidSignatureSHA512WithRSA, RSA, crypto.SHA512},
- {SHA256WithRSAPSS, "SHA256-RSAPSS", oidSignatureRSAPSS, RSA, crypto.SHA256},
- {SHA384WithRSAPSS, "SHA384-RSAPSS", oidSignatureRSAPSS, RSA, crypto.SHA384},
- {SHA512WithRSAPSS, "SHA512-RSAPSS", oidSignatureRSAPSS, RSA, crypto.SHA512},
- {DSAWithSHA1, "DSA-SHA1", oidSignatureDSAWithSHA1, DSA, crypto.SHA1},
- {DSAWithSHA256, "DSA-SHA256", oidSignatureDSAWithSHA256, DSA, crypto.SHA256},
- {ECDSAWithSHA1, "ECDSA-SHA1", oidSignatureECDSAWithSHA1, ECDSA, crypto.SHA1},
- {ECDSAWithSHA256, "ECDSA-SHA256", oidSignatureECDSAWithSHA256, ECDSA, crypto.SHA256},
- {ECDSAWithSHA384, "ECDSA-SHA384", oidSignatureECDSAWithSHA384, ECDSA, crypto.SHA384},
- {ECDSAWithSHA512, "ECDSA-SHA512", oidSignatureECDSAWithSHA512, ECDSA, crypto.SHA512},
- {PureEd25519, "Ed25519", oidSignatureEd25519, Ed25519, crypto.Hash(0) /* no pre-hashing */},
- }
- // pssParameters reflects the parameters in an AlgorithmIdentifier that
- // specifies RSA PSS. See RFC 3447, Appendix A.2.3.
- type pssParameters struct {
- // The following three fields are not marked as
- // optional because the default values specify SHA-1,
- // which is no longer suitable for use in signatures.
- Hash pkix.AlgorithmIdentifier `asn1:"explicit,tag:0"`
- MGF pkix.AlgorithmIdentifier `asn1:"explicit,tag:1"`
- SaltLength int `asn1:"explicit,tag:2"`
- TrailerField int `asn1:"optional,explicit,tag:3,default:1"`
- }
- // rsaPSSParameters returns an asn1.RawValue suitable for use as the Parameters
- // in an AlgorithmIdentifier that specifies RSA PSS.
- func rsaPSSParameters(hashFunc crypto.Hash) asn1.RawValue {
- var hashOID asn1.ObjectIdentifier
- switch hashFunc {
- case crypto.SHA256:
- hashOID = oidSHA256
- case crypto.SHA384:
- hashOID = oidSHA384
- case crypto.SHA512:
- hashOID = oidSHA512
- }
- params := pssParameters{
- Hash: pkix.AlgorithmIdentifier{
- Algorithm: hashOID,
- Parameters: asn1.NullRawValue,
- },
- MGF: pkix.AlgorithmIdentifier{
- Algorithm: oidMGF1,
- },
- SaltLength: hashFunc.Size(),
- TrailerField: 1,
- }
- mgf1Params := pkix.AlgorithmIdentifier{
- Algorithm: hashOID,
- Parameters: asn1.NullRawValue,
- }
- var err error
- params.MGF.Parameters.FullBytes, err = asn1.Marshal(mgf1Params)
- if err != nil {
- panic(err)
- }
- serialized, err := asn1.Marshal(params)
- if err != nil {
- panic(err)
- }
- return asn1.RawValue{FullBytes: serialized}
- }
- // SignatureAlgorithmFromAI converts an PKIX algorithm identifier to the
- // equivalent local constant.
- func SignatureAlgorithmFromAI(ai pkix.AlgorithmIdentifier) SignatureAlgorithm {
- if ai.Algorithm.Equal(oidSignatureEd25519) {
- // RFC 8410, Section 3
- // > For all of the OIDs, the parameters MUST be absent.
- if len(ai.Parameters.FullBytes) != 0 {
- return UnknownSignatureAlgorithm
- }
- }
- if !ai.Algorithm.Equal(oidSignatureRSAPSS) {
- for _, details := range signatureAlgorithmDetails {
- if ai.Algorithm.Equal(details.oid) {
- return details.algo
- }
- }
- return UnknownSignatureAlgorithm
- }
- // RSA PSS is special because it encodes important parameters
- // in the Parameters.
- var params pssParameters
- if _, err := asn1.Unmarshal(ai.Parameters.FullBytes, ¶ms); err != nil {
- return UnknownSignatureAlgorithm
- }
- var mgf1HashFunc pkix.AlgorithmIdentifier
- if _, err := asn1.Unmarshal(params.MGF.Parameters.FullBytes, &mgf1HashFunc); err != nil {
- return UnknownSignatureAlgorithm
- }
- // PSS is greatly overburdened with options. This code forces them into
- // three buckets by requiring that the MGF1 hash function always match the
- // message hash function (as recommended in RFC 3447, Section 8.1), that the
- // salt length matches the hash length, and that the trailer field has the
- // default value.
- if (len(params.Hash.Parameters.FullBytes) != 0 && !bytes.Equal(params.Hash.Parameters.FullBytes, asn1.NullBytes)) ||
- !params.MGF.Algorithm.Equal(oidMGF1) ||
- !mgf1HashFunc.Algorithm.Equal(params.Hash.Algorithm) ||
- (len(mgf1HashFunc.Parameters.FullBytes) != 0 && !bytes.Equal(mgf1HashFunc.Parameters.FullBytes, asn1.NullBytes)) ||
- params.TrailerField != 1 {
- return UnknownSignatureAlgorithm
- }
- switch {
- case params.Hash.Algorithm.Equal(oidSHA256) && params.SaltLength == 32:
- return SHA256WithRSAPSS
- case params.Hash.Algorithm.Equal(oidSHA384) && params.SaltLength == 48:
- return SHA384WithRSAPSS
- case params.Hash.Algorithm.Equal(oidSHA512) && params.SaltLength == 64:
- return SHA512WithRSAPSS
- }
- return UnknownSignatureAlgorithm
- }
- // RFC 3279, 2.3 Public Key Algorithms
- //
- // pkcs-1 OBJECT IDENTIFIER ::== { iso(1) member-body(2) us(840)
- //
- // rsadsi(113549) pkcs(1) 1 }
- //
- // rsaEncryption OBJECT IDENTIFIER ::== { pkcs1-1 1 }
- //
- // id-dsa OBJECT IDENTIFIER ::== { iso(1) member-body(2) us(840)
- //
- // x9-57(10040) x9cm(4) 1 }
- //
- // # RFC 5480, 2.1.1 Unrestricted Algorithm Identifier and Parameters
- //
- // id-ecPublicKey OBJECT IDENTIFIER ::= {
- // iso(1) member-body(2) us(840) ansi-X9-62(10045) keyType(2) 1 }
- var (
- OIDPublicKeyRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1}
- OIDPublicKeyRSAESOAEP = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 7}
- OIDPublicKeyDSA = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 1}
- OIDPublicKeyECDSA = asn1.ObjectIdentifier{1, 2, 840, 10045, 2, 1}
- OIDPublicKeyRSAObsolete = asn1.ObjectIdentifier{2, 5, 8, 1, 1}
- OIDPublicKeyEd25519 = oidSignatureEd25519
- )
- func getPublicKeyAlgorithmFromOID(oid asn1.ObjectIdentifier) PublicKeyAlgorithm {
- switch {
- case oid.Equal(OIDPublicKeyRSA):
- return RSA
- case oid.Equal(OIDPublicKeyDSA):
- return DSA
- case oid.Equal(OIDPublicKeyECDSA):
- return ECDSA
- case oid.Equal(OIDPublicKeyRSAESOAEP):
- return RSAESOAEP
- case oid.Equal(OIDPublicKeyEd25519):
- return Ed25519
- }
- return UnknownPublicKeyAlgorithm
- }
- // RFC 5480, 2.1.1.1. Named Curve
- //
- // secp224r1 OBJECT IDENTIFIER ::= {
- // iso(1) identified-organization(3) certicom(132) curve(0) 33 }
- //
- // secp256r1 OBJECT IDENTIFIER ::= {
- // iso(1) member-body(2) us(840) ansi-X9-62(10045) curves(3)
- // prime(1) 7 }
- //
- // secp384r1 OBJECT IDENTIFIER ::= {
- // iso(1) identified-organization(3) certicom(132) curve(0) 34 }
- //
- // secp521r1 OBJECT IDENTIFIER ::= {
- // iso(1) identified-organization(3) certicom(132) curve(0) 35 }
- //
- // secp192r1 OBJECT IDENTIFIER ::= {
- // iso(1) member-body(2) us(840) ansi-X9-62(10045) curves(3)
- // prime(1) 1 }
- //
- // NB: secp256r1 is equivalent to prime256v1,
- // secp192r1 is equivalent to ansix9p192r and prime192v1
- var (
- OIDNamedCurveP224 = asn1.ObjectIdentifier{1, 3, 132, 0, 33}
- OIDNamedCurveP256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 7}
- OIDNamedCurveP384 = asn1.ObjectIdentifier{1, 3, 132, 0, 34}
- OIDNamedCurveP521 = asn1.ObjectIdentifier{1, 3, 132, 0, 35}
- OIDNamedCurveP192 = asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 1}
- )
- func namedCurveFromOID(oid asn1.ObjectIdentifier, nfe *NonFatalErrors) elliptic.Curve {
- switch {
- case oid.Equal(OIDNamedCurveP224):
- return elliptic.P224()
- case oid.Equal(OIDNamedCurveP256):
- return elliptic.P256()
- case oid.Equal(OIDNamedCurveP384):
- return elliptic.P384()
- case oid.Equal(OIDNamedCurveP521):
- return elliptic.P521()
- case oid.Equal(OIDNamedCurveP192):
- nfe.AddError(errors.New("insecure curve (secp192r1) specified"))
- return secp192r1()
- }
- return nil
- }
- // OIDFromNamedCurve returns the OID used to specify the use of the given
- // elliptic curve.
- func OIDFromNamedCurve(curve elliptic.Curve) (asn1.ObjectIdentifier, bool) {
- switch curve {
- case elliptic.P224():
- return OIDNamedCurveP224, true
- case elliptic.P256():
- return OIDNamedCurveP256, true
- case elliptic.P384():
- return OIDNamedCurveP384, true
- case elliptic.P521():
- return OIDNamedCurveP521, true
- case secp192r1():
- return OIDNamedCurveP192, true
- }
- return nil, false
- }
- // KeyUsage represents the set of actions that are valid for a given key. It's
- // a bitmap of the KeyUsage* constants.
- type KeyUsage int
- // KeyUsage values:
- const (
- KeyUsageDigitalSignature KeyUsage = 1 << iota
- KeyUsageContentCommitment
- KeyUsageKeyEncipherment
- KeyUsageDataEncipherment
- KeyUsageKeyAgreement
- KeyUsageCertSign
- KeyUsageCRLSign
- KeyUsageEncipherOnly
- KeyUsageDecipherOnly
- )
- // RFC 5280, 4.2.1.12 Extended Key Usage
- //
- // anyExtendedKeyUsage OBJECT IDENTIFIER ::= { id-ce-extKeyUsage 0 }
- //
- // id-kp OBJECT IDENTIFIER ::= { id-pkix 3 }
- //
- // id-kp-serverAuth OBJECT IDENTIFIER ::= { id-kp 1 }
- // id-kp-clientAuth OBJECT IDENTIFIER ::= { id-kp 2 }
- // id-kp-codeSigning OBJECT IDENTIFIER ::= { id-kp 3 }
- // id-kp-emailProtection OBJECT IDENTIFIER ::= { id-kp 4 }
- // id-kp-timeStamping OBJECT IDENTIFIER ::= { id-kp 8 }
- // id-kp-OCSPSigning OBJECT IDENTIFIER ::= { id-kp 9 }
- var (
- oidExtKeyUsageAny = asn1.ObjectIdentifier{2, 5, 29, 37, 0}
- oidExtKeyUsageServerAuth = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 1}
- oidExtKeyUsageClientAuth = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 2}
- oidExtKeyUsageCodeSigning = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 3}
- oidExtKeyUsageEmailProtection = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 4}
- oidExtKeyUsageIPSECEndSystem = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 5}
- oidExtKeyUsageIPSECTunnel = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 6}
- oidExtKeyUsageIPSECUser = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 7}
- oidExtKeyUsageTimeStamping = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 8}
- oidExtKeyUsageOCSPSigning = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 9}
- oidExtKeyUsageMicrosoftServerGatedCrypto = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 10, 3, 3}
- oidExtKeyUsageNetscapeServerGatedCrypto = asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 4, 1}
- oidExtKeyUsageMicrosoftCommercialCodeSigning = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 2, 1, 22}
- oidExtKeyUsageMicrosoftKernelCodeSigning = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 61, 1, 1}
- // RFC 6962 s3.1
- oidExtKeyUsageCertificateTransparency = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 11129, 2, 4, 4}
- )
- // ExtKeyUsage represents an extended set of actions that are valid for a given key.
- // Each of the ExtKeyUsage* constants define a unique action.
- type ExtKeyUsage int
- // ExtKeyUsage values:
- const (
- ExtKeyUsageAny ExtKeyUsage = iota
- ExtKeyUsageServerAuth
- ExtKeyUsageClientAuth
- ExtKeyUsageCodeSigning
- ExtKeyUsageEmailProtection
- ExtKeyUsageIPSECEndSystem
- ExtKeyUsageIPSECTunnel
- ExtKeyUsageIPSECUser
- ExtKeyUsageTimeStamping
- ExtKeyUsageOCSPSigning
- ExtKeyUsageMicrosoftServerGatedCrypto
- ExtKeyUsageNetscapeServerGatedCrypto
- ExtKeyUsageMicrosoftCommercialCodeSigning
- ExtKeyUsageMicrosoftKernelCodeSigning
- ExtKeyUsageCertificateTransparency
- )
- // extKeyUsageOIDs contains the mapping between an ExtKeyUsage and its OID.
- var extKeyUsageOIDs = []struct {
- extKeyUsage ExtKeyUsage
- oid asn1.ObjectIdentifier
- }{
- {ExtKeyUsageAny, oidExtKeyUsageAny},
- {ExtKeyUsageServerAuth, oidExtKeyUsageServerAuth},
- {ExtKeyUsageClientAuth, oidExtKeyUsageClientAuth},
- {ExtKeyUsageCodeSigning, oidExtKeyUsageCodeSigning},
- {ExtKeyUsageEmailProtection, oidExtKeyUsageEmailProtection},
- {ExtKeyUsageIPSECEndSystem, oidExtKeyUsageIPSECEndSystem},
- {ExtKeyUsageIPSECTunnel, oidExtKeyUsageIPSECTunnel},
- {ExtKeyUsageIPSECUser, oidExtKeyUsageIPSECUser},
- {ExtKeyUsageTimeStamping, oidExtKeyUsageTimeStamping},
- {ExtKeyUsageOCSPSigning, oidExtKeyUsageOCSPSigning},
- {ExtKeyUsageMicrosoftServerGatedCrypto, oidExtKeyUsageMicrosoftServerGatedCrypto},
- {ExtKeyUsageNetscapeServerGatedCrypto, oidExtKeyUsageNetscapeServerGatedCrypto},
- {ExtKeyUsageMicrosoftCommercialCodeSigning, oidExtKeyUsageMicrosoftCommercialCodeSigning},
- {ExtKeyUsageMicrosoftKernelCodeSigning, oidExtKeyUsageMicrosoftKernelCodeSigning},
- {ExtKeyUsageCertificateTransparency, oidExtKeyUsageCertificateTransparency},
- }
- func extKeyUsageFromOID(oid asn1.ObjectIdentifier) (eku ExtKeyUsage, ok bool) {
- for _, pair := range extKeyUsageOIDs {
- if oid.Equal(pair.oid) {
- return pair.extKeyUsage, true
- }
- }
- return
- }
- func oidFromExtKeyUsage(eku ExtKeyUsage) (oid asn1.ObjectIdentifier, ok bool) {
- for _, pair := range extKeyUsageOIDs {
- if eku == pair.extKeyUsage {
- return pair.oid, true
- }
- }
- return
- }
- // SerializedSCT represents a single TLS-encoded signed certificate timestamp, from RFC6962 s3.3.
- type SerializedSCT struct {
- Val []byte `tls:"minlen:1,maxlen:65535"`
- }
- // SignedCertificateTimestampList is a list of signed certificate timestamps, from RFC6962 s3.3.
- type SignedCertificateTimestampList struct {
- SCTList []SerializedSCT `tls:"minlen:1,maxlen:65335"`
- }
- // A Certificate represents an X.509 certificate.
- type Certificate struct {
- Raw []byte // Complete ASN.1 DER content (certificate, signature algorithm and signature).
- RawTBSCertificate []byte // Certificate part of raw ASN.1 DER content.
- RawSubjectPublicKeyInfo []byte // DER encoded SubjectPublicKeyInfo.
- RawSubject []byte // DER encoded Subject
- RawIssuer []byte // DER encoded Issuer
- Signature []byte
- SignatureAlgorithm SignatureAlgorithm
- PublicKeyAlgorithm PublicKeyAlgorithm
- PublicKey interface{}
- Version int
- SerialNumber *big.Int
- Issuer pkix.Name
- Subject pkix.Name
- NotBefore, NotAfter time.Time // Validity bounds.
- KeyUsage KeyUsage
- // Extensions contains raw X.509 extensions. When parsing certificates,
- // this can be used to extract non-critical extensions that are not
- // parsed by this package. When marshaling certificates, the Extensions
- // field is ignored, see ExtraExtensions.
- Extensions []pkix.Extension
- // ExtraExtensions contains extensions to be copied, raw, into any
- // marshaled certificates. Values override any extensions that would
- // otherwise be produced based on the other fields. The ExtraExtensions
- // field is not populated when parsing certificates, see Extensions.
- ExtraExtensions []pkix.Extension
- // UnhandledCriticalExtensions contains a list of extension IDs that
- // were not (fully) processed when parsing. Verify will fail if this
- // slice is non-empty, unless verification is delegated to an OS
- // library which understands all the critical extensions.
- //
- // Users can access these extensions using Extensions and can remove
- // elements from this slice if they believe that they have been
- // handled.
- UnhandledCriticalExtensions []asn1.ObjectIdentifier
- ExtKeyUsage []ExtKeyUsage // Sequence of extended key usages.
- UnknownExtKeyUsage []asn1.ObjectIdentifier // Encountered extended key usages unknown to this package.
- // BasicConstraintsValid indicates whether IsCA, MaxPathLen,
- // and MaxPathLenZero are valid.
- BasicConstraintsValid bool
- IsCA bool
- // MaxPathLen and MaxPathLenZero indicate the presence and
- // value of the BasicConstraints' "pathLenConstraint".
- //
- // When parsing a certificate, a positive non-zero MaxPathLen
- // means that the field was specified, -1 means it was unset,
- // and MaxPathLenZero being true mean that the field was
- // explicitly set to zero. The case of MaxPathLen==0 with MaxPathLenZero==false
- // should be treated equivalent to -1 (unset).
- //
- // When generating a certificate, an unset pathLenConstraint
- // can be requested with either MaxPathLen == -1 or using the
- // zero value for both MaxPathLen and MaxPathLenZero.
- MaxPathLen int
- // MaxPathLenZero indicates that BasicConstraintsValid==true
- // and MaxPathLen==0 should be interpreted as an actual
- // maximum path length of zero. Otherwise, that combination is
- // interpreted as MaxPathLen not being set.
- MaxPathLenZero bool
- SubjectKeyId []byte
- AuthorityKeyId []byte
- // RFC 5280, 4.2.2.1 (Authority Information Access)
- OCSPServer []string
- IssuingCertificateURL []string
- // Subject Information Access
- SubjectTimestamps []string
- SubjectCARepositories []string
- // Subject Alternate Name values. (Note that these values may not be valid
- // if invalid values were contained within a parsed certificate. For
- // example, an element of DNSNames may not be a valid DNS domain name.)
- DNSNames []string
- EmailAddresses []string
- IPAddresses []net.IP
- URIs []*url.URL
- // Name constraints
- PermittedDNSDomainsCritical bool // if true then the name constraints are marked critical.
- PermittedDNSDomains []string
- ExcludedDNSDomains []string
- PermittedIPRanges []*net.IPNet
- ExcludedIPRanges []*net.IPNet
- PermittedEmailAddresses []string
- ExcludedEmailAddresses []string
- PermittedURIDomains []string
- ExcludedURIDomains []string
- // CRL Distribution Points
- CRLDistributionPoints []string
- PolicyIdentifiers []asn1.ObjectIdentifier
- RPKIAddressRanges []*IPAddressFamilyBlocks
- RPKIASNumbers, RPKIRoutingDomainIDs *ASIdentifiers
- // Certificate Transparency SCT extension contents; this is a TLS-encoded
- // SignedCertificateTimestampList (RFC 6962 s3.3).
- RawSCT []byte
- SCTList SignedCertificateTimestampList
- }
- // ErrUnsupportedAlgorithm results from attempting to perform an operation that
- // involves algorithms that are not currently implemented.
- var ErrUnsupportedAlgorithm = errors.New("x509: cannot verify signature: algorithm unimplemented")
- // InsecureAlgorithmError results when the signature algorithm for a certificate
- // is known to be insecure.
- type InsecureAlgorithmError SignatureAlgorithm
- func (e InsecureAlgorithmError) Error() string {
- return fmt.Sprintf("x509: cannot verify signature: insecure algorithm %v", SignatureAlgorithm(e))
- }
- // ConstraintViolationError results when a requested usage is not permitted by
- // a certificate. For example: checking a signature when the public key isn't a
- // certificate signing key.
- type ConstraintViolationError struct{}
- func (ConstraintViolationError) Error() string {
- return "x509: invalid signature: parent certificate cannot sign this kind of certificate"
- }
- // Equal indicates whether two Certificate objects are equal (by comparing their
- // DER-encoded values).
- func (c *Certificate) Equal(other *Certificate) bool {
- if c == nil || other == nil {
- return c == other
- }
- return bytes.Equal(c.Raw, other.Raw)
- }
- // IsPrecertificate checks whether the certificate is a precertificate, by
- // checking for the presence of the CT Poison extension.
- func (c *Certificate) IsPrecertificate() bool {
- if c == nil {
- return false
- }
- for _, ext := range c.Extensions {
- if ext.Id.Equal(OIDExtensionCTPoison) {
- return true
- }
- }
- return false
- }
- func (c *Certificate) hasSANExtension() bool {
- return oidInExtensions(OIDExtensionSubjectAltName, c.Extensions)
- }
- // Entrust have a broken root certificate (CN=Entrust.net Certification
- // Authority (2048)) which isn't marked as a CA certificate and is thus invalid
- // according to PKIX.
- // We recognise this certificate by its SubjectPublicKeyInfo and exempt it
- // from the Basic Constraints requirement.
- // See http://www.entrust.net/knowledge-base/technote.cfm?tn=7869
- //
- // TODO(agl): remove this hack once their reissued root is sufficiently
- // widespread.
- var entrustBrokenSPKI = []byte{
- 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09,
- 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
- 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
- 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01,
- 0x00, 0x97, 0xa3, 0x2d, 0x3c, 0x9e, 0xde, 0x05,
- 0xda, 0x13, 0xc2, 0x11, 0x8d, 0x9d, 0x8e, 0xe3,
- 0x7f, 0xc7, 0x4b, 0x7e, 0x5a, 0x9f, 0xb3, 0xff,
- 0x62, 0xab, 0x73, 0xc8, 0x28, 0x6b, 0xba, 0x10,
- 0x64, 0x82, 0x87, 0x13, 0xcd, 0x57, 0x18, 0xff,
- 0x28, 0xce, 0xc0, 0xe6, 0x0e, 0x06, 0x91, 0x50,
- 0x29, 0x83, 0xd1, 0xf2, 0xc3, 0x2a, 0xdb, 0xd8,
- 0xdb, 0x4e, 0x04, 0xcc, 0x00, 0xeb, 0x8b, 0xb6,
- 0x96, 0xdc, 0xbc, 0xaa, 0xfa, 0x52, 0x77, 0x04,
- 0xc1, 0xdb, 0x19, 0xe4, 0xae, 0x9c, 0xfd, 0x3c,
- 0x8b, 0x03, 0xef, 0x4d, 0xbc, 0x1a, 0x03, 0x65,
- 0xf9, 0xc1, 0xb1, 0x3f, 0x72, 0x86, 0xf2, 0x38,
- 0xaa, 0x19, 0xae, 0x10, 0x88, 0x78, 0x28, 0xda,
- 0x75, 0xc3, 0x3d, 0x02, 0x82, 0x02, 0x9c, 0xb9,
- 0xc1, 0x65, 0x77, 0x76, 0x24, 0x4c, 0x98, 0xf7,
- 0x6d, 0x31, 0x38, 0xfb, 0xdb, 0xfe, 0xdb, 0x37,
- 0x02, 0x76, 0xa1, 0x18, 0x97, 0xa6, 0xcc, 0xde,
- 0x20, 0x09, 0x49, 0x36, 0x24, 0x69, 0x42, 0xf6,
- 0xe4, 0x37, 0x62, 0xf1, 0x59, 0x6d, 0xa9, 0x3c,
- 0xed, 0x34, 0x9c, 0xa3, 0x8e, 0xdb, 0xdc, 0x3a,
- 0xd7, 0xf7, 0x0a, 0x6f, 0xef, 0x2e, 0xd8, 0xd5,
- 0x93, 0x5a, 0x7a, 0xed, 0x08, 0x49, 0x68, 0xe2,
- 0x41, 0xe3, 0x5a, 0x90, 0xc1, 0x86, 0x55, 0xfc,
- 0x51, 0x43, 0x9d, 0xe0, 0xb2, 0xc4, 0x67, 0xb4,
- 0xcb, 0x32, 0x31, 0x25, 0xf0, 0x54, 0x9f, 0x4b,
- 0xd1, 0x6f, 0xdb, 0xd4, 0xdd, 0xfc, 0xaf, 0x5e,
- 0x6c, 0x78, 0x90, 0x95, 0xde, 0xca, 0x3a, 0x48,
- 0xb9, 0x79, 0x3c, 0x9b, 0x19, 0xd6, 0x75, 0x05,
- 0xa0, 0xf9, 0x88, 0xd7, 0xc1, 0xe8, 0xa5, 0x09,
- 0xe4, 0x1a, 0x15, 0xdc, 0x87, 0x23, 0xaa, 0xb2,
- 0x75, 0x8c, 0x63, 0x25, 0x87, 0xd8, 0xf8, 0x3d,
- 0xa6, 0xc2, 0xcc, 0x66, 0xff, 0xa5, 0x66, 0x68,
- 0x55, 0x02, 0x03, 0x01, 0x00, 0x01,
- }
- // CheckSignatureFrom verifies that the signature on c is a valid signature
- // from parent.
- func (c *Certificate) CheckSignatureFrom(parent *Certificate) error {
- // RFC 5280, 4.2.1.9:
- // "If the basic constraints extension is not present in a version 3
- // certificate, or the extension is present but the cA boolean is not
- // asserted, then the certified public key MUST NOT be used to verify
- // certificate signatures."
- // (except for Entrust, see comment above entrustBrokenSPKI)
- if (parent.Version == 3 && !parent.BasicConstraintsValid ||
- parent.BasicConstraintsValid && !parent.IsCA) &&
- !bytes.Equal(c.RawSubjectPublicKeyInfo, entrustBrokenSPKI) {
- return ConstraintViolationError{}
- }
- if parent.KeyUsage != 0 && parent.KeyUsage&KeyUsageCertSign == 0 {
- return ConstraintViolationError{}
- }
- if parent.PublicKeyAlgorithm == UnknownPublicKeyAlgorithm {
- return ErrUnsupportedAlgorithm
- }
- // TODO(agl): don't ignore the path length constraint.
- return parent.CheckSignature(c.SignatureAlgorithm, c.RawTBSCertificate, c.Signature)
- }
- // CheckSignature verifies that signature is a valid signature over signed from
- // c's public key.
- func (c *Certificate) CheckSignature(algo SignatureAlgorithm, signed, signature []byte) error {
- return checkSignature(algo, signed, signature, c.PublicKey)
- }
- func (c *Certificate) hasNameConstraints() bool {
- return oidInExtensions(OIDExtensionNameConstraints, c.Extensions)
- }
- func (c *Certificate) getSANExtension() []byte {
- for _, e := range c.Extensions {
- if e.Id.Equal(OIDExtensionSubjectAltName) {
- return e.Value
- }
- }
- return nil
- }
- func signaturePublicKeyAlgoMismatchError(expectedPubKeyAlgo PublicKeyAlgorithm, pubKey interface{}) error {
- return fmt.Errorf("x509: signature algorithm specifies an %s public key, but have public key of type %T", expectedPubKeyAlgo.String(), pubKey)
- }
- // CheckSignature verifies that signature is a valid signature over signed from
- // a crypto.PublicKey.
- func checkSignature(algo SignatureAlgorithm, signed, signature []byte, publicKey crypto.PublicKey) (err error) {
- var hashType crypto.Hash
- var pubKeyAlgo PublicKeyAlgorithm
- for _, details := range signatureAlgorithmDetails {
- if details.algo == algo {
- hashType = details.hash
- pubKeyAlgo = details.pubKeyAlgo
- }
- }
- switch hashType {
- case crypto.Hash(0):
- if pubKeyAlgo != Ed25519 {
- return ErrUnsupportedAlgorithm
- }
- case crypto.MD5:
- return InsecureAlgorithmError(algo)
- default:
- if !hashType.Available() {
- return ErrUnsupportedAlgorithm
- }
- h := hashType.New()
- h.Write(signed)
- signed = h.Sum(nil)
- }
- switch pub := publicKey.(type) {
- case *rsa.PublicKey:
- if pubKeyAlgo != RSA {
- return signaturePublicKeyAlgoMismatchError(pubKeyAlgo, pub)
- }
- if algo.isRSAPSS() {
- return rsa.VerifyPSS(pub, hashType, signed, signature, &rsa.PSSOptions{SaltLength: rsa.PSSSaltLengthEqualsHash})
- } else {
- return rsa.VerifyPKCS1v15(pub, hashType, signed, signature)
- }
- case *dsa.PublicKey:
- if pubKeyAlgo != DSA {
- return signaturePublicKeyAlgoMismatchError(pubKeyAlgo, pub)
- }
- dsaSig := new(dsaSignature)
- if rest, err := asn1.Unmarshal(signature, dsaSig); err != nil {
- return err
- } else if len(rest) != 0 {
- return errors.New("x509: trailing data after DSA signature")
- }
- if dsaSig.R.Sign() <= 0 || dsaSig.S.Sign() <= 0 {
- return errors.New("x509: DSA signature contained zero or negative values")
- }
- // According to FIPS 186-3, section 4.6, the hash must be truncated if it is longer
- // than the key length, but crypto/dsa doesn't do it automatically.
- if maxHashLen := pub.Q.BitLen() / 8; maxHashLen < len(signed) {
- signed = signed[:maxHashLen]
- }
- if !dsa.Verify(pub, signed, dsaSig.R, dsaSig.S) {
- return errors.New("x509: DSA verification failure")
- }
- return
- case *ecdsa.PublicKey:
- if pubKeyAlgo != ECDSA {
- return signaturePublicKeyAlgoMismatchError(pubKeyAlgo, pub)
- }
- ecdsaSig := new(ecdsaSignature)
- if rest, err := asn1.Unmarshal(signature, ecdsaSig); err != nil {
- return err
- } else if len(rest) != 0 {
- return errors.New("x509: trailing data after ECDSA signature")
- }
- if ecdsaSig.R.Sign() <= 0 || ecdsaSig.S.Sign() <= 0 {
- return errors.New("x509: ECDSA signature contained zero or negative values")
- }
- if !ecdsa.Verify(pub, signed, ecdsaSig.R, ecdsaSig.S) {
- return errors.New("x509: ECDSA verification failure")
- }
- return
- case ed25519.PublicKey:
- if pubKeyAlgo != Ed25519 {
- return signaturePublicKeyAlgoMismatchError(pubKeyAlgo, pub)
- }
- if !ed25519.Verify(pub, signed, signature) {
- return errors.New("x509: Ed25519 verification failure")
- }
- return
- }
- return ErrUnsupportedAlgorithm
- }
- // CheckCRLSignature checks that the signature in crl is from c.
- func (c *Certificate) CheckCRLSignature(crl *pkix.CertificateList) error {
- algo := SignatureAlgorithmFromAI(crl.SignatureAlgorithm)
- return c.CheckSignature(algo, crl.TBSCertList.Raw, crl.SignatureValue.RightAlign())
- }
- // UnhandledCriticalExtension results when the certificate contains an extension
- // that is marked as critical but which is not handled by this library.
- type UnhandledCriticalExtension struct {
- ID asn1.ObjectIdentifier
- }
- func (h UnhandledCriticalExtension) Error() string {
- return fmt.Sprintf("x509: unhandled critical extension (%v)", h.ID)
- }
- // removeExtension takes a DER-encoded TBSCertificate, removes the extension
- // specified by oid (preserving the order of other extensions), and returns the
- // result still as a DER-encoded TBSCertificate. This function will fail if
- // there is not exactly 1 extension of the type specified by the oid present.
- func removeExtension(tbsData []byte, oid asn1.ObjectIdentifier) ([]byte, error) {
- var tbs tbsCertificate
- rest, err := asn1.Unmarshal(tbsData, &tbs)
- if err != nil {
- return nil, fmt.Errorf("failed to parse TBSCertificate: %v", err)
- } else if rLen := len(rest); rLen > 0 {
- return nil, fmt.Errorf("trailing data (%d bytes) after TBSCertificate", rLen)
- }
- extAt := -1
- for i, ext := range tbs.Extensions {
- if ext.Id.Equal(oid) {
- if extAt != -1 {
- return nil, errors.New("multiple extensions of specified type present")
- }
- extAt = i
- }
- }
- if extAt == -1 {
- return nil, errors.New("no extension of specified type present")
- }
- tbs.Extensions = append(tbs.Extensions[:extAt], tbs.Extensions[extAt+1:]...)
- // Clear out the asn1.RawContent so the re-marshal operation sees the
- // updated structure (rather than just copying the out-of-date DER data).
- tbs.Raw = nil
- data, err := asn1.Marshal(tbs)
- if err != nil {
- return nil, fmt.Errorf("failed to re-marshal TBSCertificate: %v", err)
- }
- return data, nil
- }
- // RemoveSCTList takes a DER-encoded TBSCertificate and removes the CT SCT
- // extension that contains the SCT list (preserving the order of other
- // extensions), and returns the result still as a DER-encoded TBSCertificate.
- // This function will fail if there is not exactly 1 CT SCT extension present.
- func RemoveSCTList(tbsData []byte) ([]byte, error) {
- return removeExtension(tbsData, OIDExtensionCTSCT)
- }
- // RemoveCTPoison takes a DER-encoded TBSCertificate and removes the CT poison
- // extension (preserving the order of other extensions), and returns the result
- // still as a DER-encoded TBSCertificate. This function will fail if there is
- // not exactly 1 CT poison extension present.
- func RemoveCTPoison(tbsData []byte) ([]byte, error) {
- return BuildPrecertTBS(tbsData, nil)
- }
- // BuildPrecertTBS builds a Certificate Transparency pre-certificate (RFC 6962
- // s3.1) from the given DER-encoded TBSCertificate, returning a DER-encoded
- // TBSCertificate.
- //
- // This function removes the CT poison extension (there must be exactly 1 of
- // these), preserving the order of other extensions.
- //
- // If preIssuer is provided, this should be a special intermediate certificate
- // that was used to sign the precert (indicated by having the special
- // CertificateTransparency extended key usage). In this case, the issuance
- // information of the pre-cert is updated to reflect the next issuer in the
- // chain, i.e. the issuer of this special intermediate:
- // - The precert's Issuer is changed to the Issuer of the intermediate
- // - The precert's AuthorityKeyId is changed to the AuthorityKeyId of the
- // intermediate.
- func BuildPrecertTBS(tbsData []byte, preIssuer *Certificate) ([]byte, error) {
- data, err := removeExtension(tbsData, OIDExtensionCTPoison)
- if err != nil {
- return nil, err
- }
- var tbs tbsCertificate
- rest, err := asn1.Unmarshal(data, &tbs)
- if err != nil {
- return nil, fmt.Errorf("failed to parse TBSCertificate: %v", err)
- } else if rLen := len(rest); rLen > 0 {
- return nil, fmt.Errorf("trailing data (%d bytes) after TBSCertificate", rLen)
- }
- if preIssuer != nil {
- // Update the precert's Issuer field. Use the RawIssuer rather than the
- // parsed Issuer to avoid any chance of ASN.1 differences (e.g. switching
- // from UTF8String to PrintableString).
- tbs.Issuer.FullBytes = preIssuer.RawIssuer
- // Also need to update the cert's AuthorityKeyID extension
- // to that of the preIssuer.
- var issuerKeyID []byte
- for _, ext := range preIssuer.Extensions {
- if ext.Id.Equal(OIDExtensionAuthorityKeyId) {
- issuerKeyID = ext.Value
- break
- }
- }
- // Check the preIssuer has the CT EKU.
- seenCTEKU := false
- for _, eku := range preIssuer.ExtKeyUsage {
- if eku == ExtKeyUsageCertificateTransparency {
- seenCTEKU = true
- break
- }
- }
- if !seenCTEKU {
- return nil, fmt.Errorf("issuer does not have CertificateTransparency extended key usage")
- }
- keyAt := -1
- for i, ext := range tbs.Extensions {
- if ext.Id.Equal(OIDExtensionAuthorityKeyId) {
- keyAt = i
- break
- }
- }
- if keyAt >= 0 {
- // PreCert has an auth-key-id; replace it with the value from the preIssuer
- if issuerKeyID != nil {
- tbs.Extensions[keyAt].Value = issuerKeyID
- } else {
- tbs.Extensions = append(tbs.Extensions[:keyAt], tbs.Extensions[keyAt+1:]...)
- }
- } else if issuerKeyID != nil {
- // PreCert did not have an auth-key-id, but the preIssuer does, so add it at the end.
- authKeyIDExt := pkix.Extension{
- Id: OIDExtensionAuthorityKeyId,
- Critical: false,
- Value: issuerKeyID,
- }
- tbs.Extensions = append(tbs.Extensions, authKeyIDExt)
- }
- // Clear out the asn1.RawContent so the re-marshal operation sees the
- // updated structure (rather than just copying the out-of-date DER data).
- tbs.Raw = nil
- }
- data, err = asn1.Marshal(tbs)
- if err != nil {
- return nil, fmt.Errorf("failed to re-marshal TBSCertificate: %v", err)
- }
- return data, nil
- }
- type basicConstraints struct {
- IsCA bool `asn1:"optional"`
- MaxPathLen int `asn1:"optional,default:-1"`
- }
- // RFC 5280, 4.2.1.4
- type policyInformation struct {
- Policy asn1.ObjectIdentifier
- // policyQualifiers omitted
- }
- const (
- nameTypeEmail = 1
- nameTypeDNS = 2
- nameTypeURI = 6
- nameTypeIP = 7
- )
- // RFC 5280, 4.2.2.1
- type accessDescription struct {
- Method asn1.ObjectIdentifier
- Location asn1.RawValue
- }
- // RFC 5280, 4.2.1.14
- type distributionPoint struct {
- DistributionPoint distributionPointName `asn1:"optional,tag:0"`
- Reason asn1.BitString `asn1:"optional,tag:1"`
- CRLIssuer asn1.RawValue `asn1:"optional,tag:2"`
- }
- type distributionPointName struct {
- FullName []asn1.RawValue `asn1:"optional,tag:0"`
- RelativeName pkix.RDNSequence `asn1:"optional,tag:1"`
- }
- func parsePublicKey(algo PublicKeyAlgorithm, keyData *publicKeyInfo, nfe *NonFatalErrors) (interface{}, error) {
- asn1Data := keyData.PublicKey.RightAlign()
- switch algo {
- case RSA, RSAESOAEP:
- // RSA public keys must have a NULL in the parameters.
- // See RFC 3279, Section 2.3.1.
- if algo == RSA && !bytes.Equal(keyData.Algorithm.Parameters.FullBytes, asn1.NullBytes) {
- nfe.AddError(errors.New("x509: RSA key missing NULL parameters"))
- }
- if algo == RSAESOAEP {
- // We only parse the parameters to ensure it is a valid encoding, we throw out the actual values
- paramsData := keyData.Algorithm.Parameters.FullBytes
- params := new(rsaesoaepAlgorithmParameters)
- params.HashFunc = sha1Identifier
- params.MaskgenFunc = mgf1SHA1Identifier
- params.PSourceFunc = pSpecifiedEmptyIdentifier
- rest, err := asn1.Unmarshal(paramsData, params)
- if err != nil {
- return nil, err
- }
- if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after RSAES-OAEP parameters")
- }
- }
- p := new(pkcs1PublicKey)
- rest, err := asn1.Unmarshal(asn1Data, p)
- if err != nil {
- var laxErr error
- rest, laxErr = asn1.UnmarshalWithParams(asn1Data, p, "lax")
- if laxErr != nil {
- return nil, laxErr
- }
- nfe.AddError(err)
- }
- if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after RSA public key")
- }
- if p.N.Sign() <= 0 {
- nfe.AddError(errors.New("x509: RSA modulus is not a positive number"))
- }
- if p.E <= 0 {
- return nil, errors.New("x509: RSA public exponent is not a positive number")
- }
- // TODO(dkarch): Update to return the parameters once crypto/x509 has come up with permanent solution (https://github.com/golang/go/issues/30416)
- pub := &rsa.PublicKey{
- E: p.E,
- N: p.N,
- }
- return pub, nil
- case DSA:
- var p *big.Int
- rest, err := asn1.Unmarshal(asn1Data, &p)
- if err != nil {
- var laxErr error
- rest, laxErr = asn1.UnmarshalWithParams(asn1Data, &p, "lax")
- if laxErr != nil {
- return nil, laxErr
- }
- nfe.AddError(err)
- }
- if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after DSA public key")
- }
- paramsData := keyData.Algorithm.Parameters.FullBytes
- params := new(dsaAlgorithmParameters)
- rest, err = asn1.Unmarshal(paramsData, params)
- if err != nil {
- return nil, err
- }
- if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after DSA parameters")
- }
- if p.Sign() <= 0 || params.P.Sign() <= 0 || params.Q.Sign() <= 0 || params.G.Sign() <= 0 {
- return nil, errors.New("x509: zero or negative DSA parameter")
- }
- pub := &dsa.PublicKey{
- Parameters: dsa.Parameters{
- P: params.P,
- Q: params.Q,
- G: params.G,
- },
- Y: p,
- }
- return pub, nil
- case ECDSA:
- paramsData := keyData.Algorithm.Parameters.FullBytes
- namedCurveOID := new(asn1.ObjectIdentifier)
- rest, err := asn1.Unmarshal(paramsData, namedCurveOID)
- if err != nil {
- return nil, errors.New("x509: failed to parse ECDSA parameters as named curve")
- }
- if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after ECDSA parameters")
- }
- namedCurve := namedCurveFromOID(*namedCurveOID, nfe)
- if namedCurve == nil {
- return nil, fmt.Errorf("x509: unsupported elliptic curve %v", namedCurveOID)
- }
- x, y := elliptic.Unmarshal(namedCurve, asn1Data)
- if x == nil {
- return nil, errors.New("x509: failed to unmarshal elliptic curve point")
- }
- pub := &ecdsa.PublicKey{
- Curve: namedCurve,
- X: x,
- Y: y,
- }
- return pub, nil
- case Ed25519:
- return ed25519.PublicKey(asn1Data), nil
- default:
- return nil, nil
- }
- }
- // NonFatalErrors is an error type which can hold a number of other errors.
- // It's used to collect a range of non-fatal errors which occur while parsing
- // a certificate, that way we can still match on certs which technically are
- // invalid.
- type NonFatalErrors struct {
- Errors []error
- }
- // AddError adds an error to the list of errors contained by NonFatalErrors.
- func (e *NonFatalErrors) AddError(err error) {
- e.Errors = append(e.Errors, err)
- }
- // Returns a string consisting of the values of Error() from all of the errors
- // contained in |e|
- func (e NonFatalErrors) Error() string {
- r := "NonFatalErrors: "
- for _, err := range e.Errors {
- r += err.Error() + "; "
- }
- return r
- }
- // HasError returns true if |e| contains at least one error
- func (e *NonFatalErrors) HasError() bool {
- if e == nil {
- return false
- }
- return len(e.Errors) > 0
- }
- // Append combines the contents of two NonFatalErrors instances.
- func (e *NonFatalErrors) Append(more *NonFatalErrors) *NonFatalErrors {
- if e == nil {
- return more
- }
- if more == nil {
- return e
- }
- combined := NonFatalErrors{Errors: make([]error, 0, len(e.Errors)+len(more.Errors))}
- combined.Errors = append(combined.Errors, e.Errors...)
- combined.Errors = append(combined.Errors, more.Errors...)
- return &combined
- }
- // IsFatal indicates whether an error is fatal.
- func IsFatal(err error) bool {
- if err == nil {
- return false
- }
- if _, ok := err.(NonFatalErrors); ok {
- return false
- }
- if errs, ok := err.(*Errors); ok {
- return errs.Fatal()
- }
- return true
- }
- func parseDistributionPoints(data []byte, crldp *[]string) error {
- // CRLDistributionPoints ::= SEQUENCE SIZE (1..MAX) OF DistributionPoint
- //
- // DistributionPoint ::= SEQUENCE {
- // distributionPoint [0] DistributionPointName OPTIONAL,
- // reasons [1] ReasonFlags OPTIONAL,
- // cRLIssuer [2] GeneralNames OPTIONAL }
- //
- // DistributionPointName ::= CHOICE {
- // fullName [0] GeneralNames,
- // nameRelativeToCRLIssuer [1] RelativeDistinguishedName }
- var cdp []distributionPoint
- if rest, err := asn1.Unmarshal(data, &cdp); err != nil {
- return err
- } else if len(rest) != 0 {
- return errors.New("x509: trailing data after X.509 CRL distribution point")
- }
- for _, dp := range cdp {
- // Per RFC 5280, 4.2.1.13, one of distributionPoint or cRLIssuer may be empty.
- if len(dp.DistributionPoint.FullName) == 0 {
- continue
- }
- for _, fullName := range dp.DistributionPoint.FullName {
- if fullName.Tag == 6 {
- *crldp = append(*crldp, string(fullName.Bytes))
- }
- }
- }
- return nil
- }
- func forEachSAN(extension []byte, callback func(tag int, data []byte) error) error {
- // RFC 5280, 4.2.1.6
- // SubjectAltName ::= GeneralNames
- //
- // GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
- //
- // GeneralName ::= CHOICE {
- // otherName [0] OtherName,
- // rfc822Name [1] IA5String,
- // dNSName [2] IA5String,
- // x400Address [3] ORAddress,
- // directoryName [4] Name,
- // ediPartyName [5] EDIPartyName,
- // uniformResourceIdentifier [6] IA5String,
- // iPAddress [7] OCTET STRING,
- // registeredID [8] OBJECT IDENTIFIER }
- var seq asn1.RawValue
- rest, err := asn1.Unmarshal(extension, &seq)
- if err != nil {
- return err
- } else if len(rest) != 0 {
- return errors.New("x509: trailing data after X.509 extension")
- }
- if !seq.IsCompound || seq.Tag != asn1.TagSequence || seq.Class != asn1.ClassUniversal {
- return asn1.StructuralError{Msg: "bad SAN sequence"}
- }
- rest = seq.Bytes
- for len(rest) > 0 {
- var v asn1.RawValue
- rest, err = asn1.Unmarshal(rest, &v)
- if err != nil {
- return err
- }
- if err := callback(v.Tag, v.Bytes); err != nil {
- return err
- }
- }
- return nil
- }
- func parseSANExtension(value []byte, nfe *NonFatalErrors) (dnsNames, emailAddresses []string, ipAddresses []net.IP, uris []*url.URL, err error) {
- err = forEachSAN(value, func(tag int, data []byte) error {
- switch tag {
- case nameTypeEmail:
- emailAddresses = append(emailAddresses, string(data))
- case nameTypeDNS:
- dnsNames = append(dnsNames, string(data))
- case nameTypeURI:
- uri, err := url.Parse(string(data))
- if err != nil {
- return fmt.Errorf("x509: cannot parse URI %q: %s", string(data), err)
- }
- if len(uri.Host) > 0 {
- if _, ok := domainToReverseLabels(uri.Host); !ok {
- return fmt.Errorf("x509: cannot parse URI %q: invalid domain", string(data))
- }
- }
- uris = append(uris, uri)
- case nameTypeIP:
- switch len(data) {
- case net.IPv4len, net.IPv6len:
- ipAddresses = append(ipAddresses, data)
- default:
- nfe.AddError(errors.New("x509: cannot parse IP address of length " + strconv.Itoa(len(data))))
- }
- }
- return nil
- })
- return
- }
- // isValidIPMask reports whether mask consists of zero or more 1 bits, followed by zero bits.
- func isValidIPMask(mask []byte) bool {
- seenZero := false
- for _, b := range mask {
- if seenZero {
- if b != 0 {
- return false
- }
- continue
- }
- switch b {
- case 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe:
- seenZero = true
- case 0xff:
- default:
- return false
- }
- }
- return true
- }
- func parseNameConstraintsExtension(out *Certificate, e pkix.Extension, nfe *NonFatalErrors) (unhandled bool, err error) {
- // RFC 5280, 4.2.1.10
- // NameConstraints ::= SEQUENCE {
- // permittedSubtrees [0] GeneralSubtrees OPTIONAL,
- // excludedSubtrees [1] GeneralSubtrees OPTIONAL }
- //
- // GeneralSubtrees ::= SEQUENCE SIZE (1..MAX) OF GeneralSubtree
- //
- // GeneralSubtree ::= SEQUENCE {
- // base GeneralName,
- // minimum [0] BaseDistance DEFAULT 0,
- // maximum [1] BaseDistance OPTIONAL }
- //
- // BaseDistance ::= INTEGER (0..MAX)
- outer := cryptobyte.String(e.Value)
- var toplevel, permitted, excluded cryptobyte.String
- var havePermitted, haveExcluded bool
- if !outer.ReadASN1(&toplevel, cryptobyte_asn1.SEQUENCE) ||
- !outer.Empty() ||
- !toplevel.ReadOptionalASN1(&permitted, &havePermitted, cryptobyte_asn1.Tag(0).ContextSpecific().Constructed()) ||
- !toplevel.ReadOptionalASN1(&excluded, &haveExcluded, cryptobyte_asn1.Tag(1).ContextSpecific().Constructed()) ||
- !toplevel.Empty() {
- return false, errors.New("x509: invalid NameConstraints extension")
- }
- if !havePermitted && !haveExcluded || len(permitted) == 0 && len(excluded) == 0 {
- // From RFC 5280, Section 4.2.1.10:
- // “either the permittedSubtrees field
- // or the excludedSubtrees MUST be
- // present”
- return false, errors.New("x509: empty name constraints extension")
- }
- getValues := func(subtrees cryptobyte.String) (dnsNames []string, ips []*net.IPNet, emails, uriDomains []string, err error) {
- for !subtrees.Empty() {
- var seq, value cryptobyte.String
- var tag cryptobyte_asn1.Tag
- if !subtrees.ReadASN1(&seq, cryptobyte_asn1.SEQUENCE) ||
- !seq.ReadAnyASN1(&value, &tag) {
- return nil, nil, nil, nil, fmt.Errorf("x509: invalid NameConstraints extension")
- }
- var (
- dnsTag = cryptobyte_asn1.Tag(2).ContextSpecific()
- emailTag = cryptobyte_asn1.Tag(1).ContextSpecific()
- ipTag = cryptobyte_asn1.Tag(7).ContextSpecific()
- uriTag = cryptobyte_asn1.Tag(6).ContextSpecific()
- )
- switch tag {
- case dnsTag:
- domain := string(value)
- if err := isIA5String(domain); err != nil {
- return nil, nil, nil, nil, errors.New("x509: invalid constraint value: " + err.Error())
- }
- trimmedDomain := domain
- if len(trimmedDomain) > 0 && trimmedDomain[0] == '.' {
- // constraints can have a leading
- // period to exclude the domain
- // itself, but that's not valid in a
- // normal domain name.
- trimmedDomain = trimmedDomain[1:]
- }
- if _, ok := domainToReverseLabels(trimmedDomain); !ok {
- nfe.AddError(fmt.Errorf("x509: failed to parse dnsName constraint %q", domain))
- }
- dnsNames = append(dnsNames, domain)
- case ipTag:
- l := len(value)
- var ip, mask []byte
- switch l {
- case 8:
- ip = value[:4]
- mask = value[4:]
- case 32:
- ip = value[:16]
- mask = value[16:]
- default:
- return nil, nil, nil, nil, fmt.Errorf("x509: IP constraint contained value of length %d", l)
- }
- if !isValidIPMask(mask) {
- return nil, nil, nil, nil, fmt.Errorf("x509: IP constraint contained invalid mask %x", mask)
- }
- ips = append(ips, &net.IPNet{IP: net.IP(ip), Mask: net.IPMask(mask)})
- case emailTag:
- constraint := string(value)
- if err := isIA5String(constraint); err != nil {
- return nil, nil, nil, nil, errors.New("x509: invalid constraint value: " + err.Error())
- }
- // If the constraint contains an @ then
- // it specifies an exact mailbox name.
- if strings.Contains(constraint, "@") {
- if _, ok := parseRFC2821Mailbox(constraint); !ok {
- nfe.AddError(fmt.Errorf("x509: failed to parse rfc822Name constraint %q", constraint))
- }
- } else {
- // Otherwise it's a domain name.
- domain := constraint
- if len(domain) > 0 && domain[0] == '.' {
- domain = domain[1:]
- }
- if _, ok := domainToReverseLabels(domain); !ok {
- nfe.AddError(fmt.Errorf("x509: failed to parse rfc822Name constraint %q", constraint))
- }
- }
- emails = append(emails, constraint)
- case uriTag:
- domain := string(value)
- if err := isIA5String(domain); err != nil {
- return nil, nil, nil, nil, errors.New("x509: invalid constraint value: " + err.Error())
- }
- if net.ParseIP(domain) != nil {
- return nil, nil, nil, nil, fmt.Errorf("x509: failed to parse URI constraint %q: cannot be IP address", domain)
- }
- trimmedDomain := domain
- if len(trimmedDomain) > 0 && trimmedDomain[0] == '.' {
- // constraints can have a leading
- // period to exclude the domain itself,
- // but that's not valid in a normal
- // domain name.
- trimmedDomain = trimmedDomain[1:]
- }
- if _, ok := domainToReverseLabels(trimmedDomain); !ok {
- nfe.AddError(fmt.Errorf("x509: failed to parse URI constraint %q", domain))
- }
- uriDomains = append(uriDomains, domain)
- default:
- unhandled = true
- }
- }
- return dnsNames, ips, emails, uriDomains, nil
- }
- if out.PermittedDNSDomains, out.PermittedIPRanges, out.PermittedEmailAddresses, out.PermittedURIDomains, err = getValues(permitted); err != nil {
- return false, err
- }
- if out.ExcludedDNSDomains, out.ExcludedIPRanges, out.ExcludedEmailAddresses, out.ExcludedURIDomains, err = getValues(excluded); err != nil {
- return false, err
- }
- out.PermittedDNSDomainsCritical = e.Critical
- return unhandled, nil
- }
- func parseCertificate(in *certificate) (*Certificate, error) {
- var nfe NonFatalErrors
- out := new(Certificate)
- out.Raw = in.Raw
- out.RawTBSCertificate = in.TBSCertificate.Raw
- out.RawSubjectPublicKeyInfo = in.TBSCertificate.PublicKey.Raw
- out.RawSubject = in.TBSCertificate.Subject.FullBytes
- out.RawIssuer = in.TBSCertificate.Issuer.FullBytes
- out.Signature = in.SignatureValue.RightAlign()
- out.SignatureAlgorithm = SignatureAlgorithmFromAI(in.TBSCertificate.SignatureAlgorithm)
- out.PublicKeyAlgorithm =
- getPublicKeyAlgorithmFromOID(in.TBSCertificate.PublicKey.Algorithm.Algorithm)
- var err error
- out.PublicKey, err = parsePublicKey(out.PublicKeyAlgorithm, &in.TBSCertificate.PublicKey, &nfe)
- if err != nil {
- return nil, err
- }
- out.Version = in.TBSCertificate.Version + 1
- out.SerialNumber = in.TBSCertificate.SerialNumber
- var issuer, subject pkix.RDNSequence
- if rest, err := asn1.Unmarshal(in.TBSCertificate.Subject.FullBytes, &subject); err != nil {
- var laxErr error
- rest, laxErr = asn1.UnmarshalWithParams(in.TBSCertificate.Subject.FullBytes, &subject, "lax")
- if laxErr != nil {
- return nil, laxErr
- }
- nfe.AddError(err)
- } else if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after X.509 subject")
- }
- if rest, err := asn1.Unmarshal(in.TBSCertificate.Issuer.FullBytes, &issuer); err != nil {
- var laxErr error
- rest, laxErr = asn1.UnmarshalWithParams(in.TBSCertificate.Issuer.FullBytes, &issuer, "lax")
- if laxErr != nil {
- return nil, laxErr
- }
- nfe.AddError(err)
- } else if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after X.509 subject")
- }
- out.Issuer.FillFromRDNSequence(&issuer)
- out.Subject.FillFromRDNSequence(&subject)
- out.NotBefore = in.TBSCertificate.Validity.NotBefore
- out.NotAfter = in.TBSCertificate.Validity.NotAfter
- for _, e := range in.TBSCertificate.Extensions {
- out.Extensions = append(out.Extensions, e)
- unhandled := false
- if len(e.Id) == 4 && e.Id[0] == OIDExtensionArc[0] && e.Id[1] == OIDExtensionArc[1] && e.Id[2] == OIDExtensionArc[2] {
- switch e.Id[3] {
- case OIDExtensionKeyUsage[3]:
- // RFC 5280, 4.2.1.3
- var usageBits asn1.BitString
- if rest, err := asn1.Unmarshal(e.Value, &usageBits); err != nil {
- return nil, err
- } else if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after X.509 KeyUsage")
- }
- var usage int
- for i := 0; i < 9; i++ {
- if usageBits.At(i) != 0 {
- usage |= 1 << uint(i)
- }
- }
- out.KeyUsage = KeyUsage(usage)
- case OIDExtensionBasicConstraints[3]:
- // RFC 5280, 4.2.1.9
- var constraints basicConstraints
- if rest, err := asn1.Unmarshal(e.Value, &constraints); err != nil {
- return nil, err
- } else if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after X.509 BasicConstraints")
- }
- out.BasicConstraintsValid = true
- out.IsCA = constraints.IsCA
- out.MaxPathLen = constraints.MaxPathLen
- out.MaxPathLenZero = out.MaxPathLen == 0
- // TODO: map out.MaxPathLen to 0 if it has the -1 default value? (Issue 19285)
- case OIDExtensionSubjectAltName[3]:
- out.DNSNames, out.EmailAddresses, out.IPAddresses, out.URIs, err = parseSANExtension(e.Value, &nfe)
- if err != nil {
- return nil, err
- }
- if len(out.DNSNames) == 0 && len(out.EmailAddresses) == 0 && len(out.IPAddresses) == 0 && len(out.URIs) == 0 {
- // If we didn't parse anything then we do the critical check, below.
- unhandled = true
- }
- case OIDExtensionNameConstraints[3]:
- unhandled, err = parseNameConstraintsExtension(out, e, &nfe)
- if err != nil {
- return nil, err
- }
- case OIDExtensionCRLDistributionPoints[3]:
- // RFC 5280, 4.2.1.13
- if err := parseDistributionPoints(e.Value, &out.CRLDistributionPoints); err != nil {
- return nil, err
- }
- case OIDExtensionAuthorityKeyId[3]:
- // RFC 5280, 4.2.1.1
- var a authKeyId
- if rest, err := asn1.Unmarshal(e.Value, &a); err != nil {
- return nil, err
- } else if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after X.509 authority key-id")
- }
- out.AuthorityKeyId = a.Id
- case OIDExtensionExtendedKeyUsage[3]:
- // RFC 5280, 4.2.1.12. Extended Key Usage
- // id-ce-extKeyUsage OBJECT IDENTIFIER ::= { id-ce 37 }
- //
- // ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
- //
- // KeyPurposeId ::= OBJECT IDENTIFIER
- var keyUsage []asn1.ObjectIdentifier
- if len(e.Value) == 0 {
- nfe.AddError(errors.New("x509: empty ExtendedKeyUsage"))
- } else {
- rest, err := asn1.Unmarshal(e.Value, &keyUsage)
- if err != nil {
- var laxErr error
- rest, laxErr = asn1.UnmarshalWithParams(e.Value, &keyUsage, "lax")
- if laxErr != nil {
- return nil, laxErr
- }
- nfe.AddError(err)
- }
- if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after X.509 ExtendedKeyUsage")
- }
- }
- for _, u := range keyUsage {
- if extKeyUsage, ok := extKeyUsageFromOID(u); ok {
- out.ExtKeyUsage = append(out.ExtKeyUsage, extKeyUsage)
- } else {
- out.UnknownExtKeyUsage = append(out.UnknownExtKeyUsage, u)
- }
- }
- case OIDExtensionSubjectKeyId[3]:
- // RFC 5280, 4.2.1.2
- var keyid []byte
- if rest, err := asn1.Unmarshal(e.Value, &keyid); err != nil {
- return nil, err
- } else if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after X.509 key-id")
- }
- out.SubjectKeyId = keyid
- case OIDExtensionCertificatePolicies[3]:
- // RFC 5280 4.2.1.4: Certificate Policies
- var policies []policyInformation
- if rest, err := asn1.Unmarshal(e.Value, &policies); err != nil {
- return nil, err
- } else if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after X.509 certificate policies")
- }
- out.PolicyIdentifiers = make([]asn1.ObjectIdentifier, len(policies))
- for i, policy := range policies {
- out.PolicyIdentifiers[i] = policy.Policy
- }
- default:
- // Unknown extensions are recorded if critical.
- unhandled = true
- }
- } else if e.Id.Equal(OIDExtensionAuthorityInfoAccess) {
- // RFC 5280 4.2.2.1: Authority Information Access
- var aia []accessDescription
- if rest, err := asn1.Unmarshal(e.Value, &aia); err != nil {
- return nil, err
- } else if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after X.509 authority information")
- }
- if len(aia) == 0 {
- nfe.AddError(errors.New("x509: empty AuthorityInfoAccess extension"))
- }
- for _, v := range aia {
- // GeneralName: uniformResourceIdentifier [6] IA5String
- if v.Location.Tag != 6 {
- continue
- }
- if v.Method.Equal(OIDAuthorityInfoAccessOCSP) {
- out.OCSPServer = append(out.OCSPServer, string(v.Location.Bytes))
- } else if v.Method.Equal(OIDAuthorityInfoAccessIssuers) {
- out.IssuingCertificateURL = append(out.IssuingCertificateURL, string(v.Location.Bytes))
- }
- }
- } else if e.Id.Equal(OIDExtensionSubjectInfoAccess) {
- // RFC 5280 4.2.2.2: Subject Information Access
- var sia []accessDescription
- if rest, err := asn1.Unmarshal(e.Value, &sia); err != nil {
- return nil, err
- } else if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after X.509 subject information")
- }
- if len(sia) == 0 {
- nfe.AddError(errors.New("x509: empty SubjectInfoAccess extension"))
- }
- for _, v := range sia {
- // TODO(drysdale): cope with non-URI types of GeneralName
- // GeneralName: uniformResourceIdentifier [6] IA5String
- if v.Location.Tag != 6 {
- continue
- }
- if v.Method.Equal(OIDSubjectInfoAccessTimestamp) {
- out.SubjectTimestamps = append(out.SubjectTimestamps, string(v.Location.Bytes))
- } else if v.Method.Equal(OIDSubjectInfoAccessCARepo) {
- out.SubjectCARepositories = append(out.SubjectCARepositories, string(v.Location.Bytes))
- }
- }
- } else if e.Id.Equal(OIDExtensionIPPrefixList) {
- out.RPKIAddressRanges = parseRPKIAddrBlocks(e.Value, &nfe)
- } else if e.Id.Equal(OIDExtensionASList) {
- out.RPKIASNumbers, out.RPKIRoutingDomainIDs = parseRPKIASIdentifiers(e.Value, &nfe)
- } else if e.Id.Equal(OIDExtensionCTSCT) {
- if rest, err := asn1.Unmarshal(e.Value, &out.RawSCT); err != nil {
- nfe.AddError(fmt.Errorf("failed to asn1.Unmarshal SCT list extension: %v", err))
- } else if len(rest) != 0 {
- nfe.AddError(errors.New("trailing data after ASN1-encoded SCT list"))
- } else {
- if rest, err := tls.Unmarshal(out.RawSCT, &out.SCTList); err != nil {
- nfe.AddError(fmt.Errorf("failed to tls.Unmarshal SCT list: %v", err))
- } else if len(rest) != 0 {
- nfe.AddError(errors.New("trailing data after TLS-encoded SCT list"))
- }
- }
- } else {
- // Unknown extensions are recorded if critical.
- unhandled = true
- }
- if e.Critical && unhandled {
- out.UnhandledCriticalExtensions = append(out.UnhandledCriticalExtensions, e.Id)
- }
- }
- if nfe.HasError() {
- return out, nfe
- }
- return out, nil
- }
- // ParseTBSCertificate parses a single TBSCertificate from the given ASN.1 DER data.
- // The parsed data is returned in a Certificate struct for ease of access.
- func ParseTBSCertificate(asn1Data []byte) (*Certificate, error) {
- var tbsCert tbsCertificate
- var nfe NonFatalErrors
- rest, err := asn1.Unmarshal(asn1Data, &tbsCert)
- if err != nil {
- var laxErr error
- rest, laxErr = asn1.UnmarshalWithParams(asn1Data, &tbsCert, "lax")
- if laxErr != nil {
- return nil, laxErr
- }
- nfe.AddError(err)
- }
- if len(rest) > 0 {
- return nil, asn1.SyntaxError{Msg: "trailing data"}
- }
- ret, err := parseCertificate(&certificate{
- Raw: tbsCert.Raw,
- TBSCertificate: tbsCert})
- if err != nil {
- errs, ok := err.(NonFatalErrors)
- if !ok {
- return nil, err
- }
- nfe.Errors = append(nfe.Errors, errs.Errors...)
- }
- if nfe.HasError() {
- return ret, nfe
- }
- return ret, nil
- }
- // ParseCertificate parses a single certificate from the given ASN.1 DER data.
- // This function can return both a Certificate and an error (in which case the
- // error will be of type NonFatalErrors).
- func ParseCertificate(asn1Data []byte) (*Certificate, error) {
- var cert certificate
- var nfe NonFatalErrors
- rest, err := asn1.Unmarshal(asn1Data, &cert)
- if err != nil {
- var laxErr error
- rest, laxErr = asn1.UnmarshalWithParams(asn1Data, &cert, "lax")
- if laxErr != nil {
- return nil, laxErr
- }
- nfe.AddError(err)
- }
- if len(rest) > 0 {
- return nil, asn1.SyntaxError{Msg: "trailing data"}
- }
- ret, err := parseCertificate(&cert)
- if err != nil {
- errs, ok := err.(NonFatalErrors)
- if !ok {
- return nil, err
- }
- nfe.Errors = append(nfe.Errors, errs.Errors...)
- }
- if nfe.HasError() {
- return ret, nfe
- }
- return ret, nil
- }
- // ParseCertificates parses one or more certificates from the given ASN.1 DER
- // data. The certificates must be concatenated with no intermediate padding.
- // This function can return both a slice of Certificate and an error (in which
- // case the error will be of type NonFatalErrors).
- func ParseCertificates(asn1Data []byte) ([]*Certificate, error) {
- var v []*certificate
- var nfe NonFatalErrors
- for len(asn1Data) > 0 {
- cert := new(certificate)
- var err error
- asn1Data, err = asn1.Unmarshal(asn1Data, cert)
- if err != nil {
- var laxErr error
- asn1Data, laxErr = asn1.UnmarshalWithParams(asn1Data, &cert, "lax")
- if laxErr != nil {
- return nil, laxErr
- }
- nfe.AddError(err)
- }
- v = append(v, cert)
- }
- ret := make([]*Certificate, len(v))
- for i, ci := range v {
- cert, err := parseCertificate(ci)
- if err != nil {
- errs, ok := err.(NonFatalErrors)
- if !ok {
- return nil, err
- }
- nfe.Errors = append(nfe.Errors, errs.Errors...)
- }
- ret[i] = cert
- }
- if nfe.HasError() {
- return ret, nfe
- }
- return ret, nil
- }
- func reverseBitsInAByte(in byte) byte {
- b1 := in>>4 | in<<4
- b2 := b1>>2&0x33 | b1<<2&0xcc
- b3 := b2>>1&0x55 | b2<<1&0xaa
- return b3
- }
- // asn1BitLength returns the bit-length of bitString by considering the
- // most-significant bit in a byte to be the "first" bit. This convention
- // matches ASN.1, but differs from almost everything else.
- func asn1BitLength(bitString []byte) int {
- bitLen := len(bitString) * 8
- for i := range bitString {
- b := bitString[len(bitString)-i-1]
- for bit := uint(0); bit < 8; bit++ {
- if (b>>bit)&1 == 1 {
- return bitLen
- }
- bitLen--
- }
- }
- return 0
- }
- // OID values for standard extensions from RFC 5280.
- var (
- OIDExtensionArc = asn1.ObjectIdentifier{2, 5, 29} // id-ce RFC5280 s4.2.1
- OIDExtensionSubjectKeyId = asn1.ObjectIdentifier{2, 5, 29, 14}
- OIDExtensionKeyUsage = asn1.ObjectIdentifier{2, 5, 29, 15}
- OIDExtensionExtendedKeyUsage = asn1.ObjectIdentifier{2, 5, 29, 37}
- OIDExtensionAuthorityKeyId = asn1.ObjectIdentifier{2, 5, 29, 35}
- OIDExtensionBasicConstraints = asn1.ObjectIdentifier{2, 5, 29, 19}
- OIDExtensionSubjectAltName = asn1.ObjectIdentifier{2, 5, 29, 17}
- OIDExtensionCertificatePolicies = asn1.ObjectIdentifier{2, 5, 29, 32}
- OIDExtensionNameConstraints = asn1.ObjectIdentifier{2, 5, 29, 30}
- OIDExtensionCRLDistributionPoints = asn1.ObjectIdentifier{2, 5, 29, 31}
- OIDExtensionIssuerAltName = asn1.ObjectIdentifier{2, 5, 29, 18}
- OIDExtensionSubjectDirectoryAttributes = asn1.ObjectIdentifier{2, 5, 29, 9}
- OIDExtensionInhibitAnyPolicy = asn1.ObjectIdentifier{2, 5, 29, 54}
- OIDExtensionPolicyConstraints = asn1.ObjectIdentifier{2, 5, 29, 36}
- OIDExtensionPolicyMappings = asn1.ObjectIdentifier{2, 5, 29, 33}
- OIDExtensionFreshestCRL = asn1.ObjectIdentifier{2, 5, 29, 46}
- OIDExtensionAuthorityInfoAccess = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 1, 1}
- OIDExtensionSubjectInfoAccess = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 1, 11}
- // OIDExtensionCTPoison is defined in RFC 6962 s3.1.
- OIDExtensionCTPoison = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 11129, 2, 4, 3}
- // OIDExtensionCTSCT is defined in RFC 6962 s3.3.
- OIDExtensionCTSCT = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 11129, 2, 4, 2}
- // OIDExtensionIPPrefixList is defined in RFC 3779 s2.
- OIDExtensionIPPrefixList = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 1, 7}
- // OIDExtensionASList is defined in RFC 3779 s3.
- OIDExtensionASList = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 1, 8}
- )
- var (
- OIDAuthorityInfoAccessOCSP = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 48, 1}
- OIDAuthorityInfoAccessIssuers = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 48, 2}
- OIDSubjectInfoAccessTimestamp = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 48, 3}
- OIDSubjectInfoAccessCARepo = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 48, 5}
- OIDAnyPolicy = asn1.ObjectIdentifier{2, 5, 29, 32, 0}
- )
- // oidInExtensions reports whether an extension with the given oid exists in
- // extensions.
- func oidInExtensions(oid asn1.ObjectIdentifier, extensions []pkix.Extension) bool {
- for _, e := range extensions {
- if e.Id.Equal(oid) {
- return true
- }
- }
- return false
- }
- // marshalSANs marshals a list of addresses into a the contents of an X.509
- // SubjectAlternativeName extension.
- func marshalSANs(dnsNames, emailAddresses []string, ipAddresses []net.IP, uris []*url.URL) (derBytes []byte, err error) {
- var rawValues []asn1.RawValue
- for _, name := range dnsNames {
- rawValues = append(rawValues, asn1.RawValue{Tag: nameTypeDNS, Class: asn1.ClassContextSpecific, Bytes: []byte(name)})
- }
- for _, email := range emailAddresses {
- rawValues = append(rawValues, asn1.RawValue{Tag: nameTypeEmail, Class: asn1.ClassContextSpecific, Bytes: []byte(email)})
- }
- for _, rawIP := range ipAddresses {
- // If possible, we always want to encode IPv4 addresses in 4 bytes.
- ip := rawIP.To4()
- if ip == nil {
- ip = rawIP
- }
- rawValues = append(rawValues, asn1.RawValue{Tag: nameTypeIP, Class: asn1.ClassContextSpecific, Bytes: ip})
- }
- for _, uri := range uris {
- rawValues = append(rawValues, asn1.RawValue{Tag: nameTypeURI, Class: asn1.ClassContextSpecific, Bytes: []byte(uri.String())})
- }
- return asn1.Marshal(rawValues)
- }
- func isIA5String(s string) error {
- for _, r := range s {
- if r >= utf8.RuneSelf {
- return fmt.Errorf("x509: %q cannot be encoded as an IA5String", s)
- }
- }
- return nil
- }
- func buildExtensions(template *Certificate, subjectIsEmpty bool, authorityKeyId []byte) (ret []pkix.Extension, err error) {
- ret = make([]pkix.Extension, 12 /* maximum number of elements. */)
- n := 0
- if template.KeyUsage != 0 &&
- !oidInExtensions(OIDExtensionKeyUsage, template.ExtraExtensions) {
- ret[n].Id = OIDExtensionKeyUsage
- ret[n].Critical = true
- var a [2]byte
- a[0] = reverseBitsInAByte(byte(template.KeyUsage))
- a[1] = reverseBitsInAByte(byte(template.KeyUsage >> 8))
- l := 1
- if a[1] != 0 {
- l = 2
- }
- bitString := a[:l]
- ret[n].Value, err = asn1.Marshal(asn1.BitString{Bytes: bitString, BitLength: asn1BitLength(bitString)})
- if err != nil {
- return
- }
- n++
- }
- if (len(template.ExtKeyUsage) > 0 || len(template.UnknownExtKeyUsage) > 0) &&
- !oidInExtensions(OIDExtensionExtendedKeyUsage, template.ExtraExtensions) {
- ret[n].Id = OIDExtensionExtendedKeyUsage
- var oids []asn1.ObjectIdentifier
- for _, u := range template.ExtKeyUsage {
- if oid, ok := oidFromExtKeyUsage(u); ok {
- oids = append(oids, oid)
- } else {
- panic("internal error")
- }
- }
- oids = append(oids, template.UnknownExtKeyUsage...)
- ret[n].Value, err = asn1.Marshal(oids)
- if err != nil {
- return
- }
- n++
- }
- if template.BasicConstraintsValid && !oidInExtensions(OIDExtensionBasicConstraints, template.ExtraExtensions) {
- // Leaving MaxPathLen as zero indicates that no maximum path
- // length is desired, unless MaxPathLenZero is set. A value of
- // -1 causes encoding/asn1 to omit the value as desired.
- maxPathLen := template.MaxPathLen
- if maxPathLen == 0 && !template.MaxPathLenZero {
- maxPathLen = -1
- }
- ret[n].Id = OIDExtensionBasicConstraints
- ret[n].Value, err = asn1.Marshal(basicConstraints{template.IsCA, maxPathLen})
- ret[n].Critical = true
- if err != nil {
- return
- }
- n++
- }
- if len(template.SubjectKeyId) > 0 && !oidInExtensions(OIDExtensionSubjectKeyId, template.ExtraExtensions) {
- ret[n].Id = OIDExtensionSubjectKeyId
- ret[n].Value, err = asn1.Marshal(template.SubjectKeyId)
- if err != nil {
- return
- }
- n++
- }
- if len(authorityKeyId) > 0 && !oidInExtensions(OIDExtensionAuthorityKeyId, template.ExtraExtensions) {
- ret[n].Id = OIDExtensionAuthorityKeyId
- ret[n].Value, err = asn1.Marshal(authKeyId{authorityKeyId})
- if err != nil {
- return
- }
- n++
- }
- if (len(template.OCSPServer) > 0 || len(template.IssuingCertificateURL) > 0) &&
- !oidInExtensions(OIDExtensionAuthorityInfoAccess, template.ExtraExtensions) {
- ret[n].Id = OIDExtensionAuthorityInfoAccess
- var aiaValues []accessDescription
- for _, name := range template.OCSPServer {
- aiaValues = append(aiaValues, accessDescription{
- Method: OIDAuthorityInfoAccessOCSP,
- Location: asn1.RawValue{Tag: 6, Class: asn1.ClassContextSpecific, Bytes: []byte(name)},
- })
- }
- for _, name := range template.IssuingCertificateURL {
- aiaValues = append(aiaValues, accessDescription{
- Method: OIDAuthorityInfoAccessIssuers,
- Location: asn1.RawValue{Tag: 6, Class: asn1.ClassContextSpecific, Bytes: []byte(name)},
- })
- }
- ret[n].Value, err = asn1.Marshal(aiaValues)
- if err != nil {
- return
- }
- n++
- }
- if len(template.SubjectTimestamps) > 0 || len(template.SubjectCARepositories) > 0 &&
- !oidInExtensions(OIDExtensionSubjectInfoAccess, template.ExtraExtensions) {
- ret[n].Id = OIDExtensionSubjectInfoAccess
- var siaValues []accessDescription
- for _, ts := range template.SubjectTimestamps {
- siaValues = append(siaValues, accessDescription{
- Method: OIDSubjectInfoAccessTimestamp,
- Location: asn1.RawValue{Tag: 6, Class: asn1.ClassContextSpecific, Bytes: []byte(ts)},
- })
- }
- for _, repo := range template.SubjectCARepositories {
- siaValues = append(siaValues, accessDescription{
- Method: OIDSubjectInfoAccessCARepo,
- Location: asn1.RawValue{Tag: 6, Class: asn1.ClassContextSpecific, Bytes: []byte(repo)},
- })
- }
- ret[n].Value, err = asn1.Marshal(siaValues)
- if err != nil {
- return
- }
- n++
- }
- if (len(template.DNSNames) > 0 || len(template.EmailAddresses) > 0 || len(template.IPAddresses) > 0 || len(template.URIs) > 0) &&
- !oidInExtensions(OIDExtensionSubjectAltName, template.ExtraExtensions) {
- ret[n].Id = OIDExtensionSubjectAltName
- // From RFC 5280, Section 4.2.1.6:
- // “If the subject field contains an empty sequence ... then
- // subjectAltName extension ... is marked as critical”
- ret[n].Critical = subjectIsEmpty
- ret[n].Value, err = marshalSANs(template.DNSNames, template.EmailAddresses, template.IPAddresses, template.URIs)
- if err != nil {
- return
- }
- n++
- }
- if len(template.PolicyIdentifiers) > 0 &&
- !oidInExtensions(OIDExtensionCertificatePolicies, template.ExtraExtensions) {
- ret[n].Id = OIDExtensionCertificatePolicies
- policies := make([]policyInformation, len(template.PolicyIdentifiers))
- for i, policy := range template.PolicyIdentifiers {
- policies[i].Policy = policy
- }
- ret[n].Value, err = asn1.Marshal(policies)
- if err != nil {
- return
- }
- n++
- }
- if (len(template.PermittedDNSDomains) > 0 || len(template.ExcludedDNSDomains) > 0 ||
- len(template.PermittedIPRanges) > 0 || len(template.ExcludedIPRanges) > 0 ||
- len(template.PermittedEmailAddresses) > 0 || len(template.ExcludedEmailAddresses) > 0 ||
- len(template.PermittedURIDomains) > 0 || len(template.ExcludedURIDomains) > 0) &&
- !oidInExtensions(OIDExtensionNameConstraints, template.ExtraExtensions) {
- ret[n].Id = OIDExtensionNameConstraints
- ret[n].Critical = template.PermittedDNSDomainsCritical
- ipAndMask := func(ipNet *net.IPNet) []byte {
- maskedIP := ipNet.IP.Mask(ipNet.Mask)
- ipAndMask := make([]byte, 0, len(maskedIP)+len(ipNet.Mask))
- ipAndMask = append(ipAndMask, maskedIP...)
- ipAndMask = append(ipAndMask, ipNet.Mask...)
- return ipAndMask
- }
- serialiseConstraints := func(dns []string, ips []*net.IPNet, emails []string, uriDomains []string) (der []byte, err error) {
- var b cryptobyte.Builder
- for _, name := range dns {
- if err = isIA5String(name); err != nil {
- return nil, err
- }
- b.AddASN1(cryptobyte_asn1.SEQUENCE, func(b *cryptobyte.Builder) {
- b.AddASN1(cryptobyte_asn1.Tag(2).ContextSpecific(), func(b *cryptobyte.Builder) {
- b.AddBytes([]byte(name))
- })
- })
- }
- for _, ipNet := range ips {
- b.AddASN1(cryptobyte_asn1.SEQUENCE, func(b *cryptobyte.Builder) {
- b.AddASN1(cryptobyte_asn1.Tag(7).ContextSpecific(), func(b *cryptobyte.Builder) {
- b.AddBytes(ipAndMask(ipNet))
- })
- })
- }
- for _, email := range emails {
- if err = isIA5String(email); err != nil {
- return nil, err
- }
- b.AddASN1(cryptobyte_asn1.SEQUENCE, func(b *cryptobyte.Builder) {
- b.AddASN1(cryptobyte_asn1.Tag(1).ContextSpecific(), func(b *cryptobyte.Builder) {
- b.AddBytes([]byte(email))
- })
- })
- }
- for _, uriDomain := range uriDomains {
- if err = isIA5String(uriDomain); err != nil {
- return nil, err
- }
- b.AddASN1(cryptobyte_asn1.SEQUENCE, func(b *cryptobyte.Builder) {
- b.AddASN1(cryptobyte_asn1.Tag(6).ContextSpecific(), func(b *cryptobyte.Builder) {
- b.AddBytes([]byte(uriDomain))
- })
- })
- }
- return b.Bytes()
- }
- permitted, err := serialiseConstraints(template.PermittedDNSDomains, template.PermittedIPRanges, template.PermittedEmailAddresses, template.PermittedURIDomains)
- if err != nil {
- return nil, err
- }
- excluded, err := serialiseConstraints(template.ExcludedDNSDomains, template.ExcludedIPRanges, template.ExcludedEmailAddresses, template.ExcludedURIDomains)
- if err != nil {
- return nil, err
- }
- var b cryptobyte.Builder
- b.AddASN1(cryptobyte_asn1.SEQUENCE, func(b *cryptobyte.Builder) {
- if len(permitted) > 0 {
- b.AddASN1(cryptobyte_asn1.Tag(0).ContextSpecific().Constructed(), func(b *cryptobyte.Builder) {
- b.AddBytes(permitted)
- })
- }
- if len(excluded) > 0 {
- b.AddASN1(cryptobyte_asn1.Tag(1).ContextSpecific().Constructed(), func(b *cryptobyte.Builder) {
- b.AddBytes(excluded)
- })
- }
- })
- ret[n].Value, err = b.Bytes()
- if err != nil {
- return nil, err
- }
- n++
- }
- if len(template.CRLDistributionPoints) > 0 &&
- !oidInExtensions(OIDExtensionCRLDistributionPoints, template.ExtraExtensions) {
- ret[n].Id = OIDExtensionCRLDistributionPoints
- var crlDp []distributionPoint
- for _, name := range template.CRLDistributionPoints {
- dp := distributionPoint{
- DistributionPoint: distributionPointName{
- FullName: []asn1.RawValue{
- {Tag: 6, Class: asn1.ClassContextSpecific, Bytes: []byte(name)},
- },
- },
- }
- crlDp = append(crlDp, dp)
- }
- ret[n].Value, err = asn1.Marshal(crlDp)
- if err != nil {
- return
- }
- n++
- }
- if (len(template.RawSCT) > 0 || len(template.SCTList.SCTList) > 0) && !oidInExtensions(OIDExtensionCTSCT, template.ExtraExtensions) {
- rawSCT := template.RawSCT
- if len(template.SCTList.SCTList) > 0 {
- rawSCT, err = tls.Marshal(template.SCTList)
- if err != nil {
- return
- }
- }
- ret[n].Id = OIDExtensionCTSCT
- ret[n].Value, err = asn1.Marshal(rawSCT)
- if err != nil {
- return
- }
- n++
- }
- // Adding another extension here? Remember to update the maximum number
- // of elements in the make() at the top of the function and the list of
- // template fields used in CreateCertificate documentation.
- return append(ret[:n], template.ExtraExtensions...), nil
- }
- func subjectBytes(cert *Certificate) ([]byte, error) {
- if len(cert.RawSubject) > 0 {
- return cert.RawSubject, nil
- }
- return asn1.Marshal(cert.Subject.ToRDNSequence())
- }
- // signingParamsForPublicKey returns the parameters to use for signing with
- // priv. If requestedSigAlgo is not zero then it overrides the default
- // signature algorithm.
- func signingParamsForPublicKey(pub interface{}, requestedSigAlgo SignatureAlgorithm) (hashFunc crypto.Hash, sigAlgo pkix.AlgorithmIdentifier, err error) {
- var pubType PublicKeyAlgorithm
- switch pub := pub.(type) {
- case *rsa.PublicKey:
- pubType = RSA
- hashFunc = crypto.SHA256
- sigAlgo.Algorithm = oidSignatureSHA256WithRSA
- sigAlgo.Parameters = asn1.NullRawValue
- case *ecdsa.PublicKey:
- pubType = ECDSA
- switch pub.Curve {
- case elliptic.P224(), elliptic.P256():
- hashFunc = crypto.SHA256
- sigAlgo.Algorithm = oidSignatureECDSAWithSHA256
- case elliptic.P384():
- hashFunc = crypto.SHA384
- sigAlgo.Algorithm = oidSignatureECDSAWithSHA384
- case elliptic.P521():
- hashFunc = crypto.SHA512
- sigAlgo.Algorithm = oidSignatureECDSAWithSHA512
- default:
- err = errors.New("x509: unknown elliptic curve")
- }
- case ed25519.PublicKey:
- pubType = Ed25519
- sigAlgo.Algorithm = oidSignatureEd25519
- default:
- err = errors.New("x509: only RSA, ECDSA and Ed25519 keys supported")
- }
- if err != nil {
- return
- }
- if requestedSigAlgo == 0 {
- return
- }
- found := false
- for _, details := range signatureAlgorithmDetails {
- if details.algo == requestedSigAlgo {
- if details.pubKeyAlgo != pubType {
- err = errors.New("x509: requested SignatureAlgorithm does not match private key type")
- return
- }
- sigAlgo.Algorithm, hashFunc = details.oid, details.hash
- if hashFunc == 0 && pubType != Ed25519 {
- err = errors.New("x509: cannot sign with hash function requested")
- return
- }
- if requestedSigAlgo.isRSAPSS() {
- sigAlgo.Parameters = rsaPSSParameters(hashFunc)
- }
- found = true
- break
- }
- }
- if !found {
- err = errors.New("x509: unknown SignatureAlgorithm")
- }
- return
- }
- // emptyASN1Subject is the ASN.1 DER encoding of an empty Subject, which is
- // just an empty SEQUENCE.
- var emptyASN1Subject = []byte{0x30, 0}
- // CreateCertificate creates a new X.509v3 certificate based on a template.
- // The following members of template are used:
- // - SerialNumber
- // - Subject
- // - NotBefore, NotAfter
- // - SignatureAlgorithm
- // - For extensions:
- // - KeyUsage
- // - ExtKeyUsage, UnknownExtKeyUsage
- // - BasicConstraintsValid, IsCA, MaxPathLen, MaxPathLenZero
- // - SubjectKeyId
- // - AuthorityKeyId
- // - OCSPServer, IssuingCertificateURL
- // - SubjectTimestamps, SubjectCARepositories
- // - DNSNames, EmailAddresses, IPAddresses, URIs
- // - PolicyIdentifiers
- // - ExcludedDNSDomains, ExcludedIPRanges, ExcludedEmailAddresses, ExcludedURIDomains, PermittedDNSDomainsCritical,
- // PermittedDNSDomains, PermittedIPRanges, PermittedEmailAddresses, PermittedURIDomains
- // - CRLDistributionPoints
- // - RawSCT, SCTList
- // - ExtraExtensions
- //
- // The certificate is signed by parent. If parent is equal to template then the
- // certificate is self-signed. The parameter pub is the public key of the
- // signee and priv is the private key of the signer.
- //
- // The returned slice is the certificate in DER encoding.
- //
- // The currently supported key types are *rsa.PublicKey, *ecdsa.PublicKey and
- // ed25519.PublicKey. pub must be a supported key type, and priv must be a
- // crypto.Signer with a supported public key.
- //
- // The AuthorityKeyId will be taken from the SubjectKeyId of parent, if any,
- // unless the resulting certificate is self-signed. Otherwise the value from
- // template will be used.
- func CreateCertificate(rand io.Reader, template, parent *Certificate, pub, priv interface{}) (cert []byte, err error) {
- key, ok := priv.(crypto.Signer)
- if !ok {
- return nil, errors.New("x509: certificate private key does not implement crypto.Signer")
- }
- if template.SerialNumber == nil {
- return nil, errors.New("x509: no SerialNumber given")
- }
- hashFunc, signatureAlgorithm, err := signingParamsForPublicKey(key.Public(), template.SignatureAlgorithm)
- if err != nil {
- return nil, err
- }
- publicKeyBytes, publicKeyAlgorithm, err := marshalPublicKey(pub)
- if err != nil {
- return nil, err
- }
- asn1Issuer, err := subjectBytes(parent)
- if err != nil {
- return
- }
- asn1Subject, err := subjectBytes(template)
- if err != nil {
- return
- }
- authorityKeyId := template.AuthorityKeyId
- if !bytes.Equal(asn1Issuer, asn1Subject) && len(parent.SubjectKeyId) > 0 {
- authorityKeyId = parent.SubjectKeyId
- }
- extensions, err := buildExtensions(template, bytes.Equal(asn1Subject, emptyASN1Subject), authorityKeyId)
- if err != nil {
- return
- }
- encodedPublicKey := asn1.BitString{BitLength: len(publicKeyBytes) * 8, Bytes: publicKeyBytes}
- c := tbsCertificate{
- Version: 2,
- SerialNumber: template.SerialNumber,
- SignatureAlgorithm: signatureAlgorithm,
- Issuer: asn1.RawValue{FullBytes: asn1Issuer},
- Validity: validity{template.NotBefore.UTC(), template.NotAfter.UTC()},
- Subject: asn1.RawValue{FullBytes: asn1Subject},
- PublicKey: publicKeyInfo{nil, publicKeyAlgorithm, encodedPublicKey},
- Extensions: extensions,
- }
- tbsCertContents, err := asn1.Marshal(c)
- if err != nil {
- return
- }
- c.Raw = tbsCertContents
- signed := tbsCertContents
- if hashFunc != 0 {
- h := hashFunc.New()
- h.Write(signed)
- signed = h.Sum(nil)
- }
- var signerOpts crypto.SignerOpts = hashFunc
- if template.SignatureAlgorithm != 0 && template.SignatureAlgorithm.isRSAPSS() {
- signerOpts = &rsa.PSSOptions{
- SaltLength: rsa.PSSSaltLengthEqualsHash,
- Hash: hashFunc,
- }
- }
- var signature []byte
- signature, err = key.Sign(rand, signed, signerOpts)
- if err != nil {
- return
- }
- return asn1.Marshal(certificate{
- nil,
- c,
- signatureAlgorithm,
- asn1.BitString{Bytes: signature, BitLength: len(signature) * 8},
- })
- }
- // pemCRLPrefix is the magic string that indicates that we have a PEM encoded
- // CRL.
- var pemCRLPrefix = []byte("-----BEGIN X509 CRL")
- // pemType is the type of a PEM encoded CRL.
- var pemType = "X509 CRL"
- // ParseCRL parses a CRL from the given bytes. It's often the case that PEM
- // encoded CRLs will appear where they should be DER encoded, so this function
- // will transparently handle PEM encoding as long as there isn't any leading
- // garbage.
- func ParseCRL(crlBytes []byte) (*pkix.CertificateList, error) {
- if bytes.HasPrefix(crlBytes, pemCRLPrefix) {
- block, _ := pem.Decode(crlBytes)
- if block != nil && block.Type == pemType {
- crlBytes = block.Bytes
- }
- }
- return ParseDERCRL(crlBytes)
- }
- // ParseDERCRL parses a DER encoded CRL from the given bytes.
- func ParseDERCRL(derBytes []byte) (*pkix.CertificateList, error) {
- certList := new(pkix.CertificateList)
- if rest, err := asn1.Unmarshal(derBytes, certList); err != nil {
- return nil, err
- } else if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after CRL")
- }
- return certList, nil
- }
- // CreateCRL returns a DER encoded CRL, signed by this Certificate, that
- // contains the given list of revoked certificates.
- func (c *Certificate) CreateCRL(rand io.Reader, priv interface{}, revokedCerts []pkix.RevokedCertificate, now, expiry time.Time) (crlBytes []byte, err error) {
- key, ok := priv.(crypto.Signer)
- if !ok {
- return nil, errors.New("x509: certificate private key does not implement crypto.Signer")
- }
- hashFunc, signatureAlgorithm, err := signingParamsForPublicKey(key.Public(), 0)
- if err != nil {
- return nil, err
- }
- // Force revocation times to UTC per RFC 5280.
- revokedCertsUTC := make([]pkix.RevokedCertificate, len(revokedCerts))
- for i, rc := range revokedCerts {
- rc.RevocationTime = rc.RevocationTime.UTC()
- revokedCertsUTC[i] = rc
- }
- tbsCertList := pkix.TBSCertificateList{
- Version: 1,
- Signature: signatureAlgorithm,
- Issuer: c.Subject.ToRDNSequence(),
- ThisUpdate: now.UTC(),
- NextUpdate: expiry.UTC(),
- RevokedCertificates: revokedCertsUTC,
- }
- // Authority Key Id
- if len(c.SubjectKeyId) > 0 {
- var aki pkix.Extension
- aki.Id = OIDExtensionAuthorityKeyId
- aki.Value, err = asn1.Marshal(authKeyId{Id: c.SubjectKeyId})
- if err != nil {
- return
- }
- tbsCertList.Extensions = append(tbsCertList.Extensions, aki)
- }
- tbsCertListContents, err := asn1.Marshal(tbsCertList)
- if err != nil {
- return
- }
- signed := tbsCertListContents
- if hashFunc != 0 {
- h := hashFunc.New()
- h.Write(signed)
- signed = h.Sum(nil)
- }
- var signature []byte
- signature, err = key.Sign(rand, signed, hashFunc)
- if err != nil {
- return
- }
- return asn1.Marshal(pkix.CertificateList{
- TBSCertList: tbsCertList,
- SignatureAlgorithm: signatureAlgorithm,
- SignatureValue: asn1.BitString{Bytes: signature, BitLength: len(signature) * 8},
- })
- }
- // CertificateRequest represents a PKCS #10, certificate signature request.
- type CertificateRequest struct {
- Raw []byte // Complete ASN.1 DER content (CSR, signature algorithm and signature).
- RawTBSCertificateRequest []byte // Certificate request info part of raw ASN.1 DER content.
- RawSubjectPublicKeyInfo []byte // DER encoded SubjectPublicKeyInfo.
- RawSubject []byte // DER encoded Subject.
- Version int
- Signature []byte
- SignatureAlgorithm SignatureAlgorithm
- PublicKeyAlgorithm PublicKeyAlgorithm
- PublicKey interface{}
- Subject pkix.Name
- // Attributes contains the CSR attributes that can parse as
- // pkix.AttributeTypeAndValueSET.
- //
- // Deprecated: Use Extensions and ExtraExtensions instead for parsing and
- // generating the requestedExtensions attribute.
- Attributes []pkix.AttributeTypeAndValueSET
- // Extensions contains all requested extensions, in raw form. When parsing
- // CSRs, this can be used to extract extensions that are not parsed by this
- // package.
- Extensions []pkix.Extension
- // ExtraExtensions contains extensions to be copied, raw, into any CSR
- // marshaled by CreateCertificateRequest. Values override any extensions
- // that would otherwise be produced based on the other fields but are
- // overridden by any extensions specified in Attributes.
- //
- // The ExtraExtensions field is not populated by ParseCertificateRequest,
- // see Extensions instead.
- ExtraExtensions []pkix.Extension
- // Subject Alternate Name values.
- DNSNames []string
- EmailAddresses []string
- IPAddresses []net.IP
- URIs []*url.URL
- }
- // These structures reflect the ASN.1 structure of X.509 certificate
- // signature requests (see RFC 2986):
- type tbsCertificateRequest struct {
- Raw asn1.RawContent
- Version int
- Subject asn1.RawValue
- PublicKey publicKeyInfo
- RawAttributes []asn1.RawValue `asn1:"tag:0"`
- }
- type certificateRequest struct {
- Raw asn1.RawContent
- TBSCSR tbsCertificateRequest
- SignatureAlgorithm pkix.AlgorithmIdentifier
- SignatureValue asn1.BitString
- }
- // oidExtensionRequest is a PKCS#9 OBJECT IDENTIFIER that indicates requested
- // extensions in a CSR.
- var oidExtensionRequest = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 14}
- // newRawAttributes converts AttributeTypeAndValueSETs from a template
- // CertificateRequest's Attributes into tbsCertificateRequest RawAttributes.
- func newRawAttributes(attributes []pkix.AttributeTypeAndValueSET) ([]asn1.RawValue, error) {
- var rawAttributes []asn1.RawValue
- b, err := asn1.Marshal(attributes)
- if err != nil {
- return nil, err
- }
- rest, err := asn1.Unmarshal(b, &rawAttributes)
- if err != nil {
- return nil, err
- }
- if len(rest) != 0 {
- return nil, errors.New("x509: failed to unmarshal raw CSR Attributes")
- }
- return rawAttributes, nil
- }
- // parseRawAttributes Unmarshals RawAttributes into AttributeTypeAndValueSETs.
- func parseRawAttributes(rawAttributes []asn1.RawValue) []pkix.AttributeTypeAndValueSET {
- var attributes []pkix.AttributeTypeAndValueSET
- for _, rawAttr := range rawAttributes {
- var attr pkix.AttributeTypeAndValueSET
- rest, err := asn1.Unmarshal(rawAttr.FullBytes, &attr)
- // Ignore attributes that don't parse into pkix.AttributeTypeAndValueSET
- // (i.e.: challengePassword or unstructuredName).
- if err == nil && len(rest) == 0 {
- attributes = append(attributes, attr)
- }
- }
- return attributes
- }
- // parseCSRExtensions parses the attributes from a CSR and extracts any
- // requested extensions.
- func parseCSRExtensions(rawAttributes []asn1.RawValue) ([]pkix.Extension, error) {
- // pkcs10Attribute reflects the Attribute structure from RFC 2986, Section 4.1.
- type pkcs10Attribute struct {
- Id asn1.ObjectIdentifier
- Values []asn1.RawValue `asn1:"set"`
- }
- var ret []pkix.Extension
- for _, rawAttr := range rawAttributes {
- var attr pkcs10Attribute
- if rest, err := asn1.Unmarshal(rawAttr.FullBytes, &attr); err != nil || len(rest) != 0 || len(attr.Values) == 0 {
- // Ignore attributes that don't parse.
- continue
- }
- if !attr.Id.Equal(oidExtensionRequest) {
- continue
- }
- var extensions []pkix.Extension
- if _, err := asn1.Unmarshal(attr.Values[0].FullBytes, &extensions); err != nil {
- return nil, err
- }
- ret = append(ret, extensions...)
- }
- return ret, nil
- }
- // CreateCertificateRequest creates a new certificate request based on a
- // template. The following members of template are used:
- //
- // - SignatureAlgorithm
- // - Subject
- // - DNSNames
- // - EmailAddresses
- // - IPAddresses
- // - URIs
- // - ExtraExtensions
- // - Attributes (deprecated)
- //
- // priv is the private key to sign the CSR with, and the corresponding public
- // key will be included in the CSR. It must implement crypto.Signer and its
- // Public() method must return a *rsa.PublicKey or a *ecdsa.PublicKey or a
- // ed25519.PublicKey. (A *rsa.PrivateKey, *ecdsa.PrivateKey or
- // ed25519.PrivateKey satisfies this.)
- //
- // The returned slice is the certificate request in DER encoding.
- func CreateCertificateRequest(rand io.Reader, template *CertificateRequest, priv interface{}) (csr []byte, err error) {
- key, ok := priv.(crypto.Signer)
- if !ok {
- return nil, errors.New("x509: certificate private key does not implement crypto.Signer")
- }
- var hashFunc crypto.Hash
- var sigAlgo pkix.AlgorithmIdentifier
- hashFunc, sigAlgo, err = signingParamsForPublicKey(key.Public(), template.SignatureAlgorithm)
- if err != nil {
- return nil, err
- }
- var publicKeyBytes []byte
- var publicKeyAlgorithm pkix.AlgorithmIdentifier
- publicKeyBytes, publicKeyAlgorithm, err = marshalPublicKey(key.Public())
- if err != nil {
- return nil, err
- }
- var extensions []pkix.Extension
- if (len(template.DNSNames) > 0 || len(template.EmailAddresses) > 0 || len(template.IPAddresses) > 0 || len(template.URIs) > 0) &&
- !oidInExtensions(OIDExtensionSubjectAltName, template.ExtraExtensions) {
- sanBytes, err := marshalSANs(template.DNSNames, template.EmailAddresses, template.IPAddresses, template.URIs)
- if err != nil {
- return nil, err
- }
- extensions = append(extensions, pkix.Extension{
- Id: OIDExtensionSubjectAltName,
- Value: sanBytes,
- })
- }
- extensions = append(extensions, template.ExtraExtensions...)
- // Make a copy of template.Attributes because we may alter it below.
- attributes := make([]pkix.AttributeTypeAndValueSET, 0, len(template.Attributes))
- for _, attr := range template.Attributes {
- values := make([][]pkix.AttributeTypeAndValue, len(attr.Value))
- copy(values, attr.Value)
- attributes = append(attributes, pkix.AttributeTypeAndValueSET{
- Type: attr.Type,
- Value: values,
- })
- }
- extensionsAppended := false
- if len(extensions) > 0 {
- // Append the extensions to an existing attribute if possible.
- for _, atvSet := range attributes {
- if !atvSet.Type.Equal(oidExtensionRequest) || len(atvSet.Value) == 0 {
- continue
- }
- // specifiedExtensions contains all the extensions that we
- // found specified via template.Attributes.
- specifiedExtensions := make(map[string]bool)
- for _, atvs := range atvSet.Value {
- for _, atv := range atvs {
- specifiedExtensions[atv.Type.String()] = true
- }
- }
- newValue := make([]pkix.AttributeTypeAndValue, 0, len(atvSet.Value[0])+len(extensions))
- newValue = append(newValue, atvSet.Value[0]...)
- for _, e := range extensions {
- if specifiedExtensions[e.Id.String()] {
- // Attributes already contained a value for
- // this extension and it takes priority.
- continue
- }
- newValue = append(newValue, pkix.AttributeTypeAndValue{
- // There is no place for the critical
- // flag in an AttributeTypeAndValue.
- Type: e.Id,
- Value: e.Value,
- })
- }
- atvSet.Value[0] = newValue
- extensionsAppended = true
- break
- }
- }
- rawAttributes, err := newRawAttributes(attributes)
- if err != nil {
- return
- }
- // If not included in attributes, add a new attribute for the
- // extensions.
- if len(extensions) > 0 && !extensionsAppended {
- attr := struct {
- Type asn1.ObjectIdentifier
- Value [][]pkix.Extension `asn1:"set"`
- }{
- Type: oidExtensionRequest,
- Value: [][]pkix.Extension{extensions},
- }
- b, err := asn1.Marshal(attr)
- if err != nil {
- return nil, errors.New("x509: failed to serialise extensions attribute: " + err.Error())
- }
- var rawValue asn1.RawValue
- if _, err := asn1.Unmarshal(b, &rawValue); err != nil {
- return nil, err
- }
- rawAttributes = append(rawAttributes, rawValue)
- }
- asn1Subject := template.RawSubject
- if len(asn1Subject) == 0 {
- asn1Subject, err = asn1.Marshal(template.Subject.ToRDNSequence())
- if err != nil {
- return nil, err
- }
- }
- tbsCSR := tbsCertificateRequest{
- Version: 0, // PKCS #10, RFC 2986
- Subject: asn1.RawValue{FullBytes: asn1Subject},
- PublicKey: publicKeyInfo{
- Algorithm: publicKeyAlgorithm,
- PublicKey: asn1.BitString{
- Bytes: publicKeyBytes,
- BitLength: len(publicKeyBytes) * 8,
- },
- },
- RawAttributes: rawAttributes,
- }
- tbsCSRContents, err := asn1.Marshal(tbsCSR)
- if err != nil {
- return
- }
- tbsCSR.Raw = tbsCSRContents
- signed := tbsCSRContents
- if hashFunc != 0 {
- h := hashFunc.New()
- h.Write(signed)
- signed = h.Sum(nil)
- }
- var signature []byte
- signature, err = key.Sign(rand, signed, hashFunc)
- if err != nil {
- return
- }
- return asn1.Marshal(certificateRequest{
- TBSCSR: tbsCSR,
- SignatureAlgorithm: sigAlgo,
- SignatureValue: asn1.BitString{
- Bytes: signature,
- BitLength: len(signature) * 8,
- },
- })
- }
- // ParseCertificateRequest parses a single certificate request from the
- // given ASN.1 DER data.
- func ParseCertificateRequest(asn1Data []byte) (*CertificateRequest, error) {
- var csr certificateRequest
- rest, err := asn1.Unmarshal(asn1Data, &csr)
- if err != nil {
- return nil, err
- } else if len(rest) != 0 {
- return nil, asn1.SyntaxError{Msg: "trailing data"}
- }
- return parseCertificateRequest(&csr)
- }
- func parseCertificateRequest(in *certificateRequest) (*CertificateRequest, error) {
- out := &CertificateRequest{
- Raw: in.Raw,
- RawTBSCertificateRequest: in.TBSCSR.Raw,
- RawSubjectPublicKeyInfo: in.TBSCSR.PublicKey.Raw,
- RawSubject: in.TBSCSR.Subject.FullBytes,
- Signature: in.SignatureValue.RightAlign(),
- SignatureAlgorithm: SignatureAlgorithmFromAI(in.SignatureAlgorithm),
- PublicKeyAlgorithm: getPublicKeyAlgorithmFromOID(in.TBSCSR.PublicKey.Algorithm.Algorithm),
- Version: in.TBSCSR.Version,
- Attributes: parseRawAttributes(in.TBSCSR.RawAttributes),
- }
- var err error
- var nfe NonFatalErrors
- out.PublicKey, err = parsePublicKey(out.PublicKeyAlgorithm, &in.TBSCSR.PublicKey, &nfe)
- if err != nil {
- return nil, err
- }
- // Treat non-fatal errors as fatal here.
- if len(nfe.Errors) > 0 {
- return nil, nfe.Errors[0]
- }
- var subject pkix.RDNSequence
- if rest, err := asn1.Unmarshal(in.TBSCSR.Subject.FullBytes, &subject); err != nil {
- return nil, err
- } else if len(rest) != 0 {
- return nil, errors.New("x509: trailing data after X.509 Subject")
- }
- out.Subject.FillFromRDNSequence(&subject)
- if out.Extensions, err = parseCSRExtensions(in.TBSCSR.RawAttributes); err != nil {
- return nil, err
- }
- for _, extension := range out.Extensions {
- if extension.Id.Equal(OIDExtensionSubjectAltName) {
- out.DNSNames, out.EmailAddresses, out.IPAddresses, out.URIs, err = parseSANExtension(extension.Value, &nfe)
- if err != nil {
- return nil, err
- }
- }
- }
- return out, nil
- }
- // CheckSignature reports whether the signature on c is valid.
- func (c *CertificateRequest) CheckSignature() error {
- return checkSignature(c.SignatureAlgorithm, c.RawTBSCertificateRequest, c.Signature, c.PublicKey)
- }
|